/** * observe point log event saved * 1. Check if reference were from user * 2. act, accept or refuse * * @param $model * @return bool */ public function saved($model) { $errors = new MessageBag(); //1. Check if reference were from user if ($model->reference_type == 'App\\Models\\User') { $gift = StoreSetting::type('referral_royalty')->Ondate('now')->first(); //check if no royalti, refuse if (!$gift) { $errors->add('PointLog', 'Tidak ada campaign untuk point reference.'); } else { //give royalti to referral $voucher = Referral::userid($model->reference_id)->first(); if ($voucher && $voucher['value'] == 0) { $referee = new PointLog(); $referee->fill(['user_id' => $model->reference_id, 'amount' => $gift->value, 'expired_at' => $model->expired_at, 'notes' => 'Mereferensikan ' . $model->user->name]); $referee->reference()->associate($model); if (!$referee->save()) { $errors->add('PointLog', $referee->getError()); } } } } if ($errors->count()) { $model['errors'] = $errors; return false; } return true; }
/** * Credit point. * * @param model transaction * @return boolean */ public function CreditPoint($transaction) { //cek all in debit active point $points = PointLog::userid($transaction->user_id)->onactive('now')->debit(true)->get(); //count leftover active point $sumpoints = PointLog::userid($transaction->user_id)->onactive('now')->sum('amount'); $idx = 0; $currentamount = 0; $transactionamount = $transaction['bills']; while ($transactionamount <= $transaction['bills'] && $points && isset($points[$idx]) && $transactionamount > 0 && $sumpoints > 0) { $sumpoints = PointLog::userid($transaction->user_id)->onactive('now')->sum('amount'); //count left over point per debit to credit $currentamount = $points[$idx]['amount']; foreach ($points[$idx]->pointlogs as $key => $value) { $currentamount = $currentamount + $value['amount']; } //if leftover more than 0 if ($currentamount > 0 && $currentamount >= $transactionamount) { $camount = 0 - $transactionamount; } else { $camount = 0 - $currentamount; } if ($currentamount > 0) { $point = new PointLog(); $point->fill(['user_id' => $points[$idx]->user_id, 'reference_id' => $transaction->id, 'reference_type' => get_class($transaction), 'point_log_id' => $points[$idx]->id, 'amount' => $camount, 'expired_at' => $points[$idx]->expired_at, 'notes' => 'Pembayaran Belanja #' . $transaction->ref_number]); if (!$point->save()) { $this->errors = $point->getError(); return false; } $transactionamount = $transactionamount + $camount; } $idx++; } return $transactionamount; }
/** * save welcome gift * * @param model of user * @return boolean */ public function giveWelcomeGift($user) { $gift = StoreSetting::type('welcome_gift')->Ondate('now')->first(); $store = StoreSetting::type('voucher_point_expired')->Ondate('now')->first(); if ($gift) { if ($store) { $expired_at = new Carbon($store->value); } else { $expired_at = new Carbon('+ 3 months'); } $point = new PointLog(); $point->fill(['user_id' => $user->id, 'amount' => $gift->value, 'expired_at' => $expired_at->format('Y-m-d H:i:s'), 'notes' => 'Welcome Gift dari BALIN']); if (!$point->save()) { $this->errors = $point->getError(); return false; } } return true; }
/** * Debit Point from voucher * * @param model transaction * @return voucher discount */ public function DebitPoint($transaction, $debit) { if (!is_null($transaction->id)) { $expired = StoreSetting::type('voucher_point_expired')->Ondate('now')->first(); $previous = PointLog::referenceid($transaction->id)->referencetype('App\\Models\\Transaction')->first(); if ($expired && !$previous) { $point = new PointLog(); $point->fill(['user_id' => $transaction->user_id, 'amount' => $debit, 'expired_at' => date('Y-m-d H:i:s', strtotime($transaction->transact_at . ' ' . $expired->value)), 'notes' => 'Bonus Belanja dengan Voucher ']); $point->reference()->associate($transaction); if (!$point->save()) { $this->errors = $point->getError(); return false; } } } return $result; }