215 lines
7.4 KiB
Python
215 lines
7.4 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']
|
|
_description = "采购订单"
|
|
|
|
_tier_validation_buttons_xpath = "/form/header/button[@id='draft_confirm'][1]"
|
|
|
|
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 button_confirm(self):
|
|
for record in self:
|
|
# if record.need_validation and record.validation_status != 'validated':
|
|
# raise ValidationError(_('此操作需要至少对一条记录进行审批。\n请发起审批申请。'))
|
|
if record.state in ['to approve']:
|
|
raise ValidationError(_('请先完成审批。'))
|
|
# if record.state == 'approved':
|
|
# record.state = 'purchase'
|
|
return super().button_confirm()
|
|
|
|
# def button_confirm(self):
|
|
# self = self.with_context(skip_validation=True)
|
|
# return super().button_confirm()
|
|
#
|
|
# def _check_state_conditions(self, vals):
|
|
# self.ensure_one()
|
|
# if self._context.get('skip_validation'):
|
|
# return False
|
|
# return (
|
|
# self._check_state_from_condition()
|
|
# and vals.get(self._state_field) in self._state_to
|
|
# )
|
|
|
|
def request_validation(self):
|
|
for record in self:
|
|
error_messages = []
|
|
|
|
# 检查必填字段
|
|
required_fields = {
|
|
'partner_ref': '合同名称',
|
|
'contract_number': '合同编号'
|
|
}
|
|
|
|
missing_fields = [
|
|
name for field, name in required_fields.items()
|
|
if not record[field]
|
|
]
|
|
|
|
if missing_fields:
|
|
error_messages.append('* 如下字段要求必须填写:%s' % '、'.join(missing_fields))
|
|
|
|
# 检查合同文件
|
|
if not record.contract_document_id:
|
|
error_messages.append('* 必须点击上传合同文件')
|
|
|
|
# 如果有任何错误,一次性显示所有错误信息
|
|
if error_messages:
|
|
raise ValidationError('\n'.join(error_messages))
|
|
|
|
# 添加通知消息
|
|
if hasattr(record, 'message_post'):
|
|
current_user = self.env.user.name
|
|
record.message_post(
|
|
body=f"<strong>{current_user}</strong> 提交审批",
|
|
message_type='notification',
|
|
subtype_xmlid='mail.mt_note'
|
|
)
|
|
res = super(jikimo_purchase_tier_validation, self).request_validation()
|
|
self.state = 'to approve'
|
|
return res
|
|
|
|
def restart_validation(self):
|
|
res = super(jikimo_purchase_tier_validation, self).restart_validation()
|
|
self.state = 'draft'
|
|
return res
|
|
|
|
def _validate_tier(self, tiers=False):
|
|
res = super(jikimo_purchase_tier_validation, self)._validate_tier(tiers)
|
|
|
|
# 检查是否所有审批都已通过
|
|
all_approved = all(
|
|
tier_review.status == 'approved'
|
|
for tier_review in self.review_ids
|
|
)
|
|
|
|
if self.review_ids and all_approved: # 确保有审批记录
|
|
self.state = 'approved'
|
|
|
|
return res
|
|
|
|
def _rejected_tier(self, tiers=False):
|
|
res = super(jikimo_purchase_tier_validation, self)._rejected_tier(tiers)
|
|
self.state = 'draft'
|
|
return res
|
|
|
|
@api.model
|
|
def _get_under_validation_exceptions(self):
|
|
res = super(jikimo_purchase_tier_validation, self)._get_under_validation_exceptions()
|
|
res.append("state")
|
|
return res
|
|
|
|
# 上传合同文件
|
|
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,
|
|
}
|
|
}
|
|
|
|
|
|
# class jikimo_purchase_request(models.Model):
|
|
# _inherit = 'purchase.request'
|
|
# _description = "采购申请"
|
|
|
|
|
|
# class jikimo_account_payment(models.Model):
|
|
# _inherit = 'account.payment'
|
|
# _description = "付款单"
|
|
|
|
|
|
# class jikimo_account_move(models.Model):
|
|
# _inherit = 'account.move'
|
|
# _description = "发票账单"
|