mirror of
https://github.com/ForgeFlow/stock-rma.git
synced 2025-01-21 12:57:49 +02:00
[MIG]rma_account to v13
This commit is contained in:
@@ -4,4 +4,4 @@
|
||||
from . import rma_order
|
||||
from . import rma_order_line
|
||||
from . import rma_operation
|
||||
from . import invoice
|
||||
from . import account_move
|
||||
|
||||
@@ -5,13 +5,13 @@ from odoo import api, fields, models
|
||||
from odoo.tools.float_utils import float_compare
|
||||
|
||||
|
||||
class AccountInvoice(models.Model):
|
||||
_inherit = "account.invoice"
|
||||
class AccountMove(models.Model):
|
||||
_inherit = "account.move"
|
||||
|
||||
@api.depends("invoice_line_ids.rma_line_ids")
|
||||
@api.depends("line_ids.rma_line_ids")
|
||||
def _compute_rma_count(self):
|
||||
for inv in self:
|
||||
rmas = self.mapped("invoice_line_ids.rma_line_ids")
|
||||
rmas = self.mapped("line_ids.rma_line_ids")
|
||||
inv.rma_count = len(rmas)
|
||||
|
||||
def _prepare_invoice_line_from_rma_line(self, line):
|
||||
@@ -19,18 +19,17 @@ class AccountInvoice(models.Model):
|
||||
if float_compare(qty, 0.0, precision_rounding=line.uom_id.rounding) <= 0:
|
||||
qty = 0.0
|
||||
# Todo fill taxes from somewhere
|
||||
invoice_line = self.env["account.invoice.line"]
|
||||
invoice_line = self.env["account.move.line"]
|
||||
data = {
|
||||
"purchase_line_id": line.id,
|
||||
"name": line.name + ": " + line.name,
|
||||
"origin": line.origin,
|
||||
"uom_id": line.uom_id.id,
|
||||
"product_uom_id": line.uom_id.id,
|
||||
"product_id": line.product_id.id,
|
||||
"account_id": invoice_line.with_context(
|
||||
{"journal_id": self.journal_id.id, "type": "in_invoice"}
|
||||
)._default_account(),
|
||||
"price_unit": line.company_id.currency_id.with_context(
|
||||
date=self.date_invoice
|
||||
date=self.date
|
||||
).compute(line.price_unit, self.currency_id, round=False),
|
||||
"quantity": qty,
|
||||
"discount": 0.0,
|
||||
@@ -45,12 +44,12 @@ class AccountInvoice(models.Model):
|
||||
if not self.partner_id:
|
||||
self.partner_id = self.add_rma_line_id.partner_id.id
|
||||
|
||||
new_line = self.env["account.invoice.line"]
|
||||
if self.add_rma_line_id not in (self.invoice_line_ids.mapped("rma_line_id")):
|
||||
new_line = self.env["account.move.line"]
|
||||
if self.add_rma_line_id not in (self.line_ids.mapped("rma_line_id")):
|
||||
data = self._prepare_invoice_line_from_rma_line(self.add_rma_line_id)
|
||||
new_line = new_line.new(data)
|
||||
new_line._set_additional_fields(self)
|
||||
self.invoice_line_ids += new_line
|
||||
self.line_ids += new_line
|
||||
self.add_rma_line_id = False
|
||||
return {}
|
||||
|
||||
@@ -63,11 +62,10 @@ class AccountInvoice(models.Model):
|
||||
help="Create a refund in based on an existing rma_line",
|
||||
)
|
||||
|
||||
@api.multi
|
||||
def action_view_rma_supplier(self):
|
||||
action = self.env.ref("rma.action_rma_supplier_lines")
|
||||
result = action.read()[0]
|
||||
rma_ids = self.mapped("invoice_line_ids.rma_line_ids").ids
|
||||
rma_ids = self.mapped("line_ids.rma_line_ids").ids
|
||||
if rma_ids:
|
||||
# choose the view_mode accordingly
|
||||
if len(rma_ids) > 1:
|
||||
@@ -78,11 +76,10 @@ class AccountInvoice(models.Model):
|
||||
result["res_id"] = rma_ids[0]
|
||||
return result
|
||||
|
||||
@api.multi
|
||||
def action_view_rma_customer(self):
|
||||
action = self.env.ref("rma.action_rma_customer_lines")
|
||||
result = action.read()[0]
|
||||
rma_ids = self.mapped("invoice_line_ids.rma_line_ids").ids
|
||||
rma_ids = self.mapped("line_ids.rma_line_ids").ids
|
||||
if rma_ids:
|
||||
# choose the view_mode accordingly
|
||||
if len(rma_ids) > 1:
|
||||
@@ -94,8 +91,8 @@ class AccountInvoice(models.Model):
|
||||
return result
|
||||
|
||||
|
||||
class AccountInvoiceLine(models.Model):
|
||||
_inherit = "account.invoice.line"
|
||||
class AccountMoveLine(models.Model):
|
||||
_inherit = "account.move.line"
|
||||
|
||||
@api.model
|
||||
def name_search(self, name, args=None, operator="ilike", limit=100):
|
||||
@@ -104,7 +101,7 @@ class AccountInvoiceLine(models.Model):
|
||||
will make impossible to get the desired result."""
|
||||
if not args:
|
||||
args = []
|
||||
lines = self.search([("invoice_id.number", operator, name)] + args, limit=limit)
|
||||
lines = self.search([("move_id.name", operator, name)] + args, limit=limit)
|
||||
res = lines.name_get()
|
||||
if limit:
|
||||
limit_rest = limit - len(lines)
|
||||
@@ -113,50 +110,48 @@ class AccountInvoiceLine(models.Model):
|
||||
limit_rest = limit
|
||||
if limit_rest or not limit:
|
||||
args += [("id", "not in", lines.ids)]
|
||||
res += super(AccountInvoiceLine, self).name_search(
|
||||
res += super(AccountMoveLine, self).name_search(
|
||||
name, args=args, operator=operator, limit=limit_rest
|
||||
)
|
||||
return res
|
||||
|
||||
@api.multi
|
||||
def name_get(self):
|
||||
res = []
|
||||
if self.env.context.get("rma"):
|
||||
for inv in self:
|
||||
if inv.invoice_id.reference:
|
||||
if inv.move_id.ref:
|
||||
res.append(
|
||||
(
|
||||
inv.id,
|
||||
"INV:%s | REF:%s | ORIG:%s | PART:%s | QTY:%s"
|
||||
% (
|
||||
inv.invoice_id.number or "",
|
||||
inv.origin or "",
|
||||
inv.invoice_id.reference or "",
|
||||
inv.move_id.name or "",
|
||||
inv.move_id.invoice_origin or "",
|
||||
inv.move_id.ref or "",
|
||||
inv.product_id.name,
|
||||
inv.quantity,
|
||||
),
|
||||
)
|
||||
)
|
||||
elif inv.invoice_id.number:
|
||||
elif inv.move_id.name:
|
||||
res.append(
|
||||
(
|
||||
inv.id,
|
||||
"INV:%s | ORIG:%s | PART:%s | QTY:%s"
|
||||
% (
|
||||
inv.invoice_id.number or "",
|
||||
inv.origin or "",
|
||||
inv.move_id.name or "",
|
||||
inv.move_id.invoice_origin or "",
|
||||
inv.product_id.name,
|
||||
inv.quantity,
|
||||
),
|
||||
)
|
||||
)
|
||||
else:
|
||||
res.append(super(AccountInvoiceLine, inv).name_get()[0])
|
||||
res.append(super(AccountMoveLine, inv).name_get()[0])
|
||||
return res
|
||||
else:
|
||||
return super(AccountInvoiceLine, self).name_get()
|
||||
return super(AccountMoveLine, self).name_get()
|
||||
|
||||
@api.multi
|
||||
def _compute_rma_count(self):
|
||||
for invl in self:
|
||||
rma_lines = invl.mapped("rma_line_ids")
|
||||
@@ -165,7 +160,7 @@ class AccountInvoiceLine(models.Model):
|
||||
rma_line_count = fields.Integer(compute="_compute_rma_count", string="# of RMA")
|
||||
rma_line_ids = fields.One2many(
|
||||
comodel_name="rma.order.line",
|
||||
inverse_name="invoice_line_id",
|
||||
inverse_name="account_move_line_id",
|
||||
string="RMA",
|
||||
readonly=True,
|
||||
help="This will contain the RMA lines for the invoice line",
|
||||
@@ -7,20 +7,18 @@ from odoo import api, fields, models
|
||||
class RmaOrder(models.Model):
|
||||
_inherit = "rma.order"
|
||||
|
||||
@api.multi
|
||||
def _compute_invoice_refund_count(self):
|
||||
for rec in self:
|
||||
invoices = rec.mapped("rma_line_ids.refund_line_ids.invoice_id")
|
||||
invoices = rec.mapped("rma_line_ids.refund_line_ids.move_id")
|
||||
rec.invoice_refund_count = len(invoices)
|
||||
|
||||
@api.multi
|
||||
def _compute_invoice_count(self):
|
||||
for rec in self:
|
||||
invoices = rec.mapped("rma_line_ids.invoice_id")
|
||||
invoices = rec.mapped("rma_line_ids.move_id")
|
||||
rec.invoice_count = len(invoices)
|
||||
|
||||
add_invoice_id = fields.Many2one(
|
||||
comodel_name="account.invoice",
|
||||
add_move_id = fields.Many2one(
|
||||
comodel_name="account.move",
|
||||
string="Add Invoice",
|
||||
ondelete="set null",
|
||||
readonly=True,
|
||||
@@ -44,30 +42,30 @@ class RmaOrder(models.Model):
|
||||
or self.rma_line_ids.product_id.categ_id.rma_supplier_operation_id
|
||||
)
|
||||
data = {
|
||||
"invoice_line_id": line.id,
|
||||
"account_move_line_id": line.id,
|
||||
"product_id": line.product_id.id,
|
||||
"name": line.name,
|
||||
"origin": line.invoice_id.number,
|
||||
"uom_id": line.uom_id.id,
|
||||
"origin": line.move_id.name,
|
||||
"uom_id": line.product_uom_id.id,
|
||||
"operation_id": operation,
|
||||
"product_qty": line.quantity,
|
||||
"price_unit": line.invoice_id.currency_id.compute(
|
||||
"price_unit": line.move_id.currency_id.compute(
|
||||
line.price_unit, line.currency_id, round=False
|
||||
),
|
||||
"rma_id": self.id,
|
||||
}
|
||||
return data
|
||||
|
||||
@api.onchange("add_invoice_id")
|
||||
@api.onchange("add_move_id")
|
||||
def on_change_invoice(self):
|
||||
if not self.add_invoice_id:
|
||||
if not self.add_move_id:
|
||||
return {}
|
||||
if not self.partner_id:
|
||||
self.partner_id = self.add_invoice_id.partner_id.id
|
||||
self.partner_id = self.add_move_id.partner_id.id
|
||||
new_lines = self.env["rma.order.line"]
|
||||
for line in self.add_invoice_id.invoice_line_ids:
|
||||
for line in self.add_move_id.line_ids:
|
||||
# Load a PO line only once
|
||||
if line in self.rma_line_ids.mapped("invoice_line_id"):
|
||||
if line in self.rma_line_ids.mapped("account_move_line_id"):
|
||||
continue
|
||||
data = self._prepare_rma_line_from_inv_line(line)
|
||||
new_line = new_lines.new(data)
|
||||
@@ -75,9 +73,9 @@ class RmaOrder(models.Model):
|
||||
|
||||
self.rma_line_ids += new_lines
|
||||
self.date_rma = fields.Datetime.now()
|
||||
self.delivery_address_id = self.add_invoice_id.partner_id.id
|
||||
self.invoice_address_id = self.add_invoice_id.partner_id.id
|
||||
self.add_invoice_id = False
|
||||
self.delivery_address_id = self.add_move_id.partner_id.id
|
||||
self.invoice_address_id = self.add_move_id.partner_id.id
|
||||
self.add_move_id = False
|
||||
return {}
|
||||
|
||||
@api.model
|
||||
@@ -92,36 +90,34 @@ class RmaOrder(models.Model):
|
||||
res["invoice_address_id"] = partner.id
|
||||
return res
|
||||
|
||||
@api.multi
|
||||
def action_view_invoice_refund(self):
|
||||
action = self.env.ref("account.action_invoice_tree2")
|
||||
result = action.read()[0]
|
||||
invoice_ids = self.mapped("rma_line_ids.refund_line_ids.invoice_id").ids
|
||||
if invoice_ids:
|
||||
move_ids = self.mapped("rma_line_ids.refund_line_ids.move_id").ids
|
||||
if move_ids:
|
||||
# choose the view_mode accordingly
|
||||
if len(invoice_ids) > 1:
|
||||
result["domain"] = [("id", "in", invoice_ids)]
|
||||
if len(move_ids) > 1:
|
||||
result["domain"] = [("id", "in", move_ids)]
|
||||
else:
|
||||
res = self.env.ref("account.invoice_supplier_form", False)
|
||||
res = self.env.ref("account.move_supplier_form", False)
|
||||
result["views"] = [(res and res.id or False, "form")]
|
||||
result["res_id"] = invoice_ids[0]
|
||||
result["res_id"] = move_ids[0]
|
||||
return result
|
||||
|
||||
@api.multi
|
||||
def action_view_invoice(self):
|
||||
if self.type == "supplier":
|
||||
action = self.env.ref("account.action_invoice_tree2")
|
||||
res = self.env.ref("account.invoice_supplier_form", False)
|
||||
res = self.env.ref("account.move_supplier_form", False)
|
||||
else:
|
||||
action = self.env.ref("account.action_invoice_tree")
|
||||
res = self.env.ref("account.invoice_form", False)
|
||||
res = self.env.ref("account.view_move_form", False)
|
||||
result = action.read()[0]
|
||||
invoice_ids = self.mapped("rma_line_ids.invoice_id").ids
|
||||
if invoice_ids:
|
||||
move_ids = self.mapped("rma_line_ids.move_id").ids
|
||||
if move_ids:
|
||||
# choose the view_mode accordingly
|
||||
if len(invoice_ids) > 1:
|
||||
result["domain"] = [("id", "in", invoice_ids)]
|
||||
if len(move_ids) > 1:
|
||||
result["domain"] = [("id", "in", move_ids)]
|
||||
else:
|
||||
result["views"] = [(res and res.id or False, "form")]
|
||||
result["res_id"] = invoice_ids[0]
|
||||
result["res_id"] = move_ids[0]
|
||||
return result
|
||||
|
||||
@@ -17,21 +17,20 @@ class RmaOrderLine(models.Model):
|
||||
return self.env["res.partner"].browse(partner_id)
|
||||
return self.env["res.partner"]
|
||||
|
||||
@api.multi
|
||||
@api.depends(
|
||||
"refund_line_ids", "refund_line_ids.invoice_id.state", "refund_policy", "type"
|
||||
"refund_line_ids", "refund_line_ids.move_id.state", "refund_policy", "type"
|
||||
)
|
||||
def _compute_qty_refunded(self):
|
||||
for rec in self:
|
||||
rec.qty_refunded = sum(
|
||||
rec.refund_line_ids.filtered(
|
||||
lambda i: i.invoice_id.state in ("open", "paid")
|
||||
lambda i: i.move_id.state in ("open", "paid")
|
||||
).mapped("quantity")
|
||||
)
|
||||
|
||||
@api.depends(
|
||||
"refund_line_ids",
|
||||
"refund_line_ids.invoice_id.state",
|
||||
"refund_line_ids.move_id.state",
|
||||
"refund_policy",
|
||||
"move_ids",
|
||||
"move_ids.state",
|
||||
@@ -48,10 +47,9 @@ class RmaOrderLine(models.Model):
|
||||
qty = res.qty_delivered - res.qty_refunded
|
||||
res.qty_to_refund = qty
|
||||
|
||||
@api.multi
|
||||
def _compute_refund_count(self):
|
||||
for rec in self:
|
||||
rec.refund_count = len(rec.refund_line_ids.mapped("invoice_id"))
|
||||
rec.refund_count = len(rec.refund_line_ids.mapped("move_id"))
|
||||
|
||||
invoice_address_id = fields.Many2one(
|
||||
"res.partner",
|
||||
@@ -64,8 +62,8 @@ class RmaOrderLine(models.Model):
|
||||
refund_count = fields.Integer(
|
||||
compute="_compute_refund_count", string="# of Refunds", default=0
|
||||
)
|
||||
invoice_line_id = fields.Many2one(
|
||||
comodel_name="account.invoice.line",
|
||||
account_move_line_id = fields.Many2one(
|
||||
comodel_name="account.move.line",
|
||||
string="Originating Invoice Line",
|
||||
ondelete="restrict",
|
||||
index=True,
|
||||
@@ -73,17 +71,17 @@ class RmaOrderLine(models.Model):
|
||||
states={"draft": [("readonly", False)]},
|
||||
)
|
||||
refund_line_ids = fields.One2many(
|
||||
comodel_name="account.invoice.line",
|
||||
comodel_name="account.move.line",
|
||||
inverse_name="rma_line_id",
|
||||
string="Refund Lines",
|
||||
copy=False,
|
||||
index=True,
|
||||
readonly=True,
|
||||
)
|
||||
invoice_id = fields.Many2one(
|
||||
"account.invoice",
|
||||
move_id = fields.Many2one(
|
||||
"account.move",
|
||||
string="Source",
|
||||
related="invoice_line_id.invoice_id",
|
||||
related="account_move_line_id.move_id",
|
||||
index=True,
|
||||
readonly=True,
|
||||
)
|
||||
@@ -125,15 +123,14 @@ class RmaOrderLine(models.Model):
|
||||
res["domain"] = {}
|
||||
domain = [
|
||||
"|",
|
||||
("invoice_id.partner_id", "=", self.partner_id.id),
|
||||
("invoice_id.partner_id", "child_of", self.partner_id.id),
|
||||
("move_id.partner_id", "=", self.partner_id.id),
|
||||
("move_id.partner_id", "child_of", self.partner_id.id),
|
||||
]
|
||||
if self.product_id:
|
||||
domain.append(("product_id", "=", self.product_id.id))
|
||||
res["domain"]["invoice_line_id"] = domain
|
||||
res["domain"]["account_move_line_id"] = domain
|
||||
return res
|
||||
|
||||
@api.multi
|
||||
def _prepare_rma_line_from_inv_line(self, line):
|
||||
self.ensure_one()
|
||||
if not self.type:
|
||||
@@ -173,15 +170,15 @@ class RmaOrderLine(models.Model):
|
||||
)
|
||||
data = {
|
||||
"product_id": line.product_id.id,
|
||||
"origin": line.invoice_id.number,
|
||||
"uom_id": line.uom_id.id,
|
||||
"origin": line.move_id.name,
|
||||
"uom_id": line.product_uom_id.id,
|
||||
"operation_id": operation.id,
|
||||
"product_qty": line.quantity,
|
||||
"price_unit": line.invoice_id.currency_id.compute(
|
||||
"price_unit": line.move_id.currency_id.compute(
|
||||
line.price_unit, line.currency_id, round=False
|
||||
),
|
||||
"delivery_address_id": line.invoice_id.partner_id.id,
|
||||
"invoice_address_id": line.invoice_id.partner_id.id,
|
||||
"delivery_address_id": line.move_id.partner_id.id,
|
||||
"invoice_address_id": line.move_id.partner_id.id,
|
||||
"receipt_policy": operation.receipt_policy,
|
||||
"refund_policy": operation.refund_policy,
|
||||
"delivery_policy": operation.delivery_policy,
|
||||
@@ -198,21 +195,20 @@ class RmaOrderLine(models.Model):
|
||||
}
|
||||
return data
|
||||
|
||||
@api.onchange("invoice_line_id")
|
||||
def _onchange_invoice_line_id(self):
|
||||
if not self.invoice_line_id:
|
||||
@api.onchange("account_move_line_id")
|
||||
def _onchange_account_move_line_id(self):
|
||||
if not self.account_move_line_id:
|
||||
return
|
||||
data = self._prepare_rma_line_from_inv_line(self.invoice_line_id)
|
||||
data = self._prepare_rma_line_from_inv_line(self.account_move_line_id)
|
||||
self.update(data)
|
||||
self._remove_other_data_origin("invoice_line_id")
|
||||
self._remove_other_data_origin("account_move_line_id")
|
||||
|
||||
@api.multi
|
||||
@api.constrains("invoice_line_id", "partner_id")
|
||||
@api.constrains("account_move_line_id", "partner_id")
|
||||
def _check_invoice_partner(self):
|
||||
for rec in self:
|
||||
if (
|
||||
rec.invoice_line_id
|
||||
and rec.invoice_line_id.invoice_id.partner_id != rec.partner_id
|
||||
rec.account_move_line_id
|
||||
and rec.account_move_line_id.move_id.partner_id != rec.partner_id
|
||||
):
|
||||
raise ValidationError(
|
||||
_(
|
||||
@@ -221,11 +217,10 @@ class RmaOrderLine(models.Model):
|
||||
)
|
||||
)
|
||||
|
||||
@api.multi
|
||||
def _remove_other_data_origin(self, exception):
|
||||
res = super(RmaOrderLine, self)._remove_other_data_origin(exception)
|
||||
if not exception == "invoice_line_id":
|
||||
self.invoice_line_id = False
|
||||
if not exception == "account_move_line_id":
|
||||
self.account_move_line_id = False
|
||||
return res
|
||||
|
||||
@api.onchange("operation_id")
|
||||
@@ -235,49 +230,45 @@ class RmaOrderLine(models.Model):
|
||||
self.refund_policy = self.operation_id.refund_policy or "no"
|
||||
return result
|
||||
|
||||
@api.multi
|
||||
@api.constrains("invoice_line_id")
|
||||
@api.constrains("account_move_line_id")
|
||||
def _check_duplicated_lines(self):
|
||||
for line in self:
|
||||
matching_inv_lines = self.env["account.invoice.line"].search(
|
||||
[("id", "=", line.invoice_line_id.id)]
|
||||
matching_inv_lines = self.env["account.move.line"].search(
|
||||
[("id", "=", line.account_move_line_id.id)]
|
||||
)
|
||||
if len(matching_inv_lines) > 1:
|
||||
raise UserError(
|
||||
_(
|
||||
"There's an rma for the invoice line %s "
|
||||
"and invoice %s"
|
||||
% (line.invoice_line_id, line.invoice_line_id.invoice_id)
|
||||
% (line.account_move_line_id, line.account_move_line_id.move_id)
|
||||
)
|
||||
)
|
||||
return {}
|
||||
|
||||
@api.multi
|
||||
def action_view_invoice(self):
|
||||
action = self.env.ref("account.action_invoice_tree")
|
||||
result = action.read()[0]
|
||||
res = self.env.ref("account.invoice_form", False)
|
||||
res = self.env.ref("account.view_move_form", False)
|
||||
result["views"] = [(res and res.id or False, "form")]
|
||||
result["view_id"] = res and res.id or False
|
||||
result["res_id"] = self.invoice_line_id.invoice_id.id
|
||||
result["res_id"] = self.account_move_line_id.move_id.id
|
||||
return result
|
||||
|
||||
@api.multi
|
||||
def action_view_refunds(self):
|
||||
action = self.env.ref("account.action_invoice_tree2")
|
||||
result = action.read()[0]
|
||||
invoice_ids = self.mapped("refund_line_ids.invoice_id").ids
|
||||
if invoice_ids:
|
||||
move_ids = self.mapped("refund_line_ids.move_id").ids
|
||||
if move_ids:
|
||||
# choose the view_mode accordingly
|
||||
if len(invoice_ids) > 1:
|
||||
result["domain"] = [("id", "in", invoice_ids)]
|
||||
if len(move_ids) > 1:
|
||||
result["domain"] = [("id", "in", move_ids)]
|
||||
else:
|
||||
res = self.env.ref("account.invoice_supplier_form", False)
|
||||
res = self.env.ref("account.move_supplier_form", False)
|
||||
result["views"] = [(res and res.id or False, "form")]
|
||||
result["res_id"] = invoice_ids[0]
|
||||
result["res_id"] = move_ids[0]
|
||||
return result
|
||||
|
||||
@api.multi
|
||||
def name_get(self):
|
||||
res = []
|
||||
if self.env.context.get("rma"):
|
||||
|
||||
Reference in New Issue
Block a user