Пример #1
0
        public void TestLoadCpv()
        {
            var uowFactory = IoC.Instance.Resolve <IUnitOfWorkFactory>();

            var item = new ArtWrapper
            {
                MANDANTID      = 68,
                MandantCode    = "VGB",
                ARTCODE        = "VGB7320560620122",
                ARTNAME        = "7320560620122",
                ARTUPDATE      = 1,
                CUSTOMPARAMVAL = new List <ArtCpvWrapper>
                {
                    new ArtCpvWrapper
                    {
                        CUSTOMPARAMCODE_R_ARTCPV = "ARTProviderArtName",
                        CPVVALUE_ARTCPV          = "Привет!"
                    },

                    new ArtCpvWrapper
                    {
                        CUSTOMPARAMCODE_R_ARTCPV = "ARTPartnerHostRef",
                        CPVVALUE_ARTCPV          = @"ООО ""Рога и копыта"", Limited"
                    },

                    //new ArtCpvWrapper
                    //{
                    //     CUSTOMPARAMCODE_R_ARTCPV = "ARTPartner",
                    //     CPVVALUE_ARTCPV = @"ООО ""Рога и копыта"", Limited"
                    //}
                }
            };

            using (var uow = uowFactory.Create(false))
            {
                try
                {
                    uow.BeginChanges();
                    var result = ArtLoadHelper.LoadCpv(item: item, cpSource: null, cpTarget: null, uow: uow, log: _log);
                    uow.CommitChanges();
                    //uow.RollbackChanges();
                }
                catch
                {
                    uow.RollbackChanges();
                    throw;
                }
            }
        }
        private void KitPosLoadInternal(KitWrapper kit, string kitCode, List <ErrorWrapper> retMessage, IUnitOfWork uow)
        {
            var errCountOld = MessageHelper.GetErrorCount(retMessage);
            var errmessage  = string.Format("Ошибка при загрузке комплектующих (комплект «{0}»)", kit.ARTNAME);
            var artNames    = kit.KITPOS.Where(p => !string.IsNullOrEmpty(p.KITPOSARTNAME)).Select(i => i.KITPOSARTNAME.ToUpper()).Distinct().ToArray();

            if (artNames.Length == 0)
            {
                var ew = new ErrorWrapper
                {
                    ERRORCODE    = MessageHelper.ErrorCode.ToString(),
                    ERRORMESSAGE =
                        string.Format("Нет артикулов в элементах комплектующей '{0}'.", kit.ARTNAME)
                };
                retMessage.Add(ew);
                throw new IntegrationLogicalException(errmessage);
            }

            var typeart  = typeof(Art);
            var artsList =
                FilterHelper.GetArrayFilterIn(string.Format("UPPER({0})", SourceNameHelper.Instance.GetPropertySourceName(typeart, Art.ArtNamePropertyName)), artNames,
                                              string.Format(" and {0} = {1}",
                                                            SourceNameHelper.Instance.GetPropertySourceName(typeart, Art.MANDANTIDPropertyName),
                                                            kit.MANDANTID));

            var artcomps = new List <Art>();

            using (var artMgr = IoC.Instance.Resolve <IBaseManager <Art> >())
            {
                if (uow != null)
                {
                    artMgr.SetUnitOfWork(uow);
                }

                foreach (var artFilter in artsList)
                {
                    var artspart = artMgr.GetFiltered(artFilter).ToArray();
                    if (artspart.Length > 0)
                    {
                        artcomps.AddRange(artspart);
                    }
                }

                foreach (var pos in kit.KITPOS)
                {
                    var    existArt = artcomps.FirstOrDefault(i => pos.KITPOSARTNAME.EqIgnoreCase(i.ArtName));
                    string artCode;

                    if (existArt == null)
                    {
                        if (pos.KITPOSINSART == 1) // НЕ создается SKU!!! (потому как НЕТ ЕИ!) - кто придумал?
                        {
                            var art = new Art
                            {
                                ArtName       = pos.KITPOSARTNAME,
                                ArtCommercDay = 0,
                                ArtPickOrder  = 1,
                                //ARTABCD = kit.ARTABCD,
                                MANDANTID = kit.MANDANTID
                            };
                            var errmessages = ArtLoadHelper.FillArtAbcd(art, kit.ARTABCD, kit.ARTNAME, Log);
                            if (errmessages.Length > 0)
                            {
                                MessageHelper.AddMessage(errmessages, retMessage);
                            }

                            SetXmlIgnore(art, false);
                            artMgr.Insert(ref art);
                            artCode = art.ArtCode;
                            Log.DebugFormat("Создан артикул комплектующей «{0}»", pos.KITPOSARTNAME);
                        }
                        else
                        {
                            var ew = new ErrorWrapper
                            {
                                ERRORCODE    = MessageHelper.ErrorCode.ToString(),
                                ERRORMESSAGE =
                                    string.Format("Не существует артикул комплектующей «{0}»", pos.KITPOSARTNAME)
                            };
                            retMessage.Add(ew);
                            continue;
                        }
                    }
                    else
                    {
                        artCode = existArt.ArtCode;
                    }

                    var skuFilter = string.Format("{0} = '{1}' and {2} = 1",
                                                  SourceNameHelper.Instance.GetPropertySourceName(typeof(SKU), SKU.ArtCodePropertyName),
                                                  artCode,
                                                  SourceNameHelper.Instance.GetPropertySourceName(typeof(SKU), SKU.SKUClientPropertyName));
                    var skuMgr = IoC.Instance.Resolve <IBaseManager <SKU> >();
                    skuMgr.SetUnitOfWork(uow);
                    var skus = skuMgr.GetFiltered(skuFilter).ToArray();
                    if (!skus.Any())
                    {
                        var ew = new ErrorWrapper
                        {
                            ERRORCODE    = MessageHelper.ErrorCode.ToString(),
                            ERRORMESSAGE =
                                string.Format("Не существует SKU комплектующей «{0}»", pos.KITPOSARTNAME)
                        };
                        retMessage.Add(ew);
                        continue;
                    }

                    var kitPosMgr = IoC.Instance.Resolve <IBaseManager <KitPos> >();
                    kitPosMgr.SetUnitOfWork(uow);
                    var kitPosObj = new KitPos();
                    kitPosObj = MapTo(pos, kitPosObj);
                    kitPosObj.KitPosSKUIDR   = skus[0].SKUID;
                    kitPosObj.KitPosCodeR    = kitCode;
                    kitPosObj.KitPosPriority = 500;
                    SetXmlIgnore(kitPosObj, false);
                    kitPosMgr.Insert(ref kitPosObj);
                }

                if (MessageHelper.GetErrorCount(retMessage) > errCountOld)
                {
                    throw new IntegrationLogicalException(errmessage);
                }
            }
        }
        private void FillArtToKit(KitWrapper kit, IUnitOfWork uow)
        {
            var arts = ArtLoadHelper.FindArtByArtName(kit.MANDANTID, kit.ARTNAME, GetModeEnum.Full, uow);

            using (var artMgr = IoC.Instance.Resolve <IBaseManager <Art> >())
            {
                if (uow != null)
                {
                    artMgr.SetUnitOfWork(uow);
                }

                // если его нет, то создаем артикул и SKU
                if (!arts.Any())
                {
                    if (kit.KITINSART == 1)
                    {
                        var art = new Art
                        {
                            ArtName       = kit.ARTNAME,
                            ArtDesc       = kit.ARTDESC,
                            ArtCommercDay = 0,
                            ArtPickOrder  = 1,
                            ARTABCD       = kit.ARTABCD,
                            MANDANTID     = kit.MANDANTID
                        };
                        SetXmlIgnore(art, false);
                        artMgr.Insert(ref art);
                        kit.ARTCODE_R = art.ArtCode;
                        Log.DebugFormat("Создан артикул комплекта '{0}'", kit.ARTCODE_R);
                    }
                    else
                    {
                        throw new IntegrationLogicalException("Не существует артикул комплекта = '{0}'", kit.ARTNAME);
                    }

                    // есть ли тип ЕИ
                    var mtFilter = string.Format("{0} = '{1}'",
                                                 SourceNameHelper.Instance.GetPropertySourceName(typeof(MeasureType), MeasureType.MeasureTypeNamePropertyName),
                                                 kit.KITMEASURE);
                    var mtMgr = IoC.Instance.Resolve <IBaseManager <MeasureType> >();
                    mtMgr.SetUnitOfWork(uow);
                    var    measureTypes = mtMgr.GetFiltered(mtFilter).ToArray();
                    string measureTypeCode;
                    if (!measureTypes.Any())
                    {
                        var measureType = new MeasureType
                        {
                            MeasureTypeCode = "U" + kit.KITMEASURE,
                            MeasureTypeName = kit.KITMEASURE
                        };
                        SetXmlIgnore(measureType, false);
                        mtMgr.Insert(ref measureType);
                        measureTypeCode = measureType.MeasureTypeCode;
                        Log.DebugFormat("Создан тип ЕИ (ID = {0})", measureTypeCode);
                    }
                    else
                    {
                        measureTypeCode = measureTypes[0].MeasureTypeCode;
                        Log.DebugFormat("Найден тип ЕИ (ID = {0})", measureTypeCode);
                    }

                    // есть ли ЕИ
                    var measureFilter = string.Format("{0} = '{1}'",
                                                      SourceNameHelper.Instance.GetPropertySourceName(typeof(Measure), Measure.MeasureNamePropertyName),
                                                      kit.KITMEASURE);
                    var measureMgr = IoC.Instance.Resolve <IBaseManager <Measure> >();
                    measureMgr.SetUnitOfWork(uow);
                    var    measures = measureMgr.GetFiltered(measureFilter).ToArray();
                    string measureCode;
                    if (!measures.Any())
                    {
                        var measure = new Measure
                        {
                            MeasureTypeCodeR = measureTypeCode,
                            MeasureFactor    = 1,
                            MeasureName      = kit.KITMEASURE,
                            MeasureCode      = kit.KITMEASURE
                        };
                        SetXmlIgnore(measure, false);
                        measureMgr.Insert(ref measure);
                        measureCode = measure.MeasureCode;
                        Log.DebugFormat("Создана ЕИ (ID = {0})", measureCode);
                    }
                    else
                    {
                        measureCode = measures[0].MeasureCode;
                        Log.DebugFormat("Найдена ЕИ (ID = {0})", measureCode);
                    }

                    var skuMgr = IoC.Instance.Resolve <IBaseManager <SKU> >();
                    skuMgr.SetUnitOfWork(uow);
                    var sku = new SKU
                    {
                        ArtCode     = kit.ARTCODE_R,
                        MeasureCode = measureCode,
                        SKUCount    = (double)SerializationHelper.ConvertToTrueType(kit.KITCOUNT, typeof(double)),
                        SKUPrimary  = true,
                        SKUName     = kit.ARTNAME + "_" + kit.KITMEASURE + "_1"
                    };
                    SetXmlIgnore(sku, false);
                    skuMgr.Insert(ref sku);
                    Log.DebugFormat("Создана SKU шапки комплекта (ID = {0})", sku.SKUID);
                }
                else
                {
                    var firstOrDefault = arts.FirstOrDefault();
                    if (firstOrDefault != null)
                    {
                        kit.ARTCODE_R = firstOrDefault.ArtCode;
                    }
                }
            }
        }