private static function submitOrder($order)
 {
     // $helper->log($order);
     // collect shipping info
     $helper = Mage::helper('localexpress_shipping');
     $status = $order->getStatus();
     $shipping_method = $order->getShippingMethod();
     $shipping_service_id = $helper->getShippingMethodId($shipping_method);
     // shippment allowed?
     if ($helper->canShipOrder($order)) {
         // get order totals
         // $order_info = Localexpress_Shipping_Helper_Data::getOrderTotals($order);
         $order_info = array();
         $order_info["qty"] = ceil($order->total_qty_ordered);
         $order_info["weight"] = ceil($order->weight);
         $order_info["price"] = (double) $order->grand_total;
         $currency = Mage::app()->getStore()->getCurrentCurrencyCode();
         $shipping_address = $order->getShippingAddress();
         $firstname = $shipping_address['firstname'];
         $lastname = $shipping_address['lastname'];
         $street = $shipping_address['street'];
         $city = $shipping_address['city'];
         $zip = $shipping_address['postcode'];
         $country_iso2 = $shipping_address['country_id'];
         $o_country = Mage::getModel('directory/country')->loadByCode($country_iso2);
         $country_name = $o_country->getName();
         // prepare shipment
         $dim = Localexpress_Shipping_Helper_Data::buildDimensions(1, 1, 1);
         $addr_origin = Localexpress_Shipping_Helper_Data::getAddressOrigin();
         $addr_dest = Localexpress_Shipping_Helper_Data::buildAddress($country_iso2, $country_name, $street, $zip, $firstname . " " . $lastname, $city);
         $comment = "Creating shipment";
         $insurance = false;
         $tmpShipment = Mage::getModel('sales/service_order', $order);
         // create shipment
         $shipment_resp = $helper->shipment($order_info["qty"], $order_info["weight"], $order_info["price"], $currency, $dim, $addr_origin, $addr_dest, $comment, $insurance, $tmpShipment->getData('number'));
         $shipment_resp = $shipment_resp["body"]->shipment;
         // create magento shipment
         $track_data = array('carrier_code' => $order->shipping_method, 'title' => $order->shipping_description, 'number' => $shipment_resp->humanId, 'description' => $shipment_resp->humanId, 'qty' => ceil($shipment_resp->quantity), 'weight' => ceil($shipment_resp->weight));
         $pre_shipment = Localexpress_Shipping_Helper_Data::getItemQtys($order);
         $shipment = Mage::getModel('sales/service_order', $order)->prepareShipment($pre_shipment);
         $track = Mage::getModel('sales/order_shipment_track')->addData($track_data);
         $shipment->addTrack($track);
         $shipment->register();
         $order->setIsInProcess(true);
         $transactionSave = Mage::getModel('core/resource_transaction')->addObject($shipment)->addObject($order)->save();
         $emailSentStatus = $shipment->getData('email_sent');
         if (!is_null($customerEmailComments) && !$emailSentStatus) {
             $shipment->sendEmail(true, $customerEmailComments);
             $shipment->setEmailSent(true);
         }
     }
 }
 public function collectRates(Mage_Shipping_Model_Rate_Request $request)
 {
     try {
         $helper = Mage::helper('localexpress_shipping');
         $fields = $helper->getFields();
         // skip if not processable
         if (!$helper->isProcessable()) {
             return false;
         }
         // return
         $result = Mage::getModel('shipping/rate_result');
         $human_id = Mage::getSingleton('checkout/session')->getData('localexpress_shipping_human_id');
         // collect order information
         $quote = Mage::getSingleton('checkout/session')->getQuote();
         $firstname = $quote->getShippingAddress()->getFirstname();
         $lastname = $quote->getShippingAddress()->getLastname();
         $address = $request->getDestStreet();
         $city = $request->getDestCity();
         $zip = $request->getDestPostcode();
         $country_iso639_2 = $request->getDestCountryId();
         $country = Mage::getModel('directory/country')->loadByCode($country_iso639_2);
         $country_name = $country->getName();
         $store_currency = Mage::app()->getStore()->getCurrentCurrencyCode();
         // get order totals
         $order_info = Localexpress_Shipping_Helper_Data::getOrderTotals($request, Localexpress_Shipping_Helper_Data::catageryOnly(), Localexpress_Shipping_Helper_Data::attributesOnly());
         if ($order_info['notPossible']) {
             return false;
         }
         // addresses
         $addr_origin = Localexpress_Shipping_Helper_Data::getAddressOrigin();
         $addr_dest = Localexpress_Shipping_Helper_Data::buildAddress($country_iso639_2, $country_name, $address, $zip, $firstname . " " . $lastname, $city);
         //TODO: demension attribute!!
         $dimensions = Localexpress_Shipping_Helper_Data::buildDimensions(1, 1, 1);
         // request quote
         $available = $helper->shipmentAvailable($country_iso639_2, $address, $zip);
         if (!$available) {
             return false;
         }
         $quote = $helper->shipmentQuote($order_info["qty"], $order_info["weight"], $order_info["price"], $store_currency, $dimensions, $addr_origin, $addr_dest, "Quote request!", false, $human_id);
         // invalid server response
         if ($quote["info"]["http_code"] != 201 && $quote["info"]["http_code"] != 200) {
             return false;
         }
         // list details
         $human_id = Mage::getSingleton('checkout/session')->setData('localexpress_shipping_human_id', $quote['body']->shipment_quote->human_id);
         $carrier_name = $fields["title"];
         $service_name = $fields["service_name"];
         $service_id = "1";
         $service_price = $quote["body"]->shipment_quote->price;
         // build response
         $method = Mage::getModel('shipping/rate_result_method');
         // carrier
         $method->setCarrier(Localexpress_Shipping_Helper_Data::NAME);
         $method->setCarrierTitle($fields['title'] . " (" . $carrier_name . ")");
         // methods
         $method->setMethod(Localexpress_Shipping_Helper_Data::SERVICE_NAME . '_' . $service_id);
         $method->setMethodTitle($service_name);
         $method->setPrice($service_price);
         $method->setCost($service_price);
         $result->append($method);
         return $result;
     } catch (\Exception $e) {
         Localexpress_Shipping_Helper_Data::log($e->getMessage());
         Localexpress_Shipping_Helper_Data::log($e->getTraceAsString());
         // halt order checkout on error
         die;
         return false;
     }
 }