Exemplo n.º 1
0
        static void Parse()
        {
            try
            {
                Console.WriteLine("GS1 GTIN parser");
                Console.WriteLine("===============\n");

                Console.WriteLine("Enter GTIN (8 or 13 chars):");

                string    code = Console.ReadLine();
                Gtin.Gtin gtin;

                if (code.Length == 8)
                {
                    gtin = new Gtin8(code);
                }
                else if (code.Length == 13)
                {
                    gtin = new Gtin13(code);
                }
                else
                {
                    throw new ArgumentException("GTIN must be 8 or 13 characters");
                }

                if (!gtin.IsValid())
                {
                    throw new ArgumentException("GTIN checksum not valid");
                }

                AbstractEntity entity = EntityFactory.Get("Sweden", gtin);

                if (entity is Product)
                {
                    Product product = (Product)entity;
                    Console.WriteLine("Company Prefix: " + product.CompanyPrefix);
                    Console.WriteLine("SKU: " + product.Sku);
                }
                else if (entity is WeightProduct)
                {
                    WeightProduct product = (WeightProduct)entity;
                    Console.WriteLine("SKU: " + product.Sku);
                    Console.WriteLine("Weight: " + product.Weight);
                }
                else if (entity is PriceProduct)
                {
                    PriceProduct product = (PriceProduct)entity;
                    Console.WriteLine("SKU: " + product.Sku);
                    Console.WriteLine("Price: " + product.Price);
                }
                else if (entity is Coupon)
                {
                    Coupon coupon = (Coupon)entity;
                    Console.WriteLine("ID (Coupon): " + coupon.Id);
                    Console.WriteLine("Discount: " + coupon.Value);
                }
                else if (entity is Publication)
                {
                    Publication publication = (Publication)entity;
                    Console.WriteLine("SKU (Publication): " + publication.Sku);
                    Console.WriteLine("Price: " + publication.Price);
                }

                Console.ReadLine();
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (FormatException)
            {
                Console.WriteLine("Fel in-värde");
            }
        }
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);
        }