Skip to content

Commit

Permalink
[IMP] create sale order from rma group
Browse files Browse the repository at this point in the history
  • Loading branch information
chafique-delli committed Jul 3, 2024
1 parent 2e59754 commit 7eba3ea
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 38 deletions.
4 changes: 2 additions & 2 deletions rma_sale/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
"data": [
"security/ir.model.access.csv",
"data/rma_operation.xml",
"wizards/rma_make_sale_order_view.xml",
"wizards/rma_add_sale.xml",
"views/rma_order_view.xml",
"views/rma_operation_view.xml",
"views/sale_order_view.xml",
"wizards/rma_order_line_make_sale_order_view.xml",
"wizards/rma_add_sale.xml",
"views/rma_order_line_view.xml",
],
"installable": True,
Expand Down
9 changes: 9 additions & 0 deletions rma_sale/models/rma_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ def _compute_sales_count(self):
sales = rma.mapped("rma_line_ids.sale_line_id.order_id")
rma.sale_count = len(sales)

@api.depends("rma_line_ids", "rma_line_ids.qty_to_sell")
def _compute_qty_to_sell(self):
for rec in self:
rec.qty_to_sell = sum(rec.rma_line_ids.mapped("qty_to_sell"))

sale_count = fields.Integer(compute="_compute_sales_count", string="# of Sales")
qty_to_sell = fields.Float(
digits="Product Unit of Measure",
compute="_compute_qty_to_sell",
)

@api.model
def _get_line_domain(self, rma_id, line):
Expand Down
4 changes: 2 additions & 2 deletions rma_sale/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_rma_order_line_make_sale_order_customer_user_item,rma.order.line.make.sale.order.customer.user,model_rma_order_line_make_sale_order,rma.group_rma_customer_user,1,1,1,1
access_rma_order_line_make_sale_order_supplier_user_item,rma.order.line.make.sale.order.supplier.user,model_rma_order_line_make_sale_order,rma.group_rma_supplier_user,1,1,1,1
access_rma_order_line_make_sale_order_customer_user_item,rma.order.line.make.sale.order.customer.user,model_rma_make_sale_order,rma.group_rma_customer_user,1,1,1,1
access_rma_order_line_make_sale_order_supplier_user_item,rma.order.line.make.sale.order.supplier.user,model_rma_make_sale_order,rma.group_rma_supplier_user,1,1,1,1
access_rma_order_line_make_sale_order_item_customer_user_item,rma.order.line.make.sale.order.item.customer.user,model_rma_order_line_make_sale_order_item,rma.group_rma_customer_user,1,1,1,1
access_rma_order_line_make_sale_order_item_supplier_user_item,rma.order.line.make.sale.order.item.supplier.user,model_rma_order_line_make_sale_order_item,rma.group_rma_supplier_user,1,1,1,1
access_rma_add_sale_customer_user_item,rma.add.sale.customer.user,model_rma_add_sale,rma.group_rma_customer_user,1,1,1,1
Expand Down
6 changes: 3 additions & 3 deletions rma_sale/views/rma_order_line_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<div name='button_box' position="inside">
<button
type="object"
name="action_view_origin_sale_order"
name="action_view_sale_order"
class="oe_stat_button"
icon="fa-strikethrough"
attrs="{'invisible': [('sale_id', '=', False)]}"
Expand Down Expand Up @@ -68,14 +68,14 @@
<field name="arch" type="xml">
<header position="inside">
<button
name="%(action_rma_order_line_make_sale_order)d"
name="%(action_rma_make_sale_order)d"
string="Create Sales Quotation"
class="oe_highlight"
attrs="{'invisible':['|', '|', '|', ('qty_to_sell', '=', 0), ('qty_to_sell', '&lt;', 0), ('state', '!=', 'approved'), ('sale_policy', '=', 'no')]}"
type="action"
/>
<button
name="%(action_rma_order_line_make_sale_order)d"
name="%(action_rma_make_sale_order)d"
string="Create Sales Quotation"
attrs="{'invisible':['|', '|', ('qty_to_sell', '>', 0), ('state', '!=', 'approved'), ('sale_policy', '=', 'no')]}"
type="action"
Expand Down
26 changes: 26 additions & 0 deletions rma_sale/views/rma_order_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,32 @@
<xpath expr="//field[@name='rma_line_ids']/tree" position="inside">
<field name="sale_policy" invisible="True" />
</xpath>
<xpath expr="//field[@name='qty_to_deliver']" position="after">
<field name="qty_to_sell" invisible="1" />
</xpath>
</field>
</record>

<record id="view_rma_button_sale_form" model="ir.ui.view">
<field name="name">rma.order.form</field>
<field name="model">rma.order</field>
<field name="inherit_id" ref="rma.view_rma_button_form" />
<field name="arch" type="xml">
<header position="inside">
<button
name="%(action_rma_make_sale_order)d"
string="Create Sales Quotation"
class="oe_highlight"
attrs="{'invisible':['|', '|', ('qty_to_sell', '=', 0), ('qty_to_sell', '&lt;', 0), ('state', '!=', 'approved')]}"
type="action"
/>
<button
name="%(action_rma_make_sale_order)d"
string="Create Sales Quotation"
attrs="{'invisible':['|', ('qty_to_sell', '>', 0), ('state', '!=', 'approved')]}"
type="action"
/>
</header>
</field>
</record>
</odoo>
2 changes: 1 addition & 1 deletion rma_sale/wizards/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from . import rma_order_line_make_sale_order
from . import rma_make_sale_order
from . import rma_make_picking
from . import rma_refund
from . import rma_add_sale
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# Copyright 2020 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)

from odoo import _, api, exceptions, fields, models
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError


class RmaLineMakeSaleOrder(models.TransientModel):
_name = "rma.order.line.make.sale.order"
_description = "Make Sales Order from RMA Line"
class RmaMakeSaleOrder(models.TransientModel):
_name = "rma.make.sale.order"
_description = "Make Sales Order from RMA"

partner_id = fields.Many2one(
comodel_name="res.partner",
Expand Down Expand Up @@ -43,24 +44,33 @@ def _prepare_item(self, line):

@api.model
def default_get(self, fields_list):
res = super(RmaLineMakeSaleOrder, self).default_get(fields_list)
context = self._context.copy()
res = super().default_get(fields_list)
rma_line_obj = self.env["rma.order.line"]
rma_line_ids = self.env.context["active_ids"] or []
active_model = self.env.context["active_model"]
rma_obj = self.env["rma.order"]
active_ids = context.get("active_ids") or []
active_model = context.get("active_model")

if not rma_line_ids:
if not active_ids:
return res
assert active_model == "rma.order.line", "Bad context propagation"
assert active_model in [
"rma.order.line",
"rma.order",
], "Bad context propagation"

items = []
lines = rma_line_obj.browse(rma_line_ids)
if active_model == "rma.order":
rma = rma_obj.browse(active_ids)
lines = rma.rma_line_ids
else:
lines = rma_line_obj.browse(active_ids)
for line in lines:
items.append([0, 0, self._prepare_item(line)])
customers = lines.mapped("partner_id")
if len(customers) == 1:
res["partner_id"] = customers.id
else:
raise exceptions.Warning(
raise ValidationError(
_(
"Only RMA lines from the same partner can be processed at "
"the same time"
Expand All @@ -72,7 +82,7 @@ def default_get(self, fields_list):
@api.model
def _prepare_sale_order(self, line):
if not self.partner_id:
raise exceptions.Warning(_("Enter a customer."))
raise ValidationError(_("Enter a customer."))
customer = self.partner_id
auto = self.env["account.fiscal.position"].search(
[("auto_apply", "=", True), ("country_id", "=", customer.country_id.id)],
Expand All @@ -83,8 +93,11 @@ def _prepare_sale_order(self, line):
fiscal_position = customer.property_account_position_id
elif auto:
fiscal_position = auto
origin = line.name
if self.env.context.get("active_model") == "rma.order":
origin = line.rma_id.name
data = {
"origin": line.name,
"origin": origin,
"partner_id": customer.id,
"warehouse_id": line.out_warehouse_id.id,
"company_id": line.company_id.id,
Expand Down Expand Up @@ -118,19 +131,20 @@ def make_sale_order(self):
sale_obj = self.env["sale.order"]
so_line_obj = self.env["sale.order.line"]
sale = False

for item in self.item_ids:
line = item.line_id
if item.product_qty <= 0.0:
raise exceptions.Warning(_("Enter a positive quantity."))
raise ValidationError(_("Enter a positive quantity."))

if self.sale_order_id:
sale = self.sale_order_id
if not sale:
line = item.line_id
po_data = self._prepare_sale_order(line)
sale = sale_obj.create(po_data)
sale.name = sale.name + " - " + line.name

rma = line
if self.env.context.get("active_model") == "rma.order":
rma = line.rma_id
sale.name = sale.name + " - " + rma.name
so_line_data = self._prepare_sale_order_line(sale, item)
sale_line = so_line_obj.create(so_line_data)
self._post_process_sale_order(item, sale_line)
Expand All @@ -146,9 +160,7 @@ class RmaLineMakeSaleOrderItem(models.TransientModel):
_name = "rma.order.line.make.sale.order.item"
_description = "RMA Line Make Sale Order Item"

wiz_id = fields.Many2one(
comodel_name="rma.order.line.make.sale.order", string="Wizard"
)
wiz_id = fields.Many2one(comodel_name="rma.make.sale.order", string="Wizard")
line_id = fields.Many2one(
comodel_name="rma.order.line", string="RMA Line", compute="_compute_line_id"
)
Expand All @@ -166,11 +178,17 @@ class RmaLineMakeSaleOrderItem(models.TransientModel):

def _compute_line_id(self):
rma_line_obj = self.env["rma.order.line"]
rma_obj = self.env["rma.order"]
active_ids = self.env.context.get("active_ids") or []
active_model = self.env.context.get("active_model")
for rec in self:
if not self.env.context["active_ids"]:
if not active_ids:
return
rma_line_ids = self.env.context["active_ids"] or []
lines = rma_line_obj.browse(rma_line_ids)
if active_model == "rma.order":
rma = rma_obj.browse(active_ids)
lines = rma.rma_line_ids
else:
lines = rma_line_obj.browse(active_ids)
rec.line_id = lines[0]

@api.onchange("product_id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<record id="view_rma_order_line_make_sale_order" model="ir.ui.view">
<field name="name">RMA Line Make Sale Order</field>
<field name="model">rma.order.line.make.sale.order</field>
<field name="model">rma.make.sale.order</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Create Quotation">
Expand Down Expand Up @@ -51,13 +51,10 @@
</field>
</record>

<record
id="action_rma_order_line_make_sale_order"
model="ir.actions.act_window"
>
<record id="action_rma_make_sale_order" model="ir.actions.act_window">
<field name="name">Create Sales Quotation</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">rma.order.line.make.sale.order</field>
<field name="res_model">rma.make.sale.order</field>
<field name="view_mode">form</field>
<field name="view_id" ref="view_rma_order_line_make_sale_order" />
<field name="target">new</field>
Expand Down

0 comments on commit 7eba3ea

Please sign in to comment.