<?php

namespace App\Http\Controllers;
use App\Group;
use App\Product;
use App\Purchase;
use App\Department;
use App\ProductUnit;
use App\Requisition;
use App\TempData;
use App\RequisitionProduct;
use App\GoodsinProduct;
use App\GoodsIssueProduct;
use App\Supplier;
use App\Unit;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;

class StockIssueController extends Controller
{
    public function index(){
        $date_today = date('Y-m-d');
        $items = Product::pluck('name','id');
        return view('inventory.stock_issue.add_stock_issue',compact('date_today','items'));
    }

    public function getSuppliers($product_id){

        $units = Unit::whereIn ('id', [7,8,10,11,12,13])->get();
     
        $suppliers = GoodsinProduct::query()->where(get_where())->where('product_id',$product_id)->where('supplier_id', '!=', null)->get(['id','supplier_id','product_unit_id']);
        $suppliers_array = array();
        if(isset($suppliers[0]->id)){
            foreach ($suppliers as $key => $value) {
                $suppliers_array[$value->supplier_id]['name'] = Supplier::find($value->supplier_id)->name;
                $suppliers_array[$value->supplier_id]['unit'] = ProductUnit::find($value->product_unit_id)->name;
                $suppliers_array[$value->supplier_id]['price'] = GoodsinProduct::where(get_where())->where('product_id',$product_id)->where('supplier_id',$value->supplier_id)->orderBy('id', 'DESC')->first()->price;
                $suppliers_array[$value->supplier_id]['qty'] = GoodsinProduct::where(get_where())->where('product_id',$product_id)->where('supplier_id',$value->supplier_id)->sum('qty') - GoodsIssueProduct::where(get_where())->where('product_id',$product_id)->where('supplier_id',$value->supplier_id)->sum('qty');
            }
        }

        return view('inventory.stock_issue.get-suppliers',compact('suppliers_array', 'units'));

    }
    
    
    
    public function store(Request $request){
    //  dd ($request->input_qty);
    $issue_unit = $request->get('unit_id');
    // dd($issue_unit);

        if(!empty($request->input_qty)){
            $i = 0;
            $key = 0;
            $issued_qty = 0;
            foreach ($request->input_qty as $supplier_id => $qty) {
                if($qty != 0){
                    $i++;
                    $key++;
                    // $price = GoodsinProduct::where(get_where())->where('product_id',$request->product_id)->where('supplier_id',$supplier_id)->sum('price') / GoodsinProduct::where(get_where())->where('product_id',$request->product_id)->where('supplier_id',$supplier_id)->count('id');
                    $price = GoodsinProduct::where(get_where())->where('product_id',$request->product_id)->where('supplier_id',$supplier_id)->sum('value')/ GoodsinProduct::where(get_where())->where('product_id',$request->product_id)->where('supplier_id',$supplier_id)->sum('qty') ;
                    // dd( $price);
                    $stock_qty = GoodsinProduct::where(get_where())->where('product_id',$request->product_id)->where('supplier_id',$supplier_id)->sum('qty') -GoodsIssueProduct::where(get_where())->where('product_id',$request->product_id)->where('supplier_id',$supplier_id)->sum('qty');
                    if($qty <= $stock_qty){
                        GoodsIssueProduct::create([
                            'unit_id' => auth_unit()??null,
                            'issue_unit' =>  $issue_unit[$issued_qty],
                            'user_id' => \Auth::id(),
                            'product_id' => $request->product_id,
                            'group_id' => Product::where('id',$request->product_id)->first()->group_id,
                            'supplier_id' => $supplier_id,
                            'qty' => $qty,
                            'price' => strval(round($price,2)),
                            'value' => strval( round($price * $qty, 2) ),
                            'reference_no' => $request->reference_no,
                            'issue_date' => $request->issue_date,
                            'remarks' => $request->note,
                        ]);
                        $issued_qty++;
                    }
                }
            }
            if($i == 0){
                session()->flash('error','Total Qty must be greater than 0');

            }else{
                if($issued_qty == 0){
                    session()->flash('error','Input Qty must be less than available Qty !');
                }else{
                    session()->flash('success','Goods Issued successfully for '.$issued_qty.' supplier !');
                }

            }
        }else{
            session()->flash('error','No supplier selected');
        }
        return back();
    }



    public function report(Request $request){
        $datas = null;
        $from = date('Y-m-d',strtotime('first day of last month'));
        $to = date('Y-m-d', strtotime('last day of last month'));
        if($request->from != null && $request->to != null){
            $from = date('Y-m-d',strtotime($request->from));
           
            $to = date('Y-m-d',strtotime($request->to));
            $datas = GoodsIssueProduct::where(get_where())->where('issue_date', '>=', $from)->where('issue_date', '<=', $to)->get();
            
        }
        return view('inventory.stock_issue.stock-issue-report', compact('to','from','datas'));
    }


}
