@@ -38,11 +38,13 @@ interface PartOrderRecord {
38
38
function SelectPartsStep ( {
39
39
records,
40
40
onRemovePart,
41
+ onSelectQuantity,
41
42
onSelectSupplierPart,
42
43
onSelectPurchaseOrder
43
44
} : {
44
45
records : PartOrderRecord [ ] ;
45
46
onRemovePart : ( part : any ) => void ;
47
+ onSelectQuantity : ( partId : number , quantity : number ) => void ;
46
48
onSelectSupplierPart : ( partId : number , supplierPart : any ) => void ;
47
49
onSelectPurchaseOrder : ( partId : number , purchaseOrder : any ) => void ;
48
50
} ) {
@@ -151,6 +153,7 @@ function SelectPartsStep({
151
153
field_type : 'related field' ,
152
154
api_url : apiUrl ( ApiEndpoints . supplier_part_list ) ,
153
155
model : ModelType . supplierpart ,
156
+ placeholder : t `Select supplier part` ,
154
157
required : true ,
155
158
value : record . supplier_part ?. pk ,
156
159
onValueChange : ( value , instance ) => {
@@ -189,6 +192,7 @@ function SelectPartsStep({
189
192
field_type : 'related field' ,
190
193
api_url : apiUrl ( ApiEndpoints . purchase_order_list ) ,
191
194
model : ModelType . purchaseorder ,
195
+ placeholder : t `Select purchase order` ,
192
196
disabled : ! record . supplier_part ?. supplier ,
193
197
value : record . purchase_order ?. pk ,
194
198
filters : {
@@ -213,6 +217,26 @@ function SelectPartsStep({
213
217
</ Group >
214
218
)
215
219
} ,
220
+ {
221
+ accessor : 'quantity' ,
222
+ title : t `Quantity` ,
223
+ width : 125 ,
224
+ render : ( record : PartOrderRecord ) => (
225
+ < StandaloneField
226
+ fieldName = 'quantity'
227
+ hideLabels = { true }
228
+ error = { record . errors ?. quantity }
229
+ fieldDefinition = { {
230
+ field_type : 'number' ,
231
+ required : true ,
232
+ value : record . quantity ,
233
+ onValueChange : ( value ) => {
234
+ onSelectQuantity ( record . part . pk , value ) ;
235
+ }
236
+ } }
237
+ />
238
+ )
239
+ } ,
216
240
{
217
241
accessor : 'right_actions' ,
218
242
title : ' ' ,
@@ -288,6 +312,22 @@ export default function OrderPartsWizard({
288
312
[ selectedParts ]
289
313
) ;
290
314
315
+ // Select a quantity to order
316
+ const selectQuantity = useCallback (
317
+ ( partId : number , quantity : number ) => {
318
+ const records = [ ...selectedParts ] ;
319
+
320
+ records . forEach ( ( record : PartOrderRecord , index : number ) => {
321
+ if ( record . part . pk === partId ) {
322
+ records [ index ] . quantity = quantity ;
323
+ }
324
+ } ) ;
325
+
326
+ setSelectedParts ( records ) ;
327
+ } ,
328
+ [ selectedParts ]
329
+ ) ;
330
+
291
331
// Select a supplier part for a part
292
332
const selectSupplierPart = useCallback (
293
333
( partId : number , supplierPart : any ) => {
@@ -327,6 +367,7 @@ export default function OrderPartsWizard({
327
367
< SelectPartsStep
328
368
records = { selectedParts }
329
369
onRemovePart = { removePart }
370
+ onSelectQuantity = { selectQuantity }
330
371
onSelectSupplierPart = { selectSupplierPart }
331
372
onSelectPurchaseOrder = { selectPurchaseOrder }
332
373
/>
@@ -400,11 +441,23 @@ export default function OrderPartsWizard({
400
441
( record : PartOrderRecord ) => record . part ?. pk === part . pk
401
442
)
402
443
) {
444
+ // TODO: Make this calculation generic and reusable
445
+ // Calculate the "to order" quantity
446
+ const required =
447
+ ( part . minimum_stock ?? 0 ) +
448
+ ( part . required_for_build_orders ?? 0 ) +
449
+ ( part . required_for_sales_orders ?? 0 ) ;
450
+ const on_hand = part . total_in_stock ?? 0 ;
451
+ const on_order = part . ordering ?? 0 ;
452
+ const in_production = part . building ?? 0 ;
453
+
454
+ const to_order = required - on_hand - on_order - in_production ;
455
+
403
456
records . push ( {
404
457
part : part ,
405
458
supplier_part : undefined ,
406
459
purchase_order : undefined ,
407
- quantity : 1 ,
460
+ quantity : Math . max ( to_order , 0 ) ,
408
461
errors : { }
409
462
} ) ;
410
463
}
0 commit comments