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.

208 lines
7.9 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. default=_get_register_number)
  85. email = fields.Char(string="Email",
  86. required=True,
  87. default=_get_email)
  88. bank_account = fields.Char(string="Bank account",
  89. required=True,
  90. default=_get_bank_account)
  91. share_product = fields.Many2one('product.product',
  92. string='Share Type',
  93. domain=_get_possible_share,
  94. default=_default_product_id,
  95. required=True)
  96. share_qty = fields.Integer(string="Share Quantity",
  97. required=True)
  98. share_unit_price = fields.Float(related='share_product.list_price',
  99. string='Share price',
  100. readonly=True)
  101. subscription_amount = fields.Float(compute='_compute_subscription_amount',
  102. string='Subscription amount',
  103. digits=dp.get_precision('Account'),
  104. readonly=True)
  105. representative_name = fields.Char(string='Representative name',
  106. default=_get_representative_name)
  107. representative_email = fields.Char(string='Representative email',
  108. default=_get_representative_email)
  109. @api.multi
  110. def create_subscription(self):
  111. sub_req = self.env['subscription.request']
  112. partner_obj = self.env['res.partner']
  113. cooperator = self.cooperator
  114. vals = {'partner_id': cooperator.id,
  115. 'share_product_id': self.share_product.id,
  116. 'ordered_parts': self.share_qty,
  117. 'user_id': self.env.uid,
  118. 'email': self.email,
  119. 'source': 'crm',
  120. 'address': self.cooperator.street,
  121. 'zip_code': self.cooperator.zip,
  122. 'city': self.cooperator.city,
  123. 'country_id': self.cooperator.country_id.id
  124. }
  125. if self.is_company:
  126. vals['company_name'] = cooperator.name
  127. vals['company_email'] = cooperator.email
  128. vals['name'] = '/'
  129. vals['company_register_number'] = self.register_number
  130. vals['is_company'] = True
  131. else:
  132. vals['name'] = cooperator.name
  133. coop_vals = {}
  134. if not self._get_email():
  135. coop_vals['email'] = self.email
  136. if not self._get_register_number():
  137. if self.is_company:
  138. coop_vals['company_register_number'] = self.register_number
  139. if self.is_company and not self._get_representative():
  140. representative = False
  141. if self.representative_email:
  142. representative = partner_obj.search(
  143. [('email', '=', self.representative_email)])
  144. if representative:
  145. if len(representative) > 1:
  146. raise UserError(_('There is two different persons with '
  147. 'the same national register number. '
  148. 'Please proceed to a merge before to '
  149. 'continue'))
  150. if representative.parent_id:
  151. raise UserError(_("A person can't be representative of "
  152. "two different companies."))
  153. representative.parent_id = cooperator.id
  154. else:
  155. if self.representative_email:
  156. represent_vals = {'name': self.representative_name,
  157. 'cooperator': True,
  158. 'email': self.representative_email,
  159. 'parent_id': cooperator.id,
  160. 'representative': True}
  161. partner_obj.create(represent_vals)
  162. if not self._get_bank_account():
  163. partner_bank = self.env['res.partner.bank']
  164. partner_bank.create({'partner_id': cooperator.id,
  165. 'acc_number': self.bank_account})
  166. vals['iban'] = self.bank_account
  167. if self.is_company:
  168. representative = self._get_representative()
  169. vals['name'] = representative.name
  170. if coop_vals:
  171. cooperator.write(coop_vals)
  172. new_sub_req = sub_req.create(vals)
  173. return {
  174. 'type': 'ir.actions.act_window',
  175. 'view_type': 'form, tree',
  176. 'view_mode': 'form',
  177. 'res_model': 'subscription.request',
  178. 'res_id': new_sub_req.id,
  179. 'target': 'current',
  180. }