Example #1
0
         echo "<td>";
         echo convert_dc($row->entry_items_dc);
         echo " ";
         echo $row->entry_items_amount;
         echo "</td>";
     }
     echo "<td>";
     if (!$print_preview) {
         $reconciliation_date = array('name' => 'reconciliation_date[' . $row->entry_items_id . ']', 'id' => 'reconciliation_date', 'maxlength' => '11', 'size' => '11', 'value' => '');
         if ($row->entry_items_reconciliation_date) {
             $reconciliation_date['value'] = date_mysql_to_php($row->entry_items_reconciliation_date);
         }
         echo form_input_date_restrict($reconciliation_date);
     } else {
         if ($row->entry_items_reconciliation_date) {
             echo date_mysql_to_php($row->entry_items_reconciliation_date);
         } else {
             echo "-";
         }
     }
     echo "</td>";
     echo "</tr>";
     $odd_even = $odd_even == "odd" ? "even" : "odd";
 }
 echo "</table>";
 if (!$print_preview) {
     echo "<p>";
     echo form_submit('submit', 'Update');
     echo "</p>";
     echo form_close();
 }
Example #2
0
 function download($statement, $id = NULL)
 {
     /********************** TRIAL BALANCE *************************/
     if ($statement == "trialbalance") {
         $this->load->model('Ledger_model');
         $all_ledgers = $this->Ledger_model->get_all_ledgers();
         $counter = 0;
         $trialbalance = array();
         $temp_dr_total = 0;
         $temp_cr_total = 0;
         $trialbalance[$counter] = array("TRIAL BALANCE", "", "", "", "", "", "", "", "");
         $counter++;
         $trialbalance[$counter] = array("FY " . date_mysql_to_php($this->config->item('account_fy_start')) . " - " . date_mysql_to_php($this->config->item('account_fy_end')), "", "", "", "", "", "", "", "");
         $counter++;
         $trialbalance[$counter][0] = "Ledger";
         $trialbalance[$counter][1] = "";
         $trialbalance[$counter][2] = "Opening";
         $trialbalance[$counter][3] = "";
         $trialbalance[$counter][4] = "Closing";
         $trialbalance[$counter][5] = "";
         $trialbalance[$counter][6] = "Dr Total";
         $trialbalance[$counter][7] = "";
         $trialbalance[$counter][8] = "Cr Total";
         $counter++;
         foreach ($all_ledgers as $ledger_id => $ledger_name) {
             if ($ledger_id == 0) {
                 continue;
             }
             $trialbalance[$counter][0] = $ledger_name;
             list($opbal_amount, $opbal_type) = $this->Ledger_model->get_op_balance($ledger_id);
             if (float_ops($opbal_amount, 0, '==')) {
                 $trialbalance[$counter][1] = "";
                 $trialbalance[$counter][2] = 0;
             } else {
                 $trialbalance[$counter][1] = convert_dc($opbal_type);
                 $trialbalance[$counter][2] = $opbal_amount;
             }
             $clbal_amount = $this->Ledger_model->get_ledger_balance($ledger_id);
             if (float_ops($clbal_amount, 0, '==')) {
                 $trialbalance[$counter][3] = "";
                 $trialbalance[$counter][4] = 0;
             } else {
                 if (float_ops($clbal_amount, 0, '<')) {
                     $trialbalance[$counter][3] = "Cr";
                     $trialbalance[$counter][4] = convert_cur(-$clbal_amount);
                 } else {
                     $trialbalance[$counter][3] = "Dr";
                     $trialbalance[$counter][4] = convert_cur($clbal_amount);
                 }
             }
             $dr_total = $this->Ledger_model->get_dr_total($ledger_id);
             if ($dr_total) {
                 $trialbalance[$counter][5] = "Dr";
                 $trialbalance[$counter][6] = convert_cur($dr_total);
                 $temp_dr_total = float_ops($temp_dr_total, $dr_total, '+');
             } else {
                 $trialbalance[$counter][5] = "";
                 $trialbalance[$counter][6] = 0;
             }
             $cr_total = $this->Ledger_model->get_cr_total($ledger_id);
             if ($cr_total) {
                 $trialbalance[$counter][7] = "Cr";
                 $trialbalance[$counter][8] = convert_cur($cr_total);
                 $temp_cr_total = float_ops($temp_cr_total, $cr_total, '+');
             } else {
                 $trialbalance[$counter][7] = "";
                 $trialbalance[$counter][8] = 0;
             }
             $counter++;
         }
         $trialbalance[$counter][0] = "";
         $trialbalance[$counter][1] = "";
         $trialbalance[$counter][2] = "";
         $trialbalance[$counter][3] = "";
         $trialbalance[$counter][4] = "";
         $trialbalance[$counter][5] = "";
         $trialbalance[$counter][6] = "";
         $trialbalance[$counter][7] = "";
         $trialbalance[$counter][8] = "";
         $counter++;
         $trialbalance[$counter][0] = "Total";
         $trialbalance[$counter][1] = "";
         $trialbalance[$counter][2] = "";
         $trialbalance[$counter][3] = "";
         $trialbalance[$counter][4] = "";
         $trialbalance[$counter][5] = "Dr";
         $trialbalance[$counter][6] = convert_cur($temp_dr_total);
         $trialbalance[$counter][7] = "Cr";
         $trialbalance[$counter][8] = convert_cur($temp_cr_total);
         $this->load->helper('csv');
         echo array_to_csv($trialbalance, "trialbalance.csv");
         return;
     }
     /********************** LEDGER STATEMENT **********************/
     if ($statement == "ledgerst") {
         $ledger_id = (int) $this->uri->segment(4);
         if ($ledger_id < 1) {
             return;
         }
         $this->load->model('Ledger_model');
         $cur_balance = 0;
         $counter = 0;
         $ledgerst = array();
         $ledgerst[$counter] = array("", "", "LEDGER STATEMENT FOR " . strtoupper($this->Ledger_model->get_name($ledger_id)), "", "", "", "", "", "", "", "");
         $counter++;
         $ledgerst[$counter] = array("", "", "FY " . date_mysql_to_php($this->config->item('account_fy_start')) . " - " . date_mysql_to_php($this->config->item('account_fy_end')), "", "", "", "", "", "", "", "");
         $counter++;
         $ledgerst[$counter][0] = "Date";
         $ledgerst[$counter][1] = "Number";
         $ledgerst[$counter][2] = "Ledger Name";
         $ledgerst[$counter][3] = "Narration";
         $ledgerst[$counter][4] = "Type";
         $ledgerst[$counter][5] = "";
         $ledgerst[$counter][6] = "Dr Amount";
         $ledgerst[$counter][7] = "";
         $ledgerst[$counter][8] = "Cr Amount";
         $ledgerst[$counter][9] = "";
         $ledgerst[$counter][10] = "Balance";
         $counter++;
         /* Opening Balance */
         list($opbalance, $optype) = $this->Ledger_model->get_op_balance($ledger_id);
         $ledgerst[$counter] = array("Opening Balance", "", "", "", "", "", "", "", "", convert_dc($optype), $opbalance);
         if ($optype == "D") {
             $cur_balance = float_ops($cur_balance, $opbalance, '+');
         } else {
             $cur_balance = float_ops($cur_balance, $opbalance, '-');
         }
         $counter++;
         $this->db->select('entries.id as entries_id, entries.number as entries_number, entries.date as entries_date, entries.narration as entries_narration, entries.entry_type as entries_entry_type, entry_items.amount as entry_items_amount, entry_items.dc as entry_items_dc');
         $this->db->from('entries')->join('entry_items', 'entries.id = entry_items.entry_id')->where('entry_items.ledger_id', $ledger_id)->order_by('entries.date', 'asc')->order_by('entries.number', 'asc');
         $ledgerst_q = $this->db->get();
         foreach ($ledgerst_q->result() as $row) {
             /* Entry Type */
             $current_entry_type = entry_type_info($row->entries_entry_type);
             $ledgerst[$counter][0] = date_mysql_to_php($row->entries_date);
             $ledgerst[$counter][1] = full_entry_number($row->entries_entry_type, $row->entries_number);
             /* Opposite entry name */
             $ledgerst[$counter][2] = $this->Ledger_model->get_opp_ledger_name($row->entries_id, $row->entries_entry_type, $row->entry_items_dc, 'text');
             $ledgerst[$counter][3] = $row->entries_narration;
             $ledgerst[$counter][4] = $current_entry_type['name'];
             if ($row->entry_items_dc == "D") {
                 $cur_balance = float_ops($cur_balance, $row->entry_items_amount, '+');
                 $ledgerst[$counter][5] = convert_dc($row->entry_items_dc);
                 $ledgerst[$counter][6] = $row->entry_items_amount;
                 $ledgerst[$counter][7] = "";
                 $ledgerst[$counter][8] = "";
             } else {
                 $cur_balance = float_ops($cur_balance, $row->entry_items_amount, '-');
                 $ledgerst[$counter][5] = "";
                 $ledgerst[$counter][6] = "";
                 $ledgerst[$counter][7] = convert_dc($row->entry_items_dc);
                 $ledgerst[$counter][8] = $row->entry_items_amount;
             }
             if (float_ops($cur_balance, 0, '==')) {
                 $ledgerst[$counter][9] = "";
                 $ledgerst[$counter][10] = 0;
             } else {
                 if (float_ops($cur_balance, 0, '<')) {
                     $ledgerst[$counter][9] = "Cr";
                     $ledgerst[$counter][10] = convert_cur(-$cur_balance);
                 } else {
                     $ledgerst[$counter][9] = "Dr";
                     $ledgerst[$counter][10] = convert_cur($cur_balance);
                 }
             }
             $counter++;
         }
         $ledgerst[$counter][0] = "Closing Balance";
         $ledgerst[$counter][1] = "";
         $ledgerst[$counter][2] = "";
         $ledgerst[$counter][3] = "";
         $ledgerst[$counter][4] = "";
         $ledgerst[$counter][5] = "";
         $ledgerst[$counter][6] = "";
         $ledgerst[$counter][7] = "";
         $ledgerst[$counter][8] = "";
         if (float_ops($cur_balance, 0, '<')) {
             $ledgerst[$counter][9] = "Cr";
             $ledgerst[$counter][10] = convert_cur(-$cur_balance);
         } else {
             $ledgerst[$counter][9] = "Dr";
             $ledgerst[$counter][10] = convert_cur($cur_balance);
         }
         $counter++;
         $ledgerst[$counter] = array("", "", "", "", "", "", "", "", "", "", "");
         $counter++;
         /* Final Opening and Closing Balance */
         $clbalance = $this->Ledger_model->get_ledger_balance($ledger_id);
         $ledgerst[$counter] = array("Opening Balance", convert_dc($optype), $opbalance, "", "", "", "", "", "", "", "");
         $counter++;
         if (float_ops($clbalance, 0, '==')) {
             $ledgerst[$counter] = array("Closing Balance", "", 0, "", "", "", "", "", "", "", "");
         } else {
             if (float_ops($clbalance, 0, '<')) {
                 $ledgerst[$counter] = array("Closing Balance", "Cr", convert_cur(-$clbalance), "", "", "", "", "", "", "", "");
             } else {
                 $ledgerst[$counter] = array("Closing Balance", "Dr", convert_cur($clbalance), "", "", "", "", "", "", "", "");
             }
         }
         $this->load->helper('csv');
         echo array_to_csv($ledgerst, "ledgerst.csv");
         return;
     }
     /********************** RECONCILIATION ************************/
     if ($statement == "reconciliation") {
         $ledger_id = (int) $this->uri->segment(4);
         $reconciliation_type = $this->uri->segment(5);
         if ($ledger_id < 1) {
             return;
         }
         if (!($reconciliation_type == 'all' or $reconciliation_type == 'pending')) {
             return;
         }
         $this->load->model('Ledger_model');
         $cur_balance = 0;
         $counter = 0;
         $ledgerst = array();
         $ledgerst[$counter] = array("", "", "RECONCILIATION STATEMENT FOR " . strtoupper($this->Ledger_model->get_name($ledger_id)), "", "", "", "", "", "", "");
         $counter++;
         $ledgerst[$counter] = array("", "", "FY " . date_mysql_to_php($this->config->item('account_fy_start')) . " - " . date_mysql_to_php($this->config->item('account_fy_end')), "", "", "", "", "", "", "");
         $counter++;
         $ledgerst[$counter][0] = "Date";
         $ledgerst[$counter][1] = "Number";
         $ledgerst[$counter][2] = "Ledger Name";
         $ledgerst[$counter][3] = "Narration";
         $ledgerst[$counter][4] = "Type";
         $ledgerst[$counter][5] = "";
         $ledgerst[$counter][6] = "Dr Amount";
         $ledgerst[$counter][7] = "";
         $ledgerst[$counter][8] = "Cr Amount";
         $ledgerst[$counter][9] = "Reconciliation Date";
         $counter++;
         /* Opening Balance */
         list($opbalance, $optype) = $this->Ledger_model->get_op_balance($ledger_id);
         $this->db->select('entries.id as entries_id, entries.number as entries_number, entries.date as entries_date, entries.narration as entries_narration, entries.entry_type as entries_entry_type, entry_items.amount as entry_items_amount, entry_items.dc as entry_items_dc, entry_items.reconciliation_date as lreconciliation');
         if ($reconciliation_type == 'all') {
             $this->db->from('entries')->join('entry_items', 'entries.id = entry_items.entry_id')->where('entry_items.ledger_id', $ledger_id)->order_by('entries.date', 'asc')->order_by('entries.number', 'asc');
         } else {
             $this->db->from('entries')->join('entry_items', 'entries.id = entry_items.entry_id')->where('entry_items.ledger_id', $ledger_id)->where('entry_items.reconciliation_date', NULL)->order_by('entries.date', 'asc')->order_by('entries.number', 'asc');
         }
         $ledgerst_q = $this->db->get();
         foreach ($ledgerst_q->result() as $row) {
             /* Entry Type */
             $current_entry_type = entry_type_info($row->entries_entry_type);
             $ledgerst[$counter][0] = date_mysql_to_php($row->entries_date);
             $ledgerst[$counter][1] = full_entry_number($row->entries_entry_type, $row->entries_number);
             /* Opposite entry name */
             $ledgerst[$counter][2] = $this->Ledger_model->get_opp_ledger_name($row->entries_id, $row->entries_entry_type, $row->entry_items_dc, 'text');
             $ledgerst[$counter][3] = $row->entries_narration;
             $ledgerst[$counter][4] = $current_entry_type['name'];
             if ($row->entry_items_dc == "D") {
                 $ledgerst[$counter][5] = convert_dc($row->entry_items_dc);
                 $ledgerst[$counter][6] = $row->entry_items_amount;
                 $ledgerst[$counter][7] = "";
                 $ledgerst[$counter][8] = "";
             } else {
                 $ledgerst[$counter][5] = "";
                 $ledgerst[$counter][6] = "";
                 $ledgerst[$counter][7] = convert_dc($row->entry_items_dc);
                 $ledgerst[$counter][8] = $row->entry_items_amount;
             }
             if ($row->lreconciliation) {
                 $ledgerst[$counter][9] = date_mysql_to_php($row->lreconciliation);
             } else {
                 $ledgerst[$counter][9] = "";
             }
             $counter++;
         }
         $counter++;
         $ledgerst[$counter] = array("", "", "", "", "", "", "", "", "", "");
         $counter++;
         /* Final Opening and Closing Balance */
         $clbalance = $this->Ledger_model->get_ledger_balance($ledger_id);
         $ledgerst[$counter] = array("Opening Balance", convert_dc($optype), $opbalance, "", "", "", "", "", "", "");
         $counter++;
         if (float_ops($clbalance, 0, '==')) {
             $ledgerst[$counter] = array("Closing Balance", "", 0, "", "", "", "", "", "", "");
         } else {
             if (float_ops($clbalance, 0, '<')) {
                 $ledgerst[$counter] = array("Closing Balance", "Cr", convert_cur(-$clbalance), "", "", "", "", "", "", "");
             } else {
                 $ledgerst[$counter] = array("Closing Balance", "Dr", convert_cur($clbalance), "", "", "", "", "", "", "");
             }
         }
         /************* Final Reconciliation Balance ***********/
         /* Reconciliation Balance - Dr */
         $this->db->select_sum('amount', 'drtotal')->from('entry_items')->join('entries', 'entries.id = entry_items.entry_id')->where('entry_items.ledger_id', $ledger_id)->where('entry_items.dc', 'D')->where('entry_items.reconciliation_date IS NOT NULL');
         $dr_total_q = $this->db->get();
         if ($dr_total = $dr_total_q->row()) {
             $reconciliation_dr_total = $dr_total->drtotal;
         } else {
             $reconciliation_dr_total = 0;
         }
         /* Reconciliation Balance - Cr */
         $this->db->select_sum('amount', 'crtotal')->from('entry_items')->join('entries', 'entries.id = entry_items.entry_id')->where('entry_items.ledger_id', $ledger_id)->where('entry_items.dc', 'C')->where('entry_items.reconciliation_date IS NOT NULL');
         $cr_total_q = $this->db->get();
         if ($cr_total = $cr_total_q->row()) {
             $reconciliation_cr_total = $cr_total->crtotal;
         } else {
             $reconciliation_cr_total = 0;
         }
         $reconciliation_total = float_ops($reconciliation_dr_total, $reconciliation_cr_total, '-');
         $reconciliation_pending = float_ops($clbalance, $reconciliation_total, '-');
         $counter++;
         if (float_ops($reconciliation_pending, 0, '==')) {
             $ledgerst[$counter] = array("Reconciliation Pending", "", 0, "", "", "", "", "", "", "");
         } else {
             if (float_ops($reconciliation_pending, 0, '<')) {
                 $ledgerst[$counter] = array("Reconciliation Pending", "Cr", convert_cur(-$reconciliation_pending), "", "", "", "", "", "", "");
             } else {
                 $ledgerst[$counter] = array("Reconciliation Pending", "Dr", convert_cur($reconciliation_pending), "", "", "", "", "", "", "");
             }
         }
         $counter++;
         if (float_ops($reconciliation_total, 0, '==')) {
             $ledgerst[$counter] = array("Reconciliation Total", "", 0, "", "", "", "", "", "", "");
         } else {
             if (float_ops($reconciliation_total, 0, '<')) {
                 $ledgerst[$counter] = array("Reconciliation Total", "Cr", convert_cur(-$reconciliation_total), "", "", "", "", "", "", "");
             } else {
                 $ledgerst[$counter] = array("Reconciliation Total", "Dr", convert_cur($reconciliation_total), "", "", "", "", "", "", "");
             }
         }
         $this->load->helper('csv');
         echo array_to_csv($ledgerst, "reconciliation.csv");
         return;
     }
     /************************ BALANCE SHEET ***********************/
     if ($statement == "balancesheet") {
         $this->load->library('accountlist');
         $this->load->model('Ledger_model');
         $liability = new Accountlist();
         $liability->init(2);
         $liability_array = $liability->build_array();
         $liability_depth = Accountlist::$max_depth;
         $liability_total = -$liability->total;
         Accountlist::reset_max_depth();
         $asset = new Accountlist();
         $asset->init(1);
         $asset_array = $asset->build_array();
         $asset_depth = Accountlist::$max_depth;
         $asset_total = $asset->total;
         $liability->to_csv($liability_array);
         Accountlist::add_blank_csv();
         $asset->to_csv($asset_array);
         $income = new Accountlist();
         $income->init(3);
         $expense = new Accountlist();
         $expense->init(4);
         $income_total = -$income->total;
         $expense_total = $expense->total;
         $pandl = float_ops($income_total, $expense_total, '-');
         $diffop = $this->Ledger_model->get_diff_op_balance();
         Accountlist::add_blank_csv();
         /* Liability side */
         $total = $liability_total;
         Accountlist::add_row_csv(array("Liabilities and Owners Equity Total", convert_cur($liability_total)));
         /* If Profit then Liability side, If Loss then Asset side */
         if (float_ops($pandl, 0, '!=')) {
             if (float_ops($pandl, 0, '>')) {
                 $total = float_ops($total, $pandl, '+');
                 Accountlist::add_row_csv(array("Profit & Loss account (Net Profit)", convert_cur($pandl)));
             }
         }
         /* If Op balance Dr then Liability side, If Op balance Cr then Asset side */
         if (float_ops($diffop, 0, '!=')) {
             if (float_ops($diffop, 0, '>')) {
                 $total = float_ops($total, $diffop, '+');
                 Accountlist::add_row_csv(array("Diff in O/P Balance", "Dr " . convert_cur($diffop)));
             }
         }
         Accountlist::add_row_csv(array("Total - Liabilities and Owners Equity", convert_cur($total)));
         /* Asset side */
         $total = $asset_total;
         Accountlist::add_row_csv(array("Asset Total", convert_cur($asset_total)));
         /* If Profit then Liability side, If Loss then Asset side */
         if (float_ops($pandl, 0, '!=')) {
             if (float_ops($pandl, 0, '<')) {
                 $total = float_ops($total, -$pandl, '+');
                 Accountlist::add_row_csv(array("Profit & Loss account (Net Loss)", convert_cur(-$pandl)));
             }
         }
         /* If Op balance Dr then Liability side, If Op balance Cr then Asset side */
         if (float_ops($diffop, 0, '!=')) {
             if (float_ops($diffop, 0, '<')) {
                 $total = float_ops($total, -$diffop, '+');
                 Accountlist::add_row_csv(array("Diff in O/P Balance", "Cr " . convert_cur(-$diffop)));
             }
         }
         Accountlist::add_row_csv(array("Total - Assets", convert_cur($total)));
         $balancesheet = Accountlist::get_csv();
         $this->load->helper('csv');
         echo array_to_csv($balancesheet, "balancesheet.csv");
         return;
     }
     /********************** PROFIT AND LOSS ***********************/
     if ($statement == "profitandloss") {
         $this->load->library('accountlist');
         $this->load->model('Ledger_model');
         /***************** GROSS CALCULATION ******************/
         /* Gross P/L : Expenses */
         $gross_expense_total = 0;
         $this->db->from('groups')->where('parent_id', 4)->where('affects_gross', 1);
         $gross_expense_list_q = $this->db->get();
         foreach ($gross_expense_list_q->result() as $row) {
             $gross_expense = new Accountlist();
             $gross_expense->init($row->id);
             $gross_expense_total = float_ops($gross_expense_total, $gross_expense->total, '+');
             $gross_exp_array = $gross_expense->build_array();
             $gross_expense->to_csv($gross_exp_array);
         }
         Accountlist::add_blank_csv();
         /* Gross P/L : Incomes */
         $gross_income_total = 0;
         $this->db->from('groups')->where('parent_id', 3)->where('affects_gross', 1);
         $gross_income_list_q = $this->db->get();
         foreach ($gross_income_list_q->result() as $row) {
             $gross_income = new Accountlist();
             $gross_income->init($row->id);
             $gross_income_total = float_ops($gross_income_total, $gross_income->total, '+');
             $gross_inc_array = $gross_income->build_array();
             $gross_income->to_csv($gross_inc_array);
         }
         Accountlist::add_blank_csv();
         Accountlist::add_blank_csv();
         /* Converting to positive value since Cr */
         $gross_income_total = -$gross_income_total;
         /* Calculating Gross P/L */
         $grosspl = float_ops($gross_income_total, $gross_expense_total, '-');
         /* Showing Gross P/L : Expenses */
         $grosstotal = $gross_expense_total;
         Accountlist::add_row_csv(array("Total Gross Expenses", convert_cur($gross_expense_total)));
         if (float_ops($grosspl, 0, '>')) {
             $grosstotal = float_ops($grosstotal, $grosspl, '+');
             Accountlist::add_row_csv(array("Gross Profit C/O", convert_cur($grosspl)));
         }
         Accountlist::add_row_csv(array("Total Expenses - Gross", convert_cur($grosstotal)));
         /* Showing Gross P/L : Incomes  */
         $grosstotal = $gross_income_total;
         Accountlist::add_row_csv(array("Total Gross Incomes", convert_cur($gross_income_total)));
         if (float_ops($grosspl, 0, '>')) {
         } else {
             if (float_ops($grosspl, 0, '<')) {
                 $grosstotal = float_ops($grosstotal, -$grosspl, '+');
                 Accountlist::add_row_csv(array("Gross Loss C/O", convert_cur(-$grosspl)));
             }
         }
         Accountlist::add_row_csv(array("Total Incomes - Gross", convert_cur($grosstotal)));
         /************************* NET CALCULATIONS ***************************/
         Accountlist::add_blank_csv();
         Accountlist::add_blank_csv();
         /* Net P/L : Expenses */
         $net_expense_total = 0;
         $this->db->from('groups')->where('parent_id', 4)->where('affects_gross !=', 1);
         $net_expense_list_q = $this->db->get();
         foreach ($net_expense_list_q->result() as $row) {
             $net_expense = new Accountlist();
             $net_expense->init($row->id);
             $net_expense_total = float_ops($net_expense_total, $net_expense->total, '+');
             $net_exp_array = $net_expense->build_array();
             $net_expense->to_csv($net_exp_array);
         }
         Accountlist::add_blank_csv();
         /* Net P/L : Incomes */
         $net_income_total = 0;
         $this->db->from('groups')->where('parent_id', 3)->where('affects_gross !=', 1);
         $net_income_list_q = $this->db->get();
         foreach ($net_income_list_q->result() as $row) {
             $net_income = new Accountlist();
             $net_income->init($row->id);
             $net_income_total = float_ops($net_income_total, $net_income->total, '+');
             $net_inc_array = $net_income->build_array();
             $net_income->to_csv($net_inc_array);
         }
         Accountlist::add_blank_csv();
         Accountlist::add_blank_csv();
         /* Converting to positive value since Cr */
         $net_income_total = -$net_income_total;
         /* Calculating Net P/L */
         $netpl = float_ops(float_ops($net_income_total, $net_expense_total, '-'), $grosspl, '+');
         /* Showing Net P/L : Expenses */
         $nettotal = $net_expense_total;
         Accountlist::add_row_csv(array("Total Expenses", convert_cur($nettotal)));
         if (float_ops($grosspl, 0, '>')) {
         } else {
             if (float_ops($grosspl, 0, '<')) {
                 $nettotal = float_ops($nettotal, -$grosspl, '+');
                 Accountlist::add_row_csv(array("Gross Loss B/F", convert_cur(-$grosspl)));
             }
         }
         if (float_ops($netpl, 0, '>')) {
             $nettotal = float_ops($nettotal, $netpl, '+');
             Accountlist::add_row_csv(array("Net Profit", convert_cur($netpl)));
         }
         Accountlist::add_row_csv(array("Total - Net Expenses", convert_cur($nettotal)));
         /* Showing Net P/L : Incomes */
         $nettotal = $net_income_total;
         Accountlist::add_row_csv(array("Total Incomes", convert_cur($nettotal)));
         if (float_ops($grosspl, 0, '>')) {
             $nettotal = float_ops($nettotal, $grosspl, '+');
             Accountlist::add_row_csv(array("Gross Profit B/F", convert_cur($grosspl)));
         }
         if (float_ops($netpl, 0, '>')) {
         } else {
             if (float_ops($netpl, 0, '<')) {
                 $nettotal = float_ops($nettotal, -$netpl, '+');
                 Accountlist::add_row_csv(array("Net Loss", convert_cur(-$netpl)));
             }
         }
         Accountlist::add_row_csv(array("Total - Net Incomes", convert_cur($nettotal)));
         $balancesheet = Accountlist::get_csv();
         $this->load->helper('csv');
         echo array_to_csv($balancesheet, "profitandloss.csv");
         return;
     }
     return;
 }
Example #3
0
 function index()
 {
     $this->template->set('page_title', 'Account Settings');
     $account_data = $this->Setting_model->get_current();
     $default_start = '01/04/';
     $default_end = '31/03/';
     if (date('n') > 3) {
         $default_start .= date('Y');
         $default_end .= date('Y') + 1;
     } else {
         $default_start .= date('Y') - 1;
         $default_end .= date('Y');
     }
     /* Form fields */
     $data['account_name'] = array('name' => 'account_name', 'id' => 'account_name', 'maxlength' => '100', 'size' => '40', 'value' => '');
     $data['account_address'] = array('name' => 'account_address', 'id' => 'account_address', 'rows' => '4', 'cols' => '47', 'value' => '');
     $data['account_email'] = array('name' => 'account_email', 'id' => 'account_email', 'maxlength' => '100', 'size' => '40', 'value' => '');
     $data['fy_start'] = '';
     $data['fy_end'] = '';
     $data['account_currency'] = array('name' => 'account_currency', 'id' => 'account_currency', 'maxlength' => '10', 'size' => '10', 'value' => '');
     $data['account_date_options'] = array('dd/mm/yyyy' => 'Day / Month / Year', 'mm/dd/yyyy' => 'Month / Day / Year', 'yyyy/mm/dd' => 'Year / Month / Day');
     $data['account_date'] = 'dd/mm/yyyy';
     $data['account_timezone'] = 'UTC';
     $data['account_locked'] = FALSE;
     /* Current account settings */
     if ($account_data) {
         $data['account_name']['value'] = print_value($account_data->name);
         $data['account_address']['value'] = print_value($account_data->address);
         $data['account_email']['value'] = print_value($account_data->email);
         $data['account_currency']['value'] = print_value($account_data->currency_symbol);
         $data['account_date'] = print_value($account_data->date_format);
         $data['account_timezone'] = print_value($account_data->timezone);
         $data['fy_start'] = date_mysql_to_php(print_value($account_data->fy_start));
         $data['fy_end'] = date_mysql_to_php(print_value($account_data->fy_end));
         $data['account_locked'] = print_value($account_data->account_locked);
     }
     /* Form validations */
     $this->form_validation->set_rules('account_name', 'Account Name', 'trim|required|min_length[2]|max_length[100]');
     $this->form_validation->set_rules('account_address', 'Account Address', 'trim|max_length[255]');
     $this->form_validation->set_rules('account_email', 'Account Email', 'trim|valid_email');
     $this->form_validation->set_rules('account_currency', 'Currency', 'trim|max_length[10]');
     $this->form_validation->set_rules('account_date', 'Date', 'trim|max_length[10]');
     $this->form_validation->set_rules('account_timezone', 'Timezone', 'trim|max_length[6]');
     $this->form_validation->set_rules('account_locked', 'Account Locked', 'trim');
     /* Repopulating form */
     if ($_POST) {
         $data['account_name']['value'] = $this->input->post('account_name', TRUE);
         $data['account_address']['value'] = $this->input->post('account_address', TRUE);
         $data['account_email']['value'] = $this->input->post('account_email', TRUE);
         $data['account_currency']['value'] = $this->input->post('account_currency', TRUE);
         $data['account_date'] = $this->input->post('account_date', TRUE);
         $data['account_timezone'] = $this->input->post('account_timezone', TRUE);
         $data['account_locked'] = $this->input->post('account_locked', TRUE);
     }
     /* Validating form */
     if ($this->form_validation->run() == FALSE) {
         $this->messages->add(validation_errors(), 'error');
         $this->template->load('template', 'setting/account', $data);
         return;
     } else {
         $data_account_name = $this->input->post('account_name', TRUE);
         $data_account_address = $this->input->post('account_address', TRUE);
         $data_account_email = $this->input->post('account_email', TRUE);
         $data_account_currency = $this->input->post('account_currency', TRUE);
         $data_account_date_form = $this->input->post('account_date', TRUE);
         /* Checking for valid format */
         if ($data_account_date_form == "dd/mm/yyyy") {
             $data_account_date = "dd/mm/yyyy";
         } else {
             if ($data_account_date_form == "mm/dd/yyyy") {
                 $data_account_date = "mm/dd/yyyy";
             } else {
                 if ($data_account_date_form == "yyyy/mm/dd") {
                     $data_account_date = "yyyy/mm/dd";
                 } else {
                     $data_account_date = "dd/mm/yyyy";
                 }
             }
         }
         $data_account_timezone = $this->input->post('timezones', TRUE);
         $data_account_locked = $this->input->post('account_locked', TRUE);
         if ($data_account_locked != 1) {
             $data_account_locked = 0;
         }
         /* Update settings */
         $this->db->trans_start();
         $update_data = array('name' => $data_account_name, 'address' => $data_account_address, 'email' => $data_account_email, 'currency_symbol' => $data_account_currency, 'date_format' => $data_account_date, 'timezone' => $data_account_timezone, 'account_locked' => $data_account_locked);
         if (!$this->db->where('id', 1)->update('settings', $update_data)) {
             $this->db->trans_rollback();
             $this->messages->add('Error updating account settings.', 'error');
             $this->logger->write_message("error", "Error updating account settings");
             $this->template->load('template', 'setting/account', $data);
             return;
         } else {
             $this->db->trans_complete();
             $this->messages->add('Account settings updated.', 'success');
             $this->logger->write_message("success", "Updated account settings");
             redirect('setting');
             return;
         }
     }
     return;
 }
		dateFormat: '<?php 
echo $this->config->item('account_date_format');
?>
',
	});
	$('.datepicker-restrict').datepick({
		dateFormat: '<?php 
echo $this->config->item('account_date_format');
?>
',
		minDate: '<?php 
echo date_mysql_to_php($this->config->item('account_fy_start'));
?>
',
		maxDate: '<?php 
echo date_mysql_to_php($this->config->item('account_fy_end'));
?>
',
	});
});
</script>

</head>
<body>
<div id="container">
	<div id="header">
		<div id="logo">
			<?php 
echo anchor('', 'Webzash', array('class' => 'anchor-link-b'));
?>
		</div>
Example #5
0
 function edit($entry_type, $entry_id = 0)
 {
     /* Check access */
     if (!check_access('edit entry')) {
         $this->messages->add('Permission denied.', 'error');
         redirect('entry/show/' . $entry_type);
         return;
     }
     /* Check for account lock */
     if ($this->config->item('account_locked') == 1) {
         $this->messages->add('Account is locked.', 'error');
         redirect('entry/show/' . $entry_type);
         return;
     }
     /* Entry Type */
     $entry_type_id = entry_type_name_to_id($entry_type);
     if (!$entry_type_id) {
         $this->messages->add('Invalid Entry type.', 'error');
         redirect('entry/show/all');
         return;
     } else {
         $current_entry_type = entry_type_info($entry_type_id);
     }
     $this->template->set('page_title', 'Edit ' . $current_entry_type['name'] . ' Entry');
     /* Load current entry details */
     if (!($cur_entry = $this->Entry_model->get_entry($entry_id, $entry_type_id))) {
         $this->messages->add('Invalid Entry.', 'error');
         redirect('entry/show/' . $current_entry_type['label']);
         return;
     }
     /* Form fields - Entry */
     $data['entry_number'] = array('name' => 'entry_number', 'id' => 'entry_number', 'maxlength' => '11', 'size' => '11', 'value' => $cur_entry->number);
     $data['entry_date'] = array('name' => 'entry_date', 'id' => 'entry_date', 'maxlength' => '11', 'size' => '11', 'value' => date_mysql_to_php($cur_entry->date));
     $data['entry_narration'] = array('name' => 'entry_narration', 'id' => 'entry_narration', 'cols' => '50', 'rows' => '4', 'value' => $cur_entry->narration);
     $data['entry_id'] = $entry_id;
     $data['entry_type_id'] = $entry_type_id;
     $data['current_entry_type'] = $current_entry_type;
     $data['entry_tag'] = $cur_entry->tag_id;
     $data['entry_tags'] = $this->Tag_model->get_all_tags();
     $data['has_reconciliation'] = FALSE;
     /* Load current ledger details if not $_POST */
     if (!$_POST) {
         $this->db->from('entry_items')->where('entry_id', $entry_id);
         $cur_ledgers_q = $this->db->get();
         if ($cur_ledgers_q->num_rows <= 0) {
             $this->messages->add('No associated Ledger accounts found.', 'error');
         }
         $counter = 0;
         foreach ($cur_ledgers_q->result() as $row) {
             $data['ledger_dc'][$counter] = $row->dc;
             $data['ledger_id'][$counter] = $row->ledger_id;
             if ($row->dc == "D") {
                 $data['dr_amount'][$counter] = $row->amount;
                 $data['cr_amount'][$counter] = "";
             } else {
                 $data['dr_amount'][$counter] = "";
                 $data['cr_amount'][$counter] = $row->amount;
             }
             if ($row->reconciliation_date) {
                 $data['has_reconciliation'] = TRUE;
             }
             $counter++;
         }
         /* Two extra rows */
         $data['ledger_dc'][$counter] = 'D';
         $data['ledger_id'][$counter] = 0;
         $data['dr_amount'][$counter] = "";
         $data['cr_amount'][$counter] = "";
         $counter++;
         $data['ledger_dc'][$counter] = 'D';
         $data['ledger_id'][$counter] = 0;
         $data['dr_amount'][$counter] = "";
         $data['cr_amount'][$counter] = "";
         $counter++;
     }
     /* Form validations */
     if ($current_entry_type['numbering'] == '3') {
         $this->form_validation->set_rules('entry_number', 'Entry Number', 'trim|is_natural_no_zero|uniqueentrynowithid[' . $entry_type_id . '.' . $entry_id . ']');
     } else {
         $this->form_validation->set_rules('entry_number', 'Entry Number', 'trim|required|is_natural_no_zero|uniqueentrynowithid[' . $entry_type_id . '.' . $entry_id . ']');
     }
     $this->form_validation->set_rules('entry_date', 'Entry Date', 'trim|required|is_date|is_date_within_range');
     $this->form_validation->set_rules('entry_narration', 'trim');
     $this->form_validation->set_rules('entry_tag', 'Tag', 'trim|is_natural');
     /* Debit and Credit amount validation */
     if ($_POST) {
         foreach ($this->input->post('ledger_dc', TRUE) as $id => $ledger_data) {
             $this->form_validation->set_rules('dr_amount[' . $id . ']', 'Debit Amount', 'trim|currency');
             $this->form_validation->set_rules('cr_amount[' . $id . ']', 'Credit Amount', 'trim|currency');
         }
     }
     /* Repopulating form */
     if ($_POST) {
         $data['entry_number']['value'] = $this->input->post('entry_number', TRUE);
         $data['entry_date']['value'] = $this->input->post('entry_date', TRUE);
         $data['entry_narration']['value'] = $this->input->post('entry_narration', TRUE);
         $data['entry_tag'] = $this->input->post('entry_tag', TRUE);
         $data['has_reconciliation'] = $this->input->post('has_reconciliation', TRUE);
         $data['ledger_dc'] = $this->input->post('ledger_dc', TRUE);
         $data['ledger_id'] = $this->input->post('ledger_id', TRUE);
         $data['dr_amount'] = $this->input->post('dr_amount', TRUE);
         $data['cr_amount'] = $this->input->post('cr_amount', TRUE);
     }
     if ($this->form_validation->run() == FALSE) {
         $this->messages->add(validation_errors(), 'error');
         $this->template->load('template', 'entry/edit', $data);
     } else {
         /* Checking for Valid Ledgers account and Debit and Credit Total */
         $data_all_ledger_id = $this->input->post('ledger_id', TRUE);
         $data_all_ledger_dc = $this->input->post('ledger_dc', TRUE);
         $data_all_dr_amount = $this->input->post('dr_amount', TRUE);
         $data_all_cr_amount = $this->input->post('cr_amount', TRUE);
         $dr_total = 0;
         $cr_total = 0;
         $bank_cash_present = FALSE;
         /* Whether atleast one Ledger account is Bank or Cash account */
         $non_bank_cash_present = FALSE;
         /* Whether atleast one Ledger account is NOT a Bank or Cash account */
         foreach ($data_all_ledger_dc as $id => $ledger_data) {
             if ($data_all_ledger_id[$id] < 1) {
                 continue;
             }
             /* Check for valid ledger id */
             $this->db->from('ledgers')->where('id', $data_all_ledger_id[$id]);
             $valid_ledger_q = $this->db->get();
             if ($valid_ledger_q->num_rows() < 1) {
                 $this->messages->add('Invalid Ledger account.', 'error');
                 $this->template->load('template', 'entry/edit', $data);
                 return;
             } else {
                 /* Check for valid ledger type */
                 $valid_ledger = $valid_ledger_q->row();
                 if ($current_entry_type['bank_cash_ledger_restriction'] == '2') {
                     if ($data_all_ledger_dc[$id] == 'D' && $valid_ledger->type == 1) {
                         $bank_cash_present = TRUE;
                     }
                     if ($valid_ledger->type != 1) {
                         $non_bank_cash_present = TRUE;
                     }
                 } else {
                     if ($current_entry_type['bank_cash_ledger_restriction'] == '3') {
                         if ($data_all_ledger_dc[$id] == 'C' && $valid_ledger->type == 1) {
                             $bank_cash_present = TRUE;
                         }
                         if ($valid_ledger->type != 1) {
                             $non_bank_cash_present = TRUE;
                         }
                     } else {
                         if ($current_entry_type['bank_cash_ledger_restriction'] == '4') {
                             if ($valid_ledger->type != 1) {
                                 $this->messages->add('Invalid Ledger account. ' . $current_entry_type['name'] . ' Entry can have only Bank or Cash Ledger accounts.', 'error');
                                 $this->template->load('template', 'entry/edit', $data);
                                 return;
                             }
                         } else {
                             if ($current_entry_type['bank_cash_ledger_restriction'] == '5') {
                                 if ($valid_ledger->type == 1) {
                                     $this->messages->add('Invalid Ledger account. ' . $current_entry_type['name'] . ' Entry cannot have any Bank or Cash Ledger account.', 'error');
                                     $this->template->load('template', 'entry/edit', $data);
                                     return;
                                 }
                             }
                         }
                     }
                 }
             }
             if ($data_all_ledger_dc[$id] == "D") {
                 $dr_total = float_ops($dr_total, $data_all_dr_amount[$id], '+');
             } else {
                 $cr_total = float_ops($cr_total, $data_all_cr_amount[$id], '+');
             }
         }
         if (float_ops($dr_total, $cr_total, '!=')) {
             $this->messages->add('Debit and Credit Total does not match!', 'error');
             $this->template->load('template', 'entry/edit', $data);
             return;
         } else {
             if (float_ops($dr_total, 0, '==') && float_ops($cr_total, 0, '==')) {
                 $this->messages->add('Cannot save empty Entry.', 'error');
                 $this->template->load('template', 'entry/edit', $data);
                 return;
             }
         }
         /* Check if atleast one Bank or Cash Ledger account is present */
         if ($current_entry_type['bank_cash_ledger_restriction'] == '2') {
             if (!$bank_cash_present) {
                 $this->messages->add('Need to Debit atleast one Bank or Cash Ledger account.', 'error');
                 $this->template->load('template', 'entry/edit', $data);
                 return;
             }
             if (!$non_bank_cash_present) {
                 $this->messages->add('Need to Debit or Credit atleast one NON - Bank or Cash Ledger account.', 'error');
                 $this->template->load('template', 'entry/edit', $data);
                 return;
             }
         } else {
             if ($current_entry_type['bank_cash_ledger_restriction'] == '3') {
                 if (!$bank_cash_present) {
                     $this->messages->add('Need to Credit atleast one Bank or Cash Ledger account.', 'error');
                     $this->template->load('template', 'entry/edit', $data);
                     return;
                 }
                 if (!$non_bank_cash_present) {
                     $this->messages->add('Need to Debit or Credit atleast one NON - Bank or Cash Ledger account.', 'error');
                     $this->template->load('template', 'entry/edit', $data);
                     return;
                 }
             }
         }
         /* Updating main entry */
         if ($current_entry_type['numbering'] == '3') {
             $data_number = $this->input->post('entry_number', TRUE);
             if (!$data_number) {
                 $data_number = NULL;
             }
         } else {
             $data_number = $this->input->post('entry_number', TRUE);
         }
         $data_date = $this->input->post('entry_date', TRUE);
         $data_narration = $this->input->post('entry_narration', TRUE);
         $data_tag = $this->input->post('entry_tag', TRUE);
         if ($data_tag < 1) {
             $data_tag = NULL;
         }
         $data_type = $entry_type_id;
         $data_date = date_php_to_mysql($data_date);
         // Converting date to MySQL
         $data_has_reconciliation = $this->input->post('has_reconciliation', TRUE);
         $this->db->trans_start();
         $update_data = array('number' => $data_number, 'date' => $data_date, 'narration' => $data_narration, 'tag_id' => $data_tag);
         if (!$this->db->where('id', $entry_id)->update('entries', $update_data)) {
             $this->db->trans_rollback();
             $this->messages->add('Error updating Entry.', 'error');
             $this->logger->write_message("error", "Error updating entry details for " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " [id:" . $entry_id . "]");
             $this->template->load('template', 'entry/edit', $data);
             return;
         }
         /* TODO : Deleting all old ledger data, Bad solution */
         if (!$this->db->delete('entry_items', array('entry_id' => $entry_id))) {
             $this->db->trans_rollback();
             $this->messages->add('Error deleting previous Ledger accounts from Entry.', 'error');
             $this->logger->write_message("error", "Error deleting previous entry items for " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " [id:" . $entry_id . "]");
             $this->template->load('template', 'entry/edit', $data);
             return;
         }
         /* Adding ledger accounts */
         $data_all_ledger_dc = $this->input->post('ledger_dc', TRUE);
         $data_all_ledger_id = $this->input->post('ledger_id', TRUE);
         $data_all_dr_amount = $this->input->post('dr_amount', TRUE);
         $data_all_cr_amount = $this->input->post('cr_amount', TRUE);
         $dr_total = 0;
         $cr_total = 0;
         foreach ($data_all_ledger_dc as $id => $ledger_data) {
             $data_ledger_dc = $data_all_ledger_dc[$id];
             $data_ledger_id = $data_all_ledger_id[$id];
             if ($data_ledger_id < 1) {
                 continue;
             }
             $data_amount = 0;
             if ($data_all_ledger_dc[$id] == "D") {
                 $data_amount = $data_all_dr_amount[$id];
                 $dr_total = float_ops($dr_total, $data_all_dr_amount[$id], '+');
             } else {
                 $data_amount = $data_all_cr_amount[$id];
                 $cr_total = float_ops($cr_total, $data_all_cr_amount[$id], '+');
             }
             $insert_ledger_data = array('entry_id' => $entry_id, 'ledger_id' => $data_ledger_id, 'amount' => $data_amount, 'dc' => $data_ledger_dc);
             if (!$this->db->insert('entry_items', $insert_ledger_data)) {
                 $this->db->trans_rollback();
                 $this->messages->add('Error adding Ledger account - ' . $data_ledger_id . ' to Entry.', 'error');
                 $this->logger->write_message("error", "Error adding Ledger account item [id:" . $data_ledger_id . "] for " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " [id:" . $entry_id . "]");
                 $this->template->load('template', 'entry/edit', $data);
                 return;
             }
         }
         /* Updating Debit and Credit Total in entries table */
         $update_data = array('dr_total' => $dr_total, 'cr_total' => $cr_total);
         if (!$this->db->where('id', $entry_id)->update('entries', $update_data)) {
             $this->db->trans_rollback();
             $this->messages->add('Error updating Entry total.', 'error');
             $this->logger->write_message("error", "Error updating entry total for " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " [id:" . $entry_id . "]");
             $this->template->load('template', 'entry/edit', $data);
             return;
         }
         /* Success */
         $this->db->trans_complete();
         $this->session->set_userdata('entry_updated_show_action', TRUE);
         $this->session->set_userdata('entry_updated_id', $entry_id);
         $this->session->set_userdata('entry_updated_type_id', $entry_type_id);
         $this->session->set_userdata('entry_updated_type_label', $current_entry_type['label']);
         $this->session->set_userdata('entry_updated_type_name', $current_entry_type['name']);
         $this->session->set_userdata('entry_updated_number', $data_number);
         if ($data_has_reconciliation) {
             $this->session->set_userdata('entry_updated_has_reconciliation', TRUE);
         } else {
             $this->session->set_userdata('entry_updated_has_reconciliation', FALSE);
         }
         /* Showing success message in show() method since message is too long for storing it in session */
         $this->logger->write_message("success", "Updated " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " [id:" . $entry_id . "]");
         redirect('entry/show/' . $current_entry_type['label']);
         return;
     }
     return;
 }
 function date_today_php()
 {
     $CI =& get_instance();
     /* Check for date beyond the current financial year range */
     $todays_date = date('Y-m-d 00:00:00');
     $fy_start = $CI->config->item('account_fy_start');
     $fy_end = $CI->config->item('account_fy_end');
     if ($CI->config->item('account_fy_start') > $todays_date) {
         return date_mysql_to_php($fy_start);
     }
     if ($CI->config->item('account_fy_end') < $todays_date) {
         return date_mysql_to_php($fy_end);
     }
     $current_date_format = $CI->config->item('account_date_format');
     switch ($current_date_format) {
         case 'dd/mm/yyyy':
             return date('d/m/Y');
             break;
         case 'mm/dd/yyyy':
             return date('m/d/Y');
             break;
         case 'yyyy/mm/dd':
             return date('Y/m/d');
             break;
         default:
             $CI->messages->add('Invalid date format. Check your account settings.', 'error');
             return "";
     }
     return;
 }
Example #7
0
 function index()
 {
     $this->load->helper('file');
     $this->load->library('accountlist');
     $this->load->model('Ledger_model');
     $this->load->model('Setting_model');
     $this->template->set('page_title', 'Carry forward account');
     /* Check access */
     if (!check_access('cf account')) {
         $this->messages->add('Permission denied.', 'error');
         redirect('setting');
         return;
     }
     /* Current settings */
     $account_data = $this->Setting_model->get_current();
     /* Form fields */
     $last_year_end = $this->config->item('account_fy_end');
     list($last_year_end_date, $last_year_end_time) = explode(' ', $last_year_end);
     list($last_year_end_year, $last_year_end_month, $last_year_end_day) = explode('-', $last_year_end_date);
     $last_year_end_ts = strtotime($last_year_end);
     $default_start_ts = $last_year_end_ts + 60 * 60 * 24;
     /* Adding 24 hours */
     $default_start = date("Y-m-d 00:00:00", $default_start_ts);
     $default_end = $last_year_end_year + 1 . "-" . $last_year_end_month . "-" . $last_year_end_day . " 00:00:00";
     /* Form fields */
     $data['account_label'] = array('name' => 'account_label', 'id' => 'account_label', 'maxlength' => '30', 'size' => '30', 'value' => '');
     $data['account_name'] = array('name' => 'account_name', 'id' => 'account_name', 'maxlength' => '100', 'size' => '40', 'value' => '');
     $data['fy_start'] = array('name' => 'fy_start', 'id' => 'fy_start', 'maxlength' => '11', 'size' => '11', 'value' => date_mysql_to_php($default_start));
     $data['fy_end'] = array('name' => 'fy_end', 'id' => 'fy_end', 'maxlength' => '11', 'size' => '11', 'value' => date_mysql_to_php($default_end));
     $data['database_name'] = array('name' => 'database_name', 'id' => 'database_name', 'maxlength' => '100', 'size' => '40', 'value' => '');
     $data['database_username'] = array('name' => 'database_username', 'id' => 'database_username', 'maxlength' => '100', 'size' => '40', 'value' => '');
     $data['database_password'] = array('name' => 'database_password', 'id' => 'database_password', 'maxlength' => '100', 'size' => '40', 'value' => '');
     $data['database_host'] = array('name' => 'database_host', 'id' => 'database_host', 'maxlength' => '100', 'size' => '40', 'value' => 'localhost');
     $data['database_port'] = array('name' => 'database_port', 'id' => 'database_port', 'maxlength' => '100', 'size' => '40', 'value' => '3306');
     $data['create_database'] = FALSE;
     $data['account_name']['value'] = $this->config->item('account_name');
     /* Form validations */
     $this->form_validation->set_rules('account_label', 'C/F Label', 'trim|required|min_length[2]|max_length[30]|alpha_numeric');
     $this->form_validation->set_rules('account_name', 'C/F Account Name', 'trim|required|min_length[2]|max_length[100]');
     $this->form_validation->set_rules('fy_start', 'C/F Financial Year Start', 'trim|required|is_date');
     $this->form_validation->set_rules('fy_end', 'C/F Financial Year End', 'trim|required|is_date');
     $this->form_validation->set_rules('database_name', 'Database Name', 'trim|required');
     $this->form_validation->set_rules('database_username', 'Database Username', 'trim|required');
     /* Repopulating form */
     if ($_POST) {
         $data['account_label']['value'] = $this->input->post('account_label', TRUE);
         $data['account_name']['value'] = $this->input->post('account_name', TRUE);
         $data['fy_start']['value'] = $this->input->post('fy_start', TRUE);
         $data['fy_end']['value'] = $this->input->post('fy_end', TRUE);
         $data['create_database'] = $this->input->post('create_database', TRUE);
         $data['database_name']['value'] = $this->input->post('database_name', TRUE);
         $data['database_username']['value'] = $this->input->post('database_username', TRUE);
         $data['database_password']['value'] = $this->input->post('database_password', TRUE);
         $data['database_host']['value'] = $this->input->post('database_host', TRUE);
         $data['database_port']['value'] = $this->input->post('database_port', TRUE);
     }
     /* Validating form */
     if ($this->form_validation->run() == FALSE) {
         $this->messages->add(validation_errors(), 'error');
         $this->template->load('template', 'setting/cf', $data);
         return;
     } else {
         $data_account_label = $this->input->post('account_label', TRUE);
         $data_account_label = strtolower($data_account_label);
         $data_account_name = $this->input->post('account_name', TRUE);
         $data_account_address = $this->config->item('account_address');
         $data_account_email = $this->config->item('account_email');
         $data_fy_start = date_php_to_mysql($this->input->post('fy_start', TRUE));
         $data_fy_end = date_php_to_mysql_end_time($this->input->post('fy_end', TRUE));
         $data_account_currency = $this->config->item('account_currency_symbol');
         $data_account_date = $this->config->item('account_date_format');
         $data_account_timezone = $this->config->item('account_timezone');
         $data_account_manage_inventory = $account_data->manage_inventory;
         $data_account_account_locked = $account_data->account_locked;
         $data_account_email_protocol = $account_data->email_protocol;
         $data_account_email_host = $account_data->email_host;
         $data_account_email_port = $account_data->email_port;
         $data_account_email_username = $account_data->email_username;
         $data_account_email_password = $account_data->email_password;
         $data_account_print_paper_height = $account_data->print_paper_height;
         $data_account_print_paper_width = $account_data->print_paper_width;
         $data_account_print_margin_top = $account_data->print_margin_top;
         $data_account_print_margin_bottom = $account_data->print_margin_bottom;
         $data_account_print_margin_left = $account_data->print_margin_left;
         $data_account_print_margin_right = $account_data->print_margin_right;
         $data_account_print_orientation = $account_data->print_orientation;
         $data_account_print_page_format = $account_data->print_page_format;
         $data_database_type = 'mysql';
         $data_database_host = $this->input->post('database_host', TRUE);
         $data_database_port = $this->input->post('database_port', TRUE);
         $data_database_name = $this->input->post('database_name', TRUE);
         $data_database_username = $this->input->post('database_username', TRUE);
         $data_database_password = $this->input->post('database_password', TRUE);
         $ini_file = $this->config->item('config_path') . "accounts/" . $data_account_label . ".ini";
         /* Check if database ini file exists */
         if (get_file_info($ini_file)) {
             $this->messages->add('Account with same label already exists.', 'error');
             $this->template->load('template', 'setting/cf', $data);
             return;
         }
         /* Check if start date is less than end date */
         if ($data_fy_end <= $data_fy_start) {
             $this->messages->add('Financial start date cannot be greater than end date.', 'error');
             $this->template->load('template', 'setting/cf', $data);
             return;
         }
         if ($data_database_host == "") {
             $data_database_host = "localhost";
         }
         if ($data_database_port == "") {
             $data_database_port = "3306";
         }
         /* Creating account database */
         if ($this->input->post('create_database', TRUE) == "1") {
             $new_link = @mysql_connect($data_database_host . ':' . $data_database_port, $data_database_username, $data_database_password);
             if ($new_link) {
                 /* Check if database already exists */
                 $db_selected = mysql_select_db($data_database_name, $new_link);
                 if ($db_selected) {
                     mysql_close($new_link);
                     $this->messages->add('Database already exists.', 'error');
                     $this->template->load('template', 'setting/cf', $data);
                     return;
                 }
                 /* Creating account database */
                 $db_create_q = 'CREATE DATABASE ' . mysql_real_escape_string($data_database_name);
                 if (mysql_query($db_create_q, $new_link)) {
                     $this->messages->add('Created account database.', 'success');
                 } else {
                     $this->messages->add('Error creating account database. ' . mysql_error(), 'error');
                     $this->template->load('template', 'setting/cf', $data);
                     return;
                 }
                 mysql_close($new_link);
             } else {
                 $this->messages->add('Error connecting to database. ' . mysql_error(), 'error');
                 $this->template->load('template', 'setting/cf', $data);
                 return;
             }
         }
         /* Setting database */
         $dsn = "mysql://{$data_database_username}:{$data_database_password}@{$data_database_host}:{$data_database_port}/{$data_database_name}";
         $newacc = $this->load->database($dsn, TRUE);
         if (!$newacc->conn_id) {
             $this->messages->add('Error connecting to database.', 'error');
             $this->template->load('template', 'setting/cf', $data);
             return;
         } else {
             if ($newacc->_error_message() != "") {
                 $this->messages->add('Error connecting to database. ' . $newacc->_error_message(), 'error');
                 $this->template->load('template', 'setting/cf', $data);
                 return;
             } else {
                 if ($newacc->query("SHOW TABLES")->num_rows() > 0) {
                     $this->messages->add('Selected database in not empty.', 'error');
                     $this->template->load('template', 'setting/cf', $data);
                     return;
                 } else {
                     /* Executing the database setup script */
                     $setup_account = read_file('system/application/controllers/admin/schema.sql');
                     $setup_account_array = explode(";", $setup_account);
                     foreach ($setup_account_array as $row) {
                         if (strlen($row) < 5) {
                             continue;
                         }
                         $newacc->query($row);
                         if ($newacc->_error_message() != "") {
                             $this->messages->add('Error initializing account database.', 'error');
                             $this->template->load('template', 'setting/cf', $data);
                             return;
                         }
                     }
                     $this->messages->add('Initialized account database.', 'success');
                     /* Adding account settings */
                     $newacc->trans_start();
                     if (!$newacc->query("INSERT INTO settings (id, name, address, email, fy_start, fy_end, currency_symbol, date_format, timezone, manage_inventory, account_locked, email_protocol, email_host, email_port, email_username, email_password, print_paper_height, print_paper_width, print_margin_top, print_margin_bottom, print_margin_left, print_margin_right, print_orientation, print_page_format, database_version) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", array(1, $data_account_name, $data_account_address, $data_account_email, $data_fy_start, $data_fy_end, $data_account_currency, $data_account_date, $data_account_timezone, $data_account_manage_inventory, 0, $data_account_email_protocol, $data_account_email_host, $data_account_email_port, $data_account_email_username, $data_account_email_password, $data_account_print_paper_height, $data_account_print_paper_width, $data_account_print_margin_top, $data_account_print_margin_bottom, $data_account_print_margin_left, $data_account_print_margin_right, $data_account_print_orientation, $data_account_print_page_format, 4))) {
                         $newacc->trans_rollback();
                         $this->messages->add('Error adding account settings.', 'error');
                         $this->template->load('template', 'setting/cf', $data);
                         return;
                     } else {
                         $newacc->trans_complete();
                         $this->messages->add('Added account settings.', 'success');
                     }
                     /**************** Importing the C/F Values : START ***************/
                     $cf_status = TRUE;
                     /* Importing Groups */
                     $this->db->from('groups')->order_by('id', 'asc');
                     $group_q = $this->db->get();
                     foreach ($group_q->result() as $row) {
                         if (!$newacc->query("INSERT INTO groups (id, parent_id, name, affects_gross) VALUES (?, ?, ?, ?)", array($row->id, $row->parent_id, $row->name, $row->affects_gross))) {
                             $this->messages->add('Failed to add Group account - ' . $row->name . '.', 'error');
                             $cf_status = FALSE;
                         }
                     }
                     /* Only importing Assets and Liability closing balance */
                     $assets = new Accountlist();
                     $assets->init(1);
                     $liability = new Accountlist();
                     $liability->init(2);
                     $cf_ledgers = array_merge($assets->get_ledger_ids(), $liability->get_ledger_ids());
                     /* Importing Ledgers */
                     $this->db->from('ledgers')->order_by('id', 'asc');
                     $ledger_q = $this->db->get();
                     foreach ($ledger_q->result() as $row) {
                         /* CF only Assets and Liability with Closing Balance */
                         if (in_array($row->id, $cf_ledgers)) {
                             /* Calculating closing balance for previous year */
                             $cl_balance = $this->Ledger_model->get_ledger_balance($row->id);
                             if (float_ops($cl_balance, 0, '<')) {
                                 $op_balance = -$cl_balance;
                                 $op_balance_dc = "C";
                             } else {
                                 $op_balance = $cl_balance;
                                 $op_balance_dc = "D";
                             }
                             if (!$newacc->query("INSERT INTO ledgers (id, group_id, name, op_balance, op_balance_dc, type, reconciliation) VALUES (?, ?, ?, ?, ?, ?, ?)", array($row->id, $row->group_id, $row->name, $op_balance, $op_balance_dc, $row->type, $row->reconciliation))) {
                                 $this->messages->add('Failed to add Ledger account - ' . $row->name . '.', 'error');
                                 $cf_status = FALSE;
                             }
                         } else {
                             if (!$newacc->query("INSERT INTO ledgers (id, group_id, name, op_balance, op_balance_dc, type, reconciliation) VALUES (?, ?, ?, ?, ?, ?, ?)", array($row->id, $row->group_id, $row->name, 0, "D", $row->type, $row->reconciliation))) {
                                 $this->messages->add('Failed to add Ledger account - ' . $row->name . '.', 'error');
                                 $cf_status = FALSE;
                             }
                         }
                     }
                     /* Importing Entry Types */
                     $this->db->from('entry_types')->order_by('id', 'asc');
                     $entry_type_q = $this->db->get();
                     foreach ($entry_type_q->result() as $row) {
                         if (!$newacc->query("INSERT INTO entry_types (id, label, name, description, base_type, numbering, prefix, suffix, zero_padding, bank_cash_ledger_restriction) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", array($row->id, $row->label, $row->name, $row->description, $row->base_type, $row->numbering, $row->prefix, $row->suffix, $row->zero_padding, $row->bank_cash_ledger_restriction))) {
                             $this->messages->add('Failed to add Entry type  - ' . $row->name . '.', 'error');
                             $cf_status = FALSE;
                         }
                     }
                     /* Importing Tags */
                     $this->db->from('tags')->order_by('id', 'asc');
                     $tag_q = $this->db->get();
                     foreach ($tag_q->result() as $row) {
                         if (!$newacc->query("INSERT INTO tags (id, title, color, background) VALUES (?, ?, ?, ?)", array($row->id, $row->title, $row->color, $row->background))) {
                             $this->messages->add('Failed to add Tag - ' . $row->title . '.', 'error');
                             $cf_status = FALSE;
                         }
                     }
                     if ($cf_status) {
                         $this->messages->add('Account carried forward.', 'success');
                     } else {
                         $this->messages->add('Error carrying forward to new account.', 'error');
                     }
                     /* Adding account settings to file. Code copied from manage controller */
                     $con_details = "[database]" . "\r\n" . "db_type = \"" . $data_database_type . "\"" . "\r\n" . "db_hostname = \"" . $data_database_host . "\"" . "\r\n" . "db_port = \"" . $data_database_port . "\"" . "\r\n" . "db_name = \"" . $data_database_name . "\"" . "\r\n" . "db_username = \"" . $data_database_username . "\"" . "\r\n" . "db_password = \"" . $data_database_password . "\"" . "\r\n";
                     $con_details_html = '[database]' . '<br />db_type = "' . $data_database_type . '"<br />db_hostname = "' . $data_database_host . '"<br />db_port = "' . $data_database_port . '"<br />db_name = "' . $data_database_name . '"<br />db_username = "******"<br />db_password = "******"<br />';
                     /* Writing the connection string to end of file - writing in 'a' append mode */
                     if (!write_file($ini_file, $con_details)) {
                         $this->messages->add('Failed to add account settings file. Check if "' . $ini_file . '" file is writable.', 'error');
                         $this->messages->add('You can manually create a text file "' . $ini_file . '" with the following content :<br /><br />' . $con_details_html, 'error');
                     } else {
                         $this->messages->add('Added account settings file to list of active accounts.', 'success');
                     }
                     redirect('setting');
                     return;
                 }
             }
         }
     }
     return;
 }
Example #8
0
 function edit($entry_type, $entry_id = 0)
 {
     /* Check access */
     if (!check_access('edit inventory entry')) {
         $this->messages->add('Permission denied.', 'error');
         redirect('inventory/transfer/show/' . $entry_type);
         return;
     }
     /* Check for account lock */
     if ($this->config->item('account_locked') == 1) {
         $this->messages->add('Account is locked.', 'error');
         redirect('entry/show/' . $entry_type);
         return;
     }
     /* Entry Type */
     $entry_type_id = entry_type_name_to_id($entry_type);
     if (!$entry_type_id) {
         $this->messages->add('Invalid Entry type.', 'error');
         redirect('entry/show/all');
         return;
     } else {
         $current_entry_type = entry_type_info($entry_type_id);
     }
     $this->template->set('page_title', 'Edit ' . $current_entry_type['name'] . ' Entry');
     /* Load current entry details */
     if (!($cur_entry = $this->Entry_model->get_entry($entry_id, $entry_type_id))) {
         $this->messages->add('Invalid Entry.', 'error');
         redirect('entry/show/' . $current_entry_type['label']);
         return;
     }
     /* Form fields */
     $data['entry_number'] = array('name' => 'entry_number', 'id' => 'entry_number', 'maxlength' => '11', 'size' => '11', 'value' => $cur_entry->number);
     $data['entry_date'] = array('name' => 'entry_date', 'id' => 'entry_date', 'maxlength' => '11', 'size' => '11', 'value' => date_mysql_to_php($cur_entry->date));
     $data['entry_narration'] = array('name' => 'entry_narration', 'id' => 'entry_narration', 'cols' => '50', 'rows' => '4', 'value' => $cur_entry->narration);
     $data['entry_id'] = $entry_id;
     $data['entry_type_id'] = $entry_type_id;
     $data['current_entry_type'] = $current_entry_type;
     $data['entry_tag'] = $cur_entry->tag_id;
     $data['entry_tags'] = $this->Tag_model->get_all_tags();
     /* Load current ledger details if not $_POST */
     if (!$_POST) {
         /* source inventory items */
         $this->db->from('inventory_entry_items')->where('entry_id', $entry_id)->where('type', 2);
         $cur_inventory_item_q = $this->db->get();
         $counter = 0;
         foreach ($cur_inventory_item_q->result() as $row) {
             $data['source_inventory_item_id'][$counter] = $row->inventory_item_id;
             $data['source_inventory_item_quantity'][$counter] = $row->quantity;
             $data['source_inventory_item_rate_per_unit'][$counter] = $row->rate_per_unit;
             $data['source_inventory_item_amount'][$counter] = $row->total;
             $counter++;
         }
         /* one extra rows */
         $data['source_inventory_item_id'][$counter] = '0';
         $data['source_inventory_item_quantity'][$counter] = "";
         $data['source_inventory_item_rate_per_unit'][$counter] = "";
         $data['source_inventory_item_amount'][$counter] = "";
         $counter++;
         /* destination inventory items */
         $this->db->from('inventory_entry_items')->where('entry_id', $entry_id)->where('type', 1);
         $cur_inventory_item_q = $this->db->get();
         $counter = 0;
         foreach ($cur_inventory_item_q->result() as $row) {
             $data['dest_inventory_item_id'][$counter] = $row->inventory_item_id;
             $data['dest_inventory_item_quantity'][$counter] = $row->quantity;
             $data['dest_inventory_item_rate_per_unit'][$counter] = $row->rate_per_unit;
             $data['dest_inventory_item_amount'][$counter] = $row->total;
             $counter++;
         }
         /* one extra rows */
         $data['dest_inventory_item_id'][$counter] = '0';
         $data['dest_inventory_item_quantity'][$counter] = "";
         $data['dest_inventory_item_rate_per_unit'][$counter] = "";
         $data['dest_inventory_item_amount'][$counter] = "";
         $counter++;
     }
     /* Form validations */
     if ($current_entry_type['numbering'] == '3') {
         $this->form_validation->set_rules('entry_number', 'Entry Number', 'trim|is_natural_no_zero|uniqueentrynowithid[' . $entry_type_id . '.' . $entry_id . ']');
     } else {
         $this->form_validation->set_rules('entry_number', 'Entry Number', 'trim|required|is_natural_no_zero|uniqueentrynowithid[' . $entry_type_id . '.' . $entry_id . ']');
     }
     $this->form_validation->set_rules('entry_date', 'Entry Date', 'trim|required|is_date|is_date_within_range');
     $this->form_validation->set_rules('entry_narration', 'trim');
     $this->form_validation->set_rules('entry_tag', 'Tag', 'trim|is_natural');
     /* Debit and Credit amount validation */
     if ($_POST) {
         foreach ($this->input->post('source_inventory_item_id', TRUE) as $id => $inventory_data) {
             $this->form_validation->set_rules('source_inventory_item_quantity[' . $id . ']', 'Inventory Item Quantity', 'trim|quantity');
             $this->form_validation->set_rules('source_inventory_item_rate_per_unit[' . $id . ']', 'Inventory Item Rate Per Unit', 'trim|currency');
             $this->form_validation->set_rules('source_inventory_item_amount[' . $id . ']', 'Inventory Item Amount', 'trim|currency');
         }
         foreach ($this->input->post('dest_inventory_item_id', TRUE) as $id => $inventory_data) {
             $this->form_validation->set_rules('dest_inventory_item_quantity[' . $id . ']', 'Inventory Item Quantity', 'trim|quantity');
             $this->form_validation->set_rules('dest_inventory_item_rate_per_unit[' . $id . ']', 'Inventory Item Rate Per Unit', 'trim|currency');
             $this->form_validation->set_rules('dest_inventory_item_amount[' . $id . ']', 'Inventory Item Amount', 'trim|currency');
         }
     }
     /* Repopulating form */
     if ($_POST) {
         $data['entry_number']['value'] = $this->input->post('entry_number', TRUE);
         $data['entry_date']['value'] = $this->input->post('entry_date', TRUE);
         $data['entry_narration']['value'] = $this->input->post('entry_narration', TRUE);
         $data['entry_tag'] = $this->input->post('entry_tag', TRUE);
         $data['source_inventory_item_id'] = $this->input->post('source_inventory_item_id', TRUE);
         $data['source_inventory_item_quantity'] = $this->input->post('source_inventory_item_quantity', TRUE);
         $data['source_inventory_item_rate_per_unit'] = $this->input->post('source_inventory_item_rate_per_unit', TRUE);
         $data['source_inventory_item_amount'] = $this->input->post('source_inventory_item_amount', TRUE);
         $data['dest_inventory_item_id'] = $this->input->post('dest_inventory_item_id', TRUE);
         $data['dest_inventory_item_quantity'] = $this->input->post('dest_inventory_item_quantity', TRUE);
         $data['dest_inventory_item_rate_per_unit'] = $this->input->post('dest_inventory_item_rate_per_unit', TRUE);
         $data['dest_inventory_item_amount'] = $this->input->post('dest_inventory_item_amount', TRUE);
     }
     if ($this->form_validation->run() == FALSE) {
         $this->messages->add(validation_errors(), 'error');
         $this->template->load('template', 'inventory/transfer/edit', $data);
     } else {
         $data_all_source_inventory_item_id = $this->input->post('source_inventory_item_id', TRUE);
         $data_all_source_inventory_item_quantity = $this->input->post('source_inventory_item_quantity', TRUE);
         $data_all_source_inventory_item_rate_per_unit = $this->input->post('source_inventory_item_rate_per_unit', TRUE);
         $data_all_source_inventory_item_amount = $this->input->post('source_inventory_item_amount', TRUE);
         $data_all_dest_inventory_item_id = $this->input->post('dest_inventory_item_id', TRUE);
         $data_all_dest_inventory_item_quantity = $this->input->post('dest_inventory_item_quantity', TRUE);
         $data_all_dest_inventory_item_rate_per_unit = $this->input->post('dest_inventory_item_rate_per_unit', TRUE);
         $data_all_dest_inventory_item_amount = $this->input->post('dest_inventory_item_amount', TRUE);
         /* Setting Inventory Item type */
         if ($current_entry_type['inventory_entry_type'] == '1') {
             $data_inventory_item_type = 1;
         } else {
             $data_inventory_item_type = 2;
         }
         /* Checking for Valid Inventory Item */
         $source_inventory_item_present = FALSE;
         $data_total_source_inventory_amount = 0;
         foreach ($data_all_source_inventory_item_id as $id => $inventory_data) {
             if ($data_all_source_inventory_item_id[$id] < 1) {
                 continue;
             }
             /* Check for valid inventory item id */
             $this->db->from('inventory_items')->where('id', $data_all_source_inventory_item_id[$id]);
             $valid_inventory_item_q = $this->db->get();
             if ($valid_inventory_item_q->num_rows() < 1) {
                 $this->messages->add('Invalid Source Inventory Item.', 'error');
                 $this->template->load('template', 'inventory/transfer/add', $data);
                 return;
             }
             $source_inventory_item_present = TRUE;
             $data_total_source_inventory_amount = float_ops($data_total_source_inventory_amount, $data_all_source_inventory_item_amount[$id], '+');
         }
         if (!$source_inventory_item_present) {
             $this->messages->add('No Soruce Inventory Item selected.', 'error');
             $this->template->load('template', 'inventory/transfer/add', $data);
             return;
         }
         $dest_inventory_item_present = FALSE;
         $data_total_dest_inventory_amount = 0;
         foreach ($data_all_dest_inventory_item_id as $id => $inventory_data) {
             if ($data_all_dest_inventory_item_id[$id] < 1) {
                 continue;
             }
             /* Check for valid inventory item id */
             $this->db->from('inventory_items')->where('id', $data_all_dest_inventory_item_id[$id]);
             $valid_inventory_item_q = $this->db->get();
             if ($valid_inventory_item_q->num_rows() < 1) {
                 $this->messages->add('Invalid Destination Inventory Item.', 'error');
                 $this->template->load('template', 'inventory/transfer/add', $data);
                 return;
             }
             $dest_inventory_item_present = TRUE;
             $data_total_dest_inventory_amount = float_ops($data_total_dest_inventory_amount, $data_all_dest_inventory_item_amount[$id], '+');
         }
         if (!$dest_inventory_item_present) {
             $this->messages->add('No Destination Inventory Item selected.', 'error');
             $this->template->load('template', 'inventory/transfer/add', $data);
             return;
         }
         /* Total amount calculations */
         if ($data_total_source_inventory_amount < 0) {
             $this->messages->add('Source total cannot be negative.', 'error');
             $this->template->load('template', 'inventory/transfer/add', $data);
             return;
         }
         if ($data_total_dest_inventory_amount < 0) {
             $this->messages->add('Destination total cannot be negative.', 'error');
             $this->template->load('template', 'inventory/transfer/add', $data);
             return;
         }
         /* Updating main entry */
         if ($current_entry_type['numbering'] == '3') {
             $data_number = $this->input->post('entry_number', TRUE);
             if (!$data_number) {
                 $data_number = NULL;
             }
         } else {
             $data_number = $this->input->post('entry_number', TRUE);
         }
         $data_date = $this->input->post('entry_date', TRUE);
         $data_narration = $this->input->post('entry_narration', TRUE);
         $data_tag = $this->input->post('entry_tag', TRUE);
         if ($data_tag < 1) {
             $data_tag = NULL;
         }
         $data_type = $entry_type_id;
         $data_date = date_php_to_mysql($data_date);
         // Converting date to MySQL
         $this->db->trans_start();
         $update_data = array('number' => $data_number, 'date' => $data_date, 'narration' => $data_narration, 'tag_id' => $data_tag, 'dr_total' => $data_total_source_inventory_amount, 'cr_total' => $data_total_dest_inventory_amount);
         if (!$this->db->where('id', $entry_id)->update('entries', $update_data)) {
             $this->db->trans_rollback();
             $this->messages->add('Error updating Entry.', 'error');
             $this->logger->write_message("error", "Error updating entry details for " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " [id:" . $entry_id . "]");
             $this->template->load('template', 'inventory/transfer/edit', $data);
             return;
         }
         /* TODO : Deleting all old inventory item */
         if (!$this->db->delete('inventory_entry_items', array('entry_id' => $entry_id))) {
             $this->db->trans_rollback();
             $this->messages->add('Error deleting previous inventory items from Entry.', 'error');
             $this->logger->write_message("error", "Error deleting previous inventory items from " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " [id:" . $entry_id . "]");
             $this->template->load('template', 'inventory/transfer/edit', $data);
             return;
         }
         /* Adding source inventory items */
         $data_all_source_inventory_item_id = $this->input->post('source_inventory_item_id', TRUE);
         $data_all_source_inventory_item_quantity = $this->input->post('source_inventory_item_quantity', TRUE);
         $data_all_source_inventory_item_rate_per_unit = $this->input->post('source_inventory_item_rate_per_unit', TRUE);
         $data_all_source_inventory_item_amount = $this->input->post('source_inventory_item_amount', TRUE);
         foreach ($data_all_source_inventory_item_id as $id => $inventory_data) {
             $data_source_inventory_item_id = $data_all_source_inventory_item_id[$id];
             if ($data_source_inventory_item_id < 1) {
                 continue;
             }
             $data_source_inventory_item_quantity = $data_all_source_inventory_item_quantity[$id];
             $data_source_inventory_item_rate_per_unit = $data_all_source_inventory_item_rate_per_unit[$id];
             $data_source_inventory_item_amount = $data_all_source_inventory_item_amount[$id];
             $insert_inventory_data = array('entry_id' => $entry_id, 'inventory_item_id' => $data_source_inventory_item_id, 'quantity' => $data_source_inventory_item_quantity, 'rate_per_unit' => $data_source_inventory_item_rate_per_unit, 'discount' => '', 'total' => $data_source_inventory_item_amount, 'type' => '2');
             if (!$this->db->insert('inventory_entry_items', $insert_inventory_data)) {
                 $this->db->trans_rollback();
                 $this->messages->add('Error adding Inventory Item - ' . $data_source_inventory_item_id . ' to Entry.', 'error');
                 $this->logger->write_message("error", "Error adding " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " since failed inserting inventory item " . "[id:" . $data_source_inventory_item_id . "]");
                 $this->template->load('template', 'inventory/transfer/add', $data);
                 return;
             }
         }
         /* Adding destination inventory items */
         $data_all_dest_inventory_item_id = $this->input->post('dest_inventory_item_id', TRUE);
         $data_all_dest_inventory_item_quantity = $this->input->post('dest_inventory_item_quantity', TRUE);
         $data_all_dest_inventory_item_rate_per_unit = $this->input->post('dest_inventory_item_rate_per_unit', TRUE);
         $data_all_dest_inventory_item_amount = $this->input->post('dest_inventory_item_amount', TRUE);
         foreach ($data_all_dest_inventory_item_id as $id => $inventory_data) {
             $data_dest_inventory_item_id = $data_all_dest_inventory_item_id[$id];
             if ($data_dest_inventory_item_id < 1) {
                 continue;
             }
             $data_dest_inventory_item_quantity = $data_all_dest_inventory_item_quantity[$id];
             $data_dest_inventory_item_rate_per_unit = $data_all_dest_inventory_item_rate_per_unit[$id];
             $data_dest_inventory_item_amount = $data_all_dest_inventory_item_amount[$id];
             $insert_inventory_data = array('entry_id' => $entry_id, 'inventory_item_id' => $data_dest_inventory_item_id, 'quantity' => $data_dest_inventory_item_quantity, 'rate_per_unit' => $data_dest_inventory_item_rate_per_unit, 'discount' => '', 'total' => $data_dest_inventory_item_amount, 'type' => '1');
             if (!$this->db->insert('inventory_entry_items', $insert_inventory_data)) {
                 $this->db->trans_rollback();
                 $this->messages->add('Error adding Inventory Item - ' . $data_dest_inventory_item_id . ' to Entry.', 'error');
                 $this->logger->write_message("error", "Error adding " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " since failed inserting inventory item " . "[id:" . $data_dest_inventory_item_id . "]");
                 $this->template->load('template', 'inventory/transfer/add', $data);
                 return;
             }
         }
         if (!$this->db->where('id', $entry_id)->update('entries', $update_data)) {
             $this->db->trans_rollback();
             $this->messages->add('Error updating Entry total.', 'error');
             $this->logger->write_message("error", "Error updating " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " since failed updating debit and credit total");
             $this->template->load('template', 'inventory/transfer/edit', $data);
             return;
         }
         /* Success */
         $this->db->trans_complete();
         $this->session->set_userdata('entry_updated_show_action', TRUE);
         $this->session->set_userdata('entry_updated_id', $entry_id);
         $this->session->set_userdata('entry_updated_type_id', $entry_type_id);
         $this->session->set_userdata('entry_updated_type_label', $current_entry_type['label']);
         $this->session->set_userdata('entry_updated_type_name', $current_entry_type['name']);
         $this->session->set_userdata('entry_updated_number', $data_number);
         $this->session->set_userdata('entry_updated_has_reconciliation', FALSE);
         /* Showing success message in show() method since message is too long for storing it in session */
         $this->logger->write_message("success", "Updated " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " [id:" . $entry_id . "]");
         redirect('entry/show/' . $current_entry_type['label']);
         return;
     }
     return;
 }
Example #9
0
 function edit($entry_type, $entry_id = 0)
 {
     /* Check access */
     if (!check_access('edit inventory entry')) {
         $this->messages->add('Permission denied.', 'error');
         redirect('inventory/entry/show/' . $entry_type);
         return;
     }
     /* Check for account lock */
     if ($this->config->item('account_locked') == 1) {
         $this->messages->add('Account is locked.', 'error');
         redirect('entry/show/' . $entry_type);
         return;
     }
     /* Entry Type */
     $entry_type_id = entry_type_name_to_id($entry_type);
     if (!$entry_type_id) {
         $this->messages->add('Invalid Entry type.', 'error');
         redirect('entry/show/all');
         return;
     } else {
         $current_entry_type = entry_type_info($entry_type_id);
     }
     $this->template->set('page_title', 'Edit ' . $current_entry_type['name'] . ' Entry');
     /* Load current entry details */
     if (!($cur_entry = $this->Entry_model->get_entry($entry_id, $entry_type_id))) {
         $this->messages->add('Invalid Entry.', 'error');
         redirect('entry/show/' . $current_entry_type['label']);
         return;
     }
     /* Form fields - Entry */
     $data['entry_number'] = array('name' => 'entry_number', 'id' => 'entry_number', 'maxlength' => '11', 'size' => '11', 'value' => $cur_entry->number);
     $data['entry_date'] = array('name' => 'entry_date', 'id' => 'entry_date', 'maxlength' => '11', 'size' => '11', 'value' => date_mysql_to_php($cur_entry->date));
     $data['entry_narration'] = array('name' => 'entry_narration', 'id' => 'entry_narration', 'cols' => '50', 'rows' => '4', 'value' => $cur_entry->narration);
     $data['entry_id'] = $entry_id;
     $data['main_account_active'] = 0;
     $data['main_entity_active'] = 0;
     $data['entry_type_id'] = $entry_type_id;
     $data['current_entry_type'] = $current_entry_type;
     $data['entry_tag'] = $cur_entry->tag_id;
     $data['entry_tags'] = $this->Tag_model->get_all_tags();
     $data['has_reconciliation'] = FALSE;
     /* Load current ledger details if not $_POST */
     if (!$_POST) {
         /* main - account */
         $this->db->from('entry_items')->where('entry_id', $entry_id)->where('inventory_type', 1);
         $cur_main_account_q = $this->db->get();
         $cur_main_account = $cur_main_account_q->row();
         $data['main_account_active'] = $cur_main_account->ledger_id;
         /* main - entity */
         $this->db->from('entry_items')->where('entry_id', $entry_id)->where('inventory_type', 2);
         $cur_main_entity_q = $this->db->get();
         $cur_main_entity = $cur_main_entity_q->row();
         $data['main_entity_active'] = $cur_main_entity->ledger_id;
         /* ledgers */
         $this->db->from('entry_items')->where('entry_id', $entry_id)->where('inventory_type', 3);
         $cur_ledgers_q = $this->db->get();
         $counter = 0;
         foreach ($cur_ledgers_q->result() as $row) {
             $data['ledger_dc'][$counter] = $row->dc;
             $data['ledger_id'][$counter] = $row->ledger_id;
             $data['rate_item'][$counter] = $row->inventory_rate;
             $data['amount_item'][$counter] = $row->amount;
             if ($row->reconciliation_date) {
                 $data['has_reconciliation'] = TRUE;
             }
             $counter++;
         }
         /* one extra rows */
         $data['ledger_dc'][$counter] = 'D';
         $data['ledger_id'][$counter] = 0;
         $data['rate_item'][$counter] = "";
         $data['amount_item'][$counter] = "";
         $counter++;
         /* inventory items */
         $this->db->from('inventory_entry_items')->where('entry_id', $entry_id);
         $cur_inventory_item_q = $this->db->get();
         $counter = 0;
         foreach ($cur_inventory_item_q->result() as $row) {
             $data['inventory_item_id'][$counter] = $row->inventory_item_id;
             $data['inventory_item_quantity'][$counter] = $row->quantity;
             $data['inventory_item_rate_per_unit'][$counter] = $row->rate_per_unit;
             $data['inventory_item_discount'][$counter] = $row->discount;
             $data['inventory_item_amount'][$counter] = $row->total;
             $counter++;
         }
         /* one extra rows */
         $data['inventory_item_id'][$counter] = '0';
         $data['inventory_item_quantity'][$counter] = "";
         $data['inventory_item_rate_per_unit'][$counter] = "";
         $data['inventory_item_discount'][$counter] = "";
         $data['inventory_item_amount'][$counter] = "";
         $counter++;
     }
     /* Form validations */
     if ($current_entry_type['numbering'] == '3') {
         $this->form_validation->set_rules('entry_number', 'Entry Number', 'trim|is_natural_no_zero|uniqueentrynowithid[' . $entry_type_id . '.' . $entry_id . ']');
     } else {
         $this->form_validation->set_rules('entry_number', 'Entry Number', 'trim|required|is_natural_no_zero|uniqueentrynowithid[' . $entry_type_id . '.' . $entry_id . ']');
     }
     $this->form_validation->set_rules('entry_date', 'Entry Date', 'trim|required|is_date|is_date_within_range');
     if ($current_entry_type['inventory_entry_type'] == '1') {
         $this->form_validation->set_rules('main_account', 'Purchase Ledger', 'trim|required');
         $this->form_validation->set_rules('main_entity', 'Creditors (Supplier)', 'trim|required');
     } else {
         $this->form_validation->set_rules('main_account', 'Sale Ledger', 'trim|required');
         $this->form_validation->set_rules('main_entity', 'Debtor (Customer)', 'trim|required');
     }
     $this->form_validation->set_rules('entry_narration', 'trim');
     $this->form_validation->set_rules('entry_tag', 'Tag', 'trim|is_natural');
     /* Debit and Credit amount validation */
     if ($_POST) {
         foreach ($this->input->post('inventory_item_id', TRUE) as $id => $inventory_data) {
             $this->form_validation->set_rules('inventory_item_quantity[' . $id . ']', 'Inventory Item Quantity', 'trim|quantity');
             $this->form_validation->set_rules('inventory_item_rate_per_unit[' . $id . ']', 'Inventory Item Rate Per Unit', 'trim|currency');
             $this->form_validation->set_rules('inventory_item_discount[' . $id . ']', 'Inventory Item Discount', 'trim|discount');
             $this->form_validation->set_rules('inventory_item_amount[' . $id . ']', 'Inventory Item Amount', 'trim|currency');
         }
         foreach ($this->input->post('ledger_dc', TRUE) as $id => $ledger_data) {
             $this->form_validation->set_rules('rate_item[' . $id . ']', 'Rate %', 'trim|rate');
             $this->form_validation->set_rules('amount_item[' . $id . ']', 'Ledger Amount', 'trim|currency');
         }
     }
     /* Repopulating form */
     if ($_POST) {
         $data['entry_number']['value'] = $this->input->post('entry_number', TRUE);
         $data['entry_date']['value'] = $this->input->post('entry_date', TRUE);
         $data['entry_narration']['value'] = $this->input->post('entry_narration', TRUE);
         $data['entry_tag'] = $this->input->post('entry_tag', TRUE);
         $data['has_reconciliation'] = $this->input->post('has_reconciliation', TRUE);
         $data['main_account_active'] = $this->input->post('main_account', TRUE);
         $data['main_entity_active'] = $this->input->post('main_entity', TRUE);
         $data['inventory_item_id'] = $this->input->post('inventory_item_id', TRUE);
         $data['inventory_item_quantity'] = $this->input->post('inventory_item_quantity', TRUE);
         $data['inventory_item_rate_per_unit'] = $this->input->post('inventory_item_rate_per_unit', TRUE);
         $data['inventory_item_discount'] = $this->input->post('inventory_item_discount', TRUE);
         $data['inventory_item_amount'] = $this->input->post('inventory_item_amount', TRUE);
         $data['ledger_dc'] = $this->input->post('ledger_dc', TRUE);
         $data['ledger_id'] = $this->input->post('ledger_id', TRUE);
         $data['rate_item'] = $this->input->post('rate_item', TRUE);
         $data['amount_item'] = $this->input->post('amount_item', TRUE);
     }
     if ($this->form_validation->run() == FALSE) {
         $this->messages->add(validation_errors(), 'error');
         $this->template->load('template', 'inventory/entry/edit', $data);
     } else {
         $data_main_account = $this->input->post('main_account', TRUE);
         $data_main_entity = $this->input->post('main_entity', TRUE);
         $data_all_inventory_item_id = $this->input->post('inventory_item_id', TRUE);
         $data_all_inventory_item_quantity = $this->input->post('inventory_item_quantity', TRUE);
         $data_all_inventory_item_rate_per_unit = $this->input->post('inventory_item_rate_per_unit', TRUE);
         $data_all_inventory_item_discount = $this->input->post('inventory_item_discount', TRUE);
         $data_all_inventory_item_amount = $this->input->post('inventory_item_amount', TRUE);
         $data_all_ledger_id = $this->input->post('ledger_id', TRUE);
         $data_all_ledger_dc = $this->input->post('ledger_dc', TRUE);
         $data_all_rate_item = $this->input->post('rate_item', TRUE);
         $data_all_amount_item = $this->input->post('amount_item', TRUE);
         $data_total_amount = 0;
         /* Setting Inventory Item type */
         if ($current_entry_type['inventory_entry_type'] == '1') {
             $data_inventory_item_type = 1;
         } else {
             $data_inventory_item_type = 2;
         }
         /* Checking for Valid Inventory Ledger account - account */
         if ($current_entry_type['inventory_entry_type'] == '1') {
             $this->db->from('ledgers')->where('id', $data_main_account)->where('type', 2);
         } else {
             $this->db->from('ledgers')->where('id', $data_main_account)->where('type', 3);
         }
         $valid_main_account_q = $this->db->get();
         if ($valid_main_account_q->num_rows() < 1) {
             if ($current_entry_type['inventory_entry_type'] == '1') {
                 $this->messages->add('Invalid Purchase Ledger.', 'error');
             } else {
                 $this->messages->add('Invalid Sale Ledger.', 'error');
             }
             $this->template->load('template', 'inventory/entry/edit', $data);
             return;
         }
         /* Checking for Valid Inventory Ledger account - entity */
         if ($current_entry_type['inventory_entry_type'] == '1') {
             $this->db->from('ledgers')->where('id', $data_main_entity)->where('type', 4)->or_where('type', 1);
         } else {
             $this->db->from('ledgers')->where('id', $data_main_entity)->where('type', 5)->or_where('type', 1);
         }
         $valid_main_account_q = $this->db->get();
         if ($valid_main_account_q->num_rows() < 1) {
             if ($current_entry_type['inventory_entry_type'] == '1') {
                 $this->messages->add('Invalid Creditor (Supplier).', 'error');
             } else {
                 $this->messages->add('Invalid Debtor (Customer).', 'error');
             }
             $this->template->load('template', 'inventory/entry/edit', $data);
             return;
         }
         /* Checking for Valid Inventory Item */
         $inventory_item_present = FALSE;
         $data_total_inventory_amount = 0;
         foreach ($data_all_inventory_item_id as $id => $inventory_data) {
             if ($data_all_inventory_item_id[$id] < 1) {
                 continue;
             }
             /* Check for valid inventory item id */
             $this->db->from('inventory_items')->where('id', $data_all_inventory_item_id[$id]);
             $valid_inventory_item_q = $this->db->get();
             if ($valid_inventory_item_q->num_rows() < 1) {
                 $this->messages->add('Invalid Inventory Item.', 'error');
                 $this->template->load('template', 'inventory/entry/edit', $data);
                 return;
             }
             $inventory_item_present = TRUE;
             $data_total_inventory_amount = float_ops($data_total_inventory_amount, $data_all_inventory_item_amount[$id], '+');
         }
         if (!$inventory_item_present) {
             $this->messages->add('No Inventory Item selected.', 'error');
             $this->template->load('template', 'inventory/entry/edit', $data);
             return;
         }
         /* Checking for Valid Ledgers */
         $data_total_ledger_amount = 0;
         foreach ($data_all_ledger_dc as $id => $ledger_data) {
             if ($data_all_ledger_id[$id] < 1) {
                 continue;
             }
             /* Check for valid ledger id */
             $this->db->from('ledgers')->where('id', $data_all_ledger_id[$id]);
             $valid_ledger_q = $this->db->get();
             if ($valid_ledger_q->num_rows() < 1) {
                 $this->messages->add('Invalid Ledger account.', 'error');
                 $this->template->load('template', 'inventory/entry/edit', $data);
                 return;
             }
             if ($data_all_ledger_dc[$id] == 'D') {
                 $data_total_ledger_amount = float_ops($data_total_ledger_amount, $data_all_amount_item[$id], '+');
             } else {
                 $data_total_ledger_amount = float_ops($data_total_ledger_amount, $data_all_amount_item[$id], '-');
             }
         }
         /* Total amount calculations */
         if ($current_entry_type['inventory_entry_type'] == '1') {
             $data_main_account_total = $data_total_inventory_amount;
             $data_main_entity_total = float_ops($data_total_inventory_amount, $data_total_ledger_amount, '+');
         } else {
             $data_main_account_total = float_ops($data_total_inventory_amount, $data_total_ledger_amount, '+');
             $data_main_entity_total = $data_total_inventory_amount;
         }
         $data_total_amount = float_ops($data_total_inventory_amount, $data_total_ledger_amount, '+');
         if ($data_total_amount < 0) {
             $this->messages->add($current_entry_type['name'] . ' Entry total cannot be negative.', 'error');
             $this->template->load('template', 'inventory/entry/edit', $data);
             return;
         }
         /* Updating main entry */
         if ($current_entry_type['numbering'] == '3') {
             $data_number = $this->input->post('entry_number', TRUE);
             if (!$data_number) {
                 $data_number = NULL;
             }
         } else {
             $data_number = $this->input->post('entry_number', TRUE);
         }
         $data_date = $this->input->post('entry_date', TRUE);
         $data_narration = $this->input->post('entry_narration', TRUE);
         $data_tag = $this->input->post('entry_tag', TRUE);
         if ($data_tag < 1) {
             $data_tag = NULL;
         }
         $data_type = $entry_type_id;
         $data_date = date_php_to_mysql($data_date);
         // Converting date to MySQL
         $data_has_reconciliation = $this->input->post('has_reconciliation', TRUE);
         $this->db->trans_start();
         $update_data = array('number' => $data_number, 'date' => $data_date, 'narration' => $data_narration, 'tag_id' => $data_tag);
         if (!$this->db->where('id', $entry_id)->update('entries', $update_data)) {
             $this->db->trans_rollback();
             $this->messages->add('Error updating Entry.', 'error');
             $this->logger->write_message("error", "Error updating entry details for " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " [id:" . $entry_id . "]");
             $this->template->load('template', 'inventory/entry/edit', $data);
             return;
         }
         /* TODO : Deleting all old ledger data, Bad solution */
         if (!$this->db->delete('inventory_entry_items', array('entry_id' => $entry_id))) {
             $this->db->trans_rollback();
             $this->messages->add('Error deleting previous inventory items from Entry.', 'error');
             $this->logger->write_message("error", "Error deleting previous inventory items from " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " [id:" . $entry_id . "]");
             $this->template->load('template', 'inventory/entry/edit', $data);
             return;
         }
         $this->db->where('inventory_type', 3);
         if (!$this->db->delete('entry_items', array('entry_id' => $entry_id))) {
             $this->db->trans_rollback();
             $this->messages->add('Error deleting previous Ledger accounts from Entry.', 'error');
             $this->logger->write_message("error", "Error deleting previous entry items for " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " [id:" . $entry_id . "]");
             $this->template->load('template', 'inventory/entry/edit', $data);
             return;
         }
         /* Updating main - account */
         $update_data = array('ledger_id' => $data_main_account, 'amount' => $data_main_account_total, 'dc' => '', 'reconciliation_date' => NULL, 'inventory_type' => 1, 'inventory_rate' => '');
         if ($current_entry_type['inventory_entry_type'] == '1') {
             $update_data['dc'] = 'D';
         } else {
             $update_data['dc'] = 'C';
         }
         if (!$this->db->where('entry_id', $entry_id)->where('inventory_type', 1)->update('entry_items', $update_data)) {
             $this->db->trans_rollback();
             if ($current_entry_type['inventory_entry_type'] == '1') {
                 $this->messages->add('Error updating Purchase Ledger account of Entry.', 'error');
                 $this->logger->write_message("error", "Error updating " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " since failed updating purchase ledger " . "[id:" . $data_main_account . "]");
             } else {
                 $this->messages->add('Error updating Sale Ledger account of Entry.', 'error');
                 $this->logger->write_message("error", "Error updating " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " since failed updating sale ledger " . "[id:" . $data_main_account . "]");
             }
             $this->template->load('template', 'inventory/entry/edit', $data);
             return;
         }
         /* Updating main - entity */
         $update_data = array('ledger_id' => $data_main_entity, 'amount' => $data_main_entity_total, 'dc' => '', 'reconciliation_date' => NULL, 'inventory_type' => 2, 'inventory_rate' => '');
         if ($current_entry_type['inventory_entry_type'] == '1') {
             $update_data['dc'] = 'C';
         } else {
             $update_data['dc'] = 'D';
         }
         if (!$this->db->where('entry_id', $entry_id)->where('inventory_type', 2)->update('entry_items', $update_data)) {
             $this->db->trans_rollback();
             if ($current_entry_type['inventory_entry_type'] == '1') {
                 $this->messages->add('Error updating Creditor (Supplier) of Entry.', 'error');
                 $this->logger->write_message("error", "Error updating " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " since failed updating creditor ledger " . "[id:" . $data_main_entity . "]");
             } else {
                 $this->messages->add('Error updating Debtor (Customer) of Entry.', 'error');
                 $this->logger->write_message("error", "Error updating " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " since failed updating debtor ledger " . "[id:" . $data_main_entity . "]");
             }
             $this->template->load('template', 'inventory/entry/edit', $data);
             return;
         }
         /* Adding inventory items */
         $data_all_inventory_item_id = $this->input->post('inventory_item_id', TRUE);
         $data_all_inventory_item_quantity = $this->input->post('inventory_item_quantity', TRUE);
         $data_all_inventory_item_rate_per_unit = $this->input->post('inventory_item_rate_per_unit', TRUE);
         $data_all_inventory_item_discount = $this->input->post('inventory_item_discount', TRUE);
         $data_all_inventory_item_amount = $this->input->post('inventory_item_amount', TRUE);
         foreach ($data_all_inventory_item_id as $id => $inventory_data) {
             $data_inventory_item_id = $data_all_inventory_item_id[$id];
             if ($data_inventory_item_id < 1) {
                 continue;
             }
             $data_inventory_item_quantity = $data_all_inventory_item_quantity[$id];
             $data_inventory_item_rate_per_unit = $data_all_inventory_item_rate_per_unit[$id];
             $data_inventory_item_discount = $data_all_inventory_item_discount[$id];
             $data_inventory_item_amount = $data_all_inventory_item_amount[$id];
             $insert_inventory_data = array('entry_id' => $entry_id, 'inventory_item_id' => $data_inventory_item_id, 'quantity' => $data_inventory_item_quantity, 'rate_per_unit' => $data_inventory_item_rate_per_unit, 'discount' => $data_inventory_item_discount, 'total' => $data_inventory_item_amount, 'type' => $data_inventory_item_type);
             if (!$this->db->insert('inventory_entry_items', $insert_inventory_data)) {
                 $this->db->trans_rollback();
                 $this->messages->add('Error adding Inventory Item - ' . $data_inventory_item_id . ' to Entry.', 'error');
                 $this->logger->write_message("error", "Error adding " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " since failed inserting inventory item " . "[id:" . $data_inventory_item_id . "]");
                 $this->template->load('template', 'inventory/entry/edit', $data);
                 return;
             }
         }
         /* Adding ledger accounts */
         $data_all_ledger_dc = $this->input->post('ledger_dc', TRUE);
         $data_all_ledger_id = $this->input->post('ledger_id', TRUE);
         $data_all_rate_item = $this->input->post('rate_item', TRUE);
         $data_all_amount_item = $this->input->post('amount_item', TRUE);
         foreach ($data_all_ledger_dc as $id => $ledger_data) {
             $data_ledger_dc = $data_all_ledger_dc[$id];
             $data_ledger_id = $data_all_ledger_id[$id];
             if ($data_ledger_id < 1) {
                 continue;
             }
             $data_rate = $data_all_rate_item[$id];
             $data_amount = $data_all_amount_item[$id];
             $insert_ledger_data = array('entry_id' => $entry_id, 'ledger_id' => $data_ledger_id, 'amount' => $data_amount, 'dc' => $data_ledger_dc, 'reconciliation_date' => NULL, 'inventory_type' => 3, 'inventory_rate' => $data_rate);
             if (!$this->db->insert('entry_items', $insert_ledger_data)) {
                 $this->db->trans_rollback();
                 $this->messages->add('Error adding Ledger account - ' . $data_ledger_id . ' to Entry.', 'error');
                 $this->logger->write_message("error", "Error adding " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " since failed inserting entry ledger item " . "[id:" . $data_ledger_id . "]");
                 $this->template->load('template', 'inventory/entry/edit', $data);
                 return;
             }
         }
         /* Updating Debit and Credit Total - entries */
         $update_data = array('dr_total' => $data_total_amount, 'cr_total' => $data_total_amount);
         if (!$this->db->where('id', $entry_id)->update('entries', $update_data)) {
             $this->db->trans_rollback();
             $this->messages->add('Error updating Entry total.', 'error');
             $this->logger->write_message("error", "Error updating " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " since failed updating debit and credit total");
             $this->template->load('template', 'inventory/entry/edit', $data);
             return;
         }
         /* Success */
         $this->db->trans_complete();
         $this->session->set_userdata('entry_updated_show_action', TRUE);
         $this->session->set_userdata('entry_updated_id', $entry_id);
         $this->session->set_userdata('entry_updated_type_id', $entry_type_id);
         $this->session->set_userdata('entry_updated_type_label', $current_entry_type['label']);
         $this->session->set_userdata('entry_updated_type_name', $current_entry_type['name']);
         $this->session->set_userdata('entry_updated_number', $data_number);
         if ($data_has_reconciliation) {
             $this->session->set_userdata('entry_updated_has_reconciliation', TRUE);
         } else {
             $this->session->set_userdata('entry_updated_has_reconciliation', FALSE);
         }
         /* Showing success message in show() method since message is too long for storing it in session */
         $this->logger->write_message("success", "Updated " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " [id:" . $entry_id . "]");
         redirect('entry/show/' . $current_entry_type['label']);
         return;
     }
     return;
 }