11import $ from 'jquery' ;
22
33$ ( ( ) => {
4- function checkForDuplicates ( e , buttonName ) {
5- const form = $ ( this ) . closest ( 'form' ) ;
6- const itemCounts = { } ; // Will look like: { "2": 3, "5": 1, "12": 2 }
7- const itemNames = { } ; // Will look like: { "2": "Item A", "5": "Item B", "12": "Item C" }
8- const itemQuantities = { } ; // Will look like: { "2": [{qty: 15, barcode: "123"} , {qty: 10, barcode: "456"}] }
4+ function processFormItems ( form ) {
5+ const itemCounts = { } ;
6+ const itemNames = { } ;
7+ const itemQuantities = { } ;
8+ const itemSections = [ ] ;
99
10- form . find ( 'select[name$ ="[item_id]"]' ) . each ( function ( ) {
10+ form . find ( 'select[name* ="[item_id]"]' ) . each ( function ( ) {
1111 const itemId = $ ( this ) . val ( ) ;
1212 const itemText = $ ( this ) . find ( 'option:selected' ) . text ( ) ;
1313 const section = $ ( this ) . closest ( '.line_item_section' ) ;
1414 const quantityInput = section . find ( 'input[name*="[quantity]"]' ) ;
1515 const itemQuantity = parseInt ( quantityInput . val ( ) ) || 0 ;
1616 const barcodeValue = section . find ( '.__barcode_item_lookup' ) . val ( ) || '' ;
1717
18- if ( ! itemId || itemText === "Choose an item" || itemQuantity === 0 ) {
19- section . remove ( ) ;
18+ if ( ! itemId || itemId === '' || itemText === "Choose an item" || itemQuantity === 0 ) {
2019 return ;
2120 }
2221
2322 itemCounts [ itemId ] = ( itemCounts [ itemId ] || 0 ) + 1 ;
2423 itemNames [ itemId ] = itemText ;
2524 if ( ! itemQuantities [ itemId ] ) itemQuantities [ itemId ] = [ ] ;
2625 itemQuantities [ itemId ] . push ( { qty : itemQuantity , barcode : barcodeValue } ) ;
26+ itemSections . push ( { itemId, section, quantity : itemQuantity } ) ;
2727 } ) ;
28+
29+ return { itemCounts, itemNames, itemQuantities, itemSections } ;
30+ }
31+
32+ function checkForDuplicates ( e , buttonName ) {
33+ e . preventDefault ( ) ; // Always prevent default first
34+
35+ const form = $ ( this ) . closest ( 'form' ) ;
36+ const { itemCounts, itemNames, itemQuantities } = processFormItems ( form ) ;
2837
2938 // Check for duplicates
3039 const duplicates = Object . keys ( itemCounts )
31- . filter ( itemId => itemCounts [ itemId ] > 1 )
40+ . filter ( itemId => itemId && itemId !== '' && itemCounts [ itemId ] > 1 )
3241 . map ( itemId => ( { name : itemNames [ itemId ] , id : itemId } ) ) ;
3342
3443 if ( duplicates . length > 0 ) {
3544 // Show modal with duplicate items
3645 showDuplicateModal ( duplicates , itemQuantities , form , buttonName ) ;
37- e . preventDefault ( ) ;
38- }
39- // else, allow form submission to proceed
46+ return false ;
47+ } else {
48+ // No duplicates, submit the form
49+ const hiddenBtn = $ ( `<button type="submit" name="${ buttonName } " style="display:none;"></button>` ) ;
50+ form . append ( hiddenBtn ) ;
51+ form . off ( 'submit' ) ; // Remove event handlers to avoid recursion
52+ hiddenBtn . trigger ( 'click' ) ;
53+ }
4054 }
4155
42- $ ( "button[name='save_progress']" ) . on ( 'click' , function ( e ) {
43- checkForDuplicates . call ( this , e , 'save_progress' ) ;
44- } ) ;
45-
46- $ ( "button[name='confirm_audit']" ) . on ( 'click' , function ( e ) {
47- checkForDuplicates . call ( this , e , 'confirm_audit' ) ;
56+ $ ( document ) . on ( 'click' , "button[name='save_progress'], button[name='confirm_audit']" , function ( e ) {
57+ const buttonName = $ ( this ) . attr ( 'name' ) ;
58+ checkForDuplicates . call ( this , e , buttonName ) ;
4859 } ) ;
4960
5061 function showDuplicateModal ( duplicateItems , duplicateQuantities , form , buttonName ) {
@@ -85,9 +96,6 @@ $(() => {
8596 </div>
8697 ` ;
8798
88- // Remove existing modal
89- $ ( '#duplicateItemsModal' ) . remove ( ) ;
90-
9199 // Add and show modal
92100 $ ( 'body' ) . append ( modalHtml ) ;
93101 $ ( '#duplicateItemsModal' ) . modal ( 'show' ) ;
@@ -108,39 +116,31 @@ $(() => {
108116 const hiddenBtn = $ ( `<button type="submit" name="${ buttonName } " style="display:none;"></button>` ) ;
109117 form . append ( hiddenBtn ) ;
110118
111- // Click the hidden button to submit with the correct parameter
119+ // // Click the hidden button to submit with the correct parameter
112120 hiddenBtn . trigger ( 'click' ) ;
113121 } ) ;
114122 }
115123
116124 function mergeDuplicateItems ( form ) {
117- const itemQuantities = { } ;
118- const itemSections = [ ] ;
125+ const { itemSections } = processFormItems ( form ) ;
126+ const mergedQuantities = { } ;
119127
120- // Collect all line items and their quantities
121- form . find ( 'select[name$="[item_id]"]' ) . each ( function ( ) {
122- const itemId = $ ( this ) . val ( ) ;
123- const section = $ ( this ) . closest ( '.line_item_section' ) ;
124- const quantityInput = section . find ( 'input[name*="[quantity]"]' ) ;
125- const quantity = parseInt ( quantityInput . val ( ) ) || 0 ;
126-
127- if ( itemId && itemId !== '' ) {
128- itemSections . push ( { itemId, section, quantity } ) ;
129- itemQuantities [ itemId ] = ( itemQuantities [ itemId ] || 0 ) + quantity ;
130- }
128+ // Calculate merged quantities
129+ itemSections . forEach ( ( { itemId, quantity } ) => {
130+ mergedQuantities [ itemId ] = ( mergedQuantities [ itemId ] || 0 ) + quantity ;
131131 } ) ;
132132
133133 // Find duplicates and merge them
134134 const processedItems = new Set ( ) ;
135135
136- itemSections . forEach ( ( { itemId, section, quantity } ) => {
136+ itemSections . forEach ( ( { itemId, section } ) => {
137137 if ( processedItems . has ( itemId ) ) {
138138 // This is a duplicate - remove it
139139 section . remove ( ) ;
140140 } else {
141141 // This is the first occurrence - update quantity to merged total
142142 const quantityInput = section . find ( 'input[name*="[quantity]"]' ) ;
143- quantityInput . val ( itemQuantities [ itemId ] ) ;
143+ quantityInput . val ( mergedQuantities [ itemId ] ) ;
144144 processedItems . add ( itemId ) ;
145145 }
146146 } ) ;
0 commit comments