<?php

namespace App;

use App\Repositories\CompanyRepository;
use App\Repositories\PreferenceRepository;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class Payment extends Model
{
    protected $table = 'payments';

    protected $primaryKey = 'id';

    protected $fillable = [
        'payment_type_id','charged', "amount"
    ];

    protected $casts = [
        "charged" => "boolean"
    ];

    protected $with = ["paymentType"];

    /**
     * EVENTS
     */
    public static function boot() {
        parent::boot();

        static::updating(function(Payment $payment) { //QUAN ES PAGUI, ES CREA UNA FACTURA
            $previous_p = Payment::findOrFail($payment->id);
            if(!$previous_p->charged && $payment->charged && $payment->amount > 0) { //si s'ha pagat
                $order = null;
                if($previous_p->box_order) {
                    //Crear factura a partir de box_order
                    $order = $previous_p->box_order;
                    $invoice = Invoice::create([
                        "company_id" => $order->box->company->id,
                        "type" => "income", //no tocar-ho en la vida
                        "from_fiscal_name" => $order->box->company->fact_name,
                        "from_nif_cif" => $order->box->company->cif,
                        "from_fiscal_address" => $order->box->company->fiscal_address,
                        "to_fiscal_name" => $order->user->name . " " . $order->user->surnames,
                        "to_nif_cif" => $order->user->nif,
                        "to_fiscal_address" => $order->user->fiscal_address,
                        "invoice_prefix" => $order->box->company->fact_prefix,
                        "invoice_serie" => $order->box->company->fact_serie,
                        //invoice number va sol
                        "emitted_date" => Carbon::now(),
                        "due_date" => Carbon::now()->addDays(PreferenceRepository::find("fact_due_date_days_after")->value),
                        "concept" => "Box Order #$order->id",
                        "subtotal" => $order->subtotal,
                        "tax_prcnt" => $order->subtotal ? round(($order->vat / $order->subtotal)*100, 2) : 0,
                        "tax_amount" => $order->vat,
                        "total" => $order->total,
                    ]);
                }
                else if($previous_p->company_order) {
                    $order = $previous_p->company_order;
                    $company = CompanyRepository::find($order->subscription->company_id);
                    $invoice = Invoice::create([
                        "company_id" => $company->id,
                        "type" => "expense",
                        "from_fiscal_name" => PreferenceRepository::find("fact_name_fiscal")->value,
                        "from_nif_cif" => PreferenceRepository::find("fact_nif_cif")->value,
                        "from_fiscal_address" => PreferenceRepository::getFiscalCompleteAddressHAW(),
                        "to_fiscal_name" => $company->fact_name,
                        "to_nif_cif" => $company->cif,
                        "to_fiscal_address" => $company->fiscal_address,
                        "invoice_prefix" => PreferenceRepository::find("fact_invoice_prefix")->value,
                        "invoice_serie" => PreferenceRepository::find("fact_invoice_serie")->value,
                        //invoice number va sol
                        "emitted_date" => Carbon::now(),
                        "due_date" => Carbon::now()->addDays(PreferenceRepository::find("fact_due_date_days_after")->value),
                        "concept" => "Company Order #$order->id",
                        "subtotal" => $order->subtotal,
                        "tax_prcnt" => round(($order->vat / $order->subtotal)*100, 2),
                        "tax_amount" => $order->vat,
                        "total" => $order->total,
                    ]);
                }

                if($order != null) {
                    //Guardem
                    $order->invoice_id = $invoice->id;
                    $order->save();
                }
            }

        });
    }


    /**
     * RELATIONSHIPS
     */
    public function paymentType(){
        return $this->belongsTo("App\PaymentType");
    }

    public function box_order() {
        return $this->hasOne("App\BoxOrder");
    }

    public function company_order() {
        return $this->hasOne("App\CompanyOrder");
    }

}
