Skip to content

Commit ac46edd

Browse files
committed
5446 refactor code and tweak modal
1 parent 9d52ff8 commit ac46edd

File tree

7 files changed

+73
-84
lines changed

7 files changed

+73
-84
lines changed

app/assets/stylesheets/modal-dialog.scss

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,54 @@
3131
width: 700px;
3232
}
3333
}
34+
35+
/* DUPLICATE ITEMS MODAL */
36+
.duplicate-container {
37+
margin-bottom: 20px;
38+
padding: 10px;
39+
background-color: #f9f9f9;
40+
border: 1px solid #ddd;
41+
border-radius: 5px;
42+
}
43+
44+
.duplicate-entry {
45+
padding: 8px;
46+
margin: 4px 0;
47+
background-color: #fff3cd;
48+
border-left: 3px solid #ffc107;
49+
}
50+
51+
.duplicate-barcode {
52+
font-size: 0.85em;
53+
color: #666;
54+
margin-top: 2px;
55+
}
56+
57+
.duplicate-merged {
58+
padding: 10px;
59+
margin: 10px 0 0 0;
60+
background-color: #d4edda;
61+
border: 2px solid #28a745;
62+
border-radius: 4px;
63+
font-weight: bold;
64+
}
65+
66+
.duplicate-modal-footer {
67+
display: flex;
68+
justify-content: space-between;
69+
align-items: center;
70+
}
71+
72+
.duplicate-modal-text {
73+
margin: 0;
74+
font-size: 0.9em;
75+
margin-right: 20px;
76+
}
77+
78+
.duplicate-modal-buttons {
79+
margin-left: auto;
80+
}
81+
82+
.duplicate-items-list {
83+
margin-bottom: 20px;
84+
}

app/controllers/audits_controller.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ def new
5151
def create
5252
@audit = current_organization.audits.new(audit_params)
5353
@audit.user = current_user
54-
@audit.merge_duplicates = params[:merge_duplicates] == 'true'
5554
if @audit.save
5655
save_audit_status_and_redirect(params)
5756
else

app/javascript/utils/audit_duplicates.js

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,18 @@ $(() => {
1414
const quantityInput = section.find('input[name*="[quantity]"]');
1515
const itemQuantity = parseInt(quantityInput.val()) || 0;
1616
const barcodeValue = section.find('.__barcode_item_lookup').val() || '';
17+
1718
if (!itemId || itemText === "Choose an item" || itemQuantity === 0) {
19+
section.remove();
1820
return;
1921
}
22+
2023
itemCounts[itemId] = (itemCounts[itemId] || 0) + 1;
2124
itemNames[itemId] = itemText;
2225
if (!itemQuantities[itemId]) itemQuantities[itemId] = [];
2326
itemQuantities[itemId].push({ qty: itemQuantity, barcode: barcodeValue });
2427
});
2528

26-
// Remove rows with zero quantity or no item selected
27-
form.find('select[name$="[item_id]"]').each(function() {
28-
const itemId = $(this).val();
29-
const itemText = $(this).find('option:selected').text();
30-
const section = $(this).closest('.line_item_section');
31-
const quantityInput = section.find('input[name*="[quantity]"]');
32-
const itemQuantity = parseInt(quantityInput.val()) || 0;
33-
34-
if (!itemId || itemText === "Choose an item" || itemQuantity === 0) {
35-
section.remove();
36-
}
37-
});
38-
3929
// Check for duplicates
4030
const duplicates = Object.keys(itemCounts)
4131
.filter(itemId => itemCounts[itemId] > 1)
@@ -45,10 +35,8 @@ $(() => {
4535
// Show modal with duplicate items
4636
showDuplicateModal(duplicates, itemQuantities, form, buttonName);
4737
e.preventDefault();
48-
} else {
49-
// No duplicates, let the form submit normally
50-
// Don't prevent default - let the button's natural submit behavior work
51-
}
38+
}
39+
// else, allow form submission to proceed
5240
}
5341

5442
$("button[name='save_progress']").on('click', function (e) {
@@ -64,33 +52,33 @@ $(() => {
6452
const entries = duplicateQuantities[item.id] || [];
6553
const total = entries.reduce((sum, entry) => sum + entry.qty, 0);
6654
const rows = entries.map((entry, i) => {
67-
const barcodeLine = entry.barcode ? `<div style="font-size: 0.85em; color: #666; margin-top: 2px;">Barcode: ${entry.barcode}</div>` : '';
68-
if (i === 0) {
69-
return `<div style="padding: 8px; margin: 4px 0; background-color: #f8f9fa; border-left: 3px solid #6c757d;">${item.name} - Quantity: ${entry.qty}${barcodeLine}</div>`;
70-
} else {
71-
return `<div style="padding: 8px; margin: 4px 0; background-color: #fff3cd; border-left: 3px solid #ffc107;"><strong>⚠ Duplicate:</strong> ${item.name} - Quantity: ${entry.qty}${barcodeLine}</div>`;
72-
}
55+
const barcodeLine = entry.barcode ? `<div class="duplicate-barcode">Barcode: ${entry.barcode}</div>` : '';
56+
return `<div class="duplicate-entry">❐ ${item.name} : ${entry.qty}${barcodeLine}</div>`;
7357
}).join('');
74-
return `<div style="margin-bottom: 20px; padding: 10px; background-color: #f9f9f9; border: 1px solid #ddd; border-radius: 5px;">${rows}<div style="padding: 10px; margin: 10px 0 0 0; background-color: #d1ecf1; border: 2px solid #0c5460; border-radius: 4px; font-weight: bold;">✓ Merged Result - Quantity: ${total}</div></div>`;
58+
return `<div class="duplicate-container">${rows}<div class="duplicate-merged">→ Merged Result: ${item.name} : ${total}</div></div>`;
7559
}).join('');
7660
const modalHtml = `
7761
<div class="modal fade" id="duplicateItemsModal" tabindex="-1">
7862
<div class="modal-dialog modal-dialog-scrollable">
7963
<div class="modal-content">
8064
<div class="modal-header">
81-
<h5 class="modal-title">Duplicate Items Detected</h5>
65+
<h5 class="modal-title">Multiple Item Entries Detected</h5>
8266
<button type="button" class="close" data-bs-dismiss="modal">
8367
<span>&times;</span>
8468
</button>
8569
</div>
8670
<div class="modal-body">
8771
<p><strong>The following items have multiple entries:</strong></p>
88-
<div>${itemRows}</div>
89-
<p style="margin-top: 15px; padding: 10px; background-color: #f8f9fa; border-left: 3px solid #6c757d;">Choose <strong>Merge Items</strong> to combine quantities and continue, or <strong>Review Entries</strong> to go back and make changes.</p>
72+
<div class="duplicate-items-list">${itemRows}</div>
9073
</div>
91-
<div class="modal-footer">
92-
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Review Entries</button>
93-
<button type="button" class="btn btn-warning" id="confirmMerge">Merge Items</button>
74+
<div class="modal-footer duplicate-modal-footer">
75+
<p class="duplicate-modal-text">
76+
Choose <strong>Merge Items</strong> to combine quantities and continue, or <strong>Make Changes</strong> to go back and edit.
77+
</p>
78+
<div class="duplicate-modal-buttons">
79+
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Make Changes</button>
80+
<button type="button" class="btn btn-success" id="confirmMerge">Merge Items</button>
81+
</div>
9482
</div>
9583
</div>
9684
</div>
@@ -109,12 +97,7 @@ $(() => {
10997
$('#duplicateItemsModal').modal('hide');
11098
});
11199

112-
// Handle review button
113-
$('#duplicateItemsModal .btn-secondary').on('click', function() {
114-
$('#duplicateItemsModal').modal('hide');
115-
});
116-
117-
// Handle confirm button
100+
// Handle Merge Items button
118101
$('#confirmMerge').on('click', function() {
119102
$('#duplicateItemsModal').modal('hide');
120103

@@ -126,7 +109,7 @@ $(() => {
126109
form.append(hiddenBtn);
127110

128111
// Click the hidden button to submit with the correct parameter
129-
hiddenBtn.click();
112+
hiddenBtn.trigger('click');
130113
});
131114
}
132115

app/models/audit.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,9 @@ class Audit < ApplicationRecord
2929
enum :status, { in_progress: 0, confirmed: 1, finalized: 2 }
3030

3131
validate :line_items_quantity_is_not_negative
32-
validate :line_items_unique_by_item_id, unless: :merge_duplicates
32+
validate :line_items_unique_by_item_id
3333
validate :user_is_organization_admin_of_the_organization
3434

35-
attr_accessor :merge_duplicates
36-
3735
def self.finalized_since?(itemizable, *location_ids)
3836
item_ids = itemizable.line_items.pluck(:item_id)
3937
where(status: "finalized")

app/views/audits/edit.html.erb

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,3 @@
3939
</section>
4040

4141
<%= render partial: "audits/form" %>
42-
43-
<!-- Duplicate Items Modal -->
44-
<div id="duplicateItemsModal" class="modal fade" tabindex="-1" role="dialog">
45-
<div class="modal-dialog" role="document">
46-
<div class="modal-content">
47-
<div class="modal-header">
48-
<h4 class="modal-title">Duplicate Items Detected</h4>
49-
<button type="button" class="close" data-dismiss="modal">&times;</button>
50-
</div>
51-
<div class="modal-body">
52-
<p>The following items appear multiple times in your audit:</p>
53-
<ul id="duplicateItemsList"></ul>
54-
<p>Would you like to merge the quantities for these duplicate items?</p>
55-
</div>
56-
<div class="modal-footer">
57-
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
58-
<button type="button" class="btn btn-primary" onclick="mergeDuplicatesAndSubmit()">Merge & Save</button>
59-
</div>
60-
</div>
61-
</div>
62-
</div>

app/views/audits/show.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
<tbody>
5353
<% @items.each do |inventory_item| %>
5454
<% line_item = @audit.line_items.find { |i| i.item_id == inventory_item.item_id } %>
55-
<tr class="<%= line_item.nil? ? "negative" : (line_item.quantity == inventory_item.quantity ? "positive" : "highlight") %>">
55+
<tr class="<%= line_item.nil? ? "negative" : (line_item.quantity.equal?(inventory_item.quantity) ? "positive" : "highlight") %>">
5656
<td><%= inventory_item.name %></td>
5757
<td><%= line_item&.quantity&.abs || "Not Audited" %></td>
5858
<td class="text-right"><%= inventory_item.quantity %></td>

app/views/barcode_items/_duplicate_item_modal.html.erb

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)