<?php

namespace App\Traits;
use App\GoodsinProduct;
use App\GoodsIssueProduct;
use App\Product;
use Illuminate\Support\Facades\DB;

trait InventoryTrait
{
    public  function get_stock_summary($from,$to,$groups)
    {
        $dataArray =[];
        foreach ($groups as $key => $group) {

            $products_array = Product::query()->where('group_id',$group->id)->pluck('id');

            $group_product_stocks = GoodsinProduct::query()->with('goodsin','product','product.group')->where(get_where())
                ->whereIn('product_id',$products_array)
                ->get()
                ->groupBy('product_id');


            foreach ($group_product_stocks as $product_id=>$stock){
//               $product_id = 537;
                //  $products = $group_product_stocks->where('product_id',$product_id);
                //  $product_issue =  GoodsIssueProduct::query()->where(get_where())->where('product_id',$product_id)->get();
                // $remain_product_qty = $products->sum('qty') - $product_issue->sum('qty');


                $initial_goods_in = GoodsinProduct::query()->where(get_where())->where('product_id',$product_id)->where('goodsin_id','=',0)->get();
                $initial_goods_price = $initial_goods_in->sum(function($t){ return $t->qty * $t->price; });

                $requisition_goods_in = GoodsinProduct::where('goodsin_products.unit_id','=',auth_unit())->where('product_id',$product_id)
                    ->join('products', 'goodsin_products.product_id', '=', 'products.id')
                    ->join('goodsins', 'goodsin_products.goodsin_id', '=', 'goodsins.id')
                    ->select('products.id as productId','products.name as product_name','goodsin_products.*','goodsins.bill_date')
                    ->get();

                $opening_stock =  $requisition_goods_in->where('bill_date','<', $from);

                $opening_stock_price =  $opening_stock->sum(function($t){ return $t->qty * $t->price; });

                $opening_goods_issue = GoodsIssueProduct::query()->where(get_where())->where('product_id',$product_id)->where('issue_date', '<', $from)->get();


                $opening_goods_issue_qty = $opening_goods_issue->sum('qty');
                $opening_goods_issue_value =  $opening_goods_issue->sum(function($t){ return $t->qty * $t->price; });

                $opening_qty =  $initial_goods_in->sum('qty') +  $opening_stock->sum('qty') - $opening_goods_issue_qty;
                $opening_value =  $initial_goods_price + $opening_stock_price  - $opening_goods_issue_value;

                $goods_received =  $requisition_goods_in->whereBetween('bill_date',[ $from,$to]);
                $goods_received_qty =    $goods_received->sum('qty');
                $goods_received_value = $goods_received->sum(function($t){ return $t->qty * $t->price; });

                $goods_issue = GoodsIssueProduct::query()->where(get_where())->where('product_id',$product_id)->whereBetween('issue_date',[$from,$to])->get();
                $goods_issue_q= GoodsIssueProduct::query()->where(get_where())->where('product_id',$product_id)->whereBetween('issue_date',[$from,$to])->whereNotNull('user_id')->get();
                $goods_issue_qty =    $goods_issue_q->sum('qty');
                $goods_issue_value = $goods_issue->sum(function($t){ return $t->qty * $t->price; });

                $closing_qty = $opening_qty + $goods_received_qty - $goods_issue_qty;
                // $closing_value = $opening_value + $goods_received_value - $goods_issue_value;

                $product_name = $requisition_goods_in->first() ? $requisition_goods_in->first()->product_name : null;
                // if($closing_qty  >0){
                    $rate = $stock->sum('price')/count($stock);
                    // $closing_value = $closing_qty * $rate;
                     $closing_value = $opening_value + $goods_received_value - $goods_issue_value;

                    $dataArray[] =[
                        'group_id'=>$group->id,
                        'group_name'=>$group->name,
                        'product_id'=>$requisition_goods_in->first() ? $requisition_goods_in->first()->productId :null,
                        'name'=>$stock->first()->product ? $stock->first()->product->name : null,
                        'unit'=>optional($stock->first()->product->unit)->name,
                        'opening_qty'=> $opening_qty > 0 ? $opening_qty : 0,
                        'opening_value'=>$opening_value > 0 ? $opening_value : 0,
                        'goods_received_qty'=>$goods_received_qty,
                        'goods_received_value'=> $goods_received_value,
                        'goods_issue_qty'=>$goods_issue_qty,
                        'goods_issue_value'=>$goods_issue_value,
                        'closing_qty'=>$closing_qty > 0 ? $closing_qty : 0,
                        'rate'=>$rate,
                        'closing_value'=> $closing_value > 0 ? $closing_value : 0,
                    ];
                // }

            }

        }

        return $dataArray;
    }


}