Ejemplo n.º 1
0
        /// <summary>
        /// 确定
        /// </summary>
        protected void OKButton_Click(object sender, EventArgs e)
        {
            var totalPer = Util.ToDecimal(SCH_YF.Text.Trim()) + Util.ToDecimal(SCH_JD.Text.Trim()) + Util.ToDecimal(SCH_TH.Text.Trim()) + Util.ToDecimal(SCH_ZB.Text.Trim());

            if (totalPer != 100)
            {
                this.ShowErrorMessage("比例输入不正确,请确保各项总额为100!");
                return;
            }

            var signDate     = LocalGlobal.ConvertDateFormat(SIGNDATE.Text.Trim()).ToString("yyyyMMdd");
            var deliverDate  = LocalGlobal.ConvertDateFormat(DELIVERYDATE.Text.Trim()).ToString("yyyyMMdd");
            var deliverDateS = LocalGlobal.ConvertDateFormat(DELIVERYDATE.Text.Trim()).ToString("yyyy-MM-dd");
            var dateModel    = LocalGlobal.GetDateModel();

            SqlConnection con = LocalGlobal.DbConnect();

            con.Open();
            SqlTransaction tran = con.BeginTransaction();//使用事务
            SqlCommand     cmd  = new SqlCommand();

            cmd.Connection  = con;
            cmd.Transaction = tran;

            try
            {
                using (ContractProvider contProvider = new ContractProvider())
                {
                    string stno = string.Empty, whCode = string.Empty;
                    if (CurrentType == "2")           //直接开票
                    {
                        stno = LocalGlobal.NewSTNo(); //发货单号

                        var wh = contProvider.GetBaseParam("WH", "1");
                        if (wh != null && wh.Rows.Count > 0)
                        {
                            whCode = wh.Rows[0]["description"].ToString().Trim();
                        }
                    }

                    string orno = string.Empty;
                    if (this.CurrentID.Length == 0)  //新增
                    {
                        orno = LocalGlobal.NewOrno(false);

                        #region 合同、收款进度头表

                        cmd.CommandText = contProvider.InsertContractSql(orno, ORDNAME.Text.Trim(), CUSTNO.Text.Trim(), CUSTNAME.Text.Trim(), CURR.SelectedValue.Trim(), signDate, deliverDate, PROTECTTERM.SelectedValue.Trim(), SCH_YF.Text.Trim(), SCH_JD.Text.Trim(), SCH_TH.Text.Trim(), SCH_ZB.Text.Trim(), ORDAMT.Text.Trim(), Remark.Text.Trim(), LocalGlobal.CurrentUserID, dateModel.DateStr, dateModel.TimeStr);
                        cmd.ExecuteNonQuery();

                        cmd.CommandText = contProvider.InsertArprocessSql(orno, ORDNAME.Text.Trim(), CUSTNO.Text.Trim(), CUSTNAME.Text.Trim(), CURR.SelectedValue.Trim(), ORDAMT.Text.Trim(), LocalGlobal.CurrentUserID, dateModel.DateStr, dateModel.TimeStr);
                        cmd.ExecuteNonQuery();

                        #endregion

                        #region 收款进度明细

                        cmd.CommandText = contProvider.InsertArprocessDetailSql(orno, "T1", Util.ToDecimal(ORDAMT.Text.Trim()), Util.ToDecimal(SCH_YF.Text.Trim()), LocalGlobal.CurrentUserID, dateModel.DateStr, dateModel.TimeStr);
                        cmd.ExecuteNonQuery();
                        cmd.CommandText = contProvider.InsertArprocessDetailSql(orno, "T2", Util.ToDecimal(ORDAMT.Text.Trim()), Util.ToDecimal(SCH_JD.Text.Trim()), LocalGlobal.CurrentUserID, dateModel.DateStr, dateModel.TimeStr);
                        cmd.ExecuteNonQuery();
                        cmd.CommandText = contProvider.InsertArprocessDetailSql(orno, "T3", Util.ToDecimal(ORDAMT.Text.Trim()), Util.ToDecimal(SCH_TH.Text.Trim()), LocalGlobal.CurrentUserID, dateModel.DateStr, dateModel.TimeStr);
                        cmd.ExecuteNonQuery();
                        cmd.CommandText = contProvider.InsertArprocessDetailSql(orno, "T4", Util.ToDecimal(ORDAMT.Text.Trim()), Util.ToDecimal(SCH_ZB.Text.Trim()), LocalGlobal.CurrentUserID, dateModel.DateStr, dateModel.TimeStr);
                        cmd.ExecuteNonQuery();
                        cmd.CommandText = contProvider.InsertArprocessDetailSql(orno, "T5", Util.ToDecimal(ORDAMT.Text.Trim()), 0, LocalGlobal.CurrentUserID, dateModel.DateStr, dateModel.TimeStr);
                        cmd.ExecuteNonQuery();

                        #endregion
                    }
                    else   //编辑
                    {
                        orno = ORDNO.Text.Trim();

                        #region 合同、收款进度头表

                        cmd.CommandText = contProvider.UpdateContractSql(orno, ORDNAME.Text.Trim(), CUSTNO.Text.Trim(), CUSTNAME.Text.Trim(), CURR.SelectedValue.Trim(), signDate, deliverDate, PROTECTTERM.SelectedValue.Trim(), SCH_YF.Text.Trim(), SCH_JD.Text.Trim(), SCH_TH.Text.Trim(), SCH_ZB.Text.Trim(), ORDAMT.Text.Trim(), Remark.Text.Trim(), LocalGlobal.CurrentUserID, dateModel.DateStr, dateModel.TimeStr);
                        cmd.ExecuteNonQuery();

                        cmd.CommandText = contProvider.UpdateArprocessSql(orno, ORDNAME.Text.Trim(), CUSTNO.Text.Trim(), CUSTNAME.Text.Trim(), CURR.SelectedValue.Trim(), ORDAMT.Text.Trim(), LocalGlobal.CurrentUserID, dateModel.DateStr, dateModel.TimeStr);
                        cmd.ExecuteNonQuery();

                        #endregion

                        #region 收款进度明细

                        cmd.CommandText = contProvider.UpdateArprocessDetailSql(orno, "T1", Util.ToDecimal(ORDAMT.Text.Trim()), Util.ToDecimal(SCH_YF.Text.Trim()), LocalGlobal.CurrentUserID, dateModel.DateStr, dateModel.TimeStr);
                        cmd.ExecuteNonQuery();
                        cmd.CommandText = contProvider.UpdateArprocessDetailSql(orno, "T2", Util.ToDecimal(ORDAMT.Text.Trim()), Util.ToDecimal(SCH_JD.Text.Trim()), LocalGlobal.CurrentUserID, dateModel.DateStr, dateModel.TimeStr);
                        cmd.ExecuteNonQuery();
                        cmd.CommandText = contProvider.UpdateArprocessDetailSql(orno, "T3", Util.ToDecimal(ORDAMT.Text.Trim()), Util.ToDecimal(SCH_TH.Text.Trim()), LocalGlobal.CurrentUserID, dateModel.DateStr, dateModel.TimeStr);
                        cmd.ExecuteNonQuery();
                        cmd.CommandText = contProvider.UpdateArprocessDetailSql(orno, "T4", Util.ToDecimal(ORDAMT.Text.Trim()), Util.ToDecimal(SCH_ZB.Text.Trim()), LocalGlobal.CurrentUserID, dateModel.DateStr, dateModel.TimeStr);
                        cmd.ExecuteNonQuery();

                        #endregion
                    }

                    if (CurrentType == "2") //直接开票
                    {
                        cmd.CommandText = contProvider.DeleteKpSql(orno);
                        cmd.ExecuteNonQuery();
                        cmd.CommandText = contProvider.DeleteShipSql(orno);
                        cmd.ExecuteNonQuery();
                        cmd.CommandText = contProvider.DeleteTransDetailSql(orno);
                        cmd.ExecuteNonQuery();
                    }

                    #region 合同明细

                    cmd.CommandText = contProvider.DeleteContractDetailSql(orno);
                    cmd.ExecuteNonQuery();

                    //bool flag = true;
                    //if (List.Rows.Count == 1) //只有一行数据,判断是否是空行
                    //{
                    //    var name = ((DataBoundLiteralControl)List.Rows[0].Cells[2].Controls[0]).Text.Trim();
                    //    if (name.Length == 0) //自动创建的空行
                    //    {
                    //        flag = false;
                    //    }
                    //}
                    //if (flag)
                    //{
                    //    for (int i = 0; i < List.Rows.Count; i++)
                    //    {
                    //        var row = List.Rows[i];
                    //        cmd.CommandText = contProvider.InsertContractDetailSql(orno, ((Label)row.FindControl("SEQ")).Text.Trim(), ((DataBoundLiteralControl)row.Cells[2].Controls[0]).Text.Trim(), ((DataBoundLiteralControl)row.Cells[4].Controls[0]).Text.Trim(), ((DataBoundLiteralControl)row.Cells[3].Controls[0]).Text.Trim(), ((DataBoundLiteralControl)row.Cells[5].Controls[0]).Text.Trim(), ((DataBoundLiteralControl)row.Cells[6].Controls[0]).Text.Trim(), ((DataBoundLiteralControl)row.Cells[7].Controls[0]).Text.Trim(), ((DataBoundLiteralControl)row.Cells[8].Controls[0]).Text.Trim());

                    //        cmd.ExecuteNonQuery();
                    //    }
                    //}

                    var table = ViewState["ViewDT"] as DataTable;
                    int seq   = 0;
                    foreach (DataRow row in table.Rows)
                    {
                        seq++;
                        cmd.CommandText = contProvider.InsertContractDetailSql(orno, seq.ToString(), row["ITEMNO"].ToString().Trim(), row["ORDQTY"].ToString(), row["DRAWNO"].ToString(), row["UM"].ToString(), row["UNITPRICE"].ToString(), row["AMT"].ToString(), row["Remark"].ToString());

                        cmd.ExecuteNonQuery();

                        #region 直接开票

                        if (CurrentType == "2") //直接开票
                        {
                            //1.扣减库存,写出库记录
                            cmd.CommandText = contProvider.InsertShipSql(stno, seq.ToString(), orno, row["DRAWNO"].ToString().Trim(), CUSTNO.Text.Trim(), CUSTNAME.Text.Trim(), row["ITEMNO"].ToString(), row["UM"].ToString(), row["ORDQTY"].ToString(), row["ORDQTY"].ToString(), row["ORDQTY"].ToString(), deliverDate, "1");
                            cmd.ExecuteNonQuery();

                            var invTable = contProvider.GetInventoryByItem(whCode, row["DRAWNO"].ToString().Trim());
                            if (invTable != null && invTable.Rows.Count > 0)
                            {
                                cmd.CommandText = contProvider.UpdateInventorySql(whCode, row["DRAWNO"].ToString().Trim(), row["ORDQTY"].ToString());
                                cmd.ExecuteNonQuery();
                            }
                            else
                            {
                                cmd.CommandText = contProvider.InsertInventorySql(whCode, row["DRAWNO"].ToString().Trim(), "", "", row["ITEMNO"].ToString(), row["ORDQTY"].ToString(), row["UM"].ToString());
                                cmd.ExecuteNonQuery();
                            }

                            cmd.CommandText = contProvider.InsertTransDetailSql(orno, row["DRAWNO"].ToString().Trim(), whCode, row["ORDQTY"].ToString(), stno, orno.Substring(0, 7), "B");
                            cmd.ExecuteNonQuery();

                            //2.开票
                            cmd.CommandText = contProvider.InsertKpSql(orno, seq.ToString(), "1", FP.Text.Trim(), row["ORDQTY"].ToString(), row["AMT"].ToString(), deliverDateS);
                            cmd.ExecuteNonQuery();
                        }

                        #endregion
                    }

                    #endregion
                }
            }
            catch (Exception error)
            {
                tran.Rollback();
                this.ShowErrorMessage("保存失败!" + error.Message);
                return;
            }

            tran.Commit();

            //回调
            this.DialogCallback("'CloseRefresh'", "window");
        }