Example #1
0
 /**
  * Add quota for upline.
  *
  * @param model transaction
  * @return boolean
  */
 public function AddQuotaForUpline($transaction)
 {
     $upline = PointLog::userid($transaction->user_id)->referencetype('App\\Models\\User')->first();
     $quota = StoreSetting::type('downline_purchase_quota_bonus')->Ondate('now')->first();
     $whoisupline = 0;
     if ($upline && $upline->reference()->count()) {
         $whoisupline = $upline->reference->referral->value;
     }
     if ($upline && $quota && $whoisupline == 0 && $upline->reference()->count() && $upline->reference->referral()->count()) {
         $quotalog = new QuotaLog();
         $quotalog->fill(['voucher_id' => $upline->reference->referral->id, 'amount' => $quota->value, 'notes' => 'Bonus belanja ' . $transaction->user->name . ' nomor nota #' . $transaction->ref_number]);
         if (!$quotalog->save()) {
             $this->errors = $quotalog->getError();
             return false;
         }
     }
     return true;
 }
 /**
  * Do count transaction voucher discount
  *
  * @param model transaction
  * @return voucher discount
  */
 public function CountVoucherDiscount($transaction)
 {
     $voucher_discount = 0;
     $voucherrules = ['started_at' => 'before:' . $transaction->transact_at->format('Y-m-d H:i:s'), 'expired_at' => 'after:' . $transaction->transact_at->format('Y-m-d H:i:s')];
     $quota = \App\Models\QuotaLog::voucherid($transaction->voucher_id)->ondate([$transaction->transact_at->format('Y-m-d H:i:s'), null])->sum('amount');
     if ($quota <= 0) {
         $this->errors = 'Voucher tidak dapat digunakan.';
         return false;
     }
     if ($transaction->voucher()->count() && $transaction->status == 'paid') {
         $validator = Validator::make($transaction->voucher['attributes'], $voucherrules);
         if (!$validator->passes()) {
             $this->errors = 'Voucher Tidak dapat digunakan.';
             return false;
         }
         switch ($transaction->voucher['attributes']['type']) {
             case 'debit_point':
                 $result = $transaction->DebitPoint($transaction, $transaction->voucher->value);
                 if (!$result) {
                     $this->errors = 'Cannot debit point';
                     return false;
                 }
                 break;
             default:
                 break;
         }
     } elseif ($transaction->voucher()->count()) {
         $validator = Validator::make($transaction->voucher['attributes'], $voucherrules);
         if (!$validator->passes()) {
             $this->errors = 'Voucher Tidak dapat digunakan.';
             return false;
         }
         switch ($transaction->voucher['attributes']['type']) {
             case 'free_shipping_cost':
                 $voucher_discount = !is_null($transaction->shipping_cost) ? $transaction->shipping_cost : 0;
                 break;
             default:
                 break;
         }
     }
     return $voucher_discount;
 }
Example #3
0
 /**
  * save referral code
  * 
  * @param model of user, referral_code
  * @return boolean
  */
 public function giveReferralCode($user, $referral)
 {
     //save voucher referral
     $newvoucher = new Referral();
     $newvoucher->fill(['user_id' => $user->id, 'code' => $referral, 'type' => 'referral', 'value' => 0, 'started_at' => null, 'expired_at' => null]);
     if (!$newvoucher->save()) {
         $this->errors = $newvoucher->getError();
         return false;
     } else {
         //save quota referral
         $quota = StoreSetting::type('first_quota')->Ondate('now')->first();
         if (!$quota) {
             $this->errors = 'Tidak dapat melakukan registrasi saat ini.';
             return false;
         } else {
             $newquota = new QuotaLog();
             $newquota->fill(['voucher_id' => $newvoucher['id'], 'amount' => $quota->value, 'notes' => 'Hadiah registrasi']);
             if (!$newquota->save()) {
                 $this->errors = $newquota->getError();
                 return false;
             }
         }
     }
     return true;
 }
Example #4
0
 /**
  * Store a Voucher
  *
  * 1. Save Vouchers
  * 2. Save Quota Logs
  *
  * @return Response
  */
 public function store()
 {
     if (!Input::has('voucher')) {
         return new JSend('error', (array) Input::all(), 'Tidak ada data voucher.');
     }
     $errors = new MessageBag();
     DB::beginTransaction();
     //1. Validate Voucher Parameter
     $voucher = Input::get('voucher');
     if (is_null($voucher['id'])) {
         $is_new = true;
     } else {
         $is_new = false;
     }
     $voucher_rules = ['user_id' => 'exists:users,id', 'code' => 'required|max:255|unique:tmp_vouchers,code,' . (!is_null($voucher['id']) ? $voucher['id'] : ''), 'type' => 'required|in:debit_point,free_shipping_cost,promo_referral', 'value' => 'required|numeric', 'started_at' => 'date_format:"Y-m-d H:i:s"', 'expired_at' => 'date_format:"Y-m-d H:i:s"'];
     //1a. Get original data
     $voucher_data = \App\Models\Voucher::findornew($voucher['id']);
     //1b. Validate Basic Voucher Parameter
     $validator = Validator::make($voucher, $voucher_rules);
     if (!$validator->passes()) {
         $errors->add('Voucher', $validator->errors());
     } else {
         //if validator passed, save voucher
         $voucher_data = $voucher_data->fill($voucher);
         if (!$voucher_data->save()) {
             $errors->add('Voucher', $voucher_data->getError());
         }
     }
     //2. Validate Voucher Logs Parameter
     //2a. save using quota
     if (!$errors->count() && isset($voucher['quota']) && $voucher['quota'] != $voucher_data['quota']) {
         $log_data = new \App\Models\QuotaLog();
         $value['voucher_id'] = $voucher_data['id'];
         $value['amount'] = $voucher['quota'] - $voucher_data['quota'];
         $log_data = $log_data->fill($value);
         if (!$log_data->save()) {
             $errors->add('Log', $log_data->getError());
         }
     }
     if (!$errors->count() && isset($voucher['quotalogs']) && is_array($voucher['quotalogs'])) {
         $log_current_ids = [];
         foreach ($voucher['quotalogs'] as $key => $value) {
             if (!$errors->count()) {
                 $log_data = \App\Models\QuotaLog::findornew($value['id']);
                 $log_rules = ['voucher_id' => 'exists:tmp_vouchers,id|' . ($is_new ? '' : 'in:' . $voucher_data['id']), 'amount' => 'required|numeric', 'notes' => 'max:512'];
                 $validator = Validator::make($value, $log_rules);
                 //if there was log and validator false
                 if (!$validator->passes()) {
                     $errors->add('Log', $validator->errors());
                 } else {
                     $value['voucher_id'] = $voucher_data['id'];
                     $log_data = $log_data->fill($value);
                     if (!$log_data->save()) {
                         $errors->add('Log', $log_data->getError());
                     } else {
                         $log_current_ids[] = $log_data['id'];
                     }
                 }
             }
         }
         //if there was no error, check if there were things need to be delete
         if (!$errors->count()) {
             $logs = \App\Models\QuotaLog::voucherid($voucher['id'])->get(['id'])->toArray();
             $log_should_be_ids = [];
             foreach ($logs as $key => $value) {
                 $log_should_be_ids[] = $value['id'];
             }
             $difference_log_ids = array_diff($log_should_be_ids, $log_current_ids);
             if ($difference_log_ids) {
                 foreach ($difference_log_ids as $key => $value) {
                     $log_data = \App\Models\QuotaLog::find($value);
                     if (!$log_data->delete()) {
                         $errors->add('Log', $log_data->getError());
                     }
                 }
             }
         }
     }
     if ($errors->count()) {
         DB::rollback();
         return new JSend('error', (array) Input::all(), $errors);
     }
     DB::commit();
     $final_voucher = \App\Models\Voucher::id($voucher_data['id'])->with(['quotalogs', 'transactions'])->first()->toArray();
     return new JSend('success', (array) $final_voucher);
 }
Example #5
0
 /**
  * boot
  *
  */
 public static function boot()
 {
     parent::boot();
     QuotaLog::observe(new QuotaLogObserver());
 }