public static function add($first_name, $last_name, $phone, $email, $password, $zipcode, $referral_code = '', $device_token = '', $device_type = 'web', $membership_type = 'Member') { $user = new User(); $user_credit = new UserCredit(); $user->first_name = $first_name; $user->last_name = $last_name; $user->phone = $phone; $user->email = $email; $user->referred_by = 0; $user->token = generate_token(); $user->token_expiry = generate_expiry() / 10; $user->social_id = 0; if ($device_type != 'web') { $user->device_token = $device_token; } else { $user->device_token = ''; } $user->device_type = $device_type; $user->image_url = ''; $user->total_referrals = 0; $user->remember_token = ''; $user_credit->earned = 0; $user_credit->spent = 0; if ($referral_code != '') { $referrer = User::where('referral_code', $referral_code)->first(); if ($referrer) { $user->referred_by = $referrer->id; $user_credit->earned = 10; // add credits to referrer $referrer_credit = UserCredit::where('user_id', $referrer->id)->first(); $referrer_credit->earned += 5; $referrer_credit->spent = 0; $referrer_credit->save(); $temp_user = User::find($referrer->id); $temp_user->total_referrals += 1; $temp_user->save(); } } $user->password = Hash::make($password); $user->membership_type = $membership_type; $user->zipcode = $zipcode; $user->referral_code = generate_referral_code($first_name, $last_name); if ($membership_type == 'Member') { $user->membership_ends_on = "2100-01-01"; } else { $user->membership_ends_on = date('Y-m-d', strtotime('+1 years')); } $user->save(); $user_credit->user_id = $user->id; $user_credit->save(); return $user; }
public function actionDownload($flag) { $user_credit_model = UserCredit::model()->find(array("condition" => "id ='".$flag."' ")); //$this->renderPartial('receipt', array('model' => $user_credit_model)); $mPDF1 = Yii::app()->ePdf->mpdf(); $mPDF1->WriteHTML($this->renderPartial('receipt', array('model'=>$user_credit_model), true)); $mPDF1->Output('receipt.pdf','D'); }
public function actionTransaction($id) { $user_id = $id; $user = Users::model()->findByPk($id); $sql = "SELECT id, COALESCE(SUM(credit),0) - COALESCE(SUM(debit),0) AS balance from user_trans where user_id = '" . $id . "'"; $result = BaseModel::executeSimpleQueryFirstRow($sql); if(!empty($result['balance'])) { $balance = $result['balance']; } else { $balance = 0; } $c_sql = "SELECT SUM(credit) AS credited FROM user_trans WHERE credit IS NOT NULL AND user_id = '$id'"; $c_result = BaseModel::executeSimpleQueryFirstRow($c_sql); if(empty($c_result['credited'])){ $credit = 0; } else { $credit = $c_result['credited']; } $d_sql = "SELECT SUM(debit) AS debited FROM user_trans WHERE debit IS NOT NULL AND user_id = '$id'"; $d_result = BaseModel::executeSimpleQueryFirstRow($d_sql); if(empty($d_result['debited'])){ $debit = 0; } else { $debit = $d_result['debited']; } $donations = new Donation('users'); $donations->unsetAttributes(); $payments = new UserCredit('users'); $payments->unsetAttributes(); $logs = new Log('users'); $logs->unsetAttributes(); $this->render('transaction', array( 'user' => $user, 'balance' => $balance, 'credit' => $credit, 'debit' => $debit, 'donations' => $donations, 'payments' => $payments, 'logs' => $logs )); }
public function update_credit() { $user_id = $this->user_id; $coupon_code = Input::get('coupon_code'); $coupon = Coupon::where('coupon_code', $coupon_code)->first(); if ($coupon) { $coupon_user = UserCoupon::where('coupon_id', $coupon->id)->where('user_id', $this->user_id)->first(); if (!$coupon_user) { $amount = $coupon->amount; $user_credit = UserCredit::where('user_id', $user_id)->first(); $user_credit->earned += $amount; $user_credit->save(); $coupon_user = new UserCoupon(); $coupon_user->coupon_id = $coupon->id; $coupon_user->user_id = $this->user_id; $coupon_user->save(); if (Request::format() == 'html') { // Redirect to credits page return Redirect::to('/user'); } else { $response_array['success'] = 'true'; $response_array['credits'] = $user_credit->toArray(); $response_code = 200; $response = Response::json($response_array, $response_code); return $response; } } else { $message = "Coupon code already used"; $error_code = 408; } } else { $message = "Invalid coupon code"; $error_code = 409; } if (Request::format() == 'html') { return Redirect::to('/user')->with('message', $message)->with('type', 'danger'); } else { $response_array = array('success' => 'false', 'error_code' => '405', 'error' => $message); $response_code = 200; $response = Response::json($response_array, $response_code); return $response; } }
public function update_order_status() { $id = Request::segment(3); $order = Order::find($id); $order->status = Input::get('status'); $order->save(); if ($order->status == 'Delivered') { $total = $order->total_amount; // Charge Users $credits = UserCredit::where('user_id', $order->user_id)->first(); $balance = $credits->earned - $credits->spent; if ($total > $balance) { $ledger_temp = UserCredit::find($credits->id); $ledger_temp->spent = $ledger_temp->earned; $ledger_temp->save(); $total = $total - $balance; } else { $ledger_temp = UserCredit::find($credits->id); $ledger_temp->spent = $ledger_temp->spent + $total; $ledger_temp->save(); $total = 0; } $order = Order::find($id); $order->card_payment = $total; $order->credit_payment = $order->total_amount - $total; $order->save(); try { $payment = Payment::find($order->payment_id); $customer_id = $payment->customer_id; Stripe::setApiKey(Config::get('app.stripe_secret_key')); Stripe_Charge::create(array("amount" => $total * 100, "currency" => "usd", "customer" => $customer_id)); $walk->is_paid = 1; if ($walk->tip) { $walk->tip_status = 2; } } catch (Exception $e) { //print_r($e); } // Payment Charged Mail $user = User::find($order->user_id); Mail::send('emails.payment_charged', array('user' => $user, 'order' => $order), function ($message) use($user) { $message->to($user->email, $user->first_name)->subject('Order Successfully Delivered!'); }); // Payment Charged Mail to Admin $admin_email = Config::get('app.admin_email'); Mail::send('emails.admin_payment_charged', array('user' => $user, 'order' => $order), function ($message) use($admin_email) { $message->to($admin_email, "Admin")->subject('Payment Charged!'); }); } $message = "Successfully updated the order status"; $type = "success"; return Redirect::to('/admin/order/' . $id . '/details')->with('type', $type)->with('message', $message); }
/** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param integer $id the ID of the model to be loaded * @return UserCredit the loaded model * @throws CHttpException */ public function loadModel($id) { $model = UserCredit::model()->findByPk($id); if ($model === null) throw new CHttpException(404, 'The requested page does not exist.'); return $model; }