| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <?php
-
- namespace App\Http\Controllers;
-
- use App\Commerce\AutomaticDiscountRules;
- use App\Commerce\DiscountCodeRules;
- use App\Models\AutomaticDiscount;
- use App\Models\DiscountCode;
- use App\Models\Product;
- use App\Models\Store;
- use Illuminate\Http\RedirectResponse;
- use Illuminate\Http\Request;
- use Inertia\Inertia;
- use Inertia\Response;
-
- /**
- * The merchant's automatic discounts for one store, listed on the Discounts tab next to the codes.
- */
- class AutomaticDiscountController extends Controller
- {
- public function create(Request $request, string $slug): Response
- {
- $store = $this->ownedStore($request, $slug);
-
- return Inertia::render('stores/discounts/automatic-form', [
- 'store' => ['name' => $store->name, 'slug' => $store->slug],
- 'offer' => null,
- 'products' => $this->productChoices($store),
- ]);
- }
-
- public function store(Request $request, string $slug): RedirectResponse
- {
- $store = $this->ownedStore($request, $slug);
-
- $offer = new AutomaticDiscount;
- $offer->store_id = $store->id;
- AutomaticDiscountRules::fill($offer, $this->validated($request, $store));
- $offer->save();
-
- Inertia::flash('toast', ['type' => 'success', 'message' => __('Automatic discount ":name" created.', ['name' => $offer->name])]);
-
- return to_route('stores.discounts.index', $store->slug);
- }
-
- public function edit(Request $request, string $slug, int $offer): Response
- {
- $store = $this->ownedStore($request, $slug);
-
- return Inertia::render('stores/discounts/automatic-form', [
- 'store' => ['name' => $store->name, 'slug' => $store->slug],
- 'offer' => self::present($this->ownedOffer($store, $offer)),
- 'products' => $this->productChoices($store),
- ]);
- }
-
- public function update(Request $request, string $slug, int $offer): RedirectResponse
- {
- $store = $this->ownedStore($request, $slug);
- $model = $this->ownedOffer($store, $offer);
-
- AutomaticDiscountRules::fill($model, $this->validated($request, $store));
- $model->save();
-
- Inertia::flash('toast', ['type' => 'success', 'message' => __('Automatic discount saved.')]);
-
- return to_route('stores.discounts.index', $store->slug);
- }
-
- public function destroy(Request $request, string $slug, int $offer): RedirectResponse
- {
- $store = $this->ownedStore($request, $slug);
-
- // Orders keep their own copy of the name and the amount.
- $this->ownedOffer($store, $offer)->delete();
-
- Inertia::flash('toast', ['type' => 'success', 'message' => __('Automatic discount deleted.')]);
-
- return to_route('stores.discounts.index', $store->slug);
- }
-
- /**
- * What the dashboard is allowed to see. Amounts are text in ringgit; dates are Malaysian days.
- *
- * @return array<string, mixed>
- */
- public static function present(AutomaticDiscount $offer): array
- {
- $timezone = DiscountCodeRules::TIMEZONE;
-
- return [
- 'id' => $offer->id,
- 'name' => $offer->name,
- 'type' => $offer->type,
- 'value' => $offer->type === DiscountCode::PERCENT ? (string) $offer->value : number_format($offer->value / 100, 2, '.', ''),
- 'description' => $offer->describe(),
- 'minimum_quantity' => $offer->minimum_quantity,
- 'minimum_subtotal' => $offer->minimum_subtotal !== null ? number_format($offer->minimum_subtotal / 100, 2, '.', '') : null,
- 'product_ids' => $offer->product_ids,
- 'starts_on' => $offer->starts_at?->setTimezone($timezone)->format('Y-m-d'),
- 'ends_on' => $offer->ends_at?->setTimezone($timezone)->format('Y-m-d'),
- 'active' => $offer->active,
- 'status' => $offer->status(),
- ];
- }
-
- /**
- * @return array<string, mixed>
- */
- private function validated(Request $request, Store $store): array
- {
- $percent = $request->input('type') === DiscountCode::PERCENT;
-
- return $request->validate(AutomaticDiscountRules::rules($store, $percent), AutomaticDiscountRules::messages($percent));
- }
-
- /**
- * @return list<array{id: int, name: string}>
- */
- private function productChoices(Store $store): array
- {
- return array_values($store->products()
- ->orderBy('name')
- ->get(['id', 'name'])
- ->map(fn (Product $product) => ['id' => $product->id, 'name' => $product->name])
- ->all());
- }
-
- private function ownedStore(Request $request, string $slug): Store
- {
- return $request->user()->stores()->where('slug', $slug)->firstOrFail();
- }
-
- private function ownedOffer(Store $store, int $id): AutomaticDiscount
- {
- return $store->automaticDiscounts()->whereKey($id)->firstOrFail();
- }
- }
|