/**
  * Merge a list Purchase Order Details and their Articles
  */
 public function mergePODetailWithArticle($id, $completedArticles)
 {
     $results = [];
     // filter to get purchaseOrderDetails
     $filter = ['Order_Number' => $id];
     // get the PurchaseOrderDetails
     $purchaseOrderDetails = $this->purchaseOrderDetailRepository->filterOn($filter, 0);
     Log::debug(__METHOD__ . '(' . __LINE__ . '):  PODs: ' . (isset($purchaseOrderDetails) ? count($purchaseOrderDetails) : 'none found'));
     //dd($purchaseOrderDetails);
     if (isset($purchaseOrderDetails) && count($purchaseOrderDetails)) {
         // foreach purchaseOrderDetail record, merge with Article
         foreach ($purchaseOrderDetails as $purchaseOrderDetail) {
             $article = $this->articleRepository->find($purchaseOrderDetail->SKU);
             Log::debug(__METHOD__ . '(' . __LINE__ . '):  articleID: ' . $article->objectID);
             // $received is calculated from ReceiptHistory counting Received UPC into Tote... entries
             $filter = ['POD' => $purchaseOrderDetail->objectID, 'Activity' => preg_replace('/ - .*/', '', Lang::get('internal.receiptHistory.putUPCinTote', ['time' => '%', 'upcSKU' => '%', 'n' => '%', 'ofn' => '%']))];
             // Status: 0 => '', 1 => 'success', 2 => 'warning'
             $status = 0;
             $expected = 0;
             $received = 0;
             $upcs = $this->upcRepository->getArticleUPCs($article->objectID, 0);
             Log::debug(__METHOD__ . '(' . __LINE__ . '):  upcs: ' . count($upcs));
             foreach ($upcs as $upc) {
                 # accumulate expected and received
                 $expected += $upcExpected = $purchaseOrderDetail->Expected_Qty * $upc->parents[$article->objectID]->Quantity;
                 $filter['UPC'] = $upc->objectID;
                 $received += $upcReceived = $this->receiptHistoryRepository->countOn($filter);
                 Log::debug(__METHOD__ . '(' . __LINE__ . '):  received: ' . $received);
                 # Status calculations
                 if ($upcExpected > $upcReceived) {
                     $upcStatus = 0;
                 } elseif ($upcExpected == $upcReceived) {
                     $upcStatus = 1;
                 } else {
                     $upcStatus = 2;
                 }
                 $status = $upcStatus > $status ? $upcStatus : $status;
             }
             $statuses = ['', 'success', 'warning'];
             Log::debug(__METHOD__ . '(' . __LINE__ . '):  status: ' . $status);
             if ($status == 1 and $expected == $received and $completedArticles == 'Show_Completed_Articles') {
                 // hide the all success Articles, when the button will say Show..
             } else {
                 // build results
                 $result = ['status' => $statuses[$status], 'purchaseOrderDetailID' => $purchaseOrderDetail->objectID, 'Expected_Qty' => $purchaseOrderDetail->Expected_Qty, 'Received_Qty' => '' . $received . '/' . $expected, '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);
     return $results;
 }
 /**
  * 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()]);
 }