empty field in database not handled as expected

SimpleInvoices Group Forum Forums Fearless359 SimpleInvoices Discussion Group empty field in database not handled as expected

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #1543
    ppmt
    Participant

    For my template I basically used the default one and have adapted it to my requirement.

    I can see that through the templates it either use isset or print_if_not_null to decide if a field should be printed or not.

    In my case the logic doesn’t work. I have a some fields that are not populated for some of my customers and yet the label for that field is still printed in the pdf generated.

    I have tried to edit the database and set the field to null and it seems to fix the problem but as soon as I go and edit the customers and save it then the fields reappear.

    My database is coming from previous update so I am wondering if something in the format of the database is wrong.

    Any idea or pointer as what could be wrong?

    #1544
    ppmt
    Participant

    Here are some printout. I could not put them in the main post as it was telling me it looks like spam!

    describe si_customers;
    +--------------------------+------------------+------+-----+---------+----------------+
    | Field                    | Type             | Null | Key | Default | Extra          |
    +--------------------------+------------------+------+-----+---------+----------------+
    | id                       | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
    | domain_id                | int(11) unsigned | NO   | PRI | NULL    |                |
    | attention                | varchar(255)     | YES  |     | NULL    |                |
    | name                     | varchar(255)     | YES  |     | NULL    |                |
    | department               | varchar(255)     | YES  |     | NULL    |                |
    | street_address           | varchar(255)     | YES  |     | NULL    |                |
    | street_address2          | varchar(255)     | YES  |     | NULL    |                |
    | city                     | varchar(255)     | YES  |     | NULL    |                |
    | state                    | varchar(255)     | YES  |     | NULL    |                |
    | zip_code                 | varchar(20)      | YES  |     | NULL    |                |
    | country                  | varchar(255)     | YES  |     | NULL    |                |
    | phone                    | varchar(255)     | YES  |     | NULL    |                |
    | mobile_phone             | varchar(255)     | YES  |     | NULL    |                |
    | fax                      | varchar(255)     | YES  |     | NULL    |                |
    | email                    | varchar(255)     | YES  |     | NULL    |                |
    | credit_card_holder_name  | varchar(255)     | YES  |     | NULL    |                |
    | credit_card_number       | varchar(20)      | YES  |     | NULL    |                |
    | credit_card_expiry_month | varchar(2)       | YES  |     | NULL    |                |
    | credit_card_expiry_year  | varchar(4)       | YES  |     | NULL    |                |
    | notes                    | text             | YES  |     | NULL    |                |
    | parent_customer_id       | int(11)          | YES  |     | NULL    |                |
    | default_invoice          | int(10) unsigned | NO   |     | 0       |                |
    | custom_field1            | varchar(255)     | YES  |     | NULL    |                |
    | custom_field2            | varchar(255)     | YES  |     | NULL    |                |
    | custom_field3            | varchar(255)     | YES  |     | NULL    |                |
    | custom_field4            | varchar(255)     | YES  |     | NULL    |                |
    | enabled                  | tinyint(1)       | NO   |     | 1       |                |
    +--------------------------+------------------+------+-----+---------+----------------+
    27 rows in set (0.002 sec)
    • This reply was modified 3 years, 7 months ago by ppmt.
    #1545
    ppmt
    Participant

    and some empty field from that table

    select attention, phone, street_address from si_customers where name= “Andy”;
    +———–+——-+—————-+
    | attention | phone | street_address |
    +———–+——-+—————-+
    | | | |
    +———–+——-+—————-+

    #1549
    RRowley
    Participant

    The isset() test for a field is to see if there is valid content to print. You can use the empty() test on a field to determine if both the label and the field should be printed. For instance here is a test to determine if custom field 1 should be printed:

    <!-- Show the Invoice Custom Fields if valid -->
     {if !empty($customFieldLabels.invoice_cf1) && isset($invoice.custom_field1)}
     <tr>
       <td nowrap class="">{$customFieldLabels.invoice_cf1|htmlSafe}:</td>
       <td class="si_right" colspan="3">{$invoice.custom_field1|htmlSafe}</td>
     </tr>
     {/if}
    #1550
    ppmt
    Participant

    Ok so !empty does fix the problem I have with isset. Thanks

    How about the print_if_not_null function? I found that there is print_if_not_empty but looking at the code it is actually the same as print_if_not_null.
    I guess I will copy that function, put it in my theme plugins and replace isset by !empty

    Is there any reason why the “empty” field are not set to NULL when not populated? The talbe definition seems to indicate that the defaul should be NULL

    #1552
    RRowley
    Participant

    I’ve gone through and removed all duplicate occurrences of the various smarty function so that the standard version in the library is used. There is no need to include a function in a invoice templates plugin directory unless you want to modify it for some reason.

    I’ve updated internal comments in the not null and not empty functions to explain the difference. Basically and item can be not null but empty (blank, 0, etc. are empty values). It appears there is no use of the not empty function but I’ll keep it around for now anyway.

    #1553
    ppmt
    Participant

    Ok so may be I am missing something but it doesn’t seem to work for me. Here is an extract of an invoice. As you can see the field phone, fax, mobile and email are empty but the field is still displayed.

    Here is the code I am using in my template:

          {print_if_not_empty label=$LANG.phoneShort field=$customer.phone class1='' class2='' colspan="3"}
          {print_if_not_empty label=$LANG.fax field=$customer.fax class1='' class2='' colspan="3"}
          {print_if_not_empty label=$LANG.mobileShort field=$customer.mobile_phone class1='' class2='' colspan="3"}
          {print_if_not_empty label=$LANG.email field=$customer.email class1='' class2='' colspan="3"}

    I have tried with print_if_not_null as well and the result is the same.

    #1555
    RRowley
    Participant

    It works in all my testing. Make sure you don’t have a local copy of the function in your invoice plugins directory. I removed them all from the standard version and have worked with this in testing the logic in the various templates and it works fine. I’m in the middle of making some significant changes to SI which includes enhancements to these functions to using multiple labels values and such. So it will be a little while before I get those delivered.

    #1556
    ppmt
    Participant

    Hi,
    Sorry for the delay been away.

    I cleared my plugins directory and deleted everything in tmp and the problem remains.
    I can wait for your next update and hopefully something will change.

    It must be something with my setting or my database then because I have the same issue when I use the other templates provided by default.

    #1557
    RRowley
    Participant

    I believe you need to start adding debug statements to the function. Try this. In the include/smarty_plugins/function.print_if_not_empty.php file add the following code after the setting of the $class2 variable:

    if ($params['label'] == "Ph.") {
        $str = "smarty_function_print_if_not_empty(): Ph is ";
        $str .= empty($params['field']) ? "empty. " : "not empty. ";
        $str .= "printIfEmpty is ";
        $str .= $printIfEmpty ? "true." : "false.";
        var_dump($str);
    }
    

    When you print the template, you will get a message at the top of the print info screen that will tell you if it sees the phone field as empty and if it is setting the print if empty option for some reason. A not empty phone field or a true setting on the print if empty variable are the only ways that the logic will print something.

    Hope this helps.

    #1558
    ppmt
    Participant

    Good news!

    It turns out I was not quite up to date with github. I couldn’t find the $class2 your were talking about.
    So I downloaded the latest version and now it works. If I use print_if_not_empty then it now works.

    #1559
    RRowley
    Participant

    These are the first three lines of the function. The code would go in after them.

     $printIfEmpty = !empty($params['printIfEmpty']) && $params['printIfEmpty'] == true;
        $class1 = empty($params['class1']) ? '' : Util::htmlSafe($params['class1']);
        $class2 = empty($params['class2']) ? '' : Util::htmlSafe($params['class2']);
    
    #1560
    ppmt
    Participant

    Hi Richards,

    Thanks for your help and patience.
    I didn’t have the latest version from the github which contains the new code for the function. After I downloaded it I can report that the print_if_not_empty function is now working as intended.

    I now just have a few tweaks to do in my template and I will be able to switch to the master_2020 version on my production machine.

Viewing 13 posts - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.