public function test_deleting_a_selection_deletes_associated_inventories() { $selection = Factory::create(new Selection()); $inventory = Factory::create(new Inventory()); $selection->inventories()->attach($inventory); $selection->delete(); $this->assertFalse(Inventory::whereId($inventory->id)->exists()); }
public function test_where_has_selections_scope() { $foo = Factory::create(new Inventory()); $s1 = Factory::create(new Selection()); $s2 = Factory::create(new Selection()); $signature = [$s1->id, $s2->id]; $bar = Factory::make(new Inventory()); $bar->setSelections($signature); $bar->save(); $this->assertEquals($foo->id, Inventory::whereHasSelections([])->first()->id); $this->assertEquals(1, Inventory::whereHasSelections([])->count()); $this->assertEquals($bar->id, Inventory::whereHasSelections($signature)->first()->id); $this->assertEquals(1, Inventory::whereHasSelections($signature)->count()); }
public function run() { //disable foreign key check for this connection before running seeders DB::statement('SET FOREIGN_KEY_CHECKS=0;'); /** * CATEGORY SEEDS */ $i = 3; foreach (['Boards', 'Shirts', 'Hoodies', 'Stickers', 'Jackets'] as $category) { Category::create(['position' => $i, 'name' => $category, 'slug' => strtolower($category), 'is_visible' => 1, 'is_active' => 1]); $i++; } foreach (['Winter', 'DVDs'] as $category) { Category::create(['position' => $i, 'name' => $category, 'slug' => strtolower($category), 'is_visible' => 0, 'is_active' => 1]); $i++; } Category::create(['position' => $i, 'name' => 'Hats', 'slug' => 'hats', 'is_visible' => 1, 'is_active' => 0]); /** * PRODUCT SEEDS */ $colors = ['red', 'blue', 'green', 'black', 'orange', 'yellow', 'purple', 'white']; $products = ['shirt', 'hat', 'board', 'sticker']; $seeds = []; foreach ($products as $product) { foreach ($colors as $color) { $seeds[] = $color . ' ' . $product; } } shuffle($seeds); Product::truncate(); foreach ($seeds as $seed) { $product = Product::create(['name' => $seed, 'slug' => str_replace(' ', '-', $seed), 'full_price' => rand(10, 20), 'description' => "Some awesome {$seed}... You should totaly buy it.", 'is_active' => rand(0, 10) > 0 ? 1 : 0, 'is_visible' => rand(0, 10) > 0 ? 1 : 0]); if (strpos($seed, 'board') !== FALSE) { $product->categories()->attach(3); } elseif (strpos($seed, 'hat') !== FALSE) { $product->categories()->attach(10); } elseif (strpos($seed, 'shirt') !== FALSE) { $product->categories()->attach(4); } elseif (strpos($seed, 'sticker') !== FALSE) { $product->categories()->attach(6); } $small = Inventory::create(['product_id' => $product->id, 'name' => 'Small', 'quantity' => rand(0, 2), 'is_active' => rand(0, 10) > 0 ? 1 : 0]); $medium = Inventory::create(['product_id' => $product->id, 'name' => 'Medium', 'quantity' => rand(0, 2), 'position' => 1, 'is_active' => rand(0, 10) > 0 ? 1 : 0]); $large = Inventory::create(['product_id' => $product->id, 'name' => 'Large', 'quantity' => rand(0, 2), 'position' => 2, 'is_active' => rand(0, 10) > 0 ? 1 : 0]); } /** * DISCOUNT SEEDS */ // Demo category discount DB::table('bedard_shop_discounts')->insert(['name' => 'Category Discount', 'amount' => rand(10, 25), 'is_percentage' => 1]); DB::table('bedard_shop_discountables')->insert(['discount_id' => 1, 'discountable_id' => rand(3, 6), 'discountable_type' => 'Bedard\\Shop\\Models\\Category']); DB::table('bedard_shop_discounts')->insert(['name' => 'Product Discount', 'amount' => rand(5, 8), 'is_percentage' => 0]); DB::table('bedard_shop_discountables')->insert(['discount_id' => 2, 'discountable_id' => rand(1, 20), 'discountable_type' => 'Bedard\\Shop\\Models\\Product']); /** * PROMO CODE */ Coupon::create(['name' => 'Foo', 'message' => 'Thanks for entering "foo".', 'amount' => rand(10, 20), 'is_percentage' => 1, 'cart_value' => rand(20, 50)]); // Enable foreign keys DB::statement('SET FOREIGN_KEY_CHECKS=1;'); /** * Mock carts */ $fnames = ['John', 'Mary', 'Alex', 'Mark', 'Sally']; $lnames = ['Smith', 'Johnson', 'Jones', 'Doe']; for ($i = 0; $i < 30; $i++) { $cart = Cart::create([]); $inventories = []; for ($j = 0; $j < rand(5, 15); $j++) { $inventories[] = rand(1, 95); } foreach ($inventories as $inventory) { $item = CartItem::firstOrCreate(['cart_id' => $cart->id, 'inventory_id' => $inventory, 'quantity' => rand(1, 2)]); } $first = $fnames[rand(0, 4)]; $last = $lnames[rand(0, 3)]; $customer = Customer::firstOrCreate(['first_name' => $first, 'last_name' => $last, 'email' => strtolower("{$first}.{$last}@example.com")]); $order = Order::create(['created_at' => Carbon::now()->subDays(rand(1, 50))]); $order->customer_id = $customer->id; $order->cart_id = $cart->id; $order->amount = $cart->total; $cart->markAsComplete($order); $order->shipping_address = ['first_name' => $first, 'last_name' => $last, 'address1' => '123 Foo Street', 'city' => 'Beverly Hills', 'state' => 'CA', 'postcode' => '90210', 'country' => 'US']; $order->gateway = 'PayPal_Express'; $order->gateway_code = 'FAKE-PAYPAL-ID'; $order->save(); } }
/** * Creates / updates inventories * @param array $inventories */ private function saveInventories($inventories) { foreach ($inventories as $i => $inventory) { // Load the inventory model via Find, First, or New $inventoryModel = $inventory['id'] ? Inventory::find($inventory['id']) : Inventory::firstOrNew(['product_id' => $this->productId, 'name' => $inventory['name']]); // Something went wrong finding / creating the model if (!$inventoryModel) { return FALSE; } // Update model values $inventoryModel->product_id = $this->productId; $inventoryModel->name = $inventory['name']; $inventoryModel->quantity = $inventory['quantity']; $inventoryModel->modifier = $inventory['modifier']; $inventoryModel->is_active = $inventory['is_active']; $inventoryModel->position = $i; // Attempt to save the model if (!$inventoryModel->save()) { return FALSE; } } }
/** * Loads a product by inventory ID or product slug * @param integer $inventoryId * @param string $slug * @return Bedard\Shop\Models\Inventory */ private function loadInventory($inventoryId, $slug) { // If an inventory ID was passed in, find and return it if ($inventoryId) { return Inventory::inStock()->find($inventoryId); } // Otherwise, check the product and return it's first inventory $product = Product::where('slug', $slug)->with('inventories')->whereHas('inventories', function ($inventory) { $inventory->inStock(); })->isActive()->first(); // Grab the first inventory foreach ($product->inventories as $inventory) { return $inventory; } }
/** * Save an inventory * * @param integer $id * @param string $sessionKey * @return array */ protected function saveInventory($id, $sessionKey) { $inventory = Inventory::findOrNew($id); $inventory->product_id = $this->model->id; $inventory->sku = input('sku'); $inventory->quantity = empty(input('quantity')) ? 0 : input('quantity'); $inventory->setSelections(input('selections'), $sessionKey); $message = $this->getLangString($inventory); $inventory->save(); if (!$this->model->id) { $this->model->inventories()->add($inventory, $sessionKey); } $name = Lang::get('bedard.shop::lang.inventory.resource_singular'); Flash::success(Lang::get($message, ['name' => $name])); return ['target' => $this->makePartial('inventories_li', ['inventory' => $inventory])]; }
/** * Returns an inventory * * @param integer $productId ID of product being added to cart * @param array $selectionIds Array of selection IDs to identify inventory * @throws \Illuminate\Database\Eloquent\ModelNotFoundException * @return \Bedard\Shop\Models\Inventory */ public function getInventory($productId, $selectionIds) { return Inventory::isEnabled()->whereProductId($productId)->whereHasSelections($selectionIds)->firstOrFail(); }
/** * Left joins the inventory count * * @param \October\Rain\Database\Builder $query * @return \October\Rain\Database\Builder */ public function scopeJoinInventory($query) { $alias = 'inventories'; $grammar = $query->getQuery()->getGrammar(); $subquery = Inventory::addSelect('bedard_shop_inventories.product_id')->selectRaw('SUM(' . $grammar->wrap('bedard_shop_inventories.quantity') . ') as ' . $grammar->wrap('inventory'))->groupBy('bedard_shop_inventories.product_id'); return $query->addSelect($alias . '.inventory')->joinSubquery($subquery, $alias, 'bedard_shop_products.id', '=', $alias . '.product_id', 'leftJoin'); }