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; }
$affected_ids = $glEntry->get_recur_ids($glEntry->recur_id, $glEntry->id); for ($i = 0; $i < count($affected_ids); $i++) { $glEntry->id = $affected_ids[$i]['id']; $glEntry->journal_main_array['id'] = $affected_ids[$i]['id']; if ($i > 0) { // Remove row id's for future posts, keep if re-posting single entry for ($j = 0; $j < count($glEntry->journal_rows); $j++) { $glEntry->journal_rows[$j]['id'] = ''; } $glEntry->post_date = $affected_ids[$i]['post_date']; } $glEntry->period = gen_calculate_period($glEntry->post_date, true); $glEntry->journal_main_array['post_date'] = $glEntry->post_date; $glEntry->journal_main_array['period'] = $glEntry->period; $glEntry->purchase_invoice_id = $affected_ids[$i]['purchase_invoice_id']; if (!$glEntry->validate_purchase_invoice_id()) { $error = true; break; } else { if (!$glEntry->Post('edit')) { $error = true; break; } } // test for single post versus rolling into future posts, terminate loop if single post if (!$glEntry->recur_frequency) { break; } } } else { // it's an insert
for ($i = 0; $i < sizeof($order->journal_rows); $i++) { $order->journal_rows[$i]['id'] = ''; $order->journal_rows[$i]['so_po_item_ref_id'] = ''; $order->journal_rows[$i]['post_date'] = $order->post_date; if ($order->journal_rows[$i]['gl_type'] == $search_gl_type) { $order->journal_rows[$i]['gl_type'] = GL_TYPE; } } // ***************************** START TRANSACTION ******************************* $db->transStart(); if ($purchase_invoice_id) { $order->journal_main_array['purchase_invoice_id'] = $purchase_invoice_id; $order->purchase_invoice_id = $purchase_invoice_id; } else { $order->purchase_invoice_id = ''; if (!$order->validate_purchase_invoice_id()) { $error = true; break; } } if (!$order->Post('insert')) { $error = true; } if ($order->purchase_invoice_id == '') { if (!$order->increment_purchase_invoice_id()) { $error = true; } } if (!$error) { gen_add_audit_log(constant('ORD_TEXT_' . JOURNAL_ID . '_WINDOW_TITLE') . ' - ' . TEXT_ADD, $order->purchase_invoice_id, $order->total_amount); // set the closed flag on the quote