<?php

namespace App\Service;
use DB;
use App\Models\User;
use App\Models\QuotationItem;
use App\Models\SaleOrderItem;
use App\Models\InvoiceItem;
use App\Models\Invoice;
use App\Models\Item;
use Validator;
use Log;

class SaleItemService
{
    public function saveItems($request,$transaction){
        
        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['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;

            if($request['sale_type']=='quotation'){
                $saleItem=QuotationItem::create($obj);
            }

            if($request['sale_type']=='sale_order'){
    
                $saleItem=SaleOrderItem::create($obj);
            }

            if($request['sale_type']=='invoice'){
                
                if(isset($obj['item_id']) && $obj['item_id']>0 && $obj['item_id']!=null){
                    $item=Item::where('id',$obj['item_id'])->first();
                    $obj['sale_acc_id']=$item['sale_acc_id']??0;
                }

                // if(!isset($transaction['total_tax'])){
                //     $transaction['total_tax']=$transaction['total_tax'] + (isset($obj['tax_amount']) ? $obj['tax_amount'] : 0);
                // }
                
                // if(!isset($transaction['total_gross_amount'])){
                //     $transaction['total_gross_amount']=$transaction['total_gross_amount'] + (isset($obj['gross_amount']) ? $obj['gross_amount'] : 0);
                // }
                
                $saleItem=InvoiceItem::create($obj);
            }

        }

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