Exemplo n.º 1
0
def write_invoice_xls(file_name, invoice):

    book = xlwt.Workbook(encoding='ascii')
    sheet = book.add_sheet(u"Facture %s" % Config.NAME_ORGA)
    sheet.col(1).width = 0x0d00 * 3
    sheet.col(4).width = 0x0d00 * 2
    rowx = 0
    sheet.write_merge(rowx, rowx + 1, 0, 3,
                      u"Facture de %s" % Config.NAME_ORGA, style_title)

    rowx += 3
    date = "Bko le %s" % invoice.date.strftime("%d/%m/%Y")
    sheet.write_merge(rowx, rowx, 2, 3, date)

    hheaders = [_(u"Quantité"), _(u"Désignation"), _(u"Prix Unitaire"),
                _(u"Montant")]
    rowx += 2
    for colx, val_center in enumerate(hheaders):
        sheet.write(rowx, colx, val_center, style_t_table)
    rowx += 1

    data = [(item.quantity, item.description.name, item.price,
             item.quantity * item.price)
            for item in InvoiceItem.filter(invoices=invoice)]

    for prod in data:
        col = 0
        for val_center in prod:
            if isinstance(val_center, str):
                style_ = style
            else:
                style_ = int_style
            sheet.write_merge(rowx, rowx, col, col, val_center, style_)
            col += 1
        rowx += 1
    book.save(file_name)

    openFile(file_name)
Exemplo n.º 2
0
def pdFview(filename, invoice):
    """
        cette views est cree pour la generation du PDF
    """

    if not filename:
        filename = get_temp_filename('pdf')
        print(filename)
    #on recupere les items de la facture
    items_invoice = InvoiceItem.filter(invoices=invoice)

    # Static source pdf to be overlayed
    PDFSOURCE = 'fact_source.pdf'
    TMP_FILE = 'tmp.pdf'
    DATE_FORMAT = u"%d/%m/%Y"

    DEFAULT_FONT_SIZE = 11
    FONT = 'Courier-Bold'
    # A simple function to return a leading 0 on any single digit int.

    def double_zero(value):
        try:
            return '%02d' % value
        except TypeError:
            return value

    # setup the empty canvas
    from io import FileIO as file
    # from Common.pyPdf import PdfFileWriter, PdfFileReader
    from PyPDF2 import PdfFileWriter, PdfFileReader

    # PDF en entrée
    input1 = PdfFileReader(file(PDFSOURCE, "rb"))

    # PDF en sortie
    output = PdfFileWriter()
    # Récupération du nombre de pages
    n_pages = input1.getNumPages()
    # Pour chaque page
    for i in range(n_pages):
        # Récupération de la page du doc initial (input1)
        page = input1.getPage(i)

        p = canvas.Canvas(TMP_FILE, pagesize=A4)
        # p.setFont(FONT, DEFAULT_FONT_SIZE)

        p.drawString(130, 683, str(invoice.number))
        p.drawString(98, 667, (invoice.client))

        p.drawString(460, 683, str(invoice.date.strftime(DATE_FORMAT)))

        # On ecrit les invoiceitem
        x, y = 40, 610
        for i in items_invoice:
            p.drawString(x, y, str(i.quantity).rjust(11, ' '))
            p.drawString(x + 75, y, str(i.description))
            p.drawString(x + 340, y, str(i.price).rjust(10, ' '))
            p.drawString(x + 430, y, str(i.price * i.quantity).rjust(10, ' '))
            y -= 20
        # on teste le type
        if invoice.type_ == "Facture":
            p.drawString(59, 80, "Pour acquit: ")
        else:
            p.drawString(59, 80, "Pour acceptation: ")

        p.drawString(435, 80, "Le fournisseur: ")
        #On calcul le montant total hors taxe et sa conversion en lettre
        ht = 0
        for i in items_invoice:
            montant = i.price * i.quantity
            ht += montant

        ht_en_lettre = cel(ht)

        p.drawString(470, 150, str(ht).rjust(10, ' '))
        ht_en_lettre1, ht_en_lettre2 = controle_caratere(ht_en_lettre +
                                                        " FCFA", 46, 40)
        p.drawString(258, 116, (ht_en_lettre  + " FCFA"))
        # p.drawString(258, 116, (ht_en_lettre1))
        # p.drawString(53, 100, (ht_en_lettre2))

        # legal_infos, legal_infos1 = controle_caratere(Config.BP, 55, 55)
        # p.drawString(90, 14, legal_infos)
        # p.drawString(90, 6, legal_infos1)
        p.showPage()
        # Sauvegarde de la page
        p.save()
        # Création du watermark
        watermark = PdfFileReader(file(TMP_FILE, "rb"))
        # Création page_initiale+watermark
        page.mergePage(watermark.getPage(0))
        # Création de la nouvelle page
        output.addPage(page)
    # Nouveau pdf
    file_dest = filename + ".pdf"
    outputStream = file(file_dest, u"wb")
    output.write(outputStream)
    outputStream.close()

    return file_dest