def test_split_to_chunks(data, expected): def chars(string): return [i for i in to_bytes(string)] data = to_bytes(data) expected = [(chars(text), fn) for text, fn in expected] assert list(_split_to_chunks(data)) == expected
def _get_pdf417_code(self): for invoice_id in self: res = [] if invoice_id.name and invoice_id.journal_id.is_cpe: res.append(invoice_id.company_id.partner_id.doc_number) res.append(invoice_id.journal_id.pe_invoice_code or '') res.append(invoice_id.name.split("-")[0] or '') res.append(invoice_id.name.split("-")[1] or '') res.append(str(invoice_id.amount_tax)) res.append(str(invoice_id.amount_total)) res.append(str(invoice_id.invoice_date)) res.append(invoice_id.partner_id.doc_type or "-") res.append(invoice_id.partner_id.doc_number or "-") res.append(invoice_id.pe_digest or "") res.append(invoice_id.pe_signature or "") res.append("") pdf417_string = '|'.join(res) data_bytes = compact_bytes(to_bytes(pdf417_string, 'utf-8')) code_words = encode_high(data_bytes, 10, 5) rows = list(chunks(code_words, 10)) codes = list(encode_rows(rows, 10, 5)) image = render_image(codes, scale=2, ratio=2, padding=7) # tmpf = tempfile.TemporaryFile(mode="r+") tmpf = BytesIO() image.save(tmpf, 'png') # tmpf.seek(0) invoice_id.sunat_pdf417_code = encodestring(tmpf.getvalue())
def _get_pdf417_code(self): for picking_id in self: res = [] if picking_id.pe_guide_number and picking_id.pe_is_eguide: res.append(picking_id.company_id.partner_id.doc_number) res.append('09') res.append(picking_id.pe_guide_number.split("-")[0] or '') res.append(picking_id.pe_guide_number.split("-")[1] or '') # res.append(str(picking_id.amount_tax)) # res.append(str(picking_id.amount_total)) res.append(str(picking_id.pe_date_issue)) res.append(picking_id.partner_id.doc_type or "-") res.append(picking_id.partner_id.doc_number or "-") res.append(picking_id.pe_digest or "") res.append(picking_id.pe_signature or "") res.append("") pdf417_string = '|'.join(res) data_bytes = compact_bytes(to_bytes(pdf417_string, 'utf-8')) code_words = encode_high(data_bytes, 10, 5) rows = list(chunks(code_words, 10)) codes = list(encode_rows(rows, 10, 5)) image = render_image(codes, scale=2, ratio=2, padding=7) tmpf = BytesIO() image.save(tmpf, 'png') # tmpf.seek(0) picking_id.sunat_pdf417_code = encodestring(tmpf.getvalue())
def test_optimizations(data, expected): def chars(string): return [i for i in to_bytes(string)] data = to_bytes(data) expected = [Chunk(chars(text), fn) for text, fn in expected] actual = _split_to_chunks(data) actual = optimizations.replace_short_numeric_chunks(actual) actual = optimizations.merge_chunks_with_same_compact_fn(actual) assert list(actual) == expected
def test_encode_high(): # High level encoding expected = [ 142, 227, 637, 601, 902, 130, 900, 865, 479, 227, 328, 765, 902, 1, 624, 142, 113, 522, 200, 900, 865, 479, 267, 630, 416, 868, 237, 1, 613, 130, 865, 479, 567, 21, 550, 26, 64, 559, 26, 902, 113, 900, 865, 479, 902, 122, 200, 900, 805, 810, 197, 121, 865, 479, 57, 246, 808, 845, 818, 547, 808, 858, 824, 169, 660, 514, 783, 857, 824, 857, 824, 857, 765, 888, 810, 648, 300, 782, 138, 570, 809, 902, 113, 900, 865, 479, 902, 122, 200, 900, 805, 810, 197, 121, 865, 479, 227, 902, 31, 251, 786, 557, 565, 1, 372, 900, 865, 479, 902, 100, 900, 865, 479, 227, 902, 1, 348, 256, 900, 865, 479, 13, 588, 865, 479, 537, 25, 644, 296, 450, 304, 570, 805, 26, 30, 536, 314, 104, 634, 865, 479, 900, 713, 846, 93, 59, 313, 515, 294, 844 ] assert encode_high(to_bytes(TEST_DATA), 6, 2) == expected
def test_encode_high(): # High level encoding expected = [ 130, 227, 637, 601, 843, 25, 479, 227, 328, 765, NUMERIC_LATCH, 1, 624, 142, 113, 522, 200, TEXT_LATCH, 865, 479, 267, 630, 416, 868, 237, 1, 613, 130, 865, 479, 567, 21, 550, 26, 64, 559, 26, 841, 115, 479, 841, 0, 0, 808, 777, 6, 514, 58, 765, 871, 818, 206, 868, 177, 258, 236, 868, 567, 425, 592, 17, 146, 118, 537, 448, 537, 448, 535, 479, 567, 21, 550, 26, 64, 559, 26, 841, 115, 479, 841, 0, 0, 808, 777, 6, 514, 58, 765, 877, 539, NUMERIC_LATCH, 31, 251, 786, 557, 565, 1, 372, TEXT_LATCH, 865, 479, 840, 25, 479, 227, 841, 63, 125, 205, 479, 13, 588, 865, 479, 537, 25, 644, 296, 450, 304, 570, 805, 26, 30, 536, 314, 104, 634, 865, 479, 73, 714, 436, 412, 39, 661, 428, 120 ] assert encode_high(to_bytes(TEST_DATA), 6, 2) == expected
def do_compact(str): return list(compact(to_bytes(str)))
def do_compact(str): return list(compact_text_interim(to_bytes(str)))
def chars(string): return [i for i in to_bytes(string)]