<?php

namespace App\Models\Orders;

use App\Models\Marketplace;
use App\Models\ClientDb;
use App\Models\Orders\Customer;
use App\Models\Orders\PayMethod;
use App\Models\Orders\SalesOrderDetail;
use App\Models\Orders\Status;
use App\Models\Transfer;
use Illuminate\Database\Eloquent\Model;

class SalesOrder extends Model
{
    protected $table = 'sale_orders';
    protected $fillable = [
        'order_id',
        'order_number',
        'customer_id',
        'payment_method_id',
        'total_price',
        'channel_id',
        'total_items',
        'pay_status',
        'marketplace_id',
        'approval_payment_id',
        'pay_updated_at',
        'giftMessage',
        'addressBilling',
        'addressShipping',
        'user_at',
        'transfer_id',
    ];

    public function customer()
    {
        return $this->belongsTo(ClientDb::class,'customer_id','No');
    }
    public function detail()
    {
        return $this->hasMany(SalesOrderDetail::class,'order_id');
    }
    public function payMethod()
    {
        return $this->belongsTo(PayMethod::class,'payment_method_id');
    }
    public function mkp()
    {
        return $this->belongsTo(Marketplace::class,'marketplace_id');
    }
    public function status()
    {
        return $this->belongsTo(Status::class,'pay_status');
    }
    public function transfer()
    {
        return $this->belongsTo(Transfer::class,'transfer_id');
    }
    // QUERY SCOPE
    public function scopeOrderNumber($query, $orderNumber){
        if($orderNumber){
            return $query->where('order_number', 'ilike', "$orderNumber%");
        }
    }

    public function scopePortal($query, $portal){
        if($portal){
            if(is_array($portal)){
                foreach ($portal as $key => $ptl) {
                    $query = $query->orWhere('order_number', 'ilike', "%$ptl");
                }
                return $query;
            }else{

                return $query->where('order_number', 'ilike', "%$portal");
            }
        }
    }

    public function scopefechaCreacionDesde($query, $fechaCreacionDesde){
        if($fechaCreacionDesde){
            return $query->where('created_at', '>=', "$fechaCreacionDesde");
        }
    }

    public function scopefechaCreacionHasta($query, $fechaCreacionHasta){
        if($fechaCreacionHasta){
            return $query->where('created_at', '<=', "$fechaCreacionHasta :23:59");
        }
    }

    public function scopeEstado($query, $estado){
        if($estado){
            return $query->with('detail.status')->whereHas('detail.status', function ($query) use($estado) {
                $query->where('name', '=', "$estado");});
        }
    }
    public function scopeTienda($query, $tienda){
        if($tienda){
            return $query->with('detail.separate')->whereHas('detail.separate', function ($query) use($tienda) {
                $query->where('shop', '=', "$tienda");});
        }
    }
    
    public function scopeGuia($query, $tienda){
        if($tienda){
            return $query->with('detail.shipping')->whereHas('detail.shipping', function ($query) use($tienda) {
                $query->where('trackingCode', '=', "$tienda");});
        }
    }

    public function scopeTransportadora($query, $tienda){
        if($tienda){
            return $query->with('detail.shipping.logistic')->whereHas('detail.shipping.logistic', function ($query) use($tienda) {
                $query->where('name', 'like', "%$tienda%");});
        }
    }

    public function scopeFactura($query, $tienda){
        if($tienda){
            return $query->with('detail.billing')->whereHas('detail.billing', function ($query) use($tienda) {
                $query->where('nmrfct', '=', "$tienda");});
        }
    }

    public function scopeIdentificacion($query, $identificacion){
        if($identificacion){
            return $query->where('customer_id', '=', "$identificacion");
        }
    }

    public function scopeUserAt($query, $asesor)    {
        if ($asesor) {
            return $query->where('user_at', 'ilike', "$asesor");
        }
    }

    public function scopeNombre($query, $nombre){
        if($nombre){
            return $query->with('customer')->whereHas('customer', function ($query) use($nombre) {
                $query->where('Name', 'ilike', "%$nombre%");});
        }
    }

    public function scopeCorreo($query, $email){
        if($email){
            return $query->with('customer')->whereHas('customer', function ($query) use($email) {
                $query->where('E_Mail', 'ilike', "%$email%");});
        }
    }

    public function scopeTelefono($query, $phone){
        if($phone){
            return $query->with('customer')->whereHas('customer', function ($query) use($phone) {
                $query->where('Phone_No', 'ilike', "%$phone%");});
        }
    }

    public function scopeRefOrBarras($query, $refOrBarras){
        if($refOrBarras){
            return $query->with('detail.item')->whereHas('detail.item', function ($query) use($refOrBarras) {
                $query->where('DefaultBarcode', '=', "$refOrBarras");});
        }
    }

    public function scopeTransferId($query, $transferId){
        if($transferId){
            return $query->where('transfer_id', '=', "$transferId");
        }
    }

    public function evidencesCr(){
        return $this->hasMany(EvidenciasClienteRecoge::class,'order_id');
    }

    public function scopeIdPago($query, $idPago){
        if($idPago){
            return $query->where('approval_payment_id', 'ilike', "%$idPago%");
        }
    }
}
