function processInventory($upload_name) { global $coa, $db, $currencies, $messageStack; if (!$this->cyberParse($upload_name)) { return false; } $post_date = gen_specific_date(date('Y-m-d'), $day_offset = -1); $glEntry = new journal(); $sku_list = array(); $coa_list = array(); $affected_accounts = array(); for ($row_id = 0, $j = 2; $row_id < count($this->records); $row_id++, $j++) { $row = $this->records[$row_id]; $total_amount = $currencies->clean_value($row['total_amount']); $qty = $currencies->clean_value($row['quantity']); // check for errors and report/exit if error found if (!gen_validate_sku($row['sku'])) { $messageStack->add(GL_ERROR_UPDATING_INVENTORY_STATUS . $row['sku'] . GL_BEG_BAL_ERROR_0 . $j, 'error'); return false; } if (!in_array($row['inv_gl_acct'], $coa) || !in_array($row['gl_acct'], $coa)) { $messageStack->add(GL_BEG_BAL_ERROR_6 . $j, 'error'); return false; } if ($qty == 0) { $messageStack->add(GL_BEG_BAL_ERROR_7 . $j, 'caution'); } else { $affected_accounts[$row['inv_gl_acct']] = true; // need list of accounts to update history $affected_accounts[$row['gl_acct']] = true; // both credit and debit $sku_list[$row['sku']]['qty'] += $qty; // load quantity indexed by sku $sku_list[$row['sku']]['total'] += $total_amount; // load total_value indexed by sku $coa_list[$row['inv_gl_acct']] += $total_amount; // add to debit total by coa $coa_list[$row['gl_acct']] -= $total_amount; // add to credit total by coa } } if (is_array($sku_list)) { $glEntry->affected_accounts = $affected_accounts; // *************** START TRANSACTION ************************* $db->transStart(); // update inventory balances on hand foreach ($sku_list as $sku => $details) { $sql = "update " . TABLE_INVENTORY . " set quantity_on_hand = quantity_on_hand + {$details['qty']} where sku = '{$sku}'"; $result = $db->Execute($sql); if ($result->AffectedRows() != 1) { $messageStack->add(sprintf(GL_BEG_BAL_ERROR_8, $sku), 'error'); $db->transRollback(); return false; } $history_array = array('ref_id' => 0, 'sku' => $sku, 'qty' => $details['qty'], 'remaining' => $details['qty'], 'unit_cost' => $details['total'] / $details['qty'], 'avg_cost' => $details['total'] / $details['qty'], 'post_date' => $post_date); $result = db_perform(TABLE_INVENTORY_HISTORY, $history_array, 'insert'); } // update chart of account beginning balances for period 1 foreach ($coa_list as $account => $amount) { $sql = "update " . TABLE_CHART_OF_ACCOUNTS_HISTORY . " set beginning_balance = beginning_balance + {$amount}\n\t\t\twhere account_id = '{$account}' and period = 1"; $result = $db->Execute($sql); if ($result->AffectedRows() != 1) { $messageStack->add(sprintf(GL_BEG_BAL_ERROR_9, $account), 'error'); $db->transRollback(); return false; } } // update the chart of accounts history through the existing periods if (!$glEntry->update_chart_history_periods($period = 1)) { if (DEBUG) { $messageStack->write_debug(); } return $glEntry->fail_message(GL_ERROR_UPDATE_COA_HISTORY); } $db->transCommit(); // post the chart of account values if (DEBUG) { $messageStack->write_debug(); } // *************** END TRANSACTION ************************* } $this->line_count = $row_id; return true; }
} // check to see if journal is still in balance $total_amount = $currencies->format($total_amount); if ($total_amount != 0) { $messageStack->add(GL_ERROR_NO_BALANCE, 'error'); break; } // *************** START TRANSACTION ************************* $db->transStart(); foreach ($glEntry->beg_bal as $account => $values) { $sql = "update " . TABLE_CHART_OF_ACCOUNTS_HISTORY . " \r\n\t\t\tset beginning_balance = " . $values['beg_bal'] . " \r\n\t\t\twhere period = 1 and account_id = '" . $account . "'"; $result = $db->Execute($sql); } if (!$glEntry->update_chart_history_periods($period = 1)) { // roll the beginning balances into chart history table $glEntry->fail_message(GL_ERROR_UPDATE_COA_HISTORY); } else { $db->transCommit(); // post the chart of account values gen_add_audit_log('Enter Beginning Balances'); gen_redirect(html_href_link(FILENAME_DEFAULT, gen_get_all_get_params(array('action')), 'SSL')); // *************** END TRANSACTION ************************* } $messageStack->add(GL_ERROR_NO_POST, 'error'); break; default: } /***************** prepare to display templates *************************/ $include_header = true; // include header flag $include_footer = true;