采购协议
This commit is contained in:
@@ -132,7 +132,7 @@ class ReSaleOrder(models.Model):
|
||||
'name': '%s/%s/%s/%s/%s/%s' % (
|
||||
self.format_float(product.model_long),
|
||||
self.format_float(product.model_width),
|
||||
self.format_float(product.model_height),
|
||||
self.format_float(product.model_height),
|
||||
self.format_float(product.model_volume),
|
||||
machining_accuracy_name,
|
||||
product.materials_id.name),
|
||||
@@ -303,13 +303,39 @@ class RePurchaseOrder(models.Model):
|
||||
string='采购类型', default='standard', store=True, compute='_compute_purchase_type')
|
||||
|
||||
# 合同编号
|
||||
contract_number = fields.Char(string='合同编号', size=20)
|
||||
contract_number = fields.Char(related='requisition_id.contract_number', string='合同编号', size=20)
|
||||
# 合同概况
|
||||
contract_summary = fields.Text(string='合同概况')
|
||||
|
||||
# 选择是否为紧急采购
|
||||
urgent_purchase = fields.Selection([('no', '否'), ('yes', '是')], string='紧急采购', default='no')
|
||||
|
||||
purchase_requisition_count = fields.Integer('采购协议数量', compute='_compute_purchase_requisition_count')
|
||||
|
||||
partner_ref = fields.Char(related='requisition_id.partner_ref')
|
||||
payment_term_id = fields.Many2one(related='requisition_id.payment_term_id')
|
||||
|
||||
requisition_id = fields.Many2one('purchase.requisition', string='采购协议', copy=False, readonly=True)
|
||||
show_create_requisition_button = fields.Boolean(string='显示创建采购协议按钮',
|
||||
compute='_compute_show_create_requisition_button'
|
||||
)
|
||||
show_requisition_fields = fields.Boolean(string='显示协议按钮', compute='_compute_show_requisition_fields')
|
||||
|
||||
@api.depends('requisition_id')
|
||||
def _compute_show_requisition_fields(self):
|
||||
for order in self:
|
||||
order.show_requisition_fields = bool(order.requisition_id and order.requisition_id.state == 'ongoing')
|
||||
|
||||
@api.depends('requisition_id')
|
||||
def _compute_show_create_requisition_button(self):
|
||||
for order in self:
|
||||
purchase_requisition = self.env['purchase.requisition'].search_count([('origin', '=', order.name)])
|
||||
order.show_create_requisition_button = purchase_requisition > 0
|
||||
|
||||
def _compute_purchase_requisition_count(self):
|
||||
for record in self:
|
||||
record.purchase_requisition_count = len(record.requisition_id)
|
||||
|
||||
@api.depends('origin')
|
||||
def _compute_purchase_type(self):
|
||||
for purchase in self:
|
||||
@@ -344,6 +370,26 @@ class RePurchaseOrder(models.Model):
|
||||
for item in self:
|
||||
if not item.order_line:
|
||||
raise UserError('该询价单未添加【产品】,请进行添加')
|
||||
missing_products = []
|
||||
over_ordered_products = []
|
||||
if item.requisition_id:
|
||||
requisition_product_ids = item.requisition_id.line_ids.mapped('product_id.id')
|
||||
for line in item.order_line:
|
||||
if line.product_id.id not in requisition_product_ids:
|
||||
missing_products.append(line.product_id.name)
|
||||
else:
|
||||
requisition_line = item.requisition_id.line_ids.filtered(
|
||||
lambda r: r.product_id.id == line.product_id.id)
|
||||
remaining_qty = requisition_line.product_qty - requisition_line.qty_ordered
|
||||
if requisition_line and line.product_qty > remaining_qty:
|
||||
over_ordered_products.append(
|
||||
f'{line.product_id.name}:{remaining_qty}{requisition_line.product_uom_id.name}')
|
||||
|
||||
if missing_products:
|
||||
raise UserError('关联协议不存在产品 %s ,无法保存。' % '、'.join(missing_products))
|
||||
if over_ordered_products:
|
||||
raise UserError('当前订购数量超过了关联协议的剩余数量,无法保存。\n涉及产品及剩余数量有:\n%s' % '\n'.join(
|
||||
over_ordered_products))
|
||||
for line in item.order_line:
|
||||
if not line.product_id:
|
||||
raise UserError('【产品】未添加,请进行添加')
|
||||
@@ -399,6 +445,21 @@ class RePurchaseOrder(models.Model):
|
||||
# raise ValidationError('【%s】已存在,请勿重复添加' % product[-1].name)
|
||||
|
||||
def button_confirm(self):
|
||||
over_ordered_products = []
|
||||
for item in self:
|
||||
if item.requisition_id:
|
||||
requisition_product_ids = item.requisition_id.line_ids.mapped('product_id.id')
|
||||
for line in item.order_line:
|
||||
if line.product_id.id in requisition_product_ids:
|
||||
requisition_line = item.requisition_id.line_ids.filtered(
|
||||
lambda r: r.product_id.id == line.product_id.id)
|
||||
remaining_qty = requisition_line.product_qty - requisition_line.qty_ordered
|
||||
if requisition_line and line.product_qty > remaining_qty:
|
||||
over_ordered_products.append(
|
||||
f'{line.product_id.name}:{remaining_qty} {requisition_line.product_uom_id.name}')
|
||||
if over_ordered_products:
|
||||
raise UserError('当前订购数量超过了关联协议的剩余数量,无法保存。\n涉及产品及剩余数量有:\n%s' % '\n'.join(
|
||||
over_ordered_products))
|
||||
result = super(RePurchaseOrder, self).button_confirm()
|
||||
for item in self:
|
||||
# 确认订单时,自动分配序列号
|
||||
@@ -439,6 +500,90 @@ class RePurchaseOrder(models.Model):
|
||||
purchase_order_overdue.write({'delivery_warning': 'normal'})
|
||||
return last_overdue_order, last_warning_order
|
||||
|
||||
def button_create_requisition(self):
|
||||
self.ensure_one()
|
||||
line_ids = []
|
||||
for order_line in self.order_line:
|
||||
line_values = {
|
||||
'product_id': order_line.product_id.id,
|
||||
'product_uom_id': order_line.product_uom.id,
|
||||
'product_qty': order_line.product_qty,
|
||||
'price_unit': order_line.price_unit,
|
||||
}
|
||||
line_ids.append((0, 0, line_values))
|
||||
initial_data = {
|
||||
'vendor_id': self.partner_id.id,
|
||||
'origin': self.name,
|
||||
'line_ids': line_ids,
|
||||
}
|
||||
return {
|
||||
'type': 'ir.actions.act_window',
|
||||
'res_model': 'purchase.requisition',
|
||||
'view_mode': 'form',
|
||||
'target': 'current',
|
||||
'context': {
|
||||
'default_origin': initial_data['origin'],
|
||||
'default_line_ids': initial_data['line_ids'],
|
||||
'default_vendor_id': initial_data['vendor_id'],
|
||||
}
|
||||
}
|
||||
|
||||
def button_link_requisition(self):
|
||||
requisition_ids = self.env['purchase.requisition'].sudo().search(
|
||||
[('state', '=', 'ongoing'), ('vendor_id', '=', self.partner_id.id),
|
||||
('line_ids.product_id', 'in', self.order_line.mapped('product_id.id'))])
|
||||
|
||||
# 新增的逻辑:过滤出符合条件的requisition_ids
|
||||
valid_requisition_ids = []
|
||||
for requisition in requisition_ids:
|
||||
for line in requisition.line_ids:
|
||||
if line.product_id in self.order_line.mapped(
|
||||
'product_id') and line.product_qty - line.qty_ordered >= self.order_line.filtered(
|
||||
lambda ol: ol.product_id == line.product_id).product_qty:
|
||||
valid_requisition_ids.append(requisition.id)
|
||||
break
|
||||
|
||||
action = {
|
||||
'res_model': 'purchase.requisition',
|
||||
'type': 'ir.actions.act_window',
|
||||
'name': _("选择采购申请"),
|
||||
'domain': [('id', 'in', valid_requisition_ids)],
|
||||
'views': [[self.env.ref('sf_sale.purchase_requisition_wizard_tree_view').id, 'list']],
|
||||
'target': 'new',
|
||||
'context': {'purchase_order_id': self.id},
|
||||
}
|
||||
return action
|
||||
|
||||
def button_unlink_requisition(self):
|
||||
self.ensure_one()
|
||||
for line in self.order_line:
|
||||
if self.requisition_id:
|
||||
requisition_line = self.requisition_id.line_ids.filtered(lambda r: r.product_id.id == line.product_id.id)
|
||||
if requisition_line:
|
||||
self.requisition_id.origin = False
|
||||
self.requisition_id.purchase_ids = [(3, self.id)]
|
||||
|
||||
|
||||
def action_view_requisition(self):
|
||||
self.ensure_one()
|
||||
requisition_ids = self.requisition_id.ids
|
||||
action = {
|
||||
'res_model': 'purchase.requisition',
|
||||
'type': 'ir.actions.act_window',
|
||||
}
|
||||
if len(requisition_ids) == 1:
|
||||
action.update({
|
||||
'view_mode': 'form',
|
||||
'res_id': requisition_ids[0],
|
||||
})
|
||||
else:
|
||||
action.update({
|
||||
'name': _("采购协议来源%s", self.name),
|
||||
'domain': [('id', 'in', requisition_ids)],
|
||||
'view_mode': 'tree,form',
|
||||
})
|
||||
return action
|
||||
|
||||
|
||||
class PurchaseOrderLine(models.Model):
|
||||
_inherit = 'purchase.order.line'
|
||||
|
||||
Reference in New Issue
Block a user