Merge branch 'stock-improvements'

This commit is contained in:
Oliver Walters 2019-04-13 00:15:39 +10:00
commit 1dcbff9bcd
6 changed files with 67 additions and 19 deletions

View File

@ -38,6 +38,8 @@ class CreateStockItemForm(HelperForm):
class MoveStockItemForm(forms.ModelForm): class MoveStockItemForm(forms.ModelForm):
note = forms.CharField(label='Notes', required=True)
class Meta: class Meta:
model = StockItem model = StockItem
@ -62,7 +64,7 @@ class EditStockItemForm(HelperForm):
model = StockItem model = StockItem
fields = [ fields = [
'quantity',
'batch', 'batch',
'status', 'status',
'notes'
] ]

View File

@ -0,0 +1,19 @@
# Generated by Django 2.2 on 2019-04-12 14:09
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('stock', '0004_auto_20190412_2030'),
]
operations = [
migrations.AddField(
model_name='stockitemtracking',
name='quantity',
field=models.PositiveIntegerField(default=1, validators=[django.core.validators.MinValueValidator(0)]),
),
]

View File

@ -209,6 +209,7 @@ class StockItem(models.Model):
item=self, item=self,
title=title, title=title,
user=user, user=user,
quantity=self.quantity,
date=datetime.now().date(), date=datetime.now().date(),
notes=notes, notes=notes,
system=system system=system
@ -217,21 +218,24 @@ class StockItem(models.Model):
track.save() track.save()
@transaction.atomic @transaction.atomic
def move(self, location, user): def move(self, location, notes, user):
if location == self.location: if location.pk == self.location.pk:
return return False # raise forms.ValidationError("Cannot move item to its current location")
note = "Moved to {loc}".format(loc=location.name) msg = "Moved to {loc} (from {src})".format(loc=location.name,
src=self.location.name)
self.location = location self.location = location
self.save() self.save()
self.add_transaction_note('Transfer', self.add_transaction_note(msg,
user, user,
notes=note, notes=notes,
system=True) system=True)
return True
@transaction.atomic @transaction.atomic
def stocktake(self, count, user, notes=''): def stocktake(self, count, user, notes=''):
@ -343,6 +347,9 @@ class StockItemTracking(models.Model):
# Was this tracking note auto-generated by the system? # Was this tracking note auto-generated by the system?
system = models.BooleanField(default=False) system = models.BooleanField(default=False)
# Keep track of the StockItem quantity throughout the tracking history
quantity = models.PositiveIntegerField(validators=[MinValueValidator(0)], default=1)
# TODO # TODO
# image = models.ImageField(upload_to=func, max_length=255, null=True, blank=True) # image = models.ImageField(upload_to=func, max_length=255, null=True, blank=True)

View File

@ -35,6 +35,7 @@ class StockTrackingSerializer(serializers.ModelSerializer):
'date', 'date',
'title', 'title',
'notes', 'notes',
'quantity',
'user', 'user',
'system', 'system',
] ]
@ -43,6 +44,7 @@ class StockTrackingSerializer(serializers.ModelSerializer):
'date', 'date',
'user', 'user',
'system', 'system',
'quantity',
] ]

View File

@ -15,11 +15,11 @@
<span class="caret"></span></button> <span class="caret"></span></button>
<ul class="dropdown-menu"> <ul class="dropdown-menu">
{% if item.in_stock %} {% if item.in_stock %}
<li><a href='#' id='stock-stocktake' title='Count stock'>Stocktake</a></li>
<li><a href='#' id='stock-add' title='Add stock'>Add to stock</a></li>
<li><a href='#' id='stock-remove' title='Remove stock'>Remove from stock</a></li>
<li><a href="#" id='stock-edit' title='Edit stock item'>Edit stock item</a></li> <li><a href="#" id='stock-edit' title='Edit stock item'>Edit stock item</a></li>
<li><a href="#" id='stock-move' title='Move stock item'>Move stock item</a></li> <li><a href="#" id='stock-move' title='Move stock item'>Move stock item</a></li>
<li><a href='#' id='stock-add' title='Add stock'>Add to stock</a></li>
<li><a href='#' id='stock-remove' title='Remove stock'>Remove from stock</a></li>
<li><a href='#' id='stock-stocktake' title='Count stock'>Stocktake</a></li>
{% endif %} {% endif %}
<li><a href="#" id='stock-delete' title='Delete stock item'>Delete stock item</a></li> <li><a href="#" id='stock-delete' title='Delete stock item'>Delete stock item</a></li>
</ul> </ul>
@ -215,12 +215,24 @@
return html; return html;
} }
}, },
{
field: 'quantity',
title: 'Quantity',
},
{ {
sortable: true, sortable: true,
field: 'user', field: 'user',
title: 'User', title: 'User',
formatter: function(value, row, index, field) { formatter: function(value, row, index, field) {
return value.username; if (value)
{
// TODO - Format the user's first and last names
return value.username;
}
else
{
return "No user information";
}
} }
} }
], ],

View File

@ -142,20 +142,26 @@ class StockItemMove(AjaxUpdateView):
if form.is_valid(): if form.is_valid():
obj = form.save() obj = self.get_object()
try: try:
loc = StockLocation.objects.get(pk=form['location'].value()) loc_id = form['location'].value()
loc_path = loc.pathstring
if loc_id:
loc = StockLocation.objects.get(pk=form['location'].value())
if str(loc.pk) == str(obj.pk):
form.errors['location'] = ['Item is already in this location']
else:
obj.move(loc, form['note'].value(), request.user)
else:
form.errors['location'] = ['Cannot move to an empty location']
except StockLocation.DoesNotExist: except StockLocation.DoesNotExist:
loc_path = '' loc_path = ''
form.errors['location'] = ['Location does not exist']
obj.add_transaction_note("Moved item to '{where}'".format(where=loc_path),
request.user,
system=True)
data = { data = {
'form_valid': form.is_valid(), 'form_valid': form.is_valid() and len(form.errors) == 0,
} }
return self.renderJsonResponse(request, form, data) return self.renderJsonResponse(request, form, data)