?
Current Path : /home1/savoy/public_html/savoyglobal.net/sec/application/models/ |
Linux gator3171.hostgator.com 4.19.286-203.ELK.el7.x86_64 #1 SMP Wed Jun 14 04:33:55 CDT 2023 x86_64 |
Current File : /home1/savoy/public_html/savoyglobal.net/sec/application/models/inventorymodel.php |
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class Inventorymodel extends CI_Model { function __construct() { // Call the Model constructor parent::__construct(); // $this->load->model('hrmodel'); $this->load->model('franchiseemodel'); } function search_purchase($pagenum) { $data = array(); $this->db->select('*'); $this->db->from('purchase'); $this->db->join('master', 'master_id=purchase_id'); $this->db->join('franchisee', 'franchisee.franchisee_id=purchase.purchase_franchisee_id'); $this->db->where("journal_id", '0'); $this->db->order_by('purchase_id', 'desc'); $data['results'] = $this->db->get(); return $data; } function search_purchase_posted($pagenum) { $data = array(); $this->db->select('*'); $this->db->from('purchase'); $this->db->join('master', 'master_id=purchase_id'); $this->db->join('franchisee', 'franchisee.franchisee_id=purchase.purchase_franchisee_id'); $this->db->where("journal_id !=", '0'); $this->db->order_by('purchase_id', 'desc'); $data['results'] = $this->db->get(); return $data; } function insert_purchase($postdata) { $data = array(); $this->db->trans_begin(); $masterid = $this->mastermodel->insertmasterdata(); $tblValues = array( 'purchase_id' => $masterid, 'purchase_franchisee_id' => $postdata['franchisee_id'], 'purchase_date' => $this->mastermodel->convertdateformat($postdata['purchase_date']), 'purchase_remarks' => $postdata['purchase_remarks'], ); $this->db->insert('purchase', $tblValues); $i = 1; while (isset($postdata['inventory_id' . $i])) { $tblValues = array( 'purchase_id' => $masterid, 'inventory_type_id' => $postdata['inventory_id' . $i], 'inventory_qty' => $postdata['inventory_qty' . $i], 'inventory_amount' => $postdata['inventory_amount' . $i], 'inventory_discount' => $postdata['inventory_discount' . $i], ); $this->db->insert('purchase_inventory', $tblValues); $i++; } $data['resfunction'] = 'search_purchase'; if ($this->db->trans_status() === FALSE) { $this->db->trans_rollback(); $data['res'] = 0; $data['msg'] = 'Error On Adding Record'; return $data; } else { $this->db->trans_commit(); $data['res'] = 1; $data['msg'] = 'Record Added Successfully'; return $data; } } function update_purchase($postdata) { $data = array(); $this->db->trans_begin(); $tblValues = array( 'purchase_franchisee_id' => $postdata['franchisee_id'], 'purchase_date' => $this->mastermodel->convertdateformat($postdata['purchase_date']), 'purchase_remarks' => $postdata['purchase_remarks'], ); $this->db->where('purchase_id', $postdata['purchase_id']); $this->db->update('purchase', $tblValues); $this->db->where('purchase_id', $postdata['purchase_id']); $this->db->delete('purchase_inventory'); $i = 1; while (isset($postdata['inventory_id' . $i])) { $tblValues = array( 'purchase_id' => $postdata['purchase_id'], 'inventory_type_id' => $postdata['inventory_id' . $i], 'inventory_qty' => $postdata['inventory_qty' . $i], 'inventory_amount' => $postdata['inventory_amount' . $i], 'inventory_discount' => $postdata['inventory_discount' . $i], ); $this->db->insert('purchase_inventory', $tblValues); $i++; } $data['resfunction'] = 'search_purchase'; if ($this->db->trans_status() === FALSE) { $this->db->trans_rollback(); $data['res'] = 0; $data['msg'] = 'Error On Updating Record'; return $data; } else { $this->db->trans_commit(); $data['res'] = 1; $data['msg'] = 'Record Updated Successfully'; return $data; } } function post_purchase_details($postdata) { $data = array(); $data['resfunction'] = 'search_purchase'; $fisc_year_id = $this->accountingmodel->getfiscalyearid(); $default_currency = $this->accountingmodel->getcurrency(); if ($fisc_year_id != 0 && $default_currency != 0) { $this->db->trans_begin(); $masterid = $this->mastermodel->insertmasterdata(); $account = explode("~", $postdata['bankacc_id']); $account_to_id = $account[0]; $account_to_code = $account[1]; $account_to_name = $account[2]; $description = "Purchase payment for " . $postdata['franchisee'] . " Received From " . $account_to_name . "-" . $account_to_code; $transaction_id = $postdata['transaction_id']; $payment_type = $postdata['payment_type_id']; $reference = $this->accountingmodel->get_transaction_code($transaction_id, $payment_type); $tblValues = array( 'journal_id' => $masterid, 'journal_date' => $this->mastermodel->convertdateformat($postdata['journal_date']), 'description' => $description, 'reference' => $reference, 'currency_id' => $default_currency, 'payment_type_id' => $postdata['payment_type_id'], 'transaction_id' => $postdata['transaction_id'], 'posted' => 1 ); $this->db->insert('finance_journal_refs', $tblValues); if ($postdata['payment_type_id'] == 2) { $tblValues = array( 'journal_id' => $masterid, 'cheque_date' => $this->mastermodel->convertdateformat($postdata['cheque_date']), 'cheque_no ' => $postdata['cheque_no'], 'chart_account_code' => $account_to_code, 'cheque_info' => $postdata['cheque_info'] ); $this->db->insert('finance_cheque_info', $tblValues); } else if ($postdata['payment_type_id'] == 3) { $tblValues = array( 'journal_id' => $masterid, 'wire_info' => $postdata['wire_info'], ); $this->db->insert('finance_wire_info', $tblValues); } else if ($postdata['payment_type_id'] == 4) { $tblValues = array( 'journal_id' => $masterid, 'card_info' => $postdata['card_info'], ); $this->db->insert('finance_card_info', $tblValues); } $total_amount = 0; if ($postdata['payment_to_id'] != "") { $chart_account = $this->mastermodel->get_data_srow('finance_chart_master', $postdata['payment_to_id'], 'chart_master_id'); $chart_account_id = $postdata['payment_to_id']; $chart_account_code = $chart_account->chart_account_code; $chart_account_name = $chart_account->chart_account_name; $total_amount = $postdata['amount']; if (empty($postdata['description'])) $description = "Purchase payment for " . $postdata['franchisee'] . " Received From " . $account_to_name . "-" . $account_to_code; else $description = $postdata['description']; $tblValues = array( 'journal_id' => $masterid, 'chart_account_code' => $chart_account_code, 'debit' => $postdata['amount'], 'credit' => 0, 'description' => $description, 'fiscal_year_id' => $fisc_year_id, ); $this->db->insert('finance_journal', $tblValues); } $tblValues = array( 'journal_id' => $masterid, 'chart_account_code' => $account_to_code, 'debit' => 0, 'credit' => $total_amount, 'description' => "Purchase payment for " . $postdata['franchisee'] . " Made to $chart_account_name - $chart_account_code", 'fiscal_year_id' => $fisc_year_id ); $this->db->insert('finance_journal', $tblValues); $tblValues = array( 'journal_id' => $masterid ); $this->db->where('purchase_id', $postdata['purchase_id']); $this->db->update('purchase', $tblValues); if ($this->db->trans_status() === FALSE) { $this->db->trans_rollback(); $data['res'] = 0; $data['msg'] = 'Error On Adding Record'; return $data; } else { $this->db->trans_commit(); $data['res'] = 1; $data['msg'] = 'Record Added Successfully'; return $data; } } else { $data['res'] = 0; if ($fisc_year_id == 0) $data['msg'] = 'Fiscal Year Not Set.'; else if ($default_currency == 0) $data['msg'] = 'Default Currency Not Set.'; else $data['msg'] = 'Fiscal Year and Default Currency Not Set.'; return $data; } } function search_sale($pagenum) { $data = array(); $this->db->select('*'); $this->db->from('batch'); $this->db->join('course', 'course.course_id=batch.course_id'); $this->db->join('inventory', 'inventory.course_id=course.course_id'); $this->db->where('close', '0'); // $this->db->group_by('batch_id'); $this->db->order_by('batch_id', 'desc'); $data['results'] = $this->db->get(); return $data; } function inventory_alloted_details($sale_id = 0) { $data = array(); $this->db->select('*'); $this->db->from('sale'); $this->db->join('sale_inventory', 'sale_inventory.sale_id=sale.sale_id'); if ($sale_id > 0) { $this->db->where('sale.sale_id !=', $sale_id); } $Q = $this->db->get(); $data['inventodry_alloted'] = array(); if ($Q->num_rows() > 0) { foreach ($Q->result_array() as $row) { if (isset($data['inventodry_alloted'][$row['sale_inventory_type_id']][$row['student_id']])) { $data['inventodry_alloted'][$row['sale_inventory_type_id']][$row['student_id']]+= $row['inventory_qty']; } else { $data['inventodry_alloted'][$row['sale_inventory_type_id']][$row['student_id']] = $row['inventory_qty']; } } } $Q->free_result(); return $data; } function get_allotted_date($student_id, $inventory_id) { $sql = "select inventory_allotted_date from sale_inventory join sale on sale.sale_id=sale_inventory.sale_id where student_id ='" . $student_id . "' and sale_inventory_type_id = '" . $inventory_id . "'"; $data = $this->db->query($sql); $result = $data->row(); if ($result) { return $result->inventory_allotted_date; } else { return ""; } } function insert_sale($postdata) { $data = array(); $this->db->trans_begin(); $student_list = explode(',', $postdata['student_id_list']); $sale_id = $this->mastermodel->get_single_field_value_two('sale', 'sale_id', 'sale_inventory_type_id', $postdata['inventory_id'], 'sale_batch_id', $postdata['batch_id']); if ($sale_id != '') { $saleid = $sale_id; $tblValues = array( 'sale_date' => $this->mastermodel->convertdateformat($postdata['sale_date']), ); $this->db->where('sale_id', $sale_id); $this->db->update('sale', $tblValues); } else { $masterid = $this->mastermodel->insertmasterdata(); $saleid = $masterid; $tblValues = array( 'sale_id' => $masterid, 'sale_batch_id' => $postdata['batch_id'], 'sale_date' => $this->mastermodel->convertdateformat($postdata['sale_date']), 'sale_inventory_type_id' => $postdata['inventory_id'] ); $this->db->insert('sale', $tblValues); } $inventory_qty = 0; foreach ($student_list as $value) { $inventory_quantity = $postdata['alloted_qty_old' . $value] - $postdata['alloted_qty' . $value]; $inventory_sale_id = $this->mastermodel->get_single_field_value_two('sale_inventory', 'sale_inventory_id', 'sale_id', $saleid, 'student_id', $postdata['student_id' . $value]); if ($inventory_sale_id != '') { if ($postdata['inventory_qty' . $value] > 0 || $inventory_quantity > 0) { $inventory_qty = $postdata['alloted_qty' . $value] + $postdata['inventory_qty' . $value]; $tblValues = array( 'inventory_qty' => $inventory_qty, 'inventory_allotted_date' => $this->mastermodel->convertdateformat($postdata['sale_date']), ); $this->db->where('sale_inventory_id', $inventory_sale_id); $this->db->update('sale_inventory', $tblValues); } } else { if ($postdata['inventory_qty' . $value] > 0) { $tblValues = array( 'sale_id' => $saleid, 'inventory_qty' => $postdata['inventory_qty' . $value], 'student_id' => $postdata['student_id' . $value], 'inventory_allotted_date' => $this->mastermodel->convertdateformat($postdata['sale_date']), ); $this->db->insert('sale_inventory', $tblValues); } } } $data['resfunction'] = 'search_sale'; if ($this->db->trans_status() === FALSE) { $this->db->trans_rollback(); $data['res'] = 0; $data['msg'] = 'Error On Adding Record'; return $data; } else { $this->db->trans_commit(); $data['res'] = 1; $data['msg'] = 'Record Added Successfully'; return $data; } } function update_sale($postdata) { $data = array(); $this->db->trans_begin(); $tblValues = array( 'sale_date' => $this->mastermodel->convertdateformat($postdata['sale_date']), 'sale_remarks' => $postdata['sale_remarks'], ); $this->db->where('sale_id', $postdata['sale_id']); $this->db->update('sale', $tblValues); $this->db->where('sale_id', $postdata['sale_id']); $this->db->delete('sale_inventory'); $student_list = explode(',', $postdata['student_id_list']); $inventory_qty = 0; $inventory_qty_old = 0; foreach ($student_list as $value) { $inventory_qty+=$postdata['inventory_qty' . $value]; $inventory_qty_old+=$postdata['inventory_qty_prev' . $value]; if ($postdata['inventory_qty' . $value] > 0) { $tblValues = array( 'sale_id' => $postdata['sale_id'], 'inventory_qty' => $postdata['inventory_qty' . $value], 'student_id' => $postdata['student_id' . $value] ); $this->db->insert('sale_inventory', $tblValues); } } $data['resfunction'] = 'search_sale'; if ($this->db->trans_status() === FALSE) { $this->db->trans_rollback(); $data['res'] = 0; $data['msg'] = 'Error On Updating Record'; return $data; } else { $this->db->trans_commit(); $data['res'] = 1; $data['msg'] = 'Record Updated Successfully'; return $data; } } function search_stock($pagenum) { $data = array(); $this->db->select('*'); $this->db->from('inventory'); $this->db->order_by('inventory_id', 'asc'); $data['results'] = $this->db->get(); return $data; } function get_stock($inventory_id) { $purchase_quantity = 0; $sold_quantity = 0; $returned_quantity = 0; $this->db->select('SUM(inventory_qty) as purchased_quantity'); $this->db->where('inventory_type_id', $inventory_id); $Q = $this->db->get('purchase_inventory'); if ($Q->num_rows() > 0) { foreach ($Q->result_array() as $row) { $purchase_quantity = $row['purchased_quantity']; } } $this->db->select('SUM(inventory_qty) as sold_quantity'); $this->db->where('sale_inventory_type_id', $inventory_id); $this->db->join('sale', 'sale.sale_id=sale_inventory.sale_id'); $Q = $this->db->get('sale_inventory'); if ($Q->num_rows() > 0) { foreach ($Q->result_array() as $row) { $sold_quantity = $row['sold_quantity']; } } $this->db->select('SUM(return_inventory_qty) as return_quantity'); $this->db->where('return_inventory_id', $inventory_id); $Q = $this->db->get('inventory_return'); if ($Q->num_rows() > 0) { foreach ($Q->result_array() as $row) { $returned_quantity = $row['return_quantity']; } } return $purchase_quantity - $sold_quantity + $returned_quantity; } function check_qty($inventory_type) { $data = array(); $this->db->select('stock_qty'); $this->db->from('stock'); $this->db->where('inventory_type_id', $inventory_type); $Q = $this->db->get(); $result = $Q->row(); if ($result) { return $result->stock_qty; } else { return ""; } } function search_inventory_return($pagenum) { $data = array(); $this->db->select('*'); $this->db->from('inventory_return'); $this->db->join('master', 'master_id=return_id'); $this->db->join('student', 'student.student_id=inventory_return.student_id'); $this->db->join('inventory', 'inventory_id=return_inventory_id'); $this->db->order_by('return_id', 'desc'); $data['results'] = $this->db->get(); return $data; } function insert_inventory_return($postdata) { $data = array(); $this->db->trans_begin(); $masterid = $this->mastermodel->insertmasterdata(); $tblValues = array( 'return_id' => $masterid, 'student_id' => $postdata['student_id'], 'return_date' => $this->mastermodel->convertdateformat($postdata['return_date']), 'return_remarks' => $postdata['return_remarks'], 'return_inventory_id' => $postdata['return_inventory_id'], 'return_inventory_qty' => $postdata['return_inventory_qty'], ); $this->db->insert('inventory_return', $tblValues); $data['resfunction'] = 'search_inventory_return'; if ($this->db->trans_status() === FALSE) { $this->db->trans_rollback(); $data['res'] = 0; $data['msg'] = 'Error On Updating Record'; return $data; } else { $this->db->trans_commit(); $data['res'] = 1; $data['msg'] = 'Record Updated Successfully'; return $data; } } function update_inventory_return($postdata) { $data = array(); $this->db->trans_begin(); $tblValues = array( 'return_date' => $this->mastermodel->convertdateformat($postdata['return_date']), 'return_remarks' => $postdata['return_remarks'], 'return_inventory_id' => $postdata['return_inventory_id'], 'return_inventory_qty' => $postdata['return_inventory_qty'], ); $this->db->where('return_id', $postdata['return_id']); $this->db->update('inventory_return', $tblValues); // $this->db->where('return_id', $postdata['return_id']); // $this->db->delete('inventory_return_details'); $data['resfunction'] = 'search_inventory_return'; if ($this->db->trans_status() === FALSE) { $this->db->trans_rollback(); $data['res'] = 0; $data['msg'] = 'Error On Adding Record'; return $data; } else { $this->db->trans_commit(); $data['res'] = 1; $data['msg'] = 'Record Added Successfully'; return $data; } } function get_inventory_return($return_id, $student_id) { $data = array(); $this->db->select('*'); $this->db->from('inventory_return_details'); $this->db->join('sale_inventory', 'sale_inventory.sale_id=inventory_return_details.sale_id'); $this->db->join('sale', 'sale.sale_id=inventory_return_details.sale_id'); $this->db->where('return_id', $return_id); $this->db->where('sale_student_id', $student_id); $Q = $this->db->get(); if ($Q->num_rows() > 0) { foreach ($Q->result_array() as $row) { $data[] = $row; } } $Q->free_result(); return $data; } } ?>