0

Create a zip file of all invoices generated with printRecord()

Hello,

I would like to create a zip file that includes all invoices from all records in the "Transactions" table.

Invoices are usually obtained individually by clicking on a button containing the printRecord(this, "Invoice_template_dynamic") function.

I would need code that generates all the invoices and then integrates them into a zip file before downloading the zip file directly to my computer (I don't want to save the zip file in Ninox).

Thanks for your help.

11 replies

null
    • Créateur de bien-être
    • Sebastien_Guillet
    • 11 days ago
    • Reported - view

    Hello,

    I am reviving the topic concerning the need to download all invoices for a given period in a zip file.

    Here is my current code that downloads each pdf independently

    let filterStart := 'Début du filtre';
    let filterEnd := 'Fin du filtre';
    let sample := (select Transactions);
    for loop in sample do
        if loop.date('Date de facturation') >= filterStart and
            loop.date('Date de facturation') <= filterEnd then
            printRecord(loop, "facture_dynamique")
        end
    end
    

    Ideally, each pdf would be named "Invoice No." followed by the invoice number found at (select Transactions).'N° de transaction' and that the work is done in the background (so that it does not block Ninox during loading time which can be very long).

    Thank you for your help.

      • Fred
      • 11 days ago
      • Reported - view

      Have you already created the invoices and now you want to find all invoices for a particular time period then zip it? Take a look at the createZipFile() command.

      • Créateur de bien-être
      • Sebastien_Guillet
      • 11 days ago
      • Reported - view

       Yes, that's exactly what I would like. I tested the createZipFile() function but I couldn't get it to work. When I integrate it into my code and click on the button, Ninox launches a loading that doesn't stop (the app freezes). I must not have understood how to integrate the function in my specific case.

      • John_Halls
      • 11 days ago
      • Reported - view

       Did you see in the documentation that this function has to be run server side?

      • Créateur de bien-être
      • Sebastien_Guillet
      • 11 days ago
      • Reported - view

      Oui :(

    • Créateur de bien-être
    • Sebastien_Guillet
    • 11 days ago
    • Reported - view

    The problem is that I can't manipulate printRecord() with createZipFile(). Either the files are downloaded directly to my machine without going through zip compression, or nothing happens.

    • Ninox partner
    • RoSoft_Steven.1
    • 11 days ago
    • Reported - view

    First you need to create all your PDF's because of the lag between the actual PDF is created and the availability of this file. Then you need a second image field where you would put your .zip file in. Lets say I call this field "zipfile(ImageField)"

    So you need 2 buttons , one to create all your PDF's and another to create your .zip file where you can add a filter in your select function (or create a dialog between the code to add some time). Be aware that these variables must be in the "do as server" area.

    example:

    do as server
    let strt := today();
    let end := today()+10;
    let inv := (select Invoices where InvoiceDate >= strt and InvoiceDate <= end).Invoice(ImageField);
    zipfile(ImageField) := createZipFile(this, inv, "ZippedInvoices.zip")
    end

    Following code won't work (the docs are a bit limited about this):

    let strt := today();
    let end := today()+10;
    let inv := (select Invoices where InvoiceDate >= strt and InvoiceDate <= end).Invoice(ImageField);
    do as server
    zipfile(ImageField) := createZipFile(this, inv, "ZippedInvoices.zip")
    end
    
      • Créateur de bien-être
      • Sebastien_Guillet
      • 11 days ago
      • Reported - view

       

      I followed your advice and the structure of your code and I managed to do what I wanted by creating several buttons.

      A first one to generate each document in a record of its own.
      A second one to create the zip file in a file field.
      A third one to download the zip file.
      A fourth one to delete the records created in the first step and the zip file.

      I created yes/no fields to manage the display of the buttons.

      Thank you for your help.

    • Créateur de bien-être
    • Sebastien_Guillet
    • 11 days ago
    • Reported - view

    If it can help someone, here is the code for each button:

    Button to generate each invoice in a record of my 'Documents' table with the indicator "Type de document' := 18" which will later be used to delete all the records that have just been created.

    let filterStart := 'Début du filtre';
    let filterEnd := 'Fin du filtre';
    let sample := (select Transactions);
    for loop in sample do
        let nomFacture := "Facture n°" + loop.'N° de transaction' + ".pdf";
        if loop.date('Date de facturation') >= filterStart and
            loop.date('Date de facturation') <= filterEnd then
            let me := this;
            let newDocFacture := (create Documents);
            newDocFacture.(Compte := loop.Compte);
            newDocFacture.('Type de document' := 18);
            newDocFacture.('Date de création' := today());
            newDocFacture.(Transactions := loop);
            newDocFacture.('Nom du document' := nomFacture);
            newDocFacture.(Document := importFile(this, printAndSaveRecord(loop, "facture_dynamique"), nomFacture))
        end
    end;
    'Documents générés en masse' := true;
    'Fichier zip généré' := false
    

    Button to create the zip in the 'Fichier zip' field.

    do as server
        'Fichier zip' := createZipFile(this, (select Documents where 'Type de document' = 18).Document, "Factures.zip")
    end;
    'Fichier zip généré' := true
    

    Button to download the zip file.

    let i := fileMetadata(this, last(split(text('Fichier zip'), "/")));
    i.name;
    downloadURL(text(shareFile('Fichier zip')), text(i.name));
    'Zip téléchargé' := true
    

    Button to delete all records that were created for invoice download. They are separated from other documents with "Type de document' := 18"

    'Fichier zip' := null;
    ---
    Suppression des documents
    ---;
    do as server
        delete (select Documents where 'Type de document' = 18)
    end;
    ---
    Fin
    ---;
    'Documents générés en masse' := false;
    'Zip téléchargé' := false;
    'Fichier zip supprimé' := true
    

    Please note however that the zip file is deleted from 'Fichier zip' and will be moved to the paperclip.

      • Ninox partner
      • RoSoft_Steven.1
      • 11 days ago
      • Reported - view
      • Créateur de bien-être
      • Sebastien_Guillet
      • 11 days ago
      • Reported - view

       Indeed that solves the problem of the paperclip. Thanks!

      removeFile('Fichier zip');
      'Fichier zip' := null;
      ---
      Suppression des documents
      ---;
      do as server
          delete (select Documents where 'Type de document' = 18)
      end;
      ---
      Fin
      ---;
      'Documents générés en masse' := false;
      'Zip téléchargé' := false;
      'Fichier zip supprimé' := true