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.

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