120 lines
4.8 KiB
Python
120 lines
4.8 KiB
Python
from odoo import api, fields, models, _
|
|
from odoo.tools import float_compare
|
|
|
|
|
|
class PurchaseOrder(models.Model):
|
|
_inherit = 'purchase.order'
|
|
|
|
state = fields.Selection([
|
|
('draft', '询价'),
|
|
('sent', '发送询价'),
|
|
('to approve', '待批准'),
|
|
("approved", "已批准"),
|
|
('purchase', '采购订单'),
|
|
('done', '完成'),
|
|
('cancel', '取消'),
|
|
('rejected', '已驳回')
|
|
], string='Status', readonly=True, index=True, copy=False, default='draft', tracking=True)
|
|
|
|
# 成品采购订单对应的坯料采购申请单和采购订单数量
|
|
purchase_request_count = fields.Integer('子·采购申请数量', compute='_compute_purchase_request')
|
|
purchase_order_count = fields.Integer('子·采购订单数量', compute='_compute_purchase_request')
|
|
|
|
@api.depends('state')
|
|
def _compute_purchase_request(self):
|
|
for record in self:
|
|
purchase_request_ids, purchase_order_ids = record.get_purchase_request_order()
|
|
record.purchase_request_count = len(purchase_request_ids)
|
|
record.purchase_order_count = len(purchase_order_ids)
|
|
|
|
def action_view_preform_body_purchase_request(self):
|
|
self.ensure_one()
|
|
name_list = self._get_pinking_name()
|
|
purchase_request_ids = self.env['purchase.request'].search([('origin', 'in', name_list)])
|
|
|
|
action = {
|
|
'res_model': 'purchase.request',
|
|
'type': 'ir.actions.act_window',
|
|
}
|
|
if len(purchase_request_ids) == 1:
|
|
action.update({
|
|
'view_mode': 'form',
|
|
'res_id': purchase_request_ids[0].id,
|
|
})
|
|
else:
|
|
action.update({
|
|
'name': _("子·采购申请"),
|
|
'domain': [('id', 'in', purchase_request_ids.ids)],
|
|
'view_mode': 'tree,form',
|
|
})
|
|
return action
|
|
|
|
def action_view_preform_body_purchase_order(self):
|
|
self.ensure_one()
|
|
name_list = self._get_pinking_name()
|
|
purchase_order_ids = self.env['purchase.order'].search([('origin', 'in', name_list)])
|
|
|
|
action = {
|
|
'res_model': 'purchase.order',
|
|
'type': 'ir.actions.act_window',
|
|
}
|
|
if len(purchase_order_ids) == 1:
|
|
action.update({
|
|
'view_mode': 'form',
|
|
'res_id': purchase_order_ids[0].id,
|
|
})
|
|
else:
|
|
action.update({
|
|
'name': _("子·采购订单"),
|
|
'domain': [('id', 'in', purchase_order_ids.ids)],
|
|
'view_mode': 'tree,form',
|
|
})
|
|
return action
|
|
|
|
def get_purchase_request_order(self):
|
|
name_list = self._get_pinking_name()
|
|
purchase_request_ids = self.env['purchase.request'].search([('origin', 'in', name_list)])
|
|
purchase_order_ids = self.env['purchase.order'].search([('origin', 'in', name_list)])
|
|
return purchase_request_ids, purchase_order_ids
|
|
|
|
def _get_pinking_name(self):
|
|
return [picking_id.name for picking_id in self.picking_ids if picking_id.name]
|
|
|
|
def button_confirm(self):
|
|
res = super(PurchaseOrder, self).button_confirm()
|
|
# 取消反向调拨单
|
|
reverse_move_ids = self.env['stock.move'].search([
|
|
('origin', '=', self.name),
|
|
('purchase_line_id', '=', False),
|
|
('state', '!=', 'done')
|
|
])
|
|
if reverse_move_ids:
|
|
reverse_move_ids.picking_id.action_cancel()
|
|
return res
|
|
|
|
def button_cancel(self):
|
|
"""
|
|
将取消的采购订单关联的库存移动撤销
|
|
"""
|
|
move_ids = self.order_line.move_dest_ids.filtered(lambda move: move.state != 'done' and not move.scrapped)
|
|
res =super(PurchaseOrder, self).button_cancel()
|
|
if move_ids.mapped('created_purchase_request_line_id'):
|
|
move_ids.write({'state': 'waiting', 'is_done': False})
|
|
return res
|
|
|
|
def write(self, vals):
|
|
res = super(PurchaseOrder, self).write(vals)
|
|
if 'state' in vals and vals['state'] == 'purchase':
|
|
purchase_request = self.order_line.purchase_request_lines.request_id
|
|
if purchase_request:
|
|
finished = True
|
|
# 判断该采购申请所有明细行是否都完成
|
|
for purchase_request_line in purchase_request.line_ids:
|
|
finished_qty = sum(purchase_request_line.purchase_lines.filtered(lambda line: line.state == 'purchase').mapped('product_qty'))
|
|
if float_compare(finished_qty ,purchase_request_line.product_qty, precision_rounding=purchase_request_line.product_id.uom_id.rounding) < 0:
|
|
finished = False
|
|
break
|
|
if finished:
|
|
purchase_request.button_done()
|
|
return res
|