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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
# -*- coding: utf-8 -*-
#############################################################################
#
# Cybrosys Technologies Pvt. Ltd.
#
# Copyright (C) 2019-TODAY Cybrosys Technologies(<https://www.cybrosys.com>)
# Author: Cybrosys Techno Solutions(<https://www.cybrosys.com>)
#
# You can modify it under the terms of the GNU LESSER
# GENERAL PUBLIC LICENSE (LGPL v3), Version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU LESSER GENERAL PUBLIC LICENSE (LGPL v3) for more details.
#
# You should have received a copy of the GNU LESSER GENERAL PUBLIC LICENSE
# (LGPL v3) along with this program.
# If not, see <http://www.gnu.org/licenses/>.
#
#############################################################################
from odoo import models, fields, api
from odoo.exceptions import UserError
from odoo.tools.translate import _
class ResPartner(models.Model):
_inherit = 'res.partner'
warning_stage = fields.Float(string='Warning Amount',
help="A warning message will appear once the "
"selected customer is crossed warning "
"amount. Set its value to 0.00 to"
" disable this feature")
blocking_stage = fields.Float(string='Blocking Amount',
help="Cannot make sales once the selected "
"customer is crossed blocking amount."
"Set its value to 0.00 to disable "
"this feature")
due_amount = fields.Float(string="Total Sale",
compute="compute_due_amount")
active_limit = fields.Boolean("Active Credit Limit", default=False)
enable_credit_limit = fields.Boolean(string="Credit Limit Enabled",
compute="_compute_enable_credit_limit")
def compute_due_amount(self):
for rec in self:
if not rec.id:
continue
rec.due_amount = rec.credit - rec.debit
def _compute_enable_credit_limit(self):
""" Check credit limit is enabled in account settings """
params = self.env['ir.config_parameter'].sudo()
customer_credit_limit = params.get_param('customer_credit_limit',
default=False)
for rec in self:
rec.enable_credit_limit = True if customer_credit_limit else False
@api.constrains('warning_stage', 'blocking_stage')
def constrains_warning_stage(self):
if self.active_limit and self.enable_credit_limit:
if self.warning_stage >= self.blocking_stage:
if self.blocking_stage > 0:
raise UserError(_(
"Warning amount should be less than Blocking amount"))
class SaleOrder(models.Model):
_inherit = 'sale.order'
def _get_outstanding_amount(self):
for rec in self:
if not rec.id:
continue
all_order_amount = 0
paid_invoice_amount = 0
outstanding_amount = 0
all_order_ids = self.env['sale.order'].search([
('state','in',['sale','done']),
('partner_id','=',rec.partner_id.id),
('id', '!=', rec.id)
])
paid_invoice_ids = self.env['account.move'].search([
('payment_state','=','paid'),
('partner_id','=',rec.partner_id.id),
('move_type','=','out_invoice')
])
if all_order_ids:
for order in all_order_ids:
all_order_amount += order.amount_total
if paid_invoice_ids:
for invoice in paid_invoice_ids:
paid_invoice_amount += invoice.amount_total
outstanding_amount = all_order_amount - paid_invoice_amount
rec.outstanding_amount = outstanding_amount
has_due = fields.Boolean()
is_warning = fields.Boolean()
due_amount = fields.Float(related='partner_id.due_amount')
outstanding_amount = fields.Float(compute="_get_outstanding_amount", string="Outstanding Amount")
def compute_due_amount(self):
for rec in self:
if not rec.id:
continue
rec.due_amount = rec.credit - rec.debit
def _action_confirm(self):
for rec in self:
"""To check the selected customers due amount is exceed than
blocking stage"""
all_order_ids = []
paid_invoice_ids = []
all_order_amount = 0
paid_invoice_amount = 0
outstanding_amount = 0
all_order_ids = self.env['sale.order'].search([
('state', 'in', ['sale','done']),
('partner_id', '=', rec.partner_id.id),
('id', '!=', rec.id)
])
paid_invoice_ids = self.env['account.move'].search([
('payment_state','=','paid'),
('partner_id','=',rec.partner_id.id),
('move_type','=','out_invoice')
])
if all_order_ids:
for order in all_order_ids:
print ("SO ", order.name)
all_order_amount += order.amount_total
print ("All order amount ", all_order_amount)
if paid_invoice_ids:
for invoice in paid_invoice_ids:
paid_invoice_amount += invoice.amount_total
print ("Paid invoice amount ", paid_invoice_amount)
outstanding_amount = all_order_amount - paid_invoice_amount
######
if rec.partner_id.active_limit \
and rec.partner_id.enable_credit_limit:
if (outstanding_amount + rec.amount_total) >= rec.partner_id.blocking_stage:
if rec.partner_id.blocking_stage != 0:
remaining_credit_limit = rec.partner_id.blocking_stage - outstanding_amount
raise UserError(_("%s is in Blocking Stage, Remaining credit limit is %s %s") % (rec.partner_id.name, rec.currency_id.symbol, remaining_credit_limit))
return super(SaleOrder, self)._action_confirm()
@api.onchange('partner_id')
def check_due(self):
"""To show the due amount and warning stage"""
if self.partner_id and self.partner_id.due_amount > 0 \
and self.partner_id.active_limit \
and self.partner_id.enable_credit_limit:
self.has_due = True
else:
self.has_due = False
if self.partner_id and self.partner_id.active_limit\
and self.partner_id.enable_credit_limit:
if self.outstanding_amount >= self.partner_id.warning_stage:
if self.partner_id.warning_stage != 0:
self.is_warning = True
else:
self.is_warning = False
class AccountMove(models.Model):
_inherit = 'account.move'
has_due = fields.Boolean()
is_warning = fields.Boolean()
due_amount = fields.Float(related='partner_id.due_amount')
outstanding_amount = fields.Float(compute="_get_outstanding_amount", string="Outstanding Amount")
def _get_outstanding_amount(self):
for rec in self:
if not rec.id:
continue
all_order_amount = 0
paid_invoice_amount = 0
outstanding_amount = 0
all_order_ids = self.env['sale.order'].search([
('state','in',['sale','done']),
('partner_id','=',rec.partner_id.id)
])
paid_invoice_ids = self.env['account.move'].search([
('payment_state','=','paid'),
('partner_id','=',rec.partner_id.id),
('move_type','=','out_invoice')
])
if all_order_ids:
for order in all_order_ids:
all_order_amount += order.amount_total
if paid_invoice_ids:
for invoice in paid_invoice_ids:
paid_invoice_amount += invoice.amount_total
outstanding_amount = all_order_amount - paid_invoice_amount
rec.outstanding_amount = outstanding_amount
def action_post(self):
"""To check the selected customers due amount is exceed than
blocking stage"""
pay_type = ['out_invoice', 'out_refund', 'out_receipt']
for rec in self:
if rec.partner_id.active_limit and rec.move_type in pay_type \
and rec.partner_id.enable_credit_limit:
if rec.outstanding_amount >= rec.partner_id.blocking_stage:
if rec.partner_id.blocking_stage != 0:
raise UserError(_(
"%s is in Blocking Stage and "
"has a due amount of %s %s to pay") % (
rec.partner_id.name, rec.due_amount,
rec.currency_id.symbol))
return super(AccountMove, self).action_post()
@api.onchange('partner_id')
def check_due(self):
"""To show the due amount and warning stage"""
if self.partner_id and self.partner_id.due_amount > 0 \
and self.partner_id.active_limit \
and self.partner_id.enable_credit_limit:
self.has_due = True
else:
self.has_due = False
if self.partner_id and self.partner_id.active_limit \
and self.partner_id.enable_credit_limit:
if self.outstanding_amount >= self.partner_id.warning_stage:
if self.partner_id.warning_stage != 0:
self.is_warning = True
else:
self.is_warning = False
|