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