119 lines
4.2 KiB
Python
119 lines
4.2 KiB
Python
from odoo import models, fields, api, _
|
|
from odoo.exceptions import ValidationError
|
|
import logging
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
class jikimo_purchase_tier_validation(models.Model):
|
|
_name = 'purchase.order'
|
|
_inherit = ['purchase.order', 'tier.validation']
|
|
|
|
contract_document_id = fields.Many2one('documents.document', string='合同文件')
|
|
contract_file = fields.Binary(related='contract_document_id.datas', string='合同文件内容')
|
|
contract_file_name = fields.Char(related='contract_document_id.attachment_id.name', string='文件名')
|
|
|
|
# 是否已上传合同文件
|
|
is_upload_contract_file = fields.Boolean(string='是否已上传合同文件', default=False)
|
|
|
|
def request_validation(self):
|
|
for record in self:
|
|
missing_fields = []
|
|
if not record.partner_ref:
|
|
missing_fields.append('合同名称')
|
|
if not record.contract_number:
|
|
missing_fields.append('合同编号')
|
|
|
|
if missing_fields:
|
|
raise ValidationError(_('如下字段要求必须填写:%s') % '、'.join(missing_fields))
|
|
|
|
return super(jikimo_purchase_tier_validation, self).request_validation()
|
|
|
|
# 上传合同文件
|
|
def upload_contract_file(self):
|
|
print('upload_contract_file===========================')
|
|
# self.ensure_one()
|
|
# return {
|
|
# 'name': _('上传合同文件'),
|
|
# 'type': 'ir.actions.act_window',
|
|
# 'res_model': 'ir.attachment',
|
|
# 'view_mode': 'form',
|
|
# 'view_type': 'form',
|
|
# 'target': 'new',
|
|
# 'context': {
|
|
# 'default_res_model': self._name,
|
|
# 'default_res_id': self.id,
|
|
# 'default_type': 'binary',
|
|
# 'default_mimetype': 'application/pdf,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,image/jpeg,image/png',
|
|
# }
|
|
# }
|
|
|
|
self.ensure_one()
|
|
action = {
|
|
'type': 'ir.actions.act_window',
|
|
'name': _('上传合同文件'),
|
|
'res_model': 'ir.attachment.wizard', # 我们需要创建一个新的向导模型
|
|
'view_mode': 'form',
|
|
'target': 'new',
|
|
'context': {
|
|
'default_res_model': self._name,
|
|
'default_res_id': self.id,
|
|
}
|
|
}
|
|
return action
|
|
|
|
# 删除合同文件
|
|
def delete_contract_file(self):
|
|
self.ensure_one()
|
|
if self.contract_document_id:
|
|
try:
|
|
document = self.contract_document_id
|
|
|
|
# 清空关联
|
|
self.write({
|
|
'contract_document_id': False,
|
|
'contract_file': False,
|
|
'contract_file_name': False
|
|
})
|
|
|
|
# 删除文档
|
|
if document:
|
|
document.with_context(no_attachment=True).sudo().unlink()
|
|
|
|
self.is_upload_contract_file = False
|
|
|
|
# 返回视图动作来刷新当前表单
|
|
return {
|
|
'type': 'ir.actions.act_window',
|
|
'res_model': 'purchase.order',
|
|
'res_id': self.id,
|
|
'view_mode': 'form',
|
|
'view_type': 'form',
|
|
'target': 'current',
|
|
'flags': {'mode': 'readonly'},
|
|
}
|
|
|
|
except Exception as e:
|
|
_logger.error('删除合同文件时出错: %s', str(e))
|
|
return {
|
|
'type': 'ir.actions.client',
|
|
'tag': 'display_notification',
|
|
'params': {
|
|
'title': _('错误'),
|
|
'message': _('删除文件时出现错误'),
|
|
'type': 'danger',
|
|
'sticky': True,
|
|
}
|
|
}
|
|
|
|
return {
|
|
'type': 'ir.actions.client',
|
|
'tag': 'display_notification',
|
|
'params': {
|
|
'title': _('提示'),
|
|
'message': _('没有需要删除的合同文件'),
|
|
'type': 'warning',
|
|
'sticky': False,
|
|
}
|
|
}
|