function is_date_within_range_reconcil($str) { $CI =& get_instance(); $cur_date = date_php_to_mysql($str); $start_date = $CI->config->item('account_fy_start'); $end_date_orig = $CI->config->item('account_fy_end'); $end_date_ts = date_mysql_to_timestamp($end_date_orig); $end_date = date("Y-m-d H:i:s", $end_date_ts + 30 * 24 * 60 * 60); /* Adding one extra month for reconciliation */ if ($cur_date < $start_date) { $CI->form_validation->set_message('is_date_within_range_reconcil', 'The %s is less than start of current financial year.'); return FALSE; } else { if ($cur_date > $end_date) { $CI->form_validation->set_message('is_date_within_range_reconcil', 'The %s is more than end of current financial year plus one month.'); return FALSE; } else { return TRUE; } } }
function reconciliation($reconciliation_type = '', $ledger_id = 0) { /* Pagination setup */ $this->load->library('pagination'); $this->template->set('page_title', 'Reconciliation'); /* Check if path is 'all' or 'pending' */ $data['show_all'] = FALSE; $data['print_preview'] = FALSE; $data['ledger_id'] = $ledger_id; if ($reconciliation_type == 'all') { $data['reconciliation_type'] = 'all'; $data['show_all'] = TRUE; if ($ledger_id > 0) { $this->template->set('nav_links', array('report/download/reconciliation/' . $ledger_id . '/all' => 'Download CSV', 'report/printpreview/reconciliation/' . $ledger_id . '/all' => 'Print Preview')); } } else { if ($reconciliation_type == 'pending') { $data['reconciliation_type'] = 'pending'; $data['show_all'] = FALSE; if ($ledger_id > 0) { $this->template->set('nav_links', array('report/download/reconciliation/' . $ledger_id . '/pending' => 'Download CSV', 'report/printpreview/reconciliation/' . $ledger_id . '/pending' => 'Print Preview')); } } else { $this->messages->add('Invalid path.', 'error'); redirect('report/reconciliation/pending'); return; } } /* Checking for valid ledger id and reconciliation status */ if ($data['ledger_id'] > 0) { $this->db->from('ledgers')->where('id', $data['ledger_id'])->where('reconciliation', 1)->limit(1); if ($this->db->get()->num_rows() < 1) { $this->messages->add('Invalid Ledger account or Reconciliation is not enabled for the Ledger account.', 'error'); redirect('report/reconciliation/' . $reconciliation_type); return; } } else { if ($data['ledger_id'] < 0) { $this->messages->add('Invalid Ledger account.', 'error'); redirect('report/reconciliation/' . $reconciliation_type); return; } } if ($_POST) { /* Check if Ledger account is changed or reconciliation is updated */ if ($_POST['submit'] == 'Submit') { $ledger_id = $this->input->post('ledger_id', TRUE); if ($this->input->post('show_all', TRUE)) { redirect('report/reconciliation/all/' . $ledger_id); return; } else { redirect('report/reconciliation/pending/' . $ledger_id); return; } } else { if ($_POST['submit'] == 'Update') { $data_reconciliation_date = $this->input->post('reconciliation_date', TRUE); /* Form validations */ foreach ($data_reconciliation_date as $id => $row) { /* If reconciliation date is present then check for valid date else only trim */ if ($row) { $this->form_validation->set_rules('reconciliation_date[' . $id . ']', 'Reconciliation date', 'trim|required|is_date|is_date_within_range_reconcil'); } else { $this->form_validation->set_rules('reconciliation_date[' . $id . ']', 'Reconciliation date', 'trim'); } } if ($this->form_validation->run() == FALSE) { $this->messages->add(validation_errors(), 'error'); $this->template->load('template', 'report/reconciliation', $data); return; } else { /* Updating reconciliation date */ foreach ($data_reconciliation_date as $id => $row) { $this->db->trans_start(); if ($row) { $update_data = array('reconciliation_date' => date_php_to_mysql($row)); } else { $update_data = array('reconciliation_date' => NULL); } if (!$this->db->where('id', $id)->update('entry_items', $update_data)) { $this->db->trans_rollback(); $this->messages->add('Error updating reconciliation.', 'error'); $this->logger->write_message("error", "Error updating reconciliation for entry item [id:" . $id . "]"); } else { $this->db->trans_complete(); } } $this->messages->add('Updated reconciliation.', 'success'); $this->logger->write_message("success", 'Updated reconciliation.'); } } } } $this->template->load('template', 'report/reconciliation', $data); return; }
function edit($entry_type, $entry_id = 0) { /* Check access */ if (!check_access('edit entry')) { $this->messages->add('Permission denied.', 'error'); redirect('entry/show/' . $entry_type); return; } /* Check for account lock */ if ($this->config->item('account_locked') == 1) { $this->messages->add('Account is locked.', 'error'); redirect('entry/show/' . $entry_type); return; } /* Entry Type */ $entry_type_id = entry_type_name_to_id($entry_type); if (!$entry_type_id) { $this->messages->add('Invalid Entry type.', 'error'); redirect('entry/show/all'); return; } else { $current_entry_type = entry_type_info($entry_type_id); } $this->template->set('page_title', 'Edit ' . $current_entry_type['name'] . ' Entry'); /* Load current entry details */ if (!($cur_entry = $this->Entry_model->get_entry($entry_id, $entry_type_id))) { $this->messages->add('Invalid Entry.', 'error'); redirect('entry/show/' . $current_entry_type['label']); return; } /* Form fields - Entry */ $data['entry_number'] = array('name' => 'entry_number', 'id' => 'entry_number', 'maxlength' => '11', 'size' => '11', 'value' => $cur_entry->number); $data['entry_date'] = array('name' => 'entry_date', 'id' => 'entry_date', 'maxlength' => '11', 'size' => '11', 'value' => date_mysql_to_php($cur_entry->date)); $data['entry_narration'] = array('name' => 'entry_narration', 'id' => 'entry_narration', 'cols' => '50', 'rows' => '4', 'value' => $cur_entry->narration); $data['entry_id'] = $entry_id; $data['entry_type_id'] = $entry_type_id; $data['current_entry_type'] = $current_entry_type; $data['entry_tag'] = $cur_entry->tag_id; $data['entry_tags'] = $this->Tag_model->get_all_tags(); $data['has_reconciliation'] = FALSE; /* Load current ledger details if not $_POST */ if (!$_POST) { $this->db->from('entry_items')->where('entry_id', $entry_id); $cur_ledgers_q = $this->db->get(); if ($cur_ledgers_q->num_rows <= 0) { $this->messages->add('No associated Ledger accounts found.', 'error'); } $counter = 0; foreach ($cur_ledgers_q->result() as $row) { $data['ledger_dc'][$counter] = $row->dc; $data['ledger_id'][$counter] = $row->ledger_id; if ($row->dc == "D") { $data['dr_amount'][$counter] = $row->amount; $data['cr_amount'][$counter] = ""; } else { $data['dr_amount'][$counter] = ""; $data['cr_amount'][$counter] = $row->amount; } if ($row->reconciliation_date) { $data['has_reconciliation'] = TRUE; } $counter++; } /* Two extra rows */ $data['ledger_dc'][$counter] = 'D'; $data['ledger_id'][$counter] = 0; $data['dr_amount'][$counter] = ""; $data['cr_amount'][$counter] = ""; $counter++; $data['ledger_dc'][$counter] = 'D'; $data['ledger_id'][$counter] = 0; $data['dr_amount'][$counter] = ""; $data['cr_amount'][$counter] = ""; $counter++; } /* Form validations */ if ($current_entry_type['numbering'] == '3') { $this->form_validation->set_rules('entry_number', 'Entry Number', 'trim|is_natural_no_zero|uniqueentrynowithid[' . $entry_type_id . '.' . $entry_id . ']'); } else { $this->form_validation->set_rules('entry_number', 'Entry Number', 'trim|required|is_natural_no_zero|uniqueentrynowithid[' . $entry_type_id . '.' . $entry_id . ']'); } $this->form_validation->set_rules('entry_date', 'Entry Date', 'trim|required|is_date|is_date_within_range'); $this->form_validation->set_rules('entry_narration', 'trim'); $this->form_validation->set_rules('entry_tag', 'Tag', 'trim|is_natural'); /* Debit and Credit amount validation */ if ($_POST) { foreach ($this->input->post('ledger_dc', TRUE) as $id => $ledger_data) { $this->form_validation->set_rules('dr_amount[' . $id . ']', 'Debit Amount', 'trim|currency'); $this->form_validation->set_rules('cr_amount[' . $id . ']', 'Credit Amount', 'trim|currency'); } } /* Repopulating form */ if ($_POST) { $data['entry_number']['value'] = $this->input->post('entry_number', TRUE); $data['entry_date']['value'] = $this->input->post('entry_date', TRUE); $data['entry_narration']['value'] = $this->input->post('entry_narration', TRUE); $data['entry_tag'] = $this->input->post('entry_tag', TRUE); $data['has_reconciliation'] = $this->input->post('has_reconciliation', TRUE); $data['ledger_dc'] = $this->input->post('ledger_dc', TRUE); $data['ledger_id'] = $this->input->post('ledger_id', TRUE); $data['dr_amount'] = $this->input->post('dr_amount', TRUE); $data['cr_amount'] = $this->input->post('cr_amount', TRUE); } if ($this->form_validation->run() == FALSE) { $this->messages->add(validation_errors(), 'error'); $this->template->load('template', 'entry/edit', $data); } else { /* Checking for Valid Ledgers account and Debit and Credit Total */ $data_all_ledger_id = $this->input->post('ledger_id', TRUE); $data_all_ledger_dc = $this->input->post('ledger_dc', TRUE); $data_all_dr_amount = $this->input->post('dr_amount', TRUE); $data_all_cr_amount = $this->input->post('cr_amount', TRUE); $dr_total = 0; $cr_total = 0; $bank_cash_present = FALSE; /* Whether atleast one Ledger account is Bank or Cash account */ $non_bank_cash_present = FALSE; /* Whether atleast one Ledger account is NOT a Bank or Cash account */ foreach ($data_all_ledger_dc as $id => $ledger_data) { if ($data_all_ledger_id[$id] < 1) { continue; } /* Check for valid ledger id */ $this->db->from('ledgers')->where('id', $data_all_ledger_id[$id]); $valid_ledger_q = $this->db->get(); if ($valid_ledger_q->num_rows() < 1) { $this->messages->add('Invalid Ledger account.', 'error'); $this->template->load('template', 'entry/edit', $data); return; } else { /* Check for valid ledger type */ $valid_ledger = $valid_ledger_q->row(); if ($current_entry_type['bank_cash_ledger_restriction'] == '2') { if ($data_all_ledger_dc[$id] == 'D' && $valid_ledger->type == 1) { $bank_cash_present = TRUE; } if ($valid_ledger->type != 1) { $non_bank_cash_present = TRUE; } } else { if ($current_entry_type['bank_cash_ledger_restriction'] == '3') { if ($data_all_ledger_dc[$id] == 'C' && $valid_ledger->type == 1) { $bank_cash_present = TRUE; } if ($valid_ledger->type != 1) { $non_bank_cash_present = TRUE; } } else { if ($current_entry_type['bank_cash_ledger_restriction'] == '4') { if ($valid_ledger->type != 1) { $this->messages->add('Invalid Ledger account. ' . $current_entry_type['name'] . ' Entry can have only Bank or Cash Ledger accounts.', 'error'); $this->template->load('template', 'entry/edit', $data); return; } } else { if ($current_entry_type['bank_cash_ledger_restriction'] == '5') { if ($valid_ledger->type == 1) { $this->messages->add('Invalid Ledger account. ' . $current_entry_type['name'] . ' Entry cannot have any Bank or Cash Ledger account.', 'error'); $this->template->load('template', 'entry/edit', $data); return; } } } } } } if ($data_all_ledger_dc[$id] == "D") { $dr_total = float_ops($dr_total, $data_all_dr_amount[$id], '+'); } else { $cr_total = float_ops($cr_total, $data_all_cr_amount[$id], '+'); } } if (float_ops($dr_total, $cr_total, '!=')) { $this->messages->add('Debit and Credit Total does not match!', 'error'); $this->template->load('template', 'entry/edit', $data); return; } else { if (float_ops($dr_total, 0, '==') && float_ops($cr_total, 0, '==')) { $this->messages->add('Cannot save empty Entry.', 'error'); $this->template->load('template', 'entry/edit', $data); return; } } /* Check if atleast one Bank or Cash Ledger account is present */ if ($current_entry_type['bank_cash_ledger_restriction'] == '2') { if (!$bank_cash_present) { $this->messages->add('Need to Debit atleast one Bank or Cash Ledger account.', 'error'); $this->template->load('template', 'entry/edit', $data); return; } if (!$non_bank_cash_present) { $this->messages->add('Need to Debit or Credit atleast one NON - Bank or Cash Ledger account.', 'error'); $this->template->load('template', 'entry/edit', $data); return; } } else { if ($current_entry_type['bank_cash_ledger_restriction'] == '3') { if (!$bank_cash_present) { $this->messages->add('Need to Credit atleast one Bank or Cash Ledger account.', 'error'); $this->template->load('template', 'entry/edit', $data); return; } if (!$non_bank_cash_present) { $this->messages->add('Need to Debit or Credit atleast one NON - Bank or Cash Ledger account.', 'error'); $this->template->load('template', 'entry/edit', $data); return; } } } /* Updating main entry */ if ($current_entry_type['numbering'] == '3') { $data_number = $this->input->post('entry_number', TRUE); if (!$data_number) { $data_number = NULL; } } else { $data_number = $this->input->post('entry_number', TRUE); } $data_date = $this->input->post('entry_date', TRUE); $data_narration = $this->input->post('entry_narration', TRUE); $data_tag = $this->input->post('entry_tag', TRUE); if ($data_tag < 1) { $data_tag = NULL; } $data_type = $entry_type_id; $data_date = date_php_to_mysql($data_date); // Converting date to MySQL $data_has_reconciliation = $this->input->post('has_reconciliation', TRUE); $this->db->trans_start(); $update_data = array('number' => $data_number, 'date' => $data_date, 'narration' => $data_narration, 'tag_id' => $data_tag); if (!$this->db->where('id', $entry_id)->update('entries', $update_data)) { $this->db->trans_rollback(); $this->messages->add('Error updating Entry.', 'error'); $this->logger->write_message("error", "Error updating entry details for " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " [id:" . $entry_id . "]"); $this->template->load('template', 'entry/edit', $data); return; } /* TODO : Deleting all old ledger data, Bad solution */ if (!$this->db->delete('entry_items', array('entry_id' => $entry_id))) { $this->db->trans_rollback(); $this->messages->add('Error deleting previous Ledger accounts from Entry.', 'error'); $this->logger->write_message("error", "Error deleting previous entry items for " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " [id:" . $entry_id . "]"); $this->template->load('template', 'entry/edit', $data); return; } /* Adding ledger accounts */ $data_all_ledger_dc = $this->input->post('ledger_dc', TRUE); $data_all_ledger_id = $this->input->post('ledger_id', TRUE); $data_all_dr_amount = $this->input->post('dr_amount', TRUE); $data_all_cr_amount = $this->input->post('cr_amount', TRUE); $dr_total = 0; $cr_total = 0; foreach ($data_all_ledger_dc as $id => $ledger_data) { $data_ledger_dc = $data_all_ledger_dc[$id]; $data_ledger_id = $data_all_ledger_id[$id]; if ($data_ledger_id < 1) { continue; } $data_amount = 0; if ($data_all_ledger_dc[$id] == "D") { $data_amount = $data_all_dr_amount[$id]; $dr_total = float_ops($dr_total, $data_all_dr_amount[$id], '+'); } else { $data_amount = $data_all_cr_amount[$id]; $cr_total = float_ops($cr_total, $data_all_cr_amount[$id], '+'); } $insert_ledger_data = array('entry_id' => $entry_id, 'ledger_id' => $data_ledger_id, 'amount' => $data_amount, 'dc' => $data_ledger_dc); if (!$this->db->insert('entry_items', $insert_ledger_data)) { $this->db->trans_rollback(); $this->messages->add('Error adding Ledger account - ' . $data_ledger_id . ' to Entry.', 'error'); $this->logger->write_message("error", "Error adding Ledger account item [id:" . $data_ledger_id . "] for " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " [id:" . $entry_id . "]"); $this->template->load('template', 'entry/edit', $data); return; } } /* Updating Debit and Credit Total in entries table */ $update_data = array('dr_total' => $dr_total, 'cr_total' => $cr_total); if (!$this->db->where('id', $entry_id)->update('entries', $update_data)) { $this->db->trans_rollback(); $this->messages->add('Error updating Entry total.', 'error'); $this->logger->write_message("error", "Error updating entry total for " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " [id:" . $entry_id . "]"); $this->template->load('template', 'entry/edit', $data); return; } /* Success */ $this->db->trans_complete(); $this->session->set_userdata('entry_updated_show_action', TRUE); $this->session->set_userdata('entry_updated_id', $entry_id); $this->session->set_userdata('entry_updated_type_id', $entry_type_id); $this->session->set_userdata('entry_updated_type_label', $current_entry_type['label']); $this->session->set_userdata('entry_updated_type_name', $current_entry_type['name']); $this->session->set_userdata('entry_updated_number', $data_number); if ($data_has_reconciliation) { $this->session->set_userdata('entry_updated_has_reconciliation', TRUE); } else { $this->session->set_userdata('entry_updated_has_reconciliation', FALSE); } /* Showing success message in show() method since message is too long for storing it in session */ $this->logger->write_message("success", "Updated " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " [id:" . $entry_id . "]"); redirect('entry/show/' . $current_entry_type['label']); return; } return; }
function index() { $this->load->helper('file'); $this->template->set('page_title', 'Create account'); /* Form fields */ $default_start = '01/04/'; $default_end = '31/03/'; if (date('n') > 3) { $default_start .= date('Y'); $default_end .= date('Y') + 1; } else { $default_start .= date('Y') - 1; $default_end .= date('Y'); } /* Form fields */ $data['account_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['account_address'] = array('name' => 'account_address', 'id' => 'account_address', 'rows' => '4', 'cols' => '47', 'value' => ''); $data['account_email'] = array('name' => 'account_email', 'id' => 'account_email', 'maxlength' => '100', 'size' => '40', 'value' => ''); $data['fy_start'] = array('name' => 'fy_start', 'id' => 'fy_start', 'maxlength' => '11', 'size' => '11', 'value' => $default_start); $data['fy_end'] = array('name' => 'fy_end', 'id' => 'fy_end', 'maxlength' => '11', 'size' => '11', 'value' => $default_end); $data['account_currency'] = array('name' => 'account_currency', 'id' => 'account_currency', 'maxlength' => '10', 'size' => '10', 'value' => ''); $data['account_date_options'] = array('dd/mm/yyyy' => 'Day / Month / Year', 'mm/dd/yyyy' => 'Month / Day / Year', 'yyyy/mm/dd' => 'Year / Month / Day'); $data['account_date'] = 'dd/mm/yyyy'; $data['account_timezone'] = 'UTC'; $data['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; /* Form validations */ $this->form_validation->set_rules('account_label', 'Label', 'trim|required|min_length[2]|max_length[30]|alpha_numeric'); $this->form_validation->set_rules('account_name', 'Account Name', 'trim|required|min_length[2]|max_length[100]'); $this->form_validation->set_rules('account_address', 'Account Address', 'trim|max_length[255]'); $this->form_validation->set_rules('account_email', 'Account Email', 'trim|valid_email'); if ($_POST) { $this->config->set_item('account_date_format', $this->input->post('account_date', TRUE)); $this->form_validation->set_rules('fy_start', 'Financial Year Start', 'trim|required|is_date'); $this->form_validation->set_rules('fy_end', 'Financial Year End', 'trim|required|is_date'); } $this->form_validation->set_rules('account_currency', 'Currency', 'trim|max_length[10]'); $this->form_validation->set_rules('account_date', 'Date', 'trim|max_length[10]'); $this->form_validation->set_rules('account_timezone', 'Timezone', 'trim|max_length[6]'); $this->form_validation->set_rules('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['account_address']['value'] = $this->input->post('account_address', TRUE); $data['account_email']['value'] = $this->input->post('account_email', TRUE); $data['fy_start']['value'] = $this->input->post('fy_start', TRUE); $data['fy_end']['value'] = $this->input->post('fy_end', TRUE); $data['account_currency']['value'] = $this->input->post('account_currency', TRUE); $data['account_date'] = $this->input->post('account_date', TRUE); $data['account_timezone'] = $this->input->post('account_timezone', TRUE); $data['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('admin_template', 'admin/create', $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->input->post('account_address', TRUE); $data_account_email = $this->input->post('account_email', TRUE); $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->input->post('account_currency', TRUE); $data_account_date_form = $this->input->post('account_date', TRUE); /* Checking for valid format */ if ($data_account_date_form == "dd/mm/yyyy") { $data_account_date = "dd/mm/yyyy"; } else { if ($data_account_date_form == "mm/dd/yyyy") { $data_account_date = "mm/dd/yyyy"; } else { if ($data_account_date_form == "yyyy/mm/dd") { $data_account_date = "yyyy/mm/dd"; } else { $data_account_date = "dd/mm/yyyy"; } } } $data_account_timezone = $this->input->post('timezones', TRUE); $data_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('admin_template', 'admin/create', $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('admin_template', 'admin/create', $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('admin_template', 'admin/create', $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('admin_template', 'admin/create', $data); return; } mysql_close($new_link); } else { $this->messages->add('Error connecting to database. ' . mysql_error(), 'error'); $this->template->load('admin_template', 'admin/create', $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('admin_template', 'admin/create', $data); return; } else { if ($newacc->_error_message() != "") { $this->messages->add('Error connecting to database. ' . $newacc->_error_message(), 'error'); $this->template->load('admin_template', 'admin/create', $data); return; } else { if ($newacc->query("SHOW TABLES")->num_rows() > 0) { $this->messages->add('Selected database in not empty.', 'error'); $this->template->load('admin_template', 'admin/create', $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('admin_template', 'admin/create', $data); return; } } $this->messages->add('Initialized account database.', 'success'); /* Initial account setup */ $setup_initial_data = read_file('system/application/controllers/admin/initialize.sql'); $setup_initial_data_array = explode(";", $setup_initial_data); $newacc->trans_start(); foreach ($setup_initial_data_array as $row) { if (strlen($row) < 5) { continue; } $newacc->query($row); if ($newacc->_error_message() != "") { $newacc->trans_rollback(); $this->messages->add('Error initializing basic accounts data.', 'error'); $this->template->load('admin_template', 'admin/create', $data); return; } } $newacc->trans_complete(); $this->messages->add('Initialized basic accounts data.', '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, account_locked, 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, 0, 4))) { $newacc->trans_rollback(); $this->messages->add('Error adding account settings.', 'error'); $this->template->load('admin_template', 'admin/create', $data); return; } else { $newacc->trans_complete(); $this->messages->add('Added account settings.', 'success'); } /* 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 create 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('admin'); return; } } } } return; }
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; }
function edit($entry_type, $entry_id = 0) { /* Check access */ if (!check_access('edit inventory entry')) { $this->messages->add('Permission denied.', 'error'); redirect('inventory/transfer/show/' . $entry_type); return; } /* Check for account lock */ if ($this->config->item('account_locked') == 1) { $this->messages->add('Account is locked.', 'error'); redirect('entry/show/' . $entry_type); return; } /* Entry Type */ $entry_type_id = entry_type_name_to_id($entry_type); if (!$entry_type_id) { $this->messages->add('Invalid Entry type.', 'error'); redirect('entry/show/all'); return; } else { $current_entry_type = entry_type_info($entry_type_id); } $this->template->set('page_title', 'Edit ' . $current_entry_type['name'] . ' Entry'); /* Load current entry details */ if (!($cur_entry = $this->Entry_model->get_entry($entry_id, $entry_type_id))) { $this->messages->add('Invalid Entry.', 'error'); redirect('entry/show/' . $current_entry_type['label']); return; } /* Form fields */ $data['entry_number'] = array('name' => 'entry_number', 'id' => 'entry_number', 'maxlength' => '11', 'size' => '11', 'value' => $cur_entry->number); $data['entry_date'] = array('name' => 'entry_date', 'id' => 'entry_date', 'maxlength' => '11', 'size' => '11', 'value' => date_mysql_to_php($cur_entry->date)); $data['entry_narration'] = array('name' => 'entry_narration', 'id' => 'entry_narration', 'cols' => '50', 'rows' => '4', 'value' => $cur_entry->narration); $data['entry_id'] = $entry_id; $data['entry_type_id'] = $entry_type_id; $data['current_entry_type'] = $current_entry_type; $data['entry_tag'] = $cur_entry->tag_id; $data['entry_tags'] = $this->Tag_model->get_all_tags(); /* Load current ledger details if not $_POST */ if (!$_POST) { /* source inventory items */ $this->db->from('inventory_entry_items')->where('entry_id', $entry_id)->where('type', 2); $cur_inventory_item_q = $this->db->get(); $counter = 0; foreach ($cur_inventory_item_q->result() as $row) { $data['source_inventory_item_id'][$counter] = $row->inventory_item_id; $data['source_inventory_item_quantity'][$counter] = $row->quantity; $data['source_inventory_item_rate_per_unit'][$counter] = $row->rate_per_unit; $data['source_inventory_item_amount'][$counter] = $row->total; $counter++; } /* one extra rows */ $data['source_inventory_item_id'][$counter] = '0'; $data['source_inventory_item_quantity'][$counter] = ""; $data['source_inventory_item_rate_per_unit'][$counter] = ""; $data['source_inventory_item_amount'][$counter] = ""; $counter++; /* destination inventory items */ $this->db->from('inventory_entry_items')->where('entry_id', $entry_id)->where('type', 1); $cur_inventory_item_q = $this->db->get(); $counter = 0; foreach ($cur_inventory_item_q->result() as $row) { $data['dest_inventory_item_id'][$counter] = $row->inventory_item_id; $data['dest_inventory_item_quantity'][$counter] = $row->quantity; $data['dest_inventory_item_rate_per_unit'][$counter] = $row->rate_per_unit; $data['dest_inventory_item_amount'][$counter] = $row->total; $counter++; } /* one extra rows */ $data['dest_inventory_item_id'][$counter] = '0'; $data['dest_inventory_item_quantity'][$counter] = ""; $data['dest_inventory_item_rate_per_unit'][$counter] = ""; $data['dest_inventory_item_amount'][$counter] = ""; $counter++; } /* Form validations */ if ($current_entry_type['numbering'] == '3') { $this->form_validation->set_rules('entry_number', 'Entry Number', 'trim|is_natural_no_zero|uniqueentrynowithid[' . $entry_type_id . '.' . $entry_id . ']'); } else { $this->form_validation->set_rules('entry_number', 'Entry Number', 'trim|required|is_natural_no_zero|uniqueentrynowithid[' . $entry_type_id . '.' . $entry_id . ']'); } $this->form_validation->set_rules('entry_date', 'Entry Date', 'trim|required|is_date|is_date_within_range'); $this->form_validation->set_rules('entry_narration', 'trim'); $this->form_validation->set_rules('entry_tag', 'Tag', 'trim|is_natural'); /* Debit and Credit amount validation */ if ($_POST) { foreach ($this->input->post('source_inventory_item_id', TRUE) as $id => $inventory_data) { $this->form_validation->set_rules('source_inventory_item_quantity[' . $id . ']', 'Inventory Item Quantity', 'trim|quantity'); $this->form_validation->set_rules('source_inventory_item_rate_per_unit[' . $id . ']', 'Inventory Item Rate Per Unit', 'trim|currency'); $this->form_validation->set_rules('source_inventory_item_amount[' . $id . ']', 'Inventory Item Amount', 'trim|currency'); } foreach ($this->input->post('dest_inventory_item_id', TRUE) as $id => $inventory_data) { $this->form_validation->set_rules('dest_inventory_item_quantity[' . $id . ']', 'Inventory Item Quantity', 'trim|quantity'); $this->form_validation->set_rules('dest_inventory_item_rate_per_unit[' . $id . ']', 'Inventory Item Rate Per Unit', 'trim|currency'); $this->form_validation->set_rules('dest_inventory_item_amount[' . $id . ']', 'Inventory Item Amount', 'trim|currency'); } } /* Repopulating form */ if ($_POST) { $data['entry_number']['value'] = $this->input->post('entry_number', TRUE); $data['entry_date']['value'] = $this->input->post('entry_date', TRUE); $data['entry_narration']['value'] = $this->input->post('entry_narration', TRUE); $data['entry_tag'] = $this->input->post('entry_tag', TRUE); $data['source_inventory_item_id'] = $this->input->post('source_inventory_item_id', TRUE); $data['source_inventory_item_quantity'] = $this->input->post('source_inventory_item_quantity', TRUE); $data['source_inventory_item_rate_per_unit'] = $this->input->post('source_inventory_item_rate_per_unit', TRUE); $data['source_inventory_item_amount'] = $this->input->post('source_inventory_item_amount', TRUE); $data['dest_inventory_item_id'] = $this->input->post('dest_inventory_item_id', TRUE); $data['dest_inventory_item_quantity'] = $this->input->post('dest_inventory_item_quantity', TRUE); $data['dest_inventory_item_rate_per_unit'] = $this->input->post('dest_inventory_item_rate_per_unit', TRUE); $data['dest_inventory_item_amount'] = $this->input->post('dest_inventory_item_amount', TRUE); } if ($this->form_validation->run() == FALSE) { $this->messages->add(validation_errors(), 'error'); $this->template->load('template', 'inventory/transfer/edit', $data); } else { $data_all_source_inventory_item_id = $this->input->post('source_inventory_item_id', TRUE); $data_all_source_inventory_item_quantity = $this->input->post('source_inventory_item_quantity', TRUE); $data_all_source_inventory_item_rate_per_unit = $this->input->post('source_inventory_item_rate_per_unit', TRUE); $data_all_source_inventory_item_amount = $this->input->post('source_inventory_item_amount', TRUE); $data_all_dest_inventory_item_id = $this->input->post('dest_inventory_item_id', TRUE); $data_all_dest_inventory_item_quantity = $this->input->post('dest_inventory_item_quantity', TRUE); $data_all_dest_inventory_item_rate_per_unit = $this->input->post('dest_inventory_item_rate_per_unit', TRUE); $data_all_dest_inventory_item_amount = $this->input->post('dest_inventory_item_amount', TRUE); /* Setting Inventory Item type */ if ($current_entry_type['inventory_entry_type'] == '1') { $data_inventory_item_type = 1; } else { $data_inventory_item_type = 2; } /* Checking for Valid Inventory Item */ $source_inventory_item_present = FALSE; $data_total_source_inventory_amount = 0; foreach ($data_all_source_inventory_item_id as $id => $inventory_data) { if ($data_all_source_inventory_item_id[$id] < 1) { continue; } /* Check for valid inventory item id */ $this->db->from('inventory_items')->where('id', $data_all_source_inventory_item_id[$id]); $valid_inventory_item_q = $this->db->get(); if ($valid_inventory_item_q->num_rows() < 1) { $this->messages->add('Invalid Source Inventory Item.', 'error'); $this->template->load('template', 'inventory/transfer/add', $data); return; } $source_inventory_item_present = TRUE; $data_total_source_inventory_amount = float_ops($data_total_source_inventory_amount, $data_all_source_inventory_item_amount[$id], '+'); } if (!$source_inventory_item_present) { $this->messages->add('No Soruce Inventory Item selected.', 'error'); $this->template->load('template', 'inventory/transfer/add', $data); return; } $dest_inventory_item_present = FALSE; $data_total_dest_inventory_amount = 0; foreach ($data_all_dest_inventory_item_id as $id => $inventory_data) { if ($data_all_dest_inventory_item_id[$id] < 1) { continue; } /* Check for valid inventory item id */ $this->db->from('inventory_items')->where('id', $data_all_dest_inventory_item_id[$id]); $valid_inventory_item_q = $this->db->get(); if ($valid_inventory_item_q->num_rows() < 1) { $this->messages->add('Invalid Destination Inventory Item.', 'error'); $this->template->load('template', 'inventory/transfer/add', $data); return; } $dest_inventory_item_present = TRUE; $data_total_dest_inventory_amount = float_ops($data_total_dest_inventory_amount, $data_all_dest_inventory_item_amount[$id], '+'); } if (!$dest_inventory_item_present) { $this->messages->add('No Destination Inventory Item selected.', 'error'); $this->template->load('template', 'inventory/transfer/add', $data); return; } /* Total amount calculations */ if ($data_total_source_inventory_amount < 0) { $this->messages->add('Source total cannot be negative.', 'error'); $this->template->load('template', 'inventory/transfer/add', $data); return; } if ($data_total_dest_inventory_amount < 0) { $this->messages->add('Destination total cannot be negative.', 'error'); $this->template->load('template', 'inventory/transfer/add', $data); return; } /* Updating main entry */ if ($current_entry_type['numbering'] == '3') { $data_number = $this->input->post('entry_number', TRUE); if (!$data_number) { $data_number = NULL; } } else { $data_number = $this->input->post('entry_number', TRUE); } $data_date = $this->input->post('entry_date', TRUE); $data_narration = $this->input->post('entry_narration', TRUE); $data_tag = $this->input->post('entry_tag', TRUE); if ($data_tag < 1) { $data_tag = NULL; } $data_type = $entry_type_id; $data_date = date_php_to_mysql($data_date); // Converting date to MySQL $this->db->trans_start(); $update_data = array('number' => $data_number, 'date' => $data_date, 'narration' => $data_narration, 'tag_id' => $data_tag, 'dr_total' => $data_total_source_inventory_amount, 'cr_total' => $data_total_dest_inventory_amount); if (!$this->db->where('id', $entry_id)->update('entries', $update_data)) { $this->db->trans_rollback(); $this->messages->add('Error updating Entry.', 'error'); $this->logger->write_message("error", "Error updating entry details for " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " [id:" . $entry_id . "]"); $this->template->load('template', 'inventory/transfer/edit', $data); return; } /* TODO : Deleting all old inventory item */ if (!$this->db->delete('inventory_entry_items', array('entry_id' => $entry_id))) { $this->db->trans_rollback(); $this->messages->add('Error deleting previous inventory items from Entry.', 'error'); $this->logger->write_message("error", "Error deleting previous inventory items from " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " [id:" . $entry_id . "]"); $this->template->load('template', 'inventory/transfer/edit', $data); return; } /* Adding source inventory items */ $data_all_source_inventory_item_id = $this->input->post('source_inventory_item_id', TRUE); $data_all_source_inventory_item_quantity = $this->input->post('source_inventory_item_quantity', TRUE); $data_all_source_inventory_item_rate_per_unit = $this->input->post('source_inventory_item_rate_per_unit', TRUE); $data_all_source_inventory_item_amount = $this->input->post('source_inventory_item_amount', TRUE); foreach ($data_all_source_inventory_item_id as $id => $inventory_data) { $data_source_inventory_item_id = $data_all_source_inventory_item_id[$id]; if ($data_source_inventory_item_id < 1) { continue; } $data_source_inventory_item_quantity = $data_all_source_inventory_item_quantity[$id]; $data_source_inventory_item_rate_per_unit = $data_all_source_inventory_item_rate_per_unit[$id]; $data_source_inventory_item_amount = $data_all_source_inventory_item_amount[$id]; $insert_inventory_data = array('entry_id' => $entry_id, 'inventory_item_id' => $data_source_inventory_item_id, 'quantity' => $data_source_inventory_item_quantity, 'rate_per_unit' => $data_source_inventory_item_rate_per_unit, 'discount' => '', 'total' => $data_source_inventory_item_amount, 'type' => '2'); if (!$this->db->insert('inventory_entry_items', $insert_inventory_data)) { $this->db->trans_rollback(); $this->messages->add('Error adding Inventory Item - ' . $data_source_inventory_item_id . ' to Entry.', 'error'); $this->logger->write_message("error", "Error adding " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " since failed inserting inventory item " . "[id:" . $data_source_inventory_item_id . "]"); $this->template->load('template', 'inventory/transfer/add', $data); return; } } /* Adding destination inventory items */ $data_all_dest_inventory_item_id = $this->input->post('dest_inventory_item_id', TRUE); $data_all_dest_inventory_item_quantity = $this->input->post('dest_inventory_item_quantity', TRUE); $data_all_dest_inventory_item_rate_per_unit = $this->input->post('dest_inventory_item_rate_per_unit', TRUE); $data_all_dest_inventory_item_amount = $this->input->post('dest_inventory_item_amount', TRUE); foreach ($data_all_dest_inventory_item_id as $id => $inventory_data) { $data_dest_inventory_item_id = $data_all_dest_inventory_item_id[$id]; if ($data_dest_inventory_item_id < 1) { continue; } $data_dest_inventory_item_quantity = $data_all_dest_inventory_item_quantity[$id]; $data_dest_inventory_item_rate_per_unit = $data_all_dest_inventory_item_rate_per_unit[$id]; $data_dest_inventory_item_amount = $data_all_dest_inventory_item_amount[$id]; $insert_inventory_data = array('entry_id' => $entry_id, 'inventory_item_id' => $data_dest_inventory_item_id, 'quantity' => $data_dest_inventory_item_quantity, 'rate_per_unit' => $data_dest_inventory_item_rate_per_unit, 'discount' => '', 'total' => $data_dest_inventory_item_amount, 'type' => '1'); if (!$this->db->insert('inventory_entry_items', $insert_inventory_data)) { $this->db->trans_rollback(); $this->messages->add('Error adding Inventory Item - ' . $data_dest_inventory_item_id . ' to Entry.', 'error'); $this->logger->write_message("error", "Error adding " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " since failed inserting inventory item " . "[id:" . $data_dest_inventory_item_id . "]"); $this->template->load('template', 'inventory/transfer/add', $data); return; } } if (!$this->db->where('id', $entry_id)->update('entries', $update_data)) { $this->db->trans_rollback(); $this->messages->add('Error updating Entry total.', 'error'); $this->logger->write_message("error", "Error updating " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " since failed updating debit and credit total"); $this->template->load('template', 'inventory/transfer/edit', $data); return; } /* Success */ $this->db->trans_complete(); $this->session->set_userdata('entry_updated_show_action', TRUE); $this->session->set_userdata('entry_updated_id', $entry_id); $this->session->set_userdata('entry_updated_type_id', $entry_type_id); $this->session->set_userdata('entry_updated_type_label', $current_entry_type['label']); $this->session->set_userdata('entry_updated_type_name', $current_entry_type['name']); $this->session->set_userdata('entry_updated_number', $data_number); $this->session->set_userdata('entry_updated_has_reconciliation', FALSE); /* Showing success message in show() method since message is too long for storing it in session */ $this->logger->write_message("success", "Updated " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " [id:" . $entry_id . "]"); redirect('entry/show/' . $current_entry_type['label']); return; } return; }
function edit($entry_type, $entry_id = 0) { /* Check access */ if (!check_access('edit inventory entry')) { $this->messages->add('Permission denied.', 'error'); redirect('inventory/entry/show/' . $entry_type); return; } /* Check for account lock */ if ($this->config->item('account_locked') == 1) { $this->messages->add('Account is locked.', 'error'); redirect('entry/show/' . $entry_type); return; } /* Entry Type */ $entry_type_id = entry_type_name_to_id($entry_type); if (!$entry_type_id) { $this->messages->add('Invalid Entry type.', 'error'); redirect('entry/show/all'); return; } else { $current_entry_type = entry_type_info($entry_type_id); } $this->template->set('page_title', 'Edit ' . $current_entry_type['name'] . ' Entry'); /* Load current entry details */ if (!($cur_entry = $this->Entry_model->get_entry($entry_id, $entry_type_id))) { $this->messages->add('Invalid Entry.', 'error'); redirect('entry/show/' . $current_entry_type['label']); return; } /* Form fields - Entry */ $data['entry_number'] = array('name' => 'entry_number', 'id' => 'entry_number', 'maxlength' => '11', 'size' => '11', 'value' => $cur_entry->number); $data['entry_date'] = array('name' => 'entry_date', 'id' => 'entry_date', 'maxlength' => '11', 'size' => '11', 'value' => date_mysql_to_php($cur_entry->date)); $data['entry_narration'] = array('name' => 'entry_narration', 'id' => 'entry_narration', 'cols' => '50', 'rows' => '4', 'value' => $cur_entry->narration); $data['entry_id'] = $entry_id; $data['main_account_active'] = 0; $data['main_entity_active'] = 0; $data['entry_type_id'] = $entry_type_id; $data['current_entry_type'] = $current_entry_type; $data['entry_tag'] = $cur_entry->tag_id; $data['entry_tags'] = $this->Tag_model->get_all_tags(); $data['has_reconciliation'] = FALSE; /* Load current ledger details if not $_POST */ if (!$_POST) { /* main - account */ $this->db->from('entry_items')->where('entry_id', $entry_id)->where('inventory_type', 1); $cur_main_account_q = $this->db->get(); $cur_main_account = $cur_main_account_q->row(); $data['main_account_active'] = $cur_main_account->ledger_id; /* main - entity */ $this->db->from('entry_items')->where('entry_id', $entry_id)->where('inventory_type', 2); $cur_main_entity_q = $this->db->get(); $cur_main_entity = $cur_main_entity_q->row(); $data['main_entity_active'] = $cur_main_entity->ledger_id; /* ledgers */ $this->db->from('entry_items')->where('entry_id', $entry_id)->where('inventory_type', 3); $cur_ledgers_q = $this->db->get(); $counter = 0; foreach ($cur_ledgers_q->result() as $row) { $data['ledger_dc'][$counter] = $row->dc; $data['ledger_id'][$counter] = $row->ledger_id; $data['rate_item'][$counter] = $row->inventory_rate; $data['amount_item'][$counter] = $row->amount; if ($row->reconciliation_date) { $data['has_reconciliation'] = TRUE; } $counter++; } /* one extra rows */ $data['ledger_dc'][$counter] = 'D'; $data['ledger_id'][$counter] = 0; $data['rate_item'][$counter] = ""; $data['amount_item'][$counter] = ""; $counter++; /* inventory items */ $this->db->from('inventory_entry_items')->where('entry_id', $entry_id); $cur_inventory_item_q = $this->db->get(); $counter = 0; foreach ($cur_inventory_item_q->result() as $row) { $data['inventory_item_id'][$counter] = $row->inventory_item_id; $data['inventory_item_quantity'][$counter] = $row->quantity; $data['inventory_item_rate_per_unit'][$counter] = $row->rate_per_unit; $data['inventory_item_discount'][$counter] = $row->discount; $data['inventory_item_amount'][$counter] = $row->total; $counter++; } /* one extra rows */ $data['inventory_item_id'][$counter] = '0'; $data['inventory_item_quantity'][$counter] = ""; $data['inventory_item_rate_per_unit'][$counter] = ""; $data['inventory_item_discount'][$counter] = ""; $data['inventory_item_amount'][$counter] = ""; $counter++; } /* Form validations */ if ($current_entry_type['numbering'] == '3') { $this->form_validation->set_rules('entry_number', 'Entry Number', 'trim|is_natural_no_zero|uniqueentrynowithid[' . $entry_type_id . '.' . $entry_id . ']'); } else { $this->form_validation->set_rules('entry_number', 'Entry Number', 'trim|required|is_natural_no_zero|uniqueentrynowithid[' . $entry_type_id . '.' . $entry_id . ']'); } $this->form_validation->set_rules('entry_date', 'Entry Date', 'trim|required|is_date|is_date_within_range'); if ($current_entry_type['inventory_entry_type'] == '1') { $this->form_validation->set_rules('main_account', 'Purchase Ledger', 'trim|required'); $this->form_validation->set_rules('main_entity', 'Creditors (Supplier)', 'trim|required'); } else { $this->form_validation->set_rules('main_account', 'Sale Ledger', 'trim|required'); $this->form_validation->set_rules('main_entity', 'Debtor (Customer)', 'trim|required'); } $this->form_validation->set_rules('entry_narration', 'trim'); $this->form_validation->set_rules('entry_tag', 'Tag', 'trim|is_natural'); /* Debit and Credit amount validation */ if ($_POST) { foreach ($this->input->post('inventory_item_id', TRUE) as $id => $inventory_data) { $this->form_validation->set_rules('inventory_item_quantity[' . $id . ']', 'Inventory Item Quantity', 'trim|quantity'); $this->form_validation->set_rules('inventory_item_rate_per_unit[' . $id . ']', 'Inventory Item Rate Per Unit', 'trim|currency'); $this->form_validation->set_rules('inventory_item_discount[' . $id . ']', 'Inventory Item Discount', 'trim|discount'); $this->form_validation->set_rules('inventory_item_amount[' . $id . ']', 'Inventory Item Amount', 'trim|currency'); } foreach ($this->input->post('ledger_dc', TRUE) as $id => $ledger_data) { $this->form_validation->set_rules('rate_item[' . $id . ']', 'Rate %', 'trim|rate'); $this->form_validation->set_rules('amount_item[' . $id . ']', 'Ledger Amount', 'trim|currency'); } } /* Repopulating form */ if ($_POST) { $data['entry_number']['value'] = $this->input->post('entry_number', TRUE); $data['entry_date']['value'] = $this->input->post('entry_date', TRUE); $data['entry_narration']['value'] = $this->input->post('entry_narration', TRUE); $data['entry_tag'] = $this->input->post('entry_tag', TRUE); $data['has_reconciliation'] = $this->input->post('has_reconciliation', TRUE); $data['main_account_active'] = $this->input->post('main_account', TRUE); $data['main_entity_active'] = $this->input->post('main_entity', TRUE); $data['inventory_item_id'] = $this->input->post('inventory_item_id', TRUE); $data['inventory_item_quantity'] = $this->input->post('inventory_item_quantity', TRUE); $data['inventory_item_rate_per_unit'] = $this->input->post('inventory_item_rate_per_unit', TRUE); $data['inventory_item_discount'] = $this->input->post('inventory_item_discount', TRUE); $data['inventory_item_amount'] = $this->input->post('inventory_item_amount', TRUE); $data['ledger_dc'] = $this->input->post('ledger_dc', TRUE); $data['ledger_id'] = $this->input->post('ledger_id', TRUE); $data['rate_item'] = $this->input->post('rate_item', TRUE); $data['amount_item'] = $this->input->post('amount_item', TRUE); } if ($this->form_validation->run() == FALSE) { $this->messages->add(validation_errors(), 'error'); $this->template->load('template', 'inventory/entry/edit', $data); } else { $data_main_account = $this->input->post('main_account', TRUE); $data_main_entity = $this->input->post('main_entity', TRUE); $data_all_inventory_item_id = $this->input->post('inventory_item_id', TRUE); $data_all_inventory_item_quantity = $this->input->post('inventory_item_quantity', TRUE); $data_all_inventory_item_rate_per_unit = $this->input->post('inventory_item_rate_per_unit', TRUE); $data_all_inventory_item_discount = $this->input->post('inventory_item_discount', TRUE); $data_all_inventory_item_amount = $this->input->post('inventory_item_amount', TRUE); $data_all_ledger_id = $this->input->post('ledger_id', TRUE); $data_all_ledger_dc = $this->input->post('ledger_dc', TRUE); $data_all_rate_item = $this->input->post('rate_item', TRUE); $data_all_amount_item = $this->input->post('amount_item', TRUE); $data_total_amount = 0; /* Setting Inventory Item type */ if ($current_entry_type['inventory_entry_type'] == '1') { $data_inventory_item_type = 1; } else { $data_inventory_item_type = 2; } /* Checking for Valid Inventory Ledger account - account */ if ($current_entry_type['inventory_entry_type'] == '1') { $this->db->from('ledgers')->where('id', $data_main_account)->where('type', 2); } else { $this->db->from('ledgers')->where('id', $data_main_account)->where('type', 3); } $valid_main_account_q = $this->db->get(); if ($valid_main_account_q->num_rows() < 1) { if ($current_entry_type['inventory_entry_type'] == '1') { $this->messages->add('Invalid Purchase Ledger.', 'error'); } else { $this->messages->add('Invalid Sale Ledger.', 'error'); } $this->template->load('template', 'inventory/entry/edit', $data); return; } /* Checking for Valid Inventory Ledger account - entity */ if ($current_entry_type['inventory_entry_type'] == '1') { $this->db->from('ledgers')->where('id', $data_main_entity)->where('type', 4)->or_where('type', 1); } else { $this->db->from('ledgers')->where('id', $data_main_entity)->where('type', 5)->or_where('type', 1); } $valid_main_account_q = $this->db->get(); if ($valid_main_account_q->num_rows() < 1) { if ($current_entry_type['inventory_entry_type'] == '1') { $this->messages->add('Invalid Creditor (Supplier).', 'error'); } else { $this->messages->add('Invalid Debtor (Customer).', 'error'); } $this->template->load('template', 'inventory/entry/edit', $data); return; } /* Checking for Valid Inventory Item */ $inventory_item_present = FALSE; $data_total_inventory_amount = 0; foreach ($data_all_inventory_item_id as $id => $inventory_data) { if ($data_all_inventory_item_id[$id] < 1) { continue; } /* Check for valid inventory item id */ $this->db->from('inventory_items')->where('id', $data_all_inventory_item_id[$id]); $valid_inventory_item_q = $this->db->get(); if ($valid_inventory_item_q->num_rows() < 1) { $this->messages->add('Invalid Inventory Item.', 'error'); $this->template->load('template', 'inventory/entry/edit', $data); return; } $inventory_item_present = TRUE; $data_total_inventory_amount = float_ops($data_total_inventory_amount, $data_all_inventory_item_amount[$id], '+'); } if (!$inventory_item_present) { $this->messages->add('No Inventory Item selected.', 'error'); $this->template->load('template', 'inventory/entry/edit', $data); return; } /* Checking for Valid Ledgers */ $data_total_ledger_amount = 0; foreach ($data_all_ledger_dc as $id => $ledger_data) { if ($data_all_ledger_id[$id] < 1) { continue; } /* Check for valid ledger id */ $this->db->from('ledgers')->where('id', $data_all_ledger_id[$id]); $valid_ledger_q = $this->db->get(); if ($valid_ledger_q->num_rows() < 1) { $this->messages->add('Invalid Ledger account.', 'error'); $this->template->load('template', 'inventory/entry/edit', $data); return; } if ($data_all_ledger_dc[$id] == 'D') { $data_total_ledger_amount = float_ops($data_total_ledger_amount, $data_all_amount_item[$id], '+'); } else { $data_total_ledger_amount = float_ops($data_total_ledger_amount, $data_all_amount_item[$id], '-'); } } /* Total amount calculations */ if ($current_entry_type['inventory_entry_type'] == '1') { $data_main_account_total = $data_total_inventory_amount; $data_main_entity_total = float_ops($data_total_inventory_amount, $data_total_ledger_amount, '+'); } else { $data_main_account_total = float_ops($data_total_inventory_amount, $data_total_ledger_amount, '+'); $data_main_entity_total = $data_total_inventory_amount; } $data_total_amount = float_ops($data_total_inventory_amount, $data_total_ledger_amount, '+'); if ($data_total_amount < 0) { $this->messages->add($current_entry_type['name'] . ' Entry total cannot be negative.', 'error'); $this->template->load('template', 'inventory/entry/edit', $data); return; } /* Updating main entry */ if ($current_entry_type['numbering'] == '3') { $data_number = $this->input->post('entry_number', TRUE); if (!$data_number) { $data_number = NULL; } } else { $data_number = $this->input->post('entry_number', TRUE); } $data_date = $this->input->post('entry_date', TRUE); $data_narration = $this->input->post('entry_narration', TRUE); $data_tag = $this->input->post('entry_tag', TRUE); if ($data_tag < 1) { $data_tag = NULL; } $data_type = $entry_type_id; $data_date = date_php_to_mysql($data_date); // Converting date to MySQL $data_has_reconciliation = $this->input->post('has_reconciliation', TRUE); $this->db->trans_start(); $update_data = array('number' => $data_number, 'date' => $data_date, 'narration' => $data_narration, 'tag_id' => $data_tag); if (!$this->db->where('id', $entry_id)->update('entries', $update_data)) { $this->db->trans_rollback(); $this->messages->add('Error updating Entry.', 'error'); $this->logger->write_message("error", "Error updating entry details for " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " [id:" . $entry_id . "]"); $this->template->load('template', 'inventory/entry/edit', $data); return; } /* TODO : Deleting all old ledger data, Bad solution */ if (!$this->db->delete('inventory_entry_items', array('entry_id' => $entry_id))) { $this->db->trans_rollback(); $this->messages->add('Error deleting previous inventory items from Entry.', 'error'); $this->logger->write_message("error", "Error deleting previous inventory items from " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " [id:" . $entry_id . "]"); $this->template->load('template', 'inventory/entry/edit', $data); return; } $this->db->where('inventory_type', 3); if (!$this->db->delete('entry_items', array('entry_id' => $entry_id))) { $this->db->trans_rollback(); $this->messages->add('Error deleting previous Ledger accounts from Entry.', 'error'); $this->logger->write_message("error", "Error deleting previous entry items for " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " [id:" . $entry_id . "]"); $this->template->load('template', 'inventory/entry/edit', $data); return; } /* Updating main - account */ $update_data = array('ledger_id' => $data_main_account, 'amount' => $data_main_account_total, 'dc' => '', 'reconciliation_date' => NULL, 'inventory_type' => 1, 'inventory_rate' => ''); if ($current_entry_type['inventory_entry_type'] == '1') { $update_data['dc'] = 'D'; } else { $update_data['dc'] = 'C'; } if (!$this->db->where('entry_id', $entry_id)->where('inventory_type', 1)->update('entry_items', $update_data)) { $this->db->trans_rollback(); if ($current_entry_type['inventory_entry_type'] == '1') { $this->messages->add('Error updating Purchase Ledger account of Entry.', 'error'); $this->logger->write_message("error", "Error updating " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " since failed updating purchase ledger " . "[id:" . $data_main_account . "]"); } else { $this->messages->add('Error updating Sale Ledger account of Entry.', 'error'); $this->logger->write_message("error", "Error updating " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " since failed updating sale ledger " . "[id:" . $data_main_account . "]"); } $this->template->load('template', 'inventory/entry/edit', $data); return; } /* Updating main - entity */ $update_data = array('ledger_id' => $data_main_entity, 'amount' => $data_main_entity_total, 'dc' => '', 'reconciliation_date' => NULL, 'inventory_type' => 2, 'inventory_rate' => ''); if ($current_entry_type['inventory_entry_type'] == '1') { $update_data['dc'] = 'C'; } else { $update_data['dc'] = 'D'; } if (!$this->db->where('entry_id', $entry_id)->where('inventory_type', 2)->update('entry_items', $update_data)) { $this->db->trans_rollback(); if ($current_entry_type['inventory_entry_type'] == '1') { $this->messages->add('Error updating Creditor (Supplier) of Entry.', 'error'); $this->logger->write_message("error", "Error updating " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " since failed updating creditor ledger " . "[id:" . $data_main_entity . "]"); } else { $this->messages->add('Error updating Debtor (Customer) of Entry.', 'error'); $this->logger->write_message("error", "Error updating " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " since failed updating debtor ledger " . "[id:" . $data_main_entity . "]"); } $this->template->load('template', 'inventory/entry/edit', $data); return; } /* Adding inventory items */ $data_all_inventory_item_id = $this->input->post('inventory_item_id', TRUE); $data_all_inventory_item_quantity = $this->input->post('inventory_item_quantity', TRUE); $data_all_inventory_item_rate_per_unit = $this->input->post('inventory_item_rate_per_unit', TRUE); $data_all_inventory_item_discount = $this->input->post('inventory_item_discount', TRUE); $data_all_inventory_item_amount = $this->input->post('inventory_item_amount', TRUE); foreach ($data_all_inventory_item_id as $id => $inventory_data) { $data_inventory_item_id = $data_all_inventory_item_id[$id]; if ($data_inventory_item_id < 1) { continue; } $data_inventory_item_quantity = $data_all_inventory_item_quantity[$id]; $data_inventory_item_rate_per_unit = $data_all_inventory_item_rate_per_unit[$id]; $data_inventory_item_discount = $data_all_inventory_item_discount[$id]; $data_inventory_item_amount = $data_all_inventory_item_amount[$id]; $insert_inventory_data = array('entry_id' => $entry_id, 'inventory_item_id' => $data_inventory_item_id, 'quantity' => $data_inventory_item_quantity, 'rate_per_unit' => $data_inventory_item_rate_per_unit, 'discount' => $data_inventory_item_discount, 'total' => $data_inventory_item_amount, 'type' => $data_inventory_item_type); if (!$this->db->insert('inventory_entry_items', $insert_inventory_data)) { $this->db->trans_rollback(); $this->messages->add('Error adding Inventory Item - ' . $data_inventory_item_id . ' to Entry.', 'error'); $this->logger->write_message("error", "Error adding " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " since failed inserting inventory item " . "[id:" . $data_inventory_item_id . "]"); $this->template->load('template', 'inventory/entry/edit', $data); return; } } /* Adding ledger accounts */ $data_all_ledger_dc = $this->input->post('ledger_dc', TRUE); $data_all_ledger_id = $this->input->post('ledger_id', TRUE); $data_all_rate_item = $this->input->post('rate_item', TRUE); $data_all_amount_item = $this->input->post('amount_item', TRUE); foreach ($data_all_ledger_dc as $id => $ledger_data) { $data_ledger_dc = $data_all_ledger_dc[$id]; $data_ledger_id = $data_all_ledger_id[$id]; if ($data_ledger_id < 1) { continue; } $data_rate = $data_all_rate_item[$id]; $data_amount = $data_all_amount_item[$id]; $insert_ledger_data = array('entry_id' => $entry_id, 'ledger_id' => $data_ledger_id, 'amount' => $data_amount, 'dc' => $data_ledger_dc, 'reconciliation_date' => NULL, 'inventory_type' => 3, 'inventory_rate' => $data_rate); if (!$this->db->insert('entry_items', $insert_ledger_data)) { $this->db->trans_rollback(); $this->messages->add('Error adding Ledger account - ' . $data_ledger_id . ' to Entry.', 'error'); $this->logger->write_message("error", "Error adding " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " since failed inserting entry ledger item " . "[id:" . $data_ledger_id . "]"); $this->template->load('template', 'inventory/entry/edit', $data); return; } } /* Updating Debit and Credit Total - entries */ $update_data = array('dr_total' => $data_total_amount, 'cr_total' => $data_total_amount); if (!$this->db->where('id', $entry_id)->update('entries', $update_data)) { $this->db->trans_rollback(); $this->messages->add('Error updating Entry total.', 'error'); $this->logger->write_message("error", "Error updating " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " since failed updating debit and credit total"); $this->template->load('template', 'inventory/entry/edit', $data); return; } /* Success */ $this->db->trans_complete(); $this->session->set_userdata('entry_updated_show_action', TRUE); $this->session->set_userdata('entry_updated_id', $entry_id); $this->session->set_userdata('entry_updated_type_id', $entry_type_id); $this->session->set_userdata('entry_updated_type_label', $current_entry_type['label']); $this->session->set_userdata('entry_updated_type_name', $current_entry_type['name']); $this->session->set_userdata('entry_updated_number', $data_number); if ($data_has_reconciliation) { $this->session->set_userdata('entry_updated_has_reconciliation', TRUE); } else { $this->session->set_userdata('entry_updated_has_reconciliation', FALSE); } /* Showing success message in show() method since message is too long for storing it in session */ $this->logger->write_message("success", "Updated " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " [id:" . $entry_id . "]"); redirect('entry/show/' . $current_entry_type['label']); return; } return; }