require DIR_FS_MODULES . 'banking/functions/banking.php'; require DIR_FS_MODULES . 'banking/classes/banking.php'; require DIR_FS_MODULES . 'orders/functions/orders.php'; require DIR_FS_MODULES . 'orders/classes/orders.php'; /************** page specific initialization *************************/ $error = false; $post_success = false; $installed_modules = array(); $order = new banking(); $action = isset($_GET['action']) ? $_GET['action'] : $_POST['todo']; $gl_acct_id = $_POST['gl_acct_id'] ? db_prepare_input($_POST['gl_acct_id']) : $order->gl_acct_id; $order->gl_acct_id = $gl_acct_id; $order->acct_1 = DEF_DEP_GL_ACCT; $default_dep_acct = JOURNAL_ID == 18 ? AR_DEF_DEPOSIT_ACCT : AP_DEF_DEPOSIT_ACCT; $post_date = $_POST['post_date'] ? gen_db_date_short($_POST['post_date']) : date('Y-m-d', time()); $period = gen_calculate_period($post_date); if (!$period) { // bad post_date was submitted $action = ''; $post_date = date('Y-m-d', time()); $period = 0; } // load available payment modules, receipts only if (JOURNAL_ID == 18) { $module_directory = DIR_FS_MODULES . 'services/payment/modules/'; $file_extension = substr($PHP_SELF, strrpos($PHP_SELF, '.')); $directory_array = array(); if ($dir = @dir($module_directory)) { while ($file = $dir->read()) { if (!is_dir($module_directory . $file)) { if (substr($file, strrpos($file, '.')) == $file_extension) {
function submitJournalEntry() { global $db, $currencies, $messageStack; $entry_count = 0; $row_cnt = 0; while ($row_cnt < count($this->records)) { $order = $this->records[$row_cnt]; $glEntry = new journal(); // determine if date is within a known period, if date is before period 1 use period = 0 (and enter beginning balances) $glEntry->period = gen_calculate_period($order['post_date'], $hide_error = true); // date format YYYY-MM-DD if (!$glEntry->period) { $glEntry->period = 1; } // if out of range default to first period (required to be valid period or it won't post) // build journal main entry $glEntry->journal_id = JOURNAL_ID; $glEntry->post_date = $order['post_date']; $glEntry->description = sprintf(TEXT_JID_ENTRY, constant('ORD_TEXT_' . JOURNAL_ID . '_WINDOW_TITLE')); $glEntry->short_name = $order['account_id']; $glEntry->ship_short_name = $order['account_id']; $glEntry->gl_acct_id = $order['gl_acct']; $glEntry->total_amount = $order['total_amount']; $glEntry->currencies_code = DEFAULT_CURRENCY; $glEntry->purchase_invoice_id = $order['order_id']; $glEntry->admin_id = $_SESSION['admin_id']; // set imported dept rep id to current logged in user if ($order['waiting']) { $glEntry->waiting = '1'; } $glEntry->bill_primary_name = $order['bill_primary_name']; $glEntry->bill_contact = $order['bill_contact']; $glEntry->bill_address1 = $order['bill_address1']; $glEntry->bill_address2 = $order['bill_address2']; $glEntry->bill_city_town = $order['bill_city_town']; $glEntry->bill_state_province = $order['bill_state_province']; $glEntry->bill_postal_code = $order['bill_postal_code']; $glEntry->bill_country_code = $order['bill_country_code']; $glEntry->bill_telephone1 = $order['telephone1']; $glEntry->bill_telephone2 = $order['telephone2']; $glEntry->bill_fax = $order['fax']; $glEntry->bill_email = $order['email']; $glEntry->bill_website = $order['website']; switch (JOURNAL_ID) { case 4: case 6: $glEntry->ship_primary_name = COMPANY_NAME; $glEntry->ship_address1 = COMPANY_ADDRESS1; $glEntry->ship_address2 = COMPANY_ADDRESS2; $glEntry->ship_city_town = COMPANY_CITY_TOWN; $glEntry->ship_state_province = COMPANY_ZONE; $glEntry->ship_postal_code = COMPANY_POSTAL_CODE; $glEntry->ship_country_code = COMPANY_COUNTRY; break; default: $glEntry->ship_primary_name = $order['ship_primary_name']; $glEntry->ship_contact = $order['ship_contact']; $glEntry->ship_address1 = $order['ship_address1']; $glEntry->ship_address2 = $order['ship_address2']; $glEntry->ship_city_town = $order['ship_city_town']; $glEntry->ship_state_province = $order['ship_state_province']; $glEntry->ship_postal_code = $order['ship_postal_code']; $glEntry->ship_country_code = $order['ship_country_code']; } $glEntry->journal_main_array = $glEntry->build_journal_main_array(); $glEntry->journal_main_array['purchase_invoice_id'] = $order['order_id']; // skip validating the invoice ID, just set it // Create the account (or update it) $glEntry->bill_acct_id = $glEntry->add_account(BB_ACCOUNT_TYPE . 'b', 0, 0, true); switch (JOURNAL_ID) { default: // update the shipping address $glEntry->ship_acct_id = $glEntry->add_account(BB_ACCOUNT_TYPE . 's', 0, 0, true); break; case 4: // skip for purchases (assume default company address) // skip for purchases (assume default company address) case 6: } // build journal row entries (2) one for the AP/AR account and the other for the beg bal equity account $glEntry->journal_rows = array(); $total_amount = 0; while (true) { $credit_debit = false; switch (JOURNAL_ID) { case 4: $credit_debit = 'debit_amount'; // for journal_id = 4 // for journal_id = 4 case 10: if (!$credit_debit) { $credit_debit = 'credit_amount'; } // for journal_id = 10 $glEntry->journal_rows[] = array('gl_type' => BB_GL_TYPE, 'qty' => $currencies->clean_value($order['quantity']), 'sku' => $order['sku'], 'description' => $order['description'], 'gl_account' => $order['inv_gl_acct'], 'taxable' => $order['taxable'] ? $order['taxable'] : 0, $credit_debit => $currencies->clean_value($order['total_cost']), 'post_date' => $order['post_date']); break; case 6: $credit_debit = 'debit_amount'; // for journal_id = 6 // for journal_id = 6 case 12: if (!$credit_debit) { $credit_debit = 'credit_amount'; } // for journal_id = 12 $glEntry->journal_rows[] = array('gl_type' => BB_GL_TYPE, 'qty' => '1', 'description' => constant('ORD_TEXT_' . JOURNAL_ID . '_WINDOW_TITLE') . '-' . TEXT_IMPORT, 'gl_account' => $order['inv_gl_acct'], 'taxable' => $order['taxable'] ? $order['taxable'] : 0, $credit_debit => $order['total_amount'], 'post_date' => $order['post_date']); break; } $total_amount += $currencies->clean_value($order['total_cost']); $next_order = $this->records[$row_cnt + 1]['order_id']; if ((JOURNAL_ID == 4 || JOURNAL_ID == 10) && $order['order_id'] == $next_order) { // more line items $row_cnt++; $order = $this->records[$row_cnt]; } else { // end of this order, break from while(true) loop break; } } // build the total journal_item row switch (JOURNAL_ID) { case 6: $total_amount = $order['total_amount']; // and continue // and continue case 4: $debit_credit = 'credit_amount'; break; case 12: $total_amount = $order['total_amount']; // and continue // and continue case 10: $debit_credit = 'debit_amount'; break; } $glEntry->journal_rows[] = array('gl_type' => 'ttl', 'description' => constant('ORD_TEXT_' . $glEntry->journal_id . '_WINDOW_TITLE') . '-' . TEXT_TOTAL, 'gl_account' => $order['gl_acct'], $debit_credit => $total_amount, 'post_date' => $order['post_date']); $glEntry->journal_main_array['total_amount'] = $total_amount; if (!$glEntry->Post('insert')) { return false; } $entry_count++; $row_cnt++; } $this->line_count = $entry_count; return true; }
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('/ /', '', $text); // the 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; }
$order->ship_contact = db_prepare_input($_POST['ship_contact'] != GEN_CONTACT ? $_POST['ship_contact'] : '', ADDRESS_BOOK_SHIP_CONTACT_REQ); $order->ship_address1 = db_prepare_input($_POST['ship_address1'] != GEN_ADDRESS1 ? $_POST['ship_address1'] : '', ADDRESS_BOOK_SHIP_ADD1_REQ); $order->ship_address2 = db_prepare_input($_POST['ship_address2'] != GEN_ADDRESS2 ? $_POST['ship_address2'] : '', ADDRESS_BOOK_SHIP_ADD2_REQ); $order->ship_city_town = db_prepare_input($_POST['ship_city_town'] != GEN_CITY_TOWN ? $_POST['ship_city_town'] : '', ADDRESS_BOOK_SHIP_CITY_REQ); $order->ship_state_province = db_prepare_input($_POST['ship_state_province'] != GEN_STATE_PROVINCE ? $_POST['ship_state_province'] : '', ADDRESS_BOOK_SHIP_STATE_REQ); $order->ship_postal_code = db_prepare_input($_POST['ship_postal_code'] != GEN_POSTAL_CODE ? $_POST['ship_postal_code'] : '', ADDRESS_BOOK_SHIP_POSTAL_CODE_REQ); $order->ship_country_code = db_prepare_input($_POST['ship_country_code']); $order->ship_telephone1 = db_prepare_input($_POST['ship_telephone1'] != GEN_TELEPHONE1 ? $_POST['ship_telephone1'] : '', ADDRESS_BOOK_TELEPHONE1_REQUIRED); $order->ship_email = db_prepare_input($_POST['ship_email'] != GEN_EMAIL ? $_POST['ship_email'] : '', ADDRESS_BOOK_EMAIL_REQUIRED); $order->shipper_code = implode(':', array(db_prepare_input($_POST['ship_carrier']), db_prepare_input($_POST['ship_service']))); // load journal main data $order->id = $_POST['id'] != '' ? $_POST['id'] : ''; // will be null unless opening an existing purchase/receive $order->journal_id = JOURNAL_ID; $order->post_date = gen_db_date_short($_POST['post_date']); $order->period = gen_calculate_period($order->post_date); if (!$order->period) { break; } // bad post_date was submitted if ($_SESSION['admin_prefs']['restrict_period'] && $order->period != CURRENT_ACCOUNTING_PERIOD) { $error = $messageStack->add(ORD_ERROR_NOT_CUR_PERIOD, 'error'); break; } $order->so_po_ref_id = db_prepare_input($_POST['so_po_ref_id']); // Internal link to reference po/so record $order->purchase_invoice_id = db_prepare_input($_POST['purchase_invoice_id']); // PhreeBooks order/invoice ID $order->purch_order_id = db_prepare_input($_POST['purch_order_id']); // customer PO/Ref number $order->store_id = db_prepare_input($_POST['store_id']);
} $rowCnt++; } // test for errors if ($source_store_id == $dest_store_id) { $error = $messageStack->add(INV_XFER_ERROR_SAME_STORE_ID, 'error'); } // process the request, first subtract from the source store if (!$error) { $glEntry = new journal(); $glEntry->id = isset($_POST['id']) ? $_POST['id'] : ''; $glEntry->so_po_ref_id = '-1'; // first of 2 adjustments $glEntry->journal_id = JOURNAL_ID; $glEntry->post_date = $post_date; $glEntry->period = gen_calculate_period($post_date); $glEntry->store_id = $source_store_id; $glEntry->bill_acct_id = $dest_store_id; $glEntry->purchase_invoice_id = db_prepare_input($_POST['purchase_invoice_id']); $glEntry->admin_id = $_SESSION['admin_id']; $glEntry->closed = '1'; // closes by default $glEntry->closed_date = $post_date; $glEntry->currencies_code = DEFAULT_CURRENCY; $glEntry->currencies_value = 1; $adj_reason = db_prepare_input($_POST['adj_reason']); // $adj_account = db_prepare_input($_POST['gl_acct']); // process the request $glEntry->journal_main_array = $glEntry->build_journal_main_array(); $rowCnt = 1; $adj_total = 0;
function gen_auto_update_period($show_message = true) { global $db, $messageStack; $period = gen_calculate_period(date('Y-m-d'), true); if ($period == CURRENT_ACCOUNTING_PERIOD) { return; } // we're in the current period if (!$period) { // we're outside of the defined fiscal years if ($show_message) { $messageStack->add(ERROR_MSG_POST_DATE_NOT_IN_FISCAL_YEAR, 'error'); } } else { // update CURRENT_ACCOUNTING_PERIOD constant with this new period $result = $db->Execute("select start_date, end_date from " . TABLE_ACCOUNTING_PERIODS . " where period = " . $period); $db->Execute("update " . TABLE_CONFIGURATION . " set configuration_value = " . $period . " \r\n\t\twhere configuration_key = 'CURRENT_ACCOUNTING_PERIOD'"); $db->Execute("update " . TABLE_CONFIGURATION . " set configuration_value = '" . $result->fields['start_date'] . "' \r\n\t\twhere configuration_key = 'CURRENT_ACCOUNTING_PERIOD_START'"); $db->Execute("update " . TABLE_CONFIGURATION . " set configuration_value = '" . $result->fields['end_date'] . "' \r\n\t\twhere configuration_key = 'CURRENT_ACCOUNTING_PERIOD_END'"); if ($show_message) { gen_add_audit_log(GEN_LOG_PERIOD_CHANGE); $messageStack->add(sprintf(ERROR_MSG_ACCT_PERIOD_CHANGE, $period), 'success'); } } }
function post_ordr($action) { global $db, $messageStack; $this->journal_rows = array(); // initialize ledger row(s) array $debit_total = 0; $credit_total = 0; switch ($this->journal_id) { // THE SEQUENCE IS IMPORTANT! case 6: // Purchase/Receive Journal // Purchase/Receive Journal case 13: // Customer Credit Memo Journal $this->closed = 0; // force the inv receipt/vcm open since it will be closed by the system, if necessary // continue like other payable prep // force the inv receipt/vcm open since it will be closed by the system, if necessary // continue like other payable prep case 3: // Purchase Quote Journal // Purchase Quote Journal case 4: // Purchase Order Journal // Purchase Order Journal case 21: // Inventory Direct Purchase Journal (POP) $debit_total += $this->add_item_journal_rows('debit'); // read in line items and add to journal row array $debit_total += $this->add_freight_journal_row('debit'); // put freight into journal row array $debit_total += $this->add_tax_journal_rows('debit'); // fetch tax rates for tax calculation $credit_total += $this->add_discount_journal_row('credit'); // put discount into journal row array $this->total_amount = $debit_total - $credit_total; $credit_total += $this->add_total_journal_row('credit'); // put total value into ledger row array break; case 7: // Vendor Credit Memo Journal // Vendor Credit Memo Journal case 12: // Sales/Invoice Journal (Invoice) $this->closed = 0; // force the inv/cm open since it will be closed by the system, if necessary // continue like other receivable prep // force the inv/cm open since it will be closed by the system, if necessary // continue like other receivable prep case 9: // Sales Quote Journal // Sales Quote Journal case 10: // Sales Order Journal // Sales Order Journal case 19: // POS Journal $credit_total += $this->add_item_journal_rows('credit'); // read in line items and add to journal row array $credit_total += $this->add_freight_journal_row('credit'); // put freight into journal row array $credit_total += $this->add_tax_journal_rows('credit'); // fetch tax rates for tax calculation $debit_total += $this->add_discount_journal_row('debit'); // put discount into journal row array $this->total_amount = $credit_total - $debit_total; $debit_total += $this->add_total_journal_row('debit'); // put total value into ledger row array break; default: return $this->fail_message('bad journal_id in pre-POST processing'); // this should never happen, JOURNAL_ID is tested at script entry! } $this->journal_main_array = $this->build_journal_main_array(); // build ledger main record // ***************************** START TRANSACTION ******************************* $messageStack->debug("\n started order post purchase_invoice_id = " . $this->purchase_invoice_id . " and id = " . $this->id); $db->transStart(); // ************* Pre-POST processing ************* // add/update address book if ($this->bill_add_update) { // billing address $this->bill_acct_id = $this->add_account($this->account_type . 'b', $this->bill_acct_id, $this->bill_address_id); if (!$this->bill_acct_id) { return false; } } if ($this->ship_add_update) { // shipping address if (!$this->ship_acct_id) { $this->ship_acct_id = $this->bill_acct_id; } // set to bill if adding contact and id not set. if ($this->bill_address_id == $this->ship_address_id) { $this->ship_address_id = ''; } // force create new ship address, here from copy button $this->ship_acct_id = $this->add_account($this->account_type . 's', $this->ship_acct_id, $this->ship_address_id); if (!$this->ship_acct_id) { return false; } } // set the ship account id to bill account id if null (new accounts with ship update box not checked) // basically defaults the shipping same as billing if not specified if (!$this->journal_main_array['ship_acct_id']) { $this->journal_main_array['ship_acct_id'] = $this->journal_main_array['bill_acct_id']; } if (!$this->journal_main_array['ship_address_id']) { $this->journal_main_array['ship_address_id'] = $this->journal_main_array['bill_address_id']; } // ************* POST journal entry ************* if ($this->recur_id > 0) { // if new record, will contain count, if edit will contain recur_id $first_id = $this->id; $first_post_date = $this->post_date; $first_purchase_invoice_id = $this->purchase_invoice_id; $first_terminal_date = $this->first_terminal_date; if ($this->id) { // it's an edit, fetch list of affected records to update if roll is enabled $affected_ids = $this->get_recur_ids($this->recur_id, $this->id); for ($i = 0; $i < count($affected_ids); $i++) { $this->id = $affected_ids[$i]['id']; $this->journal_main_array['id'] = $affected_ids[$i]['id']; $this->remove_cogs_rows(); if ($i > 0) { // Remove row id's for future posts, keep if re-posting single entry for ($j = 0; $j < count($this->journal_rows); $j++) { $this->journal_rows[$j]['id'] = ''; } $this->post_date = $affected_ids[$i]['post_date']; $this->terminal_date = $affected_ids[$i]['terminal_date']; $this->purchase_invoice_id = $affected_ids[$i]['purchase_invoice_id']; } else { // for first entry, post date may be changed, use $_POST value $this->post_date = $first_post_date; $this->terminal_date = $first_terminal_date; $this->purchase_invoice_id = $first_purchase_invoice_id; } $this->period = gen_calculate_period($this->post_date, true); $this->journal_main_array['post_date'] = $this->post_date; $this->journal_main_array['period'] = $this->period; $this->journal_main_array['terminal_date'] = $this->terminal_date; if (!$this->validate_purchase_invoice_id()) { return false; } $messageStack->debug("\n\n re-posting recur id = " . $this->id); if (!$this->Post('edit')) { return false; } // test for single post versus rolling into future posts, terminate loop if single post if (!$this->recur_frequency) { break; } } } else { // it's an insert // fetch the next recur id $this->journal_main_array['recur_id'] = time(); $day_offset = 0; $month_offset = 0; $year_offset = 0; $post_date = $this->post_date; for ($i = 0; $i < $this->recur_id; $i++) { if (!$this->validate_purchase_invoice_id()) { return false; } if (!$this->Post('insert')) { return false; } $this->id = ''; $this->journal_main_array['id'] = $this->id; $this->remove_cogs_rows(); for ($j = 0; $j < count($this->journal_rows); $j++) { $this->journal_rows[$j]['id'] = ''; } switch ($this->recur_frequency) { default: case '1': $day_offset = ($i + 1) * 7; break; // Weekly // Weekly case '2': $day_offset = ($i + 1) * 14; break; // Bi-weekly // Bi-weekly case '3': $month_offset = ($i + 1) * 1; break; // Monthly // Monthly case '4': $month_offset = ($i + 1) * 3; break; // Quarterly // Quarterly case '5': $year_offset = ($i + 1) * 1; break; // Yearly } $this->post_date = gen_specific_date($post_date, $day_offset, $month_offset, $year_offset); if ($this->terminal_date) { $this->terminal_date = gen_specific_date($this->terminal_date, $day_offset, $month_offset, $year_offset); } $this->period = gen_calculate_period($this->post_date, true); if (!$this->period && $i < $this->recur_id - 1) { // recur falls outside of available periods, ignore last calculation return $this->fail_message(ORD_PAST_LAST_PERIOD); } $this->journal_main_array['post_date'] = $this->post_date; $this->journal_main_array['period'] = $this->period; $this->journal_main_array['terminal_date'] = $this->terminal_date; if (in_array($this->journal_id, array(4, 10, 12, 19)) && $first_purchase_invoice_id == '') { $this->increment_purchase_invoice_id(true); } $this->purchase_invoice_id = string_increment($this->journal_main_array['purchase_invoice_id']); } } // restore the first values to continue with post process if (in_array($this->journal_id, array(4, 10, 12, 19)) && $first_purchase_invoice_id == '') { // special case for auto increment $first_purchase_invoice_id = $this->purchase_invoice_id; } $this->id = $first_id; $this->post_date = $first_post_date; $this->purchase_invoice_id = $first_purchase_invoice_id; $this->terminal_date = $first_terminal_date; $this->journal_main_array['id'] = $first_id; $this->journal_main_array['post_date'] = $first_post_date; $this->journal_main_array['purchase_invoice_id'] = $first_purchase_invoice_id; $this->journal_main_array['terminal_date'] = $first_terminal_date; } else { if (!$this->validate_purchase_invoice_id()) { return false; } if (!$this->Post($this->id ? 'edit' : 'insert')) { return false; } } // ************* post-POST processing ************* switch ($this->journal_id) { case 3: // Purchase Quote Journal // Purchase Quote Journal case 4: // Purchase Order Journal // Purchase Order Journal case 7: // Vendor Credit Memo Journal // Vendor Credit Memo Journal case 9: // Sales Quote Journal // Sales Quote Journal case 10: // Sales Order Journal // Sales Order Journal case 12: // Sales/Invoice Journal // Sales/Invoice Journal case 13: // Customer Credit Memo Journal if ($this->purchase_invoice_id == '') { // it's a new record, increment the po/so/inv to next number if (!$this->increment_purchase_invoice_id()) { return false; } } break; case 6: // Purchase Journal // Purchase Journal default: break; } $messageStack->debug("\n committed order post purchase_invoice_id = " . $this->purchase_invoice_id . " and id = " . $this->id); $db->transCommit(); // finished successfully //echo 'committed transaction - bailing!'; exit(); // ***************************** END TRANSACTION ******************************* $messageStack->add(sprintf(TEXT_SUCCESSFULLY_POSTED, constant('ORD_HEADING_NUMBER_' . $this->journal_id) . ' ' . $this->purchase_invoice_id), 'success'); return true; }
$month_offset = ($i + 1) * 1; break; // Monthly // Monthly case '4': $month_offset = ($i + 1) * 3; break; // Quarterly // Quarterly case '5': $year_offset = ($i + 1) * 1; break; // Yearly } $glEntry->post_date = gen_specific_date($post_date, $day_offset, $month_offset, $year_offset); $glEntry->period = gen_calculate_period($glEntry->post_date, true); if (!$glEntry->period && $i < $glEntry->recur_id - 1) { // recur falls outside of available periods, ignore last calculation $messageStack->add(ORD_PAST_LAST_PERIOD, 'error'); $error = true; break; } $glEntry->journal_main_array['post_date'] = $glEntry->post_date; $glEntry->journal_main_array['period'] = $glEntry->period; $glEntry->purchase_invoice_id = string_increment($glEntry->journal_main_array['purchase_invoice_id']); } } // restore the first values to continue with post process $glEntry->id = $first_id; $glEntry->journal_main_array['id'] = $first_id; $glEntry->post_date = $first_post_date;
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; }
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); }
function load_report_data($Prefs, $Seq, $sql = '', $GrpField = '') { global $db; // prepare the sql by temporarily replacing calculated fields with real fields $sql_fields = substr($sql, strpos($sql, 'select ') + 7, strpos($sql, ' from ') - 7); $this->sql_field_array = explode(', ', $sql_fields); for ($i = 0; $i < count($this->sql_field_array); $i++) { $this->sql_field_karray['c' . $i] = substr($this->sql_field_array[$i], 0, strpos($this->sql_field_array[$i], ' ')); } $sql = $this->replace_special_fields($sql); //echo 'sql = ' . $sql . '<br />'; exit; //echo 'Prefs = '; print_r($Prefs); echo '<br />'; $result = $db->Execute($sql); if ($result->RecordCount() == 0) { return false; } // No data so bail now // Generate the output data array /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/ $this->gl_account = $Prefs['fromvalue1']; // assumes that the gl account is the first criteria, equal to if (!$this->gl_account) { return false; } // No gl account so bail now $dates = gen_build_sql_date($Prefs['datedefault'], $Prefs['datefield']); $post_date = $dates['start_date']; $period = gen_calculate_period($post_date, $hide_error = true); if (!$period) { $period = 1; } $temp = $db->Execute("select beginning_balance from " . TABLE_CHART_OF_ACCOUNTS_HISTORY . " \r\n\t\twhere account_id = '" . $this->gl_account . "' and period = " . $period); $beginning_balance = $temp->fields['beginning_balance']; // load the payments and deposits for the current period $sql = "select sum(i.debit_amount) as deposit, sum(i.credit_amount) as withdrawal\r\n\t\tfrom " . TABLE_JOURNAL_MAIN . " m inner join " . TABLE_JOURNAL_ITEM . " i on m.id = i.ref_id\r\n\t\twhere m.period = " . $period . " and m.post_date < '" . $post_date . "' and i.gl_account = '" . $this->gl_account . "'"; $temp = $db->Execute($sql); $this->beginning_balance = $beginning_balance + $temp->fields['deposit'] - $temp->fields['withdrawal']; $this->current_balance = $this->beginning_balance; /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/ $RowCnt = 0; // Row counter for output data $ColCnt = 1; $GrpWorking = false; while (!$result->EOF) { $myrow = $result->fields; // Check to see if a total row needs to be displayed if (isset($GrpField)) { // we're checking for group totals, see if this group is complete if ($myrow[$GrpField] != $GrpWorking && $GrpWorking !== false) { // it's a new group so print totals $OutputArray[$RowCnt][0] = 'g:' . $GrpWorking; foreach ($Seq as $offset => $TotalCtl) { $OutputArray[$RowCnt][$offset + 1] = ProcessData($TotalCtl['grptotal'], $TotalCtl['processing']); $Seq[$offset]['grptotal'] = ''; // reset the total } $RowCnt++; // go to next row } $GrpWorking = $myrow[$GrpField]; // set to new grouping value } $OutputArray[$RowCnt][0] = 'd'; // let the display class know its a data element //echo 'orig myrow = '; print_r($myrow); echo '<br /><br />'; $myrow = $this->replace_data_fields($myrow, $Seq); //echo 'new myrow = '; print_r($myrow); echo '<br /><br />'; // build the new balance foreach ($Seq as $key => $TableCtl) { // calculate the new balance //echo 'key = ' . $key . ' and fieldname = '; print_r($Prefs['FieldListings'][$key+1]['fieldname']); echo '<br />'; if ($Prefs['FieldListings'][$key + 1]['fieldname'] == '[table2].debit_amount') { $this->beginning_balance += $myrow[$TableCtl['fieldname']]; } if ($Prefs['FieldListings'][$key + 1]['fieldname'] == '[table2].credit_amount') { $this->beginning_balance -= $myrow[$TableCtl['fieldname']]; } } foreach ($Seq as $key => $TableCtl) { // // insert data into output array and set to next column $OutputArray[$RowCnt][$ColCnt] = ProcessData($myrow[$TableCtl['fieldname']], $TableCtl['processing']); $ColCnt++; if ($TableCtl['total']) { // add to the running total if need be $Seq[$key]['grptotal'] += $myrow[$TableCtl['fieldname']]; $Seq[$key]['rpttotal'] += $myrow[$TableCtl['fieldname']]; } } $RowCnt++; $ColCnt = 1; $result->MoveNext(); } // add an extra line at the bottom with the newest balance $OutputArray[$RowCnt][0] = 'd'; // let the display class know its a data element foreach ($Seq as $key => $TableCtl) { // calculate the new balance if ($Prefs['FieldListings'][$key + 1]['fieldname'] == 'beg_balance') { $OutputArray[$RowCnt][$ColCnt] = ProcessData($this->beginning_balance, $TableCtl['processing']); } else { $OutputArray[$RowCnt][$ColCnt] = ''; } $ColCnt++; } $RowCnt++; $ColCnt = 1; if ($GrpWorking !== false) { // if we collected group data show the final group total $OutputArray[$RowCnt][0] = 'g:' . $GrpWorking; foreach ($Seq as $TotalCtl) { $OutputArray[$RowCnt][$ColCnt] = $TotalCtl['total'] == '1' ? ProcessData($TotalCtl['grptotal'], $TotalCtl['processing']) : ' '; $ColCnt++; } $RowCnt++; $ColCnt = 1; } // see if we have a total to send $ShowTotals = false; foreach ($Seq as $TotalCtl) { if ($TotalCtl['total'] == '1') { $ShowTotals = true; } } if ($ShowTotals) { $OutputArray[$RowCnt][0] = 'r:' . $Prefs['description']; foreach ($Seq as $TotalCtl) { if ($TotalCtl['total']) { $OutputArray[$RowCnt][$ColCnt] = ProcessData($TotalCtl['rpttotal'], $TotalCtl['processing']); } else { $OutputArray[$RowCnt][$ColCnt] = ' '; } $ColCnt++; } } // echo 'output array = '; print_r($OutputArray); echo '<br />'; exit(); return $OutputArray; }
public function start_import($ouwer_bank_account_number, $post_date, $other_bank_account_number, $credit_amount, $debit_amount, $description, $bank_gl_acct, $other_bank_account_iban) { global $db, $messageStack, $currencies; if ($post_date == '') { $messageStack->add(" date is required", 'error'); return false; exit; } $this->reset(); $messageStack->debug("\n\n*************** Start Processing Import Payment *******************"); if ($ouwer_bank_account_number != '') { $ouwer_bank = ltrim($ouwer_bank_account_number, 0); if ($ouwer_bank == '') { $messageStack->add(TEXT_BIMP_ERMSG1, 'error'); return false; exit; } $sql = "select id, description from " . TABLE_CHART_OF_ACCOUNTS . " where description like '%{$ouwer_bank}%'"; $result = $db->Execute($sql); if ($result->RecordCount() == 0) { $messageStack->add(TEXT_BIMP_ERMSG5 . ' ' . $ouwer_bank, 'error'); return false; exit; } if (!$result->RecordCount() > 1) { $messageStack->add(TEXT_BIMP_ERMSG2 . ' ' . $ouwer_bank, 'error'); return false; exit; } $this->gl_acct_id = $result->fields['id']; } else { if ($bank_gl_acct == '') { $messageStack->add(TEXT_BIMP_ERMSG1, 'error'); return false; exit; } $this->gl_acct_id = $bank_gl_acct; } $this->_description = $description; $this->_creditamount = $currencies->clean_value($credit_amount); $this->_debitamount = $currencies->clean_value($debit_amount); $this->total_amount = $this->_debitamount + $this->_creditamount; $this->post_date = gen_db_date($post_date); $this->period = gen_calculate_period($this->post_date, true); $this->admin_id = $_SESSION['admin_id']; if ($this->find_contact($other_bank_account_number, $other_bank_account_iban)) { $this->find_right_invoice(); } else { if ($this->proces_know_mutation($other_bank_account_number, $other_bank_account_iban) == false) { $this->proces_mutation(); } } $messageStack->debug("\n\n*************** End Processing Import Payment *******************"); }