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.

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