public function make_pickup_request($order_id)
 {
     $order = new WC_Order($order_id);
     $soapClient = new SoapClient(__DIR__ . '/Shipping.wsdl');
     date_default_timezone_set('Asia/Calcutta');
     $time = current_time('H', true);
     $day = current_time('N');
     //If greater than 3:00 PM
     if ($time >= 15) {
         $offset = " + 2 days";
         $order->add_order_note('Order placed after 3:00 PM cut off time');
     } else {
         $offset = ' + 1 days';
         $order->add_order_note('Order placed before 3:00 PM cut off time');
     }
     switch ($day) {
         case '5':
             $offset = ' + 3 days';
             break;
         case '6':
             $offset = ' + 2 days';
             break;
         case '7':
             $offset = ' + 2 days';
             break;
         default:
             break;
     }
     $format = 'Y-m-d\\TH:i:s';
     $pickupdate = date($format, strtotime(date("Y-m-d H:i:s", mktime(11, 30, 0)) . $offset));
     $readytime = date($format, strtotime(date("Y-m-d H:i:s", mktime(12, 30, 0)) . $offset));
     $lastpickuptime = strtotime(date($format, strtotime(date("Y-m-d H:i:s", mktime(17, 30, 0)) . $offset)));
     $closingtime = strtotime(date($format, strtotime(date("Y-m-d H:i:s", mktime(19, 00, 0)) . $offset)));
     $shippingdatetime = $pickupdate;
     $order->add_order_note("Pick up request time:" . date("Y-m-d H:i:s", strtotime($pickupdate)));
     $params = array('Pickup' => array('PickupAddress' => 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), 'PickupContact' => 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' => ''), 'PickupLocation' => 'Reception', 'PickupDate' => $pickupdate, 'ReadyTime' => $readytime, 'LastPickupTime' => $lastpickuptime, 'ClosingTime' => $closingtime, 'ShippingDateTime' => $shippingdatetime, 'Comments' => '', 'Reference1' => $order_id, 'Reference2' => '', 'Vehicle' => '', 'Status' => 'Ready', 'PickupItems' => array('PickupItemDetail' => array('ProductGroup' => 'DOM', 'ProductType' => 'ONP', 'Payment' => 'P', 'NumberOfShipments' => 1, 'PackageType' => '', 'NumberOfPieces' => $order->get_item_count(), 'Comments' => '', 'ShipmentWeight' => array('Value' => 0.5, 'Unit' => 'Kg'), 'ShipmentVolume' => array('Value' => 0.5, 'Unit' => 'Kg'), 'CashAmount' => array('Value' => 0, 'CurrencyCode' => ''), 'ExtraCharges' => array('Value' => 0, 'CurrencyCode' => ''), 'ShipmentDimensions' => array('Length' => 0, 'Width' => 0, 'Height' => 0, 'Unit' => 'cm')))), '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' => Null);
     update_post_meta($order_id, 'aramex_pickup_request', json_encode($params));
     try {
         $auth_call = $soapClient->CreatePickup($params);
         update_post_meta($order_id, 'aramex_pickup_response', json_encode($auth_call));
         if (empty($auth_call->HasErrors) || $auth_call->HasErrors == 0) {
             $pickup_id = $auth_call->ProcessedPickup->ID;
             $pickup_guid = $auth_call->ProcessedPickup->GUID;
             $order->add_order_note("Aramex Pickup Request Successful <br> \n                    Pickup Request ID:" . $pickup_id . "<br>Pickup Request GUID:" . $pickup_guid);
             update_post_meta($order_id, 'pickup_id', $pickup_id);
             update_post_meta($order_id, 'pickup_guid', $pickup_guid);
         } else {
             $msg = "Aramex Pickup 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'), 'Pick up request failed. Order ID:' . $order_id, $msg);
             }
         }
     } catch (SoapFault $fault) {
         $order->add_order_note("Failed creating Aramex pickup request. Error:" . $fault->faultstring);
         $message = "The system was unable to create an Aramex pickup request for Order ID" . $order_id . "/r/n The error we received from Aramex is as follows:/r/n" . $fault->faultstring . "/r/n";
         if ($this->verbose_reporting == true) {
             wp_mail(get_bloginfo('admin_email'), 'Pick up request failed. Order ID:' . $order_id, $message);
         }
     }
 }
 public function postAction()
 {
     $account = Mage::getStoreConfig('aramexsettings/settings/account_number');
     $country_code = Mage::getStoreConfig('aramexsettings/settings/account_country_code');
     $post = $this->getRequest()->getPost();
     $country = Mage::getModel('directory/country')->loadByCode($country_code);
     $response = array();
     $clientInfo = Mage::helper('aramexshipment')->getClientInfo();
     try {
         if (empty($post)) {
             $response['type'] = 'error';
             $response['error'] = $this->__('Invalid form data.');
             print json_encode($response);
             die;
         }
         $post = $post['pickup'];
         $_order = Mage::getModel('sales/order')->load($post['order_id']);
         $pickupDate = strtotime($post['date']);
         $readyTimeH = $post['ready_hour'];
         $readyTimeM = $post['ready_minute'];
         $readyTime = mktime($readyTimeH - 2, $readyTimeM, 0, date("m", $pickupDate), date("d", $pickupDate), date("Y", $pickupDate));
         $closingTimeH = $post['latest_hour'];
         $closingTimeM = $post['latest_minute'];
         $closingTime = mktime($closingTimeH - 2, $closingTimeM, 0, date("m", $pickupDate), date("d", $pickupDate), date("Y", $pickupDate));
         $params = array('ClientInfo' => $clientInfo, 'Transaction' => array('Reference1' => $post['reference']), 'Pickup' => array('PickupContact' => array('PersonName' => html_entity_decode($post['contact']), 'CompanyName' => html_entity_decode($post['company']), 'PhoneNumber1' => html_entity_decode($post['phone']), 'PhoneNumber1Ext' => html_entity_decode($post['ext']), 'CellPhone' => html_entity_decode($post['mobile']), 'EmailAddress' => html_entity_decode($post['email'])), 'PickupAddress' => array('Line1' => html_entity_decode($post['address']), 'City' => html_entity_decode($post['city']), 'StateOrProvinceCode' => html_entity_decode($post['state']), 'PostCode' => html_entity_decode($post['zip']), 'CountryCode' => $post['country']), 'PickupLocation' => html_entity_decode($post['location']), 'PickupDate' => $readyTime, 'ReadyTime' => $readyTime, 'LastPickupTime' => $closingTime, 'ClosingTime' => $closingTime, 'Comments' => html_entity_decode($post['comments']), 'Reference1' => html_entity_decode($post['reference']), 'Reference2' => '', 'Vehicle' => $post['vehicle'], 'Shipments' => array('Shipment' => array()), 'PickupItems' => array('PickupItemDetail' => array('ProductGroup' => $post['product_group'], 'ProductType' => $post['product_type'], 'Payment' => $post['payment_type'], 'NumberOfShipments' => $post['no_shipments'], 'NumberOfPieces' => $post['no_pieces'], 'ShipmentWeight' => array('Value' => $post['text_weight'], 'Unit' => $post['weight_unit']))), 'Status' => $post['status']));
         $baseUrl = Mage::helper('aramexshipment')->getWsdlPath();
         $soapClient = new SoapClient($baseUrl . 'shipping.wsdl');
         try {
             $results = $soapClient->CreatePickup($params);
             if ($results->HasErrors) {
                 if (count($results->Notifications->Notification) > 1) {
                     $error = "";
                     foreach ($results->Notifications->Notification as $notify_error) {
                         $error .= $this->__('Aramex: ' . $notify_error->Code . ' - ' . $notify_error->Message) . "<br>";
                     }
                     $response['error'] = $error;
                 } else {
                     $response['error'] = $this->__('Aramex: ' . $results->Notifications->Notification->Code . ' - ' . $results->Notifications->Notification->Message);
                 }
                 $response['type'] = 'error';
             } else {
                 $notify = false;
                 $visible = false;
                 $comment = "Pickup reference number ( <strong>" . $results->ProcessedPickup->ID . "</strong> ).";
                 $_order->addStatusHistoryComment($comment, $_order->getStatus())->setIsVisibleOnFront($visible)->setIsCustomerNotified($notify);
                 $_order->save();
                 $shipmentId = null;
                 $shipment = Mage::getModel('sales/order_shipment')->getCollection()->addFieldToFilter("order_id", $_order->getId())->load();
                 if ($shipment->count() > 0) {
                     foreach ($shipment as $_shipment) {
                         $shipmentId = $_shipment->getId();
                         break;
                     }
                 }
                 if ($shipmentId != null) {
                     $shipment = Mage::getModel('sales/order_shipment')->load($shipmentId);
                     $shipment->addComment($comment, false, false);
                     $shipment->save();
                 }
                 $response['type'] = 'success';
                 $amount = "<p class='amount'>Pickup reference number ( <strong>" . $results->ProcessedPickup->ID . "</strong> ).</p>";
                 $response['html'] = $amount;
             }
         } catch (Exception $e) {
             $response['type'] = 'error';
             $response['error'] = $e->getMessage();
         }
     } catch (Exception $e) {
         $response['type'] = 'error';
         $response['error'] = $e->getMessage();
     }
     print json_encode($response);
     die;
 }