# -*- coding: utf-8 -*- from odoo import models, api, fields, _ import base64 from odoo.exceptions import ValidationError,UserError TRACE_ACTIONS = [ ('forward', _('Forwarded')), ('receive', _('Received')), ('archive', _('Archived')), ('approve', _('Unit Manager Approved')), ('ceo_approve', _('CEO Approved')), ('sent', _('Sent')), ('return', _('Returned')), ('action', _('Action Taken')), ('refuse', _('Refused')), ('reply', _('Replied')), ('waite', _('Waiting Approve')), ('reopen', _('Reopened')), ] class SubjectType(models.Model): _name = 'cm.subject.type' _inherit = ['mail.thread'] _order = 'sequence' name = fields.Char(string='Transaction Type', tracking=True) sequence = fields.Integer(string='Sequence', default=5, tracking=True) second_approval = fields.Boolean(string='Second Approval ?', help='Check if this transaction type need a second (CEO) Approval.', default=True, tracking=True) transaction_need_approve = fields.Boolean(string="Transaction need approve", tracking=True) tran_tag = fields.Many2many(comodel_name='transaction.tag', string='Tags', tracking=True) def unlink(self): for rec in self: internal_transaction_id = self.env['internal.transaction'].search([('subject_type_id', '=', rec.id)]).ids outgoing_transaction_id = self.env['outgoing.transaction'].search([('subject_type_id', '=', rec.id)]).ids incoming_transaction_id = self.env['incoming.transaction'].search([('subject_type_id', '=', rec.id)]).ids if internal_transaction_id or outgoing_transaction_id or incoming_transaction_id: raise ValidationError(_("You Can Not This Delete Subject Type, Because There is a Related Transaction.")) return super(SubjectType, self).unlink() class ImportantDegree(models.Model): _name = 'cm.transaction.important' name = fields.Char(string='Important Degree') rank = fields.Integer(string='Transaction Duration') def unlink(self): for rec in self: internal_transaction_id = self.env['internal.transaction'].search([('important_id', '=', rec.id)]).ids outgoing_transaction_id = self.env['outgoing.transaction'].search([('important_id', '=', rec.id)]).ids incoming_transaction_id = self.env['incoming.transaction'].search([('important_id', '=', rec.id)]).ids if internal_transaction_id or outgoing_transaction_id or incoming_transaction_id: raise ValidationError(_("You Can Not Delete This Transaction Important, Because There is a Related Transaction.")) return super(ImportantDegree, self).unlink() class Procedure(models.Model): _name = 'cm.procedure' name = fields.Char(string='Procedure Name') def unlink(self): for rec in self: internal_transaction_id = self.env['internal.transaction'].search([('procedure_id', '=', rec.id)]).ids outgoing_transaction_id = self.env['outgoing.transaction'].search([('procedure_id', '=', rec.id)]).ids incoming_transaction_id = self.env['incoming.transaction'].search([('procedure_id', '=', rec.id)]).ids if internal_transaction_id or outgoing_transaction_id or incoming_transaction_id: raise ValidationError(_("You Can Not Delete This Procedure, Because There is a Related Transaction.")) return super(Procedure, self).unlink() class AttachmentType(models.Model): _name = 'cm.attachment.type' sequence = fields.Integer(string='Sequence', default=1) name = fields.Char(string='Name') def unlink(self): for rec in self: transaction_trace_id = self.env['cm.attachment'].search([('type_id', '=', rec.id)]).ids if transaction_trace_id: raise ValidationError(_("You Can Not Delete This Attachment, Because There is a Related Transaction Attachments.")) return super(AttachmentType, self).unlink() class Attachment(models.Model): _name = 'cm.attachment' name = fields.Char(string='Description') num_page = fields.Integer(string='No. Pages') type_id = fields.Many2one('cm.attachment.type', string='Attachment type') incoming_transaction_id = fields.Many2one(comodel_name='incoming.transaction', string='Incoming Transaction') internal_transaction_id = fields.Many2one(comodel_name='internal.transaction', string='Internal Transaction') outgoing_transaction_id = fields.Many2one(comodel_name='outgoing.transaction', string='Outgoing Transaction') class ArchiveType(models.Model): _name = 'cm.archive.type' name = fields.Char(string='Archive Type') def unlink(self): for rec in self: transaction_trace_id = self.env['cm.transaction.trace'].search([('archive_type_id', '=', rec.id)]).ids if transaction_trace_id: raise ValidationError(_("You Can Not Delete This Archive Type, Because There is a Related Transaction Trace Record.")) return super(ArchiveType, self).unlink() class AttachmentRule(models.Model): _name = 'cm.attachment.rule' def _default_employee_id(self): user = self.env.user em = self.env['cm.entity'].search([('user_id', '=', user.id)], limit=1) return len(em) and em or self.env['cm.entity'] name = fields.Char() employee_id = fields.Many2one(comodel_name='cm.entity', string='Created By', default=lambda self: self._default_employee_id(), readonly="True") entity_id = fields.Many2one(comodel_name='cm.entity', string='Unit Responsible', related='internal_transaction_id.preparation_id.manager_id', store=True) file_save = fields.Many2many('ir.attachment', String="Save File") external_drive_link = fields.Text('External Drive Link') attachment_filename = fields.Char(string="Attachment Filename") incoming_transaction_id = fields.Many2one(comodel_name='incoming.transaction', string='Incoming Transaction') internal_transaction_id = fields.Many2one(comodel_name='internal.transaction', string='Internal Transaction') outgoing_transaction_id = fields.Many2one(comodel_name='outgoing.transaction', string='Outgoing Transaction') date = fields.Datetime(string='Date', default=fields.Datetime.now) description = fields.Char(string='Description') signed = fields.Boolean(string='Signed',readonly=True) created_from_system = fields.Boolean(readonly=True) signed_user_id = fields.Many2one('res.users') def action_sign_transaction(self): self.ensure_one() return # def action_signature(self): # for rec in self: # if rec.internal_transaction_id: # x = self.env['letters.letters'].search([('internal_transaction_id','=',rec.internal_transaction_id.id)],limit=1) # elif rec.incoming_transaction_id: # x = self.env['letters.letters'].search([('incoming_transaction_id','=',rec.incoming_transaction_id.id)],limit=1) # elif rec.outgoing_transaction_id: # x = self.env['letters.letters'].search([('outgoing_transaction_id','=',rec.outgoing_transaction_id.id)],limit=1) # rec.signed_user_id = self.env.user.id # x.signed_user_id = self.env.user.id # return { # 'type': 'ir.actions.act_window', # 'name': 'Preferences', # 'res_model': 'res.users', # 'view_mode': 'form', # 'view_id': self.env.ref('base.view_users_form_simple_modif').id, # 'target': 'new', # 'context': { # 'default_id': self.env.user.id, # 'default_letter_id': x.id , # 'default_attachment_id': rec.id, # }, # 'res_id': self.env.user.id, # } # @api.constrains('file_save') # def _check_attachment_size(self): # max_size = 4 * 1024 * 1024 # 4 MB # for record in self: # if record.file_save: # file_size = len(base64.b64decode(record.file_save)) # if file_size > max_size: # raise ValidationError(_('Attachment %s exceeds the maximum allowed size of 4 MB.') % record.attachment_filename) # @api.onchange('file_save') # def _onchange_file_save(self): # max_size = 4 * 1024 * 1024 # 4 MB # for record in self: # if record.file_save: # file_size = len(base64.b64decode(record.file_save)) # if file_size > max_size: # record.file_save = False # raise UserError(_('Attachment %s exceeds the maximum allowed size of 4 MB.') % record.attachment_filename) class TransactionTrace(models.Model): _name = 'cm.transaction.trace' _description = 'Transaction Trace' _order = 'date desc' action = fields.Selection(string='Action', selection=TRACE_ACTIONS, default='forward') incoming_transaction_id = fields.Many2one(comodel_name='incoming.transaction', string='Incoming Transaction') internal_transaction_id = fields.Many2one(comodel_name='internal.transaction', string='Internal Transaction') outgoing_transaction_id = fields.Many2one(comodel_name='outgoing.transaction', string='Outgoing Transaction') date = fields.Datetime(string='Date', default=fields.Datetime.now) from_id = fields.Many2one(comodel_name='cm.entity', string='From') to_id = fields.Many2one(comodel_name='cm.entity', string='To') procedure_id = fields.Many2one(comodel_name='cm.procedure', string='Action Taken') note = fields.Char(string='Notes') archive_type_id = fields.Many2one(comodel_name='cm.archive.type', string='Archive Type') cc_ids = fields.Many2many('cm.entity', string='CC To') body = fields.Html(string='Transaction Details') @api.model def create(self, vals): record = super(TransactionTrace, self).create(vals) now = fields.Datetime.now() if record.incoming_transaction_id: record.incoming_transaction_id.last_action_date = now elif record.internal_transaction_id: record.internal_transaction_id.last_action_date = now elif record.outgoing_transaction_id: record.outgoing_transaction_id.last_action_date = now return record class ProjectType(models.Model): _name = "project.type" name = fields.Char(string='Name') sequence = fields.Integer(string='Sequence', default=1) class TransactionCategory(models.Model): _name = 'transaction.tag' name = fields.Char("Name") def unlink(self): for rec in self: internal_transaction_id = self.env['internal.transaction'].search([('tran_tag', 'in', rec.id)]).ids outgoing_transaction_id = self.env['outgoing.transaction'].search(['|',('tran_tag', 'in', rec.id),('tran_tag_unit', 'in', rec.id)]).ids incoming_transaction_id = self.env['incoming.transaction'].search([('tran_tag', 'in', rec.id)]).ids if internal_transaction_id or outgoing_transaction_id or incoming_transaction_id: raise ValidationError(_("You Can Not Delete This Transaction Category, Because There is a Related Transaction.")) return super(TransactionCategory, self).unlink()