/** * 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; }
/** * 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; }