@@ -21,7 +21,7 @@ public function index(): Response
2121
2222 $ priceLists = PriceList::withCount (['items ' , 'contacts ' ])
2323 ->orderBy ('name ' )
24- ->get ( );
24+ ->paginate ( 15 );
2525
2626 return Inertia::render ('Finance/PriceLists/Index ' , [
2727 'priceLists ' => $ priceLists ,
@@ -53,29 +53,46 @@ public function store(Request $request): RedirectResponse
5353 $ data = $ request ->validate ([
5454 'name ' => 'required|string|max:191 ' ,
5555 'description ' => 'nullable|string ' ,
56- 'currency_code ' => 'nullable |string|size:3 ' ,
56+ 'currency_code ' => 'required |string|size:3 ' ,
5757 'discount_percent ' => 'nullable|numeric|min:0|max:100 ' ,
5858 'is_active ' => 'boolean ' ,
59+ 'is_default ' => 'boolean ' ,
60+ 'valid_from ' => 'nullable|date ' ,
61+ 'valid_to ' => 'nullable|date ' ,
5962 'items ' => 'nullable|array ' ,
6063 'items.*.product_id ' => 'required|exists:products,id ' ,
6164 'items.*.unit_price ' => 'required|numeric|min:0 ' ,
6265 ]);
6366
64- $ priceList = DB ::transaction (function () use ($ data , $ request ) {
67+ $ tenantId = auth ()->user ()->tenant_id ;
68+
69+ $ priceList = DB ::transaction (function () use ($ data , $ tenantId ) {
70+ // If setting as default, clear existing defaults
71+ if (!empty ($ data ['is_default ' ])) {
72+ PriceList::where ('tenant_id ' , $ tenantId )
73+ ->where ('is_default ' , true )
74+ ->update (['is_default ' => false ]);
75+ }
76+
6577 $ list = PriceList::create ([
66- 'tenant_id ' => auth ()-> user ()-> tenant_id ,
78+ 'tenant_id ' => $ tenantId ,
6779 'name ' => $ data ['name ' ],
6880 'description ' => $ data ['description ' ] ?? null ,
69- 'currency_code ' => $ data ['currency_code ' ] ?? ' USD ' ,
81+ 'currency_code ' => $ data ['currency_code ' ],
7082 'discount_percent ' => $ data ['discount_percent ' ] ?? 0 ,
7183 'is_active ' => $ data ['is_active ' ] ?? true ,
84+ 'is_default ' => $ data ['is_default ' ] ?? false ,
85+ 'valid_from ' => $ data ['valid_from ' ] ?? null ,
86+ 'valid_to ' => $ data ['valid_to ' ] ?? null ,
7287 ]);
7388
7489 foreach ($ data ['items ' ] ?? [] as $ item ) {
7590 PriceListItem::create ([
91+ 'tenant_id ' => $ tenantId ,
7692 'price_list_id ' => $ list ->id ,
7793 'product_id ' => $ item ['product_id ' ],
7894 'unit_price ' => $ item ['unit_price ' ],
95+ 'min_quantity ' => $ item ['min_quantity ' ] ?? 1 ,
7996 ]);
8097 }
8198
@@ -90,10 +107,11 @@ public function show(PriceList $priceList): Response
90107 {
91108 $ this ->authorize ('view ' , $ priceList );
92109
93- $ priceList ->load ([ ' items. product ', ' contacts ' ] );
110+ $ items = $ priceList ->items ()-> with ( ' product ')-> paginate ( 15 );
94111
95112 return Inertia::render ('Finance/PriceLists/Show ' , [
96- 'priceList ' => $ priceList ,
113+ 'priceList ' => $ priceList ->load ('contacts ' ),
114+ 'items ' => $ items ,
97115 'breadcrumbs ' => [
98116 ['label ' => 'Finance ' ],
99117 ['label ' => 'Price Lists ' , 'href ' => route ('finance.price-lists.index ' )],
@@ -112,6 +130,9 @@ public function update(Request $request, PriceList $priceList): RedirectResponse
112130 'currency_code ' => 'nullable|string|size:3 ' ,
113131 'discount_percent ' => 'nullable|numeric|min:0|max:100 ' ,
114132 'is_active ' => 'boolean ' ,
133+ 'is_default ' => 'boolean ' ,
134+ 'valid_from ' => 'nullable|date ' ,
135+ 'valid_to ' => 'nullable|date ' ,
115136 'items ' => 'nullable|array ' ,
116137 'items.*.product_id ' => 'required|exists:products,id ' ,
117138 'items.*.unit_price ' => 'required|numeric|min:0 ' ,
@@ -124,15 +145,20 @@ public function update(Request $request, PriceList $priceList): RedirectResponse
124145 'currency_code ' => $ data ['currency_code ' ] ?? 'USD ' ,
125146 'discount_percent ' => $ data ['discount_percent ' ] ?? 0 ,
126147 'is_active ' => $ data ['is_active ' ] ?? true ,
148+ 'is_default ' => $ data ['is_default ' ] ?? false ,
149+ 'valid_from ' => $ data ['valid_from ' ] ?? null ,
150+ 'valid_to ' => $ data ['valid_to ' ] ?? null ,
127151 ]);
128152
129153 // Sync items
130154 $ priceList ->items ()->delete ();
131155 foreach ($ data ['items ' ] ?? [] as $ item ) {
132156 PriceListItem::create ([
157+ 'tenant_id ' => $ priceList ->tenant_id ,
133158 'price_list_id ' => $ priceList ->id ,
134159 'product_id ' => $ item ['product_id ' ],
135160 'unit_price ' => $ item ['unit_price ' ],
161+ 'min_quantity ' => $ item ['min_quantity ' ] ?? 1 ,
136162 ]);
137163 }
138164 });
@@ -151,6 +177,36 @@ public function destroy(PriceList $priceList): RedirectResponse
151177 ->with ('success ' , 'Price list deleted. ' );
152178 }
153179
180+ public function addItem (Request $ request , PriceList $ priceList ): RedirectResponse
181+ {
182+ $ this ->authorize ('create ' , PriceList::class);
183+
184+ $ data = $ request ->validate ([
185+ 'product_id ' => 'required|exists:products,id ' ,
186+ 'unit_price ' => 'required|numeric|min:0 ' ,
187+ 'min_quantity ' => 'required|integer|min:1 ' ,
188+ ]);
189+
190+ PriceListItem::create ([
191+ 'tenant_id ' => $ priceList ->tenant_id ,
192+ 'price_list_id ' => $ priceList ->id ,
193+ 'product_id ' => $ data ['product_id ' ],
194+ 'unit_price ' => $ data ['unit_price ' ],
195+ 'min_quantity ' => $ data ['min_quantity ' ],
196+ ]);
197+
198+ return redirect ()->back ()->with ('success ' , 'Item added to price list. ' );
199+ }
200+
201+ public function removeItem (PriceList $ priceList , PriceListItem $ item ): RedirectResponse
202+ {
203+ $ this ->authorize ('delete ' , $ priceList );
204+
205+ $ item ->delete ();
206+
207+ return redirect ()->back ()->with ('success ' , 'Item removed from price list. ' );
208+ }
209+
154210 public function priceForContact (Request $ request )
155211 {
156212 $ this ->authorize ('viewAny ' , PriceList::class);
0 commit comments