Exemplo n.º 1
0
 function index()
 {
     $this->load->model('Ledger_model');
     $this->load->library('accountlist');
     $this->template->set('page_title', 'Welcome to Webzash');
     $this->template->set('add_css', array("css/tufte-graph.css"));
     $this->template->set('add_javascript', array("js/raphael.js", "js/jquery.enumerable.js", "js/jquery.tufte-graph.js"));
     /* Bank and Cash Ledger accounts */
     $this->db->from('ledgers')->where('type', 1);
     $bank_q = $this->db->get();
     if ($bank_q->num_rows() > 0) {
         foreach ($bank_q->result() as $row) {
             $data['bank_cash_account'][] = array('id' => $row->id, 'name' => $row->name, 'balance' => $this->Ledger_model->get_ledger_balance($row->id));
         }
     } else {
         $data['bank_cash_account'] = array();
     }
     /* Calculating total of Assets, Liabilities, Incomes, Expenses */
     $asset = new Accountlist();
     $asset->init(1);
     $data['asset_total'] = $asset->total;
     $liability = new Accountlist();
     $liability->init(2);
     $data['liability_total'] = $liability->total;
     $data['show_asset_liability'] = TRUE;
     if ($data['asset_total'] == 0 && $data['liability_total'] == 0) {
         $data['show_asset_liability'] = FALSE;
     }
     $income = new Accountlist();
     $income->init(3);
     $data['income_total'] = $income->total;
     $expense = new Accountlist();
     $expense->init(4);
     $data['expense_total'] = $expense->total;
     $data['show_income_expense'] = TRUE;
     if ($data['income_total'] == 0 && $data['expense_total'] == 0) {
         $data['show_income_expense'] = FALSE;
     }
     /* Getting Log Messages */
     $data['logs'] = $this->logger->read_recent_messages();
     $this->template->load('template', 'welcome', $data);
     return;
 }
Exemplo n.º 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;
 }
Exemplo n.º 3
0
<?php

$this->load->library('accountlist');
echo "<table>";
echo "<tr valign=\"top\">";
$asset = new Accountlist();
echo "<td>";
$asset->init(0);
echo "<table border=0 cellpadding=5 class=\"simple-table account-table\">";
echo "<thead><tr><th>Account Name</th><th>Type</th><th>O/P Balance</th><th>C/L Balance</th><th></th></tr></thead>";
$asset->account_st_main(-1);
echo "</table>";
echo "</td>";
echo "</tr>";
echo "</table>";
Exemplo n.º 4
0
echo "</table>";
echo "</td>";
$liability_total = -$liability->total;
$asset = new Accountlist();
echo "<td width=\"" . $right_width . "\">";
$asset->init(1);
echo "<table border=0 cellpadding=5 class=\"simple-table balance-sheet-table\" width=\"100%\">";
echo "<thead><tr><th>Assets</th><th align=\"right\">Amount</th></tr></thead>";
$asset->account_st_short(0);
echo "</table>";
echo "</td>";
$asset_total = $asset->total;
echo "</tr>";
$income = new Accountlist();
$income->init(3);
$expense = new Accountlist();
$expense->init(4);
$income_total = -$income->total;
$expense_total = $expense->total;
$pandl = $income_total - $expense_total;
$diffop = $this->Ledger_model->get_diff_op_balance();
/* Liability side */
$total = $liability_total;
echo "<tr valign=\"top\" class=\"total-area\">";
echo "<td>";
echo "<table border=0 cellpadding=5 class=\"balance-sheet-total-table\" width=\"100%\">";
echo "<tr valign=\"top\">";
echo "<td class=\"bold\">Liability and Owners Equity Total</td>";
echo "<td align=\"right\" class=\"bold\">" . convert_cur($liability_total) . "</td>";
echo "</tr>";
/* If Profit then Liability side, If Loss then Asset side */
Exemplo n.º 5
0
 public static function reset_max_depth()
 {
     self::$max_depth = 0;
     self::$temp_max = 0;
 }
Exemplo n.º 6
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;
 }
Exemplo n.º 7
0
    $net_expense = new Accountlist();
    $net_expense->init($row->id);
    $net_expense->account_st_short(0);
    $net_expense_total += $net_expense->total;
}
echo "</table>";
echo "</td>";
/* 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();
echo "<td>";
echo "<table border=0 cellpadding=5 class=\"simple-table profit-loss-table\" width=\"100%\">";
echo "<thead><tr><th>Incomes (Net)</th><th align=\"right\">Amount</th></tr></thead>";
foreach ($net_income_list_q->result() as $row) {
    $net_income = new Accountlist();
    $net_income->init($row->id);
    $net_income->account_st_short(0);
    $net_income_total += $net_income->total;
}
echo "</table>";
echo "</td>";
$net_income_total = -$net_income_total;
/* Converting to positive value since Cr */
echo "</tr>";
/* Calculating Net P/L */
$netpl = $net_income_total - $net_expense_total + $grosspl;
/* Showing Net P/L : Expenses */
$nettotal = $net_expense_total;
echo "<tr valign=\"top\" class=\"total-area\">";
echo "<td>";