SCTMES_V5/mes_in_sct/app/action/CheckBomInfo.php

104 lines
4.3 KiB
PHP
Raw Permalink Normal View History

2025-06-14 18:55:09 +08:00
<?php
namespace app\action;
use Exception;
use libs\db\Db;
// 同步EPR物料bom数据
class CheckBomInfo
{
public function execute($post)
{
// 验证数据
$param = check_valid($post['action'], [
['batch', 'string', '生产批次号'],
['workingsubclass', 'string', '工序单元'],
['device_code', 'string', '设备编码'],
], $post['param']);
//1.获取请求体设备编码参数
$workingsubclass = $param["workingsubclass"];
$batch = $param["batch"];
$device_code = $param["device_code"];
//2.判断工序单元、设备编码是存在?
$sql = "SELECT id FROM hf_mes_process_workingsubclass WHERE code='".$workingsubclass."'";
$ret = Db::fetch($sql);
if(empty($ret)){
throw new Exception("工序单元[$workingsubclass]不存在",4001);
}
$sql = "SELECT id FROM hf_mes_device WHERE code='".$device_code."'";
$ret = Db::fetch($sql);
if(empty($ret)){
throw new Exception("设备编码[$device_code]不存在",4001);
}
//3.获取生产批次关联工艺流程工艺流程关联型号型号关联bombom关联bom关系bom关系关联物料信息
$subbatch = '';
$process_code = '';
$sql = "SELECT b.subbatch,c.process FROM hf_mes_production_planning_management_batch a
INNER JOIN hf_mes_production_planning_management_subbatch b ON a.id = b.batch_id
INNER JOIN hf_mes_technology_flow c ON b.flow_id = c.id
WHERE batch='".$batch."' ORDER BY b.id DESC LIMIT 1";
$ret = Db::fetch($sql);
if(empty($ret)){
throw new Exception("批次[".$batch."]不存在子批次信息,请检查批次是否已创建",4001);
}
$subbatch = $ret['subbatch'];
// 拿到工序编码
foreach(json_decode($ret['process'],true) as $val){
if($val[4] == $workingsubclass){
$process_code = $val[2];
}
}
//4.通过以上关联信息查询物料编码
$sql = "SELECT e.code as bom_source_code,d.bom_source_id,d.bom_id,f.code as bom_source_category_code FROM hf_mes_production_planning_management_batch a
INNER JOIN hf_mes_technology_flow b ON a.flow_id = b.id
INNER JOIN hf_mes_product_bom c ON b.product_model_id = c.product_model_id
INNER JOIN hf_mes_product_bom_relationship d ON c.id = d.bom_id
INNER JOIN hf_mes_product_bom_source e ON d.bom_source_id = e.id
INNER JOIN hf_mes_product_bom_source_category f ON e.bom_source_category_id = f.id
WHERE a.batch='".$batch."' AND d.in_workingsubclass='".$workingsubclass."'";
$ret = Db::fetchAll($sql);
if(empty($ret)){
throw new Exception('批次'.$batch.'下的工序单元'.$workingsubclass.'bom关系不存在in结构物料编码',4002);
}
//4.1获取bom的物料编码
$bom_relationship_array = $ret;
$bom_relationship_item_code = array_column($ret,'bom_source_code');
//4.2获取日志缓存表的物料信息
$sql = "SELECT * FROM hf_mes_bkv_batch_process_log WHERE workingsubclass='".$workingsubclass."' AND item_device_code='".$device_code."' AND status=0 ";
$ret = Db::fetchAll($sql);
if(empty($ret)){
throw new Exception('批次'.$batch.'下的工序单元'.$workingsubclass.'bom关系不存在in结构物料编码',4002);
}
$item_code = array_column($ret,"item_code");
$missing_material = [];
foreach($item_code as $key => $val){
if(!in_array($val, $bom_relationship_item_code) ){
$missing_material[] = $val;
}
}
if(count($missing_material)>0){
throw new Exception("上传的item_code物料[".implode(',',$missing_material)."]不在本生产批次[$batch]的[$workingsubclass]工序中投料",4002);
}
$diff_item_code = array_diff($bom_relationship_item_code,$item_code);
if(count($diff_item_code)>0){
throw new Exception("缺少item_code为[".implode(',',$diff_item_code)."]的物料",4002);
}
return '';
}
}