narsil/Tanshu.Accounts.SqlDAO/SaleVoucherDAO.cs

171 lines
8.6 KiB
C#

using System;
using System.Data.SqlClient;
using Tanshu.Accounts.Contracts;
using Tanshu.Data.DAO;
namespace Tanshu.Accounts.SqlDAO
{
public class SaleVoucherDAO : BaseDAO
{
public SaleVoucherDAO(IConnectionDAO connection)
: base(connection)
{ }
public bool Insert(SaleVoucherBO saleVoucher)
{
using (var cmd = new SqlCommand(@"
SELECT @KotID = ISNULL('K-' + CAST(MAX(CAST(SUBSTRING(KotID, 3,8) AS int)) + 1 AS nvarchar(8)), 'K-1') FROM [dbo].[SaleVoucher]
IF @Printed = 1
BEGIN
SELECT @BillID = ISNULL(CAST(MAX(CAST(REPLACE(BillID, '-', '') AS int)) + 1 AS nvarchar(9)), '010001') FROM SaleVoucher 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 SaleVoucher WHERE TableID LIKE 'C%' AND PaidStatus = 1
END
INSERT INTO [dbo].[SaleVoucher] ([VoucherID], [Floor], [BillID], [TableID], [WaiterID], [CustomerID], [AdvanceID], [PaidStatus], [VoidReason], [Printed], [Alarm], [KotID])
VALUES (@VoucherID, @Floor, @BillID, @TableID, @WaiterID, @CustomerID, @AdvanceID, @PaidStatus, @VoidReason, @Printed, @Alarm, @KotID)
"))
{
cmd.Parameters.AddWithValue("@VoucherID", saleVoucher.VoucherID);
cmd.Parameters.AddWithValue("@Floor", saleVoucher.Floor);
cmd.Parameters.Add("@BillID", System.Data.SqlDbType.NVarChar, 10);
cmd.Parameters["@BillID"].Direction = System.Data.ParameterDirection.Output;
cmd.Parameters.AddWithValue("@TableID", saleVoucher.TableID);
cmd.Parameters.AddWithValue("@WaiterID", saleVoucher.WaiterID);
cmd.Parameters.AddWithValue("@CustomerID", saleVoucher.CustomerID);
if (saleVoucher.AdvanceID.HasValue)
cmd.Parameters.AddWithValue("@AdvanceID", saleVoucher.AdvanceID);
else
cmd.Parameters.AddWithValue("@AdvanceID", DBNull.Value);
cmd.Parameters.AddWithValue("@PaidStatus", (int)saleVoucher.PaidStatus);
if (saleVoucher.VoidReason == null)
saleVoucher.VoidReason = string.Empty;
cmd.Parameters.AddWithValue("@VoidReason", saleVoucher.VoidReason);
cmd.Parameters.AddWithValue("@Printed", saleVoucher.Printed);
if (saleVoucher.Alarm.HasValue)
cmd.Parameters.AddWithValue("@Alarm", saleVoucher.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);
saleVoucher.BillID = (string)cmd.Parameters["@BillID"].Value;
saleVoucher.KotID = (string)cmd.Parameters["@KotID"].Value;
return true;
}
}
public bool Update(SaleVoucherBO saleVoucher)
{
using (var cmd = new SqlCommand(@"
IF @Printed = 1 AND (SELECT Printed FROM SaleVoucher WHERE VoucherID = @VoucherID) = 0
BEGIN
SELECT @BillID = ISNULL(CAST(MAX(CAST(REPLACE(BillID, '-', '') AS int)) + 1 AS nvarchar(9)), '010001') FROM SaleVoucher 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 SaleVoucher 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 SaleVoucher WHERE TableID LIKE 'C%' AND PaidStatus = 1
END
IF (SELECT Printed FROM SaleVoucher WHERE VoucherID = @VoucherID) = 1
SET @Printed = 1
UPDATE [dbo].[SaleVoucher] SET
[Floor] = @Floor,
[BillID] = @BillID,
[TableID] = @TableID,
[WaiterID] = @WaiterID,
[CustomerID] = @CustomerID,
[AdvanceID] = @AdvanceID,
[PaidStatus] = @PaidStatus,
[VoidReason] = @VoidReason,
[Printed] = @Printed,
[Alarm] = @Alarm
WHERE VoucherID = @VoucherID
UPDATE Vouchers SET LastEditDate = GETDATE() WHERE VoucherID = @VoucherID
SELECT @KotID = KotID FROM SaleVoucher WHERE VoucherID = @VoucherID
"))
{
cmd.Parameters.AddWithValue("@VoucherID", saleVoucher.VoucherID);
cmd.Parameters.Add("@BillID", System.Data.SqlDbType.NVarChar, 10);
cmd.Parameters["@BillID"].Direction = System.Data.ParameterDirection.Output;
cmd.Parameters.AddWithValue("@Floor", saleVoucher.Floor);
cmd.Parameters.AddWithValue("@TableID", saleVoucher.TableID);
cmd.Parameters.AddWithValue("@WaiterID", saleVoucher.WaiterID);
cmd.Parameters.AddWithValue("@CustomerID", saleVoucher.CustomerID);
if (saleVoucher.AdvanceID.HasValue)
cmd.Parameters.AddWithValue("@AdvanceID", saleVoucher.AdvanceID);
else
cmd.Parameters.AddWithValue("@AdvanceID", DBNull.Value);
cmd.Parameters.AddWithValue("@PaidStatus", saleVoucher.PaidStatus);
if (saleVoucher.VoidReason == null)
saleVoucher.VoidReason = string.Empty;
cmd.Parameters.AddWithValue("@VoidReason", saleVoucher.VoidReason);
cmd.Parameters.AddWithValue("@Printed", saleVoucher.Printed);
if (saleVoucher.Alarm.HasValue)
cmd.Parameters.AddWithValue("@Alarm", saleVoucher.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);
saleVoucher.BillID = (string)cmd.Parameters["@BillID"].Value;
saleVoucher.KotID = (string)cmd.Parameters["@KotID"].Value;
return true;
}
}
public void SetAlarm(Guid voucherID, DateTime? alarmTime)
{
using (SqlCommand cmd = new SqlCommand(@"UPDATE SaleVoucher 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 SaleVoucher 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 SaleVoucherBO GetVoucherSale(Guid voucherID)
{
var cmd = new SqlCommand("SELECT b.*, ts.Floor, ts.BillID, ts.TableID, ts.WaiterID, ts.CustomerID, ts.AdvanceID, ts.PaidStatus, ts.VoidReason, ts.Printed, ts.Alarm, ts.KotID FROM SaleVoucher ts INNER JOIN Vouchers b ON ts.VoucherID = b.VoucherID WHERE ts.VoucherID = @VoucherID");
cmd.Parameters.AddWithValue("@VoucherID", voucherID);
return BusinessObjectDAO<SaleVoucherBO>.GetBusinessObject(connection.ExecuteReader(cmd));
}
public SaleVoucherBO GetVoucherSale(string billID)
{
var cmd = new SqlCommand("SELECT b.*, ts.Floor, ts.BillID, ts.TableID, ts.WaiterID, ts.CustomerID, ts.AdvanceID, ts.PaidStatus, ts.VoidReason, ts.Printed, ts.Alarm, ts.KotID FROM SaleVoucher ts INNER JOIN Vouchers b ON ts.VoucherID = b.VoucherID WHERE ts.BillID = @billID");
cmd.Parameters.AddWithValue("@billID", billID);
return BusinessObjectDAO<SaleVoucherBO>.GetBusinessObject(connection.ExecuteReader(cmd));
}
}
}