function get_entry_name($entry_id, $entry_type_id)
 {
     /* Selecting whether to show debit side Ledger or credit side Ledger */
     $current_entry_type = entry_type_info($entry_type_id);
     $ledger_type = 'C';
     if ($current_entry_type['bank_cash_ledger_restriction'] == 3) {
         $ledger_type = 'D';
     }
     $this->db->select('ledgers.name as name');
     $this->db->from('entry_items')->join('ledgers', 'entry_items.ledger_id = ledgers.id')->where('entry_items.entry_id', $entry_id)->where('entry_items.dc', $ledger_type);
     $ledger_q = $this->db->get();
     if (!($ledger = $ledger_q->row())) {
         return "(Invalid)";
     } else {
         $ledger_multiple = $ledger_q->num_rows() > 1 ? TRUE : FALSE;
         $html = '';
         if ($ledger_multiple) {
             $html .= anchor('entry/view/' . $current_entry_type['label'] . "/" . $entry_id, "(" . $ledger->name . ")", array('title' => 'View ' . $current_entry_type['name'] . ' Entry', 'class' => 'anchor-link-a'));
         } else {
             $html .= anchor('entry/view/' . $current_entry_type['label'] . "/" . $entry_id, $ledger->name, array('title' => 'View ' . $current_entry_type['name'] . ' Entry', 'class' => 'anchor-link-a'));
         }
         return $html;
     }
     return;
 }
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
     $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.id as entry_items_id, entry_items.amount as entry_items_amount, entry_items.dc as entry_items_dc, entry_items.reconciliation_date as entry_items_reconciliation_date');
     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();
 }
 if (!$print_preview) {
     echo form_open('report/reconciliation/' . $reconciliation_type . '/' . $ledger_id . "/" . $page_count);
 }
 echo "<table border=0 cellpadding=5 class=\"simple-table reconciliation-table\">";
 echo "<thead><tr><th>Date</th><th>No.</th><th>Ledger Name</th><th>Type</th><th>Dr Amount</th><th>Cr Amount</th><th>Reconciliation Date</th></tr></thead>";
 $odd_even = "odd";
 foreach ($ledgerst_q->result() as $row) {
     $current_entry_type = entry_type_info($row->entries_entry_type);
     echo "<tr class=\"tr-" . $odd_even;
     if ($row->entry_items_reconciliation_date) {
         echo " tr-reconciled";
     }
     echo "\">";
     echo "<td>";
     echo date_mysql_to_php_display($row->entries_date);
     echo "</td>";
     echo "<td>";
     echo anchor('entry/view/' . $current_entry_type['label'] . '/' . $row->entries_id, full_entry_number($row->entries_entry_type, $row->entries_number), array('title' => 'View ' . $current_entry_type['name'] . ' Entry', 'class' => 'anchor-link-a'));
     echo "</td>";
     /* Getting opposite Ledger name */
     echo "<td>";
     echo $this->Ledger_model->get_opp_ledger_name($row->entries_id, $row->entries_entry_type, $row->entry_items_dc, 'html');
     if ($row->entries_narration) {
Example #4
0
 function email($entry_type, $entry_id = 0)
 {
     $this->load->model('Setting_model');
     $this->load->model('Ledger_model');
     $this->load->library('email');
     /* Check access */
     if (!check_access('email entry')) {
         $this->messages->add('Permission denied.', '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);
     }
     $account_data = $this->Setting_model->get_current();
     /* 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;
     }
     $data['entry_type_id'] = $entry_type_id;
     $data['current_entry_type'] = $current_entry_type;
     $data['entry_id'] = $entry_id;
     $data['entry_number'] = $cur_entry->number;
     $data['email_to'] = array('name' => 'email_to', 'id' => 'email_to', 'size' => '40', 'value' => '');
     /* Form validations */
     $this->form_validation->set_rules('email_to', 'Email to', 'trim|valid_emails|required');
     /* Repopulating form */
     if ($_POST) {
         $data['email_to']['value'] = $this->input->post('email_to', TRUE);
     }
     if ($this->form_validation->run() == FALSE) {
         $data['error'] = validation_errors();
         $this->load->view('entry/email', $data);
         return;
     } else {
         $entry_data['entry_type_id'] = $entry_type_id;
         $entry_data['current_entry_type'] = $current_entry_type;
         $entry_data['entry_number'] = $cur_entry->number;
         $entry_data['entry_date'] = date_mysql_to_php_display($cur_entry->date);
         $entry_data['entry_dr_total'] = $cur_entry->dr_total;
         $entry_data['entry_cr_total'] = $cur_entry->cr_total;
         $entry_data['entry_narration'] = $cur_entry->narration;
         /* Getting Ledger details */
         $this->db->from('entry_items')->where('entry_id', $entry_id)->order_by('dc', 'desc');
         $ledger_q = $this->db->get();
         $counter = 0;
         $entry_data['ledger_data'] = array();
         if ($ledger_q->num_rows() > 0) {
             foreach ($ledger_q->result() as $row) {
                 $entry_data['ledger_data'][$counter] = array('id' => $row->ledger_id, 'name' => $this->Ledger_model->get_name($row->ledger_id), 'dc' => $row->dc, 'amount' => $row->amount);
                 $counter++;
             }
         }
         /* Preparing message */
         $message = $this->load->view('entry/emailpreview', $entry_data, TRUE);
         /* Getting email configuration */
         $config['smtp_timeout'] = '30';
         $config['charset'] = 'utf-8';
         $config['newline'] = "\r\n";
         $config['mailtype'] = "html";
         if ($account_data) {
             $config['protocol'] = $account_data->email_protocol;
             $config['smtp_host'] = $account_data->email_host;
             $config['smtp_port'] = $account_data->email_port;
             $config['smtp_user'] = $account_data->email_username;
             $config['smtp_pass'] = $account_data->email_password;
         } else {
             $data['error'] = 'Invalid account settings.';
         }
         $this->email->initialize($config);
         /* Sending email */
         $this->email->from('', 'Webzash');
         $this->email->to($this->input->post('email_to', TRUE));
         $this->email->subject($current_entry_type['name'] . ' Entry No. ' . full_entry_number($entry_type_id, $cur_entry->number));
         $this->email->message($message);
         if ($this->email->send()) {
             $data['message'] = "Email sent.";
             $this->logger->write_message("success", "Emailed " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $cur_entry->number) . " [id:" . $entry_id . "]");
         } else {
             $data['error'] = "Error sending email. Check you email settings.";
             $this->logger->write_message("error", "Error emailing " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $cur_entry->number) . " [id:" . $entry_id . "]");
         }
         $this->load->view('entry/email', $data);
         return;
     }
     return;
 }
Example #5
0
 function get_opp_ledger_name($entry_id, $entry_type_id, $ledger_type, $output_type)
 {
     $current_entry_type = entry_type_info($entry_type_id);
     $output = '';
     if ($ledger_type == 'D') {
         $opp_ledger_type = 'C';
     } else {
         $opp_ledger_type = 'D';
     }
     $this->db->from('entry_items')->where('entry_id', $entry_id)->where('dc', $opp_ledger_type);
     $opp_entry_name_q = $this->db->get();
     if ($opp_entry_name_d = $opp_entry_name_q->row()) {
         $opp_ledger_name = $this->get_name($opp_entry_name_d->ledger_id);
         if ($opp_entry_name_q->num_rows() > 1) {
             if ($output_type == 'html') {
                 if ($current_entry_type['base_type'] == '1') {
                     $output = anchor('entry/view/' . $current_entry_type['label'] . '/' . $entry_id, "(" . $opp_ledger_name . ")", array('title' => 'View ' . ' Entry', 'class' => 'anchor-link-a'));
                 } else {
                     $output = anchor('inventory/entry/view/' . $current_entry_type['label'] . '/' . $entry_id, "(" . $opp_ledger_name . ")", array('title' => 'View ' . ' Entry', 'class' => 'anchor-link-a'));
                 }
             } else {
                 $output = "(" . $opp_ledger_name . ")";
             }
         } else {
             if ($output_type == 'html') {
                 if ($current_entry_type['base_type'] == '1') {
                     $output = anchor('entry/view/' . $current_entry_type['label'] . '/' . $entry_id, $opp_ledger_name, array('title' => 'View ' . ' Entry', 'class' => 'anchor-link-a'));
                 } else {
                     $output = anchor('inventory/entry/view/' . $current_entry_type['label'] . '/' . $entry_id, $opp_ledger_name, array('title' => 'View ' . ' Entry', 'class' => 'anchor-link-a'));
                 }
             } else {
                 $output = $opp_ledger_name;
             }
         }
     }
     return $output;
 }
Example #6
0
 function email($entry_type, $entry_id = 0)
 {
     $this->load->model('Setting_model');
     $this->load->library('email');
     /* Check access */
     if (!check_access('email inventory entry')) {
         $this->messages->add('Permission denied.', 'error');
         redirect('inventory/transfer/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);
     }
     $account_data = $this->Setting_model->get_current();
     /* 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;
     }
     /* Load current inventory items details */
     $this->db->from('inventory_entry_items')->where('entry_id', $entry_id)->where('type', 1)->order_by('id', 'asc');
     $cur_entry_source_inventory_items = $this->db->get();
     if ($cur_entry_source_inventory_items->num_rows() < 1) {
         $this->messages->add('Entry has no associated source inventory items.', 'error');
     }
     $this->db->from('inventory_entry_items')->where('entry_id', $entry_id)->where('type', 2)->order_by('id', 'asc');
     $cur_entry_dest_inventory_items = $this->db->get();
     if ($cur_entry_dest_inventory_items->num_rows() < 1) {
         $this->messages->add('Entry has no associated destination inventory items.', 'error');
     }
     $data['entry_type_id'] = $entry_type_id;
     $data['current_entry_type'] = $current_entry_type;
     $data['entry_id'] = $entry_id;
     $data['entry_number'] = $cur_entry->number;
     $data['email_to'] = array('name' => 'email_to', 'id' => 'email_to', 'size' => '40', 'value' => '');
     /* Form validations */
     $this->form_validation->set_rules('email_to', 'Email to', 'trim|valid_emails|required');
     /* Repopulating form */
     if ($_POST) {
         $data['email_to']['value'] = $this->input->post('email_to', TRUE);
     }
     if ($this->form_validation->run() == FALSE) {
         $data['error'] = validation_errors();
         $this->load->view('inventory/transfer/email', $data);
         return;
     } else {
         $data['cur_entry'] = $cur_entry;
         $data['cur_entry_source_inventory_items'] = $cur_entry_source_inventory_items;
         $data['cur_entry_dest_inventory_items'] = $cur_entry_dest_inventory_items;
         /* Preparing message */
         $message = $this->load->view('inventory/transfer/emailpreview', $data, TRUE);
         /* Getting email configuration */
         $config['smtp_timeout'] = '30';
         $config['charset'] = 'utf-8';
         $config['newline'] = "\r\n";
         $config['mailtype'] = "html";
         if ($account_data) {
             $config['protocol'] = $account_data->email_protocol;
             $config['smtp_host'] = $account_data->email_host;
             $config['smtp_port'] = $account_data->email_port;
             $config['smtp_user'] = $account_data->email_username;
             $config['smtp_pass'] = $account_data->email_password;
         } else {
             $data['error'] = 'Invalid account settings.';
         }
         $this->email->initialize($config);
         /* Sending email */
         $this->email->from('', 'Webzash');
         $this->email->to($this->input->post('email_to', TRUE));
         $this->email->subject($current_entry_type['name'] . ' Entry No. ' . full_entry_number($entry_type_id, $cur_entry->number));
         $this->email->message($message);
         if ($this->email->send()) {
             $data['message'] = "Email sent.";
             $this->logger->write_message("success", "Emailed " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $cur_entry->number) . " [id:" . $entry_id . "]");
         } else {
             $data['error'] = "Error sending email. Check you email settings.";
             $this->logger->write_message("error", "Error emailing " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $cur_entry->number) . " [id:" . $entry_id . "]");
         }
         $this->load->view('inventory/transfer/email', $data);
         return;
     }
     return;
 }
Example #7
0
             $cur_rate += $row->inventory_entry_items_rate_per_unit;
             $cur_value += $row->inventory_entry_items_total;
         } else {
             $cur_quantity -= $row->inventory_entry_items_quantity;
             $cur_rate -= $row->inventory_entry_items_rate_per_unit;
             $cur_value -= $row->inventory_entry_items_total;
         }
     }
     /* Show relative opening balance */
     echo "<tr class=\"tr-balance\"><td colspan=4>Opening Inventory</td><td>" . $cur_quantity . "</td><td>" . convert_cur($cur_rate) . "</td><td>" . convert_cur($cur_value) . "</td><td></td><td></td><td></td></tr>";
 }
 foreach ($inventory_entry_items_q->result() as $row) {
     $this->db->from('entries')->where('id', $row->entry_id)->limit(1);
     $entries_q = $this->db->get();
     $entries_data = $entries_q->row();
     $current_entry_type = entry_type_info($entries_data->entry_type);
     echo "<tr class=\"tr-" . $odd_even . "\">";
     echo "<td>";
     echo date_mysql_to_php_display($entries_data->date);
     echo "</td>";
     echo "<td>";
     if ($current_entry_type['inventory_entry_type'] == 3) {
         echo anchor('inventory/transfer/view/' . $current_entry_type['label'] . '/' . $entries_data->id, full_entry_number($entries_data->entry_type, $entries_data->number), array('title' => 'View ' . ' Entry', 'class' => 'anchor-link-a'));
     } else {
         echo anchor('inventory/entry/view/' . $current_entry_type['label'] . '/' . $entries_data->id, full_entry_number($entries_data->entry_type, $entries_data->number), array('title' => 'View ' . ' Entry', 'class' => 'anchor-link-a'));
     }
     echo "</td>";
     /* Getting opposite Ledger name */
     echo "<td>";
     echo $this->Ledger_model->get_entry_name($entries_data->id, $entries_data->entry_type);
     echo "</td>";