/// <summary>
        /// Do close dealer, after all warehouses closed.
        /// </summary>
        /// <param name="dealerCode"></param>
        /// <param name="month"></param>
        /// <param name="year"></param>
        /// <param name="forceReSum"></param>
        public static void DoClose(string dealerCode, int month, int year, bool forceReSum)
        {
            if ((year > DateTime.Now.Year) //|| (year < 2000)
                 || ((year == DateTime.Now.Year) && (month > DateTime.Now.Month))
                 || (month > 12) || (month < 1)
               )
            {
                throw new Exception("Invalid closing month!");
            }

            // check Dealer
            Dealer d = DealerDAO.GetDealerByCode(dealerCode);
            if (d == null) throw new Exception("Invalid closing dealer!");
            // check all warehouse and sub dealer closed
            if (!InventoryDAO.CanCloseDealer(dealerCode, year, month)) throw new Exception(string.Format("Cannot close {0}! All sub components must be closed before.", dealerCode));

            var dc = DCFactory.GetDataContext<PartDataContext>();

            // lock closed month
            InventoryLock ilck = InventoryDAO.GetInventoryLock(d.DealerCode);
            if (ilck == null)
            {
                if (year < 2008) throw new Exception("Invalid closing month at first time!");
                // lock month valid to first inventory action?
                Inventory frstIv = InventoryDAO.GetFirstInventory(dealerCode);
                if ((frstIv != null) && ((frstIv.Year < year) || ((frstIv.Year == year) && (frstIv.Month < month))))
                {
                    throw new Exception("At first time, closing month cannot be greater than the first month that changing inventory action happen!");
                }
                ilck = new InventoryLock() { WarehouseId = null, DealerCode = d.DealerCode, Month = month, Year = year };
                dc.InventoryLocks.InsertOnSubmit(ilck);
            }
            else
            {
                // change locked month to new Closed month
                if (ilck.Month == 12) { ilck.Month = 1; ilck.Year++; }
                else ilck.Month++;
                if ((ilck.Year > DateTime.Now.Year) || ((ilck.Year == DateTime.Now.Year) && (ilck.Month > DateTime.Now.Month))) throw new Exception("Invalid closing month!");
            }

            //org:::::: summarization
            //var parts = dc.PartInfos.Where(p => p.DealerCode == dealerCode && p.PartSafeties.Count > 0);

            //foreach (var part in parts)
            //{
            //    Inventory currIv = InventoryDAO.GetPartInventory(part.PartInfoId, d.DealerCode, ilck.Year, ilck.Month);
            //    if (currIv == null)
            //    {
            //        currIv = new Inventory() { DealerCode = d.DealerCode, Month = ilck.Month, PartInfoId = part.PartInfoId, WarehouseId = null, Year = ilck.Year, };
            //        dc.Inventories.InsertOnSubmit(currIv);
            //    }

            //    if (forceReSum)
            //    {
            //        List<Inventory> childsIv = InventoryDAO.GetWPartInventories(part.PartInfoId, d.DealerCode, ilck.Year, ilck.Month);
            //        if (childsIv != null) currIv.Quantity = childsIv.Sum(iv => iv.Quantity);
            //    }
            //}

            var inventories = dc.Inventories.Where(i => i.WarehouseId == null && i.Year == ilck.Year && i.Month == ilck.Month && i.DealerCode == dealerCode);
            var childInventories = dc.Inventories.Where(i => i.Warehouse != null && i.Year == ilck.Year && i.Month == ilck.Month && i.DealerCode == dealerCode);
            var parts = dc.PartInfos.Where(p => p.DealerCode == dealerCode && p.PartSafeties.Count > 0).ToList()
                                    .GroupJoin(inventories,
                                               p => p.PartInfoId,
                                               i => i.PartInfoId,
                                               (p, i) => new
                                               {
                                                   PartInfoId = p.PartInfoId,
                                                   InventoryExisted = i.FirstOrDefault() != null
                                               })
                                    .GroupJoin(childInventories,
                                               p => p.PartInfoId,
                                               c => c.PartInfoId,
                                               (p, c) => new
                                               {
                                                   PartInfoId = p.PartInfoId,
                                                   InventoryExisted = p.InventoryExisted,
                                                   ChildQuantity = c.Sum(ci => ci.Quantity)
                                               });

            foreach (var part in parts)
            {
                Inventory currIv = null;

                if (!part.InventoryExisted)
                {
                    currIv = new Inventory() { DealerCode = d.DealerCode, Month = ilck.Month, PartInfoId = part.PartInfoId, WarehouseId = null, Year = ilck.Year, };
                    dc.Inventories.InsertOnSubmit(currIv);
                }

                if (forceReSum)
                {
                    if (currIv == null) currIv = inventories.SingleOrDefault(i => i.PartInfoId == part.PartInfoId);
                    currIv.Quantity = part.ChildQuantity;
                }
            }

            dc.SubmitChanges();

            // gen excel file
            new PartMonthlyReport(null, dealerCode, ilck.Month, ilck.Year).DoReport();
        }
 private void detach_InventoryLocks(InventoryLock entity)
 {
     this.SendPropertyChanging();
     entity.Dealer = null;
 }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="wId"></param>
        /// <param name="month">New Closed month when no record found</param>
        /// <param name="year">New Closed year when no record found></param>
        public static void DoClose(long wId, int month, int year, bool forceReSum)
        {
            if ((year > DateTime.Now.Year) //|| (year < 2000)
                 || ((year == DateTime.Now.Year) && (month > DateTime.Now.Month))
                 || (month > 12) || (month < 1)
               )
            {
                throw new Exception("Invalid closing month!");
            }

            var dc = DCFactory.GetDataContext<PartDataContext>();

            // check warehouse
            Warehouse wh = WarehouseDAO.GetWarehouse(wId);
            if (wh == null) throw new Exception("Invalid closing warehouse!");

            // lock closed month
            InventoryLock ilck = InventoryDAO.GetInventoryLock(wId);
            if (ilck == null)
            {
                if (year < 2008) throw new Exception("Invalid closing month at first time!");
                // lock month valid to first inventory action?
                Inventory frstIv = InventoryDAO.GetFirstInventory(wId);
                if ((frstIv != null) && ((frstIv.Year < year) || ((frstIv.Year == year) && (frstIv.Month < month))))
                {
                    throw new Exception("At first time, closing month cannot be greater than the first month that changing inventory action happen!");
                }
                ilck = new InventoryLock() { WarehouseId = wId, DealerCode = wh.DealerCode, Month = month, Year = year };
                dc.InventoryLocks.InsertOnSubmit(ilck);
            }
            else
            {
                // change locked month to new Closed month
                if (ilck.Month == 12) { ilck.Month = 1; ilck.Year++; }
                else ilck.Month++;
                if ((ilck.Year > DateTime.Now.Year) || ((ilck.Year == DateTime.Now.Year) && (ilck.Month > DateTime.Now.Month))) throw new Exception("Invalid closing month!");
            }

            // summarization

            #region original code
            //List<PartSafety> pses = PartInfoDAO.GetPartSafeties(wId);
            //foreach (PartSafety ps in pses)
            //{
            //    Inventory currIv = InventoryDAO.GetPartInventory(ps.PartInfoId, wh.WarehouseId, ilck.Year, ilck.Month);
            //    if (currIv == null)
            //    {
            //        Inventory prevIv = InventoryDAO.GetPrevInventory(ps.PartInfoId, wh.WarehouseId, ilck.Year, ilck.Month);
            //        currIv = new Inventory() { PartInfoId = ps.PartInfoId, Quantity = 0, DealerCode = wh.DealerCode, WarehouseId = wh.WarehouseId, Month = ilck.Month, Year = ilck.Year };
            //        dc.Inventories.InsertOnSubmit(currIv);
            //        if (prevIv != null) currIv.Quantity = prevIv.Quantity;
            //        currIv.Quantity += TransactionDAO.Summarization(ps.PartInfoId, ilck.Month, ilck.Year);
            //    }
            //    else if (forceReSum)
            //    {
            //        Inventory prevIv = InventoryDAO.GetPrevInventory(ps.PartInfoId, wh.WarehouseId, ilck.Year, ilck.Month);
            //        currIv.Quantity = (prevIv != null) ? prevIv.Quantity : 0;
            //        currIv.Quantity += TransactionDAO.Summarization(ps.PartInfoId, ilck.Month, ilck.Year);
            //    }
            //}
            #endregion

            var dateFrom = new DateTime(ilck.Year, ilck.Month, 1);
            var dateTo = dateFrom.AddMonths(1);

            var pses = PartInfoDAO.GetPartSafeties(wId).GroupJoin(InventoryDAO.GetInventories(wh.WarehouseId, ilck.Year, ilck.Month),
                                                                  ps => ps.PartInfoId,
                                                                  i => i.PartInfoId,
                                                                  (ps, i) => new
                                                                  {
                                                                      PartInfoId = ps.PartInfoId,
                                                                      InventoryExisted = i.FirstOrDefault() != null
                                                                  })
                                                       .GroupJoin(InventoryDAO.GetPrevInventories(wh.WarehouseId, ilck.Year, ilck.Month),
                                                                  p => p.PartInfoId,
                                                                  i => i.PartInfoId,
                                                                  (p, i) => new
                                                                  {
                                                                      PartInfoId = p.PartInfoId,
                                                                      InventoryExisted = p.InventoryExisted,
                                                                      PrevInventoryQuantity = i.FirstOrDefault() == null ? 0 : i.FirstOrDefault().Quantity
                                                                  })
                                                       .GroupJoin(dc.TransactionHistories.Where(th => th.TransactionDate >= dateFrom && th.TransactionDate < dateTo),
                                                                  p => p.PartInfoId,
                                                                  t => t.PartInfoId,
                                                                  (p, t) => new
                                                                  {
                                                                      PartInfoId = p.PartInfoId,
                                                                      InventoryExisted = p.InventoryExisted,
                                                                      PrevInventoryQuantity = p.PrevInventoryQuantity,
                                                                      TransactionQuantity = t.Sum(th => th.Quantity)
                                                                  });

            var inventories = InventoryDAO.GetInventories(wh.WarehouseId, ilck.Year, ilck.Month);

            foreach (var ps in pses)
            {
                Inventory currIv = null;

                if (!ps.InventoryExisted)
                {
                    currIv = new Inventory() { PartInfoId = ps.PartInfoId, Quantity = 0, DealerCode = wh.DealerCode, WarehouseId = wh.WarehouseId, Month = ilck.Month, Year = ilck.Year };
                    dc.Inventories.InsertOnSubmit(currIv);
                    currIv.Quantity = ps.PrevInventoryQuantity + ps.TransactionQuantity;

                }
                else if (forceReSum)
                {
                    if (currIv == null) currIv = inventories.SingleOrDefault(i => i.PartInfoId == ps.PartInfoId);
                    currIv.Quantity = ps.PrevInventoryQuantity + ps.TransactionQuantity;
                }

            }

            dc.SubmitChanges();

            // gen excel file
            new PartMonthlyReport(wId.ToString(), wh.DealerCode, ilck.Month, ilck.Year).DoReport();
        }
 private void attach_InventoryLocks(InventoryLock entity)
 {
     this.SendPropertyChanging();
     entity.Warehouse = this;
 }
        /// <summary>
        /// Do close dealer, after all warehouses closed.
        /// </summary>
        /// <param name="dealerCode"></param>
        /// <param name="month"></param>
        /// <param name="year"></param>
        /// <param name="forceReSum"></param>
        public static void DoClose(string dealerCode, int month, int year, bool forceReSum)
        {
            if ((year > DateTime.Now.Year) //|| (year < 2000)
                 || ((year == DateTime.Now.Year) && (month > DateTime.Now.Month))
                 || (month > 12) || (month < 1)
               )
            {
                throw new Exception("Invalid closing month!");
            }

            // check Dealer
            Dealer d = DealerDAO.GetDealerByCode(dealerCode);
            if (d == null) throw new Exception("Invalid closing dealer!");
            // check all warehouse and sub dealer closed
            if (!InventoryDAO.CanCloseDealer(dealerCode, year, month)) throw new Exception(string.Format("Cannot close {0}! All sub components must be closed before.", dealerCode));

            var dc = DCFactory.GetDataContext<PartDataContext>();

            // lock closed month
            InventoryLock ilck = InventoryDAO.GetInventoryLock(d.DealerCode);
            if (ilck == null)
            {
                if (year < 2008) throw new Exception("Invalid closing month at first time!");
                // lock month valid to first inventory action?
                Inventory frstIv = InventoryDAO.GetFirstInventory(dealerCode);
                if ((frstIv != null) && ((frstIv.Year < year) || ((frstIv.Year == year) && (frstIv.Month < month))))
                {
                    throw new Exception("At first time, closing month cannot be greater than the first month that changing inventory action happen!");
                }
                ilck = new InventoryLock() { WarehouseId = null, DealerCode = d.DealerCode, Month = month, Year = year };
                dc.InventoryLocks.InsertOnSubmit(ilck);
            }
            else
            {
                // change locked month to new Closed month
                if (ilck.Month == 12) { ilck.Month = 1; ilck.Year++; }
                else ilck.Month++;
                if ((ilck.Year > DateTime.Now.Year) || ((ilck.Year == DateTime.Now.Year) && (ilck.Month > DateTime.Now.Month))) throw new Exception("Invalid closing month!");
            }
            // a
            // summarization
            var parts = dc.PartInfos.Where(p => p.DealerCode == dealerCode && p.PartSafeties.Count > 0);

            foreach (var part in parts)
            {
                Inventory currIv = InventoryDAO.GetPartInventory(part.PartInfoId, d.DealerCode, ilck.Year, ilck.Month);
                if (currIv == null)
                {
                    currIv = new Inventory() { DealerCode = d.DealerCode, Month = ilck.Month, PartInfoId = part.PartInfoId, WarehouseId = null, Year = ilck.Year, };
                    dc.Inventories.InsertOnSubmit(currIv);
                }

                if (forceReSum)
                {
                    var childsIv = InventoryDAO.GetWPartInventories(part.PartInfoId, d.DealerCode, ilck.Year, ilck.Month);
                    if (childsIv != null) currIv.Quantity = (childsIv.Count() > 0) ? childsIv.Sum(iv => iv.Quantity) : 0;
                }
            }

            dc.SubmitChanges();

            // gen excel file
            new PartMonthlyReport(null, dealerCode, ilck.Month, ilck.Year).DoReport();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="wId"></param>
        /// <param name="month">New Closed month when no record found</param>
        /// <param name="year">New Closed year when no record found></param>
        public static void DoClose(long wId, int month, int year, bool forceReSum)
        {
            if ((year > DateTime.Now.Year) //|| (year < 2000)
                 || ((year == DateTime.Now.Year) && (month > DateTime.Now.Month))
                 || (month > 12) || (month < 1)
               )
            {
                throw new Exception("Invalid closing month!");
            }

            var dc = DCFactory.GetDataContext<PartDataContext>();

            // check warehouse
            Warehouse wh = WarehouseDAO.GetWarehouse(wId);
            if (wh == null) throw new Exception("Invalid closing warehouse!");

            // lock closed month
            InventoryLock ilck = InventoryDAO.GetInventoryLock(wId);
            if (ilck == null)
            {
                if (year < 2008) throw new Exception("Invalid closing month at first time!");
                // lock month valid to first inventory action?
                Inventory frstIv = InventoryDAO.GetFirstInventory(wId);
                if ((frstIv != null) && ((frstIv.Year < year) || ((frstIv.Year == year) && (frstIv.Month < month))))
                {
                    throw new Exception("At first time, closing month cannot be greater than the first month that changing inventory action happen!");
                }
                ilck = new InventoryLock() { WarehouseId = wId, DealerCode = wh.DealerCode, Month = month, Year = year };
                dc.InventoryLocks.InsertOnSubmit(ilck);
            }
            else
            {
                // change locked month to new Closed month
                if (ilck.Month == 12) { ilck.Month = 1; ilck.Year++; }
                else ilck.Month++;
                if ((ilck.Year > DateTime.Now.Year) || ((ilck.Year == DateTime.Now.Year) && (ilck.Month > DateTime.Now.Month))) throw new Exception("Invalid closing month!");
            }

            // summarization
            List<PartSafety> pses = PartInfoDAO.GetPartSafeties(wId);
            foreach (PartSafety ps in pses)
            {
                Inventory currIv = InventoryDAO.GetPartInventory(ps.PartInfoId, wh.WarehouseId, ilck.Year, ilck.Month);
                if (currIv == null)
                {
                    Inventory prevIv = InventoryDAO.GetPrevInventory(ps.PartInfoId, wh.WarehouseId, ilck.Year, ilck.Month);
                    currIv = new Inventory() { PartInfoId = ps.PartInfoId, Quantity = 0, DealerCode = wh.DealerCode, WarehouseId = wh.WarehouseId, Month = ilck.Month, Year = ilck.Year };
                    dc.Inventories.InsertOnSubmit(currIv);
                    if (prevIv != null) currIv.Quantity = prevIv.Quantity;
                    currIv.Quantity += TransactionDAO.Summarization(ps.PartInfoId, ilck.Month, ilck.Year);
                }
                else if (forceReSum)
                {
                    Inventory prevIv = InventoryDAO.GetPrevInventory(ps.PartInfoId, wh.WarehouseId, ilck.Year, ilck.Month);
                    currIv.Quantity = (prevIv != null) ? prevIv.Quantity : 0;
                    currIv.Quantity += TransactionDAO.Summarization(ps.PartInfoId, ilck.Month, ilck.Year);
                }
            }

            dc.SubmitChanges();

            // gen excel file
            new PartMonthlyReport(wId.ToString(), wh.DealerCode, ilck.Month, ilck.Year).DoReport();
        }
 private void detach_InventoryLocks(InventoryLock entity)
 {
     this.SendPropertyChanging("InventoryLocks");
     entity.Warehouse = null;
 }
 private void attach_InventoryLocks(InventoryLock entity)
 {
     this.SendPropertyChanging("InventoryLocks");
     entity.Dealer = this;
 }