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.

139 lines
5.2 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_email(self):
  33. partner = self._get_partner()
  34. return partner.email
  35. @api.model
  36. def _get_register_number(self):
  37. partner = self._get_partner()
  38. if partner.is_company:
  39. return partner.company_register_number
  40. else:
  41. return partner.national_register_number
  42. @api.model
  43. def _get_bank_account(self):
  44. partner = self._get_partner()
  45. if len(partner.bank_ids) > 0:
  46. return partner.bank_ids[0]
  47. return None
  48. @api.model
  49. def _get_possible_share(self):
  50. domain = [('is_share','=',True)]
  51. partner = self._get_partner()
  52. if partner.is_company:
  53. domain.append(('by_company','=',True))
  54. else:
  55. domain.append(('by_individual','=',True))
  56. return domain
  57. @api.multi
  58. @api.depends('share_product', 'share_qty')
  59. def _compute_subscription_amount(self):
  60. for sub_request in self:
  61. sub_request.subscription_amount = sub_request.share_product.list_price * sub_request.share_qty
  62. is_company = fields.Boolean(String="Is company?", default=_get_is_company)
  63. cooperator = fields.Many2one('res.partner', string="Cooperator", default=_get_partner)
  64. register_number = fields.Char(string="Register Number", required=True, default=_get_register_number)
  65. email = fields.Char(string="Email", required=True, default=_get_email)
  66. bank_account = fields.Char(string="Bank account", required=True, default=_get_bank_account)
  67. share_product = fields.Many2one('product.product', string='Share Type', domain=lambda self: self._get_possible_share(),\
  68. default=_default_product_id, required=True)
  69. share_qty = fields.Integer(string="Share Quantity", required=True)
  70. share_unit_price = fields.Float(related='share_product.list_price', string='Share price', readonly=True)
  71. subscription_amount = fields.Float(compute='_compute_subscription_amount', string='Subscription amount', digits=dp.get_precision('Account'), readonly=True)
  72. @api.multi
  73. def create_subscription(self):
  74. sub_req = self.env['subscription.request']
  75. cooperator = self.cooperator
  76. vals = {'partner_id': cooperator.id,
  77. 'share_product_id': self.share_product.id,
  78. 'ordered_parts': self.share_qty,
  79. 'user_id': self.env.uid,
  80. 'email': self.email,
  81. 'source': 'crm'}
  82. if self.is_company:
  83. vals['company_name'] = cooperator.name
  84. vals['name'] = '/'
  85. vals['company_register_number'] = self.register_number
  86. vals['is_company'] = True
  87. else:
  88. vals['name'] = cooperator.name
  89. vals['no_registre'] = self.register_number
  90. coop_vals = {}
  91. if not self._get_email():
  92. coop_vals['email'] = self.email
  93. if not self._get_register_number():
  94. if self.is_company:
  95. coop_vals['company_register_number'] = self.register_number
  96. else:
  97. if sub_req.check_belgian_identification_id(self.register_number):
  98. coop_vals['national_register_number'] = self.register_number
  99. else:
  100. raise UserError(_("The national register number is not valid."))
  101. if not self._get_bank_account():
  102. partner_bank = self.env['res.partner.bank']
  103. partner_bank.create({'partner_id':cooperator.id,
  104. 'acc_number':self.bank_account})
  105. vals['iban'] = self.bank_account.acc_number
  106. if coop_vals:
  107. cooperator.write(coop_vals)
  108. #self.env.cr.commit()
  109. new_sub_req = sub_req.create(vals)
  110. return {
  111. 'type': 'ir.actions.act_window',
  112. 'view_type': 'form, tree',
  113. 'view_mode': 'form',
  114. 'res_model': 'subscription.request',
  115. 'res_id': new_sub_req.id,
  116. 'target': 'current',
  117. }