mirror of
https://github.com/ForgeFlow/stock-rma.git
synced 2025-01-21 12:57:49 +02:00
[IMP] : black, isort
This commit is contained in:
@@ -1,19 +1,20 @@
|
||||
# Copyright (C) 2017 Eficent Business and IT Consulting Services S.L.
|
||||
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
|
||||
|
||||
from odoo import api, fields, models, _
|
||||
from odoo.exceptions import UserError
|
||||
from datetime import datetime
|
||||
|
||||
from odoo import _, api, fields, models
|
||||
from odoo.exceptions import UserError
|
||||
|
||||
|
||||
class RmaOrder(models.Model):
|
||||
_name = "rma.order"
|
||||
_description = 'RMA Group'
|
||||
_inherit = ['mail.thread']
|
||||
_description = "RMA Group"
|
||||
_inherit = ["mail.thread"]
|
||||
|
||||
@api.model
|
||||
def _get_default_type(self):
|
||||
if 'supplier' in self.env.context:
|
||||
if "supplier" in self.env.context:
|
||||
return "supplier"
|
||||
return "customer"
|
||||
|
||||
@@ -23,7 +24,7 @@ class RmaOrder(models.Model):
|
||||
picking_ids = []
|
||||
for line in rec.rma_line_ids:
|
||||
for move in line.move_ids:
|
||||
if move.location_dest_id.usage == 'internal':
|
||||
if move.location_dest_id.usage == "internal":
|
||||
picking_ids.append(move.picking_id.id)
|
||||
else:
|
||||
if line.customer_to_supplier:
|
||||
@@ -37,7 +38,7 @@ class RmaOrder(models.Model):
|
||||
for rec in self:
|
||||
for line in rec.rma_line_ids:
|
||||
for move in line.move_ids:
|
||||
if move.location_dest_id.usage in ('supplier', 'customer'):
|
||||
if move.location_dest_id.usage in ("supplier", "customer"):
|
||||
if not line.customer_to_supplier:
|
||||
picking_ids.append(move.picking_id.id)
|
||||
shipments = list(set(picking_ids))
|
||||
@@ -45,34 +46,36 @@ class RmaOrder(models.Model):
|
||||
|
||||
@api.multi
|
||||
def _compute_supplier_line_count(self):
|
||||
self.supplier_line_count = len(self.rma_line_ids.filtered(
|
||||
lambda r: r.supplier_rma_line_ids))
|
||||
self.supplier_line_count = len(
|
||||
self.rma_line_ids.filtered(lambda r: r.supplier_rma_line_ids)
|
||||
)
|
||||
|
||||
@api.multi
|
||||
def _compute_line_count(self):
|
||||
for rec in self:
|
||||
rec.line_count = len(rec._get_valid_lines())
|
||||
|
||||
@api.depends('rma_line_ids', 'rma_line_ids.state')
|
||||
@api.depends("rma_line_ids", "rma_line_ids.state")
|
||||
@api.multi
|
||||
def _compute_state(self):
|
||||
for rec in self:
|
||||
rma_line_done = self.env['rma.order.line'].search_count(
|
||||
[('id', 'in', rec.rma_line_ids.ids), ('state', '=', 'done')])
|
||||
rma_line_approved = self.env['rma.order.line'].search_count(
|
||||
[('id', 'in', rec.rma_line_ids.ids),
|
||||
('state', '=', 'approved')])
|
||||
rma_line_to_approve = self.env['rma.order.line'].search_count(
|
||||
[('id', 'in', rec.rma_line_ids.ids),
|
||||
('state', '=', 'to_approve')])
|
||||
rma_line_done = self.env["rma.order.line"].search_count(
|
||||
[("id", "in", rec.rma_line_ids.ids), ("state", "=", "done")]
|
||||
)
|
||||
rma_line_approved = self.env["rma.order.line"].search_count(
|
||||
[("id", "in", rec.rma_line_ids.ids), ("state", "=", "approved")]
|
||||
)
|
||||
rma_line_to_approve = self.env["rma.order.line"].search_count(
|
||||
[("id", "in", rec.rma_line_ids.ids), ("state", "=", "to_approve")]
|
||||
)
|
||||
if rma_line_done != 0:
|
||||
state = 'done'
|
||||
state = "done"
|
||||
elif rma_line_approved != 0:
|
||||
state = 'approved'
|
||||
state = "approved"
|
||||
elif rma_line_to_approve != 0:
|
||||
state = 'to_approve'
|
||||
state = "to_approve"
|
||||
else:
|
||||
state = 'draft'
|
||||
state = "draft"
|
||||
rec.state = state
|
||||
|
||||
@api.model
|
||||
@@ -81,108 +84,114 @@ class RmaOrder(models.Model):
|
||||
|
||||
@api.model
|
||||
def _default_warehouse_id(self):
|
||||
warehouse = self.env['stock.warehouse'].search(
|
||||
[('company_id', '=', self.env.user.company_id.id)], limit=1)
|
||||
warehouse = self.env["stock.warehouse"].search(
|
||||
[("company_id", "=", self.env.user.company_id.id)], limit=1
|
||||
)
|
||||
return warehouse
|
||||
|
||||
name = fields.Char(
|
||||
string='Group Number', index=True, copy=False)
|
||||
name = fields.Char(string="Group Number", index=True, copy=False)
|
||||
type = fields.Selection(
|
||||
[('customer', 'Customer'), ('supplier', 'Supplier')],
|
||||
string="Type", required=True,
|
||||
[("customer", "Customer"), ("supplier", "Supplier")],
|
||||
string="Type",
|
||||
required=True,
|
||||
default=lambda self: self._get_default_type(),
|
||||
readonly=True
|
||||
readonly=True,
|
||||
)
|
||||
reference = fields.Char(
|
||||
string="Partner Reference", help="The partner reference of this RMA order."
|
||||
)
|
||||
comment = fields.Text("Additional Information")
|
||||
date_rma = fields.Datetime(
|
||||
string="Order Date", index=True, default=lambda self: self._default_date_rma()
|
||||
)
|
||||
reference = fields.Char(string='Partner Reference',
|
||||
help="The partner reference of this RMA order.")
|
||||
comment = fields.Text('Additional Information')
|
||||
date_rma = fields.Datetime(string='Order Date', index=True,
|
||||
default=lambda self: self._default_date_rma(),)
|
||||
partner_id = fields.Many2one(
|
||||
comodel_name='res.partner', string='Partner', required=True)
|
||||
rma_line_ids = fields.One2many('rma.order.line', 'rma_id',
|
||||
string='RMA lines')
|
||||
in_shipment_count = fields.Integer(compute='_compute_in_shipment_count',
|
||||
string='# of Invoices')
|
||||
out_shipment_count = fields.Integer(compute='_compute_out_shipment_count',
|
||||
string='# of Outgoing Shipments')
|
||||
line_count = fields.Integer(compute='_compute_line_count',
|
||||
string='# of Outgoing Shipments')
|
||||
supplier_line_count = fields.Integer(
|
||||
compute='_compute_supplier_line_count',
|
||||
string='# of Supplier RMAs'
|
||||
comodel_name="res.partner", string="Partner", required=True
|
||||
)
|
||||
rma_line_ids = fields.One2many("rma.order.line", "rma_id", string="RMA lines")
|
||||
in_shipment_count = fields.Integer(
|
||||
compute="_compute_in_shipment_count", string="# of Invoices"
|
||||
)
|
||||
out_shipment_count = fields.Integer(
|
||||
compute="_compute_out_shipment_count", string="# of Outgoing Shipments"
|
||||
)
|
||||
line_count = fields.Integer(
|
||||
compute="_compute_line_count", string="# of Outgoing Shipments"
|
||||
)
|
||||
supplier_line_count = fields.Integer(
|
||||
compute="_compute_supplier_line_count", string="# of Supplier RMAs"
|
||||
)
|
||||
company_id = fields.Many2one(
|
||||
"res.company",
|
||||
string="Company",
|
||||
required=True,
|
||||
default=lambda self: self.env.user.company_id,
|
||||
)
|
||||
company_id = fields.Many2one('res.company', string='Company',
|
||||
required=True, default=lambda self:
|
||||
self.env.user.company_id)
|
||||
assigned_to = fields.Many2one(
|
||||
comodel_name='res.users', track_visibility='onchange',
|
||||
comodel_name="res.users",
|
||||
track_visibility="onchange",
|
||||
default=lambda self: self.env.uid,
|
||||
)
|
||||
requested_by = fields.Many2one(
|
||||
comodel_name='res.users', track_visibility='onchange',
|
||||
comodel_name="res.users",
|
||||
track_visibility="onchange",
|
||||
default=lambda self: self.env.uid,
|
||||
)
|
||||
in_warehouse_id = fields.Many2one(
|
||||
comodel_name='stock.warehouse',
|
||||
string='Inbound Warehouse',
|
||||
comodel_name="stock.warehouse",
|
||||
string="Inbound Warehouse",
|
||||
required=True,
|
||||
default=_default_warehouse_id,
|
||||
)
|
||||
customer_to_supplier = fields.Boolean(
|
||||
'The customer will send to the supplier',
|
||||
)
|
||||
supplier_to_customer = fields.Boolean(
|
||||
'The supplier will send to the customer',
|
||||
)
|
||||
customer_to_supplier = fields.Boolean("The customer will send to the supplier")
|
||||
supplier_to_customer = fields.Boolean("The supplier will send to the customer")
|
||||
supplier_address_id = fields.Many2one(
|
||||
comodel_name='res.partner',
|
||||
string='Supplier Address',
|
||||
help="Address of the supplier in case of Customer RMA operation "
|
||||
"dropship.")
|
||||
comodel_name="res.partner",
|
||||
string="Supplier Address",
|
||||
help="Address of the supplier in case of Customer RMA operation " "dropship.",
|
||||
)
|
||||
customer_address_id = fields.Many2one(
|
||||
comodel_name='res.partner',
|
||||
string='Customer Address',
|
||||
help="Address of the customer in case of Supplier RMA operation "
|
||||
"dropship.")
|
||||
comodel_name="res.partner",
|
||||
string="Customer Address",
|
||||
help="Address of the customer in case of Supplier RMA operation " "dropship.",
|
||||
)
|
||||
state = fields.Selection(
|
||||
compute=_compute_state,
|
||||
selection=[('draft', 'Draft'),
|
||||
('to_approve', 'To Approve'),
|
||||
('approved', 'Approved'),
|
||||
('done', 'Done')],
|
||||
string='State', default='draft', store=True
|
||||
selection=[
|
||||
("draft", "Draft"),
|
||||
("to_approve", "To Approve"),
|
||||
("approved", "Approved"),
|
||||
("done", "Done"),
|
||||
],
|
||||
string="State",
|
||||
default="draft",
|
||||
store=True,
|
||||
)
|
||||
|
||||
@api.constrains("partner_id", "rma_line_ids")
|
||||
def _check_partner_id(self):
|
||||
if self.rma_line_ids and self.partner_id != self.mapped(
|
||||
"rma_line_ids.partner_id"):
|
||||
raise UserError(_(
|
||||
"Group partner and RMA's partner must be the same."))
|
||||
"rma_line_ids.partner_id"
|
||||
):
|
||||
raise UserError(_("Group partner and RMA's partner must be the same."))
|
||||
if len(self.mapped("rma_line_ids.partner_id")) > 1:
|
||||
raise UserError(_(
|
||||
"All grouped RMA's should have same partner."))
|
||||
raise UserError(_("All grouped RMA's should have same partner."))
|
||||
|
||||
@api.model
|
||||
def create(self, vals):
|
||||
if (self.env.context.get('supplier') or
|
||||
vals.get('type') == 'supplier'):
|
||||
vals['name'] = self.env['ir.sequence'].next_by_code(
|
||||
'rma.order.supplier')
|
||||
if self.env.context.get("supplier") or vals.get("type") == "supplier":
|
||||
vals["name"] = self.env["ir.sequence"].next_by_code("rma.order.supplier")
|
||||
else:
|
||||
vals['name'] = self.env['ir.sequence'].next_by_code(
|
||||
'rma.order.customer')
|
||||
vals["name"] = self.env["ir.sequence"].next_by_code("rma.order.customer")
|
||||
return super(RmaOrder, self).create(vals)
|
||||
|
||||
@api.multi
|
||||
def action_view_in_shipments(self):
|
||||
action = self.env.ref('stock.action_picking_tree_all')
|
||||
action = self.env.ref("stock.action_picking_tree_all")
|
||||
result = action.read()[0]
|
||||
picking_ids = []
|
||||
for line in self.rma_line_ids:
|
||||
for move in line.move_ids:
|
||||
if move.location_dest_id.usage == 'internal':
|
||||
if move.location_dest_id.usage == "internal":
|
||||
picking_ids.append(move.picking_id.id)
|
||||
else:
|
||||
if line.customer_to_supplier:
|
||||
@@ -191,32 +200,32 @@ class RmaOrder(models.Model):
|
||||
shipments = list(set(picking_ids))
|
||||
# choose the view_mode accordingly
|
||||
if len(shipments) > 1:
|
||||
result['domain'] = [('id', 'in', shipments)]
|
||||
result["domain"] = [("id", "in", shipments)]
|
||||
else:
|
||||
res = self.env.ref('stock.view_picking_form', False)
|
||||
result['views'] = [(res and res.id or False, 'form')]
|
||||
result['res_id'] = shipments[0]
|
||||
res = self.env.ref("stock.view_picking_form", False)
|
||||
result["views"] = [(res and res.id or False, "form")]
|
||||
result["res_id"] = shipments[0]
|
||||
return result
|
||||
|
||||
@api.multi
|
||||
def action_view_out_shipments(self):
|
||||
action = self.env.ref('stock.action_picking_tree_all')
|
||||
action = self.env.ref("stock.action_picking_tree_all")
|
||||
result = action.read()[0]
|
||||
picking_ids = []
|
||||
for line in self.rma_line_ids:
|
||||
for move in line.move_ids:
|
||||
if move.location_dest_id.usage in ('supplier', 'customer'):
|
||||
if move.location_dest_id.usage in ("supplier", "customer"):
|
||||
if not line.customer_to_supplier:
|
||||
picking_ids.append(move.picking_id.id)
|
||||
if picking_ids:
|
||||
shipments = list(set(picking_ids))
|
||||
# choose the view_mode accordingly
|
||||
if len(shipments) != 1:
|
||||
result['domain'] = [('id', 'in', shipments)]
|
||||
result["domain"] = [("id", "in", shipments)]
|
||||
else:
|
||||
res = self.env.ref('stock.view_picking_form', False)
|
||||
result['views'] = [(res and res.id or False, 'form')]
|
||||
result['res_id'] = shipments[0]
|
||||
res = self.env.ref("stock.view_picking_form", False)
|
||||
result["views"] = [(res and res.id or False, "form")]
|
||||
result["res_id"] = shipments[0]
|
||||
return result
|
||||
|
||||
@api.multi
|
||||
@@ -228,56 +237,65 @@ class RmaOrder(models.Model):
|
||||
|
||||
@api.multi
|
||||
def action_view_lines(self):
|
||||
if self.type == 'customer':
|
||||
action = self.env.ref('rma.action_rma_customer_lines')
|
||||
res = self.env.ref('rma.view_rma_line_form', False)
|
||||
if self.type == "customer":
|
||||
action = self.env.ref("rma.action_rma_customer_lines")
|
||||
res = self.env.ref("rma.view_rma_line_form", False)
|
||||
else:
|
||||
action = self.env.ref('rma.action_rma_supplier_lines')
|
||||
res = self.env.ref('rma.view_rma_line_supplier_form', False)
|
||||
action = self.env.ref("rma.action_rma_supplier_lines")
|
||||
res = self.env.ref("rma.view_rma_line_supplier_form", False)
|
||||
result = action.read()[0]
|
||||
lines = self._get_valid_lines()
|
||||
# choose the view_mode accordingly
|
||||
if len(lines.ids) != 1:
|
||||
result['domain'] = [('id', 'in', lines.ids)]
|
||||
result["domain"] = [("id", "in", lines.ids)]
|
||||
else:
|
||||
result['views'] = [(res and res.id or False, 'form')]
|
||||
result['res_id'] = lines.id
|
||||
result['context'] = {}
|
||||
result["views"] = [(res and res.id or False, "form")]
|
||||
result["res_id"] = lines.id
|
||||
result["context"] = {}
|
||||
return result
|
||||
|
||||
@api.multi
|
||||
def action_view_supplier_lines(self):
|
||||
action = self.env.ref('rma.action_rma_supplier_lines')
|
||||
action = self.env.ref("rma.action_rma_supplier_lines")
|
||||
result = action.read()[0]
|
||||
lines = self.rma_line_ids
|
||||
for line_id in lines:
|
||||
related_lines = [line.id for line in line_id.supplier_rma_line_ids]
|
||||
# choose the view_mode accordingly
|
||||
if len(related_lines) != 1:
|
||||
result['domain'] = [('id', 'in', related_lines)]
|
||||
result["domain"] = [("id", "in", related_lines)]
|
||||
else:
|
||||
res = self.env.ref('rma.view_rma_line_supplier_form', False)
|
||||
result['views'] = [(res and res.id or False, 'form')]
|
||||
result['res_id'] = related_lines[0]
|
||||
res = self.env.ref("rma.view_rma_line_supplier_form", False)
|
||||
result["views"] = [(res and res.id or False, "form")]
|
||||
result["res_id"] = related_lines[0]
|
||||
return result
|
||||
|
||||
@api.onchange('in_warehouse_id')
|
||||
@api.onchange("in_warehouse_id")
|
||||
def _onchange_in_warehouse_id(self):
|
||||
if self.in_warehouse_id and self.rma_line_ids:
|
||||
self.rma_line_ids.write(
|
||||
{'in_warehouse_id': self.in_warehouse_id.id,
|
||||
'location_id': self.in_warehouse_id.lot_rma_id.id})
|
||||
{
|
||||
"in_warehouse_id": self.in_warehouse_id.id,
|
||||
"location_id": self.in_warehouse_id.lot_rma_id.id,
|
||||
}
|
||||
)
|
||||
|
||||
@api.onchange('customer_to_supplier', 'supplier_address_id')
|
||||
@api.onchange("customer_to_supplier", "supplier_address_id")
|
||||
def _onchange_customer_to_supplier(self):
|
||||
if self.type == 'customer' and self.rma_line_ids:
|
||||
if self.type == "customer" and self.rma_line_ids:
|
||||
self.rma_line_ids.write(
|
||||
{'customer_to_supplier': self.customer_to_supplier,
|
||||
'supplier_address_id': self.supplier_address_id.id})
|
||||
{
|
||||
"customer_to_supplier": self.customer_to_supplier,
|
||||
"supplier_address_id": self.supplier_address_id.id,
|
||||
}
|
||||
)
|
||||
|
||||
@api.onchange('supplier_to_customer', 'customer_address_id')
|
||||
@api.onchange("supplier_to_customer", "customer_address_id")
|
||||
def _onchange_supplier_to_customer(self):
|
||||
if self.type == 'supplier' and self.rma_line_ids:
|
||||
if self.type == "supplier" and self.rma_line_ids:
|
||||
self.rma_line_ids.write(
|
||||
{'supplier_to_customer': self.supplier_to_customer,
|
||||
'customer_address_id': self.customer_address_id.id})
|
||||
{
|
||||
"supplier_to_customer": self.supplier_to_customer,
|
||||
"customer_address_id": self.customer_address_id.id,
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user