validate_security($security_level, 4); // security check if ($id = $_POST['id']) { $delOrd = new journal($id); $result = $db->Execute("SELECT id FROM " . TABLE_JOURNAL_MAIN . " WHERE so_po_ref_id = {$delOrd->id}"); $xfer_to_id = $result->fields['id']; // save the matching adjust ID if (!$xfer_to_id) { $error = $messageStack('cannot deltete there is no offsetting record to delete!', 'error'); } if (!$error) { // *************** START TRANSACTION ************************* $db->transStart(); if ($delOrd->unPost('delete')) { $delOrd = new journal($xfer_to_id); if ($delOrd->unPost('delete')) { $db->transCommit(); // if not successful rollback will already have been performed gen_add_audit_log(INV_LOG_ADJ . TEXT_DELETE, $delOrd->journal_rows[0]['sku'], $delOrd->journal_rows[0]['qty']); if (DEBUG) { $messageStack->write_debug(); } gen_redirect(html_href_link(FILENAME_DEFAULT, gen_get_all_get_params(array('action')), 'SSL')); break; } else { $db->transRollback(); } } else { $db->transRollback(); } }
if (DEBUG) { $messageStack->write_debug(); } gen_redirect(html_href_link(FILENAME_DEFAULT, gen_get_all_get_params(array('action')), 'SSL')); // *************** END TRANSACTION ************************* } break; case 'delete': validate_security($security_level, 4); // security check if (!$error && $glEntry->id) { $delAssy = new journal($glEntry->id); // load the posted record based on the id submitted // *************** START TRANSACTION ************************* $db->transStart(); if ($delAssy->unPost('delete')) { // unpost the prior assembly $db->transCommit(); // if not successful rollback will already have been performed gen_add_audit_log(INV_LOG_ASSY . TEXT_DELETE, $delAssy->journal_rows[0]['sku'], $delAssy->journal_rows[0]['qty']); if (DEBUG) { $messageStack->write_debug(); } gen_redirect(html_href_link(FILENAME_DEFAULT, gen_get_all_get_params(array('action')), 'SSL')); break; // *************** END TRANSACTION ************************* } } $messageStack->add(GL_ERROR_NO_DELETE, 'error'); $cInfo = new objectInfo($_POST); break;
function Post($action = 'insert', $skip_balance = false) { global $messageStack; $this->first_period = $this->period; $this->repost_ids = array(); if ($action == 'edit') { // unPost the original entry and remove from db $messageStack->debug("\n\n unPosting as part of edit journal main id = " . $this->id); $old_gl_entry = new journal($this->id); // read in the original journal entry $this->first_period = min($old_gl_entry->period, $this->first_period); if (!$old_gl_entry->unPost('edit', true)) { return false; } // unpost it $this->affected_accounts = gen_array_key_merge($this->affected_accounts, $old_gl_entry->affected_accounts); if (sizeof($old_gl_entry->repost_ids) > 0) { // rePost any journal entries unPosted to rollback COGS calculation (if edit) $messageStack->debug("\n First level unPost returned re-post_ids to be unPosted next = " . arr2string($old_gl_entry->repost_ids)); while (true) { $id = array_shift($old_gl_entry->repost_ids); $messageStack->debug("\n\n unPosting re-post Journal main id = " . $id); if (!$id) { break; } // no more to unPost, exit loop if (in_array($id, $this->repost_ids)) { continue; } // already has been unposted, skip $this->repost_ids[$id] = $id; $this->unPost_entry[$id] = new journal($id); if (!$this->unPost_entry[$id]->unPost('edit', true)) { return false; } // add the new repost_ids to the arrays, one for now, one for re-post loop later $old_gl_entry->repost_ids += $this->unPost_entry[$id]->repost_ids; $messageStack->debug("\n\n unPosting array now looks like = " . arr2string($old_gl_entry->repost_ids)); $messageStack->debug("\n re-Posting array will re-post = " . arr2string($this->repost_ids)); $this->unPost_entry[$id]->repost_ids = array(); // clear nested unPost to zero, so it doesn't re-post } } } // post journal main record $messageStack->debug("\n\nPosting Journal main ... id = " . $this->id . " and action = " . $action . " and journal_id = " . $this->journal_id); $messageStack->debug("\n main_array = " . arr2string($this->journal_main_array)); db_perform(TABLE_JOURNAL_MAIN, $this->journal_main_array, 'insert'); $this->id = db_insert_id(); // post journal rows $messageStack->debug("\n\nPosting Journal rows ..."); for ($i = 0; $i < count($this->journal_rows); $i++) { $messageStack->debug("\n journal_rows = " . arr2string($this->journal_rows[$i])); $this->journal_rows[$i]['ref_id'] = $this->id; // link the rows to the journal main id db_perform(TABLE_JOURNAL_ITEM, $this->journal_rows[$i], 'insert'); $this->journal_rows[$i]['id'] = db_insert_id(); } $messageStack->debug("\n\nStarting auxilliary post functions ..."); // Inventory needs to be posted first because function may add additional journal rows for COGS if (!$this->Post_inventory()) { return false; } if (!$this->Post_chart_balances()) { return false; } // post the chart of account values if (!$this->Post_account_sales_purchases()) { return false; } if (sizeof($this->repost_ids) > 0) { // rePost any journal entries unPosted to rollback COGS calculation (if edit) $messageStack->debug("\nStarting to Post re-post_ids to be Posted = " . arr2string($this->repost_ids)); while ($id = array_shift($this->repost_ids)) { $messageStack->debug("\n\nRe-posting as part of Post - Journal main id = " . $id); $gl_entry = $this->unPost_entry[$id]; if (!is_object($gl_entry)) { // for this case, the affected journal objects have not been created $gl_entry = new journal($id); $gl_entry->remove_cogs_rows(); // they will be regenerated during the re-post if (!$gl_entry->Post('edit', true)) { return false; } } else { $gl_entry->remove_cogs_rows(); // they will be regenerated during the re-post if (!$gl_entry->Post('insert', true)) { return false; } } $this->affected_accounts = gen_array_key_merge($this->affected_accounts, $gl_entry->affected_accounts); $this->first_period = min($gl_entry->period, $this->first_period); } } if (!$skip_balance) { if (!$this->update_chart_history_periods($this->first_period)) { return false; } } if (!$this->check_for_closed_po_so('Post')) { return false; } $messageStack->debug("\n*************** end Posting Journal ******************* id = " . $this->id . "\n"); return true; }
} else { $delGL = new journal(); $delGL->journal($glEntry->id); // load the posted record based on the id submitted $recur_id = db_prepare_input($_POST['recur_id']); $recur_frequency = db_prepare_input($_POST['recur_frequency']); // *************** START TRANSACTION ************************* $db->transStart(); if ($recur_id > 0) { // will contain recur_id $affected_ids = $delGL->get_recur_ids($recur_id, $delGL->id); for ($i = 0; $i < count($affected_ids); $i++) { $delGL->id = $affected_ids[$i]['id']; $delGL->journal($delGL->id); // load the posted record based on the id submitted if (!$delGL->unPost('delete')) { $error = true; break; } // test for single post versus rolling into future posts, terminate loop if single post if (!$recur_frequency) { break; } } } else { if (!$delGL->unPost('delete')) { $error = true; } } if (!$error) { $db->transCommit();
function Post($action = 'insert', $skip_balance = false) { global $messageStack; if (!isset($this->id) || $this->id == '') { $this->id = 0; } $this->unpost_ids = $this->check_for_re_post(); if ($action == 'edit') { $orig_post = new journal($this->id); // read in the original journal entry to get post order $idx = substr($orig_post->post_date, 0, 10) . ':' . str_pad($this->id, 8, '0', STR_PAD_LEFT); $this->unpost_ids[$idx] = $this->id; } $idx = substr($this->post_date, 0, 10) . ':' . str_pad($this->id, 8, '0', STR_PAD_LEFT); $this->post_ids[$idx] = clone $this; // save variables for later to post $this->first_period = $this->period; // start unposting all affected records if (sizeof($this->unpost_ids) > 0) { krsort($this->unpost_ids); // unpost in reverse order $messageStack->debug("\nStarting to unPost reverse sorted id array = " . arr2string($this->unpost_ids)); while (true) { if (!($id = array_shift($this->unpost_ids))) { break; } // no more to unPost, exit loop $messageStack->debug("\n/********* unPosting journal_main id = {$id}"); $unPost = new journal($id, false); if (!isset($unPost->id)) { continue; } // already has been unposted, skip if ($this->id != $id) { // re-queue to post if not current entry $idx = substr($unPost->post_date, 0, 10) . ':' . str_pad($id, 8, '0', STR_PAD_LEFT); $this->post_ids[$idx] = clone $unPost; } if (!$unPost->unPost('edit', true)) { return false; } $this->first_period = min($this->first_period, $unPost->period); $this->affected_accounts = gen_array_key_merge($this->affected_accounts, $unPost->affected_accounts); // add the new post_ids to the arrays, one for now, one for re-post loop later $this->unpost_ids += $unPost->unpost_ids; $messageStack->debug("\n unPosting array now looks like = " . arr2string($this->unpost_ids)); $messageStack->debug("\n re-Posting array keys now looks like = " . arr2string(array_keys($this->post_ids))); // $unPost->post_ids = array(); // clear nested unPost to zero, so it doesn't re-post } } // Post entry and rePost any journal entries unPosted ksort($this->post_ids); // re-post in post_date/record_id ascending order $messageStack->debug("\nStarting to Post indexes to be Posted = " . arr2string(array_keys($this->post_ids))); while ($glEntry = array_shift($this->post_ids)) { $messageStack->debug("\n/********* Posting Journal main ... id = {$glEntry->id} and journal_id = {$glEntry->journal_id}"); $this->repost_ids = $glEntry->check_for_re_post(); $glEntry->remove_cogs_rows(); // they will be regenerated during the post $messageStack->debug("\n journal_main array = " . arr2string($glEntry->journal_main_array)); db_perform(TABLE_JOURNAL_MAIN, $glEntry->journal_main_array, 'insert'); if (!$glEntry->id) { $this->id = $glEntry->id = db_insert_id(); } // post journal rows $messageStack->debug("\n Posting Journal rows ..."); for ($i = 0; $i < count($glEntry->journal_rows); $i++) { $messageStack->debug("\n journal_rows = " . arr2string($glEntry->journal_rows[$i])); $glEntry->journal_rows[$i]['ref_id'] = $glEntry->id; // link the rows to the journal main id db_perform(TABLE_JOURNAL_ITEM, $glEntry->journal_rows[$i], 'insert'); if (!$glEntry->journal_rows[$i]['id']) { $glEntry->journal_rows[$i]['id'] = db_insert_id(); } } $messageStack->debug("\nStarting auxilliary post functions ..."); // Inventory needs to be posted first because function may add additional journal rows for COGS if (!$glEntry->Post_inventory()) { return false; } if (!$glEntry->Post_chart_balances()) { return false; } if (!$glEntry->Post_account_sales_purchases()) { return false; } $this->affected_accounts = gen_array_key_merge($this->affected_accounts, $glEntry->affected_accounts); $this->first_period = min($this->first_period, $glEntry->period); if (sizeof($this->repost_ids) > 0) { ksort($this->repost_ids); // repost by post date $messageStack->debug("\nStarting to rePost entries queued from first pass, sorted id array = " . arr2string($this->repost_ids)); while (true) { if (!($id = array_shift($this->repost_ids))) { break; } // no more to unPost, exit loop $messageStack->debug("\n/********* rePosting journal_main id = {$id}"); if ($this->id == $id) { continue; } // don't repost current post $rePost = new journal($id, false); if (!$rePost->Post('edit', true)) { return false; } // add the new post_ids to the arrays, one for now, one for re-post loop later $this->repost_ids += $rePost->repost_ids; $messageStack->debug("\n rePosting array now looks like = " . arr2string($this->repost_ids)); } } if (!$glEntry->check_for_closed_po_so('Post')) { return false; } $messageStack->debug("\n*************** end Posting Journal ******************* id = {$this->id}\n"); } if (!$skip_balance) { if (!$this->update_chart_history_periods($this->first_period)) { return false; } } return true; }