using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; using NHibernate; using Tanshu.Accounts.Contracts; using Tanshu.Accounts.Entities; using Tanshu.Data.DAO; namespace Tanshu.Accounts.Repository { public class ManagementBI : FluentBasicBase { public ManagementBI() : base() { } public ManagementBI(bool beginTransaction) : base(beginTransaction) { } public ManagementBI(ISession session) : base(session) { } public ManagementBI(ISession session, bool beginTransaction) : base(session, beginTransaction) { } public decimal GetQuantity(int baseCode, DateTime startDate, DateTime finishDate) { const string query = @" select sum(i.Quantity * p.Quantity) as Quantity from Voucher v inner join v.Kots k inner join k.Inventories i inner join i.Product p where v.Date >= :startDate and v.Date <= :finishDate and v.Void = false and p.BaseCode = :baseCode "; var qty = Session .CreateQuery(query) .SetParameter("startDate", startDate) .SetParameter("finishDate", finishDate) .SetParameter("baseCode", baseCode) .UniqueResult(); return qty == null ? 0 : (decimal)qty; } public decimal SetQuantity(int baseCode, decimal quantity, DateTime startDate, DateTime finishDate) { var list = Randomize(new VoucherBI().List(x => x.Date >= startDate && x.Date <= finishDate && x.Void == false)); var current = GetQuantity(baseCode, startDate, finishDate); foreach (var item in list) { if (current <= quantity) continue; foreach (var kot in item.Kots) { if (current <= quantity) continue; foreach (var inventory in kot.Inventories) { if (current <= quantity) continue; if (inventory.Product.BaseCode == baseCode) { using (var bi = new InventoryBI()) { if (inventory.Quantity * inventory.Product.Quantity > current - quantity) { current = quantity; inventory.Quantity = (current - quantity) / inventory.Product.Quantity; var i = bi.Get(x => x.InventoryID == inventory.InventoryID); i.Quantity = current - quantity; bi.Update(i); } else { current -= inventory.Quantity * inventory.Product.Quantity; bi.Delete(x => x.InventoryID == inventory.InventoryID); } } } } } } return GetQuantity(baseCode, startDate, finishDate); } // public decimal GetBalance(decimal? tax, DateTime startDate, DateTime endDate) // { // GetFactory factory = GetFactory.GetDAOFactory(Database.GetFactoryType); // using (IConnectionDAO connection = factory.Connection) // { // using (IManagementDAO dao = factory.GetManagementDAO(startDate, endDate, connection)) // { // return dao.GetBalance(tax); // } // } // } // public List GetUpdateBillList(decimal tax, bool voided, bool paid, bool creditCard, DateTime startDate, DateTime endDate) // { // GetFactory factory = GetFactory.GetDAOFactory(Database.GetFactoryType); // using (IConnectionDAO connection = factory.Connection) // { // using (IManagementDAO dao = factory.GetManagementDAO(startDate, endDate, connection)) // { // return dao.GetUpdateBillList(tax, voided, paid, creditCard); // } // } // } // public decimal Update(Guid voucherID, decimal tax, DateTime startDate, DateTime endDate) // { // GetFactory factory = GetFactory.GetDAOFactory(Database.GetFactoryType); // using (IConnectionDAO connection = factory.Connection) // { // using (IManagementDAO dao = factory.GetManagementDAO(startDate, endDate, connection)) // { // return dao.Update(voucherID, tax); // } // } // } // public void Reorder(DateTime startDate, DateTime endDate, ShowProgessDelegate showProgressDelegate) // { // GetFactory factory = GetFactory.GetDAOFactory(Database.GetFactoryType); // using (IConnectionDAO connection = factory.Connection) // { // using (IManagementDAO dao = factory.GetManagementDAO(startDate, endDate, connection)) // { // dao.Reorder(showProgressDelegate); // } // } // } // public bool MergeData(DateTime startDate, DateTime endDate, string sourceDB, string targetDB) // { // GetFactory factory = GetFactory.GetDAOFactory(Database.GetFactoryType); // using (IConnectionDAO connection = factory.Connection) // { // using (IManagementDAO dao = factory.GetManagementDAO(startDate, endDate, connection)) // { // return dao.MergeData(sourceDB, targetDB); // } // } // } // public List GetPaidBills(DateTime startDate, DateTime finishDate) // { // GetFactory factory = GetFactory.GetDAOFactory(Database.GetFactoryType); // using (IConnectionDAO connection = factory.Connection) // { // using (IManagementDAO dao = factory.GetManagementDAO(startDate, finishDate, connection)) // { // return dao.GetPaidBills(); // } // } // } private static IList Randomize(IEnumerable list) { var tList = list.ToArray(); var rand = new Random(); for (var i = tList.Length - 1; i > 0; i--) { var swapIndex = rand.Next(i + 1); var tmp = tList[i]; tList[i] = tList[swapIndex]; tList[swapIndex] = tmp; } return tList.ToList(); } } }