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.

210 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. 'share_product_id': self.share_product.id,
  118. 'ordered_parts': self.share_qty,
  119. 'user_id': self.env.uid,
  120. 'email': self.email,
  121. 'source': 'crm',
  122. 'address': self.cooperator.street,
  123. 'zip_code': self.cooperator.zip,
  124. 'city': self.cooperator.city,
  125. 'country_id': self.cooperator.country_id.id
  126. }
  127. if self.is_company:
  128. vals['company_name'] = cooperator.name
  129. vals['company_email'] = cooperator.email
  130. vals['name'] = '/'
  131. vals['company_register_number'] = self.register_number
  132. vals['is_company'] = True
  133. else:
  134. vals['name'] = cooperator.name
  135. coop_vals = {}
  136. if not self._get_email():
  137. coop_vals['email'] = self.email
  138. if not self._get_register_number():
  139. if self.is_company:
  140. coop_vals['company_register_number'] = self.register_number
  141. if self.is_company and not self._get_representative():
  142. representative = False
  143. if self.representative_email:
  144. representative = partner_obj.search(
  145. [('email', '=', self.representative_email)])
  146. if representative:
  147. if len(representative) > 1:
  148. raise UserError(_('There is two different persons with '
  149. 'the same national register number. '
  150. 'Please proceed to a merge before to '
  151. 'continue'))
  152. if representative.parent_id:
  153. raise UserError(_("A person can't be representative of "
  154. "two different companies."))
  155. representative.parent_id = cooperator.id
  156. else:
  157. if self.representative_email:
  158. represent_vals = {'name': self.representative_name,
  159. 'cooperator': True,
  160. 'email': self.representative_email,
  161. 'parent_id': cooperator.id,
  162. 'representative': True}
  163. partner_obj.create(represent_vals)
  164. if not self._get_bank_account():
  165. partner_bank = self.env['res.partner.bank']
  166. partner_bank.create({'partner_id': cooperator.id,
  167. 'acc_number': self.bank_account})
  168. vals['iban'] = self.bank_account
  169. if self.is_company:
  170. representative = self._get_representative()
  171. vals['name'] = representative.name
  172. if coop_vals:
  173. cooperator.write(coop_vals)
  174. new_sub_req = sub_req.create(vals)
  175. return {
  176. 'type': 'ir.actions.act_window',
  177. 'view_type': 'form, tree',
  178. 'view_mode': 'form',
  179. 'res_model': 'subscription.request',
  180. 'res_id': new_sub_req.id,
  181. 'target': 'current',
  182. }