示例#1
0
        private void button2_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.Rows.Count == 0)
            {
                MessageBox.Show("请先选择需要生成的记录");
            }
            else if (comboBox3.Text.ToString().Length <= 0 || comboBox4.Text.ToString().Length <= 0 || comboBox5.Text.ToString().Length <= 0 || comboBox7.Text.ToString().Length <= 0 || comboBox9.Text.ToString().Length <= 0 || comboBox11.Text.ToString().Length <= 0 || comboBox13.Text.ToString().Length <= 0 || comboBox15.Text.ToString().Length <= 0)
            {
                MessageBox.Show("请先设置各类审批人和申请人");
            }
            else
            {
                try
                {
                    String   BID, Name, Invoice; //invoic 如果不为空  需要自动生成一个
                    String   pNmae;              //提供者公司 对应BuyId : provider
                    String   Ename;              //审批者   如果为空选择框中读取  对应BuyId 的person
                    String   Sub;                //总金额  对应BuyId SumMoney
                    String   BDate;              //购买日期   对应BuyId  OrderDate
                    DateTime Temp_BDate;         //转化的过度
                    String   PJID, BAPID, GDID;
                    String   sql1_1, sql1_2, sql1_3, sql5_1, sql4_1;
                    String   sql1, sql2, sql3, sql4, sql5, sql6;
                    String   OrderPerson, Person;
                    String   BIID, ID;
                    String   type, unit;
                    String   Num, CurrentNum; //买入数量和当前库存
                    String   Price;
                    String   PlanID;
                    DateTime PlanDate, GetDate, OrderDate, PlanGetDate, CheckDate; //入库日期是BDate  预计到货 PlanDate = BDate-3   GetDate = BDate-1   OrderDate =  BDate -7
                    DateTime EndDate;                                              //截止日期
                    DataGridViewCheckBoxCell checkCell;                            //PlanGetDate   计划到货时间
                    Boolean flag;
                    Random  r = new Random();
                    for (int row = 0; row <= this.dataGridView1.Rows.Count - 1; row++)
                    {
                        checkCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[row].Cells[0];
                        flag      = Convert.ToBoolean(checkCell.Value);
                        if (flag == false)
                        {
                            continue;
                        }
                        ID          = this.dataGridView1["ID", row].Value.ToString();
                        BID         = this.dataGridView1["BID", row].Value.ToString();
                        Name        = this.dataGridView1["Name", row].Value.ToString();
                        Invoice     = this.dataGridView1["Invoice", row].Value.ToString();
                        pNmae       = this.dataGridView1["PName", row].Value.ToString();
                        Ename       = this.dataGridView1["Ename", row].Value.ToString();
                        Sub         = this.dataGridView1["Sub", row].Value.ToString();
                        BDate       = this.dataGridView1["BDate", row].Value.ToString();
                        Temp_BDate  = Convert.ToDateTime(BDate);  //强制转化
                        PJID        = this.dataGridView1["PJID", row].Value.ToString();
                        type        = this.dataGridView1["Type", row].Value.ToString();
                        unit        = this.dataGridView1["Unit", row].Value.ToString();
                        Num         = this.dataGridView1["Num", row].Value.ToString();
                        Price       = this.dataGridView1["Price", row].Value.ToString();
                        CurrentNum  = this.dataGridView1["CurrentNum", row].Value.ToString();
                        OrderPerson = comboBox3.Text.ToString();   //申请人
                        if (Ename.Length <= 0)
                        {
                            Person = comboBox4.Text.ToString();
                        }
                        else
                        {
                            Person = Ename;
                        }
                        if (Invoice.Length <= 0)
                        {
                            TimeSpan cha = (DateTime.Now - TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)));
                            long     t   = (long)cha.TotalSeconds; //用时间戳生成一个不会重复的表示符
                            Invoice = t.ToString();
                        }
                        if (Sub.Length <= 0)
                        {
                            Sub = "0";
                        }
                        if (CurrentNum.Length <= 0)
                        {
                            CurrentNum = "0";
                        }
                        PlanDate    = Temp_BDate.AddDays(-3);
                        OrderDate   = Temp_BDate.AddDays(-7);
                        GetDate     = Temp_BDate.AddDays(-1);
                        CheckDate   = PlanDate.AddDays(r.Next(0, 1)); //审核日期   0-1
                        PlanGetDate = PlanDate.AddDays(r.Next(2, 3)); //PlanGetDate     计划到货时间  PlanDate 0-3
                        //入库日期是BDate  预计到货 PlanDate = BDate-3   GetDate = BDate-1   OrderDate =  BDate -7
                        EndDate = OrderDate.AddDays(5);               //截止日期是申请日期+1
                        sql1_1  = @"INSERT INTO BuyPlan (PlanDate,Maker,Checker,CheckDate,State,IsInShopping,PlanType) 
                                 VALUES('" + PlanDate + "','" + comboBox5.Text.ToString() + "','" + comboBox7.Text.ToString() + "','" + CheckDate + "','1','3','1')";
                        SQLHelper db1_1 = new SQLHelper();
                        DataTable dt1_1 = db1_1.ExecuteDataTable(sql1_1);

                        sql1_2 = "select top(1) ID from BuyPlan  order by id desc";
                        SQLHelper db1_2 = new SQLHelper();
                        DataTable dt1_2 = db1_2.ExecuteDataTable(sql1_2);
                        PlanID = dt1_2.Rows[0][0].ToString();   //获取planID

                        sql1_3 = @"INSERT INTO BuyPlanDetail (BPID,Name,Type,Unit,MonthNeedAmount,PJDate,PJAmount,LowAmount,TopAmount,NeedAmount,PlanBuyAmount,PlanGetDate,BuyType,Remark,BuyAmount,toothcolor,pjid)
                                 VALUES('" + PlanID + "','" + Name + "','" + type + "','" + unit + "','0','" + PlanDate + "','" + CurrentNum + "','1','0','" + Num + "','" + Num + "','" + PlanGetDate + "','0','库存报警','" + Num + "','','" + PJID + "')";
                        SQLHelper db1_3 = new SQLHelper();
                        DataTable dt1_3 = db1_3.ExecuteDataTable(sql1_3);

                        sql1 = @"INSERT INTO BuyIn (Invoice,OrderPerson,Provider,Person,SumMoney,OrderDate,PlanID,Phone,Fax,Address,PayKind) VALUES 
                            ('" + Invoice + "','" + comboBox3.Text.ToString() + "','" + pNmae + "','" + comboBox4.Text.ToString() + "','" + Sub + "','" + OrderDate + "','" + PlanID + "','','','','')";
                        SQLHelper db = new SQLHelper();
                        DataTable dt = db.ExecuteDataTable(sql1);

                        sql2 = "select top(1) ID from BuyIn  order by id desc";
                        SQLHelper db2 = new SQLHelper();
                        DataTable dt2 = db2.ExecuteDataTable(sql2);
                        BIID = dt2.Rows[0][0].ToString();   //获取BII

                        sql3 = @"INSERT INTO BuyInDetail (BIID,BIName,BItype,BIUnit,BIAmount,Price,PlanDate,GetAmount,pjid,BIArea,Remark,toothcolor) 
                        VALUES ('" + BIID + "','" + Name + "','" + type + "','" + unit + "','" + Num + "','" + Price + "','" + PlanDate + "','" + Num + "','" + PJID + "','','','')";
                        SQLHelper db3 = new SQLHelper();
                        DataTable dt3 = db3.ExecuteDataTable(sql3);

                        sql4 = @"INSERT INTO BuyInGetDetail (BIID,Name,type,unit,Price,GetDate,GetAmount,BID,pjid,Area,Remark,toothcolor,State) VALUES 
                            ('" + BIID + "','" + Name + "','" + type + "','" + unit + "','" + Price + "','" + GetDate + "','" + Num + "','" + BID + "','" + PJID + "','','','','1')";
                        SQLHelper db4 = new SQLHelper();
                        DataTable dt4 = db4.ExecuteDataTable(sql4);

                        sql4_1 = "select top(1) ID from BuyInGetDetail  order by id desc";
                        SQLHelper db4_1 = new SQLHelper();
                        DataTable dt4_1 = db4_1.ExecuteDataTable(sql4_1);
                        GDID = dt4_1.Rows[0][0].ToString();   //获取GDID

                        sql5 = @"INSERT INTO BuyApplyPay(ApplyPerson,ApplyDate,Provider,Person,Bank,BankAccount,SumMoney,endPayDate,GetDate,verify1State,verify1,verify1Date,verify2State,verify2,verify2Date,IsPayed,PayMoney,Remark,PayDate,PayPerson)
                            VALUES('" + comboBox11.Text.ToString() + "','" + OrderDate + "','" + pNmae + "','','','','" + Sub + "','" + EndDate + "','','1','" + comboBox13.Text.ToString() + "','" + CheckDate + "','1','" + comboBox13.Text.ToString() + "','" + CheckDate + "','1','" + Sub + "','','" + CheckDate + "','" + comboBox15.Text.ToString() + "')";
                        SQLHelper db5 = new SQLHelper();
                        DataTable dt5 = db5.ExecuteDataTable(sql5);

                        sql5_1 = "select top(1) ID from BuyApplyPay  order by id desc";
                        SQLHelper db5_1 = new SQLHelper();
                        DataTable dt5_1 = db5_1.ExecuteDataTable(sql5_1);
                        BAPID = dt5_1.Rows[0][0].ToString();   //获取BAPID

                        sql6 = @"INSERT INTO BuyApplyPayDetail(BAPID,GDID,BIID,Name,type,Unit,Price,Area,GetDate,GetAmount,Remark)
                            VALUES('" + BAPID + "','" + GDID + "','" + BIID + "','" + Name + "','" + type + "','" + unit + "','" + Price + "','','" + GetDate + "','" + Num + "','')";
                        SQLHelper db6 = new SQLHelper();
                        DataTable dt6 = db6.ExecuteDataTable(sql6);

                        string newLine = "入库ID:" + ID + "已生成对应的采购记录和采购计划\n";
                        // 将textBox1的内容插入到第一行
                        // 索引0是 richText1 第一行位置
                        richTextBox1.Text = richTextBox1.Text.Insert(0, newLine);
                        richTextBox1.ScrollToCaret();
                    }
                    MessageBox.Show("生成完成");
                    GetGridView();
                }
                catch (Exception e3)
                {
                    WriteLog.Write_Log(e3);
                    MessageBox.Show(e3.Message);
                }
            }
        }