/// <summary>
        /// 插入发票项目记录
        /// </summary>
        /// <param name="MODELCHARGEINVOICEPRINT"></param>
        /// <returns></returns>
        public bool InsertDetail(ModelMeter_WorkResolveFee_Invoice_Detail ModelMeter_WorkResolveFee_Invoice_Detail)
        {
            StringBuilder str = new StringBuilder();

            str.Append("INSERT INTO Meter_WorkResolveFee_Invoice_Detail(InvoicePrintID,DetailIndex,FeeItem,FeeItemInvoiceTitle,");
            str.Append("Quantity,TaxPercent,TaxMoney,TotalMoney,Units,Price) ");

            str.Append("VALUES(@InvoicePrintID,@DetailIndex,@FeeItem,@FeeItemInvoiceTitle,");
            str.Append("@Quantity,@TaxPercent,@TaxMoney,@TotalMoney,@Units,@Price) ");
            SqlParameter[] para =
            {
                new SqlParameter("@InvoicePrintID",      SqlDbType.VarChar,  30),
                new SqlParameter("@DetailIndex",         SqlDbType.Int),
                new SqlParameter("@FeeItem",             SqlDbType.VarChar,  30),
                new SqlParameter("@FeeItemInvoiceTitle", SqlDbType.VarChar,  30),
                new SqlParameter("@Quantity",            SqlDbType.Decimal),
                new SqlParameter("@TaxPercent",          SqlDbType.Decimal),
                new SqlParameter("@TaxMoney",            SqlDbType.Decimal),
                new SqlParameter("@TotalMoney",          SqlDbType.Decimal),
                new SqlParameter("@Units",               SqlDbType.VarChar,  30),
                new SqlParameter("@Price",               SqlDbType.Decimal)
            };
            para[0].Value = ModelMeter_WorkResolveFee_Invoice_Detail.InvoicePrintID;
            para[1].Value = ModelMeter_WorkResolveFee_Invoice_Detail.DetailIndex;
            para[2].Value = ModelMeter_WorkResolveFee_Invoice_Detail.FeeItem;
            para[3].Value = ModelMeter_WorkResolveFee_Invoice_Detail.FeeItemInvoiceTitle;
            para[4].Value = ModelMeter_WorkResolveFee_Invoice_Detail.Quantity;
            para[5].Value = ModelMeter_WorkResolveFee_Invoice_Detail.TaxPercent;
            para[6].Value = ModelMeter_WorkResolveFee_Invoice_Detail.TaxMoney;
            para[7].Value = ModelMeter_WorkResolveFee_Invoice_Detail.TotalMoney;
            para[8].Value = ModelMeter_WorkResolveFee_Invoice_Detail.Units;
            para[9].Value = ModelMeter_WorkResolveFee_Invoice_Detail.Price;

            if (DBUtility.DbHelperSQL.ExecuteSql(str.ToString(), para) > 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        private void toolPrint_Click(object sender, EventArgs e)
        {
            if (dgList.Rows.Count == 0)
            {
                return;
            }
            dgList.EndEdit();

            if (cmbBatch.SelectedValue == null || cmbBatch.SelectedValue == DBNull.Value)
            {
                mes.Show("请选择发票批次!");
                cmbBatch.Focus();
                return;
            }
            if (txtInvoiceNO.Text.Trim() == "")
            {
                mes.Show("请输入发票号码!");
                txtInvoiceNO.Focus();
                return;
            }
            else
            {
                if (txtInvoiceNO.Text.Length > 8)
                {
                    mes.Show("发票号码长度应小于9位,请确定发票号是否正确!");
                    txtInvoiceNO.Focus();
                    return;
                }
                if (!Information.IsNumeric(txtInvoiceNO.Text))
                {
                    mes.Show("发票号码只能由数字组成!");
                    txtInvoiceNO.Focus();
                    return;
                }
            }
            txtInvoiceNO.Text = txtInvoiceNO.Text.PadLeft(8, '0');

            for (int i = 0; i < dgList.Rows.Count; i++)
            {
                object obj = dgList.Rows[i].Cells["Quantity"].Value;
                if (!Information.IsNumeric(obj))
                {
                    mes.Show("第 " + (i + 1).ToString() + "行 数量不正确,请重新填写!");
                    return;
                }
                else if (Convert.ToDecimal(obj) == 0)
                {
                    mes.Show("第 " + (i + 1).ToString() + "行 数量为零,请重新填写!");
                    return;
                }

                obj = dgList.Rows[i].Cells["Price"].Value;
                if (!Information.IsNumeric(obj))
                {
                    mes.Show("第 " + (i + 1).ToString() + "行 单价不正确,请重新填写!");
                    return;
                }
                else if (Convert.ToDecimal(obj) == 0)
                {
                    mes.Show("第 " + (i + 1).ToString() + "行 单价为零,请重新填写!");
                    return;
                }

                obj = dgList.Rows[i].Cells["Fee"].Value;
                if (!Information.IsNumeric(obj))
                {
                    mes.Show("第 " + (i + 1).ToString() + "行 金额(含税)不正确,请重新填写!");
                    return;
                }
                else if (Convert.ToDecimal(obj) == 0)
                {
                    mes.Show("第 " + (i + 1).ToString() + "行 金额(含税)为零,请重新填写!");
                    return;
                }

                obj = dgList.Rows[i].Cells["TaxPercent"].Value;
                if (!Information.IsNumeric(obj))
                {
                    mes.Show("第 " + (i + 1).ToString() + "行 税率不正确,请重新填写!");
                    return;
                }
                else if (Convert.ToDecimal(obj) == 0)
                {
                    mes.Show("第 " + (i + 1).ToString() + "行 税率为零,请重新填写!");
                    return;
                }
            }

            #region 检查起始发票号是否可用
            DataTable dtCheckInvoiceExists = BllMeter_WorkResolveFee_Invoice_Detail.QueryMeterWorkInvoice(" AND INVOICENO='" + txtInvoiceNO.Text + "' AND INVOICEBATCHID='" + cmbBatch.SelectedValue.ToString() + "' AND INVOICECANCEL<>'3' ");
            if (dtCheckInvoiceExists.Rows.Count > 0)
            {
                if (mes.ShowQ("发票批次为'" + cmbBatch.Text + "'票号为'" + txtInvoiceNO.Text + "'的发票已使用,确定使用此发票号打印吗?") != DialogResult.OK)
                {
                    txtInvoiceNO.Focus();
                    return;
                }
            }
            #endregion

            //发票开具初始化
            bool isSuccess = FPInfoInit(this.Handle);
            if (!isSuccess)
            {
                mes.Show("发票开具初始化失败,请重试!");
                return;
            }
            StringBuilder strInvTypeCode1 = new StringBuilder();
            StringBuilder strInvNumber1   = new StringBuilder();
            bool          ret             = GetFPInfo(strInvTypeCode1, strInvNumber1);
            if (ret)
            {
                if (strInvNumber1.ToString() != txtInvoiceNO.Text)
                {
                    mes.Show("当前发票号与税控系统发票号不一致,请设置新的发票号!" + Environment.NewLine + "设置发票号:" + txtInvoiceNO.Text + Environment.NewLine + "系统发票号:" + strInvNumber1);
                    txtInvoiceNO.SelectAll();
                    return;
                }
            }
            else
            {
                mes.Show("获取税控系统发票号失败,请重试!");
                return;
            }

            #region 打印发票
            try
            {
                if (AddFPData(txtWaterUserName.Text, txtWaterUserFPTaxNO.Text, txtWaterUserBankAccount.Text, txtWaterUserAddress.Text, strCompanyBankNameAndAccountNO,
                              strCompanyAddressAndTel, txtMemo.Text, strLoginName, strCompanyChecker, strCompanyPayee, null, 2, 0))
                {
                    if (MXInfoInit())
                    {
                        for (int i = 0; i < dgList.Rows.Count; i++)
                        {
                            double dbNumber   = Convert.ToDouble(dgList.Rows[i].Cells["Quantity"].Value);
                            double dbSumMoney = Convert.ToDouble(dgList.Rows[i].Cells["Fee"].Value);

                            int intTaxPercent = (int)(Convert.ToDecimal(dgList.Rows[i].Cells["TaxPercent"].Value) * 100);

                            string strUnit            = "";
                            string strFeeInvoiceTitle = "";
                            object obj = dgList.Rows[i].Cells["InvoiceTitle"].Value;
                            if (obj != null && obj != DBNull.Value)
                            {
                                strFeeInvoiceTitle = obj.ToString();
                            }

                            obj = dgList.Rows[i].Cells["Units"].Value;
                            if (obj != null && obj != DBNull.Value)
                            {
                                strUnit = obj.ToString();
                            }

                            if (AddMXData(strFeeInvoiceTitle, "", strUnit, dbNumber, 0, dbSumMoney, intTaxPercent, 1, 0))
                            {
                            }
                            else
                            {
                                mes.Show("添加第 " + (i + 1).ToString() + "行 发票明细'" + strFeeInvoiceTitle + "'错误,请重试!");
                                return;
                            }
                        }
                        StringBuilder strInvTypeCode = new StringBuilder();
                        StringBuilder strInvNumber   = new StringBuilder();
                        if (!FPInvoice(strInvTypeCode, strInvNumber))
                        {
                            mes.Show("发票填开错误,请重试!");
                            return;
                        }
                        bool isOK = CloseInvKey();

                        try
                        {
                            //发票打印成功后,开始处理数据库部分
                            ModelMeter_WorkResolveFee_Invoice ModelMeter_WorkResolveFee_Invoice = new ModelMeter_WorkResolveFee_Invoice();
                            ModelMeter_WorkResolveFee_Invoice.InvoicePrintID   = GETTABLEID.GetTableID(strLoginID, "Meter_WorkResolveFee_Invoice");
                            ModelMeter_WorkResolveFee_Invoice.ChargeID         = ChargeID;
                            ModelMeter_WorkResolveFee_Invoice.InvoiceBatchID   = cmbBatch.SelectedValue.ToString();
                            ModelMeter_WorkResolveFee_Invoice.InvoiceBatchName = cmbBatch.Text;
                            ModelMeter_WorkResolveFee_Invoice.InvoiceNO        = txtInvoiceNO.Text;
                            ModelMeter_WorkResolveFee_Invoice.WaterUserID      = strWaterUserID;
                            ModelMeter_WorkResolveFee_Invoice.WaterUserName    = txtWaterUserName.Text;
                            ModelMeter_WorkResolveFee_Invoice.WaterUserAddress = txtWaterUserAddress.Text;
                            ModelMeter_WorkResolveFee_Invoice.WaterUserFPTaxNO = txtWaterUserFPTaxNO.Text;
                            ModelMeter_WorkResolveFee_Invoice.WaterUserFPBankNameAndAccountNO = txtWaterUserBankAccount.Text;
                            ModelMeter_WorkResolveFee_Invoice.Table_Name_CH                 = strTable_Name_CH;
                            ModelMeter_WorkResolveFee_Invoice.InvoiceFeeDepID               = strFeeDepID;
                            ModelMeter_WorkResolveFee_Invoice.InvoiceFeeDepName             = strFeeDepName;
                            ModelMeter_WorkResolveFee_Invoice.InvoiceTotalFeeMoney          = Convert.ToDecimal(txtSumMoney.Text);
                            ModelMeter_WorkResolveFee_Invoice.CompanyName                   = strCompanyName;
                            ModelMeter_WorkResolveFee_Invoice.CompanyAddress                = strCompanyAddressAndTel;
                            ModelMeter_WorkResolveFee_Invoice.CompanyFPTaxNO                = strCompanyTaxNO;
                            ModelMeter_WorkResolveFee_Invoice.CompanyFPBankNameAndAccountNO = strCompanyBankNameAndAccountNO;
                            ModelMeter_WorkResolveFee_Invoice.Payee   = strCompanyPayee;
                            ModelMeter_WorkResolveFee_Invoice.Checker = strCompanyChecker;
                            ModelMeter_WorkResolveFee_Invoice.InvoicePrintWorkerID = strLoginID;
                            ModelMeter_WorkResolveFee_Invoice.InvoicePrintWorker   = strLoginName;
                            ModelMeter_WorkResolveFee_Invoice.InvoiceType          = rbNormal.Checked ? "1" : "2"; //普通发票还是专用发票
                            ModelMeter_WorkResolveFee_Invoice.Memo = txtMemo.Text;
                            if (BllMeter_WorkResolveFee_Invoice_Detail.Insert(ModelMeter_WorkResolveFee_Invoice))
                            {
                                try
                                {
                                    for (int i = 0; i < dgList.Rows.Count; i++)
                                    {
                                        ModelMeter_WorkResolveFee_Invoice_Detail ModelMeter_WorkResolveFee_Invoice_Detail = new ModelMeter_WorkResolveFee_Invoice_Detail();
                                        ModelMeter_WorkResolveFee_Invoice_Detail.InvoicePrintID      = ModelMeter_WorkResolveFee_Invoice.InvoicePrintID;
                                        ModelMeter_WorkResolveFee_Invoice_Detail.DetailIndex         = i + 1;
                                        ModelMeter_WorkResolveFee_Invoice_Detail.FeeItem             = dgList.Rows[i].Cells["FeeItem"].Value.ToString();
                                        ModelMeter_WorkResolveFee_Invoice_Detail.FeeItemInvoiceTitle = dgList.Rows[i].Cells["InvoiceTitle"].Value.ToString();
                                        object objUnit = dgList.Rows[i].Cells["Units"].Value;
                                        if (objUnit != null && objUnit != DBNull.Value)
                                        {
                                            ModelMeter_WorkResolveFee_Invoice_Detail.Units = objUnit.ToString();
                                        }
                                        ModelMeter_WorkResolveFee_Invoice_Detail.Quantity   = decimal.Parse(dgList.Rows[i].Cells["Quantity"].Value.ToString());
                                        ModelMeter_WorkResolveFee_Invoice_Detail.Price      = decimal.Parse(dgList.Rows[i].Cells["Price"].Value.ToString());
                                        ModelMeter_WorkResolveFee_Invoice_Detail.TaxPercent = decimal.Parse(dgList.Rows[i].Cells["TaxPercent"].Value.ToString());
                                        ModelMeter_WorkResolveFee_Invoice_Detail.TaxMoney   = decimal.Parse(dgList.Rows[i].Cells["TaxMoney"].Value.ToString());
                                        ModelMeter_WorkResolveFee_Invoice_Detail.TotalMoney = decimal.Parse(dgList.Rows[i].Cells["Fee"].Value.ToString());
                                        if (BllMeter_WorkResolveFee_Invoice_Detail.InsertDetail(ModelMeter_WorkResolveFee_Invoice_Detail))
                                        {
                                        }
                                        else
                                        {
                                            BllMeter_WorkResolveFee_Invoice_Detail.Delete(ModelMeter_WorkResolveFee_Invoice.InvoicePrintID);
                                            mes.Show("插入发票项目明细失败,请重试");
                                            return;
                                        }
                                    }
                                    try
                                    {
                                        decimal decSumInvoiceSumFee = 0;

                                        string strGetSumFee = @"SELECT SUM(InvoiceTotalFeeMoney) FROM Meter_WorkResolveFee_Invoice WHERE CHARGEID='" + ChargeID + "'";
                                        object objSumFee    = BllMeter_WorkResolveFee_Invoice_Detail.GetSingle(strGetSumFee);
                                        if (Information.IsNumeric(objSumFee))
                                        {
                                            decSumInvoiceSumFee = Convert.ToDecimal(objSumFee);
                                        }

                                        if (decSumInvoiceSumFee >= decWorkResolveSumFee)
                                        {
                                            string strUpdateInvoiceSign = "UPDATE Meter_Charge SET INVOICEPRINTSIGN='1' WHERE CHARGEID='" + ChargeID + "'";
                                            int    intRows = BllMeter_WorkResolveFee_Invoice_Detail.ExcuteSQL(strUpdateInvoiceSign);
                                            if (intRows == 0)
                                            {
                                                mes.Show("更新发票标志失败,请重新打印发票!");
                                                return;
                                            }
                                            toolPrint.Enabled       = false;
                                            labInvoiceState.Visible = true;
                                            ((FrmFinance_Invoice_Print)Owner).isModify = true;
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        mes.Show("更新发票标志失败,原因:" + ex.Message);
                                        log.Write(ex.ToString(), MsgType.Error);
                                        BllMeter_WorkResolveFee_Invoice_Detail.Delete(ModelMeter_WorkResolveFee_Invoice.InvoicePrintID);
                                        return;
                                    }
                                }
                                catch (Exception ex)
                                {
                                    mes.Show("插入发票项目明细失败,原因:" + ex.Message);
                                    log.Write(ex.ToString(), MsgType.Error);
                                    BllMeter_WorkResolveFee_Invoice_Detail.Delete(ModelMeter_WorkResolveFee_Invoice.InvoicePrintID);
                                    return;
                                }
                            }
                            else
                            {
                                mes.Show("插入发票记录失败,请补登发票号!");
                                return;
                            }
                        }
                        catch (Exception ex)
                        {
                            mes.Show("插入发票记录失败,请补登发票号!\n原因:" + ex.Message);
                            log.Write(ex.ToString(), MsgType.Error);
                            return;
                        }
                    }
                    else
                    {
                        mes.Show("发票明细初始化错误,请重试!");
                        return;
                    }
                }
                else
                {
                    mes.Show("添加发票基础信息错误,请重试!");
                    return;
                }
            }
            catch (Exception ex)
            {
                mes.Show("发票打印错误:" + ex.Message);
                log.Write(ex.ToString(), MsgType.Error);
                return;
            }
            #endregion
        }