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.

225 lines
8.8 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. from odoo import api, fields, models, _
  2. import odoo.addons.decimal_precision as dp
  3. from odoo.exceptions import UserError
  4. class PartnerCreateSubscription(models.TransientModel):
  5. _name = "partner.create.subscription"
  6. _description = "Create Subscription From Partner"
  7. @api.multi
  8. @api.onchange('share_product')
  9. def on_change_share_type(self):
  10. self.share_qty = self.share_product.minimum_quantity
  11. @api.model
  12. def _default_product_id(self):
  13. domain = [('is_share', '=', True),
  14. ('default_share_product', '=', True)]
  15. active_id = self.env.context.get('active_id')
  16. if active_id:
  17. partner = self.env['res.partner'].browse(active_id)
  18. if partner.is_company:
  19. domain.append(('by_company', '=', True))
  20. else:
  21. domain.append(('by_individual', '=', True))
  22. return self.env['product.product'].search(domain)[0]
  23. def _get_representative(self):
  24. partner = self._get_partner()
  25. if partner.is_company:
  26. return self.env['res.partner'].search([('parent_id', '=', partner.id),
  27. ('representative', '=', True)])
  28. return False
  29. @api.model
  30. def _get_representative_number(self):
  31. representative = self._get_representative()
  32. if representative:
  33. return representative.national_register_number
  34. return False
  35. @api.model
  36. def _get_representative_name(self):
  37. representative = self._get_representative()
  38. if representative:
  39. return representative.name
  40. return False
  41. @api.model
  42. def _get_partner(self):
  43. active_id = self.env.context.get('active_id')
  44. return self.env['res.partner'].browse(active_id)
  45. @api.model
  46. def _get_is_company(self):
  47. return self._get_partner().is_company
  48. @api.model
  49. def _get_email(self):
  50. partner = self._get_partner()
  51. return partner.email
  52. @api.model
  53. def _get_register_number(self):
  54. partner = self._get_partner()
  55. if partner.is_company:
  56. return partner.company_register_number
  57. else:
  58. return partner.national_register_number
  59. @api.model
  60. def _get_bank_account(self):
  61. partner = self._get_partner()
  62. if len(partner.bank_ids) > 0:
  63. return partner.bank_ids[0].acc_number
  64. return None
  65. @api.model
  66. def _get_possible_share(self):
  67. domain = [('is_share', '=', True)]
  68. partner = self._get_partner()
  69. if partner.is_company:
  70. domain.append(('by_company', '=', True))
  71. else:
  72. domain.append(('by_individual', '=', True))
  73. return domain
  74. @api.multi
  75. @api.depends('share_product', 'share_qty')
  76. def _compute_subscription_amount(self):
  77. for sub_request in self:
  78. sub_request.subscription_amount = (sub_request.
  79. share_product.list_price *
  80. sub_request.share_qty)
  81. is_company = fields.Boolean(String="Is company?",
  82. default=_get_is_company)
  83. cooperator = fields.Many2one('res.partner',
  84. string="Cooperator",
  85. default=_get_partner)
  86. register_number = fields.Char(string="Register Number",
  87. required=True,
  88. default=_get_register_number)
  89. email = fields.Char(string="Email",
  90. required=True,
  91. default=_get_email)
  92. bank_account = fields.Char(string="Bank account",
  93. required=True,
  94. default=_get_bank_account)
  95. share_product = fields.Many2one('product.product',
  96. string='Share Type',
  97. domain=_get_possible_share,
  98. default=_default_product_id,
  99. required=True)
  100. share_qty = fields.Integer(string="Share Quantity",
  101. required=True)
  102. share_unit_price = fields.Float(related='share_product.list_price',
  103. string='Share price',
  104. readonly=True)
  105. subscription_amount = fields.Float(compute='_compute_subscription_amount',
  106. string='Subscription amount',
  107. digits=dp.get_precision('Account'),
  108. readonly=True)
  109. representative_name = fields.Char(string='Representative name',
  110. default=_get_representative_name)
  111. representative_number = fields.Char(string='Representative national '
  112. 'register number',
  113. default=_get_representative_number)
  114. def check_belgian_ident_id(self, register_number):
  115. sub_req = self.env['subscription.request']
  116. if sub_req.check_belgian_identification_id(register_number):
  117. return True
  118. else:
  119. raise UserError(_("The national register number is not valid."))
  120. @api.multi
  121. def create_subscription(self):
  122. sub_req = self.env['subscription.request']
  123. partner_obj = self.env['res.partner']
  124. cooperator = self.cooperator
  125. vals = {'partner_id': cooperator.id,
  126. 'share_product_id': self.share_product.id,
  127. 'ordered_parts': self.share_qty,
  128. 'user_id': self.env.uid,
  129. 'email': self.email,
  130. 'source': 'crm',
  131. 'address': self.cooperator.street,
  132. 'zip_code': self.cooperator.zip,
  133. 'city': self.cooperator.city,
  134. 'country_id': self.cooperator.country_id.id
  135. }
  136. if self.is_company:
  137. vals['company_name'] = cooperator.name
  138. vals['company_email'] = cooperator.email
  139. vals['name'] = '/'
  140. vals['company_register_number'] = self.register_number
  141. vals['is_company'] = True
  142. else:
  143. vals['name'] = cooperator.name
  144. vals['no_registre'] = self.register_number
  145. coop_vals = {}
  146. if not self._get_email():
  147. coop_vals['email'] = self.email
  148. if not self._get_register_number():
  149. if self.is_company:
  150. coop_vals['company_register_number'] = self.register_number
  151. else:
  152. if self.check_belgian_ident_id(self.register_number):
  153. coop_vals['national_register_number'] = self.register_number
  154. if self.is_company and not self._get_representative():
  155. representative = False
  156. if self.representative_number:
  157. representative_number = self.representative_number
  158. representative = partner_obj.search([('national_register_number', '=', representative_number)])
  159. if representative:
  160. if len(representative) > 1:
  161. raise UserError(_('There is two different persons with '
  162. 'the same national register number. '
  163. 'Please proceed to a merge before to '
  164. 'continue'))
  165. if representative.parent_id:
  166. raise UserError(_("A person can't be representative of "
  167. "two different companies."))
  168. representative.parent_id = cooperator.id
  169. else:
  170. if self.check_belgian_ident_id(representative_number):
  171. represent_vals = {'name': self.representative_name,
  172. 'cooperator': True,
  173. 'national_register_number': representative_number,
  174. 'parent_id': cooperator.id,
  175. 'representative': True}
  176. partner_obj.create(represent_vals)
  177. if not self._get_bank_account():
  178. partner_bank = self.env['res.partner.bank']
  179. partner_bank.create({'partner_id': cooperator.id,
  180. 'acc_number': self.bank_account})
  181. vals['iban'] = self.bank_account
  182. if self.is_company:
  183. representative = self._get_representative()
  184. vals['name'] = representative.name
  185. vals['no_registre'] = representative.national_register_number
  186. if coop_vals:
  187. cooperator.write(coop_vals)
  188. new_sub_req = sub_req.create(vals)
  189. return {
  190. 'type': 'ir.actions.act_window',
  191. 'view_type': 'form, tree',
  192. 'view_mode': 'form',
  193. 'res_model': 'subscription.request',
  194. 'res_id': new_sub_req.id,
  195. 'target': 'current',
  196. }