/** * Generate a unique ref for a new Order. * * @return string */ function generate() { $today = date("Ymd"); $order = Order::where("ref", "like", "{$today}-%")->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"; }); }