forked from OCA/donation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdonation.py
146 lines (136 loc) · 5.78 KB
/
donation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# Copyright 2015-2021 Akretion France (http://www.akretion.com/)
# @author: Alexis de Lattre <alexis.delattre@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import _, api, fields, models
from odoo.exceptions import UserError
class DonationDonation(models.Model):
_inherit = "donation.donation"
mandate_id = fields.Many2one(
"account.banking.mandate",
string="Mandate",
states={"done": [("readonly", True)]},
tracking=True,
check_company=True,
ondelete="restrict",
domain="[('state', '=', 'valid'), "
"('partner_id', '=', commercial_partner_id), "
"('company_id', '=', company_id)]",
)
mandate_required = fields.Boolean(
related="payment_mode_id.payment_method_id.mandate_required", readonly=True
)
@api.onchange("payment_mode_id")
def donation_partner_direct_debit_change(self):
if (
self.partner_id
and self.payment_mode_id
and self.payment_mode_id.payment_method_id.mandate_required
and not self.mandate_id
):
mandate = self.env["account.banking.mandate"].search(
[
("state", "=", "valid"),
("partner_id", "=", self.commercial_partner_id.id),
],
limit=1,
)
if mandate:
self.mandate_id = mandate
def _prepare_donation_move(self):
vals = super()._prepare_donation_move()
vals.update(
{
"mandate_id": self.mandate_id.id or False,
"payment_mode_id": self.payment_mode_id.id,
}
)
return vals
def _prepare_payment_order(self):
self.ensure_one()
vals = {"payment_mode_id": self.payment_mode_id.id}
return vals
def _prepare_counterpart_move_line(
self, total_company_cur, total_currency, journal
):
vals = super()._prepare_counterpart_move_line(
total_company_cur, total_currency, journal
)
journal = self.payment_mode_id.fixed_journal_id
if not self.bank_statement_line_id and self.payment_mode_id.payment_order_ok:
if not journal.donation_debit_order_account_id:
raise UserError(
_("Missing Donation by Debit Order Account on journal '%s'.")
% journal.display_name
)
vals["account_id"] = journal.donation_debit_order_account_id.id
return vals
def validate(self):
"""Create Direct debit payment order on donation validation or update
an existing draft Direct Debit pay order"""
res = super().validate()
apoo = self.env["account.payment.order"].sudo()
for donation in self:
if (
donation.payment_mode_id
and donation.payment_mode_id.payment_type == "inbound"
and donation.payment_mode_id.payment_order_ok
and donation.move_id
):
payorders = apoo.search(
[
("state", "=", "draft"),
("company_id", "=", donation.company_id.id),
("payment_mode_id", "=", donation.payment_mode_id.id),
]
)
msg = False
if payorders:
payorder = payorders[0]
else:
payorder_vals = donation._prepare_payment_order()
payorder = apoo.create(payorder_vals)
msg = _(
"A new draft direct debit order "
"<a href=# data-oe-model=account.payment.order "
"data-oe-id=%d>%s</a> has been automatically created"
) % (payorder.id, payorder.name)
# add payment line
match_account_id = (
donation.payment_mode_id.fixed_journal_id.donation_debit_order_account_id.id
)
for mline in donation.move_id.line_ids:
if mline.account_id.id == match_account_id:
mline.sudo().create_payment_line_from_move_line(payorder)
break
if not msg:
msg = _(
"A new payment line has been automatically added "
"to the existing draft direct debit order "
"<a href=# data-oe-model=account.payment.order "
"data-oe-id=%d>%s</a>."
) % (payorder.id, payorder.name)
donation.message_post(body=msg)
return res
def done2cancel(self):
for donation in self:
if donation.move_id:
donation_mv_line_ids = [line.id for line in donation.move_id.line_ids]
if donation_mv_line_ids:
plines = self.env["account.payment.line"].search(
[
("move_line_id", "in", donation_mv_line_ids),
("company_id", "=", donation.company_id.id),
("state", "in", ("draft", "open")),
]
)
if plines:
raise UserError(
_(
"You cannot cancel a donation "
"which is linked to a payment line in a "
"direct debit order. Remove it from the "
"following direct debit order: %s."
)
% plines[0].order_id.display_name
)
return super().done2cancel()