/**
  * Manages all models.
  */
 public function actionAdmin()
 {
     $model = new Beneficiary('search');
     $model->unsetAttributes();
     // clear any default values
     if (isset($_GET['Beneficiary'])) {
         $model->attributes = $_GET['Beneficiary'];
     }
     $this->render('admin', array('model' => $model));
 }
 public function actionImport()
 {
     $model = new Beneficiary('search');
     $model->unsetAttributes();
     $result = new BeneficiaryImportResult();
     $count_inserted = 0;
     $count_updated = 0;
     if (isset($_FILES) && !empty($_FILES)) {
         move_uploaded_file($_FILES['UploadFileName']['tmp_name'], Yii::app()->params['IMPORT_PATH'] . $_FILES['UploadFileName']['name']);
         Yii::import('application.extensions.phpexcel.Classes.PHPExcel', true);
         $objReader = new PHPExcel_Reader_Excel5();
         $objPHPExcel = $objReader->load(Yii::app()->params['IMPORT_PATH'] . $_FILES['UploadFileName']['name']);
         $objWorksheet = $objPHPExcel->getActiveSheet();
         $highestRow = $objWorksheet->getHighestRow();
         // e.g. 10
         $highestColumn = $objWorksheet->getHighestColumn();
         // e.g 'F'
         $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
         // e.g. 5
         $criteria = new CDbCriteria();
         $criteria->addCondition('name="Active"');
         // created Status for vouchers
         $status = BeneficiaryStatus::model()->find($criteria);
         $inserted_arr = [];
         $updated_arr = [];
         for ($row = 2; $row <= $highestRow; ++$row) {
             $criteria = new CDbCriteria();
             $criteria->addCondition('registration_code="' . $objWorksheet->getCellByColumnAndRow(0, $row)->getValue() . '"');
             // created Status for vouchers
             $beneficiary = Beneficiary::model()->find($criteria);
             $insert = 0;
             if (!$beneficiary) {
                 $beneficiary = new Beneficiary();
                 $insert = 1;
                 $count_inserted++;
                 //$result->set_count_inserted($result->get_count_inserted()++);
             } else {
                 $count_updated++;
             }
             //$result->set_count_updated($result->get_count_updated()++);
             $communitycriteria = new CDbCriteria();
             $communitycriteria->addCondition('en_name="' . $objWorksheet->getCellByColumnAndRow(6, $row)->getValue() . '"');
             $community = Community::model()->find($communitycriteria);
             $beneficiary->registration_code = $objWorksheet->getCellByColumnAndRow(0, $row)->getValue();
             $beneficiary->ar_name = $objWorksheet->getCellByColumnAndRow(1, $row)->getValue() ? $objWorksheet->getCellByColumnAndRow(1, $row)->getValue() : $beneficiary->ar_name;
             $beneficiary->en_name = $objWorksheet->getCellByColumnAndRow(2, $row)->getValue() ? $objWorksheet->getCellByColumnAndRow(2, $row)->getValue() : $beneficiary->en_name;
             $beneficiary->family_member = $objWorksheet->getCellByColumnAndRow(3, $row)->getValue() ? $objWorksheet->getCellByColumnAndRow(3, $row)->getValue() : $beneficiary->family_member;
             $beneficiary->main_income_source = $objWorksheet->getCellByColumnAndRow(4, $row)->getValue() ? $objWorksheet->getCellByColumnAndRow(4, $row)->getValue() : $beneficiary->main_income_source;
             $beneficiary->combine_household = $objWorksheet->getCellByColumnAndRow(5, $row)->getValue() ? $objWorksheet->getCellByColumnAndRow(5, $row)->getValue() : $beneficiary->combine_household;
             $beneficiary->phone_number = $objWorksheet->getCellByColumnAndRow(7, $row)->getValue() ? $objWorksheet->getCellByColumnAndRow(7, $row)->getValue() : $beneficiary->phone_number;
             $beneficiary->neighborhood_id = $objWorksheet->getCellByColumnAndRow(6, $row)->getValue() ? $community->id : $beneficiary->neighborhood_id;
             $beneficiary->status_id = $status->id;
             $beneficiary->save();
             if ($insert) {
                 array_push($inserted_arr, $beneficiary->registration_code);
             } else {
                 array_push($updated_arr, $beneficiary->registration_code);
             }
         }
     } else {
         $result->set_error('No File Selected');
     }
     $result->set_count_inserted($count_inserted);
     $result->set_count_updated($count_updated);
     $result->set_record_inserted($inserted_arr);
     $result->set_record_updated($updated_arr);
     return $this->sendAjaxResponse($result);
 }
Exemple #3
0
 public function actionGenerate()
 {
     $model = new Beneficiary('searchForVoucherAssignment');
     $model->unsetAttributes();
     if (isset($_POST) && !empty($_POST)) {
         $subdistribution = Subdistribution::model()->findByPk($_POST['subdistribution_id']);
         if ($subdistribution) {
             // getting Voucher Types assosiated to this distribution.
             $distributionVouchers = $subdistribution->distributionVouchers;
             // getting The 'PENDING' status
             $criteria = new CDbCriteria();
             $criteria->addCondition('name="PENDING"');
             // created Status for vouchers
             $status = VoucherStatus::model()->find($criteria);
             // Choose eligible beneficiaries -- who are not registered in this distribution (Not sub distribution)
             $criteria3 = new CDbCriteria();
             $criteria_string = "t.id in (0";
             if (isset($_POST['beneficiaries']) && !empty($_POST['beneficiaries'])) {
                 foreach ($_POST['beneficiaries'] as $ben_id) {
                     $criteria_string = $criteria_string . ", " . $ben_id;
                 }
             }
             $criteria_string = $criteria_string . ")";
             $criteria3->addCondition($criteria_string);
             $beneficiaries = Beneficiary::model()->findAll($criteria3);
             $characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
             $random_string_length = 10;
             foreach ($beneficiaries as $beneficiary) {
                 // for each beneficiary
                 foreach ($distributionVouchers as $distributionVoucher) {
                     // for each voucher type
                     //try to find an existing voucher assosiated to this beneficiary
                     $criteria4 = new CDbCriteria();
                     $criteria4->condition = "ben_id = " . $beneficiary->id . " and distribution_voucher_id = " . $distributionVoucher->id;
                     $exist = Voucher::model()->findAll($criteria4);
                     // if no voucher found :
                     if (count($exist) == 0) {
                         $voucher = new Voucher();
                         // create voucher
                         $string = '';
                         $exist = "";
                         //Generate random number
                         do {
                             for ($i = 0; $i < $random_string_length; $i++) {
                                 $string .= $characters[rand(0, strlen($characters) - 1)];
                             }
                             $exist = Voucher::model()->exists('code =:code', array(":code" => $string));
                         } while ($exist == "1");
                         $voucher->code = $string;
                         $voucher->distribution_voucher_id = $distributionVoucher->id;
                         $voucher->ben_id = $beneficiary->id;
                         $voucher->vendor = NULL;
                         $voucher->status_id = $status->id;
                         $voucher->create_date = new CDbExpression('NOW()');
                         $voucher->insert();
                         $voucher->save();
                     }
                 }
             }
         }
     } else {
         if (isset($_GET['Beneficiary'])) {
             $model->setAttributes($_GET['Beneficiary']);
         }
         $this->render('generate', array('model' => $model));
         //$this->render('generate');
     }
 }