<?php

namespace App\Inventory;

use Illuminate\Database\Eloquent\Model;
use App\Branch;
use App\User;
use App\Inventory\BOQItems;
use App\Inventory\PurchaseOrder;
use App\Inventory\InventoryCategory;

class BillOfQuantity 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 getAllBOQ()
    {

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

    public function getAllBOQ2()
    {

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

    public function getBOQById($id)
    {

      $condition = $this->getCondition();
      $condition += ['id' => $id];

        return BillOfQuantity::where($condition)->first();
    }

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

            $curr_boq = BillOfQuantity::find($id);

            if(isset($curr_boq->id))
            {
                $curr_boq->is_recycle   = '1';
                $curr_boq->status       = 'Rejected';

                $curr_boq->update();

                return 1;
            }

            return 0;
        });
    }

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

          $curr_boq = BillOfQuantity::find($id);

          if(isset($curr_boq->id))
          {
              $curr_boq->status       = 'Approved';
              $curr_boq->update();
              return 1;
          }
          return 0;
      });
  }


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

    }

    public function generateBOQNumberpr()
    {
        $insert_id = BillOfQuantity::min('id');
        $serial = 'RS/DR/BOQ/'.str_pad(($insert_id+1), 5, '0', STR_PAD_LEFT);
        return $serial;

    }



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


          $boq                          = new 	BillOfQuantity;
          if($object['pr_check'] == 0)
          {
          $boq->sr_no                   = $this->generateBOQNumber();
          }
          if($object['pr_check'] == 1)
          {
          $boq->sr_no                   = $this->generateBOQNumberpr();
          }
          $boq->branch_id               = $object['project'];
          $boq->remarks                 = $object['remarks'];
          $boq->user_id                 = $object['user_id'];
          $boq->pr_check                 = $object['pr_check'];
          if($object['floor_list'] == 'Other')
          {
            $boq->floor   = $object['new_floor'];
          }

          else
          {
            $boq->floor  = $object['floor_list'];
          }


          if(isset($object['status']))
          {
            $boq->status = "Waiting";
          }

          $boq->save();


            return with($boq);


        });
    }


    public function branch()
    {
        return $this->belongsTo(Branch::class,'branch_id')->withDefault();
    }
    public function preparedby()
    {
        return $this->belongsTo(User::class,'user_id')->withDefault();
    }
    public function approvedby()
    {
        return $this->belongsTo(User::class,'au_id')->withDefault();
    }
    public function getBoqItems()
    {
        return $this->hasMany(BOQItems::class,'bill_of_quantity_id');
    }
    public function getPOnumber($id)
    {
      $mypo = PurchaseOrder::where('bill_of_quantity_id',$id)->get();
        return $mypo;
    }


}
