AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Store.php 7.3KB

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