fapiao_controller.go 60KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554
  1. package controllers
  2. import (
  3. "XT_New/common"
  4. "XT_New/enums"
  5. "XT_New/invoice"
  6. "XT_New/models"
  7. "XT_New/service"
  8. "XT_New/service/fapiao"
  9. "XT_New/utils"
  10. "encoding/base64"
  11. "encoding/json"
  12. "fmt"
  13. "github.com/astaxie/beego"
  14. "github.com/shopspring/decimal"
  15. "math"
  16. "strconv"
  17. "strings"
  18. "time"
  19. )
  20. func FaPiaoApiRegistRouters() {
  21. beego.Router("/api/fapiao/sms/code", &FapiaoApiController{}, "get:GetLoginUploadCode")
  22. beego.Router("/api/fapiao/sms/login", &FapiaoApiController{}, "get:Login")
  23. beego.Router("/api/fapiao/qrcode/get", &FapiaoApiController{}, "get:GetQRCode")
  24. beego.Router("/api/fapiao/blue", &FapiaoApiController{}, "get:OpenFaPiao")
  25. beego.Router("/api/fapiao/red", &FapiaoApiController{}, "get:RedFaPiao")
  26. beego.Router("/api/order/fapiao/list", &FapiaoApiController{}, "get:GetOrderFaPiaoList")
  27. beego.Router("/api/gdfapiao/list", &FapiaoApiController{}, "get:GetFaPiaoList")
  28. beego.Router("/api/newfapiao/get", &HisApiController{}, "get:GetNewFaPiaoData")
  29. beego.Router("/api/fapiao/query", &FapiaoApiController{}, "get:QueryFaPiao")
  30. }
  31. func (c *FapiaoApiController) QueryFaPiao() {
  32. id, _ := c.GetInt64("id")
  33. oc, _ := service.GetFaPiaoOrderById(id)
  34. sdk := invoice.NewSdk(common.HMAC_SHA256, "5058f0defd12047ee2a7", "51440fbee667ea9a53aaa68c9c408367", "", "")
  35. var config models.FapiaoConfig
  36. config, _ = service.FindFaPiaoConfigInfo(c.GetAdminUserInfo().CurrentOrgId)
  37. routerAddress2, postData2 := fapiao.Query(config.SellerTaxpayerNum, oc.OrderSn, "")
  38. r3, err2 := sdk.HttpPost("https://apigw.goldentec.com", routerAddress2, postData2)
  39. fmt.Println("query")
  40. fmt.Println(string(r3))
  41. if err2 != nil {
  42. fmt.Println(err2)
  43. } else {
  44. var resp3 QueryFaPiaoResultResponse
  45. // 将 byte 切片转换为结构体
  46. json.Unmarshal(r3, &resp3)
  47. if resp3.Code == 0 {
  48. //var fapiaos models.HisFaPiaoOrder
  49. if resp3.Data.Status == 3 {
  50. // 异步调用查询操作,不阻塞主程序
  51. oc.TicketSn = resp3.Data.TicketSn
  52. oc.TicketDate = resp3.Data.TicketDate
  53. oc.AmountWithTax = resp3.Data.AmountWithoutTax
  54. oc.AmountWithoutTax = resp3.Data.AmountWithoutTax
  55. oc.FapiaoStatus = "2"
  56. oc.TaxAmount = resp3.Data.TaxAmount
  57. oc.IsRedWashed = strconv.FormatInt(int64(resp3.Data.IsRedWashed), 10)
  58. oc.PdfUrl = resp3.Data.PdfUrl
  59. oc.OfdUrl = resp3.Data.OfdUrl
  60. oc.XmlUrl = resp3.Data.XmlUrl
  61. service.SaveFaPiaoOrder(&oc)
  62. ids := strings.Split(oc.OrderIds, ",")
  63. service.UpdateFaPiaoNumber(resp3.Data.TicketSn, ids) //同步发票号码
  64. c.ServeSuccessJSON(map[string]interface{}{
  65. "msg": "同步成功",
  66. })
  67. } else if resp3.Data.Status == 1 {
  68. oc.FapiaoStatus = "1"
  69. oc.TicketSn = ""
  70. service.SaveFaPiaoOrder(&oc)
  71. c.ServeSuccessJSON(map[string]interface{}{
  72. "msg": "同步成功",
  73. })
  74. //service.UpdateFaPiaoNumber(resp3.Data.TicketSn, ids) //同步发票号码
  75. } else if resp3.Data.Status == 2 {
  76. oc.FapiaoStatus = "3"
  77. oc.TicketSn = ""
  78. oc.Message = resp3.Data.Message
  79. service.SaveFaPiaoOrder(&oc)
  80. c.ServeSuccessJSON(map[string]interface{}{
  81. "msg": "同步成功",
  82. })
  83. } else {
  84. oc.FapiaoStatus = "3"
  85. oc.TicketSn = ""
  86. oc.Message = resp3.Data.Message
  87. service.SaveFaPiaoOrder(&oc)
  88. c.ServeSuccessJSON(map[string]interface{}{
  89. "msg": "同步成功",
  90. })
  91. }
  92. } else {
  93. //错误处理
  94. c.ServeSuccessJSON(map[string]interface{}{
  95. "msg": "查询失败" + resp3.Message,
  96. })
  97. }
  98. //if resp3.Code == 0 {
  99. // // 异步调用查询操作,不阻塞主程序
  100. // oc.TicketSn = resp3.Data.TicketSn
  101. // oc.TicketDate = resp3.Data.TicketDate
  102. // oc.AmountWithTax = resp3.Data.AmountWithoutTax
  103. // oc.AmountWithoutTax = resp3.Data.AmountWithoutTax
  104. // oc.FapiaoStatus = "2"
  105. // oc.TaxAmount = resp3.Data.TaxAmount
  106. // oc.IsRedWashed = strconv.FormatInt(int64(resp3.Data.IsRedWashed), 10)
  107. // oc.PdfUrl = resp3.Data.PdfUrl
  108. // oc.OfdUrl = resp3.Data.OfdUrl
  109. // oc.XmlUrl = resp3.Data.XmlUrl
  110. // service.SaveFaPiaoOrder(oc)
  111. // ids := strings.Split(oc.OrderIds, ",")
  112. // service.UpdateFaPiaoNumber(resp3.Data.TicketSn, ids) //同步发票号码
  113. // c.ServeSuccessJSON(map[string]interface{}{
  114. // "msg": "查询成功",
  115. // })
  116. //} else {
  117. // //错误处理
  118. // c.ServeSuccessJSON(map[string]interface{}{
  119. // "msg": "查询失败" + resp3.Message,
  120. // })
  121. //}
  122. }
  123. }
  124. func (c *HisApiController) GetNewFaPiaoData() {
  125. order_ids := c.GetString("order_ids")
  126. patient_id, _ := c.GetInt64("patient_id")
  127. //number := c.GetString("number")
  128. adminUser := c.GetAdminUserInfo()
  129. orders := strings.Split(order_ids, ",")
  130. patient, _ := service.GetFaPiaoPatientByID(adminUser.CurrentOrgId, patient_id)
  131. var bedCostTotal float64 = 0 //床位总费
  132. var bedCostSelfTotal float64 = 0 //床位自费
  133. var bedCostPartSelfTotal float64 = 0 //床位部分项目自费
  134. var operationCostTotal float64 = 0 //手术费
  135. var operationCostSelfTotal float64 = 0 //手术费
  136. var operationCostPartSelfTotal float64 = 0 //手术费
  137. var otherCostTotal float64 = 0 //其他费用
  138. var otherCostSelfTotal float64 = 0 //其他费用
  139. var otherCostPartSelfTotal float64 = 0 //其他费用
  140. var materialCostTotal float64 = 0 //材料费
  141. var materialCostSelfTotal float64 = 0 //材料费
  142. var materialCostPartSelfTotal float64 = 0 //材料费
  143. var westernMedicineCostTotal float64 = 0 //西药费
  144. var westernMedicineCostSelfTotal float64 = 0 //西药费
  145. var westernMedicineCostPartSelfTotal float64 = 0 //西药费
  146. var chineseTraditionalMedicineCostTotal float64 = 0 //中成药
  147. var chineseTraditionalMedicineCostSelfTotal float64 = 0 //中成药
  148. var chineseTraditionalMedicineCostPartSelfTotal float64 = 0 //中成药
  149. var checkCostTotal float64 = 0 //检查费
  150. var checkCostSelfTotal float64 = 0 //检查费
  151. var checkCostPartSelfTotal float64 = 0 //检查费
  152. var hiliCostTotal float64 = 0 //护理费
  153. var hiliCostSelfTotal float64 = 0 //护理费
  154. //var hiliCostPartSelfTotal float64 = 0 //护理费
  155. var laboratoryCostTotal float64 = 0 //化验费
  156. var laboratoryCostSelfTotal float64 = 0 //化验费
  157. var laboratoryCostPartSelfTotal float64 = 0 //化验费
  158. var treatCostTotal float64 = 0 //治疗费用
  159. var treatCostSelfTotal float64 = 0 //治疗费用
  160. var treatCostPartSelfTotal float64 = 0 //治疗费用
  161. var zhenChaCostTotal float64 = 0 //其他费用
  162. var zhenChaCostSelfTotal float64 = 0 //其他费用
  163. var zhenChaCostPartSelfTotal float64 = 0 //其他费用
  164. decimal.DivisionPrecision = 2
  165. var MedfeeSumamt float64 = 0 //治疗费用
  166. var PsnCashPay float64 = 0 //治疗费用
  167. var FundPaySumamt float64 = 0 //治疗费用
  168. var HifpPay float64 = 0 //治疗费用
  169. var MafPay float64 = 0 //治疗费用
  170. var AcctPay float64 = 0 //治疗费用
  171. for _, item := range orders {
  172. var new_order models.HisOrder
  173. order_id_num, _ := strconv.ParseInt(item, 10, 64)
  174. new_order, _ = service.GetHisOrderByID(order_id_num)
  175. MedfeeSumamt, _ = decimal.NewFromFloat(MedfeeSumamt).Add(decimal.NewFromFloat(new_order.MedfeeSumamt)).Float64()
  176. PsnCashPay, _ = decimal.NewFromFloat(PsnCashPay).Add(decimal.NewFromFloat(new_order.PsnCashPay)).Float64()
  177. FundPaySumamt, _ = decimal.NewFromFloat(FundPaySumamt).Add(decimal.NewFromFloat(new_order.FundPaySumamt)).Float64()
  178. HifpPay, _ = decimal.NewFromFloat(HifpPay).Add(decimal.NewFromFloat(new_order.HifpPay)).Float64()
  179. MafPay, _ = decimal.NewFromFloat(MafPay).Add(decimal.NewFromFloat(new_order.MafPay)).Float64()
  180. AcctPay, _ = decimal.NewFromFloat(AcctPay).Add(decimal.NewFromFloat(new_order.AcctPay)).Float64()
  181. orderInfos_two, _ := service.GetHisOrderDetailByNumber(new_order.Number, adminUser.CurrentOrgId)
  182. var balanceAccountsType int64
  183. if new_order.IsMedicineInsurance == 1 {
  184. balanceAccountsType = 1
  185. } else {
  186. balanceAccountsType = 2
  187. }
  188. if balanceAccountsType == 2 {
  189. //orderInfos_two, _ := service.GetHisOrderDetailByNumber(order.Number, adminUser.CurrentOrgId)
  190. for _, item := range orderInfos_two {
  191. item.FulamtOwnpayAmt = item.DetItemFeeSumamt
  192. if item.HisDoctorAdviceInfo.ID > 0 && item.HisPrescriptionProject.ID == 0 { //药品
  193. item.MedChrgitmType = "09"
  194. }
  195. if item.HisPrescriptionProject.ID > 0 && item.HisDoctorAdviceInfo.ID == 0 {
  196. if item.HisPrescriptionProject.Type == 2 {
  197. switch item.HisPrescriptionProject.VMHisProject.CostClassify {
  198. case 1:
  199. item.MedChrgitmType = "14"
  200. break
  201. case 2:
  202. item.MedChrgitmType = "05"
  203. break
  204. case 3:
  205. item.MedChrgitmType = "04"
  206. break
  207. case 4:
  208. item.MedChrgitmType = "03"
  209. break
  210. case 5:
  211. item.MedChrgitmType = "08"
  212. break
  213. case 6:
  214. item.MedChrgitmType = "14"
  215. break
  216. case 7:
  217. item.MedChrgitmType = "14"
  218. break
  219. case 8:
  220. item.MedChrgitmType = "03"
  221. break
  222. case 9:
  223. item.MedChrgitmType = "14"
  224. break
  225. }
  226. } else if item.HisPrescriptionProject.Type == 3 {
  227. item.MedChrgitmType = "08"
  228. }
  229. }
  230. }
  231. for _, item := range orderInfos_two {
  232. fmt.Println(item.MedChrgitmType)
  233. if item.MedChrgitmType == "07" { //床位费
  234. hiliCostTotal, _ = decimal.NewFromFloat(hiliCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  235. hiliCostSelfTotal = hiliCostTotal
  236. }
  237. if item.MedChrgitmType == "01" { //床位费
  238. bedCostTotal, _ = decimal.NewFromFloat(bedCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  239. bedCostSelfTotal = bedCostTotal
  240. }
  241. if item.MedChrgitmType == "02" { //诊察费
  242. zhenChaCostTotal, _ = decimal.NewFromFloat(zhenChaCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  243. zhenChaCostSelfTotal = zhenChaCostTotal
  244. }
  245. if item.MedChrgitmType == "03" { //检查费
  246. checkCostTotal, _ = decimal.NewFromFloat(checkCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  247. checkCostSelfTotal = checkCostTotal
  248. }
  249. if item.MedChrgitmType == "04" { //化验费
  250. laboratoryCostTotal, _ = decimal.NewFromFloat(laboratoryCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  251. laboratoryCostSelfTotal = laboratoryCostTotal
  252. }
  253. if item.MedChrgitmType == "05" || item.MedChrgitmType == "14" || item.MedChrgitmType == "201" { //治疗费
  254. treatCostTotal, _ = decimal.NewFromFloat(treatCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  255. treatCostSelfTotal = treatCostTotal
  256. }
  257. if item.MedChrgitmType == "06" { //手术费
  258. treatCostTotal, _ = decimal.NewFromFloat(operationCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  259. operationCostSelfTotal = treatCostTotal
  260. }
  261. if item.MedChrgitmType == "08" || item.MedChrgitmType == "1402" || item.MedChrgitmType == "1403" || item.MedChrgitmType == "1401" || item.MedChrgitmType == "301" { //材料费
  262. materialCostTotal, _ = decimal.NewFromFloat(materialCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  263. materialCostSelfTotal = materialCostTotal
  264. }
  265. if item.MedChrgitmType == "09" || item.MedChrgitmType == "101" { //西药费
  266. westernMedicineCostTotal, _ = decimal.NewFromFloat(westernMedicineCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  267. westernMedicineCostSelfTotal = westernMedicineCostTotal
  268. }
  269. if item.MedChrgitmType == "11" { //中成费
  270. chineseTraditionalMedicineCostTotal, _ = decimal.NewFromFloat(chineseTraditionalMedicineCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  271. chineseTraditionalMedicineCostSelfTotal = chineseTraditionalMedicineCostTotal
  272. }
  273. if c.GetAdminUserInfo().CurrentOrgId == 10106 {
  274. if item.MedChrgitmType == "0" || item.MedChrgitmType == "12" || item.MedChrgitmType == "02" { //其他费
  275. otherCostTotal, _ = decimal.NewFromFloat(otherCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  276. otherCostSelfTotal = otherCostTotal
  277. }
  278. } else {
  279. if item.MedChrgitmType == "0" || item.MedChrgitmType == "12" { //其他费
  280. otherCostTotal, _ = decimal.NewFromFloat(otherCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  281. otherCostSelfTotal = otherCostTotal
  282. }
  283. }
  284. }
  285. } else {
  286. for _, item := range orderInfos_two {
  287. if item.MedChrgitmType == "01" { //床位费
  288. bedCostTotal, _ = decimal.NewFromFloat(bedCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  289. //bedCostSelfTotal, _ = decimal.NewFromFloat(bedCostSelfTotal).Add(decimal.NewFromFloat(item.OverlmtAmt)).Float64()
  290. //bedCostPartSelfTotal, _ = decimal.NewFromFloat(bedCostPartSelfTotal).Add(decimal.NewFromFloat(item.PreselfpayAmt)).Float64()
  291. }
  292. if item.MedChrgitmType == "02" { //诊察费
  293. zhenChaCostTotal, _ = decimal.NewFromFloat(zhenChaCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  294. //zhenChaCostSelfTotal, _ = decimal.NewFromFloat(zhenChaCostSelfTotal).Add(decimal.NewFromFloat(item.OverlmtAmt)).Float64()
  295. //zhenChaCostPartSelfTotal, _ = decimal.NewFromFloat(zhenChaCostPartSelfTotal).Add(decimal.NewFromFloat(item.PreselfpayAmt)).Float64()
  296. }
  297. if c.GetAdminUserInfo().CurrentOrgId == 10188 || c.GetAdminUserInfo().CurrentOrgId == 10217 {
  298. if item.MedChrgitmType == "03" { //检查费
  299. laboratoryCostTotal, _ = decimal.NewFromFloat(laboratoryCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  300. //laboratoryCostSelfTotal, _ = decimal.NewFromFloat(laboratoryCostSelfTotal).Add(decimal.NewFromFloat(item.OverlmtAmt)).Float64()
  301. //laboratoryCostPartSelfTotal, _ = decimal.NewFromFloat(laboratoryCostPartSelfTotal).Add(decimal.NewFromFloat(item.PreselfpayAmt)).Float64()
  302. }
  303. } else {
  304. if item.MedChrgitmType == "03" { //检查费
  305. checkCostTotal, _ = decimal.NewFromFloat(checkCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  306. //checkCostSelfTotal, _ = decimal.NewFromFloat(checkCostSelfTotal).Add(decimal.NewFromFloat(item.OverlmtAmt)).Float64()
  307. //checkCostPartSelfTotal, _ = decimal.NewFromFloat(checkCostPartSelfTotal).Add(decimal.NewFromFloat(item.PreselfpayAmt)).Float64()
  308. }
  309. }
  310. if item.MedChrgitmType == "04" { //化验费
  311. laboratoryCostTotal, _ = decimal.NewFromFloat(laboratoryCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  312. //laboratoryCostSelfTotal, _ = decimal.NewFromFloat(laboratoryCostSelfTotal).Add(decimal.NewFromFloat(item.OverlmtAmt)).Float64()
  313. //laboratoryCostPartSelfTotal, _ = decimal.NewFromFloat(laboratoryCostPartSelfTotal).Add(decimal.NewFromFloat(item.PreselfpayAmt)).Float64()
  314. }
  315. if item.MedChrgitmType == "05" || item.MedChrgitmType == "14" || item.MedChrgitmType == "201" { //治疗费
  316. treatCostTotal, _ = decimal.NewFromFloat(treatCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  317. //treatCostSelfTotal, _ = decimal.NewFromFloat(treatCostSelfTotal).Add(decimal.NewFromFloat(item.OverlmtAmt)).Float64()
  318. //treatCostPartSelfTotal, _ = decimal.NewFromFloat(treatCostPartSelfTotal).Add(decimal.NewFromFloat(item.PreselfpayAmt)).Float64()
  319. }
  320. if item.MedChrgitmType == "06" { //手术费
  321. operationCostTotal, _ = decimal.NewFromFloat(operationCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  322. //operationCostSelfTotal, _ = decimal.NewFromFloat(operationCostSelfTotal).Add(decimal.NewFromFloat(item.OverlmtAmt)).Float64()
  323. //operationCostPartSelfTotal, _ = decimal.NewFromFloat(operationCostPartSelfTotal).Add(decimal.NewFromFloat(item.PreselfpayAmt)).Float64()
  324. }
  325. if item.MedChrgitmType == "08" || item.MedChrgitmType == "1401" || item.MedChrgitmType == "1402" || item.MedChrgitmType == "1403" || item.MedChrgitmType == "301" { //材料费
  326. materialCostTotal, _ = decimal.NewFromFloat(materialCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  327. //materialCostSelfTotal, _ = decimal.NewFromFloat(materialCostSelfTotal).Add(decimal.NewFromFloat(item.OverlmtAmt)).Float64()
  328. //materialCostPartSelfTotal, _ = decimal.NewFromFloat(materialCostPartSelfTotal).Add(decimal.NewFromFloat(item.PreselfpayAmt)).Float64()
  329. }
  330. if item.MedChrgitmType == "07" { //护理
  331. hiliCostTotal, _ = decimal.NewFromFloat(hiliCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  332. //hiliCostSelfTotal, _ = decimal.NewFromFloat(hiliCostSelfTotal).Add(decimal.NewFromFloat(item.OverlmtAmt)).Float64()
  333. //hiliCostPartSelfTotal, _ = decimal.NewFromFloat(hiliCostPartSelfTotal).Add(decimal.NewFromFloat(item.PreselfpayAmt)).Float64()
  334. }
  335. if item.MedChrgitmType == "09" || item.MedChrgitmType == "101" { //西药费
  336. westernMedicineCostTotal, _ = decimal.NewFromFloat(westernMedicineCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  337. //westernMedicineCostSelfTotal, _ = decimal.NewFromFloat(westernMedicineCostSelfTotal).Add(decimal.NewFromFloat(item.OverlmtAmt)).Float64()
  338. //westernMedicineCostPartSelfTotal, _ = decimal.NewFromFloat(westernMedicineCostPartSelfTotal).Add(decimal.NewFromFloat(item.PreselfpayAmt)).Float64()
  339. }
  340. if item.MedChrgitmType == "11" { //中成费
  341. chineseTraditionalMedicineCostTotal, _ = decimal.NewFromFloat(chineseTraditionalMedicineCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  342. //chineseTraditionalMedicineCostSelfTotal, _ = decimal.NewFromFloat(chineseTraditionalMedicineCostSelfTotal).Add(decimal.NewFromFloat(item.OverlmtAmt)).Float64()
  343. //chineseTraditionalMedicineCostPartSelfTotal, _ = decimal.NewFromFloat(chineseTraditionalMedicineCostPartSelfTotal).Add(decimal.NewFromFloat(item.PreselfpayAmt)).Float64()
  344. }
  345. if item.MedChrgitmType == "0" || item.MedChrgitmType == "12" { //其他费
  346. otherCostTotal, _ = decimal.NewFromFloat(otherCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  347. //otherCostSelfTotal, _ = decimal.NewFromFloat(otherCostSelfTotal).Add(decimal.NewFromFloat(item.OverlmtAmt)).Float64()
  348. //otherCostPartSelfTotal, _ = decimal.NewFromFloat(otherCostPartSelfTotal).Add(decimal.NewFromFloat(item.PreselfpayAmt)).Float64()
  349. }
  350. }
  351. }
  352. }
  353. //pre_pay_money := service.GetUserMoney(order.PatientId, adminUser.CurrentOrgId)
  354. //sum, _ := service.RefundListSum(adminUser.CurrentOrgId, startRecordDateTime, endRecordDateTime, order.PatientId)
  355. admin, _ := service.GetAdminUserInfoByID(adminUser.CurrentOrgId, adminUser.AdminUser.Id)
  356. charge_admin, _ := service.GetAdminUserInfoByID(adminUser.CurrentOrgId, adminUser.AdminUser.Id)
  357. c.ServeSuccessJSON(map[string]interface{}{
  358. "charge_admin_name": charge_admin.UserName,
  359. "patient": patient,
  360. "admin_user_name": admin.UserName, //收款员
  361. "bedCostTotal": bedCostTotal,
  362. "bedCostSelfTotal": bedCostSelfTotal,
  363. "bedCostPartSelfTotal": bedCostPartSelfTotal,
  364. "operationCostTotal": operationCostTotal,
  365. "operationCostSelfTotal": operationCostSelfTotal,
  366. "operationCostPartSelfTotal": operationCostPartSelfTotal,
  367. "otherCostTotal": otherCostTotal,
  368. "otherCostSelfTotal": otherCostSelfTotal,
  369. "otherCostPartSelfTotal": otherCostPartSelfTotal,
  370. "materialCostTotal": materialCostTotal,
  371. "materialCostSelfTotal": materialCostSelfTotal,
  372. "materialCostPartSelfTotal": materialCostPartSelfTotal,
  373. "westernMedicineCostTotal": westernMedicineCostTotal,
  374. "westernMedicineCostSelfTotal": westernMedicineCostSelfTotal,
  375. "westernMedicineCostPartSelfTotal": westernMedicineCostPartSelfTotal,
  376. "chineseTraditionalMedicineCostTotal": chineseTraditionalMedicineCostTotal,
  377. "chineseTraditionalMedicineCostSelfTotal": chineseTraditionalMedicineCostSelfTotal,
  378. "chineseTraditionalMedicineCostPartSelfTotal": chineseTraditionalMedicineCostPartSelfTotal,
  379. "checkCostTotal": checkCostTotal,
  380. "checkCostSelfTotal": checkCostSelfTotal,
  381. "checkCostPartSelfTotal": checkCostPartSelfTotal,
  382. "laboratoryCostTotal": laboratoryCostTotal,
  383. "laboratoryCostSelfTotal": laboratoryCostSelfTotal,
  384. "laboratoryCostPartSelfTotal": laboratoryCostPartSelfTotal,
  385. "treatCostTotal": treatCostTotal,
  386. "treatCostSelfTotal": treatCostSelfTotal,
  387. "treatCostPartSelfTotal": treatCostPartSelfTotal,
  388. "zhenChaCostTotal": zhenChaCostTotal,
  389. "zhenChaCostSelfTotal": zhenChaCostSelfTotal,
  390. "zhenChaCostPartSelfTotal": zhenChaCostPartSelfTotal,
  391. "hiliCostTotal": hiliCostTotal,
  392. "hiliCostSelfTotal": hiliCostSelfTotal,
  393. "medfee_sumamt": MedfeeSumamt,
  394. "fund_pay_sumamt": FundPaySumamt,
  395. "hifp_pay": HifpPay,
  396. "maf_pay": MafPay,
  397. "acct_pay": AcctPay,
  398. "psn_cash_pay": PsnCashPay,
  399. })
  400. }
  401. type FapiaoApiController struct {
  402. BaseAuthAPIController
  403. }
  404. type InvoiceRequest struct {
  405. SellerName string `json:"seller_name,omitempty"` // 销方名称
  406. SellerTaxpayerNum string `json:"seller_taxpayer_num"` // 销方纳税人识别号
  407. SellerAddress string `json:"seller_address"` // 销方地址
  408. SellerTel string `json:"seller_tel"` // 销方电话
  409. SellerBankName string `json:"seller_bank_name"` // 销方银行名称
  410. SellerBankAccount string `json:"seller_bank_account"` // 销方银行账号
  411. TitleType int `json:"title_type"` // 抬头类型:1 个人、政府事业单位;2 企业
  412. BuyerTitle string `json:"buyer_title"` // 购方名称
  413. BuyerTaxpayerNum string `json:"buyer_taxpayer_num,omitempty"` // 购方纳税人识别号
  414. BuyerAddress string `json:"buyer_address,omitempty"` // 购方地址
  415. BuyerPhone string `json:"buyer_phone,omitempty"` // 购方电话
  416. BuyerBankName string `json:"buyer_bank_name,omitempty"` // 购方银行名称
  417. BuyerBankAccount string `json:"buyer_bank_account,omitempty"` // 购方银行账号
  418. BuyerEmail string `json:"buyer_email,omitempty"` // 收票人邮箱
  419. OrderID string `json:"order_id"` // 商户订单号
  420. InvoiceTypeCode string `json:"invoice_type_code,omitempty"` // 开具发票类型
  421. CallbackURL string `json:"callback_url"` // 发票结果回传地址
  422. Drawer string `json:"drawer"` // 开票人姓名
  423. Payee string `json:"payee,omitempty"` // 收款人姓名
  424. Checker string `json:"checker,omitempty"` // 复核人姓名
  425. TerminalCode string `json:"terminal_code"` // 税盘号
  426. UserOpenID string `json:"user_openid,omitempty"` // 商家用户标识
  427. SpecialInvoiceKind string `json:"special_invoice_kind,omitempty"` // 特殊票种标识
  428. AmountHasTax string `json:"amount_has_tax"` // 含税总金额
  429. TaxAmount string `json:"tax_amount"` // 总税额
  430. AmountWithoutTax string `json:"amount_without_tax"` // 不含税总金额
  431. Remark string `json:"remark,omitempty"` // 备注
  432. StoreNo string `json:"store_no,omitempty"` // 门店编码
  433. Template int `json:"template"` // 发票模板
  434. Info *InvoiceInfo `json:"info,omitempty"` // 冠名票信息
  435. TaxpayerConfirmation *TaxpayerConfirmation `json:"taxpayer_confirmation,omitempty"` // 纳税人确认信息
  436. SpecificBusinessCode string `json:"specific_business_code,omitempty"` // 特定业务代码
  437. RealStaticRent *RealStaticRent `json:"real_static_rent,omitempty"` // 不动产经营租赁信息
  438. PassengerTransport []PassengerTransport `json:"passenger_transport,omitempty"` // 旅客运输信息
  439. GoodsTransport []GoodsTransport `json:"goods_transport,omitempty"` // 货物运输信息
  440. AgriculturalPurchase *AgriculturalPurchase `json:"agricultural_purchase,omitempty"` // 农产品收购信息
  441. BuildingService *BuildingService `json:"building_service,omitempty"` // 建筑服务信息
  442. RealStaticSales *RealStaticSales `json:"real_static_sales,omitempty"` // 不动产销售信息
  443. TractorCombine *TractorCombine `json:"tractor_combine,omitempty"` // 拖拉机与联合收割机信息
  444. UsedCarService *UsedCarService `json:"used_car_service,omitempty"` // 二手车服务信息
  445. UsedCarSell *UsedCarSell `json:"used_car_sell,omitempty"` // 二手车销售信息
  446. DeductionService *DeductionService `json:"deduction_service,omitempty"` // 差额征税信息
  447. Items []Item `json:"items"` // 项目商品明细
  448. }
  449. type InvoiceInfo struct {
  450. UseDate string `json:"use_date,omitempty"` // 入园日期
  451. TicketName string `json:"ticket_name,omitempty"` // 票据名称
  452. }
  453. type TaxpayerConfirmation struct {
  454. RenewableResourceRecyclingFlag int `json:"renewable_resource_recycling_flag,omitempty"` // 再生资源回收单位标志
  455. DrawWithNoBuyerFlag int `json:"draw_with_nobuyer_flag,omitempty"` // 无购买方纳税人信息继续开票标志
  456. NotDutyFreeReason string `json:"not_dutyfree_reason,omitempty"` // 放弃享受减按1%征收率原因
  457. }
  458. type RealStaticRent struct {
  459. PropertyNum string `json:"property_num"` // 房屋产权证号
  460. Location string `json:"location"` // 不动产地址 (省市区县)
  461. AddressDetail string `json:"address_detail"` // 不动产详细地址
  462. RentBeginDate string `json:"rent_begin_date"` // 租赁期起
  463. RentEndDate string `json:"rent_end_date"` // 租赁期止
  464. IntercityFlag string `json:"intercity_flag"` // 跨地市标志
  465. UnitOfArea int `json:"unit_of_area"` // 面积单位
  466. }
  467. type PassengerTransport struct {
  468. Traveler string `json:"traveler,omitempty"` // 出行人
  469. IDType int `json:"id_type,omitempty"` // 证件类型
  470. IDNumber string `json:"id_number,omitempty"` // 证件号码
  471. TravelDate string `json:"travel_date,omitempty"` // 出行日期
  472. DeparturePlace string `json:"departure_place,omitempty"` // 出发地
  473. DestinationPlace string `json:"destination_palce,omitempty"` // 到达地
  474. VehicleType int `json:"vehicle_type,omitempty"` // 交通工具类型
  475. Level string `json:"level,omitempty"` // 等级
  476. }
  477. type GoodsTransport struct {
  478. TransportNumber string `json:"transport_number,omitempty"` // 运输工具号牌
  479. TransportKind string `json:"transport_kind,omitempty"` // 运输工具种类
  480. GoodsName string `json:"goods_name,omitempty"` // 货物名称
  481. DeparturePlace string `json:"departure_place,omitempty"` // 出发地
  482. DestinationPlace string `json:"destination_place,omitempty"` // 到达地
  483. }
  484. type AgriculturalPurchase struct {
  485. IDType string `json:"id_type,omitempty"` // 证件类型
  486. }
  487. type BuildingService struct {
  488. Site string `json:"site"` // 建筑服务发生地
  489. DetailSite string `json:"detail_site,omitempty"` // 详细地址
  490. BuildingProject string `json:"building_project"` // 建筑项目名称
  491. CrossSign string `json:"cross_sign"` // 跨地市标志
  492. }
  493. type RealStaticSales struct {
  494. PropertyNum string `json:"property_num,omitempty"` // 房屋产权证书号
  495. Location string `json:"location"` // 不动产地址
  496. AddressDetail string `json:"address_detail,omitempty"` // 不动产详细地址
  497. CrossSign string `json:"cross_sign"` // 跨市标志
  498. UnitOfArea int `json:"unit_of_area"` // 面积单位
  499. OnlineContractCode string `json:"online_contract_code,omitempty"` // 不动产单元代码/网签合同编码
  500. }
  501. type TractorCombine struct {
  502. ChassisNumber string `json:"chassis_number,omitempty"` // 底盘号或机架号
  503. EngineNumber string `json:"engine_number"` // 发动机号
  504. IsRegister string `json:"is_register,omitempty"` // 是否用于拖拉机和联合收割机登记
  505. }
  506. type UsedCarService struct {
  507. TicketSN string `json:"ticket_sn"` // 发票号码
  508. PaperTicketCode string `json:"paper_ticket_code"` // 纸票发票代码
  509. PaperTicketSN string `json:"paper_ticket_sn"` // 纸票发票号码
  510. }
  511. type UsedCarSell struct {
  512. FrameNumber string `json:"frame_number"` // 车架号码
  513. LicensePlateNumber string `json:"license_plate_number"` // 车牌号
  514. BrandModel string `json:"brand_model"` // 厂牌型号
  515. RegistrationCertificate string `json:"registration_certificate"` // 登记证号
  516. TransferOfficeName string `json:"transfer_office_name"` // 转入地车辆管理所名称
  517. NatureOfEnterprise string `json:"nature_of_enterprise"` // 企业性质
  518. }
  519. type DeductionService struct {
  520. TaxDiffServiceFlag int `json:"tax_diff_service_flag,omitempty"` // 差额征税标识
  521. TaxableServiceName string `json:"taxable_service_name,omitempty"` // 差额征税项目名称
  522. }
  523. type Item struct {
  524. ItemName string `json:"item_name"` // 商品名称
  525. ItemSpec string `json:"item_spec,omitempty"` // 商品规格
  526. ItemUnit string `json:"item_unit"` // 商品单位
  527. ItemQuantity float64 `json:"item_quantity"` // 商品数量
  528. ItemPrice float64 `json:"item_price"` // 商品单价
  529. ItemAmount float64 `json:"item_amount"` // 商品金额
  530. ItemTaxRate float64 `json:"item_tax_rate"` // 商品税率
  531. ItemTaxAmount float64 `json:"item_tax_amount"` // 商品税额
  532. RowType int `json:"row_type"` // 行性质标志
  533. }
  534. func (c *FapiaoApiController) GetFaPiaoList() {
  535. page, _ := c.GetInt64("page", -1)
  536. limit, _ := c.GetInt64("limit", -1)
  537. start_time := c.GetString("start_time")
  538. end_time := c.GetString("end_time")
  539. timeLayout := "2006-01-02"
  540. loc, _ := time.LoadLocation("Local")
  541. var startTime int64
  542. if len(start_time) > 0 {
  543. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  544. if err != nil {
  545. //fmt.Println(err)
  546. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  547. return
  548. }
  549. startTime = theTime.Unix()
  550. }
  551. var endTime int64
  552. if len(end_time) > 0 {
  553. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  554. if err != nil {
  555. utils.ErrorLog(err.Error())
  556. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  557. return
  558. }
  559. endTime = theTime.Unix()
  560. }
  561. adminUser := c.GetAdminUserInfo()
  562. org_id := adminUser.CurrentOrgId
  563. order, err, total := service.GetFaPiaoList(org_id, page, limit, startTime, endTime)
  564. if err == nil {
  565. c.ServeSuccessJSON(map[string]interface{}{
  566. "fapiao_record": order,
  567. "total": total,
  568. })
  569. } else {
  570. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  571. return
  572. }
  573. }
  574. func (c *FapiaoApiController) GetOrderFaPiaoList() {
  575. page, _ := c.GetInt64("page", -1)
  576. limit, _ := c.GetInt64("limit", -1)
  577. start_time := c.GetString("start_time")
  578. end_time := c.GetString("end_time")
  579. is_open_fapiao, _ := c.GetInt64("is_open_fapiao", 0)
  580. sort_type, _ := c.GetInt64("sort_type", 0)
  581. patient_id, _ := c.GetInt64("patient_id", 0)
  582. timeLayout := "2006-01-02"
  583. loc, _ := time.LoadLocation("Local")
  584. var startTime int64
  585. if len(start_time) > 0 {
  586. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  587. if err != nil {
  588. //fmt.Println(err)
  589. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  590. return
  591. }
  592. startTime = theTime.Unix()
  593. }
  594. var endTime int64
  595. if len(end_time) > 0 {
  596. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  597. if err != nil {
  598. utils.ErrorLog(err.Error())
  599. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  600. return
  601. }
  602. endTime = theTime.Unix()
  603. }
  604. adminUser := c.GetAdminUserInfo()
  605. org_id := adminUser.CurrentOrgId
  606. order, err, total := service.GetFaPiaoSettleList(org_id, page, limit, startTime, endTime, sort_type, start_time, end_time, is_open_fapiao, patient_id)
  607. if err == nil {
  608. c.ServeSuccessJSON(map[string]interface{}{
  609. "order": order,
  610. "total": total,
  611. })
  612. } else {
  613. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  614. return
  615. }
  616. }
  617. func (c *FapiaoApiController) GetLoginUploadCode() {
  618. sdk := invoice.NewSdk(common.HMAC_SHA256, "5058f0defd12047ee2a7", "51440fbee667ea9a53aaa68c9c408367", "", "test")
  619. var config models.FapiaoConfig
  620. config, _ = service.FindFaPiaoConfigInfo(c.GetAdminUserInfo().CurrentOrgId)
  621. routerAddress, postData := fapiao.GetSMSCode(config) //发票开具
  622. fmt.Println(routerAddress)
  623. fmt.Println(postData)
  624. r, err := sdk.HttpPost("https://apigw.goldentec.com", routerAddress, postData)
  625. if err != nil {
  626. fmt.Println(err)
  627. } else {
  628. var resp SmsCode
  629. // 将 byte 切片转换为结构体
  630. json.Unmarshal(r, &resp)
  631. if resp.Code == 0 {
  632. c.ServeSuccessJSON(map[string]interface{}{
  633. "msg": resp.Message,
  634. })
  635. }
  636. }
  637. }
  638. type SmsCode struct {
  639. Code int `json:"code"`
  640. Message string `json:"message"`
  641. }
  642. func (c *FapiaoApiController) Login() {
  643. code := c.GetString("code")
  644. var config models.FapiaoConfig
  645. config, _ = service.FindFaPiaoConfigInfo(c.GetAdminUserInfo().CurrentOrgId)
  646. sdk := invoice.NewSdk(common.HMAC_SHA256, "5058f0defd12047ee2a7", "51440fbee667ea9a53aaa68c9c408367", "", "test")
  647. routerAddress, postData := fapiao.Login(code, config) //发票开具
  648. r, err := sdk.HttpPost("https://apigw.goldentec.com", routerAddress, postData)
  649. if err != nil {
  650. fmt.Println(err)
  651. } else {
  652. var resp SmsCode
  653. // 将 byte 切片转换为结构体
  654. json.Unmarshal(r, &resp)
  655. if resp.Code == 0 {
  656. c.ServeSuccessJSON(map[string]interface{}{
  657. "msg": resp.Message,
  658. })
  659. }
  660. }
  661. }
  662. func (c *FapiaoApiController) GetQRCode() {
  663. sdk := invoice.NewSdk(common.HMAC_SHA256, "5058f0defd12047ee2a7", "51440fbee667ea9a53aaa68c9c408367", "", "test")
  664. var config models.FapiaoConfig
  665. config, _ = service.FindFaPiaoConfigInfo(c.GetAdminUserInfo().CurrentOrgId)
  666. routerAddress, postData := fapiao.GetQRCode(config) //发票开具
  667. r, err := sdk.HttpPost("https://apigw.goldentec.com", routerAddress, postData)
  668. if err != nil {
  669. fmt.Println(err)
  670. } else {
  671. var resp QRAuth
  672. // 将 byte 切片转换为结构体
  673. err := json.Unmarshal(r, &resp)
  674. fmt.Println(resp)
  675. if err != nil {
  676. fmt.Println("Error unmarshalling:", err)
  677. } else {
  678. if resp.Code == 0 {
  679. decodedBytes, _ := base64.StdEncoding.DecodeString(resp.Data.QrCode)
  680. if len(resp.Data.AuthId) == 0 { //还没进行授权,则获取二维码信息
  681. c.ServeSuccessJSON(map[string]interface{}{
  682. "cus_status": 1,
  683. "qr_code": string(decodedBytes),
  684. })
  685. } else {
  686. status := GetQRStatus(c.GetAdminUserInfo().CurrentOrgId, resp.Data.AuthId)
  687. if status == "UNDEFINE" {
  688. c.ServeSuccessJSON(map[string]interface{}{
  689. "cus_status": -1,
  690. "msg": "未定义",
  691. })
  692. } else if status == "UNHANDLED" {
  693. c.ServeSuccessJSON(map[string]interface{}{
  694. "cus_status": 1,
  695. "qr_code": string(decodedBytes),
  696. })
  697. } else if status == "REAL_AUTH_SUCCESS" {
  698. c.ServeSuccessJSON(map[string]interface{}{
  699. "cus_status": 2,
  700. "msg": "已经实名认证成功",
  701. })
  702. } else if status == "REFRESH" {
  703. c.ServeSuccessJSON(map[string]interface{}{
  704. "cus_status": 1,
  705. "qr_code": string(decodedBytes),
  706. })
  707. } else if status == "NEED_RELOGIN_BUREAU" {
  708. c.ServeSuccessJSON(map[string]interface{}{
  709. "cus_status": -3,
  710. "msg": "登录失效,请重新短信登录税局",
  711. })
  712. }
  713. }
  714. } else {
  715. c.ServeSuccessJSON(map[string]interface{}{
  716. "cus_status": -4,
  717. "msg": resp.Message,
  718. })
  719. }
  720. }
  721. //fmt.Println(string(r))
  722. }
  723. }
  724. func (c *FapiaoApiController) OpenFaPiao() {
  725. //var config models.FapiaoConfig
  726. //config, _ = service.FindFaPiaoConfigInfo(c.GetAdminUserInfo().CurrentOrgId)
  727. //认证接口
  728. sdk := invoice.NewSdk(common.HMAC_SHA256, "5058f0defd12047ee2a7", "51440fbee667ea9a53aaa68c9c408367", "", "")
  729. //status := GetQRStatusTwo(c.GetAdminUserInfo().CurrentOrgId)
  730. //fmt.Println("11112222")
  731. //
  732. //fmt.Println(status)
  733. //if status == 0 {
  734. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAuthWrong)
  735. // return
  736. //}
  737. //查询余量接口
  738. //routerAddress, postData := fapiao.QueryStock(config)
  739. //r, err := sdk.HttpPost("https://apigw.goldentec.com", routerAddress, postData)
  740. //fmt.Println(string(r))
  741. //if err != nil {
  742. // fmt.Println(err)
  743. //} else {
  744. // var resp2 FapiaoStock
  745. // // 将 byte 切片转换为结构体
  746. // err2 := json.Unmarshal(r, &resp2)
  747. // if err2 != nil {
  748. // fmt.Println(err2)
  749. //
  750. // } else {
  751. // if resp2.Code == 0 {
  752. // if resp2.Data.LeftQuantity <= 0 {
  753. // c.ServeSuccessJSON(map[string]interface{}{
  754. // "msg": "发票余量不足,请登录高灯平台申领发票",
  755. // "status": -1,
  756. // })
  757. // return
  758. // }
  759. // }
  760. // }
  761. //}
  762. //return
  763. //开蓝票逻辑
  764. order_ids := c.GetString("order_ids")
  765. admin_user_id, _ := c.GetInt64("admin_user_id")
  766. ids := strings.Split(order_ids, ",")
  767. orders, _ := service.GetFaPiaoOrderByIDS(ids)
  768. var MedfeeSumamt float64 = 0 //治疗费用
  769. var PsnCashPay float64 = 0 //治疗费用
  770. var FundPaySumamt float64 = 0 //治疗费用
  771. var HifmiPay float64 = 0 //治疗费用
  772. var MafPay float64 = 0 //治疗费用
  773. var HifpPay float64 = 0 //治疗费用
  774. var AcctPay float64 = 0 //治疗费用
  775. var details []models.HisOrderInfo
  776. var numbers []string
  777. decimal.DivisionPrecision = 2
  778. var remark string
  779. for _, oss := range orders {
  780. MedfeeSumamt, _ = decimal.NewFromFloat(MedfeeSumamt).Add(decimal.NewFromFloat(oss.MedfeeSumamt)).Float64()
  781. PsnCashPay, _ = decimal.NewFromFloat(PsnCashPay).Add(decimal.NewFromFloat(oss.PsnCashPay)).Float64()
  782. FundPaySumamt, _ = decimal.NewFromFloat(FundPaySumamt).Add(decimal.NewFromFloat(oss.FundPaySumamt)).Float64()
  783. HifmiPay, _ = decimal.NewFromFloat(HifmiPay).Add(decimal.NewFromFloat(oss.HifmiPay)).Float64()
  784. MafPay, _ = decimal.NewFromFloat(MafPay).Add(decimal.NewFromFloat(oss.MafPay)).Float64()
  785. HifpPay, _ = decimal.NewFromFloat(HifpPay).Add(decimal.NewFromFloat(oss.HifpPay)).Float64()
  786. AcctPay, _ = decimal.NewFromFloat(AcctPay).Add(decimal.NewFromFloat(oss.AcctPay)).Float64()
  787. numbers = append(numbers, oss.Number)
  788. }
  789. if MedfeeSumamt > 0 {
  790. MedfeeSumamt_str := fmt.Sprintf("%.2f", MedfeeSumamt)
  791. if len(remark) == 0 {
  792. //MedfeeSumamt_str := fmt.Sprintf("%.2f", MedfeeSumamt)
  793. remark = "医疗总费用: " + MedfeeSumamt_str
  794. } else {
  795. remark = remark + " 医疗总费用:" + MedfeeSumamt_str
  796. }
  797. }
  798. if FundPaySumamt > 0 {
  799. MedfeeSumamt_str := fmt.Sprintf("%.2f", FundPaySumamt)
  800. if len(remark) == 0 {
  801. //MedfeeSumamt_str := fmt.Sprintf("%.2f", MedfeeSumamt)
  802. remark = "基金支付总金额: " + MedfeeSumamt_str
  803. } else {
  804. remark = remark + " 基金支付总金额:" + MedfeeSumamt_str
  805. }
  806. }
  807. if PsnCashPay > 0 {
  808. MedfeeSumamt_str := fmt.Sprintf("%.2f", PsnCashPay)
  809. if len(remark) == 0 {
  810. //MedfeeSumamt_str := fmt.Sprintf("%.2f", MedfeeSumamt)
  811. remark = "个人现金支付: " + MedfeeSumamt_str
  812. } else {
  813. remark = remark + " 个人现金支付:" + MedfeeSumamt_str
  814. }
  815. }
  816. if HifmiPay > 0 {
  817. MedfeeSumamt_str := fmt.Sprintf("%.2f", HifmiPay)
  818. if len(remark) == 0 {
  819. //MedfeeSumamt_str := fmt.Sprintf("%.2f", MedfeeSumamt)
  820. remark = "大病基金支付: " + MedfeeSumamt_str
  821. } else {
  822. remark = remark + " 大病基金支付:" + MedfeeSumamt_str
  823. }
  824. }
  825. if AcctPay > 0 {
  826. MedfeeSumamt_str := fmt.Sprintf("%.2f", AcctPay)
  827. if len(remark) == 0 {
  828. //MedfeeSumamt_str := fmt.Sprintf("%.2f", MedfeeSumamt)
  829. remark = "个账支付: " + MedfeeSumamt_str
  830. } else {
  831. remark = remark + " 个账支付:" + MedfeeSumamt_str
  832. }
  833. }
  834. if HifpPay > 0 {
  835. MedfeeSumamt_str := fmt.Sprintf("%.2f", HifpPay)
  836. if len(remark) == 0 {
  837. //MedfeeSumamt_str := fmt.Sprintf("%.2f", MedfeeSumamt)
  838. remark = "基本统筹基金支付: " + MedfeeSumamt_str
  839. } else {
  840. remark = remark + " 基本统筹基金支付:" + MedfeeSumamt_str
  841. }
  842. }
  843. if MafPay > 0 {
  844. MedfeeSumamt_str := fmt.Sprintf("%.2f", MafPay)
  845. if len(remark) == 0 {
  846. //MedfeeSumamt_str := fmt.Sprintf("%.2f", MedfeeSumamt)
  847. remark = "医疗救助基金支付: " + MedfeeSumamt_str
  848. } else {
  849. remark = remark + " 医疗救助基金支付:" + MedfeeSumamt_str
  850. }
  851. }
  852. details, _ = service.GetFaPiaoOrderInfoByNumbers(numbers)
  853. role, _ := service.GetAdminUserRole(admin_user_id, c.GetAdminUserInfo().CurrentOrgId)
  854. //role.UserName = ""
  855. type CostType struct {
  856. name string
  857. price float64
  858. }
  859. var cts []CostType
  860. var ct CostType
  861. var bedCostTotal float64 = 0 //床位总费
  862. var operationCostTotal float64 = 0 //手术费
  863. var otherCostTotal float64 = 0 //其他费用
  864. var materialCostTotal float64 = 0 //材料费
  865. var westernMedicineCostTotal float64 = 0 //西药费
  866. var chineseTraditionalMedicineCostTotal float64 = 0 //中成药
  867. var checkCostTotal float64 = 0 //检查费
  868. var laboratoryCostTotal float64 = 0 //化验费
  869. var treatCostTotal float64 = 0 //治疗费用
  870. var huliCostTotal float64 = 0 //治疗费用
  871. var zhencaCostTotal float64 = 0 //治疗费用
  872. for _, item := range details {
  873. if item.MedChrgitmType == "01" { //床位费
  874. bedCostTotal, _ = decimal.NewFromFloat(bedCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  875. }
  876. if item.MedChrgitmType == "07" { //护理费
  877. huliCostTotal, _ = decimal.NewFromFloat(huliCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  878. }
  879. if c.GetAdminUserInfo().CurrentOrgId == 10188 || c.GetAdminUserInfo().CurrentOrgId == 10217 {
  880. if item.MedChrgitmType == "03" { //检查费
  881. laboratoryCostTotal, _ = decimal.NewFromFloat(laboratoryCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  882. }
  883. } else {
  884. if item.MedChrgitmType == "03" { //检查费
  885. checkCostTotal, _ = decimal.NewFromFloat(checkCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  886. }
  887. }
  888. if item.MedChrgitmType == "04" { //化验费
  889. laboratoryCostTotal, _ = decimal.NewFromFloat(laboratoryCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  890. }
  891. if item.MedChrgitmType == "05" || item.MedChrgitmType == "1402" || item.MedChrgitmType == "1403" { //治疗费
  892. treatCostTotal, _ = decimal.NewFromFloat(treatCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  893. }
  894. if item.MedChrgitmType == "06" { //手术费
  895. operationCostTotal, _ = decimal.NewFromFloat(operationCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  896. }
  897. if item.MedChrgitmType == "08" { //材料费
  898. materialCostTotal, _ = decimal.NewFromFloat(materialCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  899. }
  900. if item.MedChrgitmType == "09" { //西药费
  901. westernMedicineCostTotal, _ = decimal.NewFromFloat(westernMedicineCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  902. }
  903. if item.MedChrgitmType == "11" { //中成费
  904. chineseTraditionalMedicineCostTotal, _ = decimal.NewFromFloat(chineseTraditionalMedicineCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  905. }
  906. if item.MedChrgitmType == "14" || item.MedChrgitmType == "0" || item.MedChrgitmType == "12" { //其他费
  907. otherCostTotal, _ = decimal.NewFromFloat(otherCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  908. }
  909. if item.MedChrgitmType == "02" { //诊察
  910. zhencaCostTotal, _ = decimal.NewFromFloat(zhencaCostTotal).Add(decimal.NewFromFloat(item.DetItemFeeSumamt)).Float64()
  911. }
  912. }
  913. if bedCostTotal > 0 {
  914. ct.name = "床位费"
  915. ct.price = bedCostTotal
  916. cts = append(cts, ct)
  917. }
  918. if operationCostTotal > 0 {
  919. ct.name = "手术费"
  920. ct.price = operationCostTotal
  921. cts = append(cts, ct)
  922. }
  923. if otherCostTotal > 0 {
  924. ct.name = "其他费"
  925. ct.price = otherCostTotal
  926. cts = append(cts, ct)
  927. }
  928. if materialCostTotal > 0 {
  929. ct.name = "材料费"
  930. ct.price = materialCostTotal
  931. cts = append(cts, ct)
  932. }
  933. if westernMedicineCostTotal > 0 {
  934. ct.name = "西药费"
  935. ct.price = westernMedicineCostTotal
  936. cts = append(cts, ct)
  937. }
  938. if chineseTraditionalMedicineCostTotal > 0 {
  939. ct.name = "中成药费"
  940. ct.price = chineseTraditionalMedicineCostTotal
  941. cts = append(cts, ct)
  942. }
  943. if checkCostTotal > 0 {
  944. ct.name = "检查费"
  945. ct.price = checkCostTotal
  946. cts = append(cts, ct)
  947. }
  948. if laboratoryCostTotal > 0 {
  949. ct.name = "化验费"
  950. ct.price = laboratoryCostTotal
  951. cts = append(cts, ct)
  952. }
  953. if treatCostTotal > 0 {
  954. ct.name = "治疗费"
  955. ct.price = treatCostTotal
  956. cts = append(cts, ct)
  957. }
  958. if huliCostTotal > 0 {
  959. ct.name = "护理费"
  960. ct.price = huliCostTotal
  961. cts = append(cts, ct)
  962. }
  963. if zhencaCostTotal > 0 {
  964. ct.name = "诊察费"
  965. ct.price = zhencaCostTotal
  966. cts = append(cts, ct)
  967. }
  968. fmt.Println(cts)
  969. org_config, _ := service.FindMedicalInsuranceInfo(c.GetAdminUserInfo().CurrentOrgId)
  970. var config2 models.FapiaoConfig
  971. config2, _ = service.FindFaPiaoConfigInfo(c.GetAdminUserInfo().CurrentOrgId)
  972. number := strconv.FormatInt(time.Now().Unix(), 10) + "_" + strconv.FormatInt(orders[0].UserOrgId, 10) + "_" + strconv.FormatInt(orders[0].PatientId, 10)
  973. amountWithoutTax, _, taxAmount, amountHasTax := CalculatePriceDetails(MedfeeSumamt, 1, 0.00)
  974. patient, _ := service.GetXTPatientInfo(c.GetAdminUserInfo().CurrentOrgId, orders[0].PatientId)
  975. orders[0].PsnName = patient.Name
  976. role.UserName = "陈金娣"
  977. postData3 := map[string]interface{}{
  978. "seller_name": org_config.OrgName,
  979. "seller_taxpayer_num": config2.SellerTaxpayerNum,
  980. "seller_address": config2.SellerAddress,
  981. "seller_tel": config2.SellerTel,
  982. "seller_bank_name": config2.SellerBankName,
  983. "seller_bank_account": config2.SellerBankAccount,
  984. "title_type": 1,
  985. "buyer_title": orders[0].PsnName + "(个人)",
  986. "buyer_taxpayer_num": "",
  987. "buyer_address": "",
  988. "buyer_phone": "",
  989. "buyer_bank_name": "",
  990. "buyer_bank_account": "",
  991. "buyer_email": "",
  992. "order_id": number,
  993. "invoice_type_code": "082",
  994. "callback_url": "http://www.goldentec.com/callback",
  995. "drawer": role.UserName,
  996. "payee": "",
  997. "checker": "",
  998. "terminal_code": config2.TerminalCode,
  999. "user_openid": "",
  1000. "special_invoice_kind": "",
  1001. "zsfs": "",
  1002. "deduction": "",
  1003. "amount_has_tax": amountHasTax,
  1004. "tax_amount": taxAmount,
  1005. "amount_without_tax": amountWithoutTax,
  1006. "remark": remark,
  1007. }
  1008. ////项目商品明细
  1009. items := make([]map[string]interface{}, 0)
  1010. for _, ct := range cts {
  1011. item := make(map[string]interface{})
  1012. amountWithoutTax, _, taxAmount, _ := CalculatePriceDetails(ct.price, 1, 0.00)
  1013. item["name"] = ct.name
  1014. item["tax_code"] = "3070202000000000000"
  1015. item["models"] = ""
  1016. item["unit"] = ""
  1017. item["total_price"] = amountWithoutTax
  1018. item["total"] = "1"
  1019. item["price"] = amountWithoutTax
  1020. item["tax_rate"] = "0.00"
  1021. item["tax_amount"] = taxAmount
  1022. item["discount"] = "0"
  1023. item["preferential_policy_flag"] = "1"
  1024. item["zero_tax_flag"] = "1"
  1025. item["vat_special_management"] = "免税"
  1026. items = append(items, item)
  1027. }
  1028. postData3["items"] = items
  1029. routerAddress, postData4 := fapiao.Blue(postData3) //发票开具
  1030. fmt.Println(postData4)
  1031. r, err4 := sdk.HttpPost("https://apigw.goldentec.com", routerAddress, postData4)
  1032. fmt.Println("blue")
  1033. fmt.Println(err4)
  1034. fmt.Println(string(r))
  1035. if err4 != nil {
  1036. fmt.Println(err4)
  1037. } else {
  1038. var resp OpenFaPiaoResultResponse
  1039. // 将 byte 切片转换为结构体
  1040. err := json.Unmarshal(r, &resp)
  1041. fmt.Println(resp)
  1042. if err != nil {
  1043. fmt.Println("Error unmarshalling:", err)
  1044. } else {
  1045. fmt.Println(resp)
  1046. if resp.Code == 0 {
  1047. var fapiaos models.HisFaPiaoOrder
  1048. fapiaos.InvoiceId = resp.Data.InvoiceId
  1049. fapiaos.OrderSn = resp.Data.OrderSn
  1050. fapiaos.FapiaoStatus = "1"
  1051. fapiaos.OrderIds = order_ids
  1052. fapiaos.UserOrgId = c.GetAdminUserInfo().CurrentOrgId
  1053. fapiaos.Status = 1
  1054. fapiaos.PatientId = orders[0].PatientId
  1055. fapiaos.Ctime = time.Now().Unix()
  1056. fapiaos.Mtime = time.Now().Unix()
  1057. fapiaos.Creator = role.UserName
  1058. fapiaos.MedfeeSumamt = MedfeeSumamt
  1059. fapiaos.FundPaySumamt = FundPaySumamt
  1060. fapiaos.PsnCashPay = PsnCashPay
  1061. fmt.Println(ids)
  1062. service.UpdateFaPiaoSN(resp.Data.OrderSn, ids) //同步发票号码
  1063. err5 := service.SaveFaPiaoOrder(&fapiaos)
  1064. // 异步调用查询操作,通过闭包传递参数
  1065. // 异步调用查询操作,通过闭包传递多个参数
  1066. go func(num string, sn string, number string, fapiaos models.HisFaPiaoOrder, ids []string) {
  1067. // 延迟等待一定时间再查询
  1068. time.Sleep(30 * time.Second)
  1069. // 调用查询发票的函数
  1070. //queryInvoice(id, user, retries)
  1071. routerAddress2, postData2 := fapiao.Query(num, sn, number)
  1072. r3, err2 := sdk.HttpPost("https://apigw.goldentec.com", routerAddress2, postData2)
  1073. fmt.Println("query")
  1074. fmt.Println(string(r3))
  1075. if err2 != nil {
  1076. fmt.Println(err2)
  1077. } else {
  1078. var resp3 QueryFaPiaoResultResponse
  1079. // 将 byte 切片转换为结构体
  1080. json.Unmarshal(r3, &resp3)
  1081. if resp3.Code == 0 {
  1082. if resp3.Data.Status == 3 {
  1083. // 异步调用查询操作,不阻塞主程序
  1084. fapiaos.TicketSn = resp3.Data.TicketSn
  1085. fapiaos.TicketDate = resp3.Data.TicketDate
  1086. fapiaos.AmountWithTax = resp3.Data.AmountWithoutTax
  1087. fapiaos.AmountWithoutTax = resp3.Data.AmountWithoutTax
  1088. fapiaos.FapiaoStatus = "2"
  1089. fapiaos.TaxAmount = resp3.Data.TaxAmount
  1090. fapiaos.IsRedWashed = strconv.FormatInt(int64(resp3.Data.IsRedWashed), 10)
  1091. fapiaos.PdfUrl = resp3.Data.PdfUrl
  1092. fapiaos.OfdUrl = resp3.Data.OfdUrl
  1093. fapiaos.XmlUrl = resp3.Data.XmlUrl
  1094. service.SaveFaPiaoOrder(&fapiaos)
  1095. service.UpdateFaPiaoNumber(resp3.Data.TicketSn, ids) //同步发票号码
  1096. } else if resp3.Data.Status == 1 {
  1097. fapiaos.FapiaoStatus = "1"
  1098. fapiaos.TicketSn = ""
  1099. service.SaveFaPiaoOrder(&fapiaos)
  1100. //service.UpdateFaPiaoNumber(resp3.Data.TicketSn, ids) //同步发票号码
  1101. } else if resp3.Data.Status == 2 {
  1102. fapiaos.FapiaoStatus = "3"
  1103. fapiaos.TicketSn = ""
  1104. fapiaos.Message = resp3.Data.Message
  1105. service.SaveFaPiaoOrder(&fapiaos)
  1106. } else {
  1107. fapiaos.FapiaoStatus = "3"
  1108. fapiaos.TicketSn = ""
  1109. fapiaos.Message = resp3.Data.Message
  1110. service.SaveFaPiaoOrder(&fapiaos)
  1111. }
  1112. } else {
  1113. //错误处理
  1114. c.ServeSuccessJSON(map[string]interface{}{
  1115. "msg": "查询失败" + resp3.Message,
  1116. })
  1117. }
  1118. }
  1119. }(config2.SellerTaxpayerNum, resp.Data.OrderSn, number, fapiaos, ids) // 这里将多个参数传递给闭包
  1120. if err5 == nil {
  1121. c.ServeSuccessJSON(map[string]interface{}{
  1122. "msg": "开具成功,审核中。",
  1123. })
  1124. }
  1125. } else {
  1126. //错误处理
  1127. c.ServeSuccessJSON(map[string]interface{}{
  1128. "msg": "开具失败" + resp.Message,
  1129. })
  1130. }
  1131. }
  1132. }
  1133. }
  1134. func (c *FapiaoApiController) RedFaPiao() {
  1135. id, _ := c.GetInt64("id")
  1136. admin_user_id, _ := c.GetInt64("admin_user_id")
  1137. role2, _ := service.GetAdminUserRole(admin_user_id, c.GetAdminUserInfo().CurrentOrgId)
  1138. fapiao_config, _ := service.GetFaPiaoOrderById(id)
  1139. var config models.FapiaoConfig
  1140. config, _ = service.FindFaPiaoConfigInfo(c.GetAdminUserInfo().CurrentOrgId)
  1141. sdk := invoice.NewSdk(common.HMAC_SHA256, "5058f0defd12047ee2a7", "51440fbee667ea9a53aaa68c9c408367", "", "test")
  1142. routerAddress, postData := fapiao.Red(config.SellerTaxpayerNum, "", fapiao_config.OrderSn) //发票开具
  1143. fmt.Println("postData")
  1144. fmt.Println(postData)
  1145. fmt.Println("postData")
  1146. red, err := sdk.HttpPost("https://apigw.goldentec.com", routerAddress, postData)
  1147. fmt.Println("red")
  1148. fmt.Println(red)
  1149. fmt.Println("red")
  1150. if err != nil {
  1151. fmt.Println(err)
  1152. } else {
  1153. var resp RedResult
  1154. // 将 byte 切片转换为结构体
  1155. json.Unmarshal(red, &resp)
  1156. fmt.Println("resp")
  1157. fmt.Println(resp)
  1158. fmt.Println("resp")
  1159. if resp.Code == 0 {
  1160. if resp.Data[0].State == 1 {
  1161. //查询红票开具情况
  1162. routerAddress2, postData2 := fapiao.Query(config.SellerTaxpayerNum, fapiao_config.OrderSn, "")
  1163. r3, _ := sdk.HttpPost("https://apigw.goldentec.com", routerAddress2, postData2)
  1164. var resp3 QueryFaPiaoResultResponse
  1165. // 将 byte 切片转换为结构体
  1166. json.Unmarshal(r3, &resp3)
  1167. fmt.Println("resp")
  1168. fmt.Println(string(r3))
  1169. fmt.Println(resp3)
  1170. fmt.Println("resp")
  1171. if resp3.Code == 0 {
  1172. if resp3.Data.IsRedWashed == 1 {
  1173. fapiao_config.IsRedWashed = "1"
  1174. fapiao_config.Status = 0
  1175. fapiao_config.RedInvoiceId = resp.Data[0].InvoiceId
  1176. fapiao_config.RedInvoiceCreator = role2.UserName
  1177. service.SaveFaPiaoOrder(&fapiao_config)
  1178. //将结算表的发票号码清空
  1179. service.UpdateFaPiaoNumberByNumber(fapiao_config.TicketSn)
  1180. c.ServeSuccessJSON(map[string]interface{}{
  1181. "msg": "红冲成功",
  1182. })
  1183. } else {
  1184. c.ServeSuccessJSON(map[string]interface{}{
  1185. "msg": "系统错误,红冲失败",
  1186. })
  1187. }
  1188. } else {
  1189. c.ServeSuccessJSON(map[string]interface{}{
  1190. "msg": resp3.Message,
  1191. })
  1192. }
  1193. } else {
  1194. c.ServeSuccessJSON(map[string]interface{}{
  1195. "msg": resp.Message,
  1196. })
  1197. }
  1198. } else {
  1199. c.ServeSuccessJSON(map[string]interface{}{
  1200. "msg": resp.Message,
  1201. })
  1202. }
  1203. }
  1204. }
  1205. func GetQRStatusTwo(org_id int64) (status int64) {
  1206. sdk := invoice.NewSdk(common.HMAC_SHA256, "5058f0defd12047ee2a7", "51440fbee667ea9a53aaa68c9c408367", "", "test")
  1207. var config models.FapiaoConfig
  1208. config, _ = service.FindFaPiaoConfigInfo(org_id)
  1209. routerAddress, postData := fapiao.GetQRCode(config) //发票开具
  1210. r, err := sdk.HttpPost("https://apigw.goldentec.com", routerAddress, postData)
  1211. if err != nil {
  1212. fmt.Println(err)
  1213. } else {
  1214. var resp QRAuth
  1215. // 将 byte 切片转换为结构体
  1216. err := json.Unmarshal(r, &resp)
  1217. fmt.Println(resp)
  1218. if err != nil {
  1219. fmt.Println("Error unmarshalling:", err)
  1220. } else {
  1221. if len(resp.Data.AuthId) == 0 { //还没实名认证
  1222. } else {
  1223. status = 1
  1224. status_str := GetQRStatus(org_id, resp.Data.AuthId)
  1225. if status_str == "UNDEFINE" {
  1226. status = 0
  1227. } else if status_str == "UNHANDLED" {
  1228. status = 0
  1229. } else if status_str == "REAL_AUTH_SUCCESS" {
  1230. status = 1
  1231. } else if status_str == "REFRESH" {
  1232. status = 0
  1233. } else if status_str == "NEED_RELOGIN_BUREAU" {
  1234. status = 0
  1235. }
  1236. }
  1237. }
  1238. }
  1239. return
  1240. }
  1241. type RedResult struct {
  1242. Code int `json:"code"`
  1243. Data []struct {
  1244. State int `json:"state"`
  1245. Message string `json:"message"`
  1246. OrderSn string `json:"order_sn"`
  1247. InvoiceId string `json:"invoice_id"`
  1248. } `json:"data"`
  1249. Message string `json:"message"`
  1250. }
  1251. type QueryFaPiaoResultResponse struct {
  1252. Code int `json:"code"`
  1253. Data struct {
  1254. OrderSn string `json:"order_sn"`
  1255. Status int `json:"status"`
  1256. Message string `json:"message"`
  1257. TicketDate string `json:"ticket_date"`
  1258. TicketSn string `json:"ticket_sn"`
  1259. TicketCode string `json:"ticket_code"`
  1260. CheckCode string `json:"check_code"`
  1261. AmountWithTax string `json:"amount_with_tax"`
  1262. AmountWithoutTax string `json:"amount_without_tax"`
  1263. TaxAmount string `json:"tax_amount"`
  1264. IsRedWashed int `json:"is_red_washed"`
  1265. PdfUrl string `json:"pdf_url"`
  1266. OfdUrl string `json:"ofd_url"`
  1267. XmlUrl string `json:"xml_url"`
  1268. } `json:"data"`
  1269. Message string `json:"message"`
  1270. }
  1271. type OpenFaPiaoResultResponse struct {
  1272. Code int `json:"code"`
  1273. Message string `json:"message"`
  1274. Data struct {
  1275. State int `json:"state"`
  1276. OrderSn string `json:"order_sn"`
  1277. InvoiceId string `json:"invoice_id"`
  1278. } `json:"data"`
  1279. }
  1280. type SurplusStockResponse struct {
  1281. Code int `json:"code"`
  1282. Message string `json:"message"`
  1283. Data struct {
  1284. LeftQuantity int `json:"left_quantity"`
  1285. } `json:"data"`
  1286. }
  1287. func GetFaPiaoStock(org_id int64) (left_quantity int) {
  1288. sdk := invoice.NewSdk(common.HMAC_SHA256, "8ca4ee7b152c0abceff9", "17402aff152dbeedf7a7b30be553f4c4", "", "test")
  1289. var config models.FapiaoConfig
  1290. config, _ = service.FindFaPiaoConfigInfo(org_id)
  1291. routerAddress, postData := fapiao.QueryStock(config)
  1292. r, err := sdk.HttpPost("https://apigw-test.goldentec.com", routerAddress, postData)
  1293. if err != nil {
  1294. fmt.Println(err)
  1295. } else {
  1296. var resp SurplusStockResponse
  1297. // 将 byte 切片转换为结构体
  1298. err := json.Unmarshal(r, &resp)
  1299. if err != nil {
  1300. fmt.Println("Error unmarshalling:", err)
  1301. } else {
  1302. if resp.Code == 0 {
  1303. left_quantity = resp.Data.LeftQuantity
  1304. }
  1305. }
  1306. }
  1307. return
  1308. }
  1309. type Response struct {
  1310. Code int `json:"code"`
  1311. Message string `json:"message"`
  1312. Data struct {
  1313. Status string `json:"status"`
  1314. } `json:"data"`
  1315. }
  1316. func GetQRStatus(org_id int64, auth_id string) (status string) {
  1317. sdk := invoice.NewSdk(common.HMAC_SHA256, "5058f0defd12047ee2a7", "51440fbee667ea9a53aaa68c9c408367", "", "test")
  1318. fmt.Println(sdk)
  1319. var config models.FapiaoConfig
  1320. config, _ = service.FindFaPiaoConfigInfo(org_id)
  1321. fmt.Println(config)
  1322. routerAddress, postData := fapiao.GetQRStatus(auth_id, config) //发票开具
  1323. fmt.Println(routerAddress)
  1324. fmt.Println(postData)
  1325. r, err := sdk.HttpPost("https://apigw.goldentec.com", routerAddress, postData)
  1326. if err != nil {
  1327. status = "FAILD"
  1328. fmt.Println(err)
  1329. } else {
  1330. fmt.Println(r)
  1331. var resp Response
  1332. // 将 byte 切片转换为结构体
  1333. err := json.Unmarshal(r, &resp)
  1334. fmt.Println(string(r))
  1335. if err != nil {
  1336. status = "FAILD"
  1337. fmt.Println("Error unmarshalling:", err)
  1338. } else {
  1339. status = resp.Data.Status
  1340. fmt.Printf("Parsed struct: %+v\n", resp)
  1341. }
  1342. }
  1343. return status
  1344. //return
  1345. }
  1346. // 计算商品价格、税额等函数
  1347. func CalculatePriceDetails(totalCost float64, itemCount int, taxRate float64) (string, string, string, string) {
  1348. // 商品不含税总金额,保留两位小数
  1349. amountWithoutTax := roundTo(totalCost/(1+taxRate), 2)
  1350. // 商品不含税单价,保留八位小数
  1351. pricePerItem := roundTo(amountWithoutTax/float64(itemCount), 8)
  1352. // 税额,保留两位小数
  1353. taxAmount := roundTo(amountWithoutTax*taxRate, 2)
  1354. // 含税总金额
  1355. amountHasTax := amountWithoutTax + taxAmount
  1356. return fmt.Sprintf("%f", amountWithoutTax), fmt.Sprintf("%f", pricePerItem), fmt.Sprintf("%f", taxAmount), fmt.Sprintf("%f", amountHasTax)
  1357. }
  1358. // 保留指定小数位数的函数
  1359. func roundTo(value float64, places int) float64 {
  1360. scale := math.Pow(10, float64(places))
  1361. return math.Round(value*scale) / scale
  1362. }
  1363. type QRAuth struct {
  1364. Code int `json:"code"`
  1365. Message string `json:"message"`
  1366. Data struct {
  1367. AuthId string `json:"auth_id"`
  1368. QrCode string `json:"qr_code"`
  1369. } `json:"data"`
  1370. }
  1371. type FapiaoStock struct {
  1372. Code int `json:"code"`
  1373. Data struct {
  1374. LeftQuantity int `json:"left_quantity"`
  1375. } `json:"data"`
  1376. Message string `json:"message"`
  1377. }