Paid and remain amount always saved null in database
Revision 1
paid and remain Amount always saved null in database
although
i get value is not null by javascript
What I have tried:
my storedProcedure
USE [OnlineMarket]
GO
/****** Object: StoredProcedure [dbo].[InsertintoSalesInvoice] Script Date: 10/07/2023 09:02:32 م ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[InsertintoSalesInvoice]
@InvoiceNo int,
@ItemId decimal(10,2),@SalePrice decimal(10,2),
@Qty decimal(10,2),
@Discount decimal(10,2),
--@amountAfterDiscount decimal(10,2),
@Paid decimal(10,2),
@Remain decimal(10,2),
@Total decimal(10,2),
@Paiddate date,
@CustomerId int
AS
BEGIN
declare @Btotal dec
declare @count dec
declare @countSale dec
declare @Bdate date
declare @pd dec
declare @AmountAfterdiscount dec
select @Bdate=getdate()
select @countSale=count(*) from TbInvoiceSales where
InvoiceDate=@Bdate and
InvoiceNo=@InvoiceNo and
ItemId=@ItemId
select @pd =(Isnull(@Discount,1)*((@Qty*@SalePrice))-(@Qty*@SalePrice))-@Paid
select @AmountAfterdiscount=(@Qty*@SalePrice)-(Isnull(@Discount/100,1))*((@Qty*@SalePrice))
if @countSale=0
INSERT INTO [dbo].[TbInvoiceSales]
([InvoiceDate]
,[InvoiceNo]
,[ItemId]
,[SalePrice]
,[Qty]
,[Discount]
,[total]
,[Paid]
,[Remain]
,[Paiddate])
values
(@Bdate,@InvoiceNo,@ItemId,@SalePrice,@Qty,Isnull(@Discount,1),@AmountAfterdiscount,
@Paid,@Remain,
@Paiddate)
else
update TbInvoiceSales set Qty=Qty+@Qty , total=total + (@Qty*@SalePrice)
where
InvoiceDate=@Bdate and
InvoiceNo=@InvoiceNo and
ItemId=@ItemId
select @Btotal=ISNULL(sum(total),0) from TbInvoiceSales where InvoiceDate=@Bdate and InvoiceNo=@InvoiceNo;
select @count=count(*) from TbInvoiceSalesDetails where TinvoiceSaleDetailsDT=@Bdate and InvoiceNo=@InvoiceNo;
if @count=0
INSERT INTO [dbo].[TbInvoiceSalesDetails]
([TinvoiceSaleDetailsDT]
,[InvoiceNo]
,[TotalInvoice]
,[CustomerId])
values
(
@Bdate,@InvoiceNo,@Btotal,@CustomerId
)
else
update TbInvoiceSalesDetails set TotalInvoice=@Btotal where TinvoiceSaleDetailsDT=@Bdate and
InvoiceNo=@InvoiceNo
update TbItem set Qty=Qty-@Qty where Id=@ItemId
END
my view
@page
@model Food.Pages.Sales.IndexModel
@{
}
عمل فاتورة
Date
@Model.InvoiceDate.ToString("dd/MM/yyyy")
Number
total
Paid
Remain
@foreach (var s in Model.lstInvoiceSale)
{
}
Name | quantity | price | discount | total | ..... |
@s.Item.ItemName | @s.Qty | @s.Item.SalePrice | @s.Discount
| |
Delete
|
@section Scripts {
document.getElementById("ItemId").focus();
var element = document.getElementById("InsertForm");
element.addEventListener("keypress", function (event) {
if (event.key === "Enter") {
document.getElementById("iQuantity").focus();
event.preventDefault();
}
});
var Iquantity = document.getElementById("iQuantity");
Iquantity.addEventListener("keypress", function (event) {
if (event.key === "Enter") {
document.getElementById("InsertForm").submit();
event.preventDefault();
}
});
var paid = document.getElementById("Paid");
paid.addEventListener("keypress", function (event) {
if (event.key === "Enter") {
var total = parseInt(document.getElementById("amount").value);
var val2 = parseInt(document.getElementById("Paid").value);
if (!total) { total = 0; }
if (!val2) { val2 = 0; }
var ansD = document.getElementById("remain");
ansD.value = val2 - total;
}
});
}
my code
using Food.Models;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using System.Data;
namespace Food.Pages.Sales
{
public class IndexModel : PageModel
{
OnlineMarketContext db = new OnlineMarketContext();
[BindProperty]
public List<tbinvoicesale> lstInvoiceSale { get; set; }
[BindProperty]
public TbInvoiceSale InvoiceSale { get; set; }
[BindProperty]
public TbItem itemO { get; set; }
[BindProperty]
public int ItemId { get; set; }
[BindProperty]
public DateTime InvoiceDate { get; set; }
[BindProperty]
public int InvoiceNum { get; set; } = 0;
[BindProperty]
public decimal discount { get; set; }
[BindProperty]
public decimal Paid { get; set; }
[BindProperty]
public decimal remain { get; set; }
[BindProperty]
public decimal quantity { get; set; }
[BindProperty]
public decimal AmountAfterDiscount { get; set; }
[BindProperty]
public decimal total { get; set; } = 1;
[BindProperty]
public DateTime PaidDate { get; set; } = Convert.ToDateTime(DateTime.Now.ToString("dd/MM/yyyy"));
public void OnGet()
{
lstInvoiceSale = db.TbInvoiceSales.Include("Item").Where(i => i.InvoiceNo == 0).ToList();
InvoiceSale = db.TbInvoiceSales.Where(i => i.ItemId == ItemId).FirstOrDefault();
}
public void OnPost()
{
var Oitem = db.TbItems.Where(i => i.Id == ItemId).FirstOrDefault();
InvoiceDate = Convert.ToDateTime(DateTime.Now.ToString("dd/MM/yyyy"));
if (Oitem != null)
{
if (InvoiceNum == 0)
{
try
{
InvoiceNum = db.TbInvoiceSales.Where(i => i.InvoiceDate == InvoiceDate).Max(i => i.InvoiceNo) + 1;
}
catch
{
InvoiceNum = 1;
}
}
string ConnectionString = "Server=.;Database=OnlineMarket;Trusted_Connection=True;TrustServerCertificate=true;";
using (SqlConnection con = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("InsertintoSalesInvoice", con))
{
con.Open();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@InvoiceNo", InvoiceNum);
cmd.Parameters.AddWithValue("@ItemId", ItemId);
cmd.Parameters.AddWithValue("@SalePrice", Oitem.SalePrice);
cmd.Parameters.AddWithValue("@Qty", quantity);
cmd.Parameters.AddWithValue("@Discount", discount);
cmd.Parameters.AddWithValue("@Paid",Paid);
cmd.Parameters.AddWithValue("@Remain",remain);
cmd.Parameters.AddWithValue("@Total", InvoiceSale.Total);
cmd.Parameters.AddWithValue("@Paiddate", PaidDate);
cmd.Parameters.AddWithValue("@CustomerId", 1);
cmd.ExecuteNonQuery();
}
con.Close();
}
/// /
lstInvoiceSale = db.TbInvoiceSales.Include("Item").Where(i => i.InvoiceNo == InvoiceNum).ToList();
total = (decimal)db.TbInvoiceSales.Where(i => i.InvoiceNo == InvoiceNum && i.InvoiceDate == InvoiceDate)
.Sum(i => i.Total);
remain = Paid - total;
}
}
}
}
Posted 10-Jul-23 8:41am
by
A Belal.