/** * @param $purchase_id * @return array */ public static function getOrdersCollectionsByPurchaseIdAndByUserId($purchase_id, $user_id) { $orders = \DB::table(\App\Basket::TABLE_PRODUCTS_IN_BASKETS)->select('*')->where('user_id', '=', $user_id)->where('purchase_id', '=', $purchase_id)->get(); $user = \App\User::find($user_id); \App\Helpers\Assistant::assertModel($user); $orders_models = $user->orders()->where('purchase_id', '=', $purchase_id)->get(); if (!$orders_models->count()) { return []; } $orders_collections_arr = []; foreach ($orders_models as $order_model) { $order_collection = new \stdClass(); $order_collection->order_id = $order_model->id; $order_collection->amount = $order_model->amount; $order_collection->purchase_id = $order_model->purchase_id; $order_collection->product_id = $order_model->product_id; $order_collection->product_in_purchase_id = $order_model->product_in_purchase_id; $product_in_purchase_model = \App\Models\ProductInPurchaseModel::find($order_model->product_in_purchase_id); \App\Helpers\Assistant::assertModel($product_in_purchase_model); $current_max_price = $product_in_purchase_model->getMaxPrice(); $order_collection->product_price = $current_max_price; $order_collection->total_price = number_format(\App\Helpers\OrdersHelper::getTotalPrice($current_max_price, $order_model->amount), 2); $order_collection->product_name = $product_in_purchase_model->product->name; $order_collection->product_alias = $product_in_purchase_model->alias(); $orders_collections_arr[] = $order_collection; } return $orders_collections_arr; }
public static function create(array $attributes) { $purchase_id = array_get($attributes, 'purchase_id'); $product_id = array_get($attributes, 'product_id'); if (!$purchase_id or !$product_id) { return null; } $first = \App\Models\ProductInPurchaseModel::where('purchase_id', '=', $purchase_id)->where('product_id', '=', $product_id)->first(); if ($first) { return null; } return parent::create($attributes); }
/** * Добавление указанных продуктов в закупку * @throws \Exception */ public function postAddProducts() { $purchase_id = \Input::get('purchase_id'); $purchase_model = $this->user->purchases()->find($purchase_id); \App\Helpers\Assistant::assertModel($purchase_model); $products_ids_arr = \Input::get('products_ids_arr', []); if (empty($products_ids_arr)) { return ['success' => false, 'message' => 'Список продуктов пуст']; } $products_models = $this->user->products()->whereIn('id', $products_ids_arr)->get(); if (!$products_models->count()) { return ['success' => false, 'message' => 'Продукты не найдены']; } foreach ($products_models as $product_model) { $product_full_data = $product_model->getFullData(); \App\Models\ProductInPurchaseModel::create(['purchase_id' => $purchase_id, 'product_id' => $product_model->id]); } return ['success' => true]; }
<div id="collapseOne-{{ $purchase_model->id }}" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne-{{ $purchase_model->id }}" aria-expanded="true" style="height: 0px;"> <div class="panel-body"> <table class="table"> <thead> <tr> <th>Товар</th> <th>Цена*</th> <th>Количество</th> <th>Сумма</th> <th></th> </tr> </thead> <tbody> @foreach($orders_models_arr_by_purchases_ids_arr[$purchase_id] as $order_model) <?php $product_in_purchase_model = \App\Models\ProductInPurchaseModel::find($order_model->product_in_purchase_id); \App\Helpers\Assistant::assertModel($product_in_purchase_model); $current_max_price = $product_in_purchase_model->getMaxPrice(); ?> <tr id="orderValue-{{ $order_model->id }}"> <td><a href="{{ $product_in_purchase_model->alias() }}">{{ $product_in_purchase_model->product->name }}</a></td> <td class="product-price">{{ $current_max_price }}</td> <td><input type="number" style="width: 50px" data-order-id="{{ $order_model->id }}" value="{{ $order_model->amount }}"></td> <td class="total-price">{{ number_format(\App\Helpers\OrdersHelper::getTotalPrice($current_max_price, $order_model->amount), 2) }}</td> <td style="text-align: right"><button class="btn btn-xs btn-danger" onclick="destroyOrder({{ $order_model->id }});">удалить</button></td> </tr> @endforeach </tbody> <tfoot> <tr> <td></td>
public static function getProductsAvailableForSale($limit = 40, $offset = 0) { $products_in_purchase_models = \App\Models\ProductInPurchaseModel::paginate(30); return $products_in_purchase_models; }