public function actionStep3() { if (isFrontUserLoggedIn()) { $visit_code = Yii::app()->session['visit_code']; $visit_model = Visits::model()->find(array('condition' => 'visit_code = "' . $visit_code . '" ')); $step3_model = new Step3(); if (isset($_POST['Step3'])) { $step3_model->attributes = $_POST['Step3']; if ($step3_model->validate()) { $donation_model = new Donation(); $donation_model->user_id = Yii::app()->session['user_id']; $donation_model->visit_id = $visit_model->id; $donation_model->mobile = '123456789'; $donation_model->solicitor_id = $visit_model->solicitor->id; $donation_model->amount = $step3_model->amount; $donation_model->mode = 'web'; $donation_model->short_note = $step3_model->message; $donation_model->payment_status = 'pending'; $donation_model->reference_number = getToken(8); $donation_model->validate(); $donation_model->save(); // calculating the debit amount to be inserted in the user_trans table $user_id = Yii::app()->session['user_id']; $user_balance = Users::model()->getUserBalance($user_id); $user_model = Users::model()->findByPk($user_id); $credit_limits = $user_model->credit_limits; $actual_don_amt = $step3_model->amount; // if ($actual_don_amt > $user_balance) { // // deducting the user credit // if ($user_balance > 0) { // $from_user_credit = $actual_don_amt - $user_balance; // } else { // $from_user_credit = $actual_don_amt; // } // $final_user_credit = $credit_limits - $from_user_credit; // $user_model->credit_limits = $final_user_credit; // $user_model->validate(); // $user_model->save(); // } // for storing in the user_trans table $trans_model = new UserTrans(); $trans_model->tran_type = 'DONATION'; $trans_model->user_id = $donation_model->user_id; $trans_model->debit = $actual_don_amt; $trans_model->donation_id = $donation_model->id; $trans_model->save(); unset(Yii::app()->session['visit_code']); $this->redirect(array("/user/default/accountSummary")); } } $this->render('step3', array('visit_model' => $visit_model, 'step3_model' => $step3_model)); } else { $this->redirect(array("/user")); } }
/** * Add or edit donation * CODE: donation_create */ public function executeUpdate(sfWebRequest $request) { # security if (!$this->getUser()->hasCredential(array('Administrator'), false)) { $this->getUser()->setFlash("warning", 'You don\'t have permission to access this url ' . $request->getReferer()); $this->redirect('dashboard/index'); } if ($request->getParameter('id')) { $dona = DonationPeer::retrieveByPK($request->getParameter('id')); $this->forward404Unless($dona); $this->title = 'Edit donation'; $success = 'Donation information has been successfully changed!'; } else { $dona = new Donation(); $this->title = 'Add new donation'; $success = 'Donation information has been successfully created!'; } $this->form = new DonationForm($dona); if ($request->isMethod('post')) { $this->referer = $request->getParameter('referer'); $this->form->bind($request->getParameter('dona')); if ($this->form->isValid() && $this->form->getValue('campain_id') && $this->form->getValue('donor_id') && $this->form->getValue('fund_id') && $this->form->getValue('gift_type')) { $donor = DonorPeer::getByPersonId($this->form->getValue('donor_id')); if ($donor) { $id = $donor->getId(); } $dona->setDonorId($id); $dona->setGiftDate($this->form->getValue('gift_date')); $dona->setGiftAmount($this->form->getValue('gift_amount')); $dona->setDeductibleAmount($this->form->getValue('deductible_amount')); $dona->setGiftType($this->form->getValue('gift_type')); $dona->setCheckNumber($this->form->getValue('check_number')); $dona->setCampainId($this->form->getValue('campain_id')); $dona->setFundId($this->form->getValue('fund_id')); $dona->setGiftNote($this->form->getValue('gift_note')); $dona->setPrintedNote($this->form->getValue('printed_note')); $dona->setReceiptGeneratedDate($this->form->getValue('receipt_generated_date')); if ($this->form->getValue('follow_up') == null) { $dona->setFollowUp(0); } else { $dona->setFollowUp($this->form->getValue('follow_up')); } $dona->setPremiumOrderDate($this->form->getValue('premium_order_date')); $dona->save(); $this->getUser()->setFlash('success', $success); $this->redirect('@donation'); } else { $this->getUser()->setFlash('success', 'Please choice Donor, Gift Type, Campaign or Fund!'); } } else { # Set referer URL $this->referer = $request->getReferer() ? $request->getReferer() : '@danation'; } $this->dona = $dona; }
public static function add($input) { // set rules $rules = array('user_id' => 'required|exists:users,id', 'type_name' => 'required|in:social_targets,social_actions', 'type_id' => 'required|integer', 'currency' => 'required', 'email' => 'required}email', 'total' => 'required|numeric', 'message' => '', 'as_noname' => 'sometimes'); $validator = Validator::make($input, $rules); if ($validator->fails()) { // if fails return array('success' => false, 'errors' => $validator->errors()->all()); } else { // save to database $donation = new Donation(); // set input foreach ($input as $field => $value) { $donation->{$field} = $value; } $donation->status = 0; // new (waiting approval) $donation->save(); // send invoice email Newsletter::addInvoiceNewsletter($donation); return array('success' => true, 'data' => $donation); } }
/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { $model = new Donation(); if (isset($_GET['user'])) { $model->user_id = $_GET['user']; } if (isset($_GET['visit'])) { $model->visit_id = $_GET['visit']; $model->solicitor_id = Visits::model()->findByPk($model->visit_id)->solicitor_id; } $users_lists = BaseModel::getAll('Users'); $users = array(); foreach ($users_lists as $user) { $users[$user->id] = $user->first_name . ' ' . $user->last_name . '(' . $user->username . ')'; } // $users = CHtml::listData(BaseModel::getAll('Users'),'id','username'); $lists = BaseModel::getAll('Solicitor'); $solicitors = array(); foreach ($lists as $list) { $solicitors[$list->id] = $list->first_name . ' ' . $list->last_name . '(' . $list->solicitor_code . ')'; } $visits = CHtml::listData(BaseModel::getAll('Visits'), 'id', 'visit_code'); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['Donation'])) { $model->attributes = $_POST['Donation']; $model->reference_number = getToken(8); $model->validate(); if (!empty($model->user_id)) { $user_id = $model->user_id; $user_balance = Users::model()->getUserBalance($user_id); $user_model = Users::model()->findByPk($user_id); $credit_limits = $user_model->credit_limits; $donation_amt = $model->amount; if ($user_balance >= 0) { if ($user_balance < $donation_amt) { $user_allowable_amount = $user_balance + $credit_limits; if ($donation_amt > $user_allowable_amount) { $model->addError('amount', "Sorry. The user has not the sufficient balance."); } } } else { if ($user_balance < 0) { $user_allowable_amount = $credit_limits - abs($user_balance); if ($donation_amt > $user_allowable_amount) { $model->addError('amount', "Sorry. The user has not the sufficient balance."); } } } } if (empty($model->errors)) { if ($model->save()) { $trans = new UserTrans(); $trans->tran_type = 'DONATION'; $trans->user_id = $model->user_id; $trans->debit = $model->amount; $trans->donation_id = $model->id; $trans->save(); $this->redirect(array('view', 'id' => $model->id)); } } } $this->render('create', array('model' => $model, 'users' => $users, 'solicitors' => $solicitors, 'visits' => $visits)); }
/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { $model = new Donation; if(isset($_GET['user'])){ $model->user_id = $_GET['user']; } if(isset($_GET['visit'])){ $model->visit_id = $_GET['visit']; $model->solicitor_id = Visits::model()->findByPk($model->visit_id)->solicitor_id; } $users_lists = BaseModel::getAll('Users'); $users = array(); foreach($users_lists as $user){ $users[$user->id] = $user->first_name.' '.$user->last_name.'('.$user->username.')'; } // $users = CHtml::listData(BaseModel::getAll('Users'),'id','username'); $lists = BaseModel::getAll('Solicitor'); $solicitors = array(); foreach($lists as $list){ $solicitors[$list->id] = $list->first_name.' '.$list->last_name.'('.$list->solicitor_code.')'; } $visits = CHtml::listData(BaseModel::getAll('Visits'),'id','visit_code'); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if(isset($_POST['Donation'])) { $model->attributes=$_POST['Donation']; $model->reference_number = getToken(8); if($model->save()){ $user_balance = Users::model()->getUserBalance($model->user_id); $user_model = Users::model()->findByPk($model->user_id); $credit_limits = $user_model->credit_limits; if ($model->amount > $user_balance) { // deducting the user credit if ($user_balance > 0) { $from_user_credit = $model->amount - $user_balance; } else { $from_user_credit = $model->amount; } $final_user_credit = $credit_limits - $from_user_credit; $user_model->credit_limits = $final_user_credit; $user_model->save(); } // for storing in the user_trans table $trans = new UserTrans; $trans->tran_type = 'DONATION'; $trans->user_id = $model->user_id; $trans->debit = $model->amount; $trans->donation_id = $model->id; $trans->save(); $this->redirect(array('view','id'=>$model->id)); } } $this->render('create',array( 'model'=>$model, 'users'=>$users, 'solicitors'=>$solicitors, 'visits'=>$visits )); }
/** * Store a newly created resource in storage. * * @return Response */ public function store() { /*$validator = $this->validate(); if($validator->fails()){ return Redirect::back() ->withErrors($validator->messages()) ->withInput(Input::all()); }*/ $validator = $this->validate(); if ($validator->fails()) { if (Request::ajax()) { return Response::json(array('success' => false, 'messages' => $validator->messages())); } else { return Redirect::back()->withErrors($validator->messages())->withInput(Input::all()); } } $donor = Donor::find(Input::get('donor_id')); $product = Product::find(Input::get('product_id')); // Donor can only give blood of it's own blood type if (strtolower($product->category->name) == 'blood') { if (strtolower($donor->blood_group->name) != strtolower($product->name)) { //apply ajax if (Request::ajax()) { return Response::json(array('success' => false, 'messages' => ['This donor is only able to donate blood of type ' . $donor->blood_group->name . ' only.'])); } else { return Redirect::back()->withErrors(['This donor is only able to donate blood of type ' . $donor->blood_group->name . ' only.'])->withInput(Input::all()); } } } // Check if donor is eligible to make the donation if (count($donor->donations)) { // Check if donor is donating within the validity period $validity_days = $product->validity_period; $last_donated_at = new Carbon\Carbon($donor->donations->last()->donated_at); $next_possible_donation_date = $last_donated_at->addDays($validity_days); $today = Carbon\Carbon::now(); if ($next_possible_donation_date->gt($today)) { //if ajax request nam return response. if (Request::ajax()) { return Response::json(array('success' => false, 'messages' => ['This donor is not able to donate untill ' . $next_possible_donation_date->format('Y-m-d') . '.'])); } else { return Redirect::back()->withErrors(['This donor is not able to donate untill ' . $next_possible_donation_date->format('Y-m-d') . '.'])->withInput(Input::all()); } } } DB::beginTransaction(); $donation = new Donation(); $donation->donor_id = Input::get('donor_id'); $donation->product_id = Input::get('product_id'); $donation->location_id = Input::get('location_id'); $donation->quantity = Input::get('quantity'); $donation->donated_at = Input::get('donated_at'); $donation->save(); /*if (Request::ajax()){ return Response::json(array( 'success' => true, 'donors' => [''] + Donor::all()->lists('name_with_blood_group', 'id'), 'donor_id' => $donor->id, )); }*/ // Increment product quantity /*$product = $donation->product; $product->quantity = $product->quantity + $donation->quantity; $product->save();*/ // increment count in item_in table // increment count in location_products table $response = Event::fire('donation.create', array($donation)); DB::commit(); if (Request::ajax()) { return Response::json(array('success' => true, 'donors' => [''] + Donor::all()->lists('name_with_blood_group', 'id'), 'donor_id' => $donor->id)); } else { Session::flash('success', 'Successfully created donation!'); return Redirect::route('donation.index'); } }