Ejemplo n.º 1
0
        protected static FR_Base Execute(DbConnection Connection, DbTransaction Transaction, P_L3AS_SAVV_0040[] Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            #region UserCode
            var returnValue = new FR_Base();
            foreach (var vendorVariant in Parameter)
            {
                ORM_CMN_PRO_ASS_AssortmentVariant_VendorVariant vendorVariantToCreateUpdate = new ORM_CMN_PRO_ASS_AssortmentVariant_VendorVariant();
                var vendorProductExists = ORM_CMN_PRO_ASS_AssortmentVariant_VendorVariant.Query.Exists(Connection, Transaction, new ORM_CMN_PRO_ASS_AssortmentVariant_VendorVariant.Query()
                {
                    CMN_PRO_ASS_AssortmentVariant_VendorVariantID = vendorVariant.AssortmentVendorVariantID
                });
                if (vendorVariant.AssortmentVendorVariantID == Guid.Empty)
                {
                    vendorProductExists = false;
                }
                if (vendorProductExists)
                {
                    vendorVariantToCreateUpdate.Load(Connection, Transaction, vendorVariant.AssortmentVendorVariantID);
                }

                vendorVariantToCreateUpdate.CMN_PRO_Product_Variant_RefID       = vendorVariant.ProductVariantID;
                vendorVariantToCreateUpdate.CMN_PRO_ASS_AssortmentVariant_RefID = vendorVariant.AssortmentVariantID;
                vendorVariantToCreateUpdate.IsDeleted = vendorVariant.IsDeleted;
                vendorVariantToCreateUpdate.IsDefaultVendorVariant = vendorVariant.IsDefaultVendorVariant;
                vendorVariantToCreateUpdate.OrderSequence          = vendorVariant.OrderSequence;
                vendorVariantToCreateUpdate.Tenant_RefID           = securityTicket.TenantID;
                vendorVariantToCreateUpdate.Save(Connection, Transaction);
            }
            return(returnValue);

            #endregion UserCode
        }
        protected static FR_Guid Execute(DbConnection Connection, DbTransaction Transaction, P_L5PR_SPfA_1023 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            #region UserCode
            var returnValue       = new FR_Guid();
            var productParameters = Parameter.Products;
            returnValue = cls_Save_Product.Invoke(Connection, Transaction, productParameters, securityTicket);

            var productService = CatalogServiceFactory.GetProductService();
            foreach (var vendorProduct in Parameter.VendorProducts)
            {
                Guid vendorProductID = vendorProduct.VendorProductID;
                if (!string.IsNullOrEmpty(vendorProduct.ProductITL))
                {
                    ProductDetailsRequest productDetailsRequest = new ProductDetailsRequest();

                    productDetailsRequest.CatalogCode = vendorProduct.CatalogITL;
                    productDetailsRequest.ProductIDs  = new List <string>();
                    productDetailsRequest.ProductIDs.Add(vendorProduct.ProductITL);
                    List <Product> products = new List <Product>();
                    products.AddRange(productService.GetProductDetails <Product>(productDetailsRequest).ProductDetails);
                    foreach (var product in products)
                    {
                        P_L5PR_IPfC_1648 parameter = new P_L5PR_IPfC_1648();
                        parameter.Product             = new ProductsForImport();
                        parameter.Product.Code        = product.Code;
                        parameter.Product.Custom      = product.Custom;
                        parameter.Product.Description = product.Description;
                        parameter.Product.LongName    = product.LongName;
                        parameter.Product.Name        = product.Name;
                        parameter.Product.ProductITL  = product.ProductITL;
                        parameter.Product.CatalogITL  = vendorProduct.CatalogITL;
                        parameter.Product.Variants    = new List <Variant>();
                        foreach (var variant in product.Variants)
                        {
                            parameter.Product.Variants.Add(variant);
                        }
                        parameter.Product.Vat = product.Vat;

                        vendorProductID            = cls_Import_Product_from_Catalog.Invoke(Connection, Transaction, parameter, securityTicket).Result;
                        vendorProduct.ProductRefID = vendorProductID;
                    }
                }

                ORM_CMN_PRO_ASS_AssortmentProduct_VendorProduct vendorProductToSave = new ORM_CMN_PRO_ASS_AssortmentProduct_VendorProduct();
                var vendorProductExists = ORM_CMN_PRO_ASS_AssortmentProduct_VendorProduct.Query.Exists(Connection, Transaction, new ORM_CMN_PRO_ASS_AssortmentProduct_VendorProduct.Query()
                {
                    CMN_PRO_ASS_AssortmentProduct_VendorProductID = vendorProduct.VendorProductID
                });
                if (vendorProductExists)
                {
                    vendorProductToSave.Load(Connection, Transaction, vendorProduct.VendorProductID);
                }
                vendorProductToSave.CMN_PRO_ASS_AssortmentProduct_RefID = vendorProduct.AssortmentProductID;
                vendorProductToSave.CMN_PRO_Product_RefID = vendorProduct.ProductRefID;
                vendorProductToSave.IsDeleted             = vendorProduct.IsDeleted;
                vendorProductToSave.Tenant_RefID          = securityTicket.TenantID;

                vendorProductToSave.Save(Connection, Transaction);

                if (vendorProduct.IsDeleted == true)
                {
                    P_L3VA_GAVVfAP_1926 parameterAssortmentVendorVariants = new P_L3VA_GAVVfAP_1926();
                    parameterAssortmentVendorVariants.ProductID = Parameter.Products.ProductID;
                    var assortmentVendorProductVariants = cls_Get_Assortment_Vendor_Variants_for_AssortmentProductID.Invoke(Connection, Transaction, parameterAssortmentVendorVariants, securityTicket).Result;

                    P_L3VA_GAVfP_1300 parameterVendorVariantToDelete = new P_L3VA_GAVfP_1300();
                    parameterVendorVariantToDelete.ProductID = vendorProductToSave.CMN_PRO_Product_RefID;

                    var vendorProductVariantsToDelete = cls_Get_All_Variants_for_Product.Invoke(Connection, Transaction, parameterVendorVariantToDelete, securityTicket).Result;

                    if (vendorProductVariantsToDelete != null)
                    {
                        foreach (var vendorProductVariantToDelete in vendorProductVariantsToDelete)
                        {
                            if (assortmentVendorProductVariants.ToList().Count(x => x.CMN_PRO_Product_Variant_RefID == vendorProductVariantToDelete.CMN_PRO_Product_VariantID) > 0)
                            {
                                var assortmentProductVariantsIDs = assortmentVendorProductVariants.Where(x => x.CMN_PRO_Product_Variant_RefID == vendorProductVariantToDelete.CMN_PRO_Product_VariantID).Select(x => x.CMN_PRO_ASS_AssortmentVariant_VendorVariantID).ToList();

                                foreach (var assortmentProductVariantsID in assortmentProductVariantsIDs)
                                {
                                    ORM_CMN_PRO_ASS_AssortmentVariant_VendorVariant variantToDelete = new ORM_CMN_PRO_ASS_AssortmentVariant_VendorVariant();
                                    variantToDelete.Load(Connection, Transaction, assortmentProductVariantsID);
                                    variantToDelete.IsDeleted = true;
                                    variantToDelete.IsDefaultVendorVariant = false;
                                    variantToDelete.Save(Connection, Transaction);
                                }
                                assortmentVendorProductVariants.ToList().RemoveAll(x => x.CMN_PRO_Product_Variant_RefID == vendorProductVariantToDelete.CMN_PRO_Product_VariantID);

                                var assortmentVariantIDs = assortmentVendorProductVariants.Select(x => x.CMN_PRO_ASS_AssortmentVariantID).ToList();
                                foreach (var assortmentVariantID in assortmentVariantIDs.Distinct().ToList())
                                {
                                    var vendorVariantsToUpdate = ORM_CMN_PRO_ASS_AssortmentVariant_VendorVariant.Query.Search(Connection, Transaction, new ORM_CMN_PRO_ASS_AssortmentVariant_VendorVariant.Query()
                                    {
                                        CMN_PRO_ASS_AssortmentVariant_RefID = assortmentVariantID,
                                        IsDeleted    = false,
                                        Tenant_RefID = securityTicket.TenantID
                                    }).ToList();
                                    var orderSequence = 1;
                                    foreach (var variantToUpdate in vendorVariantsToUpdate)
                                    {
                                        variantToUpdate.OrderSequence = orderSequence;
                                        if (orderSequence == 0 && vendorVariantsToUpdate.Count(x => x.IsDefaultVendorVariant) == 0)
                                        {
                                            variantToUpdate.IsDefaultVendorVariant = true;
                                        }
                                        variantToUpdate.Save(Connection, Transaction);
                                        orderSequence++;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(returnValue);

            #endregion UserCode
        }