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.

434 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
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. code = fields.Char(
  95. string="Reference",
  96. required=True,
  97. default=lambda self: _("New"),
  98. track_visibility="onchange",
  99. copy=False,
  100. help="ID used for internal contract tracking.",
  101. )
  102. increase_type_id = fields.Many2one(
  103. "agreement.increasetype",
  104. string="Increase Type",
  105. track_visibility="onchange",
  106. help="The amount that certain rates may increase.",
  107. )
  108. termination_requested = fields.Date(
  109. string="Termination Requested Date",
  110. track_visibility="onchange",
  111. help="Date that a request for termination was received.",
  112. )
  113. termination_date = fields.Date(
  114. string="Termination Date",
  115. track_visibility="onchange",
  116. help="Date that the contract was terminated.",
  117. )
  118. reviewed_date = fields.Date(
  119. string="Reviewed Date", track_visibility="onchange"
  120. )
  121. reviewed_user_id = fields.Many2one(
  122. "res.users", string="Reviewed By", track_visibility="onchange"
  123. )
  124. approved_date = fields.Date(
  125. string="Approved Date", track_visibility="onchange"
  126. )
  127. approved_user_id = fields.Many2one(
  128. "res.users", string="Approved By", track_visibility="onchange"
  129. )
  130. currency_id = fields.Many2one("res.currency", string="Currency")
  131. partner_id = fields.Many2one(
  132. "res.partner",
  133. string="Partner",
  134. required=False,
  135. copy=True,
  136. help="The customer or vendor this agreement is related to.",
  137. )
  138. company_partner_id = fields.Many2one(
  139. "res.partner",
  140. string="Company",
  141. copy=True,
  142. default=lambda self: self.env.user.company_id.partner_id,
  143. )
  144. partner_contact_id = fields.Many2one(
  145. "res.partner",
  146. string="Partner Contact",
  147. copy=True,
  148. help="The primary partner contact (If Applicable).",
  149. )
  150. partner_contact_phone = fields.Char(
  151. related="partner_contact_id.phone", string="Phone"
  152. )
  153. partner_contact_email = fields.Char(
  154. related="partner_contact_id.email", string="Email"
  155. )
  156. company_contact_id = fields.Many2one(
  157. "res.partner",
  158. string="Company Contact",
  159. copy=True,
  160. help="The primary contact in the company.",
  161. )
  162. company_contact_phone = fields.Char(
  163. related="company_contact_id.phone", string="Phone"
  164. )
  165. company_contact_email = fields.Char(
  166. related="company_contact_id.email", string="Email"
  167. )
  168. use_parties_content = fields.Boolean(
  169. string="Use parties content",
  170. help="Use custom content for parties")
  171. parties = fields.Html(
  172. string="Parties",
  173. track_visibility="onchange",
  174. help="Parties of the agreement",
  175. )
  176. dynamic_parties = fields.Html(
  177. compute="_compute_dynamic_parties",
  178. string="Dynamic Parties",
  179. help="Compute dynamic parties",
  180. )
  181. agreement_type_id = fields.Many2one(
  182. "agreement.type",
  183. string="Agreement Type",
  184. track_visibility="onchange",
  185. help="Select the type of agreement.",
  186. )
  187. agreement_subtype_id = fields.Many2one(
  188. "agreement.subtype",
  189. string="Agreement Sub-type",
  190. track_visibility="onchange",
  191. help="Select the sub-type of this agreement. Sub-Types are related to "
  192. "agreement types.",
  193. )
  194. product_ids = fields.Many2many(
  195. "product.template", string="Products & Services"
  196. )
  197. assigned_user_id = fields.Many2one(
  198. "res.users",
  199. string="Assigned To",
  200. track_visibility="onchange",
  201. help="Select the user who manages this agreement.",
  202. )
  203. company_signed_user_id = fields.Many2one(
  204. "res.users",
  205. string="Signed By",
  206. track_visibility="onchange",
  207. help="The user at our company who authorized/signed the agreement or "
  208. "contract.",
  209. )
  210. partner_signed_user_id = fields.Many2one(
  211. "res.partner",
  212. string="Signed By",
  213. track_visibility="onchange",
  214. help="Contact on the account that signed the agreement/contract.",
  215. )
  216. parent_agreement_id = fields.Many2one(
  217. "agreement",
  218. string="Parent Agreement",
  219. help="Link this agreement to a parent agreement. For example if this "
  220. "agreement is an amendment to another agreement. This list will "
  221. "only show other agreements related to the same account.",
  222. )
  223. renewal_type_id = fields.Many2one(
  224. "agreement.renewaltype",
  225. string="Renewal Type",
  226. track_visibility="onchange",
  227. help="Describes what happens after the contract expires.",
  228. )
  229. recital_ids = fields.One2many(
  230. "agreement.recital", "agreement_id", string="Recitals", copy=True
  231. )
  232. sections_ids = fields.One2many(
  233. "agreement.section", "agreement_id", string="Sections", copy=True
  234. )
  235. clauses_ids = fields.One2many(
  236. "agreement.clause", "agreement_id", string="Clauses", copy=True
  237. )
  238. appendix_ids = fields.One2many(
  239. "agreement.appendix", "agreement_id", string="Appendices", copy=True
  240. )
  241. previous_version_agreements_ids = fields.One2many(
  242. "agreement",
  243. "parent_agreement_id",
  244. string="Child Agreements",
  245. copy=False,
  246. domain=[("active", "=", False)],
  247. )
  248. child_agreements_ids = fields.One2many(
  249. "agreement",
  250. "parent_agreement_id",
  251. string="Child Agreements",
  252. copy=False,
  253. domain=[("active", "=", True)],
  254. )
  255. line_ids = fields.One2many(
  256. "agreement.line",
  257. "agreement_id",
  258. string="Products/Services",
  259. copy=False,
  260. )
  261. state = fields.Selection(
  262. [("draft", "Draft"), ("active", "Active"), ("inactive", "Inactive")],
  263. default="draft",
  264. track_visibility="always",
  265. )
  266. notification_address_id = fields.Many2one(
  267. "res.partner",
  268. string="Notification Address",
  269. help="The address to send notificaitons to, if different from "
  270. "customer address.(Address Type = Other)",
  271. )
  272. signed_contract_filename = fields.Char(string="Filename")
  273. signed_contract = fields.Binary(
  274. string="Signed Document", track_visibility="always"
  275. )
  276. field_id = fields.Many2one(
  277. "ir.model.fields",
  278. string="Field",
  279. help="""Select target field from the related document model. If it is a
  280. relationship field you will be able to select a target field at the
  281. destination of the relationship.""",
  282. )
  283. sub_object_id = fields.Many2one(
  284. "ir.model",
  285. string="Sub-model",
  286. help="""When a relationship field is selected as first field, this
  287. field shows the document model the relationship goes to.""",
  288. )
  289. sub_model_object_field_id = fields.Many2one(
  290. "ir.model.fields",
  291. string="Sub-field",
  292. help="""When a relationship field is selected as first field, this
  293. field lets you select the target field within the destination document
  294. model (sub-model).""",
  295. )
  296. default_value = fields.Char(
  297. string="Default Value",
  298. help="Optional value to use if the target field is empty.",
  299. )
  300. copyvalue = fields.Char(
  301. string="Placeholder Expression",
  302. help="""Final placeholder expression, to be copy-pasted in the desired
  303. template field.""",
  304. )
  305. # compute the dynamic content for mako expression
  306. @api.multi
  307. def _compute_dynamic_description(self):
  308. MailTemplates = self.env["mail.template"]
  309. for agreement in self:
  310. lang = agreement.partner_id.lang or "en_US"
  311. description = MailTemplates.with_context(
  312. lang=lang
  313. )._render_template(
  314. agreement.description, "agreement", agreement.id
  315. )
  316. agreement.dynamic_description = description
  317. @api.multi
  318. def _compute_dynamic_parties(self):
  319. MailTemplates = self.env["mail.template"]
  320. for agreement in self:
  321. lang = agreement.partner_id.lang or "en_US"
  322. parties = MailTemplates.with_context(
  323. lang=lang
  324. )._render_template(
  325. agreement.parties, "agreement", agreement.id
  326. )
  327. agreement.dynamic_parties = parties
  328. @api.multi
  329. def _compute_dynamic_special_terms(self):
  330. MailTemplates = self.env["mail.template"]
  331. for agreement in self:
  332. lang = agreement.partner_id.lang or "en_US"
  333. special_terms = MailTemplates.with_context(
  334. lang=lang
  335. )._render_template(
  336. agreement.special_terms, "agreement", agreement.id
  337. )
  338. agreement.dynamic_special_terms = special_terms
  339. @api.onchange("field_id", "sub_model_object_field_id", "default_value")
  340. def onchange_copyvalue(self):
  341. self.sub_object_id = False
  342. self.copyvalue = False
  343. self.sub_object_id = False
  344. if self.field_id and not self.field_id.relation:
  345. self.copyvalue = "${{object.{} or {}}}".format(
  346. self.field_id.name, self.default_value or "''"
  347. )
  348. self.sub_model_object_field_id = False
  349. if self.field_id and self.field_id.relation:
  350. self.sub_object_id = self.env["ir.model"].search(
  351. [("model", "=", self.field_id.relation)]
  352. )[0]
  353. if self.sub_model_object_field_id:
  354. self.copyvalue = "${{object.{}.{} or {}}}".format(
  355. self.field_id.name,
  356. self.sub_model_object_field_id.name,
  357. self.default_value or "''",
  358. )
  359. # Used for Kanban grouped_by view
  360. @api.model
  361. def _read_group_stage_ids(self, stages, domain, order):
  362. stage_ids = self.env["agreement.stage"].search([])
  363. return stage_ids
  364. stage_id = fields.Many2one(
  365. "agreement.stage",
  366. string="Stage",
  367. group_expand="_read_group_stage_ids",
  368. help="Select the current stage of the agreement.",
  369. track_visibility="onchange",
  370. index=True,
  371. # default=lambda self: self._default_stage_id(),
  372. )
  373. # Create New Version Button
  374. @api.multi
  375. def create_new_version(self, vals):
  376. for rec in self:
  377. if not rec.state == "draft":
  378. # Make sure status is draft
  379. rec.state = "draft"
  380. default_vals = {
  381. "name": "{} - OLD VERSION".format(rec.name),
  382. "active": False,
  383. "parent_agreement_id": rec.id,
  384. }
  385. # Make a current copy and mark it as old
  386. rec.copy(default=default_vals)
  387. # Increment the Version
  388. rec.version = rec.version + 1
  389. # Reset revision to 0 since it's a new version
  390. vals["revision"] = 0
  391. return super(Agreement, self).write(vals)
  392. def create_new_agreement(self):
  393. default_vals = {
  394. "name": "NEW",
  395. "active": True,
  396. "version": 1,
  397. "revision": 0,
  398. "state": "draft",
  399. }
  400. res = self.copy(default=default_vals)
  401. return {
  402. "res_model": "agreement",
  403. "type": "ir.actions.act_window",
  404. "view_mode": "form",
  405. "view_type": "form",
  406. "res_id": res.id,
  407. }
  408. @api.model
  409. def create(self, vals):
  410. if vals.get("code", _("New")) == _("New"):
  411. vals["code"] = self.env["ir.sequence"].next_by_code(
  412. "agreement"
  413. ) or _("New")
  414. return super(Agreement, self).create(vals)
  415. # Increments the revision on each save action
  416. @api.multi
  417. def write(self, vals):
  418. vals["revision"] = self.revision + 1
  419. return super(Agreement, self).write(vals)