narsil/Tanshu.Accounts.SqlDAO/ManagementDAO.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

353 lines
16 KiB
C#

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using Tanshu.Accounts.Contracts;
using Tanshu.Data.DAO;
using System.Data;
namespace Tanshu.Accounts.SqlDAO
{
public class ManagementDAO : BaseDAO
{
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)
{
// Will not work because of Vat / ServiceTax replacing Tax
throw new NotImplementedException();
string query;
if (tax.HasValue)
query = @"
SELECT ISNULL(SUM(i.Amount), 0) FROM Vouchers v
INNER JOIN Inventory i ON v.VoucherID = i.VoucherID
WHERE v.Date BETWEEN @StartDate AND @FinishDate AND v.PaidStatus IN (2, 3)
AND i.Tax = @Tax
";
else
query = @"
SELECT ISNULL(SUM(i.Amount), 0) FROM Vouchers v
INNER JOIN Inventory i ON v.VoucherID = i.VoucherID
WHERE v.Date BETWEEN @StartDate AND @FinishDate AND v.PaidStatus IN (2, 3)
";
using (var 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)
{
throw new NotImplementedException();
//Paid = 1, CC = 0, Void = 0
var list = new List<Guid>();
const string query = @"
SELECT DISTINCT v.VoucherID
FROM Vouchers v
INNER JOIN Inventory i ON v.VoucherID = i.VoucherID
WHERE v.Date BETWEEN @StartDate AND @FinishDate
AND v.PaidStatus = @Cash AND i.Tax = @Tax";
using (var cmd = new SqlCommand(query))
{
cmd.Parameters.AddWithValue("@StartDate", startDate);
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
cmd.Parameters.AddWithValue("@Tax", tax);
cmd.Parameters.AddWithValue("@Cash", PaidStatus.Cash);
using (var dr = connection.ExecuteReader(cmd))
{
while (dr.Read())
list.Add(dr.GetGuid(0));
}
}
return list;
}
public List<PendingBillsBO> GetPaidBills()
{
string query = @"
SELECT v.VoucherID, 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
FROM Vouchers v INNER JOIN Users u ON v.UserID = u.UserID
WHERE v.LastEditDate BETWEEN @StartDate AND @FinishDate AND v.Paid = 1 AND v.Void = 0
ORDER BY v.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 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 v.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 Vouchers v
WHERE v.PaidStatus IN (1, 4, 5, 6, 7) AND v.Date BETWEEN @StartDate AND @EndDate);
DELETE FROM Vouchers WHERE PaidStatus IN (1, 4, 5, 6, 7) AND Date BETWEEN @StartDate AND @EndDate;
"))
{
cmd.Parameters.AddWithValue("@StartDate", startDate);
cmd.Parameters.AddWithValue("@EndDate", finishDate);
connection.ExecuteNonQuery(cmd);
}
var list = new List<VoucherBO>();
using (SqlCommand cmd = new SqlCommand(@"
SELECT v.VoucherID, v.BillID, v.Date
FROM Vouchers v
WHERE v.Date >= @StartDate AND v.Printed = 1
ORDER BY Date"))
{
cmd.Parameters.AddWithValue("@StartDate", startDate);
using (IDataReader dr = connection.ExecuteReader(cmd))
{
while (dr.Read())
{
list.Add(new VoucherBO { 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].[Vouchers] 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 Vouchers 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].[Vouchers] 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].[Vouchers] 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].[Vouchers] 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 VoucherID FROM {0}.dbo.Vouchers
WHERE Date BETWEEN @StartDate AND @EndDate);
DELETE FROM {0}.dbo.Vouchers WHERE Date BETWEEN @StartDate AND @EndDate;
";
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 Date
FROM {0}.dbo.Vouchers
WHERE 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, Ref, Date, Narration, UserID, CreationDate, LastEditDate, Floor, BillID, TableID, WaiterID, CustomerID, AdvanceID, Paid, Void, VoidReason, Printed, Alarm, KotID)
SELECT VoucherID, Ref, Date, Narration, UserID, CreationDate, LastEditDate, Floor, BillID, TableID, WaiterID, CustomerID, AdvanceID, Paid, Void, VoidReason, Printed, Alarm, KotID
FROM {1}.dbo.Vouchers WHERE Date BETWEEN @StartDate AND @EndDate;
INSERT INTO {0}.dbo.Inventory (InventoryID, VoucherID, ProductID, Quantity, Rate, Tax, Discount)
SELECT InventoryID, VoucherID, ProductID, Quantity, Rate, Tax, Discount
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)
{
var list = new List<VoucherBO>();
using (SqlCommand cmd = new SqlCommand(string.Format(@"
SELECT VoucherID, BillID, Date
FROM {0}.dbo.Vouchers
WHERE Date >= @StartDate AND 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 VoucherBO { 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.Vouchers 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
}
}