Added support for reference and notes fields

This commit is contained in:
eeintech 2021-05-11 17:11:09 -04:00
parent 7e6ac54015
commit e9c4ce8c5f
4 changed files with 72 additions and 71 deletions

View File

@ -160,11 +160,9 @@ class MatchItem(forms.Form):
'type': 'number',
'min': '0',
'step': 'any',
'value': clean(row['quantity']),
'value': clean(row.get('quantity', '')),
})
)
# else:
# self.fields[field_name] = forms.TextInput()
# Create item selection box
elif col_guess in file_manager.ITEM_MATCH_HEADERS:
@ -206,5 +204,8 @@ class MatchItem(forms.Form):
required=False,
default_amount=clean(value),
)
# else:
# self.fields[field_name] = forms.TextInput()
else:
self.fields[field_name] = forms.CharField(
required=False,
initial=value,
)

View File

@ -188,11 +188,6 @@ class FileManagementFormView(MultiStepFormView):
media_folder = 'file_upload/'
extra_context_data = {}
# Set keys for item matching
key_item_select = 'item_select'
key_quantity_select = 'quantity'
key_price_select = 'price'
def get_context_data(self, form, **kwargs):
context = super().get_context_data(form=form, **kwargs)
@ -382,16 +377,20 @@ class FileManagementFormView(MultiStepFormView):
}
self.rows.append(row)
# In the item selection step: update row data to contain fields
# In the item selection step: update row data with mapping to form fields
if form and self.steps.current == 'items':
# Update row data
# Find field keys
field_keys = []
for field in form.fields:
field_key = field.split('-')[0]
if field_key not in field_keys:
field_keys.append(field_key)
# Populate rows
for row in self.rows:
# Add item select field
row['item_select'] = self.key_item_select + '-' + str(row['index'])
# Add quantity select field
row['quantity_select'] = self.key_quantity_select + '-' + str(row['index'])
# Add price select field
row['price_select'] = self.key_price_select + '-' + str(row['index'])
for field_key in field_keys:
# Map row data to field
row[field_key] = field_key + '-' + str(row['index'])
def get_column_index(self, name):
""" Return the index of the column with the given name.

View File

@ -66,7 +66,7 @@
</td>
<td>
{% for field in form.visible_fields %}
{% if field.name == row.quantity_select %}
{% if field.name == row.quantity %}
{{ field }}
{% endif %}
{% endfor %}
@ -79,7 +79,19 @@
<td>
{% if item.column.guess == 'Purchase_Price' %}
{% for field in form.visible_fields %}
{% if field.name == row.price_select %}
{% if field.name == row.purchase_price %}
{{ field }}
{% endif %}
{% endfor %}
{% elif item.column.guess == 'Reference' %}
{% for field in form.visible_fields %}
{% if field.name == row.reference %}
{{ field }}
{% endif %}
{% endfor %}
{% elif item.column.guess == 'Notes' %}
{% for field in form.visible_fields %}
{% if field.name == row.notes %}
{{ field }}
{% endif %}
{% endfor %}

View File

@ -583,7 +583,14 @@ class PurchaseOrderUpload(FileManagementFormView):
_("Match Fields"),
_("Match Supplier Parts"),
]
key_price_select = 'purchase_price'
# Form field name: PurchaseOrderLineItem field
form_field_map = {
'item_select': 'part',
'quantity': 'quantity',
'purchase_price': 'purchase_price',
'reference': 'reference',
'notes': 'notes',
}
def get_order(self):
""" Get order or return 404 """
@ -680,7 +687,7 @@ class PurchaseOrderUpload(FileManagementFormView):
p_val = p_val.replace(',', '')
try:
# Attempt to extract a valid quantity from the field
# Attempt to extract a valid decimal value from the field
purchase_price = Decimal(p_val)
# Store the 'purchase_price' value
row['purchase_price'] = purchase_price
@ -689,11 +696,13 @@ class PurchaseOrderUpload(FileManagementFormView):
# Check if there is a column corresponding to "reference"
if r_idx >= 0:
pass
reference = row['data'][r_idx]['cell']
row['reference'] = reference
# Check if there is a column corresponding to "notes"
if n_idx >= 0:
pass
notes = row['data'][n_idx]['cell']
row['notes'] = notes
def done(self, form_list, **kwargs):
""" Once all the data is in, process it to add PurchaseOrderLineItem instances to the order """
@ -708,60 +717,40 @@ class PurchaseOrderUpload(FileManagementFormView):
(field, idx) = form_key.split('-')
except ValueError:
continue
if field == self.key_item_select:
if idx not in items:
# Insert into items
items.update({
idx: {
'field': form_value,
}
})
else:
# Update items
items[idx]['field'] = form_value
if field == self.key_quantity_select:
if idx not in items:
# Insert into items
items.update({
idx: {
'quantity': form_value,
}
})
else:
# Update items
items[idx]['quantity'] = form_value
if field == self.key_price_select:
if idx not in items:
# Insert into items
items.update({
idx: {
'purchase_price': form_value,
}
})
else:
# Update items
items[idx]['purchase_price'] = form_value
if idx not in items:
# Insert into items
items.update({
idx: {
self.form_field_map[field]: form_value,
}
})
else:
# Update items
items[idx][self.form_field_map[field]] = form_value
# Create PurchaseOrderLineItem instances
for purchase_order_item in items.values():
try:
supplier_part = SupplierPart.objects.get(pk=int(purchase_order_item['field']))
supplier_part = SupplierPart.objects.get(pk=int(purchase_order_item['part']))
except (ValueError, SupplierPart.DoesNotExist):
continue
purchase_order_line_item = PurchaseOrderLineItem(
order=order,
part=supplier_part,
quantity=purchase_order_item['quantity'],
purchase_price=purchase_order_item.get('purchase_price', None),
)
try:
purchase_order_line_item.save()
except IntegrityError:
# PurchaseOrderLineItem already exists
pass
quantity = purchase_order_item.get('quantity', 0)
if quantity:
purchase_order_line_item = PurchaseOrderLineItem(
order=order,
part=supplier_part,
quantity=quantity,
purchase_price=purchase_order_item.get('purchase_price', None),
reference=purchase_order_item.get('reference', ''),
notes=purchase_order_item.get('notes', ''),
)
try:
purchase_order_line_item.save()
except IntegrityError:
# PurchaseOrderLineItem already exists
pass
return HttpResponseRedirect(reverse('po-detail', kwargs={'pk': self.kwargs['pk']}))