public function indexAction()
 {
     $request = $this->getRequest();
     if (!$request instanceof ConsoleRequest) {
         throw new \RuntimeException('You can only use this action from a console!');
     }
     $serviceLocator = $this->getServiceLocator();
     $dbCheck = $serviceLocator->get('DbCheck');
     if ($dbCheck['dbConnect'] !== false) {
         $productsModel = new ProductsModel($serviceLocator);
     }
     $params = $request->getParams()->toArray();
     $products = array();
     for ($i = 1; $i <= 5; $i++) {
         $products[$i] = $this->sanitize($params['arg' . $i]);
     }
     if (isset($params['ps'])) {
         $params['ps'] = (int) $params['ps'];
     } else {
         $params['ps'] = 0;
     }
     if ($params['ps'] <= 0) {
         $params['ps'] = 1;
     }
     $params['ps'] = 'P' . $params['ps'];
     $params['name'] = (bool) $params['name'];
     $params['id'] = (bool) $params['id'];
     if ($params['name'] !== false) {
         $checkType = "by Product's name";
         $productsTitle = "Product names";
         $field = 'ProductName';
     } elseif ($params['id'] !== false) {
         $checkType = "by Product's database ID";
         $productsTitle = "Product ids";
         $field = 'ID';
     } else {
         $checkType = "by Product's warehouse location";
         $productsTitle = "Warehouse locations";
         $field = 'WarehouseLocation';
         $mapCallback = 'strtoupper';
     }
     // elseif
     $return = "";
     $return .= PHP_EOL;
     $return .= "Route calculator";
     $return .= PHP_EOL;
     $return .= $this->sep;
     $return .= "Check type:" . $this->getTab(2) . $checkType;
     $return .= PHP_EOL . PHP_EOL;
     $return .= "Picking station:" . $this->getTab() . $params['ps'];
     $return .= PHP_EOL . PHP_EOL;
     $return .= $productsTitle . " (user entry):";
     $return .= PHP_EOL;
     for ($i = 1; $i <= 5; $i++) {
         $return .= $this->getTab(3) . $i . ' - ';
         if (isset($mapCallback) && !empty($mapCallback)) {
             $return .= $mapCallback($params['arg' . $i]);
         } else {
             $return .= $params['arg' . $i];
         }
         $return .= PHP_EOL;
     }
     // for
     $return .= PHP_EOL;
     if ($dbCheck['dbConnect'] === false) {
         $return .= $this->raiseError("A database connection cannot be established." . PHP_EOL . "\tPlease check your database connection settings.");
         return $return;
     }
     // if
     if (isset($mapCallback) && !empty($mapCallback)) {
         $products = array_map($mapCallback, $products);
     }
     $productsDB = $productsModel->getProductsIdNameLocation($products, $field);
     if (count($productsDB) != 5) {
         $return .= $this->raiseError('Some of the entered parameters doesn\'t corespond to the database products.');
         return $return;
     }
     // if
     $return .= PHP_EOL;
     $return .= "Calculated route:";
     $return .= PHP_EOL;
     $return .= $this->getTab(3) . "1 -" . $this->getTab() . "Start from picking station " . $params['ps'];
     $return .= PHP_EOL . PHP_EOL;
     $productsIDsDB = array_column($productsDB, 'ID');
     $productsNamesDB = array_column($productsDB, 'ProductName');
     $warehouseLocationsDB = array_column($productsDB, 'WarehouseLocation');
     $warehouseLocations = array_combine($productsIDsDB, $warehouseLocationsDB);
     $routeCalculator = new RouteCalculatorLibrary();
     $routeCalculator->init($params['ps'], $warehouseLocations);
     $route = $routeCalculator->getRoute();
     $warehouseLocations = array_combine($productsIDsDB, $warehouseLocationsDB);
     $productsNames = array_combine($productsIDsDB, $productsNamesDB);
     for ($i = 1; $i <= 5; $i++) {
         $return .= $this->getTab(3) . ($i + 1) . ' - ';
         $return .= $this->getTab() . "Warehouse location:" . $this->getTab() . $warehouseLocations[$route[$i - 1]] . PHP_EOL;
         $return .= $this->getTab(4) . "Product ID:" . $this->getTab(2) . $route[$i - 1] . PHP_EOL;
         $return .= $this->getTab(4) . "Product name:" . $this->getTab(2) . $productsNames[$route[$i - 1]] . PHP_EOL;
         $return .= PHP_EOL;
     }
     // for
     $return .= $this->getTab(3) . "7 -" . $this->getTab() . "Back to picking station " . $params['ps'];
     $return .= PHP_EOL . PHP_EOL;
     $return .= $this->sep;
     return $return;
 }
예제 #2
0
 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);
 }