private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (cmbUsers.Text == string.Empty)
                {
                    toolTip1.Show("请选择领用人!", cmbUsers, 0, cmbUsers.Height, 2000);
                    return;
                }
                if (CBoxUsed.Text == string.Empty)
                {
                    toolTip1.Show("请选择使用人!", CBoxUsed, 0, CBoxUsed.Height, 2000);
                    return;
                }
                if (cmbCustomers.Text == string.Empty)
                {
                    toolTip1.Show("请选择客户名称!", cmbCustomers, 0, cmbCustomers.Height, 2000);
                    return;
                }
                if (cmbCard.Text == string.Empty)
                {
                    toolTip1.Show("请选择消费卡!", cmbCard, 0, cmbCard.Height, 2000);
                    return;
                }
                if (txtMealMoney.Text.Trim() == string.Empty)
                {
                    toolTip1.Show("请输入消费金额!", txtMealMoney, 0, txtMealMoney.Height, 2000);
                    return;
                }
                Decimal mealMoney = 0, maxMoney = 0, cardBalance = 0;
                if (decimal.TryParse(txtMealMoney.Text, out mealMoney) == false)
                {
                    toolTip1.Show("请输入正确的消费金额", txtMealMoney, 0, txtMealMoney.Height, 2000);
                    return;
                }
                maxMoney    = Convert.ToDecimal(txtMaxSum.Text);
                cardBalance = Convert.ToDecimal(txtCardBalance.Text);

                //if(mId!=-1 || oldCardId !=Convert.ToInt64(cmbCard.SelectedValue))
                //{
                //    //新增或换消费卡
                //    if (mealMoney > cardBalance)
                //    {
                //        toolTip1.Show("消费金额不能大于消费卡内剩余金额", txtCardBalance, 0, txtCardBalance.Height, 2000);
                //        return;
                //    }
                //}
                //else
                //{
                //    if (mealMoney-oldMealMoney > cardBalance)
                //    {
                //        toolTip1.Show("消费金额不能大于消费卡内剩余金额加原消费额", txtCardBalance, 0, txtCardBalance.Height, 2000);
                //        return;
                //    }
                //}


                long   userid     = Convert.ToInt64(cmbUsers.SelectedValue);
                string userRight  = "";
                var    mUserTable = new T_UsersTableAdapter().GetDataById(userid);
                if (mUserTable.Rows.Count > 0)
                {
                    var mUserRow = mUserTable[0];
                    userRight = mUserRow.OperRight.Trim();
                }
                else
                {
                    MessageBox.Show("查找用户权限失败!");
                    return;
                }


                if (cmbCard.Text == "现金")
                {
                    if (mealMoney - oldMealMoney > maxMoney && (!Common.AuthenticateRight.AuthOperation(110609) && !CommonClass.SttUser.blSuperUser))
                    {
                        toolTip1.Show("消费金额不能大于剩余额度", txtMealMoney, 0, txtMealMoney.Height, 2000);
                        return;
                    }
                }


                //修改日期2016/8/8   添加超额提醒
                if ((maxMoney - mealMoney) < -5000)
                {
                    DialogResult dr;
                    dr = MessageBox.Show("剩余额度已超5000元,是否继续报销", "超额提醒", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
                    if (dr == DialogResult.No)
                    {
                        return;
                    }
                }


                //数据库操作
                #region

                using (SqlConnection mConn = new SqlConnection(Common.CommonClass.SqlConnStr))
                {
                    mConn.Open();
                    using (SqlTransaction mTrans = mConn.BeginTransaction())
                    {
                        try
                        {
                            if (mId < 0)
                            {
                                //插入
                                var adapter = new T_MealsTableAdapter();
                                adapter.Connection  = mConn;
                                adapter.Transaction = mTrans;
                                int    ret    = 0;
                                string status = "现金";
                                if (cmbCard.Text != "现金")
                                {
                                    status = "已提交等待领导审核";
                                }
                                //添加餐费报销记录

                                ret = adapter.Insert(dtpDataDate.Value.Date, Convert.ToInt64(cmbUsers.SelectedValue), Convert.ToInt64(cmbCustomers.SelectedValue), Convert.ToInt64(cmbCard.SelectedValue), mealMoney, Classes.PubClass.UserId, status, Convert.ToInt64(CBoxUsed.SelectedValue));
                                if (ret > 0)
                                {
                                    //var adapter1 = new T_CardsTableAdapter();
                                    //adapter1.Connection = mConn;
                                    //adapter1.Transaction = mTrans;

                                    //ret = adapter1.ExpenseById(mealMoney, Convert.ToInt64(cmbCard.SelectedValue));
                                    if (ret > 0)
                                    {
                                        mTrans.Commit();
                                        MessageBox.Show("插入成功");
                                        btnSave.Enabled = false;
                                    }
                                    else
                                    {
                                        mTrans.Rollback();
                                        MessageBox.Show("插入失败:2");
                                        return;
                                    }
                                }
                                else
                                {
                                    mTrans.Rollback();
                                    MessageBox.Show("插入失败:1");
                                    return;
                                }
                            }
                            else
                            {
                                //修改
                                var adapter = new T_MealsTableAdapter();
                                adapter.Connection  = mConn;
                                adapter.Transaction = mTrans;
                                int  ret       = 0;
                                long newCardId = Convert.ToInt64(cmbCard.SelectedValue);
                                ret = adapter.UpdateById(dtpDataDate.Value.Date, Convert.ToInt64(cmbUsers.SelectedValue), Convert.ToInt64(cmbCustomers.SelectedValue), newCardId, mealMoney, Classes.PubClass.UserId, Convert.ToInt64(CBoxUsed.SelectedValue), mId);

                                if (ret > 0)
                                {
                                    //修改已完成的申请
                                    if (oldStatus == "已还卡" || oldStatus == "现金")
                                    {
                                        if (oldCardId != newCardId || oldMealMoney != mealMoney)
                                        {
                                            var adapter1 = new T_CardsTableAdapter();
                                            adapter1.Connection  = mConn;
                                            adapter1.Transaction = mTrans;
                                            if (oldCardName != "现金")
                                            {
                                                //旧消费方式为卡
                                                ret = adapter1.ExpenseById(-oldMealMoney, oldCardId);
                                            }
                                            if (ret > 0)
                                            {
                                                if (cmbCard.Text != "现金")
                                                {
                                                    //新消费方式为消费卡
                                                    ret = adapter1.ExpenseById(mealMoney, newCardId);
                                                    adapter.UpdateStatus("已还卡", mId);
                                                }
                                                else
                                                {
                                                    adapter.UpdateStatus("现金", mId);
                                                }
                                                if (ret > 0)
                                                {
                                                    mTrans.Commit();
                                                    MessageBox.Show("修改成功");
                                                    btnSave.Enabled = false;
                                                    return;
                                                }
                                                else
                                                {
                                                    mTrans.Rollback();
                                                    MessageBox.Show("修改失败:3");
                                                    return;
                                                }
                                            }
                                            else
                                            {
                                                mTrans.Rollback();
                                                MessageBox.Show("修改失败:2");
                                                return;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        //未完成的申请
                                        mTrans.Commit();
                                        MessageBox.Show("修改成功");
                                        btnSave.Enabled = false;
                                        return;
                                    }

                                    //}
                                }
                                else
                                {
                                    mTrans.Rollback();
                                    MessageBox.Show("修改失败:1");
                                    return;
                                }
                            }
                        }


                        catch (Exception ex)
                        {
                            mTrans.Rollback();
                            MessageBox.Show(ex.Message);
                            return;
                        }
                    }
                }


                #endregion
            }



            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }
示例#2
0
        private void dgvGiftStockOut_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
            {
                return;
            }

            if (e.ColumnIndex == dgvGiftStockOut.Columns["ColDel"].Index)
            {
                //删除

                if (dgvGiftStockOut.Rows[e.RowIndex].Cells["statusDataGridViewTextBoxColumn"].Value.ToString() == "已领卡")
                {
                    MessageBox.Show("该消费卡尚未归还,不能删除!");
                    return;
                }
                if (MessageBox.Show("确认要删除该条记录吗?", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) != DialogResult.OK)
                {
                    return;
                }

                using (SqlConnection mConn = new SqlConnection(Common.CommonClass.SqlConnStr))
                {
                    mConn.Open();
                    using (SqlTransaction mTrans = mConn.BeginTransaction())
                    {
                        try
                        {
                            var adapter = new T_MealsTableAdapter();
                            adapter.Connection  = mConn;
                            adapter.Transaction = mTrans;

                            int ret = 0;

                            ret = adapter.MyDelete(Convert.ToInt64(dgvGiftStockOut.Rows[e.RowIndex].Cells["idDataGridViewTextBoxColumn"].Value));

                            if (ret > 0)
                            {
                                if (dgvGiftStockOut.Rows[e.RowIndex].Cells["statusDataGridViewTextBoxColumn"].Value.ToString() == "已还卡") //已还卡的
                                {
                                    var adapter1 = new T_CardsTableAdapter();
                                    adapter1.Connection  = mConn;
                                    adapter1.Transaction = mTrans;

                                    ret = adapter1.ExpenseById(-Convert.ToDecimal(dgvGiftStockOut.Rows[e.RowIndex].Cells["mealMoneyDataGridViewTextBoxColumn"].Value), Convert.ToInt64(dgvGiftStockOut.Rows[e.RowIndex].Cells["cardid"].Value));
                                }
                                if (ret > 0)
                                {
                                    mTrans.Commit();
                                    MessageBox.Show("删除成功");
                                    mConn.Close();
                                    btnSearch_Click(sender, new EventArgs());
                                    return;
                                }
                                else
                                {
                                    mTrans.Rollback();
                                    MessageBox.Show("删除失败:2");
                                    return;
                                }
                            }
                            else
                            {
                                mTrans.Rollback();
                                MessageBox.Show("删除失败:1");
                                return;
                            }
                        }
                        catch (Exception ex)
                        {
                            mTrans.Rollback();
                            MessageBox.Show(ex.Message);
                            return;
                        }
                    }
                }
            }
            if (e.ColumnIndex == dgvGiftStockOut.Columns["ColModify"].Index)
            {
                //修改
                using (var mForm = new 申请餐费报销())
                {
                    mForm.mId         = Convert.ToInt64(dgvGiftStockOut.Rows[e.RowIndex].Cells["idDataGridViewTextBoxColumn"].Value);
                    mForm.oldStatus   = dgvGiftStockOut.Rows[e.RowIndex].Cells["statusDataGridViewTextBoxColumn"].Value.ToString();
                    mForm.oldCardName = dgvGiftStockOut.Rows[e.RowIndex].Cells["cardIdDataGridViewTextBoxColumn"].Value.ToString();
                    mForm.ShowDialog();
                    btnSearch_Click(sender, new EventArgs());
                }
            }
            if (e.ColumnIndex == dgvGiftStockOut.Columns["ColGetCard"].Index)
            {
                //领卡
                if (dgvGiftStockOut.Rows[e.RowIndex].Cells["statusDataGridViewTextBoxColumn"].Value.ToString() != "审核已通过" || dgvGiftStockOut.Rows[e.RowIndex].Cells["cardIdDataGridViewTextBoxColumn"].Value.ToString() == "现金")
                {
                    return;
                }

                long cardId = Convert.ToInt64(dgvGiftStockOut.Rows[e.RowIndex].Cells["CardId"].Value);
                var  mRow   = (销售管理.DAL.DataSetCards.T_CardsRow) new T_CardsTableAdapter().GetDataById(cardId).Rows[0];
                if (mRow.UserName.Trim() != string.Empty)
                {
                    MessageBox.Show("卡已被" + mRow.UserName + "借出");
                    return;
                }

                using (SqlConnection mConn = new SqlConnection(Common.CommonClass.SqlConnStr))
                {
                    mConn.Open();
                    using (SqlTransaction mTrans = mConn.BeginTransaction())
                    {
                        try
                        {
                            var adapter = new T_MealsTableAdapter();
                            adapter.Connection  = mConn;
                            adapter.Transaction = mTrans;

                            int ret = adapter.UpdateStatus("已领卡", Convert.ToInt64(dgvGiftStockOut.Rows[e.RowIndex].Cells["idDataGridViewTextBoxColumn"].Value));

                            if (ret > 0)
                            {
                                var adapter1 = new T_CardsTableAdapter();
                                adapter1.Connection  = mConn;
                                adapter1.Transaction = mTrans;

                                ret = adapter1.UpdateUserName(Convert.ToString(dgvGiftStockOut.Rows[e.RowIndex].Cells["userIdDataGridViewTextBoxColumn"].Value), cardId);
                                if (ret > 0)
                                {
                                    mTrans.Commit();
                                    mConn.Close();
                                    MessageBox.Show("领卡成功");
                                    btnSearch_Click(sender, new EventArgs());
                                    return;
                                }
                                else
                                {
                                    mTrans.Rollback();
                                    MessageBox.Show("领卡失败:2");
                                    return;
                                }
                            }
                            else
                            {
                                mTrans.Rollback();
                                MessageBox.Show("领卡失败:1");
                                return;
                            }
                        }
                        catch (Exception ex)
                        {
                            mTrans.Rollback();
                            MessageBox.Show(ex.Message);
                            return;
                        }
                    }
                }
            }


            //还卡金额录入
            if (e.ColumnIndex == dgvGiftStockOut.Columns["ColBackCard"].Index)
            {
                //还卡
                if (dgvGiftStockOut.Rows[e.RowIndex].Cells["statusDataGridViewTextBoxColumn"].Value.ToString() != "已领卡")
                {
                    MessageBox.Show("还未领卡!");
                    return;
                }

                using (var mForm = new 输入消费金额())
                {
                    long     mId    = Convert.ToInt64(dgvGiftStockOut.Rows[e.RowIndex].Cells["idDataGridViewTextBoxColumn"].Value);
                    decimal  mMoney = 0;
                    DateTime mBackDate;
                    if (mForm.ShowDialog() == DialogResult.OK)
                    {
                        mMoney    = mForm.mMoney;
                        mBackDate = mForm.mBackTime;


                        //修改日期:2016/8/8 修改内容:添加超额提示
                        #region
                        String username; //销售姓名
                        String userid;   //销售编号
                        String maxMoney; //销售剩余额度
                        username = dgvGiftStockOut.Rows[e.RowIndex].Cells["IntUsedID"].Value.ToString();
                        if (username == String.Empty)
                        {
                            MessageBox.Show("获取‘使用人’失败!");
                            return;
                        }
                        String sql = string.Format(@"SELECT id FROM dbo.T_Users WHERE UserName='******'", username);
                        userid = SqlHelper.ExecuteScalar(sql).ToString();
                        string mSql = string.Format(@"select ss.salesum - gs.GiftSum-ms.mealsum from (select isnull(sum(summoney)/200,0) salesum from T_Saledetails where saledate between (convert(varchar(4),getdate(),120) + '-01-01') and (convert(varchar(4),getdate(),120) + '-12-31') and username={0}) ss,(select isnull(sum(GiftSum),0) giftsum from t_GiftStockOut where userid={0} and datadate between (convert(varchar(4),getdate(),120) + '-01-01') and (convert(varchar(4),getdate(),120) + '-12-31') and status not in('已删除','审核未通过')) gs,(select isnull(sum(mealmoney),0) mealsum from t_meals where userid={0} and datadate between (convert(varchar(4),getdate(),120) + '-01-01') and (convert(varchar(4),getdate(),120) + '-12-31') and status <>'已删除') ms", userid);
                        maxMoney = SqlHelper.ExecuteScalar(mSql).ToString();
                        if ((Convert.ToDecimal(maxMoney) - Convert.ToDecimal(mMoney)) <= -5000)
                        {
                            DialogResult dr;
                            dr = MessageBox.Show("剩余额度已超5000元,是否继续报销", "超额提醒", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
                            if (dr == DialogResult.No)
                            {
                                return;
                            }
                        }

                        #endregion


                        using (SqlConnection mConn = new SqlConnection(Common.CommonClass.SqlConnStr))
                        {
                            mConn.Open();
                            using (SqlTransaction mTrans = mConn.BeginTransaction())
                            {
                                try
                                {
                                    var adapter = new T_MealsTableAdapter();
                                    adapter.Connection  = mConn;
                                    adapter.Transaction = mTrans;

                                    int ret = adapter.BackCard(mBackDate, mMoney, mId);

                                    if (ret > 0)
                                    {
                                        var adapter1 = new T_CardsTableAdapter();
                                        adapter1.Connection  = mConn;
                                        adapter1.Transaction = mTrans;
                                        long mCardId = Convert.ToInt64(dgvGiftStockOut.Rows[e.RowIndex].Cells["CardId"].Value);

                                        ret = adapter1.BackCard(mMoney, mCardId);
                                        if (ret > 0)
                                        {
                                            mTrans.Commit();
                                            MessageBox.Show("还卡成功");
                                            btnSearch_Click(sender, new EventArgs());
                                            return;
                                        }
                                        else
                                        {
                                            mTrans.Rollback();
                                            MessageBox.Show("还卡失败:2");
                                            return;
                                        }
                                    }
                                    else
                                    {
                                        mTrans.Rollback();
                                        MessageBox.Show("还卡失败:1");
                                        return;
                                    }
                                }
                                catch (Exception ex)
                                {
                                    mTrans.Rollback();
                                    MessageBox.Show(ex.Message);
                                    return;
                                }
                            }
                        }
                    }
                }
            }
        }