|
@@ -16,6 +16,7 @@ use App\Filament\Tables\Columns;
|
16
|
16
|
use App\Filament\Tables\Filters\DateRangeFilter;
|
17
|
17
|
use App\Models\Accounting\Adjustment;
|
18
|
18
|
use App\Models\Accounting\Bill;
|
|
19
|
+use App\Models\Accounting\DocumentLineItem;
|
19
|
20
|
use App\Models\Banking\BankAccount;
|
20
|
21
|
use App\Models\Common\Offering;
|
21
|
22
|
use App\Models\Common\Vendor;
|
|
@@ -145,16 +146,40 @@ class BillResource extends Resource
|
145
|
146
|
->searchable()
|
146
|
147
|
->required()
|
147
|
148
|
->live()
|
148
|
|
- ->afterStateUpdated(function (Forms\Set $set, Forms\Get $get, $state) {
|
|
149
|
+ ->afterStateUpdated(function (Forms\Set $set, Forms\Get $get, $state, ?DocumentLineItem $record) {
|
149
|
150
|
$offeringId = $state;
|
150
|
|
- $offeringRecord = Offering::with([
|
151
|
|
- 'purchaseTaxes' => function ($query) {
|
152
|
|
- $query->where('status', AdjustmentStatus::Active);
|
153
|
|
- },
|
154
|
|
- 'purchaseDiscounts' => function ($query) {
|
155
|
|
- $query->where('status', AdjustmentStatus::Active);
|
|
151
|
+ $discountMethod = DocumentDiscountMethod::parse($get('../../discount_method'));
|
|
152
|
+ $isPerLineItem = $discountMethod->isPerLineItem();
|
|
153
|
+
|
|
154
|
+ $existingTaxIds = [];
|
|
155
|
+ $existingDiscountIds = [];
|
|
156
|
+
|
|
157
|
+ if ($record) {
|
|
158
|
+ $existingTaxIds = $record->purchaseTaxes()->pluck('adjustments.id')->toArray();
|
|
159
|
+ if ($isPerLineItem) {
|
|
160
|
+ $existingDiscountIds = $record->purchaseDiscounts()->pluck('adjustments.id')->toArray();
|
|
161
|
+ }
|
|
162
|
+ }
|
|
163
|
+
|
|
164
|
+ $with = [
|
|
165
|
+ 'purchaseTaxes' => static function ($query) use ($existingTaxIds) {
|
|
166
|
+ $query->where(static function ($query) use ($existingTaxIds) {
|
|
167
|
+ $query->where('status', AdjustmentStatus::Active)
|
|
168
|
+ ->orWhereIn('adjustments.id', $existingTaxIds);
|
|
169
|
+ });
|
156
|
170
|
},
|
157
|
|
- ])->find($offeringId);
|
|
171
|
+ ];
|
|
172
|
+
|
|
173
|
+ if ($isPerLineItem) {
|
|
174
|
+ $with['purchaseDiscounts'] = static function ($query) use ($existingDiscountIds) {
|
|
175
|
+ $query->where(static function ($query) use ($existingDiscountIds) {
|
|
176
|
+ $query->where('status', AdjustmentStatus::Active)
|
|
177
|
+ ->orWhereIn('adjustments.id', $existingDiscountIds);
|
|
178
|
+ });
|
|
179
|
+ };
|
|
180
|
+ }
|
|
181
|
+
|
|
182
|
+ $offeringRecord = Offering::with($with)->find($offeringId);
|
158
|
183
|
|
159
|
184
|
if (! $offeringRecord) {
|
160
|
185
|
return;
|
|
@@ -166,8 +191,7 @@ class BillResource extends Resource
|
166
|
191
|
$set('unit_price', $unitPrice);
|
167
|
192
|
$set('purchaseTaxes', $offeringRecord->purchaseTaxes->pluck('id')->toArray());
|
168
|
193
|
|
169
|
|
- $discountMethod = DocumentDiscountMethod::parse($get('../../discount_method'));
|
170
|
|
- if ($discountMethod->isPerLineItem()) {
|
|
194
|
+ if ($isPerLineItem) {
|
171
|
195
|
$set('purchaseDiscounts', $offeringRecord->purchaseDiscounts->pluck('id')->toArray());
|
172
|
196
|
}
|
173
|
197
|
}),
|
|
@@ -187,12 +211,22 @@ class BillResource extends Resource
|
187
|
211
|
->default(0),
|
188
|
212
|
Forms\Components\Select::make('purchaseTaxes')
|
189
|
213
|
->label('Taxes')
|
190
|
|
- ->relationship('purchaseTaxes', 'name')
|
191
|
|
- ->disableOptionWhen(function (string $value) {
|
192
|
|
- $adjustment = Adjustment::find($value);
|
|
214
|
+ ->relationship(
|
|
215
|
+ name: 'purchaseTaxes',
|
|
216
|
+ titleAttribute: 'name',
|
|
217
|
+ modifyQueryUsing: function (Builder $query, ?DocumentLineItem $record) {
|
|
218
|
+ $existingAdjustmentIds = $record?->purchaseTaxes()
|
|
219
|
+ ->pluck('adjustments.id')
|
|
220
|
+ ->toArray() ?? [];
|
|
221
|
+
|
|
222
|
+ $query->where(function ($query) use ($existingAdjustmentIds) {
|
|
223
|
+ $query->where('status', AdjustmentStatus::Active)
|
|
224
|
+ ->orWhereIn('adjustments.id', $existingAdjustmentIds);
|
|
225
|
+ });
|
193
|
226
|
|
194
|
|
- return $adjustment?->status !== AdjustmentStatus::Active;
|
195
|
|
- })
|
|
227
|
+ return $query;
|
|
228
|
+ }
|
|
229
|
+ )
|
196
|
230
|
->saveRelationshipsUsing(null)
|
197
|
231
|
->dehydrated(true)
|
198
|
232
|
->preload()
|
|
@@ -201,12 +235,22 @@ class BillResource extends Resource
|
201
|
235
|
->searchable(),
|
202
|
236
|
Forms\Components\Select::make('purchaseDiscounts')
|
203
|
237
|
->label('Discounts')
|
204
|
|
- ->relationship('purchaseDiscounts', 'name')
|
205
|
|
- ->disableOptionWhen(function (string $value) {
|
206
|
|
- $adjustment = Adjustment::find($value);
|
|
238
|
+ ->relationship(
|
|
239
|
+ name: 'purchaseDiscounts',
|
|
240
|
+ titleAttribute: 'name',
|
|
241
|
+ modifyQueryUsing: function (Builder $query, ?DocumentLineItem $record) {
|
|
242
|
+ $existingAdjustmentIds = $record?->purchaseDiscounts()
|
|
243
|
+ ->pluck('adjustments.id')
|
|
244
|
+ ->toArray() ?? [];
|
|
245
|
+
|
|
246
|
+ $query->where(function ($query) use ($existingAdjustmentIds) {
|
|
247
|
+ $query->where('status', AdjustmentStatus::Active)
|
|
248
|
+ ->orWhereIn('adjustments.id', $existingAdjustmentIds);
|
|
249
|
+ });
|
207
|
250
|
|
208
|
|
- return $adjustment?->status !== AdjustmentStatus::Active;
|
209
|
|
- })
|
|
251
|
+ return $query;
|
|
252
|
+ }
|
|
253
|
+ )
|
210
|
254
|
->saveRelationshipsUsing(null)
|
211
|
255
|
->dehydrated(true)
|
212
|
256
|
->preload()
|