AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

AutomaticDiscountController.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Commerce\AutomaticDiscountRules;
  4. use App\Commerce\DiscountCodeRules;
  5. use App\Models\AutomaticDiscount;
  6. use App\Models\DiscountCode;
  7. use App\Models\Product;
  8. use App\Models\Store;
  9. use Illuminate\Http\RedirectResponse;
  10. use Illuminate\Http\Request;
  11. use Inertia\Inertia;
  12. use Inertia\Response;
  13. /**
  14. * The merchant's automatic discounts for one store, listed on the Discounts tab next to the codes.
  15. */
  16. class AutomaticDiscountController extends Controller
  17. {
  18. public function create(Request $request, string $slug): Response
  19. {
  20. $store = $this->ownedStore($request, $slug);
  21. return Inertia::render('stores/discounts/automatic-form', [
  22. 'store' => ['name' => $store->name, 'slug' => $store->slug],
  23. 'offer' => null,
  24. 'products' => $this->productChoices($store),
  25. ]);
  26. }
  27. public function store(Request $request, string $slug): RedirectResponse
  28. {
  29. $store = $this->ownedStore($request, $slug);
  30. $offer = new AutomaticDiscount;
  31. $offer->store_id = $store->id;
  32. AutomaticDiscountRules::fill($offer, $this->validated($request, $store));
  33. $offer->save();
  34. Inertia::flash('toast', ['type' => 'success', 'message' => __('Automatic discount ":name" created.', ['name' => $offer->name])]);
  35. return to_route('stores.discounts.index', $store->slug);
  36. }
  37. public function edit(Request $request, string $slug, int $offer): Response
  38. {
  39. $store = $this->ownedStore($request, $slug);
  40. return Inertia::render('stores/discounts/automatic-form', [
  41. 'store' => ['name' => $store->name, 'slug' => $store->slug],
  42. 'offer' => self::present($this->ownedOffer($store, $offer)),
  43. 'products' => $this->productChoices($store),
  44. ]);
  45. }
  46. public function update(Request $request, string $slug, int $offer): RedirectResponse
  47. {
  48. $store = $this->ownedStore($request, $slug);
  49. $model = $this->ownedOffer($store, $offer);
  50. AutomaticDiscountRules::fill($model, $this->validated($request, $store));
  51. $model->save();
  52. Inertia::flash('toast', ['type' => 'success', 'message' => __('Automatic discount saved.')]);
  53. return to_route('stores.discounts.index', $store->slug);
  54. }
  55. public function destroy(Request $request, string $slug, int $offer): RedirectResponse
  56. {
  57. $store = $this->ownedStore($request, $slug);
  58. // Orders keep their own copy of the name and the amount.
  59. $this->ownedOffer($store, $offer)->delete();
  60. Inertia::flash('toast', ['type' => 'success', 'message' => __('Automatic discount deleted.')]);
  61. return to_route('stores.discounts.index', $store->slug);
  62. }
  63. /**
  64. * What the dashboard is allowed to see. Amounts are text in ringgit; dates are Malaysian days.
  65. *
  66. * @return array<string, mixed>
  67. */
  68. public static function present(AutomaticDiscount $offer): array
  69. {
  70. $timezone = DiscountCodeRules::TIMEZONE;
  71. return [
  72. 'id' => $offer->id,
  73. 'name' => $offer->name,
  74. 'type' => $offer->type,
  75. 'value' => $offer->type === DiscountCode::PERCENT ? (string) $offer->value : number_format($offer->value / 100, 2, '.', ''),
  76. 'description' => $offer->describe(),
  77. 'minimum_quantity' => $offer->minimum_quantity,
  78. 'minimum_subtotal' => $offer->minimum_subtotal !== null ? number_format($offer->minimum_subtotal / 100, 2, '.', '') : null,
  79. 'product_ids' => $offer->product_ids,
  80. 'starts_on' => $offer->starts_at?->setTimezone($timezone)->format('Y-m-d'),
  81. 'ends_on' => $offer->ends_at?->setTimezone($timezone)->format('Y-m-d'),
  82. 'active' => $offer->active,
  83. 'status' => $offer->status(),
  84. ];
  85. }
  86. /**
  87. * @return array<string, mixed>
  88. */
  89. private function validated(Request $request, Store $store): array
  90. {
  91. $percent = $request->input('type') === DiscountCode::PERCENT;
  92. return $request->validate(AutomaticDiscountRules::rules($store, $percent), AutomaticDiscountRules::messages($percent));
  93. }
  94. /**
  95. * @return list<array{id: int, name: string}>
  96. */
  97. private function productChoices(Store $store): array
  98. {
  99. return array_values($store->products()
  100. ->orderBy('name')
  101. ->get(['id', 'name'])
  102. ->map(fn (Product $product) => ['id' => $product->id, 'name' => $product->name])
  103. ->all());
  104. }
  105. private function ownedStore(Request $request, string $slug): Store
  106. {
  107. return $request->user()->stores()->where('slug', $slug)->firstOrFail();
  108. }
  109. private function ownedOffer(Store $store, int $id): AutomaticDiscount
  110. {
  111. return $store->automaticDiscounts()->whereKey($id)->firstOrFail();
  112. }
  113. }