You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.6 KiB

  1. =======================
  2. Barcode action launcher
  3. =======================
  4. This module allows to use barcodes as launchers of actions.
  5. The action will launch a function that uses the barcode in order to return an action.
  6. Usage
  7. =====
  8. Actions must be configured with the following data in the context:
  9. * model: Model where we can find the method (required)
  10. * method: Method to execute (required)
  11. * res_id: Id as base (optional)
  12. The method must return an action. Installing this module with demo data will
  13. install a demo application that allows the system administrator to find a
  14. partner by the external reference encoded in a barcode.
  15. Go to *Settings / Find partners* and scan a barcode that contains the
  16. internal reference of an existing partner. As soon as you read the barcode
  17. the system will redirect you to that partner's form view.
  18. Technical implementation of this example:
  19. Action::
  20. <act_window id="res_partner_find"
  21. name="Find Partner"
  22. res_model="barcode.action"
  23. view_mode="form"
  24. view_type="form"
  25. context="{'default_model': 'res.partner', 'default_method': 'find_res_partner_by_ref_using_barcode'}"
  26. target="new"/>
  27. <menuitem id="menu_orders_customers" name="Find partners"
  28. action="res_partner_find"
  29. parent="base.menu_administration"/>
  30. Python code::
  31. import json
  32. from odoo import api, models, _
  33. from odoo.tools.safe_eval import safe_eval
  34. class ResPartner(models.Model):
  35. _inherit = 'res.partner'
  36. @api.multi
  37. def find_res_partner_by_ref_using_barcode(self, barcode):
  38. partner = self.search([('ref', '=', barcode)], limit=1)
  39. if not partner:
  40. action = self.env.ref('res_partner_find')
  41. result = action.read()[0]
  42. context = safe_eval(result['context'])
  43. context.update({
  44. 'default_state': 'warning',
  45. 'default_status': _('Partner with Internal Reference '
  46. '%s cannot be found') % barcode
  47. })
  48. result['context'] = json.dumps(context)
  49. return result
  50. action = self.env.ref('base.action_partner_form')
  51. result = action.read()[0]
  52. res = self.env.ref('base.view_partner_form', False)
  53. result['views'] = [(res and res.id or False, 'form')]
  54. result['res_id'] = partner.id
  55. return result
  56. Contributors
  57. ============
  58. * Enric Tobella <etobella@creublanca.es>
  59. * Jordi Ballester <jordi.ballester@eficent.com>