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.

402 lines
13 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
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, models, fields, _
  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. start_date = fields.Date(
  39. string="Start Date",
  40. track_visibility='onchange',
  41. help="When the agreement starts."
  42. )
  43. end_date = fields.Date(
  44. string="End Date",
  45. track_visibility='onchange',
  46. help="When the agreement ends."
  47. )
  48. color = fields.Integer()
  49. active = fields.Boolean(
  50. string="Active",
  51. default=True,
  52. help="If unchecked, it will allow you to hide the agreement without "
  53. "removing it."
  54. )
  55. company_signed_date = fields.Date(
  56. string="Company Signed Date",
  57. track_visibility='onchange',
  58. help="Date the contract was signed by Company."
  59. )
  60. customer_signed_date = fields.Date(
  61. string="Customer Signed Date",
  62. track_visibility='onchange',
  63. help="Date the contract was signed by Customer."
  64. )
  65. customer_term = fields.Integer(
  66. string="Customer Term (Months)",
  67. track_visibility='onchange',
  68. help="Number of months this agreement/contract is in effect with "
  69. "customer."
  70. )
  71. vendor_term = fields.Integer(
  72. string="Vendor Term (Months)",
  73. track_visibility='onchange',
  74. help="Number of months this agreement/contract is in effect with "
  75. "vendor."
  76. )
  77. expiration_notice = fields.Integer(
  78. string="Exp. Notice (Days)",
  79. track_visibility='onchange',
  80. help="Number of Days before expiration to be notified."
  81. )
  82. change_notice = fields.Integer(
  83. string="Change Notice (Days)",
  84. track_visibility='onchange',
  85. help="Number of Days to be notified before changes."
  86. )
  87. special_terms = fields.Text(
  88. string="Special Terms",
  89. track_visibility='onchange',
  90. help="Any terms that you have agreed to and want to track on the "
  91. "agreement/contract."
  92. )
  93. contract_value = fields.Monetary(
  94. compute='_compute_contract_value',
  95. string="Contract Value",
  96. help="Total value of the contract over ther entire term.",
  97. store=True
  98. )
  99. reference = fields.Char(
  100. string="Reference",
  101. required=True,
  102. default=lambda self: _('New'),
  103. track_visibility='onchange',
  104. help="ID used for internal contract tracking.")
  105. total_company_mrc = fields.Monetary(
  106. 'Company MRC',
  107. currency_field='currency_id',
  108. help="Total company monthly recurring costs."
  109. )
  110. total_customer_mrc = fields.Monetary(
  111. 'Customer MRC',
  112. currency_field='currency_id',
  113. help="Total custemer monthly recurring costs."
  114. )
  115. total_company_nrc = fields.Monetary(
  116. 'Company NRC',
  117. currency_field='currency_id',
  118. help="Total company non-recurring costs."
  119. )
  120. total_customer_nrc = fields.Monetary(
  121. 'Customer NRC',
  122. currency_field='currency_id',
  123. help="Total custemer non-monthly recurring costs."
  124. )
  125. increase_type_id = fields.Many2one(
  126. 'agreement.increasetype',
  127. string="Increase Type",
  128. track_visibility='onchange',
  129. help="The amount that certain rates may increase."
  130. )
  131. termination_requested = fields.Date(
  132. string="Termination Requested Date",
  133. track_visibility='onchange',
  134. help="Date that a request for termination was received."
  135. )
  136. termination_date = fields.Date(
  137. string="Termination Date",
  138. track_visibility='onchange',
  139. help="Date that the contract was terminated."
  140. )
  141. reviewed_date = fields.Date(
  142. string="Reviewed Date",
  143. track_visibility='onchange'
  144. )
  145. reviewed_user_id = fields.Many2one(
  146. 'res.users',
  147. string="Reviewed By",
  148. track_visibility='onchange'
  149. )
  150. approved_date = fields.Date(
  151. string="Approved Date",
  152. track_visibility='onchange'
  153. )
  154. approved_user_id = fields.Many2one(
  155. 'res.users',
  156. string="Approved By",
  157. track_visibility='onchange'
  158. )
  159. currency_id = fields.Many2one(
  160. 'res.currency',
  161. string='Currency'
  162. )
  163. customer_id = fields.Many2one(
  164. 'res.partner',
  165. string="Customer",
  166. copy=True,
  167. help="The customer this agreement is related to (If Applicable)."
  168. )
  169. vendor_id = fields.Many2one(
  170. 'res.partner',
  171. string="Vendor",
  172. copy=True,
  173. help="The vendor this agreement is related to (If Applicable)."
  174. )
  175. customer_contact_id = fields.Many2one(
  176. 'res.partner',
  177. string="Customer Contact",
  178. copy=True,
  179. help="The primary customer contact (If Applicable)."
  180. )
  181. customer_contact_phone = fields.Char(
  182. related='customer_contact_id.phone',
  183. string="Phone"
  184. )
  185. customer_contact_email = fields.Char(
  186. related='customer_contact_id.email',
  187. string="Email"
  188. )
  189. vendor_contact_id = fields.Many2one(
  190. 'res.partner',
  191. string="Vendor Contact",
  192. copy=True,
  193. help="The primary vendor contact (If Applicable)."
  194. )
  195. vendor_contact_phone = fields.Char(
  196. related='vendor_contact_id.phone',
  197. string="Phone"
  198. )
  199. vendor_contact_email = fields.Char(
  200. related='vendor_contact_id.email',
  201. string="Email"
  202. )
  203. agreement_type_id = fields.Many2one(
  204. 'agreement.type',
  205. string="Agreement Type",
  206. track_visibility='onchange',
  207. help="Select the type of agreement."
  208. )
  209. agreement_subtype_id = fields.Many2one(
  210. 'agreement.subtype',
  211. string="Agreement Sub-type",
  212. track_visibility='onchange',
  213. help="Select the sub-type of this agreement. Sub-Types are related to "
  214. "agreement types."
  215. )
  216. product_ids = fields.Many2many(
  217. 'product.template',
  218. string="Products & Services")
  219. sale_order_id = fields.Many2one(
  220. 'sale.order',
  221. string="Sales Order",
  222. track_visibility='onchange',
  223. copy=False,
  224. help="Select the Sales Order that this agreement is related to."
  225. )
  226. payment_term_id = fields.Many2one(
  227. 'account.payment.term',
  228. string="Payment Term",
  229. track_visibility='onchange',
  230. help="Terms of payments."
  231. )
  232. assigned_user_id = fields.Many2one(
  233. 'res.users',
  234. string="Assigned To",
  235. track_visibility='onchange',
  236. help="Select the user who manages this agreement."
  237. )
  238. company_signed_user_id = fields.Many2one(
  239. 'res.users',
  240. string="Company Signed By",
  241. track_visibility='onchange',
  242. help="The user at our company who authorized/signed the agreement or "
  243. "contract."
  244. )
  245. customer_signed_user_id = fields.Many2one(
  246. 'res.partner',
  247. string="Customer Signed By",
  248. track_visibility='onchange',
  249. help="Contact on the account that signed the agreement/contract."
  250. )
  251. parent_agreement_id = fields.Many2one(
  252. 'agreement',
  253. string="Parent Agreement",
  254. help="Link this agreement to a parent agreement. For example if this "
  255. "agreement is an amendment to another agreement. This list will "
  256. "only show other agreements related to the same account."
  257. )
  258. renewal_type_id = fields.Many2one(
  259. 'agreement.renewaltype',
  260. string="Renewal Type",
  261. track_visibility='onchange',
  262. help="Describes what happens after the contract expires."
  263. )
  264. order_lines_services_ids = fields.One2many(
  265. related='sale_order_id.order_line',
  266. string="Service Order Lines",
  267. copy=False
  268. )
  269. sections_ids = fields.One2many(
  270. 'agreement.section',
  271. 'agreement_id',
  272. string="Sections",
  273. copy=True
  274. )
  275. clauses_ids = fields.One2many(
  276. 'agreement.clause',
  277. 'agreement_id',
  278. string="Clauses",
  279. copy=True
  280. )
  281. previous_version_agreements_ids = fields.One2many(
  282. 'agreement',
  283. 'parent_agreement_id',
  284. string="Child Agreements",
  285. copy=False,
  286. domain=[('active', '=', False)]
  287. )
  288. child_agreements_ids = fields.One2many(
  289. 'agreement',
  290. 'parent_agreement_id',
  291. string="Child Agreements",
  292. copy=False,
  293. domain=[('active', '=', True)]
  294. )
  295. products_ids = fields.Many2many(
  296. 'product.template',
  297. string="Products",
  298. copy=False
  299. )
  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 contract_value field
  321. @api.depends('total_customer_mrc', 'total_customer_nrc', 'customer_term')
  322. def _compute_contract_value(self):
  323. for record in self:
  324. record.contract_value =\
  325. (record.total_customer_mrc * record.customer_term) +\
  326. record.total_customer_nrc
  327. # compute total_company_mrc field
  328. @api.depends('order_lines_services_ids', 'sale_order_id')
  329. def _compute_company_mrc(self):
  330. order_lines = self.env['sale.order.line'].search(
  331. [('is_service', '=', True)])
  332. amount_total = sum(order_lines.mapped('purchase_price'))
  333. for record in self:
  334. record.total_company_mrc = amount_total
  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 = {'name': '{0} - OLD VERSION'.format(rec.name),
  357. 'active': False,
  358. 'parent_agreement_id': rec.id}
  359. # Make a current copy and mark it as old
  360. rec.copy(default=default_vals)
  361. # Increment the Version
  362. rec.version = rec.version + 1
  363. # Reset revision to 0 since it's a new version
  364. vals['revision'] = 0
  365. return super(Agreement, self).write(vals)
  366. def create_new_agreement(self):
  367. default_vals = {'name': 'NEW',
  368. 'active': True,
  369. 'version': 1,
  370. 'revision': 0,
  371. 'state': 'draft'}
  372. res = self.copy(default=default_vals)
  373. return {'res_model': 'agreement',
  374. 'type': 'ir.actions.act_window',
  375. 'view_mode': 'form',
  376. 'view_type': 'form',
  377. 'res_id': res.id}
  378. @api.model
  379. def create(self, vals):
  380. if vals.get('reference', _('New')) == _('New'):
  381. vals['reference'] = \
  382. self.env['ir.sequence'].next_by_code('agreement') or _('New')
  383. return super(Agreement, self).create(vals)
  384. # Increments the revision on each save action
  385. @api.multi
  386. def write(self, vals):
  387. vals['revision'] = self.revision + 1
  388. return super(Agreement, self).write(vals)