Index: branches/5.1.x/units/shipping_quote_engines/usps.php =================================================================== diff -u -r13813 -r13985 --- branches/5.1.x/units/shipping_quote_engines/usps.php (.../usps.php) (revision 13813) +++ branches/5.1.x/units/shipping_quote_engines/usps.php (.../usps.php) (revision 13985) @@ -1,6 +1,6 @@ GetQuote(); if ( !isset($rates['error']) ) { - $this->Application->RemoveVar('usps_errors'); + $this->Application->RemoveVar('sqe_error'); $i = 1; foreach ($rates as $k => $rate ) { @@ -1054,7 +1054,7 @@ } else { // for Front-End (shipping screen) and Admin (Shipping Tab on editing order) - $this->Application->StoreVar('usps_errors', $rates['error']); + $this->Application->StoreVar('sqe_error', $rates['error']); } $this->Application->StoreVar('current_usps_shipping_types', serialize($shipping_types)); @@ -1158,21 +1158,21 @@ return $body; } + /** + * Returns available shipping types + * + * @return Array + * @todo Get possible shipping types based on MODULE_SHIPPING_USPS_TYPES and MODULE_SHIPPING_USPS_TYPES_INTL consntants + */ function GetAvailableTypes() { - return array(); - $conn =& $this->Application->GetADODBConnection(); - $types = $conn->Query('SELECT * FROM '.TABLE_PREFIX.'ShippingType'); - - $ret = array(); - foreach ($types as $a_type) { - $a_type['_ClassName'] = get_class($this); - $a_type['_Id'] = 'CUST_'.$a_type['ShippingID']; - $a_type['_Name'] = '(Custom) '.$a_type['Name']; - $ret[] = $a_type; - } - return $ret; - + return Array ( + Array ( + '_ClassName' => get_class($this), + '_Id' => 'USPS', + '_Name' => 'USPS (Default)' + ) + ); } function LoadParams() @@ -1196,4 +1196,130 @@ { return Array ('AccountLogin'); } + + /** + * Creates new USPS order + * + * @param OrdersItem $object + * @param bool $dry_run + * @return Array + */ + function MakeOrder(&$object, $dry_run = false) + { + $ShippingInfo = unserialize($object->GetDBField('ShippingInfo')); + $ShippingCode = $USPSMethod = ''; + $ShippingCountry = $this->GetUSPSCountry($object->GetDBField('ShippingCountry')); + + $UserName = explode(" ", $object->GetDBField('ShippingTo')); + + $item_table = TABLE_PREFIX.'OrderItems'; + if ($this->Application->isAdminUser) { + // this strange contraption actually uses temp table from object (when in temp mode) + $order_table = $object->TableName; + $item_table = str_replace('Orders', 'OrderItems', $order_table); + } + + $sOrder = Array ( + 'FirstName' => $UserName[0], + 'LastName' => $UserName[1], + 'ShippingCompany' => $object->GetDBField('ShippingCompany'), + 'ShippingAddress1' => $object->GetDBField('ShippingAddress1'), + 'ShippingAddress2' => $object->GetDBField('ShippingAddress2'), + 'ShippingCity' => $object->GetDBField('ShippingCity'), + 'ShippingZip' => $object->GetDBField('ShippingZip'), + 'ShippingCountry' => $ShippingCountry, + 'ShippingPhone' => $this->PhoneClean($object->GetDBField('ShippingPhone')), + 'ShippingFax' => $this->PhoneClean($object->GetDBField('ShippingFax')), + 'ShippingNumBoxes' => '1', + ); + + $sql = 'SELECT SUM(`Quantity` * `Weight`) + FROM ' . $item_table . ' + WHERE ' . $object->IDField . ' = ' . $object->GetID(); + $weight = $this->Application->Conn->GetOne($sql); + + $f_weight = Kg2Pounds($weight); + $sOrder['ShippingWeight'] = $f_weight[0].'.'.$f_weight[1]; + + foreach ($ShippingInfo as $k => $ShippingRow) { + $ShippingCode = $ShippingRow['Code']; + } + + if ( $object->GetDBField('ShippingCountry') == 'USA' ) { + $sOrder['ShippingState'] = $object->GetDBField('ShippingState'); + $USPSMethod = $ShippingCode; + unset($sOrder['ShippingZip']); + + $sOrder['ShippingZip5'] = substr(trim($object->GetDBField('ShippingZip')), 0, 5); + $sOrder['ShippingZip4'] = ''; + $sOrder['SubTotal'] = $object->GetDBField('SubTotal'); + } + else { + $USPSMethod = array_search($ShippingCode, $this->intl_types); + $sOrder['ShippingProvince'] = ''; + + if ( $ShippingCountry == 'CA' ) { + $sOrder['ShippingProvince'] = $object->GetField('ShippingState'); + } + + // add items + $sql = 'SELECT `Quantity`, `Weight`, `Price` + FROM ' . $item_table . ' + WHERE ' . $object->IDField . ' = ' . $object->GetID(); + $order_items = $this->Application->Conn->Query($sql); + + $i = 1; + $Items = Array(); + + foreach ($order_items as $k => $order_item) { + $p_weight = Array(); + $p_weight = Kg2Pounds($order_item['Weight']); + $Items[$i] = Array('Qty' => $order_item['Quantity'], 'Price' => $order_item['Price'], 'NetPounds' => $p_weight[0], 'NetOunces' => $p_weight[1]); + $i++; + } + + $sOrder['Items'] = $Items; + $sOrder['InvoiceNumber'] = $object->GetDBField('OrderNumber'); + } + + $sOrder['ShippingService'] = $USPSMethod; + + // make USPS order + $this->order = $sOrder; + $usps_data = $this->PostOrder(); + + // if errors + if ( array_key_exists('error_number', $usps_data) ) { + return $usps_data; + } + + if ( array_key_exists('Postage', $usps_data) ) { + $ShippingPrice = ''; + $ShippingPrice = $usps_data['Postage']; + } + + $ShippingTracking = ''; + + if ( isset($usps_data['TrackingNumber']) && $usps_data['TrackingNumber'] != '' ) { + $ShippingTracking = $usps_data['TrackingNumber']; + } + + if ( isset($usps_data['PostnetBarCode']) && $usps_data['PostnetBarCode'] != '' && $ShippingTracking == '' ) { + $ShippingTracking = $usps_data['PostnetBarCode']; + } + + if ($dry_run == false) { + $object->SetDBField('ShippingTracking', $ShippingTracking); + $object->Update(); + } + else { + $full_path = USPS_LABEL_FOLDER . $ShippingTracking . ".pdf"; + + if (file_exists($full_path)) { + unlink($full_path); + } + } + + return $usps_data; + } } \ No newline at end of file