chen.lin
20 小时以前 a45b751080a50d94d3ed2d9f1d801e14a7046b6d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>生成领料出库</title>
    <link rel="stylesheet" href="../../static/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../static/css/pda.css" media="all">
    <script type="text/javascript" src="../../static/js/jquery/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="../../static/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../../static/js/common.js" charset="utf-8"></script>
    <script type="text/javascript" src="../../static/js/cool.js" charset="utf-8"></script>
</head>
<style>
    .form-box span {
        font-size: 16px;
        display: inline-block;
        text-align: right;
    }
    .form-box input {
        width: 165px;
        padding-left: 5px;
        height: 30px;
        border: 1px solid #777777;
        overflow:hidden;
        white-space:nowrap;
        text-overflow:ellipsis;
    }
    .number-tool {
        margin-left: 10px;
        padding: 1px 0 1px 5px;
        display: inline-block;
        width: 120px;
    }
 
    .number-tool:after {
        clear: both;
        content: "";
        display: table;
    }
 
    .number-tool button {
        background-color: #fff;
        margin-top: 3px;
        font-size: 16px;
        height: 25px;
        float: left;
        width: 25px;
        border: 1px solid #777777;
    }
 
    .number-tool input {
        text-align: center;
        height: 30px;
        float: left;
        margin: 0 5px;
        width: 50px;
        padding: 0;
    }
</style>
<body>
 
<header class="layui-form">
    <div class="layui-input-inline">
        <label class="layui-form-label" >生产单号</label>
        <div class="layui-input-inline">
            <input id="soCode" class="layui-input" autocomplete="off" oninput="findCode(this, 'soCode')"
                   placeholder="扫码 / 输入"   style="width: 175px">
        </div>
    </div>
    <div class="layui-input-inline">
        <label class="layui-form-label">任务单号</label>
        <input class="layui-input" id="fbillno" onkeyup="findCode(this, 'fbillno')" placeholder="扫码 / 输入"
               style="width: 175px"
               autocomplete="off">
    </div>
    <div class="layui-input-inline">
        <label class="layui-form-label">物料编码</label>
        <input class="layui-input" id="invCode" onkeyup="findCode(this, 'invCode')" placeholder="扫码 / 输入"
               style="width: 175px"
               autocomplete="off">
    </div>
<!--    <div class="layui-input-inline">-->
<!--        <label class="layui-form-label">开工日期</label>-->
<!--        <input class="layui-input" id="fplancommitdate" placeholder="选择或输入日期"-->
<!--               style="width: 175px"-->
<!--               autocomplete="off"-->
<!--               onkeyup="findCode(this, 'fplancommitdate')"-->
<!--               onchange="findCode(this, 'fplancommitdate')">-->
<!--    </div>-->
</header>
 
<main>
    <table class="layui-table" id="materialReceiveOut" lay-filter="materialReceiveOut"></table>
</main>
 
<footer>
    <div class="layui-btn-container">
        <button type="button" id="reset-btn" class="layui-btn layui-btn-primary" onclick="reset()">重置</button>
        <button type="button" id="out-btn" class="layui-btn layui-btn-normal " onclick="materialReceiveOut()"
                style="margin-left: 20px">出库
        </button>
        <button type="button" id="retrun-btn" class="layui-btn layui-btn-primary " onclick="back()"
                style="margin-left: 20px">返回
        </button>
        <span id="tips"></span>
    </div>
</footer>
 
<!-- 修改数量弹窗 -->
<div id="modify" style="display: none; padding-top: 10px; text-align: center;">
    <div class="form-box">
        <div class="form-item">
            <table style="display: inline">
                <tr>
                    <td>
                        <span style="width: 35px; margin-right: 5px">编码</span>
                    </td>
                    <td style="text-align: left">
                        <input id="invCode2" type="text" disabled="disabled">
                    </td>
                </tr>
            </table>
        </div>
        <div class="form-item">
            <table style="display: inline">
                <tr>
                    <td style="vertical-align: top">
                        <span style="width: 35px; margin-right: 5px">名称</span>
                    </td>
                    <td style="text-align: left">
                    <textarea rows="3" style="resize: none; width: 165px" id="invName2" type="text" disabled="disabled"
                              readonly="readonly"></textarea>
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <input id="index" type="text" disabled="disabled" style="display: none;">
    <div class="form-item" style="margin-top: 5px; margin-bottom: 8px;">
        <span style="vertical-align: middle">数量</span>
        <div class="number-tool" style="vertical-align: middle">
            <button onclick="reduce()">-</button>
            <input id="outQtyInput" type="number" onchange="fix(this)">
            <button onclick="add()">+</button>
        </div>
    </div>
    <button id="remove" onclick="remove()">移除</button>
    <button id="confirm" onclick="confirm()">保存</button>
</div>
 
</body>
<script>
    var countLayer;
    // 当前点击物料的最大剩余数量
    var maxRemainQty;
    // 表格数据
    var materialReceiveList = [];
    window.onload = function () {
        document.getElementById("soCode").focus();
    }
 
    /**
     * 提示信息
     * @param msg 提示内容
     * @param warn true:红色字体
     */
    function tips(msg, warn) {
        layer.msg(msg, {icon: warn ? 2 : 1})
    }
 
    function back() {
        parent.backIndex();
    }
 
    var tableIns;
    layui.use(['table', 'form', 'laydate'], function () {
        var table = layui.table;
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var laydate = layui.laydate;
        
        // 初始化日期选择器(支持选择日期和手动输入)
        laydate.render({
            elem: '#fplancommitdate',
            type: 'date',
            format: 'yyyy-MM-dd',
            done: function(value, date, endDate) {
                // 日期选择后触发查询
                findCode(null, 'fplancommitdate');
            }
        });
 
        tableIns = table.render({
            id: 'materialReceiveOut',
            elem: '#materialReceiveOut',
            data: [],
            limit: 500,
            cellMinWidth: 50,
            cols: [[
                {field: 'soCode', align: 'center', title: '生成单号', event: 'detail', width: 100},
                {field: 'fbillno', align: 'center', title: '任务单号', event: 'detail', width: 100},
                {field: 'invCode', align: 'center', title: '物料编码', event: 'detail', width: 80},
                {field: 'whName', align: 'center', title: '库区', event: 'detail', width: 50},
                {
                    field: 'remainQty',
                    align: 'center',
                    title: '数量',
                    event: 'modify',
                    style: 'cursor: pointer;color: blue',
                    width: 50,
                    templet: function(d) {
                        // 显示出库数量(如果已修改)或剩余数量
                        // 优先显示修改后的出库数量,如果没有修改则显示剩余数量
                        if (d.outQtyOut !== undefined && d.outQtyOut !== null) {
                            return d.outQtyOut;
                        }
                        return d.remainQty || 0;
                    }
                },
                // {
                //     field: 'fplancommitdate',
                //     align: 'center',
                //     title: '开工日期',
                //     event: 'detail',
                //     width: 100,
                //     templet: function(d) {
                //         if (d.fplancommitdate) {
                //             // 格式化日期显示(如果后端返回的是完整日期时间,只显示日期部分)
                //             var dateStr = d.fplancommitdate;
                //             if (dateStr.indexOf(' ') > 0) {
                //                 return dateStr.split(' ')[0];
                //             }
                //             return dateStr;
                //         }
                //         return '';
                //     }
                // },
                {type: 'checkbox', fixed: 'right', width: 30},
            ]],
            done: function (res, curr, count) {
            }
        });
 
        // 监听行工具事件
        table.on('tool(materialReceiveOut)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'modify':
                    countLayer = layer.open({
                        type: 1,
                        offset: '20px',
                        title: '修改数量',
                        shadeClose: true,
                        area: ['80%', '240px'],
                        content: $("#modify"),
                        success: function (layero, index) {
                            $('#invCode2').val(data.invCode);
                            $('#invName2').val(data.invName);
                            $('#index').val(data.id);
                            // 显示当前出库数量(如果已修改)或剩余数量
                            var currentOutQty = (data.outQtyOut !== undefined && data.outQtyOut !== null) ? data.outQtyOut : (data.remainQty || 0);
                            $('#outQtyInput').val(currentOutQty);
                            maxRemainQty = parseFloat(data.remainQty) || 0;
                        }
                    });
                    break;
            }
        });
    });
 
    var soCodeBar;
    var fbillnoBar;
    var invCodeBar;
    var fplancommitdateBar;
 
    /* 扫码、输入生产单号、任务单号、物料编码和开工日期 */
    function findCode(el, type) {
        soCodeBar = $('#soCode').val();
        fbillnoBar = $("#fbillno").val();
        invCodeBar = $("#invCode").val();
        fplancommitdateBar = $("#fplancommitdate").val();
        
        // 去除空格
        if (soCodeBar) {
            soCodeBar = soCodeBar.trim();
        }
        if (fbillnoBar) {
            fbillnoBar = fbillnoBar.trim();
        }
        if (invCodeBar) {
            invCodeBar = invCodeBar.trim();
        }
        if (fplancommitdateBar) {
            fplancommitdateBar = fplancommitdateBar.trim();
        }
        
        switch (type) {
            case 'soCode':
                break;
            case 'fbillno':
                break;
            case 'invCode':
                break;
            case 'fplancommitdate':
                break;
            default:
                break;
        }
 
        // 如果生产单号和任务单号都为空,不执行查询
        if (!soCodeBar && !fbillnoBar) {
            // 清空表格数据
            tableIns.reload({
                data: [],
            });
            materialReceiveList = [];
            return;
        }
 
        // 构建查询参数
        var params = {};
        if (soCodeBar) {
            params.soCode = soCodeBar;
        }
        if (fbillnoBar) {
            params.fbillno = fbillnoBar;
        }
        if (invCodeBar) {
            params.invCode = invCodeBar;
        }
        if (fplancommitdateBar) {
            params.fplancommitdate = fplancommitdateBar;
        }
 
        // PDA出库查询,添加pdaQuery参数,后端会过滤掉已全部出库的物料
        params.pdaQuery = true;
        
        $.ajax({
            url: baseUrl + "/materialReceive/list/auth",
            headers: {'token': localStorage.getItem('token')},
            data: params,
            method: 'GET',
            async: false,
            success: function (res) {
                if (res && res.code === 200) {
                    var records = [];
                    if (res.data && res.data.records && res.data.records.length > 0) {
                        records = res.data.records;
                    } else if (res.data && Array.isArray(res.data)) {
                        records = res.data;
                    }
                    // 初始化出库数量字段(初始等于剩余数量,用户可以修改)
                    records.map(function (item) {
                        // 如果还没有设置出库数量,则初始化为剩余数量
                        if (item.outQtyOut === undefined || item.outQtyOut === null) {
                            item.outQtyOut = item.remainQty || 0;
                        }
                    });
                    tableIns.reload({
                        data: records,
                    });
                    materialReceiveList = records;
                } else if (res && res.code === 403) {
                    top.location.href = baseUrl + "/pda";
                } else if (res && res.msg) {
                    tips(res.msg, true)
                } else {
                    tips("查询失败,请稍后重试", true);
                }
            },
            error: function(xhr, status, error) {
                console.error("查询失败:", status, error);
                tips("查询失败,请稍后重试", true);
            }
        });
    }
 
    /* 修改数量 */
    var countDom = $('#outQtyInput');
 
    function add() {
        if (Number(countDom.val()) >= maxRemainQty) {
            return;
        }
        countDom.val(Number(countDom.val()) + 1);
    }
 
    function reduce() {
        if (Number(countDom.val()) <= 0) {
            return;
        }
        countDom.val(Number(countDom.val()) - 1);
    }
 
    function fix(e) {
        var val = Number(e.value) || 0;
        if (val > maxRemainQty) {
            countDom.val(maxRemainQty);
        }
    }
 
    function remove() {
        var invCode = $('#invCode2').val();
        var id = $('#index').val();
        for (var j = 0; j < materialReceiveList.length; j++) {
            if (invCode === materialReceiveList[j].invCode && id == materialReceiveList[j].id) {
                materialReceiveList.splice(j, 1);
            }
        }
        tableIns.reload({data: materialReceiveList});
        layer.close(countLayer);
        tips("移除成功");
    }
 
    // 修改数量
    function confirm() {
        var invCode = $('#invCode2').val();
        var outQty = parseFloat($('#outQtyInput').val()) || 0;
        var id = $('#index').val();
        
        if (outQty < 0) {
            tips("数量不能小于0", true);
            return;
        }
        
        if (outQty > maxRemainQty) {
            tips("数量不能超过剩余数量", true);
            return;
        }
        
        // 更新表格中的出库数量
        for (var j = 0; j < materialReceiveList.length; j++) {
            if (invCode === materialReceiveList[j].invCode && id == materialReceiveList[j].id) {
                materialReceiveList[j].outQtyOut = outQty;
            }
        }
        tableIns.reload({data: materialReceiveList});
        layer.close(countLayer);
        tips("修改成功");
    }
 
    /* 生成领料出库 */
    function materialReceiveOut() {
        var table = layui.table;
        var checkStatus = table.checkStatus('materialReceiveOut');
        var data = checkStatus.data;
        if (data.length == 0) {
            layer.msg("请选择物料!");
            return;
        }
        
        // 批量出库
        var outQtyList = [];
        for (var i = 0; i < data.length; i++) {
            var item = data[i];
            // 使用修改后的出库数量(outQtyOut),如果没有修改则使用剩余数量
            var outQty = 0;
            if (item.outQtyOut !== undefined && item.outQtyOut !== null) {
                outQty = parseFloat(item.outQtyOut) || 0;
            } else {
                outQty = parseFloat(item.remainQty) || 0;
            }
            
            if (outQty <= 0) {
                tips("物料 " + item.invCode + " 出库数量必须大于0", true);
                return;
            }
            var remainQty = parseFloat(item.remainQty) || 0;
            if (outQty > remainQty) {
                tips("物料 " + item.invCode + " 出库数量不能超过剩余数量", true);
                return;
            }
            outQtyList.push({
                id: item.id,
                outQty: outQty
            });
        }
        
        // 确认出库
        layer.confirm('确定要出库选中的物料吗?', {icon: 3, title: '提示'}, function(index){
            // 批量更新出库数量
            var successCount = 0;
            var failCount = 0;
            for (var i = 0; i < outQtyList.length; i++) {
                $.ajax({
                    url: baseUrl + "/materialReceive/updateOutQty/auth",
                    headers: {'token': localStorage.getItem('token')},
                    data: JSON.stringify(outQtyList[i]),
                    contentType: 'application/json;charset=UTF-8',
                    method: 'POST',
                    async: false,
                    success: function (res) {
                        if (res && res.code === 200) {
                            successCount++;
                        } else {
                            failCount++;
                        }
                    },
                    error: function(xhr, status, error) {
                        console.error("出库失败:", status, error);
                        failCount++;
                    }
                });
            }
            
            if (successCount > 0) {
                tips("成功出库 " + successCount + " 条记录");
                // 清空输入框并刷新列表
                $("#soCode").val("");
                $("#fbillno").val("");
                $("#invCode").val("");
                $("#fplancommitdate").val("");
                materialReceiveList = [];
                tableIns.reload({data: materialReceiveList});
                document.getElementById("soCode").focus();
            }
            if (failCount > 0) {
                tips("出库失败 " + failCount + " 条记录", true);
            }
            
            layer.close(index);
        });
    }
 
    function reset() {
        $("#soCode").val(null);
        $("#fbillno").val(null);
        $("#invCode").val(null);
        $("#fplancommitdate").val(null);
        materialReceiveList = [];
        tableIns.reload({data: materialReceiveList});
        layer.closeAll();
        document.getElementById("soCode").focus();
    }
</script>
</html>