Hello…
I am working on a project where i creating Sale Return, basically i am searching sale invocie id and then retrieve the sale invoice and sale invoice item data and then creates sale return. sale invocie data will be stored in sale return and sal einvocie item data will be stored in sale return item
But there i sone issue after retrieving data the sale return data is saving but save return itme data is not saving.It is also not showing any error the main issue is it i snot getting the id of item
my models are these
class SaleInvoice(models.Model):
user = models.ForeignKey(
get_user_model(),
on_delete=models.CASCADE,
)
searchCustomer=models.ForeignKey(Customer, on_delete=models.CASCADE, related_name='customersaleinvoice',null=True)
PAYMENT_CHOICES = (
('paid', 'Paid'),
('unpaid', 'Unpaid'),
)
paymentType = models.CharField(max_length=15, choices=PAYMENT_CHOICES,default=None)
SHIPMENT_CHOICES = (
('1', 'Delivery'),
('2', 'Pick Up'),
)
shipmentType=models.CharField(max_length=1, choices=SHIPMENT_CHOICES,default=None)
searchVehicle=models.ForeignKey(Vehicle, on_delete=models.CASCADE, related_name='vehiclesaleinvoice',null=True)
searchDriver=models.ForeignKey(Driver, on_delete=models.CASCADE, related_name='driversaleinvoice',null=True)
selectNTNInInvoice=models.ForeignKey(NTN, on_delete=models.CASCADE, related_name='selectntn',null=True)
invoiceDate=models.DateField(blank=True, null=True)
totalSaleAmount=models.CharField(max_length=15,null=True)
saleInvoiceAmount=models.CharField(max_length=15,null=True)
saleInvoiceDiscount=models.CharField(max_length=15,null=True)
STATUS_CHOICES = (
('in_progress', 'In Progress'),
('delivered', 'Delivered'),
('not_delivered', 'Not Delivered'),
)
shipmentStatus = models.CharField(max_length=20,choices=STATUS_CHOICES,default='in_progress',)
class SaleReturn(models.Model):
user = models.ForeignKey(
get_user_model(),
on_delete=models.CASCADE,
)
customerSaleReturn=models.TextField(null=True)
paymentTypeSaleReturn = models.TextField(null=True)
shipmentTypeSaleReturn=models.TextField(null=True)
searchVehicleSaleReturn=models.TextField(null=True)
searchDriverSaleReturn=models.TextField(null=True)
selectNTNInInvoiceSaleReturn=models.CharField(max_length=15,null=True)
invoiceDateSaleReturn=models.DateField(blank=True, null=True)
totalSaleAmountSaleReturn=models.CharField(max_length=15,null=True)
saleInvoiceAmountSaleReturn=models.CharField(max_length=15,null=True)
saleInvoiceDiscountSaleReturn=models.CharField(max_length=15,null=True)
STATUS_CHOICES = (
('in_progress', 'In Progress'),
('delivered', 'Delivered'),
('not_delivered', 'Not Delivered'),
)
shipmentStatusSaleReturn = models.CharField(max_length=20,choices=STATUS_CHOICES,default='in_progress',)
searchSaleInvoice=models.ForeignKey(SaleInvoice, on_delete=models.CASCADE, related_name='saleinvoiceid',null=True)
class SaleInvoiceItem(models.Model):
saleinvoice = models.ForeignKey(
SaleInvoice,
on_delete=models.CASCADE,related_name="saleinvoiceitem"
)
saleInvoiceItemName=models.ForeignKey(Egg, on_delete=models.CASCADE, related_name='saleinvoiceitemegg',null=True)
saleInvoiceItemQuantity=models.CharField(max_length=15,null=True)
saleInvoiceItemUnitPrice=models.CharField(max_length=15,null=True)
saleInvoiceItemTotalAmount=models.CharField(max_length=15,null=True)
saleInvoiceItemDiscount=models.CharField(max_length=15,null=True)
saleInvoiceItemDiscountPrice=models.CharField(max_length=15,null=True)
class SaleReturnItem(models.Model):
salereturn = models.ForeignKey(
SaleReturn,
on_delete=models.CASCADE,related_name="salereturnitem"
)
saleReturnItemName=models.ForeignKey(Egg, on_delete=models.CASCADE, related_name='salereturnitem',null=True)
saleReturnItemQuantity=models.CharField(max_length=15,null=True)
saleReturnItemUnitPrice=models.CharField(max_length=15,null=True)
saleReturnItemTotalAmount=models.CharField(max_length=15,null=True)
saleReturnItemDiscount=models.CharField(max_length=15,null=True)
saleReturnItemDiscountPrice=models.CharField(max_length=15,null=True)
and here i smy view function
def saleReturn(request):
if not request.user.is_authenticated:
return redirect('login')
elif request.user.userRole != 'admin':
return render(request, 'pages/404.html')
else:
if request.method == "POST":
try:
searchSaleInvoiceId = request.POST.get('searchSaleInvoice')
searchSaleInvoice = SaleInvoice.objects.get(id=searchSaleInvoiceId)
invoiceDateSaleReturn = request.POST.get('invoiceDateSaleReturn')
saleReturnObject = SaleReturn(
user=request.user,
searchSaleInvoice=searchSaleInvoice,
invoiceDateSaleReturn=invoiceDateSaleReturn,
)
saleReturnObject.invoiceDateSaleReturn = date.today()
saleReturnObject.save()
saleReturnItemNameId = request.POST.get('saleReturnItemName')
print("saleReturnItemNameId",saleReturnItemNameId)
# saleReturnItem = Egg.objects.get(id=saleReturnItemNameId)
# print("-----------------1----------------------")
# saleReturnItemNameId = saleReturnItem.id
saleReturnItemQuantity = request.POST.getlist('saleReturnItemQuantity')
saleReturnItemUnitPrice = request.POST.getlist('saleReturnItemUnitPrice')
saleReturnItemDiscount = request.POST.getlist('saleReturnItemDiscount')
totalSaleAmountSaleReturn = 0.0
saleInvoiceAmountSaleReturn = 0.0
saleInvoiceDiscountSaleReturn = 0.0
for quantity, price, discount, in zip(
saleReturnItemQuantity, saleReturnItemUnitPrice, saleReturnItemDiscount
):
total_amount = int(quantity) * float(price)
if discount == '':
discount = 0.0
else:
discount = float(discount)
discounted_amount = total_amount - discount
# item_name = saleReturnItemNames.get(id=item_id)
saleReturnItemObject = SaleReturnItem(
salereturn=saleReturnObject,
saleReturnItemName_id=saleReturnItemNameId,
saleReturnItemQuantity=quantity,
saleReturnItemUnitPrice=price,
saleReturnItemTotalAmount=str(total_amount),
saleReturnItemDiscount=discount,
saleReturnItemDiscountPrice=str(discounted_amount)
)
saleReturnItemObject.save()
# Adding Item in Egg
Egg.objects.filter(id=item_name.saleInvoiceItemName.id).update(
eggStockQuantity=F('eggStockQuantity') + int(quantity)
)
totalSaleAmountSaleReturn += total_amount
saleInvoiceAmountSaleReturn += discounted_amount
saleInvoiceDiscountSaleReturn += discount
saleReturnObject.totalSaleAmountSaleReturn = float(totalSaleAmountSaleReturn)
saleReturnObject.saleInvoiceAmountSaleReturn = float(saleInvoiceAmountSaleReturn)
saleReturnObject.saleInvoiceDiscountSaleReturn = float(saleInvoiceDiscountSaleReturn)
saleReturnObject.save()
messages.success(request, 'Record Saved Successfully.')
return redirect('saleReturn')
except Exception as e:
messages.error(request, f'Some Error Occurred While Saving Data: {str(e)}')
saleReturnData = SaleReturn.objects.all()
results = SaleInvoice.objects.all()
returnedItems = SaleInvoiceItem.objects.all()
return render(request, 'pages/addSaleReturn.html', {'saleReturnData': saleReturnData, 'results': results, 'returnedItems': returnedItems})
I am printing message in termianl and it gave me this
saleReturnItemNameId None
here is my html code
<tbody class="table-border-bottom-0" id="item-table-body">
<tr class="item-row">
<td><h6 class="item-number"></h6></td>
<td>
<input type="text" for="saleReturnItemName" id="saleReturnItemNameId" name="saleReturnItemName" class="form-control item-name" placeholder="Search" style="height: 40px; width: 150px;"/>
<!-- <input type="text" id="saleReturnItemNameId" name="saleReturnItemName" class="form-control item-name" placeholder="Search" style="height: 40px; width: 150px;"/> -->
</td>
<td>
<input for="saleReturnItemQuantity" type="number" class="form-control item-quantity" id="saleReturnItemQuantityId" name="saleReturnItemQuantity" style="height: 40px; width: 150px;"/>
</td>
<td>
<input type="string" class="form-control item-price" id="saleReturnItemUnitPriceId" name="saleReturnItemUnitPrice" />
</td>
<td>
<input type="number" class="form-control item-total-amount" id="saleReturnItemTotalAmountId" name="saleReturnItemTotalAmount"/>
</td>
<td>
<input type="number" class="form-control item-discount" id="saleReturnItemDiscountId" name="saleReturnItemDiscount" />
</td>
<td>
<input type="number" class="form-control item-discounted-amount" id="saleReturnItemDiscountPriceId" name="saleReturnItemDiscountPrice"/>
</td>
</tr>
</tbody>