add total price woth js reload

This commit is contained in:
Matthias 2022-03-06 23:21:33 +01:00
parent 56142b9303
commit f0b19e69b8
No known key found for this signature in database
GPG Key ID: F50EF5741D33E076
5 changed files with 54 additions and 0 deletions

View File

@ -23,6 +23,7 @@ from markdownx.models import MarkdownxField
from mptt.models import TreeForeignKey
from djmoney.contrib.exchange.models import convert_money
from djmoney.money import Money
from common.settings import currency_code_default
from users import models as UserModels
@ -600,6 +601,23 @@ class SalesOrder(Order):
verbose_name=_('shipped by')
)
def get_total_price(self):
"""
Calculates the total price of all order lines
"""
target_currency = currency_code_default()
total = Money(0, target_currency)
# order items
total += sum([a.quantity * convert_money(a.sale_price, target_currency) for a in self.lines.all() if a.sale_price])
# additional lines
total += sum([a.quantity * convert_money(a.sale_price, target_currency) for a in self.additional_lines.all() if a.sale_price])
# set decimal-places
total.decimal_places = 4
return total
@property
def is_overdue(self):
"""

View File

@ -515,6 +515,8 @@ class SalesOrderSerializer(ReferenceIndexingSerializerMixin, InvenTreeModelSeria
reference = serializers.CharField(required=True)
total_price_string = serializers.CharField(source='get_total_price', read_only=True)
class Meta:
model = order.models.SalesOrder
@ -535,6 +537,7 @@ class SalesOrderSerializer(ReferenceIndexingSerializerMixin, InvenTreeModelSeria
'status_text',
'shipment_date',
'target_date',
'total_price_string',
]
read_only_fields = [

View File

@ -183,6 +183,12 @@ src="{% static 'img/blank_image.png' %}"
<td>{{ order.responsible }}</td>
</tr>
{% endif %}
<tr>
<td><span class='fas fa-dollar-sign'></span></td>
<td>{% trans "Total cost" %}</td>
<td id="soTotalPrice">{{ order.get_total_price }}</td>
</tr>
</table>
{% endblock %}

View File

@ -292,6 +292,13 @@
}
);
loadOrderTotal(
'#soTotalPrice',
{
url: '{% url "api-so-detail" order.pk %}',
}
);
enableSidebar('salesorder');
{% endblock %}

View File

@ -2290,6 +2290,24 @@ function showFulfilledSubTable(index, row, element, options) {
});
}
var soTotalPriceRef = '' // safes reference to total price
var soTotalPriceOptions = {} // options to reload the price
function loadOrderTotal(reference, options={}) {
soTotalPriceRef = reference;
soTotalPriceOptions = options;
}
function reloadTotal(){
inventreeGet(
soTotalPriceOptions.url,
{},
{success: function(data){
$(soTotalPriceRef).html(data.total_price_string);
}}
);
};
/**
* Load a table displaying line items for a particular SalesOrder
@ -2587,6 +2605,7 @@ function loadSalesOrderLineItemTable(table, options={}) {
function reloadTable() {
$(table).bootstrapTable('refresh');
reloadTotal();
}
// Configure callback functions once the table is loaded
@ -2954,6 +2973,7 @@ function loadSalesOrderAdditionalLineItemTable(table, options={}) {
function reloadTable() {
$(table).bootstrapTable('refresh');
reloadTotal();
}
// Configure callback functions once the table is loaded