/
res_company_interest.py
242 lines (210 loc) · 8.63 KB
/
res_company_interest.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
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
##############################################################################
# For copyright and license notices, see __manifest__.py file in module root
# directory
##############################################################################
from odoo import models, fields, api, _
from odoo.tools.safe_eval import safe_eval
from dateutil.relativedelta import relativedelta
import logging
_logger = logging.getLogger(__name__)
class ResCompanyInterest(models.Model):
_name = 'res.company.interest'
_description = 'Account Interest'
company_id = fields.Many2one(
'res.company',
'Company',
required=True,
ondelete='cascade',
)
receivable_account_ids = fields.Many2many(
'account.account',
string='Cuentas a Cobrar',
help='Cuentas a Cobrar que se tendrán en cuenta para evaular la deuda',
required=True,
domain=lambda self: [('user_type_id.type', '=', 'receivable'),
('company_id', '=', self._context.get('default_company_id') or self.env.company.id)],
)
interest_product_id = fields.Many2one(
'product.product',
'Interest Product',
required=True,
)
analytic_account_id = fields.Many2one(
'account.analytic.account',
'Analytic account',
)
rate = fields.Float(
'Interest',
required=True,
digits=(7, 4)
)
automatic_validation = fields.Boolean(
'Automatic Validation?',
help='Automatic Invoice Validation?',
default=True,
)
rule_type = fields.Selection([
('daily', 'Day(s)'),
('weekly', 'Week(s)'),
('monthly', 'Month(s)'),
('yearly', 'Year(s)'),
],
'Recurrency',
help="Interests Invoice automatically repeat at specified interval",
default='monthly',
)
interval = fields.Integer(
'Repeat Every',
default=1,
help="Repeat every (Days/Week/Month/Year)"
)
tolerance_interval = fields.Integer(
'Tolerance',
default=1,
help="Number of periods of tolerance for dues. 0 = no tolerance"
)
next_date = fields.Date(
'Date of Next Invoice',
default=fields.Date.today,
)
domain = fields.Char(
'Additional Filters',
default="[]",
help="Extra filters that will be added to the standard search"
)
has_domain = fields.Boolean(compute="_compute_has_domain")
@api.model
def _cron_recurring_interests_invoices(self):
_logger.info('Running Interest Invoices Cron Job')
current_date = fields.Date.today()
self.search([('next_date', '<=', current_date)]).create_interest_invoices()
def create_interest_invoices(self):
for rec in self:
_logger.info(
'Creating Interest Invoices (id: %s, company: %s)', rec.id,
rec.company_id.name)
interests_date = rec.next_date
rule_type = rec.rule_type
interval = rec.interval
tolerance_interval = rec.tolerance_interval
if rule_type == 'daily':
next_delta = relativedelta(days=+interval)
tolerance_delta = relativedelta(days=+tolerance_interval)
elif rule_type == 'weekly':
next_delta = relativedelta(weeks=+interval)
tolerance_delta = relativedelta(weeks=+tolerance_interval)
elif rule_type == 'monthly':
next_delta = relativedelta(months=+interval)
tolerance_delta = relativedelta(months=+tolerance_interval)
else:
next_delta = relativedelta(years=+interval)
tolerance_delta = relativedelta(years=+tolerance_interval)
# buscamos solo facturas que vencieron
# antes de hoy menos un periodo
# TODO ver si queremos que tambien se calcule interes proporcional
# para lo que vencio en este ultimo periodo
to_date = interests_date - tolerance_delta
from_date = to_date - tolerance_delta
rec.with_context(default_l10n_ar_afip_asoc_period_start=from_date,
default_l10n_ar_afip_asoc_period_end=to_date).create_invoices(to_date)
# seteamos proxima corrida en hoy mas un periodo
rec.next_date = interests_date + next_delta
def _get_move_line_domains(self, to_date):
self.ensure_one()
move_line_domain = [
('account_id', 'in', self.receivable_account_ids.ids),
('full_reconcile_id', '=', False),
('date_maturity', '<', to_date),
('partner_id.active', '=', True),
]
return move_line_domain
def create_invoices(self, to_date, groupby='partner_id'):
self.ensure_one()
journal = self.env['account.journal'].search([
('type', '=', 'sale'),
('company_id', '=', self.company_id.id)], limit=1)
move_line_domain = self._get_move_line_domains(to_date)
# Check if a filter is set
if self.domain:
move_line_domain += safe_eval(self.domain)
fields = ['id', 'amount_residual', 'partner_id', 'account_id']
if groupby not in fields:
fields += [groupby]
move_line = self.env['account.move.line']
grouped_lines = move_line.read_group(
domain=move_line_domain,
fields=fields,
groupby=[groupby],
)
self = self.with_context(
company_id=self.company_id.id,
mail_notrack=True,
prefetch_fields=False).with_company(self.company_id)
total_items = len(grouped_lines)
_logger.info('%s interest invoices will be generated', total_items)
for idx, line in enumerate(grouped_lines):
debt = line['amount_residual']
if not debt or debt <= 0.0:
_logger.info("Debt is negative, skipping...")
continue
_logger.info(
'Creating Interest Invoice (%s of %s) with values:\n%s',
idx + 1, total_items, line)
partner_id = line[groupby][0]
partner = self.env['res.partner'].browse(partner_id)
move_vals = self._prepare_interest_invoice(
partner, debt, to_date, journal)
# We send document type for compatibility with argentinian invoices
move = self.env['account.move'].with_context(
internal_type='debit_note').create(move_vals)
if self.automatic_validation:
try:
move.action_post()
except Exception as e:
_logger.error(
"Something went wrong creating "
"interests invoice: {}".format(e))
def prepare_info(self, to_date, debt):
self.ensure_one()
# Format date to customer language
lang_code = self.env.context.get('lang', self.env.user.lang)
lang = self.env['res.lang']._lang_get(lang_code)
date_format = lang.date_format
to_date_format = to_date.strftime(date_format)
res = _(
'Deuda Vencida al %s: %s\n'
'Tasa de interés: %s') % (
to_date_format, debt, self.rate)
return res
def _prepare_interest_invoice(self, partner, debt, to_date, journal):
self.ensure_one()
comment = self.prepare_info(to_date, debt)
fpos = partner.property_account_position_id
taxes = self.interest_product_id.taxes_id.filtered(
lambda r: r.company_id == self.company_id)
tax_id = fpos.map_tax(taxes)
invoice_vals = {
'move_type': 'out_invoice',
'currency_id': self.company_id.currency_id.id,
'partner_id': partner.id,
'fiscal_position_id': fpos.id,
'user_id': partner.user_id.id or False,
'company_id': self.company_id.id,
'journal_id': journal.id,
'invoice_origin': "Interests Invoice",
'narration': self.interest_product_id.name + '.\n' + comment,
'invoice_line_ids': [(0, 0, {
"product_id": self.interest_product_id.id,
"quantity": 1.0,
"price_unit": self.rate * debt,
"partner_id": partner.id,
"name": self.interest_product_id.name + '.\n' + comment,
"analytic_account_id": self.analytic_account_id.id,
"tax_ids": [(6, 0, tax_id.ids)]
})],
}
return invoice_vals
@api.depends('domain')
def _compute_has_domain(self):
for rec in self:
rec.has_domain = len(safe_eval(rec.domain)) > 0