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.

186 lines
7.4 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. def _get_representative(self):
  25. partner = self._get_partner()
  26. if partner.is_company:
  27. return self.env['res.partner'].search([('parent_id','=',partner.id),
  28. ('representative','=',True)])
  29. return False
  30. @api.model
  31. def _get_representative_number(self):
  32. representative = self._get_representative()
  33. if representative:
  34. return representative.national_register_number
  35. return False
  36. @api.model
  37. def _get_representative_name(self):
  38. representative = self._get_representative()
  39. if representative:
  40. return representative.name
  41. return False
  42. @api.model
  43. def _get_partner(self):
  44. active_id = self.env.context.get('active_id')
  45. return self.env['res.partner'].browse(active_id)
  46. @api.model
  47. def _get_is_company(self):
  48. return self._get_partner().is_company
  49. @api.model
  50. def _get_email(self):
  51. partner = self._get_partner()
  52. return partner.email
  53. @api.model
  54. def _get_register_number(self):
  55. partner = self._get_partner()
  56. if partner.is_company:
  57. return partner.company_register_number
  58. else:
  59. return partner.national_register_number
  60. @api.model
  61. def _get_bank_account(self):
  62. partner = self._get_partner()
  63. if len(partner.bank_ids) > 0:
  64. return partner.bank_ids[0].acc_number
  65. return None
  66. @api.model
  67. def _get_possible_share(self):
  68. domain = [('is_share','=',True)]
  69. partner = self._get_partner()
  70. if partner.is_company:
  71. domain.append(('by_company','=',True))
  72. else:
  73. domain.append(('by_individual','=',True))
  74. return domain
  75. @api.multi
  76. @api.depends('share_product', 'share_qty')
  77. def _compute_subscription_amount(self):
  78. for sub_request in self:
  79. sub_request.subscription_amount = sub_request.share_product.list_price * sub_request.share_qty
  80. is_company = fields.Boolean(String="Is company?", default=_get_is_company)
  81. cooperator = fields.Many2one('res.partner', string="Cooperator", default=_get_partner)
  82. register_number = fields.Char(string="Register Number", required=True, default=_get_register_number)
  83. email = fields.Char(string="Email", required=True, default=_get_email)
  84. bank_account = fields.Char(string="Bank account", required=True, default=_get_bank_account)
  85. share_product = fields.Many2one('product.product', string='Share Type', domain=lambda self: self._get_possible_share(),\
  86. default=_default_product_id, required=True)
  87. share_qty = fields.Integer(string="Share Quantity", required=True)
  88. share_unit_price = fields.Float(related='share_product.list_price', string='Share price', readonly=True)
  89. subscription_amount = fields.Float(compute='_compute_subscription_amount', string='Subscription amount', digits=dp.get_precision('Account'), readonly=True)
  90. representative_name = fields.Char(string='Representative name', default=_get_representative_name)
  91. representative_number = fields.Char(string='Representative national register number', default=_get_representative_number)
  92. def check_belgian_ident_id(self, register_number):
  93. if self.env['subscription.request'].check_belgian_identification_id(register_number):
  94. return True
  95. else:
  96. raise UserError(_("The national register number is not valid."))
  97. @api.multi
  98. def create_subscription(self):
  99. sub_req = self.env['subscription.request']
  100. partner_obj = self.env['res.partner']
  101. cooperator = self.cooperator
  102. vals = {'partner_id': cooperator.id,
  103. 'share_product_id': self.share_product.id,
  104. 'ordered_parts': self.share_qty,
  105. 'user_id': self.env.uid,
  106. 'email': self.email,
  107. 'source': 'crm'}
  108. if self.is_company:
  109. vals['company_name'] = cooperator.name
  110. vals['name'] = '/'
  111. vals['company_register_number'] = self.register_number
  112. vals['is_company'] = True
  113. else:
  114. vals['name'] = cooperator.name
  115. vals['no_registre'] = self.register_number
  116. coop_vals = {}
  117. if not self._get_email():
  118. coop_vals['email'] = self.email
  119. if not self._get_register_number():
  120. if self.is_company:
  121. coop_vals['company_register_number'] = self.register_number
  122. else:
  123. if self.check_belgian_ident_id(self.register_number):
  124. coop_vals['national_register_number'] = self.register_number
  125. if not self._get_representative():
  126. representative_number = self.representative_number
  127. representative = partner_obj.search([('national_register_number','=',representative_number)])
  128. if representative:
  129. if len(representative) > 1:
  130. raise UserError(_('There is two different persons with the same national register number. Please proceed to a merge before to continue'))
  131. if representative.parent_id:
  132. raise UserError(_("A person can't be representative of two differents companies."))
  133. representative.parent_id = cooperator.id
  134. else:
  135. if self.check_belgian_ident_id(representative_number):
  136. represent_vals = {'name':self.representative_name,'cooperator':True,
  137. 'national_register_number':representative_number,
  138. 'parent_id':cooperator.id,'representative':True}
  139. partner_obj.create(represent_vals)
  140. if not self._get_bank_account():
  141. partner_bank = self.env['res.partner.bank']
  142. partner_bank.create({'partner_id':cooperator.id,
  143. 'acc_number':self.bank_account})
  144. vals['iban'] = self.bank_account
  145. if coop_vals:
  146. cooperator.write(coop_vals)
  147. new_sub_req = sub_req.create(vals)
  148. return {
  149. 'type': 'ir.actions.act_window',
  150. 'view_type': 'form, tree',
  151. 'view_mode': 'form',
  152. 'res_model': 'subscription.request',
  153. 'res_id': new_sub_req.id,
  154. 'target': 'current',
  155. }