account_move.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import api, fields, models
  4. from datetime import date
  5. class AccountMove(models.Model):
  6. _inherit = 'account.move'
  7. def button_draft(self):
  8. # OVERRIDE to update the cancel date.
  9. res = super(AccountMove, self).button_draft()
  10. for move in self:
  11. if move.move_type == 'out_invoice':
  12. self.env['membership.membership_line'].search([
  13. ('account_invoice_line', 'in', move.mapped('invoice_line_ids').ids)
  14. ]).write({'date_cancel': False})
  15. return res
  16. def button_cancel(self):
  17. # OVERRIDE to update the cancel date.
  18. res = super(AccountMove, self).button_cancel()
  19. for move in self:
  20. if move.move_type == 'out_invoice':
  21. self.env['membership.membership_line'].search([
  22. ('account_invoice_line', 'in', move.mapped('invoice_line_ids').ids)
  23. ]).write({'date_cancel': fields.Date.today()})
  24. return res
  25. def write(self, vals):
  26. # OVERRIDE to write the partner on the membership lines.
  27. res = super(AccountMove, self).write(vals)
  28. if 'partner_id' in vals:
  29. self.env['membership.membership_line'].search([
  30. ('account_invoice_line', 'in', self.mapped('invoice_line_ids').ids)
  31. ]).write({'partner': vals['partner_id']})
  32. return res
  33. class AccountMoveLine(models.Model):
  34. _inherit = 'account.move.line'
  35. def write(self, vals):
  36. # OVERRIDE
  37. res = super(AccountMoveLine, self).write(vals)
  38. to_process = self.filtered(lambda line: line.move_id.move_type == 'out_invoice' and line.product_id.membership)
  39. # Nothing to process, break.
  40. if not to_process:
  41. return res
  42. existing_memberships = self.env['membership.membership_line'].search([
  43. ('account_invoice_line', 'in', to_process.ids)])
  44. to_process = to_process - existing_memberships.mapped('account_invoice_line')
  45. # All memberships already exist, break.
  46. if not to_process:
  47. return res
  48. memberships_vals = []
  49. for line in to_process:
  50. date_from = line.product_id.membership_date_from
  51. date_to = line.product_id.membership_date_to
  52. if (date_from and date_from < (line.move_id.invoice_date or date.min) < (date_to or date.min)):
  53. date_from = line.move_id.invoice_date
  54. memberships_vals.append({
  55. 'partner': line.move_id.partner_id.id,
  56. 'membership_id': line.product_id.id,
  57. 'member_price': line.price_unit,
  58. 'date': fields.Date.today(),
  59. 'date_from': date_from,
  60. 'date_to': date_to,
  61. 'account_invoice_line': line.id,
  62. })
  63. self.env['membership.membership_line'].create(memberships_vals)
  64. return res
  65. @api.model_create_multi
  66. def create(self, vals_list):
  67. # OVERRIDE
  68. lines = super(AccountMoveLine, self).create(vals_list)
  69. to_process = lines.filtered(lambda line: line.move_id.move_type == 'out_invoice' and line.product_id.membership)
  70. # Nothing to process, break.
  71. if not to_process:
  72. return lines
  73. existing_memberships = self.env['membership.membership_line'].search([
  74. ('account_invoice_line', 'in', to_process.ids)])
  75. to_process = to_process - existing_memberships.mapped('account_invoice_line')
  76. # All memberships already exist, break.
  77. if not to_process:
  78. return lines
  79. memberships_vals = []
  80. for line in to_process:
  81. date_from = line.product_id.membership_date_from
  82. date_to = line.product_id.membership_date_to
  83. if (date_from and date_from < (line.move_id.invoice_date or date.min) < (date_to or date.min)):
  84. date_from = line.move_id.invoice_date
  85. memberships_vals.append({
  86. 'partner': line.move_id.partner_id.id,
  87. 'membership_id': line.product_id.id,
  88. 'member_price': line.price_unit,
  89. 'date': fields.Date.today(),
  90. 'date_from': date_from,
  91. 'date_to': date_to,
  92. 'account_invoice_line': line.id,
  93. })
  94. self.env['membership.membership_line'].create(memberships_vals)
  95. return lines