/**
  * Display a Listing of the resource.
  */
 public function index()
 {
     $purchaseOrder = Request::all();
     //dd(__METHOD__."(".__LINE__.")",compact('purchaseOrder'));
     if (count($purchaseOrder) == 0) {
         // lets provide a default filter
         $client = $this->clientRepository->filterOn(['Client_Name' => 'LCL'], 1);
         $purchaseOrder['Client'] = $client->objectID;
         $purchaseOrder['Expected'] = Carbon::today()->format('Y-m');
     }
     // using an implementation of the PurchaseOrder Repository Interface
     $purchaseOrders = $this->purchaseOrderRepository->paginate($purchaseOrder);
     // possible Statuses
     $statuses = [Lang::get('labels.enter.Status')] + Lang::get('lists.purchaseOrder.status');
     // Using the view(..) helper function
     return view('pages.purchaseOrder.index', compact('purchaseOrder', 'purchaseOrders', 'statuses'));
 }
 /**
  * Export a Filtered Listing of the resource.
  *
  * See: ViewCreators/ExportTypeCreator for a list of the exportTypes we need to support.
  */
 public function export($id)
 {
     if (Entrust::hasRole('teamLead') == False) {
         return redirect()->route('home');
     }
     $request = (object) Request::all();
     $filter = ['id' => $id];
     //dd(__METHOD__.'('.__LINE__.')',compact('id','request','filter'));
     if ($request->ExportType == 'xls') {
         /* Merge a list Purchase Order Details with their Articles
          *
          * First we build an array of this POD Articles
          * fill out the calculated received / expected values
          * and lastly hand it to our xls formatter
          */
         $podArticles = $this->articleRepository->getPODArticles($id, 0);
         $this->calculateReceivedExpected($podArticles);
         //dd(__METHOD__."(".__LINE__.")",compact('id','request','filter','podArticles'));
         // TODO file name should be calculated in a separate class
         $count = sprintf('%04d', $this->countersRepository->increment('exportFile'));
         $currentDate = Carbon::now()->format('YmdHis');
         $fileName = 'POReconciliation-' . $currentDate . $count;
         //dd(__METHOD__."(".__LINE__.")",compact('id','request','filter','podArticles','fileName'));
         // create Excel workbook
         Excel::create($fileName, function ($excel) use($filter, $podArticles) {
             $excel->sheet('New sheet', function ($sheet) use($filter, $podArticles) {
                 $sheet->loadView('pages.poReconciliation.excel')->with('filter', $filter)->with('podArticles', $podArticles);
             });
         })->export('xls');
     }
     if ($request->ExportType == 'csv') {
         /* Merge a list Purchase Order Details with their Articles
          *
          * First we build an array of this POD Articles
          * fill out the calculated received / expected values
          * and lastly hand it to our xls formatter
          */
         $podArticles = $this->articleRepository->getPODArticles($id, 0);
         $this->calculateReceivedExpected($podArticles);
         //dd(__METHOD__."(".__LINE__.")",compact('id','request','filter','podArticles'));
         // TODO file name should be calculated in a separate class
         $count = sprintf('%04d', $this->countersRepository->increment('exportFile'));
         $currentDate = Carbon::now()->format('YmdHis');
         $fileName = 'POReconciliation-' . $currentDate . $count;
         //dd(__METHOD__."(".__LINE__.")",compact('id','request','filter','podArticles','fileName'));
         // create Excel workbook
         Excel::create($fileName, function ($excel) use($filter, $podArticles) {
             $excel->sheet('New sheet', function ($sheet) use($filter, $podArticles) {
                 $sheet->loadView('pages.poReconciliation.excel')->with('filter', $filter)->with('podArticles', $podArticles);
             });
         })->export('csv');
     }
 }
 /**
  * Merge a list Purchase Order Details and their Articles
  */
 public function mergePODetailWithArticle($id)
 {
     $results = [];
     // filter to get purchaseOrderDetails
     $filter = ['Order_Number' => $id];
     // get the PurchaseOrderDetails
     $purchaseOrderDetails = $this->purchaseOrderDetailRepository->filterOn($filter, 0);
     //dd(__METHOD__."(".__LINE__.")",compact('purchaseOrderDetails'));
     if (isset($purchaseOrderDetails) && count($purchaseOrderDetails)) {
         // foreach purchaseOrderDetail record, merge with Article
         for ($i = 0; $i < count($purchaseOrderDetails); $i++) {
             $article = $this->articleRepository->find($purchaseOrderDetails[$i]->SKU);
             if (isset($article)) {
                 // build results
                 $result = ['purchaseOrderDetailID' => $purchaseOrderDetails[$i]->objectID, 'Expected_Qty' => $purchaseOrderDetails[$i]->Expected_Qty, 'articleID' => $article->objectID, 'Client_SKU' => $article->Client_SKU, 'Description' => $article->Description, 'UOM' => $article->UOM, 'Case_Pack' => $article->Case_Pack, 'Colour' => $article->Colour, 'Zone' => $article->Zone, 'rework' => isset($article->rework) ? $article->rework : Lang::get('labels.rework_unknown')];
                 $results[$article->Client_SKU] = $result;
             }
         }
     }
     ksort($results);
     //dd($results);
     $perPage = 10;
     if (Request::has('page')) {
         $bypass = $perPage * (Request::get('page') - 1) * -1;
     } else {
         $bypass = 0;
     }
     $thisPage = [];
     foreach ($results as $key => $value) {
         $bypass++;
         if ($bypass > 0) {
             $thisPage[] = $value;
         }
         if (count($thisPage) == $perPage) {
             break;
         }
     }
     $currentURL = Request::url();
     //dd(__METHOD__."(".__LINE__.")",compact('thisPage','results','bypass', 'currentURL'));
     return new LengthAwarePaginator($thisPage, count($results), $perPage, Request::get('page'), ['path' => Request::url()]);
 }