using System; using System.Collections.Generic; using System.Text; using Tanshu.Accounts.Contracts; using System.Data.SqlClient; using System.Data; //using System.Windows.Forms; using Tanshu.Data.DAO; using Tanshu.Accounts.DAOFactory; namespace Tanshu.Accounts.SqlDAO { public class SaleVoucherMixDAO : BaseDAO, ISaleVoucherMixDAO { public SaleVoucherMixDAO(IConnectionDAO connection) :base (connection) {} public SalesBillItemBO GetDefaultSaleBillItem(Guid productID) { bool isComplex; using (SqlCommand cmd = new SqlCommand("SELECT Count(*) FROM Products WHERE ProductID = @ProductID")) { cmd.Parameters.AddWithValue("@ProductID", productID); if ((int)connection.ExecuteScalar(cmd) != 0) isComplex = false; else isComplex = true; } if (!isComplex) { 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 (SqlCommand cmd = new SqlCommand(query)) { cmd.Parameters.AddWithValue("@ProductID", productID); using (IDataReader dr = connection.ExecuteReader(cmd)) { dr.Read(); SalesBillItemBO mySale = new SalesBillItemBO { productID = dr.GetGuid(0), Name = dr.GetString(1), Price = dr.GetDecimal(2), Tax = dr.GetDecimal(4), Discount = 0, Printed = 0, Quantity = 1 }; return mySale; } } } else { string query = @" SELECT c.ComplexProductID, c.Name + ' (' + c.Units +')' AS Name, c.SalePrice, c.SaleTaxID, t.Rate AS TaxRate FROM ComplexProducts c INNER JOIN Tax t ON c.SaleTaxID = t.TaxID WHERE c.ComplexProductID = @ProductID GROUP BY c.ComplexProductID, c.Name + ' (' + c.Units +')' , c.SalePrice, c.SaleTaxID, t.Rate "; using (SqlCommand cmd = new SqlCommand(query)) { cmd.Parameters.AddWithValue("@ProductID", productID); using (IDataReader dr = connection.ExecuteReader(cmd)) { dr.Read(); SalesBillItemBO mySale = new SalesBillItemBO { productID = dr.GetGuid(0), Name = dr.GetString(1), Price = dr.GetDecimal(2), Tax = dr.GetDecimal(4), Discount = 0, Printed = 0, Quantity = 1 }; return mySale; } } } } public decimal GetProductDiscountLimit(Guid productID) { string query = @"SELECT t.DiscountLimit FROM ProductTypes t INNER JOIN Products p ON t.ProductTypeID = p.ProductTypeID WHERE p.ProductID = @ProductID"; decimal discountLimit = 0; using (SqlCommand cmd = new SqlCommand(query)) { cmd.Parameters.AddWithValue("@ProductID", productID); discountLimit = (decimal)connection.ExecuteScalar(cmd); } return discountLimit; } public bool IsBillPrinted(Guid voucherID) { string query = @"SELECT Printed FROM SaleVoucher WHERE VoucherID = @VoucherID"; bool printed = true; using (SqlCommand cmd = new SqlCommand(query)) { cmd.Parameters.AddWithValue("@VoucherID", voucherID); printed = (bool)connection.ExecuteScalar(cmd); } return printed; } public List GetPendingBills(PendingType list) { string 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 FROM Vouchers t INNER JOIN SaleVoucher s ON t.VoucherID = s.VoucherID AND s.Paid = 0 AND s.Void = 0 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 (list == PendingType.Important) query += "AND s.IsImportant = 1 "; else query += "AND s.IsImportant = 0 "; 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 (IDataReader dr = connection.ExecuteReader(query)) { List outList = new List(); while (dr.Read()) { PendingBillsBO 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); outList.Add(local); } return outList; } } public Nullable GetPendingVoucherID(string tableID) { string query = "SELECT VoucherID FROM SaleVoucher where TableID = @TableID and Paid = 0 and Void = 0"; Guid? voucherID; using (SqlCommand cmd = new SqlCommand(query)) { cmd.Parameters.AddWithValue("@TableID", tableID); using (IDataReader dr = connection.ExecuteReader(cmd)) { if (dr.Read()) { voucherID = dr.GetGuid(0); dr.Close(); dr.Dispose(); return voucherID; } return null; } } } public List SaleInventory(Dictionary.ValueCollection list, Guid? voucherID) { Dictionary localList = new Dictionary(); foreach (SalesBillItemBO item in list) { InventoryBO temp = new InventoryBO(); #region Complex //if (item.location == "complex") //{ // decimal balance; // string query = "SELECT SUM(Quantity * p.SalePrice) FROM ComplexProducts c INNER JOIN Products p ON c.ProductID = p.ProductID WHERE ComplexProductID = @ComplexProductID"; // using (SqlCommand cmd = new SqlCommand(query)) // { // cmd.Parameters.AddWithValue("@ComplexProductID", item.productID); // balance = (decimal)connection.ExecuteScalar(cmd); // balance = (item.Price - balance) / balance; // } // query = "SELECT c.ProductID, c.Quantity, p.SalePrice FROM ComplexProducts c INNER JOIN Products p ON c.ProductID = p.ProductID WHERE ComplexProductID = @ComplexProductID"; // using (SqlCommand cmd = new SqlCommand(query)) // { // cmd.Parameters.AddWithValue("@ComplexProductID", item.productID); // SqlDataReader dr = connection.ExecuteReader(cmd); // while (dr.Read()) // { // InventoryBO temp = new InventoryBO(); // if (voucherID.HasValue) // temp.VoucherID = voucherID.Value; // temp.InventoryID = Guid.NewGuid(); // temp.Discount = item.Discount; // temp.IsRateFinal = true; // temp.ComplexProductID = item.productID; // temp.ProductID = dr.GetGuid(0); // temp.Quantity = item.Quantity * dr.GetDecimal(1); // temp.Rate = dr.GetDecimal(2) * (1 + balance); // temp.Tax = item.Tax; // localList.Add(temp); // } // dr.Close(); // dr.Dispose(); // } //} //else //{ // Add the lower loop here //} #endregion 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); } } List outList = new List(); foreach (var item in localList) outList.Add(item.Value); return outList; } public void GetComplexBillInformation(Guid voucherID, Guid complexProductID, ref decimal rate, ref decimal quantity, ref string name) { string query = @" SELECT TOP 1 i.Quantity / c.Quantity FROM Inventory i INNER JOIN ComplexProducts c ON i.ComplexProductID = c.ComplexProductID AND i.ProductID = c.ProductID WHERE i.VoucherID = @VoucherID AND c.ComplexProductID = @ComplexProductID "; using (SqlCommand cmd = new SqlCommand(query)) { cmd.Parameters.AddWithValue("@VoucherID", voucherID); cmd.Parameters.AddWithValue("@ComplexProductID", complexProductID); quantity = (decimal)connection.ExecuteScalar(cmd); } using (SqlCommand cmd = new SqlCommand("SELECT TOP 1 SalePrice, Name FROM ComplexProducts WHERE ComplexProductID = @ComplexProductID")) { cmd.Parameters.AddWithValue("@VoucherID", voucherID); cmd.Parameters.AddWithValue("@ComplexProductID", complexProductID); using (IDataReader dr = connection.ExecuteReader(cmd)) { dr.Read(); rate = dr.GetDecimal(0); name = dr.GetString(1); } } } private class BillPaidClass { public Guid VoucherID { get; set; } public string KotID { get; set; } public bool Printed { get; set; } } public void DeclareBillsPaid(Guid userID, List billList, bool creditCard) { List removeItems = new List(); string query = "SELECT VoucherID, KotID, Printed FROM SaleVoucher WHERE VoucherID IN ("; foreach (Guid voucherID in billList) { query += string.Format("'{0}', ", voucherID); } query = query.Substring(0, query.Length - 2) + ")"; List pbc = new List(); using (SqlCommand cmd = new SqlCommand(query)) { using (IDataReader dr = connection.ExecuteReader(cmd)) { while (dr.Read()) { pbc.Add(new BillPaidClass { VoucherID = dr.GetGuid(0), KotID = dr.GetString(1), Printed = dr.GetBoolean(2) }); } } } foreach (BillPaidClass 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 != false) { using (SqlCommand cmd = new SqlCommand("UPDATE SaleVoucher SET Paid = 1, CreditCard = @CreditCard 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("@CreditCard", creditCard); connection.ExecuteNonQuery(cmd); } removeItems.Add(item.VoucherID); } } foreach (Guid item in removeItems) billList.Remove(item); } public void ToggleImportant(Guid voucherID) { using (SqlCommand cmd = new SqlCommand("UPDATE SaleVoucher SET IsImportant= CASE WHEN IsImportant = 0 THEN 1 ELSE 0 END FROM SaleVoucher WHERE VoucherID = @VoucherID")) { cmd.Parameters.AddWithValue("@VoucherID", voucherID); connection.ExecuteNonQuery(cmd); } } } }