narsil/Tanshu.Accounts.SqlDAO/SaleVoucherMixDAO.cs
Amritanshu b6498a7b1c Feature: Sale analysis printout changed.
Feature: Sale analysis detail printing built
2013-11-16 11:31:58 +05:30

251 lines
10 KiB
C#

using System;
using System.Collections.Generic;
using Tanshu.Accounts.Contracts;
using System.Data.SqlClient;
using Tanshu.Data.DAO;
namespace Tanshu.Accounts.SqlDAO
{
public class SaleVoucherMixDAO : BaseDAO
{
public SaleVoucherMixDAO(IConnectionDAO connection)
: base(connection)
{ }
public SalesBillItemBO GetDefaultSaleBillItem(Guid productID)
{
const string query = @"
SELECT p.ProductID, p.Name + ' (' + p.Units +')',
p.SalePrice, p.SaleTaxID, t.Rate AS TaxRate,
p.SaleLedgerID, l.CostCenterID
FROM Products p INNER JOIN Tax t ON p.SaleTaxID = t.TaxID INNER JOIN Ledgers l ON p.SaleLedgerID = l.LedgerID
WHERE p.ProductID = @ProductID
";
using (var cmd = new SqlCommand(query))
{
cmd.Parameters.AddWithValue("@ProductID", productID);
using (var dr = connection.ExecuteReader(cmd))
{
dr.Read();
var mySale = new SalesBillItemBO
{
productID = dr.GetGuid(0),
Name = dr.GetString(1),
Price = dr.GetDecimal(2),
Tax = dr.GetDecimal(4),
Discount = 0,
location = "",
Printed = 0,
Quantity = 1
};
return mySale;
}
}
}
public decimal GetProductDiscountLimit(Guid productID)
{
const string query = @"SELECT t.DiscountLimit FROM ProductTypes t INNER JOIN Products p ON t.ProductTypeID = p.ProductTypeID WHERE p.ProductID = @ProductID";
decimal discountLimit;
using (var cmd = new SqlCommand(query))
{
cmd.Parameters.AddWithValue("@ProductID", productID);
discountLimit = (decimal)connection.ExecuteScalar(cmd);
}
return discountLimit;
}
public bool IsBillPrinted(Guid voucherID)
{
const string query = @"SELECT Printed FROM SaleVoucher WHERE VoucherID = @VoucherID";
using (var cmd = new SqlCommand(query))
{
cmd.Parameters.AddWithValue("@VoucherID", voucherID);
return (bool)connection.ExecuteScalar(cmd);
}
}
public List<PendingBillsBO> GetPendingBills(PendingType list, int floor)
{
var query = @"
SELECT t.VoucherID, s.Alarm AS Alarm, 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, s.AdvanceID
FROM Vouchers t INNER JOIN SaleVoucher s ON t.VoucherID = s.VoucherID AND s.PaidStatus = @PaidStatus
INNER JOIN Users u ON t.UserID = u.UserID
";
if (list == PendingType.Alarms)
query += "WHERE s.Alarm IS NOT NULL ";
else
query += "WHERE s.Alarm IS NULL ";
if (floor != 0)
query += "AND s.Floor = @Floor ";
if (list == PendingType.Today)
query += string.Format("AND t.LastEditDate >= '{0:dd-MMM-yyyy} 00:00:00' ", DateTime.Now);
else if (list == PendingType.Week)
query += string.Format("AND t.LastEditDate >= '{0:dd-MMM-yyyy} 00:00:00' ", DateTime.Now.AddDays(-7));
query += "ORDER BY t.CreationDate ";
using (var cmd = new SqlCommand(query))
{
cmd.Parameters.AddWithValue("@PaidStatus", (int)PaidStatus.Pending);
if (floor != 0)
cmd.Parameters.AddWithValue("@Floor", floor);
using (var dr = connection.ExecuteReader(cmd))
{
var outList = new List<PendingBillsBO>();
while (dr.Read())
{
var local = new PendingBillsBO();
local.voucherID = dr.GetGuid(0);
local.Alarm = !dr.IsDBNull(1);
if (local.Alarm)
local.AlarmTime = dr.GetDateTime(1);
local.TableID = dr.GetString(2);
if (!dr.IsDBNull(3))
local.Amount = dr.GetDecimal(3);
local.BillNo = dr.GetString(4);
local.BookingTime = dr.GetDateTime(5);
local.Cashier = dr.GetString(6);
local.Important = dr.GetBoolean(7);
local.Kot = dr.GetString(8);
local.LastEdited = dr.GetDateTime(9);
local.Printed = dr.GetBoolean(10);
if (!dr.IsDBNull(11))
local.AdvanceID = dr.GetGuid(11);
outList.Add(local);
}
return outList;
}
}
}
public Guid? GetPendingVoucherID(string tableID, int floor)
{
var query = "SELECT VoucherID FROM SaleVoucher where TableID = @TableID and PaidStatus = @PaidStatus";
if (floor != 0)
query += " AND Floor = @Floor ";
using (var cmd = new SqlCommand(query))
{
cmd.Parameters.AddWithValue("@TableID", tableID);
cmd.Parameters.AddWithValue("@PaidStatus", (int)PaidStatus.Pending);
if (floor != 0)
cmd.Parameters.AddWithValue("@Floor", floor);
using (var dr = connection.ExecuteReader(cmd))
{
if (dr.Read())
{
return dr.GetGuid(0);
}
return null;
}
}
}
public List<InventoryBO> SaleInventory(Dictionary<BillItemKey, SalesBillItemBO>.ValueCollection list, Guid? voucherID)
{
var localList = new Dictionary<Guid, InventoryBO>();
foreach (var item in list)
{
var temp = new InventoryBO();
if (localList.ContainsKey(item.productID))
{
temp = localList[item.productID];
temp.Quantity += item.Quantity;
}
else
{
if (voucherID.HasValue)
temp.VoucherID = voucherID.Value;
temp.InventoryID = Guid.NewGuid();
temp.Discount = item.Discount;
temp.IsRateFinal = true;
temp.ComplexProductID = null;
temp.ProductID = item.productID;
temp.Quantity = item.Quantity;
temp.Rate = item.Price;
temp.Tax = item.Tax;
localList.Add(item.productID, temp);
}
}
var outList = new List<InventoryBO>();
foreach (var item in localList)
outList.Add(item.Value);
return outList;
}
private class BillPaidClass
{
public Guid VoucherID { get; set; }
public string KotID { get; set; }
public bool Printed { get; set; }
}
public void DeclareBillsPaid(Guid userID, List<Guid> billList, PaidStatus paidStatus)
{
var removeItems = new List<Guid>();
var query = "SELECT VoucherID, KotID, Printed FROM SaleVoucher WHERE VoucherID IN (";
foreach (var voucherID in billList)
{
query += string.Format("'{0}', ", voucherID);
}
query = query.Substring(0, query.Length - 2) + ")";
var pbc = new List<BillPaidClass>();
using (var cmd = new SqlCommand(query))
{
using (var dr = connection.ExecuteReader(cmd))
{
while (dr.Read())
{
pbc.Add(new BillPaidClass
{
VoucherID = dr.GetGuid(0),
KotID = dr.GetString(1),
Printed = dr.GetBoolean(2)
});
}
}
}
foreach (var item in pbc)
{
//if ((item.Printed != false) || (MessageBox.Show("Pay unprinted bill No.: " + item.KotID + " ?", "Pay unprinted bill", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes))
if (!item.Printed)
continue;
using (var cmd = new SqlCommand("UPDATE SaleVoucher SET PaidStatus = @PaidStatus WHERE VoucherID = @VoucherID; UPDATE Vouchers SET UserID = @UserID, LastEditDate = GETDATE() WHERE VoucherID = @VoucherID"))
{
cmd.Parameters.AddWithValue("@VoucherID", item.VoucherID);
cmd.Parameters.AddWithValue("@UserID", userID);
cmd.Parameters.AddWithValue("@PaidStatus", (int)paidStatus);
connection.ExecuteNonQuery(cmd);
}
removeItems.Add(item.VoucherID);
}
foreach (var item in removeItems)
billList.Remove(item);
}
#region ISaleVoucherMixDAO Members
public decimal Amount(Guid voucherID)
{
decimal amount;
using (var cmd = new SqlCommand("SELECT SUM(Amount) FROM Inventory WHERE VoucherID = @VoucherID"))
{
cmd.Parameters.AddWithValue("@VoucherID", voucherID);
amount = (decimal)connection.ExecuteScalar(cmd);
}
object advanceID;
using (var cmd = new SqlCommand("SELECT AdvanceID FROM SaleVoucher WHERE VoucherID = @VoucherID"))
{
cmd.Parameters.AddWithValue("@VoucherID", voucherID);
advanceID = connection.ExecuteScalar(cmd);
if (advanceID == DBNull.Value)
return amount;
}
using (var cmd = new SqlCommand("SELECT Amount FROM Advances WHERE AdvanceID = @AdvanceID"))
{
cmd.Parameters.AddWithValue("@AdvanceID", (Guid)advanceID);
return amount - (decimal)connection.ExecuteScalar(cmd);
}
}
#endregion
}
}