<?php

namespace App\Inventory;

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

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

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

    }

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

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


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

        $vendor_type                           = new VendorType;
        $vendor_type->name                  =  $object['name'];
        $vendor_type->save();

        return with($vendor_type);

       });
    }
}
