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
        }
        public ReceiptMaster TransferIp2Receipt(IpMaster ipMaster)
        {
            ReceiptMaster receiptMaster = Mapper.Map<IpMaster, ReceiptMaster>(ipMaster);
            receiptMaster.Type = com.Sconit.CodeMaster.IpDetailType.Normal;
            //记录外部订单号
            if (string.IsNullOrWhiteSpace(receiptMaster.ExternalReceiptNo))
            {
                receiptMaster.ExternalReceiptNo = ipMaster.ExternalIpNo;
            }
            //记录WMSNo
            string WMSNo = string.Empty;
            foreach (IpDetail ipDetail in ipMaster.IpDetails)
            {
                if (ipDetail.IpDetailInputs.Select(i => i.WMSRecNo).Distinct().Count() > 1)
                {
                    throw new TechnicalException("WMS收货单号不一致。");
                }

                if (string.IsNullOrWhiteSpace(WMSNo))
                {
                    WMSNo = ipDetail.IpDetailInputs.First().WMSRecNo;
                }
                else if (WMSNo != ipDetail.IpDetailInputs.First().WMSRecNo)
                {
                    throw new TechnicalException("WMS收货单号不一致。");
                }
            }
            receiptMaster.WMSNo = WMSNo;

            foreach (IpDetail ipDetail in ipMaster.IpDetails)
            {
                ReceiptDetail receiptDetail = Mapper.Map<IpDetail, ReceiptDetail>(ipDetail);

                if (ipDetail.Id != 0)
                {
                    receiptDetail.IpDetailId = ipDetail.Id;
                    receiptDetail.IpNo = ipDetail.IpNo;
                    receiptDetail.IpDetailSequence = ipDetail.Sequence;
                    receiptDetail.IpDetailType = CodeMaster.IpDetailType.Normal;
                    receiptDetail.ExternalOrderNo = receiptMaster.ExternalReceiptNo;
                }

                foreach (IpDetailInput ipDetailInput in ipDetail.IpDetailInputs)
                {
                    ReceiptDetailInput receiptDetailInput = new ReceiptDetailInput();
                    receiptDetailInput.ReceiveQty = ipDetailInput.ReceiveQty;
                    receiptDetailInput.QualityType = ipDetail.QualityType;
                    //receiptDetailInput.RejectQty = ipDetailInput.RejectQty;
                    receiptDetailInput.HuId = ipDetailInput.HuId;
                    receiptDetailInput.LotNo = ipDetailInput.LotNo;
                    receiptDetailInput.IsCreatePlanBill = ipDetailInput.IsCreatePlanBill;
                    receiptDetailInput.IsConsignment = ipDetailInput.IsConsignment;
                    receiptDetailInput.PlanBill = ipDetailInput.PlanBill;
                    receiptDetailInput.ActingBill = ipDetailInput.ActingBill;
                    receiptDetailInput.IsFreeze = ipDetailInput.IsFreeze;
                    receiptDetailInput.IsATP = ipDetailInput.IsATP;
                    receiptDetailInput.OccupyType = ipDetailInput.OccupyType;
                    receiptDetailInput.OccupyReferenceNo = ipDetailInput.OccupyReferenceNo;
                    receiptDetailInput.SequenceNo = ipMaster.SequenceNo;
                    receiptDetailInput.WMSRecSeq = ipDetailInput.WMSRecSeq;
                    receiptDetailInput.ReceivedIpLocationDetailList = ipDetailInput.ReceivedIpLocationDetailList;

                    receiptDetail.AddReceiptDetailInput(receiptDetailInput);
                }

                receiptMaster.AddReceiptDetail(receiptDetail);
            }
            return receiptMaster;
        }
        public ReceiptMaster TransferIpGap2Receipt(IpMaster ipMaster, CodeMaster.IpGapAdjustOption ipGapAdjustOption)
        {
            ReceiptMaster receiptMaster = Mapper.Map<IpMaster, ReceiptMaster>(ipMaster);
            receiptMaster.Type = CodeMaster.IpDetailType.Gap;

            if (ipGapAdjustOption == CodeMaster.IpGapAdjustOption.GI)
            {
                receiptMaster.PartyFrom = ipMaster.PartyTo;
                receiptMaster.PartyFromName = ipMaster.PartyToName;
                receiptMaster.PartyTo = ipMaster.PartyFrom;
                receiptMaster.PartyToName = ipMaster.PartyFromName;
                receiptMaster.ShipFrom = ipMaster.ShipTo;
                receiptMaster.ShipFromAddress = ipMaster.ShipToAddress;
                receiptMaster.ShipFromTel = ipMaster.ShipToTel;
                receiptMaster.ShipFromCell = ipMaster.ShipToCell;
                receiptMaster.ShipFromFax = ipMaster.ShipToFax;
                receiptMaster.ShipFromContact = ipMaster.ShipToContact;
                receiptMaster.ShipTo = ipMaster.ShipFrom;
                receiptMaster.ShipToAddress = ipMaster.ShipFromAddress;
                receiptMaster.ShipToTel = ipMaster.ShipFromTel;
                receiptMaster.ShipToCell = ipMaster.ShipFromCell;
                receiptMaster.ShipToFax = ipMaster.ShipFromFax;
                receiptMaster.ShipToContact = ipMaster.ShipFromContact;
                receiptMaster.Dock = string.Empty;
            }

            foreach (IpDetail ipDetail in ipMaster.IpDetails)
            {
                ReceiptDetail receiptDetail = Mapper.Map<IpDetail, ReceiptDetail>(ipDetail);

                if (ipGapAdjustOption == CodeMaster.IpGapAdjustOption.GI)
                {
                    receiptDetail.LocationFrom = ipDetail.LocationTo;
                    receiptDetail.LocationFromName = ipDetail.LocationToName;
                    receiptDetail.LocationTo = ipDetail.LocationFrom;
                    receiptDetail.LocationToName = ipDetail.LocationFromName;
                    receiptDetail.IsInspect = false;
                }

                receiptDetail.IpDetailId = ipDetail.Id;
                receiptDetail.IpNo = ipDetail.IpNo;
                receiptDetail.IpDetailSequence = ipDetail.Sequence;
                receiptDetail.IpDetailType = ipDetail.Type;
                receiptDetail.IpGapAdjustOption = ipGapAdjustOption;

                foreach (IpDetailInput ipDetailInput in ipDetail.IpDetailInputs)
                {
                    ReceiptDetailInput receiptDetailInput = new ReceiptDetailInput();
                    receiptDetailInput.ReceiveQty = ipDetailInput.ReceiveQty;
                    receiptDetailInput.QualityType = ipDetail.QualityType;
                    //receiptDetailInput.RejectQty = ipDetailInput.RejectQty;
                    receiptDetailInput.HuId = ipDetailInput.HuId;
                    receiptDetailInput.LotNo = ipDetailInput.LotNo;
                    receiptDetailInput.IsCreatePlanBill = ipDetailInput.IsCreatePlanBill;
                    receiptDetailInput.IsConsignment = ipDetailInput.IsConsignment;
                    receiptDetailInput.PlanBill = ipDetailInput.PlanBill;
                    receiptDetailInput.ActingBill = ipDetailInput.ActingBill;
                    receiptDetailInput.IsFreeze = ipDetailInput.IsFreeze;
                    receiptDetailInput.IsATP = ipDetailInput.IsATP;
                    receiptDetailInput.OccupyType = ipDetailInput.OccupyType;
                    receiptDetailInput.OccupyReferenceNo = ipDetailInput.OccupyReferenceNo;
                    receiptDetailInput.SequenceNo = ipMaster.SequenceNo;
                    receiptDetailInput.ReceivedIpLocationDetailList = ipDetailInput.ReceivedIpLocationDetailList;

                    receiptDetail.AddReceiptDetailInput(receiptDetailInput);
                }

                receiptMaster.AddReceiptDetail(receiptDetail);
            }

            return receiptMaster;
        }
Beispiel #4
0
        public PlanBill CreatePlanBill(ReceiptDetail receiptDetail, ReceiptDetailInput receiptDetailInput, DateTime effectiveDate)
        {
            PlanBill planBill = new PlanBill();
            planBill.OrderNo = receiptDetail.OrderNo;
            planBill.IpNo = receiptDetail.IpNo;
            //planBill.ExternalIpNo = receiptDetail.ExternalIpNo;
            planBill.ReceiptNo = receiptDetail.ReceiptNo;
            planBill.ExternalReceiptNo = receiptDetail.CurrentExternalReceiptNo;
            if (receiptDetail.OrderType == com.Sconit.CodeMaster.OrderType.Procurement
                || receiptDetail.OrderType == com.Sconit.CodeMaster.OrderType.SubContract
                || receiptDetail.OrderType == com.Sconit.CodeMaster.OrderType.CustomerGoods
                || receiptDetail.OrderType == com.Sconit.CodeMaster.OrderType.ScheduleLine)
            {
                planBill.Type = com.Sconit.CodeMaster.BillType.Procurement;
                if (receiptDetail.OrderSubType == CodeMaster.OrderSubType.Normal)
                {
                    planBill.LocationFrom = receiptDetail.LocationTo;
                    planBill.Party = receiptDetail.CurrentPartyFrom;
                    planBill.PartyName = receiptDetail.CurrentPartyFromName;
                }
                else
                {
                    planBill.LocationFrom = receiptDetail.LocationFrom;
                    planBill.Party = receiptDetail.CurrentPartyTo;
                    planBill.PartyName = receiptDetail.CurrentPartyToName;
                }
            }
            else if (receiptDetail.OrderType == com.Sconit.CodeMaster.OrderType.Distribution)
            {
                planBill.Type = com.Sconit.CodeMaster.BillType.Distribution;
                if (receiptDetail.OrderSubType == CodeMaster.OrderSubType.Normal)
                {
                    planBill.LocationFrom = receiptDetail.LocationFrom;
                    planBill.Party = receiptDetail.CurrentPartyTo;
                    planBill.PartyName = receiptDetail.CurrentPartyToName;
                }
                else
                {
                    planBill.LocationFrom = receiptDetail.LocationTo;
                    planBill.Party = receiptDetail.CurrentPartyFrom;
                    planBill.PartyName = receiptDetail.CurrentPartyFromName;
                }
            }
            planBill.Item = receiptDetail.Item;
            planBill.ItemDescription = receiptDetail.ItemDescription;

            planBill.Uom = receiptDetail.Uom;
            planBill.UnitCount = receiptDetail.UnitCount;
            planBill.BillTerm = receiptDetail.BillTerm;
            planBill.BillAddress = receiptDetail.BillAddress;
            //planBill.BillAddressDescription = receiptDetail.BillAddressDescription;
            planBill.PriceList = receiptDetail.PriceList;
            planBill.Currency = receiptDetail.Currency;
            planBill.UnitPrice = receiptDetail.UnitPrice.HasValue ? receiptDetail.UnitPrice.Value : 0;
            planBill.IsProvisionalEstimate = receiptDetail.UnitPrice.HasValue ? receiptDetail.IsProvisionalEstimate : false;
            planBill.Tax = receiptDetail.Tax;
            planBill.IsIncludeTax = receiptDetail.IsIncludeTax;
            if (receiptDetail.OrderSubType == com.Sconit.CodeMaster.OrderSubType.Normal)
            {
                //planBill.PlanQty = receiptDetailInput.ReceiveQty / receiptDetail.UnitQty;
                planBill.PlanQty = receiptDetailInput.ReceiveQty;
            }
            else
            {
                //planBill.PlanQty = -receiptDetailInput.ReceiveQty / receiptDetail.UnitQty;
                planBill.PlanQty = -receiptDetailInput.ReceiveQty;
            }
            planBill.PlanAmount = planBill.UnitPrice * planBill.PlanQty;
            planBill.UnitQty = receiptDetail.UnitQty;
            planBill.HuId = receiptDetailInput.HuId;
            planBill.EffectiveDate = effectiveDate;
            planBill.Flow = receiptDetail.Flow;
            planBill.ReferenceItemCode = receiptDetail.ReferenceItemCode;

            this.genericMgr.Create(planBill);

            this.RecordPlanBillTransaction(planBill, effectiveDate, receiptDetail.Id, false);

            return planBill;
        }
        //public ReceiptMaster TransferOrder2Receipt(IList<OrderMaster> orderMasterList)
        //{
        //    #region 发货单头
        //    ReceiptMaster receiptMaster = new ReceiptMaster();

        //    #region 发货单类型
        //    //receiptMaster.Type = ReceiptMaster.TypeEnum.Normal;
        //    #endregion

        //    #region 订单类型
        //    var orderType = from om in orderMasterList select om.Type;
        //    if (orderType.Distinct().Count() > 1)
        //    {
        //        throw new BusinessErrorException("订单类型不同不能合并收货。");
        //    }
        //    receiptMaster.OrderType = orderType.Single();
        //    #endregion

        //    #region 订单质量类型
        //    var qualityType = from om in orderMasterList select om.QualityType;
        //    if (qualityType.Distinct().Count() > 1)
        //    {
        //        throw new BusinessErrorException("订单质量状态不同不能合并收货。");
        //    }
        //    receiptMaster.QualityType = qualityType.Single();
        //    #endregion

        //    #region PartyFrom
        //    var partyFrom = from om in orderMasterList select om.PartyFrom;
        //    if (partyFrom.Distinct().Count() > 1)
        //    {
        //        throw new BusinessErrorException("来源组织不同不能合并收货。");
        //    }
        //    receiptMaster.PartyFrom = partyFrom.Single();
        //    #endregion

        //    #region PartyFromName
        //    receiptMaster.PartyFromName = (from om in orderMasterList select om.PartyFromName).First();
        //    #endregion

        //    #region PartyTo
        //    var partyTo = from om in orderMasterList select om.PartyTo;
        //    if (partyTo.Distinct().Count() > 1)
        //    {
        //        throw new BusinessErrorException("目的组织不同不能合并收货。");
        //    }
        //    receiptMaster.PartyTo = partyTo.Single();
        //    #endregion

        //    #region PartyToName
        //    receiptMaster.PartyToName = (from om in orderMasterList select om.PartyToName).First();
        //    #endregion

        //    #region ShipFrom
        //    var shipFrom = from om in orderMasterList select om.ShipFrom;
        //    if (shipFrom.Distinct().Count() > 1)
        //    {
        //        throw new BusinessErrorException("发货地址不同不能合并收货。");
        //    }
        //    receiptMaster.ShipFrom = shipFrom.Single();
        //    #endregion

        //    #region ShipFromAddr
        //    receiptMaster.ShipFromAddress = (from om in orderMasterList select om.ShipFromAddress).First();
        //    #endregion

        //    #region ShipFromTel
        //    receiptMaster.ShipFromTel = (from om in orderMasterList select om.ShipFromTel).First();
        //    #endregion

        //    #region ShipFromCell
        //    receiptMaster.ShipFromCell = (from om in orderMasterList select om.ShipFromCell).First();
        //    #endregion

        //    #region ShipFromFax
        //    receiptMaster.ShipFromFax = (from om in orderMasterList select om.ShipFromFax).First();
        //    #endregion

        //    #region ShipFromContact
        //    receiptMaster.ShipFromContact = (from om in orderMasterList select om.ShipFromContact).First();
        //    #endregion

        //    #region ShipTo
        //    var shipTo = from om in orderMasterList select om.ShipTo;
        //    if (shipTo.Distinct().Count() > 1)
        //    {
        //        throw new BusinessErrorException("收货地址不同不能合并收货。");
        //    }
        //    receiptMaster.ShipTo = shipTo.Single();
        //    #endregion

        //    #region ShipToAddr
        //    receiptMaster.ShipToAddress = (from om in orderMasterList select om.ShipToAddress).First();
        //    #endregion

        //    #region ShipToTel
        //    receiptMaster.ShipToTel = (from om in orderMasterList select om.ShipToTel).First();
        //    #endregion

        //    #region ShipToCell
        //    receiptMaster.ShipToCell = (from om in orderMasterList select om.ShipToCell).First();
        //    #endregion

        //    #region ShipToFax
        //    receiptMaster.ShipToFax = (from om in orderMasterList select om.ShipToFax).First();
        //    #endregion

        //    #region ShipToContact
        //    receiptMaster.ShipToContact = (from om in orderMasterList select om.ShipToContact).First();
        //    #endregion

        //    #region Dock
        //    var dock = from om in orderMasterList select om.Dock;
        //    if (dock.Distinct().Count() > 1)
        //    {
        //        throw new BusinessErrorException("道口不同不能合并收货。");
        //    }
        //    receiptMaster.Dock = dock.Single();
        //    #endregion

        //    #region IsPrintRec
        //    receiptMaster.IsPrintReceipt = orderMasterList.Where(om => om.IsPrintReceipt == true) != null;
        //    #endregion

        //    #region IsCheckPartyFromAuth
        //    receiptMaster.IsCheckPartyFromAuthority = orderMasterList.Where(om => om.IsCheckPartyFromAuthority == true) != null;
        //    #endregion

        //    #region IsCheckPartyToAuth
        //    receiptMaster.IsCheckPartyToAuthority = orderMasterList.Where(om => om.IsCheckPartyToAuthority == true) != null;
        //    #endregion

        //    #region RecTemplate
        //    var recTemplate = orderMasterList.Select(om => om.ReceiptTemplate).First();
        //    receiptMaster.ReceiptTemplate = recTemplate;
        //    #endregion
        //    #endregion

        //    #region 发货单明细
        //    foreach (OrderMaster orderMaster in orderMasterList)
        //    {
        //        if (orderMaster.OrderDetails != null && orderMaster.OrderDetails.Count > 0)
        //        {
        //            foreach (OrderDetail orderDetail in orderMaster.OrderDetails)
        //            {
        //                ReceiptDetail receiptDetail = new ReceiptDetail();
        //                Mapper.Map<OrderDetail, ReceiptDetail>(orderDetail, receiptDetail);
        //                //receiptDetail.EffectiveDate = orderMaster.EffectiveDate;
        //                foreach (OrderDetailInput orderDetailInput in orderDetail.OrderDetailInputs)
        //                {
        //                    ReceiptDetailInput receiptDetailInput = new ReceiptDetailInput();
        //                    receiptDetailInput.ReceiveQty = orderDetailInput.ReceiveQty;
        //                    //receiptDetailInput.RejectQty = orderDetailInput.RejectQty;
        //                    receiptDetailInput.HuId = orderDetailInput.HuId;
        //                    receiptDetailInput.LotNo = orderDetailInput.LotNo;

        //                    receiptDetail.AddReceiptDetailInput(receiptDetailInput);
        //                }

        //                receiptMaster.AddReceiptDetail(receiptDetail);
        //            }
        //        }
        //    }
        //    #endregion

        //    return receiptMaster;
        //}

        public ReceiptMaster TransferOrder2Receipt(OrderMaster orderMaster)
        {
            #region 发货单头
            ReceiptMaster receiptMaster = Mapper.Map<OrderMaster, ReceiptMaster>(orderMaster);
            receiptMaster.Type = com.Sconit.CodeMaster.IpDetailType.Normal;
            receiptMaster.CreateHuOption = orderMaster.CreateHuOption;
            #endregion

            #region 发货单明细
            if (orderMaster.OrderDetails != null && orderMaster.OrderDetails.Count > 0)
            {
                foreach (OrderDetail orderDetail in orderMaster.OrderDetails)
                {
                    ReceiptDetail receiptDetail = Mapper.Map<OrderDetail, ReceiptDetail>(orderDetail);
                    receiptDetail.Flow = orderMaster.Flow;

                    receiptDetail.IsInspect = orderMaster.IsInspect && orderDetail.IsInspect; //头和明细都选择报验才报验
                    //receiptDetail.EffectiveDate = orderMaster.EffectiveDate;
                    foreach (OrderDetailInput orderDetailInput in orderDetail.OrderDetailInputs)
                    {
                        ReceiptDetailInput receiptDetailInput = new ReceiptDetailInput();
                        receiptDetailInput.ReceiveQty = orderDetailInput.ReceiveQty;
                        receiptDetailInput.ScrapQty = orderDetailInput.ScrapQty;
                        //receiptDetailInput.RejectQty = orderDetailInput.RejectQty;
                        if (orderMaster.IsReceiveScanHu)
                        {
                            receiptDetailInput.HuId = orderDetailInput.HuId;
                            receiptDetailInput.LotNo = orderDetailInput.LotNo;
                        }
                        receiptDetailInput.OccupyType = orderDetailInput.OccupyType;
                        receiptDetailInput.OccupyReferenceNo = orderDetailInput.OccupyReferenceNo;
                        if (orderDetailInput.IpDetId != 0)
                        {
                            receiptDetail.IpNo = orderDetailInput.IpNo;
                            receiptDetail.IpDetailId = orderDetailInput.IpDetId;
                        }
                        receiptDetail.AddReceiptDetailInput(receiptDetailInput);
                    }

                    receiptMaster.AddReceiptDetail(receiptDetail);
                }
            }
            #endregion

            return receiptMaster;
        }
Beispiel #6
0
 public PlanBill CreatePlanBill(ReceiptDetail receiptDetail, ReceiptDetailInput receiptDetailInput)
 {
     return CreatePlanBill(receiptDetail, receiptDetailInput, DateTime.Now);
 }
Beispiel #7
0
        public PlanBill CreatePlanBill(ReceiptDetail receiptDetail, ReceiptDetailInput receiptDetailInput, DateTime effectiveDate)
        {
            PlanBill planBill = null;

            #region 寄售查找是否有PlanBill
            if (!(receiptDetail.BillTerm == CodeMaster.OrderBillTerm.ReceivingSettlement
                || receiptDetail.BillTerm == CodeMaster.OrderBillTerm.NA))
            {
                planBill = this.genericMgr.FindEntityWithNativeSql<PlanBill>("select * from BIL_PlanBill where Item = ? and BillAddr = ? and BillTerm not in(?,?)",
                    new object[] { receiptDetail.Item, receiptDetail.BillAddress, CodeMaster.OrderBillTerm.NA, CodeMaster.OrderBillTerm.ReceivingSettlement }).FirstOrDefault();

                if (planBill != null)
                {
                    planBill.CurrentVoidQty = 0;
                    planBill.CurrentCancelVoidQty = 0;
                    planBill.CurrentActingQty = 0;
                    planBill.CurrentLocation = null;
                    planBill.CurrentHuId = null;
                    planBill.CurrentActingBill = null;
                    planBill.CurrentBillTransaction = null;

                    if (planBill.BillTerm != receiptDetail.BillTerm)
                    {
                        //throw new BusinessException("物料{0}的结算方式({1})和寄售结算方式({2})不一致。", receiptDetail.Item,
                        //   systemMgr.GetCodeDetailDescription(CodeMaster.CodeMaster.BillTerm, (int)receiptDetail.BillTerm),
                        //   systemMgr.GetCodeDetailDescription(CodeMaster.CodeMaster.BillTerm, (int)planBill.BillTerm));
                        throw new BusinessException("物料{0}的结算方式({1})和寄售结算方式({2})不一致。", receiptDetail.Item,
                           systemMgr.GetCodeDetailDescription(CodeMaster.CodeMaster.OrderBillTerm, (int)receiptDetail.BillTerm),
                           systemMgr.GetCodeDetailDescription(CodeMaster.CodeMaster.OrderBillTerm, (int)planBill.BillTerm));
                    }
                }

                //if (planBill.Uom != receiptDetail.Uom)
                //{
                //    throw new BusinessException("物料{0}的收货单位{1}和寄售结算的单位{2}不一致。", receiptDetail.Item, receiptDetail.Uom, planBill.Uom);
                //}
            }
            else
            {
                planBill = this.genericMgr.FindEntityWithNativeSql<PlanBill>("select * from BIL_PlanBill where Item = ? and BillAddr = ? and RecNo = ?",
                  new object[] { receiptDetail.Item, receiptDetail.BillAddress, receiptDetail.ReceiptNo }).FirstOrDefault();

                if (planBill != null)
                {
                    planBill.CurrentVoidQty = 0;
                    planBill.CurrentCancelVoidQty = 0;
                    planBill.CurrentActingQty = 0;
                    planBill.CurrentLocation = null;
                    planBill.CurrentHuId = null;
                    planBill.CurrentActingBill = null;
                    planBill.CurrentBillTransaction = null;
                }
            }
            #endregion

            #region 没有PlanBill,创建PlanBill
            if (planBill == null)
            {
                planBill = new PlanBill();
                if (receiptDetail.BillTerm == CodeMaster.OrderBillTerm.ReceivingSettlement
                    || receiptDetail.BillTerm == CodeMaster.OrderBillTerm.NA)
                {
                    planBill.OrderNo = receiptDetail.OrderNo;
                    planBill.IpNo = receiptDetail.IpNo;
                    //planBill.ExternalIpNo = receiptDetail.ExternalIpNo;
                    planBill.ReceiptNo = receiptDetail.ReceiptNo;
                    planBill.ExternalReceiptNo = receiptDetail.Id.ToString();
                }
                if (receiptDetail.OrderType == com.Sconit.CodeMaster.OrderType.Procurement
                    || receiptDetail.OrderType == com.Sconit.CodeMaster.OrderType.SubContract
                    || receiptDetail.OrderType == com.Sconit.CodeMaster.OrderType.ScheduleLine)
                {
                    planBill.Type = com.Sconit.CodeMaster.BillType.Procurement;
                    if (receiptDetailInput.ReceiveQty > 0)
                    {
                        planBill.Party = receiptDetail.CurrentPartyFrom;
                        planBill.PartyName = receiptDetail.CurrentPartyFromName;
                    }
                    else
                    {
                        planBill.Party = receiptDetail.CurrentPartyTo;
                        planBill.PartyName = receiptDetail.CurrentPartyToName;
                    }
                }
                else if (receiptDetail.OrderType == com.Sconit.CodeMaster.OrderType.Distribution)
                {
                    planBill.Type = com.Sconit.CodeMaster.BillType.Distribution;
                    if (receiptDetailInput.ReceiveQty > 0)
                    {
                        planBill.Party = receiptDetail.CurrentPartyTo;
                        planBill.PartyName = receiptDetail.CurrentPartyToName;
                    }
                    else
                    {
                        planBill.Party = receiptDetail.CurrentPartyFrom;
                        planBill.PartyName = receiptDetail.CurrentPartyFromName;
                    }
                }
                planBill.Item = receiptDetail.Item;
                planBill.ItemDescription = receiptDetail.ItemDescription;

                planBill.Uom = receiptDetail.Uom;
                planBill.UnitCount = receiptDetail.UnitCount;
                planBill.BillTerm = receiptDetail.BillTerm == CodeMaster.OrderBillTerm.NA ? CodeMaster.OrderBillTerm.ReceivingSettlement : receiptDetail.BillTerm;
                planBill.BillAddress = receiptDetail.BillAddress;
                //planBill.BillAddressDescription = receiptDetail.BillAddressDescription;
                planBill.PriceList = receiptDetail.PriceList;
                planBill.Currency = receiptDetail.Currency;
                planBill.UnitPrice = receiptDetail.UnitPrice.HasValue ? receiptDetail.UnitPrice.Value : 0;
                planBill.IsProvisionalEstimate = receiptDetail.UnitPrice.HasValue ? receiptDetail.IsProvisionalEstimate : false;
                planBill.Tax = receiptDetail.Tax;
                planBill.IsIncludeTax = receiptDetail.IsIncludeTax;
                planBill.PlanAmount = 0;
                if (receiptDetail.OrderSubType == com.Sconit.CodeMaster.OrderSubType.Normal)
                {
                    //planBill.PlanQty = receiptDetailInput.ReceiveQty / receiptDetail.UnitQty;
                    planBill.PlanQty = receiptDetailInput.ReceiveQty;
                }
                else
                {
                    //planBill.PlanQty = -receiptDetailInput.ReceiveQty / receiptDetail.UnitQty;
                    planBill.PlanQty = -receiptDetailInput.ReceiveQty;
                }
                planBill.UnitQty = receiptDetail.UnitQty;
                //planBill.HuId = receiptDetailInput.HuId;
                //planBill.LocationFrom = receiptDetail.LocationFrom;
                planBill.EffectiveDate = effectiveDate;

                this.genericMgr.Create(planBill);
            }
            #endregion

            #region 有PlanBill,增加待结算数量
            else
            {

                if (receiptDetail.OrderSubType == com.Sconit.CodeMaster.OrderSubType.Normal)
                {
                    if (planBill.Uom != receiptDetail.Uom)
                    {
                        planBill.PlanQty += this.itemMgr.ConvertItemUomQty(receiptDetail.Item, receiptDetail.Uom, receiptDetailInput.ReceiveQty, planBill.Uom);
                    }
                    else
                    {
                        planBill.PlanQty += receiptDetailInput.ReceiveQty;
                    }
                }
                else
                {
                    if (planBill.Uom != receiptDetail.Uom)
                    {
                        planBill.PlanQty -= this.itemMgr.ConvertItemUomQty(receiptDetail.Item, receiptDetail.Uom, receiptDetailInput.ReceiveQty, planBill.Uom);
                    }
                    else
                    {
                        planBill.PlanQty -= receiptDetailInput.ReceiveQty;
                    }
                }

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

            #region 收货结算
            if (receiptDetail.BillTerm == CodeMaster.OrderBillTerm.NA
                || receiptDetail.BillTerm == CodeMaster.OrderBillTerm.ReceivingSettlement)
            {
                if (receiptDetail.OrderSubType == com.Sconit.CodeMaster.OrderSubType.Normal)
                {
                    if (planBill.Uom != receiptDetail.Uom)
                    {
                        planBill.CurrentActingQty = this.itemMgr.ConvertItemUomQty(receiptDetail.Item, receiptDetail.Uom, receiptDetailInput.ReceiveQty, planBill.Uom);
                    }
                    else
                    {
                        planBill.CurrentActingQty = receiptDetailInput.ReceiveQty;
                    }
                }
                else
                {
                    if (planBill.Uom != receiptDetail.Uom)
                    {
                        planBill.CurrentActingQty = -this.itemMgr.ConvertItemUomQty(receiptDetail.Item, receiptDetail.Uom, receiptDetailInput.ReceiveQty, planBill.Uom);
                    }
                    else
                    {
                        planBill.CurrentActingQty = -receiptDetailInput.ReceiveQty;
                    }
                }
                BillTransaction billTransaction = this.SettleBill(planBill, effectiveDate);
                planBill.CurrentActingBill = billTransaction.ActingBill;
                planBill.CurrentBillTransaction = billTransaction.Id;
            }
            #endregion

            return planBill;
        }
        private void RecordLocationTransaction(ReceiptDetail receiptDetail, ReceiptDetailInput receiptDetailInput, DateTime effectiveDate,
           com.Sconit.CodeMaster.TransactionType transType, IList<InventoryTransaction> inventoryTransactionList)
        {
            DateTime dateTimeNow = DateTime.Now;
            //根据PlanBill和ActingBill分组,为了不同供应商的库存事务分开
            var groupedInventoryTransactionList = from trans in inventoryTransactionList
                                                  group trans by new
                                                  {
                                                      IsConsignment = trans.IsConsignment,
                                                      PlanBill = trans.PlanBill,
                                                      ActingBill = trans.ActingBill
                                                  }
                                                      into result
                                                      select new
                                                      {
                                                          IsConsignment = result.Key.IsConsignment,
                                                          PlanBill = result.Key.PlanBill,
                                                          ActingBill = result.Key.ActingBill,
                                                          Qty = result.Sum(trans => trans.Qty),
                                                          PlanBillQty = result.Sum(trans => trans.PlanBillQty),
                                                          ActingBillQty = result.Sum(trans => trans.ActingBillQty),
                                                          InventoryTransactionList = result.ToList()
                                                      };

            foreach (var groupedInventoryTransaction in groupedInventoryTransactionList)
            {
                LocationTransaction locationTransaction = new LocationTransaction();

                locationTransaction.OrderNo = receiptDetail.OrderNo;
                locationTransaction.OrderType = receiptDetail.OrderType;
                locationTransaction.OrderSubType = receiptDetail.OrderSubType;
                locationTransaction.OrderDetailSequence = receiptDetail.OrderDetailSequence;
                locationTransaction.OrderDetailId = receiptDetail.OrderDetailId.Value;
                //locationTransaction.OrderBomDetId = 
                locationTransaction.IpNo = receiptDetail.IpNo;
                locationTransaction.IpDetailId = receiptDetail.IpDetailId.HasValue ? receiptDetail.IpDetailId.Value : 0;
                locationTransaction.IpDetailSequence = receiptDetail.IpDetailSequence;
                locationTransaction.ReceiptNo = receiptDetail.ReceiptNo;
                locationTransaction.ReceiptDetailId = receiptDetail.Id;
                locationTransaction.ReceiptDetailSequence = receiptDetail.Sequence;
                //locationTransaction.SequenceNo = 
                //locationTransaction.TraceCode =
                locationTransaction.SequenceNo = receiptDetailInput.SequenceNo;
                locationTransaction.Item = receiptDetail.Item;
                locationTransaction.Uom = receiptDetail.Uom;
                locationTransaction.BaseUom = receiptDetail.BaseUom;
                locationTransaction.Qty = groupedInventoryTransaction.Qty / receiptDetail.UnitQty;
                locationTransaction.UnitQty = receiptDetail.UnitQty;
                locationTransaction.IsConsignment = groupedInventoryTransaction.IsConsignment;
                if (groupedInventoryTransaction.IsConsignment && groupedInventoryTransaction.PlanBill.HasValue)
                {
                    locationTransaction.PlanBill = groupedInventoryTransaction.PlanBill.Value;
                }
                locationTransaction.PlanBillQty = groupedInventoryTransaction.PlanBillQty / receiptDetail.UnitQty;
                if (groupedInventoryTransaction.ActingBill.HasValue)
                {
                    locationTransaction.ActingBill = groupedInventoryTransaction.ActingBill.Value;
                }
                locationTransaction.ActingBillQty = groupedInventoryTransaction.ActingBillQty / receiptDetail.UnitQty;
                locationTransaction.QualityType = receiptDetail.QualityType;
                locationTransaction.HuId = receiptDetailInput.HuId;
                locationTransaction.LotNo = receiptDetailInput.LotNo;
                locationTransaction.TransactionType = transType;
                locationTransaction.IOType = CodeMaster.TransactionIOType.In;
                locationTransaction.PartyFrom = receiptDetail.CurrentPartyFrom;
                locationTransaction.PartyTo = receiptDetail.CurrentPartyTo;
                locationTransaction.LocationFrom = receiptDetail.LocationFrom;
                locationTransaction.LocationTo = receiptDetail.LocationTo;
                locationTransaction.LocationIOReason = string.Empty;
                locationTransaction.EffectiveDate = effectiveDate;
                locationTransaction.CreateUserId = SecurityContextHolder.Get().Id;
                locationTransaction.CreateDate = dateTimeNow;

                this.genericMgr.Create(locationTransaction);

                RecordLocationTransactionDetail(locationTransaction, groupedInventoryTransaction.InventoryTransactionList);
            }
        }
Beispiel #9
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
        }
 public void AddReceiptDetailInput(ReceiptDetailInput receiptDetailInput)
 {
     if (ReceiptDetailInputs == null)
     {
         ReceiptDetailInputs = new List<ReceiptDetailInput>();
     }
     ReceiptDetailInputs.Add(receiptDetailInput);
 }