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.

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