diff --git a/stock_picking_auto_print/__manifest__.py b/stock_picking_auto_print/__manifest__.py index 5255e8a..b3001b9 100644 --- a/stock_picking_auto_print/__manifest__.py +++ b/stock_picking_auto_print/__manifest__.py @@ -18,5 +18,6 @@ 'data': [ 'views/ir_action_report_view.xml' ], + 'maintainers': ['bodedra'], 'installable': True, } diff --git a/stock_picking_auto_print/models/stock_picking.py b/stock_picking_auto_print/models/stock_picking.py index 9227122..2bae6f2 100644 --- a/stock_picking_auto_print/models/stock_picking.py +++ b/stock_picking_auto_print/models/stock_picking.py @@ -8,132 +8,77 @@ from odoo import api, models class StockPicking(models.Model): _inherit = "stock.picking" + @api.multi + def _stock_picking_default_auto_print_report(self): + user_company_id = self.env.user.company_id.id + report_action_pool = self.env['ir.actions.report'] + for picking in self.filtered(lambda p: p.sale_id): + default_report_id = False + + # Find picking defaults reports + default_report_ids = report_action_pool.search( + [('model', '=', 'stock.picking'), + ('is_default_report', '=', True)]).ids + + # Check Partner country id + country_id = False + if picking.partner_id.country_id: + country_id = picking.partner_id.country_id.id + + if default_report_ids and country_id: + # Filter report with Country and Company + report_ids = report_action_pool.search( + [('country_id', '=', country_id), + ('company_id', '=', user_company_id), + ('id', 'in', default_report_ids)], + limit=1) + if report_ids: + default_report_id = report_ids.id + + if not default_report_id and country_id: + # Filter report with Country + report_ids = report_action_pool.search( + [('country_id', '=', country_id), + ('id', 'in', default_report_ids)], + limit=1) + if report_ids: + default_report_id = report_ids.id + + if not default_report_id: + # Filter report with Company + report_ids = report_action_pool.search( + [('company_id', '=', user_company_id), + ('id', 'in', default_report_ids)], + limit=1) + if report_ids: + default_report_id = report_ids.id + + if not default_report_id: + default_report_id = self.env.ref( + 'stock.action_report_picking').with_context( + landscape=True).report_action(picking).get('id') + action_report = report_action_pool.browse( + default_report_id) + + try: + action_report.print_document(picking.id) + except: + pass + return True + @api.multi def write(self, vals): res = super(StockPicking, self).write(vals) if 'date_done' in vals: - user_company_id = self.env.user.company_id.id - report_action_pool = self.env['ir.actions.report'] - for picking in self.filtered(lambda p: p.sale_id): - # Find next picking and it's state - sale_picking_ids = picking.sale_id.picking_ids.filtered( - lambda p: p.state != 'done') - for sp in sale_picking_ids: - if sp.state == 'assigned': - default_report_id = False - # Find picking defaults reports - default_report_ids = report_action_pool.search( - [('model', '=', 'stock.picking'), - ('is_default_report', '=', True)]).ids - # Check Partner country id - country_id = False - if sp.partner_id.country_id: - country_id = sp.partner_id.country_id.id - - if default_report_ids: - # Filter report with Country and Company - report_ids = report_action_pool.search( - [('country_id', '=', country_id), - ('company_id', '=', user_company_id), - ('id', 'in', default_report_ids)], - limit=1) - if report_ids: - default_report_id = report_ids.id - - if not default_report_id: - # Filter report with Company - report_ids = report_action_pool.search( - [('company_id', '=', user_company_id), - ('id', 'in', default_report_ids)], - limit=1) - if report_ids: - default_report_id = report_ids.id - - if not default_report_id and country_id: - # Filter report with Country - report_ids = report_action_pool.search( - [('country_id', '=', country_id), - ('id', 'in', default_report_ids)], - limit=1) - if report_ids: - default_report_id = report_ids.id - - if not default_report_id: - default_report_id = self.env.ref( - 'stock.action_report_picking').with_context( - landscape=True).report_action(sp).get('id') - action_report = report_action_pool.browse( - default_report_id) - - try: - action_report.print_document(sp.id) - except: - pass + self._stock_picking_default_auto_print_report() return res @api.multi def action_assign(self): res = super(StockPicking, self).action_assign() - user_company_id = self.env.user.company_id.id - report_action_pool = self.env['ir.actions.report'] for picking in self: if (picking.picking_type_code == 'outgoing' or picking.location_dest_id.name == 'Output') and \ picking.state == 'assigned': - default_report_id = False - - # Find picking defaults reports - default_report_ids = report_action_pool.search( - [('model', '=', 'stock.picking'), - ('is_default_report', '=', True)]).ids - - # defaults are configured - if default_report_ids: - # Find country id - country_id = False - if picking.partner_id.country_id: - country_id = picking.partner_id.country_id.id - - # Filter report with Country and Company - report_ids = report_action_pool.search( - [('country_id', '=', country_id), - ('company_id', '=', user_company_id), - ('id', 'in', default_report_ids)], - limit=1) - if report_ids: - default_report_id = report_ids.id - - # just look for company record without a country - if not default_report_id: - # Filter report with Company and no country - report_ids = report_action_pool.search( - [('country_id', '=', False), - ('company_id', '=', user_company_id), - ('id', 'in', default_report_ids)], - limit=1) - if report_ids: - default_report_id = report_ids.id - - # just look for a country report without company - if not default_report_id and country_id: - # Filter report with Country - report_ids = report_action_pool.search( - [('country_id', '=', country_id), - ('company_id', '=', False) - ('id', 'in', default_report_ids)], - limit=1) - if report_ids: - default_report_id = report_ids.id - - # defaults not configured - else: - default_report_id = self.env.ref( - 'stock.action_report_picking').with_context( - landscape=True).report_action(picking).get('id') - action_report = report_action_pool.browse(default_report_id) - - try: - action_report.print_document(picking.id) - except: - pass + picking._stock_picking_default_auto_print_report() return res diff --git a/stock_picking_auto_print/tests/__init__.py b/stock_picking_auto_print/tests/__init__.py new file mode 100644 index 0000000..c9f63eb --- /dev/null +++ b/stock_picking_auto_print/tests/__init__.py @@ -0,0 +1,5 @@ +# Copyright (C) 2019 IBM Corp. +# Copyright (C) 2019 Open Source Integrators +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import test_stock_picking_auto_print diff --git a/stock_picking_auto_print/tests/test_stock_picking_auto_print.py b/stock_picking_auto_print/tests/test_stock_picking_auto_print.py new file mode 100644 index 0000000..04eca89 --- /dev/null +++ b/stock_picking_auto_print/tests/test_stock_picking_auto_print.py @@ -0,0 +1,91 @@ +# Copyright (C) 2019 IBM Corp. +# Copyright (C) 2019 Open Source Integrators +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo.tests.common import TransactionCase + + +class StockPikcing(TransactionCase): + def setUp(self): + super(StockPikcing, self).setUp() + + self.stock_location = self.env.ref('stock.stock_location_stock') + self.customer_location = self.env.ref('stock.stock_location_customers') + self.scrapped_location = self.env.ref('stock.stock_location_scrapped') + + uom_unit = self.env.ref('product.product_uom_unit') + + self.product_A = self.env['product.product'].create({ + 'name': 'Product A', + 'type': 'product', + 'list_price': 1.0, + 'categ_id': self.env.ref('product.product_category_all').id, + 'uom_id': uom_unit.id + }) + + self.country_us = self.env['res.country'].search( + [('code', 'like', 'US')], limit=1) + self.partner = self.env['res.partner'].create( + {'name': 'BOdedra', 'country_id': self.country_us.id}) + + self.so = self.env['sale.order'].create({ + 'partner_id': self.partner.id, + 'partner_invoice_id': self.partner.id, + 'partner_shipping_id': self.partner.id, + 'order_line': [(0, 0, {'name': self.product_A.name, + 'product_id': self.product_A.id, + 'product_uom_qty': 2, + 'product_uom': self.product_A.uom_id.id, + 'price_unit': self.product_A.list_price})], + 'pricelist_id': self.env.ref('product.list0').id, + 'picking_policy': 'direct', + }) + + self.uom_unit = self.env.ref('product.product_uom_unit') + + def test_stock_picking_auto_print(self): + """ Auto print when DO is ready or done + """ + self.env['stock.quant']._update_available_quantity( + self.product_A, self.stock_location, 2) + self.so.action_confirm() + for picking in self.so.picking_ids: + picking.move_lines._do_unreserve() + + # Unreserve picking and update default report configuration + picking.move_lines._do_unreserve() + + # Made Delivery Slip report as a default report + deliveryslip_report = self.env['ir.actions.report'].search( + [('report_name', '=', 'stock.report_deliveryslip')]) + + deliveryslip_report.write({'is_default_report': True}) + picking.action_assign() + + # Remove country ID from Delivery Slip report + deliveryslip_report.write({'country_id': self.country_us.id, + 'company_id': False}) + picking.action_confirm() + picking.action_assign() + + picking.move_lines._do_unreserve() + + # Unreserve picking and update default report configuration + picking.do_unreserve() + + # Remove company ID from Delivery Slip report + deliveryslip_report.write( + {'company_id': self.env.user.company_id.id, + 'country_id': False}) + picking.action_confirm() + picking.action_assign() + + picking.do_unreserve() + picking.move_lines._do_unreserve() + + deliveryslip_report.write( + {'company_id': self.env.user.company_id.id, + 'country_id': self.country_us.id}) + picking.action_confirm() + picking.action_assign() + picking.action_done()