368 lines
17 KiB
C#
368 lines
17 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Data.SqlClient;
|
|
using Tanshu.Accounts.Contracts;
|
|
using Tanshu.Data.DAO;
|
|
using System.Data;
|
|
using Tanshu.Accounts.DAOFactory;
|
|
|
|
namespace Tanshu.Accounts.SqlDAO
|
|
{
|
|
public class ManagementDAO : BaseDAO, IManagementDAO
|
|
{
|
|
private DateTime startDate;
|
|
private DateTime finishDate;
|
|
public ManagementDAO(DateTime startDate, DateTime finishDate, IConnectionDAO connection)
|
|
: base(connection)
|
|
{
|
|
this.startDate = Convert.ToDateTime(string.Format("{0:dd-MMM-yyyy} 00:00:00", startDate));
|
|
this.finishDate = Convert.ToDateTime(string.Format("{0:dd-MMM-yyyy} 23:59:59", finishDate));
|
|
}
|
|
public decimal GetBalance(decimal? tax)
|
|
{
|
|
string query;
|
|
if (tax.HasValue)
|
|
query = @"
|
|
SELECT ISNULL(SUM(i.Amount), 0) FROM Vouchers t INNER JOIN SaleVoucher s ON t.VoucherID = s.VoucherID
|
|
INNER JOIN Inventory i ON t.VoucherID = i.VoucherID
|
|
WHERE t.Type = 'S' AND t.Date BETWEEN @StartDate AND @FinishDate AND s.Void = 0
|
|
AND i.Tax = @Tax
|
|
";
|
|
else
|
|
query = @"
|
|
SELECT ISNULL(SUM(i.Amount), 0) FROM Vouchers t INNER JOIN SaleVoucher s ON t.VoucherID = s.VoucherID
|
|
INNER JOIN Inventory i ON t.VoucherID = i.VoucherID
|
|
WHERE t.Type = 'S' AND t.Date BETWEEN @StartDate AND @FinishDate AND s.Void = 0
|
|
";
|
|
using (SqlCommand cmd = new SqlCommand(query))
|
|
{
|
|
cmd.Parameters.AddWithValue("@StartDate", startDate);
|
|
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
|
|
if (tax.HasValue) cmd.Parameters.AddWithValue("@Tax", tax);
|
|
return (decimal)connection.ExecuteScalar(cmd);
|
|
}
|
|
|
|
}
|
|
public List<Guid> GetUpdateBillList(decimal tax, bool voided, bool paid, bool creditCard)
|
|
{
|
|
//Paid = 1, CC = 0, Void = 0
|
|
List<Guid> list = new List<Guid>();
|
|
string query = @"
|
|
SELECT DISTINCT t.VoucherID
|
|
FROM Vouchers t INNER JOIN SaleVoucher s ON t.VoucherID = s.VoucherID
|
|
INNER JOIN Inventory i ON t.VoucherID = i.VoucherID
|
|
WHERE t.Date BETWEEN @StartDate AND @FinishDate AND t.Type = 'S'
|
|
AND s.Void = @Void and s.Paid = @Paid AND s.CreditCard = @CreditCard AND i.Tax = @Tax";
|
|
using (SqlCommand cmd = new SqlCommand(query))
|
|
{
|
|
cmd.Parameters.AddWithValue("@StartDate", startDate);
|
|
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
|
|
cmd.Parameters.AddWithValue("@Tax", tax);
|
|
cmd.Parameters.AddWithValue("@Void", voided);
|
|
cmd.Parameters.AddWithValue("@Paid", paid);
|
|
cmd.Parameters.AddWithValue("CreditCard", creditCard);
|
|
|
|
using (IDataReader dr = connection.ExecuteReader(cmd))
|
|
{
|
|
while (dr.Read())
|
|
list.Add(dr.GetGuid(0));
|
|
}
|
|
}
|
|
return list;
|
|
}
|
|
|
|
public List<PendingBillsBO> GetPaidBills()
|
|
{
|
|
string query = @"
|
|
SELECT t.VoucherID, s.TableID AS TableID,
|
|
(SELECT SUM(Amount) FROM Inventory WHERE VoucherID = t.VoucherID) AS Amount,
|
|
s.BillID, t.CreationDate, u.Name, CAST(0 AS bit), s.KotID, t.LastEditDate, s.Printed
|
|
FROM Vouchers t INNER JOIN SaleVoucher s ON t.VoucherID = s.VoucherID AND s.Paid = 1 AND s.Void = 0
|
|
INNER JOIN Users u ON t.UserID = u.UserID WHERE
|
|
t.LastEditDate BETWEEN @StartDate AND @FinishDate ORDER BY t.CreationDate ";
|
|
using (SqlCommand cmd = new SqlCommand(query))
|
|
{
|
|
cmd.Parameters.AddWithValue("@StartDate", startDate);
|
|
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
|
|
using (IDataReader dr = connection.ExecuteReader(cmd))
|
|
{
|
|
List<PendingBillsBO> outList = new List<PendingBillsBO>();
|
|
while (dr.Read())
|
|
{
|
|
PendingBillsBO local = new PendingBillsBO();
|
|
|
|
local.voucherID = dr.GetGuid(0);
|
|
local.TableID = dr.GetString(1);
|
|
if (!dr.IsDBNull(2))
|
|
local.Amount = dr.GetDecimal(2);
|
|
local.BillNo = dr.GetString(3);
|
|
local.BookingTime = dr.GetDateTime(4);
|
|
local.Cashier = dr.GetString(5);
|
|
local.Important = dr.GetBoolean(6);
|
|
local.Kot = dr.GetString(7);
|
|
local.LastEdited = dr.GetDateTime(8);
|
|
local.Printed = dr.GetBoolean(9);
|
|
outList.Add(local);
|
|
}
|
|
return outList;
|
|
}
|
|
}
|
|
}
|
|
|
|
public decimal Update(Guid voucherID, decimal tax)
|
|
{
|
|
decimal amt;
|
|
using (SqlCommand cmd = new SqlCommand("SELECT ISNULL(SUM(Amount), 0) FROM Inventory WHERE VoucherID = @VoucherID AND Tax = @Tax"))
|
|
{
|
|
cmd.Parameters.AddWithValue("@VoucherID", voucherID);
|
|
cmd.Parameters.AddWithValue("@Tax", tax);
|
|
amt = (decimal)connection.ExecuteScalar(cmd);
|
|
}
|
|
using (SqlCommand cmd = new SqlCommand(@"
|
|
DELETE FROM Inventory WHERE VoucherID = @VoucherID;
|
|
DELETE FROM SaleVoucher WHERE VoucherID = @VoucherID;
|
|
DELETE FROM Vouchers WHERE VoucherID = @VoucherID;"))
|
|
{
|
|
cmd.Parameters.AddWithValue("@VoucherID", voucherID);
|
|
connection.ExecuteNonQuery(cmd);
|
|
}
|
|
return amt;
|
|
}
|
|
// public static List<PendingBillsBO> GetBillList(DateTime? fromDate, DateTime? toDate, decimal? minValue, decimal? maxValue, bool? showVoided)
|
|
// {
|
|
// using (ConnectionDAO connection = new ConnectionDAO())
|
|
// {
|
|
// string add = " WHERE ";
|
|
// string query = @"
|
|
//SELECT t.TransactionID AS transactionID, CAST (CASE WHEN Alarm IS NULL THEN 0 ELSE 1 END AS bit) AS Alarm,
|
|
//s.Alarm AS AlarmTime, s.TableID, (SELECT SUM(Amount) FROM Inventory i WHERE i.TransactionID = t.TransactionID) AS Amount,
|
|
//s.BillID, t.CreationDate AS BookingTime, u.Name AS Cashier, s.Void AS Important,
|
|
//s.KotID AS Kot, t.LastEditDate AS LastEdited, s.Printed AS Printed
|
|
//FROM BasicTransactions t INNER JOIN TransactionSale s ON t.TransactionID = s.TransactionID
|
|
//INNER JOIN Users u ON t.UserID = u.UserID";
|
|
// if (fromDate.HasValue)
|
|
// {
|
|
// query += add + "t.Date >= @StartDate";
|
|
// add = " AND ";
|
|
// }
|
|
// if (toDate.HasValue)
|
|
// {
|
|
// query += add + "t.Date <= @FinishDate";
|
|
// add = " AND ";
|
|
// }
|
|
// if (minValue.HasValue)
|
|
// {
|
|
// query += add + "Amount >= @MinValue";
|
|
// add = " AND ";
|
|
// }
|
|
// if (maxValue.HasValue)
|
|
// {
|
|
// query += add + "Amount <= @MaxValue";
|
|
// add = " AND ";
|
|
// }
|
|
// if (showVoided.HasValue)
|
|
// {
|
|
// query += add + "s.Void = @Void";
|
|
// }
|
|
// using (SqlCommand cmd = new SqlCommand(query))
|
|
// {
|
|
// if (fromDate.HasValue)
|
|
// cmd.Parameters.AddWithValue("@StartDate", fromDate.Value);
|
|
// if (toDate.HasValue)
|
|
// cmd.Parameters.AddWithValue("@FinishDate", toDate.Value);
|
|
// if (minValue.HasValue)
|
|
// cmd.Parameters.AddWithValue("@MinValue", minValue.Value);
|
|
// if (maxValue.HasValue)
|
|
// cmd.Parameters.AddWithValue("@MaxValue", maxValue.Value);
|
|
// if (showVoided.HasValue)
|
|
// cmd.Parameters.AddWithValue("@Void", showVoided.Value);
|
|
// return BusinessObjectDAO<PendingBillsBO>.GetBusinessObjects(connection.ExecuteReader(cmd));
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
public void Reorder(ShowProgessDelegate showProgressDelegate)
|
|
{
|
|
showProgressDelegate(0, 0, 0, "Cleaning up Void Bills");
|
|
using (SqlCommand cmd = new SqlCommand(@"
|
|
DELETE FROM Inventory WHERE VoucherID IN (
|
|
SELECT v.VoucherID FROM SaleVoucher s INNER JOIN Vouchers v ON s.VoucherID = v.VoucherID
|
|
WHERE s.Void = 1 AND v.Date BETWEEN @StartDate AND @EndDate)
|
|
|
|
DELETE FROM SaleVoucher WHERE VoucherID IN (
|
|
SELECT v.VoucherID FROM SaleVoucher s INNER JOIN Vouchers v ON s.VoucherID = v.VoucherID
|
|
WHERE s.Void = 1 AND v.Date BETWEEN @StartDate AND @EndDate)
|
|
|
|
DELETE FROM Vouchers WHERE VoucherID NOT IN (SELECT VoucherID FROM SaleVoucher)
|
|
AND Type = 'S'
|
|
"))
|
|
{
|
|
cmd.Parameters.AddWithValue("@StartDate", startDate);
|
|
cmd.Parameters.AddWithValue("@EndDate", finishDate);
|
|
connection.ExecuteNonQuery(cmd);
|
|
}
|
|
List<SaleVoucherBO> list = new List<SaleVoucherBO>();
|
|
using (SqlCommand cmd = new SqlCommand(@"
|
|
SELECT v.VoucherID, s.BillID, v.Date
|
|
FROM Vouchers v INNER JOIN SaleVoucher s ON v.VoucherID = s.VoucherID
|
|
WHERE v.Date >= @StartDate AND s.Printed = 1
|
|
ORDER BY Date"))
|
|
{
|
|
cmd.Parameters.AddWithValue("@StartDate", startDate);
|
|
using (IDataReader dr = connection.ExecuteReader(cmd))
|
|
{
|
|
while (dr.Read())
|
|
{
|
|
list.Add(new SaleVoucherBO { VoucherID = dr.GetGuid(0), BillID = dr.GetString(1), Date = dr.GetDateTime(2) });
|
|
}
|
|
}
|
|
}
|
|
if (list.Count == 0)
|
|
return;
|
|
connection.ExecuteNonQuery("ALTER INDEX [IX_TransactionSale] ON [dbo].[SaleVoucher] DISABLE");
|
|
string billID = list[0].BillID;
|
|
for (int i = 1; i < list.Count; i++)
|
|
{
|
|
billID = GetBillID(billID);
|
|
showProgressDelegate(0, list.Count, i, string.Format("Fixing bill no {0}", billID));
|
|
using (SqlCommand cmd = new SqlCommand("UPDATE SaleVoucher SET BillID = @BillID WHERE VoucherID = @VoucherID"))
|
|
{
|
|
cmd.Parameters.AddWithValue("@VoucherID", list[i].VoucherID);
|
|
cmd.Parameters.AddWithValue("@BillID", billID);
|
|
connection.ExecuteNonQuery(cmd);
|
|
}
|
|
}
|
|
connection.ExecuteNonQuery("ALTER INDEX [IX_TransactionSale] ON [dbo].[SaleVoucher] REBUILD");
|
|
showProgressDelegate(0, list.Count, list.Count, "Cleanup finished");
|
|
}
|
|
|
|
private string GetBillID(string billID)
|
|
{
|
|
decimal id = Convert.ToDecimal(billID.Replace("-", ""));
|
|
id++;
|
|
billID = id.ToString();
|
|
if (billID.EndsWith("0000"))
|
|
billID = (id + 1).ToString();
|
|
if (billID.Length == 5)
|
|
billID = "0" + billID;
|
|
return billID.Substring(0, 2) + "-" + billID.Substring(2, 4);
|
|
}
|
|
|
|
|
|
#region Merge Data
|
|
public bool MergeData(string sourceDB, string targetDB)
|
|
{
|
|
DeleteTarget(sourceDB, targetDB);
|
|
DateTime lastBillDateTime = GetBillDateTime(sourceDB, targetDB);
|
|
using (var cmd = new SqlCommand(string.Format("ALTER INDEX [IX_TransactionSale] ON [{0}].[dbo].[SaleVoucher] DISABLE", targetDB)))
|
|
{
|
|
cmd.CommandTimeout = int.MaxValue;
|
|
connection.ExecuteNonQuery(cmd);
|
|
}
|
|
CopyData(sourceDB, targetDB);
|
|
Clean(sourceDB, targetDB, lastBillDateTime);
|
|
using (var cmd = new SqlCommand(string.Format("ALTER INDEX [IX_TransactionSale] ON [{0}].[dbo].[SaleVoucher] REBUILD", targetDB)))
|
|
{
|
|
cmd.CommandTimeout = int.MaxValue;
|
|
connection.ExecuteNonQuery(cmd);
|
|
}
|
|
return true;
|
|
}
|
|
private void DeleteTarget(string sourceDB, string targetDB)
|
|
{
|
|
string query = @"
|
|
DELETE FROM {0}.dbo.Inventory WHERE VoucherID IN (
|
|
SELECT v.VoucherID FROM {0}.dbo.SaleVoucher s INNER JOIN {0}.dbo.Vouchers v ON s.VoucherID = v.VoucherID
|
|
WHERE v.Date BETWEEN @StartDate AND @EndDate)
|
|
|
|
DELETE FROM {0}.dbo.SaleVoucher WHERE VoucherID IN (
|
|
SELECT v.VoucherID FROM {0}.dbo.SaleVoucher s INNER JOIN {0}.dbo.Vouchers v ON s.VoucherID = v.VoucherID
|
|
WHERE v.Date BETWEEN @StartDate AND @EndDate)
|
|
|
|
DELETE FROM {0}.dbo.Vouchers WHERE VoucherID NOT IN (SELECT VoucherID FROM {0}.dbo.SaleVoucher)
|
|
AND Type = 'S'
|
|
";
|
|
using (SqlCommand cmd = new SqlCommand(string.Format(query, targetDB)))
|
|
{
|
|
cmd.CommandTimeout = int.MaxValue;
|
|
cmd.Parameters.AddWithValue("@StartDate", startDate);
|
|
cmd.Parameters.AddWithValue("@EndDate", finishDate);
|
|
connection.ExecuteNonQuery(cmd);
|
|
}
|
|
}
|
|
private DateTime GetBillDateTime(string sourceDB, string targetDB)
|
|
{
|
|
string query = @"
|
|
SELECT TOP 1 v.Date
|
|
FROM {0}.dbo.Vouchers v INNER JOIN {0}.dbo.SaleVoucher s ON v.VoucherID = s.VoucherID
|
|
WHERE v.Date < @StartDate
|
|
ORDER BY Date DESC";
|
|
using (SqlCommand cmd = new SqlCommand(string.Format(query, targetDB)))
|
|
{
|
|
cmd.CommandTimeout = int.MaxValue;
|
|
cmd.Parameters.AddWithValue("@StartDate", startDate);
|
|
return (DateTime)connection.ExecuteScalar(cmd);
|
|
}
|
|
}
|
|
private void CopyData(string sourceDB, string targetDB)
|
|
{
|
|
string query = @"
|
|
INSERT INTO {0}.dbo.Vouchers (VoucherID, Code, Ref, Date, Narration, UserID, CreationDate, LastEditDate, Type)
|
|
SELECT VoucherID, Code, Ref, Date, Narration, UserID, CreationDate, LastEditDate, Type
|
|
FROM {1}.dbo.Vouchers WHERE Date BETWEEN @StartDate AND @EndDate;
|
|
|
|
INSERT INTO {0}.dbo.SaleVoucher (VoucherID, BillID, TableID, WaiterID, CustomerID, Paid, Void, VoidReason, Printed, Alarm, KotID, CreditCard, IsImportant)
|
|
SELECT VoucherID, BillID, TableID, WaiterID, CustomerID, Paid, Void, VoidReason, Printed, Alarm, KotID, CreditCard, IsImportant
|
|
FROM {1}.dbo.SaleVoucher WHERE VoucherID IN (
|
|
SELECT v.VoucherID FROM {1}.dbo.Vouchers v WHERE v.Date BETWEEN @StartDate AND @EndDate)
|
|
|
|
INSERT INTO {0}.dbo.Inventory (InventoryID, VoucherID, ProductID, ComplexProductID, Quantity, Rate, Tax, Discount, IsRateFinal)
|
|
SELECT InventoryID, VoucherID, ProductID, ComplexProductID, Quantity, Rate, Tax, Discount, IsRateFinal
|
|
FROM {1}.dbo.Inventory WHERE VoucherID IN (
|
|
SELECT v.VoucherID FROM {1}.dbo.Vouchers v WHERE v.Date BETWEEN @StartDate AND @EndDate)
|
|
";
|
|
using (SqlCommand cmd = new SqlCommand(string.Format(query, targetDB, sourceDB)))
|
|
{
|
|
cmd.CommandTimeout = int.MaxValue;
|
|
cmd.Parameters.AddWithValue("@StartDate", startDate);
|
|
cmd.Parameters.AddWithValue("@EndDate", finishDate);
|
|
connection.ExecuteNonQuery(cmd);
|
|
}
|
|
}
|
|
private void Clean(string sourceDB, string targetDB, DateTime lastBillDateTime)
|
|
{
|
|
List<SaleVoucherBO> list = new List<SaleVoucherBO>();
|
|
using (SqlCommand cmd = new SqlCommand(string.Format(@"
|
|
SELECT v.VoucherID, s.BillID, v.Date
|
|
FROM {0}.dbo.Vouchers v INNER JOIN {0}.dbo.SaleVoucher s ON v.VoucherID = s.VoucherID
|
|
WHERE v.Date >= @StartDate AND s.Printed = 1
|
|
ORDER BY Date", targetDB)))
|
|
{
|
|
cmd.CommandTimeout = int.MaxValue;
|
|
cmd.Parameters.AddWithValue("@StartDate", lastBillDateTime);
|
|
using (IDataReader dr = connection.ExecuteReader(cmd))
|
|
{
|
|
while (dr.Read())
|
|
{
|
|
list.Add(new SaleVoucherBO { VoucherID = dr.GetGuid(0), BillID = dr.GetString(1), Date = dr.GetDateTime(2) });
|
|
}
|
|
}
|
|
}
|
|
string billID = list[0].BillID;
|
|
for (int i = 1; i < list.Count; i++)
|
|
{
|
|
billID = GetBillID(billID);
|
|
using (SqlCommand cmd = new SqlCommand(string.Format("UPDATE {0}.dbo.SaleVoucher SET BillID = @BillID WHERE VoucherID = @VoucherID", targetDB)))
|
|
{
|
|
cmd.CommandTimeout = int.MaxValue;
|
|
cmd.Parameters.AddWithValue("@VoucherID", list[i].VoucherID);
|
|
cmd.Parameters.AddWithValue("@BillID", billID);
|
|
connection.ExecuteNonQuery(cmd);
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
}
|