using Lskj.Business.Impl; using Lskj.Control; using Lskj.Control.BrowserSetting; using Lskj.Control.Model; using Lskj.Core; using Lskj.Model; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Windows.Forms; namespace Lskj.PubInvoiceOut { public partial class FrmMain : BaseForm { /// /// 主表数据源sql /// public string MainSourceSql; /// /// 明细数据源sql /// public string DetailSourceSql; /// /// 发票主键值 /// public string PrimaryValue; /// /// 红蓝标识 /// public string InvoiceTag; /// /// 主表 /// private DataTable MainDataTab = new DataTable(); /// /// 明细表 /// private DataTable DetailDataTab = new DataTable(); /// /// 发票流水号 /// private string InvoiceId = ""; /// /// 二维码ID /// private string Rzid = ""; /// /// 是否已经重新登录 /// private bool AgainSendCode = false; /// /// 是否已经重新认证 /// private bool AgainQrCode = false; public FrmMain() { InitializeComponent(); this.Load += OnFrmMain_Load; } /// /// 初始化时 /// /// /// private void OnFrmMain_Load(object sender, EventArgs e) { WaitForm.ShowForm(); try { if (!string.IsNullOrEmpty(MainSourceSql)) { this.MainDataTab = MainImpl.GetDataTableResult(MainSourceSql); } if (!string.IsNullOrEmpty(DetailSourceSql)) { this.DetailDataTab = MainImpl.GetDataTableResult(DetailSourceSql); } if (string.IsNullOrWhiteSpace(InvoiceTag) || !(InvoiceTag + "").Equals("1")) { MainMakeInvoice(); } else { MainMakeRedInvoice(); } } catch (Exception ex) { MessageUtil.Show(ex.Message); } WaitForm.HideForm(); this.DialogResult = DialogResult.OK; this.Close(); this.Dispose(); } /// /// 开蓝票整体方法 /// private void MainMakeInvoice() { string token = string.Empty; if (AlterTable("p_systemtab", "invoiceToken", "varchar(1000)")) { token = SqlHelper.ExecuteScalar("select invoiceToken from p_systemtab") + ""; } ResultModel makeModel = new ResultModel(); if (MakeInvoice(ref makeModel, ref token) || makeModel.Message.Contains("开票中") || makeModel.Message.Contains("编号重复") || makeModel.Message.Contains("编号重复") || makeModel.Message.Contains("手动重推") || makeModel.Message.Contains("开票完成")) { bool isMake = false; if (makeModel.Message.Contains("开票中") || makeModel.Message.Contains("编号重复") || makeModel.Message.Contains("手动重推")) { ResultModel refreshModel = new ResultModel(); RefreshInvoiceResponse(ref refreshModel, ref token, true); if (refreshModel.State.Equals("22")) { ResultModel retryModel = new ResultModel(); //重开 RetryInvoice(ref retryModel, ref token, true); if (retryModel.Code.Equals("9999")) { MessageUtil.Show($"发票重开失败,{retryModel.Message}"); return; } else if (AgainSendCode && (refreshModel.Code.Equals("0000") && !refreshModel.State.Equals("2") && refreshModel.Message.Equals("获取成功") && (refreshModel.Result.Contains("重新登录") || refreshModel.Result.Contains("重新登入")))) { string errReason = ""; try { JArray returnResult = (JArray)JsonConvert.DeserializeObject(refreshModel.Result); errReason = returnResult[0]["errReason"] + ""; } catch (Exception) { errReason = refreshModel.Result; } MessageUtil.Show($"已重开发票\r\n请再次点击更新数据"); return; } else if (AgainQrCode && (refreshModel.Code.Equals("0000") && !refreshModel.State.Equals("2") && refreshModel.Message.Equals("获取成功") && (refreshModel.Result.Contains("重新认证") || refreshModel.Result.Contains("认证")))) { string errReason = ""; try { JArray returnResult = (JArray)JsonConvert.DeserializeObject(refreshModel.Result); errReason = returnResult[0]["errReason"] + ""; } catch (Exception) { errReason = refreshModel.Result; } MessageUtil.Show($"已重开发票\r\n上次开票失败原因为:{errReason}\r\n请再次点击更新数据"); return; } } //this.InvoiceId = MainDataTab.Columns.Contains("O_invoiceId") ? MainDataTab.Rows[0]["O_invoiceId"] + "" : SqlHelper.ExecuteScalar(string.Format("select O_invoiceId from Acc_InvoiceMaintab where XMid = '{0}'", PrimaryValue)) + ""; isMake = true; } while (true) { ResultModel refreshModel = new ResultModel(); RefreshInvoiceResponse(ref refreshModel, ref token); if (refreshModel.Code.Equals("0000") && !refreshModel.State.Equals("2") && refreshModel.Message.Equals("获取成功") && refreshModel.Result.Contains("发票未生成")) { if (isMake) { MessageUtil.Show("发票未生成"); return; } else { continue; } } else if (refreshModel.Code.Equals("0000") && !refreshModel.State.Equals("2") && refreshModel.Message.Equals("获取成功") && (refreshModel.Result.Contains("重新登录") || refreshModel.Result.Contains("重新登入")))//如果需要验证码登录 { WaitForm.HideForm(); FrmSendCode frmSendCode = new FrmSendCode(); frmSendCode.SendCodeCallback += (sender, e) => { ResultModel sendModel = new ResultModel(); SendCode(sendModel, ref token); e.Code = sendModel.Code; e.Message = sendModel.Message; }; frmSendCode.LoginCallback += (sender, e) => { ResultModel sendModel = new ResultModel(); Login(sendModel, frmSendCode.SendCode, ref token); if (sendModel.Code.Equals("0000")) { AgainSendCode = true; } e.Code = sendModel.Code; e.Message = sendModel.Message; }; DialogResult dialogResult = frmSendCode.ShowDialog(); if (dialogResult == DialogResult.OK) { WaitForm.ShowForm(); MainMakeInvoice(); return; } else { MessageUtil.Show("验证失败"); return; } } else if (refreshModel.Code.Equals("0000") && !refreshModel.State.Equals("2") && refreshModel.Message.Equals("获取成功") && (refreshModel.Result.Contains("重新认证") || refreshModel.Result.Contains("认证")))//如果需要扫码 { WaitForm.HideForm(); FrmQrCode frmQrCode = new FrmQrCode(); frmQrCode.OnAttestationCallback += (sender, e) => { ResultModel sendModel = new ResultModel(); Attestation(sendModel, ref token); e.Code = sendModel.Code; e.Message = sendModel.Message; e.Result = sendModel.Result; }; frmQrCode.OnVerifyQrCodeCallback += (sender, e) => { ResultModel sendModel = new ResultModel(); VerifyQrCode(sendModel, ref token); if (sendModel.Code.Equals("0000") && sendModel.Result.Equals("2")) { AgainQrCode = true; } e.Code = sendModel.Code; e.Message = sendModel.Message; e.Result = sendModel.Result; }; DialogResult dialogResult = frmQrCode.ShowDialog(); if (dialogResult == DialogResult.OK) { WaitForm.ShowForm(); MainMakeInvoice(); return; } else { MessageUtil.Show("验证失败"); return; } } else if (refreshModel.Code.Equals("0000") && !refreshModel.State.Equals("2") && refreshModel.Message.Equals("获取成功") && refreshModel.Result.Contains("errReason")) { string errReason = ""; try { JArray returnResult = (JArray)JsonConvert.DeserializeObject(refreshModel.Result); errReason = returnResult[0]["errReason"] + ""; } catch (Exception) { errReason = refreshModel.Result; } MessageUtil.Show(errReason); return; } else { MessageUtil.Show(refreshModel.Message); return; } System.Threading.Thread.Sleep(500); } } else { MessageUtil.Show(makeModel.Message); } } /// /// 开红票整体方法 /// private void MainMakeRedInvoice() { WaitForm.ShowForm(); string token = string.Empty; if (AlterTable("p_systemtab", "invoiceToken", "varchar(1000)")) { token = SqlHelper.ExecuteScalar("select invoiceToken from p_systemtab") + ""; } string hzinvoiceId = SqlHelper.ExecuteScalar($"select O_hzinvoiceId from Acc_InvoiceMaintab where XMid = '{PrimaryValue}'") + ""; if (string.IsNullOrEmpty(hzinvoiceId)) { using (FrmSelectMsg frmSelectMsg = new FrmSelectMsg()) { if (frmSelectMsg.ShowDialog() == DialogResult.OK) { if (frmSelectMsg.SelectModel == null) { MessageUtil.Show("未选择红冲原因"); return; } ResultModel saveModel = new ResultModel(); if (FlushRedInvoice(frmSelectMsg.SelectModel.ValueMember, ref saveModel, ref token)) { hzinvoiceId = SqlHelper.ExecuteScalar($"select O_hzinvoiceId from Acc_InvoiceMaintab where XMid = '{PrimaryValue}'") + ""; if (!string.IsNullOrEmpty(hzinvoiceId)) { MainMakeRedInvoice(); return; } } else if (saveModel.Code.Equals("9999")) { if (saveModel.Code.Equals("9999") && (saveModel.Result.Contains("重新登录") || saveModel.Result.Contains("重新登入")))//如果需要验证码登录 { WaitForm.HideForm(); FrmSendCode frmSendCode = new FrmSendCode(); frmSendCode.SendCodeCallback += (sender, e) => { ResultModel sendModel = new ResultModel(); SendCode(sendModel, ref token); e.Code = sendModel.Code; e.Message = sendModel.Message; }; frmSendCode.LoginCallback += (sender, e) => { ResultModel sendModel = new ResultModel(); Login(sendModel, frmSendCode.SendCode, ref token); if (sendModel.Code.Equals("0000")) { AgainSendCode = true; } e.Code = sendModel.Code; e.Message = sendModel.Message; }; DialogResult dialogResult = frmSendCode.ShowDialog(); if (dialogResult == DialogResult.OK) { WaitForm.ShowForm(); MainMakeRedInvoice(); return; } else { MessageUtil.Show("验证失败"); return; } } else if (saveModel.Code.Equals("9999") && (saveModel.Result.Contains("重新认证") || saveModel.Result.Contains("认证")))//如果需要扫码 { WaitForm.HideForm(); FrmQrCode frmQrCode = new FrmQrCode(); frmQrCode.OnAttestationCallback += (sender, e) => { ResultModel sendModel = new ResultModel(); Attestation(sendModel, ref token); e.Code = sendModel.Code; e.Message = sendModel.Message; e.Result = sendModel.Result; }; frmQrCode.OnVerifyQrCodeCallback += (sender, e) => { ResultModel sendModel = new ResultModel(); VerifyQrCode(sendModel, ref token); if (sendModel.Code.Equals("0000") && sendModel.Result.Equals("2")) { AgainQrCode = true; } e.Code = sendModel.Code; e.Message = sendModel.Message; e.Result = sendModel.Result; }; DialogResult dialogResult = frmQrCode.ShowDialog(); if (dialogResult == DialogResult.OK) { WaitForm.ShowForm(); MainMakeRedInvoice(); return; } else { MessageUtil.Show("验证失败"); return; } } else { MessageUtil.Show(saveModel.Message); return; } } } } } else { while (true) { ResultModel refreshModel = new ResultModel(); RefreshHZInvoiceResponse(hzinvoiceId, ref refreshModel, ref token); if (refreshModel.Code.Equals("0000") && !refreshModel.State.Equals("2") && refreshModel.Message.Equals("获取成功") && refreshModel.Result.Contains("发票未生成")) { MessageUtil.Show("发票未生成"); return; } else if (refreshModel.Code.Equals("0000") && !refreshModel.State.Equals("2") && refreshModel.Message.Equals("获取成功") && (refreshModel.Result.Contains("重新登录") || refreshModel.Result.Contains("重新登入")))//如果需要验证码登录 { WaitForm.HideForm(); FrmSendCode frmSendCode = new FrmSendCode(); frmSendCode.SendCodeCallback += (sender, e) => { ResultModel sendModel = new ResultModel(); SendCode(sendModel, ref token); e.Code = sendModel.Code; e.Message = sendModel.Message; }; frmSendCode.LoginCallback += (sender, e) => { ResultModel sendModel = new ResultModel(); Login(sendModel, frmSendCode.SendCode, ref token); if (sendModel.Code.Equals("0000")) { AgainSendCode = true; } e.Code = sendModel.Code; e.Message = sendModel.Message; }; DialogResult dialogResult = frmSendCode.ShowDialog(); if (dialogResult == DialogResult.OK) { MainMakeRedInvoice(); return; } else { MessageUtil.Show("验证失败"); return; } } else if (refreshModel.Code.Equals("0000") && !refreshModel.State.Equals("2") && refreshModel.Message.Equals("获取成功") && (refreshModel.Result.Contains("重新认证") || refreshModel.Result.Contains("认证")))//如果需要扫码 { WaitForm.HideForm(); FrmQrCode frmQrCode = new FrmQrCode(); frmQrCode.OnAttestationCallback += (sender, e) => { ResultModel sendModel = new ResultModel(); Attestation(sendModel, ref token); e.Code = sendModel.Code; e.Message = sendModel.Message; e.Result = sendModel.Result; }; frmQrCode.OnVerifyQrCodeCallback += (sender, e) => { ResultModel sendModel = new ResultModel(); VerifyQrCode(sendModel, ref token); if (sendModel.Code.Equals("0000") && sendModel.Result.Equals("2")) { AgainQrCode = true; } e.Code = sendModel.Code; e.Message = sendModel.Message; e.Result = sendModel.Result; }; DialogResult dialogResult = frmQrCode.ShowDialog(); if (dialogResult == DialogResult.OK) { WaitForm.ShowForm(); MainMakeRedInvoice(); return; } else { MessageUtil.Show("验证失败"); return; } } else if (refreshModel.Code.Equals("0000") && !refreshModel.State.Equals("2") && refreshModel.Message.Equals("获取成功") && refreshModel.Result.Contains("errReason")) { string errReason = ""; try { JArray returnResult = (JArray)JsonConvert.DeserializeObject(refreshModel.Result); errReason = returnResult[0]["errReason"] + ""; } catch (Exception) { errReason = refreshModel.Result; } MessageUtil.Show(errReason); return; } else { MessageUtil.Show(refreshModel.Message); return; } System.Threading.Thread.Sleep(500); } } WaitForm.HideForm(); } /// /// 修改表结构 /// /// /// /// private bool AlterTable(string tableName, string colName, string colType) { bool isResult; try { if (!BaseImpl.HasExistsColumn(tableName, colName)) { SqlHelper.ExecuteNonQuery(string.Format("alter table {0} add {1} {2}", tableName, colName, colType)); } isResult = true; } catch (Exception) { isResult = false; } return isResult; } /// /// 获取操作token /// private string GetToken() { string token = ""; try { string url = "https://lqzs.kflmgzs.com/prod-api/aisinoOpenApi.Auth/verify/getToken"; HttpTools.setting("application/json", null, null, HttpTools.Encode.UTF8); Dictionary pmsDic = new Dictionary(); pmsDic.Add("appCode", "13425C2E855757ED621C9A8BC4D52A4BCE33CB1A961831C9"); pmsDic.Add("appSecret", "tDfa1iY0FlSx20gM"); HttpWebResponse webResponse = HttpTools.Post(url, "", pmsDic, null, HttpTools.Method.GET); string result = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8).ReadToEnd(); if (webResponse != null && !string.IsNullOrEmpty(result)) { JObject resultJObject = (JObject)JsonConvert.DeserializeObject(result); string code = resultJObject["code"] + ""; if (code.Equals("0000")) { token = resultJObject["token"] + ""; SqlHelper.ExecuteNonQuery(string.Format("update p_systemtab set invoiceToken = '{0}'", token)); } } } catch (Exception) { } return token; } /// /// 发送验证码 /// private bool SendCode(ResultModel resultModel, ref string token, bool isNext = true) { bool isSend = false; try { if (string.IsNullOrEmpty(token) && AlterTable("p_systemtab", "invoiceToken", "varchar(1000)")) { token = SqlHelper.ExecuteScalar("select invoiceToken from p_systemtab") + ""; if (string.IsNullOrEmpty(token)) { token = GetToken(); } } string url = "https://lqzs.kflmgzs.com/prod-api/aisinoOpenApi/aisino/outSide/sendCode"; HttpTools.setting("application/json", null, null, HttpTools.Encode.UTF8); Dictionary headerDic = new Dictionary(); headerDic.Add("token", token); JObject pmsJObject = new JObject(); DataRow SourceRow = this.MainDataTab.Rows.Count > 0 ? this.MainDataTab.Rows[0] : null; if (SourceRow != null) { pmsJObject.Add("taxNo", SourceRow.Table.Columns.Contains("taxNo") ? SourceRow["taxNo"] + "" : ""); pmsJObject.Add("username", SourceRow.Table.Columns.Contains("username") ? SourceRow["username"] + "" : ""); pmsJObject.Add("password", SourceRow.Table.Columns.Contains("password") ? SourceRow["password"] + "" : ""); pmsJObject.Add("relateType", SourceRow.Table.Columns.Contains("relateType") ? SourceRow["relateType"] + "" : ""); int area = SourceRow.Table.Columns.Contains("area") && int.TryParse(SourceRow["area"] + "", out area) ? area : 0; pmsJObject.Add("area", area); HttpWebResponse webResponse = HttpTools.Post(url, JsonConvert.SerializeObject(pmsJObject), null, headerDic, HttpTools.Method.POST); string result = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8).ReadToEnd(); if (webResponse != null && !string.IsNullOrEmpty(result)) { JObject resultJObject = (JObject)JsonConvert.DeserializeObject(result); resultModel.Code = resultJObject["code"] + ""; resultModel.Message = resultJObject["message"] + ""; if (resultModel.Code.Equals("0000")) { isSend = true; } else if (resultModel.Code.Equals("500")) { resultModel.Message = resultJObject + ""; isSend = false; } else if (resultModel.Code.Equals("1006") && resultModel.Message.Contains("token已失效")) { token = GetToken(); if (isNext) { resultModel = new ResultModel(); isSend = SendCode(resultModel, ref token, false); } } } } else { isSend = false; } } catch (Exception ex) { resultModel.Message = ex.Message; isSend = false; } return isSend; } /// /// 验证码登录 /// /// private bool Login(ResultModel resultModel, string smsCode, ref string token, bool isNext = true) { bool isLogin = false; try { if (string.IsNullOrEmpty(token) && AlterTable("p_systemtab", "invoiceToken", "varchar(1000)")) { token = SqlHelper.ExecuteScalar("select invoiceToken from p_systemtab") + ""; if (string.IsNullOrEmpty(token)) { token = GetToken(); } } string url = "https://lqzs.kflmgzs.com/prod-api/aisinoOpenApi/aisino/outSide/login"; HttpTools.setting("application/json", null, null, HttpTools.Encode.UTF8); Dictionary headerDic = new Dictionary(); headerDic.Add("token", token); JObject pmsJObject = new JObject(); DataRow SourceRow = this.MainDataTab.Rows.Count > 0 ? this.MainDataTab.Rows[0] : null; if (SourceRow != null) { pmsJObject.Add("taxNo", SourceRow.Table.Columns.Contains("taxNo") ? SourceRow["taxNo"] + "" : ""); pmsJObject.Add("username", SourceRow.Table.Columns.Contains("username") ? SourceRow["username"] + "" : ""); pmsJObject.Add("smsCode", smsCode); HttpWebResponse webResponse = HttpTools.Post(url, JsonConvert.SerializeObject(pmsJObject), null, headerDic, HttpTools.Method.POST); string result = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8).ReadToEnd(); if (webResponse != null && !string.IsNullOrEmpty(result)) { JObject resultJObject = (JObject)JsonConvert.DeserializeObject(result); resultModel.Code = resultJObject["code"] + ""; resultModel.Message = resultJObject["message"] + ""; if (resultModel.Code.Equals("0000")) { isLogin = true; } else if (resultModel.Code.Equals("500")) { resultModel.Message = resultJObject + ""; isLogin = false; } else if (resultModel.Code.Equals("1006") && resultModel.Message.Contains("token已失效")) { token = GetToken(); if (isNext) { resultModel = new ResultModel(); isLogin = Login(resultModel, smsCode, ref token, false); } } } } else { isLogin = false; } } catch (Exception ex) { resultModel.Message = ex.Message; isLogin = false; } return isLogin; } /// /// 获取二维码 /// /// /// /// private bool Attestation(ResultModel resultModel, ref string token, bool isNext = true) { bool isLogin = false; try { if (string.IsNullOrEmpty(token) && AlterTable("p_systemtab", "invoiceToken", "varchar(1000)")) { token = SqlHelper.ExecuteScalar("select invoiceToken from p_systemtab") + ""; if (string.IsNullOrEmpty(token)) { token = GetToken(); } } string url = "https://lqzs.kflmgzs.com/prod-api/aisinoOpenApi/aisino/outSide/attestation"; HttpTools.setting("application/json", null, null, HttpTools.Encode.UTF8); Dictionary headerDic = new Dictionary(); headerDic.Add("token", token); JObject pmsJObject = new JObject(); DataRow SourceRow = this.MainDataTab.Rows.Count > 0 ? this.MainDataTab.Rows[0] : null; if (SourceRow != null) { pmsJObject.Add("taxNo", SourceRow.Table.Columns.Contains("taxNo") ? SourceRow["taxNo"] + "" : ""); pmsJObject.Add("username", SourceRow.Table.Columns.Contains("username") ? SourceRow["username"] + "" : ""); HttpWebResponse webResponse = HttpTools.Post(url, JsonConvert.SerializeObject(pmsJObject), null, headerDic, HttpTools.Method.POST); string result = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8).ReadToEnd(); if (webResponse != null && !string.IsNullOrEmpty(result)) { JObject resultJObject = (JObject)JsonConvert.DeserializeObject(result); resultModel.Code = resultJObject["code"] + ""; resultModel.Message = resultJObject["message"] + ""; if (resultModel.Code.Equals("0000")) { resultModel.Result = resultJObject["result"]["qrCode"] + ""; this.Rzid = resultJObject["result"]["Rzid"] + ""; isLogin = true; } else if (resultModel.Code.Equals("500")) { resultModel.Message = resultJObject + ""; isLogin = false; } else if (resultModel.Code.Equals("1006") && resultModel.Message.Contains("token已失效")) { token = GetToken(); if (isNext) { isLogin = Attestation(resultModel, ref token, false); } } } } else { isLogin = false; } } catch (Exception ex) { resultModel.Message = ex.Message; isLogin = false; } return isLogin; } /// /// 二维码验证 /// /// /// /// private bool VerifyQrCode(ResultModel resultModel, ref string token, bool isNext = true) { bool isLogin = false; try { if (string.IsNullOrEmpty(token) && AlterTable("p_systemtab", "invoiceToken", "varchar(1000)")) { token = SqlHelper.ExecuteScalar("select invoiceToken from p_systemtab") + ""; if (string.IsNullOrEmpty(token)) { token = GetToken(); } } string url = "https://lqzs.kflmgzs.com/prod-api/aisinoOpenApi/aisino/outSide/verifyQrCode"; HttpTools.setting("application/json", null, null, HttpTools.Encode.UTF8); Dictionary headerDic = new Dictionary(); headerDic.Add("token", token); JObject pmsJObject = new JObject(); DataRow SourceRow = this.MainDataTab.Rows.Count > 0 ? this.MainDataTab.Rows[0] : null; if (SourceRow != null) { pmsJObject.Add("taxNo", SourceRow.Table.Columns.Contains("taxNo") ? SourceRow["taxNo"] + "" : ""); pmsJObject.Add("username", SourceRow.Table.Columns.Contains("username") ? SourceRow["username"] + "" : ""); pmsJObject.Add("Rzid", this.Rzid); HttpWebResponse webResponse = HttpTools.Post(url, JsonConvert.SerializeObject(pmsJObject), null, headerDic, HttpTools.Method.POST); string result = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8).ReadToEnd(); if (webResponse != null && !string.IsNullOrEmpty(result)) { JObject resultJObject = (JObject)JsonConvert.DeserializeObject(result); resultModel.Code = resultJObject["code"] + ""; resultModel.Message = resultJObject["message"] + ""; if (resultModel.Code.Equals("0000")) { resultModel.Result = resultJObject["result"]["status"] + ""; isLogin = true; } else if (resultModel.Code.Equals("500")) { resultModel.Message = resultJObject + ""; isLogin = false; } else if (resultModel.Code.Equals("1006") && resultModel.Message.Contains("token已失效")) { token = GetToken(); if (isNext) { isLogin = VerifyQrCode(resultModel, ref token, false); } } } } else { isLogin = false; } } catch (Exception ex) { resultModel.Message = ex.Message; isLogin = false; } return isLogin; } /// /// 蓝字发票开具 /// /// private bool MakeInvoice(ref ResultModel resultModel, ref string token, bool isNext = true) { bool isLogin = false; try { if (string.IsNullOrEmpty(token) && AlterTable("p_systemtab", "invoiceToken", "varchar(1000)")) { token = SqlHelper.ExecuteScalar("select invoiceToken from p_systemtab") + ""; if (string.IsNullOrEmpty(token)) { token = GetToken(); } } string url = "https://lqzs.kflmgzs.com/prod-api/aisinoOpenApi/aisino/outSide/makeInvoice"; HttpTools.setting("application/json", null, null, HttpTools.Encode.UTF8); Dictionary headerDic = new Dictionary(); headerDic.Add("token", token); JObject pmsJObject = new JObject(); DataRow SourceRow = this.MainDataTab.Rows.Count > 0 ? this.MainDataTab.Rows[0] : null; if (SourceRow != null) { pmsJObject.Add("taxNo", SourceRow.Table.Columns.Contains("taxNo") ? SourceRow["taxNo"] + "" : ""); JObject qptJObject = new JObject(); qptJObject.Add("username", SourceRow.Table.Columns.Contains("username") ? SourceRow["username"] + "" : ""); qptJObject.Add("password", SourceRow.Table.Columns.Contains("password") ? SourceRow["password"] + "" : ""); qptJObject.Add("Yhyywdmc", SourceRow.Table.Columns.Contains("Yhyywdmc") ? SourceRow["Yhyywdmc"] + "" : ""); qptJObject.Add("Yhzh", SourceRow.Table.Columns.Contains("Yhzh") ? SourceRow["Yhzh"] + "" : ""); pmsJObject.Add("qpt", qptJObject); JObject invoiceJObject = new JObject(); invoiceJObject.Add("documentNo", SourceRow.Table.Columns.Contains("documentNo") ? SourceRow["documentNo"] + "" : ""); invoiceJObject.Add("fjh", SourceRow.Table.Columns.Contains("fjh") ? SourceRow["fjh"] + "" : ""); invoiceJObject.Add("jqbh", SourceRow.Table.Columns.Contains("jqbh") ? SourceRow["jqbh"] + "" : ""); int fplx = SourceRow.Table.Columns.Contains("fplx") && int.TryParse(SourceRow["fplx"] + "", out fplx) ? fplx : 12; invoiceJObject.Add("fplx", fplx); int ghfType = SourceRow.Table.Columns.Contains("ghfType") && int.TryParse(SourceRow["ghfType"] + "", out ghfType) ? ghfType : 1; invoiceJObject.Add("ghfType", ghfType); invoiceJObject.Add("ghfAddr", SourceRow.Table.Columns.Contains("ghfAddr") ? SourceRow["ghfAddr"] + "" : ""); invoiceJObject.Add("ghfBankAccount", SourceRow.Table.Columns.Contains("ghfBankAccount") ? SourceRow["ghfBankAccount"] + "" : ""); invoiceJObject.Add("ghfEmail", SourceRow.Table.Columns.Contains("ghfEmail") ? SourceRow["ghfEmail"] + "" : ""); invoiceJObject.Add("ghfMobile", SourceRow.Table.Columns.Contains("ghfMobile") ? SourceRow["ghfMobile"] + "" : ""); invoiceJObject.Add("ghfNsrmc", SourceRow.Table.Columns.Contains("ghfNsrmc") ? SourceRow["ghfNsrmc"] + "" : ""); invoiceJObject.Add("ghfNsrsbh", SourceRow.Table.Columns.Contains("ghfNsrsbh") ? SourceRow["ghfNsrsbh"] + "" : ""); invoiceJObject.Add("ghfTel", SourceRow.Table.Columns.Contains("ghfTel") ? SourceRow["ghfTel"] + "" : ""); decimal hjje = SourceRow.Table.Columns.Contains("hjje") && decimal.TryParse(SourceRow["hjje"] + "", out hjje) ? hjje : 0; invoiceJObject.Add("hjje", hjje); decimal hjse = SourceRow.Table.Columns.Contains("hjse") && decimal.TryParse(SourceRow["hjse"] + "", out hjse) ? hjse : 0; invoiceJObject.Add("hjse", hjse); int invoiceType = SourceRow.Table.Columns.Contains("invoiceType") && int.TryParse(SourceRow["invoiceType"] + "", out invoiceType) ? invoiceType : 1; invoiceJObject.Add("invoiceType", invoiceType); invoiceJObject.Add("issuer", SourceRow.Table.Columns.Contains("issuer") ? SourceRow["issuer"] + "" : ""); decimal jshj = SourceRow.Table.Columns.Contains("jshj") && decimal.TryParse(SourceRow["jshj"] + "", out jshj) ? jshj : 0; invoiceJObject.Add("jshj", jshj); decimal kce = SourceRow.Table.Columns.Contains("kce") && decimal.TryParse(SourceRow["kce"] + "", out kce) ? kce : 0; invoiceJObject.Add("kce", kce); invoiceJObject.Add("memo", SourceRow.Table.Columns.Contains("memo") ? SourceRow["memo"] + "" : ""); invoiceJObject.Add("payee", SourceRow.Table.Columns.Contains("payee") ? SourceRow["payee"] + "" : ""); invoiceJObject.Add("createorName", SourceRow.Table.Columns.Contains("createorName") ? SourceRow["createorName"] + "" : ""); invoiceJObject.Add("sellerAddrName", SourceRow.Table.Columns.Contains("sellerAddrName") ? SourceRow["sellerAddrName"] + "" : ""); invoiceJObject.Add("sellerNsrmc", SourceRow.Table.Columns.Contains("sellerNsrmc") ? SourceRow["sellerNsrmc"] + "" : ""); invoiceJObject.Add("sellerNsrsbh", SourceRow.Table.Columns.Contains("sellerNsrsbh") ? SourceRow["sellerNsrsbh"] + "" : ""); invoiceJObject.Add("sellerTel", SourceRow.Table.Columns.Contains("sellerTel") ? SourceRow["sellerTel"] + "" : ""); invoiceJObject.Add("callBackUrl", SourceRow.Table.Columns.Contains("callBackUrl") ? SourceRow["callBackUrl"] + "" : ""); int tsfs = SourceRow.Table.Columns.Contains("tsfs") && int.TryParse(SourceRow["tsfs"] + "", out tsfs) ? tsfs : -1; invoiceJObject.Add("tsfs", tsfs); pmsJObject.Add("invoice", invoiceJObject); JArray invoiceItemsArray = new JArray(); foreach (DataRow item in DetailDataTab.Rows) { JObject itemJObject = new JObject(); decimal amount = item.Table.Columns.Contains("amount") && decimal.TryParse(item["amount"] + "", out amount) ? amount : 0; itemJObject.Add("amount", amount); decimal incTaxAmount = item.Table.Columns.Contains("incTaxAmount") && decimal.TryParse(item["incTaxAmount"] + "", out incTaxAmount) ? incTaxAmount : 0; itemJObject.Add("incTaxAmount", incTaxAmount); decimal incTaxPrice; if (item.Table.Columns.Contains("incTaxPrice") && decimal.TryParse(item["incTaxPrice"] + "", out incTaxPrice)) { itemJObject.Add("incTaxPrice", incTaxPrice); } decimal price; if (item.Table.Columns.Contains("price") && decimal.TryParse(item["price"] + "", out price)) { itemJObject.Add("price", price); } itemJObject.Add("productName", item.Table.Columns.Contains("productName") ? item["productName"] + "" : ""); decimal quantity; if (item.Table.Columns.Contains("quantity") && decimal.TryParse(item["quantity"] + "", out quantity)) { itemJObject.Add("quantity", quantity); } itemJObject.Add("spec", item.Table.Columns.Contains("spec") ? item["spec"] + "" : ""); int sphxz = item.Table.Columns.Contains("sphxz") && int.TryParse(item["sphxz"] + "", out sphxz) ? sphxz : 0; itemJObject.Add("sphxz", sphxz); decimal taxRate = item.Table.Columns.Contains("taxRate") && decimal.TryParse(item["taxRate"] + "", out taxRate) ? taxRate : 0; itemJObject.Add("taxRate", taxRate); itemJObject.Add("ssflbm", item.Table.Columns.Contains("ssflbm") ? item["ssflbm"] + "" : ""); decimal taxAmount = item.Table.Columns.Contains("taxAmount") && decimal.TryParse(item["taxAmount"] + "", out taxAmount) ? taxAmount : 0; int taxPre = item.Table.Columns.Contains("taxPre") && int.TryParse(item["taxPre"] + "", out taxPre) ? taxPre : 0; itemJObject.Add("taxPre", taxPre); itemJObject.Add("taxPreCon", item.Table.Columns.Contains("taxPreCon") ? item["taxPreCon"] + "" : ""); itemJObject.Add("unit", item.Table.Columns.Contains("unit") ? item["unit"] + "" : ""); itemJObject.Add("taxAmount", taxAmount); invoiceItemsArray.Add(itemJObject); } pmsJObject.Add("invoiceItems", invoiceItemsArray); HttpWebResponse webResponse = HttpTools.Post(url, JsonConvert.SerializeObject(pmsJObject), null, headerDic, HttpTools.Method.POST); string result = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8).ReadToEnd(); try { string sql = $"insert into p_invoiceMsg (method,data,rtnMsg,sendDate) values ('蓝字发票开具',LEFT('{JsonConvert.SerializeObject(pmsJObject).Replace("'", "''")}',8000),LEFT('{result.Replace("'", "''")}',8000),'{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}');"; SqlHelper.ExecuteNonQuery(sql); } catch (Exception) { } if (webResponse != null && !string.IsNullOrEmpty(result)) { JObject resultJObject = (JObject)JsonConvert.DeserializeObject(result); resultModel.Code = resultJObject["code"] + ""; resultModel.Message = resultJObject["message"] + ""; if (resultModel.Code.Equals("0000")) { InvoiceId = resultJObject["invoiceId"] + ""; SqlHelper.ExecuteNonQuery(string.Format("update Acc_InvoiceMaintab set O_invoiceId = '{0}' where XMid = '{1}'", InvoiceId, PrimaryValue)); isLogin = true; } else if (resultModel.Code.Equals("500")) { resultModel.Message = resultJObject + ""; isLogin = false; } else if (resultModel.Code.Equals("1006") && resultModel.Message.Contains("token已失效")) { token = GetToken(); if (isNext) { resultModel = new ResultModel(); isLogin = MakeInvoice(ref resultModel, ref token, false); } } } } else { isLogin = false; } } catch (Exception ex) { resultModel.Message = ex.Message; isLogin = false; } return isLogin; } private bool RefreshInvoiceResponse(ref ResultModel resultModel, ref string token, bool isInvoice = false, bool isNext = true) { if (resultModel == null) { resultModel = new ResultModel(); } bool isLogin = false; try { if (string.IsNullOrEmpty(token) && AlterTable("p_systemtab", "invoiceToken", "varchar(1000)")) { token = SqlHelper.ExecuteScalar("select invoiceToken from p_systemtab") + ""; if (string.IsNullOrEmpty(token)) { token = GetToken(); } } string url = "https://lqzs.kflmgzs.com/prod-api/aisinoOpenApi/aisino/outSide/refreshInvoiceResponse"; HttpTools.setting("application/json", null, null, HttpTools.Encode.UTF8); Dictionary headerDic = new Dictionary(); headerDic.Add("token", token); JObject pmsJObject = new JObject(); DataRow SourceRow = this.MainDataTab.Rows.Count > 0 ? this.MainDataTab.Rows[0] : null; if (SourceRow != null) { pmsJObject.Add("taxNo", SourceRow.Table.Columns.Contains("taxNo") ? SourceRow["taxNo"] + "" : ""); pmsJObject.Add("username", SourceRow.Table.Columns.Contains("username") ? SourceRow["username"] + "" : ""); JObject reInvoiceVOObject = new JObject(); if (!isInvoice) { JArray invoiceIdsArray = new JArray(); invoiceIdsArray.Add(!string.IsNullOrWhiteSpace(InvoiceId) ? InvoiceId : SourceRow.Table.Columns.Contains("O_invoiceId") ? SourceRow["O_invoiceId"] + "" : SqlHelper.ExecuteScalar(string.Format("select O_invoiceId from Acc_InvoiceMaintab where XMid = '{0}'", PrimaryValue)) + ""); reInvoiceVOObject.Add("invoiceIds", invoiceIdsArray); } else { JArray documentNosArray = new JArray(); documentNosArray.Add(SourceRow.Table.Columns.Contains("documentNo") ? SourceRow["documentNo"] + "" : ""); reInvoiceVOObject.Add("documentNos", documentNosArray); } pmsJObject.Add("reInvoiceVO", reInvoiceVOObject); HttpWebResponse webResponse = HttpTools.Post(url, JsonConvert.SerializeObject(pmsJObject), null, headerDic, HttpTools.Method.POST); string result = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8).ReadToEnd(); if (webResponse != null && !string.IsNullOrEmpty(result)) { JObject resultJObject = (JObject)JsonConvert.DeserializeObject(result); resultModel.JResult = resultJObject; resultModel.Code = resultJObject["code"] + ""; resultModel.Message = resultJObject["message"] + ""; resultModel.Result = resultJObject["result"] + ""; JArray returnResult = (JArray)JsonConvert.DeserializeObject(resultModel.Result); string invoiceStatus = returnResult[0]["invoiceStatus"] + ""; resultModel.State = invoiceStatus; if (resultModel.Code.Equals("0000")) { if (invoiceStatus.Equals("20"))//正在开票中 { resultModel = new ResultModel(); isLogin = RefreshInvoiceResponse(ref resultModel, ref token, isInvoice); } else if (!invoiceStatus.Equals("0") && !invoiceStatus.Equals("4")) { JObject resultObj = (JsonConvert.DeserializeObject(resultModel.JResult["result"] + "") as JArray)[0] as JObject; if (isInvoice) { string id = resultObj["id"] + ""; this.InvoiceId = id; SqlHelper.ExecuteNonQuery(string.Format("update Acc_InvoiceMaintab set O_invoiceId = '{0}' where XMid = '{1}'", InvoiceId, PrimaryValue)); } if (invoiceStatus.Equals("2")) { isLogin = true; string invoiceNr = resultObj["invoiceNr"] + ""; string invoiceDate = resultObj["invoiceDate"] + ""; //long timestamp = Convert.ToInt64(invoiceDate); //DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));//当地时区 //DateTime time = startTime.AddMilliseconds(timestamp); //invoiceDate = time.ToString("yyyy-MM-dd HH:mm:ss.fff"); string ghfBankAccount = resultObj["ghfBankAccount"] + ""; string ghfAddr = resultObj["ghfAddr"] + ""; string ghfTel = resultObj["ghfTel"] + ""; string hjje = resultObj["hjje"] + ""; string hjse = resultObj["hjse"] + ""; string pdfUrl = resultObj["pdfUrl"] + ""; string updateSql = string.Format("update Acc_InvoiceMaintab set O_InfoNumber = '{0}',O_InfoInvDate = '{1}',InfoClientBankAccount = '{2}',InfoClientAddressPhone = '{3}',O_infoAmount = '{4}',O_InfoTaxAmount = '{5}',O_tokenurl = '{6}' where XMid = '{7}'", invoiceNr, invoiceDate, ghfBankAccount, ghfAddr + ghfTel, hjje, hjse, pdfUrl, PrimaryValue); SqlHelper.ExecuteNonQuery(updateSql); } } else { isLogin = false; } } else if (resultModel.Code.Equals("500")) { resultModel.Message = resultJObject + ""; isLogin = false; } else if (resultModel.Code.Equals("1006") && resultModel.Message.Contains("token已失效")) { token = GetToken(); if (isNext) { resultModel = new ResultModel(); isLogin = RefreshInvoiceResponse(ref resultModel, ref token, isInvoice, false); } } } } else { isLogin = false; } } catch (Exception ex) { resultModel.Message = ex.Message; isLogin = false; } return isLogin; } private bool RefreshHZInvoiceResponse(string hzinvoiceId, ref ResultModel resultModel, ref string token, bool isNext = true) { WaitForm.ShowForm(); if (resultModel == null) { resultModel = new ResultModel(); } bool isLogin = false; try { if (string.IsNullOrEmpty(token) && AlterTable("p_systemtab", "invoiceToken", "varchar(1000)")) { token = SqlHelper.ExecuteScalar("select invoiceToken from p_systemtab") + ""; if (string.IsNullOrEmpty(token)) { token = GetToken(); } } string url = "https://lqzs.kflmgzs.com/prod-api/aisinoOpenApi/aisino/outSide/refreshInvoiceResponse"; HttpTools.setting("application/json", null, null, HttpTools.Encode.UTF8); Dictionary headerDic = new Dictionary(); headerDic.Add("token", token); JObject pmsJObject = new JObject(); DataRow SourceRow = this.MainDataTab.Rows.Count > 0 ? this.MainDataTab.Rows[0] : null; if (SourceRow != null) { pmsJObject.Add("taxNo", SourceRow.Table.Columns.Contains("taxNo") ? SourceRow["taxNo"] + "" : ""); pmsJObject.Add("username", SourceRow.Table.Columns.Contains("username") ? SourceRow["username"] + "" : ""); JObject reInvoiceVOObject = new JObject(); JArray invoiceIdsArray = new JArray(); invoiceIdsArray.Add(hzinvoiceId); reInvoiceVOObject.Add("invoiceIds", invoiceIdsArray); pmsJObject.Add("reInvoiceVO", reInvoiceVOObject); HttpWebResponse webResponse = HttpTools.Post(url, JsonConvert.SerializeObject(pmsJObject), null, headerDic, HttpTools.Method.POST); string result = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8).ReadToEnd(); if (webResponse != null && !string.IsNullOrEmpty(result)) { JObject resultJObject = (JObject)JsonConvert.DeserializeObject(result); resultModel.JResult = resultJObject; resultModel.Code = resultJObject["code"] + ""; resultModel.Message = resultJObject["message"] + ""; resultModel.Result = resultJObject["result"] + ""; JArray returnResult = (JArray)JsonConvert.DeserializeObject(resultModel.Result); if (resultModel.Code.Equals("0000")) { string invoiceStatus = returnResult[0]["invoiceStatus"] + ""; resultModel.State = invoiceStatus; if (invoiceStatus.Equals("20"))//正在开票中 { resultModel = new ResultModel(); isLogin = RefreshInvoiceResponse(ref resultModel, ref token); } else if (!invoiceStatus.Equals("0") && !invoiceStatus.Equals("4")) { JObject resultObj = (JsonConvert.DeserializeObject(resultModel.JResult["result"] + "") as JArray)[0] as JObject; if (invoiceStatus.Equals("2")) { isLogin = true; string lz_invoiceID = Safe(SourceRow.Table.Columns.Contains("invoiceId") ? SourceRow["invoiceId"] + "" : ""); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendLine($@"if exists(select lz_invoiceID from Acc_hzInvoicemaintab where lz_invoiceID = '{lz_invoiceID}')"); stringBuilder.AppendLine($@"begin delete from Acc_hzInvoicemaintab where lz_invoiceID = '{lz_invoiceID}' delete from Acc_hzInvoicelisttab where lz_invoiceID = '{lz_invoiceID}' end;"); string Safe(string s) => (s ?? "").Replace("'", "''"); // ===== 主表字段 ===== string createTime = Safe(resultObj["createTime"] + ""); string documentNo = Safe(resultObj["documentNo"] + ""); string errReason = Safe(resultObj["errReason"] + ""); string fplx = resultObj["fplx"] + ""; string ghfAddr = Safe(resultObj["ghfAddr"] + ""); string ghfNsrmc = Safe(resultObj["ghfNsrmc"] + ""); string ghfNsrsbh = Safe(resultObj["ghfNsrsbh"] + ""); string ghfType = resultObj["ghfType"] + ""; string hjje = resultObj["hjje"] + ""; string hjse = resultObj["hjse"] + ""; string invoiceDate = Safe(resultObj["invoiceDate"] + ""); string invoiceNr = Safe(resultObj["invoiceNr"] + ""); string issuer = Safe(resultObj["issuer"] + ""); string jshj = resultObj["jshj"] + ""; string kce = resultObj["kce"] + ""; string memo = Safe(resultObj["memo"] + ""); string refInvoiceNr = Safe(resultObj["refInvoiceNr"] + ""); string sellerAddrName = Safe(resultObj["sellerAddrName"] + ""); string sellerBankAccount = Safe(resultObj["sellerBankAccount"] + ""); string sellerNsrmc = Safe(resultObj["sellerNsrmc"] + ""); string sellerNsrsbh = Safe(resultObj["sellerNsrsbh"] + ""); string sellerTel = Safe(resultObj["sellerTel"] + ""); string sendStatus = resultObj["sendStatus"] + ""; string tsfs = resultObj["tsfs"] + ""; string id = Safe(resultObj["id"] + ""); stringBuilder.AppendLine($@" INSERT INTO Acc_hzInvoicemaintab (createTime,documentNo,errReason,fplx,ghfAddr,ghfNsrmc,ghfNsrsbh,ghfType,hjje,hjse,invoiceDate,invoiceNr,invoiceStatus,issuer,jshj,kce,memo,refInvoiceNr,sellerAddrName,sellerBankAccount,sellerNsrmc,sellerNsrsbh,sellerTel,sendStatus,tsfs,id,lz_invoiceID) VALUES ('{createTime}','{documentNo}','{errReason}',{fplx},'{ghfAddr}','{ghfNsrmc}','{ghfNsrsbh}',{ghfType},{hjje},{hjse},'{invoiceDate}','{invoiceNr}',{invoiceStatus},'{issuer}',{jshj},{kce},'{memo}','{refInvoiceNr}','{sellerAddrName}','{sellerBankAccount}','{sellerNsrmc}','{sellerNsrsbh}','{sellerTel}',{sendStatus},{tsfs},'{id}','{lz_invoiceID}');"); var items = (JArray)resultObj["vatOutInvoiceItems"]; foreach (var item in items) { string invoiceId = Safe(item["invoiceId"] + ""); string itemOrder = item["itemOrder"] + ""; string productName = Safe(item["productName"] + ""); string spec = Safe(item["spec"] + ""); // 如果 JSON 没有可以自定义字段 string unit = Safe(item["unit"] + ""); string quantity = item["quantity"] + ""; string price = item["price"] + ""; string incTaxPrice = item["incTaxPrice"] + ""; string amount = item["amount"] + ""; string incTaxAmount = item["incTaxAmount"] + ""; string taxAmount = item["taxAmount"] + ""; string taxRate = item["taxRate"] + ""; string taxPre = item["taxPre"] + ""; string sphxz = item["sphxz"] + ""; string ssflbm = Safe(item["ssflbm"] + ""); stringBuilder.AppendLine($@" INSERT INTO Acc_hzInvoicelisttab (invoiceId,itemOrder,productName,spec,unit,quantity,price,incTaxPrice,amount,incTaxAmount,taxAmount,taxRate,taxPre,sphxz,ssflbm,lz_invoiceID) VALUES ('{invoiceId}',{itemOrder},'{productName}','{spec}','{unit}',{quantity},{price},{incTaxPrice},{amount},{incTaxAmount},{taxAmount},{taxRate},{taxPre},{sphxz},'{ssflbm}','{lz_invoiceID}');"); } SqlHelper.ExecuteNonQuery(stringBuilder.ToString()); // 定义参数列表 List list = new List { new SqlParameter("@invoiceId", SqlDbType.VarChar, 255), new SqlParameter("@operid", SqlDbType.Int), new SqlParameter("@msg", SqlDbType.VarChar, 1000) // 输出参数 }; list[0].Value = lz_invoiceID; list[1].Value = ERPInfo.Instance.UserId; list[2].Direction = ParameterDirection.Output; SqlParameter[] cmdParams = list.ToArray(); SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "p_hzfpbillcreate_pr", cmdParams); string rtnmsg = cmdParams[2].Value + ""; if (!string.IsNullOrEmpty(rtnmsg)) { MessageUtil.Show(rtnmsg); } } } else { isLogin = false; } } else if (resultModel.Code.Equals("500")) { resultModel.Message = resultJObject + ""; isLogin = false; } else if (resultModel.Code.Equals("1006") && resultModel.Message.Contains("token已失效")) { token = GetToken(); if (isNext) { resultModel = new ResultModel(); isLogin = RefreshInvoiceResponse(ref resultModel, ref token, false); } } } } else { isLogin = false; } } catch (Exception ex) { resultModel.Message = ex.Message; isLogin = false; } WaitForm.HideForm(); return isLogin; } /// /// 重开发票 /// /// /// /// /// private bool RetryInvoice(ref ResultModel resultModel, ref string token, bool isNext = true) { if (resultModel == null) { resultModel = new ResultModel(); } bool isLogin = false; try { if (string.IsNullOrEmpty(token) && AlterTable("p_systemtab", "invoiceToken", "varchar(1000)")) { token = SqlHelper.ExecuteScalar("select invoiceToken from p_systemtab") + ""; if (string.IsNullOrEmpty(token)) { token = GetToken(); } } string url = "https://lqzs.kflmgzs.com/prod-api/aisinoOpenApi/aisino/outSide/retryInvoice"; HttpTools.setting("application/json", null, null, HttpTools.Encode.UTF8); Dictionary headerDic = new Dictionary(); headerDic.Add("token", token); JObject pmsJObject = new JObject(); DataRow SourceRow = this.MainDataTab.Rows.Count > 0 ? this.MainDataTab.Rows[0] : null; if (SourceRow != null) { pmsJObject.Add("taxNo", SourceRow.Table.Columns.Contains("taxNo") ? SourceRow["taxNo"] + "" : ""); pmsJObject.Add("id", !string.IsNullOrWhiteSpace(InvoiceId) ? InvoiceId : SourceRow.Table.Columns.Contains("O_invoiceId") ? SourceRow["O_invoiceId"] + "" : SqlHelper.ExecuteScalar(string.Format("select O_invoiceId from Acc_InvoiceMaintab where XMid = '{0}'", PrimaryValue)) + ""); JObject qptObject = new JObject(); qptObject.Add("username", SourceRow.Table.Columns.Contains("username") ? SourceRow["username"] + "" : ""); qptObject.Add("password", SourceRow.Table.Columns.Contains("password") ? SourceRow["password"] + "" : ""); qptObject.Add("Yhyywdmc", SourceRow.Table.Columns.Contains("Yhyywdmc") ? SourceRow["Yhyywdmc"] + "" : ""); qptObject.Add("Yhzh", SourceRow.Table.Columns.Contains("Yhzh") ? SourceRow["Yhzh"] + "" : ""); pmsJObject.Add("qpt", qptObject); HttpWebResponse webResponse = HttpTools.Post(url, JsonConvert.SerializeObject(pmsJObject), null, headerDic, HttpTools.Method.POST); string result = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8).ReadToEnd(); if (webResponse != null && !string.IsNullOrEmpty(result)) { JObject resultJObject = (JObject)JsonConvert.DeserializeObject(result); resultModel.JResult = resultJObject; resultModel.Code = resultJObject["code"] + ""; resultModel.Message = resultJObject["message"] + ""; if (resultModel.Code.Equals("0000")) { isLogin = true; } else if (resultModel.Code.Equals("9999")) { isLogin = false; } else if (resultModel.Code.Equals("1006") && resultModel.Message.Contains("token已失效")) { token = GetToken(); if (isNext) { resultModel = new ResultModel(); isLogin = RetryInvoice(ref resultModel, ref token, false); } } } } else { isLogin = false; } } catch (Exception ex) { resultModel.Message = ex.Message; isLogin = false; } return isLogin; } /// /// 红字确认单初始化(已作废) /// /// /// /// /// private bool InitializeRedConfirm(ResultModel resultModel, ref string token, bool isNext = true) { bool isLogin = false; try { if (string.IsNullOrEmpty(token) && AlterTable("p_systemtab", "invoiceToken", "varchar(1000)")) { token = SqlHelper.ExecuteScalar("select invoiceToken from p_systemtab") + ""; if (string.IsNullOrEmpty(token)) { token = GetToken(); } } string url = "https://lqzs.kflmgzs.com/prod-api/aisinoOpenApi/aisino/outSide/initializeRedConfirm"; HttpTools.setting("application/json", null, null, HttpTools.Encode.UTF8); Dictionary headerDic = new Dictionary(); headerDic.Add("token", token); JObject pmsJObject = new JObject(); DataRow SourceRow = this.MainDataTab.Rows.Count > 0 ? this.MainDataTab.Rows[0] : null; if (SourceRow != null) { pmsJObject.Add("taxNo", SourceRow.Table.Columns.Contains("taxNo") ? SourceRow["taxNo"] + "" : ""); InvoiceId = MainDataTab.Columns.Contains("O_invoiceId") ? MainDataTab.Rows[0]["O_invoiceId"] + "" : SqlHelper.ExecuteScalar(string.Format("select O_invoiceId from Acc_InvoiceMaintab where XMid = '{0}'", PrimaryValue)) + ""; pmsJObject.Add("invoiceId", InvoiceId); pmsJObject.Add("username", SourceRow.Table.Columns.Contains("username") ? SourceRow["username"] + "" : ""); HttpWebResponse webResponse = HttpTools.Post(url, JsonConvert.SerializeObject(pmsJObject), null, headerDic, HttpTools.Method.POST); string result = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8).ReadToEnd(); if (webResponse != null && !string.IsNullOrEmpty(result)) { JObject resultJObject = (JObject)JsonConvert.DeserializeObject(result); resultModel.Code = resultJObject["code"] + ""; resultModel.Message = resultJObject["message"] + ""; if (resultModel.Code.Equals("0000")) { string responseDec = resultJObject["result"]["responseDec"] + ""; resultModel.Result = responseDec; isLogin = true; } else if (resultModel.Code.Equals("500")) { resultModel.Message = resultJObject + ""; isLogin = false; } else if (resultModel.Code.Equals("1006") && resultModel.Message.Contains("token已失效")) { token = GetToken(); if (isNext) { resultModel = new ResultModel(); isLogin = InitializeRedConfirm(resultModel, ref token, false); } } } } else { isLogin = false; } } catch (Exception ex) { resultModel.Message = ex.Message; isLogin = false; } return isLogin; } /// /// 红字确认单申请 /// /// /// /// /// private bool SaveRedConfirm(ref ResultModel resultModel, ref string token, bool isNext = true) { bool isLogin = false; try { if (string.IsNullOrEmpty(token) && AlterTable("p_systemtab", "invoiceToken", "varchar(1000)")) { token = SqlHelper.ExecuteScalar("select invoiceToken from p_systemtab") + ""; if (string.IsNullOrEmpty(token)) { token = GetToken(); } } string url = "https://lqzs.kflmgzs.com/prod-api/aisinoOpenApi/aisino/outSide/saveRedConfirmV2"; HttpTools.setting("application/json", null, null, HttpTools.Encode.UTF8); Dictionary headerDic = new Dictionary(); headerDic.Add("token", token); JObject pmsJObject = new JObject(); DataRow SourceRow = this.MainDataTab.Rows.Count > 0 ? this.MainDataTab.Rows[0] : null; if (SourceRow != null) { pmsJObject.Add("taxNo", SourceRow.Table.Columns.Contains("taxNo") ? SourceRow["taxNo"] + "" : ""); pmsJObject.Add("createorId", SourceRow.Table.Columns.Contains("createorId") ? SourceRow["createorId"] + "" : ""); JObject vatoutRedConfirmJObject = new JObject(); vatoutRedConfirmJObject.Add("invoiceId", InvoiceId); vatoutRedConfirmJObject.Add("applySource", SourceRow.Table.Columns.Contains("applySource") ? SourceRow["applySource"] + "" : ""); decimal taxAmount = SourceRow.Table.Columns.Contains("taxAmount") && decimal.TryParse(SourceRow["taxAmount"] + "", out taxAmount) ? taxAmount : 0; vatoutRedConfirmJObject.Add("taxAmount", taxAmount); decimal taxIncludeAmount = SourceRow.Table.Columns.Contains("taxIncludeAmount") && decimal.TryParse(SourceRow["taxIncludeAmount"] + "", out taxIncludeAmount) ? taxIncludeAmount : 0; vatoutRedConfirmJObject.Add("taxIncludeAmount", taxIncludeAmount); decimal taxExcludeAmount = SourceRow.Table.Columns.Contains("taxExcludeAmount") && decimal.TryParse(SourceRow["taxExcludeAmount"] + "", out taxExcludeAmount) ? taxExcludeAmount : 0; vatoutRedConfirmJObject.Add("taxExcludeAmount", taxExcludeAmount); vatoutRedConfirmJObject.Add("redReason", SourceRow.Table.Columns.Contains("redReason") ? SourceRow["redReason"] + "" : ""); pmsJObject.Add("vatoutRedConfirm", vatoutRedConfirmJObject); JArray vatoutRedConfirmItemsArray = new JArray(); foreach (DataRow item in DetailDataTab.Rows) { JObject itemJObject = new JObject(); int blueDetailIndex = item.Table.Columns.Contains("blueDetailIndex") && int.TryParse(item["blueDetailIndex"] + "", out blueDetailIndex) ? blueDetailIndex : 0; itemJObject.Add("blueDetailIndex", blueDetailIndex); itemJObject.Add("goodsName", item.Table.Columns.Contains("goodsName") ? item["goodsName"] + "" : ""); itemJObject.Add("unit", item.Table.Columns.Contains("unit") ? item["unit"] + "" : ""); itemJObject.Add("specType", item.Table.Columns.Contains("specType") ? item["specType"] + "" : ""); decimal num = item.Table.Columns.Contains("num") && decimal.TryParse(item["num"] + "", out num) ? num : 0; itemJObject.Add("num", num); decimal price = item.Table.Columns.Contains("price") && decimal.TryParse(item["price"] + "", out price) ? price : 0; itemJObject.Add("price", price); decimal taxExcludePrice = item.Table.Columns.Contains("taxExcludePrice") && decimal.TryParse(item["taxExcludePrice"] + "", out taxExcludePrice) ? taxExcludePrice : 0; itemJObject.Add("taxExcludePrice", taxExcludePrice); decimal taxExcludeAmountDetail = item.Table.Columns.Contains("taxExcludeAmount") && decimal.TryParse(item["taxExcludeAmount"] + "", out taxExcludeAmount) ? taxExcludeAmount : 0; itemJObject.Add("taxExcludeAmount", taxExcludeAmountDetail); decimal taxAmountDetail = item.Table.Columns.Contains("taxAmount") && decimal.TryParse(item["taxAmount"] + "", out taxAmount) ? taxAmount : 0; itemJObject.Add("taxAmount", taxAmountDetail); decimal taxRate = item.Table.Columns.Contains("taxRate") && decimal.TryParse(item["taxRate"] + "", out taxRate) ? taxRate : 0; itemJObject.Add("taxRate", taxRate); itemJObject.Add("goodsCode", item.Table.Columns.Contains("goodsCode") ? item["goodsCode"] + "" : ""); itemJObject.Add("favouredPolicyFlag", item.Table.Columns.Contains("favouredPolicyFlag") ? item["favouredPolicyFlag"] + "" : ""); itemJObject.Add("issuer", item.Table.Columns.Contains("issuer") ? item["issuer"] + "" : ""); itemJObject.Add("username", item.Table.Columns.Contains("username") ? item["username"] + "" : ""); itemJObject.Add("url", item.Table.Columns.Contains("url") ? item["url"] + "" : ""); vatoutRedConfirmItemsArray.Add(itemJObject); } pmsJObject.Add("vatoutRedConfirmItems", vatoutRedConfirmItemsArray); HttpWebResponse webResponse = HttpTools.Post(url, JsonConvert.SerializeObject(pmsJObject), null, headerDic, HttpTools.Method.POST); string result = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8).ReadToEnd(); if (webResponse != null && !string.IsNullOrEmpty(result)) { JObject resultJObject = (JObject)JsonConvert.DeserializeObject(result); resultModel.Code = resultJObject["code"] + ""; resultModel.Message = resultJObject["message"] + ""; if (resultModel.Code.Equals("0000")) { string billUuid = resultJObject["billUuid"] + ""; string billNo = resultJObject["billNo"] + ""; string billStatus = resultJObject["billStatus"] + ""; resultModel.State = billStatus; resultModel.JResult = resultJObject; isLogin = true; } else if (resultModel.Code.Equals("500")) { resultModel.Message = resultJObject + ""; isLogin = false; } else if (resultModel.Code.Equals("1006") && resultModel.Message.Contains("token已失效")) { token = GetToken(); if (isNext) { resultModel = new ResultModel(); isLogin = SaveRedConfirm(ref resultModel, ref token, false); } } } } else { isLogin = false; } } catch (Exception ex) { resultModel.Message = ex.Message; isLogin = false; } return isLogin; } /// /// 红字确认单开具 /// /// /// /// /// private bool MakeRedBill(ref ResultModel resultModel, ref string token, bool isNext = true) { bool isLogin = false; try { if (string.IsNullOrEmpty(token) && AlterTable("p_systemtab", "invoiceToken", "varchar(1000)")) { token = SqlHelper.ExecuteScalar("select invoiceToken from p_systemtab") + ""; if (string.IsNullOrEmpty(token)) { token = GetToken(); } } string url = "https://lqzs.kflmgzs.com/prod-api/aisinoOpenApi/aisino/outSide/redBill"; HttpTools.setting("application/json", null, null, HttpTools.Encode.UTF8); Dictionary headerDic = new Dictionary(); headerDic.Add("token", token); JObject pmsJObject = new JObject(); DataRow SourceRow = this.MainDataTab.Rows.Count > 0 ? this.MainDataTab.Rows[0] : null; if (SourceRow != null) { pmsJObject.Add("taxNo", SourceRow.Table.Columns.Contains("taxNo") ? SourceRow["taxNo"] + "" : ""); pmsJObject.Add("username", SourceRow.Table.Columns.Contains("username") ? SourceRow["username"] + "" : ""); pmsJObject.Add("issuer", SourceRow.Table.Columns.Contains("issuer") ? SourceRow["issuer"] + "" : ""); JObject vatOutRedConfirmJObject = new JObject(); vatOutRedConfirmJObject.Add("applySource", SourceRow.Table.Columns.Contains("applySource") ? SourceRow["applySource"] + "" : ""); vatOutRedConfirmJObject.Add("sellerTaxNo", SourceRow.Table.Columns.Contains("sellerTaxNo") ? SourceRow["sellerTaxNo"] + "" : ""); vatOutRedConfirmJObject.Add("blueInvoiceNumber", SourceRow.Table.Columns.Contains("blueInvoiceNumber") ? SourceRow["blueInvoiceNumber"] + "" : ""); string billNo = resultModel.JResult["billNo"] + ""; vatOutRedConfirmJObject.Add("billNo", billNo); string billUuid = resultModel.JResult["billUuid"] + ""; vatOutRedConfirmJObject.Add("billUuid", billUuid); vatOutRedConfirmJObject.Add("invoiceId", InvoiceId); vatOutRedConfirmJObject.Add("blueInvoiceLine", SourceRow.Table.Columns.Contains("blueInvoiceLine") ? SourceRow["blueInvoiceLine"] + "" : ""); pmsJObject.Add("vatOutRedConfirm", vatOutRedConfirmJObject); HttpWebResponse webResponse = HttpTools.Post(url, JsonConvert.SerializeObject(pmsJObject), null, headerDic, HttpTools.Method.POST); string result = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8).ReadToEnd(); if (webResponse != null && !string.IsNullOrEmpty(result)) { JObject resultJObject = (JObject)JsonConvert.DeserializeObject(result); resultModel.Code = resultJObject["code"] + ""; resultModel.Message = resultJObject["message"] + ""; if (resultModel.Code.Equals("0000")) { isLogin = true; } else if (resultModel.Code.Equals("500")) { resultModel.Message = resultJObject + ""; isLogin = false; } else if (resultModel.Code.Equals("1006") && resultModel.Message.Contains("token已失效")) { token = GetToken(); if (isNext) { resultModel = new ResultModel(); isLogin = MakeRedBill(ref resultModel, ref token, false); } } } } else { isLogin = false; } } catch (Exception ex) { resultModel.Message = ex.Message; isLogin = false; } return isLogin; } /// /// 快捷红冲 /// /// /// /// /// private bool FlushRedInvoice(string redReason, ref ResultModel resultModel, ref string token, bool isNext = true) { WaitForm.ShowForm(); bool isLogin = false; try { if (string.IsNullOrEmpty(token) && AlterTable("p_systemtab", "invoiceToken", "varchar(1000)")) { token = SqlHelper.ExecuteScalar("select invoiceToken from p_systemtab") + ""; if (string.IsNullOrEmpty(token)) { token = GetToken(); } } string url = "https://lqzs.kflmgzs.com/prod-api/aisinoOpenApi/aisino/outSide/flushRedInvoice"; HttpTools.setting("application/json", null, null, HttpTools.Encode.UTF8); Dictionary headerDic = new Dictionary(); headerDic.Add("token", token); JObject pmsJObject = new JObject(); DataRow SourceRow = this.MainDataTab.Rows.Count > 0 ? this.MainDataTab.Rows[0] : null; if (SourceRow != null) { pmsJObject.Add("taxNo", SourceRow.Table.Columns.Contains("taxNo") ? SourceRow["taxNo"] + "" : ""); pmsJObject.Add("username", SourceRow.Table.Columns.Contains("username") ? SourceRow["username"] + "" : ""); pmsJObject.Add("invoiceId", SourceRow.Table.Columns.Contains("invoiceId") ? SourceRow["invoiceId"] + "" : ""); pmsJObject.Add("redReason", redReason); //pmsJObject.Add("createorId", SourceRow.Table.Columns.Contains("createorId") ? SourceRow["createorId"] + "" : ""); HttpWebResponse webResponse = HttpTools.Post(url, JsonConvert.SerializeObject(pmsJObject), null, headerDic, HttpTools.Method.POST); string result = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8).ReadToEnd(); if (webResponse != null && !string.IsNullOrEmpty(result)) { JObject resultJObject = (JObject)JsonConvert.DeserializeObject(result); resultModel.Code = resultJObject["code"] + ""; resultModel.Message = resultJObject["message"] + ""; resultModel.Result = resultJObject["message"] + ""; if (resultModel.Code.Equals("0000")) { InvoiceId = resultJObject["invoiceId"] + ""; SqlHelper.ExecuteNonQuery(string.Format("update Acc_InvoiceMaintab set O_hzinvoiceId = '{0}' where XMid = '{1}'", InvoiceId, PrimaryValue)); isLogin = true; } else if (resultModel.Code.Equals("500")) {