Skip to content

Instantly share code, notes, and snippets.

@imanilchaudhari
Created January 21, 2019 13:07
Show Gist options
  • Save imanilchaudhari/f1638f19e4bc9f77974f68c7c5731c2a to your computer and use it in GitHub Desktop.
Save imanilchaudhari/f1638f19e4bc9f77974f68c7c5731c2a to your computer and use it in GitHub Desktop.
<?php
foreach($shipments as $shipment){
$box = Boxes::model()->getBoxFromShipmentId($shipment->shipment_id);
$box_item = BoxItems::model()->findByAttributes(array('box_id' => $box->box_id));
$shipment_data = array();
$s_address = Address::model()->getAddress($shipment->shipper_id); //shipper address
$shipment_data['Shipper'] = array(
'Contact' => array(
'PersonName' => $s_address->first_name.' '.$s_address->last_name,
'CompanyName' => $s_address->first_name.' '.$s_address->last_name,
'PhoneNumber' => $s_address->mobile),
'Address' => array(
'StreetLines' => array($s_address->street1),
'City' => $s_address->city,
'StateOrProvinceCode' => $s_address->state,
'PostalCode' => $s_address->zipcode,
'CountryCode' => $s_address->country_code)
);
$r_address = Address::model()->getAddress($shipment->consignee_id); //recipient address
$shipment_data['Recipient'] = array(
'Contact' => array(
'PersonName' => $r_address->first_name.' '.$r_address->last_name,
'CompanyName' => $r_address->first_name.' '.$r_address->last_name,
'PhoneNumber' => $r_address->mobile),
'Address' => array(
'StreetLines' => array($r_address->street1),
'City' => $r_address->city,
'StateOrProvinceCode' => $r_address->state,
'PostalCode' => $r_address->zipcode,
'CountryCode' => $r_address->country_code,
)
);
$shipment_data['Recipient']['Contact']['PhoneNumber'] = preg_replace("/[^0-9\/]/","",$shipment_data['Recipient']['Contact']['PhoneNumber']);
if(strlen($shipment_data['Recipient']['Contact']['PhoneNumber']) > 15) $shipment_data['Recipient']['Contact']['PhoneNumber'] = substr($shipment_data['Recipient']['Contact']['PhoneNumber'], 0, 15);
/*
* CUSTOMER_REFERENCE : shipment id | logistic shipment id
* INVOICE_NUMBER : box id
* P_O_NUMBER : BOX ID
*/
$shipment_data['CustomerReferences'] = array(
'0' => array(
'CustomerReferenceType' => 'CUSTOMER_REFERENCE',
'Value' => 'GR'.$shipment->shipment_id
), // valid values CUSTOMER_REFERENCE, INVOICE_NUMBER, P_O_NUMBER and SHIPMENT_INTEGRITY
'1' => array(
'CustomerReferenceType' => 'INVOICE_NUMBER',
'Value' => 'INV'.$shipment->shipment_id
),
'2' => array(
'CustomerReferenceType' => 'P_O_NUMBER',
'Value' => 'PO'.$shipment->order_id
)
);
$shipment_data['Weight'] = array(
'Value' => (float)number_format($box->weight * self::LB_TO_KG,2),
'Units' => self::FEDEX_WT_UNIT
);
$shipment_data['service_type'] = ($shipment->cod == 1)?COD:NON_COD;
$total_amount = $box_item->sales_price * $box_item->qty;
$shipment_data['CodDetail']['CodCollectionAmount']= array('Currency' => 'INR', 'Amount' => $total_amount);
$shipment_data['CustomsClearanceDetail'] = array(
'DutiesPayment' => array(
'PaymentType' => 'SENDER',
'Payor' => array(
'ResponsibleParty' => array(
'AccountNumber' => Yii::app()->fedex->getProperty('shipaccount'),
'Contact' => array('Title' => 'Fedex'),
'Address' => array('CountryCode' => 'IN'),
)
)
),
'DocumentContent' => 'DOCUMENTS_ONLY',
'CustomsValue' => array(
'Currency' => 'INR',
'Amount' => $total_amount,
),
'CommercialInvoice' => array(
'Purpose' => 'SOLD'
),
'Commodities' => array(
'Name' => $box_item->description,
'NumberOfPieces' => $box_item->qty,
'Description' => $box_item->description,
'CountryOfManufacture' => 'IN',
'HarmonizedCode' => 1004000010,
'Weight' => array(
'Units' => self::FEDEX_WT_UNIT,
'Value' => (float)number_format($box->weight * self::LB_TO_KG,2),
),
'Quantity' => $box_item->qty,
'QuantityUnits' => 'EA',
'UnitPrice' => array(
'Currency' => 'INR',
'Amount' => $total_amount
),
'CustomsValue' => array(
'Currency' => 'INR',
'Amount' => $total_amount
)
)
);
$tracking = Yii::app()->fedex->createShipment($shipment_data);
if($tracking != false){
$shipment_data = array();
$shipment_data['shipment_id'] = $shipment->shipment_id;
$shipment_data['courier_tracking_id'] = $tracking['tracking_number'];
$response[$shipment->shipment_id] = $tracking['tracking_number'];
Shipment::model()->updateShipment($shipment_data);
//create image
$codlable_file_name = Yii::app()->utility->createFile('ship_codlabel_'.$tracking['tracking_number'].rand(0,9999).'.png',Yii::getPathOfAlias('fedex_path').'/cod_ground_return_label/',$tracking['cod_ground_return_label']);
$shipping_ground_label_file_name = Yii::app()->utility->createFile('shipping_ground_label_'.$tracking['tracking_number'].rand(0,9999).'.png',Yii::getPathOfAlias('fedex_path').'/shipping_ground_label/',$tracking['shipping_ground_label']);
//fedex tracking
$fedex_data = array();
$fedex_data['courier_tracking_id'] = $tracking['tracking_number'];
$fedex_data['form_id'] = $tracking['form_id'];
$fedex_data['destination_location_id'] = $tracking['destination_location_id'];
$fedex_data['shipping_ground_label'] = 'uploads/fedex/shipping_ground_label/'.$shipping_ground_label_file_name;
$fedex_data['cod_ground_return_label'] = 'uploads/fedex/cod_ground_return_label/'.$codlable_file_name;
$fedex_data['ursa_code'] = $tracking['ursa_code'];
$fedex_data['service_area'] = $tracking['service_area'];
$fedex_data['state_code'] = $tracking['state_code'];
$fedex_data['postal_code'] = $tracking['postal_code'];
$fedex_data['country_code'] = $tracking['country_code'];
$fedex_data['airport_id'] = $tracking['airport_id'];
FedexAwb::model()->addFedexAwb($fedex_data);
} else {
$shipment->courier_id = 0;
$shipment->save();
}
}
@roshansinghrajput
Copy link

Hii bro

@imanilchaudhari
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment