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.

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