Exemplo n.º 1
0
        public AbstractEntity Parse(Gtin.Gtin gtin)
        {
            AbstractEntity entity = null;
            string         code   = gtin.Code;

            if (gtin is Gtin13)
            {
                int mode = int.Parse(code[0].ToString());

                switch (mode)
                {
                case 2:
                    // Weight or price
                    int mode2 = int.Parse(code[1].ToString());

                    if (mode2 >= 0 && mode2 <= 2)
                    {
                        int   divider = price_dividers[mode2];
                        float price   = int.Parse(code.Substring(8, 4));

                        entity = new PriceProduct(code.Substring(2, 6), (divider == 0 ? price : price / divider));
                    }
                    else if (mode2 >= 3 && mode2 <= 5)
                    {
                        int   divider = weight_dividers[mode2];
                        float weight  = int.Parse(code.Substring(8, 4));

                        entity = new WeightProduct(code.Substring(2, 6), weight / divider);
                    }

                    break;

                case 7:
                    if (code.Substring(0, 4) == "7388")
                    {
                        // Publication
                        entity = new Publication(code.Substring(4, 4), float.Parse(code.Substring(8, 4)) / 10);
                    }

                    break;

                case 9:
                    // Coupon
                    entity = new Coupon(code.Substring(2, 6), float.Parse(code.Substring(8, 4)) / 10);
                    break;

                default:
                    entity = new Product(code.Substring(6, 6), code.Substring(0, 6));
                    break;
                }
            }
            else
            {
                entity = new Product(code.Substring(6, 1), code.Substring(0, 6));
            }

            return(entity);
        }
Exemplo n.º 2
0
        public Gtin.Gtin Generate(AbstractEntity entity)
        {
            Gtin.Gtin       gtin         = null;
            IFormatProvider numberFormat = CultureInfo.InvariantCulture;

            if (entity is Product)
            {
                Product product = (Product)entity;

                try
                {
                    gtin = new Gtin8();
                    gtin.SetPart(6, product.CompanyPrefix);
                    gtin.SetPart(1, product.Sku);
                }
                catch (ArgumentException)
                {
                    gtin = new Gtin13();
                    gtin.SetPart(9, product.CompanyPrefix);
                    gtin.SetPart(3, product.Sku);
                }
            }
            else if (entity is WeightProduct)
            {
                gtin = new Gtin13();
                WeightProduct product  = (WeightProduct)entity;
                decimal       weight   = (decimal)product.Weight;
                int           decimals = GetDecimalsCount(weight);
                int           modulator;
                string        weight_str = SubstringMax(weight.ToString(numberFormat).Replace(".", ""), 0, 4);

                if (decimals == 0)
                {
                    decimals    = 3;
                    weight_str += "000";
                }
                else
                {
                    decimals = Math.Max(1, Math.Min(3, decimals));
                }

                modulator = weight_modulators[decimals];

                gtin.SetPart(2, "2" + modulator);
                gtin.SetPart(6, product.Sku);
                gtin.SetPart(4, weight_str);
            }
            else if (entity is PriceProduct)
            {
                gtin = new Gtin13();
                PriceProduct product  = (PriceProduct)entity;
                decimal      price    = (decimal)product.Price;
                int          decimals = GetDecimalsCount(price);
                int          modulator;
                string       price_str = SubstringMax(price.ToString(numberFormat).Replace(".", ""), 0, 4);

                decimals  = Math.Max(0, Math.Min(2, decimals));
                modulator = price_modulators[decimals];

                gtin.SetPart(2, "2" + modulator);
                gtin.SetPart(6, product.Sku);
                gtin.SetPart(4, price_str);
            }
            else if (entity is Publication)
            {
                gtin = new Gtin13();
                Publication product   = (Publication)entity;
                decimal     price     = (decimal)product.Price;
                int         decimals  = GetDecimalsCount(price);
                string      price_str = price.ToString(numberFormat).Replace(".", "");

                if (decimals == 0)
                {
                    price_str = SubstringMax(price_str, 0, 3) + "0";
                }
                else
                {
                    price_str = SubstringMax(price_str, 0, 4);
                }

                gtin.SetPart(4, "7388");
                gtin.SetPart(4, product.Sku);
                gtin.SetPart(4, price_str);
            }
            else if (entity is Coupon)
            {
                gtin = new Gtin13();
                Coupon  coupon    = (Coupon)entity;
                decimal value     = (decimal)coupon.Value;
                int     decimals  = GetDecimalsCount(value);
                string  value_str = value.ToString(numberFormat).Replace(".", "");

                if (decimals == 0)
                {
                    value_str = SubstringMax(value_str, 0, 3) + "0";
                }
                else
                {
                    value_str = SubstringMax(value_str, 0, 4);
                }

                gtin.SetPart(2, "99");
                gtin.SetPart(6, coupon.Id);
                gtin.SetPart(4, value_str);
            }

            return(gtin);
        }