mirror of
https://github.com/OCA/account-financial-tools.git
synced 2025-02-02 12:47:26 +02:00
pre-commit
This commit is contained in:
@@ -2,22 +2,23 @@
|
||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
||||
|
||||
from odoo import _, api, fields, models
|
||||
from odoo.addons import decimal_precision as dp
|
||||
from odoo.exceptions import UserError
|
||||
|
||||
from odoo.addons import decimal_precision as dp
|
||||
|
||||
|
||||
class AccountInvoiceSpreadLine(models.Model):
|
||||
_name = 'account.spread.line'
|
||||
_description = 'Account Spread Lines'
|
||||
_order = 'date'
|
||||
_name = "account.spread.line"
|
||||
_description = "Account Spread Lines"
|
||||
_order = "date"
|
||||
|
||||
name = fields.Char('Description', readonly=True)
|
||||
amount = fields.Float(digits=dp.get_precision('Account'), required=True)
|
||||
name = fields.Char("Description", readonly=True)
|
||||
amount = fields.Float(digits=dp.get_precision("Account"), required=True)
|
||||
date = fields.Date(required=True)
|
||||
spread_id = fields.Many2one(
|
||||
'account.spread', string='Spread Board', ondelete='cascade')
|
||||
move_id = fields.Many2one(
|
||||
'account.move', string='Journal Entry', readonly=True)
|
||||
"account.spread", string="Spread Board", ondelete="cascade"
|
||||
)
|
||||
move_id = fields.Many2one("account.move", string="Journal Entry", readonly=True)
|
||||
|
||||
@api.multi
|
||||
def create_and_reconcile_moves(self):
|
||||
@@ -25,19 +26,19 @@ class AccountInvoiceSpreadLine(models.Model):
|
||||
for spread_line in self:
|
||||
spread = spread_line.spread_id
|
||||
spread_line_list = grouped_lines.get(
|
||||
spread, self.env['account.spread.line'])
|
||||
grouped_lines.update({
|
||||
spread: spread_line_list + spread_line
|
||||
})
|
||||
spread, self.env["account.spread.line"]
|
||||
)
|
||||
grouped_lines.update({spread: spread_line_list + spread_line})
|
||||
for spread in grouped_lines:
|
||||
created_moves = grouped_lines[spread]._create_moves()
|
||||
|
||||
if created_moves:
|
||||
post_msg = _("Created move(s) ")
|
||||
post_msg += ", ".join(
|
||||
'<a href=# data-oe-model=account.move data-oe-id=%d'
|
||||
'>%s</a>' % (move.id, move.name)
|
||||
for move in created_moves)
|
||||
"<a href=# data-oe-model=account.move data-oe-id=%d"
|
||||
">%s</a>" % (move.id, move.name)
|
||||
for move in created_moves
|
||||
)
|
||||
spread.message_post(body=post_msg)
|
||||
|
||||
spread._reconcile_spread_moves(created_moves)
|
||||
@@ -62,15 +63,19 @@ class AccountInvoiceSpreadLine(models.Model):
|
||||
@api.multi
|
||||
def _create_moves(self):
|
||||
if self.filtered(lambda l: l.move_id):
|
||||
raise UserError(_('This spread line is already linked to a '
|
||||
'journal entry! Please post or delete it.'))
|
||||
raise UserError(
|
||||
_(
|
||||
"This spread line is already linked to a "
|
||||
"journal entry! Please post or delete it."
|
||||
)
|
||||
)
|
||||
|
||||
created_moves = self.env['account.move']
|
||||
created_moves = self.env["account.move"]
|
||||
for line in self:
|
||||
move_vals = line._prepare_move()
|
||||
move = self.env['account.move'].create(move_vals)
|
||||
move = self.env["account.move"].create(move_vals)
|
||||
|
||||
line.write({'move_id': move.id})
|
||||
line.write({"move_id": move.id})
|
||||
created_moves += move
|
||||
return created_moves
|
||||
|
||||
@@ -78,7 +83,7 @@ class AccountInvoiceSpreadLine(models.Model):
|
||||
def _prepare_move(self):
|
||||
self.ensure_one()
|
||||
|
||||
spread_date = self.env.context.get('spread_date') or self.date
|
||||
spread_date = self.env.context.get("spread_date") or self.date
|
||||
spread = self.spread_id
|
||||
analytic = spread.account_analytic_id
|
||||
analytic_tags = [(4, tag.id, None) for tag in spread.analytic_tag_ids]
|
||||
@@ -87,37 +92,49 @@ class AccountInvoiceSpreadLine(models.Model):
|
||||
current_currency = spread.currency_id
|
||||
not_same_curr = company_currency != current_currency
|
||||
amount = current_currency._convert(
|
||||
self.amount, company_currency, spread.company_id, spread_date)
|
||||
self.amount, company_currency, spread.company_id, spread_date
|
||||
)
|
||||
|
||||
line_ids = [(0, 0, {
|
||||
'name': spread.name.split('\n')[0][:64],
|
||||
'account_id': spread.debit_account_id.id,
|
||||
'debit': amount if amount > 0.0 else 0.0,
|
||||
'credit': -amount if amount < 0.0 else 0.0,
|
||||
'partner_id': self.spread_id.invoice_id.partner_id.id,
|
||||
'analytic_account_id': analytic.id,
|
||||
'analytic_tag_ids': analytic_tags,
|
||||
'currency_id': not_same_curr and current_currency.id or False,
|
||||
'amount_currency': not_same_curr and - 1.0 * self.amount or 0.0,
|
||||
}), (0, 0, {
|
||||
'name': spread.name.split('\n')[0][:64],
|
||||
'account_id': spread.credit_account_id.id,
|
||||
'credit': amount if amount > 0.0 else 0.0,
|
||||
'debit': -amount if amount < 0.0 else 0.0,
|
||||
'partner_id': self.spread_id.invoice_id.partner_id.id,
|
||||
'analytic_account_id': analytic.id,
|
||||
'analytic_tag_ids': analytic_tags,
|
||||
'currency_id': not_same_curr and current_currency.id or False,
|
||||
'amount_currency': not_same_curr and self.amount or 0.0,
|
||||
})]
|
||||
line_ids = [
|
||||
(
|
||||
0,
|
||||
0,
|
||||
{
|
||||
"name": spread.name.split("\n")[0][:64],
|
||||
"account_id": spread.debit_account_id.id,
|
||||
"debit": amount if amount > 0.0 else 0.0,
|
||||
"credit": -amount if amount < 0.0 else 0.0,
|
||||
"partner_id": self.spread_id.invoice_id.partner_id.id,
|
||||
"analytic_account_id": analytic.id,
|
||||
"analytic_tag_ids": analytic_tags,
|
||||
"currency_id": not_same_curr and current_currency.id or False,
|
||||
"amount_currency": not_same_curr and -1.0 * self.amount or 0.0,
|
||||
},
|
||||
),
|
||||
(
|
||||
0,
|
||||
0,
|
||||
{
|
||||
"name": spread.name.split("\n")[0][:64],
|
||||
"account_id": spread.credit_account_id.id,
|
||||
"credit": amount if amount > 0.0 else 0.0,
|
||||
"debit": -amount if amount < 0.0 else 0.0,
|
||||
"partner_id": self.spread_id.invoice_id.partner_id.id,
|
||||
"analytic_account_id": analytic.id,
|
||||
"analytic_tag_ids": analytic_tags,
|
||||
"currency_id": not_same_curr and current_currency.id or False,
|
||||
"amount_currency": not_same_curr and self.amount or 0.0,
|
||||
},
|
||||
),
|
||||
]
|
||||
|
||||
return {
|
||||
'name': self.name or "/",
|
||||
'ref': self.name,
|
||||
'date': spread_date,
|
||||
'journal_id': spread.journal_id.id,
|
||||
'line_ids': line_ids,
|
||||
'company_id': spread.company_id.id,
|
||||
"name": self.name or "/",
|
||||
"ref": self.name,
|
||||
"date": spread_date,
|
||||
"journal_id": spread.journal_id.id,
|
||||
"line_ids": line_ids,
|
||||
"company_id": spread.company_id.id,
|
||||
}
|
||||
|
||||
@api.multi
|
||||
@@ -127,13 +144,13 @@ class AccountInvoiceSpreadLine(models.Model):
|
||||
"""
|
||||
self.ensure_one()
|
||||
return {
|
||||
'name': _("Journal Entry"),
|
||||
'view_type': 'form',
|
||||
'view_mode': 'form',
|
||||
'res_model': 'account.move',
|
||||
'view_id': False,
|
||||
'type': 'ir.actions.act_window',
|
||||
'res_id': self.move_id.id,
|
||||
"name": _("Journal Entry"),
|
||||
"view_type": "form",
|
||||
"view_mode": "form",
|
||||
"res_model": "account.move",
|
||||
"view_id": False,
|
||||
"type": "ir.actions.act_window",
|
||||
"res_id": self.move_id.id,
|
||||
}
|
||||
|
||||
@api.multi
|
||||
@@ -143,7 +160,7 @@ class AccountInvoiceSpreadLine(models.Model):
|
||||
"""
|
||||
for line in self:
|
||||
move = line.move_id
|
||||
if move.state == 'posted':
|
||||
if move.state == "posted":
|
||||
move.button_cancel()
|
||||
move.line_ids.remove_move_reconcile()
|
||||
post_msg = _("Deleted move %s") % line.move_id.id
|
||||
@@ -156,18 +173,21 @@ class AccountInvoiceSpreadLine(models.Model):
|
||||
"""Find spread line entries where date is in the past and
|
||||
create moves for them. Method also called by the cron job.
|
||||
"""
|
||||
lines = self.search([
|
||||
('date', '<=', fields.Date.today()),
|
||||
('move_id', '=', False)
|
||||
])
|
||||
lines = self.search(
|
||||
[("date", "<=", fields.Date.today()), ("move_id", "=", False)]
|
||||
)
|
||||
lines.create_and_reconcile_moves()
|
||||
|
||||
unposted_moves = self.search([('move_id', '!=', False)]).mapped(
|
||||
'move_id').filtered(lambda m: m.state != 'posted')
|
||||
unposted_moves.filtered(
|
||||
lambda m: m.company_id.force_move_auto_post).post()
|
||||
unposted_moves = (
|
||||
self.search([("move_id", "!=", False)])
|
||||
.mapped("move_id")
|
||||
.filtered(lambda m: m.state != "posted")
|
||||
)
|
||||
unposted_moves.filtered(lambda m: m.company_id.force_move_auto_post).post()
|
||||
|
||||
spreads_to_archive = self.env['account.spread'].search([
|
||||
('all_posted', '=', True)
|
||||
]).filtered(lambda s: s.company_id.auto_archive)
|
||||
spreads_to_archive.write({'active': False})
|
||||
spreads_to_archive = (
|
||||
self.env["account.spread"]
|
||||
.search([("all_posted", "=", True)])
|
||||
.filtered(lambda s: s.company_id.auto_archive)
|
||||
)
|
||||
spreads_to_archive.write({"active": False})
|
||||
|
||||
Reference in New Issue
Block a user