From 20ecbee99c4e68f3cafda02daa304f91f9a518c5 Mon Sep 17 00:00:00 2001 From: Jordi Ballester Date: Thu, 27 Jul 2017 18:17:19 +0200 Subject: [PATCH] init branch --- rma_purchase/README.rst | 37 +++++++ rma_purchase/__init__.py | 5 + rma_purchase/__openerp__.py | 20 ++++ rma_purchase/models/__init__.py | 5 + rma_purchase/models/rma_order.py | 62 +++++++++++ rma_purchase/models/rma_order_line.py | 72 +++++++++++++ rma_purchase/views/rma_order_line_view.xml | 33 ++++++ rma_purchase/views/rma_order_view.xml | 33 ++++++ rma_purchase/wizards/__init__.py | 6 ++ rma_purchase/wizards/rma_add_purchase.py | 114 +++++++++++++++++++++ rma_purchase/wizards/rma_add_purchase.xml | 77 ++++++++++++++ rma_purchase/wizards/rma_make_picking.py | 38 +++++++ 12 files changed, 502 insertions(+) create mode 100644 rma_purchase/README.rst create mode 100644 rma_purchase/__init__.py create mode 100644 rma_purchase/__openerp__.py create mode 100644 rma_purchase/models/__init__.py create mode 100644 rma_purchase/models/rma_order.py create mode 100644 rma_purchase/models/rma_order_line.py create mode 100644 rma_purchase/views/rma_order_line_view.xml create mode 100644 rma_purchase/views/rma_order_view.xml create mode 100644 rma_purchase/wizards/__init__.py create mode 100644 rma_purchase/wizards/rma_add_purchase.py create mode 100644 rma_purchase/wizards/rma_add_purchase.xml create mode 100644 rma_purchase/wizards/rma_make_picking.py diff --git a/rma_purchase/README.rst b/rma_purchase/README.rst new file mode 100644 index 00000000..49fbc152 --- /dev/null +++ b/rma_purchase/README.rst @@ -0,0 +1,37 @@ +.. image:: https://img.shields.io/badge/licence-LGPL--3-blue.svg + :alt: License LGPL-3 + +RMA Purchase +=========== + +Purchase as RMA source + +Usage +===== + +select add_purchase_id to fill rma from RMA purchase + + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues +`_. In case of trouble, please +check there if your issue has already been reported. If you spotted it first, +help us smashing it by providing a detailed and welcomed feedback. + + +Credits +======= + +Contributors +------------ + +* Jordi Ballester Alomar +* Aaron Henriquez + + +Maintainer +---------- + +This module is maintained by Eficent. diff --git a/rma_purchase/__init__.py b/rma_purchase/__init__.py new file mode 100644 index 00000000..4105ff51 --- /dev/null +++ b/rma_purchase/__init__.py @@ -0,0 +1,5 @@ +# -*- 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 . import models +from . import wizards diff --git a/rma_purchase/__openerp__.py b/rma_purchase/__openerp__.py new file mode 100644 index 00000000..6d287e68 --- /dev/null +++ b/rma_purchase/__openerp__.py @@ -0,0 +1,20 @@ +# -*- 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) +{ + 'name': 'RMA Purchase', + 'version': '9.0.1.0.0', + 'category': 'RMA', + 'summary': 'RMA from PO', + 'description': """ + RMA from PO +""", + 'author': 'Eficent', + 'website': 'http://www.github.com/OCA/rma', + 'depends': ['rma_account', 'purchase'], + 'data': ['views/rma_order_view.xml', + 'views/rma_order_line_view.xml', + 'wizards/rma_add_purchase.xml'], + 'installable': True, + 'auto_install': True, +} diff --git a/rma_purchase/models/__init__.py b/rma_purchase/models/__init__.py new file mode 100644 index 00000000..a3a08e5e --- /dev/null +++ b/rma_purchase/models/__init__.py @@ -0,0 +1,5 @@ +# -*- 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 . import rma_order +from . import rma_order_line diff --git a/rma_purchase/models/rma_order.py b/rma_purchase/models/rma_order.py new file mode 100644 index 00000000..3b21983e --- /dev/null +++ b/rma_purchase/models/rma_order.py @@ -0,0 +1,62 @@ +# -*- 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 random import randint +from datetime import datetime + + +class RmaOrder(models.Model): + _inherit = "rma.order" + + @api.depends('rma_line_ids', 'rma_line_ids.procurement_ids') + @api.multi + def _compute_po_count(self): + for rec in self: + purchase_list = [] + for line in rec.rma_line_ids: + for procurement_id in line.procurement_ids: + if procurement_id.purchase_id and procurement_id.purchase_id.id: + purchase_list.append(procurement_id.purchase_id.id) + rec.po_count = len(list(set(purchase_list))) + + @api.one + def _compute_origin_po_count(self): + po_list = [] + for rma_line in self.rma_line_ids: + if rma_line.purchase_order_line_id and \ + rma_line.purchase_order_line_id.id: + po_list.append(rma_line.purchase_order_line_id.order_id.id) + self.origin_po_count = len(list(set(po_list))) + + po_count = fields.Integer(compute=_compute_po_count, + string='# of PO', + copy=False, default=0) + + origin_po_count = fields.Integer(compute=_compute_origin_po_count, + string='# of Origin PO', copy=False, + default=0) + + @api.multi + def action_view_purchase_order(self): + action = self.env.ref('purchase.purchase_rfq') + result = action.read()[0] + order_ids = [] + for line in self.rma_line_ids: + for procurement_id in line.procurement_ids: + order_ids.append(procurement_id.purchase_id.id) + result['domain'] = [('id', 'in', order_ids)] + return result + + @api.multi + def action_view_origin_purchase_order(self): + action = self.env.ref('purchase.purchase_rfq') + result = action.read()[0] + order_ids = [] + for rma_line in self.rma_line_ids: + if rma_line.purchase_order_line_id and \ + rma_line.purchase_order_line_id.id: + order_ids.append(rma_line.purchase_order_line_id.order_id.id) + result['domain'] = [('id', 'in', order_ids)] + return result diff --git a/rma_purchase/models/rma_order_line.py b/rma_purchase/models/rma_order_line.py new file mode 100644 index 00000000..f87c5eb3 --- /dev/null +++ b/rma_purchase/models/rma_order_line.py @@ -0,0 +1,72 @@ +# -*- 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 random import randint + + +class RmaOrderLine(models.Model): + _inherit = "rma.order.line" + + @api.one + def _compute_purchase_count(self): + purchase_list = [] + for procurement_id in self.procurement_ids: + if procurement_id.purchase_id and procurement_id.purchase_id.id: + purchase_list.append(procurement_id.purchase_id.id) + self.purchase_count = len(list(set(purchase_list))) + + @api.one + @api.depends('procurement_ids.purchase_line_id') + def _get_purchase_order_lines(self): + purchase_list = [] + for procurement_id in self.procurement_ids: + if procurement_id.purchase_line_id and \ + procurement_id.purchase_line_id.id: + purchase_list.append(procurement_id.purchase_line_id.id) + self.purchase_order_line_ids = [(6, 0, purchase_list)] + + @api.one + @api.depends('procurement_ids.purchase_line_id') + def _compute_qty_purchased(self): + self.qty_purchased = self._get_rma_purchased_qty() + + purchase_count = fields.Integer(compute=_compute_purchase_count, + string='# of Purchases', copy=False, + default=0) + purchase_order_line_id = fields.Many2one('purchase.order.line', + string='Origin Purchase Line', + ondelete='restrict') + purchase_order_line_ids = fields.Many2many( + 'purchase.order.line', 'purchase_line_rma_line_rel', + 'rma_order_line_id', 'purchase_order_line_id', + string='Purchase Order Lines', compute=_get_purchase_order_lines) + + qty_purchased = fields.Float( + string='Qty Purchased', copy=False, + digits=dp.get_precision('Product Unit of Measure'), + readonly=True, compute=_compute_qty_purchased, + store=True) + + @api.multi + def action_view_purchase_order(self): + action = self.env.ref('purchase.purchase_rfq') + result = action.read()[0] + order_ids = [] + for procurement_id in self.procurement_ids: + order_ids.append(procurement_id.purchase_id.id) + result['domain'] = [('id', 'in', order_ids)] + return result + + @api.multi + def _get_rma_purchased_qty(self): + self.ensure_one() + qty = 0.0 + for procurement_id in self.procurement_ids: + purchase_line = procurement_id.purchase_line_id + if self.type == 'supplier': + qty += purchase_line.product_qty + else: + qty = 0.0 + return qty diff --git a/rma_purchase/views/rma_order_line_view.xml b/rma_purchase/views/rma_order_line_view.xml new file mode 100644 index 00000000..e57b4939 --- /dev/null +++ b/rma_purchase/views/rma_order_line_view.xml @@ -0,0 +1,33 @@ + + + + + + rma.order.line.supplier.form + rma.order.line + + +
+ +
+ + + + + + + + + + +
+
+ +
+
diff --git a/rma_purchase/views/rma_order_view.xml b/rma_purchase/views/rma_order_view.xml new file mode 100644 index 00000000..1875c5e8 --- /dev/null +++ b/rma_purchase/views/rma_order_view.xml @@ -0,0 +1,33 @@ + + + + + + rma.order.supplier.form + rma.order + + +
+ +
+
+ +
+
+
+ +
+
diff --git a/rma_purchase/wizards/__init__.py b/rma_purchase/wizards/__init__.py new file mode 100644 index 00000000..716f361c --- /dev/null +++ b/rma_purchase/wizards/__init__.py @@ -0,0 +1,6 @@ +# -*- 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 . import rma_make_picking +from . import rma_add_purchase diff --git a/rma_purchase/wizards/rma_add_purchase.py b/rma_purchase/wizards/rma_add_purchase.py new file mode 100644 index 00000000..c31a99ec --- /dev/null +++ b/rma_purchase/wizards/rma_add_purchase.py @@ -0,0 +1,114 @@ +# -*- 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) + +import time +from openerp import models, fields, exceptions, api, _ +from openerp.exceptions import ValidationError +from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT as DT_FORMAT +import openerp.addons.decimal_precision as dp + + +class RmaAddPurchase(models.TransientModel): + _name = 'rma_add_purchase' + _description = 'Wizard to add rma lines' + + @api.model + def default_get(self, fields): + res = super(RmaAddPurchase, self).default_get(fields) + rma_obj = self.env['rma.order'] + rma_id = self.env.context['active_ids'] or [] + active_model = self.env.context['active_model'] + if not rma_id: + return res + assert active_model == 'rma.order', 'Bad context propagation' + + rma = rma_obj.browse(rma_id) + res['rma_id'] = rma.id + res['partner_id'] = rma.partner_id.id + res['purchase_id'] = False + res['purchase_line_ids'] = False + return res + + rma_id = fields.Many2one('rma.order', + string='RMA Order', + readonly=True, + ondelete='cascade') + + partner_id = fields.Many2one(comodel_name='res.partner', string='Partner', + readonly=True) + purchase_id = fields.Many2one(comodel_name='purchase.order', string='Order') + purchase_line_ids = fields.Many2many('purchase.order.line', + 'rma_add_purchase_add_line_rel', + 'purchase_line_id', 'rma_add_purchase_id', + readonly=False, + string='Purcahse Order Lines') + + def _prepare_rma_line_from_po_line(self, line): + operation = line.product_id.rma_operation_id and \ + line.product_id.rma_operation_id.id or False + if not operation: + operation = line.product_id.categ_id.rma_operation_id and \ + line.product_id.categ_id.rma_operation_id.id or False + data = { + 'purchase_order_line_id': line.id, + 'product_id': line.product_id.id, + 'origin': line.order_id.name, + 'uom_id': line.product_uom.id, + 'operation_id': operation, + 'product_qty': line.product_qty, + 'price_unit': line.currency_id.compute( + line.price_unit, line.currency_id, round=False), + 'rma_id': self.rma_id.id + } + if not operation: + operation = self.env['rma.operation'].search( + [('type', '=', self.rma_id.type)], limit=1) + if not operation: + raise ValidationError("Please define an operation first") + if not operation.in_route_id or not operation.out_route_id: + route = self.env['stock.location.route'].search( + [('rma_selectable', '=', True)], limit=1) + if not route: + raise ValidationError("Please define an rma route") + data.update( + {'in_route_id': operation.in_route_id.id or route, + 'out_route_id': operation.out_route_id.id or route, + 'receipt_policy': operation.receipt_policy, + 'location_id': operation.location_id.id or + self.env.ref('stock.stock_location_stock').id, + 'operation_id': operation.id, + 'refund_policy': operation.refund_policy, + 'delivery_policy': operation.delivery_policy + }) + return data + + @api.model + def _get_rma_data(self): + data = { + 'date_rma': fields.Datetime.now(), + 'delivery_address_id': self.purchase_id.partner_id.id, + 'invoice_address_id': self.purchase_id.partner_id.id + } + return data + + @api.model + def _get_existing_purchase_lines(self): + existing_purchase_lines = [] + for rma_line in self.rma_id.rma_line_ids: + existing_purchase_lines.append(rma_line.purchase_order_line_id) + return existing_purchase_lines + + @api.multi + def add_lines(self): + rma_line_obj = self.env['rma.order.line'] + existing_purchase_lines = self._get_existing_purchase_lines() + for line in self.purchase_line_ids: + # Load a PO line only once + if line not in existing_purchase_lines: + data = self._prepare_rma_line_from_po_line(line) + rma_line_obj.create(data) + rma = self.rma_id + data_rma = self._get_rma_data() + rma.write(data_rma) + return {'type': 'ir.actions.act_window_close'} diff --git a/rma_purchase/wizards/rma_add_purchase.xml b/rma_purchase/wizards/rma_add_purchase.xml new file mode 100644 index 00000000..f131f7e6 --- /dev/null +++ b/rma_purchase/wizards/rma_add_purchase.xml @@ -0,0 +1,77 @@ + + + + + rma.add.purchase + rma_add_purchase + +
+ + + + + + + + + + + + + + + + + + + + + +
+
+ +
+
+ + + Add Purchase Order + ir.actions.act_window + rma_add_purchase + rma.order + form + form + new + + + + + + rma.order.line.supplier.form + rma.order + + + + + + + +
diff --git a/rma_purchase/wizards/rma_make_picking.py b/rma_purchase/wizards/rma_make_picking.py new file mode 100644 index 00000000..f80204c9 --- /dev/null +++ b/rma_purchase/wizards/rma_make_picking.py @@ -0,0 +1,38 @@ +# -*- 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 models, fields, exceptions, api, _ + + +class RmaMakePicking(models.TransientModel): + _inherit = 'rma_make_picking.wizard' + + @api.returns('rma.order.line') + def _prepare_item(self, line): + res = super(RmaMakePicking, self)._prepare_item(line) + res['purchase_order_line_id'] = line.purchase_order_line_id.id + return res + + @api.model + def _get_action(self, pickings, procurements): + po_list = [] + for procurement in procurements: + if procurement.purchase_id and \ + procurement.purchase_id.id: + po_list.append(procurement.purchase_id.id) + if len(po_list): + action = self.env.ref('purchase.purchase_rfq') + result = action.read()[0] + result['domain'] = [('id', 'in', po_list)] + return result + else: + action = super(RmaMakePicking, self)._get_action(pickings, + procurements) + return action + + +class RmaMakePickingItem(models.TransientModel): + _inherit = "rma_make_picking.wizard.item" + + purchase_order_line_id = fields.Many2one('purchase.order.line', + string='Purchase Line')