public function actionAdmin() { $model = new Beneficiary('search'); $model->unsetAttributes(); if (isset($_GET['Beneficiary'])) { $model->setAttributes($_GET['Beneficiary']); } $this->render('admin', array('model' => $model)); }
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'); } }