Skip to content

Commit

Permalink
[MIG] rma_account: Migration to v17
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikul-OSI authored and AaronHForgeFlow committed May 6, 2024
1 parent 3163521 commit 534d701
Show file tree
Hide file tree
Showing 18 changed files with 112 additions and 146 deletions.
2 changes: 1 addition & 1 deletion rma_account/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{
"name": "RMA Account",
"version": "16.0.1.0.0",
"version": "17.0.1.0.0",
"license": "LGPL-3",
"category": "RMA",
"summary": "Integrates RMA with Invoice Processing",
Expand Down
18 changes: 8 additions & 10 deletions rma_account/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ class AccountMove(models.Model):
@api.depends("line_ids.rma_line_ids")
def _compute_used_in_rma_count(self):
for inv in self:
rmas = self.mapped("line_ids.rma_line_ids")
rmas = inv.mapped("line_ids.rma_line_ids")
inv.used_in_rma_count = len(rmas)

@api.depends("line_ids.rma_line_id")
def _compute_rma_count(self):
for inv in self:
rmas = self.mapped("line_ids.rma_line_id")
rmas = inv.mapped("line_ids.rma_line_id")
inv.rma_count = len(rmas)

def _prepare_invoice_line_from_rma_line(self, rma_line):
Expand Down Expand Up @@ -45,15 +45,12 @@ def _prepare_invoice_line_from_rma_line(self, rma_line):

def _post_process_invoice_line_from_rma_line(self, new_line, rma_line):
new_line.rma_line_id = rma_line
new_line.name = "%s: %s" % (
self.add_rma_line_id.name,
new_line.name,
)
new_line.name = f"{self.add_rma_line_id.name}: {new_line.name}"
new_line.account_id = new_line.account_id
return True

@api.onchange("add_rma_line_id")
def on_change_add_rma_line_id(self):
def onchange_add_rma_line_id(self):
if not self.add_rma_line_id:
return {}
if not self.partner_id:
Expand Down Expand Up @@ -123,20 +120,21 @@ def _stock_account_prepare_anglo_saxon_out_lines_vals(self):
):
current_move = self.browse(line.get("move_id", False))
current_rma = current_move.invoice_line_ids.filtered(
lambda x: x.rma_line_id and x.product_id.id == product.id
lambda x, product=product: x.rma_line_id
and x.product_id.id == product.id
).mapped("rma_line_id")
if len(current_rma) == 1:
line.update({"rma_line_id": current_rma.id})
elif len(current_rma) > 1:
find_with_label_rma = current_rma.filtered(
lambda x: x.name == line.get("name")
lambda x, line=line: x.name == line.get("name")
)
if len(find_with_label_rma) == 1:
line.update({"rma_line_id": find_with_label_rma.id})
return res

def _stock_account_get_last_step_stock_moves(self):
rslt = super(AccountMove, self)._stock_account_get_last_step_stock_moves()
rslt = super()._stock_account_get_last_step_stock_moves()
for invoice in self.filtered(lambda x: x.move_type == "out_invoice"):
rslt += invoice.mapped("line_ids.rma_line_id.move_ids").filtered(
lambda x: x.state == "done" and x.location_dest_id.usage == "customer"
Expand Down
83 changes: 32 additions & 51 deletions rma_account/models/account_move_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,62 +8,45 @@ class AccountMoveLine(models.Model):
_inherit = "account.move.line"

@api.model
def name_search(self, name, args=None, operator="ilike", limit=100):
def _name_search(self, name, domain=None, operator="ilike", limit=None, order=None):
"""Allows to search by Invoice number. This has to be done this way,
as Odoo adds extra args to name_search on _name_search method that
will make impossible to get the desired result."""
if not args:
args = []
lines = self.search([("move_id.name", operator, name)] + args, limit=limit)
res = lines.name_get()
domain = domain or []
lines = self.search([("move_id.name", operator, name)] + domain, limit=limit)
if limit:
limit_rest = limit - len(lines)
else:
# limit can be 0 or None representing infinite
limit_rest = limit
if limit_rest or not limit:
args += [("id", "not in", lines.ids)]
res += super(AccountMoveLine, self).name_search(
name, args=args, operator=operator, limit=limit_rest
domain += [("id", "not in", lines.ids)]
return super()._name_search(
name, domain=domain, operator=operator, limit=limit_rest, order=order
)
return res
return self._search(domain, limit=limit, order=order)

def name_get(self):
res = []
if self.env.context.get("rma"):
for inv in self:
if inv.move_id.ref:
res.append(
(
inv.id,
"INV:%s | REF:%s | ORIG:%s | PART:%s | QTY:%s"
% (
inv.move_id.name or "",
inv.move_id.invoice_origin or "",
inv.move_id.ref or "",
inv.product_id.name,
inv.quantity,
),
)
)
elif inv.move_id.name:
res.append(
(
inv.id,
"INV:%s | ORIG:%s | PART:%s | QTY:%s"
% (
inv.move_id.name or "",
inv.move_id.invoice_origin or "",
inv.product_id.name,
inv.quantity,
),
)
)
else:
res.append(super(AccountMoveLine, inv).name_get()[0])
return res
else:
return super(AccountMoveLine, self).name_get()
def _compute_display_name(self):
if not self.env.context.get("rma"):
return super()._compute_display_name()
for inv in self:
if inv.move_id.ref:
name = "INV:{} | REF:{} | ORIG:{} | PART:{} | QTY:{}".format(
inv.move_id.name or "",
inv.move_id.invoice_origin or "",
inv.move_id.ref or "",
inv.product_id.name,
inv.quantity,
)
inv.display_name = name
elif inv.move_id.name:
name = "INV:{} | ORIG:{} | PART:{} | QTY:{}".format(
inv.move_id.name or "",
inv.move_id.invoice_origin or "",
inv.product_id.name,
inv.quantity,
)
inv.display_name = name

def _compute_used_in_rma_count(self):
for invl in self:
Expand Down Expand Up @@ -96,19 +79,17 @@ def _compute_rma_count(self):

def _stock_account_get_anglo_saxon_price_unit(self):
self.ensure_one()
price_unit = super(
AccountMoveLine, self
)._stock_account_get_anglo_saxon_price_unit()
price_unit = super()._stock_account_get_anglo_saxon_price_unit()
rma_line = self.rma_line_id or self.env["rma.order.line"]
if rma_line:
is_line_reversing = bool(self.move_id.reversed_entry_id)
qty_to_refund = self.product_uom_id._compute_quantity(
self.quantity, self.product_id.uom_id
)
posted_invoice_lines = rma_line.move_line_ids.filtered(
lambda l: l.move_id.move_type == "out_refund"
and l.move_id.state == "posted"
and bool(l.move_id.reversed_entry_id) == is_line_reversing
lambda line: line.move_id.move_type == "out_refund"
and line.move_id.state == "posted"
and bool(line.move_id.reversed_entry_id) == is_line_reversing
)
qty_refunded = sum(
x.product_uom_id._compute_quantity(x.quantity, x.product_id.uom_id)
Expand Down
3 changes: 1 addition & 2 deletions rma_account/models/rma_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,4 @@ def _compute_domain_valid_journal(self):
@api.onchange("automated_refund")
def _onchange_automated_refund(self):
for rec in self:
if rec.automated_refund:
rec.refund_free_of_charge = True
rec.refund_free_of_charge = rec.automated_refund
6 changes: 3 additions & 3 deletions rma_account/models/rma_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def _prepare_rma_line_from_inv_line(self, line):
return data

@api.onchange("add_move_id")
def on_change_invoice(self):
def onchange_invoice(self):
if not self.add_move_id:
return {}
if not self.partner_id:
Expand All @@ -84,13 +84,13 @@ def on_change_invoice(self):

@api.model
def prepare_rma_line(self, origin_rma, rma_id, line):
line_values = super(RmaOrder, self).prepare_rma_line(origin_rma, rma_id, line)
line_values = super().prepare_rma_line(origin_rma, rma_id, line)
line_values["invoice_address_id"] = line.invoice_address_id.id
return line_values

@api.model
def _prepare_rma_data(self, partner, origin_rma):
res = super(RmaOrder, self)._prepare_rma_data(partner, origin_rma)
res = super()._prepare_rma_data(partner, origin_rma)
res["invoice_address_id"] = partner.id
return res

Expand Down
44 changes: 18 additions & 26 deletions rma_account/models/rma_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ def _compute_refund_count(self):
"res.partner",
string="Partner invoice address",
default=lambda self: self._default_invoice_address(),
readonly=True,
states={"draft": [("readonly", False)]},
help="Invoice address for current rma order.",
)
refund_count = fields.Integer(
Expand All @@ -65,8 +63,6 @@ def _compute_refund_count(self):
string="Originating Invoice Line",
ondelete="restrict",
index=True,
readonly=True,
states={"draft": [("readonly", False)]},
)
move_line_ids = fields.One2many(
comodel_name="account.move.line",
Expand Down Expand Up @@ -138,7 +134,7 @@ def _compute_commercial_partner_id(self):
@api.onchange("product_id", "partner_id")
def _onchange_product_id(self):
"""Domain for sale_line_id is computed here to make it dynamic."""
res = super(RmaOrderLine, self)._onchange_product_id()
res = super()._onchange_product_id()
if not res.get("domain"):
res["domain"] = {}
if not self.product_id:
Expand Down Expand Up @@ -252,14 +248,14 @@ def _check_invoice_partner(self):
)

def _remove_other_data_origin(self, exception):
res = super(RmaOrderLine, self)._remove_other_data_origin(exception)
res = super()._remove_other_data_origin(exception)
if not exception == "account_move_line_id":
self.account_move_line_id = False
return res

@api.onchange("operation_id")
def _onchange_operation_id(self):
result = super(RmaOrderLine, self)._onchange_operation_id()
result = super()._onchange_operation_id()
if self.operation_id:
self.refund_policy = self.operation_id.refund_policy or "no"
return result
Expand All @@ -273,7 +269,8 @@ def _check_duplicated_lines(self):
if len(matching_inv_lines) > 1:
raise UserError(
_(
"There's an rma for the invoice line %(arg1)s and invoice %(arg2)s",
"There's an rma for the "
"invoice line %(arg1)s and invoice %(arg2)s",
arg1=line.account_move_line_id,
arg2=line.account_move_line_id.move_id,
)
Expand Down Expand Up @@ -305,20 +302,11 @@ def action_view_refunds(self):
"views": [(tree_view_ref.id, "tree"), (form_view_ref.id, "form")],
}

def name_get(self):
res = []
if self.env.context.get("rma"):
for rma in self:
res.append(
(
rma.id,
"%s %s qty:%s"
% (rma.name, rma.product_id.name, rma.product_qty),
)
)
return res
else:
return super(RmaOrderLine, self).name_get()
def _compute_display_name(self):
if not self.env.context.get("rma"):
return super()._compute_display_name()
for rma in self:
rma.display_name = f"{rma.name} {rma.product_id.name} qty:{rma.product_qty}"

def _stock_account_anglo_saxon_reconcile_valuation(self):
for rma in self:
Expand All @@ -345,9 +333,13 @@ def _stock_account_anglo_saxon_reconcile_valuation(self):
amls |= rma.move_ids.mapped(
"stock_valuation_layer_ids.account_move_id.line_ids"
)
# Search for anglo-saxon lines linked to the product in the journal entry.
# Search for anglo-saxon lines linked to the product in the journal
# entry.
amls = amls.filtered(
lambda line: line.product_id == prod
lambda line,
prod=prod,
product_interim_account=product_interim_account: line.product_id
== prod
and line.account_id == product_interim_account
and not line.reconciled
)
Expand All @@ -356,7 +348,7 @@ def _stock_account_anglo_saxon_reconcile_valuation(self):

def _get_price_unit(self):
self.ensure_one()
price_unit = super(RmaOrderLine, self)._get_price_unit()
price_unit = super()._get_price_unit()
if self.reference_move_id:
move = self.reference_move_id
layers = move.sudo().stock_valuation_layer_ids
Expand Down Expand Up @@ -402,6 +394,6 @@ def _check_refund_zero_cost(self):
"""
# For some reason api.depends on qty_received is not working. Using the
# _account_entry_move method in stock move as trigger then
for rec in self.filtered(lambda l: l.operation_id.automated_refund):
for rec in self.filtered(lambda line: line.operation_id.automated_refund):
if rec.qty_received > rec.qty_refunded:
rec._refund_at_zero_cost()
4 changes: 2 additions & 2 deletions rma_account/models/stock_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class StockMove(models.Model):
def _prepare_account_move_line(
self, qty, cost, credit_account_id, debit_account_id, svl_id, description
):
res = super(StockMove, self)._prepare_account_move_line(
res = super()._prepare_account_move_line(
qty, cost, credit_account_id, debit_account_id, svl_id, description
)
for line in res:
Expand All @@ -23,7 +23,7 @@ def _prepare_account_move_line(
return res

def _account_entry_move(self, qty, description, svl_id, cost):
res = super(StockMove, self)._account_entry_move(qty, description, svl_id, cost)
res = super()._account_entry_move(qty, description, svl_id, cost)
if self.company_id.anglo_saxon_accounting:
# Eventually reconcile together the invoice and valuation accounting
# entries on the stock interim accounts
Expand Down
2 changes: 1 addition & 1 deletion rma_account/models/stock_valuation_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class StockValuationLayer(models.Model):
_inherit = "stock.valuation.layer"

def _validate_accounting_entries(self):
res = super(StockValuationLayer, self)._validate_accounting_entries()
res = super()._validate_accounting_entries()
for svl in self:
# Eventually reconcile together the stock interim accounts
if svl.company_id.anglo_saxon_accounting:
Expand Down
3 changes: 2 additions & 1 deletion rma_account/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)

from . import test_rma_account
from . import test_rma_stock_account

# from . import test_rma_stock_account
from . import test_account_move_line_rma_order_line
7 changes: 3 additions & 4 deletions rma_account/tests/test_account_move_line_rma_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class TestAccountMoveLineRmaOrderLine(common.TransactionCase):
@classmethod
def setUpClass(cls):
super(TestAccountMoveLineRmaOrderLine, cls).setUpClass()
super().setUpClass()
cls.rma_model = cls.env["rma.order"]
cls.rma_line_model = cls.env["rma.order.line"]
cls.rma_refund_wiz = cls.env["rma.refund"]
Expand Down Expand Up @@ -207,8 +207,7 @@ def _check_account_balance(self, account_id, rma_line=None, expected_balance=0.0
self.assertEqual(
balance,
expected_balance,
"Balance is not %s for rma Line %s."
% (str(expected_balance), rma_line.name),
f"Balance is not {str(expected_balance)} for rma Line {rma_line.name}.",
)

def test_rma_invoice(self):
Expand Down Expand Up @@ -245,7 +244,7 @@ def test_rma_invoice(self):
else:
picking_ids = self.env["stock.picking"].search(res["domain"])
picking = self.env["stock.picking"].browse(picking_ids)
picking.move_line_ids.write({"qty_done": 1.0})
# picking.move_line_ids.write({"qty_done": 1.0})
picking.button_validate()
# decreasing cogs
expected_balance = -1.0
Expand Down
2 changes: 1 addition & 1 deletion rma_account/tests/test_rma_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class TestRmaAccount(common.SingleTransactionCase):
@classmethod
def setUpClass(cls):
super(TestRmaAccount, cls).setUpClass()
super().setUpClass()

cls.rma_obj = cls.env["rma.order"]
cls.rma_line_obj = cls.env["rma.order.line"]
Expand Down
Loading

0 comments on commit 534d701

Please sign in to comment.