forked from OCA/sale-workflow
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsale_advance_payment_wzd.py
More file actions
167 lines (153 loc) · 5.83 KB
/
sale_advance_payment_wzd.py
File metadata and controls
167 lines (153 loc) · 5.83 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# Copyright 2017 Omar Castiñeira, Comunitea Servicios Tecnológicos S.L.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import _, api, exceptions, fields, models
from odoo.exceptions import UserError
from odoo.tools import float_compare
class AccountVoucherWizard(models.TransientModel):
_name = "account.voucher.wizard"
_description = "Account Voucher Wizard"
order_id = fields.Many2one("sale.order", required=True)
journal_id = fields.Many2one(
"account.journal",
"Journal",
required=True,
domain=[("type", "in", ("bank", "cash"))],
)
journal_currency_id = fields.Many2one(
"res.currency",
"Journal Currency",
store=True,
readonly=False,
compute="_compute_get_journal_currency",
)
currency_id = fields.Many2one("res.currency", "Currency")
amount_total = fields.Monetary()
amount_advance = fields.Monetary(
"Amount advanced", required=True, currency_field="journal_currency_id"
)
date = fields.Date(required=True, default=fields.Date.context_today)
currency_amount = fields.Monetary(
"Curr. amount",
currency_field="currency_id",
compute="_compute_currency_amount",
store=True,
)
payment_ref = fields.Char("Ref.")
payment_type = fields.Selection(
[("inbound", "Inbound"), ("outbound", "Outbound")],
default="inbound",
required=True,
)
@api.depends("journal_id")
def _compute_get_journal_currency(self):
for wzd in self:
wzd.journal_currency_id = (
wzd.journal_id.currency_id.id
or wzd.journal_id.company_id.currency_id.id
)
@api.constrains("amount_advance")
def check_amount(self):
if self.amount_advance <= 0:
raise exceptions.ValidationError(_("Amount of advance must be positive."))
if self.env.context.get("active_id", False):
if self.payment_type == "inbound":
if (
float_compare(
self.currency_amount,
self.order_id.amount_residual,
precision_digits=2,
)
> 0
):
raise exceptions.ValidationError(
_(
"Inbound amount of advance is greater than residual "
"amount on sale"
)
)
else:
paid_in_advanced = self.order_id.amount_total - self.amount_total
if (
float_compare(
self.currency_amount,
paid_in_advanced,
precision_digits=2,
)
> 0
):
raise exceptions.ValidationError(
_(
"Outbound amount of advance is greater than the "
"advanced paid amount"
)
)
@api.model
def default_get(self, fields_list):
res = super().default_get(fields_list)
sale_ids = self.env.context.get("active_ids", [])
if not sale_ids:
return res
sale_id = fields.first(sale_ids)
sale = self.env["sale.order"].browse(sale_id)
if "amount_total" in fields_list:
res.update(
{
"order_id": sale.id,
"amount_total": sale.amount_residual,
"currency_id": sale.pricelist_id.currency_id.id
or sale.currency_id.id,
}
)
return res
@api.depends("journal_id", "date", "amount_advance")
def _compute_currency_amount(self):
for wzd in self:
if wzd.journal_currency_id != wzd.currency_id:
amount_advance = wzd.journal_currency_id._convert(
wzd.amount_advance,
wzd.currency_id,
wzd.order_id.company_id,
wzd.date or fields.Date.today(),
)
else:
amount_advance = wzd.amount_advance
wzd.currency_amount = amount_advance
def _prepare_payment_vals(self, sale):
partner_id = sale.partner_invoice_id.commercial_partner_id.id
if self.amount_advance < 0.0:
raise UserError(
_(
"The amount to advance must always be positive. "
"Please use the payment type to indicate if this "
"is an inbound or an outbound payment."
)
)
return {
"date": self.date,
"amount": self.amount_advance,
"payment_type": self.payment_type,
"partner_type": "customer",
"memo": self.payment_ref or sale.name,
"journal_id": self.journal_id.id,
"currency_id": self.journal_currency_id.id,
"partner_id": partner_id,
"payment_method_id": self.env.ref(
"account.account_payment_method_manual_in"
).id,
}
def make_advance_payment(self):
"""Create customer paylines and validates the payment"""
self.ensure_one()
payment_obj = self.env["account.payment"]
sale_obj = self.env["sale.order"]
sale_ids = self.env.context.get("active_ids", [])
if sale_ids:
sale_id = fields.first(sale_ids)
sale = sale_obj.browse(sale_id)
payment_vals = self._prepare_payment_vals(sale)
payment = payment_obj.create(payment_vals)
sale.account_payment_ids |= payment
payment.action_post()
return {
"type": "ir.actions.act_window_close",
}