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.

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