<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use \App\Models\{OP, Oti, OtiPlan};
use DB;

class Presupuesto extends Model
{
    use HasFactory;

    protected $table = 'presupuestos';
    protected $fillable = ['document_number', 'cliente_id', 'vendedor_id', 'date_at', 'delivery_term', 'terms_of_sale', 'offer_days', 'status', 'discount', 'observations', 'currency'];

    protected static function boot()
    {
        parent::boot();
        static::creating(function ($item) {
            if (! $item->document_number) {
                $max = (new Presupuesto)->orderBy('id', 'desc')->first();
                $number = $max ? $max->id + 1 : 1;
                $item->document_number = str_pad($number, 10,'0',STR_PAD_LEFT);
            }
        });
        static::updating(function ($item) {
            if (! $item->document_number) {
                $max = (new Presupuesto)->orderBy('id', 'desc')->first();
                $number = $max ? $max->id + 1 : 1;
                $item->document_number = str_pad($number, 10,'0',STR_PAD_LEFT);
            }
        });
    }

    public function detalle()
    {
        return $this->hasMany(PresupuestoDetalle::class, 'presupuesto_id')->with('producto')->with('planos');
    }

    public function cliente()
    {
        return $this->belongsTo(Cliente::class, 'cliente_id');
    }

    public function approveBudget($id)
    {
        $budget = $this->find($id);
        if (! $budget) {
            return;
        }

        DB::transaction (function() use ($budget) {
            $cliente_id = $budget->cliente_id;
            $date_at = $budget->date_at;
            foreach ($budget->detalle as $item) {
                $op = OP::create([
                    'client' => $cliente_id,
                    'datein' => $date_at,
                    'piece' => $item->producto->name,
                    'quantity' => $item->quantity,
                    'cdolarpeso' => $item->price,
                    'obs' => $item->details,
                    'product_id' => $item->producto->id,
                    'status' => 0,
                ]);
                
                foreach ($item->producto->sectores_primera_etapa as $sector) {
                    $oti = Oti::create([
                        'piece' => $item->producto->name,
                        'production_id' => $op->id,
                        'quantity' => $item->quantity,
                        'status' => 0,
                        'client_id' => $cliente_id,
                        'sector_id' => $sector->sector_id,
                        'product_id' => $item->producto->id,
                        'details' => $item->details,
                        'alto' => $item->alto,
                        'ancho' => $item->ancho,
                        'espesor' => $item->espesor,
                        'plisas' => $item->plisas,
                        'calidad_id' => $item->calidad_id
                    ]);

                    $oti_flujo = OtiFlujo::create([
                        'status' => 0,
                        'producto_sector_id' => $sector->id,
                        'oti_id' => $oti->id                        
                    ]);

                    foreach ($item->planos as $plano) {
                        $oti_plan = OtiPlan::create([
                            'oti_id' => $oti->id,
                            'plano_id' => $plano->plano_id,
                        ]);
                    }
                }
            }
            $budget->status = 'APROBADO';
            $budget->save();

            return $budget;
        });
    }
    
}
