Esempio n. 1
0
        public void CancelReceipt(ReceiptMaster receiptMaster, DateTime effectiveDate)
        {
            if (!Utility.SecurityHelper.HasPermission(receiptMaster))
            {
                //throw new BusinessException("没有此收货单{0}的操作权限。", receiptMaster.ReceiptNo);
            }

            #region 判断收货单状态,只有Close才能冲销
            if (receiptMaster.Status == CodeMaster.ReceiptStatus.Cancel)
            {
                throw new BusinessException("收货单{0}已经冲销。", receiptMaster.ReceiptNo,
                    systemMgr.GetCodeDetailDescription(com.Sconit.CodeMaster.CodeMaster.ReceiptStatus, ((int)receiptMaster.Status).ToString()));
            }
            #endregion

            #region 加载收货单明细及收货单库存明细
            //  TryLoadReceiptDetails(receiptMaster);
            IList<ReceiptLocationDetail> receiptLocationDetailList = TryLoadReceiptLocationDetails(receiptMaster);
            #endregion

            #region 加载订单头和明细
            //GAP收货取消不用调整订单数据
            IList<OrderMaster> orderMasterList = null;
            IList<OrderDetail> orderDetialList = null;
            //if (receiptMaster.Type == CodeMaster.IpDetailType.Normal)
            //{
            #region 获取订单头
            orderMasterList = LoadOrderMasters(receiptMaster.ReceiptDetails.Select(det => det.OrderNo).Distinct().ToArray());
            #endregion

            #region 获取订单明细
            orderDetialList = LoadOrderDetails(receiptMaster.ReceiptDetails.Where(det => det.OrderDetailId.HasValue).Select(det => det.OrderDetailId.Value).Distinct().ToArray());
            #endregion
            //}
            #endregion

            //小数保留位数
            int decimalLength = int.Parse(systemMgr.GetEntityPreferenceValue(EntityPreference.CodeEnum.DecimalLength));

            #region 回滚送货单
            if (!string.IsNullOrWhiteSpace(receiptMaster.IpNo))
            {
                IpMaster ipMaster = this.genericMgr.FindById<IpMaster>(receiptMaster.IpNo);

                #region 查找送货单明细
                string selectIpDetailStatement = string.Empty;
                IList<object> selectIpDetailPram = new List<object>();
                foreach (int ipDetailId in receiptMaster.ReceiptDetails.Select(recDet => recDet.IpDetailId).Distinct())
                {
                    if (selectIpDetailStatement == string.Empty)
                    {
                        selectIpDetailStatement = "from IpDetail where Id in (?";
                    }
                    else
                    {
                        selectIpDetailStatement += ",?";
                    }

                    selectIpDetailPram.Add(ipDetailId);
                }
                selectIpDetailStatement += ")";
                IList<IpDetail> ipDetailList = this.genericMgr.FindAll<IpDetail>(selectIpDetailStatement, selectIpDetailPram.ToArray());
                #endregion

                #region 查找送货单库存明细
                IList<IpLocationDetail> ipLocationDetailList = LoadIpLocationDetails(ipDetailList.Select(ipDet => ipDet.Id).ToArray());
                #endregion

                #region 查找差异送货单明细
                IList<IpDetail> gapIpDetailList = this.genericMgr.FindAll<IpDetail>("from IpDetail where GapReceiptNo = ?", receiptMaster.ReceiptNo);
                #endregion

                #region 差异全部关闭
                if (gapIpDetailList != null && gapIpDetailList.Count > 0)
                {
                    #region 查找差异送货单库存明细
                    IList<IpLocationDetail> gapIpLocationDetailList = LoadIpLocationDetails(gapIpDetailList.Select(ipDet => ipDet.Id).ToArray());
                    #endregion

                    foreach (IpDetail gapIpDetail in gapIpDetailList)
                    {
                        if (gapIpDetail.ReceivedQty != 0)
                        {
                            throw new BusinessException("收货单{0}的收货差异已经调整,不能冲销。", receiptMaster.ReceiptNo);
                        }

                        gapIpDetail.IsClose = true;
                        this.genericMgr.Update(gapIpDetail);
                    }

                    foreach (IpLocationDetail gapIpLocationDetail in gapIpLocationDetailList)
                    {
                        gapIpLocationDetail.IsClose = true;
                        this.genericMgr.Update(gapIpLocationDetail);
                    }
                }
                #endregion

                #region 打开未收货的发货明细
                //只有正常收货才能打开未收货的发货明细。
                int isGapRec = receiptMaster.ReceiptDetails.Where(recDet => recDet.IpDetailType == CodeMaster.IpDetailType.Gap).Count();
                if (isGapRec == 0)
                {
                    IList<IpDetail> unReceivedIpDetailList = this.genericMgr.FindAll<IpDetail>("from IpDetail where IpNo = ? and Type = ?",
                        new object[] { ipMaster.IpNo, CodeMaster.IpDetailType.Normal });

                    if (unReceivedIpDetailList != null && unReceivedIpDetailList.Count > 0)
                    {
                        #region 查找差异送货单库存明细
                        IList<IpLocationDetail> unReceivedIpLocationDetailList = LoadIpLocationDetails(unReceivedIpDetailList.Select(ipDet => ipDet.Id).ToArray());
                        #endregion

                        foreach (IpDetail unReceivedIpDetail in unReceivedIpDetailList)
                        {
                            if (unReceivedIpDetail.IsClose && receiptMaster.ReceiptDetails.Select(p => p.IpDetailId).Contains(unReceivedIpDetail.Id))
                            {
                                unReceivedIpDetail.IsClose = false;
                                this.genericMgr.Update(unReceivedIpDetail);
                            }
                        }

                        //foreach (IpLocationDetail unReceivedIpLocationDetail in unReceivedIpLocationDetailList)
                        //{
                        //    if (unReceivedIpLocationDetail.IsClose && receiptMaster.ReceiptDetails.Select(p => p.IpDetailId).Contains(unReceivedIpLocationDetail.IpDetailId))
                        //    {
                        //        unReceivedIpLocationDetail.IsClose = false;
                        //        this.genericMgr.Update(unReceivedIpLocationDetail);
                        //    }
                        //}
                    }
                }
                #endregion

                #region 收货库存明细和送货库存明细匹配
                foreach (ReceiptLocationDetail receiptLocationDetail in receiptLocationDetailList)
                {
                    ReceiptDetail receiptDetail = receiptMaster.ReceiptDetails.Where(recDet => recDet.Id == receiptLocationDetail.ReceiptDetailId).Single();

                    decimal remainBaseQty = receiptLocationDetail.Qty;  //基本单位
                    decimal remainQty = receiptLocationDetail.Qty / receiptDetail.UnitQty; //转为订单单位

                    if (!string.IsNullOrWhiteSpace(receiptLocationDetail.HuId)
                        && ipLocationDetailList.Where(ipLocDet => !string.IsNullOrWhiteSpace(ipLocDet.HuId)).Count() > 0)
                    {
                        #region 条码和条码匹配
                        IpLocationDetail ipLocationDetail = ipLocationDetailList.Where(ipLocDet => ipLocDet.HuId == receiptLocationDetail.HuId).SingleOrDefault();
                        IpDetail ipDetail = ipDetailList.Where(ipDet => ipDet.Id == receiptDetail.IpDetailId).Single();

                        if (ipDetail.Id != receiptDetail.IpDetailId)
                        {
                            throw new TechnicalException("收货单明细和发货单明细ID不匹配。");
                        }

                        if (ipDetail.Type != receiptDetail.IpDetailType)
                        {
                            throw new TechnicalException("收货单明细和发货单明细类型不匹配。");
                        }

                        #region 扣减发货单的收货数
                        if (ipLocationDetail != null)
                        {
                            ipLocationDetail.ReceivedQty -= remainBaseQty;
                            ipLocationDetail.IsClose = false;
                        }
                        ipDetail.ReceivedQty -= remainQty; //转为订单单位
                        ipDetail.IsClose = false;

                        remainBaseQty = 0;
                        remainQty = 0;
                        #endregion

                        if (ipLocationDetail != null)
                        {
                            this.genericMgr.Update(ipLocationDetail);
                        }
                        this.genericMgr.Update(ipDetail);
                        #endregion
                    }
                    else
                    {
                        #region 按数量匹配
                        IList<IpDetail> thisIpDetailList = ipDetailList.Where(ipDet => ipDet.ReceivedQty != 0  //过滤掉已经扣完的明细
                                                                    && ipDet.Id == receiptDetail.IpDetailId).ToList();

                        if (thisIpDetailList != null && thisIpDetailList.Count > 0)
                        {
                            IList<IpLocationDetail> thisIpLocationDetailList = null;
                            foreach (IpDetail thisIpDetail in thisIpDetailList)
                            {
                                if (thisIpDetail.ReceivedQty > 0)
                                {
                                    thisIpLocationDetailList = ipLocationDetailList.Where(
                                        ipLocDet => ipLocDet.ReceivedQty > 0 && ipLocDet.IpDetailId == thisIpDetail.Id).ToList();
                                }
                                else if (thisIpDetail.ReceivedQty < 0)
                                {
                                    thisIpLocationDetailList = ipLocationDetailList.Where(
                                        ipLocDet => ipLocDet.ReceivedQty < 0 && ipLocDet.IpDetailId == thisIpDetail.Id).ToList();
                                }

                                if (thisIpLocationDetailList != null && thisIpLocationDetailList.Count > 0)
                                {
                                    foreach (IpLocationDetail thisIpLocationDetail in thisIpLocationDetailList)
                                    {
                                        if (thisIpLocationDetail.ReceivedQty > remainBaseQty)
                                        {
                                            //如果剩余冲销数量为0则不打开送货单
                                            if (remainBaseQty != 0)
                                            {
                                                thisIpDetail.IsClose = false;
                                                thisIpLocationDetail.IsClose = false;
                                            }
                                            thisIpDetail.ReceivedQty -= remainQty;
                                            remainQty = 0;

                                            thisIpLocationDetail.ReceivedQty -= remainBaseQty;
                                            remainBaseQty = 0;


                                        }
                                        else
                                        {
                                            decimal thisBackflushQty = thisIpLocationDetail.ReceivedQty / thisIpDetail.UnitQty; //转为订单单位
                                            remainQty -= thisBackflushQty;
                                            thisIpDetail.ReceivedQty -= thisBackflushQty;

                                            remainBaseQty -= thisIpLocationDetail.ReceivedQty;
                                            thisIpLocationDetail.ReceivedQty = 0;

                                            thisIpDetail.IsClose = false;
                                            thisIpLocationDetail.IsClose = false;
                                        }

                                        this.genericMgr.Update(thisIpLocationDetail);
                                        this.genericMgr.Update(thisIpDetail);
                                    }
                                }
                                else
                                {
                                    //throw new TechnicalException("差异送货单明细和送货单库存明细不匹配。");
                                }
                            }
                        }
                        #endregion
                    }

                    if (remainBaseQty != 0)
                    {
                        throw new TechnicalException("收货单的收货数没有回冲完。");
                    }
                }
                #endregion

                #region 更新订单明细
                if (receiptMaster.OrderType != CodeMaster.OrderType.ScheduleLine)
                {
                    #region 非计划协议
                    foreach (ReceiptDetail receiptDetail in receiptMaster.ReceiptDetails)
                    {
                        OrderDetail matchedOrderDetial = orderDetialList.Where(det => det.Id == receiptDetail.OrderDetailId.Value).Single();

                        if (receiptDetail.IpDetailType == CodeMaster.IpDetailType.Normal)
                        {
                            matchedOrderDetial.ReceivedQty -= receiptDetail.ReceivedQty;
                            if (matchedOrderDetial.ReceivedQty < 0)
                            {
                                throw new TechnicalException("订单收货数小于0。");
                            }
                        }
                        else
                        {
                            //差异收货冲销
                            #region 调整发货方库存
                            if (receiptMaster.PartyFrom == ipMaster.PartyTo)   //发货方等于收货方
                            {
                                //更新订单的发货数
                                matchedOrderDetial.ShippedQty += receiptDetail.ReceivedQty;
                            }
                            #endregion

                            #region 调整收货方库存
                            else
                            {
                                //更新订单的收货数
                                matchedOrderDetial.ReceivedQty -= receiptDetail.ReceivedQty;
                            }
                            #endregion
                        }
                        genericMgr.Update(matchedOrderDetial);
                    }
                    #endregion
                }
                else
                {
                    foreach (ReceiptDetail receiptDetail in receiptMaster.ReceiptDetails)
                    {
                        decimal receivedQty = receiptDetail.ReceivedQty;

                        if (receiptDetail.IpDetailType == CodeMaster.IpDetailType.Normal)
                        {
                            #region 调整收货方库存
                            IList<OrderDetail> scheduleOrderDetailList = this.genericMgr.FindEntityWithNativeSql<OrderDetail>("select * from ORD_OrderDet_8 where ExtNo = ? and ExtSeq like ? and ScheduleType = ? and RecQty > 0 order by EndDate desc",
                                              new object[] { receiptDetail.ExternalOrderNo, receiptDetail.ExternalSequence + "-%", CodeMaster.ScheduleType.Firm });

                            foreach (OrderDetail scheduleOrderDetail in scheduleOrderDetailList)
                            {
                                if (receivedQty > (scheduleOrderDetail.ShippedQty - scheduleOrderDetail.ReceivedQty))
                                {
                                    receivedQty -= scheduleOrderDetail.ShippedQty - scheduleOrderDetail.ReceivedQty;
                                    scheduleOrderDetail.ReceivedQty -= receiptDetail.ReceivedQty;
                                }
                                else
                                {
                                    scheduleOrderDetail.ReceivedQty -= receivedQty;
                                    receivedQty = 0;
                                }

                                genericMgr.Update(scheduleOrderDetail);
                            }
                            #endregion
                        }
                        else
                        {
                            //差异收货冲销
                            #region 调整发货方库存
                            if (receiptMaster.PartyFrom == ipMaster.PartyTo)   //发货方等于收货方
                            {
                                IList<OrderDetail> scheduleOrderDetailList = this.genericMgr.FindEntityWithNativeSql<OrderDetail>("select * from ORD_OrderDet_8 where ExtNo = ? and ExtSeq like ? and ScheduleType = ? and OrderQty > ShipQty order by EndDate",
                                              new object[] { receiptDetail.ExternalOrderNo, receiptDetail.ExternalSequence + "-%", CodeMaster.ScheduleType.Firm });

                                foreach (OrderDetail scheduleOrderDetail in scheduleOrderDetailList)
                                {
                                    if (receivedQty > (scheduleOrderDetail.OrderedQty - scheduleOrderDetail.ShippedQty))
                                    {
                                        receivedQty -= scheduleOrderDetail.OrderedQty - scheduleOrderDetail.ShippedQty;
                                        scheduleOrderDetail.ShippedQty = scheduleOrderDetail.OrderedQty;
                                    }
                                    else
                                    {
                                        scheduleOrderDetail.ShippedQty += receivedQty;
                                        receivedQty = 0;
                                    }

                                    genericMgr.Update(scheduleOrderDetail);
                                }
                            }
                            #endregion

                            #region 调整收货方库存
                            else
                            {
                                IList<OrderDetail> scheduleOrderDetailList = this.genericMgr.FindEntityWithNativeSql<OrderDetail>("select * from ORD_OrderDet_8 where ExtNo = ? and ExtSeq like ? and ScheduleType = ? and RecQty > 0 order by EndDate desc",
                                              new object[] { receiptDetail.ExternalOrderNo, receiptDetail.ExternalSequence + "-%", CodeMaster.ScheduleType.Firm });

                                foreach (OrderDetail scheduleOrderDetail in scheduleOrderDetailList)
                                {
                                    if (receivedQty > (scheduleOrderDetail.ShippedQty - scheduleOrderDetail.ReceivedQty))
                                    {
                                        receivedQty -= scheduleOrderDetail.ShippedQty - scheduleOrderDetail.ReceivedQty;
                                        scheduleOrderDetail.ReceivedQty -= receiptDetail.ReceivedQty;
                                    }
                                    else
                                    {
                                        scheduleOrderDetail.ReceivedQty -= receivedQty;
                                        receivedQty = 0;
                                    }

                                    genericMgr.Update(scheduleOrderDetail);
                                }
                            }
                            #endregion
                        }
                    }
                }
                #endregion

                #region 回滚送货单状态
                //1. 普通状态的送货明细没有收过货
                //2. 差异状态的送货明细全部关闭
                if (ipDetailList.Where(ipDet => ipDet.Type == CodeMaster.IpDetailType.Normal && ipDet.ReceivedQty != 0).Count() == 0
                    && ipDetailList.Where(ipDet => ipDet.Type == CodeMaster.IpDetailType.Gap && !ipDet.IsClose).Count() == 0)
                {
                    ipMaster.Status = CodeMaster.IpStatus.Submit;
                    this.genericMgr.Update(ipMaster);

                    #region 回滚排序单状态
                    if (!string.IsNullOrWhiteSpace(ipMaster.SequenceNo))
                    {
                        SequenceMaster sequenceMaster = this.genericMgr.FindById<SequenceMaster>(ipMaster.SequenceNo);
                        sequenceMaster.Status = CodeMaster.SequenceStatus.Ship;
                        this.genericMgr.Update(sequenceMaster);
                    }
                    #endregion
                }
                else if (ipMaster.Status != CodeMaster.IpStatus.InProcess)
                {
                    ipMaster.Status = CodeMaster.IpStatus.InProcess;
                    this.genericMgr.Update(ipMaster);
                }
                #endregion

                #region 退回排序单状态
                if (!string.IsNullOrEmpty(ipMaster.SequenceNo))
                {
                    #region 更新排序单头
                    SequenceMaster sequenceMaster = this.genericMgr.FindById<SequenceMaster>(ipMaster.SequenceNo);

                    sequenceMaster.Status = CodeMaster.SequenceStatus.Ship;
                    sequenceMaster.CloseDate = null;
                    sequenceMaster.CloseUserId = 0;
                    sequenceMaster.CloseUserName = null;

                    this.genericMgr.Update(sequenceMaster);
                    #endregion

                    foreach (SequenceDetail sequenceDetail in TryLoadSequenceDetails(sequenceMaster))
                    {
                        //对于调整计划关闭的排序明细也有可能打开了,会有Bug
                        if (orderDetialList.Select(i => i.Id).Contains(sequenceDetail.OrderDetailId))
                        {
                            sequenceDetail.IsClose = false;
                            this.genericMgr.Update(sequenceDetail);
                        }
                    }
                }
                #endregion
            }
            else
            {
                #region 更新订单明细
                foreach (ReceiptDetail receiptDetail in receiptMaster.ReceiptDetails)
                {
                    if (receiptDetail != null)
                    {
                        OrderDetail matchedOrderDetial = orderDetialList.Where(det => det.Id == receiptDetail.OrderDetailId.Value).Single();
                        matchedOrderDetial.ReceivedQty -= receiptDetail.ReceivedQty;
                        matchedOrderDetial.ScrapQty -= receiptDetail.ScrapQty;
                        if (matchedOrderDetial.OrderType != CodeMaster.OrderType.Production
                           && matchedOrderDetial.OrderType != CodeMaster.OrderType.SubContract)
                        {
                            matchedOrderDetial.ShippedQty -= receiptDetail.ReceivedQty;
                            if (matchedOrderDetial.ShippedQty < 0)
                            {
                                throw new TechnicalException("订单发货数小于0。");
                            }
                        }

                        if (matchedOrderDetial.ReceivedQty < 0)
                        {
                            throw new TechnicalException("订单收货数小于0。");
                        }

                        this.genericMgr.Update(matchedOrderDetial);
                    }
                }
                #endregion
            }
            #endregion

            #region 更新订单
            foreach (OrderMaster orderMaster in orderMasterList)
            {
                if (orderMaster.Status != CodeMaster.OrderStatus.InProcess)
                {
                    orderMaster.Status = CodeMaster.OrderStatus.InProcess;
                    orderMaster.CloseDate = null;
                    orderMaster.CloseUserId = null;
                    orderMaster.CloseUserName = null;
                    this.genericMgr.Update(orderMaster);
                }
            }
            #endregion

            #region 更新收货单
            receiptMaster.Status = CodeMaster.ReceiptStatus.Cancel;
            this.genericMgr.Update(receiptMaster);
            #endregion

            #region 冲销收货记录
            List<IpDetail> cancelIpDetailList = new List<IpDetail>();
            foreach (ReceiptDetail receiptDetail in receiptMaster.ReceiptDetails)
            {
                if (receiptDetail.ReceivedQty != 0)
                {
                    receiptDetail.CurrentPartyFrom = receiptMaster.PartyFrom;  //为了记录库存事务
                    receiptDetail.CurrentPartyFromName = receiptMaster.PartyFromName;
                    receiptDetail.CurrentPartyTo = receiptMaster.PartyTo;
                    receiptDetail.CurrentPartyToName = receiptMaster.PartyToName;
                    receiptDetail.CurrentExternalReceiptNo = receiptMaster.ExternalReceiptNo;
                    receiptDetail.CurrentIsReceiveScanHu = receiptMaster.IsReceiveScanHu || orderMasterList.Where(o => o.OrderStrategy == CodeMaster.FlowStrategy.KIT).Count() > 0;
                    receiptDetail.IsVoid = true;

                    foreach (ReceiptLocationDetail receiptLocationDetail in receiptDetail.ReceiptLocationDetails)
                    {
                        ReceiptDetailInput receiptDetailInput = new ReceiptDetailInput();
                        receiptDetailInput.HuId = receiptLocationDetail.HuId;
                        receiptDetailInput.ReceiveQty = -receiptLocationDetail.Qty / receiptDetail.UnitQty; //转为订单单位
                        receiptDetailInput.LotNo = receiptLocationDetail.LotNo;
                        receiptDetailInput.IsCreatePlanBill = receiptLocationDetail.IsCreatePlanBill;
                        receiptDetailInput.IsConsignment = receiptLocationDetail.IsConsignment;
                        receiptDetailInput.PlanBill = receiptLocationDetail.PlanBill;
                        receiptDetailInput.ActingBill = receiptLocationDetail.ActingBill;
                        receiptDetailInput.IsATP = receiptLocationDetail.IsATP;
                        receiptDetailInput.IsFreeze = receiptLocationDetail.IsFreeze;
                        receiptDetailInput.OccupyType = receiptLocationDetail.OccupyType;
                        receiptDetailInput.OccupyReferenceNo = receiptLocationDetail.OccupyReferenceNo;
                        receiptDetailInput.QualityType = receiptLocationDetail.QualityType;

                        receiptDetail.AddReceiptDetailInput(receiptDetailInput);
                    }

                    #region 更新库存、记库存事务
                    IList<InventoryTransaction> rctInventoryTransactionList = this.locationDetailMgr.InventoryIn(receiptDetail, effectiveDate);
                    #endregion

                    #region 订单直接收货创建发货明细对象
                    if (!string.IsNullOrWhiteSpace(receiptMaster.IpNo))
                    {
                        //nothing todo 
                    }
                    else if (receiptMaster.Type == CodeMaster.IpDetailType.Gap)
                    {
                        //nothing todo 
                    }
                    else if (receiptMaster.OrderType == CodeMaster.OrderType.Production)
                    {
                        //nothing todo 
                    }
                    else if (receiptMaster.OrderType == CodeMaster.OrderType.SubContract && receiptMaster.OrderSubType == CodeMaster.OrderSubType.Normal)
                    {
                        //nothing todo 
                    }
                    else
                    //if (string.IsNullOrWhiteSpace(receiptMaster.IpNo)
                    //    && receiptMaster.Type != CodeMaster.IpDetailType.Gap
                    //    && receiptMaster.OrderType != CodeMaster.OrderType.Production
                    //    && (receiptMaster.OrderType != CodeMaster.OrderType.SubContract))
                    {
                        IpDetail ipdetail = new IpDetail();
                        ipdetail.OrderNo = receiptDetail.OrderNo;
                        ipdetail.OrderType = receiptDetail.OrderType;
                        ipdetail.OrderSubType = receiptDetail.OrderSubType;
                        ipdetail.OrderDetailId = receiptDetail.OrderDetailId;
                        ipdetail.OrderDetailSequence = receiptDetail.OrderDetailSequence;
                        ipdetail.Item = receiptDetail.Item;
                        ipdetail.ItemDescription = receiptDetail.ItemDescription;
                        ipdetail.ReferenceItemCode = receiptDetail.ReferenceItemCode;
                        ipdetail.BaseUom = receiptDetail.BaseUom;
                        ipdetail.Uom = receiptDetail.Uom;
                        ipdetail.UnitCount = receiptDetail.UnitCount;
                        //ipdetail.UnitCountDescription = receiptDetail.UnitCountDescription;
                        //ipdetail.Container = receiptDetail.Container;
                        //ipdetail.ContainerDescription = receiptDetail.ContainerDescription;
                        ipdetail.QualityType = receiptDetail.QualityType;
                        //ipdetail.ManufactureParty = receiptDetail.ManufactureParty;
                        if ((ipdetail.OrderType == CodeMaster.OrderType.Procurement || ipdetail.OrderType == CodeMaster.OrderType.SubContract) && ipdetail.OrderSubType == CodeMaster.OrderSubType.Return)
                        {
                            //退货的订单需要入库y
                            ipdetail.Qty = receiptDetail.ReceivedQty;
                        }
                        else
                        {
                            ipdetail.Qty = -receiptDetail.ReceivedQty;
                        }
                        //ipdetail.ReceivedQty = 
                        ipdetail.UnitQty = receiptDetail.UnitQty;
                        ipdetail.LocationFrom = receiptDetail.LocationFrom;
                        ipdetail.LocationFromName = receiptDetail.LocationFromName;
                        ipdetail.LocationTo = receiptDetail.LocationTo;
                        ipdetail.LocationToName = receiptDetail.LocationToName;
                        ipdetail.IsInspect = false;
                        //ipdetail.BillTerm = receiptDetail.BillTerm;
                        ipdetail.IsVoid = true;

                        cancelIpDetailList.Add(ipdetail);

                        foreach (InventoryTransaction inventoryTransaction in rctInventoryTransactionList)
                        {
                            IpDetailInput ipDetailInput = new IpDetailInput();

                            ipDetailInput.HuId = inventoryTransaction.HuId;
                            if ((ipdetail.OrderType == CodeMaster.OrderType.Procurement || ipdetail.OrderType == CodeMaster.OrderType.SubContract) && ipdetail.OrderSubType == CodeMaster.OrderSubType.Return)
                            {
                                ipDetailInput.ShipQty = -inventoryTransaction.Qty / ipdetail.UnitQty;
                            }
                            else
                            {
                                ipDetailInput.ShipQty = inventoryTransaction.Qty / ipdetail.UnitQty;
                            }
                            ipDetailInput.LotNo = inventoryTransaction.LotNo;
                            ipDetailInput.IsCreatePlanBill = inventoryTransaction.IsCreatePlanBill;
                            if (inventoryTransaction.ActingBill.HasValue)
                            {
                                int planBill = this.genericMgr.FindAllWithNativeSql<Int32>("select PlanBill from BIL_ActBill where Id = ?", inventoryTransaction.ActingBill.Value).Single();
                                ipDetailInput.IsConsignment = true;
                                ipDetailInput.PlanBill = planBill;
                                ipDetailInput.ActingBill = null;
                            }
                            else
                            {
                                ipDetailInput.IsConsignment = inventoryTransaction.IsConsignment;
                                ipDetailInput.PlanBill = inventoryTransaction.PlanBill;
                                ipDetailInput.ActingBill = inventoryTransaction.ActingBill;
                            }
                            ipDetailInput.OccupyType = inventoryTransaction.OccupyType;
                            ipDetailInput.OccupyReferenceNo = inventoryTransaction.OccupyReferenceNo;

                            ipdetail.AddIpDetailInput(ipDetailInput);
                        }
                    }
                    #endregion
                }
            }
            #endregion

            #region 订单直接收货,冲销发货记录
            if (cancelIpDetailList != null && cancelIpDetailList.Count > 0)
            {
                foreach (IpDetail cancelIpDetail in cancelIpDetailList)
                {
                    cancelIpDetail.CurrentPartyFrom = receiptMaster.PartyFrom;  //为了记录库存事务
                    cancelIpDetail.CurrentPartyFromName = receiptMaster.PartyFromName;  //为了记录库存事务
                    cancelIpDetail.CurrentPartyTo = receiptMaster.PartyTo;      //为了记录库存事务
                    cancelIpDetail.CurrentPartyToName = receiptMaster.PartyToName;      //为了记录库存事务

                    this.locationDetailMgr.InventoryOut(cancelIpDetail);
                }
            }
            #endregion

            #region 生产单,委外冲销退回原材料
            if (receiptMaster.OrderType == CodeMaster.OrderType.Production ||
                receiptMaster.OrderType == CodeMaster.OrderType.SubContract)
            {
                var backflushInputList = ReceiptMaster2BackflushInputList(receiptMaster);
                locationDetailMgr.CancelBackflushProductMaterial(backflushInputList, effectiveDate);
                var orderBackflushDetailList = BackflushInputList2OrderBackflushDetailList(backflushInputList);
                DateTime dateTimeNow = DateTime.Now;
                User currentUser = SecurityContextHolder.Get();
                foreach (OrderBackflushDetail orderBackflushDetail in orderBackflushDetailList)
                {
                    orderBackflushDetail.EffectiveDate = effectiveDate;
                    orderBackflushDetail.CreateUserId = currentUser.Id;
                    orderBackflushDetail.CreateUserName = currentUser.FullName;
                    orderBackflushDetail.CreateDate = dateTimeNow;
                    orderBackflushDetail.IsVoid = true;
                    this.genericMgr.Create(orderBackflushDetail);
                }
            }
            #endregion
        }
Esempio n. 2
0
        public void CancelReceipt(ReceiptMaster receiptMaster, DateTime effectiveDate)
        {
            #region 判断收货单状态,只有Close才能冲销
            if (receiptMaster.Status == CodeMaster.ReceiptStatus.Cancel)
            {
                throw new BusinessException("收货单{0}已经冲销。", receiptMaster.ReceiptNo,
                    systemMgr.GetCodeDetailDescription(com.Sconit.CodeMaster.CodeMaster.ReceiptStatus, ((int)receiptMaster.Status).ToString()));
            }
            #endregion

            #region 加载收货单明细及收货单库存明细
            //  TryLoadReceiptDetails(receiptMaster);
            IList<ReceiptLocationDetail> receiptLocationDetailList = TryLoadReceiptLocationDetails(receiptMaster);
            #endregion

            #region 加载订单头和明细
            //GAP收货取消不用调整订单数据
            IList<OrderMaster> orderMasterList = null;
            IList<OrderDetail> orderDetialList = null;
            //if (receiptMaster.Type == CodeMaster.IpDetailType.Normal)
            //{
            #region 获取订单头
            orderMasterList = LoadOrderMasters(receiptMaster.ReceiptDetails.Select(det => det.OrderNo).Distinct().ToArray());
            #endregion

            #region 获取订单明细
            orderDetialList = LoadOrderDetails(receiptMaster.ReceiptDetails.Where(det => det.OrderDetailId.HasValue).Select(det => det.OrderDetailId.Value).Distinct().ToArray());
            #endregion
            //}
            #endregion

            //小数保留位数
            int decimalLength = int.Parse(systemMgr.GetEntityPreferenceValue(EntityPreference.CodeEnum.DecimalLength));

            #region 回滚送货单
            if (!string.IsNullOrWhiteSpace(receiptMaster.IpNo))
            {
                IpMaster ipMaster = this.genericMgr.FindById<IpMaster>(receiptMaster.IpNo);

                #region 查找送货单明细
                string selectIpDetailStatement = string.Empty;
                IList<object> selectIpDetailPram = new List<object>();
                foreach (int ipDetailId in receiptMaster.ReceiptDetails.Select(recDet => recDet.IpDetailId).Distinct())
                {
                    if (selectIpDetailStatement == string.Empty)
                    {
                        selectIpDetailStatement = "from IpDetail where Id in (?";
                    }
                    else
                    {
                        selectIpDetailStatement += ",?";
                    }

                    selectIpDetailPram.Add(ipDetailId);
                }
                selectIpDetailStatement += ")";
                IList<IpDetail> ipDetailList = this.genericMgr.FindAll<IpDetail>(selectIpDetailStatement, selectIpDetailPram.ToArray());
                #endregion

                #region 查找送货单库存明细
                IList<IpLocationDetail> ipLocationDetailList = LoadIpLocationDetails(ipDetailList.Select(ipDet => ipDet.Id).ToArray());
                #endregion

                #region 查找差异送货单明细
                IList<IpDetail> gapIpDetailList = this.genericMgr.FindAll<IpDetail>("from IpDetail where GapReceiptNo = ?", receiptMaster.ReceiptNo);
                #endregion

                #region 差异全部关闭
                if (gapIpDetailList != null && gapIpDetailList.Count > 0)
                {
                    #region 查找差异送货单库存明细
                    IList<IpLocationDetail> gapIpLocationDetailList = LoadIpLocationDetails(gapIpDetailList.Select(ipDet => ipDet.Id).ToArray());
                    #endregion

                    foreach (IpDetail gapIpDetail in gapIpDetailList)
                    {
                        if (gapIpDetail.ReceivedQty != 0)
                        {
                            throw new BusinessException("收货单{0}的收货差异已经调整,不能冲销。", receiptMaster.ReceiptNo);
                        }

                        gapIpDetail.IsClose = true;
                        this.genericMgr.Update(gapIpDetail);
                    }

                    foreach (IpLocationDetail gapIpLocationDetail in gapIpLocationDetailList)
                    {
                        gapIpLocationDetail.IsClose = true;
                        this.genericMgr.Update(gapIpLocationDetail);
                    }
                }
                #endregion

                #region 打开未收货的发货明细
                //只有ASN一次性收货才打开未收货的发货明细
                if (ipMaster.IsAsnUniqueReceive)
                {
                    //只有正常收货才能打开未收货的发货明细。
                    int isGapRec = receiptMaster.ReceiptDetails.Where(recDet => recDet.IpDetailType == CodeMaster.IpDetailType.Gap).Count();
                    if (isGapRec == 0)
                    {
                        IList<IpDetail> unReceivedIpDetailList = this.genericMgr.FindAll<IpDetail>("from IpDetail where IpNo = ? and Type = ?",
                            new object[] { ipMaster.IpNo, CodeMaster.IpDetailType.Normal });

                        if (unReceivedIpDetailList != null && unReceivedIpDetailList.Count > 0)
                        {
                            #region 查找差异送货单库存明细
                            IList<IpLocationDetail> unReceivedIpLocationDetailList = LoadIpLocationDetails(unReceivedIpDetailList.Select(ipDet => ipDet.Id).ToArray());
                            #endregion

                            foreach (IpDetail unReceivedIpDetail in unReceivedIpDetailList)
                            {
                                if (unReceivedIpDetail.IsClose)
                                {
                                    unReceivedIpDetail.IsClose = false;
                                    this.genericMgr.Update(unReceivedIpDetail);
                                }
                            }

                            foreach (IpLocationDetail unReceivedIpLocationDetail in unReceivedIpLocationDetailList)
                            {
                                if (unReceivedIpLocationDetail.IsClose)
                                {
                                    unReceivedIpLocationDetail.IsClose = false;
                                    this.genericMgr.Update(unReceivedIpLocationDetail);
                                }
                            }
                        }
                    }
                }
                #endregion

                #region 收货库存明细和送货库存明细匹配
                foreach (ReceiptLocationDetail receiptLocationDetail in receiptLocationDetailList)
                {
                    ReceiptDetail receiptDetail = receiptMaster.ReceiptDetails.Where(recDet => recDet.Id == receiptLocationDetail.ReceiptDetailId).Single();

                    decimal remainBaseQty = receiptLocationDetail.Qty;  //基本单位
                    decimal remainQty = receiptLocationDetail.Qty / receiptDetail.UnitQty; //转为订单单位

                    if (!string.IsNullOrWhiteSpace(receiptLocationDetail.HuId)
                        && ipLocationDetailList.Where(ipLocDet => !string.IsNullOrWhiteSpace(ipLocDet.HuId)).Count() > 0)
                    {
                        #region 条码和条码匹配
                        IpLocationDetail ipLocationDetail = ipLocationDetailList.Where(ipLocDet => ipLocDet.HuId == receiptLocationDetail.HuId).SingleOrDefault();
                        IpDetail ipDetail = ipDetailList.Where(ipDet => ipDet.Id == receiptDetail.IpDetailId).Single();

                        if (ipDetail.Id != receiptDetail.IpDetailId)
                        {
                            throw new TechnicalException("收货单明细和发货单明细ID不匹配。");
                        }

                        if (ipDetail.Type != receiptDetail.IpDetailType)
                        {
                            throw new TechnicalException("收货单明细和发货单明细类型不匹配。");
                        }

                        #region 扣减发货单的收货数
                        if (ipLocationDetail != null)
                        {
                            ipLocationDetail.ReceivedQty -= remainBaseQty;
                            ipLocationDetail.IsClose = false;
                        }
                        ipDetail.ReceivedQty -= remainQty; //转为订单单位
                        ipDetail.IsClose = false;

                        remainBaseQty = 0;
                        remainQty = 0;
                        #endregion

                        if (ipLocationDetail != null)
                        {
                            this.genericMgr.Update(ipLocationDetail);
                        }
                        this.genericMgr.Update(ipDetail);
                        #endregion
                    }
                    else
                    {
                        #region 按数量匹配
                        IList<IpDetail> thisIpDetailList = ipDetailList.Where(ipDet => ipDet.ReceivedQty != 0  //过滤掉已经扣完的明细
                                                                    && ipDet.Id == receiptDetail.IpDetailId).ToList();

                        if (thisIpDetailList != null && thisIpDetailList.Count > 0)
                        {
                            IList<IpLocationDetail> thisIpLocationDetailList = null;
                            foreach (IpDetail thisIpDetail in thisIpDetailList)
                            {
                                if (thisIpDetail.ReceivedQty > 0)
                                {
                                    thisIpLocationDetailList = ipLocationDetailList.Where(
                                        ipLocDet => ipLocDet.ReceivedQty > 0 && ipLocDet.IpDetailId == thisIpDetail.Id).ToList();
                                }
                                else if (thisIpDetail.ReceivedQty < 0)
                                {
                                    thisIpLocationDetailList = ipLocationDetailList.Where(
                                        ipLocDet => ipLocDet.ReceivedQty < 0 && ipLocDet.IpDetailId == thisIpDetail.Id).ToList();
                                }

                                if (thisIpLocationDetailList != null && thisIpLocationDetailList.Count > 0)
                                {
                                    foreach (IpLocationDetail thisIpLocationDetail in thisIpLocationDetailList)
                                    {
                                        if (thisIpLocationDetail.ReceivedQty > remainBaseQty)
                                        {
                                            thisIpDetail.ReceivedQty -= remainQty;
                                            remainQty = 0;

                                            thisIpLocationDetail.ReceivedQty -= remainBaseQty;
                                            remainBaseQty = 0;

                                            thisIpDetail.IsClose = false;
                                            thisIpLocationDetail.IsClose = false;
                                        }
                                        else
                                        {
                                            decimal thisBackflushQty = thisIpLocationDetail.ReceivedQty / thisIpDetail.UnitQty; //转为订单单位
                                            remainQty -= thisBackflushQty;
                                            thisIpDetail.ReceivedQty -= thisBackflushQty;

                                            remainBaseQty -= thisIpLocationDetail.ReceivedQty;
                                            thisIpLocationDetail.ReceivedQty = 0;

                                            thisIpDetail.IsClose = false;
                                            thisIpLocationDetail.IsClose = false;
                                        }

                                        this.genericMgr.Update(thisIpLocationDetail);
                                        this.genericMgr.Update(thisIpDetail);
                                    }
                                }
                                else
                                {
                                    //throw new TechnicalException("差异送货单明细和送货单库存明细不匹配。");
                                }
                            }
                        }
                        #endregion
                    }

                    if (remainBaseQty != 0)
                    {
                        throw new TechnicalException("收货单的收货数没有回冲完。");
                    }
                }
                #endregion

                #region 更新订单明细
                if (receiptMaster.OrderType != CodeMaster.OrderType.ScheduleLine)
                {
                    #region 非计划协议
                    foreach (ReceiptDetail receiptDetail in receiptMaster.ReceiptDetails)
                    {
                        OrderDetail matchedOrderDetial = orderDetialList.Where(det => det.Id == receiptDetail.OrderDetailId.Value).Single();

                        if (receiptDetail.IpDetailType == CodeMaster.IpDetailType.Normal)
                        {
                            matchedOrderDetial.ReceivedQty -= receiptDetail.ReceivedQty;
                            if (matchedOrderDetial.ReceivedQty < 0)
                            {
                                throw new TechnicalException("订单收货数小于0。");
                            }
                        }
                        else
                        {
                            //差异收货冲销
                            #region 调整发货方库存
                            if (receiptMaster.PartyFrom == ipMaster.PartyTo)   //发货方等于收货方
                            {
                                //更新订单的发货数
                                matchedOrderDetial.ShippedQty += receiptDetail.ReceivedQty;
                            }
                            #endregion

                            #region 调整收货方库存
                            else
                            {
                                //更新订单的收货数
                                matchedOrderDetial.ReceivedQty -= receiptDetail.ReceivedQty;
                            }
                            #endregion
                        }
                        genericMgr.Update(matchedOrderDetial);
                    }
                    #endregion
                }
                else
                {
                    foreach (ReceiptDetail receiptDetail in receiptMaster.ReceiptDetails)
                    {
                        decimal receivedQty = receiptDetail.ReceivedQty;

                        if (receiptDetail.IpDetailType == CodeMaster.IpDetailType.Normal)
                        {
                            #region 调整收货方库存
                            IList<OrderDetail> scheduleOrderDetailList = this.genericMgr.FindEntityWithNativeSql<OrderDetail>("select * from ORD_OrderDet_8 where ExtNo = ? and ExtSeq = ? and ScheduleType = ? and RecQty > 0 order by EndDate desc",
                                              new object[] { receiptDetail.ExternalOrderNo, receiptDetail.ExternalSequence, CodeMaster.ScheduleType.Firm });

                            foreach (OrderDetail scheduleOrderDetail in scheduleOrderDetailList)
                            {
                                if (receivedQty > (scheduleOrderDetail.ShippedQty - scheduleOrderDetail.ReceivedQty))
                                {
                                    receivedQty -= scheduleOrderDetail.ShippedQty - scheduleOrderDetail.ReceivedQty;
                                    scheduleOrderDetail.ReceivedQty -= receiptDetail.ReceivedQty;
                                }
                                else
                                {
                                    scheduleOrderDetail.ReceivedQty -= receivedQty;
                                    receivedQty = 0;
                                }

                                genericMgr.Update(scheduleOrderDetail);
                            }
                            #endregion
                        }
                        else
                        {
                            //差异收货冲销
                            #region 调整发货方库存
                            if (receiptMaster.PartyFrom == ipMaster.PartyTo)   //发货方等于收货方
                            {
                                IList<OrderDetail> scheduleOrderDetailList = this.genericMgr.FindEntityWithNativeSql<OrderDetail>("select * from ORD_OrderDet_8 where ExtNo = ? and ExtSeq = ? and ScheduleType = ? and OrderQty > ShipQty order by EndDate",
                                              new object[] { receiptDetail.ExternalOrderNo, receiptDetail.ExternalSequence, CodeMaster.ScheduleType.Firm });

                                foreach (OrderDetail scheduleOrderDetail in scheduleOrderDetailList)
                                {
                                    if (receivedQty > (scheduleOrderDetail.OrderedQty - scheduleOrderDetail.ShippedQty))
                                    {
                                        receivedQty -= scheduleOrderDetail.OrderedQty - scheduleOrderDetail.ShippedQty;
                                        scheduleOrderDetail.ShippedQty = scheduleOrderDetail.OrderedQty;
                                    }
                                    else
                                    {
                                        scheduleOrderDetail.ShippedQty += receivedQty;
                                        receivedQty = 0;
                                    }

                                    genericMgr.Update(scheduleOrderDetail);
                                }
                            }
                            #endregion

                            #region 调整收货方库存
                            else
                            {
                                IList<OrderDetail> scheduleOrderDetailList = this.genericMgr.FindEntityWithNativeSql<OrderDetail>("select * from ORD_OrderDet_8 where ExtNo = ? and ExtSeq = ? and ScheduleType = ? and RecQty > 0 order by EndDate desc",
                                              new object[] { receiptDetail.ExternalOrderNo, receiptDetail.ExternalSequence, CodeMaster.ScheduleType.Firm });

                                foreach (OrderDetail scheduleOrderDetail in scheduleOrderDetailList)
                                {
                                    if (receivedQty > (scheduleOrderDetail.ShippedQty - scheduleOrderDetail.ReceivedQty))
                                    {
                                        receivedQty -= scheduleOrderDetail.ShippedQty - scheduleOrderDetail.ReceivedQty;
                                        scheduleOrderDetail.ReceivedQty -= receiptDetail.ReceivedQty;
                                    }
                                    else
                                    {
                                        scheduleOrderDetail.ReceivedQty -= receivedQty;
                                        receivedQty = 0;
                                    }

                                    genericMgr.Update(scheduleOrderDetail);
                                }
                            }
                            #endregion
                        }
                    }
                }
                #endregion

                #region 回滚送货单状态
                //1. 普通状态的送货明细没有收过货
                //2. 差异状态的送货明细全部关闭
                if (ipDetailList.Where(ipDet => ipDet.Type == CodeMaster.IpDetailType.Normal && ipDet.ReceivedQty != 0).Count() == 0
                    && ipDetailList.Where(ipDet => ipDet.Type == CodeMaster.IpDetailType.Gap && !ipDet.IsClose).Count() == 0)
                {
                    ipMaster.Status = CodeMaster.IpStatus.Submit;
                    this.genericMgr.Update(ipMaster);
                }
                else if (ipMaster.Status != CodeMaster.IpStatus.InProcess)
                {
                    ipMaster.Status = CodeMaster.IpStatus.InProcess;
                    this.genericMgr.Update(ipMaster);
                }
                #endregion
            }
            else
            {
                #region 更新订单明细
                foreach (ReceiptDetail receiptDetail in receiptMaster.ReceiptDetails)
                {
                    if (receiptDetail != null)
                    {
                        OrderDetail matchedOrderDetial = orderDetialList.Where(det => det.Id == receiptDetail.OrderDetailId.Value).Single();
                        matchedOrderDetial.ReceivedQty -= receiptDetail.ReceivedQty;
                        matchedOrderDetial.ScrapQty -= receiptDetail.ScrapQty;
                        if (matchedOrderDetial.OrderType != CodeMaster.OrderType.Production
                            && matchedOrderDetial.OrderType != CodeMaster.OrderType.SubContract)
                        {
                            matchedOrderDetial.ShippedQty -= receiptDetail.ReceivedQty;
                            if (matchedOrderDetial.ShippedQty < 0)
                            {
                                throw new TechnicalException("订单发货数小于0。");
                            }
                        }

                        if (matchedOrderDetial.ReceivedQty < 0)
                        {
                            throw new TechnicalException("订单收货数小于0。");
                        }

                        this.genericMgr.Update(matchedOrderDetial);
                    }
                }
                #endregion
            }
            #endregion

            #region 更新订单
            foreach (OrderMaster orderMaster in orderMasterList)
            {
                if (orderMaster.Status != CodeMaster.OrderStatus.InProcess)
                {
                    orderMaster.Status = CodeMaster.OrderStatus.InProcess;
                    orderMaster.CloseDate = null;
                    orderMaster.CloseUserId = null;
                    orderMaster.CompleteUserName = null;
                    this.genericMgr.Update(orderMaster);
                }
            }
            #endregion

            #region 更新收货单
            receiptMaster.Status = CodeMaster.ReceiptStatus.Cancel;
            this.genericMgr.Update(receiptMaster);
            #endregion

            #region 冲销收货记录
            List<IpDetail> cancelIpDetailList = new List<IpDetail>();
            foreach (ReceiptDetail receiptDetail in receiptMaster.ReceiptDetails)
            {
                if (receiptDetail.ReceivedQty != 0)
                {
                    receiptDetail.CurrentPartyFrom = receiptMaster.PartyFrom;  //为了记录库存事务
                    receiptDetail.CurrentPartyFromName = receiptMaster.PartyFromName;
                    receiptDetail.CurrentPartyTo = receiptMaster.PartyTo;
                    receiptDetail.CurrentPartyToName = receiptMaster.PartyToName;
                    receiptDetail.CurrentExternalReceiptNo = receiptMaster.ExternalReceiptNo;
                    receiptDetail.CurrentIsReceiveScanHu = receiptMaster.IsReceiveScanHu;
                    receiptDetail.IsVoid = true;

                    foreach (ReceiptLocationDetail receiptLocationDetail in receiptDetail.ReceiptLocationDetails)
                    {
                        ReceiptDetailInput receiptDetailInput = new ReceiptDetailInput();
                        receiptDetailInput.HuId = receiptLocationDetail.HuId;
                        receiptDetailInput.ReceiveQty = -receiptLocationDetail.Qty / receiptDetail.UnitQty; //转为订单单位
                        receiptDetailInput.LotNo = receiptLocationDetail.LotNo;
                        receiptDetailInput.IsCreatePlanBill = receiptLocationDetail.IsCreatePlanBill;
                        receiptDetailInput.IsConsignment = receiptLocationDetail.IsConsignment;
                        receiptDetailInput.PlanBill = receiptLocationDetail.PlanBill;
                        receiptDetailInput.ActingBill = receiptLocationDetail.ActingBill;
                        receiptDetailInput.IsATP = receiptLocationDetail.IsATP;
                        receiptDetailInput.IsFreeze = receiptLocationDetail.IsFreeze;
                        receiptDetailInput.OccupyType = receiptLocationDetail.OccupyType;
                        receiptDetailInput.OccupyReferenceNo = receiptLocationDetail.OccupyReferenceNo;
                        receiptDetailInput.QualityType = receiptLocationDetail.QualityType;

                        receiptDetail.AddReceiptDetailInput(receiptDetailInput);
                    }

                    #region 更新库存、记库存事务
                    IList<InventoryTransaction> rctInventoryTransactionList = this.locationDetailMgr.InventoryIn(receiptDetail, effectiveDate);
                    #endregion

                    #region 订单直接收货创建发货明细对象
                    if (string.IsNullOrWhiteSpace(receiptMaster.IpNo)
                        && receiptMaster.Type != CodeMaster.IpDetailType.Gap
                        && receiptMaster.OrderType != CodeMaster.OrderType.Production
                        && receiptMaster.OrderType != CodeMaster.OrderType.SubContract)
                    {
                        IpDetail ipdetail = new IpDetail();
                        ipdetail.OrderNo = receiptDetail.OrderNo;
                        ipdetail.OrderType = receiptDetail.OrderType;
                        ipdetail.OrderSubType = receiptDetail.OrderSubType;
                        ipdetail.OrderDetailId = receiptDetail.OrderDetailId;
                        ipdetail.OrderDetailSequence = receiptDetail.OrderDetailSequence;
                        ipdetail.Item = receiptDetail.Item;
                        ipdetail.ItemDescription = receiptDetail.ItemDescription;
                        ipdetail.ReferenceItemCode = receiptDetail.ReferenceItemCode;
                        ipdetail.BaseUom = receiptDetail.BaseUom;
                        ipdetail.Uom = receiptDetail.Uom;
                        ipdetail.UnitCount = receiptDetail.UnitCount;
                        //ipdetail.UnitCountDescription = receiptDetail.UnitCountDescription;
                        //ipdetail.Container = receiptDetail.Container;
                        //ipdetail.ContainerDescription = receiptDetail.ContainerDescription;
                        ipdetail.QualityType = receiptDetail.QualityType;
                        //ipdetail.ManufactureParty = receiptDetail.ManufactureParty;
                        ipdetail.Qty = receiptDetail.ReceivedQty;
                        //ipdetail.ReceivedQty = 
                        ipdetail.UnitQty = receiptDetail.UnitQty;
                        ipdetail.LocationFrom = receiptDetail.LocationFrom;
                        ipdetail.LocationFromName = receiptDetail.LocationFromName;
                        ipdetail.LocationTo = receiptDetail.LocationTo;
                        ipdetail.LocationToName = receiptDetail.LocationToName;
                        ipdetail.IsInspect = false;
                        //ipdetail.BillTerm = receiptDetail.BillTerm;
                        ipdetail.IsVoid = true;

                        cancelIpDetailList.Add(ipdetail);

                        foreach (InventoryTransaction inventoryTransaction in rctInventoryTransactionList)
                        {
                            IpDetailInput ipDetailInput = new IpDetailInput();

                            ipDetailInput.HuId = inventoryTransaction.HuId;
                            ipDetailInput.ShipQty = inventoryTransaction.Qty / ipdetail.UnitQty;
                            ipDetailInput.LotNo = inventoryTransaction.LotNo;
                            ipDetailInput.IsCreatePlanBill = inventoryTransaction.IsCreatePlanBill;
                            if (inventoryTransaction.ActingBill.HasValue)
                            {
                                int planBill = this.genericMgr.FindAllWithNativeSql<Int32>("select PlanBill from BIL_ActBill where Id = ?", inventoryTransaction.ActingBill.Value).Single();
                                ipDetailInput.IsConsignment = true;
                                ipDetailInput.PlanBill = planBill;
                                ipDetailInput.ActingBill = null;
                            }
                            else
                            {
                                ipDetailInput.IsConsignment = inventoryTransaction.IsConsignment;
                                ipDetailInput.PlanBill = inventoryTransaction.PlanBill;
                                ipDetailInput.ActingBill = inventoryTransaction.ActingBill;
                            }
                            ipDetailInput.OccupyType = inventoryTransaction.OccupyType;
                            ipDetailInput.OccupyReferenceNo = inventoryTransaction.OccupyReferenceNo;

                            ipdetail.AddIpDetailInput(ipDetailInput);
                        }
                    }
                    #endregion
                }

                #region 来源区域是LOC、SQC 的冲销后更新 读取安吉文件中间表的冲销数
                if ((receiptMaster.PartyFrom == systemMgr.GetEntityPreferenceValue(Entity.SYS.EntityPreference.CodeEnum.WMSAnjiRegion) || receiptMaster.PartyFrom == "SQC")
                    && receiptMaster.OrderSubType == com.Sconit.CodeMaster.OrderSubType.Normal && !string.IsNullOrWhiteSpace(receiptDetail.ExternalOrderNo))
                {
                    var wmsDatfileById = this.genericMgr.FindAll<WMSDatFile>(" select w from WMSDatFile as w where w.WMSId=? ", receiptDetail.ExternalOrderNo, NHibernate.NHibernateUtil.String);
                    if (wmsDatfileById != null && wmsDatfileById.Count > 0)
                    {
                        wmsDatfileById.First().CancelQty += receiptDetail.ReceivedQty;
                        this.genericMgr.Update(wmsDatfileById.First());
                    }
                    //this.genericMgr.UpdateWithNativeQuery(" update FIS_WMSDatFile set CancelQty=CancelQty+? where WMSId=? ",new object[]{ receiptDetail.ReceivedQty, receiptDetail.ExternalOrderNo},new IType[]{NHibernate.NHibernateUtil.Decimal,NHibernate.NHibernateUtil.String});
                    #region 冲销的写入中间表传给安吉
                    this.genericMgr.Create(new CancelReceiptMasterDAT { WMSNo = receiptDetail.ExternalOrderNo, WMSSeq = receiptDetail.OrderDetailId, ReceivedQty = receiptDetail.ReceivedQty, CreateDate = System.DateTime.Now });
                    #endregion
                }
                #endregion
            }
            #endregion

            #region 订单直接收货,冲销发货记录
            if (cancelIpDetailList != null && cancelIpDetailList.Count > 0)
            {
                foreach (IpDetail cancelIpDetail in cancelIpDetailList)
                {
                    cancelIpDetail.CurrentPartyFrom = receiptMaster.PartyFrom;  //为了记录库存事务
                    cancelIpDetail.CurrentPartyFromName = receiptMaster.PartyFromName;  //为了记录库存事务
                    cancelIpDetail.CurrentPartyTo = receiptMaster.PartyTo;      //为了记录库存事务
                    cancelIpDetail.CurrentPartyToName = receiptMaster.PartyToName;      //为了记录库存事务

                    this.locationDetailMgr.InventoryOut(cancelIpDetail);
                }
            }
            #endregion

            #region 生产单冲销退回原材料
            //if (receiptMaster.OrderType == CodeMaster.OrderType.Production
            //    || receiptMaster.OrderType == CodeMaster.OrderType.SubContract)
            //{
            //    DateTime dateTimeNow = DateTime.Now;
            //    User currentUser = SecurityContextHolder.Get();
            //    IList<OrderBackflushDetail> orderBackflushDetailList = new List<OrderBackflushDetail>();
            //    FlowMaster productLine = this.genericMgr.FindById<FlowMaster>(receiptMaster.Flow);
            //    foreach (ReceiptDetail receiptDetail in receiptMaster.ReceiptDetails)
            //    {
            //        OrderMaster orderMaster = orderMasterList.Where(mstr => mstr.OrderNo == receiptDetail.OrderNo).Single();
            //        OrderDetail orderDetail = orderDetialList.Where(det => det.Id == receiptDetail.OrderDetailId).Single();
            //        IList<OrderBomDetail> orderBomDetailList = this.genericMgr.FindAll<OrderBomDetail>("from OrderBomDetail where OrderDetailId = ?", receiptDetail.OrderDetailId);

            //        IList<BackflushInput> backflushInputList = (from bom in orderBomDetailList
            //                                                    where bom.OrderedQty != 0
            //                                                    select new BackflushInput
            //                                                    {
            //                                                        OrderNo = orderMaster.OrderNo,
            //                                                        OrderType = orderMaster.Type,
            //                                                        OrderSubType = orderMaster.SubType,
            //                                                        OrderDetailSequence = orderDetail.Sequence,
            //                                                        OrderDetailId = orderDetail.Id,
            //                                                        OrderBomDetail = bom,
            //                                                        ReceiptNo = receiptMaster.ReceiptNo,
            //                                                        ReceiptDetailId = receiptDetail.Id,
            //                                                        ReceiptDetailSequence = receiptDetail.Sequence,
            //                                                        TraceCode = orderMaster.TraceCode,
            //                                                        Item = bom.Item,
            //                                                        ItemDescription = bom.ItemDescription,
            //                                                        ReferenceItemCode = bom.ReferenceItemCode,
            //                                                        Uom = bom.Uom,
            //                                                        BaseUom = bom.BaseUom,
            //                                                        Qty = bom.BomUnitQty * (receiptDetail.ReceivedQty + receiptDetail.ScrapQty),
            //                                                        UnitQty = bom.UnitQty,
            //                                                        Location = string.IsNullOrWhiteSpace(bom.Location) ? (string.IsNullOrWhiteSpace(orderDetail.LocationFrom) ? orderMaster.LocationFrom : orderDetail.LocationFrom) : bom.Location,
            //                                                        CurrentProductLine = productLine,
            //                                                        ProductLine = productLine.Code,
            //                                                        FGItem = orderDetail.Item,
            //                                                        FGQualityType = CodeMaster.QualityType.Qualified,
            //                                                        Operation = bom.Operation,
            //                                                        OpReference = bom.OpReference,

            //                                                    }).ToList();

            //        IList<InventoryTransaction> inventoryTransactionList = this.locationDetailMgr.CancelBackflushProductMaterial(backflushInputList, effectiveDate);

            //        foreach (BackflushInput backflushInput in backflushInputList)
            //        {
            //            ((List<OrderBackflushDetail>)orderBackflushDetailList).AddRange(from trans in backflushInput.InventoryTransactionList
            //                                                                            group trans by trans.PlanBill into g
            //                                                                            select new OrderBackflushDetail
            //                                                                            {
            //                                                                                OrderNo = backflushInput.OrderNo,
            //                                                                                OrderDetailId = backflushInput.OrderDetailId,
            //                                                                                OrderDetailSequence = backflushInput.OrderDetailSequence,
            //                                                                                OrderBomDetailId = backflushInput.OrderBomDetail.Id,
            //                                                                                OrderBomDetailSequence = backflushInput.OrderBomDetail.Sequence,
            //                                                                                ReceiptNo = backflushInput.ReceiptNo,
            //                                                                                ReceiptDetailId = backflushInput.ReceiptDetailId,
            //                                                                                ReceiptDetailSequence = backflushInput.ReceiptDetailSequence,
            //                                                                                Bom = backflushInput.OrderBomDetail.Bom,
            //                                                                                FGItem = backflushInput.FGItem,
            //                                                                                Item = backflushInput.Item,
            //                                                                                ItemDescription = backflushInput.ItemDescription,
            //                                                                                ReferenceItemCode = backflushInput.ReferenceItemCode,
            //                                                                                Uom = backflushInput.Uom,
            //                                                                                BaseUom = backflushInput.BaseUom,
            //                                                                                UnitQty = backflushInput.UnitQty,
            //                                                                                ManufactureParty = backflushInput.OrderBomDetail.ManufactureParty,
            //                                                                                TraceCode = backflushInput.TraceCode,
            //                                                                                //HuId = result.Key.HuId,
            //                                                                                //LotNo = result.Key.LotNo,
            //                                                                                Operation = backflushInput.Operation,
            //                                                                                OpReference = backflushInput.OpReference,
            //                                                                                BackflushedQty = backflushInput.FGQualityType == CodeMaster.QualityType.Qualified ? g.Sum(trans => trans.Qty) / backflushInput.UnitQty : 0,   //根据收货成品的质量状态记录至不同的回冲数量中
            //                                                                                BackflushedRejectQty = backflushInput.FGQualityType == CodeMaster.QualityType.Reject ? g.Sum(trans => trans.Qty) / backflushInput.UnitQty : 0,
            //                                                                                //BackflushedScrapQty = input.BackflushedQty,
            //                                                                                LocationFrom = backflushInput.OrderBomDetail.Location,
            //                                                                                ProductLine = backflushInput.ProductLine,
            //                                                                                ProductLineFacility = backflushInput.ProductLineFacility,
            //                                                                                PlanBill = g.Key,
            //                                                                                EffectiveDate = effectiveDate,
            //                                                                                CreateUserId = currentUser.Id,
            //                                                                                CreateUserName = currentUser.FullName,
            //                                                                                CreateDate = dateTimeNow,
            //                                                                                IsVoid = true,
            //                                                                                ReserveNo = backflushInput.OrderBomDetail.ReserveNo,
            //                                                                                ReserveLine = backflushInput.OrderBomDetail.ReserveLine,
            //                                                                                AUFNR = backflushInput.OrderBomDetail.AUFNR,
            //                                                                                ICHARG = backflushInput.OrderBomDetail.ICHARG,
            //                                                                                BWART = (int.Parse(backflushInput.OrderBomDetail.BWART) + 1).ToString(),
            //                                                                            });
            //        }
            //    }

            //    foreach (OrderBackflushDetail orderBackflushDetail in orderBackflushDetailList)
            //    {
            //        this.genericMgr.Create(orderBackflushDetail);
            //    }
            //}
            #endregion
        }