IP 1.5.2 - recurring invoices crash

I’m running InvoicePlane from the beginning (the time before also mylcientbase and then fusioninvoice).
After upgrading to 1.5.2 everything seemed fine, unless I recognized my monthly recurring invoices were not created (I have multiple recurring invoices, the first one gets created, than it crashes.) When running the cron again, the first one will be created again before it crashes and so on…
These are the same recurring invoices I was already using for a long time with earlier versions, nothing fancy new.

running the cronjob manually:

[root@server../invoiceplane]# /usr/bin/wget -O -  https://invoice.example.org/invoices/cron/recur/csBDb2SQkoKmxusD 
--2017-06-03 22:43:26--  https://invoice.example.org/invoices/cron/recur/csBDb2SQkoKmxusD
Resolving invoice.example.org (invoice.example.org)... 10.xx.xx.xx
Connecting to invoice.example.org (invoice.example.org)|10.xx.xx.xx|:443... connected.
HTTP request sent, awaiting response... 500 Internal Server Error
2017-06-03 22:43:26 ERROR 500: Internal Server Error.

The log shows:

ERROR - 2017-06-03 22:33:45 --> Query error: Duplicate entry '612-0' for key 'invoice_id' - Invalid query: INSERT INTO `ip_invoice_custom` (`invoice_id`, `invoice_custom_fieldid`, `invoice_custom_fieldvalue`) VALUES (612, 'invoice_custom_fieldid', '11')
ERROR - 2017-06-03 22:34:30 --> Query error: Duplicate entry '613-0' for key 'invoice_id' - Invalid query: INSERT INTO `ip_invoice_custom` (`invoice_id`, `invoice_custom_fieldid`, `invoice_custom_fieldvalue`) VALUES (613, 'invoice_custom_fieldid', '11')
ERROR - 2017-06-03 22:36:55 --> Query error: Duplicate entry '614-0' for key 'invoice_id' - Invalid query: INSERT INTO `ip_invoice_custom` (`invoice_id`, `invoice_custom_fieldid`, `invoice_custom_fieldvalue`) VALUES (614, 'invoice_custom_fieldid', '11')
ERROR - 2017-06-03 22:37:52 --> Query error: Duplicate entry '615-0' for key 'invoice_id' - Invalid query: INSERT INTO `ip_invoice_custom` (`invoice_id`, `invoice_custom_fieldid`, `invoice_custom_fieldvalue`) VALUES (615, 'invoice_custom_fieldid', '11')
ERROR - 2017-06-03 22:39:33 --> Query error: Duplicate entry '616-0' for key 'invoice_id' - Invalid query: INSERT INTO `ip_invoice_custom` (`invoice_id`, `invoice_custom_fieldid`, `invoice_custom_fieldvalue`) VALUES (616, 'invoice_custom_fieldid', '11')
ERROR - 2017-06-03 22:43:26 --> Query error: Duplicate entry '617-0' for key 'invoice_id' - Invalid query: INSERT INTO `ip_invoice_custom` (`invoice_id`, `invoice_custom_fieldid`, `invoice_custom_fieldvalue`) VALUES (617, 'invoice_custom_fieldid', '11')

and this is the corresponding sql query snippet. before upgrading, invoice_id was counting one number up each time, now it doesn’t seem to do that:

mysql> select * from ip_invoice_custom;
+-------------------+------------+------------------------+----------------------------------------------------------------------------+
| invoice_custom_id | invoice_id | invoice_custom_fieldid | invoice_custom_fieldvalue                                                  |
+-------------------+------------+------------------------+----------------------------------------------------------------------------+
....
|              2038 |        611 |                     13 | NULL                                                                       |
|              2039 |        611 |                     12 | NULL                                                                       |
|              2040 |        611 |                      7 | NULL               

Any ideas how to get rid of this issue ?

Thanks a lot and best Regards
Peter

I just saw this as well.

I “fixed” this by dropping the index on invoice_id on ip_invoice_custom, but after doing so, I got to the next bug:

An Error Was Encountered
Unable to load the requested file: invoice_templates/pdf/default.php

Hi EPEC,

wow, thanks. dropping the index with

mysql> ALTER TABLE ip_invoice_custom DROP INDEX invoice_id;
Query OK, 0 rows affected (0.76 sec)
Records: 0  Duplicates: 0  Warnings: 0

really solved the cron issue (but 0 rows affected ?) and all the invoices are created. But as you said, when trying to generate the PDF, there are lots of “ERROR - -> Severity: Notice --> Undefined index” erros…

For now I disabled the cron, waiting for a solution…

1 Like

I had the same issue, but with that ALTER TABLE ip_invoice_custom DROP INDEX invoice_id; cron is working again and I don’t have problems generating a PDF.
How about you?

There seem to be a bug indeed, within the custom fields module from what I’ve tested so far.

I managed to reproduce the bug this way:

  1. Create a custom field for invoices and save it.
  2. Edit an existing invoice of your choice, put some text in your custom field, save the invoice.
  3. Create an other new custom field, save it.
  4. Go back to the previous invoice, edit it. You should see your very first custom field is already filled with your previous text, so now just fill your very new custom field with some text, then save your invoice.

Now you would normally expect both your 2 custom fields to be filled with text, but actually the very first custom field has been emptied while the last custom field is ok.

I think the bug is somewhere inside Mdl_invoices.php within method copy_invoice() right before or after it calls method save_custom() inside Mdl_invoice_custom.php

My guess is there is something wrong about how it gets the last mysql insert id. We must check the num_rows() and row(). The $invoice_custom_id property is probably not correctly incremented in my opinion. I noticed the entry within the table ip_invoice_custom was overwritten from this bug, which might very well explain your Duplicate entry
Pretty confident about this being the issue.
I’ll see if I can fix it later today.

@denysvitali

getting close I think, probably something wrong within Mdl_invoice_custom.php between line 147-167.
looping arrays + regex can be a great source of error.

I’ll look into this ASAP and provide a fix. Sorry about the issue

1 Like

Thank you.
The regex is fine btw and populates the array as expected. Don’t mind my previous message.
It seems like something is overwritting “invoice_custom_fieldid”.

I’m also interested by the fix :slight_smile:

Any news about this fix ? :slight_smile:

I’m not sure, maybe lost track of the many issues that are present right now.
@musa @denysvitali Do you have any idea about this?

Unfortunately I haven’t had time to look into this issue. I’ll check right now and report back

Edit: By looking at the error thrown, and the SQL query, apparently the cronjob is trying to insert into the ip_invoice_custom table a TEXT value (in this case invoice_custom_fieldid) instead of its corresponding fieldid value (which is an INT).

If this is the real problem, the fix seems pretty easy.

I will update this comment whenever I investigate the issue further

Looks like the problem is here

1 Like

I’m not sure it is the same problem…

Why do you think this isn’t the problem?

I’m not not an expert.
Maybe you are right, the recurring invoice problem could comes from the custom fields which are not copied correctly during duplication.
Sorry for my previous comment…

Don’t worry: that is indeed the problem :slight_smile:
I’m going to fix it right now, and make a PR so you can all enjoy having recurring invoices again

Youhou hou ! Thanks :wink:

Assigned IP-555.

PR proposed, it is being discussed here.
In the meantime, @Spheerys @peterbeck @musa if you want to test it you can clone my v1.5.4 branch (git clone -b v1.5.4 https://github.com/Coelis/InvoicePlane/)