<?php

namespace App\Inventory;

use Illuminate\Database\Eloquent\Model;
use App\Inventory\BillOfQuantity;
use App\Inventory\BOQItems;
use App\User;
use Auth;

class PurchaseOrder extends Model
{
    //
    private $generic_condition;

   public function __construct()
    {

       $this->setCondition(['is_recycle' => '0']);
    }

    public function setCondition($condition = [])
    {
        $this->generic_condition = $condition;
    }

    public function getCondition()
    {
        return $this->generic_condition;
    }

    public function massInsertPO($object)
    {
        return \DB::transaction(function () use ($object)  {

            PurchaseOrder::insert($object);
        });
    }

       public function getPurchaseListByColumn($column)
   {
       return PurchaseOrder::where($this->getCondition())->distinct($column)->orderBy($column,'asc')->get([$column]);
   }

          //GENERATE MS NUMBER
    public function generatePONumber($i)
    {
        $insert_id = PurchaseOrder::max('id');
        $insert_id = $insert_id+$i;
        $serial = 'SL/PO/'.str_pad(($insert_id+1), 4, '0', STR_PAD_LEFT);
        return $serial;

    }

    public function InsertPO($object)
      {
          return \DB::transaction(function () use ($object)  {


            $po                          = new 	PurchaseOrder;

            $po->bill_of_quantity_id               = $object['bill_of_quantity_id'];
            $po->b_o_q_item_id                 = $object['b_o_q_item_id'];
            $po->user_id               = $object['user_id'];
            $po->unit_price                 = $object['unit_price'];
            $po->remarks               = $object['remarks'];
            $po->p_number                 = $object['p_number'];
            $po->quotation               = $object['quotation'];
            $po->status                 = $object['status'];
            $po->brand                  = $object['brand'];
            //$po->au_id                  = $object['au_id'];
            $po->branch_id                  = $object['branch_id'];




            $po->save();


              return with($po);


          });
      }


    public function getAllPurchaseOrders()
    {
        $po = PurchaseOrder::
        where($this->getCondition())
        ->groupBy('p_number','bill_of_quantity_id','status')
        ->selectRaw('p_number,bill_of_quantity_id,status')
        ->get();


        return $po;
    }
    public function getAllPurchaseOrdersProject($id)
    {
        $po = PurchaseOrder::
        where($this->getCondition())->where('branch_id',$id)
        ->groupBy('p_number','bill_of_quantity_id','status','user_id')
        ->selectRaw('p_number,bill_of_quantity_id,status,user_id')
        ->get();


        return $po;
    }

    public function getAllPurchaseOrdersVendor($id)
    {
        $po = PurchaseOrder::
        where($this->getCondition())->where('user_id',$id)
        ->selectRaw('p_number,bill_of_quantity_id,status')
        ->groupBy('p_number','bill_of_quantity_id','status')
        ->get();


        return $po;
    }

    public function rejectPO($id)
    {
        return \DB::transaction(function () use ($id) {

            $curr_pos = PurchaseOrder::where('p_number',$id)->get();

            foreach($curr_pos as $curr_po)
            {
            if(isset($curr_po->id))
            {
                $curr_po->is_recycle   = '1';
                $curr_po->status       = 'Rejected';

                $curr_po->update();
            }
            }
                //return 1;


            return 1;
        });
    }

        public function acceptPO($id,$date)
    {
        return \DB::transaction(function () use ($id,$date) {

            $curr_pos = PurchaseOrder::where('p_number',$id)->get();
            foreach($curr_pos as $curr_po)
            {
            if(isset($curr_po->id))
            {

                $curr_po->status       = 'Approved';
                $curr_po->delivery_date       = $date;
                $curr_po->action_by       = \Auth::User()->id;

                $curr_po->update();
            }
            }
                return with($curr_pos);


            return 0;
        });
    }

    public function getPObyId($id)
    {
        $curr_po = PurchaseOrder::where('id',$id)->first();
        return $curr_po;
      }

      public function getPObyNoAll($id)
      {
          $curr_po = PurchaseOrder::where('p_number',$id)->get();

          return $curr_po;
        }

      public function getPODuplicate($user_id,$bo_id)
      {
          $curr_po = PurchaseOrder::where('user_id',$user_id)->where('bill_of_quantity_id', $bo_id)->first();
          return $curr_po;
        }

      public function getPObyNo($po_number)
      {
          $curr_po = PurchaseOrder::where('p_number',$po_number)->where('status', "Approved")->first();
          return $curr_po;
        }


    public function boq()
    {
        return $this->belongsTo(BillOfQuantity::class,'bill_of_quantity_id')->withDefault();
    }

     public function boqitem()
    {
        return $this->belongsTo(BOQItems::class,'b_o_q_item_id')->withDefault();
    }

     public function user()
    {
        return $this->belongsTo(User::class,'user_id')->withDefault();
    }

    public function approvedby()
   {
       return $this->belongsTo(User::class,'action_by')->withDefault();
   }

    public function getGRNPO()
    {
        return $this->hasMany(ReceivingNote::class,'purchase_order_id');
    }


}
