using System; using System.Data; using System.Collections; using System.Collections.Generic; using HcUtility.Core; using Newtonsoft.Json; using DSWeb.TruckMng.Helper; using Newtonsoft.Json.Converters; using DSWeb.Areas.Account.Models.Chfee_Invoice; using NuonuoSDK; using DSWeb.Areas.Account.Models.Chfee_Invoiceapplication; using DSWeb.Areas.CommMng.DAL; using Microsoft.Practices.EnterpriseLibrary.Data; using DSWeb.Areas.Account.DAL.Chfee_Invoice_HangXin; using HcUtility.Comm; using System.Web.Mvc; using DSWeb.MvcShipping.Models.CodeGoodInv; using Quartz; using DSWeb.Areas.MvcShipping.Comm; using Newtonsoft.Json.Linq; using DSWeb.Areas.SysMng.Models.DBTableConn; using DSWeb.Areas.SysMng.DAL.DBTableConn; using DSWeb.MvcShipping.DAL.MsSysInvInterFaceSet; using static System.Net.WebRequestMethods; using sun.security.krb5.@internal.crypto; using DSWeb.Areas.CommMng.Controllers; using DSWeb.MvcShipping.DAL.MsSysParamSet; using System.Web; using DSWeb.MvcShipping.DAL.MsOpExpressDAL; namespace DSWeb.Areas.Account.Models.Chfee_Invoice_HangXin { [JsonObject] public class ChInvoice_HangXin : Chfee_Invoice.ChInvoice { #region 私有成员 在Chfee_Invoice.ChInvoice 基础上增加的字段 private string _CUSTTEL = string.Empty;//购方电话 private string _CUSTADDR = string.Empty;//购方地址 private string _PTORRED = "1"; //开票类型:1,正票;2,红票 80001 private string _REDCODE = string.Empty;//冲红时填写的对应蓝票票代码(红票必填,不满12位请左补0) private string _REDNUM = string.Empty;//冲红时填写的对应蓝票票号码(红票必填,不满8位请左补0) private string _PUSHMODE = "-1";//推送方式:-1,不推送;0,邮箱;1,手机(默认);2,邮箱、手机 80002 private string _PUSHMOBILE = string.Empty;//购方手机(开票成功会短信提醒购方,不受推送方式影响) private string _PUSHEMAIL = string.Empty;//推送邮箱(pushMode为0或2时,此项为必填) private string _INVOICELINE = "pc"; //发票种类:p,普通发票(电票)(默认);c,普通发票(纸票);s,专用发票;e,收购发票(电票);f,收购发票(纸质) 80003 public bool 全电发票() { return (_INVOICELINE == "bs" || _INVOICELINE == "pc" || _INVOICELINE == "es" || _INVOICELINE == "ec"); } private string _EINVOICESTATE = string.Empty; //接口开票是否成功。开票成功时记录为true 80004 private string _INVOICEINFOURL = string.Empty; //发票详情地址 一个url 直接显示诺诺开票的发票详情页 开票成功后读取发票详情并记录 private string _INVOICEPDFURL = string.Empty; //发票PDF地址 开票成功后读取发票详情并记录 private string _PTORREDREF = string.Empty; // private string _PUSHMODEREF = string.Empty; private string _EINVOICESTATEREF = string.Empty; private string _INVOICELINEREF = string.Empty; private string _TAXCODE = string.Empty; private string _OFFICEPHONE = string.Empty; private string _ADDRESS = string.Empty; private string _invoiceSerialNum = string.Empty;//诺诺开票的流水号 private string _INVOICECODE = string.Empty;//发票代码 private string _PAYEE = string.Empty;//收款人 private string _CHECKER = string.Empty;//复核人 private string _SETRED = string.Empty;//是否被冲红 private string _INVITERFACE = string.Empty;//发票接口 #endregion #region 读写属性 [ModelDB] public string CUSTTEL { get { return _CUSTTEL; } set { _CUSTTEL = value; } } [ModelDB] public string CUSTADDR { get { return _CUSTADDR; } set { _CUSTADDR = value; } } /// /// 1正票 2红票 /// [ModelDB] public string PTORRED { get { return _PTORRED; } set { _PTORRED = value; } } /// /// 被冲红(蓝票)发票代码 /// [ModelDB] public string REDCODE { get { return _REDCODE; } set { _REDCODE = value; } } /// /// 被冲红(蓝票)发票号码 /// [ModelDB] public string REDNUM { get { return _REDNUM; } set { _REDNUM = value; } } /// /// -1不推送 0邮箱 1电话 2邮箱+电话 /// [ModelDB] public string PUSHMODE { get { return _PUSHMODE; } set { _PUSHMODE = value; } } [ModelDB] public string PUSHMOBILE { get { return _PUSHMOBILE; } set { _PUSHMOBILE = value; } } [ModelDB] public string PUSHEMAIL { get { return _PUSHEMAIL; } set { _PUSHEMAIL = value; } } [ModelDB] public string INVOICELINE { get { return _INVOICELINE; } set { _INVOICELINE = value; } } public bool ISEInvoice { get { if (INVOICELINE == "p" || INVOICELINE == "e") return true; else return false; } } [ModelDB] public string EINVOICESTATE { get { return _EINVOICESTATE; } set { _EINVOICESTATE = value; } } [ModelDB] public string INVOICEINFOURL { get { return _INVOICEINFOURL; } set { _INVOICEINFOURL = value; } } [ModelDB] public string INVOICEPDFURL { get { return _INVOICEPDFURL; } set { _INVOICEPDFURL = value; } } /// /// 发票流水号 诺诺平台接收正确的开票信息后返回该值 /// [ModelDB] public string INVOICESERIALNUM { get { return _invoiceSerialNum; } set { _invoiceSerialNum = value; } } [ModelDB] public string INVOICECODE { get { return _INVOICECODE; } set { _INVOICECODE = value; } } public string PTORREDREF { get { return _PTORREDREF; } set { _PTORREDREF = value; } } public string PUSHMODEREF { get { return _PUSHMODEREF; } set { _PUSHMODEREF = value; } } public string INVOICELINEREF { get { return _INVOICELINEREF; } set { _INVOICELINEREF = value; } } public string EINVOICESTATEREF { get { return _EINVOICESTATEREF; } set { _EINVOICESTATEREF = value; } } /// /// 销方税号 /// new public string TAXCODE { get { return _TAXCODE; } set { _TAXCODE = value; } } public string OFFICEPHONE { get { return _OFFICEPHONE; } set { _OFFICEPHONE = value; } } public string ADDRESS { get { return _ADDRESS; } set { _ADDRESS = value; } } public string BILLRISES { get; set; } /// /// 收款人 /// [ModelDB] public string PAYEE { get { return _PAYEE; } set { _PAYEE = value; } } /// /// 复核人 /// [ModelDB] public string CHECKER { get { return _CHECKER; } set { _CHECKER = value; } } [ModelDB] public string SETRED { get { return _SETRED; } set { _SETRED = value; } } [ModelDB] public string INVITERFACE { get { return _INVITERFACE; } set { _INVITERFACE = value; } } [ModelDB] public bool ISFRINV { get; set; } #endregion #region 诺诺全电发票增加的字段 /// /// 冲红原因:1:销货退回;2:开票有误;3:服务中止;4:发生销售折让(开具红票时且票种为 p, c, e, f, r 需要传--成品油发票除外;不传时默认为 1) /// [ModelDB] public string redReason { get; set; } = ""; /// /// 红字信息表编号.专票冲红时此项必填,且必须在备注中注明“开具红字增值税专用发票信息表编号 ZZZZZZZZZZZZZZZZ” 字样 /// ,其 中“Z” 为开具红字增值税专用发票所需要的长度为 16位信息表编号(建议 16 位,最长可支持 24 位)。 /// [ModelDB] public string billInfoNo { get; set; } = ""; /// /// 红字确认单 uuid /// [ModelDB] public string billUuid { get; set; } = ""; /// /// 购买方经办人姓名(全电发票特有字段) /// [ModelDB] public string buyerManagerName { get; set; } = ""; /// /// 经办人证件类型:101-组织机构代码证, 102-营业执照, 103-税务登记证, 199-其他单位证件 /// , 201-居民身份证, 202-军官证, 203-武警警官证, 204-士兵证, 205-军队离退休干部证 /// , 206-残疾人证, 207-残疾军人证(1-8 级),208-外国护照, 210-港澳居民来往内地通行证 /// , 212-中华人民共和国往来港澳通行证, 213-台湾居民来往大陆通行证 /// , 214-大陆居民往来台湾通行证, 215-外国人居留证, 216-外交官证 299-其他个人证件(全电发票特有) /// [ModelDB] public string managerCardType { get; set; } = ""; [ModelDB] public string managerCardNo { get; set; } = ""; #endregion public ChInvoice_HangXin() { TableName = "ch_fee_invoice"; } public static decimal getarraysum(string[] feearray) { var result = 0M; for (var _i = 0; _i < feearray.Length; _i++) { try { result += Convert.ToDecimal(feearray[_i]); } catch { } } return result; } public void SetP2Value(BaseTableMC P2data) { var connList = DBTableConnDAL.getConn("联力P2发票"); BSNO = P2data.GetValue("BSNO"); CUSTOMERNAME = P2data.GetValue("CUSTOMERNAME"); INVOICECUSTNAME = P2data.GetValue("INVOICECUSTNAME"); VESSELVOYAGE = P2data.GetValue("VESSEL") + ";" + P2data.GetValue("VOYAGE"); ETD = P2data.GetValue("ETD"); POL = P2data.GetValue("POL"); POD = P2data.GetValue("POD"); RECVCURR = P2data.GetValue("CURRENCY"); CURRENCY = "RMB"; CUSTRATENO = P2data.GetValue("CUSTRATENO"); if (RECVCURR == "RMB") { AMOUNT = getarraysum(P2data.GetSplitField("AMOUNT", "\r\n")); }; if (RECVCURR != "RMB") { AMOUNT = 0M; var pricearray = P2data.GetSplitField("INVAMOUNT", "\r\n"); var numarray = P2data.GetSplitField("NUM", "\r\n"); for (var _i = 0; _i < pricearray.Length; _i++) { try { AMOUNT += Convert.ToDecimal(pricearray[_i]) * Convert.ToDecimal(numarray[_i]); } catch { } } } INVAMOUNT = AMOUNT; if (RECVCURR != "RMB") { OTCURRAMOUNT = getarraysum(P2data.GetSplitField("AMOUNT", "\r\n")); } else { OTCURRAMOUNT = 0M; } AMOUNTCAPITAL = P2data.GetValue("AMOUNTCAPITAL"); REMARK = P2data.GetValue("REMARK"); INVOICETYPE = 2; OPERATETIME = DateTime.Now; MBLNO = P2data.GetValue("MBLNO"); CUSTADDRTEL = P2data.GetValue("CUSTADDRTEL"); CUSTBANK = P2data.GetValue("CUSTBANK"); PUSHMODE = "0"; PUSHMOBILE = "0"; PUSHEMAIL = ""; //如果是红票 需要设定一些值 PTORRED = "1";//默认正票 INVOICELINE = "p";//默认普通电票 if (P2data.GetValue("REDINVOICE") == "1") { PTORRED = "2"; BaseTableMC fromdbbase = new BaseTableMC("M_INVOICE", "INVOICE_ID", "", "P2DB"); var BeREDDataList = DBTableConnDAL.getToList(fromdbbase, "联力P2发票", " INVOICE_ID='" + P2data.GetValue("REDBSNO") + "'"); if (BeREDDataList.Count > 0) { var BeRedinvoice = BeREDDataList[0]; REDNUM = BeRedinvoice.GetValue("INVOICENO"); REDCODE = BeRedinvoice.GetValue("INVOICECODE"); CUSTADDRTEL = BeRedinvoice.GetValue("CUSTADDRTEL"); CUSTBANK = BeRedinvoice.GetValue("CUSTBANK"); CUSTRATENO = BeRedinvoice.GetValue("CUSTRATENO"); } } } } [JsonObject] public class HXInvoice { public HXInvoice_Head order; public HXInvoice(ChInvoice_HangXin head, List body) { this.order = new HXInvoice_Head(head); List invoiceDetail = new List(); foreach (var detail in body) { var item = new HXInvoice_Detail(detail); invoiceDetail.Add(item); } this.order.invoiceDetail = invoiceDetail; } } [JsonObject] public class HXInvoice_Head { protected ChInvoice_HangXin ChInvoice_HangXin; public HXInvoice_Head() { } public HXInvoice_Head(ChInvoice_HangXin ChInvoice_HangXin) { this.ChInvoice_HangXin = ChInvoice_HangXin; } public List invoiceDetail; #region R&W public string buyerName { get { return ChInvoice_HangXin.INVOICECUSTNAME; } } public string buyerTaxNum { get { return ChInvoice_HangXin.CUSTRATENO; } } public string buyerTel { get { return ChInvoice_HangXin.CUSTTEL; } } public string buyerAddress { get { return ChInvoice_HangXin.CUSTADDR; } } public string buyerAccount { get { return ChInvoice_HangXin.CUSTBANK.Replace(" ", ""); } } public string orderNo { get { return ChInvoice_HangXin.BILLNO; } } public string invoiceDate { get { return ChInvoice_HangXin.INVOICEMAKETIME + " 12:00:00"; } } /// /// 开票员 /// public string clerk { get { return ChInvoice_HangXin.OPERATORNAME; } } public string salerTaxNum { get { return ChInvoice_HangXin.TAXCODE; } } public string salerTel { get { return ChInvoice_HangXin.OFFICEPHONE; } } public string salerAddress { get { return ChInvoice_HangXin.ADDRESS; } } public string salerAccount { get { return ChInvoice_HangXin.ACCOUNT; } } public string invoiceType { get { return ChInvoice_HangXin.PTORRED; } } public string remark { get { return ChInvoice_HangXin.REMARK; } } public string invoiceCode { get { if (string.IsNullOrWhiteSpace(ChInvoice_HangXin.REDCODE)) { return ""; } return ChInvoice_HangXin.REDCODE.Length == 11 ? "0" + ChInvoice_HangXin.REDCODE : ChInvoice_HangXin.REDCODE; } } public string invoiceNum { get { return ChInvoice_HangXin.REDNUM; } } public string pushMode { get { return ChInvoice_HangXin.PUSHMODE; } } public string buyerPhone { get { return ChInvoice_HangXin.PUSHMOBILE; } } public string email { get { return ChInvoice_HangXin.PUSHEMAIL; } } public string invoiceLine { get { return ChInvoice_HangXin.INVOICELINE; } } /// /// 收款人 /// public string payee { get { return ChInvoice_HangXin.PAYEE; } } public string checker { get { return ChInvoice_HangXin.CHECKER; } } public string extensionNumber { get; set; } #endregion } //ChInvoiceDetail [JsonObject] public class HXInvoice_Detail { protected ChInvoiceDetail ChInvoiceDetail; public HXInvoice_Detail() { } public HXInvoice_Detail(ChInvoiceDetail ChInvoiceDetail) { this.ChInvoiceDetail = ChInvoiceDetail; } #region R&W public string goodsName//品名 { get { return ChInvoiceDetail.GOODSNAMEREF; } } public string num//数量 { get { return Math.Round(ChInvoiceDetail.PKGS, 0, MidpointRounding.AwayFromZero).ToString(); } } public string withTaxFlag//单价是否含税0不含1含税 固定为0 { get { return "0"; } } public string price//单价 { get { return Math.Round(ChInvoiceDetail.PRICE, 2, MidpointRounding.AwayFromZero).ToString(); } } public string tax //税额,[不含税金额] * [税率] = [税额];税额允许误差为 0.06。红票为负。不含税金额、税额、含税金额任何一个不传时,会根据传入的单价,数量进行计算,可能和实际数值存在误差,建议都传入 { get { return Math.Round(ChInvoiceDetail.TAX, 2, MidpointRounding.AwayFromZero).ToString(); } } public string taxIncludedAmount //"含税金额,[不含税金额] + [税额] = [含税金额],红票为负。不含税金额、税额、含税金额任何一个不传时,会根据传入的单价,数量进行计算,可能和实际数值存在误差,建议都传入" { get { return (Math.Round(ChInvoiceDetail.AMOUNT, 2, MidpointRounding.AwayFromZero) + Math.Round(ChInvoiceDetail.TAX, 2, MidpointRounding.AwayFromZero)).ToString(); } } public string taxExcludedAmount //不含税金额。红票为负。不含税金额、税额、含税金额任何一个不传时,会根据传入的单价,数量进行计算,可能和实际数值存在误差,建议都传入 { get { return Math.Round(ChInvoiceDetail.AMOUNT, 2, MidpointRounding.AwayFromZero).ToString(); } } public string taxRate { get { return Math.Round((ChInvoiceDetail.TAXRATE / 100), 2, MidpointRounding.AwayFromZero).ToString(); } } public string specType { get { return ChInvoiceDetail.SPEC; } } public string unit { get { return ChInvoiceDetail.UNIT; } } public string goodsCode { get { return ChInvoiceDetail.GOODCODE; } } /// ///下列三项从相关的code_goods_inv表中查找 优惠政策0不使用1使用 ///增值税特殊管理 ///零税率标识:空,非零税;1,免税;2,不征税;3,普通零税率 ///如无则分别填入 1:0不使用 2:空 3:空 非零税率 /// /// 全电票内容来自枚举类型80007 /// 全电发票时: 01:简易征收 02:稀土产品 03:免税 04:不征税 05:先征后退 06:100%先征后退 07:50%先征后退 /// 08:按 3%简易征收 09:按 5%简易征收 10:按 5%简易征收减按 1.5%计征 11:即征即退 30% 12:即征即退 50% /// 13:即征即退 70% 14:即征即退 100% 15:超税负 3%即征即退 16:超税负 8%即征即退 /// 17:超税负 12%即征即退 18:超税负6%即征即退 /// /// public string favouredPolicyFlag { get { return ChInvoiceDetail.ISUSEPREF; } } public string favouredPolicyName { get { return ChInvoiceDetail.DEFREMARK; } } public string zeroRateFlag { get { return ChInvoiceDetail.ZTAXTYPE; } } #endregion } [JsonObject] public class NuoNuoQuanDianInvoice { public NuoNuoQuanDian_Head order; public NuoNuoQuanDianInvoice(ChInvoice_HangXin head, List body) { this.order = new NuoNuoQuanDian_Head(head); order.payee = ""; order.checker = ""; List invoiceDetail = new List(); foreach (var detail in body) { var item = new NuoNuoQuanDian_Detail(detail); invoiceDetail.Add(item); } this.order.invoiceDetail = invoiceDetail; } } [JsonObject] public class NuoNuoQuanDian_Head : HXInvoice_Head { public NuoNuoQuanDian_Head() { } public NuoNuoQuanDian_Head(ChInvoice_HangXin _ChInvoice) { ChInvoice_HangXin = _ChInvoice; //需增加 //获取callback地址 } //public string redReason { get { return ChInvoice_HangXin.redReason; } } public string billInfoNo { get { return ChInvoice_HangXin.billInfoNo; } } public string departmentId { get; set; } = ""; public string clerkId { get; set; } = ""; public string listFlag { get; set; } = "0"; public string listName { get; set; } = ""; /// /// 全电纸票类型(全电纸票时才需要传):(票种为 ec 时,默认 04;票种为 es 时,默认为 1130); 04 2016 版增值税普通发票(二联折叠票) /// , 05 2016 版增值税普通发票(五联折叠票), 1130增值税专用发票(中文三联无金额限制版) /// , 1140 增值税专用发票(中文四联无金额限制版),1160 增值税专用发票(中文六联无金额限制版) /// , 1170 增值税专用发票(中文七联无金额限制版) /// public string paperInvoiceType { get { if (ChInvoice_HangXin.INVOICELINE == "ec") return "04"; if (ChInvoice_HangXin.INVOICELINE == "es") return "1130"; return ""; } } /// /// 特定要素:0 普通发票(默认)、 1 成品油 、 3 建筑服务、 4货物运输服务、 31 建安发票、 32 房地产销售发票 /// 目前默认普通发票 /// public string specificFactor { get; set; } = "0"; /// /// 回传发票信息地址(开票完成、开票失败) /// 增加系统配置项目 诺诺电票回调地址 /// public string callBackUrl { get; set; } = "0"; /// /// 分机号(开票终端号,只能 为空或数字) /// public string extensionNumber { get; set; } = "150"; /// /// 终端号(开票终端号,只能 为空或数字) /// public string terminalNumber { get; set; } = ""; /// /// 机器编号(12 位盘号) /// 增加系统配置项目 税盘盘号 /// public string machineCode { get; set; } = ""; /// /// 是否机动车类专票 0-否 1-是 /// public string vehicleFlag { get; set; } = "0"; /// /// 是否隐藏编码表版本号 0-否 1-是(默认 0,在企业资质中也配置为是隐藏的时候, /// 并且此字段传 1 的时候代开发票 税率显示***) /// public string hiddenBmbbbh { get; set; } = "0"; public string nextInvoiceCode { get; set; } = ""; public string nextInvoiceNum { get; set; } = ""; public string invoiceNumEnd { get; set; } = ""; public string surveyAnswerType { get; set; } = ""; public string buyerManagerName { get { return ChInvoice_HangXin.buyerManagerName; } } public string managerCardType { get { return ChInvoice_HangXin.managerCardType; } } public string managerCardNo { get { return ChInvoice_HangXin.managerCardNo; } } public string checker { get; set; } public string payee { get; set; } public List invoiceDetail; } [JsonObject] public class NuoNuoQuanDian_Detail : HXInvoice_Detail { public NuoNuoQuanDian_Detail() { } public NuoNuoQuanDian_Detail(ChInvoiceDetail ChInvoiceDetail) { this.ChInvoiceDetail = ChInvoiceDetail; //favouredPolicyNameDic = new Dictionary //{ // /// 全电票内容来自枚举类型80007 // /// 全电发票时: 01:简易征收 02:稀土产品 03:免税 04:不征税 05:先征后退 06:100%先征后退 07:50%先征后退 // /// 08:按 3%简易征收 09:按 5%简易征收 10:按 5%简易征收减按 1.5%计征 11:即征即退 30% 12:即征即退 50% // /// 13:即征即退 70% 14:即征即退 100% 15:超税负 3%即征即退 16:超税负 8%即征即退 // /// 17:超税负 12%即征即退 18:超税负6%即征即退 // //{ "1", "简易征收" }, // //{ "2", "稀土产品" }, // //{ "3", "免税" }, // //{ "4", "不征税" }, // //{ "5", "先征后退" }, // //{ "6", "100%先征后退" }, // //{ "7", "50%先征后退" }, // //{ "8", "按 3%简易征收" }, // //{ "9", "按 5%简易征收" }, // //{ "10", "按 5%简易征收减按 1.5%计征" }, // //{ "11", "即征即退 30%" }, // //{ "12", "即征即退 50%" }, // //{ "13", "即征即退 70%" }, // //{ "14", "即征即退 100%" }, // //{ "15", "超税负 3%即征即退" }, // //{ "16", "超税负 8%即征即退" }, // //{ "17", "超税负 12%即征即退" }, // //{ "18", "超税负6%即征即退" } // { "1", "免税" }, // { "0", "零税率" } //}; } //public string goodsCode { get; set; } = ""; public string selfCode { get; set; } = ""; /// /// 发票行性质:0,正常行;1,折扣行;2,被折扣行,红票只有正常行 /// public string invoiceLineProperty { get; set; } = "0"; //public string favouredPolicyName //{ // get // { // if (favouredPolicyNameDic.ContainsKey(favouredPolicyFlag)) // { // return favouredPolicyNameDic[favouredPolicyFlag]; // } // else // return ""; // } //} private Dictionary favouredPolicyNameDic { get; set; } } [JsonObject] public class NuoNuoQuanDian_SetRed : HXInvoice_Head { public NuoNuoQuanDian_SetRed() { } public NuoNuoQuanDian_SetRed(ChInvoice_HangXin _ChInvoice) { ChInvoice_HangXin = _ChInvoice; } public string billInfoNo { get { return ChInvoice_HangXin.billInfoNo; } } public string deptId { get; set; } = ""; public string departmentId { get; set; } = ""; public string clerkId { get; set; } = ""; public string listFlag { get; set; } = "0"; public string orderTime { get; set; } = ""; public string invoiceId { get { return ChInvoice_HangXin.INVOICESERIALNUM; } } public string billNo { get; set; } = ""; public string billUuid { get; set; } = ""; public string invoiceLine { get { return ChInvoice_HangXin.INVOICELINE; } } public string callBackUrl { get; set; } = ""; public string invoiceCode { get { return ChInvoice_HangXin.INVOICECODE; } } public string invoiceNumber { get { return ChInvoice_HangXin.INVOICENO; } } public string taxNum { get { return ChInvoice_HangXin.TAXCODE; } } public string extensionNumber { get; set; } = "150"; } [JsonObject] public class NuoNuoQuanDian_RedConfirm //: HXInvoice_Head { protected ChInvoice_HangXin ChInvoice_HangXin; public NuoNuoQuanDian_RedConfirm() { } public NuoNuoQuanDian_RedConfirm(ChInvoice_HangXin _ChInvoice) { ChInvoice_HangXin = _ChInvoice; //ChInvoice_HangXin.billUuid= _ChInvoice.billUuid; } /// /// 红字确认单申请号,需要保持唯一,不传的话系统自动生成一个; /// 此处使用蓝票的billno /// 20240423 改为使用红票的GID 并去除- /// 在初次冲红时记录在蓝票的billUuid /// public string billId { get; set; } /// /// 对应蓝票发票种类: bs:电子发票(增值税专用发票), pc:电子发票(普通发票),es:全电纸质发票(增值税专用发票), ec:全电纸质发票(普通发票 /// public string blueInvoiceLine { get { return ChInvoice_HangXin.INVOICELINE; } } /// /// 申请方(录入方)身份: 0 销方 1 购方 /// public string applySource { get; set; } = "0"; /// /// 对应蓝票全电号码(全电普票、全电专票都需要) /// public string blueInvoiceNumber { get { return ChInvoice_HangXin.REDNUM; } } public string billTime { get; set; } /// /// 销方税号 /// public string sellerTaxNo { get { return ChInvoice_HangXin.TAXCODE; } } /// /// 销方名称,申请说明为销方申请时可为空 /// public string sellerName { get; set; } = ""; /// /// 部门门店 id(诺诺网系统中的 id) /// public string departmentId { get; set; } = ""; /// /// 开票员 id(诺诺网系统中的 id) /// public string clerkId { get; set; } = ""; /// /// 购方税号 /// public string buyerTaxNo { get { return ChInvoice_HangXin.CUSTRATENO; } } ///// ///// 购方名称 父类已有 ///// public string buyerName { get { return ChInvoice_HangXin.INVOICECUSTNAME; } } /// /// 蓝字发票增值税用途(预留字段可为空): 1 勾选抵扣 2 出口退税 3 代办出口退税 4 不抵扣 /// public string vatUsage { get; set; } /// /// 蓝字发票消费税用途(预留字段可为空) /// public string saleTaxUsage { get; set; } /// /// 发票入账状态(预留字段可为空): 0 未入账 1已入账 /// public string accountStatus { get; set; } /// /// 冲红原因: 1 销货退回 2 开票有误 3 服务中止 4 销售折让 /// public string redReason { get { return ChInvoice_HangXin.redReason; } } /// /// 分机号 150 /// public string extensionNumber { get; set; } = "150"; /// /// 是否自动开票,0 否(不传默认 0)1 是;传 1时, /// 所申请的确认单变为购销双方已确认或无需确认状态时, /// 而自动开具红票。目前该字段不生效,电票都自动开,纸票都不自动 /// public string autoInvoice { get; set; } = "1"; //若有值,则在无需确认或购销双方已确认后去自动开红票,发票的开票单号用该值 public string orderNo { get { return ChInvoice_HangXin.BILLNO; } } public string phone { get { if (ChInvoice_HangXin.PUSHMOBILE == "0") return ""; else return ChInvoice_HangXin.PUSHMOBILE; } } public string email { get { return ChInvoice_HangXin.PUSHEMAIL; } } public string callbackUrl { get; set; } = ""; } /// /// 依托ds7系统中保存的红票,来读取当时发出的红字发票确认单 /// 由于当时发出的申请单编号,是红字发票的BILLNO,故可以依据这个进行读取 /// [JsonObject] public class NuoNuoQuanDian_ReadRedConfirm : HXInvoice_Head { public NuoNuoQuanDian_ReadRedConfirm() { } public NuoNuoQuanDian_ReadRedConfirm(ChInvoice_HangXin _ChInvoice) { ChInvoice_HangXin = _ChInvoice; } /// /// 操作方身份: 0 销方 1 购方 /// public string identity { get; set; } = "0"; /// /// 红字确认单状态(不传则查全部状态): /// 01 无需确认 /// 02销方录入待购方确认 03 购方录入待销方确认 /// 04 购销双方已确认 /// 05 作废(销方录入购方否认) /// 06 作废(购方录入销方否认) 07 作废(超 72 小时未确认) /// 08 作废(发起方已撤销) 09 作废(确认后撤销) 15 申请中 16 申请失败 /// public string billStatus { get; set; } /// /// 红字确认单申请号 ds7当中红票的billno /// 20240423 改为使用红票的GID 并去除- /// 在初次冲红时记录在蓝票的billUuid /// public string billId { get { if (string.IsNullOrWhiteSpace(ChInvoice_HangXin.billUuid)) { return ChInvoice_HangXin.GID; } else { return ChInvoice_HangXin.billUuid; } } } /// /// 红字确认单编号 /// public string billNo { get; set; } /// /// 红字确认单 uuid /// public string billUuid { get; set; } /// /// 填开起始时间,确认单申请号/编号/uuid 有值时,可为空, 允许最大查询范围为 90 天 /// public string billTimeStart { get; set; } /// /// 填开结束时间,确认单申请号/编号/uuid 有值时,可为空, 允许最大查询范围为 90 天 /// public string billTimeEnd { get; set; } /// /// 每页数量(默认 10,最大 50) /// public string pageSize { get; set; } /// /// 当前页码(默认 1) /// public string pageNo { get; set; } } /// /// 航信接口token管理 /// public class HangXinRequestHelper { public Boolean CanUse = true; public string ERRORMSG = ""; public static List HXTokenHelperList = new List(); #region 基础属性 /// /// 我公司的appKey和密码 不写入数据库 只存在程序内 /// /// /* 沙盒测试账户 需使用青岛鑫鹏威 * 339901999999142 Abcd1234 public static string appKey { get { return "SD23646802"; } } public static string appSecret { get { return "SDFAFF5B0AED4465"; } } */ /* 正式地址测试账户 纸票测试用 需使用深圳鑫鹏威 //339901999999516 public static string appKey { get { return "G6bvh0ke"; } } public static string appSecret { get { return "6850C5AED14E4177"; } } */ /* 正式地址 大简云*/ public string appKey { get; set; } = "23646802"; public string appSecret { get; set; } = "CBFAFF5B0AED4465"; public string extensionNumber { get; set; } = ""; /// /// 使用沙盒地址还是实际工作地址 /// public static string URL { get { //return sandboxUrl; //沙盒测试地址 return normalUrl; } } /// /// 商户模式的Token /// public string MerchantToken { get; set; } /// /// 在服务商模式当中,客户输入选定账户的认证信息后返回的一个随机码 /// 每次认证时会被更新。但执行getISVToken或refreshISVToken不会改变code /// public string code { get; set; } /// /// 税号 客户输入的选定账户 /// public string taxnum { get; set; } /// /// 接收诺诺平台返回值的url,需要在其中标好账户id,以对应正确的实例化helper对象 /// public static string redirectUri { get; set; } //private static string ISVToken { get; set; } /// /// getISVToken时返回 refreshToken 时不变 /// public string accessToken { get; set; } /// /// getISVToken时返回 refreshToken时改变 /// public string refreshToken { get; set; } /// /// 同一客户税号这个userid似乎不变 /// public string userId { get; set; } /// /// 如果此次返回内容有错误编号 则返回错误 /// public bool refstateTrue { get { if (!string.IsNullOrEmpty(error)) { return false; } else return true; } } public string error_description { get; set; } public string error { get; set; } private static string sandboxUrl = "https://sandbox.nuonuocs.cn/open/v1/services"; private static string normalUrl = "https://sdk.nuonuo.com/open/v1/services"; //爬虫服务器接口 private static string _IURL = "http://47.104.90.170:5200/fp/token/"; //private static string _IURL = "http://118.190.106.151:5200/fp/token/"; //沙盒爬虫 private static string _SandBoxURL = "http://47.104.90.170:5200/cs/token/"; //private static string _SandBoxURL = "http://118.190.106.151:5200/cs/token/"; private static string _url { get; set; } const string ContentType = "application/x-www-form-urlencoded;charset=UTF-8"; public static string returnStr { get; set; } protected static string getSendid() { return Guid.NewGuid().ToString().Replace("-", "").Substring(0, 32); // 唯一标识,由企业自己生成32位随机码 } private DateTime _tokenstarttime { get; set; } //public DateTime gettokenstarttime { get { return _tokenstarttime; } } public DateTime tokenstarttime { get { return _tokenstarttime; } set { _tokenstarttime = value; } } public DateTime tokenneedresettime { get { return _tokenstarttime.AddHours(23); } } /// /// 需要初始化。如果有用户认证的code,则返回true,否则需要前台弹出StartUrl窗口,录入认证信息; /// 20191122 改为接口维护token后 固定返回false /// public bool needStart { get { //if (string.IsNullOrEmpty(code)) //{ // return true; //} //else if (System.DateTime.Now > _tokenstarttime.AddHours(24)) //{ // return true; //} if (accessToken == "") return true; else return false; } } /// /// 需要刷新 即当前时间晚于上次刷新23小时 /// /// 20191122 改为接口维护token后 固定返回false /// public bool needresettoken { get { //if (System.DateTime.Now >= tokenneedresettime) //{ // return true; //} return false; } } /// /// 具备刷新条件 即token不为空 /// //public bool canresettoken //{ // get // { // if (!string.IsNullOrEmpty(refreshToken)) // return true; // else return false; // } //} #endregion //public bool ResetToken() //{ // //BasicDataRefDAL.SaveLog("tokenstarttime="+ tokenstarttime.ToString("yyyy-MM-dd HH:mm:ss")+ ";refreshToken=" + refreshToken + ";accessToken=" + accessToken + ";needresettoken=" + needresettoken.ToString() + ";canresettoken=" + canresettoken.ToString(), "诺诺平台", "刷新令牌", "判断"); // if (needresettoken && canresettoken) // { // refreshISVToken(); // return !needresettoken; // } // else return false; //} /// /// 商户获取访问令牌 /// /// //public string getMerchantToken() //{ // //BasicDataRefDAL.SaveLog("appKey="+appKey+ ";appSecret=" + appSecret, "诺诺平台", "商户获取访问令牌", "发送值"); // var result = NNOpenSDK.getMerchantToken(appKey, appSecret); // dealResponse_token(returnStr); // //DoSaveToken(); // BasicDataRefDAL.SaveLog(result, "诺诺平台", "商户获取访问令牌", "返回值"); // return result; //} //public string setMerchantToken() //{ // this.MerchantToken = getMerchantToken(); // return this.MerchantToken; //} /// /// ISV获取访问令牌 /// /// //private ISVTokenmb getISVToken() //{ // ISVTokenmb _ISVToken = new ISVTokenmb(); // //BasicDataRefDAL.SaveLog("appKey=" + appKey + ";appSecret=" + appSecret + ";code=" + code + ";taxnum=" + taxnum + ";redirectUri=" + redirectUri, "诺诺平台", "ISV获取访问令牌", "发送值"); // returnStr = NNOpenSDK.getISVToken(appKey, appSecret, code, taxnum, redirectUri); // BasicDataRefDAL.SaveLog(returnStr, "诺诺平台", "ISV获取访问令牌", "返回值"); // dealResponse_token(returnStr); // return _ISVToken; //} /// /// 用认证窗口返回的数据读取token /// /// //public string setISVToken() //{ // //此处应判断 // //1 token 是否已存在 如不存在则获取needStart // //如存在则判断更新时间needresettoken // //if (needStart || needresettoken) // //{ // var _ISVToken = getISVToken(); // return returnStr; // //} // //else return "no need to start Token"; // /*"{\"access_token\":\"98600c833f178852e2f4d51cexk9yegi\",\"refresh_token\":\"faf038898dec26eac62640aaed0506cb\",\"oauthUser\":\"{\\\"userName\\\":\\\"339901999999142\\\",\\\"registerType\\\":\\\"1\\\"}\",\"expires_in\":86356,\"userId\":\"d1aa0da6bc9840ae83c1120cecd057ac\"}"*/ // /*{"error_description":"The code is used a second time, please authorization again","error":"070317"}*/ // /*{"error_description":"Incorrect parameter or Authorization out of date","error":"070313"}*/ //} /// /// ISV获取刷新令牌 强制执行 /// /// //public string refreshISVToken() //{ // try // { // BasicDataRefDAL.SaveLog("refreshToken=" + refreshToken + ";userId=" + userId + ";appSecret=" + appSecret , "诺诺平台", "ISV获取刷新令牌", "发送值"); // returnStr = NNOpenSDK.refreshISVToken(refreshToken, userId, appSecret); // BasicDataRefDAL.SaveLog(returnStr, "诺诺平台", "ISV获取刷新令牌", "返回值"); // dealRefresh_token(returnStr); // return returnStr; // } // catch { // return ""; // } //} /// /// ISV获取刷新令牌 如非必要则不执行 /// /// //public string setrefreshISVToken() //{ // if (needStart) // { // return "need to start,can not refresh"; // } // if (needresettoken) // { // return (refreshISVToken()); // } // else return "no need to refresh"; //} public HangXinRequestHelper() { tokenstarttime = Convert.ToDateTime("1900-01-01"); } public HangXinRequestHelper(string taxnum) { this.taxnum = taxnum; if (HXTokenHelperList == null) HXTokenHelperList = new List(); HXTokenHelperList.Add(this); } public HangXinRequestHelper(string taxnum, string accessToken) { this.taxnum = taxnum; this.accessToken = accessToken; if (HXTokenHelperList == null) HXTokenHelperList = new List(); var curr = HXTokenHelperList.Find(o => o.taxnum == taxnum); if (!(curr == null)) { curr.accessToken = accessToken; } else { HXTokenHelperList.Add(this); } } /// /// 处理 获取令牌/刷新令牌 命令的返回值 /// /// //private void dealResponse_token(string response) { // var _ISVToken = DSWeb.TruckMng.Helper.JsonConvert.Deserialize(response); // error = _ISVToken.error; // if (string.IsNullOrEmpty(error)) // { // accessToken = _ISVToken.access_token; // refreshToken = _ISVToken.refresh_token; // userId = _ISVToken.userId; // var userinfo = _ISVToken._oauthUser; // taxnum = userinfo.userName; // tokenstarttime = System.DateTime.Now; // var i = BasicDataRefDAL.ExecSql(getUpdStr()); // if (i == 0) // { // try // { // i = BasicDataRefDAL.ExecSql(getInsertStr()); // BasicDataRefDAL.SaveLog(getInsertStr(), "诺诺平台", "保存Token", "Insert"); // } // catch { } // } // else // { // BasicDataRefDAL.SaveLog(getUpdStr(), "诺诺平台", "保存Token", "Update"); // } // } // else // { // error_description = _ISVToken.error_description; // } //} //private void dealRefresh_token(string response) { // var _ISVToken = DSWeb.TruckMng.Helper.JsonConvert.Deserialize(response); // error = _ISVToken.error; // if (string.IsNullOrEmpty(error)) // { // accessToken = _ISVToken.access_token; // refreshToken = _ISVToken.refresh_token; // var i = BasicDataRefDAL.ExecSql(getUpdStr()); // BasicDataRefDAL.SaveLog(getUpdStr(), "诺诺平台", "保存Token", "refresh Update"); // } // else // { // error_description = _ISVToken.error_description; // } //} public static HangXinRequestHelper getHelper(string taxnum) { //ReadToken(); //foreach (var h in HXTokenHelperList) //{ // if (h.taxnum == taxnum) // { // return h; // } //} //var result = new HangXinRequestHelper(taxnum); //return result; var _token = getPortToken(taxnum); return _token; } protected static HangXinRequestHelper getPortToken(string taxnum) { try { ///读取接口token //JObject reqObj = new JObject(); //reqObj.Add("taxnum", taxnum); var dic = new Dictionary(); dic.Add("taxnum", taxnum); var rtn = WebRequestHelper.DoPost(_IURL, dic, 10000); //var rtn = WebRequestHelper.DoPost(_SandBoxURL, dic, 20000); //RepCZModel CZM = Newtonsoft.Json.JsonConvert.DeserializeObject(rtn); PortTokenmb pt = Newtonsoft.Json.JsonConvert.DeserializeObject(rtn); if (pt.status == "1") { foreach (var hx in HXTokenHelperList) { if (hx.taxnum == taxnum) { hx.accessToken = pt.message; hx.CanUse = true; hx.ERRORMSG = ""; return hx; } } var result = new HangXinRequestHelper(taxnum, pt.message); return result; } else return new HangXinRequestHelper(taxnum, ""); } catch (Exception e) { var result = new HangXinRequestHelper(taxnum, ""); result.CanUse = false; result.ERRORMSG = "没有取得开票密钥:" + e.Message; return result; } } protected static HangXinRequestHelper getPortToken_诺诺全电发票自应用模式(string taxnum, string CORPID) { try { var result = new HangXinRequestHelper(); var INVITERFACE = MsSysInvInterFaceSetDAL.GetData($"INVITERFACE='诺诺全电发票' and CORPID='{CORPID}'", ""); if (string.IsNullOrWhiteSpace(INVITERFACE.NUONUOAPPKEY)) { return result; } else { result.appKey = INVITERFACE.NUONUOAPPKEY; result.appSecret = INVITERFACE.NUONUOAPPSECRET; result.extensionNumber = INVITERFACE.extensionNumber; } var dic = new Dictionary { { "taxnum", taxnum }, { "client_id", result.appKey }, { "client_secret", result.appSecret }, { "regain", "0" } }; //诺诺全电token接口 var porturl = "http://47.104.90.170:5200/fp/free_type/"; var rtn = WebRequestHelper.DoPost(porturl, dic, 10000); //var rtn = WebRequestHelper.DoPost(_SandBoxURL, dic, 20000); //RepCZModel CZM = Newtonsoft.Json.JsonConvert.DeserializeObject(rtn); PortTokenmb pt = Newtonsoft.Json.JsonConvert.DeserializeObject(rtn); if (pt.status == "1") { //foreach (var hx in HXTokenHelperList) //{ // if (hx.taxnum == taxnum) // { // hx.accessToken = pt.message; // hx.CanUse = true; // hx.ERRORMSG = ""; // return hx; // } //} result.accessToken = pt.message; return result; } else return new HangXinRequestHelper(taxnum, ""); } catch (Exception e) { var result = new HangXinRequestHelper(taxnum); result.CanUse = false; result.ERRORMSG = "没有取得开票密钥:" + e.Message; return result; } } /// /// 20231016 增加 /// /// /// /// protected static HangXinRequestHelper 诺诺全电发票重置token(string taxnum, string CORPID) { try { var result = new HangXinRequestHelper(); var INVITERFACE = MsSysInvInterFaceSetDAL.GetData($"INVITERFACE='诺诺全电发票' and CORPID='{CORPID}'", ""); if (string.IsNullOrWhiteSpace(INVITERFACE.NUONUOAPPKEY)) { return result; } else { result.appKey = INVITERFACE.NUONUOAPPKEY; result.appSecret = INVITERFACE.NUONUOAPPSECRET; result.extensionNumber = INVITERFACE.extensionNumber; } var dic = new Dictionary { { "taxnum", taxnum }, { "client_id", result.appKey }, { "client_secret", result.appSecret }, { "regain", "1" } }; //诺诺全电token接口 var porturl = "http://47.104.90.170:5200/fp/free_type/"; var rtn = WebRequestHelper.DoPost(porturl, dic, 10000); //var rtn = WebRequestHelper.DoPost(_SandBoxURL, dic, 20000); //RepCZModel CZM = Newtonsoft.Json.JsonConvert.DeserializeObject(rtn); PortTokenmb pt = Newtonsoft.Json.JsonConvert.DeserializeObject(rtn); if (pt.status == "1") { //foreach (var hx in HXTokenHelperList) //{ // if (hx.taxnum == taxnum) // { // hx.accessToken = pt.message; // hx.CanUse = true; // hx.ERRORMSG = ""; // return hx; // } //} result.accessToken = pt.message; return result; } else return new HangXinRequestHelper(taxnum, ""); } catch (Exception e) { var result = new HangXinRequestHelper(taxnum); result.CanUse = false; result.ERRORMSG = "没有取得开票密钥:" + e.Message; return result; } } /// /// 开出发票 /// /// /// /// public DBResult PostInvoice(ChInvoice_HangXin head, List detailList) { var billno = head.BILLNO; //20191028 如果该发票的状态为开票失败 则将其billno临时变更为billno+时分秒,以避开诺诺平台的orderNo不能重复的逻辑 //然后处理返回值时将新的流水号写回。 //20191108 //if (head.EINVOICESTATE == "3"|| head.EINVOICESTATE == "0") //{ // head.BILLNO= billno+ DateTime.Now.ToString("HHmmss"); //} var _head = new HXInvoice(head, detailList); var json = DSWeb.TruckMng.Helper.JsonConvert.Serialize(_head); var _r = ""; _r = PostInvoice(json); var _return = dealresponse_KP(_r); if (_return.code == "E0000") { //开票提交成功 var invoiceSerialNum = _return.result.invoiceSerialNum; //立即调用读取发票信息的方法 将返回值设置进head 并保存 head.INVOICESERIALNUM = invoiceSerialNum; head.BILLNO = billno; head.DbOperationType = DbOperationType.DbotUpd; head.ModelUIStatus = "E"; var modb = new ModelObjectDB(); var result = modb.Save(head); result = ReadInvoice(head); //var returndate = ChinvoiceDAL.GetDataList(0, 1, "cm.INVOICESERIALNUM='" + head.INVOICESERIALNUM + "'", head.OPERATOR); //result.Data = returndate; return result; } else if (_return.code == "070101" || _return.code == "070301") { //appkey与token问题 需要重新认证 //HangXinRequestHelper.clear(accessToken); var result = new DBResult(false, "需要重新认证", null); return result; } else if (_return.code == "E9106") { //E9106 订单编号不能重复 //同billno重复发送 执行一次用订单号读取 return ReadInvoice_BILLNO(head); } else { var reault = new DBResult(false, _return.message, head); return reault; } } public string PostInvoice(string json) { string method = "nuonuo.electronInvoice.requestBilling"; BasicDataRefDAL.SaveLog("URL=" + URL + ";appKey=" + appKey + ";appSecret=" + appSecret + ";accessToken=" + accessToken + ";taxnum=" + taxnum + ";method=" + method + ";content=" + json, "诺诺平台", "发票开出", "发送值"); string result = NNOpenSDK.sendPostSyncRequest(URL, getSendid(), appKey, appSecret, accessToken, taxnum, method, json); BasicDataRefDAL.SaveLog(result, "诺诺平台", "发票开出", "返回值"); return result; } public DBResult ReadInvoice(string INVOICESERIALNUM, string USERID) { var head = ChinvoiceDAL.GetData("cm.INVOICESERIALNUM='" + INVOICESERIALNUM + "'"); var _r = ReadInvoice(head); return _r; } public DBResult ReadInvoice(ChInvoice_HangXin head) { if (string.IsNullOrEmpty(head.INVOICESERIALNUM)) { var result = new DBResult { Success = false, Message = "还未成功进行平台开票,无可读数据", Data = "" }; return result; } string method = "nuonuo.electronInvoice.CheckEInvoice"; var json = "{" + "\"invoiceSerialNum\":[\"" + head.INVOICESERIALNUM + "\"]" + "}"; if (string.IsNullOrWhiteSpace(head.INVOICESERIALNUM)) { //如果流水号为空 则改为按业务编号读取 method = "nuonuo.electronInvoice.querySerialNum"; json = "{" + "\"orderNo\":[\"" + head.BILLNO + "\"]" + "}"; } //读取业务 //string method = "nuonuo.electronInvoice.querySerialNum"; //var json = "{" + "\"orderNo\":[\"" + head.BILLNO + "\"]" + "}"; BasicDataRefDAL.SaveLog("URL=" + URL + ";appKey=" + appKey + ";appSecret=" + appSecret + ";accessToken=" + accessToken + ";taxnum=" + taxnum + ";method=" + method + ";content=" + json, "诺诺平台", "读取发票信息", "发送值"); string _r = NNOpenSDK.sendPostSyncRequest(URL, getSendid(), appKey, appSecret, accessToken, taxnum, method, json); BasicDataRefDAL.SaveLog(_r, "诺诺平台", "读取发票信息", "返回值"); var _return = dealresponse_Read(_r); if (_return.code == "070101" || _return.code == "070301") { //appkey与token问题 需要重新认证 //HangXinRequestHelper.clear(accessToken); var result = new DBResult(false, "需要重新认证", null); return result; } if (_return.code == "070302") { //appkey与token问题 需要重新认证 //HangXinRequestHelper.clear(accessToken); var result = new DBResult(false, "需要联系诺诺平台开通相关权限", null); return result; } if (_return.code == "E0000") { //开票信息读取成功 var returndate = ChinvoiceDAL.GetData("cm.INVOICESERIALNUM='" + head.INVOICESERIALNUM + "'"); head = returndate; //var invoiceSerialNum = _return.result[0].invoiceSerialNum; var info = _return.result[0]; //立即调用读取发票信息的方法 将返回值设置进head 并保存 //head.INVOICESERIALNUM = invoiceSerialNum; head.INVOICECODE = info.invoiceCode; head.INVOICENO = info.invoiceNum; head.EINVOICESTATE = info.status; head.INVOICEINFOURL = info.invoiceImageUrl; head.INVOICEPDFURL = info.invoiceFileUrl; head.INVOICEMAKETIME = info.invoiceDate; //if (!string.IsNullOrWhiteSpace(info.invoiceSerialNum)) //{ // head.INVOICESERIALNUM = info.invoiceSerialNum; //} //head.REMARK = head.REMARK.Replace(info.remark,"")+ info.remark; head.DbOperationType = DbOperationType.DbotUpd; head.ModelUIStatus = "E"; var modb = new ModelObjectDB(); var result = modb.Save(head); result.Message = info.resultMsg; if (info.resultMsg.Contains("开具结果:0,开具失败原因:开票异常")) { result.Message = "开票异常,请尝试重新连接税控盘、重新启动税控软件和诺诺客户端,然后再次读取发票信息;如仍显示此提示,请联系诺诺平台客服人员,并提供销方税号和此发票的流水号"; } return result; } else { var msg = _return.code + ":" + _return.describe; var result = new DBResult { Success = false, Message = DSWeb.TruckMng.Helper.JsonConvert.Serialize(msg), Data = "" }; return result; } } public DBResult ReadInvoice_BILLNO(ChInvoice_HangXin head) { //读取业务 string method = "nuonuo.electronInvoice.querySerialNum"; var json = "{" + "\"orderNo\":[\"" + head.BILLNO + "\"]" + "}"; BasicDataRefDAL.SaveLog("URL=" + URL + ";appKey=" + appKey + ";appSecret=" + appSecret + ";accessToken=" + accessToken + ";taxnum=" + taxnum + ";method=" + method + ";content=" + json, "诺诺平台", "读取发票信息", "发送值"); string _r = NNOpenSDK.sendPostSyncRequest(URL, getSendid(), appKey, appSecret, accessToken, taxnum, method, json); BasicDataRefDAL.SaveLog(_r, "诺诺平台", "读取发票信息", "返回值"); var _return = dealresponse_Read(_r); if (_return.code == "070101" || _return.code == "070301") { //appkey与token问题 需要重新认证 //HangXinRequestHelper.clear(accessToken); var result = new DBResult(false, "需要重新认证", null); return result; } if (_return.code == "070302") { //appkey与token问题 需要重新认证 //HangXinRequestHelper.clear(accessToken); var result = new DBResult(false, "需要联系诺诺平台开通相关权限", null); return result; } if (_return.code == "E0000") { //开票信息读取成功 var returndate = ChinvoiceDAL.GetData("cm.INVOICESERIALNUM='" + head.INVOICESERIALNUM + "'"); head = returndate; //var invoiceSerialNum = _return.result[0].invoiceSerialNum; var info = _return.result[0]; //立即调用读取发票信息的方法 将返回值设置进head 并保存 //head.INVOICESERIALNUM = invoiceSerialNum; head.INVOICECODE = info.invoiceCode; head.INVOICENO = info.invoiceNum; head.EINVOICESTATE = info.status; head.INVOICEINFOURL = info.invoiceImageUrl; head.INVOICEPDFURL = info.invoiceFileUrl; head.INVOICEMAKETIME = info.invoiceDate; if (!string.IsNullOrWhiteSpace(info.invoiceSerialNum)) { head.INVOICESERIALNUM = info.invoiceSerialNum; } //head.REMARK = head.REMARK.Replace(info.remark,"")+ info.remark; head.DbOperationType = DbOperationType.DbotUpd; head.ModelUIStatus = "E"; var modb = new ModelObjectDB(); var result = modb.Save(head); result.Message = info.resultMsg; if (info.resultMsg.Contains("开具结果:0,开具失败原因:开票异常")) { result.Message = "开票异常,请尝试重新连接税控盘、重新启动税控软件和诺诺客户端,然后再次读取发票信息;如仍显示此提示,请联系诺诺平台客服人员,并提供销方税号和此发票的流水号"; } return result; } else { var msg = _return.code + ":" + _return.describe; var result = new DBResult { Success = false, Message = DSWeb.TruckMng.Helper.JsonConvert.Serialize(msg), Data = "" }; return result; } } public DBResult VoidInvoice(ChInvoice_HangXin head) { if (string.IsNullOrEmpty(head.INVOICESERIALNUM)) { var result = new DBResult { Success = false, Message = "还未成功进行平台开票,不需要执行作废", Data = "" }; return result; } //作废接口 string method = "nuonuo.electronInvoice.invoiceCancellation"; // String content = "{" + //"\"invoiceId\":\"19051413444101000011\"," + //"\"invoiceCode\":\"033001800211\"," + //"\"invoiceNo\":\"07912653\"" + //"}"; var json = "{" + "\"invoiceId\":\"" + head.INVOICESERIALNUM + "\"," + "\"invoiceCode\":\"" + head.INVOICECODE + "\"," + "\"invoiceNo\":\"" + head.INVOICENO + "\"" + "}"; BasicDataRefDAL.SaveLog("URL=" + URL + ";appKey=" + appKey + ";appSecret=" + appSecret + ";accessToken=" + accessToken + ";taxnum=" + taxnum + ";method=" + method + ";content=" + json, "诺诺平台", "发票作废", "发送值"); string _r = NNOpenSDK.sendPostSyncRequest(URL, getSendid(), appKey, appSecret, accessToken, taxnum, method, json); BasicDataRefDAL.SaveLog(_r, "诺诺平台", "发票作废", "返回值"); var _return = dealresponse_KP(_r); if (_return.code == "E0000") { //作废请求成功 var invoiceSerialNum = _return.result.invoiceId; head.INVOICESERIALNUM = invoiceSerialNum; head.ISDELETE = "true"; var result = ReadInvoice(head); //var result = new DBResult { Success = true, Message = _return.code + ":" + _return.describe, Data = head }; return result; } else { var msg = _return.code + _return.status + ":" + _return.describe + _return.msg; var result = new DBResult { Success = false, Message = DSWeb.TruckMng.Helper.JsonConvert.Serialize(msg), Data = "" }; return result; } } /// /// 处理开票命令返回值 /// /// /// private KPResponse dealresponse_KP(string json) { var result = new KPResponse(); result = DSWeb.TruckMng.Helper.JsonConvert.Deserialize(json.Trim('"')); return result; } /// /// 处理 读取发票信息命令 /// /// /// private ReadResponse dealresponse_Read(string json) { var result = new ReadResponse(); result = DSWeb.TruckMng.Helper.JsonConvert.Deserialize(json.Trim('"')); return result; } /// /// 执行save操作 /// //private void DoSaveToken() //{ // if (HXTokenHelperList==null ||HXTokenHelperList.Count == 0) return; // SaveToken(); //} /// /// 将数据库内记录的token信息读入static列表 /// private static void ReadToken() { //BasicDataRefDAL.SaveLog(" SELECT * from Invoice_HangXinToken ", "诺诺平台", "读取数据库token", "读取数据库token"); /* 20191210 废弃 var strSql = " SELECT * from Invoice_HangXinToken "; Database db = DatabaseFactory.CreateDatabase(); bool neednew = false; using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { neednew = true; if (HXTokenHelperList == null) HXTokenHelperList = new List(); foreach (var hx in HXTokenHelperList) { //BasicDataRefDAL.SaveLog(" 循环税号token:" + hx.taxnum+";reader税号:"+ Convert.ToString(reader["TAXNUM"]), "诺诺平台", "读取数据库token", "读取数据库token"); if (hx.taxnum == Convert.ToString(reader["TAXNUM"])) { //BasicDataRefDAL.SaveLog(" 刷新税号token:"+ hx.taxnum, "诺诺平台", "读取数据库token", "读取数据库token"); hx.code = Convert.ToString(reader["CODE"]); hx.userId = Convert.ToString(reader["USERID"]); hx.accessToken = Convert.ToString(reader["ACCESSTOKEN"]); hx.refreshToken = Convert.ToString(reader["REFRESHTOKEN"]); if (reader["STARTTIME"] != DBNull.Value) hx.tokenstarttime = Convert.ToDateTime(reader["STARTTIME"]); //if (reader["SAVETIME"] != DBNull.Value) // hx = Convert.ToString(reader["SAVETIME"]); hx.error = Convert.ToString(reader["ERROR"]); hx.error_description = Convert.ToString(reader["ERROR_DESCRIPTION"]); neednew = false; break; } } if (neednew) { var hx = new HangXinRequestHelper(Convert.ToString(reader["TAXNUM"])); //BasicDataRefDAL.SaveLog(" 增加税号token:" + hx.taxnum, "诺诺平台", "读取数据库token", "读取数据库token"); hx.code = Convert.ToString(reader["CODE"]); hx.userId = Convert.ToString(reader["USERID"]); hx.accessToken = Convert.ToString(reader["ACCESSTOKEN"]); hx.refreshToken = Convert.ToString(reader["REFRESHTOKEN"]); if (reader["STARTTIME"] != DBNull.Value) hx.tokenstarttime = Convert.ToDateTime(reader["STARTTIME"]); //if (reader["SAVETIME"] != DBNull.Value) // hx = Convert.ToString(reader["SAVETIME"]); hx.error = Convert.ToString(reader["ERROR"]); hx.error_description = Convert.ToString(reader["ERROR_DESCRIPTION"]); } //BasicDataRefDAL.SaveLog(HangXinRequestHelper.HXTokenHelperList.Count.ToString(), "诺诺平台", "Token实体类总数", "读取数据库token"); } } */ } /// /// 保存token信息,当获取和刷新时执行一次 /// /// /// //private bool SaveToken() { // bool result = false; // var i = BasicDataRefDAL.ExecSql(getUpdStr()); // if (i == 0) { // try // { // i = BasicDataRefDAL.ExecSql(getInsertStr()); // BasicDataRefDAL.SaveLog(getInsertStr(), "诺诺平台", "保存Token", "SaveToken"); // } // catch { } // } // else { // BasicDataRefDAL.SaveLog(getUpdStr(), "诺诺平台", "保存Token", "SaveToken"); // } // if (i > 0) result = true; // return result; //} /// /// 获取插入数据库所需的语句 /// Invoice_HangXinToken /// TAXNUM,CODE,USERID,ACCESSTOKEN,REFRESHTOKEN,STARTTIME,SAVETIME,ERROR,ERROR_DESCRIPTION /// /// private string getInsertStr() { var result = string.Format(" insert into Invoice_HangXinToken values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}')", taxnum, code, userId, accessToken, refreshToken, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), error, error_description); return result; } private string getUpdStr() { var result = string.Format(" update Invoice_HangXinToken set CODE='{1}',USERID='{2}',ACCESSTOKEN='{3}',REFRESHTOKEN='{4}',STARTTIME='{5}',SAVETIME='{6}',ERROR='{7}',ERROR_DESCRIPTION='{8}' where TAXNUM='{0}'", taxnum, code, userId, accessToken, refreshToken, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), error, error_description); return result; } } public class 诺诺全电发票 : HangXinRequestHelper { public static 诺诺全电发票 getHelper(ChInvoice_HangXin head)//string taxnum,string CORPID) { var _token = getPortToken_诺诺全电发票自应用模式(head.TAXCODE, head.SALECORPID); 诺诺全电发票 result = new 诺诺全电发票(); result.appKey = _token.appKey; result.appSecret = _token.appSecret; result.accessToken = _token.accessToken; result.taxnum = head.TAXCODE; result.extensionNumber = _token.extensionNumber; return result; } public static 诺诺全电发票 RegainToken(string TAXCODE, string SALECORPID)//string taxnum,string CORPID) { var _token = 诺诺全电发票重置token(TAXCODE, SALECORPID); 诺诺全电发票 result = new 诺诺全电发票(); result.appKey = _token.appKey; result.appSecret = _token.appSecret; result.accessToken = _token.accessToken; result.taxnum = TAXCODE; result.extensionNumber = _token.extensionNumber; return result; } //接口函数的地址不同 public DBResult ReadInvoice(ChInvoice_HangXin head) { if (string.IsNullOrEmpty(head.INVOICESERIALNUM) && !head.全电发票()) { var result = new DBResult { Success = false, Message = "还未成功进行平台开票,无可读数据", Data = "" }; return result; } string method = "nuonuo.OpeMplatform.queryInvoiceResult"; var json = "{" + "\"serialNos\":[\"" + head.INVOICESERIALNUM + "\"]," + "\"orderNo\":[]," + "\"isOfferInvoiceDetail\":0 }"; if (string.IsNullOrWhiteSpace(head.INVOICESERIALNUM)) { //如果流水号为空 则改为按业务编号读取 method = "nuonuo.OpeMplatform.queryInvoiceResult"; json = "{" + "\"orderNos\":[\"" + head.BILLNO + "\"]," + "\"serialNos\":[]," + "\"isOfferInvoiceDetail\":0 }"; } //读取业务 //string method = "nuonuo.electronInvoice.querySerialNum"; //var json = "{" + "\"orderNo\":[\"" + head.BILLNO + "\"]" + "}"; BasicDataRefDAL.SaveLog("URL=" + URL + ";appKey=" + appKey + ";appSecret=" + appSecret + ";accessToken=" + accessToken + ";taxnum=" + taxnum + ";method=" + method + ";content=" + json, "诺诺平台", "读取发票信息", "发送值"); string _r = NNOpenSDK.sendPostSyncRequest(URL, getSendid(), appKey, appSecret, accessToken, taxnum, method, json); BasicDataRefDAL.SaveLog(_r, "诺诺平台", "读取发票信息", "返回值"); var _return = dealresponse_Read(_r); if (_return.code == "070101" || _return.code == "070301") { //appkey与token问题 需要重新认证 //HangXinRequestHelper.clear(accessToken); var result = new DBResult(false, "需要重新认证", null); return result; } if (_return.code == "070302") { //appkey与token问题 需要重新认证 //HangXinRequestHelper.clear(accessToken); var result = new DBResult(false, "需要联系诺诺平台开通相关权限", null); return result; } if (_return.code == "E0000") { //开票信息读取成功 var returndate = new ChInvoice_HangXin(); if (!head.全电发票()) { returndate = ChinvoiceDAL.GetData("cm.INVOICESERIALNUM='" + head.INVOICESERIALNUM + "'"); } if (head.全电发票()) { returndate = ChinvoiceDAL.GetData("cm.BILLNO='" + head.BILLNO + "'"); } head = returndate; //var invoiceSerialNum = _return.result[0].invoiceSerialNum; var info = _return.result[0]; //立即调用读取发票信息的方法 将返回值设置进head 并保存 //head.INVOICESERIALNUM = invoiceSerialNum; head.INVOICECODE = info.invoiceCode; head.INVOICENO = info.invoiceNo; head.EINVOICESTATE = info.status; head.INVOICEINFOURL = info.pdfUrl; head.INVOICEPDFURL = info.pdfUrl; head.INVOICEMAKETIME = BasicDataRefDAL.Timestamp2Datetime(info.invoiceTime).ToString("yyyy-MM-dd HH:mm:ss"); //if (!string.IsNullOrWhiteSpace(info.invoiceSerialNum)) //{ // head.INVOICESERIALNUM = info.invoiceSerialNum; //} //head.REMARK = head.REMARK.Replace(info.remark,"")+ info.remark; head.DbOperationType = DbOperationType.DbotUpd; head.ModelUIStatus = "E"; //head.INVOICEMAKETIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); var modb = new ModelObjectDB(); var result = modb.Save(head); if (!result.Success) return result; result.Message = info.statusMsg; if (result.Message.IndexOf("失败") >= 0) { result.Success = false; result.Message += ":" + info.failCause; //此处调用一次重推 var hx = getHelper(head); hx.重推(head); } else { result.Message += ":" + info.failCause; } result.Data = head; return result; } else { var failCause = ""; try { var info = _return.result[0]; failCause = "[" + info.failCause + "]"; } catch { } var msg = _return.code + ":" + _return.describe + failCause; var result = new DBResult { Success = false, Message = DSWeb.TruckMng.Helper.JsonConvert.Serialize(msg), Data = "" }; return result; } } public DBResult ReadRedInvoice(ChInvoice_HangXin head) { if (string.IsNullOrEmpty(head.INVOICESERIALNUM) && !head.全电发票()) { var result = new DBResult { Success = false, Message = "还未成功进行平台开票,无可读数据", Data = "" }; return result; } string method = "nuonuo.OpeMplatform.queryInvoiceResult"; var json = "{" + "\"serialNos\":[\"" + head.INVOICESERIALNUM + "\"]," + "\"orderNo\":[]," + "\"isOfferInvoiceDetail\":0 }"; if (string.IsNullOrWhiteSpace(head.INVOICESERIALNUM)) { //如果流水号为空 则改为按业务编号读取 method = "nuonuo.OpeMplatform.queryInvoiceResult"; json = "{" + "\"orderNos\":[\"" + head.GID.Replace("-", "") + "\"]," + "\"serialNos\":[]," + "\"isOfferInvoiceDetail\":0 }"; } //读取业务 //string method = "nuonuo.electronInvoice.querySerialNum"; //var json = "{" + "\"orderNo\":[\"" + head.BILLNO + "\"]" + "}"; BasicDataRefDAL.SaveLog("URL=" + URL + ";appKey=" + appKey + ";appSecret=" + appSecret + ";accessToken=" + accessToken + ";taxnum=" + taxnum + ";method=" + method + ";content=" + json, "诺诺平台", "读取发票信息", "发送值"); string _r = NNOpenSDK.sendPostSyncRequest(URL, getSendid(), appKey, appSecret, accessToken, taxnum, method, json); BasicDataRefDAL.SaveLog(_r, "诺诺平台", "读取发票信息", "返回值"); var _return = dealresponse_Read(_r); if (_return.code == "070101" || _return.code == "070301") { //appkey与token问题 需要重新认证 //HangXinRequestHelper.clear(accessToken); var result = new DBResult(false, "需要重新认证", null); return result; } if (_return.code == "070302") { //appkey与token问题 需要重新认证 //HangXinRequestHelper.clear(accessToken); var result = new DBResult(false, "需要联系诺诺平台开通相关权限", null); return result; } if (_return.code == "E0000") { //开票信息读取成功 var returndate = new ChInvoice_HangXin(); if (!head.全电发票()) { returndate = ChinvoiceDAL.GetData("cm.INVOICESERIALNUM='" + head.INVOICESERIALNUM + "'"); } if (head.全电发票()) { returndate = ChinvoiceDAL.GetData("cm.BILLNO='" + head.BILLNO + "'"); } head = returndate; //var invoiceSerialNum = _return.result[0].invoiceSerialNum; var info = _return.result[0]; //立即调用读取发票信息的方法 将返回值设置进head 并保存 //head.INVOICESERIALNUM = invoiceSerialNum; head.INVOICECODE = info.invoiceCode; head.INVOICENO = info.invoiceNo; head.EINVOICESTATE = info.status; head.INVOICEINFOURL = info.pdfUrl; head.INVOICEPDFURL = info.pdfUrl; head.INVOICEMAKETIME = BasicDataRefDAL.Timestamp2Datetime(info.invoiceTime).ToString("yyyy-MM-dd HH:mm:ss"); //if (!string.IsNullOrWhiteSpace(info.invoiceSerialNum)) //{ // head.INVOICESERIALNUM = info.invoiceSerialNum; //} //head.REMARK = head.REMARK.Replace(info.remark,"")+ info.remark; head.DbOperationType = DbOperationType.DbotUpd; head.ModelUIStatus = "E"; //head.INVOICEMAKETIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); var modb = new ModelObjectDB(); var result = modb.Save(head); if (!result.Success) return result; result.Message = info.statusMsg; if (result.Message.IndexOf("失败") >= 0) { result.Success = false; result.Message += ":" + info.failCause; //此处调用一次重推 var hx = getHelper(head); hx.重推(head); } else { result.Message += ":" + info.failCause; } result.Data = head; return result; } else { var failCause = ""; try { var info = _return.result[0]; failCause = "[" + info.failCause + "]"; } catch { } var msg = _return.code + ":" + _return.describe + failCause; var result = new DBResult { Success = false, Message = DSWeb.TruckMng.Helper.JsonConvert.Serialize(msg), Data = "" }; return result; } } public DBResult 重推(ChInvoice_HangXin head) { var result = new DBResult(); if (string.IsNullOrEmpty(head.INVOICESERIALNUM)) { result = new DBResult { Success = false, Message = "还未成功进行平台开票,无重试数据", Data = "" }; return result; } var senid = Guid.NewGuid().ToString(); string method = "nuonuo.OpeMplatform.reInvoice"; var content = new { fpqqlsh = head.INVOICESERIALNUM, orderno = head.BILLNO, nextInvoiceCode = "", invoiceNumStart = "", invoiceNumEnd = "" }; var json = DSWeb.MvcShipping.Helper.JsonConvert.Serialize(content); //"{" + "\"fpqqlsh\":\"" + head.INVOICESERIALNUM + "\"," + "\"orderNo\":[]," + "\"isOfferInvoiceDetail\":0 }"; //读取业务 //string method = "nuonuo.electronInvoice.querySerialNum"; //var json = "{" + "\"orderNo\":[\"" + head.BILLNO + "\"]" + "}"; BasicDataRefDAL.SaveLog("URL=" + URL + ";appKey=" + appKey + ";appSecret=" + appSecret + ";accessToken=" + accessToken + ";taxnum=" + taxnum + ";method=" + method + ";content=" + json, "诺诺平台", "重推发票信息", "发送值"); string _r = NNOpenSDK.sendPostSyncRequest(URL, getSendid(), appKey, appSecret, accessToken, taxnum, method, json); BasicDataRefDAL.SaveLog(_r, "诺诺平台", "重推发票信息", "返回值"); var _return = dealresponse_Read(_r); return result; } private ReadResponse_诺诺全电发票 dealresponse_Read(string json) { var result = new ReadResponse_诺诺全电发票(); result = DSWeb.TruckMng.Helper.JsonConvert.Deserialize(json.Trim('"')); return result; } public DBResult PostInvoice(ChInvoice_HangXin head, List detailList) { var billno = head.BILLNO; var _head = new NuoNuoQuanDianInvoice(head, detailList); _head.order.extensionNumber = extensionNumber; var json = DSWeb.TruckMng.Helper.JsonConvert.Serialize(_head); var _r = ""; _r = PostInvoice(json); var _return = dealresponse_KP(_r); if (_return.code == "E0000") { //开票提交成功 var invoiceSerialNum = _return.result.invoiceSerialNum; //立即调用读取发票信息的方法 将返回值设置进head 并保存 head.INVOICESERIALNUM = invoiceSerialNum; head.BILLNO = billno; head.DbOperationType = DbOperationType.DbotUpd; head.ModelUIStatus = "E"; var modb = new ModelObjectDB(); var result = modb.Save(head); result = ReadInvoice(head); //var returndate = ChinvoiceDAL.GetDataList(0, 1, "cm.INVOICESERIALNUM='" + head.INVOICESERIALNUM + "'", head.OPERATOR); //result.Data = returndate; return result; } else if (_return.code == "070101" || _return.code == "070301") { //appkey与token问题 需要重新认证 //HangXinRequestHelper.clear(accessToken); var result = new DBResult(false, "需要重新认证", null); return result; } else if (_return.code == "E9106") { //E9106 订单编号不能重复 //同billno重复发送 执行一次用订单号读取 return ReadInvoice_BILLNO(head); } else { var reault = new DBResult(false, _return.message, head); return reault; } } private string PostInvoice(string json) { string method = "nuonuo.OpeMplatform.requestBillingNew"; BasicDataRefDAL.SaveLog("URL=" + URL + ";appKey=" + appKey + ";appSecret=" + appSecret + ";accessToken=" + accessToken + ";taxnum=" + taxnum + ";method=" + method + ";content=" + json, "诺诺平台", "发票开出", "发送值"); string result = NNOpenSDK.sendPostSyncRequest(URL, getSendid(), appKey, appSecret, accessToken, taxnum, method, json); BasicDataRefDAL.SaveLog(result, "诺诺平台", "发票开出", "返回值"); return result; } /// /// 诺诺全电 快速冲红 /// /// /// /// public DBResult PostRedInvoice(ref ChInvoice_HangXin head) { //var billno = head.BILLNO; var _head = new NuoNuoQuanDian_SetRed(head); var json = DSWeb.TruckMng.Helper.JsonConvert.Serialize(_head); string method = "nuonuo.OpeMplatform.fastInvoiceRed"; BasicDataRefDAL.SaveLog("URL=" + URL + ";appKey=" + appKey + ";appSecret=" + appSecret + ";accessToken=" + accessToken + ";taxnum=" + taxnum + ";method=" + method + ";content=" + json, "诺诺平台", "快捷冲红", "发送值"); string rtn = NNOpenSDK.sendPostSyncRequest(URL, getSendid(), appKey, appSecret, accessToken, taxnum, method, json); BasicDataRefDAL.SaveLog(rtn, "诺诺平台", "快捷冲红", "返回值"); var _return = dealresponse_KP(rtn); var result = new DBResult(); if (_return.code == "E0000") { //冲红提交成功 var invoiceSerialNum = _return.result.invoiceSerialNum; //立即调用读取发票信息的方法 将返回值设置进head 并保存 head.INVOICESERIALNUM = invoiceSerialNum; return result.OK(); } else if (_return.code == "070101" || _return.code == "070301") { //appkey与token问题 需要重新认证 //HangXinRequestHelper.clear(accessToken); result.SetErrorInfo("需要重新认证"); return result; } else if (_return.code == "E9106") { //E9106 订单编号不能重复 //同billno重复发送 执行一次用订单号读取 return ReadInvoice_BILLNO(head); } else { var reault = new DBResult(false, _return.message, head); return reault; } } /// /// 诺诺全电冲红 红字确认单申请 发出红字确认单 /// /// /// /// public DBResult PostRedConfirm(ChInvoice_HangXin head, ref ChInvoice_HangXin bluehead, List detailList, HttpSessionStateBase Session) { //var billno = head.BILLNO; //20230529 增加判断 如果INVOICEMAKETIME到当前时间 小于24小时 则提示错误并返回 // var _head = new NuoNuoQuanDian_RedConfirm(head); _head.extensionNumber = extensionNumber; _head.billId = bluehead.GID.Replace("-", ""); var json = DSWeb.TruckMng.Helper.JsonConvert.Serialize(_head); string method = "nuonuo.OpeMplatform.saveInvoiceRedConfirm"; BasicDataRefDAL.SaveLog("URL=" + URL + ";appKey=" + appKey + ";appSecret=" + appSecret + ";accessToken=" + accessToken + ";taxnum=" + taxnum + ";method=" + method + ";content=" + json, "诺诺全电", "红字确认单", "发送值"); string rtn = NNOpenSDK.sendPostSyncRequest(URL, getSendid(), appKey, appSecret, accessToken, taxnum, method, json); BasicDataRefDAL.SaveLog(rtn, "诺诺全电", "红字确认单", "返回值"); var _return = DSWeb.TruckMng.Helper.JsonConvert.Deserialize<红字申请单发送返回值>(rtn.Trim('"')); var result = new DBResult(); if (_return.code == "E0000") { //红字申请单提交成功 head.GID = bluehead.GID.Replace("-", ""); //读取红字确认单 var _r = ReadRedConfirm(ref head, detailList); if (!_r.Success) { //if (_r.Message != "红字确认单申请中") // bluehead.billUuid = ""; return _r; } else { var hx = 诺诺全电发票.getHelper(head); if (!hx.CanUse) { result.SetErrorInfo(hx.ERRORMSG); return result; } //if (hx.accessToken == "") //{ // BasicDataRefDAL.SaveLog("NNReadInv;税号" + head.TAXCODE + "的 accessToken为空", "", "诺诺平台", "返回认证窗口"); // var _r = TokenPage(); // return _r; //} result = hx.ReadInvoice(head); if (!result.Success) { return result; } //if (result.Message == "需要重新认证") //{ // //HangXinRequestHelper.clear(hx.accessToken); // BasicDataRefDAL.SaveLog("ReadInvoice 反回‘需要重新认证’", "", "诺诺平台", "返回认证窗口"); // var _r = TokenPage(); // return _r; //} var returndate = ChinvoiceDAL.GetData(" BILLNO='" + head.BILLNO + "'"); if (head.PTORRED == "2") { ChinvoiceDAL.SetRed(head); //将原票内的申请和费用恢复原状 var BlueInvoice = ChinvoiceDAL.GetData(" cm.INVOICECODE='" + head.REDCODE + "' and cm.INVOICENO='" + head.REDNUM + "'"); result = ChinvoiceDAL.SetInvoiceFee(BlueInvoice, Session); } //如果状态为开出成功 则执行锁定 if (returndate.EINVOICESTATE == "2" && returndate.BILLSTATUS == 0) { //ChinvoiceDAL.Lock(returndate.BILLNO); //20200106 添加 如sys_param_set.PARAMNAME=INVOICEAUTOLOCK 没有值或值为false 则不执行锁定 var needautolock = MsSysParamSetDAL.GetSysParam("INVOICEAUTOLOCK").isnullortrue(); if (needautolock) { ChinvoiceDAL.Lock(returndate.BILLNO, Convert.ToString(Session["USERID"])); } returndate = ChinvoiceDAL.GetData(" BILLNO='" + head.BILLNO + "'"); } result.Data = returndate; return result; } } //else if (_return.code == "070101" || _return.code == "070301") //{ // //appkey与token问题 需要重新认证 // //HangXinRequestHelper.clear(accessToken); // result.SetErrorInfo("需要重新认证"); // return result; //} //else if (_return.code == "E9106") //{ // //E9106 订单编号不能重复 // //同billno重复发送 执行一次用订单号读取 // return ReadInvoice_BILLNO(head); //} else { //bluehead.billUuid = ""; var reault = new DBResult(false, _return.message, head); return reault; } } /// /// 用户查询全电发票红字确认单 /// /// /// public DBResult ReadRedConfirm(ref ChInvoice_HangXin head, List detailList = null) { //var billno = head.BILLNO; var _head = new NuoNuoQuanDian_ReadRedConfirm(head); var json = DSWeb.TruckMng.Helper.JsonConvert.Serialize(_head); string method = "nuonuo.OpeMplatform.queryInvoiceRedConfirm"; BasicDataRefDAL.SaveLog("URL=" + URL + ";appKey=" + appKey + ";appSecret=" + appSecret + ";accessToken=" + accessToken + ";taxnum=" + taxnum + ";method=" + method + ";content=" + json, "诺诺全电", "读红字单", "发送值"); string rtn = NNOpenSDK.sendPostSyncRequest(URL, getSendid(), appKey, appSecret, accessToken, taxnum, method, json); BasicDataRefDAL.SaveLog(rtn, "诺诺全电", "读红字单", "返回值"); var _return = DSWeb.TruckMng.Helper.JsonConvert.Deserialize<红字申请单读取返回值>(rtn.Trim('"')); var result = new DBResult(); if (_return.code == "E0000") { //根据查询结果读取红票信息 //税务那边实际的红票的订单编号 即为红字申请单orderno,也就是当时传过去的咱们的发票BILLNO if (_return.申请成功()) { //则读取红票 //如不为空 这就是发出红字申请单之后返回结果成功立刻读的 //如为空 就是已产生的红票读取 就不保存了 if (detailList != null) { head.INVOICESERIALNUM = ""; var modb = new ModelObjectDB(); head.BILLNO = _return.result.list[0].orderNo; head.INVOICESERIALNUM = _return.result.list[0].invoiceSerialNum; result = ChinvoiceDAL.SaveInvDetail(head.GID, detailList); if (result.Success == true) { result = modb.Save(head); }; } //result = ReadInvoice(head); //return result; ; } else { /*红字确认单状态(不传则查全部状态): 01 无需确认 02 销方录入待购方确认 03 购方录入待销方确认 04 购销双方 已确认 05 作废(销方录入购方否认) 06 作废(购方录入 销方否认) 07 作废(超 72 小时未确认) 08 作废(发起方 已撤销) 09 作废(确认后撤销) 15 申请中 16 申请失败*/ if (_return.申请中()) { return result.SetErrorInfo("红字确认单申请中"); } else { return result.SetErrorInfo(_return.错误信息()); } } return result.OK(); } else { var reault = new DBResult(false, _return.message, head); return reault; } } private KPResponse dealresponse_KP(string json) { var result = new KPResponse(); result = DSWeb.TruckMng.Helper.JsonConvert.Deserialize(json.Trim('"')); return result; } public DBResult VoidInvoice(ChInvoice_HangXin head) { if (string.IsNullOrEmpty(head.INVOICESERIALNUM)) { var result = new DBResult { Success = false, Message = "还未成功进行平台开票,不需要执行作废", Data = "" }; return result; } //作废接口 string method = "nuonuo.electronInvoice.invoiceCancellation"; // String content = "{" + //"\"invoiceId\":\"19051413444101000011\"," + //"\"invoiceCode\":\"033001800211\"," + //"\"invoiceNo\":\"07912653\"" + //"}"; var json = "{" + "\"invoiceId\":\"" + head.INVOICESERIALNUM + "\"," + "\"invoiceCode\":\"" + head.INVOICECODE + "\"," + "\"invoiceNo\":\"" + head.INVOICENO + "\"" + "}"; BasicDataRefDAL.SaveLog("URL=" + URL + ";appKey=" + appKey + ";appSecret=" + appSecret + ";accessToken=" + accessToken + ";taxnum=" + taxnum + ";method=" + method + ";content=" + json, "诺诺平台", "发票作废", "发送值"); string _r = NNOpenSDK.sendPostSyncRequest(URL, getSendid(), appKey, appSecret, accessToken, taxnum, method, json); BasicDataRefDAL.SaveLog(_r, "诺诺平台", "发票作废", "返回值"); var _return = dealresponse_KP(_r); if (_return.code == "E0000") { //作废请求成功 var invoiceSerialNum = _return.result.invoiceId; head.INVOICESERIALNUM = invoiceSerialNum; head.ISDELETE = "true"; var result = ReadInvoice(head); //var result = new DBResult { Success = true, Message = _return.code + ":" + _return.describe, Data = head }; return result; } else { var msg = _return.code + _return.status + ":" + _return.describe + _return.msg; var result = new DBResult { Success = false, Message = DSWeb.TruckMng.Helper.JsonConvert.Serialize(msg), Data = "" }; return result; } } } public class PortTokenmb { public string message { get; set; } public string status { get; set; } } [JsonObject] public class ISVTokenmb : ModelObjectBillHead { public string access_token { get; set; } public string refresh_token { get; set; } public string expires_in { get; set; } public string userId { get; set; } public string oauthUser { get; set; } public oauthUser _oauthUser { get { oauthUser r_oauthUser = DSWeb.TruckMng.Helper.JsonConvert.Deserialize(oauthUser);//DSWeb.TruckMng.Helper. return r_oauthUser; } } public string error_description { get; set; } public string error { get; set; } /*"{\"access_token\":\"98600c833f178852e2f4d51cexk9yegi\", * \"refresh_token\":\"faf038898dec26eac62640aaed0506cb\", * \"oauthUser\":\" * {\\\"userName\\\":\\\"339901999999142\\\", * \\\"registerType\\\":\\\"1\\\"}\", * \"expires_in\":86356, * \"userId\":\"d1aa0da6bc9840ae83c1120cecd057ac\"}"*/ } [JsonObject] public class Refreshtokenmb { //{"access_token":"1213ef77eab8280536a1a41jehndvz3i","refresh_token":"1a743ee6fd1d965db2b39943e703da4a","expires_in":86400} //{"error_description":"Incorrect refreshToken[8ffb4a7e21691a5d295ee4f64920fc0d]","error":"070314"} public string access_token { get; set; } public string refresh_token { get; set; } public string error_description { get; set; } public string error { get; set; } } [JsonObject] public class oauthUser : ModelObjectBillBody { public string userName { get; set; } public string registerType { get; set; } } [JsonObject] ///解析开票命令返回值;作废命令返回值 public class KPResponse { public string code { get; set; } public string describe { get; set; } public string msg { get; set; } public string status { get; set; } public string message { get { return code + ":" + describe; } } //public KPResult _result { get {return DSWeb.TruckMng.Helper.JsonConvert.Deserialize(result);} } public KPResult result { get; set; } } [JsonObject] ///解析开票命令返回值内的result public class KPResult { public string invoiceSerialNum { get; set; } public string invoiceId { get; set; } } [JsonObject] public class 红字申请单发送返回值 : KPResponse { //{"code":"E0000","describe":"调用成功","result":"IN2023051900001"} public new string result { get; set; } } [JsonObject] ///解析开票命令返回值;作废命令返回值 public class 红字申请单读取返回值 : KPResponse { public new 红字申请单result result { get; set; } public bool 申请成功() { return result.billStatus() == "01"; } public string 错误信息() { return result.errormsg(); } public bool 申请中() { return result.billStatus() == "15"; } } public class 红字申请单result { public int total { get; set; } public new List<红字申请单> list { get; set; } public string billStatus() { if (list == null || list.Count == 0) return null; return list[0].billStatus; } public string errormsg() { if (list == null || list.Count == 0) return ""; return list[0].billMessage; } } public class 红字申请单明细 { /// /// /// public int id { get; set; } /// /// /// public string billId { get; set; } /// /// /// public int detailIndex { get; set; } /// /// /// public int blueDetailIndex { get; set; } /// /// 代理运杂费 /// public string goodsName { get; set; } /// /// /// public string unit { get; set; } /// /// /// public string specType { get; set; } /// /// /// public string taxExcludedPrice { get; set; } /// /// /// public string taxExcludedAmount { get; set; } /// /// /// public string num { get; set; } /// /// /// public string taxRate { get; set; } /// /// /// public string taxAmount { get; set; } /// /// /// public string goodsCode { get; set; } /// /// /// public string favouredPolicyFlag { get; set; } /// /// /// public string favouredPolicyName { get; set; } /// /// /// public string zeroRateFlag { get; set; } /// /// 经纪代理服务 /// public string goodsCodeAbb { get; set; } /// /// /// public string price { get; set; } /// /// /// public int withTaxFlag { get; set; } /// /// /// public string deduction { get; set; } /// /// /// public string taxIncludedAmount { get; set; } } public class 红字申请单 { /// /// /// public int id { get; set; } /// /// /// public string billId { get; set; } /// /// /// public string billNo { get; set; } public string orderNo { get; set; } public string invoiceSerialNum { get; set; } /// /// /// public string billUuid { get; set; } /// /// /// public string billStatus { get; set; } /// /// /// public string requestStatus { get; set; } /// /// /// public string billMessage { get; set; } /// /// /// public int openStatus { get; set; } /// /// /// public int applySource { get; set; } /// /// /// public DateTime billTime { get; set; } /// /// /// public string taxExcludedAmount { get; set; } /// /// /// public string taxAmount { get; set; } /// /// /// public string taxIncludeAmount { get; set; } /// /// /// public string blueElecInvoiceNumber { get; set; } /// /// /// public string blueInvoiceNumber { get; set; } /// /// /// public string blueInvoiceCode { get; set; } /// /// /// public DateTime blueInvoiceTime { get; set; } /// /// /// public string blueInvoiceLine { get; set; } /// /// /// public string sellerTaxNo { get; set; } /// /// 青岛锦海运通供应链有限公司 /// public string sellerName { get; set; } /// /// /// public string buyerTaxNo { get; set; } /// /// 青岛东宝电子有限公司 /// public string buyerName { get; set; } /// /// /// public DateTime createTime { get; set; } /// /// /// public DateTime updateTime { get; set; } /// /// /// public string sellerAccount { get; set; } /// /// 中国建设银行平度支行 37101997906051000773 /// public string buyerAccount { get; set; } /// /// /// public string vatUsage { get; set; } /// /// /// public string saleTaxUsage { get; set; } /// /// /// public string accountStatus { get; set; } /// /// /// public string departmentId { get; set; } /// /// /// public string clerkId { get; set; } /// /// /// public string extensionNumber { get; set; } /// /// /// public string taxNum { get; set; } /// /// /// public string account { get; set; } /// /// /// public string redReason { get; set; } /// /// /// public string confirmAgreement { get; set; } /// /// /// public string confirmReason { get; set; } /// /// /// public string confirmTime { get; set; } /// /// /// public int requestSrc { get; set; } /// /// /// public List<红字申请单明细> detail { get; set; } } [JsonObject] ///解析读取发票信息命令返回值 public class ReadResponse { public string code { get; set; } public string describe { get; set; } public bool success { get { if (code == "E0000") return true; else return false; } } /* public List _result { get { List _list = new List(); foreach (var str in result) { var info = DSWeb.TruckMng.Helper.JsonConvert.Deserialize(str); _list.Add(info); } return _list; } //set { _result = value } } public List result { get; set; } */ public List result { get; set; } } [JsonObject] ///解析读取发票信息命令返回值 public class ReadResponse_诺诺全电发票 : ReadResponse { public List result { get; set; } } [JsonObject] ///用于解析“读取发票信息”方法返回的发票信息 public class ReadResult { #region private Fields private string _orderNo = string.Empty; private string _invoiceSerialNum = string.Empty; private string _status = string.Empty; private string _statusMsg = string.Empty; private string _resultMsg = string.Empty; private string _invoiceFileUrl = string.Empty; private string _invoiceImageUrl = string.Empty; private string _invoiceDate = string.Empty; private string _invoiceCode = string.Empty; private string _invoiceNum = string.Empty; private string _taxExcludedAmount = string.Empty; private string _taxIncludedAmount = string.Empty; private string _buyerName = string.Empty; private string _buyerTaxNum = string.Empty; private string _invoiceLine = string.Empty; #endregion #region public Fields public string orderNo { get { return _orderNo; } set { _orderNo = value; } } public string invoiceSerialNum { get { return _invoiceSerialNum; } set { _invoiceSerialNum = value; } } public string status { get { return _status; } set { _status = value; } } public string statusMsg { get { return _statusMsg; } set { _statusMsg = value; } } public string resultMsg { get { return _resultMsg; } set { _resultMsg = value; } } public string invoiceFileUrl { get { return _invoiceFileUrl; } set { _invoiceFileUrl = value; } } public string invoiceImageUrl { get { return _invoiceImageUrl; } set { _invoiceImageUrl = value; } } public string invoiceDate { get { return _invoiceDate; } set { _invoiceDate = value; } } public string invoiceCode { get { return _invoiceCode; } set { _invoiceCode = value; } } public string invoiceNum { get { return _invoiceNum; } set { _invoiceNum = value; } } public string taxExcludedAmount { get { return _taxExcludedAmount; } set { _taxExcludedAmount = value; } } public string taxIncludedAmount { get { return _taxIncludedAmount; } set { _taxIncludedAmount = value; } } public string buyerName { get { return _buyerName; } set { _buyerName = value; } } public string buyerTaxNum { get { return _buyerTaxNum; } set { _buyerTaxNum = value; } } public string invoiceLine { get { return _invoiceLine; } set { _invoiceLine = value; } } public string remark { get; set; } #endregion } ///用于解析“读取发票信息”方法返回的发票信息 public class ReadResult_诺诺全电发票 { /// /// 流水号 /// public string serialNo { get; set; } /// /// 业务编号 /// public string orderNo { get; set; } /// /// /// public string status { get; set; } /// /// 开票完成(最终状态) /// public string statusMsg { get; set; } /// /// /// public string failCause { get; set; } /// /// /// public string pdfUrl { get; set; } /// /// /// public string pictureUrl { get; set; } /// /// /// public long invoiceTime { get; set; } /// /// /// public string invoiceCode { get; set; } /// /// /// public string invoiceNo { get; set; } /// /// /// public string exTaxAmount { get; set; } /// /// /// public string taxAmount { get; set; } /// /// 个人2 /// public string payerName { get; set; } /// /// /// public string payerTaxNo { get; set; } /// /// 电子增值税普通发票 /// public string invoiceKind { get; set; } /// /// /// public string checkCode { get; set; } /// /// 含底图纸票pdf地址 /// public string paperPdfUrl { get; set; } public string ofdUrl { get; set; } /// /// /// public List invoiceItems { get; set; } } public class InvoiceItems { /// /// 门票 /// public string itemName { get; set; } /// /// 张 /// public string itemUnit { get; set; } /// /// /// public string itemPrice { get; set; } /// /// /// public string itemTaxRate { get; set; } /// /// /// public string itemNum { get; set; } /// /// /// public string itemAmount { get; set; } /// /// /// public string itemTaxAmount { get; set; } /// /// /// public string itemSpec { get; set; } /// /// /// public string itemCode { get; set; } /// /// /// public string isIncludeTax { get; set; } /// /// /// public string invoiceLineProperty { get; set; } /// /// /// public string zeroRateFlag { get; set; } /// /// /// public string favouredPolicyName { get; set; } /// /// /// public string favouredPolicyFlag { get; set; } } [JsonObject] public class P2INVOICE : BaseTableMC { } //public class InvoiceFeeSum //{ // public string CURR { get; set; } // public decimal amount { get; set; } // public decimal exchange { get; set; } //} }