/**
  * Display a listing of the resource.
  *
  * @return Response
  */
 public function index()
 {
     $search_query = \Input::get('q');
     $matches_ids = [];
     if ($search_query) {
         $sphinx = \Sphinx\SphinxClient::create();
         $sphinx->setServer('127.0.0.1', 3312);
         $sphinx->setMatchMode(\Sphinx\SphinxClient::SPH_MATCH_ANY);
         $sphinx->setSortMode(\Sphinx\SphinxClient::SPH_SORT_RELEVANCE);
         $sphinx->setFieldWeights(array('name' => 20, 'description' => 10));
         $sphinx->addQuery($search_query, '*');
         $res = $sphinx->runQueries();
         if (!$res or !array_key_exists('matches', $res[0])) {
             return [];
         }
         $matches_ids = array_keys($res[0]['matches']);
     }
     $products_in_purchases_arr = \DB::table('products_in_purchase')->select('*')->take(10);
     if (!empty($matches_ids)) {
         $products_in_purchases_arr = $products_in_purchases_arr->whereIn('product_id', $matches_ids);
     }
     $products_in_purchases_arr = $products_in_purchases_arr->get();
     if (empty($products_in_purchases_arr)) {
         return [];
     }
     $products_ins_arr = [];
     foreach ($products_in_purchases_arr as $product_in_purchase_mix) {
         $product = \App\BusinessLogic\Models\Product::find($product_in_purchase_mix->product_id);
         $purchase = \App\BusinessLogic\Models\Purchase::find($product_in_purchase_mix->purchase_id);
         $products_ins_arr[$product_in_purchase_mix->purchase_id . '_' . $product_in_purchase_mix->product_id] = json_decode(new \App\BusinessLogic\ProductInPurchase($product, $purchase));
     }
     return $products_ins_arr;
 }
 public function getProductsInPurchases()
 {
     $filter = \Input::get('filter', []);
     $search_query = '';
     $category_id = 0;
     if (array_key_exists('query', $filter)) {
         $search_query = trim($filter['query']);
     }
     if (array_key_exists('category', $filter)) {
         $category_id = intval($filter['category']);
     }
     $matches_ids = [];
     if ($category_id) {
         $products_ids_arr = \App\Helpers\ProjectHelper::getProductsIdsArrByTagId($category_id);
         $matches_ids = array_merge($matches_ids, $products_ids_arr);
     }
     if ($search_query) {
         $sphinx = \Sphinx\SphinxClient::create();
         $sphinx->setServer('127.0.0.1', 3312);
         $sphinx->setMatchMode(\Sphinx\SphinxClient::SPH_MATCH_ANY);
         $sphinx->setSortMode(\Sphinx\SphinxClient::SPH_SORT_RELEVANCE);
         $sphinx->setFieldWeights(array('name' => 20, 'description' => 10));
         $sphinx->addQuery($search_query, '*');
         $res = $sphinx->runQueries();
         if (!$res or !array_key_exists('matches', $res[0])) {
             return [];
         }
         $matches_ids = array_merge($matches_ids, array_keys($res[0]['matches']));
     }
     $products_in_purchases_query = \DB::table('products_in_purchase')->select('*');
     if (!empty($matches_ids)) {
         $products_in_purchases_query = $products_in_purchases_query->whereIn('product_id', $matches_ids);
     }
     $products_in_purchases_arr = $products_in_purchases_query->take(12)->get();
     if (empty($products_in_purchases_arr)) {
         return [];
     }
     $products_arr = [];
     foreach ($products_in_purchases_arr as $product_in_purchase_mix) {
         $product = \App\BusinessLogic\Models\Product::find($product_in_purchase_mix->product_id);
         $purchase = \App\BusinessLogic\Models\Purchase::find($product_in_purchase_mix->purchase_id);
         $products_arr[] = new ProductInPurchase($product, $purchase);
     }
     return $products_arr;
 }
 public function getProduct($alias = null)
 {
     if (empty($alias)) {
         return response(view('errors.product_404'), 404);
     }
     $product_id = \App\Helpers\PurchaseHelper::getProductIdByAlias($alias);
     $product_model = \App\BusinessLogic\Models\Product::find($product_id);
     \App\Helpers\Assistant::assertModel($product_model);
     $purchase_id = \App\Helpers\PurchaseHelper::getPurchaseIdByAlias($alias);
     $purchase_model = \App\BusinessLogic\Models\Purchase::find($purchase_id);
     \App\Helpers\Assistant::assertModel($purchase_model);
     //$product_in_purchase = \App\Models\ProductInPurchaseModel::findByProductIdAndByPurchaseId($product_id, $purchase_id);
     $product_in_purchase = new \App\Services\BusinessLogic\Models\ProductInPurchase($product_model, $purchase_model);
     //    \App\Models\AttendanceCounterModel::enrol($product_in_purchase->id, \App\Models\AttendanceCounterModel::TARGET_TYPE_PRODUCT_IN_PURCHASE);
     if (!$product_model or !$purchase_model) {
         return response(view('errors.product_404'), 404);
     }
     return view('product.card', ['product_in_purchase' => $product_in_purchase]);
 }
 /**
  * Изменение закупки
  * @param $purchase_id
  * @param array $fields
  * @return mixed
  */
 public function updatePurchase($purchase_id, array $fields)
 {
     $validator = \Validator::make($fields, ['name' => 'required|max:255', 'description' => 'max:5000', 'minimum_total_amount' => 'required|numeric', 'pricing_grid_id' => 'required|integer', 'expiration_time' => 'required']);
     if ($validator->fails()) {
         return $validator->errors();
     }
     // TODO: сделать проверку прав на изменение закупки
     $purchase = \App\BusinessLogic\Models\Purchase::find($purchase_id);
     if (!$purchase) {
         return ['Purchase not found'];
     }
     $purchase->name = $fields['name'];
     $purchase->description = $fields['description'];
     $purchase->minimum_total_amount = $fields['minimum_total_amount'];
     $purchase->pricing_grid_id = $fields['pricing_grid_id'];
     $purchase->expiration_time = $fields['expiration_time'];
     $purchase->save();
     return $purchase;
 }
 /**
  * Тестирование функции:
  * \App\Models\PurchaseModel::getProductsPricesArr
  */
 public function testGetProductsPricesArr()
 {
     /**
      * @var $purchase \App\BusinessLogic\Models\Purchase
      */
     $purchases = \App\BusinessLogic\Models\Purchase::take(10)->get();
     if (empty($purchases)) {
         return;
     }
     foreach ($purchases as $purchase) {
         $products_prices_arr = $purchase->getProductsInOrdersPricesArr();
         if (empty($products_prices_arr)) {
             continue;
         }
         foreach ($products_prices_arr as $product_prices_arr) {
             //
         }
     }
 }
 /**
  * Возвращает текущую максимальную цену
  * @param $purchase_id
  * @return float
  * @throws \Exception
  */
 public function getCurrentMaxPriceForPurchase($purchase_id)
 {
     $purchase = \App\BusinessLogic\Models\Purchase::find($purchase_id);
     \App\Helpers\Assistant::assertModel($purchase);
     $current_max_pricing_grid_column_id = $purchase->getCurrentMaxPricingGridColumnId();
     $price_obj = \DB::table('prices')->where('product_id', '=', $this->id)->where('column_id', '=', $current_max_pricing_grid_column_id)->first();
     if (!$price_obj) {
         \App\Helpers\Assistant::exception("Не указана цена для продукта {$this->id} в колонке {$current_max_pricing_grid_column_id}");
         return 0;
     }
     return doubleval($price_obj->price);
 }