<?php

namespace App\Inventory;

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

class InventoryUnit 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 getAllInventoryUnit()
    {
        return InventoryUnit::where($this->getCondition())->orderBy('id','asc')->get();
    }

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

    }


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

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


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

        $inventory_unit                        = new InventoryUnit;
        $inventory_unit->name                  =  $object['name'];
        $inventory_unit->save();

        return with($inventory_unit);

       });
    }
}
