126 lines
5.8 KiB
PHP
126 lines
5.8 KiB
PHP
<?php
|
||
|
||
namespace app\action;
|
||
|
||
use Exception;
|
||
use libs\db\Db;
|
||
|
||
/**
|
||
* ERP成品数据同步接口
|
||
* Class SetInvoice
|
||
* @package app\action
|
||
*/
|
||
class SetInvoice
|
||
{
|
||
public function execute($post)
|
||
{
|
||
// 验证数据
|
||
$param = check_valid($post['action'], [
|
||
['listData', 'array', '成品数据集'],
|
||
], $post['param']);
|
||
|
||
try {
|
||
$listData = $param['listData'];
|
||
|
||
// 检索是否存在必要字段
|
||
foreach ($listData as $index => $item) {
|
||
$idx = $index + 1;
|
||
$check_feilds = [
|
||
['name' => 'delivery_code', 'feild_type' => 'string', 'required' => true, 'len' => 100],
|
||
['name' => 'source_herder_num', 'feild_type' => 'string', 'required' => true, 'len' => 100],
|
||
['name' => 'source_line_num', 'feild_type' => 'string', 'required' => true, 'len' => 100],
|
||
['name' => 'material_code', 'feild_type' => 'string', 'required' => true, 'len' => 100],
|
||
['name' => 'customer_name', 'feild_type' => 'string', 'required' => true, 'len' => 100],
|
||
['name' => 'customer_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' => 'quantity', 'feild_type' => 'string', 'required' => true, 'len' => 20],
|
||
['name' => 'create_time', 'feild_type' => 'timestamp', 'required' => true, 'len' => 6],
|
||
['name' => 'update_time', 'feild_type' => 'timestamp', 'required' => true, 'len' => 6]
|
||
];
|
||
foreach ($check_feilds as $val) {
|
||
if (!isset($item[$val['name']])) {
|
||
throw new Exception("供应商数据集第[{$idx}]位置数据对象不存在字段[{$val['name']}]");
|
||
}
|
||
|
||
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);
|
||
}
|
||
}
|
||
|
||
// 判断创建修改时间格式是否正确
|
||
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']}]的值不能为空");
|
||
}
|
||
}
|
||
}
|
||
|
||
$supplierData[$index]['status'] = 1;
|
||
}
|
||
|
||
// 以发票号分组
|
||
$new_group_data = array_group($listData, 'delivery_code');
|
||
|
||
foreach ($new_group_data as $k1 => $v1) {
|
||
// 检测是否存在发票号,如果存在,先作废掉之前的数据,再新增数据
|
||
$sql = sprintf(
|
||
"SELECT id
|
||
FROM hf_mes_wms_erp_product_outbound
|
||
WHERE delivery_code='%s' LIMIT 1;",
|
||
$k1
|
||
);
|
||
$ret = Db::fetch($sql);
|
||
if (!empty($ret)) {
|
||
// 作废数据
|
||
$sql = sprintf(
|
||
"UPDATE \"%s\" SET status=%s WHERE delivery_code='%s'",
|
||
'hf_mes_wms_erp_product_outbound',
|
||
2,
|
||
$k1
|
||
);
|
||
Db::query($sql);
|
||
}
|
||
|
||
// 新增发票号父级数据
|
||
$erp_product_outbound_id = Db::insert('hf_mes_wms_erp_product_outbound', [
|
||
'delivery_code' => $k1,
|
||
'status' => 1,
|
||
'customer_code' => $v1[0]['customer_code'],
|
||
'customer_name' => $v1[0]['customer_name']
|
||
]);
|
||
|
||
foreach ($v1 as $k2 => $v2) {
|
||
Db::insert('hf_mes_wms_erp_product_outbound_details', [
|
||
'erp_product_outbound_id' => $erp_product_outbound_id,
|
||
'source_herder_num' => $v2['source_herder_num'],
|
||
'source_line_num' => $v2['source_line_num'],
|
||
'material_code' => $v2['material_code'],
|
||
'quantity' => $v2['quantity'],
|
||
'unit_code' => $v2['unit_code'],
|
||
'unit_name' => $v2['unit_name'],
|
||
'create_time' => $v2['create_time'],
|
||
'update_time' => $v2['update_time']
|
||
]);
|
||
}
|
||
}
|
||
} catch (Exception $e) {
|
||
throw new Exception($e->getMessage());
|
||
}
|
||
|
||
return '';
|
||
}
|
||
}
|