|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Modules\Finance\Http\Controllers; |
| 4 | + |
| 5 | +use App\Http\Controllers\Controller; |
| 6 | +use App\Modules\Finance\Models\Contact; |
| 7 | +use App\Modules\Finance\Models\PriceList; |
| 8 | +use App\Modules\Finance\Models\PriceListItem; |
| 9 | +use App\Modules\Inventory\Models\Product; |
| 10 | +use Illuminate\Http\RedirectResponse; |
| 11 | +use Illuminate\Http\Request; |
| 12 | +use Illuminate\Support\Facades\DB; |
| 13 | +use Inertia\Inertia; |
| 14 | +use Inertia\Response; |
| 15 | + |
| 16 | +class PriceListController extends Controller |
| 17 | +{ |
| 18 | + public function index(): Response |
| 19 | + { |
| 20 | + $this->authorize('viewAny', PriceList::class); |
| 21 | + |
| 22 | + $priceLists = PriceList::withCount(['items', 'contacts']) |
| 23 | + ->orderBy('name') |
| 24 | + ->get(); |
| 25 | + |
| 26 | + return Inertia::render('Finance/PriceLists/Index', [ |
| 27 | + 'priceLists' => $priceLists, |
| 28 | + 'breadcrumbs' => [ |
| 29 | + ['label' => 'Finance'], |
| 30 | + ['label' => 'Price Lists', 'href' => route('finance.price-lists.index')], |
| 31 | + ], |
| 32 | + ]); |
| 33 | + } |
| 34 | + |
| 35 | + public function create(): Response |
| 36 | + { |
| 37 | + $this->authorize('create', PriceList::class); |
| 38 | + |
| 39 | + return Inertia::render('Finance/PriceLists/Create', [ |
| 40 | + 'products' => Product::orderBy('name')->get(['id', 'name', 'sku', 'sale_price']), |
| 41 | + 'breadcrumbs' => [ |
| 42 | + ['label' => 'Finance'], |
| 43 | + ['label' => 'Price Lists', 'href' => route('finance.price-lists.index')], |
| 44 | + ['label' => 'New Price List'], |
| 45 | + ], |
| 46 | + ]); |
| 47 | + } |
| 48 | + |
| 49 | + public function store(Request $request): RedirectResponse |
| 50 | + { |
| 51 | + $this->authorize('create', PriceList::class); |
| 52 | + |
| 53 | + $data = $request->validate([ |
| 54 | + 'name' => 'required|string|max:191', |
| 55 | + 'description' => 'nullable|string', |
| 56 | + 'currency_code' => 'nullable|string|size:3', |
| 57 | + 'discount_percent' => 'nullable|numeric|min:0|max:100', |
| 58 | + 'is_active' => 'boolean', |
| 59 | + 'items' => 'nullable|array', |
| 60 | + 'items.*.product_id' => 'required|exists:products,id', |
| 61 | + 'items.*.unit_price' => 'required|numeric|min:0', |
| 62 | + ]); |
| 63 | + |
| 64 | + $priceList = DB::transaction(function () use ($data, $request) { |
| 65 | + $list = PriceList::create([ |
| 66 | + 'tenant_id' => auth()->user()->tenant_id, |
| 67 | + 'name' => $data['name'], |
| 68 | + 'description' => $data['description'] ?? null, |
| 69 | + 'currency_code' => $data['currency_code'] ?? 'USD', |
| 70 | + 'discount_percent' => $data['discount_percent'] ?? 0, |
| 71 | + 'is_active' => $data['is_active'] ?? true, |
| 72 | + ]); |
| 73 | + |
| 74 | + foreach ($data['items'] ?? [] as $item) { |
| 75 | + PriceListItem::create([ |
| 76 | + 'price_list_id' => $list->id, |
| 77 | + 'product_id' => $item['product_id'], |
| 78 | + 'unit_price' => $item['unit_price'], |
| 79 | + ]); |
| 80 | + } |
| 81 | + |
| 82 | + return $list; |
| 83 | + }); |
| 84 | + |
| 85 | + return redirect()->route('finance.price-lists.show', $priceList) |
| 86 | + ->with('success', 'Price list created.'); |
| 87 | + } |
| 88 | + |
| 89 | + public function show(PriceList $priceList): Response |
| 90 | + { |
| 91 | + $this->authorize('view', $priceList); |
| 92 | + |
| 93 | + $priceList->load(['items.product', 'contacts']); |
| 94 | + |
| 95 | + return Inertia::render('Finance/PriceLists/Show', [ |
| 96 | + 'priceList' => $priceList, |
| 97 | + 'breadcrumbs' => [ |
| 98 | + ['label' => 'Finance'], |
| 99 | + ['label' => 'Price Lists', 'href' => route('finance.price-lists.index')], |
| 100 | + ['label' => $priceList->name], |
| 101 | + ], |
| 102 | + ]); |
| 103 | + } |
| 104 | + |
| 105 | + public function update(Request $request, PriceList $priceList): RedirectResponse |
| 106 | + { |
| 107 | + $this->authorize('update', $priceList); |
| 108 | + |
| 109 | + $data = $request->validate([ |
| 110 | + 'name' => 'required|string|max:191', |
| 111 | + 'description' => 'nullable|string', |
| 112 | + 'currency_code' => 'nullable|string|size:3', |
| 113 | + 'discount_percent' => 'nullable|numeric|min:0|max:100', |
| 114 | + 'is_active' => 'boolean', |
| 115 | + 'items' => 'nullable|array', |
| 116 | + 'items.*.product_id' => 'required|exists:products,id', |
| 117 | + 'items.*.unit_price' => 'required|numeric|min:0', |
| 118 | + ]); |
| 119 | + |
| 120 | + DB::transaction(function () use ($data, $priceList) { |
| 121 | + $priceList->update([ |
| 122 | + 'name' => $data['name'], |
| 123 | + 'description' => $data['description'] ?? null, |
| 124 | + 'currency_code' => $data['currency_code'] ?? 'USD', |
| 125 | + 'discount_percent' => $data['discount_percent'] ?? 0, |
| 126 | + 'is_active' => $data['is_active'] ?? true, |
| 127 | + ]); |
| 128 | + |
| 129 | + // Sync items |
| 130 | + $priceList->items()->delete(); |
| 131 | + foreach ($data['items'] ?? [] as $item) { |
| 132 | + PriceListItem::create([ |
| 133 | + 'price_list_id' => $priceList->id, |
| 134 | + 'product_id' => $item['product_id'], |
| 135 | + 'unit_price' => $item['unit_price'], |
| 136 | + ]); |
| 137 | + } |
| 138 | + }); |
| 139 | + |
| 140 | + return redirect()->route('finance.price-lists.show', $priceList) |
| 141 | + ->with('success', 'Price list updated.'); |
| 142 | + } |
| 143 | + |
| 144 | + public function destroy(PriceList $priceList): RedirectResponse |
| 145 | + { |
| 146 | + $this->authorize('delete', $priceList); |
| 147 | + |
| 148 | + $priceList->delete(); |
| 149 | + |
| 150 | + return redirect()->route('finance.price-lists.index') |
| 151 | + ->with('success', 'Price list deleted.'); |
| 152 | + } |
| 153 | + |
| 154 | + public function priceForContact(Request $request) |
| 155 | + { |
| 156 | + $this->authorize('viewAny', PriceList::class); |
| 157 | + |
| 158 | + $data = $request->validate([ |
| 159 | + 'contact_id' => 'required|exists:contacts,id', |
| 160 | + 'product_id' => 'required|exists:products,id', |
| 161 | + ]); |
| 162 | + |
| 163 | + $contact = Contact::find($data['contact_id']); |
| 164 | + $product = Product::find($data['product_id']); |
| 165 | + |
| 166 | + if (!$contact->price_list_id) { |
| 167 | + return response()->json(['price' => (float) $product->sale_price]); |
| 168 | + } |
| 169 | + |
| 170 | + $price = PriceList::priceFor($contact->price_list_id, $data['product_id'], (float) $product->sale_price); |
| 171 | + return response()->json(['price' => $price]); |
| 172 | + } |
| 173 | +} |
0 commit comments