AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Store.php 6.5KB

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