Exemple #1
0
 public function facility_stock_first_run($checker, $import = null)
 {
     $facility_code = $this->session->userdata('facility_id');
     $which_view_to_load = $checker == 'first_run' ? "facility/facility_stock_data/update_facility_stock_on_first_run_v" : "facility/facility_stock_data/update_facility_stock_v";
     $data['title'] = "Update Stock Level";
     $data['content_view'] = $which_view_to_load;
     $data['banner_text'] = "Update Stock Level";
     $data['commodities'] = Commodities::get_facility_commodities($facility_code);
     $data['commodity_source'] = commodity_source::get_all();
     $data['import'] = $import;
     $this->load->view("shared_files/template/template", $data);
 }
Exemple #2
0
 public function add_stock_level()
 {
     //echo "<pre>"; print_r($_POST); echo "</pre>"; exit;
     if ($this->input->post('commodity_id')) {
         if ($this->input->post('new_source_name')) {
             $source_name = $this->input->post('new_source_name');
             $name_exists = commodity_source::check_name_exists($source_name);
             $other_source_id = array();
             if ($name_exists == 1) {
                 $other_source_id = commodity_source::find_source_id($source_name);
                 /*for($i = 0; $i < count($other_source_id); $i++){
                 			$other_source_id[$i] = commodity_source::find_source_id($source_name);
                 		}*/
                 //print_r($other_source_id);
                 //exit;
             } else {
                 $insert_name_trans = Doctrine_Manager::getInstance()->getCurrentConnection();
                 $insert_name_trans->execute("INSERT INTO commodity_source_other(source_name) VALUES ('{$source_name}')");
                 $other_source_id = $insert_name_trans->lastInsertId();
                 /*for($i = 0; $i < count($other_source_id); $i++){
                 			$other_source_id[$i] = $insert_name_trans->lastInsertId();
                 		}*/
                 //print_r($other_source_id);
                 //exit;
             }
         }
         $facility_code = $this->session->userdata('facility_id');
         $form_type = $this->input->post('form_type');
         $commodity_id = array_values($this->input->post('desc'));
         //this rearranges the array such that the index starts at 0
         $expiry_date = array_values($this->input->post('clone_datepicker'));
         $batch_no = array_values($this->input->post('commodity_batch_no'));
         $manu = array_values($this->input->post('commodity_manufacture'));
         $total_unit_count = array_values($this->input->post('commodity_total_units'));
         $source_of_item = array_values($this->input->post('source_of_item'));
         $commodity_price = array_values($this->input->post('price'));
         $date_of_entry_ = $form_type == 'first_run' ? date('y-m-d H:i:s') : array_values($this->input->post('date_received'));
         $count = count($commodity_id);
         $commodity_id_array = $data_array_facility_issues = $data_array_facility_transaction = array();
         //collect n set the data in the array
         for ($i = 0; $i < $count; $i++) {
             $status = $total_unit_count[$i] > 0 ? true : false;
             $status = $status && strtotime(str_replace(",", " ", $expiry_date[$i])) > strtotime('now') ? 1 : 2;
             $date_of_entry = $form_type == 'first_run' ? date('y-m-d H:i:s') : date('Y-m-d', strtotime($date_of_entry_[$i]));
             $mydata = array('facility_code' => $facility_code, 'commodity_id' => $commodity_id[$i], 'batch_no' => $batch_no[$i], 'manufacture' => $manu[$i], 'expiry_date' => date('y-m-d', strtotime(str_replace(",", " ", $expiry_date[$i]))), 'initial_quantity' => $total_unit_count[$i], 'current_balance' => $total_unit_count[$i], 'source_of_commodity' => $source_of_item[$i], 'other_source_id' => $other_source_id[$i], 'date_added' => $date_of_entry, 'status' => $status);
             //echo "<pre>"; print_r($mydata); echo "</pre>";
             if ($this->input->post('price')) {
                 $price_data = array('facility_code' => $facility_code, 'commodity_id' => $commodity_id[$i], 'batch_no' => $batch_no[$i], 'other_source_id' => $other_source_id[$i], 'price' => $commodity_price[$i]);
                 //echo "<pre>"; print_r($price_data); echo "</pre>"; exit;
                 commodity_source_other_prices::update_prices($price_data);
                 /*$store_price_transaction = Doctrine_Manager::getInstance() -> getCurrentConnection();
                 		$store_price_transaction -> execute("INSERT INTO commodity_source_other_prices(facility_code, commodity_id,
                 			batch_no, other_source_id, price) VALUES ('$facility_code', '$commodity_id[$i]', '$batch_no[$i]', '$other_source_id', '$commodity_price[$i]')");*/
             }
             //get the closing stock of the given item
             $facility_stock_ = facility_stocks::get_facility_commodity_total($facility_code, $commodity_id[$i], $date_of_entry)->toArray();
             //update the facility stock table
             facility_stocks::update_facility_stock($mydata);
             //check
             $facility_has_commodity = facility_transaction_table::get_if_commodity_is_in_table($facility_code, $commodity_id[$i]);
             $total_unit_count_ = $total_unit_count[$i] * 1;
             if ($facility_has_commodity > 0 && $status == 1) {
                 //update the opening balance for the transaction table
                 $inserttransaction = Doctrine_Manager::getInstance()->getCurrentConnection();
                 $inserttransaction->execute("UPDATE `facility_transaction_table` SET `opening_balance` =`opening_balance`+{$total_unit_count[$i]},\n\t\t\t`closing_stock` =`closing_stock`+{$total_unit_count[$i]}\n            WHERE `commodity_id`= '{$commodity_id[$i]}' and status='1' and facility_code={$facility_code}");
                 $mydata_ = array('facility_code' => $facility_code, 's11_No' => '(+ve Adj) Stock Addition', 'commodity_id' => $commodity_id[$i], 'batch_no' => !isset($batch_no[$i]) ? "N/A" : $batch_no[$i], 'expiry_date' => date('y-m-d', strtotime(str_replace(",", " ", $expiry_date[$i]))), 'balance_as_of' => isset($facility_stock_[0]['commodity_balance']) ? $facility_stock_[0]['commodity_balance'] : 0, 'qty_issued' => $total_unit_count_, 'date_issued' => date('y-m-d'), 'issued_to' => 'N/A', 'issued_by' => $this->session->userdata('user_id'));
                 //create the array to push to the db
                 array_push($data_array_facility_issues, $mydata_);
             } else {
                 //get the data to send to the facility_transaction_table
                 if ($status == 1) {
                     $mydata2 = array('facility_code' => $facility_code, 'commodity_id' => $commodity_id[$i], 'opening_balance' => $total_unit_count[$i], 'total_issues' => 0, 'total_receipts' => 0, 'adjustmentpve' => 0, 'adjustmentnve' => 0, 'date_added' => $date_of_entry, 'closing_stock' => $total_unit_count[$i], 'status' => 1);
                     //send the data to the facility_transaction_table
                     $this->db->insert('facility_transaction_table', $mydata2);
                     $mydata_ = array('facility_code' => $facility_code, 's11_No' => $form_type == 'first_run' ? 'initial stock update' : '(+ve Adj) Stock Addition', 'commodity_id' => $commodity_id[$i], 'batch_no' => $form_type == 'first_run' ? "N/A" : $batch_no[$i], 'expiry_date' => $form_type == 'first_run' ? 'N/A' : date('y-m-d', strtotime(str_replace(",", " ", $expiry_date[$i]))), 'balance_as_of' => isset($facility_stock_[0]['commodity_balance']) ? $facility_stock_[0]['commodity_balance'] : 0, 'qty_issued' => $total_unit_count_, 'date_issued' => date('y-m-d'), 'issued_to' => 'N/A', 'issued_by' => $this->session->userdata('user_id'));
                     //create the array to push to the db
                     array_push($data_array_facility_issues, $mydata_);
                 }
             }
         }
         //updates the log table accordingly based on the action carried out by the user involved
         $user = $this->session->userdata('user_id');
         $user_action = "add_stock";
         $update = Doctrine_Manager::getInstance()->getCurrentConnection();
         $update->execute("update log set {$user_action} = 1\n\t\t\twhere `user_id`= {$user}\n\t\t\tAND action = 'Logged In'\n\t\t\tand UNIX_TIMESTAMP( `end_time_of_event`) = 0");
         //send a text message to the facility admin and sub county pharmacist
         $this->hcmp_functions->send_system_text($user_action);
         $this->db->insert_batch('facility_issues', $data_array_facility_issues);
         //delete the record from the db
         facility_stocks_temp::delete_facility_temp(null, null, $facility_code);
         //set the notifications
         //$this->hcmp_functions->send_stock_update_sms();
         $updateCase = Doctrine_Manager::getInstance()->getCurrentConnection();
         $updateCase->execute("UPDATE facility_stocks SET\n        manufacture=CONCAT(UCASE(SUBSTRING(`manufacture`, 1, 1)),LOWER(SUBSTRING(`manufacture`, 2)))");
         //
         $this->session->set_flashdata('system_success_message', "Stock Levels Have Been Updated");
         redirect('reports/facility_stock_data');
     }
 }