AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Store.php 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. namespace App\Models;
  3. use App\Enums\StoreCategory;
  4. use App\Enums\StoreLanguage;
  5. use App\Enums\StoreStatus;
  6. use Carbon\CarbonInterface;
  7. use Database\Factories\StoreFactory;
  8. use Illuminate\Database\Eloquent\Attributes\Fillable;
  9. use Illuminate\Database\Eloquent\Builder;
  10. use Illuminate\Database\Eloquent\Factories\HasFactory;
  11. use Illuminate\Database\Eloquent\Model;
  12. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  13. use Illuminate\Database\Eloquent\Relations\HasMany;
  14. use Illuminate\Database\Eloquent\Relations\HasOne;
  15. use Illuminate\Support\Carbon;
  16. use Illuminate\Support\Str;
  17. /**
  18. * A merchant's online store. Every store-related table points back here with `store_id`.
  19. *
  20. * @property int $id
  21. * @property int $user_id
  22. * @property string $name
  23. * @property string $slug
  24. * @property StoreCategory $category
  25. * @property string $description
  26. * @property StoreLanguage $language
  27. * @property StoreStatus $status
  28. * @property array<string, mixed>|null $plan
  29. * @property string|null $whatsapp_number
  30. * @property string|null $contact_email Where shoppers' replies to the store's emails go
  31. * @property int $shipping_west
  32. * @property int $shipping_east
  33. * @property int|null $free_shipping_over
  34. * @property array{name: string, phone: string, line1: string, line2: string|null, city: string, postcode: string, state: string}|null $pickup_address Where couriers collect parcels
  35. * @property int $parcel_weight The usual parcel's weight in grams
  36. * @property array{length: int, width: int, height: int}|null $parcel_size The usual parcel's size in cm
  37. * @property bool $announcement_enabled
  38. * @property list<string>|null $announcement_messages The merchant's own announcement lines
  39. * @property array<string, mixed>|null $published_plan
  40. * @property CarbonInterface|null $published_at
  41. * @property Carbon|null $created_at
  42. * @property Carbon|null $updated_at
  43. */
  44. #[Fillable(['name', 'category', 'description', 'language'])]
  45. class Store extends Model
  46. {
  47. /** @use HasFactory<StoreFactory> */
  48. use HasFactory;
  49. /**
  50. * Slugs that can never be used, because they will clash with our own subdomains.
  51. *
  52. * @var list<string>
  53. */
  54. public const array RESERVED_SLUGS = [
  55. 'www', 'app', 'api', 'admin', 'dashboard', 'mail', 'store', 'stores', 'create',
  56. 'localhost', 'static', 'assets', 'cdn', 'help', 'support', 'status', 'blog', 'billing', 'login', 'register',
  57. ];
  58. /**
  59. * Default values for new stores.
  60. *
  61. * @var array<string, string|int>
  62. */
  63. protected $attributes = [
  64. 'language' => 'en',
  65. 'status' => 'draft',
  66. // Starting shipping fees in sen, so a new store has sensible numbers before the merchant changes them.
  67. 'shipping_west' => 800,
  68. 'shipping_east' => 1500,
  69. ];
  70. protected static function booted(): void
  71. {
  72. static::creating(function (Store $store) {
  73. if (blank($store->slug)) {
  74. $store->slug = static::uniqueSlug($store->name);
  75. }
  76. });
  77. }
  78. /**
  79. * Build a web-safe slug from a store name that no other store is using.
  80. */
  81. public static function uniqueSlug(string $name): string
  82. {
  83. $base = Str::slug($name) ?: 'store';
  84. $slug = $base;
  85. $suffix = 2;
  86. while (in_array($slug, self::RESERVED_SLUGS, true) || static::query()->where('slug', $slug)->exists()) {
  87. $slug = $base.'-'.$suffix++;
  88. }
  89. return $slug;
  90. }
  91. public function getRouteKeyName(): string
  92. {
  93. return 'slug';
  94. }
  95. /**
  96. * @return BelongsTo<User, $this>
  97. */
  98. public function user(): BelongsTo
  99. {
  100. return $this->belongsTo(User::class);
  101. }
  102. /**
  103. * Stores that shoppers can open.
  104. *
  105. * @param Builder<Store> $query
  106. */
  107. public function scopePublished(Builder $query): void
  108. {
  109. $query->where('status', StoreStatus::Published)->whereNotNull('published_plan');
  110. }
  111. public function isPublished(): bool
  112. {
  113. return $this->status === StoreStatus::Published && $this->published_plan !== null;
  114. }
  115. /**
  116. * True when the working design differs from the copy shoppers see.
  117. */
  118. public function hasUnpublishedChanges(): bool
  119. {
  120. return $this->isPublished() && $this->plan != $this->published_plan;
  121. }
  122. /**
  123. * @return HasMany<Product, $this>
  124. */
  125. public function products(): HasMany
  126. {
  127. return $this->hasMany(Product::class);
  128. }
  129. /**
  130. * Groups of products, such as "Sun care".
  131. *
  132. * @return HasMany<Collection, $this>
  133. */
  134. public function collections(): HasMany
  135. {
  136. return $this->hasMany(Collection::class)->orderBy('position')->orderBy('id');
  137. }
  138. /**
  139. * Photos uploaded for the design, such as a hero banner.
  140. *
  141. * @return HasMany<StoreImage, $this>
  142. */
  143. public function images(): HasMany
  144. {
  145. return $this->hasMany(StoreImage::class);
  146. }
  147. /**
  148. * @return HasMany<DiscountCode, $this>
  149. */
  150. public function discountCodes(): HasMany
  151. {
  152. return $this->hasMany(DiscountCode::class);
  153. }
  154. /**
  155. * @return HasMany<AutomaticDiscount, $this>
  156. */
  157. public function automaticDiscounts(): HasMany
  158. {
  159. return $this->hasMany(AutomaticDiscount::class);
  160. }
  161. /**
  162. * The EasyParcel account the merchant connected, if any.
  163. *
  164. * @return HasOne<EasyParcelAccount, $this>
  165. */
  166. public function easyparcel(): HasOne
  167. {
  168. return $this->hasOne(EasyParcelAccount::class);
  169. }
  170. /**
  171. * The merchant's own Stripe account, where shoppers' payments go.
  172. *
  173. * @return HasOne<StripeAccount, $this>
  174. */
  175. public function stripeAccount(): HasOne
  176. {
  177. return $this->hasOne(StripeAccount::class);
  178. }
  179. /**
  180. * @return HasMany<Order, $this>
  181. */
  182. public function orders(): HasMany
  183. {
  184. return $this->hasMany(Order::class);
  185. }
  186. /**
  187. * The AI jobs run for this store, newest first.
  188. *
  189. * @return HasMany<StoreGeneration, $this>
  190. */
  191. public function generations(): HasMany
  192. {
  193. return $this->hasMany(StoreGeneration::class)->latest('id');
  194. }
  195. /**
  196. * Get the attributes that should be cast.
  197. *
  198. * @return array<string, string>
  199. */
  200. protected function casts(): array
  201. {
  202. return [
  203. 'category' => StoreCategory::class,
  204. 'language' => StoreLanguage::class,
  205. 'status' => StoreStatus::class,
  206. 'plan' => 'array',
  207. 'published_plan' => 'array',
  208. 'published_at' => 'datetime',
  209. 'shipping_west' => 'integer',
  210. 'shipping_east' => 'integer',
  211. 'free_shipping_over' => 'integer',
  212. 'announcement_enabled' => 'boolean',
  213. 'announcement_messages' => 'array',
  214. 'pickup_address' => 'array',
  215. 'parcel_weight' => 'integer',
  216. 'parcel_size' => 'array',
  217. ];
  218. }
  219. }