public CodebarResult GenerateBoleto(string bank, string date, string value)
        {
            CodebarResult boleto = new CodebarResult();

            string block01 = bank.PadLeft(3, '0') + randomValues.Next(50, 999999).ToString().PadLeft(6, '0');

            string block02 = randomValues.Next(1000, 99999).ToString().PadLeft(5, '0') + randomValues.Next(1000, 99999).ToString().PadLeft(5, '0');

            string block03 = randomValues.Next(1000, 99999).ToString().PadLeft(5, '0') + randomValues.Next(1000, 99999).ToString().PadLeft(5, '0');

            string block05 = String.Concat(Utils.GetFactorFromDate(date), Utils.GetFactorFromValueFormatted(value));

            string block04 = Barcode.Mod11.GetMod11Digit(String.Concat(block01.Insert(4, block05), block02, block03), 1);

            block01 = Barcode.Mod10.ConcactMod10Digit(block01).Insert(5, ".");
            block02 = Barcode.Mod10.ConcactMod10Digit(block02).Insert(5, ".");
            block03 = Barcode.Mod10.ConcactMod10Digit(block03).Insert(5, ".");

            boleto.LineFormatted = String.Format("{0} {1} {2} {3} {4}", block01, block02, block03, block04, block05);
            boleto.Line          = Utils.GetNumbersFromLine(boleto.LineFormatted);
            boleto.BarcodeLine   = Utils.ConvertToBarcode(boleto.LineFormatted);
            Image img = Image.FromStream(new MemoryStream(new C2of5i(boleto.BarcodeLine, 1, 50, boleto.BarcodeLine.Length).ToByte()));

            using (MemoryStream imgJpg = new MemoryStream())
            {
                img.Save(imgJpg, ImageFormat.Png);
                boleto.BarcodeBase64 = Convert.ToBase64String(imgJpg.ToArray());
            }
            return(boleto);
        }
        public CodebarResult GenerateConsumo(int segment, string code, string value)
        {
            int    identifierType = randomValues.Next(2) == 0 ? 6 : 8;
            string valueF         = Utils.GetFactorFromValueFormatted(value, 11);

            CodebarResult consumo = new CodebarResult();

            string block01 = String.Concat("8", segment, identifierType, valueF.Substring(0, 7));

            string block02 = String.Concat(valueF.Substring(7, 4), code, randomCode.Next(1000).ToString().PadLeft(3, '0'));

            string block03 = String.Concat(
                randomCode.Next(100000).ToString().PadLeft(5, '0'),
                randomCode.Next(1000).ToString().PadLeft(3, '0'),
                randomCode.Next(1000).ToString().PadRight(3, '0'));

            string block04 = String.Concat(
                randomCode.Next(100000).ToString().PadLeft(5, '0'),
                randomCode.Next(1000).ToString().PadLeft(3, '0'),
                randomCode.Next(1000).ToString().PadRight(3, '0'));

            string allBlocks = String.Concat(block01, block02, block03, block04);

            string digVMaster = identifierType == 6 ? Mod10.GetMod10Digit(allBlocks) : Mod11.GetMod11Digit(allBlocks);

            block01 = block01.Insert(3, digVMaster);


            if (identifierType == 6)
            {
                block01 = Mod10.ConcactMod10Digit(block01);
                block02 = Mod10.ConcactMod10Digit(block02);
                block03 = Mod10.ConcactMod10Digit(block03);
                block04 = Mod10.ConcactMod10Digit(block04);
            }
            else
            {
                block01 = Mod11.ConcactMod11Digit(block01);
                block02 = Mod11.ConcactMod11Digit(block02);
                block03 = Mod11.ConcactMod11Digit(block03);
                block04 = Mod11.ConcactMod11Digit(block04);
            }

            consumo.LineFormatted = String.Format("{0} {1} {2} {3}", block01, block02, block03, block04);
            consumo.Line          = Utils.GetNumbersFromLine(consumo.LineFormatted);
            consumo.BarcodeLine   = Utils.ConvertToBarcode(consumo.LineFormatted);
            Image img = Image.FromStream(new MemoryStream(new C2of5i(consumo.BarcodeLine, 1, 50, consumo.BarcodeLine.Length).ToByte()));

            using (MemoryStream imgJpg = new MemoryStream())
            {
                img.Save(imgJpg, ImageFormat.Png);
                consumo.BarcodeBase64 = Convert.ToBase64String(imgJpg.ToArray());
            }
            return(consumo);
        }
Exemple #3
0
        public JsonResult GenerateBoleto(string boleto)
        {
            if (!String.IsNullOrEmpty(boleto))
            {
                BoletoInfo boletoJson = JsonConvert.DeserializeObject <BoletoInfo>(boleto);

                BoletoWorker bWorker = new BoletoWorker();

                if (boletoJson.autoGenerate)
                {
                    boletoJson.bankCode = bWorker.RandomBank().ToString();

                    if (boletoJson.valueCheck)
                    {
                        boletoJson.value = bWorker.RandomValue(boletoJson.valueStart, boletoJson.valueEnd);
                    }

                    if (boletoJson.expirateCheck)
                    {
                        boletoJson.expirate = bWorker.RandomDate(boletoJson.expirateStart, boletoJson.expirateEnd).ToString("dd/MM/yyyy");
                    }

                    CodebarResult boletoResult = bWorker.GenerateBoleto(boletoJson.bankCode, boletoJson.expirate, boletoJson.value.ToString());

                    boletoJson.line          = boletoResult.Line;
                    boletoJson.lineFormatted = boletoResult.LineFormatted;
                    boletoJson.barcodeBase64 = boletoResult.BarcodeBase64;

                    return(Json(JsonConvert.SerializeObject(boletoJson)));
                }
                else
                {
                    boletoJson.value = boletoJson.value.Replace(".", ",");
                    Double v;
                    Double.TryParse(boletoJson.value, out v);
                    boletoJson.value = v.ToString("N");

                    CodebarResult boletoResult = bWorker.GenerateBoleto(boletoJson.bankCode, boletoJson.expirate, boletoJson.value.ToString());

                    boletoJson.line          = boletoResult.Line;
                    boletoJson.lineFormatted = boletoResult.LineFormatted;
                    boletoJson.barcodeBase64 = boletoResult.BarcodeBase64;

                    return(Json(JsonConvert.SerializeObject(boletoJson)));
                }
            }

            return(Json(new { boleto = boleto }));
        }
        public CodebarResult GenerateBarcodeByLine(string line)
        {
            CodebarResult barcode = new CodebarResult();

            barcode.BarcodeLine   = Utils.ConvertToBarcode(Utils.FormatLine(line));
            barcode.Line          = Utils.GetNumbersFromLine(line);
            barcode.LineFormatted = Utils.FormatLine(line);

            Image img = Image.FromStream(new MemoryStream(new C2of5i(barcode.BarcodeLine, 1, 50, barcode.BarcodeLine.Length).ToByte()));

            using (MemoryStream imgJpg = new MemoryStream())
            {
                img.Save(imgJpg, ImageFormat.Png);
                barcode.BarcodeBase64 = Convert.ToBase64String(imgJpg.ToArray());
            }
            return(barcode);
        }
        public JsonResult ByLine(string line)
        {
            if (!String.IsNullOrEmpty(line))
            {
                ByLineInfo lineJson = JsonConvert.DeserializeObject <ByLineInfo>(line);

                ExtraWorker   extra       = new ExtraWorker();
                CodebarResult extraResult = extra.GenerateBarcodeByLine(lineJson.fromLine);

                //+ 47 boleto
                if (extraResult.Line.Length == 47)
                {
                    int    bank  = Int32.Parse(extraResult.Line.Substring(0, 3));
                    string date  = extraResult.Line.Substring(33, 4);
                    string value = extraResult.Line.Substring(37, 10);

                    var _banks = BoletoWorker.GetBanks(Server.MapPath("~/")).Where(x => x.value == bank).FirstOrDefault();
                    lineJson.bank = _banks != null ? _banks.label : "";

                    lineJson.expirate = Utils.GetDateFromFactor(date).ToString("dd/MM/yyyy");
                    lineJson.value    = Utils.GetValueFormattedFromFactor(value);
                }
                //+ 48 conta de consumo
                else
                {
                    var    l       = extraResult.Line;
                    int    segment = Int32.Parse(l.Substring(1, 1));
                    string code    = l.Substring(16, 4);
                    string value   = string.Concat(l.Substring(4, 7), l.Substring(12, 4));

                    ConsumoWorker consumo = new ConsumoWorker(Server.MapPath("~/"));
                    var           _dealer = consumo.GetDealerships().Where(x => x.segment == segment && x.code == code).FirstOrDefault();
                    lineJson.dealership = _dealer != null ? _dealer.dealership : "";
                    lineJson.value      = Utils.GetValueFormattedFromFactor(value);
                }

                lineJson.line          = extraResult.Line;
                lineJson.lineFormatted = extraResult.LineFormatted;
                lineJson.barcodeBase64 = extraResult.BarcodeBase64;

                return(Json(JsonConvert.SerializeObject(lineJson)));
            }

            return(Json(null));
        }
Exemple #6
0
        public JsonResult GenerateConsumo(string consumo)
        {
            if (!String.IsNullOrEmpty(consumo))
            {
                ConsumoInfo consumoJson = JsonConvert.DeserializeObject <ConsumoInfo>(consumo);

                ConsumoWorker cWorker = new ConsumoWorker(Server.MapPath("~/"));

                if (consumoJson.autoGenerate)
                {
                    DealershipInfo dealership = cWorker.RandomDealership();
                    consumoJson.segment = dealership.segment;
                    consumoJson.code    = dealership.code;

                    consumoJson.value = cWorker.RandomValue(consumoJson.valueStart, consumoJson.valueEnd);

                    CodebarResult consumoResult = cWorker.GenerateConsumo(consumoJson.segment, consumoJson.code, consumoJson.value.ToString());

                    consumoJson.line          = consumoResult.Line;
                    consumoJson.lineFormatted = consumoResult.LineFormatted;
                    consumoJson.barcodeBase64 = consumoResult.BarcodeBase64;

                    return(Json(JsonConvert.SerializeObject(consumoJson)));
                }
                else
                {
                    consumoJson.value = consumoJson.value.Replace(".", ",");
                    Double v;
                    Double.TryParse(consumoJson.value, out v);
                    consumoJson.value = v.ToString("N");

                    CodebarResult consumoResult = cWorker.GenerateConsumo(consumoJson.segment, consumoJson.code, consumoJson.value.ToString());

                    consumoJson.line          = consumoResult.Line;
                    consumoJson.lineFormatted = consumoResult.LineFormatted;
                    consumoJson.barcodeBase64 = consumoResult.BarcodeBase64;

                    return(Json(JsonConvert.SerializeObject(consumoJson)));
                }
            }

            return(Json(new { consumo = consumo }));
        }