<?php

namespace App\Http\Controllers\Inventory;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Inventory\PurchaseOrder;
use App\Inventory\ReceivingNote;
use App\Inventory\Item;
use App\Inventory\Payable;
use Illuminate\Contracts\Encryption\DecryptException;
use PDF;


class PurchaseOrderController extends Controller
{
    public function viewAllPurchaseOrders()
    {
    	$purchase_order = new PurchaseOrder;

    	$purchase_order_list = $purchase_order->getAllPurchaseOrders();

      $curr_date =date("Y-m-d");
      $time = strtotime($curr_date.'-2 days');
      $date = date("Y-m-d", $time);

      $alerts = $purchase_order_list->where('status' , "Approved");
      $a_array = [];
      if(count($alerts)>0)
      {
      foreach($alerts as $a)
      {

        $alert_curr = $purchase_order->getPObyNoAll($a->p_number);

        if(count($alert_curr) > 0)
        {

        foreach($alert_curr as $ac)
        {

          if($ac->delivery_date < $date)
          {

          $myvar = ReceivingNote::where('purchase_order_id' , $ac->id)->get();
          //dd($myvar);
          if($myvar)
          {
            $myvar2 = ReceivingNote::where('purchase_order_id' , $ac->id)->sum('accepted_quantity');
          if(isset($myvar[0]->total_quantity) && $myvar[0]->total_quantity == $myvar2)
          {

          }
          else{
            $a_array[] = $ac;

          }
        }
        }
        }
        }

    }
  }

        // $a_array = $a_array->toArray();
        //dd($a_array);
        	return view('inventory_module.purchase_order.manage_purchase_order',
         	[
         		'purchase_order_list' => $purchase_order_list,
            'alerts' =>$a_array
         	]
	  	);

}

    public function rejectPO($id)
    {
      try
      {

            $id = decrypt($id);

            $po = new PurchaseOrder;

            if($po->rejectPO($id))
            {
                parent::successMessage('Purchase Order has been rejected.');
            }

            return redirect()->back();


      }
      catch(DecryptException $e)
      {

      }


    }

    public function acceptPO(Request $request)
    {
      try
      {

            $id = decrypt($request->po_id);

            $date = $request->po_date;

            $po = new PurchaseOrder;

            $payable = new Payable;

            if($date == null || $date == "")
            {
              $date = "2050-01-01";
            }

            $curr_pos = $po->acceptPO($id,$date);
            $curr_pos = $curr_pos->first();
// dd($curr_pos->boqitem->item);  
            $curr_item = $curr_pos->boqitem->item;

            if(isset($curr_item) && $curr_item->type == "Service")
            {
              $curr_pos->status = "Completed";
         
              $curr_pos->update();

              $object['id'] =  $curr_pos->id;

              $qty     = $curr_pos->boqitem->quantity;

              $object['total_amount']      = $qty * $curr_pos->unit_price;

              $object['received_amount']      = NULL;

              $payable = $payable->storeServicesPayable($object);



            }
            else{
              
            }
            if(isset($curr_pos))
            {
              parent::successMessage('Purchase Order has been accepted');
            }

            return redirect()->back();


      }
      catch(DecryptException $e)
      {

      }


    }

    public function PODetail($id)
    {
      try
      {
            $id = decrypt($id);
            $po = new PurchaseOrder;
            $po_detail = $po->getPObyNoAll($id);

            if($po_detail[0]->id)
            {
              return view('inventory_module.purchase_order.purchase_order_detail',
             	[
             		'po_detail' => $po_detail,
             	]
    	  	);
        }
            return redirect()->back();
      }
      catch(DecryptException $e)
      {
      }
    }

    public function downloadPDFpo($id)
    {

      try
      {

      $id = decrypt($id);
      $po = new PurchaseOrder;
      $curr_po = $po->getPObyNoAll($id);



      if(isset($curr_po[0]->id))
      {
      $pdf = PDF::loadView('inventory_module.reports.PoPDF',[
             'po'                   => $curr_po,
      ]);

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

      }

      return redirect()->back();



    }

     public function downloadPDFporate($id)
    {

      try
      {

      $id = decrypt($id);
      $po = new PurchaseOrder;
      $curr_po = $po->getPObyNoAll($id);



      if(isset($curr_po[0]->id))
      {
      $pdf = PDF::loadView('inventory_module.reports.ratePoPDF',[
             'po'                   => $curr_po,
      ]);

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

      }

      return redirect()->back();



    }

}
