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.

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