This question has been flagged
4 Replies
6834 Views

I've been working on a module for interests. What I have to do is to make possible to create an invoice which has interests from overdue invoices as invoice lines. To do this, I need to pass few fields to another window with new invoice creation. Here is my problem. I tried to pass field value of invoice comment as a test. But when I add comment field with additional default, I noticed, that default is not read at all. When I edit this field in different way (e.g. I add readonly=False) then changes are visible. Only default do not work. Anyone know why?

Original field:

comment = fields.Text('Additional Information', readonly=True, states={'draft': [('readonly', False)]})


Class that inherit account.invoice:


class InterestNote(models.Model):
_inherit = 'account.invoice'
_translate = True

interest_paid = fields.Boolean(string='Are interests paid?', help='Boolean to see if interests are already paid.',
default=False)

@api.multi
def interest_note(self):
_logger.info(f'ID FAKTURY: {self.id}')
_logger.info(f'NUMER FAKTURY: {self.number}')
_logger.info(f'PARTNER: {self.partner_id}')
_logger.info(f'COMMENT: {self.comment}')
_logger.info(f'COMMENT TYP: {type(self.comment)}')
com = self.comment
_logger.info(f'COM: {com}')
_logger.info(f'COM TYPE: {type(com)}')

return {
'name': 'Create new interest note',
'type': 'ir.actions.act_window',
'view_type': 'form',
'view_mode': 'form',
'res_model': 'account.invoice',
'context': {'t_comment': com},
'target': 'new'
}

@api.model
def get_comment_test(self):
_logger.info('FUNKCJA TEST ROZPOCZYNA SIĘ')
test_com = self.env.context.get('t_comment')
_logger.info(f'TEST COM: {test_com}')
_logger.info(f'TEST COM TYPE: {type(test_com)}')

if not test_com:
test_com = ''

return test_com

comment = fields.Text('Additional Information', default=get_comment_test,
readonly=False, states={'draft': [('readonly', False)]})

Method interest_note works fine, it is callable but button and when I clicked it, the log messages appear normally. What should happen, is after clicking the button, new windows to create an invoice pop up, with filled comment field, after the invoice from which the button was clicked. When I write default='get_comment_test' it also do not work. So the get_comment_test method, but with default. Logger message from this method never showed up on logs. Funny thing is that in Odoo 11 I've done something almost identical and it worked. Only in 12 I have this issue. Anyone have an idea what is going on?

Avatar
Discard

Here is an example (odoo v12):

Call new form and set three fields by default value (order_id, partner_id, order_line)

return {

'name': _('Check'),

'view_type': 'form',

'view_mode': 'form',

'res_model': 'check.purchase.order.lines',

'views': [

(form_view.id, 'form'),

],

'type': 'ir.actions.act_window',

'target': 'current',

'context': {

'default_order_id': self.id,

'default_partner_id': self.partner_id.id,

'default_order_line': self.get_unverified_record_ids()

},

Standard construction: write 'context': {'default_field_name': value}

In your example: write 'context': {'default_t_comment': com}

Author

@Gleb I have no idea what are you writing. It has no connection to invoices... I tried to convert it to my example and it didn't work anyway. My question is simple really. Why it do not work? Show me error in my code, and write correction. Not entire examples, which are not connected to my issue and solving nothing.

Author Best Answer

@Zbik

Hello. I tried adding view_id, because everything else I have the same (without target) and still it doesn't work. What I found interesting, is that on one field, my default works just fine. In comment it doesn't.


class InterestNote(models.Model):
_inherit = 'account.invoice'
_translate = True

interest_paid = fields.Boolean(string='Are interests paid?', help='Boolean to see if interests are already paid.',
default=False)
# tester = fields.Text(string='TEST', default=get_comment_test)

@api.multi
def interest_note(self):
_logger.info(f'ID FAKTURY: {self.id}')
_logger.info(f'NUMER FAKTURY: {self.number}')
_logger.info(f'PARTNER: {self.partner_id}')
_logger.info(f'COMMENT: {self.comment}')
_logger.info(f'COMMENT TYP: {type(self.comment)}')
com = self.comment
# ctx = dict(
# type='out_invoice',
# comment=com
# )
_logger.info(f'COM: {com}')
_logger.info(f'COM TYPE: {type(com)}')

# inv_obj = self.env['account.invoice']
# inv = inv_obj.create(ctx)
# _logger.info(f'ID FAKTURY INV: {inv.id}')
# return {'type': 'ir.actions.act_window_close'}

return {
'name': 'Create new interest note',
'type': 'ir.actions.act_window',
'view_type': 'form',
'view_mode': 'form',
'view_id': self.env.ref('account.invoice_form').id,
'res_model': 'account.invoice',
'context': {
'type': 'out_invoice',
'default_type': 'out_invoice',
'comment': com,
'default_comment': com
},
'target': 'current'
}

@api.model
def get_comment_test(self):
_logger.info('FUNKCJA TEST ROZPOCZYNA SIĘ')
test_com = self.env.context.get('comment')
_logger.info(f'TEST COM: {test_com}')
_logger.info(f'TEST COM TYPE: {type(test_com)}')

if not test_com:
test_com = '456'

return test_com

comment = fields.Text('Additional Information', default=get_comment_test,
readonly=True, states={'draft': [('readonly', False)]})
tester = fields.Text(string='TEST', default=get_comment_test)

Field comment is still without any values when new invoice, but field tester works. Why is that? It's so confusing right now, I really do not understand why in one field something works, but in another it doesn't.

Avatar
Discard

Steps:

1. Your module set comment - for example: comment = "XYZ"

2. Now is started method in module sale

3. Field invoice comment is set in module sale - in this case comment = self.env.user.company_id.sale_note

4. sale_note is porobably False => in effect comment = False

Author

What? My module is inheriting an "account.invoice", and field comment is also in this module. What I'm trying to do is to create an invoice from within another invoice, and pass a field to newly created one. It has nothing to do with sale. And I already told my issue few times, I really don't know why people on this forum so often act like they don't know the question, but they give an answer anyway. My methods works. Method get_comment_test works. In field tester everything seems fine, default method get_comment_test work. This do not work only in comment. default for comment do not work, where change in readonly is working, so only this default on this particular field cause problems. Does anyone know why, and how to solve it?

If you have the module sale installed and field "Default Terms & Conditions" in sale config is checked, value in field comment depends on it. Too bad you do not understand. Insert "LOREM IPSUM" in Default Terms & Conditions and see on effect.

Best Answer

Your problem is probably related to something else.
In module sele field comment is redefined too.
See:
def _default_comment(self):
invoice_type = self.env.context.get('type', 'out_invoice')
if invoice_type == 'out_invoice' and self.env['ir.config_parameter'].sudo().get_param('sale.use_sale_note'):
return self.env.user.company_id.sale_note

comment = fields.Text(default=_default_comment)

If your module not depends on sale then result in this field may be different than you expect .

PS. It is easier to use (if sale not inherit, without redefinition the commment field):  

return {
  'name': 'Create new interest note',
  'type': 'ir.actions.act_window',
  'view_type': 'form',
  'view_mode': 'form',
  'view_id': self.env.ref('account.invoice_form').id,
  'res_model': 'account.invoice',
  'context': {'type':'out_invoice', 'default_type': 'out_invoice', 'default_comment': com },
  'target': 'new'
}


Avatar
Discard