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.

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