public function disable($regimen_drug_id)
 {
     $this->load->database();
     $query = $this->db->query("UPDATE regimen_drug SET active='0'WHERE drugcode='{$regimen_drug_id}'");
     $results = Drugcode::getDrugCode($regimen_drug_id);
     //$this -> session -> set_userdata('message_counter', '2');
     $this->session->set_userdata('msg_error', $results->Drug . ' was disabled!');
     redirect('settings_management');
 }
 public function synchronize_patients()
 {
     $data['regimens'] = Regimen::getAll();
     $data['supporters'] = Supporter::getAll();
     $data['service_types'] = Regimen_Service_Type::getAll();
     $data['sources'] = Patient_Source::getAll();
     $data['drugs'] = Drugcode::getAll();
     $data['regimen_change_purpose'] = Regimen_Change_Purpose::getAll();
     $data['visit_purpose'] = Visit_Purpose::getAll();
     $data['opportunistic_infections'] = Opportunistic_Infection::getAll();
     $data['regimen_drugs'] = Regimen_Drug::getAll();
 }
 public function delete($id)
 {
     $brand = Brand::getBrandName($id);
     $rowdelete = Drugcode::deleteBrand($id);
     //If query succeeds
     if ($rowdelete > 0) {
         //$this -> session -> set_userdata('message_counter', '1');
         $this->session->set_userdata('msg_error', $brand['Brand'] . ' was deleted !');
     } else {
         //$this -> session -> set_userdata('message_counter', '2');
         $this->session->set_userdata('msg_error', 'An error occured while deleting the brand. Try again !');
     }
     redirect("settings_management");
 }
Example #4
0
 public function load_form($form_id = NULL)
 {
     if ($form_id == "patient_details") {
         $data['pob'] = District::getItems();
         $data['gender'] = Gender::getItems();
         $data['current_status'] = Patient_Status::getItems();
         $data['source'] = Patient_Source::getItems();
         $data['drug_prophylaxis'] = Drug_Prophylaxis::getItems();
         $data['service'] = Regimen_Service_Type::getItems();
         $data['fplan'] = Family_Planning::getItems();
         $data['other_illnesses'] = Other_Illnesses::getItems();
         $data['pep_reason'] = Pep_Reason::getItems();
         $data['drug_allergies'] = Drugcode::getItems();
         $regimens = Regimen::getItems();
         $data['start_regimen'] = $regimens;
         $data['current_regimen'] = $regimens;
         $data['who_stage'] = Who_Stage::getItems();
         //Get facilities beacuse of UTF-8 encoding
         $this->db->select('facilitycode AS id, name AS Name');
         $query = $this->db->get('facilities');
         $facilities = $query->result_array();
         foreach ($facilities as $facility) {
             $facility_list[] = array('id' => $facility['id'], 'Name' => utf8_encode($facility['Name']));
         }
         $data['transfer_from'] = $facility_list;
     }
     echo json_encode($data);
 }
 public function save()
 {
     /*
      * Get posted data from the client
      */
     $balance = "";
     $facility = $this->session->userdata("facility");
     $facility_detail = facilities::getSupplier($facility);
     $supplier_name = $facility_detail->supplier->name;
     $get_user = $this->session->userdata("user_id");
     $cdrr_id = $this->input->post("cdrr_id");
     $get_qty_choice = $this->input->post("quantity_choice");
     $get_qty_out_choice = $this->input->post("quantity_out_choice");
     $get_source = $this->input->post("source");
     $get_source_name = $this->input->post("source_name");
     $get_destination_name = $this->input->post("destination_name");
     $get_destination = $this->input->post("destination");
     $get_transaction_date = date('Y-m-d', strtotime($this->input->post("transaction_date")));
     $get_ref_number = $this->input->post("reference_number");
     $get_transaction_type = $this->input->post("transaction_type");
     $transaction_type_name = $this->input->post("trans_type");
     $transaction_effect = $this->input->post("trans_effect");
     $get_drug_id = $this->input->post("drug_id");
     $get_batch = $this->input->post("batch");
     $get_expiry = $this->input->post("expiry");
     $get_packs = $this->input->post("packs");
     $get_qty = $this->input->post("quantity");
     $get_available_qty = $this->input->post("available_qty");
     $get_unit_cost = $this->input->post("unit_cost");
     $get_amount = $this->input->post("amount");
     $get_comment = $this->input->post("comment");
     $get_stock_type = $this->input->post("stock_type");
     $stock_type_name = $this->input->post("stock_transaction");
     //Name of kind of transaction being carried
     $all_drugs_supplied = $this->input->post("all_drugs_supplied");
     $time_stamp = $this->input->post("time_stamp");
     $email = $this->input->post("emailaddress");
     $balance = 0;
     $pharma_balance = 0;
     $store_balance = 0;
     $sql_queries = "";
     $source_destination = $this->input->post("source_destination");
     $check_optgroup = $this->input->post("optgroup");
     //Check if store selected as source or destination
     $source_dest_type = '';
     $running_balance = 0;
     $other_running_balance = 0;
     //For other store
     // If email is not empty
     if ($email != "") {
         $this->sendemail($email);
     }
     // STEP 1, GET BALANCES FROM DRUG STOCK BALANCE TABLE
     //Get running balance in drug stock movement
     $sql_run_balance = $this->db->query("SELECT machine_code as balance FROM drug_stock_movement WHERE drug ='{$get_drug_id}' AND ccc_store_sp ='{$get_stock_type}' AND expiry_date >=CURDATE() ORDER BY id DESC  LIMIT 1");
     $run_balance_array = $sql_run_balance->result_array();
     if (count($run_balance_array) > 0) {
         $run_balance = $run_balance_array[0]["balance"];
     } else {
         //If drug does not exist, initialise the balance to zero
         $run_balance = 0;
     }
     //If transaction has positive effect to current transaction type
     if (stripos($transaction_type_name, "received") === 0 || stripos($transaction_type_name, "balance") === 0 || stripos($transaction_type_name, "returns") === 0 && $transaction_effect == 1 || stripos($transaction_type_name, "adjustment") === 0 && $transaction_effect == 1 || stripos($transaction_type_name, "startingstock") === 0 || stripos($transaction_type_name, "physicalcount") === 0) {
         $source_dest_type = $get_source;
         //Get remaining balance for the drug
         $get_balance_sql = $this->db->query("SELECT dsb.balance FROM drug_stock_balance dsb  WHERE dsb.facility_code='{$facility}' AND dsb.stock_type='{$get_stock_type}' AND dsb.drug_id='{$get_drug_id}' AND dsb.batch_number='{$get_batch}' AND dsb.balance>0 AND dsb.expiry_date>=CURDATE() AND dsb.expiry_date='{$get_expiry}' LIMIT 1");
         $balance_array = $get_balance_sql->result_array();
         //Check if drug exists in the drug_stock_balance table
         if (count($balance_array) > 0) {
             $bal = $balance_array[0]["balance"];
         } else {
             //If drug does not exist, initialise the balance to zero
             $bal = 0;
         }
         //If many transactions from the same drug, set balances to zero only once
         if ($this->session->userdata("updated_dsb") && $this->session->userdata("updated_dsb") == $get_drug_id) {
         } else {
             //If transaction is physical count, set actual quantity as physical count
             if (stripos($transaction_type_name, "startingstock") === 0 || stripos($transaction_type_name, "physicalcount") === 0) {
                 $bal = 0;
                 $run_balance = 0;
                 //Set all balances fro each batch of the drug to be zero in drug_stock_balance for physical count transaction type
                 $sql = "UPDATE drug_stock_balance SET balance =0 WHERE drug_id='{$get_drug_id}' AND stock_type='{$get_stock_type}' AND facility_code='{$facility}'";
                 $set_bal_zero = $this->db->query($sql);
                 $this->session->set_userdata("updated_dsb", $get_drug_id);
             }
         }
         //If stock coming in from another store, get current store
         if ($check_optgroup == 'Stores') {
             $source_dest_type = $get_source;
             //If transaction type is returns from(+),
             if (stripos($transaction_type_name, "returns") === 0 && $transaction_effect == 1) {
                 $source_dest_type = $get_destination;
             }
             //Get remaining balance for the drug
             $get_balance_sql = $this->db->query("SELECT dsb.balance FROM drug_stock_balance dsb  \n\t\t\t\tWHERE dsb.facility_code='{$facility}' AND dsb.stock_type='" . $source_dest_type . "' AND dsb.drug_id='{$get_drug_id}' AND dsb.batch_number='{$get_batch}' \n\t\t\t\tAND dsb.balance>0 AND dsb.expiry_date>=CURDATE() AND dsb.expiry_date='{$get_expiry}' LIMIT 1");
             $balance_array = $get_balance_sql->result_array();
             //Check if drug exists in the drug_stock_balance table
             if (count($balance_array) > 0) {
                 $bal_pharma = $balance_array[0]["balance"];
             } else {
                 //If drug does not exist, initialise the balance to zero
                 $bal_pharma = 0;
             }
             //Get running balance in drug stock movement
             $sql_run_balance = $this->db->query("SELECT machine_code as balance FROM drug_stock_movement WHERE drug ='{$get_drug_id}' AND ccc_store_sp ='{$source_dest_type}' AND expiry_date >=CURDATE() ORDER BY id DESC  LIMIT 1");
             $run_balance_array = $sql_run_balance->result_array();
             if (count($run_balance_array) > 0) {
                 $other_run_balance = $run_balance_array[0]["balance"];
             } else {
                 //If drug does not exist, initialise the balance to zero
                 $other_run_balance = 0;
             }
             $pharma_balance = $bal_pharma - $get_qty;
             //New balance
             $other_running_balance = $other_run_balance - $get_qty;
         }
         $balance = $get_qty + $bal;
         //Current store balance
         $running_balance = $get_qty + $run_balance;
     } else {
         //If transaction has negative effect (Issuing, returns(-) ...)
         //If issuing to a store(Pharmacy or Main Store), get remaining balance in destination
         if ($check_optgroup == 'Stores') {
             $source_dest_type = $get_destination;
             //If transaction type is returns to(-), get use source instead of destination as where the transaction came from
             if (stripos($transaction_type_name, "returns") === 0 && $transaction_effect == 0) {
                 $source_dest_type = $get_source;
             }
             //Get remaining balance for the drug
             $get_balance_sql = $this->db->query("SELECT dsb.balance FROM drug_stock_balance dsb  \n\t\t\t\tWHERE dsb.facility_code='{$facility}' AND dsb.stock_type='" . $source_dest_type . "' AND dsb.drug_id='{$get_drug_id}' AND dsb.batch_number='{$get_batch}' \n\t\t\t\tAND dsb.balance>0 AND dsb.expiry_date>=CURDATE() AND dsb.expiry_date='{$get_expiry}' LIMIT 1");
             $balance_array = $get_balance_sql->result_array();
             //Check if drug exists in the drug_stock_balance table
             if (count($balance_array) > 0) {
                 $bal_pharma = $balance_array[0]["balance"];
             } else {
                 //If drug does not exist, initialise the balance to zero
                 $bal_pharma = 0;
             }
             //Get running balance in drug stock movement
             $sql_run_balance = $this->db->query("SELECT machine_code as balance FROM drug_stock_movement WHERE drug ='{$get_drug_id}' AND ccc_store_sp ='{$source_dest_type}' AND expiry_date >=CURDATE() ORDER BY id DESC  LIMIT 1");
             $run_balance_array = $sql_run_balance->result_array();
             if (count($run_balance_array) > 0) {
                 $other_run_balance = $run_balance_array[0]["balance"];
             } else {
                 //If drug does not exist, initialise the balance to zero
                 $other_run_balance = 0;
             }
             $pharma_balance = $bal_pharma + $get_qty;
             //New balance
             $other_running_balance = $other_run_balance + $get_qty;
         }
         //Substract balance from qty going out
         $balance = $get_available_qty - $get_qty;
         $running_balance = $run_balance - $get_qty;
     }
     /*
      * Get transaction source and destination depending on type of transaction
      */
     // STEP 2, SET SOURCE AND DESTINATION
     //Check if stock type is store or pharmacy
     $s_d = "";
     if ($check_optgroup == 'Stores') {
         $source_destination = $get_source_name;
         if (stripos($stock_type_name, "pharmacy")) {
             //If pharmacy transaction, source and destinations is facility code
             $source = $facility;
             $destination = $facility;
             //Check if transaction is coming in or going out to find what to put in source and destination
             //If transaction is coming, destination is current store
             if ($transaction_effect == 1) {
                 $source_destination = $get_source_name;
                 if (stripos($transaction_type_name, "returns") === 0 && $transaction_effect == 1) {
                     //If transaction is returns from(+), source is current store
                     $source_destination = $get_destination_name;
                 }
             } else {
                 if ($transaction_effect == 0) {
                     //If transaction is going out, current store is sources
                     $source_destination = $get_destination_name;
                     if (stripos($transaction_type_name, "returns") === 0 && $transaction_effect == 0) {
                         //If transaction is returns from(-), destination is current store
                         $source_destination = $get_source_name;
                     }
                 } else {
                     //Transaction does not have effect ( Error)
                     $time = date("Y-m-d H:is:s");
                     $error[] = 'An error occured while saving your data ! No transaction effect found! (' . $time . ')';
                 }
             }
         } elseif (stripos($stock_type_name, "store")) {
             //If store transaction, source or destination is facility code
             //Check if transaction is coming in or going out to find what to put in source and destination
             //If transaction is coming, destination is current store
             if ($transaction_effect == 1) {
                 //If transaction is coming in, destination is current store
                 $source = $get_source_name;
                 $destination = $facility;
                 $source_destination = $get_source_name;
                 if (stripos($transaction_type_name, "returns") === 0 && $transaction_effect == 1) {
                     //If transaction is returns from(+), source is current store
                     $source = $facility;
                     $destination = $get_destination_name;
                     $source_destination = $get_destination_name;
                 }
             } else {
                 if ($transaction_effect == 0) {
                     //If transaction is going out, current store is sources
                     $source = $facility;
                     $destination = $get_destination_name;
                     $source_destination = $get_destination_name;
                     if (stripos($transaction_type_name, "returns") === 0 && $transaction_effect == 0) {
                         //If transaction is returns from(-), destination is current store
                         $source = $get_source_name;
                         $destination = $facility;
                         $source_destination = $get_source_name;
                     }
                 } else {
                     //Transaction does not have effect ( Error)
                     $time = date("Y-m-d H:is:s");
                     $error[] = 'An error occured while saving your data ! No transaction effect found! (' . $time . ')';
                 }
             }
         }
     } else {
         if (stripos($stock_type_name, "pharmacy")) {
             //If pharmacy transaction, source and destinations is facility code
             $source = $facility;
             $destination = $facility;
             if ($transaction_effect == 1) {
                 $source_destination = $get_source;
                 $s_d = 's';
                 if (stripos($transaction_type_name, "returns") === 0 && $transaction_effect == 1) {
                     //If transaction is returns from(+), source is current store
                     $source_destination = $get_destination;
                     $s_d = 'd';
                 }
             } else {
                 if ($transaction_effect == 0) {
                     //If transaction is going out, current store is sources
                     $source_destination = $get_destination;
                     $s_d = 'd';
                     if (stripos($transaction_type_name, "returns") === 0 && $transaction_effect == 0) {
                         //If transaction is returns from(-), destination is current store
                         $source_destination = $get_source;
                         $s_d = 's';
                     }
                 } else {
                     //Transaction does not have effect ( Error)
                     $time = date("Y-m-d H:is:s");
                     $error[] = 'An error occured while saving your data ! No transaction effect found! (' . $time . ')';
                 }
             }
         } elseif (stripos($stock_type_name, "store")) {
             //If store transaction, source or destination is facility code
             if ($transaction_effect == 1) {
                 //If transaction is coming in, destination is current store
                 $source = $get_source;
                 $destination = $facility;
                 $source_destination = $get_source;
                 $s_d = 's';
                 if (stripos($transaction_type_name, "returns") === 0 && $transaction_effect == 1) {
                     //If transaction is returns from(+), source is current store
                     $source = $facility;
                     $destination = $get_destination;
                     $source_destination = $get_destination;
                     $s_d = 'd';
                 }
             } else {
                 if ($transaction_effect == 0) {
                     //If transaction is going out, current store is sources
                     $source = $facility;
                     $destination = $get_destination;
                     $source_destination = $get_destination;
                     $s_d = 'd';
                     if (stripos($transaction_type_name, "returns") === 0 && $transaction_effect == 0) {
                         //If transaction is returns from(-), destination is current store
                         $source = $get_source;
                         $destination = $facility;
                         $source_destination = $get_source;
                         $s_d = 's';
                     }
                 } else {
                     //Transaction does not have effect ( Error)
                     $time = date("Y-m-d H:is:s");
                     $error[] = 'An error occured while saving your data ! No transaction effect found! (' . $time . ')';
                 }
             }
         }
     }
     //Sanitize by removing (store) or (pharmacy)
     $source_destination = str_ireplace('(store)', '', $source_destination);
     $source_destination = str_ireplace('(pharmacy)', '', $source_destination);
     //If source or destination is central site or satellite, insert exact name instead of IDs
     if ($check_optgroup == 'Central Site' || $check_optgroup == 'Satelitte Sites') {
         if ($s_d == 'd') {
             $source_destination = $get_destination_name;
         } elseif ($s_d == 's') {
             $source_destination = $get_source_name;
         }
     }
     //echo json_encode($running_balance ." -- ".$other_running_balance);die();
     //echo json_encode($source_destination);die();
     // STEP 3, INSERT TRANSACTION IN DRUG STOCK MOVEMENT FOR CURRENT STORES
     $drug_stock_mvt_transact = array('drug' => $get_drug_id, 'transaction_date' => $get_transaction_date, 'batch_number' => $get_batch, 'transaction_type' => $get_transaction_type, 'source' => $source, 'destination' => $destination, 'expiry_date' => $get_expiry, 'packs' => $get_packs, $get_qty_choice => $get_qty, $get_qty_out_choice => '0', 'balance' => $balance, 'unit_cost' => $get_unit_cost, 'amount' => $get_amount, 'remarks' => $get_comment, 'operator' => $get_user, 'order_number' => $get_ref_number, 'facility' => $facility, 'Source_Destination' => $source_destination, 'timestamp' => $time_stamp, 'machine_code' => $running_balance, 'ccc_store_sp' => $get_stock_type);
     $this->db->insert('drug_stock_movement', $drug_stock_mvt_transact);
     //check if query inserted
     $inserted = $this->db->affected_rows();
     if ($inserted < 1) {
         //If query did not insert
         $time = date("Y-m-d H:is:s");
         $errNo = $this->db->_error_number();
         $errMess = $this->db->_error_message();
         $remaining_drugs = $this->input->post("remaining_drugs");
         $error[] = 'An error occured while saving your data(Drug Transaction 1) ! Error  ' . $errNo . ' : ' . $errMess . ' (' . $time . ')';
         echo json_encode($error);
         die;
     }
     //STEP 4, UPDATE DRUG STOCK BALANCE FOR CURRENT STORE
     if ($transaction_effect == 1) {
         $balance_sql = "INSERT INTO drug_stock_balance(drug_id,batch_number,expiry_date,stock_type,facility_code,balance,ccc_store_sp) VALUES('" . $get_drug_id . "','" . $get_batch . "','" . $get_expiry . "','" . $get_stock_type . "','" . $facility . "','" . $get_qty . "','" . $get_stock_type . "') ON DUPLICATE KEY UPDATE balance=balance + " . $get_qty . ";";
         if (stripos($transaction_type_name, "physical")) {
             //Physical Count
             $balance_sql = "INSERT INTO drug_stock_balance(drug_id,batch_number,expiry_date,stock_type,facility_code,balance,ccc_store_sp) VALUES('" . $get_drug_id . "','" . $get_batch . "','" . $get_expiry . "','" . $get_stock_type . "','" . $facility . "','" . $get_qty . "','" . $get_stock_type . "') ON DUPLICATE KEY UPDATE balance=" . $get_qty . ";";
         }
     } else {
         if ($transaction_effect == 0) {
             $balance_sql = "UPDATE drug_stock_balance SET balance=balance - " . $get_qty . " WHERE drug_id='" . $get_drug_id . "' AND batch_number='" . $get_batch . "' AND expiry_date='" . $get_expiry . "' AND stock_type='" . $get_stock_type . "' AND facility_code='" . $facility . "';";
         }
     }
     $sql_dsb_current_store = $this->db->query($balance_sql);
     $inserted = $this->db->affected_rows();
     if ($inserted < 1) {
         //If query did not insert
         $time = date("Y-m-d H:is:s");
         $errNo = $this->db->_error_number();
         $errMess = $this->db->_error_message();
         $remaining_drugs = $this->input->post("remaining_drugs");
         $error[] = 'An error occured while saving your data (Drug Balance)! Error  ' . $errNo . ' : ' . $errMess . ' (' . $time . ')';
         echo json_encode($error);
         die;
     }
     //STEP 5, IF STORE TRANSACTIONS, UPDATE OTHER STORE DETAILS
     if ($check_optgroup == 'Stores') {
         // If transaction if from one store to another, update drug stock balance in the other store
         //STEP 6, UPDATE DRUG STOCK MOVEMENT FOR THE OTHER STORE
         if (stripos($source_destination, "pharmacy")) {
             //If pharmacy transaction, source and destinations is facility code
             $source = $facility;
             $destination = $facility;
         }
         $source_destination = $stock_type_name;
         //Get corresponding transaction types
         $sql = "";
         if (stripos($transaction_type_name, "receive") === 0) {
             //If transaction is received, insert an issued to
             $sql = "SELECT id FROM transaction_type WHERE name LIKE '%issued%' LIMIT 1";
         } else {
             if (stripos($transaction_type_name, "issued") === 0) {
                 //Issued, insert a received
                 $sql = "SELECT id FROM transaction_type WHERE name LIKE '%received%' LIMIT 1";
             } else {
                 if (stripos($transaction_type_name, "returns") === 0 && $transaction_effect == 1) {
                     //Returns froms(+), insert an returns to (-)
                     $sql = "SELECT id FROM transaction_type WHERE name LIKE '%returns%' AND effect='0' LIMIT 1";
                 } else {
                     if (stripos($transaction_type_name, "returns") === 0 && $transaction_effect == 0) {
                         //Returns to(-), insert an returns from (+)
                         $sql = "SELECT id FROM transaction_type WHERE name LIKE '%returns%' AND effect='1' LIMIT 1";
                     }
                 }
             }
         }
         $get_trans_id = $this->db->query($sql);
         $get_trans_id = $get_trans_id->result_array();
         $transaction_type = $get_trans_id[0]['id'];
         //Sanitize by removing (store) or (pharmacy)
         $source_destination = str_ireplace('(store)', '', $source_destination);
         $source_destination = str_ireplace('(pharmacy)', '', $source_destination);
         $drug_stock_mvt_other_trans = array('drug' => $get_drug_id, 'transaction_date' => $get_transaction_date, 'batch_number' => $get_batch, 'transaction_type' => $transaction_type, 'source' => $source, 'destination' => $destination, 'expiry_date' => $get_expiry, 'packs' => $get_packs, $get_qty_choice => '0', $get_qty_out_choice => $get_qty, 'balance' => $pharma_balance, 'unit_cost' => $get_unit_cost, 'amount' => $get_amount, 'remarks' => $get_comment, 'operator' => $get_user, 'order_number' => $get_ref_number, 'facility' => $facility, 'Source_Destination' => $source_destination, 'timestamp' => $time_stamp, 'machine_code' => $other_running_balance, 'ccc_store_sp' => $source_dest_type);
         $this->db->insert('drug_stock_movement', $drug_stock_mvt_other_trans);
         //echo json_encode($source_destination);die();
         //check if query inserted
         $inserted = $this->db->affected_rows();
         if ($inserted < 1) {
             //If query did not insert
             $time = date("Y-m-d H:is:s");
             $errNo = $this->db->_error_number();
             $errMess = $this->db->_error_message();
             $remaining_drugs = $this->input->post("remaining_drugs");
             $error[] = 'An error occured while saving your data(Drug Transaction 2) ! Error  ' . $errNo . ' : ' . $errMess . ' (' . $time . ')';
             echo json_encode($error);
             die;
         }
         //STEP 7, UPDATE DRUG STOCK BALANCE FOR THE OTHER STORE
         //If transaction has a positive effect on current store, it will have a negative effect on the other store
         if ($transaction_effect == 1) {
             //If transaction has a positive effect, substract balance in the other store
             $balance_sql = "UPDATE drug_stock_balance SET balance=balance - " . $get_qty . " WHERE drug_id='" . $get_drug_id . "' AND batch_number='" . $get_batch . "' AND expiry_date='" . $get_expiry . "' AND stock_type='" . $get_source . "' AND facility_code='" . $facility . "';";
             if (stripos($transaction_type_name, "returns") === 0 && $transaction_effect == 1) {
                 //If returns from(+), substract from other store
                 $balance_sql = "INSERT INTO drug_stock_balance(drug_id,batch_number,expiry_date,stock_type,facility_code,balance,ccc_store_sp) VALUES('" . $get_drug_id . "','" . $get_batch . "','" . $get_expiry . "','" . $get_destination . "','" . $facility . "','" . $get_qty . "','" . $get_stock_type . "') ON DUPLICATE KEY UPDATE balance=balance - " . $get_qty . ";";
             }
         } else {
             if ($transaction_effect == 0) {
                 //If transaction has negative effect, add to balance in the other store
                 $balance_sql = "INSERT INTO drug_stock_balance(drug_id,batch_number,expiry_date,stock_type,facility_code,balance,ccc_store_sp) VALUES('" . $get_drug_id . "','" . $get_batch . "','" . $get_expiry . "','" . $get_destination . "','" . $facility . "','" . $get_qty . "','" . $get_stock_type . "') ON DUPLICATE KEY UPDATE balance=balance + " . $get_qty . ";";
                 if (stripos($transaction_type_name, "returns") === 0 && $transaction_effect == 0) {
                     //If returns to(-), add to drug stock balance in the other store
                     $balance_sql = "UPDATE drug_stock_balance SET balance=balance + " . $get_qty . " WHERE drug_id='" . $get_drug_id . "' AND batch_number='" . $get_batch . "' AND expiry_date='" . $get_expiry . "' AND stock_type='" . $get_source . "' AND facility_code='" . $facility . "';";
                 }
             }
         }
         $sql_dsb_store = $this->db->query($balance_sql);
         $inserted = $this->db->affected_rows();
         if ($inserted < 1) {
             //If query did not insert
             $time = date("Y-m-d H:is:s");
             $errNo = $this->db->_error_number();
             $errMess = $this->db->_error_message();
             $remaining_drugs = $this->input->post("remaining_drugs");
             $error[] = 'An error occured while saving your data(Drug Balance 2) ! Error  ' . $errNo . ' : ' . $errMess . ' (' . $time . ')';
             echo json_encode($error);
             die;
         }
     }
     //Check if transaction came from picking list and not all drugs where supplied
     if ($all_drugs_supplied == 0) {
         //Update supplied drugs
         $sql = "UPDATE cdrr_item SET publish='1' WHERE id='{$cdrr_id}'";
         $this->db->query($sql);
     }
     //Get drug_name
     $drug_det = Drugcode::getDrugCodeHydrated($get_drug_id);
     $drug_name = $drug_det[0]['Drug'];
     echo json_encode($drug_name);
     die;
 }
 public function getHelp($stock_type = '2', $start_date = '2013-08-01', $end_date = '2013-08-31')
 {
     /*
      * Loop through all respective transaction types and add beginning balance at the beginning
      * Outer Loop through all active drugs
      * Inner Loop through all respective transaction types for the outer drug
      */
     $first_value = "AND ccc_store_sp = {$stock_type}";
     $second_value = "AND dst.ccc_store_sp = {$stock_type}";
     $drugs = Drugcode::getEnabledDrugs();
     $transactions = Transaction_Type::getAllTypes();
     $overall_array = array();
     $trans_sections = array();
     $prev_start = date("Y-m-d", strtotime("-1 month", strtotime($start_date)));
     $prev_end = date("Y-m-d", strtotime("-1 month", strtotime($end_date)));
     $trans_sections['Beginning Balance'] = 0;
     foreach ($transactions as $transaction) {
         $trans_sections[$transaction['Name']] = $transaction['id'];
     }
     foreach ($drugs as $drug) {
         $drug_id = $drug['id'];
         $drug_name = $drug['Drug'];
         foreach ($trans_sections as $section_index => $sections) {
             if ($sections == 0) {
                 /*
                  * Runs when transaction is beginngin balance
                  * Get Beginning Balance of drug
                  */
                 $sql = "SELECT SUM( dst.balance ) AS total FROM drug_stock_movement dst, \n\t\t\t\t\t\t\t(SELECT drug, batch_number, MAX( transaction_date ) AS trans_date FROM  `drug_stock_movement` \n\t\t\t\t\t\t\tWHERE transaction_date BETWEEN  '{$prev_start}' AND  '{$prev_end}' AND drug ='{$drug_id}' {$first_value} \n\t\t\t\t\t\t\tGROUP BY batch_number) AS temp WHERE dst.drug = temp.drug AND dst.batch_number = temp.batch_number \n\t\t\t\t\t\t\tAND dst.transaction_date = temp.trans_date {$second_value}";
             } else {
                 $effect = Transaction_Type::getEffect($sections);
                 if ($effect['Effect'] == 1) {
                     $balance_value = "quantity";
                 } else {
                     $balance_value = "quantity_out";
                 }
                 $sql = "SELECT SUM({$balance_value}) AS total FROM  `drug_stock_movement` \n\t\t\t\t\t\t\tWHERE transaction_date BETWEEN  '{$start_date}' AND  '{$end_date}' {$first_value} \n\t\t\t\t\t\t\tAND transaction_type ='{$sections}' AND drug='{$drug_id}'";
             }
             $query = $this->db->query($sql);
             $results = $query->result_array();
             if ($results) {
                 if ($results[0]['total'] != null) {
                     $overall_array[$drug_name][$section_index] = $results[0]['total'];
                 } else {
                     $overall_array[$drug_name][$section_index] = 0;
                 }
             } else {
                 $overall_array[$drug_name][$section_index] = 0;
             }
         }
     }
 }
 public function getNonMappedDrugs($param = '0')
 {
     $data = array();
     $query = $this->db->query("SELECT s.id,CONCAT_WS('] ',CONCAT_WS(' [',s.name,s.abbreviation),CONCAT_WS(' | ',s.strength,s.formulation)) as name,s.packsize\n                                       FROM sync_drug s \n                                       WHERE s.id NOT IN(SELECT dc.map\n                                                         FROM drugcode dc\n                                                         WHERE dc.map !='0')\n                                       AND (s.category_id='1' or s.category_id='2' or s.category_id='3')\n                                       ORDER BY name asc");
     $data['sync_drugs'] = $query->result_array();
     if ($param == 1) {
         echo json_encode($data['sync_drugs']);
         die;
     }
     $data['non_mapped_drugs'] = Drugcode::getNonMappedDrugs();
     //Not mapped regimens
     echo json_encode($data);
 }
 public function view_balance()
 {
     $data['drugs'] = Drugcode::getEnabledDrugs();
     $data['quick_link'] = "balance";
     $data['title'] = "webADT | Running Balance";
     $data['banner_text'] = "Running Balance Management";
     $data['link'] = "auto_management";
     $this->load->view('running_balance_v', $data);
 }
Example #9
0
 public function create_order($type = "cdrr", $order_type, $content_array = array())
 {
     $data['hide_generate'] = 0;
     $data['hide_save'] = 0;
     $data['hide_btn'] = 0;
     $data['stand_alone'] = 0;
     if ($type == "cdrr") {
         $this->session->set_userdata("order_go_back", "cdrr");
         $data['hide_side_menu'] = 0;
         $data['options'] = "none";
         if ($order_type == 1) {
             //Dispensing Point
             $data['page_title'] = "Central Dispensing Point(F-CDRR)";
             $data['banner_text'] = "Central Dispensing Point(F-CDRR)";
             $facility = $this->session->userdata("facility");
         } else {
             if ($order_type == 2) {
                 //Satellite
                 $data['page_title'] = "Satellite Facility(F-CDRR)";
                 $data['banner_text'] = "Satellite Facility(F-CDRR)";
                 $facility = $this->input->post("satellite_facility", TRUE);
                 if ($facility == null) {
                     $facility = $this->session->userdata("facility");
                 } else {
                     $data['hide_generate'] = 1;
                 }
             } else {
                 if ($order_type == 3) {
                     $data['page_title'] = "Stand-alone(F-CDRR)";
                     $data['banner_text'] = "Stand-alone(F-CDRR)";
                     $facility = $this->session->userdata("facility");
                     $data['stand_alone'] = 1;
                 } else {
                     //Aggregate
                     $data['page_title'] = "Central Aggregate(D-CDRR)";
                     $data['banner_text'] = "Central Aggregate(D-CDRR)";
                     $data['hide_generate'] = 2;
                     $facility = $this->session->userdata("facility");
                 }
             }
         }
         if (!empty($content_array)) {
             $cdrr_array = $content_array;
             $data['cdrr_array'] = $cdrr_array['cdrr_array'];
             $data['status_name'] = strtolower($cdrr_array['cdrr_array'][0]['status_name']);
             $facility_id = $cdrr_array['cdrr_array'][0]['facility_id'];
             $data['facility_id'] = $facility_id;
             $facilities = Sync_Facility::getCode($facility_id, $order_type);
             $facility = $facilities['code'];
             $code = $cdrr_array['cdrr_array'][0]['code'];
             $code = $this->getDummyCode($code, $order_type);
             $data['options'] = $cdrr_array['options'];
             if ($data['options'] == "view") {
                 $data['hide_save'] = 1;
             }
             $data['hide_btn'] = 1;
             $cdrr_id = $cdrr_array['cdrr_array'][0]['cdrr_id'];
             $data['cdrr_id'] = $cdrr_id;
             $data['logs'] = Cdrr_Log::getLogs($cdrr_id);
             if ($data['options'] == "view" || $data['options'] == "update") {
                 if ($data['status_name'] == "prepared" || $data['status_name'] == "review") {
                     $data['option_links'] = "<li class='active'><a href='" . site_url("order/view_order/cdrr/" . $cdrr_id) . "'>view</a></li><li><a href='" . site_url("order/update_order/cdrr/" . $cdrr_id) . "'>update</a></li><li><a class='delete' href='" . site_url("order/delete_order/cdrr/" . $cdrr_id) . "'>delete</a></li>";
                 } else {
                     $data['option_links'] = "<li class='active'><a href='" . site_url("order/view_order/cdrr/" . $cdrr_id) . "'>view</a></li>";
                 }
             }
             if ($code == 0) {
                 $and = "";
             } else {
                 $and = "AND ci.resupply !='0'";
             }
             if ($cdrr_array['options'] == "update") {
                 $supplier = Facilities::getSupplier($facility);
                 $data['commodities'] = Sync_Drug::getActiveList();
             } else {
                 $sql = "SELECT sd.id,CONCAT_WS('] ',CONCAT_WS(' [',name,abbreviation),CONCAT_WS(' ',strength,formulation)) as Drug,unit as Unit_Name,packsize as Pack_Size,category_id as Category\n\t\t\t        FROM cdrr_item ci\n\t\t\t        LEFT JOIN sync_drug sd ON sd.id=ci.drug_id\n\t\t\t        WHERE ci.cdrr_id='{$cdrr_id}'\n\t\t\t        AND(sd.category_id='1' OR sd.category_id='2' OR sd.category_id='3')";
                 $query = $this->db->query($sql);
                 $data['commodities'] = $query->result();
             }
         } else {
             $period_start = date('Y-m-01', strtotime(date('Y-m-d') . "-1 month"));
             $period_end = date('Y-m-t', strtotime(date('Y-m-d') . "-1 month"));
             $code = $this->getActualCode($order_type, $type);
             $facilities = Sync_Facility::getId($facility, $order_type);
             $duplicate = $this->check_duplicate($code, $period_start, $period_end, $facilities['id'], $type);
             $data['commodities'] = Sync_Drug::getActiveList();
             $data['duplicate'] = $duplicate;
             if ($duplicate == true) {
                 //redirect("order");
             }
         }
         $facilities = Sync_Facility::getId($facility, $order_type);
         $data['facility_id'] = $facilities['id'];
         $data['facility_object'] = Facilities::getCodeFacility($facility);
         $data['content_view'] = "orders/cdrr_template";
         $data['report_type'] = $order_type;
         $data['stores'] = CCC_store_service_point::getStoreGroups();
         $this->base_params($data);
     } else {
         if ($type == "maps") {
             $this->session->set_userdata("order_go_back", "fmaps");
             $data['o_type'] = "FMAP";
             $data['options'] = "none";
             $data["is_update"] = 0;
             $data["is_view"] = 0;
             if ($order_type == 1) {
                 //Central Dispensing point
                 $facility_code = $this->session->userdata('facility');
                 $facility_id = $this->session->userdata('facility_id');
                 $supplier['supplied_by'] = Facilities::getSupplier($facility_code);
                 $data['commodities'] = Drugcode::getAllObjects($supplier['supplied_by']);
                 $data['page_title'] = "Central Dispensing Point";
                 $data['banner_text'] = "Maps Form";
             } else {
                 if ($order_type == 2) {
                     //Satellite
                     $facility_code = $this->input->post("satellite_facility", TRUE);
                     $data['page_title'] = "Satellite Facility(F-MAPS)";
                     $data['banner_text'] = "Satellite Facility(F-MAPS)";
                     if ($facility_code == null) {
                         $facility_code = $this->session->userdata("facility");
                     } else {
                         $data['hide_generate'] = 1;
                     }
                 } else {
                     if ($order_type == 3) {
                         //Stand-alone Maps
                         $facility_code = $this->session->userdata('facility');
                         $facility_id = $this->session->userdata('facility_id');
                         $supplier['supplied_by'] = Facilities::getSupplier($facility_code);
                         $data['commodities'] = Drugcode::getAllObjects($supplier['supplied_by']);
                         $data['page_title'] = "Stand-Alone MAPS";
                         $data['banner_text'] = "Maps Form";
                     } else {
                         //Aggregated order
                         $facility_code = $this->session->userdata('facility');
                         $data['page_title'] = "Aggregate Maps List";
                         $facility = Facilities::getParent($facility_code);
                         $parent_code = $facility['parent'];
                         if ($parent_code == $facility_code) {
                             //Check if button was clicked to start new aggregate order
                             $data['hide_generate'] = 2;
                         }
                         $data['banner_text'] = "Aggregate Maps List";
                     }
                 }
             }
             if (!empty($content_array)) {
                 $fmaps_array = $content_array;
                 $data['fmaps_array'] = $fmaps_array['fmaps_array'];
                 $facility_id = $fmaps_array['fmaps_array'][0]['facility_id'];
                 $data['facility_id'] = $facility_id;
                 $facilities = Sync_Facility::getCode($facility_id, $order_type);
                 $facility_code = $facilities['code'];
                 $data['supplier'] = $this->get_supplier($facility_code);
                 $code = $fmaps_array['fmaps_array'][0]['code'];
                 $code = $this->getDummyCode($code, $order_type);
                 //Central or Satellite or Aggregate
                 $data['status'] = strtolower($fmaps_array['fmaps_array'][0]['status_name']);
                 $data['created'] = $fmaps_array['fmaps_array'][0]['created'];
                 // Pending, Approved, ...
                 $data['options'] = $fmaps_array['options'];
                 $data['hide_btn'] = 1;
                 $maps_id = $fmaps_array['fmaps_array'][0]['maps_id'];
                 //Complet id with #
                 $map_id = $fmaps_array['fmaps_array'][0]['map_id'];
                 //Id from DB
                 $data['maps_id'] = $maps_id;
                 $data['map_id'] = $map_id;
                 $data['logs'] = Maps_Log::getMapLogs($map_id);
                 /*echo "<pre>";
                 		print_r($data);
                 		echo "</pre>";
                 		die();*/
                 if ($data['options'] == "view") {
                     $data['hide_save'] = 1;
                     $regimen_table = 'sync_regimen';
                     $regimen_cat_table = 'sync_regimen_category';
                     $regimen_code = 'r.code';
                     $regimen_desc = 'r.name as description';
                     $regimen_cat_join = 'r.category_id';
                     $regimen_join = 'mi.regimen_id=r.id';
                     $sql_regimen = "SELECT rc.id,r.id as reg_id,rc.Name as name,{$regimen_code},{$regimen_desc},{$regimen_cat_join},mi.total\n\t\t\t\t\t\t\t\t\tFROM {$regimen_table} r\n\t\t\t\t\t\t\t\t\tLEFT JOIN {$regimen_cat_table} rc ON rc.id = {$regimen_cat_join}\n\t\t\t\t\t\t\t\t\tLEFT JOIN maps_item mi ON {$regimen_join}\n\t\t\t\t\t\t\t\t\tWHERE maps_id='{$map_id}'";
                     $query_regimen = $this->db->query($sql_regimen);
                     $regimen_array = $query_regimen->result_array();
                     $regimen_categories = array();
                     foreach ($regimen_array as $value) {
                         $regimen_categories[] = $value['name'];
                     }
                     $regimen_categories = array_unique($regimen_categories);
                     $data['regimen_categories'] = $regimen_categories;
                     $data['regimen_array'] = $regimen_array;
                 }
                 if ($data['options'] == "update") {
                     $data["is_update"] = 1;
                     $data['regimen_categories'] = Sync_Regimen_Category::getAll();
                 } else {
                     $data["is_view"] = 1;
                     $data['regimens'] = Maps_Item::getOrderItems($maps_id);
                 }
             } else {
                 $data['supplier'] = $this->get_supplier($facility_code);
                 if ($data['supplier'] == 'KEMSA') {
                     $data['regimen_categories'] = Sync_Regimen_Category::getAll();
                 } else {
                     if ($data['supplier'] == 'KENYA PHARMA') {
                         $data['regimen_categories'] = Sync_Regimen_Category::getAll();
                     }
                 }
                 $period_start = date('Y-m-01', strtotime(date('Y-m-d') . "-1 month"));
                 $period_end = date('Y-m-t', strtotime(date('Y-m-d') . "-1 month"));
                 $code = $this->getActualCode($order_type, $type);
                 $facilities = Sync_Facility::getId($facility_code, $order_type);
                 $duplicate = $this->check_duplicate($code, $period_start, $period_end, $facilities['id'], $type);
                 $data['duplicate'] = $duplicate;
                 if ($duplicate == true) {
                     //redirect("order");
                 }
             }
             $facilities = Sync_Facility::getId($facility_code, $order_type);
             $data['facility_id'] = $facilities['id'];
             $data['content_view'] = "orders/fmap_template";
             $data['report_type'] = $order_type;
             $data['facility_object'] = Facilities::getCodeFacility($facility_code);
             $this->base_params($data);
         }
     }
 }