sales.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. package controllers
  2. import (
  3. "Medical_ERP/common/global"
  4. _ "Medical_ERP/common/response"
  5. "Medical_ERP/dto"
  6. "Medical_ERP/models"
  7. "Medical_ERP/services"
  8. "Medical_ERP/utils"
  9. "errors"
  10. "fmt"
  11. "github.com/signintech/gopdf"
  12. "github.com/xuri/excelize/v2"
  13. "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth/beegouser"
  14. "os"
  15. "time"
  16. )
  17. type SalesController struct {
  18. BaseController
  19. }
  20. // SalesList 销售管理
  21. // @Summary 销售管理
  22. // @Description 销售管理
  23. // @Tags 销售管理
  24. // @Param body body dto.SalesPageReq true "body"
  25. // @Success 200 {object} response.Page "{"code": 200, "data": [...]}"
  26. // @Router /sales/list [post]
  27. // @Security Bearer
  28. func (c SalesController) SalesList() {
  29. s := services.Sales{}
  30. reqData := dto.SalesPageReq{}
  31. if err := c.ParseAndValidate(&c.Ctx.Input.RequestBody, &reqData); err != nil {
  32. err = errors.New("解析表单数据异常")
  33. c.Error(global.ParseFormErr, err, err.Error())
  34. return
  35. }
  36. deptId := beegouser.GetDeptId(c.Ctx)
  37. list, count, err := s.SalesList(&reqData, deptId)
  38. if err != nil {
  39. c.Error(500, err, err.Error())
  40. return
  41. }
  42. c.PageOK(list, int(count), reqData.GetPageIndex(), reqData.GetPageSize(), "查询成功")
  43. }
  44. // SalesExcel 二类销售清单
  45. // @Summary 二类销售清单
  46. // @Description 二类销售清单
  47. // @Tags 销售管理
  48. // @Param body body dto.SalesPageReq true "body"
  49. // @Success 200 {object} response.Page "{"code": 200, "data": [...]}"
  50. // @Router /sales/excel [post]
  51. // @Security Bearer
  52. func (c SalesController) SalesExcel() {
  53. s := services.Sales{}
  54. reqData := dto.SalesPageReq{}
  55. if err := c.ParseAndValidate(&c.Ctx.Input.RequestBody, &reqData); err != nil {
  56. err = errors.New("解析表单数据异常")
  57. c.Error(global.ParseFormErr, err, err.Error())
  58. return
  59. }
  60. deptId := beegouser.GetDeptId(c.Ctx)
  61. deptName := beegouser.GetDeptName(c.Ctx)
  62. list, _, err := s.SalesListExcel(&reqData, deptId)
  63. if err != nil {
  64. c.Error(500, err, err.Error())
  65. return
  66. }
  67. f := excelize.NewFile() // 设置单元格的值
  68. StyleTitle, _ := f.NewStyle(
  69. &excelize.Style{
  70. Font: &excelize.Font{Bold: true, Size: 20, Family: "宋体"},
  71. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  72. })
  73. f.MergeCell("Sheet1", "A1", "N1")
  74. f.SetRowStyle("Sheet1", 1, 1, StyleTitle)
  75. f.SetRowHeight("Sheet1", 1, 50)
  76. var interval string
  77. if len(reqData.StartDate) > 0 && len(reqData.EndDate) > 0 {
  78. interval = fmt.Sprintf("%s至%s", reqData.StartDate, reqData.EndDate)
  79. }
  80. f.SetCellValue("Sheet1", "A1", fmt.Sprintf("%s%s销售清单", deptName, interval))
  81. // 这里设置表头
  82. f.SetCellValue("Sheet1", "A2", "日期")
  83. f.SetCellValue("Sheet1", "B2", "购货单位")
  84. f.SetCellValue("Sheet1", "C2", "品种")
  85. f.SetCellValue("Sheet1", "D2", "生产企业")
  86. f.SetCellValue("Sheet1", "E2", "规格")
  87. f.SetCellValue("Sheet1", "F2", "剂型")
  88. f.SetCellValue("Sheet1", "G2", "批号")
  89. f.SetCellValue("Sheet1", "H2", "数量")
  90. f.SetCellValue("Sheet1", "I2", "单位")
  91. f.SetCellValue("Sheet1", "J2", "单价")
  92. f.SetCellValue("Sheet1", "K2", "金额")
  93. //f.SetCellValue("Sheet1", "L2", "销售单价")
  94. //f.SetCellValue("Sheet1", "M2", "销售金额")
  95. f.SetCellValue("Sheet1", "L2", "批准文号")
  96. // 设置列宽
  97. f.SetColWidth("Sheet1", "A", "A", 12)
  98. f.SetColWidth("Sheet1", "B", "C", 15)
  99. f.SetColWidth("Sheet1", "C", "E", 23)
  100. f.SetColWidth("Sheet1", "E", "F", 15)
  101. f.SetColWidth("Sheet1", "F", "G", 12)
  102. f.SetColWidth("Sheet1", "H", "I", 10)
  103. f.SetColWidth("Sheet1", "J", "L", 12)
  104. line := 2
  105. // 循环写入数据
  106. for _, v := range list {
  107. line++
  108. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), v["date"])
  109. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v["receiving_unit"])
  110. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v[models.FieldProductName])
  111. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v[models.FieldEnterpriseName])
  112. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v[models.FieldSpecName])
  113. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v[models.FieldDosageFormName])
  114. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v[models.FieldBatchNumber])
  115. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), v["quantity"])
  116. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), v[models.FieldUnitName])
  117. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), utils.ToFloat64(v["unit_price"]))
  118. f.SetCellValue("Sheet1", fmt.Sprintf("K%d", line), utils.ToFloat64(v["money"]))
  119. f.SetCellValue("Sheet1", fmt.Sprintf("L%d", line), v[models.FieldApprovalNumber])
  120. }
  121. Style1, _ := f.NewStyle(
  122. &excelize.Style{
  123. Font: &excelize.Font{Size: 12, Family: "宋体", Bold: true},
  124. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  125. Border: []excelize.Border{
  126. {Type: "left", Color: "000000", Style: 1},
  127. {Type: "top", Color: "000000", Style: 1},
  128. {Type: "bottom", Color: "000000", Style: 1},
  129. {Type: "right", Color: "000000", Style: 1},
  130. },
  131. })
  132. f.SetCellStyle("Sheet1", "A2", "N2", Style1)
  133. Style2, _ := f.NewStyle(
  134. &excelize.Style{
  135. Font: &excelize.Font{Size: 12, Family: "宋体"},
  136. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  137. Border: []excelize.Border{
  138. {Type: "left", Color: "000000", Style: 1},
  139. {Type: "top", Color: "000000", Style: 1},
  140. {Type: "bottom", Color: "000000", Style: 1},
  141. {Type: "right", Color: "000000", Style: 1},
  142. },
  143. })
  144. f.SetCellStyle("Sheet1", "A3", fmt.Sprintf("N%d", line), Style2)
  145. filename := "二类销售清单" + time.Now().Format("20060102150405") + ".xlsx"
  146. // 保存文件
  147. if err := f.SaveAs("ofile/" + filename); err != nil {
  148. fmt.Println(err)
  149. }
  150. defer func() {
  151. os.Remove("ofile/" + filename)
  152. }()
  153. // 返回生成的 Excel 文件
  154. c.Ctx.Output.Download("ofile/" + filename)
  155. }
  156. // SalesReportExcel 销售报表
  157. // @Summary 销售报表
  158. // @Description 销售报表
  159. // @Tags 销售管理
  160. // @Param body body dto.SalesPageReq true "body"
  161. // @Success 200 {object} response.Page "{"code": 200, "data": [...]}"
  162. // @Router /sales/excel [post]
  163. // @Security Bearer
  164. func (c SalesController) SalesReportExcel() {
  165. s := services.Sales{}
  166. reqData := dto.SalesPageReq{}
  167. if err := c.ParseAndValidate(&c.Ctx.Input.RequestBody, &reqData); err != nil {
  168. err = errors.New("解析表单数据异常")
  169. c.Error(global.ParseFormErr, err, err.Error())
  170. return
  171. }
  172. deptId := beegouser.GetDeptId(c.Ctx)
  173. deptName := beegouser.GetDeptName(c.Ctx)
  174. list, err := s.SalesReportList(&reqData, deptId)
  175. if err != nil {
  176. c.Error(500, err, err.Error())
  177. return
  178. }
  179. f := excelize.NewFile() // 设置单元格的值
  180. StyleTitle, _ := f.NewStyle(
  181. &excelize.Style{
  182. Font: &excelize.Font{Bold: true, Size: 20, Family: "宋体"},
  183. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  184. })
  185. f.MergeCell("Sheet1", "A1", "G1")
  186. f.SetRowStyle("Sheet1", 1, 1, StyleTitle)
  187. f.SetRowHeight("Sheet1", 1, 50)
  188. var interval string
  189. if len(reqData.StartDate) > 0 && len(reqData.EndDate) > 0 {
  190. interval = fmt.Sprintf("%s至%s", reqData.StartDate, reqData.EndDate)
  191. }
  192. f.SetCellValue("Sheet1", "A1", fmt.Sprintf("%s%s销售报表", deptName, interval))
  193. // 这里设置表头
  194. f.SetRowHeight("Sheet1", 2, 25)
  195. f.SetCellValue("Sheet1", "A2", "品种")
  196. f.SetCellValue("Sheet1", "B2", "生产企业")
  197. f.SetCellValue("Sheet1", "C2", "规格")
  198. f.SetCellValue("Sheet1", "D2", "数量")
  199. f.SetCellValue("Sheet1", "E2", "单位")
  200. f.SetCellValue("Sheet1", "F2", "单价")
  201. f.SetCellValue("Sheet1", "G2", "金额")
  202. //f.SetCellValue("Sheet1", "H2", "销售单价")
  203. //f.SetCellValue("Sheet1", "I2", "销售金额")
  204. //f.SetCellValue("Sheet1", "J2", "利润金额")
  205. // 设置列宽
  206. f.SetColWidth("Sheet1", "A", "C", 27)
  207. f.SetColWidth("Sheet1", "D", "E", 10)
  208. f.SetColWidth("Sheet1", "F", "G", 12)
  209. line := 2
  210. // 循环写入数据
  211. for _, v := range list {
  212. line++
  213. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), v[models.FieldProductName])
  214. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v[models.FieldEnterpriseName])
  215. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v[models.FieldSpecName])
  216. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), utils.ToInt(v["total"]))
  217. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v[models.FieldUnitName])
  218. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), utils.ToFloat64(v["unit_price"]))
  219. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), utils.ToFloat64(v["money"]))
  220. //f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), utils.ToFloat64(v["sales_unit_price"]))
  221. //f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), utils.ToFloat64(v["sales_money"]))
  222. //f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), utils.ToFloat64(v["profit_money"]))
  223. }
  224. line += 1
  225. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), "合计")
  226. f.SetCellFormula("Sheet1", fmt.Sprintf("D%d", line), fmt.Sprintf("SUM(D3:D%d)", line-1))
  227. f.SetCellFormula("Sheet1", fmt.Sprintf("G%d", line), fmt.Sprintf("SUM(G3:G%d)", line-1))
  228. //f.SetCellFormula("Sheet1", fmt.Sprintf("I%d", line), fmt.Sprintf("SUM(I3:I%d)", line-1))
  229. //f.SetCellFormula("Sheet1", fmt.Sprintf("J%d", line), fmt.Sprintf("SUM(J3:J%d)", line-1))
  230. Style1, _ := f.NewStyle(
  231. &excelize.Style{
  232. Font: &excelize.Font{Size: 14, Family: "宋体", Bold: true},
  233. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  234. Border: []excelize.Border{
  235. {Type: "left", Color: "000000", Style: 1},
  236. {Type: "top", Color: "000000", Style: 1},
  237. {Type: "bottom", Color: "000000", Style: 1},
  238. {Type: "right", Color: "000000", Style: 1},
  239. },
  240. })
  241. f.SetCellStyle("Sheet1", "A2", "G2", Style1)
  242. Style2, _ := f.NewStyle(
  243. &excelize.Style{
  244. Font: &excelize.Font{Size: 12, Family: "宋体"},
  245. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  246. Border: []excelize.Border{
  247. {Type: "left", Color: "000000", Style: 1},
  248. {Type: "top", Color: "000000", Style: 1},
  249. {Type: "bottom", Color: "000000", Style: 1},
  250. {Type: "right", Color: "000000", Style: 1},
  251. },
  252. })
  253. f.SetCellStyle("Sheet1", "A3", fmt.Sprintf("G%d", line), Style2)
  254. filename := "销售报表" + time.Now().Format("20060102150405") + ".xlsx"
  255. // 保存文件
  256. if err := f.SaveAs("ofile/" + filename); err != nil {
  257. fmt.Println(err)
  258. }
  259. defer func() {
  260. os.Remove("ofile/" + filename)
  261. }()
  262. // 返回生成的 Excel 文件
  263. c.Ctx.Output.Download("ofile/" + filename)
  264. }
  265. // SalesOrderList 销售订单
  266. // @Summary 销售订单
  267. // @Description 销售订单
  268. // @Tags 销售管理
  269. // @Param body body dto.SalesOrderPageReq true "body"
  270. // @Success 200 {object} response.Page "{"code": 200, "data": [...]}"
  271. // @Router /sales/order_list [post]
  272. // @Security Bearer
  273. func (c SalesController) SalesOrderList() {
  274. s := services.Sales{}
  275. reqData := dto.SalesOrderPageReq{}
  276. if err := c.ParseAndValidate(&c.Ctx.Input.RequestBody, &reqData); err != nil {
  277. err = errors.New("解析表单数据异常")
  278. c.Error(global.ParseFormErr, err, err.Error())
  279. return
  280. }
  281. deptId := beegouser.GetDeptId(c.Ctx)
  282. list, count, err := s.SalesOrderList(&reqData, deptId)
  283. if err != nil {
  284. c.Error(500, err, err.Error())
  285. return
  286. }
  287. c.PageOK(list, int(count), reqData.GetPageIndex(), reqData.GetPageSize(), "查询成功")
  288. }
  289. func (c SalesController) SalesStockOutExcel(reqData dto.SalesStockOutExcelReq) {
  290. s := services.Sales{}
  291. deptId := beegouser.GetDeptId(c.Ctx)
  292. list, err := s.SalesStockOutExcel(&reqData, deptId)
  293. if err != nil {
  294. c.Error(500, err, err.Error())
  295. return
  296. }
  297. f := excelize.NewFile() // 设置单元格的值
  298. StyleTitle, _ := f.NewStyle(
  299. &excelize.Style{
  300. Font: &excelize.Font{Bold: true, Size: 20, Family: "宋体"},
  301. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  302. })
  303. f.MergeCell("Sheet1", "A1", "K1")
  304. f.SetRowStyle("Sheet1", 1, 1, StyleTitle)
  305. f.SetRowHeight("Sheet1", 1, 40)
  306. f.SetCellValue("Sheet1", "A1", fmt.Sprintf("%s二类出库单", beegouser.GetDeptName(c.Ctx)))
  307. // 这里设置表头
  308. f.SetRowHeight("Sheet1", 2, 25)
  309. f.MergeCell("Sheet1", "A2", "C2")
  310. f.MergeCell("Sheet1", "D2", "F2")
  311. f.MergeCell("Sheet1", "G2", "G2")
  312. f.MergeCell("Sheet1", "H2", "K2")
  313. f.SetRowHeight("Sheet1", 3, 40)
  314. f.SetCellValue("Sheet1", "A2", "购货单位:"+reqData.ReceivingUnit)
  315. f.SetCellValue("Sheet1", "D2", "下单日期:"+reqData.Date)
  316. //f.SetCellValue("Sheet1", "G2", "编号:")
  317. //f.SetCellValue("Sheet1", "I2", "第1联,共3联(第1联,库房处留存)")
  318. f.SetCellValue("Sheet1", "A3", "品种")
  319. f.SetCellValue("Sheet1", "B3", "剂型")
  320. f.SetCellValue("Sheet1", "C3", "规格")
  321. f.SetCellValue("Sheet1", "D3", "生产企业")
  322. f.SetCellValue("Sheet1", "E3", "批号")
  323. f.SetCellValue("Sheet1", "F3", "失效日期")
  324. f.SetCellValue("Sheet1", "G3", "批准文号")
  325. f.SetCellValue("Sheet1", "H3", "数量")
  326. f.SetCellValue("Sheet1", "I3", "单位")
  327. f.SetCellValue("Sheet1", "J3", "销售单价")
  328. f.SetCellValue("Sheet1", "K3", "销售金额")
  329. // 设置列宽
  330. f.SetColWidth("Sheet1", "A", "B", 15)
  331. f.SetColWidth("Sheet1", "B", "B", 6)
  332. f.SetColWidth("Sheet1", "C", "C", 15)
  333. f.SetColWidth("Sheet1", "D", "D", 10)
  334. f.SetColWidth("Sheet1", "E", "G", 12)
  335. f.SetColWidth("Sheet1", "H", "I", 6)
  336. f.SetColWidth("Sheet1", "J", "K", 10)
  337. line := 3
  338. var money float64
  339. // 循环写入数据
  340. for _, v := range list {
  341. line++
  342. //f.SetRowHeight("Sheet1", line, 25)
  343. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), v[models.FieldProductName])
  344. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v[models.FieldDosageFormName])
  345. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v[models.FieldSpecName])
  346. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v[models.FieldEnterpriseName])
  347. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v[models.FieldBatchNumber])
  348. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v[models.FieldExpiryDate])
  349. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v[models.FieldApprovalNumber])
  350. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), utils.ToInt(v["quantity"]))
  351. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), v[models.FieldUnitName])
  352. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), utils.ToFloat64(v["sales_unit_price"]))
  353. f.SetCellValue("Sheet1", fmt.Sprintf("K%d", line), utils.ToFloat64(v["sales_money"]))
  354. money += utils.ToFloat64(v["sales_money"])
  355. }
  356. line += 1
  357. f.SetRowHeight("Sheet1", line, 25)
  358. line += 1
  359. f.SetRowHeight("Sheet1", line, 25)
  360. f.MergeCell("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("D%d", line))
  361. f.MergeCell("Sheet1", fmt.Sprintf("E%d", line), fmt.Sprintf("K%d", line))
  362. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("整单合计金额(小写):¥%.2f元", money))
  363. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), fmt.Sprintf("整单合计金额(发货人签名:):%s", utils.AmountConvert(money, true)))
  364. line += 1
  365. f.SetRowHeight("Sheet1", line, 25)
  366. f.MergeCell("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("C%d", line))
  367. f.MergeCell("Sheet1", fmt.Sprintf("D%d", line), fmt.Sprintf("E%d", line))
  368. f.MergeCell("Sheet1", fmt.Sprintf("F%d", line), fmt.Sprintf("I%d", line))
  369. f.MergeCell("Sheet1", fmt.Sprintf("J%d", line), fmt.Sprintf("K%d", line))
  370. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("开票日期:%s", time.Now().Format("2006年01月02日")))
  371. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), fmt.Sprintf("开票员:%s", reqData.Drawer))
  372. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), fmt.Sprintf("发货人:%s 财务:%s", reqData.Consigner, reqData.Finance))
  373. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), fmt.Sprintf("收货人:%s", reqData.Consignee))
  374. Style0, _ := f.NewStyle(
  375. &excelize.Style{
  376. Font: &excelize.Font{Size: 10, Family: "宋体", Bold: true},
  377. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  378. })
  379. f.SetCellStyle("Sheet1", "A2", "K2", Style0)
  380. Style1, _ := f.NewStyle(
  381. &excelize.Style{
  382. Font: &excelize.Font{Size: 12, Family: "宋体", Bold: true},
  383. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  384. Border: []excelize.Border{
  385. {Type: "left", Color: "000000", Style: 1},
  386. {Type: "top", Color: "000000", Style: 1},
  387. {Type: "bottom", Color: "000000", Style: 1},
  388. {Type: "right", Color: "000000", Style: 1},
  389. },
  390. })
  391. f.SetCellStyle("Sheet1", "A3", "K3", Style1)
  392. Style2, _ := f.NewStyle(
  393. &excelize.Style{
  394. Font: &excelize.Font{Size: 11, Family: "宋体"},
  395. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  396. Border: []excelize.Border{
  397. {Type: "left", Color: "000000", Style: 1},
  398. {Type: "top", Color: "000000", Style: 1},
  399. {Type: "bottom", Color: "000000", Style: 1},
  400. {Type: "right", Color: "000000", Style: 1},
  401. },
  402. })
  403. f.SetCellStyle("Sheet1", "A4", fmt.Sprintf("k%d", line-1), Style2)
  404. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("k%d", line), Style0)
  405. filename := "二类出库单" + time.Now().Format("20060102150405") + ".xlsx"
  406. // 保存文件
  407. if err := f.SaveAs("ofile/" + filename); err != nil {
  408. fmt.Println(err)
  409. }
  410. defer func() {
  411. os.Remove("ofile/" + filename)
  412. }()
  413. // 返回生成的 Excel 文件
  414. c.Ctx.Output.Download("ofile/" + filename)
  415. }
  416. func (c SalesController) SalesStockOutPdf(reqData dto.SalesStockOutExcelReq) {
  417. s := services.Sales{}
  418. deptId := beegouser.GetDeptId(c.Ctx)
  419. list, err := s.SalesStockOutExcel(&reqData, deptId)
  420. if err != nil {
  421. c.Error(500, err, err.Error())
  422. return
  423. }
  424. cols := []float64{65, 29.5, 65, 53, 59, 59, 59, 29.5, 29.5, 53, 53}
  425. header := []string{"品种", "剂型", "规格", "生产企业", "批号", "失效日期", "批准文号", "数量", "单位", "销售单价", "销售金额"}
  426. rows := [][]string{}
  427. money := 0.0
  428. for _, row := range list {
  429. temp := []string{}
  430. temp = append(temp, fmt.Sprintf("%s", row[models.FieldProductName]))
  431. temp = append(temp, fmt.Sprintf("%s", row[models.FieldDosageFormName]))
  432. temp = append(temp, fmt.Sprintf("%s", row[models.FieldSpecName]))
  433. temp = append(temp, fmt.Sprintf("%s", row[models.FieldEnterpriseName]))
  434. temp = append(temp, fmt.Sprintf("%s", row[models.FieldBatchNumber]))
  435. temp = append(temp, fmt.Sprintf("%s", utils.ToDate(row[models.FieldExpiryDate])))
  436. if row[models.FieldApprovalNumber] == nil {
  437. temp = append(temp, "")
  438. } else {
  439. temp = append(temp, fmt.Sprintf("%s", row[models.FieldApprovalNumber]))
  440. }
  441. temp = append(temp, fmt.Sprintf("%d", utils.ToInt(row["quantity"])))
  442. temp = append(temp, fmt.Sprintf("%s", row[models.FieldUnitName]))
  443. temp = append(temp, fmt.Sprintf("%.2f", utils.ToFloat64(row["sales_unit_price"])))
  444. temp = append(temp, fmt.Sprintf("%.2f", utils.ToFloat64(row["sales_money"])))
  445. rows = append(rows, temp)
  446. money += utils.ToFloat64(row["sales_money"])
  447. }
  448. rows = append(rows, []string{"", "", "", "", "", "", "", "", "", "", ""})
  449. pdf := &gopdf.GoPdf{}
  450. pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}})
  451. pdf.SetMarginTop(20)
  452. pdf.SetMarginBottom(40)
  453. pdf.AddPage()
  454. pdf.AddTTFFont("simsun", "static/fonts/MiSans-Medium.ttf")
  455. pdf.SetFont("simsun", "", 22)
  456. titleStr := fmt.Sprintf("%s二类出库单", beegouser.GetDeptName(c.Ctx))
  457. titleWd, _ := pdf.MeasureTextWidth(titleStr)
  458. pdf.SetX((595 / 2) - (titleWd / 2))
  459. pdf.SetY(40)
  460. pdf.Text(titleStr)
  461. curx, y := 20., 70.
  462. pdf.SetXY(curx, y)
  463. pdf.SetFont("simsun", "", 11)
  464. pdf.Text(fmt.Sprintf("购货单位:%s", reqData.ReceivingUnit))
  465. pdf.SetXY(curx+cols[0]+cols[1]+cols[2], y)
  466. pdf.Text(fmt.Sprintf("下单日期:%s", reqData.Date))
  467. //pdf.Text(titleX*2+20, y, fmt.Sprintf("编号:"))
  468. //pdf.Text(titleX*3, y, "第1联,共3联(第1联,库房处留存)")
  469. h := 25.0
  470. pdf.SetFont("simsun", "", 12)
  471. x := curx
  472. y += 10
  473. pdf.SetXY(curx, y)
  474. for i := 0; i < len(header); i++ {
  475. utils.RectFillColorMultiCell(pdf, header[i], 11, x, y, cols[i], h, 0, 0, 0, gopdf.Center, gopdf.Middle)
  476. x += cols[i]
  477. }
  478. pdf.SetFont("simsun", "", 10)
  479. y += h
  480. pdf.SetXY(curx, y)
  481. h = 20.0
  482. for _, row := range rows {
  483. y = pdf.GetY()
  484. x = curx
  485. height := h
  486. for j, txt := range row {
  487. lineTexts, _ := pdf.SplitText(txt, cols[j])
  488. //lineHt := h * float64(len(lineTexts))
  489. if len(lineTexts) > 1 {
  490. lineTexts, _ = pdf.SplitText(txt, cols[j]-4)
  491. lineHt := h * float64(len(lineTexts)) * 0.8
  492. if lineHt > height {
  493. height = lineHt
  494. }
  495. }
  496. }
  497. for i, txt := range row {
  498. width := cols[i]
  499. utils.RectFillColorMultiCell(pdf, txt, 9, x, y, width, height, 0, 0, 0, gopdf.Center, gopdf.Middle)
  500. x += width
  501. }
  502. pdf.SetNewY(y+height, height)
  503. }
  504. y = pdf.GetY()
  505. pdf.SetXY(curx, y)
  506. width1 := cols[0] + cols[1] + cols[2] + cols[3]
  507. utils.RectFillColor(pdf, fmt.Sprintf("整单合计金额(小写):¥%.2f元", money), 9, curx, y, width1, h, 0, 0, 0, gopdf.Center, gopdf.Middle)
  508. width2 := cols[4] + cols[5] + cols[6] + cols[7] + cols[8] + cols[9] + cols[10]
  509. utils.RectFillColor(pdf, fmt.Sprintf("整单合计金额(大写):%s", utils.AmountConvert(money, true)), 9, curx+width1, y, width2, h, 0, 0, 0, gopdf.Center, gopdf.Middle)
  510. y += h
  511. y += 15
  512. pdf.SetNewY(y, h+15)
  513. pdf.SetX(curx)
  514. pdf.Text(fmt.Sprintf("开票日期:%s", time.Now().Format("2006年01月02日")))
  515. width := cols[0] + cols[1] + cols[2]
  516. curx += width
  517. pdf.SetX(curx)
  518. pdf.Text(fmt.Sprintf("开票员:%s", reqData.Drawer))
  519. width = cols[3] + cols[4]
  520. curx += width
  521. pdf.SetX(curx)
  522. pdf.Text(fmt.Sprintf("发货人:%s 财务:%s", reqData.Consigner, reqData.Finance))
  523. width = cols[5] + cols[6] + cols[7] + cols[8]
  524. curx += width
  525. pdf.SetX(curx)
  526. pdf.Text(fmt.Sprintf("收货人:%s", reqData.Consignee))
  527. filename := "二类出库单" + time.Now().Format("20060102150405") + ".pdf"
  528. // 保存文件
  529. if err = pdf.WritePdf("ofile/" + filename); err != nil {
  530. fmt.Println(err)
  531. }
  532. defer func() {
  533. os.Remove("ofile/" + filename)
  534. }()
  535. // 返回生成的 Excel 文件
  536. c.Ctx.Output.Download("ofile/" + filename)
  537. }
  538. // SalesStockOutExport 二类出库单
  539. // @Summary 二类出库单
  540. // @Description 二类出库单
  541. // @Tags 销售管理
  542. // @Param body body dto.SalesStockOutExcelReq true "body"
  543. // @Success 200 {object} response.Page "{"code": 200, "data": [...]}"
  544. // @Router /sales/order_list [post]
  545. // @Security Bearer
  546. func (c SalesController) SalesStockOutExport() {
  547. reqData := dto.SalesStockOutExcelReq{}
  548. if err := c.ParseAndValidate(&c.Ctx.Input.RequestBody, &reqData); err != nil {
  549. err = errors.New("解析表单数据异常")
  550. c.Error(global.ParseFormErr, err, err.Error())
  551. return
  552. }
  553. if reqData.Type == "excel" {
  554. c.SalesStockOutExcel(reqData)
  555. }
  556. if reqData.Type == "pdf" {
  557. c.SalesStockOutPdf(reqData)
  558. }
  559. }