%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/html/hr/api/app/Models/
Upload File :
Create Path :
Current File : /var/www/html/hr/api/app/Models/MaintenancePayment.php

<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Throwable;

/**
 * @property integer id
 * @property string name
 * @property string description
 * @property string type
 * @property float amount
 * @property string currency
 * @property integer cycle
 * @property mixed start_date
 * @property mixed next_payment_date
 * @property mixed canceled_at
 * @property boolean is_active
 * @property integer client_id
 * @property Client client
 * @property mixed transactions
 */
class MaintenancePayment extends BaseModel
{
    const EARNING = 'EARNING';
    const EXPENSE = 'EXPENSE';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'description',
        'type',
        'amount',
        'currency',
        'cycle',
        'start_date',
        'next_payment_date',
        'canceled_at',
        'is_active',
    ];

    protected $guarded = [
        'client_id',
    ];

    protected $dates = [
        'start_date',
        'next_payment_date',
        'canceled_at',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'id'                => 'integer',
        'name'              => 'string',
        'description'       => 'string',
        'type'              => 'string',
        'amount'            => 'float',
        'currency'          => 'string',
        'cycle'             => 'integer',
        'start_date'        => 'date',
        'next_payment_date' => 'date',
        'is_active'         => 'boolean',
        'client_id'         => 'integer',
    ];

    /**
     * @param $data
     * @return self|null
     * @throws Throwable
     */
    public static function createItem($data)
    {
        try {
            DB::beginTransaction();
            /** @var self $item */
            $item = self::query()
                        ->make([
                            'name'              => $data['name'],
                            'type'              => array_key_exists('type', $data)
                                ? $data['type']
                                : self::EARNING,
                            'description'       => array_key_exists('description', $data)
                                ? $data['description']
                                : null,
                            'amount'            => $data['amount'],
                            'currency'          => array_key_exists('currency', $data)
                                ? $data['currency']
                                : Project::EURO,
                            'cycle'             => $data['cycle'],
                            'start_date'        => $data['start_date'],
                            'next_payment_date' => $data['start_date'],
                            'is_active'         => true,
                        ]);
            $item->client()
                 ->associate($data['client_id']);
            $item->save();
            DB::commit();
            return $item->fresh();
        } catch (Throwable $e) {
            DB::rollBack();
            throw $e;
        }
    }

    public function client()
    {
        return $this->belongsTo(Client::class);
    }

    /**
     * @param $data
     * @return self|null
     * @throws Throwable
     */
    public function updateItem($data)
    {
        try {
            $this->name = $data['name'];
            $this->description = array_key_exists('description', $data)
                ? $data['description']
                : null;
            $this->amount = $data['amount'];
            $this->currency = array_key_exists('currency', $data)
                ? $data['currency']
                : Project::EURO;
            $this->cycle = $data['cycle'];
            $this->save();
            return $this->fresh();
        } catch (Throwable $e) {
            throw $e;
        }
    }

    /**
     * @return $this
     */
    public function cancel()
    {
        $this->is_active = false;
        $this->canceled_at = Carbon::now();
        $this->save();
        return $this;
    }

    /**
     * @throws Throwable
     */
    public function paymentReceived()
    {
        DB::beginTransaction();
        try {
            $this->transactions()
                 ->create([
                     'amount'   => $this->amount,
                     'currency' => $this->currency,
                     'paid_at'  => Carbon::now(),
                 ]);
            $this->next_payment_date = Carbon::parse($this->next_payment_date)
                                             ->addMonths($this->cycle);
            $this->save();
            DB::commit();
            return $this;
        } catch (Throwable $e) {
            DB::rollBack();
            throw $e;
        }
    }

    public function transactions()
    {
        return $this->hasMany(MaintenancePaymentTransaction::class, 'payment_id');
    }

    public function isCancelable()
    {
        return $this->is_active;
    }

    public function canReceivePayments()
    {
        return $this->is_active;
    }

    public function isDue()
    {
        return Carbon::parse($this->next_payment_date)
                     ->isBefore(Carbon::now());
    }

    public function isDueSoon()
    {
        return Carbon::parse($this->next_payment_date)
                     ->isBefore(Carbon::now()
                                      ->addDays(3));
    }

}

Zerion Mini Shell 1.0