mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-03 22:55:43 +00:00 
			
		
		
		
	Added support for reference and notes fields
This commit is contained in:
		@@ -160,11 +160,9 @@ class MatchItem(forms.Form):
 | 
				
			|||||||
                                    'type': 'number',
 | 
					                                    'type': 'number',
 | 
				
			||||||
                                    'min': '0',
 | 
					                                    'min': '0',
 | 
				
			||||||
                                    'step': 'any',
 | 
					                                    'step': 'any',
 | 
				
			||||||
                                    'value': clean(row['quantity']),
 | 
					                                    'value': clean(row.get('quantity', '')),
 | 
				
			||||||
                                })
 | 
					                                })
 | 
				
			||||||
                            )
 | 
					                            )
 | 
				
			||||||
                        # else:
 | 
					 | 
				
			||||||
                        #     self.fields[field_name] = forms.TextInput()
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    # Create item selection box
 | 
					                    # Create item selection box
 | 
				
			||||||
                    elif col_guess in file_manager.ITEM_MATCH_HEADERS:
 | 
					                    elif col_guess in file_manager.ITEM_MATCH_HEADERS:
 | 
				
			||||||
@@ -206,5 +204,8 @@ class MatchItem(forms.Form):
 | 
				
			|||||||
                                required=False,
 | 
					                                required=False,
 | 
				
			||||||
                                default_amount=clean(value),
 | 
					                                default_amount=clean(value),
 | 
				
			||||||
                            )
 | 
					                            )
 | 
				
			||||||
                        # else:
 | 
					                        else:
 | 
				
			||||||
                        #     self.fields[field_name] = forms.TextInput()
 | 
					                            self.fields[field_name] = forms.CharField(
 | 
				
			||||||
 | 
					                                required=False,
 | 
				
			||||||
 | 
					                                initial=value,
 | 
				
			||||||
 | 
					                            )
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -188,11 +188,6 @@ class FileManagementFormView(MultiStepFormView):
 | 
				
			|||||||
    media_folder = 'file_upload/'
 | 
					    media_folder = 'file_upload/'
 | 
				
			||||||
    extra_context_data = {}
 | 
					    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):
 | 
					    def get_context_data(self, form, **kwargs):
 | 
				
			||||||
        context = super().get_context_data(form=form, **kwargs)
 | 
					        context = super().get_context_data(form=form, **kwargs)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -382,16 +377,20 @@ class FileManagementFormView(MultiStepFormView):
 | 
				
			|||||||
                }
 | 
					                }
 | 
				
			||||||
                self.rows.append(row)
 | 
					                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':
 | 
					        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:
 | 
					            for row in self.rows:
 | 
				
			||||||
                # Add item select field
 | 
					                for field_key in field_keys:
 | 
				
			||||||
                row['item_select'] = self.key_item_select + '-' + str(row['index'])
 | 
					                    # Map row data to field
 | 
				
			||||||
                # Add quantity select field
 | 
					                    row[field_key] = field_key + '-' + str(row['index'])
 | 
				
			||||||
                row['quantity_select'] = self.key_quantity_select + '-' + str(row['index'])
 | 
					 | 
				
			||||||
                # Add price select field
 | 
					 | 
				
			||||||
                row['price_select'] = self.key_price_select + '-' + str(row['index'])
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def get_column_index(self, name):
 | 
					    def get_column_index(self, name):
 | 
				
			||||||
        """ Return the index of the column with the given name.
 | 
					        """ Return the index of the column with the given name.
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -66,7 +66,7 @@
 | 
				
			|||||||
                </td>
 | 
					                </td>
 | 
				
			||||||
                <td>
 | 
					                <td>
 | 
				
			||||||
                    {% for field in form.visible_fields %}
 | 
					                    {% for field in form.visible_fields %}
 | 
				
			||||||
                        {% if field.name == row.quantity_select %}
 | 
					                        {% if field.name == row.quantity %}
 | 
				
			||||||
                            {{ field }}
 | 
					                            {{ field }}
 | 
				
			||||||
                        {% endif %}
 | 
					                        {% endif %}
 | 
				
			||||||
                    {% endfor %}
 | 
					                    {% endfor %}
 | 
				
			||||||
@@ -79,7 +79,19 @@
 | 
				
			|||||||
                <td>
 | 
					                <td>
 | 
				
			||||||
                    {% if item.column.guess == 'Purchase_Price' %}
 | 
					                    {% if item.column.guess == 'Purchase_Price' %}
 | 
				
			||||||
                        {% for field in form.visible_fields %}
 | 
					                        {% 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 }}
 | 
					                                {{ field }}
 | 
				
			||||||
                            {% endif %}
 | 
					                            {% endif %}
 | 
				
			||||||
                        {% endfor %}
 | 
					                        {% endfor %}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -583,7 +583,14 @@ class PurchaseOrderUpload(FileManagementFormView):
 | 
				
			|||||||
        _("Match Fields"),
 | 
					        _("Match Fields"),
 | 
				
			||||||
        _("Match Supplier Parts"),
 | 
					        _("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):
 | 
					    def get_order(self):
 | 
				
			||||||
        """ Get order or return 404 """
 | 
					        """ Get order or return 404 """
 | 
				
			||||||
@@ -680,7 +687,7 @@ class PurchaseOrderUpload(FileManagementFormView):
 | 
				
			|||||||
                    p_val = p_val.replace(',', '')
 | 
					                    p_val = p_val.replace(',', '')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    try:
 | 
					                    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)
 | 
					                        purchase_price = Decimal(p_val)
 | 
				
			||||||
                        # Store the 'purchase_price' value
 | 
					                        # Store the 'purchase_price' value
 | 
				
			||||||
                        row['purchase_price'] = purchase_price
 | 
					                        row['purchase_price'] = purchase_price
 | 
				
			||||||
@@ -689,11 +696,13 @@ class PurchaseOrderUpload(FileManagementFormView):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            # Check if there is a column corresponding to "reference"
 | 
					            # Check if there is a column corresponding to "reference"
 | 
				
			||||||
            if r_idx >= 0:
 | 
					            if r_idx >= 0:
 | 
				
			||||||
                pass
 | 
					                reference = row['data'][r_idx]['cell']
 | 
				
			||||||
 | 
					                row['reference'] = reference
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            # Check if there is a column corresponding to "notes"
 | 
					            # Check if there is a column corresponding to "notes"
 | 
				
			||||||
            if n_idx >= 0:
 | 
					            if n_idx >= 0:
 | 
				
			||||||
                pass
 | 
					                notes = row['data'][n_idx]['cell']
 | 
				
			||||||
 | 
					                row['notes'] = notes
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def done(self, form_list, **kwargs):
 | 
					    def done(self, form_list, **kwargs):
 | 
				
			||||||
        """ Once all the data is in, process it to add PurchaseOrderLineItem instances to the order """
 | 
					        """ 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('-')
 | 
					                (field, idx) = form_key.split('-')
 | 
				
			||||||
            except ValueError:
 | 
					            except ValueError:
 | 
				
			||||||
                continue
 | 
					                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:
 | 
				
			||||||
                if idx not in items:
 | 
					                # Insert into items
 | 
				
			||||||
                    # Insert into items
 | 
					                items.update({
 | 
				
			||||||
                    items.update({
 | 
					                    idx: {
 | 
				
			||||||
                        idx: {
 | 
					                        self.form_field_map[field]: form_value,
 | 
				
			||||||
                            'quantity': form_value,
 | 
					                    }
 | 
				
			||||||
                        }
 | 
					                })
 | 
				
			||||||
                    })
 | 
					            else:
 | 
				
			||||||
                else:
 | 
					                # Update items
 | 
				
			||||||
                    # Update items
 | 
					                items[idx][self.form_field_map[field]] = form_value
 | 
				
			||||||
                    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
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # Create PurchaseOrderLineItem instances
 | 
					        # Create PurchaseOrderLineItem instances
 | 
				
			||||||
        for purchase_order_item in items.values():
 | 
					        for purchase_order_item in items.values():
 | 
				
			||||||
            try:
 | 
					            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):
 | 
					            except (ValueError, SupplierPart.DoesNotExist):
 | 
				
			||||||
                continue
 | 
					                continue
 | 
				
			||||||
            purchase_order_line_item = PurchaseOrderLineItem(
 | 
					
 | 
				
			||||||
                order=order,
 | 
					            quantity = purchase_order_item.get('quantity', 0)
 | 
				
			||||||
                part=supplier_part,
 | 
					            if quantity:
 | 
				
			||||||
                quantity=purchase_order_item['quantity'],
 | 
					                purchase_order_line_item = PurchaseOrderLineItem(
 | 
				
			||||||
                purchase_price=purchase_order_item.get('purchase_price', None),
 | 
					                    order=order,
 | 
				
			||||||
            )
 | 
					                    part=supplier_part,
 | 
				
			||||||
            try:
 | 
					                    quantity=quantity,
 | 
				
			||||||
                purchase_order_line_item.save()
 | 
					                    purchase_price=purchase_order_item.get('purchase_price', None),
 | 
				
			||||||
            except IntegrityError:
 | 
					                    reference=purchase_order_item.get('reference', ''),
 | 
				
			||||||
                # PurchaseOrderLineItem already exists
 | 
					                    notes=purchase_order_item.get('notes', ''),
 | 
				
			||||||
                pass
 | 
					                )
 | 
				
			||||||
 | 
					                try:
 | 
				
			||||||
 | 
					                    purchase_order_line_item.save()
 | 
				
			||||||
 | 
					                except IntegrityError:
 | 
				
			||||||
 | 
					                    # PurchaseOrderLineItem already exists
 | 
				
			||||||
 | 
					                    pass
 | 
				
			||||||
            
 | 
					            
 | 
				
			||||||
        return HttpResponseRedirect(reverse('po-detail', kwargs={'pk': self.kwargs['pk']}))
 | 
					        return HttpResponseRedirect(reverse('po-detail', kwargs={'pk': self.kwargs['pk']}))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user