function add($entry_type) { /* Check access */ if (!check_access('add inventory 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('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); } if ($current_entry_type['inventory_entry_type'] != '3') { $this->messages->add('Invalid Entry type.', 'error'); redirect('entry/show/all'); return; } $this->template->set('page_title', 'Add ' . $current_entry_type['name'] . ' Entry'); /* Form fields */ $data['entry_number'] = array('name' => 'entry_number', 'id' => 'entry_number', 'maxlength' => '11', 'size' => '11', 'value' => ''); $data['entry_date'] = array('name' => 'entry_date', 'id' => 'entry_date', 'maxlength' => '11', 'size' => '11', 'value' => date_today_php()); $data['entry_narration'] = array('name' => 'entry_narration', 'id' => 'entry_narration', 'cols' => '50', 'rows' => '4', 'value' => ''); $data['entry_type_id'] = $entry_type_id; $data['current_entry_type'] = $current_entry_type; $data['entry_tags'] = $this->Tag_model->get_all_tags(); $data['entry_tag'] = 0; /* Form validations */ if ($current_entry_type['numbering'] == '2') { $this->form_validation->set_rules('entry_number', 'Entry Number', 'trim|required|is_natural_no_zero|uniqueentryno[' . $entry_type_id . ']'); } else { if ($current_entry_type['numbering'] == '3') { $this->form_validation->set_rules('entry_number', 'Entry Number', 'trim|is_natural_no_zero|uniqueentryno[' . $entry_type_id . ']'); } else { $this->form_validation->set_rules('entry_number', 'Entry Number', 'trim|is_natural_no_zero|uniqueentryno[' . $entry_type_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'] == '3') { /* TODO */ } $this->form_validation->set_rules('entry_narration', 'trim'); $this->form_validation->set_rules('entry_tag', 'Tag', 'trim|is_natural'); /* inventory item 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); } else { for ($count = 0; $count <= 3; $count++) { $data['source_inventory_item_id'][$count] = '0'; $data['source_inventory_item_quantity'][$count] = ''; $data['source_inventory_item_rate_per_unit'][$count] = ''; $data['source_inventory_item_amount'][$count] = ''; } for ($count = 0; $count <= 3; $count++) { $data['dest_inventory_item_id'][$count] = '0'; $data['dest_inventory_item_quantity'][$count] = ''; $data['dest_inventory_item_rate_per_unit'][$count] = ''; $data['dest_inventory_item_amount'][$count] = ''; } } if ($this->form_validation->run() == FALSE) { $this->messages->add(validation_errors(), 'error'); $this->template->load('template', 'inventory/transfer/add', $data); return; } 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; } /* Adding main Entry */ if ($current_entry_type['numbering'] == '2') { $data_number = $this->input->post('entry_number', TRUE); } else { if ($current_entry_type['numbering'] == '3') { $data_number = $this->input->post('entry_number', TRUE); if (!$data_number) { $data_number = NULL; } } else { if ($this->input->post('entry_number', TRUE)) { $data_number = $this->input->post('entry_number', TRUE); } else { $data_number = $this->Entry_model->next_entry_number($entry_type_id); } } } $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 $entry_id = NULL; /* Adding Entry */ $this->db->trans_start(); $insert_data = array('number' => $data_number, 'date' => $data_date, 'narration' => $data_narration, 'entry_type' => $data_type, 'tag_id' => $data_tag, 'dr_total' => $data_total_source_inventory_amount, 'cr_total' => $data_total_dest_inventory_amount); if (!$this->db->insert('entries', $insert_data)) { $this->db->trans_rollback(); $this->messages->add('Error addding 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"); $this->template->load('template', 'inventory/transfer/add', $data); return; } else { $entry_id = $this->db->insert_id(); } /* 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; } } /* Success */ $this->db->trans_complete(); $this->session->set_userdata('entry_added_show_action', TRUE); $this->session->set_userdata('entry_added_id', $entry_id); $this->session->set_userdata('entry_added_type_id', $entry_type_id); $this->session->set_userdata('entry_added_type_label', $current_entry_type['label']); $this->session->set_userdata('entry_added_type_name', $current_entry_type['name']); $this->session->set_userdata('entry_added_type_base_type', $current_entry_type['base_type']); $this->session->set_userdata('entry_added_number', $data_number); /* Showing success message in show() method since message is too long for storing it in session */ $this->logger->write_message("success", "Added " . $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 add($entry_type) { /* Check access */ if (!check_access('add 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', 'Add ' . $current_entry_type['name'] . ' Entry'); /* Form fields */ $data['entry_number'] = array('name' => 'entry_number', 'id' => 'entry_number', 'maxlength' => '11', 'size' => '11', 'value' => ''); $data['entry_date'] = array('name' => 'entry_date', 'id' => 'entry_date', 'maxlength' => '11', 'size' => '11', 'value' => date_today_php()); $data['entry_narration'] = array('name' => 'entry_narration', 'id' => 'entry_narration', 'cols' => '50', 'rows' => '4', 'value' => ''); $data['entry_type_id'] = $entry_type_id; $data['current_entry_type'] = $current_entry_type; $data['entry_tags'] = $this->Tag_model->get_all_tags(); $data['entry_tag'] = 0; /* Form validations */ if ($current_entry_type['numbering'] == '2') { $this->form_validation->set_rules('entry_number', 'Entry Number', 'trim|required|is_natural_no_zero|uniqueentryno[' . $entry_type_id . ']'); } else { if ($current_entry_type['numbering'] == '3') { $this->form_validation->set_rules('entry_number', 'Entry Number', 'trim|is_natural_no_zero|uniqueentryno[' . $entry_type_id . ']'); } else { $this->form_validation->set_rules('entry_number', 'Entry Number', 'trim|is_natural_no_zero|uniqueentryno[' . $entry_type_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['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); } else { for ($count = 0; $count <= 3; $count++) { if ($count == 0 && $entry_type == "payment") { $data['ledger_dc'][$count] = "C"; } else { if ($count == 1 && $entry_type != "payment") { $data['ledger_dc'][$count] = "C"; } else { $data['ledger_dc'][$count] = "D"; } } $data['ledger_id'][$count] = 0; $data['dr_amount'][$count] = ""; $data['cr_amount'][$count] = ""; } } if ($this->form_validation->run() == FALSE) { $this->messages->add(validation_errors(), 'error'); $this->template->load('template', 'entry/add', $data); return; } 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/add', $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/add', $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/add', $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/add', $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/add', $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/add', $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/add', $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 account.', 'error'); $this->template->load('template', 'entry/add', $data); return; } if (!$non_bank_cash_present) { $this->messages->add('Need to Debit or Credit atleast one NON - Bank or Cash account.', 'error'); $this->template->load('template', 'entry/add', $data); return; } } } /* Adding main entry */ if ($current_entry_type['numbering'] == '2') { $data_number = $this->input->post('entry_number', TRUE); } else { if ($current_entry_type['numbering'] == '3') { $data_number = $this->input->post('entry_number', TRUE); if (!$data_number) { $data_number = NULL; } } else { if ($this->input->post('entry_number', TRUE)) { $data_number = $this->input->post('entry_number', TRUE); } else { $data_number = $this->Entry_model->next_entry_number($entry_type_id); } } } $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 $entry_id = NULL; $this->db->trans_start(); $insert_data = array('number' => $data_number, 'date' => $data_date, 'narration' => $data_narration, 'entry_type' => $data_type, 'tag_id' => $data_tag); if (!$this->db->insert('entries', $insert_data)) { $this->db->trans_rollback(); $this->messages->add('Error addding 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"); $this->template->load('template', 'entry/add', $data); return; } else { $entry_id = $this->db->insert_id(); } /* 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 " . $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', 'entry/add', $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 adding " . $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', 'entry/add', $data); return; } /* Success */ $this->db->trans_complete(); $this->session->set_userdata('entry_added_show_action', TRUE); $this->session->set_userdata('entry_added_id', $entry_id); $this->session->set_userdata('entry_added_type_id', $entry_type_id); $this->session->set_userdata('entry_added_type_label', $current_entry_type['label']); $this->session->set_userdata('entry_added_type_name', $current_entry_type['name']); $this->session->set_userdata('entry_added_type_base_type', $current_entry_type['base_type']); $this->session->set_userdata('entry_added_number', $data_number); /* Showing success message in show() method since message is too long for storing it in session */ $this->logger->write_message("success", "Added " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " [id:" . $entry_id . "]"); redirect('entry/show/' . $current_entry_type['label']); $this->template->load('template', 'entry/add', $data); return; } return; }
function add($entry_type) { /* Check access */ if (!check_access('add inventory 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('inventory/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', 'Add ' . $current_entry_type['name'] . ' Entry'); /* Form fields */ $data['entry_number'] = array('name' => 'entry_number', 'id' => 'entry_number', 'maxlength' => '11', 'size' => '11', 'value' => ''); $data['entry_date'] = array('name' => 'entry_date', 'id' => 'entry_date', 'maxlength' => '11', 'size' => '11', 'value' => date_today_php()); $data['entry_narration'] = array('name' => 'entry_narration', 'id' => 'entry_narration', 'cols' => '50', 'rows' => '4', 'value' => ''); $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_tags'] = $this->Tag_model->get_all_tags(); $data['entry_tag'] = 0; /* Form validations */ if ($current_entry_type['numbering'] == '2') { $this->form_validation->set_rules('entry_number', 'Entry Number', 'trim|required|is_natural_no_zero|uniqueentryno[' . $entry_type_id . ']'); } else { if ($current_entry_type['numbering'] == '3') { $this->form_validation->set_rules('entry_number', 'Entry Number', 'trim|is_natural_no_zero|uniqueentryno[' . $entry_type_id . ']'); } else { $this->form_validation->set_rules('entry_number', 'Entry Number', 'trim|is_natural_no_zero|uniqueentryno[' . $entry_type_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['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); } else { for ($count = 0; $count <= 3; $count++) { $data['inventory_item_id'][$count] = '0'; $data['inventory_item_quantity'][$count] = ''; $data['inventory_item_rate_per_unit'][$count] = ''; $data['inventory_item_discount'][$count] = ''; $data['inventory_item_amount'][$count] = ''; } for ($count = 0; $count <= 1; $count++) { $data['ledger_dc'][$count] = "D"; $data['ledger_id'][$count] = 0; $data['rate_item'][$count] = ""; $data['amount_item'][$count] = ""; } } if ($this->form_validation->run() == FALSE) { $this->messages->add(validation_errors(), 'error'); $this->template->load('template', 'inventory/entry/add', $data); return; } 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/add', $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/add', $data); return; } /* Checking for Valid Inventory Item account */ $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/add', $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/add', $data); return; } /* Checking for Valid Ledgers account */ $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/add', $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/add', $data); return; } /* Adding main entry */ if ($current_entry_type['numbering'] == '2') { $data_number = $this->input->post('entry_number', TRUE); } else { if ($current_entry_type['numbering'] == '3') { $data_number = $this->input->post('entry_number', TRUE); if (!$data_number) { $data_number = NULL; } } else { if ($this->input->post('entry_number', TRUE)) { $data_number = $this->input->post('entry_number', TRUE); } else { $data_number = $this->Entry_model->next_entry_number($entry_type_id); } } } $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 $entry_id = NULL; /* Adding Entry */ $this->db->trans_start(); $insert_data = array('number' => $data_number, 'date' => $data_date, 'narration' => $data_narration, 'entry_type' => $data_type, 'tag_id' => $data_tag); if (!$this->db->insert('entries', $insert_data)) { $this->db->trans_rollback(); $this->messages->add('Error addding 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"); $this->template->load('template', 'inventory/entry/add', $data); return; } else { $entry_id = $this->db->insert_id(); } /* Adding main - account */ $insert_data = array('entry_id' => $entry_id, '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') { $insert_data['dc'] = 'D'; } else { $insert_data['dc'] = 'C'; } if (!$this->db->insert('entry_items', $insert_data)) { $this->db->trans_rollback(); if ($current_entry_type['inventory_entry_type'] == '1') { $this->messages->add('Error adding Purchase Ledger account 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 purchase ledger " . "[id:" . $data_main_account . "]"); } else { $this->messages->add('Error adding Sale Ledger account 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 sale ledger " . "[id:" . $data_main_account . "]"); } $this->template->load('template', 'inventory/entry/add', $data); return; } else { $main_entry_id = $this->db->insert_id(); } /* Adding main - entity */ $insert_data = array('entry_id' => $entry_id, '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') { $insert_data['dc'] = 'C'; } else { $insert_data['dc'] = 'D'; } if (!$this->db->insert('entry_items', $insert_data)) { $this->db->trans_rollback(); if ($current_entry_type['inventory_entry_type'] == '1') { $this->messages->add('Error adding Creditor (Supplier) 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 creditor ledger " . "[id:" . $data_main_entity . "]"); } else { $this->messages->add('Error adding Debtor (Customer) - 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 debtor ledger " . "[id:" . $data_main_entity . "]"); } $this->template->load('template', 'inventory/entry/add', $data); return; } else { $entity_entry_id = $this->db->insert_id(); } /* 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/add', $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/add', $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 adding " . $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/add', $data); return; } /* Success */ $this->db->trans_complete(); $this->session->set_userdata('entry_added_show_action', TRUE); $this->session->set_userdata('entry_added_id', $entry_id); $this->session->set_userdata('entry_added_type_id', $entry_type_id); $this->session->set_userdata('entry_added_type_label', $current_entry_type['label']); $this->session->set_userdata('entry_added_type_name', $current_entry_type['name']); $this->session->set_userdata('entry_added_type_base_type', $current_entry_type['base_type']); $this->session->set_userdata('entry_added_number', $data_number); /* Showing success message in show() method since message is too long for storing it in session */ $this->logger->write_message("success", "Added " . $current_entry_type['name'] . " Entry number " . full_entry_number($entry_type_id, $data_number) . " [id:" . $entry_id . "]"); redirect('entry/show/' . $current_entry_type['label']); $this->template->load('template', 'inventory/entry/add', $data); return; } return; }