mirror of
https://github.com/ForgeFlow/stock-rma.git
synced 2025-01-21 12:57:49 +02:00
235 lines
9.4 KiB
Python
235 lines
9.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
# © 2017 Eficent Business and IT Consulting Services S.L.
|
|
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
|
|
|
|
from openerp import _, api, fields, models
|
|
from openerp.addons import decimal_precision as dp
|
|
from openerp.exceptions import UserError
|
|
from dateutil.relativedelta import relativedelta
|
|
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT
|
|
from datetime import datetime
|
|
|
|
|
|
class RmaOrder(models.Model):
|
|
_name = "rma.order"
|
|
_inherit = ['mail.thread']
|
|
|
|
@api.model
|
|
def _compute_rule_id(self):
|
|
if self.company_id and self.company_id.id:
|
|
if self.company_id.rma_rule_id and self.company_id.rma_rule_id.id:
|
|
self.rule_id = self.company_id.rma_rule_id
|
|
|
|
@api.model
|
|
def _get_default_type(self):
|
|
if 'supplier' in self.env.context:
|
|
return "supplier"
|
|
else:
|
|
return "customer"
|
|
|
|
@api.multi
|
|
def _compute_in_shipment_count(self):
|
|
for rec in self:
|
|
rec.in_shipment_count = len(rec.rma_line_ids.mapped(
|
|
'procurement_ids.move_ids').filtered(
|
|
lambda m: m.location_dest_id.usage == 'internal').mapped(
|
|
'picking_id'))
|
|
|
|
@api.multi
|
|
def _compute_out_shipment_count(self):
|
|
for rec in self:
|
|
rec.out_shipment_count = len(rec.rma_line_ids.mapped(
|
|
'procurement_ids.move_ids').filtered(
|
|
lambda m: m.location_id.usage == 'internal').mapped(
|
|
'picking_id'))
|
|
|
|
@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))
|
|
|
|
@api.multi
|
|
def _compute_line_count(self):
|
|
self.ensure_one()
|
|
self.line_count = len(self._get_valid_lines())
|
|
|
|
name = fields.Char(string='Order Number', index=True,
|
|
readonly=True,
|
|
states={'progress': [('readonly', False)]},
|
|
copy=False)
|
|
type = fields.Selection(
|
|
[('customer', 'Customer'), ('supplier', 'Supplier')],
|
|
string="Type", required=True, default=_get_default_type, readonly=True)
|
|
reference = fields.Char(string='Reference',
|
|
help="The partner reference of this RMA order.")
|
|
|
|
comment = fields.Text('Additional Information', readonly=True, states={
|
|
'draft': [('readonly', False)]})
|
|
|
|
state = fields.Selection([('draft', 'Draft'), ('to_approve', 'To Approve'),
|
|
('approved', 'Approved'),
|
|
('done', 'Done')], string='State', index=True,
|
|
default='draft')
|
|
date_rma = fields.Datetime(string='Order Date', index=True, copy=False)
|
|
partner_id = fields.Many2one('res.partner', string='Partner',
|
|
required=True, readonly=True,
|
|
states={'draft': [('readonly', False)]})
|
|
assigned_to = fields.Many2one('res.users', 'Assigned to',
|
|
track_visibility='onchange')
|
|
requested_by = fields.Many2one('res.users', 'Requested by',
|
|
track_visibility='onchange',
|
|
default=lambda self: self.env.user)
|
|
rule_id = fields.Many2one('rma.rule', string='Approval Criteria',
|
|
compute=_compute_rule_id)
|
|
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',
|
|
copy=False)
|
|
supplier_line_count = fields.Integer(compute=_compute_supplier_line_count,
|
|
string='# of Outgoing Shipments',
|
|
copy=False)
|
|
company_id = fields.Many2one('res.company', string='Company',
|
|
required=True, default=lambda self:
|
|
self.env.user.company_id)
|
|
|
|
@api.model
|
|
def create(self, vals):
|
|
if self.env.context.get('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')
|
|
return super(RmaOrder, self).create(vals)
|
|
|
|
@api.multi
|
|
def action_view_in_shipments(self):
|
|
action = self.env.ref('stock.action_picking_tree_all')
|
|
result = action.read()[0]
|
|
picking_ids = []
|
|
suppliers = self.env.ref('stock.stock_location_suppliers')
|
|
customers = self.env.ref('stock.stock_location_customers')
|
|
for line in self.rma_line_ids:
|
|
if line.type == 'customer':
|
|
for move in line.move_ids:
|
|
if move.picking_id.location_id == customers:
|
|
picking_ids.append(move.picking_id.id)
|
|
else:
|
|
for move in line.move_ids:
|
|
if move.picking_id.location_id == suppliers:
|
|
picking_ids.append(move.picking_id.id)
|
|
shipments = list(set(picking_ids))
|
|
# choose the view_mode accordingly
|
|
if len(shipments) != 1:
|
|
result['domain'] = "[('id', 'in', " + \
|
|
str(shipments) + ")]"
|
|
elif len(shipments) == 1:
|
|
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')
|
|
result = action.read()[0]
|
|
picking_ids = []
|
|
suppliers = self.env.ref('stock.stock_location_suppliers')
|
|
customers = self.env.ref('stock.stock_location_customers')
|
|
for line in self.rma_line_ids:
|
|
if line.type == 'customer':
|
|
for move in line.move_ids:
|
|
if move.picking_id.location_id != customers:
|
|
picking_ids.append(move.picking_id.id)
|
|
else:
|
|
for move in line.move_ids:
|
|
if move.picking_id.location_id != suppliers:
|
|
picking_ids.append(move.picking_id.id)
|
|
shipments = list(set(picking_ids))
|
|
# choose the view_mode accordingly
|
|
if len(shipments) != 1:
|
|
result['domain'] = "[('id', 'in', " + \
|
|
str(shipments) + ")]"
|
|
elif len(shipments) == 1:
|
|
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_rma_to_approve(self):
|
|
for rec in self:
|
|
rec.state = 'to_approve'
|
|
if rec.rule_id and rec.rule_id.id:
|
|
if rec.rule_id.approval_policy == 'always':
|
|
rec.assigned_to = self.env.uid
|
|
rec.action_rma_approve()
|
|
return True
|
|
|
|
@api.multi
|
|
def action_rma_draft(self):
|
|
for rec in self:
|
|
rec.state = 'draft'
|
|
return True
|
|
|
|
@api.multi
|
|
def action_rma_approve(self):
|
|
# pass the supplier address in case this is a customer RMA
|
|
for rec in self:
|
|
rec.state = 'approved'
|
|
return True
|
|
|
|
@api.multi
|
|
def action_rma_done(self):
|
|
for rec in self:
|
|
rec.state = 'done'
|
|
return True
|
|
|
|
@api.multi
|
|
def _get_valid_lines(self):
|
|
self.ensure_one()
|
|
return self.rma_line_ids
|
|
|
|
@api.multi
|
|
def action_view_lines(self):
|
|
if self.type == 'customer':
|
|
action = self.env.ref('rma.action_rma_customer_lines')
|
|
else:
|
|
action = self.env.ref('rma.action_rma_supplier_lines')
|
|
result = action.read()[0]
|
|
lines = self._get_valid_lines()
|
|
# choose the view_mode accordingly
|
|
if len(lines) != 1:
|
|
result['domain'] = "[('id', 'in', " + \
|
|
str(lines.ids) + ")]"
|
|
elif len(lines) == 1:
|
|
if self.type == 'customer':
|
|
res = self.env.ref('rma.view_rma_line_form', False)
|
|
else:
|
|
res = self.env.ref('rma.view_rma_line_supplier_form', False)
|
|
|
|
result['views'] = [(res and res.id or False, 'form')]
|
|
result['res_id'] = lines.id
|
|
return result
|
|
|
|
@api.multi
|
|
def action_view_supplier_lines(self):
|
|
action = self.env.ref('rma.action_rma_supplier_lines')
|
|
result = action.read()[0]
|
|
lines = self.rma_line_ids
|
|
related_lines = [line.id for line in lines.children_ids]
|
|
# choose the view_mode accordingly
|
|
if len(related_lines) != 1:
|
|
result['domain'] = "[('id', 'in', " + \
|
|
str(related_lines) + ")]"
|
|
elif len(related_lines) == 1:
|
|
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
|