<?php

namespace App\Service;
use DB;
use App\Models\User;
use App\Models\QuotationItem;
use App\Models\Item;
use Validator;
use Log;
use Auth;

class SaleItemService
{
    public function saveItems($request,$transaction){
        $user=Auth::user();
        
        foreach ($request['items'] as $key => $item) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
            $obj['item_id']=$item['item_id']??0;
            $obj['item_name']=$item['item_name']??null;
            $obj['hsn_code']=$item['hsn_code']??null;
            $obj['quantity']=$item['quantity']??0;
            $obj['unit_id']=$item['unit_id']??0;
            
            $obj['item_remark']=$item['item_remark']??null;
            $obj['warehouse']=$item['warehouse']??0;
            $obj['zone']=$item['zone']??0;
            $obj['rack']=$item['rack']??0;
            $obj['bin']=$item['bin']??0;
            $obj['type']=$item['type']??null;
            $obj['is_service']=$item['is_service']??0;
            $obj['service_data']=$item['service_data']??null;
            $obj['account_id']=$item['account_id']??null;

            // Amounts
            $obj['rate']=$item['rate']??0;
            $obj['gross_amount']=$item['gross_amount']??0;
            $obj['tax_rate']=$item['tax_rate']??0;
            $obj['tax_amount']=$item['tax_amount']??0;
            $obj['discount']=$item['discount']??0;
            $obj['discount_percent']=$item['discount_percent']??null;
            $obj['discount_type']=$item['discount_type']??0;
            $obj['discount_before_tax']=$item['discount_before_tax']??0;
            $obj['discount_after_tax']=$item['discount_after_tax']??0;
            $obj['final_amount']=$item['final_amount']??0;

            $obj['transaction_id']=$transaction['id'];

            $new_item=null;

            // check if item is not created and is not a service
            if(!isset($obj['item_id']) && $obj['type']!='Service'){

                // creating New item
                $ob['name']=$obj['item_name'];
                $ob['user_id']=$user['id'];
                $ob['unit']=$obj['unit_id']??0;
                $ob['price']=$obj['rate']??0;
                $ob['tax_rate']=$obj['tax_rate']??0;
                $ob['type']='Product';
                $ob['sale_acc_id']=53;
                $ob['purchase_acc_id']=30;
                $ob['tax_preference']=$ob['tax_rate']>0?1:2;
                
                $new_item=Item::create($ob);

                $obj['item_id']=$new_item['id']??0;
            }

            $saleItem=null;

            $saleItem=QuotationItem::create($obj);
            

        }

        $transaction->update();
        
        return true;
        
    }
}