invoice.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. package invoice
  2. import (
  3. "dashoo.cn/backend/api/business/invoiceinfo"
  4. "dashoo.cn/backend/api/business/oilsupplier/supplier"
  5. "dashoo.cn/backend/api/business/paymentinfo"
  6. "dashoo.cn/backend/api/business/register"
  7. "dashoo.cn/backend/api/business/workflow"
  8. . "dashoo.cn/backend/api/controllers"
  9. "dashoo.cn/backend/api/models"
  10. baseparameter "dashoo.cn/business2/parameter"
  11. "dashoo.cn/utils"
  12. "encoding/json"
  13. "fmt"
  14. "strconv"
  15. "time"
  16. )
  17. type OilInvoiceController struct {
  18. BaseController
  19. }
  20. type InvoiceJson struct {
  21. Identity string `json:"identity"`
  22. Order InvoiceOrderJson `json:"order"`
  23. }
  24. type InvoiceId struct {
  25. Id string `json:"Id"`
  26. SrcIds string `json:"SrcIds"`
  27. }
  28. type SelectInvoiceJson struct {
  29. Identity string `json:"identity"`
  30. Fpqqlsh string `json:"fpqqlsh"`
  31. }
  32. type InvoiceOrderJson struct {
  33. Buyername string `json:"buyername"`
  34. Phone string `json:"phone"`
  35. Taxnum string `json:"taxnum"`
  36. Address string `json:"address"`
  37. Account string `json:"account"`
  38. Telephone string `json:"telephone"`
  39. Orderno string `json:"orderno"`
  40. Invoicedate string `json:"invoicedate"`
  41. Clerk string `json:"clerk"`
  42. Saleaccount string `json:"saleaccount"`
  43. Salephone string `json:"salephone"`
  44. Saleaddress string `json:"saleaddress"`
  45. Saletaxnum string `json:"saletaxnum"`
  46. Kptype string `json:"kptype"`
  47. Message string `json:"message"`
  48. Payee string `json:"payee"`
  49. Checker string `json:"checker"`
  50. Tsfs string `json:"tsfs"`
  51. Email string `json:"email"`
  52. Qdbz string `json:"qdbz"`
  53. Qdxmmc string `json:"qdxmmc"`
  54. Dkbz string `json:"dkbz"`
  55. Deptid string `json:"deptid"`
  56. Clerkid string `json:"clerkid"`
  57. InvoiceLine string `json:"invoiceLine"`
  58. Cpybz string `json:"cpybz"`
  59. Detail []InvoiceDetailJson `json:"detail"`
  60. }
  61. type InvoiceDetailJson struct {
  62. Goodsname string `json:"goodsname"`
  63. Num string `json:"num"`
  64. Price string `json:"price"`
  65. Hsbz string `json:"hsbz"`
  66. Taxrate string `json:"taxrate"`
  67. Spec string `json:"spec"`
  68. Unit string `json:"unit"`
  69. Spbm string `json:"spbm"`
  70. Zsbm string `json:"zsbm"`
  71. Fphxz string `json:"fphxz"`
  72. Yhzcbs string `json:"yhzcbs"`
  73. Zzstsgl string `json:"zzstsgl"`
  74. Lslbs string `json:"lslbs"`
  75. Kce string `json:"kce"`
  76. Taxfreeamt string `json:"taxfreeamt"`
  77. Tax string `json:"tax"`
  78. Taxamt string `json:"taxamt"`
  79. }
  80. // @Title 开票
  81. // @Description 开票
  82. // @Success 200 {object} controllers.Request
  83. // @router /openInvoice [get]
  84. func (this *OilInvoiceController) OpenInvoice() {
  85. var invoiceId InvoiceId
  86. id := this.GetString("Id")
  87. srcIds := this.GetString("SrcIds")
  88. invoiceId.Id = id
  89. invoiceId.SrcIds = srcIds
  90. selS, _ := json.Marshal(invoiceId)
  91. invoice := invoiceinfo.GetInvoiceService(utils.DBE)
  92. Url := utils.Cfg.MustValue("Invoice", "invoiceUrl")
  93. userToken, _ := models.CreateToken(this.User.Username)
  94. //云平台链接23,然后请求诺诺
  95. go func () {
  96. invoice.Post2(Url, string(selS), userToken.Token)
  97. }()
  98. var errinfo ErrorInfo
  99. errinfo.Code = 0
  100. errinfo.Message = "开票成功!"
  101. this.Data["json"] = &errinfo
  102. this.ServeJSON()
  103. }
  104. // @Title 开票and更新发票状态
  105. // @Description 更新发票状态
  106. // @Success 200 {object} controllers.Request
  107. // @router /updateisinvoice [post]
  108. func (this *OilInvoiceController) UpdateIsInvoice() {
  109. var invoiceId InvoiceId
  110. var jsonBlob = this.Ctx.Input.RequestBody
  111. json.Unmarshal(jsonBlob, &invoiceId)
  112. session := utils.DBE.NewSession()
  113. defer session.Close()
  114. err := session.Begin()
  115. paramSvc := baseparameter.GetBaseparameterService(utils.DBE)
  116. key := paramSvc.GetBaseparameterMessage("INVOICE", "paramset", "InvoiceKey64")
  117. // 诺诺网发票接口
  118. invoice := invoiceinfo.GetInvoiceService(utils.DBE)
  119. url := paramSvc.GetBaseparameterMessage("INVOICE", "paramset", "InvoiceUrl")
  120. var entity invoiceinfo.OilInvoiceInfo
  121. where := "Id=" + invoiceId.Id
  122. invoice.GetEntity(&entity, where)
  123. sup := supplier.GetOilSupplierService(utils.DBE)
  124. var supplierEntity supplier.OilSupplier
  125. whereS := "Id=" + strconv.Itoa(entity.SupplierId)
  126. sup.GetEntity(&supplierEntity, whereS)
  127. var paylist []paymentinfo.PaymentinfoList
  128. svcp := paymentinfo.GetPaymentService(utils.DBE)
  129. where = "p.Id in (" + entity.SrcIds + ")"
  130. svcp.GetPaymentinfoList1(where, &paylist)
  131. //paramsString := ""
  132. var paramsDetail []InvoiceDetailJson
  133. for _, pay := range paylist {
  134. var detail InvoiceDetailJson
  135. var amount float64
  136. var rate1 float64
  137. var a float64
  138. var tax string
  139. yhzcbs := "0"
  140. environment := paramSvc.GetBaseparameterMessage("INVOICE", "paramset", "isDev")
  141. zzstsgl := paramSvc.GetBaseparameterMessage("INVOICE", "paramset", "InvoiceZzstsgl")
  142. if environment == "1" {
  143. // 测试环境
  144. pay.OilPaymentInfo.Amount = "500.00"
  145. }
  146. amount,_ = strconv.ParseFloat(pay.OilPaymentInfo.Amount, 64)
  147. rate := paramSvc.GetBaseparameterMessage("INVOICE", "paramset", "InvoiceRate")
  148. //含税
  149. israte := paramSvc.GetBaseparameterMessage("INVOICE", "paramset", "IsInvoiceRate")
  150. //"yhzcbs":"1","zzstsgl":"免税","lslbs":"1" ----免税
  151. //"yhzcbs":1,"zzstsgl":"不征税", "lslbs":2 ----不征税
  152. //"yhzcbs":"0","zzstsgl":"","lslbs":3 -------零税率
  153. if israte == "" {
  154. zzstsgl = ""
  155. rate1,_ = strconv.ParseFloat(rate, 64)
  156. aq := rate1 + 1
  157. tax = fmt.Sprintf("%.2f", amount * rate1 / aq)
  158. //tax := math.Trunc(amount * 0.06 / 1.06)
  159. a,_ = strconv.ParseFloat(tax, 64)
  160. } else if israte == "1" {
  161. //免税
  162. yhzcbs = "1"
  163. tax = "0.00"
  164. a,_ = strconv.ParseFloat(tax, 64)
  165. zzstsgl = "免税"
  166. rate = "0.00"
  167. } else if israte == "2" {
  168. //不征税
  169. yhzcbs = "1"
  170. zzstsgl = "不征税"
  171. tax = "0.00"
  172. a,_ = strconv.ParseFloat(tax, 64)
  173. rate = "0.00"
  174. } else if israte == "3" {
  175. //普通0税率
  176. zzstsgl = ""
  177. tax = "0.00"
  178. a,_ = strconv.ParseFloat(tax, 64)
  179. rate = "0.00"
  180. }
  181. //typeString := ""
  182. //if pay.OilPaymentInfo.PayType == "1" {
  183. // typeString = "准入交费"
  184. //} else if pay.OilPaymentInfo.PayType == "2" {
  185. // typeString = "年审交费"
  186. //} else if pay.OilPaymentInfo.PayType == "3" {
  187. // typeString = "增项交费"
  188. //} else if pay.OilPaymentInfo.PayType == "7" {
  189. // typeString = "信息变更交费"
  190. //}
  191. //paramsString += "{'goodsname':'" + pay.SupplierTypeName + " " + typeString + "','num':'','price':'','hsbz':'1','taxrate':'" + rate + "','spec':'','unit':'','spbm':'10101150101','zsbm':'','fphxz':'0','yhzcbs':'" + yhzcbs + "','zzstsgl':'" + zzstsgl + "','lslbs':'" + israte + "','kce':'','taxfreeamt':" + fmt.Sprintf("%.2f", amount-a) + ",'tax':" + tax + ",'taxamt':" + strconv.Itoa(int(amount)) + "}"
  192. //detail.Goodsname = pay.SupplierTypeName + " " + typeString
  193. detail.Goodsname = "资质认证服务费"
  194. detail.Hsbz = "1"
  195. detail.Taxrate = rate
  196. detail.Fphxz = "0"
  197. detail.Yhzcbs = yhzcbs
  198. detail.Zzstsgl = zzstsgl
  199. detail.Lslbs = israte
  200. detail.Tax = tax
  201. detail.Spbm = "3040101"
  202. detail.Taxamt = strconv.Itoa(int(amount))
  203. detail.Taxfreeamt = fmt.Sprintf("%.2f", amount-a)
  204. paramsDetail = append(paramsDetail, detail)
  205. }
  206. // 身份认证,在诺诺网备案后,由诺诺网提供,每个企业一个
  207. identity := paramSvc.GetBaseparameterMessage("INVOICE", "paramset", "InvoiceIdentity")
  208. var params InvoiceJson
  209. var paramsOrder InvoiceOrderJson
  210. params.Identity = identity
  211. paramsOrder.Detail = paramsDetail
  212. paramsOrder.Buyername = entity.SupplierName
  213. paramsOrder.Taxnum = entity.CommercialNo
  214. paramsOrder.Phone = supplierEntity.Mobile
  215. paramsOrder.Address = entity.Address
  216. paramsOrder.Account = entity.DepositBank + " " + entity.BankAccount
  217. paramsOrder.Telephone = supplierEntity.Telphone
  218. paramsOrder.Orderno = "dg_" + strconv.Itoa(entity.SupplierId) + strconv.Itoa(entity.Id)
  219. paramsOrder.Invoicedate = time.Now().Format("2006-01-02 15:04:05")
  220. paramsOrder.Clerk = paramSvc.GetBaseparameterMessage("INVOICE", "paramset", "InvoicePeople3")
  221. paramsOrder.Saleaccount = paramSvc.GetBaseparameterMessage("INVOICE", "paramset", "InvoiceSaleaccount")
  222. paramsOrder.Salephone = paramSvc.GetBaseparameterMessage("INVOICE", "paramset", "InvoicePhone")
  223. paramsOrder.Saleaddress = paramSvc.GetBaseparameterMessage("INVOICE", "paramset", "InvoiceAddress")
  224. paramsOrder.Saletaxnum = paramSvc.GetBaseparameterMessage("INVOICE", "paramset", "InvoiceSaletaxnum")
  225. paramsOrder.Kptype = "1"
  226. paramsOrder.Payee = paramSvc.GetBaseparameterMessage("INVOICE", "paramset", "InvoicePeople1")
  227. paramsOrder.Checker = paramSvc.GetBaseparameterMessage("INVOICE", "paramset", "InvoicePeople2")
  228. paramsOrder.Email = entity.Email
  229. if entity.Email != "" {
  230. paramsOrder.Tsfs = "2" //-1,不推送;0,邮箱;1,手机(默认);2,邮箱、手机
  231. } else {
  232. var corporateInfo register.OilCorporateInfo
  233. sup.GetEntity(&corporateInfo, "CheckStatus = 11 and CommercialNo = '" + entity.CommercialNo + "'")
  234. if corporateInfo.EMail != "" {
  235. paramsOrder.Email = corporateInfo.EMail
  236. paramsOrder.Tsfs = "2" //-1,不推送;0,邮箱;1,手机(默认);2,邮箱、手机
  237. } else {
  238. paramsOrder.Tsfs = "1" //-1,不推送;0,邮箱;1,手机(默认);2,邮箱、手机
  239. }
  240. }
  241. paramsOrder.Qdbz = "0"
  242. paramsOrder.Dkbz = "0"
  243. paramsOrder.InvoiceLine = "p"
  244. params.Order = paramsOrder
  245. var errinfo ErrorInfo
  246. paramsStr, err := json.Marshal(params)
  247. if err != nil {
  248. errinfo.Code = -1
  249. errinfo.Message = "转json失败!"
  250. this.Data["json"] = &errinfo
  251. this.ServeJSON()
  252. }
  253. //param1 := "{'identity':'" + identity + "','order':{'buyername':'" + entity.SupplierName + "','taxnum':'" + entity.CommercialNo + "','phone':'" + supplierEntity.Mobile +
  254. // "','address':'" + entity.Address + "','account':'" + entity.DepositBank + " " + entity.BankAccount + "','telephone':'" + supplierEntity.Telphone + "','orderno':'dg123" +
  255. // entity.BankSerialNum + "','invoicedate':'" + time.Now().Format("2006-01-02 15:04:05") + "','clerk':'" + paramSvc.GetBaseparameterMessage("INVOICE", "paramset", "InvoicePeople3") +
  256. // "','saleaccount':'" + paramSvc.GetBaseparameterMessage("INVOICE", "paramset", "InvoiceSaleaccount") + "','salephone':'" + paramSvc.GetBaseparameterMessage("INVOICE", "paramset", "InvoicePhone") +
  257. // "','saleaddress':'" + paramSvc.GetBaseparameterMessage("INVOICE", "paramset", "InvoiceAddress") + "','saletaxnum':'" + paramSvc.GetBaseparameterMessage("INVOICE", "paramset", "InvoiceSaletaxnum") +
  258. // "','kptype':'1','message':'','payee':'" + paramSvc.GetBaseparameterMessage("INVOICE", "paramset", "InvoicePeople1") + "','checker':'" + paramSvc.GetBaseparameterMessage("INVOICE", "paramset", "InvoicePeople2") +
  259. // "','tsfs':'1','email':'" + entity.Email + "','qdbz':'0','qdxmmc':'','dkbz':'0','deptid':'','clerkid':'','invoiceLine':'p','cpybz':'','detail':["+paramsString+"]}}"
  260. fmt.Println(string(paramsStr), "参数")
  261. invoiceJson := invoice.Post(url, string(paramsStr), key)
  262. fmt.Println(invoiceJson, "诺诺接口返回")
  263. if invoiceJson.Status != "0000"{
  264. //session.Rollback()
  265. errinfo.Code = -1
  266. errinfo.Message = "开票失败!" + invoiceJson.Message
  267. this.Data["json"] = &errinfo
  268. this.ServeJSON()
  269. } else if invoiceJson.Status == "0000" && invoiceJson.Fpqqlsh != "" {
  270. cols := []string{"IsInvoice", "InvoiceOn", "NuNuSerialNum"}
  271. svc := invoiceinfo.GetOilInvoiceInfoSession(session)
  272. var info invoiceinfo.OilInvoiceInfo
  273. info.IsInvoice = "1"
  274. info.InvoiceOn = time.Now()
  275. info.NuNuSerialNum = invoiceJson.Fpqqlsh
  276. _, err = svc.UpdateEntityByIdCols(invoiceId.Id, &info, cols)
  277. var payinfo paymentinfo.OilPaymentInfo
  278. payinfo.IsInvoice = "1"
  279. paycols := []string{"IsInvoice"}
  280. where := "Id in (" + invoiceId.SrcIds + ")"
  281. err = svc.UpdateEntityBywheretbl(OilPaymentInfoName, &payinfo, paycols, where)
  282. svcActiviti := workflow.GetActivitiService(utils.DBE)
  283. var ActiComplete workflow.ActiCompleteVM
  284. ActiComplete.ProcessKey = workflow.OIL_SUPPLIER_VERIFY
  285. ActiComplete.BusinessKey = invoiceId.Id
  286. ActiComplete.UserId = this.User.Id
  287. ActiComplete.Remarks = ""
  288. ActiComplete.Result = "1"
  289. receiveVal := svcActiviti.TaskComplete(ActiComplete)
  290. fmt.Println("receiveVal:" + receiveVal)
  291. if receiveVal == "true" && err == nil {
  292. session.Commit()
  293. } else {
  294. session.Rollback()
  295. }
  296. }
  297. errinfo.Code = 0
  298. errinfo.Message = "开票成功!"
  299. this.Data["json"] = &errinfo
  300. this.ServeJSON()
  301. }
  302. // @Title 获取开票状态
  303. // @Description 获取开票状态
  304. // @Success 200 {object} controllers.Request
  305. // @router /selectInvoice [get]
  306. func (this *OilInvoiceController) SelectInvoice(Fpqqlsh string, Id int) invoiceinfo.InvoiceNumberJson {
  307. paramSvc := baseparameter.GetBaseparameterService(utils.DBE)
  308. var sel SelectInvoiceJson
  309. key := paramSvc.GetBaseparameterMessage("INVOICE", "paramset", "InvoiceKey64")
  310. sel.Identity = paramSvc.GetBaseparameterMessage("INVOICE", "paramset", "InvoiceIdentity")
  311. sel.Fpqqlsh = Fpqqlsh
  312. selS, _ := json.Marshal(sel)
  313. invoice := invoiceinfo.GetInvoiceService(utils.DBE)
  314. url1 := paramSvc.GetBaseparameterMessage("INVOICE", "paramset", "InvoiceLiuShuiUrl")
  315. //params := "{'identity':'" + identity + "','fpqqlsh':['" + invoiceJson.Fpqqlsh + "']}"
  316. invoiceJson1 := invoice.SerialNumberPost(url1, string(selS), key)
  317. // 根据流水号查询结果
  318. //params := "{'identity':'" + identity + "','fpqqlsh':['" + invoiceJson.Fpqqlsh + "']}"
  319. //invoiceJson1 := invoice.SerialNumberPost(url, params, key)
  320. invoiceStr, _ := json.Marshal(invoiceJson1)
  321. fmt.Print("获取开票状态" + string(invoiceStr))
  322. if invoiceJson1.Result == "success" && len(invoiceJson1.InvoiceList) > 0 {
  323. results := invoiceJson1.InvoiceList[0]
  324. if results.Status == "2" {
  325. cols := []string{"Url"}
  326. var info invoiceinfo.OilInvoiceInfo
  327. info.Url = results.Url
  328. _, err := invoice.UpdateEntityByIdCols(Id, &info, cols)
  329. if err != nil {
  330. fmt.Print("修改url失败")
  331. }
  332. }
  333. }
  334. return invoiceJson1
  335. }
  336. // @Title 获取已开发票没有url的
  337. // @Description 获取已开发票没有url的
  338. // @Success 200 {object} controllers.Request
  339. // @router /selectInvoiceStatus [get]
  340. func (this *OilInvoiceController) SelectInvoiceStatus() {
  341. var invoiceList []invoiceinfo.OilInvoiceInfo
  342. svc := invoiceinfo.GetInvoiceService(utils.DBE)
  343. svc.GetEntitysByWhere("OilInvoiceInfo","NuNuSerialNum != '' and (Url = '' or Url is null)", &invoiceList)
  344. for _, invoice := range invoiceList {
  345. this.SelectInvoice(invoice.NuNuSerialNum, invoice.Id)
  346. }
  347. }
  348. // @Title 走工作流
  349. // @Description 走工作流
  350. // @Success 200 {object} controllers.Request
  351. // @router /workflowInvoice/:id [get]
  352. func (this *OilInvoiceController) WorkflowInvoice() {
  353. Id := this.Ctx.Input.Param(":id")
  354. svcActiviti := workflow.GetActivitiService(utils.DBE)
  355. var ActiComplete workflow.ActiCompleteVM
  356. ActiComplete.ProcessKey = workflow.OIL_SUPPLIER_VERIFY
  357. ActiComplete.BusinessKey = Id
  358. ActiComplete.UserId = this.User.Id
  359. ActiComplete.Remarks = ""
  360. ActiComplete.Result = "1"
  361. receiveVal := svcActiviti.TaskComplete(ActiComplete)
  362. fmt.Println("receiveVal:" + receiveVal)
  363. }