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.

StoreSettingsController.php 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Commerce\MalaysianStates;
  4. use App\Commerce\Shipping\EasyParcel\EasyParcel;
  5. use App\Commerce\Shipping\EasyParcel\EasyParcelException;
  6. use App\Models\EasyParcelAccount;
  7. use App\Models\Store;
  8. use App\Storefront\Announcements;
  9. use App\Support\MalaysianPhone;
  10. use Illuminate\Http\RedirectResponse;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Validation\Rule;
  13. use Illuminate\Validation\ValidationException;
  14. use Inertia\Inertia;
  15. use Inertia\Response;
  16. /**
  17. * Store-wide settings that are not part of the design: WhatsApp number, shipping fees, EasyParcel and the announcement bar.
  18. */
  19. class StoreSettingsController extends Controller
  20. {
  21. public function edit(Request $request, string $slug, EasyParcel $easyparcel): Response
  22. {
  23. $store = $this->ownedStore($request, $slug);
  24. $account = $store->easyparcel;
  25. $pickup = $store->pickup_address;
  26. $size = $store->parcel_size ?? OrderController::DEFAULT_SIZE;
  27. return Inertia::render('stores/settings', [
  28. 'store' => ['name' => $store->name, 'slug' => $store->slug],
  29. 'settings' => [
  30. 'whatsapp_number' => $store->whatsapp_number,
  31. 'shipping_west' => self::ringgit($store->shipping_west),
  32. 'shipping_east' => self::ringgit($store->shipping_east),
  33. 'free_shipping_over' => $store->free_shipping_over !== null ? self::ringgit($store->free_shipping_over) : null,
  34. 'announcement_enabled' => $store->announcement_enabled,
  35. // One line per message, the way the form edits them.
  36. 'announcement_messages' => implode("\n", $store->announcement_messages ?? []),
  37. 'pickup' => [
  38. 'name' => $pickup['name'] ?? '',
  39. 'phone' => $pickup['phone'] ?? '',
  40. 'line1' => $pickup['line1'] ?? '',
  41. 'line2' => $pickup['line2'] ?? '',
  42. 'city' => $pickup['city'] ?? '',
  43. 'postcode' => $pickup['postcode'] ?? '',
  44. 'state' => $pickup['state'] ?? '',
  45. ],
  46. 'parcel_weight' => rtrim(rtrim(number_format($store->parcel_weight / 1000, 2, '.', ''), '0'), '.'),
  47. 'parcel_length' => (string) $size['length'],
  48. 'parcel_width' => (string) $size['width'],
  49. 'parcel_height' => (string) $size['height'],
  50. ],
  51. 'states' => MalaysianStates::ALL,
  52. 'easyparcel' => [
  53. // False until the app's own EasyParcel keys are in .env.local.
  54. 'available' => $easyparcel->isConfigured(),
  55. 'connected' => $account !== null,
  56. 'account_name' => $account?->account_name,
  57. 'redirect_uri' => $easyparcel->redirectUri(),
  58. ],
  59. // Asked from EasyParcel after the page shows, so a slow answer never holds up Settings.
  60. 'easyparcelBalance' => $account !== null ? Inertia::defer(fn () => self::balance($easyparcel, $account)) : null,
  61. // What the store adds by itself today, so the merchant sees the whole bar.
  62. 'automaticAnnouncements' => Announcements::automatic($store, $store->automaticDiscounts()->where('active', true)->orderByDesc('value')->get()->filter->isLive()->values()),
  63. ]);
  64. }
  65. public function update(Request $request, string $slug): RedirectResponse
  66. {
  67. $store = $this->ownedStore($request, $slug);
  68. $validated = $request->validate([
  69. 'whatsapp_number' => ['nullable', 'string', 'max:30'],
  70. 'shipping_west' => ['required', 'numeric', 'decimal:0,2', 'min:0', 'max:9999.99'],
  71. 'shipping_east' => ['required', 'numeric', 'decimal:0,2', 'min:0', 'max:9999.99'],
  72. 'free_shipping_over' => ['nullable', 'numeric', 'decimal:0,2', 'min:0', 'max:999999.99'],
  73. 'announcement_enabled' => ['boolean'],
  74. // One message per line.
  75. 'announcement_messages' => ['nullable', 'string', 'max:400'],
  76. // Where couriers collect parcels. All or nothing.
  77. 'pickup_name' => ['nullable', 'string', 'max:60'],
  78. 'pickup_phone' => ['nullable', 'string', 'max:30', 'required_with:pickup_line1'],
  79. 'pickup_line1' => ['nullable', 'string', 'max:100', 'required_with:pickup_name,pickup_phone,pickup_city,pickup_postcode'],
  80. 'pickup_line2' => ['nullable', 'string', 'max:100'],
  81. 'pickup_city' => ['nullable', 'string', 'max:60', 'required_with:pickup_line1'],
  82. 'pickup_postcode' => ['nullable', 'regex:/^\d{5}$/', 'required_with:pickup_line1'],
  83. 'pickup_state' => ['nullable', Rule::in(MalaysianStates::ALL), 'required_with:pickup_line1'],
  84. 'parcel_weight' => ['nullable', 'numeric', 'min:0.01', 'max:70'],
  85. 'parcel_length' => ['nullable', 'integer', 'min:1', 'max:200'],
  86. 'parcel_width' => ['nullable', 'integer', 'min:1', 'max:200'],
  87. 'parcel_height' => ['nullable', 'integer', 'min:1', 'max:200'],
  88. ], [
  89. 'pickup_postcode.regex' => __('Enter a 5-digit postcode.'),
  90. 'pickup_line1.required_with' => __('Enter the street address.'),
  91. 'pickup_phone.required_with' => __('Enter a phone number the courier can call.'),
  92. 'pickup_city.required_with' => __('Enter the town or city.'),
  93. 'pickup_postcode.required_with' => __('Enter the postcode.'),
  94. 'pickup_state.required_with' => __('Choose the state.'),
  95. ]);
  96. $pickup = null;
  97. if (filled($validated['pickup_line1'] ?? null)) {
  98. $phone = MalaysianPhone::normalize($validated['pickup_phone'] ?? null);
  99. if ($phone === null) {
  100. throw ValidationException::withMessages(['pickup_phone' => __('Enter a Malaysian phone number, such as 012-345 6789.')]);
  101. }
  102. $pickup = [
  103. 'name' => trim((string) ($validated['pickup_name'] ?? '')) ?: $store->name,
  104. 'phone' => $phone,
  105. 'line1' => trim($validated['pickup_line1']),
  106. 'line2' => filled($validated['pickup_line2'] ?? null) ? trim($validated['pickup_line2']) : null,
  107. 'city' => trim($validated['pickup_city']),
  108. 'postcode' => $validated['pickup_postcode'],
  109. 'state' => $validated['pickup_state'],
  110. ];
  111. }
  112. $messages = array_values(array_filter(array_map('trim', preg_split('/\R/', (string) ($validated['announcement_messages'] ?? '')) ?: [])));
  113. if (count($messages) > Announcements::MAX_OWN) {
  114. throw ValidationException::withMessages(['announcement_messages' => __('Write at most :count messages, one per line.', ['count' => Announcements::MAX_OWN])]);
  115. }
  116. foreach ($messages as $message) {
  117. if (mb_strlen($message) > Announcements::MAX_LENGTH) {
  118. throw ValidationException::withMessages(['announcement_messages' => __('Keep each message to :count characters or fewer.', ['count' => Announcements::MAX_LENGTH])]);
  119. }
  120. }
  121. $whatsapp = null;
  122. if (filled($validated['whatsapp_number'] ?? null)) {
  123. $whatsapp = MalaysianPhone::normalize($validated['whatsapp_number']);
  124. if ($whatsapp === null) {
  125. throw ValidationException::withMessages([
  126. 'whatsapp_number' => __('Enter a Malaysian mobile number, such as 012-345 6789.'),
  127. ]);
  128. }
  129. }
  130. $store->whatsapp_number = $whatsapp;
  131. $store->shipping_west = self::sen($validated['shipping_west']);
  132. $store->shipping_east = self::sen($validated['shipping_east']);
  133. $store->free_shipping_over = filled($validated['free_shipping_over'] ?? null) ? self::sen($validated['free_shipping_over']) : null;
  134. $store->announcement_enabled = (bool) ($validated['announcement_enabled'] ?? false);
  135. $store->announcement_messages = $messages === [] ? null : $messages;
  136. $store->pickup_address = $pickup;
  137. // The usual parcel. Left as it was when the form does not send it.
  138. if (filled($validated['parcel_weight'] ?? null)) {
  139. $store->parcel_weight = (int) round(((float) $validated['parcel_weight']) * 1000);
  140. }
  141. if (filled($validated['parcel_length'] ?? null) && filled($validated['parcel_width'] ?? null) && filled($validated['parcel_height'] ?? null)) {
  142. $store->parcel_size = [
  143. 'length' => (int) $validated['parcel_length'],
  144. 'width' => (int) $validated['parcel_width'],
  145. 'height' => (int) $validated['parcel_height'],
  146. ];
  147. }
  148. $store->save();
  149. Inertia::flash('toast', ['type' => 'success', 'message' => __('Settings saved.')]);
  150. return to_route('stores.settings', $store->slug);
  151. }
  152. /**
  153. * @return array{amount: float|null, error: string|null}
  154. */
  155. private static function balance(EasyParcel $easyparcel, EasyParcelAccount $account): array
  156. {
  157. try {
  158. $sen = $easyparcel->balance($account);
  159. return ['amount' => $sen !== null ? $sen / 100 : null, 'error' => null];
  160. } catch (EasyParcelException $e) {
  161. return ['amount' => null, 'error' => $e->getMessage()];
  162. }
  163. }
  164. private static function sen(mixed $ringgit): int
  165. {
  166. return (int) round(((float) $ringgit) * 100);
  167. }
  168. private static function ringgit(int $sen): string
  169. {
  170. return number_format($sen / 100, 2, '.', '');
  171. }
  172. private function ownedStore(Request $request, string $slug): Store
  173. {
  174. return $request->user()->stores()->where('slug', $slug)->firstOrFail();
  175. }
  176. }