mirror of
https://github.com/OCA/account-financial-tools.git
synced 2025-02-02 12:47:26 +02:00
[13.0][MIG] account_spread_cost_revenue
This commit is contained in:
@@ -1,11 +1,9 @@
|
||||
# Copyright 2016-2019 Onestein (<https://www.onestein.eu>)
|
||||
# Copyright 2016-2020 Onestein (<https://www.onestein.eu>)
|
||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
||||
|
||||
from odoo import _, api, fields, models
|
||||
from odoo.exceptions import UserError
|
||||
|
||||
from odoo.addons import decimal_precision as dp
|
||||
|
||||
|
||||
class AccountInvoiceSpreadLine(models.Model):
|
||||
_name = "account.spread.line"
|
||||
@@ -13,14 +11,11 @@ class AccountInvoiceSpreadLine(models.Model):
|
||||
_order = "date"
|
||||
|
||||
name = fields.Char("Description", readonly=True)
|
||||
amount = fields.Float(digits=dp.get_precision("Account"), required=True)
|
||||
amount = fields.Float(digits="Account", required=True)
|
||||
date = fields.Date(required=True)
|
||||
spread_id = fields.Many2one(
|
||||
"account.spread", string="Spread Board", ondelete="cascade"
|
||||
)
|
||||
spread_id = fields.Many2one("account.spread", ondelete="cascade")
|
||||
move_id = fields.Many2one("account.move", string="Journal Entry", readonly=True)
|
||||
|
||||
@api.multi
|
||||
def create_and_reconcile_moves(self):
|
||||
grouped_lines = {}
|
||||
for spread_line in self:
|
||||
@@ -40,27 +35,15 @@ class AccountInvoiceSpreadLine(models.Model):
|
||||
for move in created_moves
|
||||
)
|
||||
spread.message_post(body=post_msg)
|
||||
if spread.invoice_id.state == "posted":
|
||||
spread._reconcile_spread_moves(created_moves)
|
||||
spread._post_spread_moves(created_moves)
|
||||
|
||||
spread._reconcile_spread_moves(created_moves)
|
||||
self._post_spread_moves(spread, created_moves)
|
||||
|
||||
@api.model
|
||||
def _post_spread_moves(self, spread, moves):
|
||||
if not moves:
|
||||
return
|
||||
if spread.company_id.force_move_auto_post:
|
||||
moves.post()
|
||||
elif spread.move_line_auto_post:
|
||||
moves.post()
|
||||
|
||||
@api.multi
|
||||
def create_move(self):
|
||||
"""Button to manually create a move from a spread line entry.
|
||||
"""
|
||||
"""Button to manually create a move from a spread line entry."""
|
||||
self.ensure_one()
|
||||
self.create_and_reconcile_moves()
|
||||
|
||||
@api.multi
|
||||
def _create_moves(self):
|
||||
if self.filtered(lambda l: l.move_id):
|
||||
raise UserError(
|
||||
@@ -74,19 +57,21 @@ class AccountInvoiceSpreadLine(models.Model):
|
||||
for line in self:
|
||||
move_vals = line._prepare_move()
|
||||
move = self.env["account.move"].create(move_vals)
|
||||
|
||||
line.write({"move_id": move.id})
|
||||
line.move_id = move
|
||||
created_moves += move
|
||||
return created_moves
|
||||
|
||||
@api.multi
|
||||
def _prepare_move(self):
|
||||
self.ensure_one()
|
||||
|
||||
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]
|
||||
analytic_tags = []
|
||||
if self.env["account.analytic.tag"].check_access_rights(
|
||||
"read", raise_exception=False
|
||||
):
|
||||
analytic_tags = [(6, 0, spread.analytic_tag_ids.ids)]
|
||||
|
||||
company_currency = spread.company_id.currency_id
|
||||
current_currency = spread.currency_id
|
||||
@@ -95,13 +80,17 @@ class AccountInvoiceSpreadLine(models.Model):
|
||||
self.amount, company_currency, spread.company_id, spread_date
|
||||
)
|
||||
|
||||
debit_credit = spread.invoice_type in ["in_invoice", "out_refund"]
|
||||
|
||||
line_ids = [
|
||||
(
|
||||
0,
|
||||
0,
|
||||
{
|
||||
"name": spread.name.split("\n")[0][:64],
|
||||
"account_id": spread.debit_account_id.id,
|
||||
"name": spread.name,
|
||||
"account_id": spread.debit_account_id.id
|
||||
if debit_credit
|
||||
else spread.credit_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,
|
||||
@@ -115,8 +104,10 @@ class AccountInvoiceSpreadLine(models.Model):
|
||||
0,
|
||||
0,
|
||||
{
|
||||
"name": spread.name.split("\n")[0][:64],
|
||||
"account_id": spread.credit_account_id.id,
|
||||
"name": spread.name,
|
||||
"account_id": spread.credit_account_id.id
|
||||
if debit_credit
|
||||
else spread.debit_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,
|
||||
@@ -135,17 +126,14 @@ class AccountInvoiceSpreadLine(models.Model):
|
||||
"journal_id": spread.journal_id.id,
|
||||
"line_ids": line_ids,
|
||||
"company_id": spread.company_id.id,
|
||||
"partner_id": spread.invoice_id.partner_id.id,
|
||||
}
|
||||
|
||||
@api.multi
|
||||
def open_move(self):
|
||||
"""Used by a button to manually view a move from a
|
||||
spread line entry.
|
||||
"""
|
||||
"""Used by a button to manually view a move from a spread line entry."""
|
||||
self.ensure_one()
|
||||
return {
|
||||
"name": _("Journal Entry"),
|
||||
"view_type": "form",
|
||||
"view_mode": "form",
|
||||
"res_model": "account.move",
|
||||
"view_id": False,
|
||||
@@ -153,18 +141,15 @@ class AccountInvoiceSpreadLine(models.Model):
|
||||
"res_id": self.move_id.id,
|
||||
}
|
||||
|
||||
@api.multi
|
||||
def unlink_move(self):
|
||||
"""Used by a button to manually unlink a move
|
||||
from a spread line entry.
|
||||
"""
|
||||
"""Used by a button to manually unlink a move from a spread line entry."""
|
||||
for line in self:
|
||||
move = line.move_id
|
||||
if move.state == "posted":
|
||||
move.button_cancel()
|
||||
move.line_ids.remove_move_reconcile()
|
||||
post_msg = _("Deleted move %s") % line.move_id.id
|
||||
move.unlink()
|
||||
move.with_context(force_delete=True).unlink()
|
||||
line.move_id = False
|
||||
line.spread_id.message_post(body=post_msg)
|
||||
|
||||
@@ -188,6 +173,6 @@ class AccountInvoiceSpreadLine(models.Model):
|
||||
spreads_to_archive = (
|
||||
self.env["account.spread"]
|
||||
.search([("all_posted", "=", True)])
|
||||
.filtered(lambda s: s.company_id.auto_archive)
|
||||
.filtered(lambda s: s.company_id.auto_archive_spread)
|
||||
)
|
||||
spreads_to_archive.write({"active": False})
|
||||
|
||||
Reference in New Issue
Block a user