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.

197 lines
7.8 KiB

  1. # -*- coding: utf-8 -*-
  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. 'address': self.cooperator.street,
  109. 'zip_code': self.cooperator.zip,
  110. 'city': self.cooperator.city,
  111. 'country_id': self.cooperator.country_id.id
  112. }
  113. if self.is_company:
  114. vals['company_name'] = cooperator.name
  115. vals['company_email'] = cooperator.email
  116. vals['name'] = '/'
  117. vals['company_register_number'] = self.register_number
  118. vals['is_company'] = True
  119. else:
  120. vals['name'] = cooperator.name
  121. vals['no_registre'] = self.register_number
  122. coop_vals = {}
  123. if not self._get_email():
  124. coop_vals['email'] = self.email
  125. if not self._get_register_number():
  126. if self.is_company:
  127. coop_vals['company_register_number'] = self.register_number
  128. else:
  129. if self.check_belgian_ident_id(self.register_number):
  130. coop_vals['national_register_number'] = self.register_number
  131. if self.is_company and not self._get_representative():
  132. representative = False
  133. if self.representative_number:
  134. representative_number = self.representative_number
  135. representative = partner_obj.search([('national_register_number','=',representative_number)])
  136. if representative:
  137. if len(representative) > 1:
  138. raise UserError(_('There is two different persons with the same national register number. Please proceed to a merge before to continue'))
  139. if representative.parent_id:
  140. raise UserError(_("A person can't be representative of two differents companies."))
  141. representative.parent_id = cooperator.id
  142. else:
  143. if self.check_belgian_ident_id(representative_number):
  144. represent_vals = {'name':self.representative_name,'cooperator':True,
  145. 'national_register_number':representative_number,
  146. 'parent_id':cooperator.id,'representative':True}
  147. partner_obj.create(represent_vals)
  148. if not self._get_bank_account():
  149. partner_bank = self.env['res.partner.bank']
  150. partner_bank.create({'partner_id':cooperator.id,
  151. 'acc_number':self.bank_account})
  152. vals['iban'] = self.bank_account
  153. if self.is_company:
  154. representative = self._get_representative()
  155. vals['name'] = representative.name
  156. if coop_vals:
  157. cooperator.write(coop_vals)
  158. new_sub_req = sub_req.create(vals)
  159. return {
  160. 'type': 'ir.actions.act_window',
  161. 'view_type': 'form, tree',
  162. 'view_mode': 'form',
  163. 'res_model': 'subscription.request',
  164. 'res_id': new_sub_req.id,
  165. 'target': 'current',
  166. }