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.

100 lines
4.0 KiB

  1. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  2. import time
  3. from openerp import api, fields, models, _
  4. import openerp.addons.decimal_precision as dp
  5. from openerp.exceptions import UserError
  6. class PartnerCreateSubscription(models.TransientModel):
  7. _name = "partner.create.subscription"
  8. _description = "Create Subscription From Partner"
  9. @api.multi
  10. @api.onchange('share_product')
  11. def on_change_share_type(self):
  12. self.share_qty = self.share_product.minimum_quantity
  13. @api.model
  14. def _default_product_id(self):
  15. domain = [('is_share','=',True),('default_share_product','=',True)]
  16. active_id = self.env.context.get('active_id')
  17. if active_id:
  18. partner = self.env['res.partner'].browse(active_id)
  19. if partner.is_company:
  20. domain.append(('by_company','=',True))
  21. else:
  22. domain.append(('by_individual','=',True))
  23. return self.env['product.product'].search(domain)[0]
  24. @api.model
  25. def _get_partner(self):
  26. active_id = self.env.context.get('active_id')
  27. return self.env['res.partner'].browse(active_id)
  28. @api.model
  29. def _get_is_company(self):
  30. return self._get_partner().is_company
  31. @api.model
  32. def _get_register_number(self):
  33. partner = self._get_partner()
  34. if partner.is_company:
  35. return partner.company_register_number
  36. else:
  37. return partner.national_register_number
  38. @api.model
  39. def _get_possible_share(self):
  40. domain = [('is_share','=',True)]
  41. partner = self._get_partner()
  42. if partner.is_company:
  43. domain.append(('by_company','=',True))
  44. else:
  45. domain.append(('by_individual','=',True))
  46. return domain
  47. @api.multi
  48. @api.depends('share_product', 'share_qty')
  49. def _compute_subscription_amount(self):
  50. for sub_request in self:
  51. sub_request.subscription_amount = sub_request.share_product.list_price * sub_request.share_qty
  52. is_company = fields.Boolean(String="Is company?", default=_get_is_company)
  53. cooperator = fields.Many2one('res.partner', string="Cooperator", default=_get_partner)
  54. register_number = fields.Char(string="Register Number", required=True, default=_get_register_number)
  55. share_product = fields.Many2one('product.product', string='Share Type', domain=lambda self: self._get_possible_share(),\
  56. default=_default_product_id, required=True)
  57. share_qty = fields.Integer(string="Share Quantity", required=True)
  58. share_unit_price = fields.Float(related='share_product.list_price', string='Share price', readonly=True)
  59. subscription_amount = fields.Float(compute='_compute_subscription_amount', string='Subscription amount', digits=dp.get_precision('Account'), readonly=True)
  60. @api.multi
  61. def create_subscription(self):
  62. sub_req = self.env['subscription.request']
  63. cooperator = self.cooperator
  64. vals = {'partner_id': cooperator.id,
  65. 'name': cooperator.name,
  66. 'share_product_id':self.share_product.id,
  67. 'ordered_parts':self.share_qty,
  68. 'user_id':self.env.uid}
  69. if self.is_company:
  70. vals['company_register_number'] = self.register_number
  71. vals['is_company'] = True
  72. else:
  73. vals['no_registre'] = self.register_number
  74. if not self._get_register_number():
  75. if self.is_company:
  76. cooperator.write({'company_register_number':self.register_number})
  77. else:
  78. if sub_req.check_belgian_identification_id(self.register_number):
  79. cooperator.write({'national_register_number':self.register_number})
  80. else:
  81. raise UserError(_("The national register number is not valid."))
  82. sub_req.create(vals)
  83. return {'type': 'ir.actions.act_window_close'}