<?php

namespace App\Http\Controllers\Inventory;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Inventory\PurchaseOrder;
use App\Inventory\Item;
use App\Inventory\Payable;
use App\Inventory\ReceivingNote;
use App\Inventory\BillOfQuantity;
use App\Inventory\IssueNote;
use App\Inventory\BOQItems;
use App\Inventory\Stock;
use App\User;
use App\Branch;
use PDF;
use App\Http\Requests\InventoryRequests\GinRequest;
use Illuminate\Contracts\Encryption\DecryptException;



class IssueNoteController extends Controller
{
    //
    public function view()
    {

      $item = new Item;
      $type = ['Service'];
      $type2 = ['Product'];
      $user_type = ['Supplier'];

      $boq = new BillOfQuantity;
      $boq_list = $boq->getAllBOQ2();
      $service_list = $item->getItemListByType($type);
      $product_list = $item->getItemListByType($type2);
      $branch_obj = new Branch;
      $branch    =  $branch_obj->getBranchList();
      $floor_list  = $boq->getBOQListByColumn('floor');

      $users = new User;
      $user = $users->getUserListByType($user_type);



$auser_list = $users->getUserListByType(['Employee']);

      $issueNote = new IssueNote;
      $product_note = $issueNote->getNoteByType('Product');
      $service_note = $issueNote->getNoteByType('Service');

      $purchase_order = new PurchaseOrder;

      $purchase_order_list = $purchase_order->getAllPurchaseOrders();

          return view('inventory_module.issuance_notes.manage_issuance_note',
          	[
          		'branch' => $branch,
              'product_list'=>$product_list,
              'service_list'=>$service_list,
              'user'=>$user,
              'product_note'=>$product_note,
              'service_note'=>$service_note,
              'auser_list'=>$auser_list,
              'floor_list'=>$floor_list,
              'purchase_order_list'=>$purchase_order_list,
              'boq_list'=>$boq_list
          	]
            );
    }

    public function commitGin(GinRequest $request)
    {

      try
      {
        $a_u = \Auth::User();
        $au_id = $a_u->id;
      $form_collect = [];
      $form_collect['product_id'] = decrypt($request->product_id);
      $form_collect['receiver_id'] = decrypt($request->receiver_id);
      $form_collect['project_id'] = decrypt($request->project);
      $form_collect['remarks'] = $request->remarks;
      $form_collect['floor'] = $request->floor;
      $form_collect['p_number'] = $request->remarks;
      $form_collect['quantity'] = $request->quantity;
      $form_collect['type'] = "Product";
      $form_collect['au_id'] = decrypt($request->au_user);
      if($form_collect['project_id'] == '0' || $form_collect['product_id'] == '0')
      {
        parent::errorMessage('Please Select options.');
        return redirect()->back();
      }

      $issueNote = new IssueNote;
      $curr_note = $issueNote->createGIN($form_collect);

      if(isset($curr_note->id))
      {
        $stock_form = [];
        $stock_form['issue_note_id'] = $curr_note->id;
        $stock_form['out_quantity'] = $form_collect['quantity'];
        $stock_form['date'] = date('Y-m-d');
        $stock_form['item_id'] = $form_collect['product_id'];
        $stock_form['branch_id'] = $curr_note->project_id;

        $stock = new Stock;
        $curr_stock = $stock->outStock($stock_form);

        $item = new Item;
        $curr_item = $item->updateItemQty($form_collect['product_id'],$form_collect['quantity']);

        parent::setLogMessage('GIN "'.$curr_note->id.' Remarks: '.$curr_note->remarks.'" has been created');
        parent::successMessage('GIN has been created successfully.');
        return redirect()->back();
      }

      parent::errorMessage('Something went wrong.');
      return redirect()->back();

      }
      catch(DecryptException $e)
      {

      }
   return redirect()->back();

    }

    public function downloadPDFgin($id)
    {

      try
      {

      $id = decrypt($id);
      $issueNote = new IssueNote;
      $curr_note = $issueNote->issueNoteByID($id);



      if(isset($curr_note->id))
      {
      $pdf = PDF::loadView('inventory_module.reports.ginPDF',[
             'gin'                   => $curr_note,
      ]);

      return $pdf->setPaper('legal','potrait')->stream('GIN.pdf');
      }
      parent::errorMessage('Something went wrong.');
      return redirect()->back();
      }
      catch(DecryptException $e)
      {

      }

      return redirect()->back();



    }

    public function commitGinService(GinRequest $request)
    {

      try
      {

      $form_collect = [];
      $form_collect['service_id'] = decrypt($request->service_id);
      $form_collect['receiver_id'] = decrypt($request->receiver_id);
      $form_collect['project_id'] = decrypt($request->project);
      $form_collect['remarks'] = $request->remarks;
      $form_collect['type'] = "Service";
      if($form_collect['project_id'] == '0' || $form_collect['service_id'] == '0')
      {
        parent::errorMessage('Please Select options.');
        return redirect()->back();
      }

      $issueNote = new IssueNote;
      $curr_note = $issueNote->createGINService($form_collect);

      if(isset($curr_note->id))
      {

        parent::setLogMessage('GIN "'.$curr_note->id.' Remarks: '.$curr_note->remarks.'" has been created');
        parent::successMessage('GIN has been created successfully.');
        return redirect()->back();
      }

      parent::errorMessage('Something went wrong.');
      return redirect()->back();

      }
      catch(DecryptException $e)
      {

      }

       return redirect()->back();


    }





    public function viewdetailsGIN($id)
    {

      try
      {

      $id = decrypt($id);
      $issueNote = new IssueNote;
      $curr_note = $issueNote->issueNoteByID($id);



      if(isset($curr_note->id))
      {
      return view('inventory_module.issuance_notes.note_details',[
             'gin'                   => $curr_note,
      ]);

      //return $pdf->setPaper('legal','potrait')->stream('GIN.pdf');
      }
      parent::errorMessage('Something went wrong.');
      return redirect()->back();
      }
      catch(DecryptException $e)
      {

      }

      return redirect()->back();



    }
}
