Files
gzth/yuthon_purchase/models/inherit_purchase_order.py
T
2026-01-14 15:51:41 +08:00

126 lines
5.5 KiB
Python

# -*- coding: utf-8 -*-
from odoo import api, fields, models
import json
from odoo.exceptions import UserError, ValidationError
class InheritPurchaseOrder(models.Model):
# _inherit = 'purchase.order'
_name = 'purchase.order'
_inherit = ['purchase.order', 'soong.workflow.mixin']
active = fields.Boolean('Active', default=True, tracking=True)
json_display = fields.Char(
string="物品详情",
compute='_compute_json_display',
store=True
)
name_title = fields.Char(string="默认标题", default='采购申请单', readonly=True)
code = fields.Char(string="流水号", default=lambda self: self.env['ir.sequence'].next_by_code('purchase_workflow_approval_code'))
state = fields.Selection(selection_add=[('申请人', '申请人'), ('部门经理', '部门经理'), ('工会主席', '工会主席'),
('协办', '协办'),
('复核', '复核'),
('分管领导', '分管领导'),
('总经理', '总经理'), ('归档', '归档')], default='申请人')
work_end = fields.Boolean(string="结束")
user_id = fields.Many2one('res.users', string="申请人", default=lambda self: self.env.user)
company_id = fields.Many2one(related='user_id.employee_ids.company_id', string="公司")
department_id = fields.Many2one(related='user_id.employee_ids.department_id', string="部门")
document_ids = fields.Many2many('ir.attachment', string="附件")
start_date = fields.Date(string="申请日期", default=fields.Date.today())
category_id = fields.Many2one('product.category', string="采购标的类型")
self_users_ids = fields.Many2many('res.users', string="当前经办人",
default=lambda self: [(6, 0, [self.env.user.id])])
pay_way = fields.Selection([('onetime', '一次性'), ('installment', '分期')], string="付款方式", default='onetime')
reason_text = fields.Text(string="申请事由")
pay_line_ids = fields.One2many('purchase.order.pay.line', 'order_id', string="付款详情")
def update_state(self):
order_ids = self.env["purchase.order"].browse(
self._context.get('active_ids', self._context.get('active_id')))
for order in order_ids:
order.button_cancel()
@api.onchange('category_id')
def _onchange_category_id(self):
for record in self:
if record.pay_line_ids:
for line in record.pay_line_ids:
line.category_id = record.category_id.id
def end_approval(self):
self.button_confirm()
self.state = 'purchase'
self.write({'work_end': True})
@api.depends(
'order_line.product_id',
'order_line.product_qty',
'order_line.product_uom',
'order_line.price_unit',
'order_line.price_subtotal'
)
def _compute_json_display(self):
for record in self:
json_data = {
"template": "json_widget_purchase.table_purchase",
"title": f"{record.name}的明细数据",
"lines": [
{
'product_id': line.product_id.name or '',
'product_qty': line.product_uom_qty or '',
'product_uom': line.product_uom.name or '',
'price_unit': line.price_unit or '',
'price_subtotal': line.price_subtotal or ''
}
for idx, line in enumerate(record.order_line)
]
}
record.json_display = json.dumps(json_data, ensure_ascii=False)
def action_refresh_json_display(self):
"""手动刷新json_display字段"""
for record in self:
record._compute_json_display()
return True
def get_no_read_count(self):
domain = [
# ('company_id', '=', self.env.user.company_id.id),
('state','=','draft')
]
return self.search_count(domain)
def action_view_picking(self):
if self.user_id == self.env.user:
raise UserError("您不能接收自己创建的采购订单产品")
return super(InheritPurchaseOrder, self).action_view_picking()
def action_print_purchase_order(self):
"""打印采购审批单"""
report_ref = self.env.ref('yuthon_purchase.action_purchase_order_print_report')
# 使用 sudo 以便打印时绕过联系人等模型的访问限制
return report_ref.report_action(self.sudo())
class InheritPurchaseOrderLine(models.Model):
_inherit = 'purchase.order.line'
category_id = fields.Many2one('product.category', string="产品分类")
category_ids = fields.Many2many('product.category', string="产品分类", compute='_compute_category_id')
@api.depends('category_id')
def _compute_category_id(self):
for record in self:
matching_categories = self.env['product.category']
if record.category_id:
matching_categories = self.env['product.category'].search([
'|',
('id', '=', record.category_id.id),
'|',
('parent_id', '=', record.category_id.id),
('parent_id.parent_id', '=', record.category_id.id),
])
record.category_ids = [(6, 0, matching_categories.ids)] if matching_categories else [(5,)]