<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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


    public function getTaxSlab($salary)
    {

     
      $condition = $this->getCondition();

      
      $tax = Tax::where($condition)->where('minimum', '<', $salary)->where('maximum','>=',$salary)->first();


      if(!isset($tax->id))
      {
          $tax = Tax::where($condition)->where('minimum', '<', $salary)->where('maximum',0)->first();
      }

  
     
      return $tax;

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

            

          $tax                = new Tax;

          $tax->title         = $object['title'];

          $tax->minimum       = $object['minimum'];
          
          $tax->maximum       = $object['maximum'];
          
          $tax->percentage    = $object['percentage'];
          
          $tax->fix_amount    = $object['fix_amount'];


          $tax->save();


            return with($tax);
          

        });
    }

    public function updateTax($object,$tax_id)
    {

       return \DB::transaction(function () use ($object, $tax_id) {

            $tax = Tax::find($tax_id);

            if(isset($tax->id))
            {
       
           
             $tax->title           = $object['title'];

             $tax->minimum         = $object['minimum'];

             $tax->maximum         = $object['maximum'];

             $tax->percentage      = $object['percentage'];

             $tax->fix_amount      = $object['fix_amount'];

             $tax->update();

                return with($tax);
                
                return 1;
            }
            
            return 0;
        });
    }
}
