public function place_shipment_request($order_id)
 {
     $order = new WC_Order($order_id);
     $codamount = array('Value' => 0, 'CurrencyCode' => 'INR');
     $cashadditionalamount = null;
     $cashadditionaldescription = null;
     $items = $order->get_items();
     $itemarr = array();
     foreach ($items as $item) {
         $itemarr[] = $item['name'];
     }
     if ($order->payment_method == 'cod') {
         $codamount['Value'] = $order->order_total;
         $cashadditionalamount = $order->order_total;
         $cashadditionaldescription = "Cash to be picked up on delivery";
     }
     if (isset($order->shipping_address_1) && !empty($order->shipping_address_1)) {
         $consignee_address = array('Line1' => $order->shipping_address_1, 'Line2' => $order->shipping_address_2, 'Line3' => '', 'City' => $order->shipping_city, 'StateOrProvinceCode' => $order->shipping_state, 'PostCode' => $order->shipping_postcode, 'CountryCode' => $order->shipping_country);
         $consignee_contact = array('Department' => '', 'PersonName' => $order->shipping_first_name . " " . $order->shipping_last_name, 'Title' => '', 'CompanyName' => $order->shipping_first_name . " " . $order->shipping_last_name, 'PhoneNumber1' => $order->shipping_phone, 'PhoneNumber1Ext' => '', 'PhoneNumber2' => '', 'PhoneNumber2Ext' => '', 'FaxNumber' => '', 'CellPhone' => $order->shipping_phone, 'EmailAddress' => $order->billing_email, 'Type' => '');
     } else {
         $consignee_address = array('Line1' => $order->billing_address_1, 'Line2' => $order->billing_address_2, 'Line3' => '', 'City' => $order->billing_city, 'StateOrProvinceCode' => $order->billing_state, 'PostCode' => $order->billing_postcode, 'CountryCode' => $order->billing_country);
         $consignee_contact = array('Department' => '', 'PersonName' => $order->billing_first_name . " " . $order->billing_last_name, 'Title' => '', 'CompanyName' => $order->billing_first_name . " " . $order->billing_last_name, 'PhoneNumber1' => $order->billing_phone, 'PhoneNumber1Ext' => '', 'PhoneNumber2' => '', 'PhoneNumber2Ext' => '', 'FaxNumber' => '', 'CellPhone' => $order->billing_phone, 'EmailAddress' => $order->billing_email, 'Type' => '');
     }
     $soapClient = new SoapClient(__DIR__ . '/Shipping.wsdl');
     $params = array('Shipments' => array('Shipment' => array('Shipper' => array('Reference1' => $order_id, 'Reference2' => '', 'AccountNumber' => $this->account_num, 'PartyAddress' => array('Line1' => $this->address_1, 'Line2' => $this->address_2, 'Line3' => $this->address_3, 'City' => $this->city, 'StateOrProvinceCode' => $this->state, 'PostCode' => $this->postcode, 'CountryCode' => $this->country), 'Contact' => array('Department' => $this->dept, 'PersonName' => $this->contact_name, 'Title' => $this->contact_title, 'CompanyName' => $this->company_name, 'PhoneNumber1' => $this->phonenumber1, 'PhoneNumber1Ext' => $this->phonenumber1ext, 'PhoneNumber2' => $this->phonenumber2, 'PhoneNumber2Ext' => $this->phonenumber2ext, 'FaxNumber' => $this->faxnumber, 'CellPhone' => $this->cellphone, 'EmailAddress' => $this->emailaddress, 'Type' => '')), 'Consignee' => array('Reference1' => $order_id, 'Reference2' => '', 'AccountNumber' => '', 'PartyAddress' => $consignee_address, 'Contact' => $consignee_contact), 'ThirdParty' => array('Reference1' => '', 'Reference2' => '', 'AccountNumber' => '', 'PartyAddress' => array('Line1' => '', 'Line2' => '', 'Line3' => '', 'City' => '', 'StateOrProvinceCode' => '', 'PostCode' => '', 'CountryCode' => ''), 'Contact' => array('Department' => '', 'PersonName' => '', 'Title' => '', 'CompanyName' => '', 'PhoneNumber1' => '', 'PhoneNumber1Ext' => '', 'PhoneNumber2' => '', 'PhoneNumber2Ext' => '', 'FaxNumber' => '', 'CellPhone' => '', 'EmailAddress' => '', 'Type' => '')), 'Reference1' => $order_id, 'Reference2' => '', 'Reference3' => '', 'ForeignHAWB' => '', 'TransportType' => 0, 'ShippingDateTime' => time(), 'DueDate' => time(), 'PickupLocation' => 'Reception', 'PickupGUID' => '', 'Comments' => '', 'AccountingInstrcutions' => '', 'OperationsInstructions' => '', 'Details' => array('Dimensions' => array('Length' => 0, 'Width' => 0, 'Height' => 0, 'Unit' => 'cm'), 'ActualWeight' => array('Value' => 0.5, 'Unit' => 'Kg'), 'ChargeableWeight' => array('Value' => 0.5, 'Unit' => 'Kg'), 'ProductGroup' => 'DOM', 'ProductType' => 'CDA', 'PaymentType' => 'P', 'PaymentOptions' => '', 'Services' => 'CODS', 'NumberOfPieces' => $order->get_item_count(), 'DescriptionOfGoods' => 'Docs', 'GoodsOriginCountry' => 'IN', 'CashOnDeliveryAmount' => $codamount, 'InsuranceAmount' => NULL, 'CollectAmount' => NULL, 'CashAdditionalAmount' => NULL, 'CashAdditionalAmountDescription' => NULL, 'CustomsValueAmount' => Null, 'Items' => array()))), 'ClientInfo' => array('AccountCountryCode' => $this->country, 'AccountEntity' => $this->entity, 'AccountNumber' => $this->account_num, 'AccountPin' => $this->pin, 'UserName' => $this->username, 'Password' => $this->password, 'Version' => 'v1.0'), 'Transaction' => array('Reference1' => $order_id, 'Reference2' => '', 'Reference3' => '', 'Reference4' => '', 'Reference5' => ''), 'LabelInfo' => array('ReportID' => 9729, 'ReportType' => 'URL'));
     $params['Shipments']['Shipment']['Details']['Items'][] = array('PackageType' => 'Box', 'Quantity' => 1, 'Weight' => array('Value' => 0.5, 'Unit' => 'Kg'), 'Comments' => 'Docs', 'Reference' => '');
     try {
         update_post_meta($order_id, 'aramex_createshipment_request', json_encode($params));
         $auth_call = $soapClient->CreateShipments($params);
         update_post_meta($order_id, 'aramex_createshipment_response', json_encode($auth_call));
         if ($auth_call->Notifications->HasErrors == 1 || $auth_call->HasErrors == 1) {
             //Shipping call failed
             $msg = "Aramex Create Shipment Request Failed due to the following error(s):<br>";
             foreach ($auth_call->Notifications as $notification) {
                 $msg .= "Error " . $notification->Code . ": " . $notification->Message . "<br>";
             }
             $order->add_order_note($msg);
             if ($this->verbose_reporting == true) {
                 wp_mail(get_bloginfo('admin_email'), 'Create Shipment request failed. Order ID:' . $order_id, $msg);
             }
         } else {
             //print_r($auth_call);
             //Shipping call was successfull
             $shipment_id = $auth_call->Shipments->ProcessedShipment->ID;
             $sipping_label = $auth_call->Shipments->ShipmentLabel->LabelURL;
             update_post_meta($order_id, 'shipment_id', $shipment_id);
             $order->add_order_note("Aramex Shipment Request Successfull, ID:" . $shipment_id);
             $this->get_shipment_label($order_id, $shipment_id);
             $this->make_pickup_request($order_id);
         }
     } catch (SoapFault $fault) {
         //Aramex API Failed
         $order->add_order_note($fault->faultstring);
         $message = "The system was unable to place a shipment and pick up request for Oder ID" . $order_id . "/r/n The error we received from Aramex is as follows:/r/n" . $fault->faultstring . "/r/n";
         wp_mail(get_bloginfo('admin_email'), 'Shipping request failed. Order ID:' . $order_id, $message);
     }
 }
Example #2
0
 public function postAction()
 {
     $baseUrl = Mage::helper('aramexshipment')->getWsdlPath();
     //SOAP object
     $soapClient = new SoapClient($baseUrl . 'shipping.wsdl');
     $aramex_errors = false;
     $post = $this->getRequest()->getPost();
     $flag = true;
     $error = "";
     try {
         if (empty($post)) {
             Mage::throwException($this->__('Invalid form data.'));
         }
         /* here's your form processing */
         $order = Mage::getModel('sales/order')->loadByIncrementId($post['aramex_shipment_original_reference']);
         $payment = $order->getPayment();
         $totalWeight = 0;
         $totalItems = 0;
         $items = $order->getAllItems();
         $aramex_items_counter = 0;
         foreach ($post['aramex_items'] as $key => $value) {
             $aramex_items_counter++;
             if ($value != 0) {
                 //itrating order items
                 foreach ($items as $item) {
                     if ($item->getId() == $key) {
                         //get weight
                         if ($item->getWeight() != 0) {
                             $weight = $item->getWeight() * $item->getQtyOrdered();
                         } else {
                             $weight = 0.5 * $item->getQtyOrdered();
                         }
                         // collect items for aramex
                         $aramex_items[] = array('PackageType' => 'Box', 'Quantity' => $post[$item->getId()], 'Weight' => array('Value' => $weight, 'Unit' => 'Kg'), 'Comments' => $item->getName(), 'Reference' => '');
                         $totalWeight += $weight;
                         $totalItems += $post[$item->getId()];
                     }
                 }
             }
         }
         $aramex_atachments = array();
         //attachment
         for ($i = 1; $i <= 3; $i++) {
             $fileName = $_FILES['file' . $i]['name'];
             if (isset($fileName) != '') {
                 $fileName = explode('.', $fileName);
                 $fileName = $fileName[0];
                 //filename without extension
                 $fileData = '';
                 if ($_FILES['file' . $i]['tmp_name'] != '') {
                     $fileData = file_get_contents($_FILES['file' . $i]['tmp_name']);
                 }
                 //$fileData = base64_encode($fileData); //base64binary encode
                 $ext = pathinfo($_FILES['file' . $i]['name'], PATHINFO_EXTENSION);
                 //file extension
                 if ($fileName && $ext && $fileData) {
                     $aramex_atachments[] = array('FileName' => $fileName, 'FileExtension' => $ext, 'FileContents' => $fileData);
                 }
             }
         }
         $totalWeight = $post['order_weight'];
         $params = array();
         //shipper parameters
         $params['Shipper'] = array('Reference1' => $post['aramex_shipment_shipper_reference'], 'Reference2' => '', 'AccountNumber' => $post['aramex_shipment_info_billing_account'] == 1 ? $post['aramex_shipment_shipper_account'] : $post['aramex_shipment_shipper_account'], 'PartyAddress' => array('Line1' => addslashes($post['aramex_shipment_shipper_street']), 'Line2' => '', 'Line3' => '', 'City' => $post['aramex_shipment_shipper_city'], 'StateOrProvinceCode' => $post['aramex_shipment_shipper_state'], 'PostCode' => $post['aramex_shipment_shipper_postal'], 'CountryCode' => $post['aramex_shipment_shipper_country']), 'Contact' => array('Department' => '', 'PersonName' => $post['aramex_shipment_shipper_name'], 'Title' => '', 'CompanyName' => $post['aramex_shipment_shipper_company'], 'PhoneNumber1' => $post['aramex_shipment_shipper_phone'], 'PhoneNumber1Ext' => '', 'PhoneNumber2' => '', 'PhoneNumber2Ext' => '', 'FaxNumber' => '', 'CellPhone' => $post['aramex_shipment_shipper_phone'], 'EmailAddress' => $post['aramex_shipment_shipper_email'], 'Type' => ''));
         //consinee parameters
         $params['Consignee'] = array('Reference1' => $post['aramex_shipment_receiver_reference'], 'Reference2' => '', 'AccountNumber' => $post['aramex_shipment_info_billing_account'] == 2 ? $post['aramex_shipment_shipper_account'] : '', 'PartyAddress' => array('Line1' => $post['aramex_shipment_receiver_street'], 'Line2' => '', 'Line3' => '', 'City' => $post['aramex_shipment_receiver_city'], 'StateOrProvinceCode' => '', 'PostCode' => $post['aramex_shipment_receiver_postal'], 'CountryCode' => $post['aramex_shipment_receiver_country']), 'Contact' => array('Department' => '', 'PersonName' => $post['aramex_shipment_receiver_name'], 'Title' => '', 'CompanyName' => $post['aramex_shipment_receiver_company'], 'PhoneNumber1' => $post['aramex_shipment_receiver_phone'], 'PhoneNumber1Ext' => '', 'PhoneNumber2' => '', 'PhoneNumber2Ext' => '', 'FaxNumber' => '', 'CellPhone' => $post['aramex_shipment_receiver_phone'], 'EmailAddress' => $post['aramex_shipment_receiver_email'], 'Type' => ''));
         //new
         if ($post['aramex_shipment_info_billing_account'] == 3) {
             $params['ThirdParty'] = array('Reference1' => $post['aramex_shipment_shipper_reference'], 'Reference2' => '', 'AccountNumber' => $post['aramex_shipment_shipper_account'], 'PartyAddress' => array('Line1' => addslashes(Mage::getStoreConfig('aramexsettings/shipperdetail/address')), 'Line2' => '', 'Line3' => '', 'City' => Mage::getStoreConfig('aramexsettings/shipperdetail/city'), 'StateOrProvinceCode' => Mage::getStoreConfig('aramexsettings/shipperdetail/state'), 'PostCode' => Mage::getStoreConfig('aramexsettings/shipperdetail/postalcode'), 'CountryCode' => Mage::getStoreConfig('aramexsettings/shipperdetail/country')), 'Contact' => array('Department' => '', 'PersonName' => Mage::getStoreConfig('aramexsettings/shipperdetail/name'), 'Title' => '', 'CompanyName' => Mage::getStoreConfig('aramexsettings/shipperdetail/company'), 'PhoneNumber1' => Mage::getStoreConfig('aramexsettings/shipperdetail/phone'), 'PhoneNumber1Ext' => '', 'PhoneNumber2' => '', 'PhoneNumber2Ext' => '', 'FaxNumber' => '', 'CellPhone' => Mage::getStoreConfig('aramexsettings/shipperdetail/phone'), 'EmailAddress' => Mage::getStoreConfig('aramexsettings/shipperdetail/email'), 'Type' => ''));
         }
         // Other Main Shipment Parameters
         $params['Reference1'] = $post['aramex_shipment_info_reference'];
         //'Shpt0001';
         $params['Reference2'] = '';
         $params['Reference3'] = '';
         $params['ForeignHAWB'] = $post['aramex_shipment_info_foreignhawb'];
         $params['TransportType'] = 0;
         $params['ShippingDateTime'] = time();
         //date('m/d/Y g:i:sA');
         $params['DueDate'] = time() + 7 * 24 * 60 * 60;
         //date('m/d/Y g:i:sA');
         $params['PickupLocation'] = 'Reception';
         $params['PickupGUID'] = '';
         $params['Comments'] = $post['aramex_shipment_info_comment'];
         $params['AccountingInstrcutions'] = '';
         $params['OperationsInstructions'] = '';
         $params['Details'] = array('Dimensions' => array('Length' => '0', 'Width' => '0', 'Height' => '0', 'Unit' => 'cm'), 'ActualWeight' => array('Value' => $totalWeight, 'Unit' => $post['weight_unit']), 'ProductGroup' => $post['aramex_shipment_info_product_group'], 'ProductType' => $post['aramex_shipment_info_product_type'], 'PaymentType' => $post['aramex_shipment_info_payment_type'], 'PaymentOptions' => $post['aramex_shipment_info_payment_option'], 'Services' => $post['aramex_shipment_info_service_type'], 'NumberOfPieces' => $totalItems, 'DescriptionOfGoods' => $post['aramex_shipment_description'], 'GoodsOriginCountry' => $post['aramex_shipment_shipper_country'], 'Items' => $aramex_items);
         if (count($aramex_atachments)) {
             $params['Attachments'] = $aramex_atachments;
         }
         $params['Details']['CashOnDeliveryAmount'] = array('Value' => $post['aramex_shipment_info_cod_amount'], 'CurrencyCode' => $post['aramex_shipment_currency_code']);
         $params['Details']['CustomsValueAmount'] = array('Value' => $post['aramex_shipment_info_custom_amount'], 'CurrencyCode' => $post['aramex_shipment_currency_code']);
         $major_par['Shipments'][] = $params;
         $clientInfo = Mage::helper('aramexshipment')->getClientInfo();
         $major_par['ClientInfo'] = $clientInfo;
         $report_id = (int) Mage::getStoreConfig('aramexsettings/config/report_id');
         if (!$report_id) {
             $report_id = 9729;
         }
         $major_par['LabelInfo'] = array('ReportID' => $report_id, 'ReportType' => 'URL');
         $formSession = Mage::getSingleton('adminhtml/session');
         $formSession->setData("form_data", $post);
         try {
             //create shipment call
             $auth_call = $soapClient->CreateShipments($major_par);
             if ($auth_call->HasErrors) {
                 if (empty($auth_call->Shipments)) {
                     if (count($auth_call->Notifications->Notification) > 1) {
                         foreach ($auth_call->Notifications->Notification as $notify_error) {
                             Mage::throwException($this->__('Aramex: ' . $notify_error->Code . ' - ' . $notify_error->Message));
                         }
                     } else {
                         Mage::throwException($this->__('Aramex: ' . $auth_call->Notifications->Notification->Code . ' - ' . $auth_call->Notifications->Notification->Message));
                     }
                 } else {
                     if (count($auth_call->Shipments->ProcessedShipment->Notifications->Notification) > 1) {
                         $notification_string = '';
                         foreach ($auth_call->Shipments->ProcessedShipment->Notifications->Notification as $notification_error) {
                             $notification_string .= $notification_error->Code . ' - ' . $notification_error->Message . ' <br />';
                         }
                         Mage::throwException($notification_string);
                     } else {
                         Mage::throwException($this->__('Aramex: ' . $auth_call->Shipments->ProcessedShipment->Notifications->Notification->Code . ' - ' . $auth_call->Shipments->ProcessedShipment->Notifications->Notification->Message));
                     }
                 }
             } else {
                 if ($order->canShip() && $post['aramex_return_shipment_creation_date'] == "create") {
                     $shipmentid = Mage::getModel('sales/order_shipment_api')->create($order->getIncrementId(), $post['aramex_items'], "AWB No. " . $auth_call->Shipments->ProcessedShipment->ID . " - Order No. " . $auth_call->Shipments->ProcessedShipment->Reference1 . " - <a href='javascript:void(0);' onclick='myObj.printLabel();'>Print Label</a>");
                     $ship = true;
                     $ship = Mage::getModel('sales/order_shipment_api')->addTrack($shipmentid, 'aramex', 'Aramex', $auth_call->Shipments->ProcessedShipment->ID);
                     /* sending mail */
                     if ($ship) {
                         /* if($post['aramex_email_customer'] == 'yes'){ */
                         /* send shipment mail */
                         $storeId = $order->getStore()->getId();
                         $copyTo = Mage::helper('aramex_core')->getEmails(self::XML_PATH_SHIPMENT_EMAIL_COPY_TO, $storeId);
                         $copyMethod = Mage::getStoreConfig(self::XML_PATH_SHIPMENT_EMAIL_COPY_METHOD, $storeId);
                         $templateId = Mage::getStoreConfig(self::XML_PATH_SHIPMENT_EMAIL_TEMPLATE, $storeId);
                         if ($order->getCustomerIsGuest()) {
                             $customerName = $order->getBillingAddress()->getName();
                         } else {
                             $customerName = $order->getCustomerName();
                         }
                         $shipments_id = $auth_call->Shipments->ProcessedShipment->ID;
                         $mailer = Mage::getModel('core/email_template_mailer');
                         $emailInfo = Mage::getModel('core/email_info');
                         $emailInfo->addTo($order->getCustomerEmail(), $customerName);
                         if ($copyTo && $copyMethod == 'bcc') {
                             /* Add bcc to customer email  */
                             foreach ($copyTo as $email) {
                                 $emailInfo->addBcc($email);
                             }
                         }
                         $mailer->addEmailInfo($emailInfo);
                         /* Email copies are sent as separated emails if their copy method is 'copy' */
                         if ($copyTo && $copyMethod == 'copy') {
                             foreach ($copyTo as $email) {
                                 $emailInfo = Mage::getModel('core/email_info');
                                 $emailInfo->addTo($email);
                                 $mailer->addEmailInfo($emailInfo);
                             }
                         }
                         $senderName = Mage::getStoreConfig(self::XML_PATH_TRANS_IDENTITY_NAME, $storeId);
                         $senderEmail = Mage::getStoreConfig(self::XML_PATH_TRANS_IDENTITY_EMAIL, $storeId);
                         /* Set all required params and send emails */
                         $mailer->setSender(array('name' => $senderName, 'email' => $senderEmail));
                         $mailer->setStoreId($storeId);
                         $mailer->setTemplateId($templateId);
                         $mailer->setTemplateParams(array('order' => $order, 'shipments_id' => $shipments_id));
                         try {
                             $mailer->send();
                         } catch (Exception $ex) {
                             Mage::getSingleton('core/session')->addError('Unable to send email.');
                         }
                         /* } */
                     }
                     Mage::getSingleton('core/session')->addSuccess('Aramex Shipment Number: ' . $auth_call->Shipments->ProcessedShipment->ID . ' has been created.');
                     /* $order->setState('warehouse_pickup_shipped', true); */
                 } elseif ($post['aramex_return_shipment_creation_date'] == "return") {
                     $message = "Aramex Shipment Return Order AWB No. " . $auth_call->Shipments->ProcessedShipment->ID . " - Order No. " . $auth_call->Shipments->ProcessedShipment->Reference1 . " - <a href='javascript:void(0);' onclick='myObj.printLabel();'>Print Label</a>";
                     Mage::getSingleton('core/session')->addSuccess('Aramex Shipment Return Order Number: ' . $auth_call->Shipments->ProcessedShipment->ID . ' has been created.');
                     $order->addStatusToHistory($order->getStatus(), $message, false);
                     $order->save();
                 } else {
                     Mage::throwException($this->__('Cannot do shipment for the order.'));
                 }
             }
         } catch (Exception $e) {
             $aramex_errors = true;
             Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
         }
         if ($aramex_errors) {
             $strip = strstr($post['aramex_shipment_referer'], "aramexpopup", true);
             $url = $strip;
             if (empty($strip)) {
                 $url = $post['aramex_shipment_referer'];
             }
             $this->_redirectUrl($url . 'aramexpopup/show');
         } else {
             $this->_redirectUrl($post['aramex_shipment_referer']);
         }
     } catch (Exception $e) {
         Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
     }
 }