<?php

namespace App\Inventory;

use Illuminate\Database\Eloquent\Model;
use DB;

class InventoryCategory 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 getCategoryListByType($type)
    {

      $condition = $this->getCondition();


        return InventoryCategory::where($condition)->whereIn('category_type',$type)->orderBy('id','asc')->get();
    }


     public function getCategoryListByColumn($column,$condition =[])
   {

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



    return InventoryCategory::distinct($column)->where($condition)->get();

   }

   public function getbyID($id)
 {

  return InventoryCategory::where('id', $id)->first();

 }



    public function checkCategory($name)
    {
        $mycount =  InventoryCategory::where($this->getCondition())->where('name','ilike',$name)->count();

        if($mycount > 0)
        {
          return 1;
        }
        else{
          return 0;
        }
    }


    public function checkCode($code)
    {
        $mycount =  InventoryCategory::where($this->getCondition())->where('category_code',$code)->count();

        if($mycount > 0)
        {
          return 1;
        }
        else{
          return 0;
        }
    }

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

        $inventory_category = new InventoryCategory;

        $inventory_category->name              =  $object['name'];
        $inventory_category->category_type     =  $object['category_type'];
        $inventory_category->category_code     =  $object['category_code'];


        $inventory_category->save();

        return with($inventory_category);

       });
    }
}
