mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-29 20:30:39 +00:00 
			
		
		
		
	add total price woth js reload
This commit is contained in:
		| @@ -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): | ||||
|         """ | ||||
|   | ||||
| @@ -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 = [ | ||||
|   | ||||
| @@ -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 %} | ||||
|  | ||||
|   | ||||
| @@ -292,6 +292,13 @@ | ||||
|         } | ||||
|     ); | ||||
|  | ||||
|     loadOrderTotal( | ||||
|         '#soTotalPrice', | ||||
|         { | ||||
|             url: '{% url "api-so-detail" order.pk %}', | ||||
|         } | ||||
|     ); | ||||
|  | ||||
|     enableSidebar('salesorder'); | ||||
|      | ||||
| {% endblock %} | ||||
| @@ -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 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user