'required' ]; public $morphOne = [ 'transaction' => [Transaction::class, 'name' => 'transactable','delete'=>true] ]; public $belongsTo = [ 'user' => 'RainLab\User\Models\User' ]; public $attachOne = [ 'bank_file' => 'System\Models\File' ]; public function beforeUpdate() { if($this->status == 'approved' || $this->payment_type == 'gift' ) { if(!$transaction = $this->transaction) { $this->createTransaction($this->payment_type != 'gift'? $this->patment_type:'sowgat'); } else{ $transaction->amount = $this->amount; $this->payment_type == 'gift' ? $desc = 'sowgat' : $desc = ''; $transaction->description = "Balansyn doldurulmagy {$desc} {$this->amount} manat"; $transaction->save(); } } } public function afterUpdate() { if($this->status == 'approved' && $this->payment_type == 'bank' ){ // Log::info($this); Event::fire('tps.payment.reviewed',[$this,$this->user]); } } public function beforeValidate() { if(\App::runningInBackend()) { $this->rules['amount'] = 'required|gte:0'; } else { $this->rules['amount'] = 'required'; } } protected function beforeCreate() { parent::beforeCreate(); if(\App::runningInBackend()) { $this->payment_type = self::PAYMENT_TYPE_GIFT; $this->status = self::PAYMENT_STATUS_APPROVED; $this->created_at = Carbon::now(); $this->updated_at = Carbon::now(); } else{ if($this->payment_type == self::PAYMENT_TYPE_GIFT) { // when newly registered user gets gift $this->status = self::PAYMENT_STATUS_APPROVED; } else { $this->status = self::PAYMENT_STATUS_NEW; } } } protected function afterCreate() { parent::afterCreate(); if($this->payment_type == self::PAYMENT_TYPE_GIFT){ $this->createTransaction(); Event::fire('tps.payment.gifted',[$this,$this->user]); } } private function createTransaction($desc='sowgat'){ $transaction = new Transaction([ 'user_id' => $this->user_id, 'amount' => $this->amount, 'description' => "Balansyn doldurulmagy {$desc} {$this->amount} manat" ]); $this->transaction()->save($transaction); } public function filterFields($fields, $context = null){ if($this->payment_type == 'online'){ $fields->amount->disabled = true; $fields->user->disabled = true; $fields->created_at->disabled = true; $fields->status->disabled = true; } if ($this->payment_type == 'gift') { $fields->status->hidden = true; $fields->bank_file->hidden = true; } if($this->payment_type == 'bank' && $this->status == 'approved') { $fields->status->disabled = true; } } }