private void btnBuyMaterial_Click(object sender, EventArgs e)
        {
            if (dgvStudents.SelectedRows.Count <= 0)
                return;
            DataGridViewRow row = dgvStudents.SelectedRows[0];

            BuyMaterialsForm frmBuyMaterials = new BuyMaterialsForm();
            frmBuyMaterials.StudentName = txtName.Text;
            frmBuyMaterials.Balance = txtBalance.Text;
            if(frmBuyMaterials.ShowDialog() != DialogResult.OK)
                return;
            // 1> 添加购买记录
            materialsCostTableAdapter.Insert(int.Parse(txtID.Text), frmBuyMaterials.MaterialID,
                frmBuyMaterials.MaterialPrice, frmBuyMaterials.BuyCount, frmBuyMaterials.TotalCost,
                DateTime.Now, User.CurrentUser.UserName);
            // 2> 更新balance
            int balance = int.Parse(txtBalance.Text);
            balance -= frmBuyMaterials.TotalCost;
            studentsTableAdapter.UpdateBalance(balance, int.Parse(txtID.Text));
            txtBalance.Text = balance.ToString();
            row.Cells["Balance"].Value = balance;

            if (MessageBox.Show("需要打印发票吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                List<PrintRecord> list = new List<PrintRecord>();
                PrintRecord prAmount = new PrintRecord();
                prAmount.id = 1;
                prAmount.name = frmBuyMaterials.MaterialName;
                prAmount.num = frmBuyMaterials.BuyCount+"";
                prAmount.price = frmBuyMaterials.MaterialPrice+"";
                prAmount.amount = frmBuyMaterials.TotalCost + "";
                list.Add(prAmount);

                InvoicePrintingForm printForm = new InvoicePrintingForm();
                printForm.MdiParent = this.Owner;
                printForm.StartPosition = FormStartPosition.CenterScreen;
                printForm.isReadOnly = true;
                printForm.userName = txtName.Text;
                printForm.recordList = list;
                printForm.totalAmount = frmBuyMaterials.TotalCost;
                printForm.Show();
            }
        }
        private bool SetPrintRecord()
        {
            try
            {
                totalAmount = float.Parse(txtTotal.Text);
            }
            catch (Exception)
            {
                totalAmount = 0;
            }

            if (totalAmount > 99999.99)
            {
                MessageBox.Show("发票金额不能大于99999.99元", "发票金额不能大于99999.99元", MessageBoxButtons.OK,
                    MessageBoxIcon.Warning);
                return false;
            }
            if (totalAmount > 0)
            {
                totalAmountStr = ConvertNum(totalAmount);
            }
            else {
                MessageBox.Show("发票金额不能小于等于0元", "发票金额不能小于等于0元", MessageBoxButtons.OK,
                   MessageBoxIcon.Warning);
                return false;
            }

            if (!isReadOnly)
            {
                recordList = null;
                formatPrint = null;
            }
            formatPrint = new FormatPrint();
            formatPrint.loadPrintTemplate("PrtTemplate.xml");
            topList = formatPrint.topList;
            totalList = formatPrint.totalList;
            bottomList = formatPrint.bottomList;

            // 票头
            formatPrint.SetPrintPointValue("userName", txtUserName.Text, topList, 0);
            formatPrint.SetPrintPointValue("year", DateTime.Now.Year.ToString(), topList, 0);
            formatPrint.SetPrintPointValue("month", DateTime.Now.Month.ToString(), topList, 0);
            formatPrint.SetPrintPointValue("day", DateTime.Now.Day.ToString(), topList, 0);

            if (recordList == null) {
                recordList = new List<PrintRecord>();
                for (int m = 1; m <= 4; m++) {
                    PrintRecord pr = new PrintRecord();
                    pr.id = m;
                    TextBox txtName = GetTextBoxByName("txtName" + m);
                    pr.name = "".Equals(txtName.Text) ? "" : txtName.Text;
                    TextBox txtNum = GetTextBoxByName("txtNum" + m);
                    pr.num = "".Equals(txtNum.Text) ? "" : txtNum.Text;
                    TextBox txtPrice = GetTextBoxByName("txtPrice" + m);
                    pr.price = "".Equals(txtPrice.Text) ? "" : Math.Round(float.Parse(txtPrice.Text),2).ToString("0.00");
                    TextBox txtAmount = GetTextBoxByName("txtAmount" + m);
                    pr.amount = "".Equals(txtAmount.Text) ? "" : txtAmount.Text;
                    recordList.Add(pr);
                }
            }

            int j = 1;
            foreach (PrintRecord pr in recordList)
            {
                // 内容
                formatPrint.SetPrintPointValue("name", pr.name, null, j);
                formatPrint.SetPrintPointValue("num", pr.num + "", null, j);
                formatPrint.SetPrintPointValue("price", pr.price, null, j);
                if (!"".Equals(pr.amount)) {
                    formatPrint.SetPrintPointValue("unit", "元", null, j);
                    string amountStr = Math.Round(float.Parse(pr.amount),2).ToString("00000.00").Replace(".","");
                    formatPrint.SetPrintPointValue("amountWan", amountStr.Substring(0, 1), null, j);
                    formatPrint.SetPrintPointValue("amountQian", amountStr.Substring(1, 1), null, j);
                    formatPrint.SetPrintPointValue("amountBai", amountStr.Substring(2, 1), null, j);
                    formatPrint.SetPrintPointValue("amountShi", amountStr.Substring(3, 1), null, j);
                    formatPrint.SetPrintPointValue("amountYuan", amountStr.Substring(4, 1), null, j);
                    formatPrint.SetPrintPointValue("amountJiao", amountStr.Substring(5, 1), null, j);
                    formatPrint.SetPrintPointValue("amountFen", amountStr.Substring(6, 1), null, j);
                }
                formatPrint.SetPrintPointValue("remark", GetTextBoxByName("txtRemark" + j).Text, null, j);
                j++;
            }

            if (!"".Equals(totalAmountStr))
            {
                formatPrint.SetPrintPointValue("totalWan", totalAmountStr.Substring(0, 1), totalList, 0);
                formatPrint.SetPrintPointValue("totalQian", totalAmountStr.Substring(1, 1), totalList, 0);
                formatPrint.SetPrintPointValue("totalBai", totalAmountStr.Substring(2, 1), totalList, 0);
                formatPrint.SetPrintPointValue("totalShi", totalAmountStr.Substring(3, 1), totalList, 0);
                formatPrint.SetPrintPointValue("totalYuan", totalAmountStr.Substring(4, 1), totalList, 0);
                formatPrint.SetPrintPointValue("totalJiao", totalAmountStr.Substring(5, 1), totalList, 0);
                formatPrint.SetPrintPointValue("totalFen", totalAmountStr.Substring(6, 1), totalList, 0);
            }
            formatPrint.SetPrintPointValue("totalNum", totalAmount.ToString(), totalList, 0);

            // 票尾
            formatPrint.SetPrintPointValue("head", txtHead.Text, bottomList, 0);
            formatPrint.SetPrintPointValue("accounting", txtAccounting.Text, bottomList, 0);
            formatPrint.SetPrintPointValue("cashier", txtCashier.Text, bottomList, 0);

            return true;
        }
        private void btnExtendSignUp_Click(object sender, EventArgs e)
        {
            if (dgvStudentCourses.SelectedRows.Count <= 0)
                return;
            int rowIndex = dgvStudents.CurrentRow.Index;
            DataGridViewRow row = dgvStudentCourses.SelectedRows[0];

            // Get course id;
            int selectedStudentCourseIndex = GetSelectedStudentCourseIndex();
            int studentCourseId = studentsDataSet.StudentCourses.Rows[selectedStudentCourseIndex].Field<int>("ID");
            int courseID = studentsDataSet.StudentCourses.Rows[selectedStudentCourseIndex].Field<int>("CourseID");
            int studentId = int.Parse(txtID.Text);

            CourseExtendForm frmCourseExtend = new CourseExtendForm();
            frmCourseExtend.StudentName = txtName.Text;
            frmCourseExtend.Balance = txtBalance.Text;

            frmCourseExtend.CurrentExpireDate = row.Cells["ExpireTimeColumn"].Value.ToString();
            frmCourseExtend.CourseType = row.Cells["CourseTypeColumn"].Value.ToString();
            frmCourseExtend.CourseSubtype = row.Cells["CourseSubtypeColumn"].Value.ToString();
            frmCourseExtend.CourseName = row.Cells["CourseNameColumn"].Value.ToString();
            frmCourseExtend.ChargeType = row.Cells["ChargeTypeColumn"].Value.ToString();
            frmCourseExtend.ChargeAmount = row.Cells["ChargeAmountColumn"].Value.ToString();

            if (frmCourseExtend.ShowDialog() != DialogResult.OK)
                return;

            // 续报班:
            // 1> 扣除余额
            int balance = int.Parse(txtBalance.Text);
            balance -= frmCourseExtend.ActualCost;
            studentsTableAdapter.UpdateBalance(balance, int.Parse(txtID.Text));
            studentCoursesTableAdapter.UpdateBalance(frmCourseExtend.TotalCost, frmCourseExtend.signUpTime, studentId, courseID);
            txtBalance.Text = balance.ToString();

            // 2> 添加扣钱记录
            studentCostTableAdapter1.InsertCostWithExpireTime(int.Parse(txtID.Text), courseID, frmCourseExtend.TotalCost, DateTime.Now, 100,
                frmCourseExtend.ActualCost, frmCourseExtend.DiscountReason, User.CurrentUser.UserName, frmCourseExtend.ExpireDate);

            // 3> 更新课程到期时间
            studentCoursesTableAdapter.UpdateExpireTime(frmCourseExtend.ExpireDate, studentCourseId);

            // 4> 更新学生的学费过期时间,看哪个过期时间更近
            studentsTableAdapter.UpdateExpireTime(frmCourseExtend.ExpireDate, int.Parse(txtID.Text));

            // 5> 刷新显示
            studentCoursesTableAdapter.Fill(studentsDataSet.StudentCourses, studentId);
            if (this.UserType == 0)
            {
                // 只加载被授权的学生
                this.studentsTableAdapter.FillByUserName(this.studentsDataSet.Students, this.UserName);
            }
            else
            {
                // 加载所有的学生
                this.studentsTableAdapter.FillByStatus(this.studentsDataSet.Students);
            }
            if (rowIndex >= 0)
            {
                this.dgvStudents.Rows[rowIndex].Selected = true;
                this.dgvStudents.CurrentCell = this.dgvStudents.Rows[rowIndex].Cells[0];
            }

            if (MessageBox.Show("需要打印发票吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                List<PrintRecord> list = new List<PrintRecord>();
                PrintRecord prAmount = new PrintRecord();
                prAmount.id = 1;
                prAmount.name = frmCourseExtend.CourseName + "学费";
                prAmount.num = "1";
                prAmount.price = frmCourseExtend.ActualCost.ToString();
                prAmount.amount = frmCourseExtend.ActualCost.ToString();
                prAmount.remark = "续报";
                list.Add(prAmount);

                InvoicePrintingForm printForm = new InvoicePrintingForm();
                printForm.MdiParent = this.Owner;
                printForm.StartPosition = FormStartPosition.CenterScreen;
                printForm.isReadOnly = true;
                printForm.userName = txtName.Text;
                printForm.recordList = list;
                printForm.totalAmount = frmCourseExtend.ActualCost;
                printForm.Show();
            }
        }
        private void btnCharge_Click(object sender, EventArgs e)
        {
            if (dgvStudents.SelectedRows.Count <= 0)
                return;

            int studentId = int.Parse(txtID.Text);

            GetDepositAmountForm frmGetDepositAmount = new GetDepositAmountForm();
            frmGetDepositAmount.StudentName = txtName.Text;
            frmGetDepositAmount.CardNo = txtCardNo.Text;
            frmGetDepositAmount.CardType = txtCardType.Text;
            if (frmGetDepositAmount.ShowDialog() != DialogResult.OK)
                return;

            int depositAmount = frmGetDepositAmount.DepositAmount;
            int paidAmount = frmGetDepositAmount.PaidAmount;
            int tuitionAmount = frmGetDepositAmount.tuitionAmount;
            int materialsAmount = frmGetDepositAmount.materialsAmount;
            int otherAmount = frmGetDepositAmount.otherAmount;

            DataGridViewRow row = dgvStudents.SelectedRows[0];

            ConfirmDepositForm frmConfirmDeposit = new ConfirmDepositForm();
            frmConfirmDeposit.DepositAmount = depositAmount.ToString();
            frmConfirmDeposit.PaidAmount = paidAmount.ToString();
            frmConfirmDeposit.TuitionAmount = tuitionAmount.ToString();
            frmConfirmDeposit.MaterialsAmount = materialsAmount.ToString();
            frmConfirmDeposit.OtherAmount = otherAmount.ToString();
            frmConfirmDeposit.StudentName = txtName.Text;
            frmConfirmDeposit.StudentSex = row.Cells["SexColumn"].Value.ToString();
            frmConfirmDeposit.StudentBirthday = row.Cells["BirthdayColumn"].Value.ToString();
            frmConfirmDeposit.StudentAddress = row.Cells["AddressColumn"].Value.ToString();
            frmConfirmDeposit.StudentPhone = row.Cells["PhoneColumn"].Value.ToString();
            frmConfirmDeposit.StudentFartherName = row.Cells["FartherNameColumn"].Value.ToString();
            frmConfirmDeposit.StudentFartherPhone = row.Cells["FartherTelColumn"].Value.ToString();
            frmConfirmDeposit.StudentFartherWork = row.Cells["FartherWorkColumn"].Value.ToString();
            frmConfirmDeposit.StudentMotherName = row.Cells["MotherNameColumn"].Value.ToString();
            frmConfirmDeposit.StudentMotherPhone = row.Cells["MotherTelColumn"].Value.ToString();
            frmConfirmDeposit.StudentMotherWork = row.Cells["MotherWorkColumn"].Value.ToString();
            frmConfirmDeposit.Text = "确定为“" + txtName.Text + "”充值" + depositAmount + "元吗?";
            if (frmConfirmDeposit.ShowDialog() != DialogResult.OK)
                return;

            depositListTableAdapter.Insert(studentId, depositAmount, DateTime.Now, paidAmount, User.CurrentUser.UserName);

            int newBalance = int.Parse(txtBalance.Text) + depositAmount;
            studentsTableAdapter.UpdateBalance(newBalance, studentId);

            txtBalance.Text = newBalance.ToString();
            row.Cells["Balance"].Value = txtBalance.Text;

            if (MessageBox.Show("需要打印发票吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) {
                List<PrintRecord> list = new List<PrintRecord>();
                if (tuitionAmount == 0 && materialsAmount == 0 && otherAmount == 0)
                {
                    PrintRecord prAmount = new PrintRecord();
                    prAmount.id = 1;
                    prAmount.name = "充值";
                    prAmount.num = "1";
                    prAmount.price = paidAmount.ToString();
                    prAmount.amount = paidAmount.ToString();
                    list.Add(prAmount);
                }
                else {
                    if (tuitionAmount != 0)
                    {
                        PrintRecord prTuition = new PrintRecord();
                        prTuition.id = 1;
                        prTuition.name = "学费";
                        prTuition.num = "1";
                        prTuition.price = tuitionAmount.ToString();
                        prTuition.amount = tuitionAmount.ToString();
                        list.Add(prTuition);
                    }

                    if (materialsAmount != 0)
                    {
                        PrintRecord prMaterials = new PrintRecord();
                        prMaterials.id = 2;
                        prMaterials.name = "材料费";
                        prMaterials.num = "1";
                        prMaterials.price = materialsAmount.ToString();
                        prMaterials.amount = materialsAmount.ToString();
                        list.Add(prMaterials);
                    }

                    if (otherAmount != 0)
                    {
                        PrintRecord prOther = new PrintRecord();
                        prOther.id = 3;
                        prOther.name = "其他";
                        prOther.num = "1";
                        prOther.price = otherAmount.ToString();
                        prOther.amount = otherAmount.ToString();
                        list.Add(prOther);
                    }
                }

                InvoicePrintingForm printForm = new InvoicePrintingForm();
                printForm.MdiParent = this.Owner;
                printForm.StartPosition = FormStartPosition.CenterScreen;
                printForm.isReadOnly = true;
                printForm.userName = txtName.Text;
                printForm.recordList = list;
                printForm.totalAmount = depositAmount;
                printForm.Show();
            }
        }