示例#1
0
 function buildJournalEntry()
 {
     global $messageStack, $currencies;
     // set some preliminary information
     switch (strtoupper($this->order['function'])) {
         case 'SALESINVOICEENTRY':
             define('JOURNAL_ID', 12);
             define('GL_TYPE', 'sos');
             break;
         case 'SALESORDERENTRY':
         default:
             define('JOURNAL_ID', 10);
             define('GL_TYPE', 'soo');
     }
     if ($this->order['receivables_gl_acct'] != '') {
         // see if requestor specifies a AR account else use default
         define('DEF_GL_ACCT', $this->order['receivables_gl_acct']);
     } else {
         define('DEF_GL_ACCT', AR_DEFAULT_GL_ACCT);
     }
     $account_type = 'c';
     $psOrd = new orders();
     // make the received string look like a form submission then post as usual
     $psOrd->account_type = $account_type;
     $psOrd->id = '';
     // should be null unless opening an existing purchase/receive
     $psOrd->journal_id = JOURNAL_ID;
     $psOrd->post_date = $this->order['post_date'];
     // date format should already be YYYY-MM-DD
     $psOrd->terminal_date = $this->order['post_date'];
     // make same as order date for now
     $psOrd->period = gen_calculate_period($psOrd->post_date);
     $psOrd->store_id = $this->get_account_id($this->order['store_id'], 'b');
     $psOrd->admin_id = $this->get_user_id($this->username);
     $psOrd->description = SOAP_XML_SUBMITTED_SO;
     $psOrd->gl_acct_id = DEF_GL_ACCT;
     $psOrd->freight = $currencies->clean_value(db_prepare_input($this->order['freight_total']), DEFAULT_CURRENCY);
     $psOrd->discount = $currencies->clean_value(db_prepare_input($this->order['discount_total']), DEFAULT_CURRENCY);
     $psOrd->sales_tax = db_prepare_input($this->order['tax_total']);
     $psOrd->total_amount = db_prepare_input($this->order['order_total']);
     // The order ID should be set by the submitter
     $psOrd->purchase_invoice_id = db_prepare_input($this->order['order_id']);
     $psOrd->purch_order_id = db_prepare_input($this->order['purch_order_id']);
     $psOrd->shipper_code = db_prepare_input($this->order['freight_carrier']);
     // BOF - Added by PhreeSoft for Portable Power Systems to map the shipping codes to PhreeBooks Codes
     $psOrd->shipper_code = substr($psOrd->shipper_code, 0, 16);
     switch ($psOrd->shipper_code) {
         default:
         case 'FedEx (Ground):':
             $psOrd->shipper_code = 'fedex_v7:GND';
             break;
         case 'FedEx (Ground LT':
             $psOrd->shipper_code = 'fedex_v7:GndFrt';
             break;
         case 'FedEx (Home Deli':
             $psOrd->shipper_code = 'fedex_v7:GDR';
             break;
         case 'FedEx (Express S':
             $psOrd->shipper_code = 'fedex_v7:3Dpm';
             break;
         case 'FedEx (Express 2':
             $psOrd->shipper_code = 'fedex_v7:2Dpm';
             break;
         case 'FedEx (Standard ':
             $psOrd->shipper_code = 'fedex_v7:1Dpm';
             break;
         case 'FedEx (Priority ':
             $psOrd->shipper_code = 'fedex_v7:1Dam';
             break;
         case 'UPS (Ground):':
         case 'UPS (Ground Resi':
             $psOrd->shipper_code = 'ups:GND';
             break;
         case 'UPS (3 Day Selec':
             $psOrd->shipper_code = 'ups:3Dpm';
             break;
         case 'UPS (2 Day Air):':
             $psOrd->shipper_code = 'ups:2Dpm';
             break;
         case 'UPS (Next Day PM':
             $psOrd->shipper_code = 'ups:1Dpm';
             break;
         case 'UPS (Next Day):':
             $psOrd->shipper_code = 'ups:1Dam';
             break;
         case 'FREE SHIPPING! (':
             $psOrd->shipper_code = 'usps:3Dpm';
             break;
         case 'Flat (Best Way):':
             $psOrd->shipper_code = 'usps:2Dpm';
             break;
     }
     // EOF - Added by PhreeSoft for PPS
     /* Values below are not used at this time
     		$psOrd->sales_tax_auths
     		$psOrd->terms
     		$psOrd->drop_ship = 0;
     		$psOrd->waiting = 0;
     		$psOrd->closed = 0;
     		$psOrd->subtotal
     		*/
     $psOrd->bill_add_update = 1;
     // force an address book update
     // see if the customer record exists
     $psOrd->short_name = db_prepare_input($this->order['customer']['customer_id']);
     $psOrd->ship_short_name = $psOrd->short_name;
     $result = $this->checkForCustomerExists($psOrd);
     if (!$result) {
         return false;
     }
     $psOrd->ship_add_update = $result['ship_add_update'];
     $psOrd->bill_acct_id = $result['bill_acct_id'];
     $psOrd->bill_address_id = $result['bill_address_id'];
     $psOrd->ship_acct_id = $result['ship_acct_id'];
     $psOrd->ship_address_id = $result['ship_address_id'];
     // Phreebooks requires a primary name or the order is not valid, use company name if exists, else contact name
     if ($this->order['billing']['primary_name'] == '') {
         $psOrd->bill_primary_name = $this->order['billing']['contact'];
         $psOrd->bill_contact = '';
     } else {
         $psOrd->bill_primary_name = $this->order['billing']['primary_name'];
         $psOrd->bill_contact = $this->order['billing']['contact'];
     }
     $psOrd->bill_address1 = $this->order['billing']['address1'];
     $psOrd->bill_address2 = $this->order['billing']['address2'];
     $psOrd->bill_city_town = $this->order['billing']['city_town'];
     $psOrd->bill_state_province = $this->order['billing']['state_province'];
     $psOrd->bill_postal_code = $this->order['billing']['postal_code'];
     $psOrd->bill_country_code = gen_get_country_iso_3_from_2($this->order['billing']['country_code']);
     $psOrd->bill_telephone1 = $this->order['customer']['telephone'];
     $psOrd->bill_email = $this->order['customer']['email'];
     if ($this->order['shipping']['primary_name'] == '') {
         $psOrd->ship_primary_name = $this->order['shipping']['contact'];
         $psOrd->ship_contact = '';
     } else {
         $psOrd->ship_primary_name = $this->order['shipping']['primary_name'];
         $psOrd->ship_contact = $this->order['shipping']['contact'];
     }
     $psOrd->ship_address1 = $this->order['shipping']['address1'];
     $psOrd->ship_address2 = $this->order['shipping']['address2'];
     $psOrd->ship_city_town = $this->order['shipping']['city_town'];
     $psOrd->ship_state_province = $this->order['shipping']['state_province'];
     $psOrd->ship_postal_code = $this->order['shipping']['postal_code'];
     $psOrd->ship_country_code = gen_get_country_iso_3_from_2($this->order['shipping']['country_code']);
     $psOrd->ship_telephone1 = $this->order['customer']['telephone'];
     $psOrd->ship_email = $this->order['customer']['email'];
     // check for truncation of addresses
     if (strlen($psOrd->bill_primary_name) > 32 || strlen($psOrd->bill_address1) > 32 || strlen($psOrd->ship_primary_name) > 32 || strlen($psOrd->ship_address1) > 32) {
         $messageStack->add('Either the Primary Name or Address has been truncated to fit in the PhreeBooks database field sizes. Please check source information.', 'caution');
     }
     // load the item rows
     switch (JOURNAL_ID) {
         case 12:
             $index = 'pstd';
             break;
         case 10:
         default:
             $index = 'qty';
             break;
     }
     for ($i = 0; $i < count($this->order['items']); $i++) {
         $psOrd->item_rows[] = array('gl_type' => GL_TYPE, $index => db_prepare_input($this->order['items'][$i]['quantity']), 'sku' => db_prepare_input($this->order['items'][$i]['sku']), 'desc' => db_prepare_input($this->order['items'][$i]['description']), 'price' => db_prepare_input($this->order['items'][$i]['unit_price']), 'acct' => db_prepare_input($this->order['items'][$i]['gl_acct']), 'tax' => db_prepare_input($this->order['items'][$i]['taxable']), 'total' => db_prepare_input($this->order['items'][$i]['total_price']));
     }
     // error check input
     if (!$psOrd->short_name) {
         return $this->responseXML('18', SOAP_NO_CUSTOMER_ID, 'error');
     }
     if (!$psOrd->post_date) {
         return $this->responseXML('20', SOAP_NO_POST_DATE, 'error');
     }
     if (!$psOrd->period) {
         return $this->responseXML('21', SOAP_BAD_POST_DATE, 'error');
     }
     if (!$psOrd->bill_primary_name) {
         return $this->responseXML('30', SOAP_NO_BILLING_PRIMARY_NAME, 'error');
     }
     if (ADDRESS_BOOK_CONTACT_REQUIRED && !$psOrd->bill_contact) {
         return $this->responseXML('31', SOAP_NO_BILLING_CONTACT, 'error');
     }
     if (ADDRESS_BOOK_ADDRESS1_REQUIRED && !$psOrd->bill_address1) {
         return $this->responseXML('32', SOAP_NO_BILLING_ADDRESS1, 'error');
     }
     if (ADDRESS_BOOK_ADDRESS2_REQUIRED && !$psOrd->bill_address2) {
         return $this->responseXML('33', SOAP_NO_BILLING_ADDRESS2, 'error');
     }
     if (ADDRESS_BOOK_CITY_TOWN_REQUIRED && !$psOrd->bill_city_town) {
         return $this->responseXML('34', SOAP_NO_BILLING_CITY_TOWN, 'error');
     }
     if (ADDRESS_BOOK_STATE_PROVINCE_REQUIRED && !$psOrd->bill_state_province) {
         return $this->responseXML('35', SOAP_NO_BILLING_STATE_PROVINCE, 'error');
     }
     if (ADDRESS_BOOK_POSTAL_CODE_REQUIRED && !$psOrd->bill_postal_code) {
         return $this->responseXML('36', SOAP_NO_BILLING_POSTAL_CODE, 'error');
     }
     if (!$psOrd->bill_country_code) {
         return $this->responseXML('37', SOAP_NO_BILLING_COUNTRY_CODE, 'error');
     }
     if (!$psOrd->ship_primary_name) {
         return $this->responseXML('40', SOAP_NO_SHIPPING_PRIMARY_NAME, 'error');
     }
     if (ADDRESS_BOOK_CONTACT_REQUIRED && !$psOrd->ship_contact) {
         return $this->responseXML('41', SOAP_NO_SHIPPING_CONTACT, 'error');
     }
     if (ADDRESS_BOOK_ADDRESS1_REQUIRED && !$psOrd->ship_address1) {
         return $this->responseXML('42', SOAP_NO_SHIPPING_ADDRESS1, 'error');
     }
     if (ADDRESS_BOOK_ADDRESS2_REQUIRED && !$psOrd->ship_address2) {
         return $this->responseXML('43', SOAP_NO_SHIPPING_ADDRESS2, 'error');
     }
     if (ADDRESS_BOOK_CITY_TOWN_REQUIRED && !$psOrd->ship_city_town) {
         return $this->responseXML('44', SOAP_NO_SHIPPING_CITY_TOWN, 'error');
     }
     if (ADDRESS_BOOK_STATE_PROVINCE_REQUIRED && !$psOrd->ship_state_province) {
         return $this->responseXML('45', SOAP_NO_SHIPPING_STATE_PROVINCE, 'error');
     }
     if (ADDRESS_BOOK_POSTAL_CODE_REQUIRED && !$psOrd->ship_postal_code) {
         return $this->responseXML('46', SOAP_NO_SHIPPING_POSTAL_CODE, 'error');
     }
     if (!$psOrd->ship_country_code) {
         return $this->responseXML('47', SOAP_NO_SHIPPING_COUNTRY_CODE, 'error');
     }
     // post the sales order
     //echo 'ready to post =><br />'; echo 'psOrd object = '; print_r($psOrd); echo '<br />';
     $post_success = $psOrd->post_ordr($action);
     if (!$post_success) {
         // extract the error message from the messageStack and return with error
         $text = strip_tags($messageStack->output());
         $text = preg_replace('/&nbsp;/', '', $text);
         // the &nbsp; messes up the response XML
         return $this->responseXML('90', SOAP_SO_POST_ERROR . $text, 'error');
     }
     // Begin - additional operations added by PhreeSoft for PPS
     global $db;
     if ($this->order['payment']['encval']) {
         $sql_array = array('module' => 'contacts', 'ref_1' => $psOrd->bill_acct_id, 'ref_2' => $psOrd->bill_address_id, 'hint' => $this->order['payment']['hint'], 'enc_value' => $this->order['payment']['encval']);
         $result = $db->Execute("select id from " . TABLE_DATA_SECURITY . " \r\n\t\t\t\twhere module = 'contacts' \r\n\t\t\t\tand ref_1 = '" . $psOrd->bill_acct_id . "' \r\n\t\t\t\tand ref_2 = '" . $psOrd->bill_address_id . "' \r\n\t\t\t\tand hint  = '" . $this->order['payment']['hint'] . "'");
         if ($result->RecordCount() > 0) {
             db_perform(TABLE_DATA_SECURITY, $sql_array, 'update', 'id = ' . $result->fields['id']);
         } else {
             db_perform(TABLE_DATA_SECURITY, $sql_array, 'insert');
         }
     }
     // End - additional operations added by PhreeSoft for PPS
     gen_add_audit_log(constant('AUDIT_LOG_SOAP_' . JOURNAL_ID . '_ADDED'), $psOrd->purchase_invoice_id, $psOrd->total_amount);
     $this->responseXML('0', sprintf(constant('SOAP_' . JOURNAL_ID . '_SUCCESS'), $psOrd->purchase_invoice_id), 'success');
     return true;
 }
                 $label_data .= fread($handle, filesize($file_path));
                 fclose($handle);
             }
             if (!$error) {
                 $auto_print = true;
                 $label_data = str_replace("\r", "", addslashes($label_data));
                 // for javascript multi-line
                 $label_data = str_replace("\n", "\\n", $label_data);
             }
         } else {
             // send to viewer window
             gen_redirect(html_href_link(FILENAME_DEFAULT, 'module=shipping&page=popup_label_viewer&method=' . $shipping_module . '&date=' . $sInfo->ship_date . '&labels=' . $tracking_list, 'SSL'));
         }
     } else {
         $messageStack->add(SHIPPING_NO_PACKAGES, 'error');
         $sInfo->ship_country_code = gen_get_country_iso_3_from_2($sInfo->ship_country_code);
     }
     break;
 case 'view':
     $date = $_GET['date'];
     $labels = $_GET['labels'];
     $labels_array = explode(':', $labels);
     if (count($labels_array) == 0) {
         die('No labels were passed to label_viewer.php!');
     }
     // check output method, pdf load window, thermal print from here
     if (MODULE_SHIPPING_UPS_PRINTER_TYPE == 'Thermal') {
         // autoprint the label
         // fetch the tracking labels
         $file_path = SHIPPING_DEFAULT_LABEL_DIR . $shipping_module . '/' . str_replace('-', '/', $date) . '/';
         foreach ($labels_array as $tracking_num) {
示例#3
0
 function processOrders($upload_name = 'file_name')
 {
     global $db, $messageStack;
     $period = gen_calculate_period(date('Y-m-d'));
     if (!defined('JOURNAL_ID')) {
         define('JOURNAL_ID', 12);
     }
     // load the amazon contact record info
     $result = $db->Execute("SELECT id FROM " . TABLE_CONTACTS . " WHERE short_name='" . MODULE_AMAZON_CUSTOMER_ID . "'");
     $cID = $result->fields['id'];
     if (!$cID) {
         $messageStack->add("Contact could not be found in the Customer database. Please make sure the setting in the defaults.php file match your Customers value.", 'error');
         return;
     }
     $result = $db->Execute("SELECT * FROM " . TABLE_ADDRESS_BOOK . " WHERE ref_id={$cID} AND type='cm'");
     $commonMain = array('post_date' => date('Y-m-d'), 'period' => $period, 'journal_id' => JOURNAL_ID, 'currencies_code' => DEFAULT_CURRENCY, 'terminal_date' => date('Y-m-d'), 'store_id' => 0, 'admin_id' => $_SESSION['admin_id'], 'rep_id' => 0, 'gl_acct_id' => MODULE_AMAZON_DEFAULT_RECEIVABLES_GL_ACCT, 'bill_acct_id' => $result->fields['ref_id'], 'bill_address_id' => $result->fields['address_id'], 'bill_primary_name' => $result->fields['primary_name'], 'bill_contact' => $result->fields['contact'], 'bill_address1' => $result->fields['address1'], 'bill_address2' => $result->fields['address2'], 'bill_city_town' => $result->fields['city_town'], 'bill_state_province' => $result->fields['state_province'], 'bill_postal_code' => $result->fields['postal_code'], 'bill_country_code' => $result->fields['country_code'], 'bill_telephone1' => $result->fields['telephone1'], 'bill_email' => $result->fields['email'], 'drop_ship' => '1');
     $bill_acct_id = $result->fields['ref_id'];
     // iterate through the map to set journal post variables, orders may be on more than 1 line
     // ***************************** START TRANSACTION *******************************
     $db->transStart();
     $itemCnt = 1;
     $items = array();
     $totals = array();
     $inStock = true;
     $orderCnt = 0;
     $skip = false;
     $runaway = 0;
     $rows = file($_FILES[$upload_name]['tmp_name']);
     $row = array_shift($rows);
     // heading
     $this->headings = explode("\t", $row);
     $row = array_shift($rows);
     // first order
     if (!$row) {
         $messageStack->add("There were no orders to process!", 'caution');
         return;
     }
     $data = $this->processRow($row);
     while (true) {
         if (!$row) {
             break;
         }
         $main = $commonMain;
         $main['purch_order_id'] = $data['order-id'];
         $main['description'] = "Amazon Order # " . $data['order-id'];
         $main['shipper_code'] = MODULE_AMAZON_DEFAULT_SHIPPING_CARRIER;
         if (strlen($data['recipient-name']) > 32 || strlen($data['ship-address-1']) > 32 || strlen($data['ship-address-2']) > 32) {
             $messageStack->add(sprintf("Order # %s has a name or address that is too long for the PhreeBooks db and has been truncated: %s", $data['order-id'], $data['recipient-name']), 'caution');
         }
         $main['ship_primary_name'] = $data['recipient-name'];
         $main['ship_address1'] = $data['ship-address-1'];
         $main['ship_address2'] = $data['ship-address-2'];
         $main['ship_contact'] = $data['ship-address-3'];
         $main['ship_city_town'] = $data['ship-city'];
         $main['ship_state_province'] = $data['ship-state'];
         $main['ship_postal_code'] = $data['ship-postal-code'];
         $main['ship_country_code'] = gen_get_country_iso_3_from_2($data['ship-country']);
         $main['ship_telephone1'] = $data['buyer-phone-number'];
         $main['ship_email'] = $data['buyer-email'];
         // build the item, check stock if auto_journal
         $inv = $db->Execute("SELECT * FROM " . TABLE_INVENTORY . " WHERE sku='{$data['sku']}'");
         $messageStack->debug("\n Executing sql = " . "SELECT * FROM " . TABLE_INVENTORY . " WHERE sku='{$data['sku']}' resulting in:" . print_r($inv->fields, true));
         if (!$inv->fields || sizeof($inv->fields) == 0) {
             $messageStack->add(sprintf("SKU: %s not found in the database, this import was skipped!", $data['sku']));
             $skip = true;
         } else {
             if ($inv->fields['qty_stock'] < $data['quantity-purchased']) {
                 $inStock = false;
             }
         }
         $items[] = array('item_cnt' => $itemCnt, 'gl_type' => 'sos', 'sku' => $data['sku'], 'qty' => $data['quantity-purchased'], 'description' => $data['product-name'], 'credit_amount' => $data['item-price'], 'gl_account' => $inv->fields['account_sales_income'] ? $inv->fields['account_sales_income'] : MODULE_AMAZON_DEFAULT_SALES_GL_ACCT, 'taxable' => 0, 'full_price' => $inv->fields['full_price'], 'post_date' => substr($data['purchase-date'], 0, 10));
         // preset some totals to keep running balance
         if (!isset($totals['discount'])) {
             $totals['discount'] = 0;
         }
         if (!isset($totals['sales_tax'])) {
             $totals['sales_tax'] = 0;
         }
         if (!isset($totals['total_amount'])) {
             $totals['total_amount'] = 0;
         }
         if (!isset($totals['freight'])) {
             $totals['freight'] = 0;
         }
         // fill in order info
         $totals['discount'] += $data['item-promotion-discount'] + $data['ship-promotion-discount'];
         $totals['sales_tax'] += $data['item-tax'];
         $totals['total_amount'] += $data['item-price'] + $data['item-tax'] + $data['shipping-price'] + $data['shipping-tax'];
         // missing from file: $data['gift-wrap-price'] and $data['gift-wrap-tax']
         $totals['freight'] += $data['shipping-price'];
         // check for continuation order
         $row = array_shift($rows);
         if ($runaway++ > 1000) {
             $messageStack->add("runaway reached, exiting!", 'error');
             break;
         }
         if ($row) {
             // check for continuation order
             $nextData = $this->processRow($row);
             //				$messageStack->debug("\nContinuing order check, Next order = {$nextData['order-id']} and this order = {$main['purch_order_id']}");
             if ($nextData['order-id'] == $main['purch_order_id']) {
                 $data = $nextData;
                 $itemCnt++;
                 continue;
                 // more items for the same order
             }
         }
         // finish main and item to post
         $main['total_amount'] = $totals['total_amount'];
         // @todo add tax, shipping, gift wrap, and notes records (add to item array)
         $items[] = array('qty' => 1, 'gl_type' => 'frt', 'description' => "Shipping Amazon # " . $data['order-id'], 'credit_amount' => $totals['freight'], 'gl_account' => MODULE_AMAZON_DEFAULT_FREIGHT_GL_ACCT, 'taxable' => 0, 'post_date' => substr($data['purchase-date'], 0, 10));
         $items[] = array('qty' => 1, 'gl_type' => 'ttl', 'description' => "Total Amazon # " . $data['order-id'], 'debit_amount' => $totals['total_amount'], 'gl_account' => MODULE_AMAZON_DEFAULT_RECEIVABLES_GL_ACCT, 'post_date' => substr($data['purchase-date'], 0, 10));
         $dup = $db->Execute("SELECT id FROM " . TABLE_JOURNAL_MAIN . " WHERE purch_order_id='{$main['purch_order_id']}'");
         if ($dup->fields['id']) {
             //				$messageStack->debug("duplicate order id = ".$dup->fields['id']." and main = ".print_r($main, true));
             $messageStack->add(sprintf("Order # %s has already been imported! It will be skipped.", $data['order-id']), 'caution');
             continue;
         }
         $ledger = new journal();
         $ledger->post_date = substr($data['purchase-date'], 0, 10);
         $ledger->period = $period;
         $ledger->closed = '0';
         $ledger->journal_id = JOURNAL_ID;
         $ledger->bill_acct_id = $bill_acct_id;
         $ledger->journal_main_array = $main;
         $ledger->journal_rows = $items;
         if (!$skip) {
             if (!$ledger->validate_purchase_invoice_id()) {
                 return false;
             }
             if (!$ledger->Post('insert')) {
                 return;
             }
             $orderCnt++;
         }
         // prepare for next order.
         $data = $nextData;
         $itemCnt = 1;
         $items = array();
         $totals = array();
         $inStock = true;
         $skip = false;
     }
     if ($orderCnt) {
         if (!$ledger->update_chart_history_periods($period)) {
             return;
         }
     }
     $db->transCommit();
     // finished successfully
     // ***************************** END TRANSACTION *******************************
     $messageStack->add(sprintf("Successfully posted %s Amazon transactions.", $orderCnt), 'success');
     if (DEBUG) {
         $messageStack->write_debug();
     }
     return true;
 }
示例#4
0
 function buildJournalEntry()
 {
     global $db, $messageStack, $currencies;
     // set some preliminary information
     $account_type = 'c';
     $psOrd = new orders();
     // make the received string look like a form submission then post as usual
     $psOrd->account_type = $account_type;
     $psOrd->id = '';
     // should be null unless opening an existing purchase/receive
     $psOrd->journal_id = JOURNAL_ID;
     $psOrd->post_date = $this->order['post_date'];
     // date format should already be YYYY-MM-DD
     $psOrd->terminal_date = $this->order['post_date'];
     // make same as order date for now
     $psOrd->period = gen_calculate_period($psOrd->post_date);
     $psOrd->store_id = $this->get_account_id($this->order['store_id'], 'b');
     $psOrd->admin_id = $this->get_user_id($this->username);
     $psOrd->description = SOAP_XML_SUBMITTED_SO;
     $psOrd->gl_acct_id = DEF_GL_ACCT;
     $psOrd->freight = $currencies->clean_value(db_prepare_input($this->order['freight_total']), DEFAULT_CURRENCY);
     $psOrd->discount = $currencies->clean_value(db_prepare_input($this->order['discount_total']), DEFAULT_CURRENCY);
     $psOrd->sales_tax = db_prepare_input($this->order['tax_total']);
     $psOrd->total_amount = db_prepare_input($this->order['order_total']);
     // The order ID should be set by the submitter
     $psOrd->purchase_invoice_id = db_prepare_input($this->order['order_id']);
     $psOrd->purch_order_id = db_prepare_input($this->order['purch_order_id']);
     $psOrd->shipper_code = db_prepare_input($this->order['freight_carrier']);
     /* Values below are not used at this time
     	$psOrd->sales_tax_auths
     	$psOrd->drop_ship = 0;
     	$psOrd->waiting = 0;
     	$psOrd->closed = 0;
     	$psOrd->subtotal
     	*/
     $psOrd->bill_add_update = 1;
     // force an address book update
     // see if the customer record exists
     $psOrd->short_name = db_prepare_input($this->order['billing']['customer_id']);
     if (!$psOrd->short_name && AUTO_INC_CUST_ID) {
         $result = $db->Execute("select next_cust_id_num from " . TABLE_CURRENT_STATUS);
         $short_name = $result->fields['next_cust_id_num'];
         $next_id = $short_name++;
         $db->Execute("update " . TABLE_CURRENT_STATUS . " set next_cust_id_num = '{$next_id}'");
     }
     $psOrd->ship_short_name = $psOrd->short_name;
     if (!($result = $this->checkForCustomerExists($psOrd))) {
         return;
     }
     $psOrd->ship_add_update = $result['ship_add_update'];
     $psOrd->bill_acct_id = $result['bill_acct_id'];
     $psOrd->bill_address_id = $result['bill_address_id'];
     $psOrd->ship_acct_id = $result['ship_acct_id'];
     $psOrd->ship_address_id = $result['ship_address_id'];
     if ($result['terms']) {
         $psOrd->terms = $result['terms'];
     }
     // Phreebooks requires a primary name or the order is not valid, use company name if exists, else contact name
     if ($this->order['billing']['primary_name'] == '') {
         $psOrd->bill_primary_name = $this->order['billing']['contact'];
         $psOrd->bill_contact = '';
     } else {
         $psOrd->bill_primary_name = $this->order['billing']['primary_name'];
         $psOrd->bill_contact = $this->order['billing']['contact'];
     }
     $psOrd->bill_address1 = $this->order['billing']['address1'];
     $psOrd->bill_address2 = $this->order['billing']['address2'];
     $psOrd->bill_city_town = $this->order['billing']['city_town'];
     $psOrd->bill_state_province = $this->order['billing']['state_province'];
     $psOrd->bill_postal_code = $this->order['billing']['postal_code'];
     $psOrd->bill_country_code = gen_get_country_iso_3_from_2($this->order['billing']['country_code']);
     $psOrd->bill_telephone1 = $this->order['billing']['telephone'];
     $psOrd->bill_email = $this->order['billing']['email'];
     if ($this->order['shipping']['primary_name'] == '') {
         $psOrd->ship_primary_name = $this->order['shipping']['contact'];
         $psOrd->ship_contact = '';
     } else {
         $psOrd->ship_primary_name = $this->order['shipping']['primary_name'];
         $psOrd->ship_contact = $this->order['shipping']['contact'];
     }
     $psOrd->ship_address1 = $this->order['shipping']['address1'];
     $psOrd->ship_address2 = $this->order['shipping']['address2'];
     $psOrd->ship_city_town = $this->order['shipping']['city_town'];
     $psOrd->ship_state_province = $this->order['shipping']['state_province'];
     $psOrd->ship_postal_code = $this->order['shipping']['postal_code'];
     $psOrd->ship_country_code = gen_get_country_iso_3_from_2($this->order['shipping']['country_code']);
     $psOrd->ship_telephone1 = $this->order['shipping']['telephone'];
     $psOrd->ship_email = $this->order['shipping']['email'];
     // check for truncation of addresses
     if (strlen($psOrd->bill_primary_name) > 32 || strlen($psOrd->bill_address1) > 32 || strlen($psOrd->ship_primary_name) > 32 || strlen($psOrd->ship_address1) > 32) {
         $this->msgAdd('Either the Primary Name or Address has been truncated to fit in the Phreedom database field sizes. Please check source information.', 'caution');
     }
     // load the item rows
     switch (JOURNAL_ID) {
         case 12:
             $index = 'pstd';
             break;
         case 10:
         default:
             $index = 'qty';
             break;
     }
     for ($i = 0; $i < count($this->order['items']); $i++) {
         $psOrd->item_rows[] = array('gl_type' => GL_TYPE, $index => db_prepare_input($this->order['items'][$i]['quantity']), 'sku' => db_prepare_input($this->order['items'][$i]['sku']), 'desc' => db_prepare_input($this->order['items'][$i]['description']), 'price' => db_prepare_input($this->order['items'][$i]['unit_price']), 'acct' => db_prepare_input($this->order['items'][$i]['gl_acct']), 'tax' => db_prepare_input($this->order['items'][$i]['taxable']), 'total' => db_prepare_input($this->order['items'][$i]['total_price']));
     }
     // error check input
     $missing_fields = array();
     if (!$psOrd->short_name && !AUTO_INC_CUST_ID) {
         $missing_fields[] = ACT_SHORT_NAME;
     }
     if (!$psOrd->post_date) {
         $missing_fields[] = TEXT_POST_DATE;
     }
     if (!$psOrd->period) {
         $missing_fields[] = TEXT_PERIOD;
     }
     if (!$psOrd->bill_primary_name) {
         $missing_fields[] = GEN_PRIMARY_NAME;
     }
     if (!$psOrd->bill_country_code) {
         $missing_fields[] = GEN_COUNTRY_CODE;
     }
     if (ADDRESS_BOOK_CONTACT_REQUIRED && !$psOrd->bill_contact) {
         $missing_fields[] = GEN_CONTACT;
     }
     if (ADDRESS_BOOK_ADDRESS1_REQUIRED && !$psOrd->bill_address1) {
         $missing_fields[] = GEN_ADDRESS1;
     }
     if (ADDRESS_BOOK_ADDRESS2_REQUIRED && !$psOrd->bill_address2) {
         $missing_fields[] = GEN_ADDRESS2;
     }
     if (ADDRESS_BOOK_CITY_TOWN_REQUIRED && !$psOrd->bill_city_town) {
         $missing_fields[] = GEN_CITY_TOWN;
     }
     if (ADDRESS_BOOK_STATE_PROVINCE_REQUIRED && !$psOrd->bill_state_province) {
         $missing_fields[] = GEN_STATE_PROVINCE;
     }
     if (ADDRESS_BOOK_POSTAL_CODE_REQUIRED && !$psOrd->bill_postal_code) {
         $missing_fields[] = GEN_POSTAL_CODE;
     }
     if (defined('MODULE_SHIPPING_STATUS')) {
         //	  if (!$psOrd->ship_primary_name)                                         $missing_fields[] = GEN_PRIMARY_NAME;
         //	  if (!$psOrd->ship_country_code)                                         $missing_fields[] = GEN_COUNTRY_CODE;
         if (ADDRESS_BOOK_SHIP_CONTACT_REQ && !$psOrd->ship_contact) {
             $missing_fields[] = GEN_CONTACT;
         }
         if (ADDRESS_BOOK_SHIP_ADD1_REQ && !$psOrd->ship_address1) {
             $missing_fields[] = GEN_ADDRESS1;
         }
         if (ADDRESS_BOOK_SHIP_ADD2_REQ && !$psOrd->ship_address2) {
             $missing_fields[] = GEN_ADDRESS2;
         }
         if (ADDRESS_BOOK_SHIP_CITY_REQ && !$psOrd->ship_city_town) {
             $missing_fields[] = GEN_CITY_TOWN;
         }
         if (ADDRESS_BOOK_SHIP_STATE_REQ && !$psOrd->ship_state_province) {
             $missing_fields[] = GEN_STATE_PROVINCE;
         }
         if (ADDRESS_BOOK_SHIP_POSTAL_CODE_REQ && !$psOrd->ship_postal_code) {
             $missing_fields[] = GEN_POSTAL_CODE;
         }
     }
     if (sizeof($missing_fields) > 0) {
         $this->msgAdd(sprintf(SOAP_MISSING_FIELDS, $this->order['reference'], implode(', ', $missing_fields)), 'error');
         return;
     }
     if (function_exists('xtra_order_before_post')) {
         xtra_order_before_post($psOrd, $this->order);
     }
     // post the sales order
     $this->msgDebug('ready to post =>' . print_r($psOrd, true));
     $post_success = $psOrd->post_ordr('insert');
     if (!$post_success) {
         // extract the error message from the messageStack and return with error
         $db->transRollback();
         return;
     }
     if (function_exists('xtra_order_after_post')) {
         xtra_order_after_post($psOrd, $this->order);
     }
     gen_add_audit_log(constant('AUDIT_LOG_SOAP_' . JOURNAL_ID . '_ADDED'), $psOrd->purchase_invoice_id, $psOrd->total_amount);
 }