From b413847d8514813251fc693108a483f512a67c3e Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Tue, 8 Nov 2016 17:54:30 +0100 Subject: [PATCH 01/42] Port sale_start_end_dates and sale_rental (#353) --- sale_rental/README.rst | 98 +++++++ sale_rental/__init__.py | 4 + sale_rental/__manifest__.py | 27 ++ sale_rental/data/rental_data.xml | 26 ++ sale_rental/demo/rental_demo.xml | 63 +++++ sale_rental/models/__init__.py | 6 + sale_rental/models/product.py | 51 ++++ sale_rental/models/sale_order.py | 241 ++++++++++++++++++ sale_rental/models/sale_rental.py | 176 +++++++++++++ sale_rental/models/stock.py | 219 ++++++++++++++++ sale_rental/security/sale_rental_security.xml | 20 ++ sale_rental/views/product.xml | 36 +++ sale_rental/views/sale_order.xml | 35 +++ sale_rental/views/sale_rental.xml | 119 +++++++++ sale_rental/views/stock.xml | 35 +++ sale_rental/wizard/__init__.py | 3 + sale_rental/wizard/create_rental_product.py | 87 +++++++ .../wizard/create_rental_product_view.xml | 40 +++ 18 files changed, 1286 insertions(+) create mode 100644 sale_rental/README.rst create mode 100644 sale_rental/__init__.py create mode 100644 sale_rental/__manifest__.py create mode 100644 sale_rental/data/rental_data.xml create mode 100644 sale_rental/demo/rental_demo.xml create mode 100644 sale_rental/models/__init__.py create mode 100644 sale_rental/models/product.py create mode 100644 sale_rental/models/sale_order.py create mode 100644 sale_rental/models/sale_rental.py create mode 100644 sale_rental/models/stock.py create mode 100644 sale_rental/security/sale_rental_security.xml create mode 100644 sale_rental/views/product.xml create mode 100644 sale_rental/views/sale_order.xml create mode 100644 sale_rental/views/sale_rental.xml create mode 100644 sale_rental/views/stock.xml create mode 100644 sale_rental/wizard/__init__.py create mode 100644 sale_rental/wizard/create_rental_product.py create mode 100644 sale_rental/wizard/create_rental_product_view.xml diff --git a/sale_rental/README.rst b/sale_rental/README.rst new file mode 100644 index 00000000..50a662de --- /dev/null +++ b/sale_rental/README.rst @@ -0,0 +1,98 @@ +.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 + +=========== +Sale Rental +=========== + +With this module, you can rent products with Odoo. This module supports: + +* regular rentals, +* rental extensions, +* sale of rented products. + +Configuration +============= + +In the menu *Sales > Products > Product Variants*, on the form view +of a stockable product or consumable, in the *Rental* tab, there is a +button *Create Rental Service* which starts a wizard to generate the +corresponding rental service. + +In the menu *Warehouse > Configuration > Warehouses*, on the form view +of the warehouse, in the *Technical Information* tab, you will see two +additional stock locations: *Rental In* (stock of products to rent) and +*Rental Out* (products currently rented). In the *Warehouse Configuration* tab, +make sure that the option *Rental Allowed* is checked. + +To use the module, you need to have access to the form view of sale +order lines. For that, you must add your user to one of these groups: + +* Manage Product Packaging +* Properties on lines + +Usage +===== + +In a sale order line (form view, not tree view), if you select a rental +service, you can : + +* create a new rental with a start date and an end date: when the sale + order is confirmed, it will generate a delivery order and an incoming + shipment. +* extend an existing rental: the incoming shipment will be postponed to + the end date of the extension. + +In a sale order line, if you select a product that has a corresponding +rental service, you can decide to sell the rented product that the +customer already has. If the sale order is confirmed, the incoming +shipment will be cancelled and a new delivery order will be created with +a stock move from *Rental Out* to *Customers*. + +Please refer to `this screencast ` +to get a demo of the installation, configuration and use of this module +(note that this screencast is for Odoo v7). + +.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas + :alt: Try me on Runbot + :target: https://runbot.odoo-community.org/runbot/167/9.0 + +Known issues / Roadmap +====================== + +This module has the following limitations: + + * No support for planning/agenda of the rented products + * the unit of measure of the rental services must be *Day* (the rental per hours / per week / per month is not supported for the moment) + +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 +------------ + +* Alexis de Lattre + +Maintainer +---------- + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +This module is maintained by the OCA. + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +To contribute to this module, please visit https://odoo-community.org. diff --git a/sale_rental/__init__.py b/sale_rental/__init__.py new file mode 100644 index 00000000..35e7c960 --- /dev/null +++ b/sale_rental/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- + +from . import models +from . import wizard diff --git a/sale_rental/__manifest__.py b/sale_rental/__manifest__.py new file mode 100644 index 00000000..9ef77e5f --- /dev/null +++ b/sale_rental/__manifest__.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +# © 2014-2016 Akretion (http://www.akretion.com) +# @author Alexis de Lattre +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + 'name': 'Rental', + 'version': '9.0.1.0.0', + 'category': 'Sales Management', + 'license': 'AGPL-3', + 'summary': 'Manage Rental of Products', + 'author': 'Akretion, Odoo Community Association (OCA)', + 'website': 'http://www.akretion.com', + 'depends': ['sale_start_end_dates', 'sale_stock'], + 'data': [ + 'data/rental_data.xml', + 'views/sale_order.xml', + 'views/stock.xml', + 'views/sale_rental.xml', + 'wizard/create_rental_product_view.xml', + 'views/product.xml', + 'security/ir.model.access.csv', + 'security/sale_rental_security.xml', + ], + 'demo': ['demo/rental_demo.xml'], + 'installable': True, +} diff --git a/sale_rental/data/rental_data.xml b/sale_rental/data/rental_data.xml new file mode 100644 index 00000000..d01beb25 --- /dev/null +++ b/sale_rental/data/rental_data.xml @@ -0,0 +1,26 @@ + + + + + + + Rent + 100 + + + + + + Sell Rented Product + 100 + + + + + + + diff --git a/sale_rental/demo/rental_demo.xml b/sale_rental/demo/rental_demo.xml new file mode 100644 index 00000000..722e571c --- /dev/null +++ b/sale_rental/demo/rental_demo.xml @@ -0,0 +1,63 @@ + + + + + + + + Rental of one iPad Mini + RENT-A1232 + + + 5 + service + + + + + + + + Rental of one iMac + RENT-A1090 + + + 4 + service + + + + + + + + Rental of one Laptop E5023 + RENT-LAP-E5 + + + 6 + service + + + + + + + + + Inventory for rented products + + + + + + + + + + + diff --git a/sale_rental/models/__init__.py b/sale_rental/models/__init__.py new file mode 100644 index 00000000..ad5a5ab5 --- /dev/null +++ b/sale_rental/models/__init__.py @@ -0,0 +1,6 @@ +# -*- coding: utf-8 -*- + +from . import product +from . import sale_order +from . import sale_rental +from . import stock diff --git a/sale_rental/models/product.py b/sale_rental/models/product.py new file mode 100644 index 00000000..fecb0928 --- /dev/null +++ b/sale_rental/models/product.py @@ -0,0 +1,51 @@ +# -*- coding: utf-8 -*- +# © 2014-2016 Akretion (http://www.akretion.com) +# @author Alexis de Lattre +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from openerp import models, fields, api, _ +from openerp.exceptions import ValidationError + + +class ProductProduct(models.Model): + _inherit = 'product.product' + + # Link rental service -> rented HW product + rented_product_id = fields.Many2one( + 'product.product', string='Related Rented Product', + domain=[('type', 'in', ('product', 'consu'))]) + # Link rented HW product -> rental service + rental_service_ids = fields.One2many( + 'product.product', 'rented_product_id', + string='Related Rental Services') + + @api.one + @api.constrains('rented_product_id', 'must_have_dates', 'type', 'uom_id') + def _check_rental(self): + if self.rented_product_id and self.type != 'service': + raise ValidationError(_( + "The rental product '%s' must be of type 'Service'.") + % self.name) + if self.rented_product_id and not self.must_have_dates: + raise ValidationError(_( + "The rental product '%s' must have the option " + "'Must Have Start and End Dates' checked.") + % self.name) + # In the future, we would like to support all time UoMs + # but it is more complex and requires additionnal developments + day_uom = self.env.ref('product.product_uom_day') + if self.rented_product_id and self.uom_id != day_uom: + raise ValidationError(_( + "The unit of measure of the rental product '%s' must " + "be 'Day'.") % self.name) + + @api.multi + def _need_procurement(self): + # Missing self.ensure_one() in the native code ! + res = super(ProductProduct, self)._need_procurement() + if not res: + for product in self: + if product.type == 'service' and product.rented_product_id: + return True + # TODO find a replacement for soline.rental_type == 'new_rental') + return res diff --git a/sale_rental/models/sale_order.py b/sale_rental/models/sale_order.py new file mode 100644 index 00000000..92ae86b3 --- /dev/null +++ b/sale_rental/models/sale_order.py @@ -0,0 +1,241 @@ +# -*- coding: utf-8 -*- +# © 2014-2016 Akretion (http://www.akretion.com) +# @author Alexis de Lattre +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from openerp import models, fields, api, _ +from openerp.exceptions import ValidationError +from openerp.exceptions import UserError +from openerp.tools import float_compare +from dateutil.relativedelta import relativedelta +import openerp.addons.decimal_precision as dp +import logging + +logger = logging.getLogger(__name__) +# TODO : block if we sell a rented product already sold => state + + +class SaleOrder(models.Model): + _inherit = 'sale.order' + + @api.multi + def action_confirm(self): + res = super(SaleOrder, self).action_confirm() + for order in self: + for line in order.order_line: + if line.rental_type == 'new_rental': + self.env['sale.rental'].create(line._prepare_rental()) + elif line.rental_type == 'rental_extension': + line.extension_rental_id.in_move_id.date_expected =\ + line.end_date + line.extension_rental_id.in_move_id.date = line.end_date + elif line.sell_rental_id: + if line.sell_rental_id.out_move_id.state != 'done': + raise UserError(_( + 'Cannot sell the rental %s because it has ' + 'not been delivered') + % line.sell_rental_id.display_name) + line.sell_rental_id.in_move_id.action_cancel() + return res + + +class SaleOrderLine(models.Model): + _inherit = 'sale.order.line' + + rental = fields.Boolean(string='Rental') + can_sell_rental = fields.Boolean(string='Can Sell from Rental') + rental_type = fields.Selection([ + ('new_rental', 'New Rental'), + ('rental_extension', 'Rental Extension'), + ], 'Rental Type', + readonly=True, states={'draft': [('readonly', False)]}) + extension_rental_id = fields.Many2one( + 'sale.rental', string='Rental to Extend') + rental_qty = fields.Float( + string='Rental Quantity', + digits=dp.get_precision('Product Unit of Measure'), + help="Indicate the number of items that will be rented.") + sell_rental_id = fields.Many2one( + 'sale.rental', string='Rental to Sell') + + @api.one + @api.constrains( + 'rental_type', 'extension_rental_id', 'start_date', 'end_date', + 'rental_qty', 'product_uom_qty', 'product_id', 'must_have_dates') + def _check_sale_line_rental(self): + if self.rental_type == 'rental_extension': + if not self.extension_rental_id: + raise ValidationError(_( + "Missing 'Rental to Extend' on the sale order line with " + "rental service %s") + % self.product_id.name) + if self.rental_qty != self.extension_rental_id.rental_qty: + raise ValidationError(_( + "On the sale order line with rental service %s, " + "you are trying to extend a rental with a rental " + "quantity (%s) that is different from the quantity " + "of the original rental (%s). This is not supported.") % ( + self.product_id.name, + self.rental_qty, + self.extension_rental_id.rental_qty)) + if self.rental_type in ('new_rental', 'rental_extension'): + if not self.product_id.rented_product_id: + raise ValidationError(_( + "On the 'new rental' sale order line with product '%s', " + "we should have a rental service product !") % ( + self.product_id.name)) + if self.product_uom_qty != self.rental_qty * self.number_of_days: + raise ValidationError(_( + "On the sale order line with product '%s' " + "the Product Quantity (%s) should be the " + "number of days (%s) " + "multiplied by the Rental Quantity (%s).") % ( + self.product_id.name, self.product_uom_qty, + self.number_of_days, self.rental_qty)) + # TODO: If we confirm self.must_have_dates in a related field + # we can remove this check + if not self.must_have_dates: + raise ValidationError(_( + "On the rental sale order line with product %s " + "the must have dates option should be enabled") + % self.product_id.name) + # the module sale_start_end_dates checks that, when we have + # must_have_dates, we have start + end dates + elif self.sell_rental_id: + if self.product_uom_qty != self.sell_rental_id.rental_qty: + raise ValidationError(_( + "On the sale order line with product %s " + "you are trying to sell a rented product with a " + "quantity (%s) that is different from the rented " + "quantity (%s). This is not supported.") % ( + self.product_id.name, + self.product_uom_qty, + self.sell_rental_id.rental_qty)) + + @api.multi + def _prepare_rental(self): + self.ensure_one() + return {'start_order_line_id': self.id} + + @api.multi + def _get_rental_date_planned(self): + self.ensure_one() + return self.start_date + + @api.multi + def _prepare_order_line_procurement(self, group_id=False): + self.ensure_one() + res = super(SaleOrderLine, self)._prepare_order_line_procurement( + group_id=group_id) + if ( + self.product_id.rented_product_id and + self.rental_type == 'new_rental'): + res.update({ + 'product_id': self.product_id.rented_product_id.id, + 'product_qty': self.rental_qty, + 'product_uom': self.product_id.rented_product_id.uom_id.id, + 'location_id': + self.order_id.warehouse_id.rental_out_location_id.id, + 'route_ids': + [(6, 0, [self.order_id.warehouse_id.rental_route_id.id])], + 'date_planned': self._get_rental_date_planned(), + }) + elif self.sell_rental_id: + res['route_ids'] = [(6, 0, [ + self.order_id.warehouse_id.sell_rented_product_route_id.id])] + return res + + @api.onchange('product_id') + def rental_product_id_change(self): + res = {} + if self.product_id: + if self.product_id.rented_product_id: + self.rental = True + self.can_sell_rental = False + self.sell_rental_id = False + if not self.rental_type: + self.rental_type = 'new_rental' + elif ( + self.rental_type == 'new_rental' and + self.rental_qty and self.order_id.warehouse_id): + product_uom = self.product_id.rented_product_id.uom_id + warehouse = self.order_id.warehouse_id + rental_in_location = warehouse.rental_in_location_id + rented_product_ctx = self.with_context( + location=rental_in_location.id).product_id.\ + rented_product_id + in_location_available_qty = rented_product_ctx.\ + qty_available - rented_product_ctx.outgoing_qty + compare_qty = float_compare( + in_location_available_qty, self.rental_qty, + precision_rounding=product_uom.rounding) + if compare_qty == -1: + res['warning'] = { + 'title': _("Not enough stock !"), + 'message': _( + "You want to rent %.2f %s but you only " + "have %.2f %s currently available on the " + "stock location '%s' ! Make sure that you " + "get some units back in the mean time or " + "re-supply the stock location '%s'.") % ( + self.rental_qty, + product_uom.name, + in_location_available_qty, + product_uom.name, + rental_in_location.name, + rental_in_location.name) + } + elif self.product_id.rental_service_ids: + self.can_sell_rental = True + self.rental = False + self.rental_type = False + self.rental_qty = 0 + self.extension_rental_id = False + else: + self.rental_type = False + self.rental = False + self.rental_qty = 0 + self.extension_rental_id = False + self.can_sell_rental = False + self.sell_rental_id = False + else: + self.rental_type = False + self.rental = False + self.rental_qty = 0 + self.extension_rental_id = False + self.can_sell_rental = False + self.sell_rental_id = False + return res + + @api.onchange('extension_rental_id') + def extension_rental_id_change(self): + if ( + self.product_id and + self.rental_type == 'rental_extension' and + self.extension_rental_id): + if self.extension_rental_id.rental_product_id != self.product_id: + raise UserError(_( + "The Rental Service of the Rental Extension you just " + "selected is '%s' and it's not the same as the " + "Product currently selected in this Sale Order Line.") + % self.extension_rental_id.rental_product_id.name) + initial_end_date = fields.Date.from_string( + self.extension_rental_id.end_date) + self.start_date = initial_end_date + relativedelta(days=1) + self.rental_qty = self.extension_rental_id.rental_qty + + @api.onchange('sell_rental_id') + def sell_rental_id_change(self): + if self.sell_rental_id: + self.product_uom_qty = self.sell_rental_id.rental_qty + + @api.onchange('rental_qty', 'number_of_days', 'product_id') + def rental_qty_number_of_days_change(self): + if self.product_id.rented_product_id: + qty = self.rental_qty * self.number_of_days + self.product_uom_qty = qty + + @api.onchange('rental_type') + def rental_type_change(self): + if self.rental_type == 'new_rental': + self.extension_rental_id = False diff --git a/sale_rental/models/sale_rental.py b/sale_rental/models/sale_rental.py new file mode 100644 index 00000000..28ff6faa --- /dev/null +++ b/sale_rental/models/sale_rental.py @@ -0,0 +1,176 @@ +# -*- coding: utf-8 -*- +# © 2014-2016 Akretion (http://www.akretion.com) +# @author Alexis de Lattre +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from openerp import models, fields, api +import logging + +logger = logging.getLogger(__name__) +# TODO : block if we sell a rented product already sold => state + + +class SaleRental(models.Model): + _name = 'sale.rental' + _description = "Rental" + _order = "id desc" + _rec_name = "display_name" + + @api.one + @api.depends( + 'start_order_line_id', 'extension_order_line_ids.end_date', + 'extension_order_line_ids.state', 'start_order_line_id.end_date') + def _compute_display_name_field(self): + self.display_name = u'[%s] %s - %s > %s (%s)' % ( + self.partner_id.name, + self.rented_product_id.name, + self.start_date, + self.end_date, + self._fields['state'].convert_to_export(self.state, self.env)) + + @api.one + @api.depends( + 'start_order_line_id.order_id.state', + 'start_order_line_id.procurement_ids.move_ids.state', + 'start_order_line_id.procurement_ids.move_ids.move_dest_id.state', + 'sell_order_line_ids.procurement_ids.move_ids.state', + ) + def _compute_procurement_and_move(self): + procurement = False + in_move = False + out_move = False + sell_procurement = False + sell_move = False + state = False + if ( + self.start_order_line_id and + self.start_order_line_id.procurement_ids): + + procurement = self.start_order_line_id.procurement_ids[0] + if procurement.move_ids: + for move in procurement.move_ids: + if move.move_dest_id: + out_move = move + in_move = move.move_dest_id + if ( + self.sell_order_line_ids and + self.sell_order_line_ids[0].procurement_ids): + sell_procurement =\ + self.sell_order_line_ids[0].procurement_ids[0] + if sell_procurement.move_ids: + sell_move = sell_procurement.move_ids[0] + state = 'ordered' + if out_move and in_move: + if out_move.state == 'done': + state = 'out' + if out_move.state == 'done' and in_move.state == 'done': + state = 'in' + if ( + out_move.state == 'done' and + in_move.state == 'cancel' and + sell_procurement): + state = 'sell_progress' + if sell_move and sell_move.state == 'done': + state = 'sold' + if self.start_order_line_id.order_id.state == 'cancel': + state = 'cancel' + self.procurement_id = procurement + self.in_move_id = in_move + self.out_move_id = out_move + self.state = state + self.sell_procurement_id = sell_procurement + self.sell_move_id = sell_move + + @api.one + @api.depends( + 'extension_order_line_ids.end_date', 'extension_order_line_ids.state', + 'start_order_line_id.end_date') + def _compute_end_date(self): + end_date = False + if self.extension_order_line_ids: + for extension in self.extension_order_line_ids: + if extension.state not in ('cancel', 'draft'): + if extension.end_date > end_date: + end_date = extension.end_date + if not end_date and self.start_order_line_id: + end_date = self.start_order_line_id.end_date + self.end_date = end_date + + display_name = fields.Char( + compute='_compute_display_name_field', string='Display Name', + readonly=True) + start_order_line_id = fields.Many2one( + 'sale.order.line', string='Rental Sale Order Line', readonly=True) + start_date = fields.Date( + related='start_order_line_id.start_date', readonly=True, store=True) + rental_product_id = fields.Many2one( + 'product.product', related='start_order_line_id.product_id', + string="Rental Service", readonly=True) + rented_product_id = fields.Many2one( + 'product.product', + related='start_order_line_id.product_id.rented_product_id', + string="Rented Product", readonly=True) + rental_qty = fields.Float( + related='start_order_line_id.rental_qty', readonly=True) + start_order_id = fields.Many2one( + 'sale.order', related='start_order_line_id.order_id', + string='Rental Sale Order', readonly=True) + company_id = fields.Many2one( + 'res.company', related='start_order_line_id.order_id.company_id', + string='Company', readonly=True) + partner_id = fields.Many2one( + 'res.partner', related='start_order_line_id.order_id.partner_id', + string='Customer', readonly=True, store=True) + procurement_id = fields.Many2one( + 'procurement.order', string="Procurement", readonly=True, + compute='_compute_procurement_and_move', store=True) + out_move_id = fields.Many2one( + 'stock.move', compute='_compute_procurement_and_move', + string='Outgoing Stock Move', readonly=True, store=True) + in_move_id = fields.Many2one( + 'stock.move', compute='_compute_procurement_and_move', + string='Return Stock Move', readonly=True, store=True) + out_state = fields.Selection( + related='out_move_id.state', + string='State of the Outgoing Stock Move', readonly=True) + in_state = fields.Selection( + related='in_move_id.state', + string='State of the Return Stock Move', readonly=True) + out_picking_id = fields.Many2one( + 'stock.picking', related='out_move_id.picking_id', + string='Delivery Order', readonly=True) + in_picking_id = fields.Many2one( + 'stock.picking', related='in_move_id.picking_id', + string='Return Picking', readonly=True) + extension_order_line_ids = fields.One2many( + 'sale.order.line', 'extension_rental_id', + string='Rental Extensions', readonly=True) + sell_order_line_ids = fields.One2many( + 'sale.order.line', 'sell_rental_id', + string='Sell Rented Product', readonly=True) + sell_procurement_id = fields.Many2one( + 'procurement.order', string="Sell Procurement", readonly=True, + compute='_compute_procurement_and_move', store=True) + sell_move_id = fields.Many2one( + 'stock.move', compute='_compute_procurement_and_move', + string='Sell Stock Move', readonly=True, store=True) + sell_state = fields.Selection( + related='sell_move_id.state', + string='State of the Sell Stock Move', readonly=True) + sell_picking_id = fields.Many2one( + 'stock.picking', related='sell_move_id.picking_id', + string='Sell Delivery Order', readonly=True) + end_date = fields.Date( + compute='_compute_end_date', string='End Date (extensions included)', + readonly=True, + help="End Date of the Rental, taking into account all the " + "extensions sold to the customer.") + state = fields.Selection([ + ('ordered', 'Ordered'), + ('out', 'Out'), + ('sell_progress', 'Sell in progress'), + ('sold', 'Sold'), + ('in', 'Back In'), + ('cancel', 'Cancelled'), + ], string='State', compute='_compute_procurement_and_move', + readonly=True, store=True) diff --git a/sale_rental/models/stock.py b/sale_rental/models/stock.py new file mode 100644 index 00000000..e5e377c0 --- /dev/null +++ b/sale_rental/models/stock.py @@ -0,0 +1,219 @@ +# -*- coding: utf-8 -*- +# © 2014-2016 Akretion (http://www.akretion.com) +# @author Alexis de Lattre +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from openerp import models, fields, api, _ +from openerp.exceptions import UserError +import logging + +logger = logging.getLogger(__name__) + + +class StockWarehouse(models.Model): + _inherit = "stock.warehouse" + + rental_view_location_id = fields.Many2one( + 'stock.location', 'Parent Rental', domain=[('usage', '=', 'view')]) + rental_in_location_id = fields.Many2one( + 'stock.location', 'Rental In', domain=[('usage', '!=', 'view')]) + rental_out_location_id = fields.Many2one( + 'stock.location', 'Rental Out', domain=[('usage', '!=', 'view')]) + rental_allowed = fields.Boolean('Rental Allowed') + rental_route_id = fields.Many2one( + 'stock.location.route', string='Rental Route') + sell_rented_product_route_id = fields.Many2one( + 'stock.location.route', string='Sell Rented Product Route') + + @api.multi + def _get_rental_push_pull_rules(self): + self.ensure_one() + route_obj = self.env['stock.location.route'] + try: + rental_route = self.env.ref('sale_rental.route_warehouse0_rental') + except: + rental_routes = route_obj.search([('name', '=', _('Rent'))]) + rental_route = rental_routes and rental_routes[0] or False + if not rental_route: + raise UserError(_("Can't find any generic 'Rent' route.")) + try: + sell_rented_product_route = self.env.ref( + 'sale_rental.route_warehouse0_sell_rented_product') + except: + sell_rented_product_routes = route_obj.search( + [('name', '=', _('Sell Rented Product'))]) + sell_rented_product_route =\ + sell_rented_product_routes and sell_rented_product_routes[0]\ + or False + if not sell_rented_product_route: + raise UserError(_( + "Can't find any generic 'Sell Rented Product' route.")) + + if not self.rental_in_location_id: + raise UserError(_( + "The Rental Input stock location is not set on the " + "warehouse %s") % self.name) + if not self.rental_out_location_id: + raise UserError(_( + "The Rental Output stock location is not set on the " + "warehouse %s") % self.name) + rental_pull_rule = { + 'name': self.env['stock.warehouse']._format_rulename( + self, self.rental_in_location_id, + self.rental_out_location_id), + 'location_id': self.rental_out_location_id.id, + 'location_src_id': self.rental_in_location_id.id, + 'route_id': rental_route.id, + 'action': 'move', + 'picking_type_id': self.out_type_id.id, + 'warehouse_id': self.id, + } + rental_push_rule = { + 'name': self.env['stock.warehouse']._format_rulename( + self, self.rental_out_location_id, + self.rental_in_location_id), + 'location_from_id': self.rental_out_location_id.id, + 'location_dest_id': self.rental_in_location_id.id, + 'route_id': rental_route.id, + 'auto': 'auto', + 'picking_type_id': self.in_type_id.id, + 'warehouse_id': self.id, + } + customer_loc = self.env.ref('stock.stock_location_customers') + sell_rented_product_pull_rule = { + 'name': self.env['stock.warehouse']._format_rulename( + self, self.rental_out_location_id, customer_loc), + 'location_id': customer_loc.id, + 'location_src_id': self.rental_out_location_id.id, + 'route_id': sell_rented_product_route.id, + 'action': 'move', + 'picking_type_id': self.out_type_id.id, + 'warehouse_id': self.id, + } + res = { + 'procurement.rule': [ + rental_pull_rule, + sell_rented_product_pull_rule], + 'stock.location.path': [rental_push_rule], + } + return res + + @api.multi + def write(self, vals): + if 'rental_allowed' in vals: + rental_route = self.env.ref('sale_rental.route_warehouse0_rental') + sell_rented_route = self.env.ref( + 'sale_rental.route_warehouse0_sell_rented_product') + if vals.get('rental_allowed'): + for warehouse in self: + # create stock locations + slo = self.env['stock.location'] + if not warehouse.rental_view_location_id: + view_loc = slo.create({ + 'name': _('Rental'), + 'location_id': self.view_location_id.id, + 'usage': 'view', + }) + logger.debug( + 'New view rental stock location created ID %d', + view_loc.id) + warehouse.rental_view_location_id = view_loc.id + if not warehouse.rental_in_location_id: + in_loc = slo.create({ + 'name': _('Rental In'), + 'location_id': + warehouse.rental_view_location_id.id, + }) + logger.debug( + 'New in rental stock location created ID %d', + in_loc.id) + warehouse.rental_in_location_id = in_loc.id + if not warehouse.rental_out_location_id: + out_loc = slo.create({ + 'name': _('Rental Out'), + 'location_id': + warehouse.rental_view_location_id.id, + }) + logger.debug( + 'New out rental stock location created ID %d', + out_loc.id) + warehouse.rental_out_location_id = out_loc.id + warehouse.write({ + 'route_ids': [(4, rental_route.id)], + 'rental_route_id': rental_route.id, + 'sell_rented_product_route_id': sell_rented_route.id, + }) + for obj, rules_list in\ + self._get_rental_push_pull_rules().iteritems(): + for rule in rules_list: + self.env[obj].create(rule) + else: + for warehouse in self: + pull_rules_to_delete = self.env['procurement.rule'].search( + [ + ('route_id', 'in', ( + warehouse.rental_route_id.id, + warehouse.sell_rented_product_route_id.id)), + ('location_src_id', 'in', ( + warehouse.rental_out_location_id.id, + warehouse.rental_in_location_id.id)), + ('action', '=', 'move')]) + pull_rules_to_delete.unlink() + push_rule_to_delete =\ + self.env['stock.location.path'].search([ + ('route_id', '=', warehouse.rental_route_id.id), + ('location_from_id', '=', + warehouse.rental_out_location_id.id), + ('location_dest_id', '=', + warehouse.rental_in_location_id.id)]) + push_rule_to_delete.unlink() + warehouse.write({ + 'route_ids': [(3, rental_route.id)], + 'rental_route_id': False, + 'sell_rented_product_route_id': False, + }) + return super(StockWarehouse, self).write(vals) + + +class StockLocationPath(models.Model): + _inherit = 'stock.location.path' + + @api.model + def _prepare_push_apply(self, rule, move): + '''Inherit to write the end date of the rental on the return move''' + vals = super(StockLocationPath, self)._prepare_push_apply(rule, move) + if ( + move.procurement_id and + move.procurement_id.location_id == + move.procurement_id.warehouse_id.rental_out_location_id and + move.procurement_id.sale_line_id and + move.procurement_id.sale_line_id.rental_type == 'new_rental'): + rental_end_date = move.procurement_id.sale_line_id.end_date + vals['date'] = vals['date_expected'] = rental_end_date + return vals + + +class StockInventory(models.Model): + _inherit = 'stock.inventory' + + @api.multi + def create_demo_and_validate(self): + silo = self.env['stock.inventory.line'] + demo_inv = self.env.ref('sale_rental.rental_inventory') + rental_in_loc = self.env.ref('stock.warehouse0').rental_in_location_id + products = [ + ('product.product_product_6', 56), + ('product.product_product_8', 46), + ('product.product_product_25', 2), + ] + for (product_xmlid, qty) in products: + product = self.env.ref(product_xmlid) + silo.create({ + 'product_id': product.id, + 'product_uom_id': product.uom_id.id, + 'inventory_id': demo_inv.id, + 'product_qty': qty, + 'location_id': rental_in_loc.id, + }) + demo_inv.action_done() + return True diff --git a/sale_rental/security/sale_rental_security.xml b/sale_rental/security/sale_rental_security.xml new file mode 100644 index 00000000..a7eeb3e5 --- /dev/null +++ b/sale_rental/security/sale_rental_security.xml @@ -0,0 +1,20 @@ + + + + + + + + Sale rental multi-company + + ['|', ('company_id', '=', False), ('company_id', 'child_of', [user.company_id.id])] + + + + + + + diff --git a/sale_rental/views/product.xml b/sale_rental/views/product.xml new file mode 100644 index 00000000..c865dd68 --- /dev/null +++ b/sale_rental/views/product.xml @@ -0,0 +1,36 @@ + + + + + + + + rental.product.product.form + product.product + + + + + + + + + +