<?php

namespace App\Inventory;
use App\Inventory\BillOfQuantity;
use App\Inventory\BOQItems;
use App\User;
use App\Inventory\PurchaseOrder;

use Illuminate\Database\Eloquent\Model;

class ReceivingNote 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 storeGRN($object)
      {

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


            $grn                         = new 	ReceivingNote;
            $grn->purchase_order_id      = $object['po_id'];
            $grn->user_id                = $object['user_id'];
            $grn->p_id                   = '0';
            $grn->total_quantity         = $object['total_quantity'];
            $grn->accepted_quantity      = $object['accepted_quantity'];
            $grn->rejected_quantity      = $object['rejected_quantity'];
            $grn->grn_type               = $object['grn_type'];
            $grn->grn_status             = '0';
            $grn->receive_date           = $object['date'];
            $grn->au_id           = $object['au_id'];
            $grn->status           = "Pending";

            $grn->grn_number             = $this->generateGRNNumber();
            $grn->save();



            return with($grn);
          });
      }



      public function getGrnByPO($id)
      {
        $condition = $this->getCondition();
        $condition += ['purchase_order_id' => $id];
          $curr_po = ReceivingNote::where($condition)->get();

          return $curr_po;
     }

       public function getGrnById($id)
      {
        $condition = $this->getCondition();
        $condition += ['id' => $id];
        return ReceivingNote::where($condition)->first();


     }



    public function getAllGRN()
    {

        return ReceivingNote::where($this->getCondition())->orderBy('id','asc')->get();
    }

     public function getGrnAcceptedQuantity($id)
     {
        $condition = $this->getCondition();
        $condition += ['purchase_order_id' => $id];
          return ReceivingNote::where($condition)->sum('accepted_quantity');
     }

    public function getGrnListByStatus($status)
    {
      $condition = $this->getCondition();
    return ReceivingNote::where($condition)->whereIn('grn_status',$status)->get();
    }
    public function generateGRNNumber()
    {
        $insert_id = ReceivingNote::max('id');
        $insert_id = $insert_id;
        $serial = 'RS/GRN/'.str_pad(($insert_id+1), 4, '0', STR_PAD_LEFT);
        return $serial;

    }


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

            $curr_grn = ReceivingNote::find($id);

            if(isset($curr_grn->id))
            {
               $curr_grn->grn_status = '1';

                $curr_grn->update();

                return 1;
            }

            return 0;
        });
    }


      public function purchaseOrder()
    {
        return $this->belongsTo(PurchaseOrder::class,'purchase_order_id')->withDefault();
    }
       public function receiver()
    {
        return $this->belongsTo(User::class,'user_id')->withDefault();
    }
    public function puser()
 {
     return $this->belongsTo(User::class,'au_id')->withDefault();
 }
}
