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.

209 lines
8.0 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 partner.search([('parent_id', '=', partner.id),
  27. ('representative', '=', True)])
  28. return False
  29. @api.model
  30. def _get_representative_email(self):
  31. representative = self._get_representative()
  32. if representative:
  33. return representative.email
  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. return self._get_partner().email
  51. @api.model
  52. def _get_register_number(self):
  53. partner = self._get_partner()
  54. if partner.is_company:
  55. return partner.company_register_number
  56. @api.model
  57. def _get_bank_account(self):
  58. partner = self._get_partner()
  59. if len(partner.bank_ids) > 0:
  60. return partner.bank_ids[0].acc_number
  61. return None
  62. @api.model
  63. def _get_possible_share(self):
  64. domain = [('is_share', '=', True)]
  65. partner = self._get_partner()
  66. if partner.is_company:
  67. domain.append(('by_company', '=', True))
  68. else:
  69. domain.append(('by_individual', '=', True))
  70. return domain
  71. @api.multi
  72. @api.depends('share_product', 'share_qty')
  73. def _compute_subscription_amount(self):
  74. for sub_request in self:
  75. sub_request.subscription_amount = (sub_request.
  76. share_product.list_price *
  77. sub_request.share_qty)
  78. is_company = fields.Boolean(String="Is company?",
  79. default=_get_is_company)
  80. cooperator = fields.Many2one('res.partner',
  81. string="Cooperator",
  82. default=_get_partner)
  83. register_number = fields.Char(string="Register Company Number",
  84. required=True,
  85. default=_get_register_number)
  86. email = fields.Char(string="Email",
  87. required=True,
  88. default=_get_email)
  89. bank_account = fields.Char(string="Bank account",
  90. required=True,
  91. default=_get_bank_account)
  92. share_product = fields.Many2one('product.product',
  93. string='Share Type',
  94. domain=_get_possible_share,
  95. default=_default_product_id,
  96. required=True)
  97. share_qty = fields.Integer(string="Share Quantity",
  98. required=True)
  99. share_unit_price = fields.Float(related='share_product.list_price',
  100. string='Share price',
  101. readonly=True)
  102. subscription_amount = fields.Float(compute='_compute_subscription_amount',
  103. string='Subscription amount',
  104. digits=dp.get_precision('Account'),
  105. readonly=True)
  106. representative_name = fields.Char(string='Representative name',
  107. default=_get_representative_name)
  108. representative_email = fields.Char(string='Representative email',
  109. default=_get_representative_email)
  110. @api.multi
  111. def create_subscription(self):
  112. sub_req = self.env['subscription.request']
  113. partner_obj = self.env['res.partner']
  114. cooperator = self.cooperator
  115. vals = {'partner_id': cooperator.id,
  116. 'share_product_id': self.share_product.id,
  117. 'ordered_parts': self.share_qty,
  118. 'user_id': self.env.uid,
  119. 'email': self.email,
  120. 'source': 'crm',
  121. 'address': self.cooperator.street,
  122. 'zip_code': self.cooperator.zip,
  123. 'city': self.cooperator.city,
  124. 'country_id': self.cooperator.country_id.id
  125. }
  126. if self.is_company:
  127. vals['company_name'] = cooperator.name
  128. vals['company_email'] = cooperator.email
  129. vals['name'] = '/'
  130. vals['company_register_number'] = self.register_number
  131. vals['is_company'] = True
  132. else:
  133. vals['name'] = cooperator.name
  134. coop_vals = {}
  135. if not self._get_email():
  136. coop_vals['email'] = self.email
  137. if not self._get_register_number():
  138. if self.is_company:
  139. coop_vals['company_register_number'] = self.register_number
  140. if self.is_company and not self._get_representative():
  141. representative = False
  142. if self.representative_email:
  143. representative = partner_obj.search(
  144. [('email', '=', self.representative_email)])
  145. if representative:
  146. if len(representative) > 1:
  147. raise UserError(_('There is two different persons with '
  148. 'the same national register number. '
  149. 'Please proceed to a merge before to '
  150. 'continue'))
  151. if representative.parent_id:
  152. raise UserError(_("A person can't be representative of "
  153. "two different companies."))
  154. representative.parent_id = cooperator.id
  155. else:
  156. if self.representative_email:
  157. represent_vals = {'name': self.representative_name,
  158. 'cooperator': True,
  159. 'email': self.representative_email,
  160. 'parent_id': cooperator.id,
  161. 'representative': True}
  162. partner_obj.create(represent_vals)
  163. if not self._get_bank_account():
  164. partner_bank = self.env['res.partner.bank']
  165. partner_bank.create({'partner_id': cooperator.id,
  166. 'acc_number': self.bank_account})
  167. vals['iban'] = self.bank_account
  168. if self.is_company:
  169. representative = self._get_representative()
  170. vals['name'] = representative.name
  171. if coop_vals:
  172. cooperator.write(coop_vals)
  173. new_sub_req = sub_req.create(vals)
  174. return {
  175. 'type': 'ir.actions.act_window',
  176. 'view_type': 'form, tree',
  177. 'view_mode': 'form',
  178. 'res_model': 'subscription.request',
  179. 'res_id': new_sub_req.id,
  180. 'target': 'current',
  181. }