sale_discount_approval.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. from openerp import api, models, fields
  2. from openerp.osv import fields, osv
  3. from openerp import SUPERUSER_ID
  4. ##############################################################sale settings##############################################################
  5. class Sale_config_settings(osv.TransientModel):
  6. _inherit = 'sale.config.settings'
  7. _columns = {
  8. 'limit_discount': fields.integer('Discount limit requires approval %', required=True,
  9. help="Discount after which approval of sale is required."),
  10. 'module_sale_discount_approval': fields.boolean("Force two levels of approvals",
  11. help='Provide a double validation mechanism for sale exceeding minimum discount.\n'
  12. ),
  13. }
  14. _defaults = {
  15. 'limit_discount': 40,
  16. }
  17. def get_default_limit_discount(self, cr, uid, ids, context=None):
  18. ir_values = self.pool.get('ir.values')
  19. limit_discount = ir_values.get_default(cr, uid, 'sale.config.settings', 'limit_discount')
  20. return {
  21. 'limit_discount': limit_discount,
  22. }
  23. def set_limit_discount(self, cr, uid, ids, context=None):
  24. ir_values = self.pool.get('ir.values')
  25. wizard = self.browse(cr, uid, ids)[0]
  26. if wizard.limit_discount:
  27. limit_discount = wizard.limit_discount
  28. ir_values.set_default(cr, SUPERUSER_ID, 'sale.config.settings', 'limit_discount', limit_discount)
  29. def get_default_module_sale_discount_approval(self, cr, uid, ids, context=None):
  30. ir_values = self.pool.get('ir.values')
  31. module_sale_discount_approval = ir_values.get_default(cr, uid, 'sale.config.settings',
  32. 'module_sale_discount_approval')
  33. return {
  34. 'module_sale_discount_approval': module_sale_discount_approval == 'True',
  35. }
  36. def set_module_sale_discount_approval(self, cr, uid, ids, context=None):
  37. ir_values = self.pool.get('ir.values')
  38. wizard = self.browse(cr, uid, ids)[0]
  39. if wizard.module_sale_discount_approval:
  40. module_sale_discount_approval = 'True'
  41. else:
  42. module_sale_discount_approval = 'False'
  43. ir_values.set_default(cr, SUPERUSER_ID, 'sale.config.settings', 'module_sale_discount_approval',
  44. module_sale_discount_approval)
  45. #######################################################sale order workflow##############################################################
  46. class SaleInherit(osv.Model):
  47. _inherit = 'sale.order'
  48. _columns = {
  49. 'state': fields.selection([('draft', 'Draft Quotation'),
  50. ('sent', 'Quotation Sent'),
  51. ('cancel', 'Cancelled'),
  52. ('waiting_date', 'Waiting Schedule'),
  53. ('waitingapproval', 'Waiting Approval'),
  54. ('progress', 'Sales Order'),
  55. ('manual', 'Sale to Invoice'),
  56. ('shipping_except', 'Shipping Exception'),
  57. ('invoice_except', 'Invoice Exception'),
  58. ('done', 'Done')], required=True, track_visibility='onchange'),
  59. }
  60. def action_button_confirm(self, cr, uid, ids, context=None):
  61. discnt = 0.0
  62. no_line = 0.0
  63. line_dicnt = 0.0
  64. prod_price = 0.0
  65. conf = self.pool.get('ir.values')
  66. sale_obj = self.browse(cr, uid, ids, context)
  67. double_valid = conf.get_default(cr, uid, 'sale.config.settings', 'module_sale_discount_approval')
  68. if double_valid == 'True':
  69. min_disct = conf.get_default(cr, uid, 'sale.config.settings', 'limit_discount')
  70. for line in sale_obj.order_line:
  71. no_line += 1
  72. discnt += line.discount
  73. discnt = (discnt / no_line)
  74. if discnt >= min_disct:
  75. assert len(ids) == 1, 'This option should only be used for a single id at a time.'
  76. self.signal_workflow(cr, uid, ids, 'order_toapprov')
  77. return True
  78. else:
  79. return super(SaleInherit, self).action_button_confirm(cr, uid, ids, context)
  80. else:
  81. return super(SaleInherit, self).action_button_confirm(cr, uid, ids, context)
  82. ####################################### workflow functions#############################################################################
  83. @api.one
  84. def wait_approval(self):
  85. self.state = 'waitingapproval'
  86. return True