Ejemplo n.º 1
0
 public function addMonthlyConsumption2Draft()
 {
     $data = $this->form_values;
     $date = date("Y-m-d h:i:s");
     /* -----------------Delete Before Insert -------------- */
     $rows = $this->_em->getRepository('HfDataMasterDraft')->findBy(array('warehouse' => $data['wh_id'], 'reportingStartDate' => $data['rpt_date']));
     foreach ($rows as $row) {
         $this->_em->remove($row);
     }
     $this->_em->flush();
     //
     //        $posted_array = $data['flitm_id'];
     //        $opening_array = $data['opening_balance'];
     //        $received_array = $data['received'];
     //        $dispensed_array = $data['dispensed'];
     //        // $vials_used_array = $data['vials_used'];
     //        $unusable_vials_array = $data['unusable_vials'];
     //        $closing_balance_array = $data['closing_balance'];
     //        // $nearest_expiry_array = $data['nearest_expiry'];
     //        $doses_per_unit_array = $data['doses_per_unit'];
     //
     //        foreach ($closing_balance_array as $key => $val) {
     //            if ($val != '' && $val >= 0) {
     //                // $vials_used = $vials_used_array[$key];
     //                // $wh_id = $data['wh_id'];
     //                $vials_used = ($opening_array[$key] + $received_array[$key] - $closing_balance_array[$key] ) - $unusable_vials_array[$key];
     //                $year_month = $data['yearmonth'];
     //                $doses = $doses_per_unit_array[$key];
     //                $issue = $dispensed_array[$key];
     //                $itemid = $posted_array[$key];
     //                $wh_adj = $unusable_vials_array[$key] * $doses;
     //                $wastages = (( $vials_used) - $issue ) + abs($wh_adj);
     //                $date = date("Y-m-d h:i:s");
     //
     //                $hf_data_master = new HfDataMasterDraft();
     //                $item = $this->_em->getRepository('ItemPackSizes')->find($posted_array[$key]);
     //                $warehouse = $this->_em->getRepository('Warehouses')->find($data['wh_id']);
     //                $hf_data_master->setItemPackSize($item);
     //                $hf_data_master->setWarehouse($warehouse);
     //                $user_id = $this->_em->getRepository('Users')->find($this->_user_id);
     //                $hf_data_master->setCreatedBy($user_id);
     //                $hf_data_master->setOpeningBalance($opening_array[$key]);
     //                $hf_data_master->setReceivedBalance($received_array[$key]);
     //                $hf_data_master->setIssueBalance($issue);
     //                $hf_data_master->setClosingBalance($val);
     //                $hf_data_master->setVialsUsed($vials_used);
     //                $hf_data_master->setAdjustments($unusable_vials_array[$key]);
     //                $hf_data_master->setReportingStartDate(new \DateTime(App_Controller_Functions::dateToDbFormat($data['rpt_date'])));
     //
     //                $hf_data_master->setWastages($wastages);
     //                $hf_data_master->setCreatedDate(new \DateTime(App_Controller_Functions::dateToDbFormat($date)));
     //                $hf_data_master->setModifiedDate(new \DateTime(App_Controller_Functions::dateToDbFormat(date("Y-m-d"))));
     //
     //
     //                $this->_em->persist($hf_data_master);
     //                $this->_em->flush();
     //            }
     //        }
     $posted_array = $data['flitm_id'];
     $vaccine_schedule_id = $data['vaccine_schedule_id'];
     $opening_array = $data['opening_balance'];
     $received_array = $data['received'];
     $dispensed_array = $data['dispensed'];
     $pregenant_women = $data['pregenant_women'];
     $non_pregenant_women = $data['non_pregenant_women'];
     $pregnant_women = $data['pregnant_women1'];
     $cba = $data['cba'];
     $pregenant_women_total = $data['pregenant_women_total'];
     $non_pregenant_women_total = $data['non_pregenant_women_total'];
     $item_category = $data['item_category'];
     // $vials_used_array = $data['vials_used'];
     $unusable_vials_array = $data['unusable_vials'];
     $closing_balance_array = $data['closing_balance'];
     // $nearest_expiry_array = $data['nearest_expiry'];
     $doses_per_unit_array = $data['doses_per_unit'];
     //$record_array = $data['record_id'];
     // vaccine schedule
     $strat_no = $data['start_no'];
     $no_of_doses = $data['no_of_doses'];
     $children_live_birth = $data['children_live_birth'];
     $surviving_children_0_11_M = $data['surviving_children_0_11_M'];
     $children_aged_12_23_M = $data['children_aged_12_23_M'];
     // for ($i = $strat_no; $i <= $no_of_doses; $i++) {
     // }
     foreach ($closing_balance_array as $key => $val) {
         if ($val != '' && $val >= 0) {
             if ($item_category[$key] == 1 || $item_category[$key] == 2) {
                 $vials_used = $opening_array[$key] + $received_array[$key] - $closing_balance_array[$key] - $unusable_vials_array[$key];
             } else {
                 $vials_used = $opening_array[$key] + $received_array[$key] - $closing_balance_array[$key];
             }
             $year_month = $data['yearmonth'];
             $doses = $doses_per_unit_array[$key];
             $wh_id = $data['wh_id'];
             if ($item_category[$key] == 1) {
                 // echo $strat_no[$key]."</br>";
                 //  echo $item_id."</br>";;
                 $nod = $no_of_doses[$key];
                 for ($i = $strat_no[$key]; $i <= $no_of_doses[$key]; $i++) {
                     if ($i == 0) {
                         $nod += 1;
                     }
                     //$vacine_schedule = ($i == 1 && $i == $nod) ? '' : $i;
                     $vacine_schedule = $i == 1 && $i == $nod ? '' : $i;
                     if (strlen($vacine_schedule) == 0) {
                         $vacine_schedule = 1;
                     }
                     $fix_inuc_m_11 = $data['fix_inuc_m_11_' . $posted_array[$key] . '_' . $vacine_schedule];
                     $fix_inuc_f_11 = $data['fix_inuc_f_11_' . $posted_array[$key] . '_' . $vacine_schedule];
                     $fix_outuc_m_11 = $data['fix_outuc_m_11_' . $posted_array[$key] . '_' . $vacine_schedule];
                     $fix_outuc_f_11 = $data['fix_outuc_f_11_' . $posted_array[$key] . '_' . $vacine_schedule];
                     $ref_outuc_m_11 = $data['ref_outuc_m_11_' . $posted_array[$key] . '_' . $vacine_schedule];
                     $ref_outuc_f_11 = $data['ref_outuc_f_11_' . $posted_array[$key] . '_' . $vacine_schedule];
                     $outreach_m_11 = $data['outreach_m_11_' . $posted_array[$key] . '_' . $vacine_schedule];
                     $outreach_f_11 = $data['outreach_f_11_' . $posted_array[$key] . '_' . $vacine_schedule];
                     $fix_inuc_m_23 = $data['fix_inuc_m_23_' . $posted_array[$key] . '_' . $vacine_schedule];
                     $fix_inuc_f_23 = $data['fix_inuc_f_23_' . $posted_array[$key] . '_' . $vacine_schedule];
                     $fix_outuc_m_23 = $data['fix_outuc_m_23_' . $posted_array[$key] . '_' . $vacine_schedule];
                     $fix_outuc_f_23 = $data['fix_outuc_f_23_' . $posted_array[$key] . '_' . $vacine_schedule];
                     $ref_outuc_m_23 = $data['ref_outuc_m_23_' . $posted_array[$key] . '_' . $vacine_schedule];
                     $ref_outuc_f_23 = $data['ref_outuc_f_23_' . $posted_array[$key] . '_' . $vacine_schedule];
                     $outreach_m_23 = $data['outreach_m_23_' . $posted_array[$key] . '_' . $vacine_schedule];
                     $outreach_f_23 = $data['outreach_f_23_' . $posted_array[$key] . '_' . $vacine_schedule];
                     $issue = $fix_inuc_m_11 + $fix_inuc_f_11 + $fix_outuc_m_11 + $fix_outuc_f_11 + $ref_outuc_m_11 + $ref_outuc_f_11 + $outreach_m_11 + $outreach_f_11 + $fix_inuc_m_23 + $fix_inuc_f_23 + $fix_outuc_m_23 + $fix_outuc_f_23 + $ref_outuc_m_23 + $ref_outuc_f_23 + $outreach_m_23 + $outreach_f_23;
                 }
             } else {
                 if ($item_category[$key] == 2) {
                     $issue = $pregenant_women_total + $non_pregenant_women_total;
                 } else {
                     if ($item_category[$key] == 3) {
                         $issue = $dispensed_array[$key];
                     }
                 }
             }
             $itemid = $posted_array[$key];
             $wh_adj = $unusable_vials_array[$key];
             if ($item_category[$key] == 1 || $item_category[$key] == 2) {
                 $wastages = $vials_used - $issue + abs($wh_adj);
             } else {
                 $wastages = $vials_used - $issue;
             }
             $hf_data_master = new HfDataMasterDraft();
             $item = $this->_em->getRepository('ItemPackSizes')->find($posted_array[$key]);
             $warehouse = $this->_em->getRepository('Warehouses')->find($data['wh_id']);
             $hf_data_master->setItemPackSize($item);
             $hf_data_master->setWarehouse($warehouse);
             $user_id = $this->_em->getRepository('Users')->find($this->_user_id);
             $hf_data_master->setCreatedBy($user_id);
             $hf_data_master->setOpeningBalance($opening_array[$key]);
             $hf_data_master->setReceivedBalance($received_array[$key]);
             $hf_data_master->setIssueBalance($issue);
             $hf_data_master->setClosingBalance($val);
             $hf_data_master->setVialsUsed($vials_used);
             if ($item_category[$key] == 1 || $item_category[$key] == 2) {
                 $hf_data_master->setAdjustments($unusable_vials_array[$key]);
             }
             $hf_data_master->setReportingStartDate(new \DateTime(App_Controller_Functions::dateToDbFormat($data['rpt_date'])));
             $hf_data_master->setWastages($wastages);
             $hf_data_master->setCreatedDate(new \DateTime(App_Controller_Functions::dateToDbFormat($date)));
             $hf_data_master->setModifiedDate(new \DateTime(App_Controller_Functions::dateToDbFormat(date("Y-m-d"))));
             //echo $this->monthlyConsumtion2Targets($wh_id, App_Controller_Functions::dateToDbFormat($data['rpt_date']));
             if ($data['is_new_report'] == 1) {
                 $hf_data_master->setChildrenLiveBirth($children_live_birth);
                 $hf_data_master->setSurvivingChildren011($surviving_children_0_11_M);
                 $hf_data_master->setChildrenAged1223($children_aged_12_23_M);
                 $hf_data_master->setPregnantWomen($pregnant_women);
                 $hf_data_master->setCbas($cba);
             } else {
                 if ($data['is_new_report'] == 0) {
                     $this->updateYearlyTargets($wh_id, App_Controller_Functions::dateToDbFormat($data['rpt_date']), $children_live_birth, $surviving_children_0_11_M, $children_aged_12_23_M, $pregnant_women);
                 }
             }
             $this->_em->persist($hf_data_master);
             $this->_em->flush();
             $hf_data_master_id = $hf_data_master->getPkId();
             if ($item_category[$key] == 1) {
                 $nod = $no_of_doses[$key];
                 for ($i = $strat_no[$key]; $i <= $no_of_doses[$key]; $i++) {
                     if ($i == 0) {
                         $nod += 1;
                     }
                     $vacine_schedule = $i == 1 && $i == $nod ? '' : $i;
                     if (strlen($vacine_schedule) == 0) {
                         $vacine_schedule = 1;
                     }
                     $fix_inuc_m_11 = $data['fix_inuc_m_11_' . $posted_array[$key] . '_' . $vacine_schedule];
                     $fix_inuc_f_11 = $data['fix_inuc_f_11_' . $posted_array[$key] . '_' . $vacine_schedule];
                     $fix_outuc_m_11 = $data['fix_outuc_m_11_' . $posted_array[$key] . '_' . $vacine_schedule];
                     $fix_outuc_f_11 = $data['fix_outuc_f_11_' . $posted_array[$key] . '_' . $vacine_schedule];
                     $ref_outuc_m_11 = $data['ref_outuc_m_11_' . $posted_array[$key] . '_' . $vacine_schedule];
                     $ref_outuc_f_11 = $data['ref_outuc_f_11_' . $posted_array[$key] . '_' . $vacine_schedule];
                     $outreach_m_11 = $data['outreach_m_11_' . $posted_array[$key] . '_' . $vacine_schedule];
                     $outreach_f_11 = $data['outreach_f_11_' . $posted_array[$key] . '_' . $vacine_schedule];
                     $fix_inuc_m_23 = $data['fix_inuc_m_23_' . $posted_array[$key] . '_' . $vacine_schedule];
                     $fix_inuc_f_23 = $data['fix_inuc_f_23_' . $posted_array[$key] . '_' . $vacine_schedule];
                     $fix_outuc_m_23 = $data['fix_outuc_m_23_' . $posted_array[$key] . '_' . $vacine_schedule];
                     $fix_outuc_f_23 = $data['fix_outuc_f_23_' . $posted_array[$key] . '_' . $vacine_schedule];
                     $ref_outuc_m_23 = $data['ref_outuc_m_23_' . $posted_array[$key] . '_' . $vacine_schedule];
                     $ref_outuc_f_23 = $data['ref_outuc_f_23_' . $posted_array[$key] . '_' . $vacine_schedule];
                     $outreach_m_23 = $data['outreach_m_23_' . $posted_array[$key] . '_' . $vacine_schedule];
                     $outreach_f_23 = $data['outreach_f_23_' . $posted_array[$key] . '_' . $vacine_schedule];
                     $hf_data_detail = new HfDataDetailDraft();
                     $ageGroup = $this->_em->getRepository('ListDetail')->find(Model_ListDetail::AGE_0_11);
                     $hf_data_detail->setAgeGroup($ageGroup);
                     $hf_data_detail->setFixedInsideUcFemale($fix_inuc_f_11);
                     $hf_data_detail->setFixedInsideUcMale($fix_inuc_m_11);
                     $hf_data_detail->setFixedOutsideUcFemale($fix_outuc_f_11);
                     $hf_data_detail->setFixedOutsideUcMale($fix_outuc_m_11);
                     $hf_data_detail->setReferalFemale($ref_outuc_f_11);
                     $hf_data_detail->setReferalMale($ref_outuc_m_11);
                     $hf_data_detail->setOutreachFemale($outreach_f_11);
                     $hf_data_detail->setOutreachMale($outreach_m_11);
                     $hf_data_detail->setVaccineScheduleId($vaccine_schedule);
                     $hf_data_master_i = $this->_em->getRepository('HfDataMasterDraft')->find($hf_data_master_id);
                     $hf_data_detail->setHfDataMaster($hf_data_master_i);
                     $this->_em->persist($hf_data_detail);
                     $this->_em->flush();
                     $hf_data_detail1 = new HfDataDetailDraft();
                     $ageGroup1 = $this->_em->getRepository('ListDetail')->find(Model_ListDetail::AGE_12_23);
                     $hf_data_detail1->setAgeGroup($ageGroup1);
                     $hf_data_detail1->setFixedInsideUcFemale($fix_inuc_f_23);
                     $hf_data_detail1->setFixedInsideUcMale($fix_inuc_m_23);
                     $hf_data_detail1->setFixedOutsideUcFemale($fix_outuc_f_23);
                     $hf_data_detail1->setFixedOutsideUcMale($fix_outuc_m_23);
                     $hf_data_detail1->setReferalFemale($ref_outuc_f_23);
                     $hf_data_detail1->setReferalMale($ref_outuc_m_23);
                     $hf_data_detail1->setOutreachFemale($outreach_f_23);
                     $hf_data_detail1->setOutreachMale($outreach_m_23);
                     $hf_data_detail1->setVaccineScheduleId($vaccine_schedule);
                     $hf_data_master_i1 = $this->_em->getRepository('HfDataMasterDraft')->find($hf_data_master_id);
                     $hf_data_detail1->setHfDataMaster($hf_data_master_i1);
                     $this->_em->persist($hf_data_detail1);
                     $this->_em->flush();
                 }
             }
             if ($item_category[$key] == 2) {
                 foreach ($pregenant_women as $key => $val) {
                     $hf_data_detail = new HfDataDetailDraft();
                     $hf_data_detail->setPregnantWomen($val);
                     $hf_data_detail->setNonPregnantWomen($non_pregenant_women[$key]);
                     $hf_data_detail->setVaccineScheduleId($vaccine_schedule_id[$key]);
                     $hf_data_master_i = $this->_em->getRepository('HfDataMasterDraft')->find($hf_data_master_id);
                     $hf_data_detail->setHfDataMaster($hf_data_master_i);
                     $this->_em->persist($hf_data_detail);
                     $this->_em->flush();
                 }
             }
         }
     }
 }