/// <summary>
        /// Update into XMDeliveryProductInventory
        /// </summary>
        /// <param name="xmdeliveryproductinventory">XMDeliveryProductInventory</param>
        public void UpdateXMDeliveryProductInventory(XMDeliveryProductInventory xmdeliveryproductinventory)
        {
            if (xmdeliveryproductinventory == null)
            {
                return;
            }

            if (this._context.IsAttached(xmdeliveryproductinventory))
            {
                this._context.XMDeliveryProductInventories.Attach(xmdeliveryproductinventory);
            }

            this._context.SaveChanges();
        }
        /// <summary>
        /// Insert into XMDeliveryProductInventory
        /// </summary>
        /// <param name="xmdeliveryproductinventory">XMDeliveryProductInventory</param>
        public void InsertXMDeliveryProductInventory(XMDeliveryProductInventory xmdeliveryproductinventory)
        {
            if (xmdeliveryproductinventory == null)
            {
                return;
            }

            if (!this._context.IsAttached(xmdeliveryproductinventory))
            {
                this._context.XMDeliveryProductInventories.AddObject(xmdeliveryproductinventory);
            }

            this._context.SaveChanges();
        }
        public List <XMDeliveryProductInventory> ToUpdateOrderProduceList(List <XMProductNew> list, bool IsAdd, List <XMDeliveryProductInventory> OrderProduceList, int Year, int Month, int UserId)
        {
            var NickGroup = list.GroupBy(x => x.NickId).ToList();

            foreach (var Item in NickGroup)
            {
                var NickList = Item.ToList();
                if (NickList != null && NickList.Count > 0)
                {
                    var nickList     = OrderProduceList.Where(x => x.NickId == NickList[0].NickId).ToList();
                    var ProductGroup = NickList.GroupBy(x => x.ManufacturersCode).ToList();
                    foreach (var item in ProductGroup)
                    {
                        var ProductList = item.ToList();
                        if (ProductList != null && ProductList.Count > 0)
                        {
                            int     Count  = (int)ProductList.Sum(x => x.count);
                            decimal Amount = (decimal)ProductList.Sum(x => x.Saleprice);

                            if (!IsAdd)
                            {
                                Count  = -Count;
                                Amount = -Amount;
                            }

                            var productGroup = nickList.Where(x => x.ManufacturersCode == ProductList[0].ManufacturersCode).ToList();
                            if (productGroup != null && productGroup.Count > 0)
                            {
                                //入库
                                productGroup[0].StorageCount  += Count;
                                productGroup[0].StorageAmount += Amount;
                                //出库
                                productGroup[0].DeliveryCount  += Count;
                                productGroup[0].DeliveryAmount += +Amount;
                            }
                            else
                            {
                                XMDeliveryProductInventory one = new XMDeliveryProductInventory();
                                one.NickId            = NickList[0].NickId;
                                one.Year              = Year;
                                one.Month             = Month;
                                one.ManufacturersCode = ProductList[0].ManufacturersCode;
                                one.ProductName       = ProductList[0].ProductName;
                                one.Specifications    = ProductList[0].Specifications;

                                //入库
                                one.StorageCount  = Count;
                                one.StorageAmount = Amount;
                                //出库
                                one.DeliveryCount  = Count;
                                one.DeliveryAmount = Amount;
                                //库存
                                one.InventoryCount  = one.StorageCount - one.DeliveryCount;
                                one.InventoryAmount = one.StorageAmount - one.DeliveryAmount;

                                one.IsEnable   = false;
                                one.CreateDate = one.UpdateDate = DateTime.Now;
                                one.CreateID   = one.UpdateID = UserId;

                                OrderProduceList.Add(one);
                            }
                        }
                    }
                }
            }

            return(OrderProduceList);
        }
        public void ToAddDeliveryProductInventory(DateTime date)
        {
            try
            {
                int UserId = 0;
                if (HozestERPContext.Current.User != null)
                {
                    UserId = HozestERPContext.Current.User.CustomerID;
                }
                else
                {
                    string          UserName = "******";
                    List <Customer> customer = IoC.Resolve <ICustomerService>().GetCustomerByUsernameList(UserName);
                    if (customer.Count > 0)
                    {
                        UserId = customer[0].CustomerID;
                    }
                }

                DateTime end   = date;
                DateTime begin = date.AddMonths(-1);
                int      Month = begin.Month;
                int      Year  = begin.Year;
                List <XMDeliveryProductInventory> OrderProduceList = new List <XMDeliveryProductInventory>();

                #region 订单生成

                //在途库存明细
                var DeliveryListNotShipped = IoC.Resolve <IXMOrderInfoService>().GetrderInfoProductDetailsListNotShipped(begin, end);
                //订单已发货
                var DeliveryList = IoC.Resolve <IXMOrderInfoService>().GetXMOrderInfoListByDeliveryTime(begin, end);
                if (DeliveryList != null && DeliveryList.Count > 0)
                {
                    //订单已完成
                    var CompletionList = DeliveryList.Where(x => x.CompletionTime != null && x.CompletionTime >= begin && x.CompletionTime < end).ToList();

                    var NickGroup = DeliveryList.GroupBy(x => x.NickID).ToList();
                    foreach (var Item in NickGroup)
                    {
                        var NickList = Item.ToList();
                        if (NickList != null && NickList.Count > 0)
                        {
                            var nickList      = CompletionList.Where(x => x.NickID == NickList[0].NickID).ToList();
                            var DeliveryGroup = ToGetProductDetailsList(NickList);
                            var deliveryGroup = ToGetProductDetailsList(nickList);
                            if (DeliveryGroup.Count > 0)
                            {
                                var ProductGroup = DeliveryGroup.GroupBy(x => x.TManufacturersCode).ToList();
                                foreach (var item in ProductGroup)
                                {
                                    var ProductList = item.ToList();
                                    if (ProductList != null && ProductList.Count > 0)
                                    {
                                        XMDeliveryProductInventory one = new XMDeliveryProductInventory();
                                        var productGroup = deliveryGroup.Where(x => x.TManufacturersCode == ProductList[0].TManufacturersCode).ToList();
                                        one.NickId            = NickList[0].NickID;
                                        one.Year              = Year;
                                        one.Month             = Month;
                                        one.ManufacturersCode = ProductList[0].TManufacturersCode;
                                        one.ProductName       = ProductList[0].ProductName;
                                        one.Specifications    = ProductList[0].Specifications;
                                        //剩余库存
                                        one.SurplusInventory = DeliveryListNotShipped.Where(x => x.Manufacturers == one.ManufacturersCode).Sum(x => x.ProductNum);
                                        //入库
                                        one.StorageCount  = ProductList.Sum(x => x.ProductNum);
                                        one.StorageAmount = ProductList.Sum(x => x.SalesPrice);
                                        //出库
                                        one.DeliveryCount  = productGroup.Sum(x => x.ProductNum);
                                        one.DeliveryAmount = productGroup.Sum(x => x.SalesPrice);
                                        //库存
                                        one.InventoryCount  = one.StorageCount - one.DeliveryCount;
                                        one.InventoryAmount = one.StorageAmount - one.DeliveryAmount;

                                        one.IsEnable   = false;
                                        one.CreateDate = one.UpdateDate = DateTime.Now;
                                        one.CreateID   = one.UpdateID = UserId;

                                        OrderProduceList.Add(one);
                                    }
                                }
                            }
                        }
                    }
                }

                #endregion

                #region 退换货生成

                List <XMApplication> ApplicationList = new List <XMApplication>();
                List <int?>          ApplicationType = new List <int?>();
                ApplicationType.Add(5);                                                                                       //先退货后退款
                ApplicationType.Add(7);                                                                                       //先退款后退货
                var applicationList1 = IoC.Resolve <IXMApplicationService>().GetXMApplicationListByReturnTime(begin, end, ApplicationType);
                var applicationList2 = IoC.Resolve <IXMApplicationService>().GetXMApplicationListByFinishTime(begin, end, 6); //换货
                ApplicationList.AddRange(applicationList1);
                ApplicationList.AddRange(applicationList2);

                var DetailList = ToGetApplicationDetailsList(ApplicationList, 1); //换货新增的产品
                var detailList = ToGetApplicationDetailsList(ApplicationList, 2); //退货旧产品
                OrderProduceList = ToUpdateOrderProduceList(DetailList, true, OrderProduceList, Year, Month, UserId);
                OrderProduceList = ToUpdateOrderProduceList(detailList, false, OrderProduceList, Year, Month, UserId);

                #endregion

                foreach (var Info in OrderProduceList)
                {
                    IoC.Resolve <IXMDeliveryProductInventoryService>().InsertXMDeliveryProductInventory(Info);
                }
            }
            catch (Exception ex)
            {
            }
        }