yangle
2023-05-24 b129762a4bb5ec2cde5edf16bc9cbda57c73587d
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace WarM
{
    public partial class Kf_InitStockBill : Form
    {
        private int HItemIDCol = 0;
        private int HNumberCol = 0;
        private int HWHID;
 
        public DBUtility.frm_B_BillQueryCondition_New frmCondition;
        const string ViewName = "h_v_Kf_InitStockBill";
        const string ModName = "物料";
        public DBUtility.ClsPub.Enum_InputMode InputMode;  //录入状态标志(0-非录入状态 1-增加 2-修改)
        public long HItemID;//项目ID
        public string sCondition; //过滤条件
        SQLHelper.ClsCN oCn = new SQLHelper.ClsCN();
        DataSet Ds = new DataSet();
        TreeNode CurNode = new TreeNode();
        public Kf_InitStockBill()
        {
            InitializeComponent();
        }
        //以下代码基本不需要修改
        #region 固定代码
        //保存GRID列宽
        private void Kf_InitStockBill_FormClosing(object sender, FormClosingEventArgs e)
        {
            DBUtility.Xt_BaseBillFun.SaveGrid(grdMain, this.Name);
        }
        //窗体加载
        private void Kf_InitStockBill_Load(object sender, EventArgs e)
        {
            frmCondition = new DBUtility.frm_B_BillQueryCondition_New();
            LoadTree();
            initGrid();
            InputMode = DBUtility.ClsPub.Enum_InputMode.InputMode_View;
            RefreshTool();
        }
        //加载子结点
        private void LoadAllNodes(TreeNode sNode)
        {
            if (sNode != null)
            {
                try
                {
                    long sName = Convert.ToInt64(sNode.Name.Substring(1, sNode.Name.Length - 1));
                    sNode.Nodes.Clear();
                    DataSet Ds = oCn.RunProcReturn("select hitemid,hnumber,hname from Gy_Warehouse where HParentID='" + sName + "'", "Gy_Warehouse");
                    for (int i = 0; i < Ds.Tables[0].Rows.Count; i++)
                    {
                        TreeNode oNode = sNode.Nodes.Add("T" + Ds.Tables[0].Rows[i]["HItemID"].ToString(), Ds.Tables[0].Rows[i]["HNumber"].ToString() + "-" + Ds.Tables[0].Rows[i]["HName"].ToString(), 0,1);
                    }
                    sNode.Expand();
                }
                catch (Exception e)
                {
                    MessageBox.Show("加载子项目失败!" + e.Message, "提示");
                }
            }
        }
        //加载树形
        private void LoadTree()
        {
            try
            {
                tv.Nodes.Clear();
                tv.ImageList = imageList1;
                TreeNode sNode = tv.Nodes.Add("T0", this.Text, 0, 1);
                LoadAllNodes(sNode);
            }
            catch (Exception e )
            {
                MessageBox.Show("加载树型失败!" + e.Message, "提示");
            }
        }
     
     
        //退出模块
        private void tc_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //刷新
        private void xs_Click(object sender, EventArgs e)
        {
            if (HWHID != 0)
            {
                Display();
            }
        }
    
        //得到对应列
        private Int32 Fun_GetCol(string sCol)
        {
            return DBUtility.Gy_BaseFun.Fun_GetCol(sCol, grdMain);
        }
        //刷新TOOLBAR
        private void RefreshTool()
        {
            if (InputMode == DBUtility.ClsPub.Enum_InputMode.InputMode_View)
            {
                grdMain.Enabled = true;
                tc.Enabled = true;
                yl.Enabled = true;
            }
            else
            {
                grdMain.Enabled = false;
                tc.Enabled = true;
                yl.Enabled = false;
            }
        }
     
   
     
        //树型 单击
        private void tv_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (CurNode.Text != "" && CurNode.Text != "期初录入")
            {
                if (MessageBox.Show("是否要保存记录?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    save();
                }
            }
            CurNode = e.Node;
            if (CurNode != null)
            {
                if (CurNode.Nodes.Count == 0)
                    LoadAllNodes(CurNode);
                //找到选中结点
                FindRow(CurNode.Text);
            }
        }
        //查找结点
        private void FindRow(string sCode)
        {
            DBUtility.Gy_BaseFun.FindRow(sCode, ModName, ref sCondition);
            Display(); 
 
        }
     
   
        //选中行是否正常
        //private bool AllowRow()
        //{
        //    if (grdMain.Rows >= grdMain.FixedRows && grdMain.Row <= grdMain.Rows - 1)
        //        return true;
        //    else
        //        return false;
        //}
       
        //
 
 
 
        private void bclk_Click(object sender, EventArgs e)
        {
            //保存列宽
            DBUtility.Xt_BaseBillFun.SaveGrid(grdMain, this.Name);
        }
 
        private void mrlk_Click(object sender, EventArgs e)
        {
            DBUtility.Xt_BaseBillFun.DefaultGridView(grdMain, this.Name);
        }
        //时间控件
        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            if (HWHID != 0)
            {
                Display();
            }
        }
     
        //初始化GRID
        private void initGrid()
        {
            grdMain.RowTemplate.Height = 18;
            grdMain.RowTemplate.MinimumHeight = 18;
            grdMain.ColumnHeadersHeight = 40;
            grdMain.RowHeadersVisible = false;
            grdMain.ColumnHeadersVisible = true;
            grdMain.ColumnCount = 0;
            grdMain.RowCount = 0;
            //格式化
            //表格设置
            grdMain.RowsDefaultCellStyle.SelectionForeColor = Color.White;
            grdMain.RowsDefaultCellStyle.SelectionBackColor = Color.DarkCyan;
            grdMain.DefaultCellStyle.SelectionBackColor = Color.DarkCyan;
            grdMain.DefaultCellStyle.SelectionForeColor = Color.White;
            grdMain.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            grdMain.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grdMain.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            grdMain.ReadOnly = false;
            grdMain.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing; //允许用户调整列宽
            grdMain.EnableHeadersVisualStyles = false;     //网格标题 样式
            grdMain.AllowUserToAddRows = false;             //是否允许自增--否
            grdMain.AllowUserToDeleteRows = false;          //是否允许删除--否 
            grdMain.AllowUserToResizeColumns = true;         //允许调整列宽--是
            grdMain.AllowUserToResizeRows = false;           //允许调整行高--否
            grdMain.AllowUserToOrderColumns = true;         //允许 拖拽列
            grdMain.BackgroundColor = Color.White;
            grdMain.GridColor = Color.LightGray;             //网格线颜色
            grdMain.ShowCellToolTips = true;
            grdMain.CellBorderStyle = DataGridViewCellBorderStyle.Raised;
            //grdMain.ColumnHeadersDefaultCellStyle.BackColor = Color.AliceBlue;
            //
            DBUtility.ClsPub.GetGridView(grdMain, this.Name, DBUtility.ClsPub.AppPath);//设置列宽
        }
        //显示列表
        private void Display()
        {
            SQLHelper.ClsCN SubCn = new SQLHelper.ClsCN();
            DataSet DSet;
            string sSql = "";
            string sWhere = "";
            //刷新树形
            //LoadTree();
            //过滤条件
 
            //sSql = "Select * from " + ViewName + "  order by " + ModName + "代码";
            sSql = "exec h_p_Kf_InitStockBill " + HWHID;
            //执行SQL
            DSet = SubCn.RunProcReturn(sSql, ViewName);
            //生成首行标题
            //
            for (int i = 0; i < grdMain.ColumnCount; i++)
            {
                grdMain.Columns[i].Frozen = false;
            }
            //
            grdMain.DataSource = DSet.Tables[0].DefaultView;
 
            //设置可编辑列和合计列
 
            int HMaterNumberCol = Fun_GetCol("物料代码");
            int HMaterNameCol = Fun_GetCol("物料名称");
            int HMaterModelCol = Fun_GetCol("规格型号");
            int HUnitNameCol = Fun_GetCol("单位");
            int HPriceCol = Fun_GetCol("单价");
            int HQtyCol = Fun_GetCol("数量");
            int HMoneyCol = Fun_GetCol("金额");
            grdMain.Columns[HMaterNumberCol].ReadOnly = true;
            grdMain.Columns[HMaterNameCol].ReadOnly = true;
            grdMain.Columns[HMaterModelCol].ReadOnly = true;
            grdMain.Columns[HUnitNameCol].ReadOnly = true;
            double HQty=0;
            double HMoney=0;
            double HPrice = 0;
            for (int i = 0; i < grdMain.RowCount; i++)
            {
                HQty = DBUtility.ClsPub.isDoule(grdMain.Rows[i].Cells[HQtyCol].Value);
                HMoney = DBUtility.ClsPub.isDoule(grdMain.Rows[i].Cells[HMoneyCol].Value);
                HPrice = DBUtility.ClsPub.isDoule(grdMain.Rows[i].Cells[HPriceCol].Value);
                if (HQty == 0)
                {
                    grdMain.Rows[i].Cells[HPriceCol].Value = 0;
                }
                else
                {
                     HPrice= HMoney / HQty;
                     grdMain.Rows[i].Cells[HPriceCol].Value = HPrice;
                }
                grdMain.Rows[i].Cells[HMoneyCol].Value = HMoney;
                grdMain.Rows[i].Cells[HQtyCol].Value = HQty;
            }
            //加载列宽,隐藏列
            DBUtility.Gy_BaseFun.DisplayGrid(grdMain, this.Name);
        }
 
        #endregion
 
        private void bc_Click(object sender, EventArgs e)
        {
            save();
        }
        private void save()
        {
            int HMaterIDCol = Fun_GetCol("hmaterid");
            int HUnitIDCol = Fun_GetCol("hunitid");
            int HBatchNoCol = Fun_GetCol("批次");
            int HQtyCol = Fun_GetCol("数量");
            int HMoneyCol = Fun_GetCol("金额");
            int HRemarkCol = Fun_GetCol("备注");
            int HMaterID = 0;
            int HUnitID = 0;
            string HBatchNo = "";
            double HQty = 0;
            double HPrice = 0;
            double HMoney = 0;
            string HRemark = "";
            int HSpID = 0;
            double HReceiveQty = 0;
            double HSendQty = 0;
            double HEndQty = 0;
            double HReceiveBal = 0;
            double HSendBal = 0;
            double HEndBal = 0;
            int sYear = 0;
            int sPeriod = 0;
            DBUtility.ClsPub.Sub_GetPeriodByDate(DBUtility.ClsPub.isDate(DBUtility.ClsPub.GetServerDate(-1), 0), ref sYear, ref sPeriod);
            oCn.RunProc("delete Kf_InitStockBill where hwhid=" + HWHID);
            for (int i = 0; i <= grdMain.RowCount - 1; i++)
            {
                if (DBUtility.ClsPub.isLong(grdMain.Rows[i].Cells[HQtyCol].Value) != 0)
                {
                    //
                    HMaterID = DBUtility.ClsPub.isInt(grdMain.Rows[i].Cells[HMaterIDCol].Value);
                    HUnitID = DBUtility.ClsPub.isInt(grdMain.Rows[i].Cells[HUnitIDCol].Value);
                    HQty = DBUtility.ClsPub.isDoule(grdMain.Rows[i].Cells[HQtyCol].Value);
                    HMoney = DBUtility.ClsPub.isDoule(grdMain.Rows[i].Cells[HMoneyCol].Value);
                    HBatchNo = DBUtility.ClsPub.isStrNull(grdMain.Rows[i].Cells[HBatchNoCol].Value);
                    HRemark = DBUtility.ClsPub.isStrNull(grdMain.Rows[i].Cells[HRemarkCol].Value);
                    //
                    HSpID = 0;
                    HReceiveQty = 0;
                    HSendQty = 0;
                    HEndQty = 0;
                    HReceiveBal = 0;
                    HSendBal = 0;
                    HEndBal = 0;
                    oCn.RunProc("Insert into Kf_InitStockBill " +
                    " (HYear,HPeriod,HBillType,HDate,HMaterID,HUnitID,HWHID" +
                    ",HBeginQty,HBeginBal,HMaker,HMakeDate" +
                    ",HSpID,HReceiveQty,HSendQty,HEndQty,HReceiveBal,HSendBal,HEndBal" +
                    ",HRemark,HBatchNo)" +
                    " values("
                    + sYear + "," + sPeriod + ",'1299','" + DBUtility.ClsPub.GetServerDate(-1) + "'," + HMaterID.ToString() + "," + HUnitID.ToString() + "," + HWHID.ToString() +
                    "," + HQty.ToString() + "," + HMoney.ToString() + ",'" + DBUtility.ClsPub.CurUserName + "','" + DBUtility.ClsPub.GetServerDate(-1) +
                    "'," + HSpID.ToString() + "," + HReceiveQty.ToString() + "," + HSendQty.ToString() + "," + HEndQty.ToString() + "," + HReceiveBal.ToString() + "," + HSendBal.ToString() + "," + HEndBal.ToString() +
                     ",'" + HRemark + "','" + HBatchNo + "') ");
                }
 
            }
        }
        private void grdMain_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            
            int HPriceCol = Fun_GetCol("单价");
            int HQtyCol = Fun_GetCol("数量");
            int HMoneyCol = Fun_GetCol("金额");
            double HPrice = DBUtility.ClsPub.isDoule(grdMain.Rows[e.RowIndex].Cells[HPriceCol].Value);
            double HQty = DBUtility.ClsPub.isDoule(grdMain.Rows[e.RowIndex].Cells[HQtyCol].Value);
            double HMoney = DBUtility.ClsPub.isDoule(grdMain.Rows[e.RowIndex].Cells[HMoneyCol].Value);
            if (e.ColumnIndex == HPriceCol)
            {
                HMoney = HPrice * HQty;
                grdMain.Rows[e.RowIndex].Cells[HMoneyCol].Value = HMoney.ToString();
            }
            else if (e.ColumnIndex == HMoneyCol)
            {
                if (HQty == 0)
                {
                    grdMain.Rows[e.RowIndex].Cells[HPriceCol].Value = 0;
                }
                else
                {
                     HPrice = HMoney / HQty;
                     grdMain.Rows[e.RowIndex].Cells[HPriceCol].Value = HPrice.ToString();
                }
            }
        }
 
    }
}