<?php


namespace Gisplus\Package\EstatechVendor\Models;

use Illuminate\Database\Eloquent\Model;

use DB;

class PurchaseItem 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 getItemCodeList()
    {
        return PurchaseItem::where($this->getCondition())->pluck('item_code')->toArray();
    }

    public function storePurchaseItem($object)
    {
        return \DB::transaction(function () use ($object)  
        {
                      
            for ($i = 0; $i < @count($object['amount']) ; $i++)
            {
                if($object['amount'][$i] > 0)
                {

                    $purchase_item = new PurchaseItem;

                    $purchase_item->invoice_id = $object['invoice'];

                    $purchase_item->item_code = $object['item_code'][$i];

                    $purchase_item->description = $object['description'][$i];
                    
                    $purchase_item->unit_price = $object['unit_price'][$i];
                    
                    $purchase_item->discount = $object['discount'][$i];
                    
                    $purchase_item->quantity = $object['quantity'][$i];
                    
                    $purchase_item->gst_value = $object['gst_value'][$i];
                    
                    $purchase_item->amount = $object['amount'][$i];
                    
                    $purchase_item->save();
                }
            }
               return 1;

        });
    }

    public function updatePurchaseItem($object)
    {
        return \DB::transaction(function () use ($object)  
        {
            for ($i = 0; $i < @count($object['amount']) ; $i++)
            {
                if($object['amount'][$i] > 0)
                {

                    $purchase_item = PurchaseItem::find($object['purchase_item'][$i]);

                    
                    $purchase_item->invoice_id = $object['invoice'];

                    $purchase_item->item_code = $object['item_code'][$i];

                    $purchase_item->description = $object['description'][$i];
                    
                    $purchase_item->unit_price = $object['unit_price'][$i];
                    
                    $purchase_item->discount = $object['discount'][$i];
                    
                    $purchase_item->quantity = $object['quantity'][$i];
                    
                    $purchase_item->gst_value = $object['gst_value'][$i];
                    
                    $purchase_item->amount = $object['amount'][$i];
                    
                    $purchase_item->update();
                }
            }
               return 1;

        });
    }

   
    public function removePurchaseItem($invoice_id)
    {
        return DB::transaction(function () use ($invoice_id) {

            PurchaseItem::where(['invoice_id' => $invoice_id])->update(['is_recycle' => 1]);
        });
    }
}       
