/** * Generate a unique ref for a new Order. * * @return string */ function generate() { $today = date("Ymd"); $order = Order::orderBy("ref", "desc")->first(); if (!$order) { return "{$today}-1"; } list($date, $increment) = explode("-", $order->ref); $increment++; return "{$today}-{$increment}"; }
/** * Assign cart_id and client_id to the new order */ public static function boot() { parent::boot(); Order::creating(function ($order) { $clientId = merx_current_client_id(); if (!$clientId) { throw new NoCurrentClientException(); } $cart = $order->cart; if (!$cart) { $cart = merx_current_cart(); $order->cart()->associate($cart); } static::checkCartIsValid($cart); if (!$order->ref) { // Have to generate a unique ref $order->ref = static::generateRef(); } elseif (Order::where("ref", $order->ref)->count()) { throw new OrderWithThisRefAlreadyExist(); } $order->client_id = $clientId; $order->state = "draft"; }); }
/** @test */ public function we_can_add_multiple_custom_attributes_at_once_to_an_order() { $this->createCartAndClient(); $order = Order::create(["ref" => "123"]); $order->setMultipleCustomAttribute(["custom" => "value", "custom2" => "value2"]); $this->assertEquals("value", $order->customAttribute("custom")); $this->assertEquals("value2", $order->customAttribute("custom2")); $this->seeInDatabase('merx_orders', ["id" => $order->id, "custom_attributes" => json_encode(["custom" => "value", "custom2" => "value2"])]); }
/** @test */ public function we_cant_add_an_item_on_a_closed_cart() { $cart = Cart::create(); $cart->addItem(new CartItem($this->itemAttributes())); $client = $this->loginClient(); Order::create(["cart_id" => $cart->id, "client_id" => $client->id, "ref" => "123"])->update(["state" => "completed"]); $cart = $cart->fresh(); $this->setExpectedException(CartClosedException::class); $cart->addItem(new CartItem($this->itemAttributes())); }
/** * Generate a unique ref for a new Order. * * @return string */ function generate() { $order = Order::orderBy("ref", "desc")->first(); return $order ? $order->ref + 1 : 1; }