<?php

namespace App\Http\Controllers\Inventory;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Inventory\Item;
use App\Inventory\InventoryCategory;
use App\Inventory\InventoryUnit;
use App\Inventory\ProjectStock;
use App\Inventory\Stock;
use App\Branch;
use App\Inventory\ReceivingNote;
use App\Inventory\Payable;
use PDF;

use Illuminate\Contracts\Encryption\DecryptException;

class InventoryController extends Controller
{

     public function index()
    {

        return view('inventory_module.inventory_dashboard');
    }

    public function reports()
   {
     $branch_obj  = new Branch;
     $grn            = new ReceivingNote;

     $branch      = $branch_obj->getBranchList();
     $grn_list            = $grn->getAllGrn();

       return view('inventory_module.reports.dashboard',
       	[
       		'branch'    =>  $branch,
       		'grn_list'    =>  $grn_list,
       	]);
   }



   public function projectwise(Request $request)
   {
    $projectid = decrypt($request->project);
    $branch_obj  = new Branch;
    $item = new Item;
    $type = ['Product'];
    $product_list          = $item->getItemListByType($type);
    $branch      = $branch_obj->getBranchById($projectid);

    if(isset($branch->id))
    {
      return view('inventory_module.reports.projectwise',
       [
         'branch'    =>  $branch,
         'product_list' => $product_list,
       ]);
    }
    return redirect()->back();

   }

    public function inventoryPayable(Request $request)
    {
      try{

        $form_collect = $request->input();
        
        $flag = false;

        $payable = new Payable;

        $payable_condition['receiving_note_id']   = decrypt($form_collect['grn']);

        if($payable_condition['receiving_note_id'] == "0")
        {
            $flag = true;
        }

        $payable_condition    = parent::filterConditionalArray($payable_condition);
       
        $payable_list = $payable->getPayableListByGRN($payable_condition[0]);

        $pdf = PDF::loadView('inventory_module.reports.payable_report',[
  
          'payable_list' => $payable_list,
          'flag'         => $flag,
  
          ]);

      return $pdf->setPaper('a4', 'landscape')->stream('Payable Report.pdf');

      }
      catch(DecryptException $e){

      }
    }
}
