2010-03-02 17:56:21 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Data.SqlClient;
|
|
|
|
|
using Tanshu.Accounts.Contracts;
|
|
|
|
|
using Tanshu.Data.DAO;
|
|
|
|
|
using Tanshu.Accounts.DAOFactory;
|
|
|
|
|
|
|
|
|
|
namespace Tanshu.Accounts.SqlDAO
|
|
|
|
|
{
|
|
|
|
|
public class SaleVoucherDAO : BaseDAO, ISaleVoucherDAO
|
|
|
|
|
{
|
|
|
|
|
public SaleVoucherDAO(IConnectionDAO connection)
|
|
|
|
|
: base(connection)
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
public bool Insert(SaleVoucherBO saleVoucher)
|
|
|
|
|
{
|
2011-12-05 09:23:02 +00:00
|
|
|
|
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)
|
|
|
|
|
SELECT @timestamp = timestamp FROM [dbo].[SaleVoucher] WHERE VoucherID = @VoucherID
|
|
|
|
|
"))
|
2010-03-02 17:56:21 +00:00
|
|
|
|
{
|
|
|
|
|
cmd.Parameters.AddWithValue("@VoucherID", saleVoucher.VoucherID);
|
2011-12-05 09:23:02 +00:00
|
|
|
|
cmd.Parameters.AddWithValue("@Floor", saleVoucher.Floor);
|
2010-03-02 17:56:21 +00:00
|
|
|
|
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);
|
2011-12-05 09:23:02 +00:00
|
|
|
|
if (saleVoucher.AdvanceID.HasValue)
|
|
|
|
|
cmd.Parameters.AddWithValue("@AdvanceID", saleVoucher.AdvanceID);
|
|
|
|
|
else
|
|
|
|
|
cmd.Parameters.AddWithValue("@AdvanceID", DBNull.Value);
|
|
|
|
|
cmd.Parameters.AddWithValue("@PaidStatus", (int)saleVoucher.PaidStatus);
|
2010-03-02 17:56:21 +00:00
|
|
|
|
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;
|
|
|
|
|
cmd.Parameters.Add("@timestamp", System.Data.SqlDbType.Timestamp);
|
|
|
|
|
cmd.Parameters["@timestamp"].Direction = System.Data.ParameterDirection.Output;
|
|
|
|
|
connection.ExecuteNonQuery(cmd);
|
|
|
|
|
saleVoucher.SaleTimestamp = (byte[])cmd.Parameters["@timestamp"].Value;
|
|
|
|
|
saleVoucher.BillID = (string)cmd.Parameters["@BillID"].Value;
|
|
|
|
|
saleVoucher.KotID = (string)cmd.Parameters["@KotID"].Value;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public bool Update(SaleVoucherBO saleVoucher)
|
|
|
|
|
{
|
2011-12-05 09:23:02 +00:00
|
|
|
|
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 @timestamp = timestamp, @KotID = KotID FROM SaleVoucher WHERE VoucherID = @VoucherID
|
|
|
|
|
"))
|
2010-03-02 17:56:21 +00:00
|
|
|
|
{
|
|
|
|
|
cmd.Parameters.AddWithValue("@VoucherID", saleVoucher.VoucherID);
|
|
|
|
|
|
|
|
|
|
cmd.Parameters.Add("@BillID", System.Data.SqlDbType.NVarChar, 10);
|
|
|
|
|
cmd.Parameters["@BillID"].Direction = System.Data.ParameterDirection.Output;
|
2011-12-05 09:23:02 +00:00
|
|
|
|
cmd.Parameters.AddWithValue("@Floor", saleVoucher.Floor);
|
2010-03-02 17:56:21 +00:00
|
|
|
|
cmd.Parameters.AddWithValue("@TableID", saleVoucher.TableID);
|
|
|
|
|
cmd.Parameters.AddWithValue("@WaiterID", saleVoucher.WaiterID);
|
|
|
|
|
cmd.Parameters.AddWithValue("@CustomerID", saleVoucher.CustomerID);
|
2011-12-05 09:23:02 +00:00
|
|
|
|
if (saleVoucher.AdvanceID.HasValue)
|
|
|
|
|
cmd.Parameters.AddWithValue("@AdvanceID", saleVoucher.AdvanceID);
|
|
|
|
|
else
|
|
|
|
|
cmd.Parameters.AddWithValue("@AdvanceID", DBNull.Value);
|
|
|
|
|
cmd.Parameters.AddWithValue("@PaidStatus", saleVoucher.PaidStatus);
|
2010-03-02 17:56:21 +00:00
|
|
|
|
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;
|
|
|
|
|
cmd.Parameters.Add("@timestamp", System.Data.SqlDbType.Timestamp);
|
|
|
|
|
cmd.Parameters["@timestamp"].Direction = System.Data.ParameterDirection.Output;
|
|
|
|
|
connection.ExecuteNonQuery(cmd);
|
|
|
|
|
saleVoucher.timestamp = (byte[])cmd.Parameters["@timestamp"].Value;
|
|
|
|
|
saleVoucher.SaleTimestamp = (byte[])cmd.Parameters["@timestamp"].Value;
|
|
|
|
|
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)
|
|
|
|
|
{
|
2011-12-05 09:23:02 +00:00
|
|
|
|
using (SqlCommand cmd = new SqlCommand(@"UPDATE SaleVoucher SET PaidStatus = @PaidStatus, VoidReason = @VoidReason WHERE VoucherID = @VoucherID"))
|
2010-03-02 17:56:21 +00:00
|
|
|
|
{
|
|
|
|
|
cmd.Parameters.AddWithValue("@VoucherID", voucherID);
|
2011-12-05 09:23:02 +00:00
|
|
|
|
cmd.Parameters.AddWithValue("@PaidStatus", (int)PaidStatus.Void);
|
2010-03-02 17:56:21 +00:00
|
|
|
|
cmd.Parameters.AddWithValue("@VoidReason", reason);
|
|
|
|
|
connection.ExecuteNonQuery(cmd);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public SaleVoucherBO GetVoucherSale(Guid voucherID)
|
|
|
|
|
{
|
2011-12-05 09:23:02 +00:00
|
|
|
|
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, ts.timestamp AS SaleTimestamp FROM SaleVoucher ts INNER JOIN Vouchers b ON ts.VoucherID = b.VoucherID WHERE ts.VoucherID = @VoucherID");
|
2010-03-02 17:56:21 +00:00
|
|
|
|
cmd.Parameters.AddWithValue("@VoucherID", voucherID);
|
|
|
|
|
return BusinessObjectDAO<SaleVoucherBO>.GetBusinessObject(connection.ExecuteReader(cmd));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|