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.

123 lines
11 KiB

6 years ago
  1. from odoo import models, fields, api
  2. #Main Agreement Records Model
  3. class Agreement(models.Model):
  4. _name = 'partner_agreement.agreement'
  5. _inherit = ['mail.thread']
  6. #General
  7. name = fields.Char(string="Title", required=True)
  8. is_template = fields.Boolean(string="Is a Template?", default=False, copy=False, help="Make this agreement a template.")
  9. version = fields.Integer(string="Version", default=1, help="The versions are used to keep track of document history and previous versions can be referenced.", copy=False)
  10. revision = fields.Integer(string="Revision", default=0, help="The revision will increase with every save event.", copy=False)
  11. description = fields.Text(string="Description", help="Description of the agreement", track_visibility='onchange')
  12. start_date = fields.Date(string="Start Date", help="When the agreement starts.", track_visibility='onchange')
  13. end_date = fields.Date(string="End Date", help="When the agreement ends.", track_visibility='onchange')
  14. color = fields.Integer()
  15. active = fields.Boolean(string="Active", default=True, help="If unchecked, it will allow you to hide the agreement without removing it.")
  16. company_signed_date = fields.Date(string="Company Signed Date", help="Date the contract was signed by Company.", track_visibility='onchange')
  17. customer_signed_date = fields.Date(string="Customer Signed Date", help="Date the contract was signed by Customer.", track_visibility='onchange')
  18. customer_term = fields.Integer(string="Customer Term (Months)", help="Number of months this agreement/contract is in effect with customer.", track_visibility='onchange')
  19. vendor_term = fields.Integer(string="Vendor Term (Months)", help="Number of months this agreement/contract is in effect with vendor.", track_visibility='onchange')
  20. expiration_notice = fields.Integer(string="Exp. Notice (Days)", help="Number of Days before expiration to be notified.", track_visibility='onchange')
  21. change_notice = fields.Integer(string="Change Notice (Days)", help="Number of Days to be notified before changes.", track_visibility='onchange')
  22. special_terms = fields.Text(string="Special Terms", help="Any terms that you have agreed to and want to track on the agreement/contract.", track_visibility='onchange')
  23. contract_value = fields.Monetary(compute='_compute_contract_value', string="Contract Value", help="Total value of the contract over ther entire term.", store=True)
  24. contract_id = fields.Char(string="ID", help="ID used for internal contract tracking.", track_visibility='onchange')
  25. total_company_mrc = fields.Monetary('Company MRC', currency_field='currency_id', help="Total company monthly recurring costs.")#, compute='_compute_company_mrc')
  26. total_customer_mrc = fields.Monetary('Customer MRC', currency_field='currency_id', help="Total custemer monthly recurring costs.")
  27. total_company_nrc = fields.Monetary('Company NRC', currency_field='currency_id', help="Total company non-recurring costs.")
  28. total_customer_nrc = fields.Monetary('Customer NRC', currency_field='currency_id', help="Total custemer non-monthly recurring costs.")
  29. increase_type = fields.Many2one('partner_agreement.increasetype', string="Increase Type", help="The amount that certain rates may increase.", track_visibility='onchange')
  30. termination_requested = fields.Date(string="Termination Requested Date", help="Date that a request for termination was received.", track_visibility='onchange')
  31. termination_date = fields.Date(string="Termination Date", help="Date that the contract was terminated.", track_visibility='onchange')
  32. customer_address = fields.Char(related='customer.contact_address', string="Address")
  33. customer_street = fields.Char(related='customer.street', string="Street")
  34. customer_street2 = fields.Char(related='customer.street2', string="Street 2")
  35. customer_city = fields.Char(related='customer.city', string="City")
  36. customer_state = fields.Many2one(related='customer.state_id', string="State")
  37. customer_zip = fields.Char(related='customer.zip', string="Zip")
  38. vendor_address = fields.Char(related='vendor.contact_address', string="Address")
  39. vendor_street = fields.Char(related='vendor.street', string="Street")
  40. vendor_street2 = fields.Char(related='vendor.street2', string="Street 2")
  41. vendor_city = fields.Char(related='vendor.city', string="City")
  42. vendor_state = fields.Many2one(related='vendor.state_id', string="State")
  43. vendor_zip = fields.Char(related='vendor.zip', string="Zip")
  44. reviewed_date = fields.Date(string="Reviewed Date", track_visibility='onchange')
  45. reviewed_by = fields.Many2one('res.users', string="Reviewed By", track_visibility='onchange')
  46. approved_date = fields.Date(string="Approved Date", track_visibility='onchange')
  47. approved_by = fields.Many2one('res.users', string="Approved By", track_visibility='onchange')
  48. currency_id = fields.Many2one('res.currency', string='Currency')
  49. customer = fields.Many2one('res.partner', string="Customer", copy=True, help="The customer this agreement is related to (If Applicable).")
  50. vendor = fields.Many2one('res.partner', string="Vendor", copy=True, help="The vendor this agreement is related to (If Applicable).")
  51. customer_contact = fields.Many2one('res.partner', string="Customer Contact", copy=True, help="The primary customer contact (If Applicable).")
  52. customer_contact_phone = fields.Char(related='customer_contact.phone', string="Phone")
  53. customer_contact_email = fields.Char(related='customer_contact.email', string="Email")
  54. vendor_contact = fields.Many2one('res.partner', string="Vendor Contact", copy=True, help="The primary vendor contact (If Applicable).")
  55. vendor_contact_phone = fields.Char(related='vendor_contact.phone', string="Phone")
  56. vendor_contact_email = fields.Char(related='vendor_contact.email', string="Email")
  57. type = fields.Many2one('partner_agreement.type', string="Agreement Type", help="Select the type of agreement.", track_visibility='onchange')
  58. subtype = fields.Many2one('partner_agreement.subtype', string="Agreement Sub-type", help="Select the sub-type of this agreement. Sub-Types are related to agreement types.", track_visibility='onchange')
  59. sale_order = fields.Many2one('sale.order', string="Sales Order", help="Select the Sales Order that this agreement is related to.", copy=False, track_visibility='onchange')
  60. payment_term = fields.Many2one('account.payment.term', string="Payment Term", help="Terms of payments.", track_visibility='onchange')
  61. assigned_to = fields.Many2one('res.users', string="Assigned To", help="Select the user who manages this agreement.", track_visibility='onchange')
  62. company_signed_by = fields.Many2one('res.users', string="Company Signed By", help="The user at our company who authorized/signed the agreement or contract.", track_visibility='onchange')
  63. customer_signed_by = fields.Many2one('res.partner', string="Customer Signed By", help="Contact on the account that signed the agreement/contract.", track_visibility='onchange')
  64. parent_agreement = fields.Many2one('partner_agreement.agreement', string="Parent Agreement", help="Link this agreement to a parent agreement. For example if this agreement is an amendment to another agreement. This list will only show other agreements related to the same account.")
  65. renewal_type = fields.Many2one('partner_agreement.renewaltype', string="Renewal Type", help="Describes what happens after the contract expires.", track_visibility='onchange')
  66. order_lines_services = fields.One2many(related='sale_order.order_line', string="Service Order Lines", copy=False)
  67. # order_lines_nonservices = fields.One2many(related='sale_order.order_line', string="Non Service Order Lines", copy=False, store=False)
  68. sections = fields.One2many('partner_agreement.section', 'agreement', string="Sections", copy=True)
  69. clauses = fields.One2many('partner_agreement.clause', 'agreement', string="Clauses", copy=True)
  70. previous_version_agreements = fields.One2many('partner_agreement.agreement', 'parent_agreement', string="Child Agreements", copy=False, domain=[('active', '=', False)])
  71. child_agreements = fields.One2many('partner_agreement.agreement', 'parent_agreement', string="Child Agreements", copy=False, domain=[('active', '=', True)])
  72. products = fields.Many2many('product.template', string="Products", copy=False)
  73. state = fields.Selection([('draft', 'Draft'),('active', 'Active'),('inactive', 'Inactive')], default='draft', track_visibility='always')
  74. notification_address = fields.Many2one('res.partner', string="Notification Address", help="The address to send notificaitons to, if different from customer address.(Address Type = Other)")
  75. signed_contract_filename = fields.Char(string="Filename")
  76. signed_contract = fields.Binary(string="Signed Document", track_visibility='always')
  77. #compute contract_value field
  78. @api.depends('total_customer_mrc','total_customer_nrc','customer_term')
  79. def _compute_contract_value (self):
  80. for record in self:
  81. record.contract_value = (record.total_customer_mrc * record.customer_term) + record.total_customer_nrc
  82. #compute total_company_mrc field
  83. #@api.multi
  84. @api.depends('order_lines_services','sale_order')
  85. def _compute_company_mrc(self):
  86. order_lines = self.env['sale.order.line'].search([('is_service', '=', True)])
  87. amount_total = sum(order_lines.mapped('purchase_price'))
  88. for record in self:
  89. record.total_company_mrc = amount_total
  90. #Used for Kanban grouped_by view
  91. @api.model
  92. def _read_group_stage_ids(self,stages,domain,order):
  93. stage_ids = self.env['partner_agreement.stage'].search([])
  94. return stage_ids
  95. stage = fields.Many2one('partner_agreement.stage', string="Stage", group_expand='_read_group_stage_ids', help="Select the current stage of the agreement.")
  96. #Create New Version Button
  97. @api.multi
  98. def create_new_version(self, vals):
  99. self.write({'state': 'draft'}) #Make sure status is draft
  100. self.copy(default={'name': self.name + ' - OLD VERSION', 'active': False, 'parent_agreement': self.id}) #Make a current copy and mark it as old
  101. self.write({'version': self.version + 1}) #Increment the Version
  102. #Reset revision to 0 since it's a new version
  103. vals['revision'] = 0
  104. return super(Agreement, self).write(vals)
  105. def create_new_agreement(self):
  106. res = self.copy(default={'name': 'NEW', 'active': True, 'version': 1, 'revision': 0, 'state': 'draft', 'start_date': "", 'end_date':""})
  107. return {'res_model': 'partner_agreement.agreement', 'type': 'ir.actions.act_window', 'view_mode': 'form', 'view_type': 'form', 'res_id': res.id}
  108. #Increments the revision on each save action
  109. def write(self, vals):
  110. vals['revision'] = self.revision + 1
  111. return super(Agreement, self).write(vals)