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.

418 lines
14 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
  1. # Copyright (C) 2018 - TODAY, Pavlov Media
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo import _, api, fields, models
  4. class Agreement(models.Model):
  5. _name = "agreement"
  6. _inherit = ["agreement", "mail.thread"]
  7. def _default_stage_id(self):
  8. return self.env.ref("agreement_legal.agreement_stage_new")
  9. # General
  10. name = fields.Char(string="Title", required=True)
  11. is_template = fields.Boolean(
  12. string="Is a Template?",
  13. default=False,
  14. copy=False,
  15. help="Make this agreement a template.",
  16. )
  17. version = fields.Integer(
  18. string="Version",
  19. default=1,
  20. copy=False,
  21. help="The versions are used to keep track of document history and "
  22. "previous versions can be referenced.",
  23. )
  24. revision = fields.Integer(
  25. string="Revision",
  26. default=0,
  27. copy=False,
  28. help="The revision will increase with every save event.",
  29. )
  30. description = fields.Text(
  31. string="Description",
  32. track_visibility="onchange",
  33. help="Description of the agreement",
  34. )
  35. dynamic_description = fields.Text(
  36. compute="_compute_dynamic_description",
  37. string="Dynamic Description",
  38. help="Compute dynamic description",
  39. )
  40. start_date = fields.Date(
  41. string="Start Date",
  42. track_visibility="onchange",
  43. help="When the agreement starts.",
  44. )
  45. end_date = fields.Date(
  46. string="End Date",
  47. track_visibility="onchange",
  48. help="When the agreement ends.",
  49. )
  50. color = fields.Integer()
  51. active = fields.Boolean(
  52. string="Active",
  53. default=True,
  54. help="If unchecked, it will allow you to hide the agreement without "
  55. "removing it.",
  56. )
  57. company_signed_date = fields.Date(
  58. string="Signed on",
  59. track_visibility="onchange",
  60. help="Date the contract was signed by Company.",
  61. )
  62. partner_signed_date = fields.Date(
  63. string="Signed on",
  64. track_visibility="onchange",
  65. help="Date the contract was signed by the Partner.",
  66. )
  67. term = fields.Integer(
  68. string="Term (Months)",
  69. track_visibility="onchange",
  70. help="Number of months this agreement/contract is in effect with the "
  71. "partner.",
  72. )
  73. expiration_notice = fields.Integer(
  74. string="Exp. Notice (Days)",
  75. track_visibility="onchange",
  76. help="Number of Days before expiration to be notified.",
  77. )
  78. change_notice = fields.Integer(
  79. string="Change Notice (Days)",
  80. track_visibility="onchange",
  81. help="Number of Days to be notified before changes.",
  82. )
  83. special_terms = fields.Text(
  84. string="Special Terms",
  85. track_visibility="onchange",
  86. help="Any terms that you have agreed to and want to track on the "
  87. "agreement/contract.",
  88. )
  89. dynamic_special_terms = fields.Text(
  90. compute="_compute_dynamic_special_terms",
  91. string="Dynamic Special Terms",
  92. help="Compute dynamic special terms",
  93. )
  94. reference = fields.Char(
  95. string="Reference",
  96. copy=False,
  97. required=True,
  98. default=lambda self: _("New"),
  99. track_visibility="onchange",
  100. help="ID used for internal contract tracking.",
  101. )
  102. code = fields.Char(
  103. required=True,
  104. default=lambda self: _("New"),
  105. copy=False)
  106. increase_type_id = fields.Many2one(
  107. "agreement.increasetype",
  108. string="Increase Type",
  109. track_visibility="onchange",
  110. help="The amount that certain rates may increase.",
  111. )
  112. termination_requested = fields.Date(
  113. string="Termination Requested Date",
  114. track_visibility="onchange",
  115. help="Date that a request for termination was received.",
  116. )
  117. termination_date = fields.Date(
  118. string="Termination Date",
  119. track_visibility="onchange",
  120. help="Date that the contract was terminated.",
  121. )
  122. reviewed_date = fields.Date(
  123. string="Reviewed Date", track_visibility="onchange"
  124. )
  125. reviewed_user_id = fields.Many2one(
  126. "res.users", string="Reviewed By", track_visibility="onchange"
  127. )
  128. approved_date = fields.Date(
  129. string="Approved Date", track_visibility="onchange"
  130. )
  131. approved_user_id = fields.Many2one(
  132. "res.users", string="Approved By", track_visibility="onchange"
  133. )
  134. currency_id = fields.Many2one("res.currency", string="Currency")
  135. partner_id = fields.Many2one(
  136. "res.partner",
  137. string="Partner",
  138. required=False,
  139. copy=True,
  140. help="The customer or vendor this agreement is related to.",
  141. )
  142. company_partner_id = fields.Many2one(
  143. "res.partner",
  144. string="Company",
  145. copy=True,
  146. default=lambda self: self.env.user.company_id.partner_id,
  147. )
  148. partner_contact_id = fields.Many2one(
  149. "res.partner",
  150. string="Partner Contact",
  151. copy=True,
  152. help="The primary partner contact (If Applicable).",
  153. )
  154. partner_contact_phone = fields.Char(
  155. related="partner_contact_id.phone", string="Phone"
  156. )
  157. partner_contact_email = fields.Char(
  158. related="partner_contact_id.email", string="Email"
  159. )
  160. company_contact_id = fields.Many2one(
  161. "res.partner",
  162. string="Company Contact",
  163. copy=True,
  164. help="The primary contact in the company.",
  165. )
  166. company_contact_phone = fields.Char(
  167. related="company_contact_id.phone", string="Phone"
  168. )
  169. company_contact_email = fields.Char(
  170. related="company_contact_id.email", string="Email"
  171. )
  172. agreement_type_id = fields.Many2one(
  173. "agreement.type",
  174. string="Agreement Type",
  175. track_visibility="onchange",
  176. help="Select the type of agreement.",
  177. )
  178. agreement_subtype_id = fields.Many2one(
  179. "agreement.subtype",
  180. string="Agreement Sub-type",
  181. track_visibility="onchange",
  182. help="Select the sub-type of this agreement. Sub-Types are related to "
  183. "agreement types.",
  184. )
  185. product_ids = fields.Many2many(
  186. "product.template", string="Products & Services"
  187. )
  188. assigned_user_id = fields.Many2one(
  189. "res.users",
  190. string="Assigned To",
  191. track_visibility="onchange",
  192. help="Select the user who manages this agreement.",
  193. )
  194. company_signed_user_id = fields.Many2one(
  195. "res.users",
  196. string="Signed By",
  197. track_visibility="onchange",
  198. help="The user at our company who authorized/signed the agreement or "
  199. "contract.",
  200. )
  201. partner_signed_user_id = fields.Many2one(
  202. "res.partner",
  203. string="Signed By",
  204. track_visibility="onchange",
  205. help="Contact on the account that signed the agreement/contract.",
  206. )
  207. parent_agreement_id = fields.Many2one(
  208. "agreement",
  209. string="Parent Agreement",
  210. help="Link this agreement to a parent agreement. For example if this "
  211. "agreement is an amendment to another agreement. This list will "
  212. "only show other agreements related to the same account.",
  213. )
  214. renewal_type_id = fields.Many2one(
  215. "agreement.renewaltype",
  216. string="Renewal Type",
  217. track_visibility="onchange",
  218. help="Describes what happens after the contract expires.",
  219. )
  220. recital_ids = fields.One2many(
  221. "agreement.recital", "agreement_id", string="Recitals", copy=True
  222. )
  223. sections_ids = fields.One2many(
  224. "agreement.section", "agreement_id", string="Sections", copy=True
  225. )
  226. clauses_ids = fields.One2many(
  227. "agreement.clause", "agreement_id", string="Clauses", copy=True
  228. )
  229. appendix_ids = fields.One2many(
  230. "agreement.appendix", "agreement_id", string="Appendices", copy=True
  231. )
  232. previous_version_agreements_ids = fields.One2many(
  233. "agreement",
  234. "parent_agreement_id",
  235. string="Child Agreements",
  236. copy=False,
  237. domain=[("active", "=", False)],
  238. )
  239. child_agreements_ids = fields.One2many(
  240. "agreement",
  241. "parent_agreement_id",
  242. string="Child Agreements",
  243. copy=False,
  244. domain=[("active", "=", True)],
  245. )
  246. line_ids = fields.One2many(
  247. "agreement.line",
  248. "agreement_id",
  249. string="Products/Services",
  250. copy=False,
  251. )
  252. state = fields.Selection(
  253. [("draft", "Draft"), ("active", "Active"), ("inactive", "Inactive")],
  254. default="draft",
  255. track_visibility="always",
  256. )
  257. notification_address_id = fields.Many2one(
  258. "res.partner",
  259. string="Notification Address",
  260. help="The address to send notificaitons to, if different from "
  261. "customer address.(Address Type = Other)",
  262. )
  263. signed_contract_filename = fields.Char(string="Filename")
  264. signed_contract = fields.Binary(
  265. string="Signed Document", track_visibility="always"
  266. )
  267. field_id = fields.Many2one(
  268. "ir.model.fields",
  269. string="Field",
  270. help="""Select target field from the related document model. If it is a
  271. relationship field you will be able to select a target field at the
  272. destination of the relationship.""",
  273. )
  274. sub_object_id = fields.Many2one(
  275. "ir.model",
  276. string="Sub-model",
  277. help="""When a relationship field is selected as first field, this
  278. field shows the document model the relationship goes to.""",
  279. )
  280. sub_model_object_field_id = fields.Many2one(
  281. "ir.model.fields",
  282. string="Sub-field",
  283. help="""When a relationship field is selected as first field, this
  284. field lets you select the target field within the destination document
  285. model (sub-model).""",
  286. )
  287. default_value = fields.Char(
  288. string="Default Value",
  289. help="Optional value to use if the target field is empty.",
  290. )
  291. copyvalue = fields.Char(
  292. string="Placeholder Expression",
  293. help="""Final placeholder expression, to be copy-pasted in the desired
  294. template field.""",
  295. )
  296. # compute the dynamic content for mako expression
  297. @api.multi
  298. def _compute_dynamic_description(self):
  299. MailTemplates = self.env["mail.template"]
  300. for agreement in self:
  301. lang = agreement.partner_id.lang or "en_US"
  302. description = MailTemplates.with_context(
  303. lang=lang
  304. )._render_template(
  305. agreement.description, "agreement", agreement.id
  306. )
  307. agreement.dynamic_description = description
  308. @api.multi
  309. def _compute_dynamic_special_terms(self):
  310. MailTemplates = self.env["mail.template"]
  311. for agreement in self:
  312. lang = agreement.partner_id.lang or "en_US"
  313. special_terms = MailTemplates.with_context(
  314. lang=lang
  315. )._render_template(
  316. agreement.special_terms, "agreement", agreement.id
  317. )
  318. agreement.dynamic_special_terms = special_terms
  319. @api.onchange("field_id", "sub_model_object_field_id", "default_value")
  320. def onchange_copyvalue(self):
  321. self.sub_object_id = False
  322. self.copyvalue = False
  323. self.sub_object_id = False
  324. if self.field_id and not self.field_id.relation:
  325. self.copyvalue = "${{object.{} or {}}}".format(
  326. self.field_id.name, self.default_value or "''"
  327. )
  328. self.sub_model_object_field_id = False
  329. if self.field_id and self.field_id.relation:
  330. self.sub_object_id = self.env["ir.model"].search(
  331. [("model", "=", self.field_id.relation)]
  332. )[0]
  333. if self.sub_model_object_field_id:
  334. self.copyvalue = "${{object.{}.{} or {}}}".format(
  335. self.field_id.name,
  336. self.sub_model_object_field_id.name,
  337. self.default_value or "''",
  338. )
  339. # Used for Kanban grouped_by view
  340. @api.model
  341. def _read_group_stage_ids(self, stages, domain, order):
  342. stage_ids = self.env["agreement.stage"].search([])
  343. return stage_ids
  344. stage_id = fields.Many2one(
  345. "agreement.stage",
  346. string="Stage",
  347. group_expand="_read_group_stage_ids",
  348. help="Select the current stage of the agreement.",
  349. track_visibility="onchange",
  350. index=True,
  351. # default=lambda self: self._default_stage_id(),
  352. )
  353. # Create New Version Button
  354. @api.multi
  355. def create_new_version(self, vals):
  356. for rec in self:
  357. if not rec.state == "draft":
  358. # Make sure status is draft
  359. rec.state = "draft"
  360. default_vals = {
  361. "name": "{} - OLD VERSION".format(rec.name),
  362. "active": False,
  363. "parent_agreement_id": rec.id,
  364. "code": "{} - OLD VERSION".format(rec.code),
  365. }
  366. # Make a current copy and mark it as old
  367. rec.copy(default=default_vals)
  368. # Increment the Version
  369. rec.version = rec.version + 1
  370. rec.code = rec.code + "v" + str(rec.version)
  371. # Reset revision to 0 since it's a new version
  372. vals["revision"] = 0
  373. return super(Agreement, self).write(vals)
  374. def create_new_agreement(self):
  375. default_vals = {
  376. "name": "NEW",
  377. "code": "NEW",
  378. "active": True,
  379. "version": 1,
  380. "revision": 0,
  381. "state": "draft",
  382. }
  383. res = self.copy(default=default_vals)
  384. return {
  385. "res_model": "agreement",
  386. "type": "ir.actions.act_window",
  387. "view_mode": "form",
  388. "view_type": "form",
  389. "res_id": res.id,
  390. }
  391. @api.model
  392. def create(self, vals):
  393. if vals.get("reference", _("New")) == _("New"):
  394. vals["reference"] = self.env["ir.sequence"].next_by_code(
  395. "agreement"
  396. ) or _("New")
  397. if vals.get("code", _("New")) == _("New"):
  398. vals["code"] = vals["reference"]
  399. return super(Agreement, self).create(vals)
  400. # Increments the revision on each save action
  401. @api.multi
  402. def write(self, vals):
  403. vals["revision"] = self.revision + 1
  404. return super(Agreement, self).write(vals)