mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-16 20:15:44 +00:00
Merge branch 'master' into spaces!
This commit is contained in:
@ -211,6 +211,7 @@ class EditSalesOrderLineItemForm(HelperForm):
|
||||
'part',
|
||||
'quantity',
|
||||
'reference',
|
||||
'sale_price',
|
||||
'notes'
|
||||
]
|
||||
|
||||
|
24
InvenTree/order/migrations/0045_auto_20210504_1946.py
Normal file
24
InvenTree/order/migrations/0045_auto_20210504_1946.py
Normal file
@ -0,0 +1,24 @@
|
||||
# Generated by Django 3.2 on 2021-05-04 19:46
|
||||
|
||||
from django.db import migrations
|
||||
import djmoney.models.fields
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('order', '0044_auto_20210404_2016'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='salesorderlineitem',
|
||||
name='sale_price',
|
||||
field=djmoney.models.fields.MoneyField(blank=True, decimal_places=4, default_currency='USD', help_text='Unit sale price', max_digits=19, null=True, verbose_name='Sale Price'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='salesorderlineitem',
|
||||
name='sale_price_currency',
|
||||
field=djmoney.models.fields.CurrencyField(choices=[('AUD', 'Australian Dollar'), ('GBP', 'British Pound'), ('CAD', 'Canadian Dollar'), ('EUR', 'Euro'), ('JPY', 'Japanese Yen'), ('NZD', 'New Zealand Dollar'), ('USD', 'US Dollar')], default='USD', editable=False, max_length=3),
|
||||
),
|
||||
]
|
@ -367,7 +367,7 @@ class PurchaseOrder(Order):
|
||||
stock.save()
|
||||
|
||||
text = _("Received items")
|
||||
note = f"{_('Received')} {quantity} {_('items against order')} {str(self)}"
|
||||
note = _('Received {n} items against order {name}').format(n=quantity, name=str(self))
|
||||
|
||||
# Add a new transaction note to the newly created stock item
|
||||
stock.addTransactionNote(text, user, note)
|
||||
@ -672,12 +672,22 @@ class SalesOrderLineItem(OrderLineItem):
|
||||
Attributes:
|
||||
order: Link to the SalesOrder that this line item belongs to
|
||||
part: Link to a Part object (may be null)
|
||||
sale_price: The unit sale price for this OrderLineItem
|
||||
"""
|
||||
|
||||
order = models.ForeignKey(SalesOrder, on_delete=models.CASCADE, related_name='lines', verbose_name=_('Order'), help_text=_('Sales Order'))
|
||||
|
||||
part = models.ForeignKey('part.Part', on_delete=models.SET_NULL, related_name='sales_order_line_items', null=True, verbose_name=_('Part'), help_text=_('Part'), limit_choices_to={'salable': True})
|
||||
|
||||
sale_price = MoneyField(
|
||||
max_digits=19,
|
||||
decimal_places=4,
|
||||
default_currency='USD',
|
||||
null=True, blank=True,
|
||||
verbose_name=_('Sale Price'),
|
||||
help_text=_('Unit sale price'),
|
||||
)
|
||||
|
||||
class Meta:
|
||||
unique_together = [
|
||||
]
|
||||
|
@ -278,6 +278,7 @@ class SOLineItemSerializer(InvenTreeModelSerializer):
|
||||
quantity = serializers.FloatField()
|
||||
allocated = serializers.FloatField(source='allocated_quantity', read_only=True)
|
||||
fulfilled = serializers.FloatField(source='fulfilled_quantity', read_only=True)
|
||||
sale_price_string = serializers.CharField(source='sale_price', read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = SalesOrderLineItem
|
||||
@ -294,6 +295,9 @@ class SOLineItemSerializer(InvenTreeModelSerializer):
|
||||
'order_detail',
|
||||
'part',
|
||||
'part_detail',
|
||||
'sale_price',
|
||||
'sale_price_currency',
|
||||
'sale_price_string',
|
||||
]
|
||||
|
||||
|
||||
|
@ -146,6 +146,8 @@ $('#view-calendar').click(function() {
|
||||
|
||||
$("#purchase-order-calendar").show();
|
||||
$("#view-list").show();
|
||||
|
||||
calendar.render();
|
||||
});
|
||||
|
||||
$("#view-list").click(function() {
|
||||
|
@ -223,6 +223,14 @@ $("#so-lines-table").inventreeTable({
|
||||
field: 'quantity',
|
||||
title: '{% trans "Quantity" %}',
|
||||
},
|
||||
{
|
||||
sortable: true,
|
||||
field: 'sale_price',
|
||||
title: '{% trans "Unit Price" %}',
|
||||
formatter: function(value, row) {
|
||||
return row.sale_price_string || row.sale_price;
|
||||
}
|
||||
},
|
||||
{
|
||||
field: 'allocated',
|
||||
{% if order.status == SalesOrderStatus.PENDING %}
|
||||
@ -279,7 +287,7 @@ $("#so-lines-table").inventreeTable({
|
||||
html += makeIconButton('fa-hashtag icon-green', 'button-add-by-sn', pk, '{% trans "Allocate serial numbers" %}');
|
||||
}
|
||||
|
||||
html += makeIconButton('fa-sign-in-alt icon-green', 'button-add', pk, '{% trans "Allocate stock" %}');
|
||||
html += makeIconButton('fa-sign-in-alt icon-green', 'button-add', pk, '{% trans "Allocate stock" %}');
|
||||
|
||||
if (part.purchaseable) {
|
||||
html += makeIconButton('fa-shopping-cart', 'button-buy', row.part, '{% trans "Purchase stock" %}');
|
||||
@ -289,6 +297,7 @@ $("#so-lines-table").inventreeTable({
|
||||
html += makeIconButton('fa-tools', 'button-build', row.part, '{% trans "Build stock" %}');
|
||||
}
|
||||
|
||||
html += makeIconButton('fa-dollar-sign icon-green', 'button-price', pk, '{% trans "Calculate price" %}');
|
||||
}
|
||||
|
||||
html += makeIconButton('fa-edit icon-blue', 'button-edit', pk, '{% trans "Edit line item" %}');
|
||||
@ -388,6 +397,26 @@ function setupCallbacks() {
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
$(".button-price").click(function() {
|
||||
var pk = $(this).attr('pk');
|
||||
var idx = $(this).closest('tr').attr('data-index');
|
||||
var row = table.bootstrapTable('getData')[idx];
|
||||
|
||||
launchModalForm(
|
||||
"{% url 'line-pricing' %}",
|
||||
{
|
||||
submit_text: '{% trans "Calculate price" %}',
|
||||
data: {
|
||||
line_item: pk,
|
||||
quantity: row.quantity,
|
||||
},
|
||||
buttons: [{name: 'update_price',
|
||||
title: '{% trans "Update Unit Price" %}'},],
|
||||
success: reloadTable,
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
{% endblock %}
|
@ -144,6 +144,8 @@ $('#view-calendar').click(function() {
|
||||
|
||||
$("#sales-order-calendar").show();
|
||||
$("#view-list").show();
|
||||
|
||||
calendar.render();
|
||||
});
|
||||
|
||||
$("#view-list").click(function() {
|
||||
|
@ -31,6 +31,7 @@ purchase_order_urls = [
|
||||
url(r'^new/', views.PurchaseOrderCreate.as_view(), name='po-create'),
|
||||
|
||||
url(r'^order-parts/', views.OrderParts.as_view(), name='order-parts'),
|
||||
url(r'^pricing/', views.LineItemPricing.as_view(), name='line-pricing'),
|
||||
|
||||
# Display detail view for a single purchase order
|
||||
url(r'^(?P<pk>\d+)/', include(purchase_order_detail_urls)),
|
||||
|
@ -6,13 +6,14 @@ Django views for interacting with Order app
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import transaction
|
||||
from django.http.response import JsonResponse
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.urls import reverse
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.views.generic import DetailView, ListView, UpdateView
|
||||
from django.views.generic.edit import FormMixin
|
||||
from django.forms import HiddenInput
|
||||
from django.forms import HiddenInput, IntegerField
|
||||
|
||||
import logging
|
||||
from decimal import Decimal, InvalidOperation
|
||||
@ -29,6 +30,7 @@ from part.models import Part
|
||||
from common.models import InvenTreeSetting
|
||||
|
||||
from . import forms as order_forms
|
||||
from part.views import PartPricing
|
||||
|
||||
from InvenTree.views import AjaxView, AjaxCreateView, AjaxUpdateView, AjaxDeleteView
|
||||
from InvenTree.helpers import DownloadFile, str2bool
|
||||
@ -1245,6 +1247,18 @@ class SOLineItemCreate(AjaxCreateView):
|
||||
|
||||
return initials
|
||||
|
||||
def save(self, form):
|
||||
ret = form.save()
|
||||
# check if price s set in form - else autoset
|
||||
if not ret.sale_price:
|
||||
price = ret.part.get_price(ret.quantity)
|
||||
# only if price is avail
|
||||
if price:
|
||||
ret.sale_price = price / ret.quantity
|
||||
ret.save()
|
||||
self.object = ret
|
||||
return ret
|
||||
|
||||
|
||||
class SOLineItemEdit(AjaxUpdateView):
|
||||
""" View for editing a SalesOrderLineItem """
|
||||
@ -1407,7 +1421,7 @@ class SalesOrderAssignSerials(AjaxView, FormMixin):
|
||||
except StockItem.DoesNotExist:
|
||||
self.form.add_error(
|
||||
'serials',
|
||||
_('No matching item for serial') + f" '{serial}'"
|
||||
_('No matching item for serial {serial}').format(serial=serial)
|
||||
)
|
||||
continue
|
||||
|
||||
@ -1417,7 +1431,7 @@ class SalesOrderAssignSerials(AjaxView, FormMixin):
|
||||
if not stock_item.in_stock:
|
||||
self.form.add_error(
|
||||
'serials',
|
||||
f"'{serial}' " + _("is not in stock")
|
||||
_('{serial} is not in stock').format(serial=serial)
|
||||
)
|
||||
continue
|
||||
|
||||
@ -1425,7 +1439,7 @@ class SalesOrderAssignSerials(AjaxView, FormMixin):
|
||||
if stock_item.is_allocated():
|
||||
self.form.add_error(
|
||||
'serials',
|
||||
f"'{serial}' " + _("already allocated to an order")
|
||||
_('{serial} already allocated to an order').format(serial=serial)
|
||||
)
|
||||
continue
|
||||
|
||||
@ -1571,3 +1585,101 @@ class SalesOrderAllocationDelete(AjaxDeleteView):
|
||||
ajax_form_title = _("Remove allocation")
|
||||
context_object_name = 'allocation'
|
||||
ajax_template_name = "order/so_allocation_delete.html"
|
||||
|
||||
|
||||
class LineItemPricing(PartPricing):
|
||||
""" View for inspecting part pricing information """
|
||||
|
||||
class EnhancedForm(PartPricing.form_class):
|
||||
pk = IntegerField(widget=HiddenInput())
|
||||
so_line = IntegerField(widget=HiddenInput())
|
||||
|
||||
form_class = EnhancedForm
|
||||
|
||||
def get_part(self, id=False):
|
||||
if 'line_item' in self.request.GET:
|
||||
try:
|
||||
part_id = self.request.GET.get('line_item')
|
||||
part = SalesOrderLineItem.objects.get(id=part_id).part
|
||||
except Part.DoesNotExist:
|
||||
return None
|
||||
elif 'pk' in self.request.POST:
|
||||
try:
|
||||
part_id = self.request.POST.get('pk')
|
||||
part = Part.objects.get(id=part_id)
|
||||
except Part.DoesNotExist:
|
||||
return None
|
||||
else:
|
||||
return None
|
||||
|
||||
if id:
|
||||
return part.id
|
||||
return part
|
||||
|
||||
def get_so(self, pk=False):
|
||||
so_line = self.request.GET.get('line_item', None)
|
||||
if not so_line:
|
||||
so_line = self.request.POST.get('so_line', None)
|
||||
|
||||
if so_line:
|
||||
try:
|
||||
sales_order = SalesOrderLineItem.objects.get(pk=so_line)
|
||||
if pk:
|
||||
return sales_order.pk
|
||||
return sales_order
|
||||
except Part.DoesNotExist:
|
||||
return None
|
||||
return None
|
||||
|
||||
def get_quantity(self):
|
||||
""" Return set quantity in decimal format """
|
||||
qty = Decimal(self.request.GET.get('quantity', 1))
|
||||
if qty == 1:
|
||||
return Decimal(self.request.POST.get('quantity', 1))
|
||||
return qty
|
||||
|
||||
def get_initials(self):
|
||||
initials = super().get_initials()
|
||||
initials['pk'] = self.get_part(id=True)
|
||||
initials['so_line'] = self.get_so(pk=True)
|
||||
return initials
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
# parse extra actions
|
||||
REF = 'act-btn_'
|
||||
act_btn = [a.replace(REF, '') for a in self.request.POST if REF in a]
|
||||
|
||||
# check if extra action was passed
|
||||
if act_btn and act_btn[0] == 'update_price':
|
||||
# get sales order
|
||||
so_line = self.get_so()
|
||||
if not so_line:
|
||||
self.data = {'non_field_errors': [_('Sales order not found')]}
|
||||
else:
|
||||
quantity = self.get_quantity()
|
||||
price = self.get_pricing(quantity).get('unit_part_price', None)
|
||||
|
||||
if not price:
|
||||
self.data = {'non_field_errors': [_('Price not found')]}
|
||||
else:
|
||||
# set normal update note
|
||||
note = _('Updated {part} unit-price to {price}')
|
||||
|
||||
# check qunatity and update if different
|
||||
if so_line.quantity != quantity:
|
||||
so_line.quantity = quantity
|
||||
note = _('Updated {part} unit-price to {price} and quantity to {qty}')
|
||||
|
||||
# update sale_price
|
||||
so_line.sale_price = price
|
||||
so_line.save()
|
||||
|
||||
# parse response
|
||||
data = {
|
||||
'form_valid': True,
|
||||
'success': note.format(part=str(so_line.part), price=str(so_line.sale_price), qty=quantity)
|
||||
}
|
||||
return JsonResponse(data=data)
|
||||
|
||||
# let the normal pricing view run
|
||||
return super().post(request, *args, **kwargs)
|
||||
|
Reference in New Issue
Block a user