-1

I added commission field in purchase order model and i want to show it in account invoice when i click on "create bill". below is my code but it doesn't work, I hope somebody can help me. Many thanks in advance.

class ConfirmComm(models.Model):
  _inherit = "purchase.order"
  commission = fields.Float(string='Commission', required='true', default=0)

      @api.multi
      def action_view_invoice(self, cr, uid, order, context=None):
        if context is None:
          context = {}
        journal_id = self.pool['account.invoice'].default_get(cr, uid, ['journal_id'], context=context)['journal_id']
        if not journal_id:
          raise UserError(_('Error!'),
                          _('Please define purchase journal for this company: "%s" (id:%d).') % (
                            order.company_id.name, order.company_id.id))
        invoice_vals = {
          'name': order.partner_ref or '',
          'origin': order.name,
          # 'type': 'in_invoice',
          # Sale order id as source_id
          # 'source_id': order.id,
          'reference': order.partner_ref or order.name,
          'account_id': order.partner_invoice_id.property_account_receivable.id,
          'partner_id': order.partner_invoice_id.id,
          'journal_id': journal_id,
          'commission': order.commission,
          # 'invoice_line': [(6, 0, lines)],
          'currency_id': order.pricelist_id.currency_id.id,
          # 'comment': order.note,
          'payment_term_id': order.payment_term_id or False,
          'fiscal_position_id': order.fiscal_position_id,
          'date_invoice': context.get('date_invoice', False),
          'company_id': order.company_id.id,
          'user_id': order.user_id and order.user_id.id or False,
        }
        _logger.info("KTR this is commissionTest $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ %d", order.commission)
        invoice_vals.update(self._inv_get(cr, uid, order, context=context))
        return invoice_vals
CZoellner
  • 13,553
  • 3
  • 25
  • 38
myriam
  • 15
  • 4

1 Answers1

0

You need to add this field to model account.invoice, too, because there is no direct relation between purchase.order and account.invoice to use a related field or something fancy. To see that field in the invoice form view, you have to add it there, too (as usual).

The rest of your code should be okay, because setting the value by using in it purchase.order.action_view_invoice() would be the next important part, but you've already done that ;-)

CZoellner
  • 13,553
  • 3
  • 25
  • 38
  • i already added it in account_invoice. The problem that odoo still execute the action_view_invoice of purchase.py. Ithink that action_view_invoice is not a method to create invoice but i didin't find the function for doing that. – myriam Sep 19 '19 at 14:30
  • there is how i added the commission field in account.invoice model: _inherit = 'account.invoice' commission = fields.Float(string='Commission', required='true', default=0) – myriam Sep 19 '19 at 14:33
  • You should add the Odoo Version tag to your question, because it has changed a lot in the last few versions. – CZoellner Sep 19 '19 at 15:12
  • in tags odoo 12 does not exist – myriam Sep 19 '19 at 15:14
  • IIRC the process of creating purchase invoices is fully on the `account.invoice` model, [here](https://github.com/odoo/odoo/blob/35d4a8ccce8f3a34701d231e17a324cbbf5abfab/addons/purchase/models/account_invoice.py#L89-L117) the magic is happening. – CZoellner Sep 19 '19 at 15:19
  • can you explain to me please, in this example what i will do? – myriam Sep 19 '19 at 15:28
  • i was resolved the problem, I overrided the "action_view_invoice" and just added in the context the created field : 'default_commission': self.commission – myriam Sep 20 '19 at 07:54
  • Yes that can be one solution ;-) – CZoellner Sep 20 '19 at 07:57