<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
function wpcsr_get_origin( $shipment_id ){
	$location 	= wpcsr_get_location_origin( $shipment_id );
	$postcode 	= get_post_meta( $shipment_id, 'wpcsr_origin_postcode', true );
	return $postcode ? $location.' - '.$postcode : $location ;
}
function wpcsr_get_destination( $shipment_id ){
	$location 	= wpcsr_get_location_destination( $shipment_id );
	$postcode 	= get_post_meta( $shipment_id, 'wpcsr_destination_postcode', true );
	return $postcode ? $location.' - '.$postcode : $location ;
}
function wpcsr_declared_weight( $shipment_id ){
	$packages 		= maybe_unserialize( get_post_meta( $shipment_id, 'wpc-multiple-package', true ) );
	$dec_weight		= get_post_meta( $shipment_id, '__wpcsr_package_weight', true );
	$package_info 	= wpcsr_calculate_package_information( $packages );
	return $dec_weight ? $dec_weight : $package_info['sugg_weight'] ;
}
function wpcsr_calculate_package_information( $packages ){
	$metric_factor 	= wpcsr_metric_factor();
	$qty 			= 0;
	$volume 		= 0;
	$total_weight	= 0;
	$package_weight = 0;
	if( !empty( $packages ) ){
		foreach ( $packages  as $package ) {
			$qty_default 	= array_key_exists( 'wpc-pm-qty', $package ) && !empty( $package['wpc-pm-qty'] ) ? floatval( $package['wpc-pm-qty'] ) : 1;
			$weight 		= array_key_exists( 'wpc-pm-weight', $package ) && !empty( $package['wpc-pm-weight'] ) ? floatval( $package['wpc-pm-weight'] ) : 0 ;	
			$length 		= array_key_exists( 'wpc-pm-length', $package ) && !empty( $package['wpc-pm-length'] ) ? floatval( $package['wpc-pm-length'] ) : 0 ;	
			$width 			= array_key_exists( 'wpc-pm-width', $package ) && !empty( $package['wpc-pm-width'] ) ? floatval( $package['wpc-pm-width'] ) : 0 ;	
			$height 		= array_key_exists( 'wpc-pm-height', $package ) && !empty( $package['wpc-pm-height'] ) ? floatval( $package['wpc-pm-height'] ) : 0 ;	

			$qty 			+= $qty_default;
			$volume 		+= $qty_default * ( ( $length * $width * $height) / $metric_factor );
			$total_weight 	+= $qty_default * $weight;			
		}
		$package_weight 	= ( $total_weight > $volume ) ? $total_weight : $volume ;
	}
	$package_information = array(
		'packages' 		=> $packages,
		'qty' 			=> $qty,
		'volume' 		=> $volume,
		'total_weight' 	=> $total_weight,
		'sugg_weight' 	=> $package_weight
	);
	return apply_filters( 'wpcsr_calculate_package_information', $package_information );
}
function wpcsr_get_shipment_rate( $rate_id, $package_weight, $protection, $shipment_id = false ){
	global $wpcargo;
	$rateInfo   = get_wpcsr_rate_by_id( $rate_id );
	if(!$rateInfo){
		return false;
	}
	
    // Calculate Flat Rate
    /**
     * $rateInfo->rate_type
     * 1 = Cost per weight  
     * Formula: {weight_form_submitted} x {Flat rate/ Cost Per Weight}
     **/
    $cost = (float)$rateInfo->price;
    if(  $rateInfo->weight_cost && (float)$rateInfo->weight_cost > 0 ){
        $cost += ( ( $package_weight - (float)$rateInfo->min_weight ) * (float)$rateInfo->weight_cost );
    }

    // Calculate Cost per weight 
    // Override the Cost form Flat rate calculation
	/**
	 * 0 = Flat Rate
     * Formula: {Flat rate/ Cost Per Weight} + ( {weight_form_submitted} - {Min Weight} ) x {Exceeding Cost After Min Weight}
     **/
	if( $rateInfo->rate_type == 1){
		$cost = (float)$package_weight * (float)$rateInfo->price;
	}

    // Add TAX to Cost
    if( abs($wpcargo->tax) > 0 ){
        $wpcsr_tax  =  ( $cost * ( $wpcargo->tax / 100 ) ) + $cost;
        $cost       += apply_filters( 'wpcsr_calculated_tax', $wpcsr_tax );
    }
	// Calculate the Rate price 
	if( (int)$protection == 1 ){  
        // Check if TAX is enable 
        $cost = (float)$rateInfo->rate_protection + $cost;
	}
	return apply_filters( 'wpcsr_shipment_cost', $cost, $rate_id, $package_weight, $protection, $shipment_id );
}