public function confirm_offline_issue() { $today = date('Y-m-d h:i:s'); $count = count($this->input->post('commodity_id')); $commodity_name = $this->input->post('commodity_name'); $commodity_id = $this->input->post('commodity_id'); $expiry_date = $this->input->post('expiry_date'); $manufacturer = $this->input->post('manufacturer'); $batch_no = $this->input->post('batch_no'); $total_commodity_units = $this->input->post('total_commodity_units'); $facility_code = $this->session->userdata('facility_id'); for ($i = 0; $i < $count; $i++) { $commodity_details = commodities::get_details_name($commodity_name[$i]); foreach ($commodity_details as $key => $value) { $new_commodity_id = $value['id']; $commodity_code = $value['commodity_code']; $unit_size = $value['pack_size']; $commodity_source_id = $value['commodity_source_id']; //Convert the Packs to Units $received_quantity = $total_commodity_units[$i]; //Check if batch exists in the facility stocks table $batch_details = facility_stocks::get_batch_details($batch_no[$i], $facility_code); //If the batch does not exits if (count($batch_details) <= 0) { $facility_stocks_data = array(array('facility_code' => $facility_code, 'commodity_id' => $new_commodity_id, 'batch_no' => $batch_no[$i], 'manufacture' => $manufacturer[$i], 'initial_quantity' => $received_quantity, 'current_balance' => $received_quantity, 'date_added' => $today, 'date_modified' => $today, 'source_of_commodity' => $commodity_source_id, 'status' => 1, 'expiry_date' => $expiry_date[$i])); // echo "<pre>";print_r($facility_stocks_data);die; $this->db->insert_batch('facility_stocks', $facility_stocks_data); } else { // echo "<pre>";print_r($batch_details);die; $current_balance = intval($batch_details[0]['current_balance']); $quantity_units = $total_commodity_units[$i]; $current_balance = $current_balance + intval($quantity_units); $facility_stocks_data_update = array('batch_no' => $batch_no[$i], 'current_balance' => $current_balance, 'date_modified' => $today, 'expiry_date' => $expiry_date[$i]); // echo "<pre>";print_r($facility_stocks_data_update);die; $this->db->where('batch_no', $batch_no[$i]); $this->db->where('facility_code', $facility_code); $this->db->update('facility_stocks', $facility_stocks_data_update); //Above code updates the facility Stocks table if the batch exists } } $receive_redistribution_data_update = array('batch_no' => $batch_no[$i], 'status' => 0); $this->db->where('batch_no', $batch_no[$i]); $this->db->where('status', 1); $this->db->where('facility_code', $facility_code); $this->db->update('receive_redistributions', $receive_redistribution_data_update); } $this->session->set_flashdata('system_success_message', "Redistribution Data Has Been Updated"); redirect('home'); }