<?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\ProjectStock;
use App\Inventory\Stock;
use App\Branch;
use App\User;
use Crypt;

class StockController extends Controller
{
    //

    public function view()
    {

      $branch_obj = new Branch;
      $branch    =  $branch_obj->getBranchList();
      $product = new Item;
      $type = ['Product'];

      $product_list = $product->getItemListByType($type);

          return view('inventory_module.stock.manageStock' , ['branch' => $branch,'product_list'=>$product_list]);
        }


    public function stockDetail($id)
    {

      try
      {

          $id = decrypt($id);
          $product = new Item;
          $product_list = $product->getItemById($id);

          $stock = new Stock;
          $stock_list = $stock->getStockbyItem($id);

              return view('inventory_module.stock.stockDetail' , ['product_list'=>$product_list,'stock_list'=>$stock_list]);
    }


      catch(DecryptException $e)
      {

      }
    }


    public function transfer(Request $request)
    {

      try
      {

          $form_collect = [];
          $form_collect['to_branch_id'] = decrypt($request->project);
          $form_collect['from_branch_id'] = decrypt($request->project1);
          $form_collect['stock_in_qty'] = ($request->quantity);
          $form_collect['stock_out_qty'] = ($request->quantity);
          $form_collect['item_id'] = decrypt($request->product_id);
          $form_collect['remarks'] = ($request->remarks);
          
          $stock = new ProjectStock;
          $curr_stock = $stock->transferStock($form_collect);

          if(isset($curr_stock->id))
          {
            parent::successMessage('Stock has been transfered.');
            return redirect()->back();
          }
    }


      catch(DecryptException $e)
      {

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

}
