雅琪诺MES智能条码管理系统
zgq
2021-01-12 14fbc3da54fb7254940d65c574f16ed76dc9afa8
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using gregn6Lib;
using Pub_Class;
using System.Threading;
using System.IO.Ports;
 
namespace SCM
{
    public partial class Sc_ICMOReportToBarCode : Form
    {
        public Sc_ICMOReportToBarCode()
        {
            InitializeComponent();
        }
 
        public frmBillQueryCondition_Base frmCondition;
        public string ModCaption = "成品分切在线检验";
        public string ModName = "Sc_ICMOReportToBarCode";
        public string HSourceBillTypeID = "";   //单据类型
        public string HSourceBillType = "";     //单据类型名称
        public string HSourceBillNo = "";
        public Int64 HSourceInterID = 0;
        public Int64 HSourceEntryID = 0;
        public int HTMCol = 0;
        public int HMaterIDCol = 1;
        Pub_Class.ClsGridListSum oSumGrid = new Pub_Class.ClsGridListSum();
        public Int32 iTopRow = 0;//画横线
        SCM.WMSWeb.WebService1 oWeb = new SCM.WMSWeb.WebService1();
        //
 
        #region  固定代码
 
        //清空界面
        public void Sub_ClearBill()
        {
            //清空界面控件 for 控件   
            foreach (Control ct in gbUp.Controls)
            {
                switch (ct.GetType().Name)
                {
                    case "ComboBox":
                        ((ComboBox)ct).SelectedIndex = 0;
                        break;
                    case "TextBox":
                        ((TextBox)ct).Text = "";
                        break;
                    case "DateTimePicker":
                        ((DateTimePicker)ct).Value = DateTime.Today;
                        break;
                    default:
                        break;
                }
            }
            //
            txtHMaker.Text = ClsPub.CurUserName;
            initGrid();
        }
 
        private void Sc_ICMOReportToBarCode_Load(object sender, EventArgs e)
        {
            //设置动态URL
            oWeb.Url = SCM.ClsPub1.WEBSURL;
            //
            frmCondition = new frmBillQueryCondition_Base();
            this.Text = ModCaption; 
            oSumGrid.ogrdMain = grdSub;  //初始化 new
            oSumGrid.oGridsum = grdSum;
            initGrid();
            //cmbHBillType.SelectedIndex = 0;
            //cmbHSourceBillType.SelectedIndex = 0;
            cmbFQMB.Items.Add(this.Text);
            cmbFQMB.SelectedIndex = 0;
 
            if(DBUtility.ClsPub.CurUserName.ToLower()=="admin")
            {
                txtHDQQty.ReadOnly = false;
                txtHKSQty.ReadOnly = false;
            }
        }
 
        //初始化网格
        private void initGrid()
        {
            ClsPub1.initGridList(grdMain, this.Name);
            ClsPub1.initGridList(grdSub, this.Name + "grdSub");
            ClsPub1.initGridFst(grdEmp, this.Name + "grdEmp");
            grdMain.RowTemplate.Height = 30;
            grdMain.RowTemplate.MinimumHeight = 30;
            grdMain.RowsDefaultCellStyle.Font = new Font("宋体", 15);
            grdMain.ReadOnly = true;
 
            grdSub.RowTemplate.Height = 30;
            grdSub.RowTemplate.MinimumHeight = 30;
            grdSub.RowsDefaultCellStyle.Font = new Font("宋体", 15);
            grdSub.ReadOnly = true;
 
            grdEmp.RowTemplate.Height = 30;
            grdEmp.RowTemplate.MinimumHeight = 30;
            grdEmp.RowsDefaultCellStyle.Font = new Font("宋体", 15);
            grdEmp.ReadOnly = false;
            //DBUtility.Xt_BaseBillFun.initGridList(grdMain, this.Name);
            //DBUtility.Xt_BaseBillFun.initGridList(grdSub, this.Name + "grdSub");
            DBUtility.Xt_BaseBillFun.GetGrid(grdMain, this.Name);
            DBUtility.Xt_BaseBillFun.GetGrid(grdSub, this.Name + "grdSub");
            DBUtility.Xt_BaseBillFun.GetGrid(grdEmp, this.Name + "grdEmp");
        }
 
        //时间控件
        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled=false;
            Sub_ClearBill();
        }
 
        private void timer2_Tick(object sender, EventArgs e)
        {
            timer2.Enabled = false;
            //
            if (ClsIni.ReadIni("COMINFO", "txtPortName", DBUtility.ClsPub.AppPath + @"/HXERP.ini").Contains("没有找到")==false)
            {
                txtPortName.Text = ClsIni.ReadIni("COMINFO", "txtPortName", DBUtility.ClsPub.AppPath + @"/HXERP.ini");
                txtBaudRate.Text = ClsIni.ReadIni("COMINFO", "txtBaudRate", DBUtility.ClsPub.AppPath + @"/HXERP.ini");
                cmbParity.Text = ClsIni.ReadIni("COMINFO", "cmbParity", DBUtility.ClsPub.AppPath + @"/HXERP.ini");
                txtDataBits.Text = ClsIni.ReadIni("COMINFO", "txtDataBits", DBUtility.ClsPub.AppPath + @"/HXERP.ini");
                cmbStopBits.Text = ClsIni.ReadIni("COMINFO", "cmbStopBits", DBUtility.ClsPub.AppPath + @"/HXERP.ini");
                txtHSend0.Text = ClsIni.ReadIni("COMINFO", "txtHSend0", DBUtility.ClsPub.AppPath + @"/HXERP.ini");
                txtHSend.Text = ClsIni.ReadIni("COMINFO", "txtHSend", DBUtility.ClsPub.AppPath + @"/HXERP.ini");
                txtHSourceID.Text = ClsIni.ReadIni("COMINFO", "txtHSourceID", DBUtility.ClsPub.AppPath + @"/HXERP.ini");
                txtHSourceID.Tag = ClsIni.ReadIni("COMINFO", "txtHSourceIDTag", DBUtility.ClsPub.AppPath + @"/HXERP.ini");
                this.Text = this.Text + "-" + txtHSourceID.Text;
            }
 
            //初始化串口
            ComDevice = new SerialPort();
            ComDevice.DataReceived += new SerialDataReceivedEventHandler(Com_DataReceived);//绑定事件
            string[] PortNames = SerialPort.GetPortNames();
            //打开串口
            OpenCom();
        }
 
        //退出
        private void tc_Click(object sender, EventArgs e)
        {
            this.Close();
        }
 
        private void grdMain_Paint(object sender, PaintEventArgs e)
        {
            GraphicsGrid();
        }
 
        private void GraphicsGrid()
        {
            //画底线
            DBUtility.Xt_BaseBillFun.GraphicsGrid(grdMain);
            //画横线
            DBUtility.Xt_BaseBillFun.GraphicsRowGrid(grdMain, iTopRow, iTopRow + 50, Fun_GetCol("hmainid"));
        }
 
        private Int32 Fun_GetCol(string sCol)
        {
            return DBUtility.Xt_BaseBillFun.Fun_GetCol(sCol, grdMain);
        }
 
        //保存列宽
        private void bclk_Click(object sender, EventArgs e)
        {
            DBUtility.Xt_BaseBillFun.SaveGrid(grdMain, this.Name);
            DBUtility.Xt_BaseBillFun.SaveGrid(grdSub, this.Name + "grdSub");
            DBUtility.Xt_BaseBillFun.SaveGrid(grdEmp, this.Name + "grdEmp");
        }
 
        //默认列宽
        private void mrlk_Click(object sender, EventArgs e)
        {
            DBUtility.Xt_BaseBillFun.DefaultGridView(grdMain, this.Name);
            DBUtility.Xt_BaseBillFun.DefaultGridView(grdSub, this.Name + "grdSub");
            DBUtility.Xt_BaseBillFun.DefaultGridView(grdEmp, this.Name + "grdEmp");
        }
 
        #endregion
 
 
        #region 返回源单信息
 
        private void xd_Click(object sender, EventArgs e)
        {
 
        }
 
 
        private void Display(string SourceBillType, Int64 HSourceBillInterID, Int64 HSourceBillEntryID)
        {
            DataSet Ds;
            //生产订单
            if (SourceBillType == "生产订单")
            {
                //得到信息
                Ds = oWeb.getDataSetBySQL("select * from h_v_IFCLD_ICMOList where hmainid=" + HSourceBillInterID + " and hsubid=" + HSourceBillEntryID, "h_v_IFCLD_ICMOList", ref DBUtility.ClsPub.sExeReturnInfo);
                //写入信息
                Sub_WriteInForm(Ds.Tables[0], 0);
            }
            //收料通知单
            if (SourceBillType == "收料通知单")
            {
                //得到信息
                Ds = oWeb.getDataSetBySQL("select * from h_v_IF_POInStockList where hmainid=" + HSourceBillInterID + " and hsubid=" + HSourceBillEntryID, "h_v_IF_POInStockList", ref DBUtility.ClsPub.sExeReturnInfo);
                //写入信息
                Sub_WriteInForm(Ds.Tables[0], 0);
            }
            //生产汇报单
            if (SourceBillType == "生产汇报单")
            {
                //得到信息
                Ds = oWeb.getDataSetBySQL("select * from h_v_IF_ICMOReportBillList where hmainid=" + HSourceBillInterID + " and hsubid=" + HSourceBillEntryID, "h_v_IF_POInStockList", ref DBUtility.ClsPub.sExeReturnInfo);
                //写入信息
                Sub_WriteInForm(Ds.Tables[0], 0);
            }
        }
 
        //根据TABLE写入界面
        private void Sub_WriteInForm(DataTable oTable, int i)
        {
            try
            {
                txtHSourceBillNo.Text = oTable.Rows[0]["单据号"].ToString();
                txtHMaterNumber.Text = oTable.Rows[0]["物料代码"].ToString();
                txtHOldMaterNumber.Text = oTable.Rows[0]["旧物料编码"].ToString();
                txtHMaterNumber.Tag = oTable.Rows[0]["HMaterID"].ToString();
                txtHMaterName.Text = oTable.Rows[0]["物料名称"].ToString();
                txtHMaterName.Tag = oTable.Rows[0]["HMaterID"].ToString();
                txtHMaterModel.Text = oTable.Rows[0]["规格型号"].ToString();
                txtHUnitName.Text = oTable.Rows[0]["计量单位"].ToString();
                txtHUnitName.Tag = oTable.Rows[0]["HUnitID"].ToString();
                txtHBatchNo.Text = oTable.Rows[0]["批次"].ToString();
                txtHSumQty.Text = oTable.Rows[0]["数量"].ToString();
                txtHRelationQty.Text = oTable.Rows[0]["已生成条码数量"].ToString();
                txtHSYQty.Text = oTable.Rows[0]["未生成条码数量"].ToString();
                txtHKSQty.Text = lblHNowQty.Text;
                txtHDQQty.Text = lblHNowQty.Text;
                if (DBUtility.ClsPub.isDoule(txtHDQQty.Text) - DBUtility.ClsPub.isDoule(txtHKSQty.Text)>0)
                {
                    txtHSJQty.Text = DBUtility.ClsPub.isDoule(DBUtility.ClsPub.isDoule(txtHDQQty.Text) - DBUtility.ClsPub.isDoule(txtHKSQty.Text), 2);
                }
                else
                {
                    txtHSJQty.Text = "0";
                }
                DisplayMain();
                //DisplaySub();
            }
            catch(Exception e)
            {
                MessageBox.Show("读取失败!" + e.Message);
            }
        }
 
 
        private void DisplayMain()
        {
            DataSet DSet;
            string sSql = "";
            string sWhere = "";
            //过滤条件 
            sSql = " exec h_p_Sc_ICMOReportToBarCode_All   '" + HSourceBillTypeID + "'," + HSourceInterID.ToString() + "," + HSourceEntryID.ToString(); 
            //
            DSet = oWeb.getDataSetBySQL(sSql, "h_p_Sc_ICMOReportToBarCode_All", ref DBUtility.ClsPub.sExeReturnInfo);
            //生成首行标题
            if (DSet == null)
            {
                MessageBox.Show("没有返回任何结果,尝试再次查询!" + DBUtility.ClsPub.sExeReturnInfo);
                return;
            }
            //
            if (DSet.Tables[0].Rows.Count>0)
            {
                grdMain.DataSource = DSet.Tables[0].DefaultView;
            }
            //冻结
            int FrCol = DBUtility.ClsPub.isInt(0);
            string s = "是";
            ClsPub1.DisplayGrid(grdMain, this.Name, s, FrCol);
            //画线 
            //Total();
            ///////////////////////////////////////////
            if (DSet.Tables[1].Rows.Count > 0)
            {
                grdSub.DataSource = DSet.Tables[1].DefaultView;
            }
            //冻结 
            ClsPub1.DisplayGrid(grdSub, this.Name + "grdSub", s, FrCol);
            /////////////////////////////
            txtHRelationQty.Text = DBUtility.ClsPub.isDoule(DSet.Tables[2].Rows[0]["关联数量"], 2);
            if(DBUtility.ClsPub.isDoule(txtHSumQty.Text) - DBUtility.ClsPub.isDoule(txtHRelationQty.Text)>=0)
            {
                txtHSYQty.Text = DBUtility.ClsPub.isDoule(DBUtility.ClsPub.isDoule(txtHSumQty.Text) - DBUtility.ClsPub.isDoule(txtHRelationQty.Text), 2);
            }
            else
            {
                txtHSYQty.Text = "0";
            }
            //////////////////////////
        }
 
        private void DisplaySub()
        {
            DataSet DSet;
            string sSql = "";
            string sWhere = "";
            //过滤条件 
            sSql = " exec h_p_Sc_ICMOReportToBarCode_Sub   '" + HSourceBillTypeID + "'," + HSourceInterID.ToString() + "," + HSourceEntryID.ToString();
            
            //
            DSet = oWeb.getDataSetBySQL(sSql, "h_p_Sc_ICMOReportToBarCode_Sub", ref DBUtility.ClsPub.sExeReturnInfo);
            //生成首行标题
            if (DSet == null)
            {
                MessageBox.Show("没有返回任何结果,尝试再次查询!" + DBUtility.ClsPub.sExeReturnInfo);
                return;
            }
            //
            grdSub.DataSource = DSet.Tables[0].DefaultView;
 
 
            //冻结
            int FrCol = DBUtility.ClsPub.isInt(0);
            string s = "是";
            ClsPub1.DisplayGrid(grdSub, this.Name + "grdSub", s, FrCol);
            //画线 
            //Total();
        }
 
 
        #endregion
 
 
        #region  //打印设置
 
        GridppReport Report;
        string sBarCode = "";
 
        //预览
        int CurRows = 0;
        
 
        
 
 
        
 
        #endregion
 
 
        #region 界面控件处理
 
        private void cmdHDeptID_Click(object sender, EventArgs e)
        {
            SCM.ClsIF_Department_View oDept = new SCM.ClsIF_Department_View();
            string sWhere = "";
            if (oDept.RefreshView(sWhere))
            {
                this.txtHDeptID.Text = oDept.oModel.HName;
                this.txtHDeptID.Tag = oDept.oModel.HItemID.ToString(); 
            }
            else
            {
                this.txtHDeptID.Text = ""; 
            }
        }
 
        private void txtHDeptID_TextChanged(object sender, EventArgs e)
        {
            BLL.ClsPub_BLL.Sub_ClearText(txtHDeptID);
        }
 
        #endregion
 
 
        //选择源单
        private void cmdHSourceBillNo_Click(object sender, EventArgs e)
        {
            Gy_SourceBillList_Touch oGy_SourceBillList_Touch = new Gy_SourceBillList_Touch();
            oGy_SourceBillList_Touch.HSourceBillType = HSourceBillType;//类型名称
            oGy_SourceBillList_Touch.ShowDialog();
            if (oGy_SourceBillList_Touch.OKTag == 1)
            {
                //根据选中的源单内码和子内码,返回相应信息
                HSourceEntryID = oGy_SourceBillList_Touch.HSelEntryID;
                HSourceInterID = oGy_SourceBillList_Touch.HSelInterID;
                HSourceBillTypeID = oGy_SourceBillList_Touch.HSourceBillTypeID;
                HSourceBillNo = oGy_SourceBillList_Touch.HSourceBillNo;
                Display(HSourceBillType, oGy_SourceBillList_Touch.HSelInterID, oGy_SourceBillList_Touch.HSelEntryID);
            }
            else
            {
                HSourceEntryID = 0;
                HSourceInterID = 0;
                HSourceBillTypeID = "";
                HSourceBillNo = "";
            }
        }
 
        //
        private void cmdCancel_Click(object sender, EventArgs e)
        {
            CloseCom();
            this.Close();
        }
 
        private void label19_Click(object sender, EventArgs e)
        {
 
        }
 
        public bool SaveBill(string sType)
        {
            try
            {
                int LSHlen = 2;             //流水号长度
                int SumLen = 10;            //总长度
                string TM = "";             //条码
                string HNumber = "";        //物料代码
                double HSumQty = 0;         //产品数量
                double HMinQty = 0;         //最小包装数
                int HBQty = 0;              //箱数
                double HQty = 0;            //数量
                string WeiShu = "";         //尾数
                int LSH = 0;                //流水号
                string LSH2 = "";           //流水号转换成字符
                string sDate = "";          //日期
                string sYear = "";          //年
                string sPeriod = "";        //月
                string sDay = "";           //日
                string HBatchNo = "";       //批次
                int k = 0;
                int n = 0;                  //同一批生成条码中的第几条 
                string sTMNumber = "";      //条码自定义前缀
                DataSet Ds;
                //
                string HWei = "";      //尾数
                HBarCode = "";
                string HBarCodeType = "";
                Int64 HMaterID = 0;
                Int64 HAuxPropID = 0;
                Int64 HUnitID = 0;
                double HQty2 = 0;
                string HBatchNo2 = "";
                Int64 HSupID = 0;
                Int64 HGroupID = 0;
                int HPrintQty = 0;
                Int64 HBarcodeNo = 0;       //托号
                Int64 HBarcodeQtys = 0;     //总托数
                Int64 HDeptID = 0;
                Int64 HWhID = 0;
                Int64 HSPID = 0;
                string HRemark = "";
                string HMaterName = "";
                string HMaterModel = "";
                string HPinfan = "";
                string HMTONo = "";
                Int64 HCusID = 0;
                string HCusType = "";
                DateTime HEndDate;
                string HWorkLineName = "";
                DateTime HBeginDate;
                string HSeOrderBillNo = "";
                string HJiaYe2 = "";
                string HPressModel = "";
                string HCusModel = "";
                string HMaterialModel = "";
                string HColor = "";
                string HLogo = "";
                string HPackageSize = "";
                double HMaterialJQty = 0;
                double HMaterialMQty = 0;
                string HCustomBatchNo = "";
                string HGBBarCode = "";
                string POOrderBillNo = "";
                //
                cmdHSaveBill.Enabled = false;
                //判断是否存在 源单信息;
                if (HSourceInterID <= 0 || HSourceEntryID <= 0 || DBUtility.ClsPub.isLong(txtHMaterName.Tag) <= 0 || DBUtility.ClsPub.isLong(txtHUnitName.Tag) <= 0)//判断是否有源单
                {
                    MessageBox.Show("没有选单!");
                    cmdHSaveBill.Enabled = true;
                    return false;
                }
                if (DBUtility.ClsPub.isDoule(txtHDQQty.Text) - DBUtility.ClsPub.isDoule(txtHKSQty.Text) >= 0)
                {
                    txtHSJQty.Text = DBUtility.ClsPub.isDoule(DBUtility.ClsPub.isDoule(txtHDQQty.Text) - DBUtility.ClsPub.isDoule(txtHKSQty.Text), 2); //实际米数= 当前米数-开始米数
                    lblHRelQty.Text = txtHSJQty.Text;
                }
                else
                {
                    if (MessageBox.Show("当前米数小于开始米数,是否确定要清零开始米数", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        txtHKSQty.Text = "0";
                        txtHSJQty.Text = DBUtility.ClsPub.isDoule(DBUtility.ClsPub.isDoule(txtHDQQty.Text), 2); //实际米数= 当前米数-开始米数
                        lblHRelQty.Text = txtHSJQty.Text;
                    }
                }
                if (DBUtility.ClsPub.isDoule(txtHSJQty.Text) <= 0)
                {
                    MessageBox.Show("当前数量不能小于等于0!");
                    cmdHSaveBill.Enabled = true;
                    return false;
                }
                //if (DBUtility.ClsPub.isDoule(txtHSYQty.Text) <= 0)
                //{
                //    MessageBox.Show("剩余数量已经为0,不能在裁切!");
                //    cmdHSaveBill.Enabled = true;
                //    return false;
                //}
                if (DBUtility.ClsPub.isLong(txtHEmpID.Tag) == 0 || DBUtility.ClsPub.isLong(txtHEmpID2.Tag) == 0 ) 
                {
                    MessageBox.Show("职员1或者职员2没有选择!");
                    cmdHSaveBill.Enabled = true;
                    return false;
                }
                if (DBUtility.ClsPub.isLong(txtHGroupID.Tag) == 0 )
                {
                    MessageBox.Show("班组没有选择!");
                    cmdHSaveBill.Enabled = true;
                    return false;
                }
                
 
                //根据源单 生成条形码 并保存如数据库
                HNumber = DBUtility.ClsPub.isStrNull(txtHMaterName.Tag);
                HBatchNo = DBUtility.ClsPub.isStrNull(txtHBatchNo.Text);
                sDate = dtpHDate.Value.ToShortDateString();
                sYear = ClsPub.isDate(sDate).Year.ToString().Substring(2, 2);
                sPeriod = "0" + ClsPub.isDate(sDate).Month.ToString();
                sPeriod = sPeriod.Substring(sPeriod.Length - 2, 2);
                sDay = "0" + ClsPub.isDate(sDate).Day.ToString();
                sDay = sDay.Substring(sDay.Length - 2, 2);
 
                //条码前缀 = 组织代码 + 物料内码 + 年 + 月 + 日
                //sTMNumber = HOrgNumber + HNumber + sYear + sPeriod + sDay;
                //sTMNumber = DBUtility.ClsPub.isStrNull(txtHBatchNo.Text);
                //获取最大流水号,降等品加CP后缀以重新生成流水号
                if (sType == "SaveDown")
                {
                    sTMNumber = HNumber + ";" + DBUtility.ClsPub.isStrNull(txtHBatchNo.Text) + "CP";
                }
                else if (sType == "Sample")
                {
                    sTMNumber = HNumber + ";" + DBUtility.ClsPub.isStrNull(txtHBatchNo.Text) + "B";
                }
                else
                {
                    sTMNumber = HNumber + ";" + DBUtility.ClsPub.isStrNull(txtHBatchNo.Text);
                }
                //sTMNumber = HNumber + ";" + DBUtility.ClsPub.isStrNull(txtHBatchNo.Text);
 
                Ds = oWeb.getDataSetBySQL("exec h_p_WMS_GetMaxNo  '" + sTMNumber + "'", "h_p_WMS_GetMaxNo", ref DBUtility.ClsPub.sExeReturnInfo);//获取最大流水号
                LSH = ClsPub.isInt(Ds.Tables[0].Rows[0][0]);
 
                //
                HQty = DBUtility.ClsPub.isDoule(txtHSJQty.Text);
                //
                string sSQLNoMul = " exec h_p_WMS_SetMaxNo '" + sTMNumber + "' ";
                oWeb.getRunProc(sSQLNoMul, ref DBUtility.ClsPub.sExeReturnInfo);
                //
                HWei = "";
                LSH = LSH + 1;
                LSH2 = LSH.ToString();
                while (LSH2.Length < LSHlen)  //如果流水号小于6位数前面补0
                {
                    LSH2 = "0" + LSH2;
                }
 
                //条码编号 = 条码前缀 + 流水号
                //降等品时增加CP后缀
                string sTMNumberNew = HNumber + ";" + DBUtility.ClsPub.isStrNull(txtHBatchNo.Text);
                if (sType == "SaveDown")
                {
                    TM = sTMNumberNew + "CP-" + LSH2;
                }
                else if (sType == "Sample")
                {
                    TM = sTMNumberNew + "B-" + LSH2;
                }
                else
                {
                    TM = sTMNumberNew + "-" + LSH2;
                }
                //TM = sTMNumber + "-" + LSH2;
 
                HBarCode = TM;
                HBarCodeType = "唯一条码";
 
                HMaterID = DBUtility.ClsPub.isLong(txtHMaterName.Tag);
                HAuxPropID = 0;
                HUnitID = DBUtility.ClsPub.isLong(txtHUnitName.Tag);
                HQty2 = HQty;
                //HBatchNo2 = HBatchNo;
                //if (HSourceBillType != "生产订单")
                //{
                //HBatchNo2 = TM;
                //HBatchNo2 = HBatchNo + "-" + LSH2;
                //判断按钮类型给批次增加后缀
                if (sType == "SaveDown")
                {
                    HBatchNo2 = HBatchNo + "CP-" + LSH2;
                }
                else if (sType == "Sample")
                {
                    HBatchNo2 = HBatchNo + "B-" + LSH2;
                }
                else
                {
                    HBatchNo2 = HBatchNo + "-" + LSH2;
                }
                //}
                //HSourceBillType = HSourceBillTypeID;
                HBarcodeQtys = 0;
                HBarcodeNo = 1;
                HSupID = 0;
                HDeptID = 0;
                HWhID = 0;
                HSPID = 0;
                if (sType == "SaveDown")
                {
                    HRemark = "二等品";
                }
                else if (sType == "Sample")
                {
                    HRemark = "样本纸";
                }
                else
                {
                    HRemark = "";
                }
                HMaterName = DBUtility.ClsPub.isStrNull(txtHMaterName.Text);
                HMaterModel = DBUtility.ClsPub.isStrNull(txtHMaterModel.Text);
                HPinfan = "";
                HMTONo = "";
                HCusID = 0;
                HCusType = "";
                HEndDate = dtpHDate.Value;
                HWorkLineName = DBUtility.ClsPub.isStrNull(txtHEmpID.Text) + ";" + DBUtility.ClsPub.isStrNull(txtHEmpID2.Text) + ";" + DBUtility.ClsPub.isStrNull(txtHEmpID3.Text);
                HBeginDate = dtpHDate.Value;
                HSeOrderBillNo = "";
                HGroupID = DBUtility.ClsPub.isLong(txtHGroupID.Tag);
                HJiaYe2 = DBUtility.ClsPub.isLong(txtHEmpID.Tag).ToString() + ";" + DBUtility.ClsPub.isLong(txtHEmpID2.Tag).ToString() + ";" + DBUtility.ClsPub.isLong(txtHEmpID3.Tag).ToString();
                HPressModel = "";
                HCusModel = "";
                HMaterialModel = "";
                HColor = "";
                HLogo = "";
                HPackageSize = "";
                HMaterialJQty = 0;
                HMaterialMQty = 0;
                HCustomBatchNo = "";
                HGBBarCode = "";
                POOrderBillNo = "";
                string sSQLMul = " insert into Gy_BarCodeBill (HBarCode,HBarCodeType,HMaterID,HUnitID,HQty" +
                            ",HBatchNo,HSupID,HGroupID,HMaker,HMakeDate,HPrintQty,HinitQty" +
                            ",HSourceInterID,HSourceEntryID,HSourceBillNo,HSourceBillType,HEndQty " +
                            ",HBarcodeQtys,HBarcodeNo,HDeptID,HWhID,HSPID,HRemark " +
                            ",HCusID,HCusType,HEndDate,HWorkLineName,HJiaYe " +
                            ",HPressModel,HCusModel,HMaterialModel,HColor,HBarCodeDate " +
                            ",HLogo,HPackageSize,HMaterialJQty,HMaterialMQty,HCustomBatchNo " +
                            ",HSTOCKORGID,HOWNERID,HBeginDate,HSeOrderBillNo,HGBBarCode " +
                            ",POOrderBillNo,HSourceID " +
                            ",HMaterName,HMaterModel,HPinfan,HAuxPropID,HMTONo " +
                            ") values ("
                            + "'" + HBarCode + "','" + HBarCodeType + "'," + HMaterID.ToString() + "," + HUnitID.ToString() + "," + HQty2.ToString()
                            + ",'" + HBatchNo2 + "'," + HSupID.ToString() + "," + HGroupID.ToString() + ",'" + ClsPub.CurUserName + "',getdate()," + HPrintQty.ToString() + "," + HQty2.ToString()
                            + ", " + HSourceInterID.ToString() + "," + HSourceEntryID.ToString() + ",'" + HSourceBillNo + "','" + HSourceBillTypeID + "','" + HWei + "'"
                            + ", " + HBarcodeQtys.ToString() + "," + HBarcodeNo.ToString() + "," + HDeptID.ToString() + "," + HWhID.ToString() + "," + HSPID.ToString() + ",'" + HRemark + "'"
                            + ", " + HCusID.ToString() + ",'" + HCusType + "','" + HEndDate.ToShortDateString() + "','" + HWorkLineName + "','" + HJiaYe2 + "'"
                            + ",'" + HPressModel + "','" + HCusModel + "','" + HMaterialModel + "','" + HColor + "','" + sDate + "'"
                            + ",'" + HLogo + "','" + HPackageSize + "'," + HMaterialJQty.ToString() + "," + HMaterialMQty.ToString() + ",'" + HCustomBatchNo + "'"
                            + ", " + HOrgID.ToString() + "," + HOrgID.ToString() + ",'" + HBeginDate.ToShortDateString() + "','" + HSeOrderBillNo + "','" + HGBBarCode + "'"
                            + ",'" + POOrderBillNo + "'" + "," + DBUtility.ClsPub.isLong(txtHSourceID.Tag) 
                            + ",'" + HMaterName + "','" + HMaterModel + "','" + HPinfan + "'," + HAuxPropID.ToString() + ",'" + HMTONo + "')";
                oWeb.getRunProc(sSQLMul, ref DBUtility.ClsPub.sExeReturnInfo);
 
                
                //刷新界面
                DisplayMain();
                //DisplaySub();  
                //根据界面选择打印模板,打印条形码
                Sub_SetReport(cmbFQMB.Text);
                if (DBUtility.ClsPub.CurUserName.ToLower() == "admin")
                {
                    Report.PrintPreview(false);
                }
                else
                {
                    Report.Print(false);
                }
                
                Thread.Sleep(500);
 
                if (HSourceBillType != "生产汇报单")
                {
                    txtHKSQty.Text = "0";
                }
 
                cmdHSaveBill.Enabled = true;
                return true;
            }
            catch (Exception e1)
            {
                MessageBox.Show("保存失败!" + e1.Message);
                cmdHSaveBill.Enabled = true;
                return false;
            }
        }
 
        public string HOrgNumber = "1001";
        public Int64 HOrgID = 1;
        public string HBarCode = "";
 
        //保存 并生成打印条码
        private void cmdHSaveBill_Click(object sender, EventArgs e)
        {
            SaveBill("");
        }
 
 
        private void Sub_SetReport(string sOpenTmp)
        {
            //判断行数
 
            Report = new GridppReport();
            Report.LoadFromFile(DBUtility.ClsPub.AppPath + @"\" + sOpenTmp + ".grf");  //here .
            Report.BeforePostRecord += new _IGridppReportEvents_BeforePostRecordEventHandler(ReportBeforePostRecord);
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecordByDataTable);
            //Report.PrintEnd += new _IGridppReportEvents_PrintEndEventHandler(ReportPrintEnd);
        }
 
 
        private void Sub_SetReportList(string sOpenTmp)
        {
            //判断行数
            for (int i = 0; i < grdMain.Rows.Count; i++)
            {
                grdMain.Rows[i].Cells[0].Value = "";
            }
            for (int i = 0; i < grdMain.SelectedRows.Count; i++)
            {
                grdMain.Rows[grdMain.SelectedRows[i].Index].Cells[0].Value = "*";
            }
            Report = new GridppReport();
            Report.LoadFromFile(DBUtility.ClsPub.AppPath + @"\" + sOpenTmp + ".grf");  //here .
            Report.BeforePostRecord += new _IGridppReportEvents_BeforePostRecordEventHandler(ReportBeforePostRecordList);
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecordByDataTableList);
            //Report.PrintEnd += new _IGridppReportEvents_PrintEndEventHandler(ReportPrintEnd);
        }
 
        //填入单据表头信息
        private void ReportBeforePostRecord()// 
        {
            try
            {
                Report.FieldByName("物料代码").AsString = ClsPub.isStrNull(txtHMaterNumber.Text);
                Report.FieldByName("物料名称").AsString = ClsPub.isStrNull(txtHMaterName.Text);
                Report.FieldByName("规格型号").AsString = ClsPub.isStrNull(txtHMaterModel.Text);
                Report.FieldByName("条码编号").AsString = HBarCode;
                Report.FieldByName("数量").AsString = ClsPub.isStrNull(txtHSJQty.Text);
                Report.FieldByName("班组").AsString = ClsPub.isStrNull(txtHGroupID.Text);
                Report.FieldByName("组员").AsString = ClsPub.isStrNull(txtHEmpID.Text) + ";" + ClsPub.isStrNull(txtHEmpID2.Text) + ";" + ClsPub.isStrNull(txtHEmpID3.Text);
                Report.FieldByName("数量").AsString = ClsPub.isStrNull(txtHSJQty.Text);
                Report.FieldByName("旧物料编码").AsString = ClsPub.isStrNull(txtHOldMaterNumber.Text);
                //Report.FieldByName("源单单号").AsString = ClsPub.isStrNull(grdSub.Rows[CurRows].Cells[HSourceBillNoCol].Value);
                //Report.FieldByName("销售订单号").AsString = ClsPub.isStrNull(grdSub.Rows[CurRows].Cells[HSeOrderBillNo2Col].Value);
                //Report.FieldByName("生产车间").AsString = ClsPub.isStrNull(grdSub.Rows[CurRows].Cells[HDeptName2Col].Value);
                //Report.FieldByName("备注").AsString = ClsPub.isStrNull(grdSub.Rows[CurRows].Cells[HRemark2Col].Value);
            }
            catch (Exception e)
            {
                MessageBox.Show("打印失败!表头:" + e.Message);
            }
        }
 
        private void ReportFetchRecordByDataTable()
        {
            try
            {
                DataTable ds = new DataTable();
                BLL.Utility.FillRecordToReport_FstRow(Report, grdMain, ds, 0);
            }
            catch (Exception e)
            {
                MessageBox.Show("打印失败!表体:" + e.Message);
            }
        }
 
        //填入单据表头信息
        private void ReportBeforePostRecordList()// 
        {
            try
            {
                //Report.FieldByName("物料代码").AsString = ClsPub.isStrNull(txtHMaterNumber.Text);
                //Report.FieldByName("物料名称").AsString = ClsPub.isStrNull(txtHMaterName.Text);
                //Report.FieldByName("规格型号").AsString = ClsPub.isStrNull(txtHMaterModel.Text);
                //Report.FieldByName("条码编号").AsString = HBarCode;
                //Report.FieldByName("数量").AsString = ClsPub.isStrNull(txtHSJQty.Text);
                //Report.FieldByName("源单单号").AsString = ClsPub.isStrNull(grdSub.Rows[CurRows].Cells[HSourceBillNoCol].Value);
                //Report.FieldByName("销售订单号").AsString = ClsPub.isStrNull(grdSub.Rows[CurRows].Cells[HSeOrderBillNo2Col].Value);
                //Report.FieldByName("生产车间").AsString = ClsPub.isStrNull(grdSub.Rows[CurRows].Cells[HDeptName2Col].Value);
                //Report.FieldByName("备注").AsString = ClsPub.isStrNull(grdSub.Rows[CurRows].Cells[HRemark2Col].Value);
            }
            catch (Exception e)
            {
                MessageBox.Show("打印失败!表头:" + e.Message);
            }
        }
 
        private void ReportFetchRecordByDataTableList()
        {
            try
            {
                DataTable ds = new DataTable();
                SCM.Utility.FillRecordToReport(Report, grdMain, ds, Fun_GetCol("选择"));
            }
            catch (Exception e)
            {
                MessageBox.Show("打印失败!表体:" + e.Message);
            }
        }
 
        private SerialPort ComDevice = null;
 
        public void OpenCom()
        {
            if (!ComDevice.IsOpen)
            {
                
                try
                {
                    ComDevice.PortName = txtPortName.Text;
                    ComDevice.BaudRate = int.Parse(txtBaudRate.Text);
                    switch (cmbParity.SelectedItem.ToString())
                    {
                        case "0":
                            ComDevice.Parity = Parity.None;
                            break;
                        case "1":
                            ComDevice.Parity = Parity.Odd;
                            break;
                        case "2":
                            ComDevice.Parity = Parity.Even;
                            break;
                        case "3":
                            ComDevice.Parity = Parity.Mark;
                            break;
                        case "4":
                            ComDevice.Parity = Parity.Space;
                            break;
                        default:
                            break;
                    }
                  
                    ComDevice.DataBits = int.Parse(txtDataBits.Text);
                    switch (cmbStopBits.SelectedItem.ToString())
                    {
                        case "0":
                            ComDevice.StopBits = StopBits.None;
                            break;
                        case "1":
                            ComDevice.StopBits = StopBits.One;
                            break;
                        case "2":
                            ComDevice.StopBits = StopBits.Two;
                            break;
                        case "1.5":
                            ComDevice.StopBits = StopBits.OnePointFive;
                            break;
                        default:
                            break;
                    }
                    
                    ComDevice.Open(); 
                    lblComStatus.Text = "串口状态:开启";
                    timer3.Enabled = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("打开串口失败!" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); 
                    return;
                }
            }
            else
            {
                //try
                //{
                //    ComDevice.Close();
                //    textBox3.Enabled = true;
                //    textBox4.Enabled = true;
                //    textBox5.Enabled = true;
                //    comboBox1.Enabled = true;
                //    comboBox2.Enabled = true;
                //    AddContent($"串口已关闭");
                //    button1.Enabled = false;
                //    btnPush.Text = "开启串口";
                //}
                //catch (Exception ex)
                //{
                //    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //    AddContent($"串口关闭失败:{ex.Message}");
                //    return;
                //}
            }
        }
 
        private void cmdOpenCom_Click(object sender, EventArgs e)
        {
            OpenCom();
        }
 
        private void cmdCloseCom_Click(object sender, EventArgs e)
        {
            CloseCom();
        }
 
        public void CloseCom()
        {
            if (!ComDevice.IsOpen)
            {
                 
            }
            else
            {
                try
                {
                    ComDevice.Close(); 
                    lblComStatus.Text = "串口状态:关闭";
                    timer3.Enabled = false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); 
                    return;
                }
            }
        }
 
        //设备清零
        private void cmdHSetZero_Click(object sender, EventArgs e)
        {
            cmdHSetZero.Enabled=false;
            byte[] sendData = strToHexByte(txtHSend0.Text);
            SendData(sendData);
            txtHSJQty.Text = "0";
            txtHDQQty.Text = "0";
            txtHKSQty.Text = "0";
            lblHNowQty.Text = "0";
            lblHRelQty.Text = "0";
            cmdHSetZero.Enabled = true;
        }
 
        /// <summary>
        /// 字符串转换16进制字节数组
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        private byte[] strToHexByte(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Replace(" ", ""), 16);
            return returnBytes;
        }
 
 
         /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="data"></param>
        public bool SendData(byte[] data)
        { 
            if (ComDevice.IsOpen)
            {
                try
                {
                    ComDevice.Write(data, 0, data.Length);//发送数据 
                    return true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); 
                }
                finally
                {
                    //cmdHSetZero.Enabled = true;
                }
            }
            else
            {
                MessageBox.Show("串口未打开", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); 
            }
            return false;
        }
 
        private void timer3_Tick(object sender, EventArgs e)
        {
            if (!ComDevice.IsOpen)
            {
                return;
            }
            byte[] sendData = strToHexByte(txtHSend.Text);
            SendData(sendData);
 
        }
 
        /// <summary>
        /// 接收数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Com_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            //if (!ComDevice.IsOpen)
            //{
            //    return;
            //}
            Thread.Sleep(100);
            byte[] ReDatas = new byte[ComDevice.BytesToRead];
            ComDevice.Read(ReDatas, 0, ReDatas.Length);//读取数据
            this.AddData(ReDatas);//输出数据
        }
 
 
        ///// <summary>
        ///// 添加数据
        ///// </summary>
        ///// <param name="data">字节数组</param>
        //public void AddData(byte[] data)
        //{
        //    StringBuilder sb = new StringBuilder();
        //    for (int i = 0; i < data.Length; i++)
        //    {
        //        sb.AppendFormat("{0:x2}" + " ", data[i]);
        //    }
        //    string str = sb.ToString().ToUpper() ;
        //    string[] strArr = str.Split(' ');
        //    string newStr = string.Empty;
        //    try
        //    {
        //        if (strArr[4] != "00")
        //        {
        //            char[] bw = strArr[4].ToCharArray();
        //            if(bw[0]=='0')
        //            {
        //                newStr = bw[1].ToString();
        //            }
        //            else
        //            {
        //                newStr = strArr[4];
        //            }
                  
        //        }
        //        if (string.IsNullOrEmpty(newStr))
        //        {
        //            char[] bw = strArr[5].ToCharArray();
        //            if (bw[0] == '0')
        //            {
        //                newStr = bw[1].ToString();
        //            }
        //            else
        //            {
        //                newStr = strArr[5];
        //            }
        //        }
        //        else
        //        {
        //            newStr += strArr[5];
        //        }
        //        if (strArr[6] != "00" && newStr=="00")
        //        {
        //            //newStr = $"0.{strArr[6]}";
        //            newStr = "0." + strArr[6];
        //        }
        //        if(strArr[6]!="00" && newStr != "00")
        //        {
        //            //newStr += $".{strArr[6]}";
        //            newStr = newStr + strArr[6];
 
        //        }
        //    }
        //    catch
        //    {
        //        newStr = "-1";
        //    }
        //    //将当前数   厘米 转化为 米
        //    double NewQty = DBUtility.ClsPub.isDoule(newStr) / 100.0000;
        //    if (NewQty <= 0)
        //    {
        //        newStr = "0";
        //    }
        //    else
        //    {
        //        newStr = DBUtility.ClsPub.isDoule(NewQty, 2);
        //    }
        //    //
        //    lblHNowQty.Text = newStr;  //当前米数  标签
        //    txtHDQQty.Text = newStr; //当前数量  文本
        //    txtHSJQty.Text = DBUtility.ClsPub.isDoule(DBUtility.ClsPub.isDoule(txtHDQQty.Text) - DBUtility.ClsPub.isDoule(txtHKSQty.Text), 2); //实际米数= 当前米数-开始米数
        //    lblHRelQty.Text = txtHSJQty.Text;
        //}
 
 
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="data">字节数组</param>
        public void AddData(byte[] data)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sb.AppendFormat("{0:x2}" + " ", data[i]);
            }
            var str = sb.ToString().ToUpper().Trim();
            string[] strArr = str.Split(' ');
            string newStr = string.Empty;
            try
            {
                if (strArr[4] != "00")
                {
                    char[] bw = strArr[4].ToCharArray();
                    if(bw[0]=='0')
                    {
                        newStr = bw[1].ToString();
                    }
                    else
                    {
                        newStr = strArr[4];
                    }
                    
                }
                if (string.IsNullOrEmpty(newStr))
                {
                    char[] bw = strArr[5].ToCharArray();
                    if (bw[0] == '0')
                    {
                        newStr = bw[1].ToString();
                    }
                    else
                    {
                        newStr = strArr[5];
                    }
                }
                else
                {
                    newStr += strArr[5];
                }
                if (strArr[6] != "00"&&newStr=="00")
                {
                    //newStr = $"0.{strArr[6]}";
                    newStr = "0." + strArr[6];
                }
                if(strArr[6]!="00"&&newStr!="00")
                {
                    //newStr += $".{strArr[6]}";
                    newStr = newStr + "." + strArr[6];
                }
            }
            catch
            {
                newStr = "-1";
            }
 
 
            //将当前数   厘米 转化为 米
            double NewQty = DBUtility.ClsPub.isDoule(newStr) ;
            if (NewQty <= 0)
            {
                newStr = "0";
            }
            else
            {
                newStr = DBUtility.ClsPub.isDoule(NewQty, 2);
            }
            //
            lblHNowQty.Text = newStr;  //当前米数  标签
            txtHDQQty.Text = newStr; //当前数量  文本
            if (DBUtility.ClsPub.isDoule(txtHDQQty.Text) - DBUtility.ClsPub.isDoule(txtHKSQty.Text) > 0)
            {
                txtHSJQty.Text = DBUtility.ClsPub.isDoule(DBUtility.ClsPub.isDoule(txtHDQQty.Text) - DBUtility.ClsPub.isDoule(txtHKSQty.Text), 2); //实际米数= 当前米数-开始米数
                lblHRelQty.Text = txtHSJQty.Text;
            }
            else
            { 
                txtHSJQty.Text = DBUtility.ClsPub.isDoule(DBUtility.ClsPub.isDoule(txtHDQQty.Text), 2); //实际米数= 当前米数-开始米数
                lblHRelQty.Text = txtHSJQty.Text; 
            }
        }
        
        //删除选中的行数
        private void btnFQSH_Click(object sender, EventArgs e)
        {
            DelRow();
        }
 
        public void DelRow()
        {
            //WEBS
            //if (!oWeb.getSecurity_Log(ModRightNameEdit, DBUtility.ClsPub.CurUserName, ref DBUtility.ClsPub.sExeReturnInfo))
            //{
            //    MessageBox.Show("您没有删除权限!");
            //    return;
            //}
            DataSet DS;
            DataSet DS2;
            bool sBool = true;
            Int64 lngBillKey = 0;
            if (grdMain.CurrentRow == null)
                return;
            if (MessageBox.Show("确定要删除所选行?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                for (int i = 0; i <= grdMain.SelectedRows.Count - 1; i++)
                {
                    lngBillKey = DBUtility.ClsPub.isLong(grdMain.SelectedRows[i].Cells[Fun_GetCol("HItemID")].Value);
                    if (lngBillKey == 0)
                        return;
                    try
                    { 
                        oWeb.getRunProc("Delete from Gy_BarCodeBill where HItemID=" + lngBillKey, ref DBUtility.ClsPub.sExeReturnInfo);
                        oWeb.getRunProc("insert into System_log values(GETDATE(),'" + DBUtility.ClsPub.CurUserName + "','" + DBUtility.ClsPub.CurUserName + "','删除条码:" + DBUtility.ClsPub.isStrNull(grdMain.SelectedRows[i].Cells[Fun_GetCol("条形码")].Value) + "' ,'WMS系统','','删除')", ref DBUtility.ClsPub.sExeReturnInfo);
       
                    }
                    catch (Exception e)
                    {
                        throw (e);
                    }
                }
            }
            MessageBox.Show("删除成功!", "提示");
            DisplayMain();
        }
 
        private void btnFQBD_Click(object sender, EventArgs e)
        {
            //根据界面选择打印模板,打印条形码
            Sub_SetReportList(cmbFQMB.Text);
            Report.PrintPreview(false);
            Thread.Sleep(500);
        }
 
        private void cmdHSaveInfo_Click(object sender, EventArgs e)
        { 
            ClsIni.WriteIni("COMINFO", "txtPortName", DBUtility.ClsPub.isStrNull(txtPortName.Text), DBUtility.ClsPub.AppPath + @"/HXERP.ini");
            ClsIni.WriteIni("COMINFO", "txtBaudRate", DBUtility.ClsPub.isStrNull(txtBaudRate.Text), DBUtility.ClsPub.AppPath + @"/HXERP.ini");
            ClsIni.WriteIni("COMINFO", "cmbParity", DBUtility.ClsPub.isStrNull(cmbParity.Text), DBUtility.ClsPub.AppPath + @"/HXERP.ini");
            ClsIni.WriteIni("COMINFO", "txtDataBits", DBUtility.ClsPub.isStrNull(txtDataBits.Text), DBUtility.ClsPub.AppPath + @"/HXERP.ini");
            ClsIni.WriteIni("COMINFO", "cmbStopBits", DBUtility.ClsPub.isStrNull(cmbStopBits.Text), DBUtility.ClsPub.AppPath + @"/HXERP.ini");
            ClsIni.WriteIni("COMINFO", "txtHSend0", DBUtility.ClsPub.isStrNull(txtHSend0.Text), DBUtility.ClsPub.AppPath + @"/HXERP.ini");
            ClsIni.WriteIni("COMINFO", "txtHSend", DBUtility.ClsPub.isStrNull(txtHSend.Text), DBUtility.ClsPub.AppPath + @"/HXERP.ini");
            ClsIni.WriteIni("COMINFO", "txtHSourceID", DBUtility.ClsPub.isStrNull(txtHSourceID.Text), DBUtility.ClsPub.AppPath + @"/HXERP.ini");
            ClsIni.WriteIni("COMINFO", "txtHSourceIDTag", DBUtility.ClsPub.isStrNull(txtHSourceID.Tag), DBUtility.ClsPub.AppPath + @"/HXERP.ini"); 
        }
 
        private void btnBLHB_Click(object sender, EventArgs e)
        {
            Gy_BadReasonList oGy_BadReasonList = new Gy_BadReasonList();
            oGy_BadReasonList.ShowDialog();
            if (oGy_BadReasonList.ISOK == 1)
            {
                txtHReasonList.Text = "";
                txtHReasonList.Text = oGy_BadReasonList.txtHBadReason.Text;
                if (SaveBadReasonBill())
                {
                    MessageBox.Show("保存不良原因成功!");
 
                    DisplayMain();
                }
                else
                {
                    MessageBox.Show("保存不良原因失败!" + DBUtility.ClsPub.sExeReturnInfo);
                }
            }
            oGy_BadReasonList = null;
        }
 
        private void cmdSaveAndNext_Click(object sender, EventArgs e)
        {
            if (SaveBill("Save"))
            {
                txtHKSQty.Text = txtHDQQty.Text;//继续裁切,将当前米数作为开始米数;
            }
        }
 
        private void btnBLBD_Click(object sender, EventArgs e)
        {
 
        }
 
        private void btnBLSH_Click(object sender, EventArgs e)
        {
 
        }
 
        private void cmdHWaitList_Click(object sender, EventArgs e)
        {
            //跳出该产线 未处理的单据
            Sc_WaitingForCutting_List oSc_WaitingForCutting_List = new Sc_WaitingForCutting_List();
            if (HSourceBillType == "生产订单")
            {
                oSc_WaitingForCutting_List.HBillType = "3710";
            }
            else if (HSourceBillType == "生产汇报单")
            {
                oSc_WaitingForCutting_List.HBillType = "3711";
            }
            else if (HSourceBillType == "收料通知单")
            {
                oSc_WaitingForCutting_List.HBillType = "1203";
            }
            else
            {
                MessageBox.Show("错误的单据类型!");
                return;
            }
 
            oSc_WaitingForCutting_List.ShowDialog();
 
        }
 
        private bool SaveBadReasonBill()
        {
            try
            {
                if (txtHReasonList.Text.Trim() == "")
                {
                    MessageBox.Show("没有选择不良原因!");
                    return false;
                }
                //将选择不良原因分解并保存入数据库
                WMSWeb.ClsQC_NoPassProdCheckBillMain oQC_NoPassProdCheckBillMain = new WMSWeb.ClsQC_NoPassProdCheckBillMain();
                oQC_NoPassProdCheckBillMain.HDate = DateTime.Today;
                oQC_NoPassProdCheckBillMain.HSourceID = DBUtility.ClsPub.isLong(txtHSourceID.Tag);
                oQC_NoPassProdCheckBillMain.HMaterID = DBUtility.ClsPub.isLong(txtHMaterNumber.Tag);
                oQC_NoPassProdCheckBillMain.HProcID = DBUtility.ClsPub.isLong(0);
                oQC_NoPassProdCheckBillMain.HWorkCenterID = DBUtility.ClsPub.isLong(0);
                oQC_NoPassProdCheckBillMain.HICMOInterID = 0;
                oQC_NoPassProdCheckBillMain.HICMOBillNo = "";
                oQC_NoPassProdCheckBillMain.HInStockQty = 0;
                oQC_NoPassProdCheckBillMain.HCheckQty = 0;
                oQC_NoPassProdCheckBillMain.HRightQty = 0;
                oQC_NoPassProdCheckBillMain.HBadQty = DBUtility.ClsPub.isDoule(txtHDQQty.Text);
                oQC_NoPassProdCheckBillMain.HPlanQty = 0;
                oQC_NoPassProdCheckBillMain.HBadPNL = 0;
                oQC_NoPassProdCheckBillMain.HPlanPNL = 0;
                oQC_NoPassProdCheckBillMain.HFirstCheckEmp = 0;
                oQC_NoPassProdCheckBillMain.HCheckerResult = txtHReasonList.Text;
                oQC_NoPassProdCheckBillMain.HNote = "在线检验";
                oQC_NoPassProdCheckBillMain.HProcExchInterID = HSourceInterID;//源单主内码
                oQC_NoPassProdCheckBillMain.HProcExchEntryID = HSourceEntryID;//源单子内码
                oQC_NoPassProdCheckBillMain.HProcExchBillNo = HSourceBillTypeID;//源单类型
                oQC_NoPassProdCheckBillMain.HMainSourceInterID = 0;
                oQC_NoPassProdCheckBillMain.HGroupName = "";
                oQC_NoPassProdCheckBillMain.HOrderProcNo = "";
                oQC_NoPassProdCheckBillMain.HProcExchQty = 0;
                if (oWeb.set_SaveNoPassProdCheckBill_New(oQC_NoPassProdCheckBillMain, HSourceBillTypeID, txtHReasonList.Text.Trim(), ref DBUtility.ClsPub.sExeReturnInfo))
                {
                    txtHReasonList.Text = "";
                    return true;
                }
                txtHReasonList.Text = "";
                return false;
            }
            catch(Exception e1)
            {
                MessageBox.Show(e1.Message);
                return false;
            }
        }
 
        private void cmdHSourceID_Click(object sender, EventArgs e)
        {
            SCM.ClsIF_Source_View oSource = new SCM.ClsIF_Source_View();
            string sWhere = "";
            if (oSource.RefreshView(sWhere))
            {
                this.txtHSourceID.Text = oSource.oModel.HName;
                this.txtHSourceID.Tag = oSource.oModel.HItemID.ToString();
            }
            else
            {
                this.txtHSourceID.Text = "";
            }
        }
 
        private void cmdSaveAndDown_Click(object sender, EventArgs e)
        {
            if (SaveBill("SaveDown"))
            {
                txtHKSQty.Text = txtHDQQty.Text;//继续裁切,将当前米数作为开始米数;
            }
        }
 
        private void cmdHGroupID_Click(object sender, EventArgs e)
        {
            SCM.ClsIF_Group_View oGroup= new SCM.ClsIF_Group_View();
            string sWhere = "";
            if (oGroup.RefreshView(sWhere))
            {
                this.txtHGroupID.Text = oGroup.oModel.HName;
                this.txtHGroupID.Tag = oGroup.oModel.HItemID.ToString();
            }
            else
            {
                this.txtHGroupID.Text = "";
            }
        }
 
        private void txtHSourceID_TextChanged(object sender, EventArgs e)
        {
            if (txtHSourceID.Text.Trim() == "")
            {
                txtHSourceID.Tag = "";
            }
        }
 
        private void txtHGroupID_TextChanged(object sender, EventArgs e)
        {
            if (txtHGroupID.Text.Trim() == "")
            {
                txtHGroupID.Tag = "";
            }
        }
 
        private void cmdHEmp_Click(object sender, EventArgs e)
        {
            SCM.ClsIF_Employee_View oEmp = new SCM.ClsIF_Employee_View();
            string sWhere = "";
            if (oEmp.RefreshView(sWhere))
            {
                this.txtHEmpID.Text = oEmp.oModel.HName;
                this.txtHEmpID.Tag = oEmp.oModel.HItemID.ToString();
            }
            else
            {
                this.txtHEmpID.Text = "";
            }
        }
 
        private void cmdHEmp2_Click(object sender, EventArgs e)
        {
            SCM.ClsIF_Employee_View oEmp = new SCM.ClsIF_Employee_View();
            string sWhere = "";
            if (oEmp.RefreshView(sWhere))
            {
                this.txtHEmpID2.Text = oEmp.oModel.HName;
                this.txtHEmpID2.Tag = oEmp.oModel.HItemID.ToString();
            }
            else
            {
                this.txtHEmpID2.Text = "";
            }
        }
 
        private void txtHEmpID_TextChanged(object sender, EventArgs e)
        {
            if (txtHEmpID.Text.Trim() == "")
            {
                txtHEmpID.Tag = "";
            }
        }
 
        private void txtHEmpID2_TextChanged(object sender, EventArgs e)
        {
            if (txtHEmpID2.Text.Trim() == "")
            {
                txtHEmpID2.Tag = "";
            }
        }
 
        private void cmdHEmp3_Click(object sender, EventArgs e)
        {
            SCM.ClsIF_Employee_View oEmp = new SCM.ClsIF_Employee_View();
            string sWhere = "";
            if (oEmp.RefreshView(sWhere))
            {
                this.txtHEmpID3.Text = oEmp.oModel.HName;
                this.txtHEmpID3.Tag = oEmp.oModel.HItemID.ToString();
            }
            else
            {
                this.txtHEmpID3.Text = "";
            }
        }
 
        private void txtHEmpID3_TextChanged(object sender, EventArgs e)
        {
            if (txtHEmpID3.Text.Trim() == "")
            {
                txtHEmpID3.Tag = "";
            }
        }
 
        private void cmdZero_Click(object sender, EventArgs e)
        {
            txtHKSQty.Text = "0";
            txtHSJQty.Text = DBUtility.ClsPub.isDoule(DBUtility.ClsPub.isDoule(txtHDQQty.Text) - DBUtility.ClsPub.isDoule(txtHKSQty.Text), 2);
 
        }
 
        private void cmdSaveAndDown2_Click(object sender, EventArgs e)
        {
            if (SaveBill("Sample"))
            {
                txtHKSQty.Text = txtHDQQty.Text;//继续裁切,将当前米数作为开始米数;
            }
        }
 
    }
}