GnuCash
Contact   Instructions
Bug 798132 - Invoice Importing crashes when importing low quantity values
Summary: Invoice Importing crashes when importing low quantity values
Status: RESOLVED FIXED
Alias: None
Product: GnuCash
Classification: Unclassified
Component: Import - CSV (show other bugs)
Version: 4.4
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Mike Evans
QA Contact: import
URL:
Whiteboard:
Keywords:
: 798066 (view as bug list)
Depends on:
Blocks:
 
Reported: 2021-02-20 04:30 EST by Wmsxx
Modified: 2021-03-15 15:08 EDT (History)
7 users (show)

See Also:


Attachments
Tracefile (88.26 KB, text/plain)
2021-02-23 09:40 EST, Wmsxx
no flags Details
File causing the trouble with small value to import (135 bytes, text/plain)
2021-02-23 09:41 EST, Wmsxx
no flags Details
Test Account (3.95 KB, application/octet-stream)
2021-02-24 13:45 EST, Wmsxx
no flags Details
CSV Test file with header alignment (359 bytes, application/octet-stream)
2021-02-25 03:11 EST, Wmsxx
no flags Details
Stack Trace file (4.13 KB, text/plain)
2021-02-25 21:42 EST, Wmsxx
no flags Details
Stack Trace file (7.23 KB, text/plain)
2021-03-11 19:55 EST, Wmsxx
no flags Details

Description Wmsxx 2021-02-20 04:30:00 EST
Please be aware that i use the portable windows version

I have to use very low quantity values for a special type of invoice. I used the invoice editor and never had problems. Now I wanted to import an invoice from a CSV and the import crashes without any error.

XXXXXXX1;20.07.2020;XXXXXXX2;XXXXXXX3;XXXXXXX4;20.07.2020;XXXXXXX10;XXXXXXX5;XXXXXXX6:XXXXXXX7;0,00005;54,60;;;;;x;;20.07.2020;20.08.2020;XXXXXXX8:XXXXXXX9;XXXXXXX0;

I used XXXXXXX0 ... XXXXXXX10 for privacy reason and they seem not to be the problem. I can import quantity values of 1 down to 0,0005 but 0,00005 and lower cause a crash.

Please do not say such low quantity values do not make sense in my particular case it makes sense.
Comment 1 John Ralls 2021-02-20 12:35:31 EST
5/10000 of what?

In particular, what is the commodity's fraction traded and what have you set for the smallest fraction in the receiving account?

Is 54,60 the value of the items?

There are probably errors reported in the trace file, see https://wiki.gnucash.org/wiki/Tracefile for help finding it. Please attach the one from the crash using the "Add an attachment" link in the box above.
Comment 2 Wmsxx 2021-02-21 10:49:40 EST
Thank you for your quick response. Unfortunately I couldn't find any actual tracefiles. There are some from 2 years ago. However, I can't remember how I generated them. I need to go into more details.

It seems that the standard way of generating tracefiles as  described in the link does not work with the portable version of gc. Unfortunately I do not have the chance to install a version to see if it also crashes in the regular version. 

I will come back as soon as I have found anything new. Or do you know how to generate the tracefiles in the portable version? .\GnuCashPortable.exe -debug -extra does not start at all. Only without any option.
Comment 3 John Ralls 2021-02-21 11:52:58 EST
You'll have to ask PortableApps support. We don't support them because they modify our code and violate the GPL by not publishing their modifications.
Comment 4 Wmsxx 2021-02-23 09:40:04 EST
Created attachment 374006 [details]
Tracefile

Comment: A tracefile of the time before crash
Comment 5 Wmsxx 2021-02-23 09:41:42 EST
Created attachment 374007 [details]
File causing the trouble with small value to import

comment: Trouble file
Comment 6 Wmsxx 2021-02-23 09:47:31 EST
I could manage to install GNC on a new machine and reproduce the error. It does not only happen on the portable installation. I set up a test account and the machine crashed when I was importing a too small number (here 0,00001). I just XXXXXX out some personal data in the trace file.
Comment 7 John Ralls 2021-02-23 11:37:47 EST
The CSV isn't useful unless you also tell us what columns go to what GnuCash fields and the commodities and accounts involved. Since you made a test book you could attach that as well.

Since you declined to answer the questions in comment 1, I have to guess based on the trace file: It suggests that the fractions on the accounts are all either 1/1 or 1/100, so trying to import an amount of 5/100,000 will round to zero and computing the price (I guess 54,60 is the value?) would cause a divide-by-zero exception. You need to set the smallest fraction of both the commodity and the account to a value that will accommodate what you want to use.

Since you made a test book you can presumably attach that without any privacy concerns. I also need the field mapping for the CSV to reproduce the crash.
Comment 8 Wmsxx 2021-02-24 13:45:03 EST
Created attachment 374008 [details]
Test Account
Comment 9 Wmsxx 2021-02-24 14:29:01 EST
The test book is attached.

Oh I thought there is a fixed link between the columns. I used this Definition:

https://www.gnucash.org/docs/v4/C/gnucash-guide/busnss-imp-bills-invoices.html#:~:text=%20GnuCash%20executes%20the%20import%20process%20in%20three,creates,%20updates%20and%20posts%20the%20invoices.%20More

The mapping is
id; date_opened; owner_id; billingid; notes; date; desc; action; account; quantity; price; disc_type; disc_how; discount; taxable; taxincluded; tax_table; date_posted; due_date; account_posted; memo_posted; accu_splits;

Sorry I didn't see your comment. You are right I let the fractions unchanged at usual fractions. Abd yes in this example 54,60 is the value / price.

I now changed the fractions for the accounts but still have this problem.

I hope you can reproduce the crash. I am also not sure if I used the fractions correctly and that is the cause for the crash. But at least the program should provide a warning.
Comment 10 John Ralls 2021-02-24 21:27:00 EST
Sorry, I'd forgotten that bill/invoice csv import was fixed-format.

No crash, but I didn't try on Windows yet... but the sample CSV you attached has a quantity of 1 and a price of .0000001. That of course produces a zero value because Euros only have hundredths. Checking my original divide-by-zero hypothesis I switched the quantity and price. No problem there either, though this time both the quantity and value are zero.

Lining up the line that crashed in your original description with the headings gives

id      ; date_opened; owner_id; billingid; notes  ; date     ; desc    ; action; 
XXXXXXX1;  20.07.2020;  XXXXXXX2; XXXXXXX3;XXXXXXX4;20.07.2020;XXXXXXX10;XXXXXXX5;

quantity; price  ; disc_type; disc_how; discount; taxable; taxincluded;
XXXXXXX6:XXXXXXX7;   0,00005;    54,60;         ;        ;            ;

 tax_table; date_posted; due_date; account_posted; memo_posted; accu_splits;
          ;           x;         ;20.07.2020     ;  20.08.2020;    XXXXXXX8:

XXXXXXX9;XXXXXXX0;

I guess there's a misalignment there somewhere.
Comment 11 Wmsxx 2021-02-25 03:09:29 EST
That is stange. I tried it on a windows (my portable installation) where i realised the crash. And the test system was on a Linux machine. 

In all cases I an see the correct allignement in the import "editor".

I used the csv file and started with 0,01 (or 0,05 or what value I used) and inserted in every new step a further zero.

All imports worked, only when there have been too many zeros after the comma it crashed. May also try if you can insert more zeros.

Could it may relate to the country schema. I will make more variation when I have access to my Linux system again.

Just for completeness: In your above alignment the account after the "action" is missing. But I  think that is just citing error as you could import it correctly.

I added a file with the header alignment.
Comment 12 Wmsxx 2021-02-25 03:11:17 EST
Created attachment 374009 [details]
CSV Test file with header alignment
Comment 13 John Ralls 2021-02-25 12:02:24 EST
Ah, since you can crash on Linux then you can easily get a stack trace: See https://wiki.gnucash.org/wiki/Stack_Trace for instructions.

> Just for completeness: In your above alignment the account after the "action" is missing. But I  think that is just citing error as you could import it correctly.

Ah, account got lost while dealing with the wrapping, and I suppose the ':' in XXXXXXX6:XXXXXXX7 is the account separator, not a typo.

> Could it may relate to the country schema. I will make more variation when I have access to my Linux system again.

It's possible that it has something to do with localization, but I think it unlikely.
Comment 14 Wmsxx 2021-02-25 21:41:41 EST
(In reply to John Ralls from comment #13)
> Ah, since you can crash on Linux then you can easily get a stack trace: See
> https://wiki.gnucash.org/wiki/Stack_Trace for instructions.

I added the stack trace file

I imported the file with price = 0,0001 which worked and let me successfully import a bill. Then I added a zero to the price 0,00001 and it crashed after I entered "C" for continue.

The German terms in the file have the following meaning:

Datei oder Verzeichnis nicht gefunden = File or folder not found
Register können nicht ausgelesen werden: Kein passender Prozess gefunden. = Register cannot be read: no respective process.

I hope that helps.

> 
> > Just for completeness: In your above alignment the account after the "action" is missing. But I  think that is just citing error as you could import it correctly.
> 
> Ah, account got lost while dealing with the wrapping, and I suppose the ':'
> in XXXXXXX6:XXXXXXX7 is the account separator, not a typo.

That is correct
Comment 15 Wmsxx 2021-02-25 21:42:55 EST
Created attachment 374010 [details]
Stack Trace file
Comment 16 John Ralls 2021-02-28 18:46:14 EST
Comment on attachment 374010 [details]
Stack Trace file

You skipped the most important step in https://wiki.gnucash.org/wiki/Stack_Trace#Obtain_the_stack_trace
"2. Type bt (bt full might provide additional info about local variables)."

Please try again.
Comment 17 John Ralls 2021-02-28 18:49:53 EST
To be more clear, tell gdb 'bt' instead of 'c' when it tells you that terminate was called.
Comment 18 Wmsxx 2021-03-11 19:55:11 EST
Created attachment 374013 [details]
Stack Trace file
Comment 19 Wmsxx 2021-03-11 19:57:00 EST
Sorry I was not active recently. I added the trace file and hope this time it is useful
Comment 20 John Ralls 2021-03-11 20:38:36 EST
Yes, that's good. The crash is an unhandled exception in the Bill/Invoice importer caused by rounding of the quantity to 1/100 of the currency SCU.

That was done to fix bug 761172. I've added the author of that change (and much of the rest of the bi-importer) so that he can comment. Mike Evans, that's you.
Comment 21 Wmsxx 2021-03-12 01:43:12 EST
Great. Thank you.
Comment 22 Mike Evans 2021-03-12 09:10:45 EST
I can see where the bug needs to be handled, in dialog-bi_import.c line 793 and others with the same code. Probably.

But gnc_numeric_convert doesn't catch that error and return a suitable gnc_numeric_error, so there's no error to handle in the import code and it crashes.

Maybe calling gnc_numeric_convert with GNC_HOW_RND_NEVER is also a bug and it should be GNC_HOW_RND_ROUND_HALF_UP anyway.

As John mentioned in Comment 7 these small values will round to zero anyway for normal currencies.  Which begs the question, how many decimal places to support in the import?  I don't know of any _real_ currencies that require more places than we already provide.  Crypo is not supported in GnuCash AFAIK.

The comma as decimal threw me for a while.
Comment 23 Wmsxx 2021-03-12 11:10:14 EST
I still don't think it is because it rounds to 0. You can choose any factor like 100000 (price) and 0.00001 (quatity) (or 0,00001) which should result in 1 (should be available in any currency) and it crashes. You have such numbers for example when you sell land. And the small numbers are just the factors but the result is in a normal range.
The other interesting point is that I manually can creat such invoices with those small quantity factors and there is no problem with calculating the end price. The crash just occurs when I want to import such an invoice. GC itself can handle the numbers perfectly. I use these factors since many years. Only recently I wanted to use the import function and realised it crashes.
Comment 24 Mike Evans 2021-03-12 11:36:06 EST
(In reply to Wmsxx from comment #23)
> I still don't think it is because it rounds to 0.

The import code doesn't catch the rounding error or do anything to avoid the crash.  The smallest value the importer can handle is, 0.0001 as you have seen.  This will be fixed once I decide the best way without losing values due to rounding.
Comment 25 John Ralls 2021-03-12 15:00:35 EST
Mike,

Forcing the amount and price to be denominated at 1/100 of the currency SCU doesn't make sense. Price in particular should always be exact subject to the limits of 64-bit integers and the commodity that amount is in will have its own SCU. Instead calculate the value and check that for rounding errors. 

I'll fix gnc_numeric_convert to catch the exception and return a GNC_NUMERIC error so that you can use it to check amount * price and decide what to do about rounding.

BTW, GnuCash does have limited support for crypto currencies (except etherium because its SCU is an absurd 1/10^16), allowing the SCU up to 1/10^9. The limitation is that they're not currencies so you can't use them for Root, Equity, or AP/AR accounts.
Comment 26 Mike Evans 2021-03-12 15:52:14 EST
(In reply to John Ralls from comment #25)
> Mike,
> 
> Forcing the amount and price to be denominated at 1/100 of the currency SCU
> doesn't make sense.
I agree. No idea why I did that.
Comment 27 Mike Evans 2021-03-13 08:08:36 EST
Bug 734183 - Leads us to here too.
Comment 28 Mike Evans 2021-03-13 09:27:08 EST
*** Bug 798066 has been marked as a duplicate of this bug. ***
Comment 29 Mike Evans 2021-03-15 07:07:38 EDT
Commit 7f1335e6a2f7e87 should fix.  Please test.
Comment 30 Wmsxx 2021-03-15 14:37:25 EDT
I would like to test however, I am not an expert with these commits and self compiling. Is there an instruction somewhere from which I could try to build it.
Comment 31 John Ralls 2021-03-15 15:08:48 EDT
You don't need to build it, just get https://code.gnucash.org/builds/win32/maint/gnucash-4.4-2021-03-15-git-4.4-292-gb51d227af+.setup.exe and install it.

Note You need to log in before you can comment on or make changes to this bug.