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.

396 lines
12 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. # General
  8. name = fields.Char(
  9. string="Title",
  10. required=True
  11. )
  12. is_template = fields.Boolean(
  13. string="Is a Template?",
  14. default=False,
  15. copy=False,
  16. help="Make this agreement a template."
  17. )
  18. version = fields.Integer(
  19. string="Version",
  20. default=1,
  21. copy=False,
  22. help="The versions are used to keep track of document history and "
  23. "previous versions can be referenced."
  24. )
  25. revision = fields.Integer(
  26. string="Revision",
  27. default=0,
  28. copy=False,
  29. help="The revision will increase with every save event."
  30. )
  31. description = fields.Text(
  32. string="Description",
  33. track_visibility='onchange',
  34. help="Description of the agreement"
  35. )
  36. start_date = fields.Date(
  37. string="Start Date",
  38. track_visibility='onchange',
  39. help="When the agreement starts."
  40. )
  41. end_date = fields.Date(
  42. string="End Date",
  43. track_visibility='onchange',
  44. help="When the agreement ends."
  45. )
  46. color = fields.Integer()
  47. active = fields.Boolean(
  48. string="Active",
  49. default=True,
  50. help="If unchecked, it will allow you to hide the agreement without "
  51. "removing it."
  52. )
  53. company_signed_date = fields.Date(
  54. string="Company Signed Date",
  55. track_visibility='onchange',
  56. help="Date the contract was signed by Company."
  57. )
  58. customer_signed_date = fields.Date(
  59. string="Customer Signed Date",
  60. track_visibility='onchange',
  61. help="Date the contract was signed by Customer."
  62. )
  63. customer_term = fields.Integer(
  64. string="Customer Term (Months)",
  65. track_visibility='onchange',
  66. help="Number of months this agreement/contract is in effect with "
  67. "customer."
  68. )
  69. vendor_term = fields.Integer(
  70. string="Vendor Term (Months)",
  71. track_visibility='onchange',
  72. help="Number of months this agreement/contract is in effect with "
  73. "vendor."
  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. customer_id = fields.Many2one(
  162. 'res.partner',
  163. string="Customer",
  164. copy=True,
  165. help="The customer this agreement is related to (If Applicable)."
  166. )
  167. vendor_id = fields.Many2one(
  168. 'res.partner',
  169. string="Vendor",
  170. copy=True,
  171. help="The vendor this agreement is related to (If Applicable)."
  172. )
  173. customer_contact_id = fields.Many2one(
  174. 'res.partner',
  175. string="Customer Contact",
  176. copy=True,
  177. help="The primary customer contact (If Applicable)."
  178. )
  179. customer_contact_phone = fields.Char(
  180. related='customer_contact_id.phone',
  181. string="Phone"
  182. )
  183. customer_contact_email = fields.Char(
  184. related='customer_contact_id.email',
  185. string="Email"
  186. )
  187. vendor_contact_id = fields.Many2one(
  188. 'res.partner',
  189. string="Vendor Contact",
  190. copy=True,
  191. help="The primary vendor contact (If Applicable)."
  192. )
  193. vendor_contact_phone = fields.Char(
  194. related='vendor_contact_id.phone',
  195. string="Phone"
  196. )
  197. vendor_contact_email = fields.Char(
  198. related='vendor_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="Company Signed By",
  239. track_visibility='onchange',
  240. help="The user at our company who authorized/signed the agreement or "
  241. "contract."
  242. )
  243. customer_signed_user_id = fields.Many2one(
  244. 'res.partner',
  245. string="Customer 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. sections_ids = fields.One2many(
  268. 'agreement.section',
  269. 'agreement_id',
  270. string="Sections",
  271. copy=True
  272. )
  273. clauses_ids = fields.One2many(
  274. 'agreement.clause',
  275. 'agreement_id',
  276. string="Clauses",
  277. copy=True
  278. )
  279. previous_version_agreements_ids = fields.One2many(
  280. 'agreement',
  281. 'parent_agreement_id',
  282. string="Child Agreements",
  283. copy=False,
  284. domain=[('active', '=', False)]
  285. )
  286. child_agreements_ids = fields.One2many(
  287. 'agreement',
  288. 'parent_agreement_id',
  289. string="Child Agreements",
  290. copy=False,
  291. domain=[('active', '=', True)]
  292. )
  293. products_ids = fields.Many2many(
  294. 'product.template',
  295. string="Products",
  296. copy=False
  297. )
  298. state = fields.Selection([
  299. ('draft', 'Draft'),
  300. ('active', 'Active'),
  301. ('inactive', 'Inactive')],
  302. default='draft',
  303. track_visibility='always'
  304. )
  305. notification_address_id = fields.Many2one(
  306. 'res.partner',
  307. string="Notification Address",
  308. help="The address to send notificaitons to, if different from "
  309. "customer address.(Address Type = Other)"
  310. )
  311. signed_contract_filename = fields.Char(
  312. string="Filename"
  313. )
  314. signed_contract = fields.Binary(
  315. string="Signed Document",
  316. track_visibility='always'
  317. )
  318. # compute contract_value field
  319. @api.depends('total_customer_mrc', 'total_customer_nrc', 'customer_term')
  320. def _compute_contract_value(self):
  321. for record in self:
  322. record.contract_value =\
  323. (record.total_customer_mrc * record.customer_term) +\
  324. record.total_customer_nrc
  325. # compute total_company_mrc field
  326. @api.depends('order_lines_services_ids', 'sale_order_id')
  327. def _compute_company_mrc(self):
  328. order_lines = self.env['sale.order.line'].search(
  329. [('is_service', '=', True)])
  330. amount_total = sum(order_lines.mapped('purchase_price'))
  331. for record in self:
  332. record.total_company_mrc = amount_total
  333. # Used for Kanban grouped_by view
  334. @api.model
  335. def _read_group_stage_ids(self, stages, domain, order):
  336. stage_ids = self.env['agreement.stage'].search([])
  337. return stage_ids
  338. stage_id = fields.Many2one(
  339. 'agreement.stage',
  340. string="Stage",
  341. group_expand='_read_group_stage_ids',
  342. help="Select the current stage of the agreement."
  343. )
  344. # Create New Version Button
  345. @api.multi
  346. def create_new_version(self, vals):
  347. for rec in self:
  348. if not rec.state == 'draft':
  349. # Make sure status is draft
  350. rec.state = 'draft'
  351. default_vals = {'name': '{0} - OLD VERSION'.format(rec.name),
  352. 'active': False,
  353. 'parent_agreement_id': rec.id}
  354. # Make a current copy and mark it as old
  355. rec.copy(default=default_vals)
  356. # Increment the Version
  357. rec.version = rec.version + 1
  358. # Reset revision to 0 since it's a new version
  359. vals['revision'] = 0
  360. return super(Agreement, self).write(vals)
  361. def create_new_agreement(self):
  362. default_vals = {'name': 'NEW',
  363. 'active': True,
  364. 'version': 1,
  365. 'revision': 0,
  366. 'state': 'draft'}
  367. res = self.copy(default=default_vals)
  368. return {'res_model': 'agreement',
  369. 'type': 'ir.actions.act_window',
  370. 'view_mode': 'form',
  371. 'view_type': 'form',
  372. 'res_id': res.id}
  373. @api.model
  374. def create(self, vals):
  375. if vals.get('reference', _('New')) == _('New'):
  376. vals['reference'] = \
  377. self.env['ir.sequence'].next_by_code('agreement') or _('New')
  378. return super(Agreement, self).create(vals)
  379. # Increments the revision on each save action
  380. @api.multi
  381. def write(self, vals):
  382. vals['revision'] = self.revision + 1
  383. return super(Agreement, self).write(vals)