invoice_merge.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # -*- coding: utf-8 -*-
  2. # © 2010-2011 Ian Li <ian.li@elico-corp.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from openerp import api, fields, models, _
  5. from openerp.exceptions import Warning as UserError
  6. class InvoiceMerge(models.TransientModel):
  7. _name = "invoice.merge"
  8. _description = "Merge Partner Invoice"
  9. keep_references = fields.Boolean(
  10. string='Keep references from original invoices', default=True)
  11. date_invoice = fields.Date('Invoice Date')
  12. @api.model
  13. def _dirty_check(self):
  14. if self._context.get('active_model') == 'account.invoice':
  15. ids = self._context['active_ids']
  16. if len(ids) < 2:
  17. raise UserError(
  18. _('Please select multiple invoice to merge in the list '
  19. 'view.'))
  20. invs = self.env['account.invoice'].browse(ids)
  21. for i, inv in enumerate(invs):
  22. if not inv._get_draft_invoices():
  23. raise UserError(
  24. _('At least one of the selected invoices is %s!') %
  25. inv.state)
  26. if i > 0:
  27. if inv.account_id != invs[0].account_id:
  28. raise UserError(
  29. _('Not all invoices use the same account!'))
  30. if inv.company_id != invs[0].company_id:
  31. raise UserError(
  32. _('Not all invoices are at the same company!'))
  33. if inv.partner_id != invs[0].partner_id:
  34. raise UserError(
  35. _('Not all invoices are for the same partner!'))
  36. if inv.type != invs[0].type:
  37. raise UserError(
  38. _('Not all invoices are of the same type!'))
  39. if inv.currency_id != invs[0].currency_id:
  40. raise UserError(
  41. _('Not all invoices are at the same currency!'))
  42. if inv.journal_id != invs[0].journal_id:
  43. raise UserError(
  44. _('Not all invoices are at the same journal!'))
  45. if inv.partner_bank_id != invs[0].partner_bank_id:
  46. raise UserError(
  47. _('Not all invoices have the same '
  48. 'Partner Bank Account!'))
  49. return {}
  50. @api.model
  51. def fields_view_get(self, view_id=None, view_type='form', toolbar=False,
  52. submenu=False):
  53. """Changes the view dynamically
  54. @param self: The object pointer.
  55. @param cr: A database cursor
  56. @param uid: ID of the user currently logged in
  57. @param context: A standard dictionary
  58. @return: New arch of view.
  59. """
  60. res = super(InvoiceMerge, self).fields_view_get(
  61. view_id=view_id, view_type=view_type, toolbar=toolbar,
  62. submenu=False)
  63. self._dirty_check()
  64. return res
  65. @api.multi
  66. def merge_invoices(self):
  67. """To merge similar type of account invoices.
  68. @param self: The object pointer.
  69. @param cr: A database cursor
  70. @param uid: ID of the user currently logged in
  71. @param ids: the ID or list of IDs
  72. @param context: A standard dictionary
  73. @return: account invoice action
  74. """
  75. aw_obj = self.env['ir.actions.act_window']
  76. ids = self._context.get('active_ids', [])
  77. invoices = self.env['account.invoice'].browse(ids)
  78. allinvoices = invoices.do_merge(keep_references=self.keep_references,
  79. date_invoice=self.date_invoice)[0]
  80. xid = {
  81. 'out_invoice': 'action_invoice_tree1',
  82. 'out_refund': 'action_invoice_tree3',
  83. 'in_invoice': 'action_invoice_tree2',
  84. 'in_refund': 'action_invoice_tree4',
  85. }[invoices[0].type]
  86. action = aw_obj.for_xml_id('account', xid)
  87. action.update({
  88. 'domain': [('id', 'in', list(ids) + allinvoices.keys())],
  89. })
  90. return action