Button / view / form to create a new SalesOrderLineItem

This commit is contained in:
Oliver Walters 2020-04-21 09:02:10 +10:00
parent 617fbf2f02
commit b204618e79
4 changed files with 75 additions and 4 deletions

View File

@ -117,3 +117,19 @@ class EditPurchaseOrderLineItemForm(HelperForm):
'reference', 'reference',
'notes', 'notes',
] ]
class EditSalesOrderLineItemForm(HelperForm):
""" Form for editing a SalesOrderLineItem object """
quantity = RoundingDecimalFormField(max_digits=10, decimal_places=5)
class Meta:
model = SalesOrderLineItem
fields = [
'order',
'part',
'quantity',
'reference',
'notes'
]

View File

@ -124,6 +124,9 @@ class PurchaseOrder(Order):
ORDER_PREFIX = "PO" ORDER_PREFIX = "PO"
def __str__(self):
return "PO {ref} - {company}".format(ref=self.reference, company=self.supplier.name)
supplier = models.ForeignKey( supplier = models.ForeignKey(
Company, on_delete=models.CASCADE, Company, on_delete=models.CASCADE,
limit_choices_to={ limit_choices_to={
@ -255,6 +258,9 @@ class SalesOrder(Order):
customer_reference: Optional field for customer order reference code customer_reference: Optional field for customer order reference code
""" """
def __str__(self):
return "SO {ref} - {company}".format(ref=self.reference, company=self.customer.name)
def get_absolute_url(self): def get_absolute_url(self):
return reverse('so-detail', kwargs={'pk': self.id}) return reverse('so-detail', kwargs={'pk': self.id})

View File

@ -14,9 +14,7 @@
<h4>{% trans "Sales Order Items" %}</h4> <h4>{% trans "Sales Order Items" %}</h4>
<div id='order-toolbar-buttons' class='btn-group' style='float: right;'> <div id='order-toolbar-buttons' class='btn-group' style='float: right;'>
{% if order.status == OrderStatus.PENDING %} <button type='button' class='btn btn-default' id='new-so-line'>{% trans "Add Line Item" %}</button>
<button type='button' class='btn btn-default' id='new-po-line'>{% trans "Add Line Item" %}</button>
{% endif %}
</div> </div>
<table class='table table-striped table-condensed' id='so-lines-table' data-toolbar='#order-toolbar-buttons'> <table class='table table-striped table-condensed' id='so-lines-table' data-toolbar='#order-toolbar-buttons'>
@ -28,6 +26,17 @@
{% block js_ready %} {% block js_ready %}
{{ block.super }} {{ block.super }}
$("#new-so-line").click(function() {
launchModalForm("{% url 'so-line-item-create' %}", {
reload: true,
data: {
order: {{ order.id }},
},
secondary: [
]
});
});
$("#so-lines-table").inventreeTable({ $("#so-lines-table").inventreeTable({
formatNoMatches: function() { return "No matching line items"; }, formatNoMatches: function() { return "No matching line items"; },
queryParams: { queryParams: {

View File

@ -1008,7 +1008,47 @@ class POLineItemCreate(AjaxCreateView):
order = PurchaseOrder.objects.get(id=order_id) order = PurchaseOrder.objects.get(id=order_id)
initials['order'] = order initials['order'] = order
except PurchaseOrder.DoesNotExist: except (PurchaseOrder.DoesNotExist, ValueError):
pass
return initials
class SOLineItemCreate(AjaxCreateView):
""" Ajax view for creating a new SalesOrderLineItem object """
model = SalesOrderLineItem
context_order_name = 'line'
form_class = order_forms.EditSalesOrderLineItemForm
ajax_form_title = _('Add Line Item')
def get_initial(self):
"""
Extract initial data for this line item:
Options:
order: The SalesOrder object
part: The Part object
"""
initials = super().get_initial().copy()
order_id = self.request.GET.get('order', None)
part_id = self.request.GET.get('part', None)
if order_id:
try:
order = SalesOrder.objects.get(id=order_id)
initials['order'] = order
except (SalesOrder.DoesNotExist, ValueError):
pass
if part_id:
try:
part = Part.objects.get(id=part_id)
if part.salable:
initials['part'] = part
except (Part.DoesNotExist, ValueError):
pass pass
return initials return initials