Stock.go 101 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161
  1. package controllers
  2. import (
  3. "ERP_storage/Nats"
  4. "ERP_storage/Nats/NatsServer"
  5. "ERP_storage/conf"
  6. "ERP_storage/dto"
  7. "ERP_storage/logs"
  8. "ERP_storage/models/Account"
  9. "ERP_storage/models/Basic"
  10. "ERP_storage/models/Contract"
  11. "ERP_storage/models/Property"
  12. "ERP_storage/models/Stock"
  13. "fmt"
  14. "github.com/beego/beego/v2/adapter/orm"
  15. beego "github.com/beego/beego/v2/server/web"
  16. "github.com/robfig/cron/v3"
  17. "github.com/xuri/excelize/v2"
  18. natslibs "gogs.baozhida.cn/zoie/ERP_libs/Nats"
  19. userlibs "gogs.baozhida.cn/zoie/ERP_libs/User"
  20. "gogs.baozhida.cn/zoie/ERP_libs/lib"
  21. "math"
  22. "os"
  23. "strconv"
  24. "strings"
  25. "time"
  26. )
  27. type StockController struct {
  28. beego.Controller
  29. User userlibs.User
  30. }
  31. func (c *StockController) Prepare() {
  32. c.User = *Account.User_r
  33. }
  34. func (c *StockController) Device_List() {
  35. // 分页参数 初始化
  36. page, _ := c.GetInt("page")
  37. if page < 1 {
  38. page = 1
  39. }
  40. page_z, _ := c.GetInt("page_z")
  41. if page_z < 1 {
  42. page_z = conf.Page_size
  43. }
  44. T_state, _ := c.GetInt("T_state")
  45. T_name := c.GetString("T_name")
  46. T_product_name := c.GetString("T_product_name") // 产品名称
  47. T_product_model := c.GetString("T_product_model") // 产品型号
  48. DeviceDao := Stock.NewDevice(orm.NewOrm())
  49. R_List, R_cnt := DeviceDao.Read_Device_List(T_name, T_product_name, T_product_model, T_state, page, page_z)
  50. var r_jsons lib.R_JSONS
  51. r_jsons.Num = R_cnt
  52. r_jsons.Data = R_List
  53. r_jsons.Page = page
  54. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  55. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  56. c.ServeJSON()
  57. return
  58. }
  59. func (c *StockController) Device_Check() {
  60. T_sn := c.GetString("T_sn")
  61. T_type, _ := c.GetInt("T_type") //1-出库 2-入库
  62. T_product_id, _ := c.GetInt("T_product_id")
  63. DeviceDao := Stock.NewDevice(orm.NewOrm())
  64. device, err := DeviceDao.Read_Device_ByT_sn(T_sn)
  65. if err != nil && err.Error() != orm.ErrNoRows.Error() {
  66. c.Data["json"] = lib.JSONS{Code: 202, Msg: "请稍后重试!"}
  67. c.ServeJSON()
  68. return
  69. }
  70. // T_State 1-已出库 2-未出库/入库
  71. if len(device.T_in_number) > 0 && device.T_State == 2 && T_type == 2 {
  72. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】已入库,请勿重复提交!", T_sn)}
  73. c.ServeJSON()
  74. return
  75. }
  76. if T_type == 2 {
  77. mqtt := Stock.Read_MqttUser(T_sn)
  78. if len(mqtt.Username) == 0 {
  79. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】未授权,请先授权!", T_sn)}
  80. c.ServeJSON()
  81. return
  82. }
  83. }
  84. if len(device.T_out_number) > 0 && device.T_State == 1 && T_type == 1 {
  85. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】已出库,请勿重复提交!", T_sn)}
  86. c.ServeJSON()
  87. return
  88. }
  89. if T_type == 1 {
  90. if device.Id == 0 {
  91. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】未入库,请先入库!", T_sn)}
  92. c.ServeJSON()
  93. return
  94. }
  95. if device.T_product_id != T_product_id {
  96. c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库产品与出库产品不一致,请检查!"}
  97. c.ServeJSON()
  98. return
  99. }
  100. }
  101. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  102. c.ServeJSON()
  103. return
  104. }
  105. func (c *StockController) Stock_List() {
  106. // 分页参数 初始化
  107. page, _ := c.GetInt("page")
  108. if page < 1 {
  109. page = 1
  110. }
  111. page_z, _ := c.GetInt("page_z")
  112. if page_z < 1 {
  113. page_z = conf.Page_size
  114. }
  115. // 查询
  116. T_depot_id, _ := c.GetInt("T_depot_id")
  117. // 查询
  118. T_product_name := c.GetString("T_product_name")
  119. T_product_model := c.GetString("T_product_model")
  120. T_product_class, _ := c.GetInt("T_product_class")
  121. userList, _ := NatsServer.Read_User_List_All()
  122. Account.Read_User_All_Map(userList)
  123. Basic.Read_Depot_All_Map()
  124. StockDao := Stock.NewStock(orm.NewOrm())
  125. R_List, R_cnt := StockDao.Read_Stock_List(T_depot_id, T_product_class, T_product_name, T_product_model, page, page_z)
  126. var r_jsons lib.R_JSONS
  127. r_jsons.Num = R_cnt
  128. r_jsons.Data = R_List
  129. r_jsons.Page = page
  130. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  131. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  132. c.ServeJSON()
  133. return
  134. }
  135. // 修改库存排序
  136. func (c *StockController) Stock_Edit_Sort() {
  137. T_id, _ := c.GetInt("T_id")
  138. T_sort, _ := c.GetInt("T_sort")
  139. StockDao := Stock.NewStock(orm.NewOrm())
  140. Stock_r, err := StockDao.Read_Stock_ById(T_id)
  141. if err != nil {
  142. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_id Err!"}
  143. c.ServeJSON()
  144. return
  145. }
  146. Stock_r.T_sort = T_sort
  147. if err = StockDao.Update_Stock(Stock_r, "T_sort"); err != nil {
  148. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  149. c.ServeJSON()
  150. return
  151. }
  152. NatsServer.AddUserLogs(c.User.T_uuid, "库存", "修改", Stock_r)
  153. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  154. c.ServeJSON()
  155. return
  156. }
  157. func (c *StockController) Stock_Detail_List() {
  158. // 查询
  159. T_depot_id, _ := c.GetInt("T_depot_id")
  160. // 查询
  161. T_product_id, _ := c.GetInt("T_product_id")
  162. T_start_date := c.GetString("T_start_date")
  163. T_end_date := c.GetString("T_end_date")
  164. now := time.Now()
  165. if len(T_start_date) == 0 {
  166. T_start_date = time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local).Format("2006-01")
  167. } else {
  168. T_start_date_t, _ := lib.DateStrToTime(T_start_date)
  169. T_start_date = T_start_date_t.Format("2006-01")
  170. }
  171. if len(T_end_date) == 0 {
  172. T_end_date = now.Format("2006-01")
  173. } else {
  174. T_end_date_t, _ := lib.DateStrToTime(T_end_date)
  175. T_end_date = T_end_date_t.Format("2006-01")
  176. }
  177. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  178. StockMonthDao := Stock.NewStockMonth(orm.NewOrm())
  179. R_List := StockMonthDao.Read_StockMonth_List(T_depot_id, T_product_id, 0, T_start_date, T_end_date)
  180. for i := 0; i < len(R_List); i++ {
  181. R_List[i].T_project = strings.Join(StockOutDao.Read_StockOut_T_project(T_depot_id, R_List[i].T_product_id, R_List[i].T_month), "|")
  182. }
  183. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: R_List}
  184. c.ServeJSON()
  185. return
  186. }
  187. func (c *StockController) Stock_Detail_Excel() {
  188. // 查询
  189. T_depot_id, _ := c.GetInt("T_depot_id")
  190. T_product_id, _ := c.GetInt("T_product_id")
  191. // 查询
  192. T_start_date := c.GetString("T_start_date")
  193. T_end_date := c.GetString("T_end_date")
  194. now := time.Now()
  195. if len(T_start_date) == 0 {
  196. T_start_date = time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local).Format("2006-01")
  197. } else {
  198. T_start_date_t, _ := lib.DateStrToTime(T_start_date)
  199. T_start_date = T_start_date_t.Format("2006-01")
  200. }
  201. if len(T_end_date) == 0 {
  202. T_end_date = now.Format("2006-01")
  203. } else {
  204. T_end_date_t, _ := lib.DateStrToTime(T_end_date)
  205. T_end_date = T_end_date_t.Format("2006-01")
  206. }
  207. var class_List []Basic.ProductClass_R
  208. filename := fmt.Sprintf("进销存(%s)", lib.GetRandstring(6, "0123456789", 0))
  209. if T_product_id > 0 {
  210. product, _ := Basic.Read_Product_ById(T_product_id)
  211. filename = fmt.Sprintf("进销存-%s明细(%s)", product.T_name, lib.GetRandstring(6, "0123456789", 0))
  212. class, _ := Basic.Read_ProductClass_ById(product.T_class)
  213. class_List = append(class_List, Basic.ProductClassToProductClass_R(class))
  214. } else {
  215. class_List, _ = Basic.Read_ProductClass_List("", 0, 9999)
  216. }
  217. f := excelize.NewFile() // 设置单元格的值
  218. Style1, _ := f.NewStyle(
  219. &excelize.Style{
  220. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  221. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  222. })
  223. Style2, _ := f.NewStyle(
  224. &excelize.Style{
  225. Font: &excelize.Font{Bold: true, Size: 10, Family: "宋体"},
  226. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  227. Border: []excelize.Border{
  228. {Type: "left", Color: "000000", Style: 1},
  229. {Type: "top", Color: "000000", Style: 1},
  230. {Type: "bottom", Color: "000000", Style: 1},
  231. {Type: "right", Color: "000000", Style: 1},
  232. },
  233. })
  234. Style3, _ := f.NewStyle(
  235. &excelize.Style{
  236. Font: &excelize.Font{Bold: true, Size: 10, Family: "宋体", Color: "FF0000"},
  237. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  238. Border: []excelize.Border{
  239. {Type: "left", Color: "000000", Style: 1},
  240. {Type: "top", Color: "000000", Style: 1},
  241. {Type: "bottom", Color: "000000", Style: 1},
  242. {Type: "right", Color: "000000", Style: 1},
  243. },
  244. })
  245. Style4, _ := f.NewStyle(
  246. &excelize.Style{
  247. Font: &excelize.Font{Size: 10, Family: "宋体"},
  248. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  249. Border: []excelize.Border{
  250. {Type: "left", Color: "000000", Style: 1},
  251. {Type: "top", Color: "000000", Style: 1},
  252. {Type: "bottom", Color: "000000", Style: 1},
  253. {Type: "right", Color: "000000", Style: 1},
  254. },
  255. })
  256. StockMonthDao := Stock.NewStockMonth(orm.NewOrm())
  257. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  258. var j = 0
  259. for _, r := range class_List {
  260. StockList := StockMonthDao.Read_StockMonth_List(T_depot_id, T_product_id, r.Id, T_start_date, T_end_date)
  261. if len(StockList) == 0 {
  262. continue
  263. }
  264. if j == 0 {
  265. f.SetSheetName("Sheet1", r.T_name)
  266. } else {
  267. f.NewSheet(r.T_name)
  268. }
  269. j += 1
  270. f.MergeCell(r.T_name, "A1", "J1")
  271. f.SetRowStyle(r.T_name, 1, 1, Style1)
  272. f.SetCellValue(r.T_name, "A1", fmt.Sprintf("宝智达科技产品进销存统计表"))
  273. f.SetRowHeight(r.T_name, 1, 30)
  274. // 这里设置表头
  275. f.SetCellStyle(r.T_name, "A2", "J2", Style2)
  276. f.SetRowHeight(r.T_name, 2, 20)
  277. f.SetCellValue(r.T_name, "A2", "序号")
  278. f.SetCellValue(r.T_name, "B2", "产品名称")
  279. f.SetCellValue(r.T_name, "C2", "型号")
  280. f.SetCellValue(r.T_name, "D2", "规格")
  281. f.SetCellValue(r.T_name, "E2", "月份")
  282. f.SetCellValue(r.T_name, "F2", "期初库存")
  283. f.SetCellValue(r.T_name, "G2", "当月入库")
  284. f.SetCellValue(r.T_name, "H2", "当月出库")
  285. f.SetCellValue(r.T_name, "I2", "期末库存")
  286. f.SetCellValue(r.T_name, "J2", "出库项目")
  287. //f.SetCellValue(r.T_name, "K2", "备注")
  288. // 设置列宽
  289. f.SetColWidth(r.T_name, "A", "A", 10)
  290. f.SetColWidth(r.T_name, "B", "B", 10)
  291. f.SetColWidth(r.T_name, "C", "D", 10)
  292. f.SetColWidth(r.T_name, "D", "D", 10)
  293. f.SetColWidth(r.T_name, "E", "E", 10)
  294. f.SetColWidth(r.T_name, "F", "F", 10)
  295. f.SetColWidth(r.T_name, "G", "G", 10)
  296. f.SetColWidth(r.T_name, "H", "H", 10)
  297. f.SetColWidth(r.T_name, "I", "I", 10)
  298. f.SetColWidth(r.T_name, "J", "J", 70)
  299. //f.SetColWidth(r.T_name, "K", "K", 10)
  300. line := 2
  301. // 循环写入数据
  302. for _, v := range StockList {
  303. line++
  304. product, _ := Basic.Read_Product_ById(v.T_product_id)
  305. f.SetCellValue(r.T_name, fmt.Sprintf("A%d", line), line-2)
  306. f.SetCellValue(r.T_name, fmt.Sprintf("B%d", line), product.T_name)
  307. f.SetCellValue(r.T_name, fmt.Sprintf("C%d", line), product.T_model)
  308. f.SetCellValue(r.T_name, fmt.Sprintf("D%d", line), product.T_spec)
  309. f.SetCellValue(r.T_name, fmt.Sprintf("E%d", line), v.T_month)
  310. f.SetCellValue(r.T_name, fmt.Sprintf("F%d", line), v.T_beginning)
  311. f.SetCellValue(r.T_name, fmt.Sprintf("G%d", line), v.T_in)
  312. f.SetCellValue(r.T_name, fmt.Sprintf("H%d", line), v.T_out)
  313. f.SetCellValue(r.T_name, fmt.Sprintf("I%d", line), v.T_ending)
  314. if product.T_early_warning > v.T_ending {
  315. f.SetCellStyle(r.T_name, fmt.Sprintf("I%d", line), fmt.Sprintf("I%d", line), Style3)
  316. } else {
  317. f.SetCellStyle(r.T_name, fmt.Sprintf("I%d", line), fmt.Sprintf("I%d", line), Style4)
  318. }
  319. T_project := StockOutDao.Read_StockOut_T_project(T_depot_id, product.Id, v.T_month)
  320. //f.SetCellValue(r.T_name, fmt.Sprintf("J%d", line), v.T_project)
  321. f.SetCellValue(r.T_name, fmt.Sprintf("J%d", line), strings.Join(T_project, "\r\n"))
  322. }
  323. f.SetCellStyle(r.T_name, "A2", fmt.Sprintf("H%d", line), Style4)
  324. Style5, _ := f.NewStyle(
  325. &excelize.Style{
  326. Font: &excelize.Font{Size: 10, Family: "宋体"},
  327. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  328. Border: []excelize.Border{
  329. {Type: "left", Color: "000000", Style: 1},
  330. {Type: "top", Color: "000000", Style: 1},
  331. {Type: "bottom", Color: "000000", Style: 1},
  332. {Type: "right", Color: "000000", Style: 1},
  333. },
  334. })
  335. f.SetCellStyle(r.T_name, "J2", fmt.Sprintf("J%d", line), Style5)
  336. }
  337. // 保存文件
  338. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  339. fmt.Println(err)
  340. }
  341. var url string
  342. //// 上传 OSS
  343. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  344. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  345. if !is {
  346. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  347. c.ServeJSON()
  348. return
  349. }
  350. //删除目录
  351. err := os.Remove("ofile/" + filename + ".xlsx")
  352. if err != nil {
  353. logs.Error(lib.FuncName(), err)
  354. }
  355. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  356. c.ServeJSON()
  357. return
  358. }
  359. func (c *StockController) StockIn_List() {
  360. // 分页参数 初始化
  361. page, _ := c.GetInt("page")
  362. if page < 1 {
  363. page = 1
  364. }
  365. page_z, _ := c.GetInt("page_z")
  366. if page_z < 1 {
  367. page_z = conf.Page_size
  368. }
  369. // 查询
  370. T_depot_id, _ := c.GetInt("T_depot_id")
  371. T_start_date := c.GetString("T_start_date")
  372. T_end_date := c.GetString("T_end_date")
  373. T_name := c.GetString("T_name")
  374. userList, _ := NatsServer.Read_User_List_All()
  375. Account.Read_User_All_Map(userList)
  376. Basic.Read_Depot_All_Map()
  377. StockInDao := Stock.NewStockIn(orm.NewOrm())
  378. R_List, R_cnt := StockInDao.Read_StockIn_List(T_name, T_depot_id, T_start_date, T_end_date, page, page_z)
  379. var r_jsons lib.R_JSONS
  380. r_jsons.Num = R_cnt
  381. r_jsons.Data = R_List
  382. r_jsons.Page = page
  383. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  384. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  385. c.ServeJSON()
  386. return
  387. }
  388. func (c *StockController) StockIn_List_Product() {
  389. // 分页参数 初始化
  390. page, _ := c.GetInt("page")
  391. if page < 1 {
  392. page = 1
  393. }
  394. page_z, _ := c.GetInt("page_z")
  395. if page_z < 1 {
  396. page_z = conf.Page_size
  397. }
  398. // 查询
  399. T_depot_id, _ := c.GetInt("T_depot_id")
  400. T_name := c.GetString("T_name")
  401. T_start_date := c.GetString("T_start_date")
  402. T_end_date := c.GetString("T_end_date")
  403. T_product_name := c.GetString("T_product_name") // 产品名称
  404. T_product_model := c.GetString("T_product_model") // 产品型号
  405. userList, _ := NatsServer.Read_User_List_All()
  406. Account.Read_User_All_Map(userList)
  407. Basic.Read_Depot_All_Map()
  408. StockInDao := Stock.NewStockIn(orm.NewOrm())
  409. R_List, R_cnt := StockInDao.Read_StockInProduct_List(T_name, T_start_date, T_end_date, T_depot_id, T_product_name, T_product_model, page, page_z)
  410. var r_jsons lib.R_JSONS
  411. r_jsons.Num = R_cnt
  412. r_jsons.Data = R_List
  413. r_jsons.Page = page
  414. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  415. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  416. c.ServeJSON()
  417. return
  418. }
  419. func (c *StockController) StockIn_List_Product_Excel() {
  420. // 查询
  421. T_depot_id, _ := c.GetInt("T_depot_id")
  422. T_name := c.GetString("T_name")
  423. T_start_date := c.GetString("T_start_date")
  424. T_end_date := c.GetString("T_end_date")
  425. T_product_name := c.GetString("T_product_name") // 产品名称
  426. T_product_model := c.GetString("T_product_model") // 产品型号
  427. userList, _ := NatsServer.Read_User_List_All()
  428. Account.Read_User_All_Map(userList)
  429. Basic.Read_Depot_All_Map()
  430. StockInDao := Stock.NewStockIn(orm.NewOrm())
  431. R_List, _ := StockInDao.Read_StockInProduct_List(T_name, T_start_date, T_end_date, T_depot_id, T_product_name, T_product_model, 0, 9999)
  432. filename := fmt.Sprintf("入库明细单(%s)", lib.GetRandstring(6, "0123456789", 0))
  433. f := excelize.NewFile() // 设置单元格的值
  434. Style1, _ := f.NewStyle(
  435. &excelize.Style{
  436. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  437. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  438. })
  439. Style2, _ := f.NewStyle(
  440. &excelize.Style{
  441. Font: &excelize.Font{Bold: true, Size: 11, Family: "宋体"},
  442. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  443. Border: []excelize.Border{
  444. {Type: "left", Color: "000000", Style: 1},
  445. {Type: "top", Color: "000000", Style: 1},
  446. {Type: "bottom", Color: "000000", Style: 1},
  447. {Type: "right", Color: "000000", Style: 1},
  448. },
  449. })
  450. Style3, _ := f.NewStyle(
  451. &excelize.Style{
  452. Font: &excelize.Font{Size: 11, Family: "宋体"},
  453. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  454. Border: []excelize.Border{
  455. {Type: "left", Color: "000000", Style: 1},
  456. {Type: "top", Color: "000000", Style: 1},
  457. {Type: "bottom", Color: "000000", Style: 1},
  458. {Type: "right", Color: "000000", Style: 1},
  459. },
  460. })
  461. f.MergeCell("Sheet1", "A1", "L1")
  462. f.SetRowStyle("Sheet1", 1, 1, Style1)
  463. f.SetCellValue("Sheet1", "A1", fmt.Sprintf("入库明细单"))
  464. f.SetRowHeight("Sheet1", 1, 30)
  465. // 这里设置表头
  466. f.SetCellStyle("Sheet1", "A2", "L2", Style2)
  467. f.SetRowHeight("Sheet1", 2, 20)
  468. f.SetCellValue("Sheet1", "A2", "序号")
  469. f.SetCellValue("Sheet1", "B2", "入库单号")
  470. f.SetCellValue("Sheet1", "C2", "经办人")
  471. f.SetCellValue("Sheet1", "D2", "入库日期")
  472. f.SetCellValue("Sheet1", "E2", "入库仓库")
  473. f.SetCellValue("Sheet1", "F2", "入库类型")
  474. f.SetCellValue("Sheet1", "G2", "产品名称")
  475. f.SetCellValue("Sheet1", "H2", "产品型号")
  476. f.SetCellValue("Sheet1", "I2", "数量")
  477. f.SetCellValue("Sheet1", "J2", "是否关联SN")
  478. f.SetCellValue("Sheet1", "K2", "备注")
  479. f.SetCellValue("Sheet1", "L2", "SN")
  480. // 设置列宽
  481. f.SetColWidth("Sheet1", "A", "A", 10)
  482. f.SetColWidth("Sheet1", "B", "B", 15)
  483. f.SetColWidth("Sheet1", "C", "D", 10)
  484. f.SetColWidth("Sheet1", "D", "D", 10)
  485. f.SetColWidth("Sheet1", "E", "E", 10)
  486. f.SetColWidth("Sheet1", "F", "F", 10)
  487. f.SetColWidth("Sheet1", "G", "G", 20)
  488. f.SetColWidth("Sheet1", "H", "H", 15)
  489. f.SetColWidth("Sheet1", "I", "I", 10)
  490. f.SetColWidth("Sheet1", "J", "J", 12)
  491. f.SetColWidth("Sheet1", "K", "K", 30)
  492. f.SetColWidth("Sheet1", "L", "L", 100)
  493. line := 2
  494. for _, product := range R_List {
  495. line += 1
  496. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), line-2)
  497. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), product.T_number)
  498. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), product.T_submit_name)
  499. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), product.T_date)
  500. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), product.T_depot_name)
  501. var type_str string
  502. if product.T_type == 1 {
  503. type_str = "入库"
  504. } else {
  505. type_str = "退库"
  506. }
  507. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), type_str)
  508. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), product.T_product_name)
  509. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), product.T_product_model)
  510. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), product.T_num)
  511. var relation_sn string
  512. if product.T_product_relation_sn == 1 {
  513. relation_sn = "是"
  514. } else {
  515. relation_sn = "否"
  516. }
  517. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), relation_sn)
  518. f.SetCellValue("Sheet1", fmt.Sprintf("K%d", line), product.T_remark)
  519. f.SetCellValue("Sheet1", fmt.Sprintf("L%d", line), product.T_relation_sn)
  520. }
  521. f.SetCellStyle("Sheet1", "A3", fmt.Sprintf("L%d", line), Style3)
  522. // 保存文件
  523. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  524. fmt.Println(err)
  525. }
  526. var url string
  527. //// 上传 OSS
  528. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  529. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  530. if !is {
  531. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  532. c.ServeJSON()
  533. return
  534. }
  535. //删除目录
  536. err := os.Remove("ofile/" + filename + ".xlsx")
  537. if err != nil {
  538. logs.Error(lib.FuncName(), err)
  539. }
  540. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  541. c.ServeJSON()
  542. return
  543. }
  544. func (c *StockController) StockIn_Get() {
  545. // 查询
  546. T_number := c.GetString("T_number")
  547. o := orm.NewOrm()
  548. StockInDao := Stock.NewStockIn(o)
  549. StockInProductDao := Stock.NewStockInProduct(o)
  550. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  551. if err != nil {
  552. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  553. c.ServeJSON()
  554. return
  555. }
  556. productList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  557. var pList []Stock.StockInProduct_R
  558. for _, v := range productList {
  559. pList = append(pList, Stock.StockInProductToStockInProduct_R(v))
  560. }
  561. userList, _ := NatsServer.Read_User_List_All()
  562. Account.Read_User_All_Map(userList)
  563. Basic.Read_Depot_All_Map()
  564. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Stock.StockInToStockIn_Detail(stockIn, pList)}
  565. c.ServeJSON()
  566. return
  567. }
  568. func (c *StockController) StockIn_Add() {
  569. rand_x := 0
  570. T_number := ""
  571. o := orm.NewOrm()
  572. o.Begin()
  573. StockInDao := Stock.NewStockIn(o)
  574. for true {
  575. T_number = "RK-" + lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", int64(rand_x))
  576. _, err := StockInDao.Read_StockIn_ByT_number(T_number)
  577. if err != nil && err.Error() == orm.ErrNoRows.Error() {
  578. break
  579. }
  580. rand_x += 1
  581. }
  582. T_depot_id, _ := c.GetInt("T_depot_id")
  583. T_type, _ := c.GetInt("T_type") // 1-入库 2-退库
  584. T_date := c.GetString("T_date")
  585. T_product := c.GetString("T_product")
  586. T_remark := c.GetString("T_remark")
  587. date, is := lib.DateStrToTime(T_date)
  588. if !is {
  589. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  590. c.ServeJSON()
  591. return
  592. }
  593. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "入库", T_product)
  594. var_ := Stock.StockIn{
  595. T_number: T_number,
  596. T_depot_id: T_depot_id,
  597. T_type: T_type,
  598. T_date: T_date,
  599. T_remark: T_remark,
  600. T_submit: c.User.T_uuid,
  601. }
  602. StockInProductDao := Stock.NewStockInProduct(o)
  603. StockDao := Stock.NewStock(o)
  604. DeviceDao := Stock.NewDevice(o)
  605. IotCardDao := Property.NewIotCard(&o)
  606. _, err := StockInDao.Add_StockIn(var_)
  607. if err != nil {
  608. o.Rollback()
  609. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  610. c.ServeJSON()
  611. return
  612. }
  613. if len(T_product) == 0 {
  614. o.Rollback()
  615. c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
  616. c.ServeJSON()
  617. return
  618. }
  619. productList := lib.SplitString(T_product, "|")
  620. allProductList := []int{}
  621. for _, v := range productList {
  622. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  623. allProductList = append(allProductList, product_id)
  624. product, _ := Basic.Read_Product_ById(product_id)
  625. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  626. T_relation_sn := strings.Split(v, "-")[2]
  627. if T_relation_sn == "" && product.T_relation_sn == 1 {
  628. o.Rollback()
  629. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  630. c.ServeJSON()
  631. return
  632. }
  633. if len(T_relation_sn) > 0 {
  634. snList := strings.Split(strings.Trim(T_relation_sn, ","), ",")
  635. for _, sn := range snList {
  636. mqtt := Stock.Read_MqttUser(sn)
  637. // 添加设备
  638. device := Stock.Device{
  639. T_contract_number: "",
  640. T_out_number: "",
  641. T_product_id: product_id,
  642. T_in_number: T_number,
  643. T_sn: sn,
  644. T_iccid: mqtt.Iccid,
  645. T_imei: mqtt.Imei,
  646. T_State: 2,
  647. CreateTime: date,
  648. }
  649. _, err = DeviceDao.AddOrUpdate_Device(device, 2)
  650. if err != nil {
  651. o.Rollback()
  652. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  653. c.ServeJSON()
  654. return
  655. }
  656. // 添加物联网卡
  657. iotCard := Property.IotCard{
  658. T_iccid: mqtt.Iccid,
  659. T_sn: sn,
  660. T_State: 2, //1-未使用 2-已使用 3-已作废
  661. T_type: "4G",
  662. }
  663. _, err = IotCardDao.AddOrUpdate_IotCard(iotCard)
  664. if err != nil {
  665. o.Rollback()
  666. c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库失败"}
  667. c.ServeJSON()
  668. return
  669. }
  670. }
  671. }
  672. stockInProduct := Stock.StockInProduct{
  673. T_number: T_number,
  674. T_product_id: product_id,
  675. T_depot_id: T_depot_id,
  676. T_num: num, // 入库数量
  677. T_date: T_date, // 入库日期
  678. T_relation_sn: T_relation_sn,
  679. }
  680. _, err = StockInProductDao.Add_StockInProduct(stockInProduct)
  681. if err != nil {
  682. o.Rollback()
  683. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  684. c.ServeJSON()
  685. return
  686. }
  687. _, err = StockDao.AddOrUpdate_Stock(T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num, 2)
  688. if err != nil {
  689. o.Rollback()
  690. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  691. c.ServeJSON()
  692. return
  693. }
  694. }
  695. o.Commit()
  696. StockIn_Edit_StockMonth(T_date, T_depot_id, allProductList)
  697. NatsServer.AddUserLogs(c.User.T_uuid, "入库", "入库", var_)
  698. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  699. c.ServeJSON()
  700. return
  701. }
  702. func StockIn_Edit_StockMonth(T_date string, T_depot_id int, allProductList []int) error {
  703. date, _ := lib.DateStrToTime(T_date)
  704. //if date.Year() == time.Now().Year() && date.Month() == time.Now().Month() {
  705. // // 修改本月数据不同步库存
  706. // return nil
  707. //}
  708. //T_month := date.Format("2006-01")
  709. months := generateMonthList(date)
  710. o := orm.NewOrm()
  711. StockOutDao := Stock.NewStockOut(o)
  712. StockMonthDao := Stock.NewStockMonth(o)
  713. StockOutProductDao := Stock.NewStockOutProduct(o)
  714. StockInProductDao := Stock.NewStockInProduct(o)
  715. for _, T_product_id := range allProductList {
  716. for _, T_month := range months {
  717. // 获取当前产品本月出入库数量
  718. lastMonth := StockMonthDao.Read_LastMonth_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
  719. stockMonth := StockMonthDao.Read_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
  720. if stockMonth.Id > 0 {
  721. // 修改
  722. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(T_depot_id, T_product_id, T_month)
  723. if len(lastMonth) > 0 {
  724. stockMonth.T_beginning = lastMonth[0].T_ending
  725. } else {
  726. stockMonth.T_beginning = 0
  727. }
  728. // 期末库存 = 期初库存+入库-出库
  729. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  730. err := StockMonthDao.Update_StockMonth(stockMonth, "T_in", "T_beginning", "T_ending")
  731. if err != nil {
  732. return err
  733. }
  734. } else {
  735. product, _ := Basic.Read_Product_ById(T_product_id)
  736. // 添加库存记录
  737. stockMonth = Stock.StockMonth{
  738. T_depot_id: T_depot_id,
  739. T_product_id: T_product_id,
  740. T_product_class: product.T_class,
  741. T_month: T_month,
  742. }
  743. // 获取当前产品本月出入库数量
  744. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(T_depot_id, T_product_id, T_month)
  745. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(T_depot_id, T_product_id, T_month)
  746. if len(lastMonth) > 0 {
  747. stockMonth.T_beginning = lastMonth[0].T_ending
  748. } else {
  749. stockMonth.T_beginning = 0
  750. }
  751. // 期末库存 = 期初库存+入库-出库
  752. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  753. stockMonth.T_project = StockOutDao.Read_StockOut_T_contract_number(T_depot_id, T_product_id, T_month)
  754. _, err := StockMonthDao.Add_StockMonth(stockMonth)
  755. if err != nil {
  756. return err
  757. }
  758. }
  759. }
  760. }
  761. return nil
  762. }
  763. func (c *StockController) StockIn_Edit() {
  764. T_number := c.GetString("T_number") // 入库单号
  765. //T_depot_id, _ := c.GetInt("T_depot_id")
  766. T_date := c.GetString("T_date")
  767. T_product := c.GetString("T_product")
  768. T_remark := c.GetString("T_remark")
  769. date, is := lib.DateStrToTime(T_date)
  770. if !is {
  771. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  772. c.ServeJSON()
  773. return
  774. }
  775. if date.Year() != time.Now().Year() && date.Month() != time.Now().Month() {
  776. T_date = ""
  777. }
  778. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "修改入库", T_product)
  779. o := orm.NewOrm()
  780. o.Begin()
  781. StockInDao := Stock.NewStockIn(o)
  782. StockInProductDao := Stock.NewStockInProduct(o)
  783. StockDao := Stock.NewStock(o)
  784. DeviceDao := Stock.NewDevice(o)
  785. IotCardDao := Property.NewIotCard(&o)
  786. // 查询入库信息
  787. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  788. T_old_date := stockIn.T_date
  789. if err != nil {
  790. o.Rollback()
  791. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  792. c.ServeJSON()
  793. return
  794. }
  795. // 查询入库产品信息
  796. productOldList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  797. var oldProductList []dto.StockProduct
  798. var newProductList []dto.StockProduct
  799. allProductListMap := make(map[int]struct{})
  800. productOldMap := map[int]dto.StockProduct{}
  801. for _, product := range productOldList {
  802. stockProduct := dto.StockProduct{
  803. T_product_id: product.T_product_id,
  804. T_num: product.T_num,
  805. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  806. }
  807. oldProductList = append(oldProductList, stockProduct)
  808. productOldMap[product.T_product_id] = stockProduct
  809. if _, ok := allProductListMap[product.T_product_id]; !ok {
  810. allProductListMap[product.T_product_id] = struct{}{}
  811. }
  812. }
  813. productNewList := lib.SplitString(T_product, "|")
  814. productNewMap := map[int]dto.StockProduct{}
  815. for _, v := range productNewList {
  816. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  817. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  818. T_relation_sn := strings.Split(v, "-")[2]
  819. stockProduct := dto.StockProduct{
  820. T_product_id: product_id,
  821. T_num: num,
  822. T_relation_sn: lib.SplitString(T_relation_sn, ","),
  823. }
  824. newProductList = append(newProductList, stockProduct)
  825. productNewMap[product_id] = stockProduct
  826. if _, ok := allProductListMap[product_id]; !ok {
  827. allProductListMap[product_id] = struct{}{}
  828. }
  829. }
  830. // 判断产品列表信息是否相同
  831. StockProductListIsEqual, needDelete, needAdd, needEdit, snDiff := dto.StockProductListsEqual(oldProductList, newProductList)
  832. // 两次提交的数据相同,则不用修改
  833. if !StockProductListIsEqual {
  834. if len(needDelete) > 0 {
  835. // 删除入库产品列表
  836. for _, v := range needDelete {
  837. product, _ := Basic.Read_Product_ById(v.T_product_id)
  838. // 删除入库产品列表
  839. err = StockInProductDao.Delete_StockInProduct(stockIn.T_number, stockIn.T_depot_id, v.T_product_id)
  840. if err != nil {
  841. o.Rollback()
  842. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  843. c.ServeJSON()
  844. return
  845. }
  846. if product.T_relation_sn == 1 {
  847. // 删除该入库单关联sn
  848. for _, sn := range v.T_relation_sn {
  849. err = DeviceDao.Delete_Device_ByT_in_number(stockIn.T_number, sn)
  850. if err != nil {
  851. o.Rollback()
  852. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库设备失败"}
  853. c.ServeJSON()
  854. return
  855. }
  856. }
  857. }
  858. // 减少库存
  859. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
  860. if err != nil {
  861. o.Rollback()
  862. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
  863. c.ServeJSON()
  864. return
  865. }
  866. }
  867. }
  868. if len(needAdd) > 0 {
  869. // 新增入库产品列表
  870. for _, v := range needAdd {
  871. product, _ := Basic.Read_Product_ById(v.T_product_id)
  872. if product.T_relation_sn == 1 && len(v.T_relation_sn) == 0 {
  873. o.Rollback()
  874. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  875. c.ServeJSON()
  876. return
  877. }
  878. // 添加该入库单关联sn
  879. for _, sn := range v.T_relation_sn {
  880. mqtt := Stock.Read_MqttUser(sn)
  881. // 添加设备
  882. device := Stock.Device{
  883. T_contract_number: "",
  884. T_out_number: "",
  885. T_product_id: v.T_product_id,
  886. T_in_number: stockIn.T_number,
  887. T_sn: sn,
  888. T_iccid: mqtt.Iccid,
  889. T_imei: mqtt.Imei,
  890. T_State: 2,
  891. CreateTime: date,
  892. UpdateTime: stockIn.UpdateTime,
  893. }
  894. _, err = DeviceDao.AddOrUpdate_Device(device, 2)
  895. if err != nil {
  896. o.Rollback()
  897. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  898. c.ServeJSON()
  899. return
  900. }
  901. // 添加物联网卡
  902. iotCard := Property.IotCard{
  903. T_iccid: mqtt.Iccid,
  904. T_sn: sn,
  905. T_State: 2, //1-未使用 2-已使用 3-已作废
  906. T_type: "4G",
  907. }
  908. _, err = IotCardDao.AddOrUpdate_IotCard(iotCard)
  909. if err != nil {
  910. o.Rollback()
  911. c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库失败"}
  912. c.ServeJSON()
  913. return
  914. }
  915. }
  916. stockInProduct := Stock.StockInProduct{
  917. T_number: T_number,
  918. T_product_id: v.T_product_id,
  919. T_depot_id: stockIn.T_depot_id,
  920. T_num: v.T_num, // 入库数量
  921. T_date: stockIn.T_date, // 入库日期
  922. T_relation_sn: strings.Join(v.T_relation_sn, ","),
  923. }
  924. _, err = StockInProductDao.Add_StockInProduct(stockInProduct)
  925. if err != nil {
  926. o.Rollback()
  927. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  928. c.ServeJSON()
  929. return
  930. }
  931. // 增加库存
  932. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 2)
  933. if err != nil {
  934. o.Rollback()
  935. c.Data["json"] = lib.JSONS{Code: 203, Msg: "增加库存失败"}
  936. c.ServeJSON()
  937. return
  938. }
  939. }
  940. }
  941. if len(needEdit) > 0 {
  942. for _, v := range needEdit {
  943. product, _ := Basic.Read_Product_ById(v.T_product_id)
  944. stockInProduct := Stock.StockInProduct{
  945. T_number: stockIn.T_number,
  946. T_product_id: v.T_product_id,
  947. T_depot_id: stockIn.T_depot_id,
  948. T_num: productNewMap[v.T_product_id].T_num, // 入库数量
  949. T_date: stockIn.T_date, // 入库日期
  950. }
  951. // 入库数量比之前多,增加库存
  952. var T_type int
  953. var T_num int
  954. if productNewMap[v.T_product_id].T_num > productOldMap[v.T_product_id].T_num {
  955. // 增加库存
  956. T_type = 2
  957. T_num = productNewMap[v.T_product_id].T_num - productOldMap[v.T_product_id].T_num
  958. } else {
  959. // 减少库存
  960. T_type = 1
  961. T_num = productOldMap[v.T_product_id].T_num - productNewMap[v.T_product_id].T_num
  962. }
  963. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, T_num, T_type)
  964. if err != nil {
  965. o.Rollback()
  966. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  967. c.ServeJSON()
  968. return
  969. }
  970. // 更新产品库存表
  971. err = StockInProductDao.Update_StockInProduct(stockInProduct)
  972. if err != nil {
  973. o.Rollback()
  974. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库产品信息失败"}
  975. c.ServeJSON()
  976. return
  977. }
  978. }
  979. }
  980. for _, diff := range snDiff {
  981. product, _ := Basic.Read_Product_ById(diff.T_product_id)
  982. // 删除减少的sn
  983. if len(diff.T_delete_relation_sn) > 0 {
  984. for _, sn := range diff.T_delete_relation_sn {
  985. err = DeviceDao.Delete_Device_ByT_in_number(stockIn.T_number, sn)
  986. if err != nil {
  987. o.Rollback()
  988. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  989. c.ServeJSON()
  990. return
  991. }
  992. }
  993. // 减少库存
  994. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, len(diff.T_delete_relation_sn), 1)
  995. if err != nil {
  996. o.Rollback()
  997. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  998. c.ServeJSON()
  999. return
  1000. }
  1001. }
  1002. if len(diff.T_add_relation_sn) > 0 {
  1003. for _, sn := range diff.T_add_relation_sn {
  1004. mqtt := Stock.Read_MqttUser(sn)
  1005. // 添加设备
  1006. device := Stock.Device{
  1007. T_contract_number: "",
  1008. T_out_number: "",
  1009. T_product_id: diff.T_product_id,
  1010. T_in_number: stockIn.T_number,
  1011. T_sn: sn,
  1012. T_iccid: mqtt.Iccid,
  1013. T_imei: mqtt.Imei,
  1014. T_State: 2,
  1015. CreateTime: date,
  1016. UpdateTime: stockIn.UpdateTime,
  1017. }
  1018. _, err = DeviceDao.AddOrUpdate_Device(device, 2)
  1019. if err != nil {
  1020. o.Rollback()
  1021. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  1022. c.ServeJSON()
  1023. return
  1024. }
  1025. // 添加物联网卡
  1026. iotCard := Property.IotCard{
  1027. T_iccid: mqtt.Iccid,
  1028. T_sn: sn,
  1029. T_State: 2, //1-未使用 2-已使用 3-已作废
  1030. T_type: "4G",
  1031. }
  1032. _, err = IotCardDao.AddOrUpdate_IotCard(iotCard)
  1033. if err != nil {
  1034. o.Rollback()
  1035. c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库失败"}
  1036. c.ServeJSON()
  1037. return
  1038. }
  1039. }
  1040. // 增加库存
  1041. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, len(diff.T_add_relation_sn), 2)
  1042. if err != nil {
  1043. o.Rollback()
  1044. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  1045. c.ServeJSON()
  1046. return
  1047. }
  1048. }
  1049. stockInProduct := Stock.StockInProduct{
  1050. T_number: stockIn.T_number,
  1051. T_product_id: diff.T_product_id,
  1052. T_depot_id: stockIn.T_depot_id,
  1053. T_num: productNewMap[diff.T_product_id].T_num, // 入库数量
  1054. T_date: stockIn.T_date, // 入库日期
  1055. T_relation_sn: strings.Join(productNewMap[diff.T_product_id].T_relation_sn, ","),
  1056. }
  1057. // 更新产品库存表
  1058. err = StockInProductDao.Update_StockInProduct(stockInProduct)
  1059. if err != nil {
  1060. o.Rollback()
  1061. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库产品信息失败"}
  1062. c.ServeJSON()
  1063. return
  1064. }
  1065. }
  1066. }
  1067. if len(T_remark) > 0 {
  1068. stockIn.T_remark = T_remark
  1069. }
  1070. if len(T_date) > 0 {
  1071. stockIn.T_date = T_date
  1072. }
  1073. err = StockInDao.Update_StockIn(stockIn, "T_remark", "T_date")
  1074. if err != nil {
  1075. o.Rollback()
  1076. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库失败"}
  1077. c.ServeJSON()
  1078. return
  1079. }
  1080. o.Commit()
  1081. if len(T_date) > 0 && T_old_date != T_date {
  1082. // 修改出库产品日期
  1083. StockInProductDao.Update_StockInProduct_T_date(stockIn.T_number, T_date)
  1084. DeviceDao.Update_Device_CreateTimeByT_in_number(stockIn.T_number, T_date)
  1085. }
  1086. // 更新月份统计表
  1087. allProductList := []int{}
  1088. for i, _ := range allProductListMap {
  1089. allProductList = append(allProductList, i)
  1090. }
  1091. StockIn_Edit_StockMonth(stockIn.T_date, stockIn.T_depot_id, allProductList)
  1092. NatsServer.AddUserLogs(c.User.T_uuid, "入库", "修改", stockIn)
  1093. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  1094. c.ServeJSON()
  1095. return
  1096. }
  1097. // 删除入库
  1098. func (c *StockController) StockIn_Del() {
  1099. T_number := c.GetString("T_number") // 入库单号
  1100. o := orm.NewOrm()
  1101. o.Begin()
  1102. StockInDao := Stock.NewStockIn(o)
  1103. StockInProductDao := Stock.NewStockInProduct(o)
  1104. StockDao := Stock.NewStock(o)
  1105. DeviceDao := Stock.NewDevice(o)
  1106. // 查询入库信息
  1107. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  1108. if err != nil {
  1109. o.Rollback()
  1110. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1111. c.ServeJSON()
  1112. return
  1113. }
  1114. // 查询入库产品信息
  1115. productOldList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  1116. var oldProductList []dto.StockProduct
  1117. allProductList := []int{}
  1118. for _, product := range productOldList {
  1119. oldProductList = append(oldProductList, dto.StockProduct{
  1120. T_product_id: product.T_product_id,
  1121. T_num: product.T_num,
  1122. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  1123. })
  1124. allProductList = append(allProductList, product.T_product_id)
  1125. }
  1126. // 删除入库产品列表
  1127. for _, v := range oldProductList {
  1128. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1129. // 删除入库产品列表
  1130. err = StockInProductDao.Delete_StockInProduct(stockIn.T_number, stockIn.T_depot_id, v.T_product_id)
  1131. if err != nil {
  1132. o.Rollback()
  1133. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  1134. c.ServeJSON()
  1135. return
  1136. }
  1137. if product.T_relation_sn == 1 {
  1138. // 删除该入库单关联sn
  1139. for _, sn := range v.T_relation_sn {
  1140. err = DeviceDao.Delete_Device_ByT_in_number(stockIn.T_number, sn)
  1141. if err != nil {
  1142. o.Rollback()
  1143. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库设备失败"}
  1144. c.ServeJSON()
  1145. return
  1146. }
  1147. }
  1148. }
  1149. // 减少库存
  1150. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
  1151. if err != nil {
  1152. o.Rollback()
  1153. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
  1154. c.ServeJSON()
  1155. return
  1156. }
  1157. }
  1158. err = StockInDao.Delete_StockIn(stockIn)
  1159. if err != nil {
  1160. o.Rollback()
  1161. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库失败"}
  1162. c.ServeJSON()
  1163. return
  1164. }
  1165. o.Commit()
  1166. // 更新月份统计表
  1167. StockIn_Edit_StockMonth(stockIn.T_date, stockIn.T_depot_id, allProductList)
  1168. NatsServer.AddUserLogs(c.User.T_uuid, "入库", "删除", stockIn)
  1169. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  1170. c.ServeJSON()
  1171. return
  1172. }
  1173. // 导出出库单
  1174. func (c *StockController) StockIn_Excel() {
  1175. // 查询
  1176. T_number := c.GetString("T_number")
  1177. o := orm.NewOrm()
  1178. StockInDao := Stock.NewStockIn(o)
  1179. StockInProductDao := Stock.NewStockInProduct(o)
  1180. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  1181. if err != nil {
  1182. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1183. c.ServeJSON()
  1184. return
  1185. }
  1186. productList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  1187. var pList []Stock.StockInProduct_R
  1188. for _, v := range productList {
  1189. pList = append(pList, Stock.StockInProductToStockInProduct_R(v))
  1190. }
  1191. userList, _ := NatsServer.Read_User_List_All()
  1192. Account.Read_User_All_Map(userList)
  1193. stockInDetail := Stock.StockInToStockIn_Detail(stockIn, pList)
  1194. T_date, _ := lib.DateStrToTime(stockIn.T_date)
  1195. filename := fmt.Sprintf("冷链产品入库单(%s)", lib.GetRandstring(6, "0123456789", 0))
  1196. f := excelize.NewFile() // 设置单元格的值
  1197. Style1, _ := f.NewStyle(
  1198. &excelize.Style{
  1199. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  1200. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1201. })
  1202. Style2, _ := f.NewStyle(
  1203. &excelize.Style{
  1204. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1205. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  1206. })
  1207. Style3, _ := f.NewStyle(
  1208. &excelize.Style{
  1209. Font: &excelize.Font{Size: 10, Family: "宋体", Bold: true},
  1210. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1211. Border: []excelize.Border{
  1212. {Type: "left", Color: "000000", Style: 1},
  1213. {Type: "top", Color: "000000", Style: 1},
  1214. {Type: "bottom", Color: "000000", Style: 1},
  1215. {Type: "right", Color: "000000", Style: 1},
  1216. },
  1217. })
  1218. Style4, _ := f.NewStyle(
  1219. &excelize.Style{
  1220. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1221. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1222. Border: []excelize.Border{
  1223. {Type: "left", Color: "000000", Style: 1},
  1224. {Type: "top", Color: "000000", Style: 1},
  1225. {Type: "bottom", Color: "000000", Style: 1},
  1226. {Type: "right", Color: "000000", Style: 1},
  1227. },
  1228. })
  1229. productList2 := lib.ChunkBy(productList, 10)
  1230. for i, products := range productList2 {
  1231. if len(products) < 10 {
  1232. products = append(products, make([]Stock.StockInProduct, 10-len(products))...)
  1233. }
  1234. row := i*17 + 1
  1235. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1236. f.SetRowStyle("Sheet1", row, row, Style1)
  1237. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("宝智达科技产品进销存统计表"))
  1238. f.SetRowHeight("Sheet1", row, 20)
  1239. row += 1
  1240. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1241. f.SetRowStyle("Sheet1", row, row, Style1)
  1242. title := "硬件入库单"
  1243. if len(productList2) > 1 {
  1244. title += fmt.Sprintf(" %d/%d", i+1, len(productList2))
  1245. }
  1246. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), title)
  1247. f.SetRowHeight("Sheet1", row, 20)
  1248. row += 1
  1249. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1250. f.SetRowStyle("Sheet1", row, row, Style2)
  1251. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("入库单号:%s", stockIn.T_number))
  1252. f.SetRowHeight("Sheet1", row, 20)
  1253. row += 1
  1254. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1255. f.SetRowStyle("Sheet1", row, row, Style2)
  1256. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("项目名称:%s", stockIn.T_remark))
  1257. f.SetRowHeight("Sheet1", row, 20)
  1258. row += 1
  1259. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1260. f.SetRowStyle("Sheet1", row, row, Style2)
  1261. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("入库时间:%s", T_date.Format("2006年01月02日")))
  1262. f.SetRowHeight("Sheet1", row, 20)
  1263. // 这里设置表头
  1264. row += 1
  1265. f.SetRowHeight("Sheet1", row, 20)
  1266. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "序号")
  1267. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), "产品名称")
  1268. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), "型号")
  1269. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), "单位")
  1270. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), "领用数量")
  1271. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), "备注")
  1272. // 设置列宽
  1273. f.SetColWidth("Sheet1", "A", "A", 5)
  1274. f.SetColWidth("Sheet1", "B", "B", 20)
  1275. f.SetColWidth("Sheet1", "C", "C", 12)
  1276. f.SetColWidth("Sheet1", "D", "D", 10)
  1277. f.SetColWidth("Sheet1", "E", "E", 10)
  1278. f.SetColWidth("Sheet1", "F", "F", 15)
  1279. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row), Style3)
  1280. sRow := row
  1281. // 循环写入数据
  1282. for j, v := range products {
  1283. row += 1
  1284. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1285. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), j+1)
  1286. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), product.T_name)
  1287. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), product.T_model)
  1288. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), product.T_spec)
  1289. if v.T_num > 0 {
  1290. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), v.T_num)
  1291. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), stockIn.T_remark)
  1292. }
  1293. }
  1294. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", sRow), fmt.Sprintf("F%d", row), Style4)
  1295. row += 1
  1296. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("C%d", row))
  1297. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("制单:%s", c.User.T_name))
  1298. f.MergeCell("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("F%d", row))
  1299. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("经办人:%s", stockInDetail.T_submit_name))
  1300. f.SetRowStyle("Sheet1", row, row, Style2)
  1301. f.SetRowHeight("Sheet1", row, 20)
  1302. }
  1303. // 保存文件
  1304. if err = f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  1305. fmt.Println(err)
  1306. }
  1307. var url string
  1308. //// 上传 OSS
  1309. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  1310. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  1311. if !is {
  1312. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  1313. c.ServeJSON()
  1314. return
  1315. }
  1316. //删除目录
  1317. err = os.Remove("ofile/" + filename + ".xlsx")
  1318. if err != nil {
  1319. logs.Error(lib.FuncName(), err)
  1320. }
  1321. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1322. c.ServeJSON()
  1323. return
  1324. }
  1325. // 批量导出出库单
  1326. func (c *StockController) StockIn_Excel_Batch() {
  1327. // 查询
  1328. T_number_list := c.GetString("T_number_list")
  1329. o := orm.NewOrm()
  1330. StockInDao := Stock.NewStockIn(o)
  1331. StockInProductDao := Stock.NewStockInProduct(o)
  1332. list := strings.Split(strings.Trim(T_number_list, "|"), "|")
  1333. userList, _ := NatsServer.Read_User_List_All()
  1334. Account.Read_User_All_Map(userList)
  1335. filename := fmt.Sprintf("冷链产品入库单(%s)", lib.GetRandstring(6, "0123456789", 0))
  1336. f := excelize.NewFile() // 设置单元格的值
  1337. Style1, _ := f.NewStyle(
  1338. &excelize.Style{
  1339. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  1340. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1341. })
  1342. Style2, _ := f.NewStyle(
  1343. &excelize.Style{
  1344. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1345. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  1346. })
  1347. Style3, _ := f.NewStyle(
  1348. &excelize.Style{
  1349. Font: &excelize.Font{Size: 10, Family: "宋体", Bold: true},
  1350. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1351. Border: []excelize.Border{
  1352. {Type: "left", Color: "000000", Style: 1},
  1353. {Type: "top", Color: "000000", Style: 1},
  1354. {Type: "bottom", Color: "000000", Style: 1},
  1355. {Type: "right", Color: "000000", Style: 1},
  1356. },
  1357. })
  1358. Style4, _ := f.NewStyle(
  1359. &excelize.Style{
  1360. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1361. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1362. Border: []excelize.Border{
  1363. {Type: "left", Color: "000000", Style: 1},
  1364. {Type: "top", Color: "000000", Style: 1},
  1365. {Type: "bottom", Color: "000000", Style: 1},
  1366. {Type: "right", Color: "000000", Style: 1},
  1367. },
  1368. })
  1369. var num int
  1370. for _, T_number := range list {
  1371. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  1372. if err != nil {
  1373. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1374. c.ServeJSON()
  1375. return
  1376. }
  1377. productList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  1378. productList = append(productList, productList...)
  1379. productList = append(productList, productList...)
  1380. productList = append(productList, productList...)
  1381. var pList []Stock.StockInProduct_R
  1382. for _, v := range productList {
  1383. pList = append(pList, Stock.StockInProductToStockInProduct_R(v))
  1384. }
  1385. stockInDetail := Stock.StockInToStockIn_Detail(stockIn, pList)
  1386. T_date, _ := lib.DateStrToTime(stockIn.T_date)
  1387. productList2 := lib.ChunkBy(productList, 10)
  1388. for i, products := range productList2 {
  1389. if len(products) < 10 {
  1390. products = append(products, make([]Stock.StockInProduct, 10-len(products))...)
  1391. }
  1392. row := num*18 + 1
  1393. num += 1
  1394. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1395. f.SetRowStyle("Sheet1", row, row, Style1)
  1396. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("宝智达科技产品进销存统计表"))
  1397. f.SetRowHeight("Sheet1", row, 20)
  1398. row += 1
  1399. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1400. f.SetRowStyle("Sheet1", row, row, Style1)
  1401. title := "硬件入库单"
  1402. if len(productList2) > 1 {
  1403. title += fmt.Sprintf(" %d/%d", i+1, len(productList2))
  1404. }
  1405. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), title)
  1406. f.SetRowHeight("Sheet1", row, 20)
  1407. row += 1
  1408. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1409. f.SetRowStyle("Sheet1", row, row, Style2)
  1410. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("入库单号:%s", stockIn.T_number))
  1411. f.SetRowHeight("Sheet1", row, 20)
  1412. row += 1
  1413. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1414. f.SetRowStyle("Sheet1", row, row, Style2)
  1415. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("项目名称:%s", stockIn.T_remark))
  1416. f.SetRowHeight("Sheet1", row, 20)
  1417. row += 1
  1418. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1419. f.SetRowStyle("Sheet1", row, row, Style2)
  1420. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("入库时间:%s", T_date.Format("2006年01月02日")))
  1421. f.SetRowHeight("Sheet1", row, 20)
  1422. // 这里设置表头
  1423. row += 1
  1424. f.SetRowHeight("Sheet1", row, 20)
  1425. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "序号")
  1426. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), "产品名称")
  1427. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), "型号")
  1428. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), "单位")
  1429. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), "领用数量")
  1430. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), "备注")
  1431. // 设置列宽
  1432. f.SetColWidth("Sheet1", "A", "A", 5)
  1433. f.SetColWidth("Sheet1", "B", "B", 20)
  1434. f.SetColWidth("Sheet1", "C", "C", 12)
  1435. f.SetColWidth("Sheet1", "D", "D", 10)
  1436. f.SetColWidth("Sheet1", "E", "E", 10)
  1437. f.SetColWidth("Sheet1", "F", "F", 15)
  1438. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row), Style3)
  1439. sRow := row
  1440. // 循环写入数据
  1441. for j, v := range products {
  1442. row += 1
  1443. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1444. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), j+1)
  1445. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), product.T_name)
  1446. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), product.T_model)
  1447. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), product.T_spec)
  1448. if v.T_num > 0 {
  1449. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), v.T_num)
  1450. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), stockIn.T_remark)
  1451. }
  1452. }
  1453. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", sRow), fmt.Sprintf("F%d", row), Style4)
  1454. row += 1
  1455. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("C%d", row))
  1456. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("制单:%s", c.User.T_name))
  1457. f.MergeCell("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("F%d", row))
  1458. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("经办人:%s", stockInDetail.T_submit_name))
  1459. f.SetRowStyle("Sheet1", row, row, Style2)
  1460. f.SetRowHeight("Sheet1", row, 20)
  1461. // 空一行
  1462. row += 1
  1463. //f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1464. //f.SetRowStyle("Sheet1", row, row, Style1)
  1465. //f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "")
  1466. // 插入下一页分页符
  1467. f.InsertPageBreak("Sheet1", fmt.Sprintf("A%d", row))
  1468. }
  1469. }
  1470. // 保存文件
  1471. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  1472. fmt.Println(err)
  1473. }
  1474. var url string
  1475. //// 上传 OSS
  1476. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  1477. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  1478. if !is {
  1479. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  1480. c.ServeJSON()
  1481. return
  1482. }
  1483. //删除目录
  1484. err := os.Remove("ofile/" + filename + ".xlsx")
  1485. if err != nil {
  1486. logs.Error(lib.FuncName(), err)
  1487. }
  1488. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1489. c.ServeJSON()
  1490. return
  1491. }
  1492. func (c *StockController) StockOut_List() {
  1493. // 分页参数 初始化
  1494. page, _ := c.GetInt("page")
  1495. if page < 1 {
  1496. page = 1
  1497. }
  1498. page_z, _ := c.GetInt("page_z")
  1499. if page_z < 1 {
  1500. page_z = conf.Page_size
  1501. }
  1502. // 查询
  1503. T_depot_id, _ := c.GetInt("T_depot_id")
  1504. T_contract_number := c.GetString("T_contract_number")
  1505. T_start_date := c.GetString("T_start_date")
  1506. T_end_date := c.GetString("T_end_date")
  1507. T_name := c.GetString("T_name")
  1508. userList, _ := NatsServer.Read_User_List_All()
  1509. Account.Read_User_All_Map(userList)
  1510. Basic.Read_Depot_All_Map()
  1511. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  1512. R_List, R_cnt := StockOutDao.Read_StockOut_List(T_name, T_depot_id, T_contract_number, T_start_date, T_end_date, page, page_z)
  1513. var r_jsons lib.R_JSONS
  1514. r_jsons.Num = R_cnt
  1515. r_jsons.Data = R_List
  1516. r_jsons.Page = page
  1517. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  1518. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1519. c.ServeJSON()
  1520. return
  1521. }
  1522. func (c *StockController) StockOut_List_Product() {
  1523. // 分页参数 初始化
  1524. page, _ := c.GetInt("page")
  1525. if page < 1 {
  1526. page = 1
  1527. }
  1528. page_z, _ := c.GetInt("page_z")
  1529. if page_z < 1 {
  1530. page_z = conf.Page_size
  1531. }
  1532. // 查询
  1533. T_depot_id, _ := c.GetInt("T_depot_id")
  1534. T_name := c.GetString("T_name")
  1535. T_start_date := c.GetString("T_start_date")
  1536. T_end_date := c.GetString("T_end_date")
  1537. T_product_name := c.GetString("T_product_name") // 产品名称
  1538. T_product_model := c.GetString("T_product_model") // 产品型号
  1539. userList, _ := NatsServer.Read_User_List_All()
  1540. Account.Read_User_All_Map(userList)
  1541. Basic.Read_Depot_All_Map()
  1542. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  1543. R_List, R_cnt := StockOutDao.Read_StockOutProduct_List(T_name, T_start_date, T_end_date, T_depot_id, T_product_name, T_product_model, page, page_z)
  1544. var r_jsons lib.R_JSONS
  1545. r_jsons.Num = R_cnt
  1546. r_jsons.Data = R_List
  1547. r_jsons.Page = page
  1548. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  1549. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1550. c.ServeJSON()
  1551. return
  1552. }
  1553. func (c *StockController) StockOut_List_Product_Excel() {
  1554. // 查询
  1555. T_depot_id, _ := c.GetInt("T_depot_id")
  1556. T_name := c.GetString("T_name")
  1557. T_start_date := c.GetString("T_start_date")
  1558. T_end_date := c.GetString("T_end_date")
  1559. T_product_name := c.GetString("T_product_name") // 产品名称
  1560. T_product_model := c.GetString("T_product_model") // 产品型号
  1561. userList, _ := NatsServer.Read_User_List_All()
  1562. Account.Read_User_All_Map(userList)
  1563. Basic.Read_Depot_All_Map()
  1564. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  1565. R_List, _ := StockOutDao.Read_StockOutProduct_List(T_name, T_start_date, T_end_date, T_depot_id, T_product_name, T_product_model, 0, 9999)
  1566. filename := fmt.Sprintf("出库明细单(%s)", lib.GetRandstring(6, "0123456789", 0))
  1567. f := excelize.NewFile() // 设置单元格的值
  1568. Style1, _ := f.NewStyle(
  1569. &excelize.Style{
  1570. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  1571. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1572. })
  1573. Style2, _ := f.NewStyle(
  1574. &excelize.Style{
  1575. Font: &excelize.Font{Bold: true, Size: 11, Family: "宋体"},
  1576. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1577. Border: []excelize.Border{
  1578. {Type: "left", Color: "000000", Style: 1},
  1579. {Type: "top", Color: "000000", Style: 1},
  1580. {Type: "bottom", Color: "000000", Style: 1},
  1581. {Type: "right", Color: "000000", Style: 1},
  1582. },
  1583. })
  1584. Style3, _ := f.NewStyle(
  1585. &excelize.Style{
  1586. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1587. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  1588. Border: []excelize.Border{
  1589. {Type: "left", Color: "000000", Style: 1},
  1590. {Type: "top", Color: "000000", Style: 1},
  1591. {Type: "bottom", Color: "000000", Style: 1},
  1592. {Type: "right", Color: "000000", Style: 1},
  1593. },
  1594. })
  1595. f.MergeCell("Sheet1", "A1", "L1")
  1596. f.SetRowStyle("Sheet1", 1, 1, Style1)
  1597. f.SetCellValue("Sheet1", "A1", fmt.Sprintf("出库明细单"))
  1598. f.SetRowHeight("Sheet1", 1, 30)
  1599. // 这里设置表头
  1600. f.SetCellStyle("Sheet1", "A2", "L2", Style2)
  1601. f.SetRowHeight("Sheet1", 2, 20)
  1602. f.SetCellValue("Sheet1", "A2", "序号")
  1603. f.SetCellValue("Sheet1", "B2", "出库单号")
  1604. f.SetCellValue("Sheet1", "C2", "领取人")
  1605. f.SetCellValue("Sheet1", "D2", "出库日期")
  1606. f.SetCellValue("Sheet1", "E2", "出库仓库")
  1607. f.SetCellValue("Sheet1", "F2", "关联项目")
  1608. f.SetCellValue("Sheet1", "G2", "产品名称")
  1609. f.SetCellValue("Sheet1", "H2", "产品型号")
  1610. f.SetCellValue("Sheet1", "I2", "数量")
  1611. f.SetCellValue("Sheet1", "J2", "是否关联SN")
  1612. f.SetCellValue("Sheet1", "K2", "备注")
  1613. f.SetCellValue("Sheet1", "L2", "SN")
  1614. // 设置列宽
  1615. f.SetColWidth("Sheet1", "A", "A", 10)
  1616. f.SetColWidth("Sheet1", "B", "B", 15)
  1617. f.SetColWidth("Sheet1", "C", "D", 10)
  1618. f.SetColWidth("Sheet1", "D", "D", 10)
  1619. f.SetColWidth("Sheet1", "E", "E", 10)
  1620. f.SetColWidth("Sheet1", "F", "F", 20)
  1621. f.SetColWidth("Sheet1", "G", "G", 20)
  1622. f.SetColWidth("Sheet1", "H", "H", 15)
  1623. f.SetColWidth("Sheet1", "I", "I", 10)
  1624. f.SetColWidth("Sheet1", "J", "J", 12)
  1625. f.SetColWidth("Sheet1", "K", "K", 30)
  1626. f.SetColWidth("Sheet1", "L", "L", 100)
  1627. line := 2
  1628. for _, product := range R_List {
  1629. line += 1
  1630. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), line-2)
  1631. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), product.T_number)
  1632. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), product.T_receive_name)
  1633. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), product.T_date)
  1634. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), product.T_depot_name)
  1635. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), product.T_project)
  1636. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), product.T_product_name)
  1637. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), product.T_product_model)
  1638. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), product.T_num)
  1639. var relation_sn string
  1640. if product.T_product_relation_sn == 1 {
  1641. relation_sn = "是"
  1642. } else {
  1643. relation_sn = "否"
  1644. }
  1645. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), relation_sn)
  1646. f.SetCellValue("Sheet1", fmt.Sprintf("K%d", line), product.T_remark)
  1647. f.SetCellValue("Sheet1", fmt.Sprintf("L%d", line), product.T_relation_sn)
  1648. }
  1649. f.SetCellStyle("Sheet1", "A3", fmt.Sprintf("L%d", line), Style3)
  1650. // 保存文件
  1651. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  1652. fmt.Println(err)
  1653. }
  1654. var url string
  1655. //// 上传 OSS
  1656. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  1657. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  1658. if !is {
  1659. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  1660. c.ServeJSON()
  1661. return
  1662. }
  1663. //删除目录
  1664. err := os.Remove("ofile/" + filename + ".xlsx")
  1665. if err != nil {
  1666. logs.Error(lib.FuncName(), err)
  1667. }
  1668. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1669. c.ServeJSON()
  1670. return
  1671. }
  1672. func (c *StockController) StockOut_Get() {
  1673. // 查询
  1674. T_number := c.GetString("T_number")
  1675. o := orm.NewOrm()
  1676. StockOutDao := Stock.NewStockOut(o)
  1677. StockOutProductDao := Stock.NewStockOutProduct(o)
  1678. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  1679. if err != nil {
  1680. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1681. c.ServeJSON()
  1682. return
  1683. }
  1684. productList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  1685. var pList []Stock.StockOutProduct_R
  1686. for _, v := range productList {
  1687. pList = append(pList, Stock.StockOutProductToStockOutProduct_R(v))
  1688. }
  1689. userList, _ := NatsServer.Read_User_List_All()
  1690. Account.Read_User_All_Map(userList)
  1691. Basic.Read_Depot_All_Map()
  1692. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Stock.StockOutToStockOut_Detail(stockOut, pList)}
  1693. c.ServeJSON()
  1694. return
  1695. }
  1696. func (c *StockController) StockOut_Add() {
  1697. rand_x := 0
  1698. T_number := ""
  1699. o := orm.NewOrm()
  1700. o.Begin()
  1701. StockOutDao := Stock.NewStockOut(o)
  1702. for true {
  1703. T_number = "CK-" + lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", int64(rand_x))
  1704. _, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  1705. if err != nil && err.Error() == orm.ErrNoRows.Error() {
  1706. break
  1707. }
  1708. rand_x += 1
  1709. }
  1710. T_contract_number := c.GetString("T_contract_number")
  1711. T_depot_id, _ := c.GetInt("T_depot_id")
  1712. T_type, _ := c.GetInt("T_type") // 出库类型 1-领料出库 2-销售出库
  1713. T_date := c.GetString("T_date")
  1714. T_receive := c.GetString("T_receive")
  1715. T_product := c.GetString("T_product")
  1716. T_remark := c.GetString("T_remark")
  1717. T_project := c.GetString("T_project")
  1718. T_delivery_type, _ := c.GetInt("T_delivery_type")
  1719. T_signer_unit := c.GetString("T_signer_unit")
  1720. T_signer := c.GetString("T_signer")
  1721. T_signer_phone := c.GetString("T_signer_phone")
  1722. T_signer_date := c.GetString("T_signer_date")
  1723. T_courier_number := c.GetString("T_courier_number")
  1724. date, is := lib.DateStrToTime(T_date)
  1725. if !is {
  1726. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  1727. c.ServeJSON()
  1728. return
  1729. }
  1730. var_ := Stock.StockOut{
  1731. T_number: T_number,
  1732. T_contract_number: T_contract_number,
  1733. T_depot_id: T_depot_id,
  1734. T_type: T_type,
  1735. T_date: T_date,
  1736. T_receive: T_receive,
  1737. T_remark: T_remark,
  1738. T_project: T_project,
  1739. T_submit: c.User.T_uuid,
  1740. // 销售出库
  1741. T_delivery_type: T_delivery_type, // 1-自送 2-自提 3-快递
  1742. T_signer_unit: T_signer_unit,
  1743. T_signer: T_signer,
  1744. T_signer_phone: T_signer_phone,
  1745. T_signer_date: T_signer_date,
  1746. T_courier_number: T_courier_number,
  1747. }
  1748. StockOutProductDao := Stock.NewStockOutProduct(o)
  1749. StockDao := Stock.NewStock(o)
  1750. DeviceDao := Stock.NewDevice(o)
  1751. ContractDao := Contract.NewContract(o)
  1752. ContractProductDao := Contract.NewContractProduct(o)
  1753. _, err := StockOutDao.Add_StockOut(var_)
  1754. if err != nil {
  1755. o.Rollback()
  1756. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
  1757. c.ServeJSON()
  1758. return
  1759. }
  1760. // 1、添加出库单
  1761. contract, _ := ContractDao.Read_Contract_ByT_number(T_contract_number)
  1762. if contract.Id == 0 && T_type == 2 {
  1763. o.Rollback()
  1764. c.Data["json"] = lib.JSONS{Code: 203, Msg: "合同编号错误!"}
  1765. c.ServeJSON()
  1766. return
  1767. }
  1768. if len(T_product) == 0 {
  1769. o.Rollback()
  1770. c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
  1771. c.ServeJSON()
  1772. return
  1773. }
  1774. productList := lib.SplitString(T_product, "|")
  1775. allProductList := []int{}
  1776. for _, v := range productList {
  1777. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  1778. allProductList = append(allProductList, product_id)
  1779. product, _ := Basic.Read_Product_ById(product_id)
  1780. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  1781. T_relation_sn := strings.Split(v, "-")[2]
  1782. if T_relation_sn == "" && product.T_relation_sn == 1 {
  1783. o.Rollback()
  1784. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  1785. c.ServeJSON()
  1786. return
  1787. }
  1788. // 2、更新设备状态为已出库
  1789. if len(T_relation_sn) > 0 {
  1790. snList := strings.Split(T_relation_sn, ",")
  1791. for _, sn := range snList {
  1792. mqtt := Stock.Read_MqttUser(sn)
  1793. device := Stock.Device{
  1794. T_contract_number: T_contract_number,
  1795. T_product_id: product_id,
  1796. T_out_number: T_number,
  1797. T_sn: sn,
  1798. T_iccid: mqtt.Iccid,
  1799. T_imei: mqtt.Imei,
  1800. T_State: 1,
  1801. T_project: T_project,
  1802. CreateTime: date,
  1803. }
  1804. _, err = DeviceDao.AddOrUpdate_Device(device, 1)
  1805. if err != nil {
  1806. o.Rollback()
  1807. c.Data["json"] = lib.JSONS{Code: 202, Msg: "出库失败"}
  1808. c.ServeJSON()
  1809. return
  1810. }
  1811. }
  1812. }
  1813. // 3、添加出库产品清单
  1814. stockOutProduct := Stock.StockOutProduct{
  1815. T_number: T_number,
  1816. T_product_id: product_id,
  1817. T_depot_id: T_depot_id,
  1818. T_num: num, // 出库数量
  1819. T_date: T_date, // 出库数量
  1820. T_relation_sn: T_relation_sn,
  1821. }
  1822. _, err = StockOutProductDao.Add_StockOutProduct(stockOutProduct)
  1823. if err != nil {
  1824. o.Rollback()
  1825. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
  1826. c.ServeJSON()
  1827. return
  1828. }
  1829. // 4、更改合同产品列表清单
  1830. contractProduct, _ := ContractProductDao.Read_ContractProduct_ByT_number_T_product_id(T_contract_number, product_id)
  1831. contractProduct.T_product_out += num
  1832. if contractProduct.T_product_out > contractProduct.T_product_total && T_type == 2 {
  1833. o.Rollback()
  1834. p, _ := Basic.Read_Product_ById(product_id)
  1835. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】出库数量超过预计出库数量!", p.T_name)}
  1836. c.ServeJSON()
  1837. return
  1838. }
  1839. contractProduct.T_State = 2
  1840. if contractProduct.T_product_out == 0 {
  1841. contractProduct.T_State = 1
  1842. }
  1843. if contractProduct.T_product_out == contractProduct.T_product_total {
  1844. contractProduct.T_State = 3
  1845. }
  1846. err = ContractProductDao.Update_ContractProduct(contractProduct, "T_product_out", "T_State")
  1847. if err != nil {
  1848. o.Rollback()
  1849. c.Data["json"] = lib.JSONS{Code: 202, Msg: "更新合同产品清单失败"}
  1850. c.ServeJSON()
  1851. return
  1852. }
  1853. // 5、更新产品库存列表
  1854. _, err = StockDao.AddOrUpdate_Stock(T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num, 1)
  1855. if err != nil {
  1856. o.Rollback()
  1857. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存失败"}
  1858. c.ServeJSON()
  1859. return
  1860. }
  1861. }
  1862. o.Commit()
  1863. o2 := orm.NewOrm()
  1864. o2.Begin()
  1865. ContractDao2 := Contract.NewContract(o2)
  1866. ContractProductDao2 := Contract.NewContractProduct(o2)
  1867. // 5、更新合同出库状态
  1868. var T_out int
  1869. if contract.T_State == 1 {
  1870. T_out = 2
  1871. // 查询合同产品清单是否全部都为已出库
  1872. state := ContractProductDao2.Read_ContractProduct_T_State_List(contract.T_number)
  1873. if state == 1 {
  1874. T_out = 1
  1875. }
  1876. if state == 3 {
  1877. T_out = 3
  1878. }
  1879. }
  1880. if T_out != contract.T_out {
  1881. contract.T_out = T_out
  1882. err = ContractDao2.Update_Contract(contract, "T_out")
  1883. if err != nil {
  1884. o2.Rollback()
  1885. c.Data["json"] = lib.JSONS{Code: 202, Msg: "更新合同出库状态失败"}
  1886. c.ServeJSON()
  1887. return
  1888. }
  1889. }
  1890. o2.Commit()
  1891. StockOut_Edit_StockMonth(T_date, T_depot_id, allProductList)
  1892. NatsServer.AddUserLogs(c.User.T_uuid, "出库", "出库", var_)
  1893. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  1894. c.ServeJSON()
  1895. return
  1896. }
  1897. func (c *StockController) StockOut_Edit_Delivery() {
  1898. T_number := c.GetString("T_number")
  1899. T_remark := c.GetString("T_remark")
  1900. T_project := c.GetString("T_project")
  1901. T_delivery_type, _ := c.GetInt("T_delivery_type")
  1902. T_signer_unit := c.GetString("T_signer_unit")
  1903. T_signer := c.GetString("T_signer")
  1904. T_signer_phone := c.GetString("T_signer_phone")
  1905. T_signer_date := c.GetString("T_signer_date")
  1906. T_courier_number := c.GetString("T_courier_number")
  1907. o := orm.NewOrm()
  1908. StockOutDao := Stock.NewStockOut(o)
  1909. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  1910. if err != nil {
  1911. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库编号错误!"}
  1912. c.ServeJSON()
  1913. return
  1914. }
  1915. if len(T_remark) > 0 {
  1916. stockOut.T_remark = T_remark
  1917. }
  1918. if len(T_project) > 0 {
  1919. stockOut.T_project = T_project
  1920. }
  1921. if T_delivery_type > 0 {
  1922. stockOut.T_delivery_type = T_delivery_type
  1923. }
  1924. if len(T_signer_unit) > 0 {
  1925. stockOut.T_signer_unit = T_signer_unit
  1926. }
  1927. if len(T_signer) > 0 {
  1928. stockOut.T_signer = T_signer
  1929. }
  1930. if len(T_signer_phone) > 0 {
  1931. stockOut.T_signer_phone = T_signer_phone
  1932. }
  1933. if len(T_signer_date) > 0 {
  1934. stockOut.T_signer_date = T_signer_date
  1935. }
  1936. if len(T_courier_number) > 0 {
  1937. stockOut.T_courier_number = T_courier_number
  1938. }
  1939. err = StockOutDao.Update_StockOut(stockOut, "T_remark", "T_delivery_type", "T_signer_unit", "T_signer", "T_signer_phone", "T_signer_date", "T_courier_number", "T_project")
  1940. if err != nil {
  1941. o.Rollback()
  1942. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
  1943. c.ServeJSON()
  1944. return
  1945. }
  1946. NatsServer.AddUserLogs(c.User.T_uuid, "出库", "修改发货单", stockOut)
  1947. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  1948. c.ServeJSON()
  1949. return
  1950. }
  1951. func (c *StockController) StockOut_Edit() {
  1952. T_number := c.GetString("T_number") // 出库单号
  1953. //T_depot_id, _ := c.GetInt("T_depot_id")
  1954. T_date := c.GetString("T_date")
  1955. T_receive := c.GetString("T_receive")
  1956. T_project := c.GetString("T_project")
  1957. T_product := c.GetString("T_product")
  1958. T_remark := c.GetString("T_remark")
  1959. date, is := lib.DateStrToTime(T_date)
  1960. if !is {
  1961. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  1962. c.ServeJSON()
  1963. return
  1964. }
  1965. if date.Year() != time.Now().Year() && date.Month() != time.Now().Month() {
  1966. T_date = ""
  1967. }
  1968. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "修改出库", T_product)
  1969. o := orm.NewOrm()
  1970. o.Begin()
  1971. StockOutDao := Stock.NewStockOut(o)
  1972. StockOutProductDao := Stock.NewStockOutProduct(o)
  1973. StockDao := Stock.NewStock(o)
  1974. DeviceDao := Stock.NewDevice(o)
  1975. IotCardDao := Property.NewIotCard(&o)
  1976. // 查询入库信息
  1977. StockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  1978. T_old_date := StockOut.T_date
  1979. if err != nil {
  1980. o.Rollback()
  1981. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1982. c.ServeJSON()
  1983. return
  1984. }
  1985. // 查询入库产品信息
  1986. productOldList := StockOutProductDao.Read_StockOutProduct_List(StockOut.T_number)
  1987. var oldProductList []dto.StockProduct
  1988. var newProductList []dto.StockProduct
  1989. productOldMap := map[int]dto.StockProduct{}
  1990. allProductListMap := make(map[int]struct{})
  1991. for _, product := range productOldList {
  1992. stockProduct := dto.StockProduct{
  1993. T_product_id: product.T_product_id,
  1994. T_num: product.T_num,
  1995. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  1996. }
  1997. oldProductList = append(oldProductList, stockProduct)
  1998. productOldMap[product.T_product_id] = stockProduct
  1999. if _, ok := allProductListMap[product.T_product_id]; !ok {
  2000. allProductListMap[product.T_product_id] = struct{}{}
  2001. }
  2002. }
  2003. productNewList := lib.SplitString(T_product, "|")
  2004. productNewMap := map[int]dto.StockProduct{}
  2005. for _, v := range productNewList {
  2006. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  2007. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  2008. T_relation_sn := strings.Split(v, "-")[2]
  2009. stockProduct := dto.StockProduct{
  2010. T_product_id: product_id,
  2011. T_num: num,
  2012. T_relation_sn: lib.SplitString(T_relation_sn, ","),
  2013. }
  2014. newProductList = append(newProductList, stockProduct)
  2015. productNewMap[product_id] = stockProduct
  2016. if _, ok := allProductListMap[product_id]; !ok {
  2017. allProductListMap[product_id] = struct{}{}
  2018. }
  2019. }
  2020. // 判断产品列表信息是否相同
  2021. StockProductListIsEqual, needDelete, needAdd, needEdit, snDiff := dto.StockProductListsEqual(oldProductList, newProductList)
  2022. // 两次提交的数据相同,则不用修改
  2023. if !StockProductListIsEqual {
  2024. if len(needDelete) > 0 {
  2025. // 删除入库产品列表
  2026. for _, v := range needDelete {
  2027. product, _ := Basic.Read_Product_ById(v.T_product_id)
  2028. // 删除入库产品列表
  2029. err = StockOutProductDao.Delete_StockOutProduct(StockOut.T_number, StockOut.T_depot_id, v.T_product_id)
  2030. if err != nil {
  2031. o.Rollback()
  2032. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  2033. c.ServeJSON()
  2034. return
  2035. }
  2036. if product.T_relation_sn == 1 {
  2037. // 删除该入库单关联sn
  2038. for _, sn := range v.T_relation_sn {
  2039. err = DeviceDao.Delete_Device_ByT_out_number(StockOut.T_number, sn)
  2040. if err != nil {
  2041. o.Rollback()
  2042. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库设备失败"}
  2043. c.ServeJSON()
  2044. return
  2045. }
  2046. }
  2047. }
  2048. // 增加库存
  2049. _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 2)
  2050. if err != nil {
  2051. o.Rollback()
  2052. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
  2053. c.ServeJSON()
  2054. return
  2055. }
  2056. }
  2057. }
  2058. if len(needAdd) > 0 {
  2059. // 新增入库产品列表
  2060. for _, v := range needAdd {
  2061. product, _ := Basic.Read_Product_ById(v.T_product_id)
  2062. if product.T_relation_sn == 1 && len(v.T_relation_sn) == 0 {
  2063. o.Rollback()
  2064. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  2065. c.ServeJSON()
  2066. return
  2067. }
  2068. // 添加该入库单关联sn
  2069. for _, sn := range v.T_relation_sn {
  2070. mqtt := Stock.Read_MqttUser(sn)
  2071. // 添加设备
  2072. device := Stock.Device{
  2073. T_contract_number: "",
  2074. T_out_number: StockOut.T_number,
  2075. T_product_id: v.T_product_id,
  2076. T_in_number: "",
  2077. T_sn: sn,
  2078. T_iccid: mqtt.Iccid,
  2079. T_imei: mqtt.Imei,
  2080. T_State: 1,
  2081. CreateTime: date,
  2082. UpdateTime: StockOut.UpdateTime,
  2083. }
  2084. _, err = DeviceDao.AddOrUpdate_Device(device, 1)
  2085. if err != nil {
  2086. o.Rollback()
  2087. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  2088. c.ServeJSON()
  2089. return
  2090. }
  2091. // 添加物联网卡
  2092. iotCard := Property.IotCard{
  2093. T_iccid: mqtt.Iccid,
  2094. T_sn: sn,
  2095. T_State: 2, //1-未使用 2-已使用 3-已作废
  2096. T_type: "4G",
  2097. }
  2098. _, err = IotCardDao.AddOrUpdate_IotCard(iotCard)
  2099. if err != nil {
  2100. o.Rollback()
  2101. c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库失败"}
  2102. c.ServeJSON()
  2103. return
  2104. }
  2105. }
  2106. StockOutProduct := Stock.StockOutProduct{
  2107. T_number: T_number,
  2108. T_product_id: v.T_product_id,
  2109. T_depot_id: StockOut.T_depot_id,
  2110. T_num: v.T_num, // 入库数量
  2111. T_date: StockOut.T_date, // 入库日期
  2112. T_relation_sn: strings.Join(v.T_relation_sn, ","),
  2113. }
  2114. _, err = StockOutProductDao.Add_StockOutProduct(StockOutProduct)
  2115. if err != nil {
  2116. o.Rollback()
  2117. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  2118. c.ServeJSON()
  2119. return
  2120. }
  2121. // 减少库存
  2122. _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
  2123. if err != nil {
  2124. o.Rollback()
  2125. c.Data["json"] = lib.JSONS{Code: 203, Msg: "增加库存失败"}
  2126. c.ServeJSON()
  2127. return
  2128. }
  2129. }
  2130. }
  2131. if len(needEdit) > 0 {
  2132. for _, v := range needEdit {
  2133. product, _ := Basic.Read_Product_ById(v.T_product_id)
  2134. StockOutProduct := Stock.StockOutProduct{
  2135. T_number: StockOut.T_number,
  2136. T_product_id: v.T_product_id,
  2137. T_depot_id: StockOut.T_depot_id,
  2138. T_num: productNewMap[v.T_product_id].T_num, // 入库数量
  2139. T_date: StockOut.T_date, // 入库日期
  2140. }
  2141. // 出库数量比之前多,减少库存
  2142. var T_type int
  2143. var T_num int
  2144. if productNewMap[v.T_product_id].T_num > productOldMap[v.T_product_id].T_num {
  2145. // 减少库存
  2146. T_type = 1
  2147. T_num = productNewMap[v.T_product_id].T_num - productOldMap[v.T_product_id].T_num
  2148. } else {
  2149. // 增加库存
  2150. T_type = 2
  2151. T_num = productOldMap[v.T_product_id].T_num - productNewMap[v.T_product_id].T_num
  2152. }
  2153. _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, T_num, T_type)
  2154. if err != nil {
  2155. o.Rollback()
  2156. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  2157. c.ServeJSON()
  2158. return
  2159. }
  2160. // 更新产品库存表
  2161. err = StockOutProductDao.Update_StockOutProduct(StockOutProduct)
  2162. if err != nil {
  2163. o.Rollback()
  2164. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改出库产品信息失败"}
  2165. c.ServeJSON()
  2166. return
  2167. }
  2168. }
  2169. }
  2170. for _, diff := range snDiff {
  2171. product, _ := Basic.Read_Product_ById(diff.T_product_id)
  2172. // 删除减少的sn
  2173. if len(diff.T_delete_relation_sn) > 0 {
  2174. for _, sn := range diff.T_delete_relation_sn {
  2175. err = DeviceDao.Delete_Device_ByT_out_number(StockOut.T_number, sn)
  2176. if err != nil {
  2177. o.Rollback()
  2178. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  2179. c.ServeJSON()
  2180. return
  2181. }
  2182. }
  2183. // 增加库存
  2184. _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, len(diff.T_delete_relation_sn), 2)
  2185. if err != nil {
  2186. o.Rollback()
  2187. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  2188. c.ServeJSON()
  2189. return
  2190. }
  2191. }
  2192. if len(diff.T_add_relation_sn) > 0 {
  2193. for _, sn := range diff.T_add_relation_sn {
  2194. mqtt := Stock.Read_MqttUser(sn)
  2195. // 添加设备
  2196. device := Stock.Device{
  2197. T_contract_number: "",
  2198. T_out_number: StockOut.T_number,
  2199. T_product_id: diff.T_product_id,
  2200. T_in_number: "",
  2201. T_sn: sn,
  2202. T_iccid: mqtt.Iccid,
  2203. T_imei: mqtt.Imei,
  2204. T_State: 1,
  2205. CreateTime: date,
  2206. UpdateTime: StockOut.UpdateTime,
  2207. }
  2208. _, err = DeviceDao.AddOrUpdate_Device(device, 1)
  2209. if err != nil {
  2210. o.Rollback()
  2211. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  2212. c.ServeJSON()
  2213. return
  2214. }
  2215. // 添加物联网卡
  2216. iotCard := Property.IotCard{
  2217. T_iccid: mqtt.Iccid,
  2218. T_sn: sn,
  2219. T_State: 2, //1-未使用 2-已使用 3-已作废
  2220. T_type: "4G",
  2221. }
  2222. _, err = IotCardDao.AddOrUpdate_IotCard(iotCard)
  2223. if err != nil {
  2224. o.Rollback()
  2225. c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库失败"}
  2226. c.ServeJSON()
  2227. return
  2228. }
  2229. }
  2230. // 减少库存
  2231. _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, len(diff.T_add_relation_sn), 1)
  2232. if err != nil {
  2233. o.Rollback()
  2234. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  2235. c.ServeJSON()
  2236. return
  2237. }
  2238. }
  2239. StockOutProduct := Stock.StockOutProduct{
  2240. T_number: StockOut.T_number,
  2241. T_product_id: diff.T_product_id,
  2242. T_depot_id: StockOut.T_depot_id,
  2243. T_num: productNewMap[diff.T_product_id].T_num, // 入库数量
  2244. T_date: StockOut.T_date, // 入库日期
  2245. T_relation_sn: strings.Join(productNewMap[diff.T_product_id].T_relation_sn, ","),
  2246. }
  2247. // 更新产品库存表
  2248. err = StockOutProductDao.Update_StockOutProduct(StockOutProduct)
  2249. if err != nil {
  2250. o.Rollback()
  2251. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改出库产品信息失败"}
  2252. c.ServeJSON()
  2253. return
  2254. }
  2255. }
  2256. }
  2257. if len(T_remark) > 0 {
  2258. StockOut.T_remark = T_remark
  2259. }
  2260. if len(T_receive) > 0 {
  2261. StockOut.T_receive = T_receive
  2262. }
  2263. if len(T_project) > 0 {
  2264. StockOut.T_project = T_project
  2265. }
  2266. if len(T_date) > 0 {
  2267. StockOut.T_date = T_date
  2268. }
  2269. err = StockOutDao.Update_StockOut(StockOut, "T_remark", "T_receive", "T_project", "T_date")
  2270. if err != nil {
  2271. o.Rollback()
  2272. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库失败"}
  2273. c.ServeJSON()
  2274. return
  2275. }
  2276. o.Commit()
  2277. if len(T_date) > 0 && T_old_date != T_date {
  2278. // 修改出库产品日期
  2279. StockOutProductDao.Update_StockOutProduct_T_date(StockOut.T_number, T_date)
  2280. DeviceDao.Update_Device_CreateTimeByT_out_number(StockOut.T_number, T_date)
  2281. }
  2282. if len(T_project) > 0 {
  2283. DeviceDao.Update_Device_ByT_out_number_T_project(StockOut.T_number, T_project)
  2284. }
  2285. // 更新月份统计表
  2286. allProductList := []int{}
  2287. for i, _ := range allProductListMap {
  2288. allProductList = append(allProductList, i)
  2289. }
  2290. StockOut_Edit_StockMonth(StockOut.T_date, StockOut.T_depot_id, allProductList)
  2291. NatsServer.AddUserLogs(c.User.T_uuid, "出库", "修改", StockOut)
  2292. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  2293. c.ServeJSON()
  2294. return
  2295. }
  2296. // 删除入库
  2297. func (c *StockController) StockOut_Del() {
  2298. T_number := c.GetString("T_number") // 入库单号
  2299. o := orm.NewOrm()
  2300. o.Begin()
  2301. StockOutDao := Stock.NewStockOut(o)
  2302. StockOutProductDao := Stock.NewStockOutProduct(o)
  2303. StockDao := Stock.NewStock(o)
  2304. DeviceDao := Stock.NewDevice(o)
  2305. // 查询入库信息
  2306. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  2307. if err != nil {
  2308. o.Rollback()
  2309. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  2310. c.ServeJSON()
  2311. return
  2312. }
  2313. // 查询出库产品信息
  2314. productOldList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  2315. var oldProductList []dto.StockProduct
  2316. allProductList := []int{}
  2317. for _, product := range productOldList {
  2318. oldProductList = append(oldProductList, dto.StockProduct{
  2319. T_product_id: product.T_product_id,
  2320. T_num: product.T_num,
  2321. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  2322. })
  2323. allProductList = append(allProductList, product.T_product_id)
  2324. }
  2325. // 删除入库产品列表
  2326. for _, v := range oldProductList {
  2327. product, _ := Basic.Read_Product_ById(v.T_product_id)
  2328. // 删除入库产品列表
  2329. err = StockOutProductDao.Delete_StockOutProduct(stockOut.T_number, stockOut.T_depot_id, v.T_product_id)
  2330. if err != nil {
  2331. o.Rollback()
  2332. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品出库信息失败!"}
  2333. c.ServeJSON()
  2334. return
  2335. }
  2336. if product.T_relation_sn == 1 {
  2337. // 删除该入库单关联sn
  2338. for _, sn := range v.T_relation_sn {
  2339. err = DeviceDao.Delete_Device_ByT_out_number(stockOut.T_number, sn)
  2340. if err != nil {
  2341. o.Rollback()
  2342. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除出库设备失败"}
  2343. c.ServeJSON()
  2344. return
  2345. }
  2346. }
  2347. }
  2348. // 增加库存
  2349. _, err = StockDao.AddOrUpdate_Stock(stockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 2)
  2350. if err != nil {
  2351. o.Rollback()
  2352. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
  2353. c.ServeJSON()
  2354. return
  2355. }
  2356. }
  2357. err = StockOutDao.Delete_StockOut(stockOut)
  2358. if err != nil {
  2359. o.Rollback()
  2360. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除出库失败"}
  2361. c.ServeJSON()
  2362. return
  2363. }
  2364. o.Commit()
  2365. // 更新月份统计表
  2366. StockOut_Edit_StockMonth(stockOut.T_date, stockOut.T_depot_id, allProductList)
  2367. NatsServer.AddUserLogs(c.User.T_uuid, "出库", "删除", stockOut)
  2368. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  2369. c.ServeJSON()
  2370. return
  2371. }
  2372. // 导出出库单
  2373. func (c *StockController) StockOut_Excel() {
  2374. // 查询
  2375. T_number := c.GetString("T_number")
  2376. o := orm.NewOrm()
  2377. StockOutDao := Stock.NewStockOut(o)
  2378. StockOutProductDao := Stock.NewStockOutProduct(o)
  2379. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  2380. if err != nil {
  2381. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  2382. c.ServeJSON()
  2383. return
  2384. }
  2385. productList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  2386. //productList = append(productList, productList...)
  2387. //productList = append(productList, productList...)
  2388. //productList = append(productList, productList...)
  2389. var pList []Stock.StockOutProduct_R
  2390. for _, v := range productList {
  2391. pList = append(pList, Stock.StockOutProductToStockOutProduct_R(v))
  2392. }
  2393. userList, _ := NatsServer.Read_User_List_All()
  2394. Account.Read_User_All_Map(userList)
  2395. stockOutDetail := Stock.StockOutToStockOut_Detail(stockOut, pList)
  2396. T_date, _ := lib.DateStrToTime(stockOut.T_date)
  2397. filename := fmt.Sprintf("冷链产品出库单(%s)", lib.GetRandstring(6, "0123456789", 0))
  2398. f := excelize.NewFile() // 设置单元格的值
  2399. Style1, _ := f.NewStyle(
  2400. &excelize.Style{
  2401. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  2402. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  2403. })
  2404. Style2, _ := f.NewStyle(
  2405. &excelize.Style{
  2406. Font: &excelize.Font{Size: 11, Family: "宋体"},
  2407. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  2408. })
  2409. Style3, _ := f.NewStyle(
  2410. &excelize.Style{
  2411. Font: &excelize.Font{Size: 10, Family: "宋体", Bold: true},
  2412. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  2413. Border: []excelize.Border{
  2414. {Type: "left", Color: "000000", Style: 1},
  2415. {Type: "top", Color: "000000", Style: 1},
  2416. {Type: "bottom", Color: "000000", Style: 1},
  2417. {Type: "right", Color: "000000", Style: 1},
  2418. },
  2419. })
  2420. Style4, _ := f.NewStyle(
  2421. &excelize.Style{
  2422. Font: &excelize.Font{Size: 11, Family: "宋体"},
  2423. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  2424. Border: []excelize.Border{
  2425. {Type: "left", Color: "000000", Style: 1},
  2426. {Type: "top", Color: "000000", Style: 1},
  2427. {Type: "bottom", Color: "000000", Style: 1},
  2428. {Type: "right", Color: "000000", Style: 1},
  2429. },
  2430. })
  2431. productList2 := lib.ChunkBy(productList, 10)
  2432. for i, products := range productList2 {
  2433. if len(products) < 10 {
  2434. products = append(products, make([]Stock.StockOutProduct, 10-len(products))...)
  2435. }
  2436. row := i*17 + 1
  2437. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2438. f.SetRowStyle("Sheet1", row, row, Style1)
  2439. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("宝智达科技产品进销存统计表"))
  2440. f.SetRowHeight("Sheet1", row, 20)
  2441. row += 1
  2442. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2443. f.SetRowStyle("Sheet1", row, row, Style1)
  2444. title := "硬件出库单"
  2445. if len(productList2) > 1 {
  2446. title += fmt.Sprintf(" %d/%d", i+1, len(productList2))
  2447. }
  2448. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), title)
  2449. f.SetRowHeight("Sheet1", row, 20)
  2450. row += 1
  2451. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2452. f.SetRowStyle("Sheet1", row, row, Style2)
  2453. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("出库单号:%s", stockOut.T_number))
  2454. f.SetRowHeight("Sheet1", row, 20)
  2455. row += 1
  2456. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2457. f.SetRowStyle("Sheet1", row, row, Style2)
  2458. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("项目名称:%s", stockOut.T_project))
  2459. f.SetRowHeight("Sheet1", row, 20)
  2460. row += 1
  2461. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2462. f.SetRowStyle("Sheet1", row, row, Style2)
  2463. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("领用时间:%s", T_date.Format("2006年01月02日")))
  2464. f.SetRowHeight("Sheet1", row, 20)
  2465. // 这里设置表头
  2466. row += 1
  2467. f.SetRowHeight("Sheet1", row, 20)
  2468. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "序号")
  2469. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), "产品名称")
  2470. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), "型号")
  2471. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), "单位")
  2472. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), "领用数量")
  2473. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), "备注")
  2474. // 设置列宽
  2475. f.SetColWidth("Sheet1", "A", "A", 5)
  2476. f.SetColWidth("Sheet1", "B", "B", 20)
  2477. f.SetColWidth("Sheet1", "C", "C", 12)
  2478. f.SetColWidth("Sheet1", "D", "D", 10)
  2479. f.SetColWidth("Sheet1", "E", "E", 10)
  2480. f.SetColWidth("Sheet1", "F", "F", 15)
  2481. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row), Style3)
  2482. sRow := row
  2483. // 循环写入数据
  2484. for j, v := range products {
  2485. row += 1
  2486. product, _ := Basic.Read_Product_ById(v.T_product_id)
  2487. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), j+1)
  2488. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), product.T_name)
  2489. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), product.T_model)
  2490. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), product.T_spec)
  2491. if v.T_num > 0 {
  2492. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), v.T_num)
  2493. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), stockOut.T_remark)
  2494. }
  2495. }
  2496. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", sRow), fmt.Sprintf("F%d", row), Style4)
  2497. row += 1
  2498. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("C%d", row))
  2499. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("制单:%s", c.User.T_name))
  2500. f.MergeCell("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("F%d", row))
  2501. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("领用人:%s", stockOutDetail.T_receive_name))
  2502. f.SetRowStyle("Sheet1", row, row, Style2)
  2503. f.SetRowHeight("Sheet1", row, 20)
  2504. }
  2505. // 保存文件
  2506. if err = f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  2507. fmt.Println(err)
  2508. }
  2509. var url string
  2510. //// 上传 OSS
  2511. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  2512. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  2513. if !is {
  2514. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  2515. c.ServeJSON()
  2516. return
  2517. }
  2518. //删除目录
  2519. err = os.Remove("ofile/" + filename + ".xlsx")
  2520. if err != nil {
  2521. logs.Error(lib.FuncName(), err)
  2522. }
  2523. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  2524. c.ServeJSON()
  2525. return
  2526. }
  2527. func (c *StockController) StockOut_Excel_Batch() {
  2528. // 查询
  2529. T_number_list := c.GetString("T_number_list")
  2530. o := orm.NewOrm()
  2531. StockOutDao := Stock.NewStockOut(o)
  2532. StockOutProductDao := Stock.NewStockOutProduct(o)
  2533. list := strings.Split(strings.Trim(T_number_list, "|"), "|")
  2534. userList, _ := NatsServer.Read_User_List_All()
  2535. Account.Read_User_All_Map(userList)
  2536. filename := fmt.Sprintf("冷链产品出库单(%s)", lib.GetRandstring(6, "0123456789", 0))
  2537. f := excelize.NewFile() // 设置单元格的值
  2538. Style1, _ := f.NewStyle(
  2539. &excelize.Style{
  2540. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  2541. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  2542. })
  2543. Style2, _ := f.NewStyle(
  2544. &excelize.Style{
  2545. Font: &excelize.Font{Size: 11, Family: "宋体"},
  2546. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  2547. })
  2548. Style3, _ := f.NewStyle(
  2549. &excelize.Style{
  2550. Font: &excelize.Font{Size: 10, Family: "宋体", Bold: true},
  2551. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  2552. Border: []excelize.Border{
  2553. {Type: "left", Color: "000000", Style: 1},
  2554. {Type: "top", Color: "000000", Style: 1},
  2555. {Type: "bottom", Color: "000000", Style: 1},
  2556. {Type: "right", Color: "000000", Style: 1},
  2557. },
  2558. })
  2559. Style4, _ := f.NewStyle(
  2560. &excelize.Style{
  2561. Font: &excelize.Font{Size: 11, Family: "宋体"},
  2562. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  2563. Border: []excelize.Border{
  2564. {Type: "left", Color: "000000", Style: 1},
  2565. {Type: "top", Color: "000000", Style: 1},
  2566. {Type: "bottom", Color: "000000", Style: 1},
  2567. {Type: "right", Color: "000000", Style: 1},
  2568. },
  2569. })
  2570. var num int
  2571. for _, T_number := range list {
  2572. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  2573. if err != nil {
  2574. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  2575. c.ServeJSON()
  2576. return
  2577. }
  2578. productList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  2579. productList = append(productList, productList...)
  2580. productList = append(productList, productList...)
  2581. productList = append(productList, productList...)
  2582. productList = append(productList, productList...)
  2583. var pList []Stock.StockOutProduct_R
  2584. for _, v := range productList {
  2585. pList = append(pList, Stock.StockOutProductToStockOutProduct_R(v))
  2586. }
  2587. stockOutDetail := Stock.StockOutToStockOut_Detail(stockOut, pList)
  2588. T_date, _ := lib.DateStrToTime(stockOut.T_date)
  2589. productList2 := lib.ChunkBy(productList, 10)
  2590. for i, products := range productList2 {
  2591. if len(products) < 10 {
  2592. products = append(products, make([]Stock.StockOutProduct, 10-len(products))...)
  2593. }
  2594. row := num*18 + 1
  2595. num += 1
  2596. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2597. f.SetRowStyle("Sheet1", row, row, Style1)
  2598. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("宝智达科技产品进销存统计表"))
  2599. f.SetRowHeight("Sheet1", row, 20)
  2600. row += 1
  2601. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2602. f.SetRowStyle("Sheet1", row, row, Style1)
  2603. title := "硬件出库单"
  2604. if len(productList2) > 1 {
  2605. title += fmt.Sprintf(" %d/%d", i+1, len(productList2))
  2606. }
  2607. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), title)
  2608. f.SetRowHeight("Sheet1", row, 20)
  2609. row += 1
  2610. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2611. f.SetRowStyle("Sheet1", row, row, Style2)
  2612. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("出库单号:%s", stockOut.T_number))
  2613. f.SetRowHeight("Sheet1", row, 20)
  2614. row += 1
  2615. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2616. f.SetRowStyle("Sheet1", row, row, Style2)
  2617. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("项目名称:%s", stockOut.T_project))
  2618. f.SetRowHeight("Sheet1", row, 20)
  2619. row += 1
  2620. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2621. f.SetRowStyle("Sheet1", row, row, Style2)
  2622. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("领用时间:%s", T_date.Format("2006年01月02日")))
  2623. f.SetRowHeight("Sheet1", row, 20)
  2624. // 这里设置表头
  2625. row += 1
  2626. f.SetRowHeight("Sheet1", row, 20)
  2627. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "序号")
  2628. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), "产品名称")
  2629. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), "型号")
  2630. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), "单位")
  2631. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), "领用数量")
  2632. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), "备注")
  2633. // 设置列宽
  2634. f.SetColWidth("Sheet1", "A", "A", 5)
  2635. f.SetColWidth("Sheet1", "B", "B", 20)
  2636. f.SetColWidth("Sheet1", "C", "C", 12)
  2637. f.SetColWidth("Sheet1", "D", "D", 10)
  2638. f.SetColWidth("Sheet1", "E", "E", 10)
  2639. f.SetColWidth("Sheet1", "F", "F", 15)
  2640. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row), Style3)
  2641. sRow := row
  2642. // 循环写入数据
  2643. for j, v := range products {
  2644. row += 1
  2645. product, _ := Basic.Read_Product_ById(v.T_product_id)
  2646. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), j+1)
  2647. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), product.T_name)
  2648. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), product.T_model)
  2649. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), product.T_spec)
  2650. if v.T_num > 0 {
  2651. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), v.T_num)
  2652. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), stockOut.T_remark)
  2653. }
  2654. }
  2655. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", sRow), fmt.Sprintf("F%d", row), Style4)
  2656. row += 1
  2657. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("C%d", row))
  2658. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("制单:%s", c.User.T_name))
  2659. f.MergeCell("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("F%d", row))
  2660. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("领用人:%s", stockOutDetail.T_receive_name))
  2661. f.SetRowStyle("Sheet1", row, row, Style2)
  2662. f.SetRowHeight("Sheet1", row, 20)
  2663. // 空一行
  2664. row += 1
  2665. //f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2666. //f.SetRowStyle("Sheet1", row, row, Style1)
  2667. //f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "")
  2668. // 插入下一页分页符
  2669. f.InsertPageBreak("Sheet1", fmt.Sprintf("A%d", row))
  2670. }
  2671. }
  2672. // 保存文件
  2673. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  2674. fmt.Println(err)
  2675. }
  2676. var url string
  2677. //// 上传 OSS
  2678. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  2679. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  2680. if !is {
  2681. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  2682. c.ServeJSON()
  2683. return
  2684. }
  2685. //删除目录
  2686. err := os.Remove("ofile/" + filename + ".xlsx")
  2687. if err != nil {
  2688. logs.Error(lib.FuncName(), err)
  2689. }
  2690. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  2691. c.ServeJSON()
  2692. return
  2693. }
  2694. func StockOut_Edit_StockMonth(T_date string, T_depot_id int, allProductList []int) error {
  2695. date, _ := lib.DateStrToTime(T_date)
  2696. //if date.Year() == time.Now().Year() && date.Month() == time.Now().Month() {
  2697. // // 修改本月数据不同步库存
  2698. // return nil
  2699. //}
  2700. //T_month := date.Format("2006-01")
  2701. months := generateMonthList(date)
  2702. o := orm.NewOrm()
  2703. StockOutDao := Stock.NewStockOut(o)
  2704. StockMonthDao := Stock.NewStockMonth(o)
  2705. StockOutProductDao := Stock.NewStockOutProduct(o)
  2706. StockInProductDao := Stock.NewStockInProduct(o)
  2707. for _, T_product_id := range allProductList {
  2708. for _, T_month := range months {
  2709. // 获取当前产品本月出入库数量
  2710. lastMonth := StockMonthDao.Read_LastMonth_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
  2711. stockMonth := StockMonthDao.Read_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
  2712. if stockMonth.Id > 0 {
  2713. // 修改
  2714. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(T_depot_id, T_product_id, T_month)
  2715. if len(lastMonth) > 0 {
  2716. stockMonth.T_beginning = lastMonth[0].T_ending
  2717. } else {
  2718. stockMonth.T_beginning = 0
  2719. }
  2720. // 期末库存 = 期初库存+入库-出库
  2721. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  2722. err := StockMonthDao.Update_StockMonth(stockMonth, "T_out", "T_beginning", "T_ending")
  2723. if err != nil {
  2724. return err
  2725. }
  2726. } else {
  2727. product, _ := Basic.Read_Product_ById(T_product_id)
  2728. // 添加库存记录
  2729. stockMonth = Stock.StockMonth{
  2730. T_depot_id: T_depot_id,
  2731. T_product_id: T_product_id,
  2732. T_product_class: product.T_class,
  2733. T_month: T_month,
  2734. }
  2735. // 获取当前产品本月出入库数量
  2736. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(T_depot_id, T_product_id, T_month)
  2737. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(T_depot_id, T_product_id, T_month)
  2738. if len(lastMonth) > 0 {
  2739. stockMonth.T_beginning = lastMonth[0].T_ending
  2740. } else {
  2741. stockMonth.T_beginning = 0
  2742. }
  2743. // 期末库存 = 期初库存+入库-出库
  2744. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  2745. stockMonth.T_project = StockOutDao.Read_StockOut_T_contract_number(T_depot_id, T_product_id, T_month)
  2746. _, err := StockMonthDao.Add_StockMonth(stockMonth)
  2747. if err != nil {
  2748. return err
  2749. }
  2750. }
  2751. }
  2752. }
  2753. return nil
  2754. }
  2755. func Cron_StockMonth() {
  2756. //创建一个定时任务对象
  2757. c := cron.New(cron.WithSeconds())
  2758. //给对象增加定时任务
  2759. // @monthly 每月运行一次,每月第一天午夜 0 0 0 1 * *
  2760. //c.AddFunc("0 */1 * * * ?", Cron_StockMonth_Add)
  2761. c.AddFunc("@monthly", Cron_StockMonth_Add)
  2762. //启动定时任务
  2763. c.Start()
  2764. defer c.Stop()
  2765. //查询语句,阻塞,让main函数不退出,保持程序运行
  2766. select {}
  2767. }
  2768. // 保存每月入库出库明细
  2769. func Cron_StockMonth_Add() {
  2770. T_month := time.Now().Format("2006-01")
  2771. //T_month := time.Now().AddDate(0, -1, 0).Format("2006-01")
  2772. logs.Info("开始统计" + T_month + "库存明细数据")
  2773. o := orm.NewOrm()
  2774. StockDao := Stock.NewStock(o)
  2775. StockOutDao := Stock.NewStockOut(o)
  2776. StockMonthDao := Stock.NewStockMonth(o)
  2777. StockOutProductDao := Stock.NewStockOutProduct(o)
  2778. StockInProductDao := Stock.NewStockInProduct(o)
  2779. stockList, _ := StockDao.Read_Stock_List(0, 0, "", "", 0, 9999)
  2780. for _, stock := range stockList {
  2781. lastMonth := StockMonthDao.Read_LastMonth_StockMonth_ByT_depot_id_T_product_id(stock.T_depot_id, stock.T_product_id, T_month)
  2782. stockMonth := StockMonthDao.Read_StockMonth_ByT_depot_id_T_product_id(stock.T_depot_id, stock.T_product_id, T_month)
  2783. if stockMonth.Id > 0 {
  2784. // 修改
  2785. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(stock.T_depot_id, stock.T_product_id, T_month)
  2786. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(stock.T_depot_id, stock.T_product_id, T_month)
  2787. if len(lastMonth) > 0 {
  2788. stockMonth.T_beginning = lastMonth[0].T_ending
  2789. } else {
  2790. stockMonth.T_beginning = 0
  2791. }
  2792. // 期末库存 = 期初库存+入库-出库
  2793. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  2794. err := StockMonthDao.Update_StockMonth(stockMonth, "T_in", "T_out", "T_beginning", "T_ending")
  2795. if err != nil {
  2796. NatsServer.AddSysLogs("库存明细统计", fmt.Sprintf("库存明细统计失败%s:%d:%d", T_month, stock.T_depot_id, stock.T_product_id), stockMonth)
  2797. }
  2798. } else {
  2799. stockMonth = Stock.StockMonth{
  2800. T_depot_id: stock.T_depot_id,
  2801. T_product_id: stock.T_product_id,
  2802. T_product_class: stock.T_product_class,
  2803. T_month: T_month,
  2804. }
  2805. // 获取当前产品本月出入库数量
  2806. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(stock.T_depot_id, stock.T_product_id, T_month)
  2807. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(stock.T_depot_id, stock.T_product_id, T_month)
  2808. if len(lastMonth) > 0 {
  2809. stockMonth.T_beginning = lastMonth[0].T_ending
  2810. } else {
  2811. stockMonth.T_beginning = 0
  2812. }
  2813. // 期末库存 = 期初库存+入库-出库
  2814. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  2815. stockMonth.T_project = StockOutDao.Read_StockOut_T_contract_number(stock.T_depot_id, stock.T_product_id, T_month)
  2816. _, err := StockMonthDao.Add_StockMonth(stockMonth)
  2817. if err != nil {
  2818. NatsServer.AddSysLogs("库存明细统计", fmt.Sprintf("库存明细统计失败%s:%d:%d", T_month, stock.T_depot_id, stock.T_product_id), stockMonth)
  2819. }
  2820. }
  2821. }
  2822. }
  2823. func generateMonthList(startMonth time.Time) []string {
  2824. var months []string
  2825. endMonth := time.Now().AddDate(0, 0, 0)
  2826. for month := startMonth; month.Before(endMonth); month = month.AddDate(0, 1, 0) {
  2827. fmt.Println("=========================", month.Format("2006-01"))
  2828. months = append(months, month.Format("2006-01"))
  2829. }
  2830. return months
  2831. }