Tryton - Issues

 

Issue7192

Title Payment type is missing when paying customer invoice in foreign currency
Priority bug Status resolved
Superseder Nosy List Artem, ced, pokoli, reviewbot, roffez, roundup-bot, rvong
Type behavior Components account_invoice
Assigned To ced Keywords review
Reviews 52471002
View: 52471002

Created on 2018-03-05.13:27:27 by Artem, last changed by roundup-bot.

Messages
New changeset dce1f8434d48 by Cédric Krier in branch 'default':
Compare amount in invoice currency against amount_to_pay
https://hg.tryton.org/tryton-env/rev/dce1f8434d48
New changeset 2cbd0314a9c0 by Cédric Krier in branch 'default':
Compare amount in invoice currency against amount_to_pay
https://hg.tryton.org/modules/account_invoice/rev/2cbd0314a9c0

New changeset 13842d6c22f2 by Cédric Krier in branch 'default':
Add scenario about invoice in alternate currency with lower rate
https://hg.tryton.org/modules/account_invoice/rev/13842d6c22f2
review52471002 updated at https://codereview.tryton.org/52471002/#ps20001
msg43860 (view) Author: [hidden] (ced) (Tryton committer) (Tryton translator) Date: 2018-09-13.17:49:59
I do not think we should use getattr because the workflow of the wizard should step to the ask transition if the amount paid is different of the amount of the invoice or if the amount in company currency is different.
So I added a scenario in the review52471002 that test this case. Without the correction, the scenario fails with the same traceback as msg39831 and with the correction, it does not fail. So for me, the case is fixed.
msg43859 (view) Author: [hidden] (rvong) Date: 2018-09-13.15:31:30
Hi Cédric, in my first message msg43846 although it is related to the issue6842 regarding the currency handling is a bit misleading. Fixing it may not solve the attribute error. Please read my last message (msg43852) where I propose a better solution to the problem. At the end it has to do with uninitialized default values. My apologies for the confusion. 

I will investigate on how to use the code review tool and maybe create a patch to help out. :-)
msg43856 (view) Author: [hidden] (ced) (Tryton committer) (Tryton translator) Date: 2018-09-13.09:35:14
Thanks to your description @rvong, I could create a scenario to reproduce it. The key was to have a lower rate. Indeed it is because issue6842 was not complete.
So here is review52471002 which should fix it.
New review52471002 at https://codereview.tryton.org/52471002/#ps1
msg43852 (view) Author: [hidden] (rvong) Date: 2018-09-13.02:33:24
Please ignore my previous message, I should have dig a little deeper and provide more information before posting messages here. So, this time I would like to share what I have found.

The error states that object account.invoice.pay.ask does not have the attribute "type" in the dictionary _values of the model. This happens because default values were not initialized. I noticed this should have been done with the call to "default_ask" function in the PayInvoice wizard class. I put logging in the function _execute from class Wizard and found that it was never called. This is because the payment was for the entire amount of the invoice hence it jumped from state "choice" to "pay".

Here is the log statement that I put on the _execution function:
        log.debug('Calling {}._execute, state_name: {}, class of state: {}'.format(
                type(self).__name__, state_name, type(state).__name__))

Here is the output:

Thu Sep 13 00:20:59 2018] DEBUG:trytond.wizard.wizard:Calling account.invoice.pay._execute, state_name: start, class of state: StateView
Thu Sep 13 00:21:28 2018] DEBUG:trytond.wizard.wizard:Calling account.invoice.pay._execute, state_name: choice, class of state: StateTransition
Thu Sep 13 00:21:28 2018] DEBUG:trytond.wizard.wizard:Calling account.invoice.pay._execute, state_name: pay, class of state: StateTransition

I think a check must be put in the transition_pay function when the "ask" state was never used and therefore no default values.

I think the solution is change the statement:

 ... self.ask.type != 'writeoff' 

to:
  ... getattr(self.ask, 'type'. '') != 'writeoff'...
msg43846 (view) Author: [hidden] (rvong) Date: 2018-09-12.22:18:28
I got the same error as describe by roffez. The bug may be related how invoice payment is handled when paying with a foreign currency. I am using trytond from pypi version 4.8.3 and trytond_account_invoice version 4.8.1. I have put some logging code around the "if (abs(amount) > abs(invoice.amount_to_pay)" statement in line 2695 in the "transition_pay" function in class PayInvoice, and I have found that "amount" and "invoice.amount_to_pay" are in different currencies, which will then continues evaluating with the second condition of the if statement and "type" attribute is missing from the object account.invoice.pay.ask.

I don't know why "amount" and "invoice.amount_to_pay" are compared this way, but an easy way to replicate this error without modifying the code with logging statements is to play with the currencies setup:
CASE 1: 
1- Setup a company to work with a currency that has lower value than the currency used in the invoice.  For example, my company is using CRC (Costa Rican Currency) which has the rate of "1" (default currency) and the invoice is in USD with rate of 0.001724 (meaning every 0.001724 USD is equal to 1 CRC).
2- Setup the rates for the date to create the invoice
3- Create an invoice for 100 USD and post it.
4- Click on Pay button to pay the invoice
5- Leave the default amount which is 100 USD and select journal "CASH"
6- Click OK and the error popups.

CASE 2:
1- Setup the currency the other way around: Set the company to work with USD currency and the invoice in CRC. 
2- Setup the rates for the invoice date: "1" for USD and "580" for CRC. 
3- Create an invoice for 1000 CRC and post it
4- Click on Pay button 
5- Leave the default amount which is 1000 CRC
6- Click Ok and no errors.
msg42934 (view) Author: [hidden] (roffez) Date: 2018-08-17.15:55:12
Was able to reproduce here. I'm issuing a lot of invoices in EUR and this happens only from time to time, very strange!

What I did (similar to Artem)
- Company's main currency is CHF
- Create customer invoice of EUR 107.00
- Post invoice
- Press "Pay"
- Enter 107.00 EUR, Journal "Cash". Press "OK" => Error occurs.



Traceback (most recent call last):
  File "/trytond/wsgi.py", line 70, in dispatch_request
    return endpoint(request, **request.view_args)
  File "/trytond/protocols/dispatcher.py", line 41, in rpc
    request, database_name, *request.rpc_params)
  File "/trytond/wsgi.py", line 41, in auth_required
    return wrapped(*args, **kwargs)
  File "/trytond/protocols/wrappers.py", line 110, in wrapper
    return func(request, pool, *args, **kwargs)
  File "/trytond/protocols/dispatcher.py", line 165, in _dispatch
    result = rpc.result(meth(*c_args, **c_kwargs))
  File "/trytond/wizard/wizard.py", line 287, in execute
    return wizard._execute(state_name)
  File "/trytond/wizard/wizard.py", line 318, in _execute
    result = self._execute(transition())
  File "/trytond/wizard/wizard.py", line 318, in _execute
    result = self._execute(transition())
  File "/trytond/modules/account_invoice/invoice.py", line 2625, in transition_pay
    and self.ask.type != 'writeoff'):
  File "/trytond/model/model.py", line 387, in __getattr__
    % (self.__name__, name, self._values))
AttributeError: 'account.invoice.pay.ask' Model has no attribute 'type': None
msg42246 (view) Author: [hidden] (ced) (Tryton committer) (Tryton translator) Date: 2018-07-22.01:27:08
No news in 3 months. As we can not reproduce the issue, I propose to close it. Re-open it if you can still reproduce on vanilla installation.
msg39958 (view) Author: [hidden] (pokoli) (Tryton committer) (Tryton translator) Date: 2018-04-13.16:42:19
I confirm that I can not reproduce it also.
msg39831 (view) Author: [hidden] (Artem) Date: 2018-04-06.09:55:10
Here is the full traceback: 

Traceback (most recent call last):
  File "/trytond/model/model.py", line 384, in __getattr__
    return self._values[name]
TypeError: 'NoneType' object is not subscriptable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/trytond/wsgi.py", line 70, in dispatch_request
    return endpoint(request, **request.view_args)
  File "/trytond/protocols/dispatcher.py", line 41, in rpc
    request, database_name, *request.rpc_params)
  File "/trytond/wsgi.py", line 41, in auth_required
    return wrapped(*args, **kwargs)
  File "/trytond/protocols/wrappers.py", line 110, in wrapper
    return func(request, pool, *args, **kwargs)
  File "/trytond/protocols/dispatcher.py", line 165, in _dispatch
    result = rpc.result(meth(*c_args, **c_kwargs))
  File "/trytond/wizard/wizard.py", line 287, in execute
    return wizard._execute(state_name)
  File "/trytond/wizard/wizard.py", line 318, in _execute
    result = self._execute(transition())
  File "/trytond/wizard/wizard.py", line 318, in _execute
    result = self._execute(transition())
  File "/trytond/modules/account_invoice/invoice.py", line 2625, in transition_pay
    and self.ask.type != 'writeoff'):
  File "/trytond/model/model.py", line 387, in __getattr__
    % (self.__name__, name, self._values))
AttributeError: 'account.invoice.pay.ask' Model has no attribute 'type': None
msg39638 (view) Author: [hidden] (ced) (Tryton committer) (Tryton translator) Date: 2018-04-01.19:12:59
I can not reproduce and it is difficult to understand without the full traceback.
msg38802 (view) Author: [hidden] (Artem) Date: 2018-03-05.13:27:26
To replicate the error:
- Company's main currency is USD
- create customer invoice for the amount of EUR 100
- Invoice date and invoice accounting date are equal (in this case - 2018-03-04
- Post invoice
- Currency rates as of 2018-03-04: for USD - 1, for EUR - 0.9
- Press "Pay" button in the posted customer invoice
- The pop-up window "Pay Invoice" appears
- Fill in the values in this window: amount: 100, currency: EUR, Date: 2018-03-04, journal - CASH journal
- press OK

As a result, the Attribute Error appears: 'account.invoice.pay.ask' Model has no attribute 'type': None
History
Date User Action Args
2018-09-17 17:52:28roundup-botsetmessages: + msg43921
2018-09-17 17:52:12roundup-botsetstatus: testing -> resolved
nosy: + roundup-bot
messages: + msg43920
2018-09-16 18:51:15reviewbotsetmessages: + msg43907
2018-09-13 17:49:59cedsetmessages: + msg43860
2018-09-13 15:31:30rvongsetmessages: + msg43859
2018-09-13 09:35:15cedsetstatus: chatting -> testing
assignedto: ced
messages: + msg43856
2018-09-13 09:33:31reviewbotsetnosy: + reviewbot
messages: + msg43855
2018-09-13 09:33:30reviewbotsetreviews: 52471002
keyword: + review
2018-09-13 02:33:24rvongsetmessages: + msg43852
2018-09-12 22:18:28rvongsetstatus: unread -> chatting
nosy: + rvong
component: + account_invoice
messages: + msg43846

Showing 10 items. Show all history (warning: this could be VERY long)