<?php

namespace App\Modules\Invoices;

class Sale extends AbstractInvoice
{
    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = ['total_paid', 'amount_due', 'net_price', 'method_of_payment'];

    /**
     *	Register events.
     */
    public static function boot()
    {
        parent::boot();

        // When a sale is created we want to
        // update the created_by field
        static::creating(function ($sale) {
            $sale->type = 'sale';
        });
    }

    /**
     * Get the method of payment for a sales receipt.
     *
     * @return string
     */
    public function getMethodOfPaymentAttribute()
    {
        $transaction = $this->transactions()->first();

        return $transaction ? $transaction->method_of_payment : 'cash';
    }
}
