Skip to content

Instantly share code, notes, and snippets.

@craigderington
Created January 26, 2017 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craigderington/18c60733d3af2554c8444f6ad2387353 to your computer and use it in GitHub Desktop.
Save craigderington/18c60733d3af2554c8444f6ad2387353 to your computer and use it in GitHub Desktop.
class Item(TimeStampedModel):
LEADTIME_PERIOD = (
('D', 'DAYS'),
('W', 'WEEKS'),
('M', 'MONTHS'),
)
sku = models.CharField(max_length=50, blank=False, null=False)
item_name = models.CharField(max_length=50, blank=False, null=False)
item_description = models.TextField()
item_search = models.CharField(max_length=50, blank=False, null=False)
item_category = models.ForeignKey('ItemCategory')
item_group = models.ForeignKey('ItemGroup')
item_lead_time = models.PositiveIntegerField()
item_lead_time_period = models.CharField(max_length=1, choices=LEADTIME_PERIOD,
default='D', null=True, blank=True)
reorder_point = models.PositiveIntegerField()
unit_price = models.DecimalField(max_digits=5, decimal_places=2, blank=True)
unit_cost = models.DecimalField(max_digits=5, decimal_places=2, blank=True)
manufacturer = models.ForeignKey(Manufacturer, on_delete=models.CASCADE)
vendor = models.ForeignKey(Vendor, on_delete=models.CASCADE)
total_qty_on_hand = models.PositiveSmallIntegerField(default=0, null=True, blank=True)
total_qty_on_order = models.PositiveSmallIntegerField(default=0, null=True, blank=True)
total_qty_in_transit = models.PositiveIntegerField(default=0, null=True, blank=True)
is_consumable = models.BooleanField(default=False)
def update_totals(self):
total_qty_on_hand = 0
total_qty_on_order = 0
total_qty_in_transit = 0
for item in self.itemlocation_set.all():
total_qty_on_hand += item.qty_on_hand
total_qty_on_order += item.qty_on_order
total_qty_in_transit += item.qty_in_transit
self.total_qty_on_hand = total_qty_on_hand
self.total_qty_on_order = total_qty_on_order
self.total_qty_in_transit = total_qty_in_transit
self.save()
def __unicode__(self):
return unicode(str(self.item_name + '-' + self.sku))
class Meta:
ordering = ('item_name',)
class ItemLocation(models.Model):
item = models.ForeignKey(Item, on_delete=models.CASCADE)
location = models.ForeignKey(Location, on_delete=models.CASCADE)
qty_on_hand = models.PositiveIntegerField()
qty_on_order = models.PositiveIntegerField()
qty_in_transit = models.PositiveIntegerField()
def __unicode__(self):
return "%s - %s - %s" % (self.item, self.location, self.qty_on_hand)
class Meta:
verbose_name = 'Items By Location'
ordering = ('-id',)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment