<?php

namespace App\Inventory;

use Illuminate\Database\Eloquent\Model;
use App\Branch;

class Item 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 getItemListByType($type)
    {
        return Item::where($this->getCondition())->whereIn('type',$type)->orderBy('id','desc')->get();
    }

    public function getItemById($id)
    {
      $condition = $this->getCondition();

      $condition += ['id' => $id];

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



    public function getItemList($condition = [])
    {
      $condition += $this->getCondition();
      return Item::where($condition)->get();
    }

    public function getItemListByCategory($condition = [],$category)
    {

      $condition += $this->getCondition();

     return  Item::where($condition)->whereHas('inventoryCategory',function($query) use ($category){
            $query->where('id',$category) ->orderBy('id','asc');
          })->get();

    }


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


          $product                      = new Item;
          $product->name                = $object['product_name'];
          $product->code                = $object['product_code'];
          $product->description         = $object['product_description'];
          $product->inventory_unit_id   = $object['product_unit'];
          $product->branch_id           = 1;
          $product->minimum             = $object['product_minimum'];
          $product->maximum             = $object['product_maximum'];
          $product->type                = $object['type'];
          $product->inventory_category_id            = $object['product_category'];



          $product->save();


            return with($product);


        });
    }

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


         $curr_item = Item::find($object['id']);

         if(isset($curr_item->id))
         {

             $curr_item->quantity = $curr_item->quantity + $object['quantity'];


             $curr_item->update();

         }







            return with($curr_item);


        });
    }

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

          $curr_product                 = Item::find($object['product']);

        if(isset($curr_product->id))
        {
        $curr_product->name                = $object['product_name'];
        $curr_product->code                = $object['product_code'];
        $curr_product->description         = $object['product_description'];
        $curr_product->inventory_unit_id   = $object['product_unit'];
        $curr_product->branch_id           = 1;
        $curr_product->minimum             = $object['product_minimum'];
        $curr_product->maximum             = $object['product_maximum'];
        $curr_product->inventory_category_id            = $object['product_category'];


         $curr_product->update();


         }

            return with($curr_product);


        });
    }

		public function updateItemQty($id,$qty)
    {
        return \DB::transaction(function () use ($id,$qty)  {

          $curr_product                 = Item::find($id);

        if(isset($curr_product->id))
        {
        $curr_product->quantity                = $curr_product->quantity-$qty;

         $curr_product->update();


         }

            return with($curr_product);


        });
    }

    public function branch()
    {
        return $this->belongsTo(Branch::class,'branch_id')->where($this->getCondition())->withDefault();
    }

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


          $service                      = new Item;
          $service->name                = $object['service_name'];
          $service->code                = $object['service_code'];
          $service->description         = $object['service_description'];
          $service->inventory_unit_id   = $object['service_unit'];
          $service->branch_id           = 1;
          $service->minimum             = $object['service_minimum'];
          $service->maximum             = $object['service_maximum'];
					$service->type                = $object['type'];
          $service->inventory_category_id            = $object['service_category'];



          $service->save();


            return with($service);


        });
    }

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

          $curr_service                 = Item::find($object['service']);

        if(isset($curr_service->id))
        {
        $curr_service->name                = $object['service_name'];
        $curr_service->code                = $object['service_code'];
        $curr_service->description         = $object['service_description'];
        $curr_service->inventory_unit_id   = $object['service_unit'];
        $curr_service->branch_id           = 1;
        $curr_service->minimum             = $object['service_minimum'];
        $curr_service->maximum             = $object['service_maximum'];
        $curr_service->inventory_category_id           = $object['service_category'];

         $curr_service->update();


         }

            return with($curr_service);


        });
    }

    public function inventoryCategory()
    {
       return $this->belongsTo(InventoryCategory::class,'inventory_category_id')->where($this->getCondition())->withDefault();

    }

     public function inventoryUnit()
    {
       return $this->belongsTo(InventoryUnit::class,'inventory_unit_id')->where($this->getCondition())->withDefault();

    }
}
