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.

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