mirror of
https://github.com/OCA/report-print-send.git
synced 2025-02-16 07:11:31 +02:00
[ADD] Test cases and simplify code
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user