Hello,
I am relatively new to Django and this is my first post here. This is also my first real project with Django.
I want to be able to update the quantity of product whenever items are received or sold. I used Class Based Views.
I have tried to implement the various methods of doing this I found on the internet. None worked for me.
I just had to come here to seek for help.
Thanks in advance for any help.
Here are my respective models:
Product
from django.db import models
from datetime import datetime
from django.urls import reverse
from pharmacycategory.models import TblPharmacyCategory
from pharmacybrand.models import TblPharmacyBrand
from pharmacydepartment.models import TblPharmacyDepartment
from pharmacypresentation.models import TblPharmacyPresentation
from tariff.models import TblTariff
class TblPharmacyProduct(models.Model):
date = models.DateField(default=datetime.now, blank=False)
item = models.ForeignKey(TblTariff, on_delete = models.CASCADE,null=True, blank=True )
category = models.ForeignKey(TblPharmacyCategory, on_delete=models.CASCADE)
brand = models.ForeignKey(TblPharmacyBrand, on_delete = models.CASCADE,null=True, blank=True )
department = models.ForeignKey(TblPharmacyDepartment, on_delete = models.CASCADE,null=True, blank=True )
presentation = models.ForeignKey(TblPharmacyPresentation, on_delete = models.CASCADE,null=True, blank=True )
unitpack = models.IntegerField(default=0)
quantity = models.IntegerField(default=0)
unitcost = models.DecimalField(max_digits=10, decimal_places=2, default=0.00)
unitprice = models.DecimalField(max_digits=10, decimal_places=2, default=0.00)
reorderlevel = models.IntegerField(default=0)
reorderquantity = models.IntegerField(default=0)
expirydate = models.DateField(default=datetime.now, blank=False)
notes = models.TextField()
def __str__(self):
return str(self.item)
class Meta:
db_table = 'tblpharmacyproduct'
ordering = ['department']
#objects = models.Manager()
#broswer = ProductManager()
def get_absolute_url(self):
return reverse('pharmacyproduct:pharmacyproduct-detail', kwargs={'pk': self.pk})
Sales
from django.db import models
from datetime import datetime
from django.urls import reverse
from pharmacyproduct.models import TblPharmacyProduct
#from django.db.models.signals import post_delete
#from django.dispatch import receiver
class TblPharmacySales(models.Model):
date = models.DateField(default=datetime.now, blank=False)
invoiceno = models.CharField(max_length=20, null=True, blank=True)
hospitalno = models.CharField(max_length=20, null=True, blank=True)
patient = models.CharField(max_length=100, null=True, blank=True)
item = models.ForeignKey(TblPharmacyProduct, on_delete = models.CASCADE)
quantity = models.IntegerField(default=0)
unitprice = models.DecimalField(max_digits=10, decimal_places=2, default=0.00)
discount = models.DecimalField(max_digits=10, decimal_places=2, default=0.00)
amountreceived = models.DecimalField(max_digits=10, decimal_places=2, default=0.00)
def __str__(self):
return self.item
class Meta:
db_table = 'tblpharmacysales'
ordering = ['item']
def get_absolute_url(self):
return reverse('pharmacysales:pharmacysales-detail', kwargs={'pk': self.pk})
Receives (Purchases)
from django.db import models
from datetime import datetime
from django.urls import reverse
from pharmacyproduct.models import TblPharmacyProduct
class TblPharmacyReceives(models.Model):
date = models.DateField(default=datetime.now, blank=False)
supplier = models.CharField(max_length=20, null=True, blank=True)
invoiceno = models.CharField(max_length=20, null=True, blank=True)
item = models.ForeignKey(TblPharmacyProduct, on_delete = models.CASCADE)
quantity = models.IntegerField(default=0)
unitcost = models.DecimalField(max_digits=10, decimal_places=2, default=0.00)
unitprice = models.DecimalField(max_digits=10, decimal_places=2, default=0.00)
expirydate = models.DateField(default=datetime.now, blank=False)
def __str__(self):
return self.item
class Meta:
db_table = 'tblpharmacyreceives'
ordering = ['item']
def get_absolute_url(self):
return reverse('pharmacyreceives:pharmacyreceives-detail', kwargs={'pk': self.pk})