narsil/Tanshu.Accounts.SqlDAO/VoucherDAO.cs
unknown 0172fc4e01 Added Service Tax and CIN Information to the bill printout
Added Nc Option in settlement
Merged Vouchers and SaleVoucher table. Need to update the Sql Schema
2014-08-08 17:35:38 +05:30

352 lines
17 KiB
C#

using System;
using System.Data.SqlClient;
using Tanshu.Accounts.Contracts;
using Tanshu.Data.DAO;
using System.Collections.Generic;
namespace Tanshu.Accounts.SqlDAO
{
public class VoucherDAO : BaseDAO
{
public VoucherDAO(IConnectionDAO connection)
: base(connection)
{ }
public bool Insert(VoucherBO voucher)
{
using (var cmd = new SqlCommand(@"
SELECT @VoucherID = NEWID(),
@CreationDate = GETDATE(),
@LastEditDate = GETDATE(),
@Date = COALESCE(@Date, GETDATE());
SELECT @KotID = ISNULL('K-' + CAST(MAX(CAST(SUBSTRING(KotID, 3,8) AS int)) + 1 AS nvarchar(8)), 'K-1') FROM Vouchers
IF @Printed = 1
BEGIN
SELECT @BillID = ISNULL(CAST(MAX(CAST(REPLACE(BillID, '-', '') AS int)) + 1 AS nvarchar(9)), '010001') FROM Vouchers WHERE BillID LIKE '__-____'
IF LEN(@BillID) = 5
SET @BillID = '0' + @BillID
SET @BillID = SUBSTRING(@BillID, 1, 2) + '-' + SUBSTRING(@BillID, 3, 7)
IF SUBSTRING(@BillID,3,7) = '-0000'
SET @BillID = SUBSTRING(@BillID, 1, 2) + '-0001'
END
ELSE
BEGIN
SET @BillID = @KotID
END
IF @TableID = ''
BEGIN
SELECT @TableID = 'C' + CAST(ISNULL(MAX(CAST(SUBSTRING(TableID, 2, 10) AS int)), 0) + 1 AS nvarchar(10)) FROM Vouchers WHERE TableID LIKE 'C%' AND PaidStatus = 1
END
INSERT INTO Vouchers (VoucherID, Date, Narration, UserID, CreationDate, LastEditDate, Floor, BillID, TableID, WaiterID, CustomerID, AdvanceID, PaidStatus, VoidReason, Printed, Alarm, KotID)
VALUES (@VoucherID, @Date, @Narration, @UserID, @CreationDate, @LastEditDate, @Floor, @BillID, @TableID, @WaiterID, @CustomerID, @AdvanceID, @PaidStatus, @VoidReason, @Printed, @Alarm, @KotID)
"))
{
cmd.Parameters.Add("@VoucherID", System.Data.SqlDbType.UniqueIdentifier);
cmd.Parameters["@VoucherID"].Direction = System.Data.ParameterDirection.Output;
cmd.Parameters.Add("@Date", System.Data.SqlDbType.DateTime);
if (voucher.Date.HasValue)
cmd.Parameters["@Date"].Value = voucher.Date.Value;
else
cmd.Parameters["@Date"].Value = DBNull.Value;
cmd.Parameters["@Date"].Direction = System.Data.ParameterDirection.InputOutput;
cmd.Parameters.AddWithValue("@Narration", voucher.Narration);
cmd.Parameters.AddWithValue("@UserID", voucher.UserID);
cmd.Parameters.Add("@CreationDate", System.Data.SqlDbType.DateTime);
cmd.Parameters["@CreationDate"].Direction = System.Data.ParameterDirection.Output;
cmd.Parameters.Add("@LastEditDate", System.Data.SqlDbType.DateTime);
cmd.Parameters["@LastEditDate"].Direction = System.Data.ParameterDirection.Output;
cmd.Parameters.AddWithValue("@Floor", voucher.Floor);
cmd.Parameters.Add("@BillID", System.Data.SqlDbType.NVarChar, 10);
cmd.Parameters["@BillID"].Direction = System.Data.ParameterDirection.Output;
cmd.Parameters.AddWithValue("@TableID", voucher.TableID);
cmd.Parameters.AddWithValue("@WaiterID", voucher.WaiterID);
cmd.Parameters.AddWithValue("@CustomerID", voucher.CustomerID);
if (voucher.AdvanceID.HasValue)
cmd.Parameters.AddWithValue("@AdvanceID", voucher.AdvanceID);
else
cmd.Parameters.AddWithValue("@AdvanceID", DBNull.Value);
cmd.Parameters.AddWithValue("@PaidStatus", (int)voucher.PaidStatus);
if (voucher.VoidReason == null)
voucher.VoidReason = string.Empty;
cmd.Parameters.AddWithValue("@VoidReason", voucher.VoidReason);
cmd.Parameters.AddWithValue("@Printed", voucher.Printed);
if (voucher.Alarm.HasValue)
cmd.Parameters.AddWithValue("@Alarm", voucher.Alarm);
else
cmd.Parameters.AddWithValue("@Alarm", DBNull.Value);
cmd.Parameters.Add("@KotID", System.Data.SqlDbType.NVarChar, 10);
cmd.Parameters["@KotID"].Direction = System.Data.ParameterDirection.Output;
connection.ExecuteNonQuery(cmd);
voucher.VoucherID = (Guid)cmd.Parameters["@VoucherID"].Value;
voucher.CreationDate = (DateTime)cmd.Parameters["@CreationDate"].Value;
voucher.LastEditDate = (DateTime)cmd.Parameters["@LastEditDate"].Value;
voucher.Date = (DateTime)cmd.Parameters["@Date"].Value;
voucher.BillID = (string)cmd.Parameters["@BillID"].Value;
voucher.KotID = (string)cmd.Parameters["@KotID"].Value;
return true;
}
}
public VoucherBO GetVoucher(Guid voucherID)
{
SqlCommand cmd = new SqlCommand("SELECT * FROM Vouchers WHERE VoucherID = @VoucherID");
cmd.Parameters.AddWithValue("@VoucherID", voucherID);
return BusinessObjectDAO<VoucherBO>.GetBusinessObject(connection.ExecuteReader(cmd));
}
public VoucherBO GetVoucher(string billID)
{
SqlCommand cmd = new SqlCommand("SELECT * FROM Vouchers WHERE BillID = @BillID");
cmd.Parameters.AddWithValue("@BillID", billID);
return BusinessObjectDAO<VoucherBO>.GetBusinessObject(connection.ExecuteReader(cmd));
}
public bool Delete(Guid voucherID)
{
using (SqlCommand cmd = new SqlCommand("DELETE from Vouchers where VoucherID = @VoucherID"))
{
cmd.Parameters.AddWithValue("@VoucherID", voucherID);
connection.ExecuteNonQuery(cmd);
return true;
}
}
public bool Update(VoucherBO voucher)
{
using (var cmd = new SqlCommand(@"
SELECT @CreationDate = CreationDate, @LastEditDate = GETDATE(), @Date = COALESCE(@Date, GETDATE()), @KotID = KotID
FROM Vouchers
WHERE VoucherID = @VoucherID;
IF @Printed = 1 AND (SELECT Printed FROM Vouchers WHERE VoucherID = @VoucherID) = 0
BEGIN
SELECT @BillID = ISNULL(CAST(MAX(CAST(REPLACE(BillID, '-', '') AS int)) + 1 AS nvarchar(9)), '010001') FROM Vouchers WHERE BillID LIKE '__-____'
IF LEN(@BillID) = 5
SET @BillID = '0' + @BillID
SET @BillID = SUBSTRING(@BillID, 1, 2) + '-' + SUBSTRING(@BillID, 3, 7)
IF SUBSTRING(@BillID,3,7) = '-0000'
SET @BillID = SUBSTRING(@BillID, 1, 2) + '-0001'
END
ELSE
BEGIN
SELECT @BillID = BillID FROM Vouchers WHERE VoucherID = @VoucherID
END
IF @TableID = ''
BEGIN
SELECT @TableID = 'C' + CAST(ISNULL(MAX(CAST(SUBSTRING(TableID, 2, 10) AS int)), 0) + 1 AS nvarchar(10)) FROM Vouchers WHERE TableID LIKE 'C%' AND PaidStatus = 1
END
IF (SELECT Printed FROM Vouchers WHERE VoucherID = @VoucherID) = 1
SET @Printed = 1
UPDATE [Vouchers] SET
[Date] = @Date,
[Narration] = @Narration,
[UserID] = @UserID,
[LastEditDate] = @LastEditDate,
[Floor] = @Floor,
[BillID] = @BillID,
[TableID] = @TableID,
[WaiterID] = @WaiterID,
[CustomerID] = @CustomerID,
[AdvanceID] = @AdvanceID,
[PaidStatus] = @PaidStatus,
[VoidReason] = @VoidReason,
[Printed] = @Printed,
[Alarm] = @Alarm
WHERE VoucherID = @VoucherID;
"))
{
cmd.Parameters.AddWithValue("@VoucherID", voucher.VoucherID);
cmd.Parameters.Add("@Date", System.Data.SqlDbType.DateTime);
if (voucher.Date.HasValue)
cmd.Parameters["@Date"].Value = voucher.Date.Value;
else
cmd.Parameters["@Date"].Value = DBNull.Value;
cmd.Parameters["@Date"].Direction = System.Data.ParameterDirection.InputOutput;
cmd.Parameters.AddWithValue("@Narration", voucher.Narration);
cmd.Parameters.AddWithValue("@UserID", voucher.UserID);
cmd.Parameters.Add("@CreationDate", System.Data.SqlDbType.DateTime);
cmd.Parameters["@CreationDate"].Direction = System.Data.ParameterDirection.Output;
cmd.Parameters.Add("@LastEditDate", System.Data.SqlDbType.DateTime);
cmd.Parameters["@LastEditDate"].Direction = System.Data.ParameterDirection.Output;
cmd.Parameters.Add("@BillID", System.Data.SqlDbType.NVarChar, 10);
cmd.Parameters["@BillID"].Direction = System.Data.ParameterDirection.Output;
cmd.Parameters.AddWithValue("@Floor", voucher.Floor);
cmd.Parameters.AddWithValue("@TableID", voucher.TableID);
cmd.Parameters.AddWithValue("@WaiterID", voucher.WaiterID);
cmd.Parameters.AddWithValue("@CustomerID", voucher.CustomerID);
if (voucher.AdvanceID.HasValue)
cmd.Parameters.AddWithValue("@AdvanceID", voucher.AdvanceID);
else
cmd.Parameters.AddWithValue("@AdvanceID", DBNull.Value);
cmd.Parameters.AddWithValue("@PaidStatus", voucher.PaidStatus);
if (voucher.VoidReason == null)
voucher.VoidReason = string.Empty;
cmd.Parameters.AddWithValue("@VoidReason", voucher.VoidReason);
cmd.Parameters.AddWithValue("@Printed", voucher.Printed);
if (voucher.Alarm.HasValue)
cmd.Parameters.AddWithValue("@Alarm", voucher.Alarm);
else
cmd.Parameters.AddWithValue("@Alarm", DBNull.Value);
cmd.Parameters.Add("@KotID", System.Data.SqlDbType.NVarChar, 10);
cmd.Parameters["@KotID"].Direction = System.Data.ParameterDirection.Output;
connection.ExecuteNonQuery(cmd);
voucher.BillID = (string)cmd.Parameters["@BillID"].Value;
voucher.KotID = (string)cmd.Parameters["@KotID"].Value;
voucher.CreationDate = (DateTime)cmd.Parameters["@CreationDate"].Value;
voucher.LastEditDate = (DateTime)cmd.Parameters["@LastEditDate"].Value;
voucher.Date = (DateTime)cmd.Parameters["@Date"].Value;
return true;
}
}
public void SetAlarm(Guid voucherID, DateTime? alarmTime)
{
using (var cmd = new SqlCommand(@"UPDATE Vouchers SET Alarm = @Alarm WHERE VoucherID = @VoucherID"))
{
cmd.Parameters.AddWithValue("@VoucherID", voucherID);
cmd.Parameters.AddWithValue("@Alarm", alarmTime);
connection.ExecuteNonQuery(cmd);
}
}
public void VoidBill(Guid voucherID, string reason, Guid userID)
{
using (SqlCommand cmd = new SqlCommand(@"UPDATE Vouchers SET PaidStatus = @PaidStatus, VoidReason = @VoidReason WHERE VoucherID = @VoucherID; UPDATE Vouchers SET UserID = @UserID, LastEditDate = GETDATE() WHERE VoucherID = @VoucherID"))
{
cmd.Parameters.AddWithValue("@VoucherID", voucherID);
cmd.Parameters.AddWithValue("@UserID", userID);
cmd.Parameters.AddWithValue("@PaidStatus", (int)PaidStatus.Void);
cmd.Parameters.AddWithValue("@VoidReason", reason);
connection.ExecuteNonQuery(cmd);
}
}
public List<PendingBillsBO> GetPendingBills(PendingType list, int floor)
{
var query = @"
SELECT v.VoucherID, v.Alarm AS Alarm, v.TableID AS TableID,
(SELECT SUM(Amount) FROM Inventory WHERE VoucherID = v.VoucherID) AS Amount,
v.BillID, v.CreationDate, u.Name, CAST(0 AS bit), v.KotID, v.LastEditDate, v.Printed, v.AdvanceID
FROM Vouchers v INNER JOIN Users u ON v.UserID = u.UserID
WHERE v.PaidStatus = @PaidStatus
";
if (list == PendingType.Alarms)
query += "AND v.Alarm IS NOT NULL ";
else
query += "AND v.Alarm IS NULL ";
if (floor != 0)
query += "AND v.Floor = @Floor ";
if (list == PendingType.Today)
query += string.Format("AND v.LastEditDate >= '{0:dd-MMM-yyyy} 00:00:00' ", DateTime.Now);
else if (list == PendingType.Week)
query += string.Format("AND v.LastEditDate >= '{0:dd-MMM-yyyy} 00:00:00' ", DateTime.Now.AddDays(-7));
query += "ORDER BY v.CreationDate ";
using (var cmd = new SqlCommand(query))
{
cmd.Parameters.AddWithValue("@PaidStatus", (int)PaidStatus.Pending);
if (floor != 0)
cmd.Parameters.AddWithValue("@Floor", floor);
using (var dr = connection.ExecuteReader(cmd))
{
var outList = new List<PendingBillsBO>();
while (dr.Read())
{
var local = new PendingBillsBO();
local.voucherID = dr.GetGuid(0);
local.Alarm = !dr.IsDBNull(1);
if (local.Alarm)
local.AlarmTime = dr.GetDateTime(1);
local.TableID = dr.GetString(2);
if (!dr.IsDBNull(3))
local.Amount = dr.GetDecimal(3);
local.BillNo = dr.GetString(4);
local.BookingTime = dr.GetDateTime(5);
local.Cashier = dr.GetString(6);
local.Important = dr.GetBoolean(7);
local.Kot = dr.GetString(8);
local.LastEdited = dr.GetDateTime(9);
local.Printed = dr.GetBoolean(10);
if (!dr.IsDBNull(11))
local.AdvanceID = dr.GetGuid(11);
outList.Add(local);
}
return outList;
}
}
}
public Guid? GetPendingVoucherID(string tableID, int floor)
{
var query = "SELECT VoucherID FROM Vouchers where TableID = @TableID and PaidStatus = @PaidStatus";
if (floor != 0)
query += " AND Floor = @Floor ";
using (var cmd = new SqlCommand(query))
{
cmd.Parameters.AddWithValue("@TableID", tableID);
cmd.Parameters.AddWithValue("@PaidStatus", (int)PaidStatus.Pending);
if (floor != 0)
cmd.Parameters.AddWithValue("@Floor", floor);
using (var dr = connection.ExecuteReader(cmd))
{
if (dr.Read())
{
return dr.GetGuid(0);
}
return null;
}
}
}
private class BillPaidClass
{
public Guid VoucherID { get; set; }
public string KotID { get; set; }
public bool Printed { get; set; }
}
public void DeclareBillsPaid(Guid userID, List<Guid> billList, PaidStatus paidStatus)
{
if (billList.Count == 0)
return;
var removeItems = new List<Guid>();
var query = "SELECT VoucherID, KotID, Printed FROM Vouchers WHERE VoucherID IN (";
foreach (var voucherID in billList)
{
query += string.Format("'{0}', ", voucherID);
}
query = query.Substring(0, query.Length - 2) + ")";
var pbc = new List<BillPaidClass>();
using (var cmd = new SqlCommand(query))
{
using (var dr = connection.ExecuteReader(cmd))
{
while (dr.Read())
{
pbc.Add(new BillPaidClass
{
VoucherID = dr.GetGuid(0),
KotID = dr.GetString(1),
Printed = dr.GetBoolean(2)
});
}
}
}
foreach (var item in pbc)
{
if (!item.Printed)
continue;
using (var cmd = new SqlCommand(@"
UPDATE Vouchers SET PaidStatus = @PaidStatus, UserID = @UserID, LastEditDate = GETDATE() WHERE VoucherID = @VoucherID;
"))
{
cmd.Parameters.AddWithValue("@VoucherID", item.VoucherID);
cmd.Parameters.AddWithValue("@UserID", userID);
cmd.Parameters.AddWithValue("@PaidStatus", (int)paidStatus);
connection.ExecuteNonQuery(cmd);
}
removeItems.Add(item.VoucherID);
}
foreach (var item in removeItems)
billList.Remove(item);
}
}
}