narsil/Tanshu.Accounts.SqlDAO/SaleVoucherDAO.cs
2010-03-02 23:26:21 +05:30

113 lines
6.2 KiB
C#

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)
{
using (SqlCommand cmd = new SqlCommand("SaleVoucher_Insert"))
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
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("@TableID", saleVoucher.TableID);
cmd.Parameters.AddWithValue("@WaiterID", saleVoucher.WaiterID);
cmd.Parameters.AddWithValue("@CustomerID", saleVoucher.CustomerID);
cmd.Parameters.AddWithValue("@Paid", saleVoucher.Paid);
cmd.Parameters.AddWithValue("@Void", saleVoucher.Void);
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;
cmd.Parameters.AddWithValue("@CreditCard", saleVoucher.CreditCard);
cmd.Parameters.AddWithValue("@IsImportant", saleVoucher.IsImportant);
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)
{
using (SqlCommand cmd = new SqlCommand("SaleVoucher_Update"))
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
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("@TableID", saleVoucher.TableID);
cmd.Parameters.AddWithValue("@WaiterID", saleVoucher.WaiterID);
cmd.Parameters.AddWithValue("@CustomerID", saleVoucher.CustomerID);
cmd.Parameters.AddWithValue("@Paid", saleVoucher.Paid);
cmd.Parameters.AddWithValue("@Void", saleVoucher.Void);
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;
cmd.Parameters.AddWithValue("@CreditCard", saleVoucher.CreditCard);
cmd.Parameters.AddWithValue("@IsImportant", saleVoucher.IsImportant);
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)
{
using (SqlCommand cmd = new SqlCommand(@"UPDATE SaleVoucher SET Void = @Void, VoidReason = @VoidReason WHERE VoucherID = @VoucherID"))
{
cmd.Parameters.AddWithValue("@VoucherID", voucherID);
cmd.Parameters.AddWithValue("@Void", true);
cmd.Parameters.AddWithValue("@VoidReason", reason);
connection.ExecuteNonQuery(cmd);
}
}
public SaleVoucherBO GetVoucherSale(Guid voucherID)
{
SqlCommand cmd = new SqlCommand("SELECT b.*, ts.BillID, ts.TableID, ts.WaiterID ,ts.CustomerID ,ts.Paid ,ts.Void ,ts.VoidReason ,ts.Printed ,ts.Alarm ,ts.KotID ,ts.timestamp AS SaleTimestamp ,ts.CreditCard 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));
}
}
}