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.

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