From 9c7972d018cc98fc6adf4d57e18084255db103c1 Mon Sep 17 00:00:00 2001
From: llj <132905093+newwwwwwtree@users.noreply.github.com>
Date: 星期三, 24 九月 2025 17:23:21 +0800
Subject: [PATCH] PDA库存转移模块

---
 WarM/条码打印/Gy_BarCodeBill_automaticallyByPLC_New.cs |  299 ++++++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 261 insertions(+), 38 deletions(-)

diff --git "a/WarM/\346\235\241\347\240\201\346\211\223\345\215\260/Gy_BarCodeBill_automaticallyByPLC_New.cs" "b/WarM/\346\235\241\347\240\201\346\211\223\345\215\260/Gy_BarCodeBill_automaticallyByPLC_New.cs"
index 5b895f1..daf7c8f 100644
--- "a/WarM/\346\235\241\347\240\201\346\211\223\345\215\260/Gy_BarCodeBill_automaticallyByPLC_New.cs"
+++ "b/WarM/\346\235\241\347\240\201\346\211\223\345\215\260/Gy_BarCodeBill_automaticallyByPLC_New.cs"
@@ -13,6 +13,8 @@
 using System.Drawing.Printing;
 using System.IO;
 using System.Management;
+using System.Net.NetworkInformation;
+using System.Printing;
 
 namespace WarM
 {
@@ -223,14 +225,14 @@
                 //绯荤粺鍙傛暟锛岀敓鎴愭潯鐮佹暟閲忓彲鍚﹁秴婧愬崟鏁伴噺鎺у埗锛圢涓轰笉鍙秴婧愬崟鏁伴噺锛�
                 if (SourceQtyCtl == "N")
                 {
-                    sWhere = " and HOrgID =" + HOrgID.ToString() + " and 浠诲姟鏁伴噺>0 and 鐢熶骇绾�='" + comboBox_SourceNameParams.Text + "' ";
+                    sWhere = " and HOrgID =" + HOrgID.ToString() + " and 浠诲姟鏁伴噺>0 and 鐘舵�� = '寮�宸�' and 鐢熶骇绾�='" + comboBox_SourceNameParams.Text + "' ";
                 }
                 else
                 {
-                    sWhere = " and HOrgID =" + HOrgID.ToString();
+                    sWhere = " and HOrgID =" + HOrgID.ToString() + " and 鐘舵�� = '寮�宸�' and 鐢熶骇绾�='" + comboBox_SourceNameParams.Text + "' "; ;
                 }
 
-                DAL.Cls_S_IFCLD_ICMOList oIFCLD_ICMOList = new DAL.Cls_S_IFCLD_ICMOList();
+                DAL.Cls_S_IFCLD_ICMOList1 oIFCLD_ICMOList = new DAL.Cls_S_IFCLD_ICMOList1();
                 if (oIFCLD_ICMOList.Refresh(sWhere))  //閫夋嫨鍘熷崟
                 {
                     FillSelectData(oIFCLD_ICMOList.oBillSelectColl);
@@ -406,9 +408,40 @@
         #region 鐢熸垚鎸夐挳
         private void bc_Click(object sender, EventArgs e)
         {
+            //瀹℃牳鏉冮檺
+            if (!DBUtility.ClsPub.Security_Log_second("Gy_BarCodeBill_automaticallyByPLC_ProduceByHand", 1, false, DBUtility.ClsPub.CurUserName))
+            {
+                MessageBox.Show("鐢熸垚澶辫触锛屾棤鏉冮檺锛�");
+                return;
+            }
+
+            //娓呴櫎鎵撳嵃浠诲姟
+            string msg = "";
+            if (!clearPrinterTask(ref msg))
+            {
+                MessageBox.Show(msg);
+                return;
+            }
+
             this.Sub_SaveBill();
             Display();
 
+            if (radioButton_IsPrint_Yes.Checked == true)
+            {
+                //璁剧疆鎵撳嵃妯℃澘锛屾墦鍗�
+                grdList.Rows[0].Cells[0].Value = "*";
+                Report = new GridppReport();
+                Report.LoadFromFile(DBUtility.ClsPub.AppPath + @"\" + textBox_PrintModelParams.Text + ".grf");  //here .
+                Report.BeforePostRecord += new _IGridppReportEvents_BeforePostRecordEventHandler(ReportBeforePostRecord);
+                Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecordByDataTable);
+                Report.PrintEnd += new _IGridppReportEvents_PrintEndEventHandler(ReportPrintEnd);
+                if (comboBox_PrinterParams.Text != "")
+                {
+                    Report.Printer.PrinterName = comboBox_PrinterParams.Text.Replace("(榛樿)", "");
+                }
+                Report.Print(false);
+            }
+            
             Display4();
         }
         //淇濆瓨鍗曟嵁
@@ -813,6 +846,7 @@
                         HWhID = ClsPub.isLong(grdSub.Rows[i].Cells[HWhID2Col].Value);
                         HSPID = ClsPub.isLong(grdSub.Rows[i].Cells[HSPID2Col].Value);
                         HRemark = ClsPub.isStrNull(grdSub.Rows[i].Cells[HRemark2Col].Value);
+                        HRemark = "PLC鑷姩鐢熸垚";
                         HMaterName = ClsPub.isStrNull(grdSub.Rows[i].Cells[HMaterName2Col].Value);
                         HMaterModel = ClsPub.isStrNull(grdSub.Rows[i].Cells[HMaterModel2Col].Value);
                         HPinfan = ClsPub.isStrNull(grdSub.Rows[i].Cells[HPinfan2Col].Value);
@@ -919,14 +953,14 @@
                 string LIU = "";
                 int LEN = 4;
                 DataSet ds;
-                string sql = "select * from h_v_IF_BarCodeBillList Where HMaterID = " + grdMain.Rows[0].Cells[HMaterIDCol].Value + " and HSTOCKORGID = " + HOrgID + " and CONVERT(varchar(100),鏉$爜鏃ユ湡, 23) = '" + HDate + "' order by 鎵瑰彿 desc";
+                string sql = "select * from h_v_IF_BarCodeBillList Where HMaterID = " + grdMain.Rows[0].Cells[HMaterIDCol].Value + " and HSTOCKORGID = " + HOrgID + " and CONVERT(varchar(100),鏉$爜鏃ユ湡, 23) = '" + HDate + "' and 澶囨敞 = 'PLC鑷姩鐢熸垚' order by 鎵瑰彿 desc";
                 ds = oCn.RunProcReturn(sql, "h_v_IF_BarCodeBillList");
                 if (ds != null)
                 {
                     long count = 0;
                     if (ds.Tables[0].Rows.Count > 0)
                     {
-                        count = ClsPub.isLong(ds.Tables[0].Rows[0]["鎵瑰彿"].ToString().Replace(sYear + sPeriod + sDay, ""));
+                        count = ClsPub.isLong(ds.Tables[0].Rows[0]["鎵瑰彿"].ToString().Replace(sYear + sPeriod + sDay+" ", ""));
                     }
                     LIU += count + 1;
                     while (LIU.Length < LEN)  //濡傛灉娴佹按鍙峰皬浜�6浣嶆暟鍓嶉潰琛�0
@@ -940,7 +974,7 @@
                 }
 
                 //鎷兼帴鎵瑰彿
-                HBatchNo = sYear + sPeriod + sDay + LIU;
+                HBatchNo = sYear + sPeriod + sDay+" " + LIU;
             }
 
 
@@ -966,34 +1000,68 @@
             string LIU = "";
             int LEN = 4;
 
-            //鑾峰彇褰撴棩鏈骇鍝佹潯鐮佺紪鍙锋渶澶х殑璁板綍
-            DataSet ds;
-            string sql = "select * from h_v_IF_BarCodeBillList Where HMaterID = " + grdMain.Rows[0].Cells[HMaterIDCol].Value + " and HSTOCKORGID = " + HOrgID + " and CONVERT(varchar(100),鏉$爜鏃ユ湡, 23) = '" + HDate + "' order by 鏉$爜缂栧彿 desc";
-            ds = oCn.RunProcReturn(sql, "h_v_IF_BarCodeBillList");
-            if (ds != null)
+            //鑾峰彇浜у搧鏉$爜澶囦唤琛ㄤ腑褰撴棩鏈骇鍝佹潯鐮佺紪鍙锋渶澶х殑璁板綍
+            long HMaxNo_Bak = 0;                //璁板綍浜у搧鏉$爜澶囦唤琛ㄤ腑褰撴棩鏈骇鍝佹潯鐮佺紪鍙锋渶澶х殑璁板綍鐨勬潯鐮佺紪鍙风殑娴佹按鍙�
+            DataSet ds1;
+            string sql1 = "select * from Gy_BarCodeBill_SWELL_SubBarCodeBill Where HMaterID = " + grdMain.Rows[0].Cells[HMaterIDCol].Value + " and HSTOCKORGID = " + HOrgID + " and CONVERT(varchar(100),HBarCodeDate, 23) = '" + HDate + "' and HInterID <>0 order by HBarCode desc";
+            ds1 = oCn.RunProcReturn(sql1, "Gy_BarCodeBill_SWELL_SubBarCodeBill");
+            if (ds1 != null && ds1.Tables[0].Rows.Count>0)
             {
-                long count = 0;
-                if (ds.Tables[0].Rows.Count > 0)
-                {
-                    string HBarCodeMaxNo = ds.Tables[0].Rows[0]["鏉$爜缂栧彿"].ToString();
-                    HBarCodeMaxNo = HBarCodeMaxNo.Substring(HBarCodeMaxNo.Length - LEN, LEN);
-                    count = ClsPub.isLong(HBarCodeMaxNo);
-                }
-                LIU += count + 1;
-                while (LIU.Length < LEN)  //濡傛灉娴佹按鍙峰皬浜�6浣嶆暟鍓嶉潰琛�0
-                {
-                    LIU = "0" + LIU;
-                }
+                string HBarCodeMaxNo = ds1.Tables[0].Rows[0]["HBarCode"].ToString();
+                HBarCodeMaxNo = HBarCodeMaxNo.Substring(HBarCodeMaxNo.Length - LEN, LEN);
+                HMaxNo_Bak = ClsPub.isLong(HBarCodeMaxNo);
             }
-            else
+
+            ////鑾峰彇鏉$爜妗f涓綋鏃ユ湰浜у搧鏉$爜缂栧彿鏈�澶х殑璁板綍
+            long HMaxNo = 0;                //璁板綍鏉$爜妗f涓綋鏃ユ湰浜у搧鏉$爜缂栧彿鏈�澶х殑璁板綍鐨勬潯鐮佺紪鍙风殑娴佹按鍙�
+            DataSet ds;
+            string sql = "select * from h_v_IF_BarCodeBillList Where HMaterID = " + grdMain.Rows[0].Cells[HMaterIDCol].Value + " and HSTOCKORGID = " + HOrgID + " and CONVERT(varchar(100),鏉$爜鏃ユ湡, 23) = '" + HDate + "' and HInterID <> 0 order by 鏉$爜缂栧彿 desc";
+            ds = oCn.RunProcReturn(sql, "h_v_IF_BarCodeBillList");
+            if (ds != null && ds.Tables[0].Rows.Count > 0)
             {
-                return "";
+                string HBarCodeMaxNo = ds.Tables[0].Rows[0]["鏉$爜缂栧彿"].ToString();
+                HBarCodeMaxNo = HBarCodeMaxNo.Substring(HBarCodeMaxNo.Length - LEN, LEN);
+                HMaxNo = ClsPub.isLong(HBarCodeMaxNo);
+            }
+
+
+            //鑾峰彇褰撴棩鏈骇鍝� 鍦ㄦ潯鐮佹。妗堝拰浜у搧鏉$爜澶囦唤琛ㄤ腑 鏈�澶х殑娴佹按鍙�
+            long count = 0;
+            count = HMaxNo >= HMaxNo_Bak ? HMaxNo : HMaxNo_Bak;
+
+            //涓烘祦姘村彿鍓嶉潰琛ラ浂
+            LIU += count + 1;
+            while (LIU.Length < LEN)  //濡傛灉娴佹按鍙峰皬浜�6浣嶆暟鍓嶉潰琛�0
+            {
+                LIU = "0" + LIU;
             }
 
             //鎷兼帴鎵瑰彿
             HBarCode_MaxNo = sYear + sPeriod + sDay + LIU;
 
-            return HBarCode_MaxNo;
+            long HBarCode_MaxNo_long = long.Parse(HBarCode_MaxNo);
+
+            while (true)
+            {
+                int HCount_BarCodeBillList = 0;
+                int HCount_SubBarCodeBill = 0;
+                string sql_confirm = "select * from h_v_IF_BarCodeBillList where 鏉$爜缂栧彿 = '" + (ClsPub.isStrNull(grdSub.Rows[0].Cells[HMaterModel2Col].Value) + HBarCode_MaxNo_long.ToString()) + "' and 鏉$爜绫诲瀷 = '鍞竴鏉$爜'" + " and CONVERT(varchar(100),鏉$爜鏃ユ湡, 23) = '" + HDate + "'";
+                ds = oCn.RunProcReturn(sql_confirm, "h_v_IF_BarCodeBillList");
+                HCount_BarCodeBillList = ds.Tables[0].Rows.Count;
+                sql_confirm = "select * from Gy_BarCodeBill_SWELL_SubBarCodeBill Where HBarCode = '" + (ClsPub.isStrNull(grdSub.Rows[0].Cells[HMaterModel2Col].Value) + HBarCode_MaxNo_long.ToString()) + "' and HBarCodeType = '鍞竴鏉$爜'" + " and CONVERT(varchar(100),HMakeDate, 23) = '" + HDate + "'";
+                ds = oCn.RunProcReturn(sql_confirm, "Gy_BarCodeBill_SWELL_SubBarCodeBill");
+                HCount_SubBarCodeBill = ds.Tables[0].Rows.Count;
+                if (HCount_BarCodeBillList + HCount_SubBarCodeBill > 0)
+                {
+                    HBarCode_MaxNo_long += 1;
+                }
+                else
+                {
+                    break;
+                }
+            }
+
+            return HBarCode_MaxNo_long.ToString();
         }
         #endregion
 
@@ -1005,6 +1073,12 @@
         {
             try
             {
+                if (socket != null && socket.Connected)
+                {
+                    MessageBox.Show("宸插缓绔嬭繛鎺ワ紝涓嶅彲閲嶅寤虹珛杩炴帴锛�");
+                    return;
+                }
+
                 if (grdMain.Rows.Count == 0 || grdMain.Rows[0].Cells[HMaterIDCol].Value == null || grdMain.Rows[0].Cells[HMaterIDCol].Value.ToString() == "" || grdMain.Rows[0].Cells[HMaterIDCol].Value.ToString() == "0")
                 {
                     MessageBox.Show("璇烽�夋嫨婧愬崟锛�");
@@ -1048,6 +1122,8 @@
                 Thread thread = new Thread(ReceiveMess);
                 thread.Start();
                 MessageBox.Show("杩炴帴鎴愬姛锛�");
+
+                timer3.Enabled = true;
             }
             catch (Exception ex)
             {
@@ -1072,13 +1148,17 @@
                     {
                         string RMess = Encoding.UTF8.GetString(buffer, 0, length);
                         produceQty = ClsPub.isLong(RMess);
+                        if (produceQty == 0)
+                        {
+                            produceQty = 1;
+                        }
                     }
                 }
             }
             catch (Exception ex)
             {
-                MessageBox.Show("鎺ュ彛寮傚父锛屽凡鍏抽棴杩炴帴锛�" + ex.Message);
-                socket.Close();
+                //MessageBox.Show("鎺ュ彛寮傚父锛屽凡鍏抽棴杩炴帴锛�" + ex.Message);
+                //socket.Close();
             }
         }
 
@@ -1118,9 +1198,43 @@
             return false;
         }
 
+        //娓呴櫎鎸囧畾鎵撳嵃鏈轰换鍔�
+        private bool clearPrinterTask(ref string msg)
+        {
+            try
+            {
+                PrintServer localPrintServer = new LocalPrintServer();
+                PrintQueue pq = localPrintServer.GetPrintQueue(comboBox_PrinterParams.Text.Replace("(榛樿)", "").ToLower());
+                pq.Refresh();
+                PrintJobInfoCollection allPrintJobs = pq.GetPrintJobInfoCollection();
+                foreach (PrintSystemJobInfo printJob in allPrintJobs)
+                {
+                    printJob.Cancel();
+
+                }
+                return true;
+            }
+            catch(Exception ex)
+            {
+                msg = ex.Message;
+                return false;
+            }
+        }
+
         //鏍规嵁閫氳淇℃伅鐢熸垚鏉$爜骞舵墦鍗�
         private void timer2_Tick(object sender, EventArgs e)
         {
+            if (produceQty > 0)
+            {
+                //娓呴櫎鎵撳嵃浠诲姟
+                string msg = "";
+                if (!clearPrinterTask(ref msg))
+                {
+                    MessageBox.Show(msg);
+                    produceQty = 0;
+                }
+            }
+
             for (int i = 0; i < produceQty; i++)
             {
                 //鐢熸垚鏉$爜
@@ -1129,18 +1243,21 @@
                 //鑾峰彇闇�瑕佹墦鍗扮殑鏁版嵁
                 Display();
 
-                //璁剧疆鎵撳嵃妯℃澘锛屾墦鍗�
-                grdList.Rows[0].Cells[0].Value = "*";
-                Report = new GridppReport();
-                Report.LoadFromFile(DBUtility.ClsPub.AppPath + @"\" + textBox_PrintModelParams.Text + ".grf");  //here .
-                Report.BeforePostRecord += new _IGridppReportEvents_BeforePostRecordEventHandler(ReportBeforePostRecord);
-                Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecordByDataTable);
-                Report.PrintEnd += new _IGridppReportEvents_PrintEndEventHandler(ReportPrintEnd);
-                if (comboBox_PrinterParams.Text != "")
+                if (radioButton_IsPrint_Yes.Checked == true)
                 {
-                    Report.Printer.PrinterName = comboBox_PrinterParams.Text.Replace("(榛樿)", "");
+                    //璁剧疆鎵撳嵃妯℃澘锛屾墦鍗�
+                    grdList.Rows[0].Cells[0].Value = "*";
+                    Report = new GridppReport();
+                    Report.LoadFromFile(DBUtility.ClsPub.AppPath + @"\" + textBox_PrintModelParams.Text + ".grf");  //here .
+                    Report.BeforePostRecord += new _IGridppReportEvents_BeforePostRecordEventHandler(ReportBeforePostRecord);
+                    Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecordByDataTable);
+                    Report.PrintEnd += new _IGridppReportEvents_PrintEndEventHandler(ReportPrintEnd);
+                    if (comboBox_PrinterParams.Text != "")
+                    {
+                        Report.Printer.PrinterName = comboBox_PrinterParams.Text.Replace("(榛樿)", "");
+                    }
+                    Report.Print(false);
                 }
-                Report.Print(false);
 
                 if (i == produceQty - 1)
                 {
@@ -1220,6 +1337,7 @@
                 if (socket.Connected)
                 {
                     socket.Close();
+                    MessageBox.Show("杩炴帴鍏抽棴鎴愬姛锛�");
                 }
                 else
                 {
@@ -1230,6 +1348,10 @@
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message);
+            }
+            finally
+            {
+                timer3.Enabled = false;
             }
         }
         #endregion
@@ -2122,6 +2244,7 @@
                 {
                     sMoney = ClsPub.isDoule(grdMain.Rows[sRow].Cells[HQtyCol].Value) / ClsPub.isDoule(grdMain.Rows[sRow].Cells[HMinQtyCol].Value);
                 }
+                sMoney = Math.Round(sMoney, 10);    // 淇濈暀10浣嶅皬鏁拌繘琛屽洓鑸嶄簲鍏�
                 sMoney = Math.Ceiling(sMoney);
                 grdMain.Rows[sRow].Cells[HBQtyCol].Value = sMoney;
             }
@@ -2337,5 +2460,105 @@
 
             }
         }
+
+
+
+        #region 鏂嚎閲嶈繛
+        private int maxRepeatConnectTimes = 5;
+        private int currentRepeatConnectTimes = 0;
+
+        #region 瀹氭椂鍣ㄦ娴嬭繛鎺ョ姸鎬併�佹柇绾块噸杩�
+        private void timer3_Tick(object sender, EventArgs e)
+        {
+            if (currentRepeatConnectTimes < maxRepeatConnectTimes)
+            {
+                //鍒ゆ柇瀹㈡埛绔槸鍚﹀彲浠ヨ闂湇鍔″櫒锛岃嫢涓嶅彲浠ヨ闂紝鍏抽棴socket杩炴帴
+                if (!getTargetInternetStatus())
+                {
+                    if (socket != null && socket.Connected)
+                    {
+                        socket.Close();
+                    }
+
+                    currentRepeatConnectTimes += 1;
+                }
+                else if (getTargetInternetStatus() && !socket.Connected)
+                {
+                    //鑻ュ鎴风鍙互璁块棶鏈嶅姟鍣紝socket閲嶆柊杩炴帴鏈嶅姟鍣�
+                    try
+                    {
+                        socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
+                        IPAddress iPAddress = IPAddress.Parse(textBox_IPParams.Text);
+                        IPEndPoint point = new IPEndPoint(iPAddress, Convert.ToInt32(textBox_PortParams.Text));
+                        socket.Connect(point);
+
+                        Thread thread = new Thread(ReceiveMess);
+                        thread.Start();
+
+                        currentRepeatConnectTimes = 0;
+                    }
+                    catch (Exception ex)
+                    {
+                        currentRepeatConnectTimes += 1;
+                    }
+                }
+            }
+            else
+            {
+                //timer3.Enabled = false;
+                currentRepeatConnectTimes = 0;
+                MessageBox.Show("杩炴帴宸叉柇寮�锛岃閲嶆柊杩炴帴锛�!");
+            }
+        }
+        #endregion
+
+        #region 鑾峰彇鐩爣璁惧缃戠粶杩炴帴鐘舵��
+        private bool getTargetInternetStatus()
+        {
+            try
+            {
+                string targetIp = textBox_IPParams.Text; // 鏇挎崲涓虹洰鏍囩數鑴戠殑IP鍦板潃
+                Ping pingSender = new Ping();
+                PingOptions options = new PingOptions();
+                // 浣跨敤64瀛楄妭鐨勬暟鎹寘锛屸�屼綘鍙互鏍规嵁闇�瑕佽皟鏁磋繖涓��
+                string data = "Hello from Ping!";
+                byte[] buffer = Encoding.ASCII.GetBytes(data);
+                int timeout = 1200; // 瓒呮椂鏃堕棿锛屸�屽崟浣嶄负姣
+                PingReply reply = pingSender.Send(targetIp, timeout, buffer, options);
+
+                if (reply.Status == IPStatus.Success)
+                {
+                    return true;
+                }
+                else
+                {
+                    return false;
+                }
+            }
+            catch (Exception ex)
+            {
+                return false;
+            }
+
+        }
+        #endregion
+        #endregion
+
+        #region 寤虹珛閫氳鍚庯紝涓嶆柇鍚戞湇鍔″櫒鍙戦�佹暟鎹紝鐢ㄤ簬甯姪鏈嶅姟绔‘瀹氳繛鎺ョ姸鎬�
+        private void timer4_Tick(object sender, EventArgs e)
+        {
+            if (getTargetInternetStatus()&& socket!=null && socket.Connected)
+            {
+                try
+                {
+                    socket.Send(Encoding.ASCII.GetBytes("1"));
+                }
+                catch (Exception ex)
+                {
+
+                }
+            }
+        }
+        #endregion
     }
 }
\ No newline at end of file

--
Gitblit v1.9.1