|
|
@@ -2985,105 +2985,233 @@ class WorkOrderSpotCheck extends Api{
|
|
|
$this->error('参数错误');
|
|
|
}
|
|
|
|
|
|
- // 获取当前最大批次号
|
|
|
- $lastBatch = \db('物料_库存')
|
|
|
- ->field("批次号")
|
|
|
- ->orderRaw("CAST(SUBSTRING(批次号, 4) AS UNSIGNED) DESC")
|
|
|
- ->limit(1)
|
|
|
- ->value('批次号');
|
|
|
-
|
|
|
- $currentNumber = 0;
|
|
|
- if (!empty($lastBatch) && preg_match('/^PCH(\d+)$/i', $lastBatch, $match)) {
|
|
|
- $currentNumber = intval($match[1]);
|
|
|
- }
|
|
|
+ // 开启事务
|
|
|
+ Db::startTrans();
|
|
|
|
|
|
- foreach ($param as $item) {
|
|
|
- // 查找是否已有相同物料编号 + 关联号的库存记录
|
|
|
- $existingBatch = \db('物料_库存')
|
|
|
- ->where('物料编号', $item['物料编码'])
|
|
|
- ->where('物料名称', $item['物料名称'])
|
|
|
- ->whereNull('Mod_rq')
|
|
|
- ->where('关联号', $item['关联编号'])
|
|
|
+ try {
|
|
|
+ //获取最大批次号
|
|
|
+ $lastBatch = Db::name('物料_库存')
|
|
|
+ ->where('批次号', 'like', 'GDGL%')
|
|
|
+ ->orderRaw('CAST(SUBSTRING(批次号, 5) AS UNSIGNED) DESC')
|
|
|
->value('批次号');
|
|
|
+ $currentNumber = $lastBatch ? intval(substr($lastBatch, 4)) : 0;
|
|
|
|
|
|
+ $insertInventory = []; // 新入库
|
|
|
+ $updateInventory = []; // 更新库存
|
|
|
+ $insertLogs = []; // 出入库日志
|
|
|
|
|
|
- // 更新库存数据
|
|
|
- $Inventory = \db('物料_库存')
|
|
|
- ->where('批次号',$item['批次号'])
|
|
|
- ->field('库存数量,入仓总量')
|
|
|
- ->find();
|
|
|
+ foreach ($param as $item) {
|
|
|
+ if (empty($item['物料编码']) || empty($item['关联编号']) || !isset($item['number'])) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 唯一KEY:物料编码 + 关联号(判断是否已存在)
|
|
|
+ $uniqueKey = $item['物料编码'] . '_' . $item['关联编号'];
|
|
|
|
|
|
+ // 检查是否已存在
|
|
|
+ static $exists = [];
|
|
|
+ if (!isset($exists[$uniqueKey])) {
|
|
|
+ $exists[$uniqueKey] = Db::name('物料_库存')
|
|
|
+ ->where('物料编号', $item['物料编码'])
|
|
|
+ ->where('关联号', $item['关联编号'])
|
|
|
+ ->whereNull('Mod_rq')
|
|
|
+ ->value('批次号');
|
|
|
+ }
|
|
|
+ $existingBatch = $exists[$uniqueKey];
|
|
|
+
|
|
|
+ if ($existingBatch) {
|
|
|
+ //更新
|
|
|
+ $updateInventory[] = [
|
|
|
+ '批次号' => $existingBatch,
|
|
|
+ 'data' => [
|
|
|
+ '入仓总量' => Db::raw('入仓总量 + ' . intval($item['number'])),
|
|
|
+ '库存数量' => Db::raw('库存数量 + ' . intval($item['number'])),
|
|
|
+ '实际门幅' => $item['实际门幅'],
|
|
|
+ 'departname' => $item['departname'],
|
|
|
+ 'remark' => $item['remark'] ?? '',
|
|
|
+ 'sys_rq' => date('Y-m-d H:i:s'),
|
|
|
+ ]
|
|
|
+ ];
|
|
|
+ $BatchNumber = $existingBatch;
|
|
|
+ } else {
|
|
|
+ //新增
|
|
|
+ $currentNumber++;
|
|
|
+ $BatchNumber = 'GDGL' . $currentNumber;
|
|
|
+
|
|
|
+ $insertInventory[] = [
|
|
|
+ '批次号' => $BatchNumber,
|
|
|
+ '关联号' => $item['关联编号'],
|
|
|
+ '物料编号' => $item['物料编码'],
|
|
|
+ '物料名称' => $item['物料名称'],
|
|
|
+ '入仓总量' => $item['number'],
|
|
|
+ '库存数量' => $item['number'],
|
|
|
+ '单位' => $item['单位'],
|
|
|
+ '实际门幅' => $item['实际门幅'],
|
|
|
+ '状态' => 1,
|
|
|
+ 'departname' => $item['departname'],
|
|
|
+ 'remark' => $item['remark'] ?? '',
|
|
|
+ 'sys_id' => $item['sys_id'],
|
|
|
+ 'sys_rq' => date('Y-m-d H:i:s'),
|
|
|
+ ];
|
|
|
+ }
|
|
|
|
|
|
- if ($existingBatch) {
|
|
|
- $BatchNumber = $existingBatch; // 已入库,使用原批次号
|
|
|
- $InventoryData = [
|
|
|
- '批次号' => $item['批次号'],
|
|
|
- '关联号' => $item['关联编号'],
|
|
|
- '物料编号' => $item['物料编码'],
|
|
|
+ //日志
|
|
|
+ $insertLogs[] = [
|
|
|
+ '批次号' => $BatchNumber,
|
|
|
+ 'order_id' => $item['order_id'],
|
|
|
+ '款号' => $item['款号'],
|
|
|
+ '物料编码' => $item['物料编码'],
|
|
|
'物料名称' => $item['物料名称'],
|
|
|
- '入仓总量' => $Inventory['入仓总量'] + $item['number'],
|
|
|
- '库存数量' => $Inventory['库存数量'] + $item['number'],
|
|
|
- '实际门幅' => $item['实际门幅'],
|
|
|
- '单位' => $item['单位'],
|
|
|
- '状态' => 1,
|
|
|
- 'departname' => $item['departname'],
|
|
|
- 'remark' => isset($item['remark']) ? $item['remark'] : '',
|
|
|
- 'sys_id' => $item['sys_id'],
|
|
|
- 'sys_rq' => date('Y-m-d H:i:s'),
|
|
|
- ];
|
|
|
- $sql = \db('物料_库存')->fetchSql(true)->where('批次号',$BatchNumber)->update($InventoryData);
|
|
|
- \db()->query($sql);
|
|
|
- } else {
|
|
|
- // 生成新批次号
|
|
|
- $currentNumber++;
|
|
|
- $BatchNumber = 'PCH' . $currentNumber;
|
|
|
-
|
|
|
- // 插入库存表
|
|
|
- $InventoryData = [
|
|
|
- '批次号' => $BatchNumber,
|
|
|
- '关联号' => $item['关联编号'],
|
|
|
- '物料编号' => $item['物料编码'],
|
|
|
- '物料名称' => $item['物料名称'],
|
|
|
- '入仓总量' => $item['number'],
|
|
|
- '库存数量' => $item['number'],
|
|
|
- '单位' => $item['单位'],
|
|
|
- '实际门幅' => $item['实际门幅'],
|
|
|
- '状态' => 1,
|
|
|
- 'departname' => $item['departname'],
|
|
|
- 'remark' => isset($item['remark']) ? $item['remark'] : '',
|
|
|
- 'sys_id' => $item['sys_id'],
|
|
|
- 'sys_rq' => date('Y-m-d H:i:s'),
|
|
|
+ '库存数量' => $item['number'],
|
|
|
+ 'number' => $item['number'],
|
|
|
+ 'rq' => $item['rq'],
|
|
|
+ 'name' => $item['name'],
|
|
|
+ 'sys_id' => $item['sys_id'],
|
|
|
+ 'receipt_number' => $item['receipt_number'],
|
|
|
+ 'departname' => $item['departname'],
|
|
|
+ 'remark' => $item['remark'] ?? '',
|
|
|
+ '客户编号' => $item['客户编号'],
|
|
|
+ '款式' => $item['款式'],
|
|
|
+ 'sys_rq' => date('Y-m-d H:i:s'),
|
|
|
];
|
|
|
- $sql = \db('物料_库存')->fetchSql(true)->insert($InventoryData);
|
|
|
- \db()->query($sql);
|
|
|
}
|
|
|
|
|
|
- // 每条都写一条报工日志
|
|
|
- $ReportData = [
|
|
|
- '批次号' => $BatchNumber,
|
|
|
- 'order_id' => $item['order_id'],
|
|
|
- '款号' => $item['款号'],
|
|
|
- '物料编码' => $item['物料编码'],
|
|
|
- '物料名称' => $item['物料名称'],
|
|
|
- '库存数量' => $item['number'],
|
|
|
- 'number' => $item['number'],
|
|
|
- 'rq' => $item['rq'],
|
|
|
- 'name' => $item['name'],
|
|
|
- 'sys_id' => $item['sys_id'],
|
|
|
- 'receipt_number' => $item['receipt_number'],
|
|
|
- 'departname' => $item['departname'],
|
|
|
- 'remark' => isset($item['remark']) ? $item['remark'] : '',
|
|
|
- '客户编号' => $item['客户编号'],
|
|
|
- '款式' => $item['款式'],
|
|
|
- 'sys_rq' => date('Y-m-d H:i:s'),
|
|
|
- ];
|
|
|
- $sql = \db('库存_出入库明细')->fetchSql(true)->insert($ReportData);
|
|
|
- \db()->query($sql);
|
|
|
- }
|
|
|
+ // 批量新增库存
|
|
|
+ if ($insertInventory) {
|
|
|
+ Db::name('物料_库存')->insertAll($insertInventory);
|
|
|
+ }
|
|
|
|
|
|
- $this->success('成功');
|
|
|
+ // 批量更新库存
|
|
|
+ foreach ($updateInventory as $u) {
|
|
|
+ Db::name('物料_库存')
|
|
|
+ ->where('批次号', $u['批次号'])
|
|
|
+ ->update($u['data']);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 批量写入日志
|
|
|
+ if ($insertLogs) {
|
|
|
+ Db::name('库存_出入库明细')->insertAll($insertLogs);
|
|
|
+ }
|
|
|
+
|
|
|
+ Db::commit();
|
|
|
+ $this->success('入库成功');
|
|
|
+
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Db::rollback();
|
|
|
+ $this->error('操作失败:' . $e->getMessage());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+// public function inputStash()
|
|
|
+// {
|
|
|
+// if (!$this->request->isPost()) {
|
|
|
+// $this->error('请求错误');
|
|
|
+// }
|
|
|
+//
|
|
|
+// $param = Request::instance()->post();
|
|
|
+// if (empty($param)) {
|
|
|
+// $this->error('参数错误');
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 获取当前最大批次号
|
|
|
+// $lastBatch = \db('物料_库存')
|
|
|
+// ->field("批次号")
|
|
|
+// ->orderRaw("CAST(SUBSTRING(批次号, 4) AS UNSIGNED) DESC")
|
|
|
+// ->limit(1)
|
|
|
+// ->value('批次号');
|
|
|
+//
|
|
|
+// $currentNumber = 0;
|
|
|
+// if (!empty($lastBatch) && preg_match('/^PCH(\d+)$/i', $lastBatch, $match)) {
|
|
|
+// $currentNumber = intval($match[1]);
|
|
|
+// }
|
|
|
+//
|
|
|
+// foreach ($param as $item) {
|
|
|
+// // 查找是否已有相同物料编号 + 关联号的库存记录
|
|
|
+// $existingBatch = \db('物料_库存')
|
|
|
+// ->where('物料编号', $item['物料编码'])
|
|
|
+// ->where('物料名称', $item['物料名称'])
|
|
|
+// ->whereNull('Mod_rq')
|
|
|
+// ->where('关联号', $item['关联编号'])
|
|
|
+// ->value('批次号');
|
|
|
+//
|
|
|
+//
|
|
|
+// // 更新库存数据
|
|
|
+// $Inventory = \db('物料_库存')
|
|
|
+// ->where('批次号',$item['批次号'])
|
|
|
+// ->field('库存数量,入仓总量')
|
|
|
+// ->find();
|
|
|
+//
|
|
|
+//
|
|
|
+// if ($existingBatch) {
|
|
|
+// $BatchNumber = $existingBatch; // 已入库,使用原批次号
|
|
|
+// $InventoryData = [
|
|
|
+// '批次号' => $item['批次号'],
|
|
|
+// '关联号' => $item['关联编号'],
|
|
|
+// '物料编号' => $item['物料编码'],
|
|
|
+// '物料名称' => $item['物料名称'],
|
|
|
+// '入仓总量' => $Inventory['入仓总量'] + $item['number'],
|
|
|
+// '库存数量' => $Inventory['库存数量'] + $item['number'],
|
|
|
+// '实际门幅' => $item['实际门幅'],
|
|
|
+// '单位' => $item['单位'],
|
|
|
+// '状态' => 1,
|
|
|
+// 'departname' => $item['departname'],
|
|
|
+// 'remark' => isset($item['remark']) ? $item['remark'] : '',
|
|
|
+// 'sys_id' => $item['sys_id'],
|
|
|
+// 'sys_rq' => date('Y-m-d H:i:s'),
|
|
|
+// ];
|
|
|
+// $sql = \db('物料_库存')->fetchSql(true)->where('批次号',$BatchNumber)->update($InventoryData);
|
|
|
+// \db()->query($sql);
|
|
|
+// } else {
|
|
|
+// // 生成新批次号
|
|
|
+// $currentNumber++;
|
|
|
+// $BatchNumber = 'PCH' . $currentNumber;
|
|
|
+//
|
|
|
+// // 插入库存表
|
|
|
+// $InventoryData = [
|
|
|
+// '批次号' => $BatchNumber,
|
|
|
+// '关联号' => $item['关联编号'],
|
|
|
+// '物料编号' => $item['物料编码'],
|
|
|
+// '物料名称' => $item['物料名称'],
|
|
|
+// '入仓总量' => $item['number'],
|
|
|
+// '库存数量' => $item['number'],
|
|
|
+// '单位' => $item['单位'],
|
|
|
+// '实际门幅' => $item['实际门幅'],
|
|
|
+// '状态' => 1,
|
|
|
+// 'departname' => $item['departname'],
|
|
|
+// 'remark' => isset($item['remark']) ? $item['remark'] : '',
|
|
|
+// 'sys_id' => $item['sys_id'],
|
|
|
+// 'sys_rq' => date('Y-m-d H:i:s'),
|
|
|
+// ];
|
|
|
+// $sql = \db('物料_库存')->fetchSql(true)->insert($InventoryData);
|
|
|
+// \db()->query($sql);
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 每条都写一条报工日志
|
|
|
+// $ReportData = [
|
|
|
+// '批次号' => $BatchNumber,
|
|
|
+// 'order_id' => $item['order_id'],
|
|
|
+// '款号' => $item['款号'],
|
|
|
+// '物料编码' => $item['物料编码'],
|
|
|
+// '物料名称' => $item['物料名称'],
|
|
|
+// '库存数量' => $item['number'],
|
|
|
+// 'number' => $item['number'],
|
|
|
+// 'rq' => $item['rq'],
|
|
|
+// 'name' => $item['name'],
|
|
|
+// 'sys_id' => $item['sys_id'],
|
|
|
+// 'receipt_number' => $item['receipt_number'],
|
|
|
+// 'departname' => $item['departname'],
|
|
|
+// 'remark' => isset($item['remark']) ? $item['remark'] : '',
|
|
|
+// '客户编号' => $item['客户编号'],
|
|
|
+// '款式' => $item['款式'],
|
|
|
+// 'sys_rq' => date('Y-m-d H:i:s'),
|
|
|
+// ];
|
|
|
+// $sql = \db('库存_出入库明细')->fetchSql(true)->insert($ReportData);
|
|
|
+// \db()->query($sql);
|
|
|
+// }
|
|
|
+//
|
|
|
+// $this->success('成功');
|
|
|
+// }
|
|
|
+
|
|
|
/**
|
|
|
* 出库订单信息查询
|
|
|
*/
|