/** * @param int $newQuantity * @param ProductOption $option * * @return int */ private function increaseStock(int $newQuantity, ProductOption $option) : int { /** @var StockItem[] $newStock */ $newStock = []; $add = $newQuantity - count($option->availableStock); for ($i = 0; $i < $add; $i++) { $newStock[] = new StockItem(); } $option->stockItems()->saveMany($newStock); return count($newStock); }
/** * Run the migrations. * * @return void */ public function up() { Schema::create(self::TABLE_NAME, function (Blueprint $table) { $table->increments('id'); // A stock item is for a product option. $table->integer('product_option_id')->unsigned()->index(); $table->foreign('product_option_id')->references('id')->on('product_options')->onDelete('cascade'); // A stock item can be allocated to an order item. $table->integer('order_item_id')->unsigned()->index()->nullable(); $table->foreign('order_item_id')->references('id')->on('order_items')->onDelete('cascade'); $table->timestamps(); $table->softDeletes(); }); // Initialise stock items for existing product options. ProductOption::all()->each(function (ProductOption $productOption) { $productOption->stockItems()->save(new StockItem()); }); }
/** * Seed an order. */ private function seedOrder() { $basket = Basket::create(); $address = Address::create(['name' => $this->faker()->name, 'line_one' => $this->faker()->streetAddress, 'line_two' => $this->faker()->streetName, 'city' => $this->faker()->city, 'post_code' => $this->faker()->postcode, 'country_code' => $this->faker()->countryCode]); $basket->address()->associate($address); $basket->save(); $basketItems = []; $this->repeat(function () use($basket, &$basketItems) { $basketItems[] = BasketItem::create(['basket_id' => $basket->id, 'product_option_id' => ProductOption::inRandomOrder()->first()->id]); }, random_int(1, 4)); $order = Order::create(); $order->address()->associate($address); $order->save(); /** @var BasketItem $basketItem */ foreach ($basketItems as $basketItem) { $stockItem = new StockItem(); $basketItem->productOption->stockItems()->save($stockItem); $orderItem = OrderItem::create(['basket_item_id' => $basketItem->id, 'order_id' => $order->id, 'price' => $basketItem->priceAsFloat()]); $orderItem->stockItem()->save($stockItem); } Payment::create(['order_id' => $order->id, 'settlement_id' => PayPalSettlement::create(['payment_id' => $this->faker()->uuid, 'payer_id' => $this->faker()->uuid])->id, 'settlement_type' => PayPalSettlement::class]); }
/** * @param Request $request * * @return array */ public function rules(Request $request) : array { return ProductOption::validationRules((int) $request->route('productId'), (int) $request->route('optionId')); }
/** * @param int $optionId * * @return ProductOption */ public function loadById(int $optionId) { return $this->optionResource->with('product')->where('id', '=', $optionId)->first(); }
/** * @return array */ public function rules() : array { return array_merge(ProductOption::validationRules((int) $this->request->get('option-product')), ['option-product' => 'required|int|exists:products,id']); }