mirror of
https://github.com/inventree/InvenTree.git
synced 2025-07-01 11:10:54 +00:00
Merge branch 'decimal-quantity'
This commit is contained in:
19
InvenTree/stock/migrations/0016_auto_20191118_2146.py
Normal file
19
InvenTree/stock/migrations/0016_auto_20191118_2146.py
Normal file
@ -0,0 +1,19 @@
|
||||
# Generated by Django 2.2.5 on 2019-11-18 21:46
|
||||
|
||||
import django.core.validators
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('stock', '0015_auto_20190913_1407'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='stockitem',
|
||||
name='quantity',
|
||||
field=models.DecimalField(decimal_places=5, default=1, max_digits=15, validators=[django.core.validators.MinValueValidator(0)]),
|
||||
),
|
||||
]
|
19
InvenTree/stock/migrations/0017_auto_20191118_2311.py
Normal file
19
InvenTree/stock/migrations/0017_auto_20191118_2311.py
Normal file
@ -0,0 +1,19 @@
|
||||
# Generated by Django 2.2.5 on 2019-11-18 23:11
|
||||
|
||||
import django.core.validators
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('stock', '0016_auto_20191118_2146'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='stockitemtracking',
|
||||
name='quantity',
|
||||
field=models.DecimalField(decimal_places=5, default=1, max_digits=15, validators=[django.core.validators.MinValueValidator(0)]),
|
||||
),
|
||||
]
|
@ -18,6 +18,7 @@ from django.dispatch import receiver
|
||||
|
||||
from mptt.models import TreeForeignKey
|
||||
|
||||
from decimal import Decimal, InvalidOperation
|
||||
from datetime import datetime
|
||||
from InvenTree import helpers
|
||||
|
||||
@ -294,43 +295,43 @@ class StockItem(models.Model):
|
||||
)
|
||||
|
||||
part = models.ForeignKey('part.Part', on_delete=models.CASCADE,
|
||||
related_name='stock_items', help_text='Base part',
|
||||
related_name='stock_items', help_text=_('Base part'),
|
||||
limit_choices_to={
|
||||
'is_template': False,
|
||||
'active': True,
|
||||
})
|
||||
|
||||
supplier_part = models.ForeignKey('company.SupplierPart', blank=True, null=True, on_delete=models.SET_NULL,
|
||||
help_text='Select a matching supplier part for this stock item')
|
||||
help_text=_('Select a matching supplier part for this stock item'))
|
||||
|
||||
location = TreeForeignKey(StockLocation, on_delete=models.DO_NOTHING,
|
||||
related_name='stock_items', blank=True, null=True,
|
||||
help_text='Where is this stock item located?')
|
||||
help_text=_('Where is this stock item located?'))
|
||||
|
||||
belongs_to = models.ForeignKey('self', on_delete=models.DO_NOTHING,
|
||||
related_name='owned_parts', blank=True, null=True,
|
||||
help_text='Is this item installed in another item?')
|
||||
help_text=_('Is this item installed in another item?'))
|
||||
|
||||
customer = models.ForeignKey('company.Company', on_delete=models.SET_NULL,
|
||||
related_name='stockitems', blank=True, null=True,
|
||||
help_text='Item assigned to customer?')
|
||||
help_text=_('Item assigned to customer?'))
|
||||
|
||||
serial = models.PositiveIntegerField(blank=True, null=True,
|
||||
help_text='Serial number for this item')
|
||||
help_text=_('Serial number for this item'))
|
||||
|
||||
URL = InvenTreeURLField(max_length=125, blank=True)
|
||||
|
||||
batch = models.CharField(max_length=100, blank=True, null=True,
|
||||
help_text='Batch code for this stock item')
|
||||
help_text=_('Batch code for this stock item'))
|
||||
|
||||
quantity = models.PositiveIntegerField(validators=[MinValueValidator(0)], default=1)
|
||||
quantity = models.DecimalField(max_digits=15, decimal_places=5, validators=[MinValueValidator(0)], default=1)
|
||||
|
||||
updated = models.DateField(auto_now=True, null=True)
|
||||
|
||||
build = models.ForeignKey(
|
||||
'build.Build', on_delete=models.SET_NULL,
|
||||
blank=True, null=True,
|
||||
help_text='Build for this stock item',
|
||||
help_text=_('Build for this stock item'),
|
||||
related_name='build_outputs',
|
||||
)
|
||||
|
||||
@ -339,7 +340,7 @@ class StockItem(models.Model):
|
||||
on_delete=models.SET_NULL,
|
||||
related_name='stock_items',
|
||||
blank=True, null=True,
|
||||
help_text='Purchase order for this stock item'
|
||||
help_text=_('Purchase order for this stock item')
|
||||
)
|
||||
|
||||
# last time the stock was checked / counted
|
||||
@ -350,7 +351,7 @@ class StockItem(models.Model):
|
||||
|
||||
review_needed = models.BooleanField(default=False)
|
||||
|
||||
delete_on_deplete = models.BooleanField(default=True, help_text='Delete this Stock Item when stock is depleted')
|
||||
delete_on_deplete = models.BooleanField(default=True, help_text=_('Delete this Stock Item when stock is depleted'))
|
||||
|
||||
status = models.PositiveIntegerField(
|
||||
default=StockStatus.OK,
|
||||
@ -510,6 +511,11 @@ class StockItem(models.Model):
|
||||
if self.serialized:
|
||||
return
|
||||
|
||||
try:
|
||||
quantity = Decimal(quantity)
|
||||
except (InvalidOperation, ValueError):
|
||||
return
|
||||
|
||||
# Doesn't make sense for a zero quantity
|
||||
if quantity <= 0:
|
||||
return
|
||||
@ -549,7 +555,10 @@ class StockItem(models.Model):
|
||||
quantity: If provided, override the quantity (default = total stock quantity)
|
||||
"""
|
||||
|
||||
quantity = int(kwargs.get('quantity', self.quantity))
|
||||
try:
|
||||
quantity = Decimal(kwargs.get('quantity', self.quantity))
|
||||
except InvalidOperation:
|
||||
return False
|
||||
|
||||
if quantity <= 0:
|
||||
return False
|
||||
@ -599,12 +608,19 @@ class StockItem(models.Model):
|
||||
if self.serialized:
|
||||
return
|
||||
|
||||
try:
|
||||
self.quantity = Decimal(quantity)
|
||||
except (InvalidOperation, ValueError):
|
||||
return
|
||||
|
||||
if quantity < 0:
|
||||
quantity = 0
|
||||
|
||||
self.quantity = quantity
|
||||
|
||||
if quantity <= 0 and self.delete_on_deplete and self.can_delete():
|
||||
if quantity == 0 and self.delete_on_deplete and self.can_delete():
|
||||
|
||||
# TODO - Do not actually "delete" stock at this point - instead give it a "DELETED" flag
|
||||
self.delete()
|
||||
return False
|
||||
else:
|
||||
@ -618,7 +634,10 @@ class StockItem(models.Model):
|
||||
record the date of stocktake
|
||||
"""
|
||||
|
||||
count = int(count)
|
||||
try:
|
||||
count = Decimal(count)
|
||||
except InvalidOperation:
|
||||
return False
|
||||
|
||||
if count < 0 or self.infinite:
|
||||
return False
|
||||
@ -646,7 +665,10 @@ class StockItem(models.Model):
|
||||
if self.serialized:
|
||||
return False
|
||||
|
||||
quantity = int(quantity)
|
||||
try:
|
||||
quantity = Decimal(quantity)
|
||||
except InvalidOperation:
|
||||
return False
|
||||
|
||||
# Ignore amounts that do not make sense
|
||||
if quantity <= 0 or self.infinite:
|
||||
@ -670,7 +692,10 @@ class StockItem(models.Model):
|
||||
if self.serialized:
|
||||
return False
|
||||
|
||||
quantity = int(quantity)
|
||||
try:
|
||||
quantity = Decimal(quantity)
|
||||
except InvalidOperation:
|
||||
return False
|
||||
|
||||
if quantity <= 0 or self.infinite:
|
||||
return False
|
||||
@ -691,7 +716,7 @@ class StockItem(models.Model):
|
||||
sn=self.serial)
|
||||
else:
|
||||
s = '{n} x {part}'.format(
|
||||
n=self.quantity,
|
||||
n=helpers.decimal2string(self.quantity),
|
||||
part=self.part.full_name)
|
||||
|
||||
if self.location:
|
||||
@ -722,17 +747,17 @@ class StockItemTracking(models.Model):
|
||||
|
||||
date = models.DateTimeField(auto_now_add=True, editable=False)
|
||||
|
||||
title = models.CharField(blank=False, max_length=250, help_text='Tracking entry title')
|
||||
title = models.CharField(blank=False, max_length=250, help_text=_('Tracking entry title'))
|
||||
|
||||
notes = models.CharField(blank=True, max_length=512, help_text='Entry notes')
|
||||
notes = models.CharField(blank=True, max_length=512, help_text=_('Entry notes'))
|
||||
|
||||
URL = InvenTreeURLField(blank=True, help_text='Link to external page for further information')
|
||||
URL = InvenTreeURLField(blank=True, help_text=_('Link to external page for further information'))
|
||||
|
||||
user = models.ForeignKey(User, on_delete=models.SET_NULL, blank=True, null=True)
|
||||
|
||||
system = models.BooleanField(default=False)
|
||||
|
||||
quantity = models.PositiveIntegerField(validators=[MinValueValidator(0)], default=1)
|
||||
quantity = models.DecimalField(max_digits=15, decimal_places=5, validators=[MinValueValidator(0)], default=1)
|
||||
|
||||
# TODO
|
||||
# image = models.ImageField(upload_to=func, max_length=255, null=True, blank=True)
|
||||
|
@ -1,5 +1,6 @@
|
||||
{% extends "stock/stock_app_base.html" %}
|
||||
{% load static %}
|
||||
{% load inventree_extras %}
|
||||
{% load i18n %}
|
||||
{% block content %}
|
||||
|
||||
@ -86,7 +87,7 @@
|
||||
{% else %}
|
||||
<tr>
|
||||
<td>{% trans "Quantity" %}</td>
|
||||
<td>{{ item.quantity }}</td>
|
||||
<td>{% decimal item.quantity %} {% if item.part.units %}{{ item.part.units }}{% endif %}</td>
|
||||
</tr>
|
||||
{% endif %}
|
||||
{% if item.batch %}
|
||||
|
@ -245,7 +245,7 @@ class StockTest(TestCase):
|
||||
w1 = StockItem.objects.get(pk=100)
|
||||
w2 = StockItem.objects.get(pk=101)
|
||||
|
||||
# Take 25 units from w1
|
||||
# Take 25 units from w1 (there are only 10 in stock)
|
||||
w1.take_stock(30, None, notes='Took 30')
|
||||
|
||||
# Get from database again
|
||||
|
@ -20,6 +20,8 @@ from InvenTree.views import QRCodeView
|
||||
|
||||
from InvenTree.helpers import str2bool, DownloadFile, GetExportFormats
|
||||
from InvenTree.helpers import ExtractSerialNumbers
|
||||
|
||||
from decimal import Decimal, InvalidOperation
|
||||
from datetime import datetime
|
||||
|
||||
from company.models import Company
|
||||
@ -91,7 +93,7 @@ class StockLocationEdit(AjaxUpdateView):
|
||||
form_class = EditStockLocationForm
|
||||
context_object_name = 'location'
|
||||
ajax_template_name = 'modal_form.html'
|
||||
ajax_form_title = 'Edit Stock Location'
|
||||
ajax_form_title = _('Edit Stock Location')
|
||||
|
||||
def get_form(self):
|
||||
""" Customize form data for StockLocation editing.
|
||||
@ -115,7 +117,7 @@ class StockLocationEdit(AjaxUpdateView):
|
||||
class StockLocationQRCode(QRCodeView):
|
||||
""" View for displaying a QR code for a StockLocation object """
|
||||
|
||||
ajax_form_title = "Stock Location QR code"
|
||||
ajax_form_title = _("Stock Location QR code")
|
||||
|
||||
def get_qr_data(self):
|
||||
""" Generate QR code data for the StockLocation """
|
||||
@ -130,7 +132,7 @@ class StockExportOptions(AjaxView):
|
||||
""" Form for selecting StockExport options """
|
||||
|
||||
model = StockLocation
|
||||
ajax_form_title = 'Stock Export Options'
|
||||
ajax_form_title = _('Stock Export Options')
|
||||
form_class = ExportOptionsForm
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
@ -238,7 +240,7 @@ class StockExport(AjaxView):
|
||||
class StockItemQRCode(QRCodeView):
|
||||
""" View for displaying a QR code for a StockItem object """
|
||||
|
||||
ajax_form_title = "Stock Item QR Code"
|
||||
ajax_form_title = _("Stock Item QR Code")
|
||||
|
||||
def get_qr_data(self):
|
||||
""" Generate QR code data for the StockItem """
|
||||
@ -261,7 +263,7 @@ class StockAdjust(AjaxView, FormMixin):
|
||||
"""
|
||||
|
||||
ajax_template_name = 'stock/stock_adjust.html'
|
||||
ajax_form_title = 'Adjust Stock'
|
||||
ajax_form_title = _('Adjust Stock')
|
||||
form_class = AdjustStockForm
|
||||
stock_items = []
|
||||
|
||||
@ -398,8 +400,9 @@ class StockAdjust(AjaxView, FormMixin):
|
||||
valid = form.is_valid()
|
||||
|
||||
for item in self.stock_items:
|
||||
|
||||
try:
|
||||
item.new_quantity = int(item.new_quantity)
|
||||
item.new_quantity = Decimal(item.new_quantity)
|
||||
except ValueError:
|
||||
item.error = _('Must enter integer value')
|
||||
valid = False
|
||||
@ -543,7 +546,7 @@ class StockAdjust(AjaxView, FormMixin):
|
||||
if destination == item.location and item.new_quantity == item.quantity:
|
||||
continue
|
||||
|
||||
item.move(destination, note, self.request.user, quantity=int(item.new_quantity))
|
||||
item.move(destination, note, self.request.user, quantity=item.new_quantity)
|
||||
|
||||
count += 1
|
||||
|
||||
@ -582,7 +585,7 @@ class StockItemEdit(AjaxUpdateView):
|
||||
form_class = EditStockItemForm
|
||||
context_object_name = 'item'
|
||||
ajax_template_name = 'modal_form.html'
|
||||
ajax_form_title = 'Edit Stock Item'
|
||||
ajax_form_title = _('Edit Stock Item')
|
||||
|
||||
def get_form(self):
|
||||
""" Get form for StockItem editing.
|
||||
@ -618,7 +621,7 @@ class StockLocationCreate(AjaxCreateView):
|
||||
form_class = EditStockLocationForm
|
||||
context_object_name = 'location'
|
||||
ajax_template_name = 'modal_form.html'
|
||||
ajax_form_title = 'Create new Stock Location'
|
||||
ajax_form_title = _('Create new Stock Location')
|
||||
|
||||
def get_initial(self):
|
||||
initials = super(StockLocationCreate, self).get_initial().copy()
|
||||
@ -639,7 +642,7 @@ class StockItemSerialize(AjaxUpdateView):
|
||||
|
||||
model = StockItem
|
||||
ajax_template_name = 'stock/item_serialize.html'
|
||||
ajax_form_title = 'Serialize Stock'
|
||||
ajax_form_title = _('Serialize Stock')
|
||||
form_class = SerializeStockForm
|
||||
|
||||
def get_initial(self):
|
||||
@ -719,7 +722,7 @@ class StockItemCreate(AjaxCreateView):
|
||||
form_class = CreateStockItemForm
|
||||
context_object_name = 'item'
|
||||
ajax_template_name = 'modal_form.html'
|
||||
ajax_form_title = 'Create new Stock Item'
|
||||
ajax_form_title = _('Create new Stock Item')
|
||||
|
||||
def get_form(self):
|
||||
""" Get form for StockItem creation.
|
||||
@ -783,7 +786,7 @@ class StockItemCreate(AjaxCreateView):
|
||||
try:
|
||||
original = StockItem.objects.get(pk=item_to_copy)
|
||||
initials = model_to_dict(original)
|
||||
self.ajax_form_title = "Copy Stock Item"
|
||||
self.ajax_form_title = _("Copy Stock Item")
|
||||
except StockItem.DoesNotExist:
|
||||
initials = super(StockItemCreate, self).get_initial().copy()
|
||||
|
||||
@ -828,11 +831,12 @@ class StockItemCreate(AjaxCreateView):
|
||||
part_id = form['part'].value()
|
||||
try:
|
||||
part = Part.objects.get(id=part_id)
|
||||
quantity = int(form['quantity'].value())
|
||||
except (Part.DoesNotExist, ValueError):
|
||||
quantity = Decimal(form['quantity'].value())
|
||||
except (Part.DoesNotExist, ValueError, InvalidOperation):
|
||||
part = None
|
||||
quantity = 1
|
||||
valid = False
|
||||
form.errors['quantity'] = [_('Invalid quantity')]
|
||||
|
||||
if part is None:
|
||||
form.errors['part'] = [_('Invalid part selection')]
|
||||
@ -914,7 +918,7 @@ class StockLocationDelete(AjaxDeleteView):
|
||||
success_url = '/stock'
|
||||
ajax_template_name = 'stock/location_delete.html'
|
||||
context_object_name = 'location'
|
||||
ajax_form_title = 'Delete Stock Location'
|
||||
ajax_form_title = _('Delete Stock Location')
|
||||
|
||||
|
||||
class StockItemDelete(AjaxDeleteView):
|
||||
@ -927,7 +931,7 @@ class StockItemDelete(AjaxDeleteView):
|
||||
success_url = '/stock/'
|
||||
ajax_template_name = 'stock/item_delete.html'
|
||||
context_object_name = 'item'
|
||||
ajax_form_title = 'Delete Stock Item'
|
||||
ajax_form_title = _('Delete Stock Item')
|
||||
|
||||
|
||||
class StockItemTrackingDelete(AjaxDeleteView):
|
||||
@ -938,7 +942,7 @@ class StockItemTrackingDelete(AjaxDeleteView):
|
||||
|
||||
model = StockItemTracking
|
||||
ajax_template_name = 'stock/tracking_delete.html'
|
||||
ajax_form_title = 'Delete Stock Tracking Entry'
|
||||
ajax_form_title = _('Delete Stock Tracking Entry')
|
||||
|
||||
|
||||
class StockTrackingIndex(ListView):
|
||||
@ -955,7 +959,7 @@ class StockItemTrackingEdit(AjaxUpdateView):
|
||||
""" View for editing a StockItemTracking object """
|
||||
|
||||
model = StockItemTracking
|
||||
ajax_form_title = 'Edit Stock Tracking Entry'
|
||||
ajax_form_title = _('Edit Stock Tracking Entry')
|
||||
form_class = TrackingEntryForm
|
||||
|
||||
|
||||
@ -964,7 +968,7 @@ class StockItemTrackingCreate(AjaxCreateView):
|
||||
"""
|
||||
|
||||
model = StockItemTracking
|
||||
ajax_form_title = "Add Stock Tracking Entry"
|
||||
ajax_form_title = _("Add Stock Tracking Entry")
|
||||
form_class = TrackingEntryForm
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
|
Reference in New Issue
Block a user