public function process() { switch ($_REQUEST['_process']) { case 'quick_save_finance': if (isset($_REQUEST['link_go']) && $_REQUEST['link_go'] == 'go') { module_finance::handle_link_transactions(); } else { // check for date / name at least. $date = trim($_REQUEST['transaction_date']); $name = trim($_REQUEST['name']); if (!$date || !$name) { redirect_browser(module_finance::link_open(false)); } $credit = trim($_REQUEST['credit']); $debit = trim($_REQUEST['debit']); if ($credit > 0) { $_POST['type'] = 'i'; $_POST['amount'] = $credit; } else { $_POST['type'] = 'e'; $_POST['amount'] = $debit; } } case 'save_finance': if (isset($_REQUEST['butt_del'])) { $this->delete($_REQUEST['finance_id']); redirect_browser(self::link_open(false)); } if (isset($_REQUEST['butt_unlink'])) { // unlink this finance_id from other finance_ids. $sql = "UPDATE `" . _DB_PREFIX . "finance` SET parent_finance_id = 0 WHERE parent_finance_id = '" . (int) $_REQUEST['finance_id'] . "'"; query($sql); $sql = "UPDATE `" . _DB_PREFIX . "invoice_payment` SET parent_finance_id = 0 WHERE parent_finance_id = '" . (int) $_REQUEST['finance_id'] . "'"; query($sql); redirect_browser(self::link_open(false)); } $temp_data = $this->get_finance($_REQUEST['finance_id']); $data = $_POST + $temp_data; // save the finance categories and account. $account_id = $_REQUEST['finance_account_id']; if ((string) (int) $account_id != (string) $account_id && strlen($account_id) > 2) { // we have a new account to create. $account_id = update_insert('finance_account_id', 'new', 'finance_account', array('name' => $account_id)); } $data['finance_account_id'] = $account_id; $finance_id = update_insert('finance_id', isset($_REQUEST['finance_id']) ? $_REQUEST['finance_id'] : 'new', 'finance', $data); module_extra::save_extras('finance', 'finance_id', $finance_id); if (!isset($data['tax_ids']) && isset($data['taxes']) && is_array($data['taxes'])) { // default data when saving a new invoice payment to finance area $data['tax_ids'] = array(); $data['tax_names'] = array(); $data['tax_percents'] = array(); $data['tax_increment_checkbox'] = 0; foreach ($data['taxes'] as $tax) { $data['tax_ids'][] = false; $data['tax_names'][] = $tax['name']; $data['tax_percents'][] = $tax['percent']; $data['tax_amount'][] = $tax['amount']; if ($tax['increment']) { $data['tax_increment_checkbox'] = 1; } } } // save the finance tax rates (copied from invoice.php) if (isset($data['tax_ids']) && isset($data['tax_names']) && $data['tax_percents']) { $existing_taxes = get_multiple('finance_tax', array('finance_id' => $finance_id), 'finance_tax_id', 'exact', 'order'); $order = 1; foreach ($data['tax_ids'] as $key => $val) { if ((int) $val > 0 && isset($existing_taxes[$val])) { // this means we are trying to update an existing record on the finance_tax table, we confirm this id matches this finance. $finance_tax_id = $val; unset($existing_taxes[$finance_tax_id]); // so we know which ones to remove from the end. } else { $finance_tax_id = false; // create new record } $finance_tax_data = array('finance_id' => $finance_id, 'percent' => isset($data['tax_percents'][$key]) ? $data['tax_percents'][$key] : 0, 'amount' => isset($data['tax_amount'][$key]) ? $data['tax_amount'][$key] : 0, 'name' => isset($data['tax_names'][$key]) ? $data['tax_names'][$key] : 'TAX', 'order' => $order++, 'increment' => isset($data['tax_increment_checkbox']) && $data['tax_increment_checkbox'] ? 1 : 0); $finance_tax_id = update_insert('finance_tax_id', $finance_tax_id, 'finance_tax', $finance_tax_data); } foreach ($existing_taxes as $existing_tax) { delete_from_db('finance_tax', array('finance_id', 'finance_tax_id'), array($finance_id, $existing_tax['finance_tax_id'])); } } $category_ids = isset($_REQUEST['finance_category_id']) && is_array($_REQUEST['finance_category_id']) ? $_REQUEST['finance_category_id'] : array(); $sql = "DELETE FROM `" . _DB_PREFIX . "finance_category_rel` WHERE finance_id = {$finance_id}"; query($sql); foreach ($category_ids as $category_id) { $category_id = (int) $category_id; if ($category_id <= 0) { continue; } $sql = "REPLACE INTO `" . _DB_PREFIX . "finance_category_rel` SET finance_id = {$finance_id}, finance_category_id = {$category_id}"; query($sql); } if (isset($_REQUEST['finance_category_new']) && strlen(trim($_REQUEST['finance_category_new'])) > 0) { $category_name = trim($_REQUEST['finance_category_new']); $category_id = update_insert('finance_category_id', 'new', 'finance_category', array('name' => $category_name)); if (isset($_REQUEST['finance_category_new_checked'])) { $sql = "REPLACE INTO `" . _DB_PREFIX . "finance_category_rel` SET finance_id = {$finance_id}, finance_category_id = {$category_id}"; query($sql); } } if (isset($_REQUEST['invoice_payment_id']) && (int) $_REQUEST['invoice_payment_id'] > 0) { // link this as a child invoice payment to this one. update_insert('invoice_payment_id', $_REQUEST['invoice_payment_id'], 'invoice_payment', array('parent_finance_id' => $finance_id)); } if (isset($_REQUEST['finance_recurring_id']) && (int) $_REQUEST['finance_recurring_id'] > 0) { // if we have set a custom "next recurring date" then we don't recalculate this date unless we are saving a new finance id. $recurring = self::get_recurring($_REQUEST['finance_recurring_id']); if (!(int) $_REQUEST['finance_id'] || !$recurring['next_due_date_custom']) { self::calculate_recurring_date((int) $_REQUEST['finance_recurring_id'], true); } // we also have to adjust the starting balance of our recurring amount by this amount. // just a little helpful feature. if (!(int) $_REQUEST['finance_id']) { $balance = module_config::c('finance_recurring_start_balance', 0); if ($balance != 0) { if ($data['type'] == 'e') { $balance -= $data['amount']; } else { if ($data['type'] == 'i') { $balance += $data['amount']; } } module_config::save_config('finance_recurring_start_balance', $balance); } } // redirect back to recurring listing. set_message('Recurring transaction saved successfully'); if (isset($_REQUEST['recurring_next']) && $_REQUEST['recurring_next']) { redirect_browser($_REQUEST['recurring_next']); } redirect_browser(self::link_open_recurring(false)); } set_message(_l('Transaction saved successfully: %s', module_finance::link_open($finance_id, true))); if (isset($_REQUEST['job_id']) && (int) $_REQUEST['job_id'] > 0) { redirect_browser(module_job::link_open((int) $_REQUEST['job_id'])); } if (isset($_REQUEST['butt_save_return'])) { if (isset($_REQUEST['_redirect']) && strlen($_REQUEST['_redirect'])) { redirect_browser($_REQUEST['_redirect']); } redirect_browser(self::link_open(false, false)); } if ($_REQUEST['_process'] == 'quick_save_finance') { redirect_browser(self::link_open(false, false)); } redirect_browser(self::link_open($finance_id, false)); break; case 'save_recurring': if (isset($_REQUEST['butt_del'])) { $this->delete_recurring($_REQUEST['finance_recurring_id']); redirect_browser(self::link_open_recurring(false)); } $data = $_POST; // save the finance categories and account. $account_id = $_REQUEST['finance_account_id']; if ((string) (int) $account_id != (string) $account_id && strlen($account_id) > 2) { // we have a new account to create. $account_id = update_insert('finance_account_id', 'new', 'finance_account', array('name' => $account_id)); } if (isset($_REQUEST['finance_recurring_id']) && (int) $_REQUEST['finance_recurring_id']) { $original_finance_recurring = self::get_recurring($_REQUEST['finance_recurring_id']); } else { $original_finance_recurring = array(); } $data['finance_account_id'] = $account_id; $finance_recurring_id = update_insert('finance_recurring_id', isset($_REQUEST['finance_recurring_id']) ? $_REQUEST['finance_recurring_id'] : 'new', 'finance_recurring', $data); if ((int) $finance_recurring_id > 0) { $category_ids = isset($_REQUEST['finance_category_id']) && is_array($_REQUEST['finance_category_id']) ? $_REQUEST['finance_category_id'] : array(); $sql = "DELETE FROM `" . _DB_PREFIX . "finance_recurring_catrel` WHERE finance_recurring_id = {$finance_recurring_id}"; query($sql); foreach ($category_ids as $category_id) { $category_id = (int) $category_id; if ($category_id <= 0) { continue; } $sql = "REPLACE INTO `" . _DB_PREFIX . "finance_recurring_catrel` SET finance_recurring_id = {$finance_recurring_id}, finance_category_id = {$category_id}"; query($sql); } if (isset($_REQUEST['finance_category_new']) && strlen(trim($_REQUEST['finance_category_new'])) > 0) { $category_name = trim($_REQUEST['finance_category_new']); $category_id = update_insert('finance_category_id', 'new', 'finance_category', array('name' => $category_name)); if (isset($_REQUEST['finance_category_new_checked'])) { $sql = "REPLACE INTO `" . _DB_PREFIX . "finance_recurring_catrel` SET finance_recurring_id = {$finance_recurring_id}, finance_category_id = {$category_id}"; query($sql); } } $calculated_next_date = self::calculate_recurring_date($finance_recurring_id); if (isset($data['set_next_due_date']) && $data['set_next_due_date']) { $next_date = input_date($data['set_next_due_date']); $next_due_date_real = module_finance::calculate_recurring_date($finance_recurring_id, true, false); if ($next_date != $next_due_date_real) { // we have accustom date. update_insert('finance_recurring_id', $finance_recurring_id, 'finance_recurring', array('next_due_date' => $next_date, 'next_due_date_custom' => 1)); } else { // date is the same. not doing a custom date any more update_insert('finance_recurring_id', $finance_recurring_id, 'finance_recurring', array('next_due_date' => $next_due_date_real, 'next_due_date_custom' => 0)); } } /* $finance_recurring = self::get_recurring($finance_recurring_id); if($finance_recurring['next_due_date_custom']){ $next_due_date_real = module_finance::calculate_recurring_date($finance_recurring_id,true,false); // unset the "custom" flag if we've picked the same date as what it should be. if($next_due_date_real == $finance_recurring['next_due_date']){ module_finance::calculate_recurring_date($finance_recurring_id,true,true); } }*/ } set_message('Recurring transaction saved successfully'); //redirect_browser(self::link_open($finance_id,false)); redirect_browser(self::link_open_recurring(false, false)); break; } }
echo '</span> '; } else { //echo print_date($recurring['next_due_date']); } } if ($show_record_button) { $days = ceil(($next_due - time()) / 86400); if (abs($days) == 0) { _e('(today)'); } else { _e(' (%s days)', $days); } } if ($recurring['next_due_date_custom']) { echo ' '; $next_due_date_real = module_finance::calculate_recurring_date($finance_recurring_id, true, false); if ($next_due_date_real != $recurring['next_due_date']) { echo '<em>'; _e('this is a custom date, it should be %s', print_date($next_due_date_real)); echo '</em>'; } } } ?> </td> </tr> <?php if ($show_record_button) { ?> <tr>