public function printAction()
 {
     $request = $this->getRequest();
     $uri = $request->getUriString();
     if (strpos($uri, '/print/') !== false) {
         $startPos = strpos($uri, '/print/') + 7;
         $orderID = (int) substr($uri, $startPos, strlen($uri) - $startPos);
         if ($orderID <= 0) {
             return $this->redirect()->toUrl('/error-404');
         }
         $serviceLocator = $this->getServiceLocator();
         $ordersModel = new OrdersModel($serviceLocator);
         $data['order'] = $ordersModel->getOrderByID($orderID);
         if (empty($data['order'])) {
             return $this->redirect()->toUrl('/error-404');
         }
         $productsModel = new ProductsModel($serviceLocator);
         $data['products'] = $productsModel->getProductsByOrder($orderID);
         if (count($data['products']) != 5) {
             return $this->redirect()->toUrl('/error-404');
         }
     } else {
         return $this->redirect()->toUrl('/error-404');
     }
     // else
     $this->layout('warehouse/orders/print');
     $this->layout()->setVariables($data);
 }
 public function indexAction()
 {
     $data = array();
     $data['title'] = 'Products';
     $data['new_order'] = true;
     $data['menu'] = array('link' => 'orders', 'anchor' => 'View orders');
     $serviceLocator = $this->getServiceLocator();
     $dbCheck = $serviceLocator->get('DbCheck');
     if ($dbCheck['dbConnect'] === false) {
         $this->layout('db/error');
         return false;
     }
     // if
     $stations = new PickingStationsLibrary($serviceLocator);
     $data['pickingStations'] = $stations->getPickingStations();
     $currentPickingStation = $stations->getCurrentPickingStation();
     $data['currentPickingStationName'] = $currentPickingStation['StationName'];
     $productsModel = new ProductsModel($serviceLocator);
     $form = new ProductsForm('newOrder');
     $request = $this->getRequest();
     if ($request->isPost()) {
         $filter = new ProductsFilter();
         $form->setInputFilter($filter->getInputFilter());
         $form->setData($request->getPost());
         if (!$form->isValid()) {
             $messages = $form->getMessages();
             return $this->raiseError(current($messages['ssf']));
         }
         // if
         $products = $filter->isExtraValid($request->getPost('product'));
         if ($products === false) {
             return $this->raiseError('The "Send Order" operation encountered some problems. If this message persists, please refer to website\'s admin.');
         }
         $productsIDs = array_keys($products);
         $availableStockUnitsAndLocations = $productsModel->getProductsAvailableUnitsAndLocations($productsIDs);
         $availableUnitsDB = array_column($availableStockUnitsAndLocations, 'AvailableStockUnits');
         $productsIDsDB = array_column($availableStockUnitsAndLocations, 'ID');
         $availableUnits = array_combine($productsIDsDB, $availableUnitsDB);
         foreach ($products as $id => $units) {
             if ($availableUnits[$id] < $units) {
                 return $this->raiseError('There aren\'t enough available units to cover your request');
             }
         }
         // foreach
         $warehouseLocationsDB = array_column($availableStockUnitsAndLocations, 'WarehouseLocation');
         $warehouseLocations = array_combine($productsIDsDB, $warehouseLocationsDB);
         $routeCalculator = new RouteCalculatorLibrary();
         $routeCalculator->init($currentPickingStation['StationAlias'], $warehouseLocations);
         $route = $routeCalculator->getRoute();
         $routes = array_flip($route);
         $ordersModel = new OrdersModel($serviceLocator);
         $insertOrder = array('OrderDate' => date('Y-m-d H:i:s'), 'PickingStationID' => $currentPickingStation['ID']);
         $insertedOrderID = $ordersModel->newOrder($insertOrder);
         $insertOrderProducts = array();
         foreach ($products as $productID => $orderedUnits) {
             $insertOrderProducts[] = array('OrderID' => $insertedOrderID, 'ProductID' => $productID, 'OrderedUnits' => $orderedUnits, 'Route' => $routes[$productID] + 1);
         }
         // foreach
         $insertedOrderProducts = $productsModel->addOrderProducts($insertOrderProducts);
         $updatedProducts = $productsModel->updateStockValue($products);
         return $this->redirect()->toRoute('orders', array(), array('query' => array('view' => $insertedOrderID)));
     }
     // if
     $data['form'] = $form;
     $data['letters'] = range('A', 'Z');
     $products = $productsModel->getProducts();
     $data['products'] = $this->sortProducts($products);
     $this->layout()->setVariables($data);
     return new ViewModel($data);
 }