SCTMES_V5/mes_in_sct/app/action/SetMaterial.php

313 lines
13 KiB
PHP
Raw Normal View History

2025-06-14 18:55:09 +08:00
<?php
namespace app\action;
use Exception;
use libs\db\Db;
/**
* ERP物料同步接口
* Class SetMaterial
* @package app\action
*/
class SetMaterial
{
public function execute($post)
{
// 验证数据
$param = check_valid($post['action'], [
['materialData', 'array', '物料数据集'],
], $post['param']);
try{
$materialData = $param['materialData'];
// 检索是否存在必要字段
foreach ($materialData as $index => $item) {
$idx = $index + 1;
$check_feilds = [
['name' => 'name', 'feild_type' => 'string', 'required' => true, 'len' => 100],
['name' => 'code', 'feild_type' => 'string', 'required' => true, 'len' => 100],
['name' => 'unit_code', 'feild_type' => 'string', 'required' => true, 'len' => 100],
['name' => 'unit_name', 'feild_type' => 'string', 'required' => true, 'len' => 100],
['name' => 'create_time', 'feild_type' => 'timestamp', 'required' => true, 'len' => 6],
['name' => 'update_time', 'feild_type' => 'timestamp', 'required' => true, 'len' => 6],
['name' => 'remark', 'feild_type' => 'text', 'required' => false, 'len' => 1],
['name' => 'active', 'feild_type' => 'int', 'required' => true, 'len' => 100],
['name' => 'sublibrary_code', 'feild_type' => 'string', 'required' => false, 'len' => 100],
['name' => 'sublibrary_name', 'feild_type' => 'string', 'required' => false, 'len' => 100],
];
foreach ($check_feilds as $val) {
if (!isset($item[$val['name']])) {
throw new Exception("物料数据集第[{$idx}]位置数据对象不存在字段[{$val['name']}]", 4001);
}
if ($val['required']) {
if ($val['feild_type'] == 'string') {
// 判断是否是一个字符串
if (!is_string($item[$val['name']])) {
throw new Exception("物料数据集第[{$idx}]位置数据对象[{$val['name']}]的值不是字符串类型,请检查!", 4001);
}
// 判断长度
if (strlen($item[$val['name']]) > $val['len']) {
throw new Exception("物料数据集第[{$idx}]位置数据对象[{$val['name']}]的值不符合规定的长度[{$val['len']}],请检查!", 4001);
}
} elseif ($val['feild_type'] == 'int') {
if (!is_int($item[$val['name']])) {
throw new Exception("物料数据集第[{$idx}]位置数据对象对应的字段[{$val['name']}]值不是整数数字类型", 4001);
}
if ($val == 'active') {
if (!in_array($item[$val['name']], [0, 1])) {
throw new Exception("物料数据集第[{$idx}]位置数据对象对应的字段[{$val['name']}]值只能是0或者1", 4001);
}
}
}
// 判断创建修改时间格式是否正确
if ($val['name'] == 'create_time' || $val['name'] == 'update_time') {
if (date('Y-m-d H:i:s', strtotime($item[$val['name']])) != $item[$val['name']]) {
throw new Exception("物料数据集第[{$idx}]位置数据对象字段[{$val['name']}]的时间格式不正确正确例子如2023-01-01 09:09:09", 4001);
}
}
if (empty($item[$val['name']])) {
throw new Exception("物料数据集第[{$idx}]位置数据对象字段[{$val['name']}]的值不能为空", 4001);
}
}
}
$materialData[$index]['status'] = $item['active'];
}
}catch (Exception $e) {
throw new Exception($e->getMessage(), $e->getCode() ? $e->getCode() : 4001);
}
Db::beginTrans();
try {
foreach ($materialData as $key => $value) {
$idx = $key + 1;
// ERP品名开头两个字符是FB或者BS从品名中截取
$value['tax_info'] = '';
$tax_str = substr($value['name'] , 0 , 2);
if(in_array($tax_str, ['FB', 'BS'])){
$value['tax_info'] = $tax_str;
}
// 判断子库编码是否存在
$sql = sprintf(
"SELECT id
FROM hf_mes_wms_erp_sublibrary
WHERE code='%s' LIMIT 1;",
$value['sublibrary_code']
);
$ret = Db::fetch($sql);
$value['erp_sublibrary_id'] = 0;
if (empty($ret)) {
$value['erp_sublibrary_id'] = Db::insert('hf_mes_wms_erp_sublibrary', [
'code' => $value['sublibrary_code'],
'name' => $value['sublibrary_name']
]);
}else{
$value['erp_sublibrary_id'] = $ret['id'];
}
// 判断计量单位是否存在,不存在则新增
$sql = sprintf(
"SELECT id
FROM hf_mes_unit
WHERE code='%s' LIMIT 1;",
$value['unit_code']
);
$ret = Db::fetch($sql);
if (empty($ret)) {
$value['unit_id'] = Db::insert('hf_mes_unit', [
'code' => $value['unit_code'],
'name' => $value['unit_name']
]);
}else{
$value['unit_id'] = $ret['id'];
}
// 检测编码是否存在
$sql = sprintf(
"SELECT id
FROM hf_mes_wms_material
WHERE code='%s' LIMIT 1;",
$value['code']
);
$ret = Db::fetch($sql);
if (!empty($ret)) {
// 修改数据
$update_keys = [
'name',
'code',
'create_time',
'update_time',
'status',
'unit_id',
'material_category_name',
'material_category_code',
'sublibrary_code',
'sublibrary_name',
'barcode',
'erp_sublibrary_id',
'tax_info',
'remark'
];
$update_str = '';
foreach ($update_keys as $key) {
if (isset($value[$key])) {
if (!empty($value[$key])) {
if ($key == 'status') {
$update_str .= "\"{$key}\"={$value[$key]},";
} else {
$update_str .= "\"{$key}\"='{$value[$key]}',";
}
}
}
}
$sql = sprintf(
"UPDATE \"%s\" SET %s WHERE id='%s'",
'hf_mes_wms_material',
rtrim($update_str, ','),
$ret['id']
);
Db::query($sql);
} else {
// 新增数据
$insert_keys = [
'name',
'code',
'create_time',
'update_time',
'status',
'unit_id',
'material_category_name',
'material_category_code',
'barcode',
'sublibrary_code',
'sublibrary_name',
'erp_sublibrary_id',
'tax_info',
'remark'
];
$insert_key_str = '';
$insert_val_str = '';
foreach ($insert_keys as $key) {
if (isset($value[$key])) {
if (!empty($value[$key])) {
$insert_key_str .= "\"{$key}\",";
if ($key == 'status') {
$insert_val_str .= "{$value[$key]},";
} else {
$insert_val_str .= "'{$value[$key]}',";
}
}
}
}
$sql = sprintf(
"INSERT INTO %s(%s) VALUES (%s)",
'hf_mes_wms_material',
rtrim($insert_key_str, ','),
rtrim($insert_val_str, ',')
);
$row = Db::query($sql);
if (!$row) {
throw new Exception("新增物料失败", 4001);
}
}
// 检测bom_source编码是否存在
$sql = sprintf(
"SELECT id
FROM hf_mes_product_bom_source
WHERE code='%s' LIMIT 1;",
$value['code']
);
$ret = Db::fetch($sql);
if (!empty($ret)) {
// 修改数据
$update_keys = [
'name',
'code',
'create_time',
'update_time',
'status',
'unit_id',
'remark'
];
$update_str = '';
foreach ($update_keys as $key) {
if (isset($value[$key])) {
if (!empty($value[$key])) {
if ($key == 'status') {
$update_str .= "\"{$key}\"={$value[$key]},";
} else {
$update_str .= "\"{$key}\"='{$value[$key]}',";
}
}
}
}
$sql = sprintf(
"UPDATE \"%s\" SET %s WHERE id='%s'",
'hf_mes_product_bom_source',
rtrim($update_str, ','),
$ret['id']
);
Db::query($sql);
} else {
// 新增数据
$insert_keys = [
'name',
'code',
'create_time',
'update_time',
'status',
'unit_id',
'remark'
];
$insert_key_str = '';
$insert_val_str = '';
foreach ($insert_keys as $key) {
if (isset($value[$key])) {
if (!empty($value[$key])) {
$insert_key_str .= "\"{$key}\",";
if ($key == 'status') {
$insert_val_str .= "{$value[$key]},";
} else {
$insert_val_str .= "'{$value[$key]}',";
}
}
}
}
$sql = sprintf(
"INSERT INTO %s(%s) VALUES (%s)",
'hf_mes_product_bom_source',
rtrim($insert_key_str, ','),
rtrim($insert_val_str, ',')
);
$row = Db::query($sql);
if (!$row) {
throw new Exception("新增物料失败", 4001);
}
}
}
} catch (Exception $e) {
Db::rollBackTrans();
throw new Exception($e->getMessage(), $e->getCode() ? $e->getCode() : 4001);
}
Db::commitTrans();
return '';
}
}