Stock.go 136 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319
  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. "errors"
  14. "fmt"
  15. "math"
  16. "os"
  17. "strconv"
  18. "strings"
  19. "time"
  20. "github.com/beego/beego/v2/adapter/orm"
  21. beego "github.com/beego/beego/v2/server/web"
  22. "github.com/robfig/cron/v3"
  23. "github.com/xuri/excelize/v2"
  24. natslibs "gogs.baozhida.cn/zoie/ERP_libs/Nats"
  25. userlibs "gogs.baozhida.cn/zoie/ERP_libs/User"
  26. "gogs.baozhida.cn/zoie/ERP_libs/lib"
  27. )
  28. type StockController struct {
  29. beego.Controller
  30. User userlibs.User
  31. }
  32. func (c *StockController) Prepare() {
  33. c.User = *Account.User_r
  34. }
  35. func (c *StockController) Device_List() {
  36. // 分页参数 初始化
  37. page, _ := c.GetInt("page")
  38. if page < 1 {
  39. page = 1
  40. }
  41. page_z, _ := c.GetInt("page_z")
  42. if page_z < 1 {
  43. page_z = conf.Page_size
  44. }
  45. T_state, _ := c.GetInt("T_state")
  46. T_name := c.GetString("T_name")
  47. T_product_name := c.GetString("T_product_name") // 产品名称
  48. T_product_model := c.GetString("T_product_model") // 产品型号
  49. DeviceDao := Stock.NewDevice(orm.NewOrm())
  50. R_List, R_cnt := DeviceDao.Read_Device_List(T_name, T_product_name, T_product_model, T_state, page, page_z)
  51. var r_jsons lib.R_JSONS
  52. r_jsons.Num = R_cnt
  53. r_jsons.Data = R_List
  54. r_jsons.Page = page
  55. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  56. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  57. c.ServeJSON()
  58. return
  59. }
  60. func (c *StockController) Device_Excel() {
  61. T_state, _ := c.GetInt("T_state")
  62. T_name := c.GetString("T_name")
  63. T_product_name := c.GetString("T_product_name") // 产品名称
  64. T_product_model := c.GetString("T_product_model") // 产品型号
  65. DeviceDao := Stock.NewDevice(orm.NewOrm())
  66. R_List, _ := DeviceDao.Read_Device_List(T_name, T_product_name, T_product_model, T_state, 0, 9999)
  67. // 生成Excel文件名
  68. filename := fmt.Sprintf("设备列表(%s)", lib.GetRandstring(6, "0123456789", 0))
  69. // 创建Excel文件
  70. f := excelize.NewFile()
  71. // 设置样式
  72. Style1, _ := f.NewStyle(
  73. &excelize.Style{
  74. Font: &excelize.Font{Bold: true, Size: 14, Family: "宋体"},
  75. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  76. })
  77. Style2, _ := f.NewStyle(
  78. &excelize.Style{
  79. Font: &excelize.Font{Bold: true, Size: 10, Family: "宋体"},
  80. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  81. Border: []excelize.Border{
  82. {Type: "left", Color: "000000", Style: 1},
  83. {Type: "top", Color: "000000", Style: 1},
  84. {Type: "bottom", Color: "000000", Style: 1},
  85. {Type: "right", Color: "000000", Style: 1},
  86. },
  87. })
  88. Style3, _ := f.NewStyle(
  89. &excelize.Style{
  90. Font: &excelize.Font{Size: 10, Family: "宋体"},
  91. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  92. Border: []excelize.Border{
  93. {Type: "left", Color: "000000", Style: 1},
  94. {Type: "top", Color: "000000", Style: 1},
  95. {Type: "bottom", Color: "000000", Style: 1},
  96. {Type: "right", Color: "000000", Style: 1},
  97. },
  98. })
  99. // 设置工作表名称
  100. f.SetSheetName("Sheet1", "设备列表")
  101. // 设置标题
  102. f.MergeCell("设备列表", "A1", "M1")
  103. f.SetRowStyle("设备列表", 1, 1, Style1)
  104. f.SetCellValue("设备列表", "A1", "宝智达科技设备列表")
  105. f.SetRowHeight("设备列表", 1, 30)
  106. // 设置表头
  107. f.SetCellStyle("设备列表", "A2", "M2", Style2)
  108. f.SetRowHeight("设备列表", 2, 20)
  109. // 设置表头内容
  110. f.SetCellValue("设备列表", "A2", "序号")
  111. f.SetCellValue("设备列表", "B2", "合同编号")
  112. f.SetCellValue("设备列表", "C2", "出库单号")
  113. f.SetCellValue("设备列表", "D2", "关联项目")
  114. f.SetCellValue("设备列表", "E2", "产品名称")
  115. f.SetCellValue("设备列表", "F2", "产品分类")
  116. f.SetCellValue("设备列表", "G2", "产品型号")
  117. f.SetCellValue("设备列表", "H2", "产品规格")
  118. f.SetCellValue("设备列表", "I2", "设备SN")
  119. f.SetCellValue("设备列表", "J2", "模组imei")
  120. f.SetCellValue("设备列表", "K2", "物联网卡号")
  121. f.SetCellValue("设备列表", "L2", "设备编号")
  122. f.SetCellValue("设备列表", "M2", "状态")
  123. // 设置列宽
  124. f.SetColWidth("设备列表", "A", "A", 8) // 序号
  125. f.SetColWidth("设备列表", "B", "B", 15) // 合同编号
  126. f.SetColWidth("设备列表", "C", "C", 15) // 出库单号
  127. f.SetColWidth("设备列表", "D", "D", 20) // 关联项目
  128. f.SetColWidth("设备列表", "E", "E", 20) // 产品名称
  129. f.SetColWidth("设备列表", "F", "F", 15) // 产品分类
  130. f.SetColWidth("设备列表", "G", "G", 15) // 产品型号
  131. f.SetColWidth("设备列表", "H", "H", 15) // 产品规格
  132. f.SetColWidth("设备列表", "I", "I", 20) // 设备SN
  133. f.SetColWidth("设备列表", "J", "J", 15) // 模组imei
  134. f.SetColWidth("设备列表", "K", "K", 20) // 物联网卡号
  135. f.SetColWidth("设备列表", "L", "L", 18) // 设备编号
  136. f.SetColWidth("设备列表", "M", "M", 10) // 状态
  137. // 填充数据
  138. for i, device := range R_List {
  139. row := i + 3 // 从第3行开始填充数据
  140. // 状态转换
  141. stateText := "未出库"
  142. if device.T_State == 1 {
  143. stateText = "已出库"
  144. }
  145. f.SetCellStyle("设备列表", fmt.Sprintf("A%d", row), fmt.Sprintf("M%d", row), Style3)
  146. //f.SetRowHeight("设备列表", row, 20)
  147. f.SetCellValue("设备列表", fmt.Sprintf("A%d", row), i+1) // 序号
  148. f.SetCellValue("设备列表", fmt.Sprintf("B%d", row), device.T_contract_number) // 合同编号
  149. f.SetCellValue("设备列表", fmt.Sprintf("C%d", row), device.T_out_number) // 出库单号
  150. f.SetCellValue("设备列表", fmt.Sprintf("D%d", row), device.T_project) // 关联项目
  151. f.SetCellValue("设备列表", fmt.Sprintf("E%d", row), device.T_product_name) // 产品名称
  152. f.SetCellValue("设备列表", fmt.Sprintf("F%d", row), device.T_product_class_name) // 产品分类
  153. f.SetCellValue("设备列表", fmt.Sprintf("G%d", row), device.T_product_model) // 产品型号
  154. f.SetCellValue("设备列表", fmt.Sprintf("H%d", row), device.T_product_spec) // 产品规格
  155. f.SetCellValue("设备列表", fmt.Sprintf("I%d", row), device.T_sn) // 设备SN
  156. f.SetCellValue("设备列表", fmt.Sprintf("J%d", row), device.T_imei) // 模组imei
  157. f.SetCellValue("设备列表", fmt.Sprintf("K%d", row), device.T_iccid) // 物联网卡号
  158. f.SetCellValue("设备列表", fmt.Sprintf("L%d", row), device.T_device_number) // 设备编号
  159. f.SetCellValue("设备列表", fmt.Sprintf("M%d", row), stateText) // 状态
  160. }
  161. // 保存文件
  162. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  163. fmt.Println(err)
  164. }
  165. var url string
  166. //// 上传 OSS
  167. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  168. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  169. if !is {
  170. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  171. c.ServeJSON()
  172. return
  173. }
  174. //删除目录
  175. err := os.Remove("ofile/" + filename + ".xlsx")
  176. if err != nil {
  177. logs.Error(lib.FuncName(), err)
  178. }
  179. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  180. c.ServeJSON()
  181. return
  182. }
  183. func (c *StockController) Device_Check() {
  184. T_sn := c.GetString("T_sn")
  185. T_type, _ := c.GetInt("T_type") //1-出库 2-入库
  186. T_product_id, _ := c.GetInt("T_product_id")
  187. DeviceDao := Stock.NewDevice(orm.NewOrm())
  188. device, err := DeviceDao.Read_Device_ByT_sn(T_sn)
  189. if err != nil && err.Error() != orm.ErrNoRows.Error() {
  190. c.Data["json"] = lib.JSONS{Code: 202, Msg: "请稍后重试!"}
  191. c.ServeJSON()
  192. return
  193. }
  194. // T_State 1-已出库 2-未出库/入库
  195. if len(device.T_in_number) > 0 && device.T_State == 2 && T_type == 2 {
  196. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】已入库,请勿重复提交!", T_sn)}
  197. c.ServeJSON()
  198. return
  199. }
  200. if T_type == 2 {
  201. mqtt := Stock.Read_MqttUser(T_sn)
  202. if len(mqtt.Username) == 0 {
  203. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】未授权,请先授权!", T_sn)}
  204. c.ServeJSON()
  205. return
  206. }
  207. }
  208. if len(device.T_out_number) > 0 && device.T_State == 1 && T_type == 1 {
  209. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】已出库,请勿重复提交!", T_sn)}
  210. c.ServeJSON()
  211. return
  212. }
  213. if T_type == 1 {
  214. if device.Id == 0 {
  215. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】未入库,请先入库!", T_sn)}
  216. c.ServeJSON()
  217. return
  218. }
  219. if device.T_product_id != T_product_id {
  220. c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库产品与出库产品不一致,请检查!"}
  221. c.ServeJSON()
  222. return
  223. }
  224. }
  225. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  226. c.ServeJSON()
  227. return
  228. }
  229. func (c *StockController) Device_Take_Stock() {
  230. T_product_name := c.GetString("T_product_name") // 产品名称
  231. T_product_model := c.GetString("T_product_model") // 产品型号
  232. T_sn_list := c.GetString("T_sn_list") // sn列表
  233. DeviceDao := Stock.NewDevice(orm.NewOrm())
  234. // 获取库存中的所有设备(如果有产品名称和型号过滤)
  235. R_List, _ := DeviceDao.Read_Device_List("", T_product_name, T_product_model, 0, 0, 9999)
  236. // 创建设备SN到设备信息的映射,用于快速查找
  237. deviceMap := make(map[string]Stock.Device_R)
  238. // 保存在库设备列表
  239. var inStockDevices []Stock.Device_R
  240. for _, device := range R_List {
  241. deviceMap[device.T_sn] = device
  242. // 收集所有在库设备(状态为2)
  243. if device.T_State == 2 {
  244. inStockDevices = append(inStockDevices, device)
  245. }
  246. }
  247. // 创建结果数据结构
  248. type DeviceCheckResult struct {
  249. T_sn string `json:"t_sn"` // 设备SN
  250. T_exists bool `json:"t_exists"` // 是否存在于库存
  251. T_state int `json:"t_state"` // 设备状态:1-已出库 2-未出库/入库
  252. T_state_desc string `json:"t_state_desc"` // 状态描述
  253. T_product_id int `json:"t_product_id"` // 产品ID
  254. T_product_name string `json:"t_product_name"` // 产品名称
  255. T_product_model string `json:"t_product_model"` // 产品型号
  256. T_iccid string `json:"t_iccid"` // SIM卡号
  257. T_imei string `json:"t_imei"` // 模组IMEI
  258. T_in_number string `json:"t_in_number"` // 入库编号
  259. T_out_number string `json:"t_out_number"` // 出库编号
  260. T_from_input bool `json:"t_from_input"` // 是否来自输入列表
  261. }
  262. // 处理每个输入的SN
  263. var results []DeviceCheckResult
  264. var processedSNs = make(map[string]bool) // 用于跟踪已处理的SN
  265. // 解析输入的SN列表(如果有)
  266. var snList []string
  267. if len(T_sn_list) > 0 {
  268. snList = lib.SplitString(T_sn_list, ",")
  269. for _, sn := range snList {
  270. sn = strings.TrimSpace(sn)
  271. if len(sn) == 0 {
  272. continue
  273. }
  274. result := DeviceCheckResult{
  275. T_sn: sn,
  276. T_exists: false,
  277. T_from_input: true, // 标记为来自输入列表
  278. }
  279. // 检查设备是否存在于库存
  280. if device, exists := deviceMap[sn]; exists {
  281. result.T_exists = true
  282. result.T_state = device.T_State
  283. result.T_product_id = device.T_product_id
  284. result.T_product_name = device.T_product_name
  285. result.T_product_model = device.T_product_model
  286. result.T_iccid = device.T_iccid
  287. result.T_imei = device.T_imei
  288. result.T_in_number = device.T_in_number
  289. result.T_out_number = device.T_out_number
  290. // 设置状态描述
  291. if device.T_State == 1 {
  292. result.T_state_desc = "已出库"
  293. } else if device.T_State == 2 {
  294. result.T_state_desc = "在库"
  295. } else {
  296. result.T_state_desc = "未知状态"
  297. }
  298. } else {
  299. // 设备不存在于库存
  300. result.T_state_desc = "未入库"
  301. }
  302. results = append(results, result)
  303. processedSNs[sn] = true
  304. }
  305. }
  306. // 添加所有在库设备(如果还没有处理过)
  307. for _, device := range inStockDevices {
  308. // 如果这个SN已经处理过(来自输入列表),则跳过
  309. if _, processed := processedSNs[device.T_sn]; processed {
  310. continue
  311. }
  312. result := DeviceCheckResult{
  313. T_sn: device.T_sn,
  314. T_exists: true,
  315. T_state: device.T_State,
  316. T_state_desc: "在库",
  317. T_product_id: device.T_product_id,
  318. T_product_name: device.T_product_name,
  319. T_product_model: device.T_product_model,
  320. T_iccid: device.T_iccid,
  321. T_imei: device.T_imei,
  322. T_in_number: device.T_in_number,
  323. T_out_number: device.T_out_number,
  324. T_from_input: false, // 标记为不是来自输入列表
  325. }
  326. results = append(results, result)
  327. }
  328. // 统计结果
  329. totalInputCount := 0
  330. existsCount := 0
  331. inStockCount := 0
  332. outStockCount := 0
  333. notExistsCount := 0
  334. for _, result := range results {
  335. if result.T_from_input {
  336. totalInputCount++
  337. if result.T_exists {
  338. existsCount++
  339. if result.T_state == 2 {
  340. inStockCount++
  341. } else if result.T_state == 1 {
  342. outStockCount++
  343. }
  344. } else {
  345. notExistsCount++
  346. }
  347. }
  348. }
  349. // 组装返回数据
  350. r_jsons := map[string]interface{}{
  351. "devices": results,
  352. "stats": map[string]interface{}{
  353. "total_input": totalInputCount,
  354. "exists": existsCount,
  355. "in_stock": inStockCount,
  356. "out_stock": outStockCount,
  357. "not_exists": notExistsCount,
  358. "total_in_stock": len(inStockDevices),
  359. },
  360. }
  361. c.Data["json"] = lib.JSONS{Code: 200, Msg: "盘点完成", Data: r_jsons}
  362. c.ServeJSON()
  363. return
  364. }
  365. func (c *StockController) Stock_List() {
  366. // 分页参数 初始化
  367. page, _ := c.GetInt("page")
  368. if page < 1 {
  369. page = 1
  370. }
  371. page_z, _ := c.GetInt("page_z")
  372. if page_z < 1 {
  373. page_z = conf.Page_size
  374. }
  375. // 查询
  376. T_depot_id, _ := c.GetInt("T_depot_id")
  377. // 查询
  378. T_product_name := c.GetString("T_product_name")
  379. T_product_model := c.GetString("T_product_model")
  380. T_product_class, _ := c.GetInt("T_product_class")
  381. userList, _ := NatsServer.Read_User_List_All()
  382. Account.Read_User_All_Map(userList)
  383. Basic.Read_Depot_All_Map()
  384. StockDao := Stock.NewStock(orm.NewOrm())
  385. R_List, R_cnt := StockDao.Read_Stock_List(T_depot_id, T_product_class, T_product_name, T_product_model, page, page_z)
  386. var r_jsons lib.R_JSONS
  387. r_jsons.Num = R_cnt
  388. r_jsons.Data = R_List
  389. r_jsons.Page = page
  390. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  391. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  392. c.ServeJSON()
  393. return
  394. }
  395. // 修改库存排序
  396. func (c *StockController) Stock_Edit_Sort() {
  397. T_id, _ := c.GetInt("T_id")
  398. T_sort, _ := c.GetInt("T_sort")
  399. StockDao := Stock.NewStock(orm.NewOrm())
  400. Stock_r, err := StockDao.Read_Stock_ById(T_id)
  401. if err != nil {
  402. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_id Err!"}
  403. c.ServeJSON()
  404. return
  405. }
  406. Stock_r.T_sort = T_sort
  407. if err = StockDao.Update_Stock(Stock_r, "T_sort"); err != nil {
  408. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  409. c.ServeJSON()
  410. return
  411. }
  412. NatsServer.AddUserLogs(c.User.T_uuid, "库存", "修改", Stock_r)
  413. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  414. c.ServeJSON()
  415. return
  416. }
  417. func (c *StockController) Stock_Detail_List() {
  418. // 查询
  419. T_depot_id, _ := c.GetInt("T_depot_id")
  420. // 查询
  421. T_product_id, _ := c.GetInt("T_product_id")
  422. T_start_date := c.GetString("T_start_date")
  423. T_end_date := c.GetString("T_end_date")
  424. now := time.Now()
  425. if len(T_start_date) == 0 {
  426. T_start_date = time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local).Format("2006-01")
  427. } else {
  428. T_start_date_t, _ := lib.DateStrToTime(T_start_date)
  429. T_start_date = T_start_date_t.Format("2006-01")
  430. }
  431. if len(T_end_date) == 0 {
  432. T_end_date = now.Format("2006-01")
  433. } else {
  434. T_end_date_t, _ := lib.DateStrToTime(T_end_date)
  435. T_end_date = T_end_date_t.Format("2006-01")
  436. }
  437. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  438. StockMonthDao := Stock.NewStockMonth(orm.NewOrm())
  439. R_List := StockMonthDao.Read_StockMonth_List(T_depot_id, T_product_id, 0, T_start_date, T_end_date)
  440. for i := 0; i < len(R_List); i++ {
  441. 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), "|")
  442. }
  443. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: R_List}
  444. c.ServeJSON()
  445. return
  446. }
  447. func (c *StockController) Stock_Detail_Excel() {
  448. // 查询
  449. T_depot_id, _ := c.GetInt("T_depot_id")
  450. T_product_id, _ := c.GetInt("T_product_id")
  451. // 查询
  452. T_start_date := c.GetString("T_start_date")
  453. T_end_date := c.GetString("T_end_date")
  454. now := time.Now()
  455. if len(T_start_date) == 0 {
  456. T_start_date = time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local).Format("2006-01")
  457. } else {
  458. T_start_date_t, _ := lib.DateStrToTime(T_start_date)
  459. T_start_date = T_start_date_t.Format("2006-01")
  460. }
  461. if len(T_end_date) == 0 {
  462. T_end_date = now.Format("2006-01")
  463. } else {
  464. T_end_date_t, _ := lib.DateStrToTime(T_end_date)
  465. T_end_date = T_end_date_t.Format("2006-01")
  466. }
  467. var class_List []Basic.ProductClass_R
  468. filename := fmt.Sprintf("进销存(%s)", lib.GetRandstring(6, "0123456789", 0))
  469. if T_product_id > 0 {
  470. product, _ := Basic.Read_Product_ById(T_product_id)
  471. filename = fmt.Sprintf("进销存-%s明细(%s)", product.T_name, lib.GetRandstring(6, "0123456789", 0))
  472. class, _ := Basic.Read_ProductClass_ById(product.T_class)
  473. class_List = append(class_List, Basic.ProductClassToProductClass_R(class))
  474. } else {
  475. class_List, _ = Basic.Read_ProductClass_List("", 0, 0, 9999)
  476. }
  477. f := excelize.NewFile() // 设置单元格的值
  478. Style1, _ := f.NewStyle(
  479. &excelize.Style{
  480. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  481. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  482. })
  483. Style2, _ := f.NewStyle(
  484. &excelize.Style{
  485. Font: &excelize.Font{Bold: true, Size: 10, Family: "宋体"},
  486. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  487. Border: []excelize.Border{
  488. {Type: "left", Color: "000000", Style: 1},
  489. {Type: "top", Color: "000000", Style: 1},
  490. {Type: "bottom", Color: "000000", Style: 1},
  491. {Type: "right", Color: "000000", Style: 1},
  492. },
  493. })
  494. Style3, _ := f.NewStyle(
  495. &excelize.Style{
  496. Font: &excelize.Font{Bold: true, Size: 10, Family: "宋体", Color: "FF0000"},
  497. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  498. Border: []excelize.Border{
  499. {Type: "left", Color: "000000", Style: 1},
  500. {Type: "top", Color: "000000", Style: 1},
  501. {Type: "bottom", Color: "000000", Style: 1},
  502. {Type: "right", Color: "000000", Style: 1},
  503. },
  504. })
  505. Style4, _ := f.NewStyle(
  506. &excelize.Style{
  507. Font: &excelize.Font{Size: 10, Family: "宋体"},
  508. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  509. Border: []excelize.Border{
  510. {Type: "left", Color: "000000", Style: 1},
  511. {Type: "top", Color: "000000", Style: 1},
  512. {Type: "bottom", Color: "000000", Style: 1},
  513. {Type: "right", Color: "000000", Style: 1},
  514. },
  515. })
  516. StockMonthDao := Stock.NewStockMonth(orm.NewOrm())
  517. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  518. var j = 0
  519. for _, r := range class_List {
  520. StockList := StockMonthDao.Read_StockMonth_List(T_depot_id, T_product_id, r.Id, T_start_date, T_end_date)
  521. if len(StockList) == 0 {
  522. continue
  523. }
  524. if j == 0 {
  525. f.SetSheetName("Sheet1", r.T_name)
  526. } else {
  527. f.NewSheet(r.T_name)
  528. }
  529. j += 1
  530. f.MergeCell(r.T_name, "A1", "J1")
  531. f.SetRowStyle(r.T_name, 1, 1, Style1)
  532. f.SetCellValue(r.T_name, "A1", fmt.Sprintf("宝智达科技产品进销存统计表"))
  533. f.SetRowHeight(r.T_name, 1, 30)
  534. // 这里设置表头
  535. f.SetCellStyle(r.T_name, "A2", "J2", Style2)
  536. f.SetRowHeight(r.T_name, 2, 20)
  537. f.SetCellValue(r.T_name, "A2", "序号")
  538. f.SetCellValue(r.T_name, "B2", "产品名称")
  539. f.SetCellValue(r.T_name, "C2", "型号")
  540. f.SetCellValue(r.T_name, "D2", "规格")
  541. f.SetCellValue(r.T_name, "E2", "月份")
  542. f.SetCellValue(r.T_name, "F2", "期初库存")
  543. f.SetCellValue(r.T_name, "G2", "当月入库")
  544. f.SetCellValue(r.T_name, "H2", "当月出库")
  545. f.SetCellValue(r.T_name, "I2", "期末库存")
  546. f.SetCellValue(r.T_name, "J2", "出库项目")
  547. //f.SetCellValue(r.T_name, "K2", "备注")
  548. // 设置列宽
  549. f.SetColWidth(r.T_name, "A", "A", 10)
  550. f.SetColWidth(r.T_name, "B", "B", 10)
  551. f.SetColWidth(r.T_name, "C", "D", 10)
  552. f.SetColWidth(r.T_name, "D", "D", 10)
  553. f.SetColWidth(r.T_name, "E", "E", 10)
  554. f.SetColWidth(r.T_name, "F", "F", 10)
  555. f.SetColWidth(r.T_name, "G", "G", 10)
  556. f.SetColWidth(r.T_name, "H", "H", 10)
  557. f.SetColWidth(r.T_name, "I", "I", 10)
  558. f.SetColWidth(r.T_name, "J", "J", 70)
  559. //f.SetColWidth(r.T_name, "K", "K", 10)
  560. line := 2
  561. // 循环写入数据
  562. for _, v := range StockList {
  563. line++
  564. product, _ := Basic.Read_Product_ById(v.T_product_id)
  565. f.SetCellValue(r.T_name, fmt.Sprintf("A%d", line), line-2)
  566. f.SetCellValue(r.T_name, fmt.Sprintf("B%d", line), product.T_name)
  567. f.SetCellValue(r.T_name, fmt.Sprintf("C%d", line), product.T_model)
  568. f.SetCellValue(r.T_name, fmt.Sprintf("D%d", line), product.T_spec)
  569. f.SetCellValue(r.T_name, fmt.Sprintf("E%d", line), v.T_month)
  570. f.SetCellValue(r.T_name, fmt.Sprintf("F%d", line), v.T_beginning)
  571. f.SetCellValue(r.T_name, fmt.Sprintf("G%d", line), v.T_in)
  572. f.SetCellValue(r.T_name, fmt.Sprintf("H%d", line), v.T_out)
  573. f.SetCellValue(r.T_name, fmt.Sprintf("I%d", line), v.T_ending)
  574. if product.T_early_warning > v.T_ending {
  575. f.SetCellStyle(r.T_name, fmt.Sprintf("I%d", line), fmt.Sprintf("I%d", line), Style3)
  576. } else {
  577. f.SetCellStyle(r.T_name, fmt.Sprintf("I%d", line), fmt.Sprintf("I%d", line), Style4)
  578. }
  579. T_project := StockOutDao.Read_StockOut_T_project(T_depot_id, product.Id, v.T_month)
  580. //f.SetCellValue(r.T_name, fmt.Sprintf("J%d", line), v.T_project)
  581. f.SetCellValue(r.T_name, fmt.Sprintf("J%d", line), strings.Join(T_project, "\r\n"))
  582. }
  583. f.SetCellStyle(r.T_name, "A2", fmt.Sprintf("H%d", line), Style4)
  584. Style5, _ := f.NewStyle(
  585. &excelize.Style{
  586. Font: &excelize.Font{Size: 10, Family: "宋体"},
  587. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  588. Border: []excelize.Border{
  589. {Type: "left", Color: "000000", Style: 1},
  590. {Type: "top", Color: "000000", Style: 1},
  591. {Type: "bottom", Color: "000000", Style: 1},
  592. {Type: "right", Color: "000000", Style: 1},
  593. },
  594. })
  595. f.SetCellStyle(r.T_name, "J2", fmt.Sprintf("J%d", line), Style5)
  596. }
  597. // 保存文件
  598. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  599. fmt.Println(err)
  600. }
  601. var url string
  602. //// 上传 OSS
  603. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  604. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  605. if !is {
  606. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  607. c.ServeJSON()
  608. return
  609. }
  610. //删除目录
  611. err := os.Remove("ofile/" + filename + ".xlsx")
  612. if err != nil {
  613. logs.Error(lib.FuncName(), err)
  614. }
  615. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  616. c.ServeJSON()
  617. return
  618. }
  619. func (c *StockController) StockIn_List() {
  620. // 分页参数 初始化
  621. page, _ := c.GetInt("page")
  622. if page < 1 {
  623. page = 1
  624. }
  625. page_z, _ := c.GetInt("page_z")
  626. if page_z < 1 {
  627. page_z = conf.Page_size
  628. }
  629. // 查询
  630. T_depot_id, _ := c.GetInt("T_depot_id")
  631. T_start_date := c.GetString("T_start_date")
  632. T_end_date := c.GetString("T_end_date")
  633. T_name := c.GetString("T_name")
  634. T_batch_number := c.GetString("T_batch_number")
  635. userList, _ := NatsServer.Read_User_List_All()
  636. Account.Read_User_All_Map(userList)
  637. Basic.Read_Depot_All_Map()
  638. StockInDao := Stock.NewStockIn(orm.NewOrm())
  639. R_List, R_cnt := StockInDao.Read_StockIn_List(T_batch_number, T_name, T_depot_id, T_start_date, T_end_date, page, page_z)
  640. var r_jsons lib.R_JSONS
  641. r_jsons.Num = R_cnt
  642. r_jsons.Data = R_List
  643. r_jsons.Page = page
  644. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  645. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  646. c.ServeJSON()
  647. return
  648. }
  649. func (c *StockController) StockIn_List_Product() {
  650. // 分页参数 初始化
  651. page, _ := c.GetInt("page")
  652. if page < 1 {
  653. page = 1
  654. }
  655. page_z, _ := c.GetInt("page_z")
  656. if page_z < 1 {
  657. page_z = conf.Page_size
  658. }
  659. // 查询
  660. T_depot_id, _ := c.GetInt("T_depot_id")
  661. T_name := c.GetString("T_name")
  662. T_start_date := c.GetString("T_start_date")
  663. T_end_date := c.GetString("T_end_date")
  664. T_product_name := c.GetString("T_product_name") // 产品名称
  665. T_product_model := c.GetString("T_product_model") // 产品型号
  666. userList, _ := NatsServer.Read_User_List_All()
  667. Account.Read_User_All_Map(userList)
  668. Basic.Read_Depot_All_Map()
  669. StockInDao := Stock.NewStockIn(orm.NewOrm())
  670. 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)
  671. var r_jsons lib.R_JSONS
  672. r_jsons.Num = R_cnt
  673. r_jsons.Data = R_List
  674. r_jsons.Page = page
  675. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  676. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  677. c.ServeJSON()
  678. return
  679. }
  680. func (c *StockController) StockIn_List_Product_Excel() {
  681. // 查询
  682. T_depot_id, _ := c.GetInt("T_depot_id")
  683. T_name := c.GetString("T_name")
  684. T_start_date := c.GetString("T_start_date")
  685. T_end_date := c.GetString("T_end_date")
  686. T_product_name := c.GetString("T_product_name") // 产品名称
  687. T_product_model := c.GetString("T_product_model") // 产品型号
  688. userList, _ := NatsServer.Read_User_List_All()
  689. Account.Read_User_All_Map(userList)
  690. Basic.Read_Depot_All_Map()
  691. StockInDao := Stock.NewStockIn(orm.NewOrm())
  692. 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)
  693. filename := fmt.Sprintf("入库明细单(%s)", lib.GetRandstring(6, "0123456789", 0))
  694. f := excelize.NewFile() // 设置单元格的值
  695. Style1, _ := f.NewStyle(
  696. &excelize.Style{
  697. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  698. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  699. })
  700. Style2, _ := f.NewStyle(
  701. &excelize.Style{
  702. Font: &excelize.Font{Bold: true, Size: 11, Family: "宋体"},
  703. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  704. Border: []excelize.Border{
  705. {Type: "left", Color: "000000", Style: 1},
  706. {Type: "top", Color: "000000", Style: 1},
  707. {Type: "bottom", Color: "000000", Style: 1},
  708. {Type: "right", Color: "000000", Style: 1},
  709. },
  710. })
  711. Style3, _ := f.NewStyle(
  712. &excelize.Style{
  713. Font: &excelize.Font{Size: 11, Family: "宋体"},
  714. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  715. Border: []excelize.Border{
  716. {Type: "left", Color: "000000", Style: 1},
  717. {Type: "top", Color: "000000", Style: 1},
  718. {Type: "bottom", Color: "000000", Style: 1},
  719. {Type: "right", Color: "000000", Style: 1},
  720. },
  721. })
  722. f.MergeCell("Sheet1", "A1", "L1")
  723. f.SetRowStyle("Sheet1", 1, 1, Style1)
  724. f.SetCellValue("Sheet1", "A1", fmt.Sprintf("入库明细单"))
  725. f.SetRowHeight("Sheet1", 1, 30)
  726. // 这里设置表头
  727. f.SetCellStyle("Sheet1", "A2", "L2", Style2)
  728. f.SetRowHeight("Sheet1", 2, 20)
  729. f.SetCellValue("Sheet1", "A2", "序号")
  730. f.SetCellValue("Sheet1", "B2", "入库单号")
  731. f.SetCellValue("Sheet1", "C2", "经办人")
  732. f.SetCellValue("Sheet1", "D2", "入库日期")
  733. f.SetCellValue("Sheet1", "E2", "入库仓库")
  734. f.SetCellValue("Sheet1", "F2", "入库类型")
  735. f.SetCellValue("Sheet1", "G2", "产品名称")
  736. f.SetCellValue("Sheet1", "H2", "产品型号")
  737. f.SetCellValue("Sheet1", "I2", "数量")
  738. f.SetCellValue("Sheet1", "J2", "是否关联SN")
  739. f.SetCellValue("Sheet1", "K2", "备注")
  740. f.SetCellValue("Sheet1", "L2", "SN")
  741. // 设置列宽
  742. f.SetColWidth("Sheet1", "A", "A", 10)
  743. f.SetColWidth("Sheet1", "B", "B", 15)
  744. f.SetColWidth("Sheet1", "C", "D", 10)
  745. f.SetColWidth("Sheet1", "D", "D", 10)
  746. f.SetColWidth("Sheet1", "E", "E", 10)
  747. f.SetColWidth("Sheet1", "F", "F", 10)
  748. f.SetColWidth("Sheet1", "G", "G", 20)
  749. f.SetColWidth("Sheet1", "H", "H", 15)
  750. f.SetColWidth("Sheet1", "I", "I", 10)
  751. f.SetColWidth("Sheet1", "J", "J", 12)
  752. f.SetColWidth("Sheet1", "K", "K", 30)
  753. f.SetColWidth("Sheet1", "L", "L", 100)
  754. line := 2
  755. for _, product := range R_List {
  756. line += 1
  757. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), line-2)
  758. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), product.T_number)
  759. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), product.T_submit_name)
  760. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), product.T_date)
  761. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), product.T_depot_name)
  762. var type_str string
  763. if product.T_type == 1 {
  764. type_str = "入库"
  765. } else {
  766. type_str = "退库"
  767. }
  768. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), type_str)
  769. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), product.T_product_name)
  770. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), product.T_product_model)
  771. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), product.T_num)
  772. var relation_sn string
  773. if product.T_product_relation_sn == 1 {
  774. relation_sn = "是"
  775. } else {
  776. relation_sn = "否"
  777. }
  778. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), relation_sn)
  779. f.SetCellValue("Sheet1", fmt.Sprintf("K%d", line), product.T_remark)
  780. f.SetCellValue("Sheet1", fmt.Sprintf("L%d", line), product.T_relation_sn)
  781. }
  782. f.SetCellStyle("Sheet1", "A3", fmt.Sprintf("L%d", line), Style3)
  783. // 保存文件
  784. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  785. fmt.Println(err)
  786. }
  787. var url string
  788. //// 上传 OSS
  789. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  790. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  791. if !is {
  792. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  793. c.ServeJSON()
  794. return
  795. }
  796. //删除目录
  797. err := os.Remove("ofile/" + filename + ".xlsx")
  798. if err != nil {
  799. logs.Error(lib.FuncName(), err)
  800. }
  801. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  802. c.ServeJSON()
  803. return
  804. }
  805. func (c *StockController) StockIn_Get() {
  806. // 查询
  807. T_number := c.GetString("T_number")
  808. o := orm.NewOrm()
  809. StockInDao := Stock.NewStockIn(o)
  810. StockInProductDao := Stock.NewStockInProduct(o)
  811. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  812. if err != nil {
  813. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  814. c.ServeJSON()
  815. return
  816. }
  817. productList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  818. var pList []Stock.StockInProduct_R
  819. for _, v := range productList {
  820. pList = append(pList, Stock.StockInProductToStockInProduct_R(v))
  821. }
  822. userList, _ := NatsServer.Read_User_List_All()
  823. Account.Read_User_All_Map(userList)
  824. Basic.Read_Depot_All_Map()
  825. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Stock.StockInToStockIn_Detail(stockIn, pList)}
  826. c.ServeJSON()
  827. return
  828. }
  829. func (c *StockController) StockIn_generate_number() {
  830. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  831. rand_x := 0
  832. T_number := ""
  833. for true {
  834. T_number = "RK-" + lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", int64(rand_x))
  835. _, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  836. if err != nil && err.Error() == orm.ErrNoRows.Error() {
  837. break
  838. }
  839. rand_x += 1
  840. }
  841. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  842. c.ServeJSON()
  843. return
  844. }
  845. func (c *StockController) StockIn_Add() {
  846. o := orm.NewOrm()
  847. o.Begin()
  848. StockInDao := Stock.NewStockIn(o)
  849. T_number := c.GetString("T_number")
  850. // 检查前端传入的T_number是否已提交过
  851. _, err := StockInDao.Read_StockIn_ByT_number(T_number)
  852. if err == nil {
  853. // 入库编号已存在,不需要做任何处理
  854. o.Rollback()
  855. c.Data["json"] = lib.JSONS{Code: 200, Msg: "入库编号已存在,无需重复提交!", Data: T_number}
  856. c.ServeJSON()
  857. return
  858. }
  859. if err != nil && err.Error() != orm.ErrNoRows.Error() {
  860. // 数据库查询错误
  861. o.Rollback()
  862. c.Data["json"] = lib.JSONS{Code: 203, Msg: "查询入库编号失败"}
  863. c.ServeJSON()
  864. return
  865. }
  866. T_depot_id, _ := c.GetInt("T_depot_id")
  867. T_type, _ := c.GetInt("T_type") // 1-入库 2-退库
  868. T_date := c.GetString("T_date")
  869. T_product := c.GetString("T_product")
  870. T_remark := c.GetString("T_remark")
  871. T_project := c.GetString("T_project")
  872. T_return_user := c.GetString("T_return_user")
  873. T_batch_number := c.GetString("T_batch_number")
  874. date, is := lib.DateStrToTime(T_date)
  875. if !is {
  876. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  877. c.ServeJSON()
  878. return
  879. }
  880. if T_type == 2 && len(T_project) == 0 {
  881. c.Data["json"] = lib.JSONS{Code: 202, Msg: "退库时,项目不能为空!"}
  882. c.ServeJSON()
  883. return
  884. }
  885. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "入库", T_product)
  886. var_ := Stock.StockIn{
  887. T_number: T_number,
  888. T_depot_id: T_depot_id,
  889. T_type: T_type,
  890. T_date: T_date,
  891. T_remark: T_remark,
  892. T_submit: c.User.T_uuid,
  893. T_project: T_project,
  894. T_return_user: T_return_user,
  895. T_batch_number: T_batch_number,
  896. }
  897. StockInProductDao := Stock.NewStockInProduct(o)
  898. StockDao := Stock.NewStock(o)
  899. DeviceDao := Stock.NewDevice(o)
  900. IotCardDao := Property.NewIotCard(&o)
  901. _, err = StockInDao.Add_StockIn(var_)
  902. if err != nil {
  903. o.Rollback()
  904. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  905. c.ServeJSON()
  906. return
  907. }
  908. if len(T_product) == 0 {
  909. o.Rollback()
  910. c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
  911. c.ServeJSON()
  912. return
  913. }
  914. productList := lib.SplitString(T_product, "|")
  915. allProductList := []int{}
  916. for _, v := range productList {
  917. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  918. allProductList = append(allProductList, product_id)
  919. product, _ := Basic.Read_Product_ById(product_id)
  920. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  921. T_relation_sn := strings.Split(v, "-")[2]
  922. if T_relation_sn == "" && product.T_relation_sn == 1 {
  923. o.Rollback()
  924. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  925. c.ServeJSON()
  926. return
  927. }
  928. if len(T_relation_sn) > 0 {
  929. snList := strings.Split(strings.Trim(T_relation_sn, ","), ",")
  930. num = len(snList)
  931. for _, sn := range snList {
  932. mqtt := Stock.Read_MqttUser(sn)
  933. // 添加设备
  934. device := Stock.Device{
  935. T_contract_number: "",
  936. T_out_number: "",
  937. T_product_id: product_id,
  938. T_in_number: T_number,
  939. T_sn: sn,
  940. T_iccid: mqtt.Iccid,
  941. T_imei: mqtt.Imei,
  942. T_State: 2,
  943. T_batch_number: T_batch_number,
  944. CreateTime: date,
  945. }
  946. _, err = DeviceDao.AddOrUpdate_Device(device, 2)
  947. if err != nil {
  948. o.Rollback()
  949. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  950. c.ServeJSON()
  951. return
  952. }
  953. // 添加物联网卡
  954. iotCard := Property.IotCard{
  955. T_iccid: mqtt.Iccid,
  956. T_sn: sn,
  957. T_State: 2, //1-未使用 2-已使用 3-已作废
  958. T_type: "4G",
  959. }
  960. _, err = IotCardDao.AddOrUpdate_IotCard(iotCard)
  961. if err != nil {
  962. o.Rollback()
  963. c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库失败"}
  964. c.ServeJSON()
  965. return
  966. }
  967. }
  968. }
  969. stockInProduct := Stock.StockInProduct{
  970. T_number: T_number,
  971. T_product_id: product_id,
  972. T_depot_id: T_depot_id,
  973. T_num: num, // 入库数量
  974. T_date: T_date, // 入库日期
  975. T_relation_sn: T_relation_sn,
  976. T_batch_number: T_batch_number,
  977. }
  978. _, err = StockInProductDao.Add_StockInProduct(stockInProduct)
  979. if err != nil {
  980. o.Rollback()
  981. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  982. c.ServeJSON()
  983. return
  984. }
  985. _, err = StockDao.AddOrUpdate_Stock(T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num, 2)
  986. if err != nil {
  987. o.Rollback()
  988. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  989. c.ServeJSON()
  990. return
  991. }
  992. }
  993. // 批量更新所有相关设备的批次号
  994. err = DeviceDao.Update_Device_BatchNumber_ByInNumber(T_number, T_batch_number)
  995. if err != nil {
  996. o.Rollback()
  997. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新设备批次号失败"}
  998. c.ServeJSON()
  999. return
  1000. }
  1001. o.Commit()
  1002. StockIn_Edit_StockMonth(T_date, T_depot_id, allProductList)
  1003. NatsServer.AddUserLogs(c.User.T_uuid, "入库", "入库", var_)
  1004. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  1005. c.ServeJSON()
  1006. return
  1007. }
  1008. func StockIn_Edit_StockMonth(T_date string, T_depot_id int, allProductList []int) error {
  1009. if len(T_date) == 0 {
  1010. return errors.New("日期不能为空")
  1011. }
  1012. date, _ := lib.DateStrToTime(T_date)
  1013. //if date.Year() == time.Now().Year() && date.Month() == time.Now().Month() {
  1014. // // 修改本月数据不同步库存
  1015. // return nil
  1016. //}
  1017. //T_month := date.Format("2006-01")
  1018. months := generateMonthList(date, "in")
  1019. o := orm.NewOrm()
  1020. StockOutDao := Stock.NewStockOut(o)
  1021. StockMonthDao := Stock.NewStockMonth(o)
  1022. StockOutProductDao := Stock.NewStockOutProduct(o)
  1023. StockInProductDao := Stock.NewStockInProduct(o)
  1024. for _, T_product_id := range allProductList {
  1025. for _, T_month := range months {
  1026. // 获取当前产品本月出入库数量
  1027. lastMonth := StockMonthDao.Read_LastMonth_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
  1028. stockMonth := StockMonthDao.Read_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
  1029. if stockMonth.Id > 0 {
  1030. // 修改
  1031. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(T_depot_id, T_product_id, T_month)
  1032. if len(lastMonth) > 0 {
  1033. stockMonth.T_beginning = lastMonth[0].T_ending
  1034. } else {
  1035. stockMonth.T_beginning = 0
  1036. }
  1037. // 期末库存 = 期初库存+入库-出库
  1038. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  1039. err := StockMonthDao.Update_StockMonth(stockMonth, "T_in", "T_beginning", "T_ending")
  1040. if err != nil {
  1041. return err
  1042. }
  1043. } else {
  1044. product, _ := Basic.Read_Product_ById(T_product_id)
  1045. // 添加库存记录
  1046. stockMonth = Stock.StockMonth{
  1047. T_depot_id: T_depot_id,
  1048. T_product_id: T_product_id,
  1049. T_product_class: product.T_class,
  1050. T_month: T_month,
  1051. }
  1052. // 获取当前产品本月出入库数量
  1053. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(T_depot_id, T_product_id, T_month)
  1054. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(T_depot_id, T_product_id, T_month)
  1055. if len(lastMonth) > 0 {
  1056. stockMonth.T_beginning = lastMonth[0].T_ending
  1057. } else {
  1058. stockMonth.T_beginning = 0
  1059. }
  1060. // 期末库存 = 期初库存+入库-出库
  1061. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  1062. stockMonth.T_project = StockOutDao.Read_StockOut_T_contract_number(T_depot_id, T_product_id, T_month)
  1063. _, err := StockMonthDao.Add_StockMonth(stockMonth)
  1064. if err != nil {
  1065. return err
  1066. }
  1067. }
  1068. }
  1069. }
  1070. return nil
  1071. }
  1072. func (c *StockController) StockIn_Edit() {
  1073. T_number := c.GetString("T_number") // 入库单号
  1074. //T_depot_id, _ := c.GetInt("T_depot_id")
  1075. T_date := c.GetString("T_date")
  1076. T_product := c.GetString("T_product")
  1077. T_remark := c.GetString("T_remark")
  1078. T_project := c.GetString("T_project")
  1079. T_return_user := c.GetString("T_return_user")
  1080. T_batch_number := c.GetString("T_batch_number")
  1081. date, is := lib.DateStrToTime(T_date)
  1082. if !is {
  1083. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  1084. c.ServeJSON()
  1085. return
  1086. }
  1087. if date.Year() != time.Now().Year() && date.Month() != time.Now().Month() {
  1088. T_date = ""
  1089. }
  1090. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "修改入库", T_product)
  1091. o := orm.NewOrm()
  1092. o.Begin()
  1093. StockInDao := Stock.NewStockIn(o)
  1094. StockInProductDao := Stock.NewStockInProduct(o)
  1095. StockDao := Stock.NewStock(o)
  1096. DeviceDao := Stock.NewDevice(o)
  1097. IotCardDao := Property.NewIotCard(&o)
  1098. // 查询入库信息
  1099. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  1100. if err != nil {
  1101. o.Rollback()
  1102. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1103. c.ServeJSON()
  1104. return
  1105. }
  1106. T_old_date := stockIn.T_date
  1107. // 查询入库产品信息
  1108. productOldList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  1109. var oldProductList []dto.StockProduct
  1110. var newProductList []dto.StockProduct
  1111. allProductListMap := make(map[int]struct{})
  1112. productOldMap := map[int]dto.StockProduct{}
  1113. for _, product := range productOldList {
  1114. stockProduct := dto.StockProduct{
  1115. T_product_id: product.T_product_id,
  1116. T_num: product.T_num,
  1117. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  1118. }
  1119. oldProductList = append(oldProductList, stockProduct)
  1120. productOldMap[product.T_product_id] = stockProduct
  1121. if _, ok := allProductListMap[product.T_product_id]; !ok {
  1122. allProductListMap[product.T_product_id] = struct{}{}
  1123. }
  1124. }
  1125. productNewList := lib.SplitString(T_product, "|")
  1126. productNewMap := map[int]dto.StockProduct{}
  1127. for _, v := range productNewList {
  1128. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  1129. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  1130. T_relation_sn := strings.Split(v, "-")[2]
  1131. snList := lib.SplitString(T_relation_sn, ",")
  1132. if len(snList) > 0 {
  1133. num = len(snList)
  1134. }
  1135. stockProduct := dto.StockProduct{
  1136. T_product_id: product_id,
  1137. T_num: num,
  1138. T_relation_sn: snList,
  1139. }
  1140. newProductList = append(newProductList, stockProduct)
  1141. productNewMap[product_id] = stockProduct
  1142. if _, ok := allProductListMap[product_id]; !ok {
  1143. allProductListMap[product_id] = struct{}{}
  1144. }
  1145. }
  1146. // 判断产品列表信息是否相同
  1147. StockProductListIsEqual, needDelete, needAdd, needEdit, snDiff := dto.StockProductListsEqual(oldProductList, newProductList)
  1148. // 两次提交的数据相同,则不用修改
  1149. if !StockProductListIsEqual {
  1150. if len(needDelete) > 0 {
  1151. // 删除入库产品列表
  1152. for _, v := range needDelete {
  1153. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1154. // 删除入库产品列表
  1155. err = StockInProductDao.Delete_StockInProduct(stockIn.T_number, stockIn.T_depot_id, v.T_product_id)
  1156. if err != nil {
  1157. o.Rollback()
  1158. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  1159. c.ServeJSON()
  1160. return
  1161. }
  1162. if product.T_relation_sn == 1 {
  1163. // 删除该入库单关联sn
  1164. for _, sn := range v.T_relation_sn {
  1165. err = DeviceDao.Delete_Device_ByT_in_number(stockIn.T_number, sn)
  1166. if err != nil {
  1167. o.Rollback()
  1168. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库设备失败"}
  1169. c.ServeJSON()
  1170. return
  1171. }
  1172. }
  1173. }
  1174. // 减少库存
  1175. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
  1176. if err != nil {
  1177. o.Rollback()
  1178. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
  1179. c.ServeJSON()
  1180. return
  1181. }
  1182. }
  1183. }
  1184. if len(needAdd) > 0 {
  1185. // 新增入库产品列表
  1186. for _, v := range needAdd {
  1187. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1188. if product.T_relation_sn == 1 && len(v.T_relation_sn) == 0 {
  1189. o.Rollback()
  1190. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  1191. c.ServeJSON()
  1192. return
  1193. }
  1194. // 添加该入库单关联sn
  1195. for _, sn := range v.T_relation_sn {
  1196. mqtt := Stock.Read_MqttUser(sn)
  1197. // 添加设备
  1198. device := Stock.Device{
  1199. T_contract_number: "",
  1200. T_out_number: "",
  1201. T_product_id: v.T_product_id,
  1202. T_in_number: stockIn.T_number,
  1203. T_sn: sn,
  1204. T_iccid: mqtt.Iccid,
  1205. T_imei: mqtt.Imei,
  1206. T_State: 2,
  1207. T_batch_number: T_batch_number,
  1208. CreateTime: date,
  1209. UpdateTime: stockIn.UpdateTime,
  1210. }
  1211. _, err = DeviceDao.AddOrUpdate_Device(device, 2)
  1212. if err != nil {
  1213. o.Rollback()
  1214. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  1215. c.ServeJSON()
  1216. return
  1217. }
  1218. // 添加物联网卡
  1219. iotCard := Property.IotCard{
  1220. T_iccid: mqtt.Iccid,
  1221. T_sn: sn,
  1222. T_State: 2, //1-未使用 2-已使用 3-已作废
  1223. T_type: "4G",
  1224. }
  1225. _, err = IotCardDao.AddOrUpdate_IotCard(iotCard)
  1226. if err != nil {
  1227. o.Rollback()
  1228. c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库失败"}
  1229. c.ServeJSON()
  1230. return
  1231. }
  1232. }
  1233. stockInProduct := Stock.StockInProduct{
  1234. T_number: T_number,
  1235. T_product_id: v.T_product_id,
  1236. T_depot_id: stockIn.T_depot_id,
  1237. T_num: v.T_num, // 入库数量
  1238. T_date: stockIn.T_date, // 入库日期
  1239. T_relation_sn: strings.Join(v.T_relation_sn, ","),
  1240. }
  1241. _, err = StockInProductDao.Add_StockInProduct(stockInProduct)
  1242. if err != nil {
  1243. o.Rollback()
  1244. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  1245. c.ServeJSON()
  1246. return
  1247. }
  1248. // 增加库存
  1249. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 2)
  1250. if err != nil {
  1251. o.Rollback()
  1252. c.Data["json"] = lib.JSONS{Code: 203, Msg: "增加库存失败"}
  1253. c.ServeJSON()
  1254. return
  1255. }
  1256. }
  1257. }
  1258. if len(needEdit) > 0 {
  1259. for _, v := range needEdit {
  1260. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1261. stockInProduct := Stock.StockInProduct{
  1262. T_number: stockIn.T_number,
  1263. T_product_id: v.T_product_id,
  1264. T_depot_id: stockIn.T_depot_id,
  1265. T_num: productNewMap[v.T_product_id].T_num, // 入库数量
  1266. T_date: stockIn.T_date, // 入库日期
  1267. }
  1268. // 入库数量比之前多,增加库存
  1269. var T_type int
  1270. var T_num int
  1271. if productNewMap[v.T_product_id].T_num > productOldMap[v.T_product_id].T_num {
  1272. // 增加库存
  1273. T_type = 2
  1274. T_num = productNewMap[v.T_product_id].T_num - productOldMap[v.T_product_id].T_num
  1275. } else {
  1276. // 减少库存
  1277. T_type = 1
  1278. T_num = productOldMap[v.T_product_id].T_num - productNewMap[v.T_product_id].T_num
  1279. }
  1280. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, T_num, T_type)
  1281. if err != nil {
  1282. o.Rollback()
  1283. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  1284. c.ServeJSON()
  1285. return
  1286. }
  1287. // 更新产品库存表
  1288. err = StockInProductDao.Update_StockInProduct(stockInProduct)
  1289. if err != nil {
  1290. o.Rollback()
  1291. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库产品信息失败"}
  1292. c.ServeJSON()
  1293. return
  1294. }
  1295. }
  1296. }
  1297. for _, diff := range snDiff {
  1298. product, _ := Basic.Read_Product_ById(diff.T_product_id)
  1299. // 删除减少的sn
  1300. if len(diff.T_delete_relation_sn) > 0 {
  1301. for _, sn := range diff.T_delete_relation_sn {
  1302. err = DeviceDao.Delete_Device_ByT_in_number(stockIn.T_number, sn)
  1303. if err != nil {
  1304. o.Rollback()
  1305. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  1306. c.ServeJSON()
  1307. return
  1308. }
  1309. }
  1310. // 减少库存
  1311. _, 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)
  1312. if err != nil {
  1313. o.Rollback()
  1314. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  1315. c.ServeJSON()
  1316. return
  1317. }
  1318. }
  1319. if len(diff.T_add_relation_sn) > 0 {
  1320. for _, sn := range diff.T_add_relation_sn {
  1321. mqtt := Stock.Read_MqttUser(sn)
  1322. // 添加设备
  1323. device := Stock.Device{
  1324. T_contract_number: "",
  1325. T_out_number: "",
  1326. T_product_id: diff.T_product_id,
  1327. T_in_number: stockIn.T_number,
  1328. T_sn: sn,
  1329. T_iccid: mqtt.Iccid,
  1330. T_imei: mqtt.Imei,
  1331. T_State: 2,
  1332. CreateTime: date,
  1333. UpdateTime: stockIn.UpdateTime,
  1334. }
  1335. _, err = DeviceDao.AddOrUpdate_Device(device, 2)
  1336. if err != nil {
  1337. o.Rollback()
  1338. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  1339. c.ServeJSON()
  1340. return
  1341. }
  1342. // 添加物联网卡
  1343. iotCard := Property.IotCard{
  1344. T_iccid: mqtt.Iccid,
  1345. T_sn: sn,
  1346. T_State: 2, //1-未使用 2-已使用 3-已作废
  1347. T_type: "4G",
  1348. }
  1349. _, err = IotCardDao.AddOrUpdate_IotCard(iotCard)
  1350. if err != nil {
  1351. o.Rollback()
  1352. c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库失败"}
  1353. c.ServeJSON()
  1354. return
  1355. }
  1356. }
  1357. // 增加库存
  1358. _, 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)
  1359. if err != nil {
  1360. o.Rollback()
  1361. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  1362. c.ServeJSON()
  1363. return
  1364. }
  1365. }
  1366. stockInProduct := Stock.StockInProduct{
  1367. T_number: stockIn.T_number,
  1368. T_product_id: diff.T_product_id,
  1369. T_depot_id: stockIn.T_depot_id,
  1370. T_num: productNewMap[diff.T_product_id].T_num, // 入库数量
  1371. T_date: stockIn.T_date, // 入库日期
  1372. T_relation_sn: strings.Join(productNewMap[diff.T_product_id].T_relation_sn, ","),
  1373. }
  1374. // 更新产品库存表
  1375. err = StockInProductDao.Update_StockInProduct(stockInProduct)
  1376. if err != nil {
  1377. o.Rollback()
  1378. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库产品信息失败"}
  1379. c.ServeJSON()
  1380. return
  1381. }
  1382. }
  1383. }
  1384. if len(T_remark) > 0 {
  1385. stockIn.T_remark = T_remark
  1386. }
  1387. if len(T_date) > 0 {
  1388. stockIn.T_date = T_date
  1389. }
  1390. if len(T_project) > 0 {
  1391. stockIn.T_project = T_project
  1392. }
  1393. if len(T_return_user) > 0 {
  1394. stockIn.T_return_user = T_return_user
  1395. }
  1396. if len(T_batch_number) > 0 {
  1397. stockIn.T_batch_number = T_batch_number
  1398. }
  1399. err = StockInDao.Update_StockIn(stockIn, "T_remark", "T_date", "T_project", "T_return_user", "T_batch_number")
  1400. if err != nil {
  1401. o.Rollback()
  1402. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库失败"}
  1403. c.ServeJSON()
  1404. return
  1405. }
  1406. o.Commit()
  1407. if len(T_date) > 0 && T_old_date != T_date {
  1408. // 修改出库产品日期
  1409. StockInProductDao.Update_StockInProduct_T_date(stockIn.T_number, T_date)
  1410. DeviceDao.Update_Device_CreateTimeByT_in_number(stockIn.T_number, T_date)
  1411. }
  1412. // 修改出库产品日期
  1413. StockInProductDao.Update_StockInProduct_T_batch_number(stockIn.T_number, T_batch_number)
  1414. // 修改设备批次号
  1415. DeviceDao.Update_Device_BatchNumber_ByInNumber(stockIn.T_number, T_batch_number)
  1416. // 更新月份统计表
  1417. allProductList := []int{}
  1418. for i, _ := range allProductListMap {
  1419. allProductList = append(allProductList, i)
  1420. }
  1421. StockIn_Edit_StockMonth(stockIn.T_date, stockIn.T_depot_id, allProductList)
  1422. NatsServer.AddUserLogs(c.User.T_uuid, "入库", "修改", stockIn)
  1423. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  1424. c.ServeJSON()
  1425. return
  1426. }
  1427. // 删除入库
  1428. func (c *StockController) StockIn_Del() {
  1429. T_number := c.GetString("T_number") // 入库单号
  1430. o := orm.NewOrm()
  1431. o.Begin()
  1432. StockInDao := Stock.NewStockIn(o)
  1433. StockInProductDao := Stock.NewStockInProduct(o)
  1434. StockDao := Stock.NewStock(o)
  1435. DeviceDao := Stock.NewDevice(o)
  1436. // 查询入库信息
  1437. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  1438. if err != nil {
  1439. o.Rollback()
  1440. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1441. c.ServeJSON()
  1442. return
  1443. }
  1444. // 查询入库产品信息
  1445. productOldList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  1446. var oldProductList []dto.StockProduct
  1447. allProductList := []int{}
  1448. for _, product := range productOldList {
  1449. oldProductList = append(oldProductList, dto.StockProduct{
  1450. T_product_id: product.T_product_id,
  1451. T_num: product.T_num,
  1452. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  1453. })
  1454. allProductList = append(allProductList, product.T_product_id)
  1455. }
  1456. // 删除入库产品列表
  1457. for _, v := range oldProductList {
  1458. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1459. // 删除入库产品列表
  1460. err = StockInProductDao.Delete_StockInProduct(stockIn.T_number, stockIn.T_depot_id, v.T_product_id)
  1461. if err != nil {
  1462. o.Rollback()
  1463. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  1464. c.ServeJSON()
  1465. return
  1466. }
  1467. if product.T_relation_sn == 1 {
  1468. // 删除该入库单关联sn
  1469. for _, sn := range v.T_relation_sn {
  1470. err = DeviceDao.Delete_Device_ByT_in_number(stockIn.T_number, sn)
  1471. if err != nil {
  1472. o.Rollback()
  1473. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库设备失败"}
  1474. c.ServeJSON()
  1475. return
  1476. }
  1477. }
  1478. }
  1479. // 减少库存
  1480. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
  1481. if err != nil {
  1482. o.Rollback()
  1483. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
  1484. c.ServeJSON()
  1485. return
  1486. }
  1487. }
  1488. err = StockInDao.Delete_StockIn(stockIn)
  1489. if err != nil {
  1490. o.Rollback()
  1491. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库失败"}
  1492. c.ServeJSON()
  1493. return
  1494. }
  1495. o.Commit()
  1496. // 更新月份统计表
  1497. StockIn_Edit_StockMonth(stockIn.T_date, stockIn.T_depot_id, allProductList)
  1498. NatsServer.AddUserLogs(c.User.T_uuid, "入库", "删除", stockIn)
  1499. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  1500. c.ServeJSON()
  1501. return
  1502. }
  1503. // 导出出库单
  1504. func (c *StockController) StockIn_Excel() {
  1505. // 查询
  1506. T_number := c.GetString("T_number")
  1507. o := orm.NewOrm()
  1508. StockInDao := Stock.NewStockIn(o)
  1509. StockInProductDao := Stock.NewStockInProduct(o)
  1510. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  1511. if err != nil {
  1512. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1513. c.ServeJSON()
  1514. return
  1515. }
  1516. productList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  1517. var pList []Stock.StockInProduct_R
  1518. for _, v := range productList {
  1519. pList = append(pList, Stock.StockInProductToStockInProduct_R(v))
  1520. }
  1521. userList, _ := NatsServer.Read_User_List_All()
  1522. Account.Read_User_All_Map(userList)
  1523. stockInDetail := Stock.StockInToStockIn_Detail(stockIn, pList)
  1524. T_date, _ := lib.DateStrToTime(stockIn.T_date)
  1525. filename := fmt.Sprintf("冷链产品入库单(%s)", lib.GetRandstring(6, "0123456789", 0))
  1526. f := excelize.NewFile() // 设置单元格的值
  1527. Style1, _ := f.NewStyle(
  1528. &excelize.Style{
  1529. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  1530. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1531. })
  1532. Style2, _ := f.NewStyle(
  1533. &excelize.Style{
  1534. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1535. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  1536. })
  1537. Style3, _ := f.NewStyle(
  1538. &excelize.Style{
  1539. Font: &excelize.Font{Size: 10, Family: "宋体", Bold: true},
  1540. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1541. Border: []excelize.Border{
  1542. {Type: "left", Color: "000000", Style: 1},
  1543. {Type: "top", Color: "000000", Style: 1},
  1544. {Type: "bottom", Color: "000000", Style: 1},
  1545. {Type: "right", Color: "000000", Style: 1},
  1546. },
  1547. })
  1548. Style4, _ := f.NewStyle(
  1549. &excelize.Style{
  1550. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1551. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1552. Border: []excelize.Border{
  1553. {Type: "left", Color: "000000", Style: 1},
  1554. {Type: "top", Color: "000000", Style: 1},
  1555. {Type: "bottom", Color: "000000", Style: 1},
  1556. {Type: "right", Color: "000000", Style: 1},
  1557. },
  1558. })
  1559. productList2 := lib.ChunkBy(productList, 10)
  1560. for i, products := range productList2 {
  1561. if len(products) < 10 {
  1562. products = append(products, make([]Stock.StockInProduct, 10-len(products))...)
  1563. }
  1564. row := i*17 + 1
  1565. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1566. f.SetRowStyle("Sheet1", row, row, Style1)
  1567. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("宝智达科技产品进销存统计表"))
  1568. f.SetRowHeight("Sheet1", row, 20)
  1569. row += 1
  1570. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1571. f.SetRowStyle("Sheet1", row, row, Style1)
  1572. title := "硬件入库单"
  1573. if len(productList2) > 1 {
  1574. title += fmt.Sprintf(" %d/%d", i+1, len(productList2))
  1575. }
  1576. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), title)
  1577. f.SetRowHeight("Sheet1", row, 20)
  1578. row += 1
  1579. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1580. f.SetRowStyle("Sheet1", row, row, Style2)
  1581. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("入库单号:%s", stockIn.T_number))
  1582. f.SetRowHeight("Sheet1", row, 20)
  1583. row += 1
  1584. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1585. f.SetRowStyle("Sheet1", row, row, Style2)
  1586. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("项目名称:%s", stockIn.T_remark))
  1587. f.SetRowHeight("Sheet1", row, 20)
  1588. row += 1
  1589. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1590. f.SetRowStyle("Sheet1", row, row, Style2)
  1591. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("入库时间:%s", T_date.Format("2006年01月02日")))
  1592. f.SetRowHeight("Sheet1", row, 20)
  1593. // 这里设置表头
  1594. row += 1
  1595. f.SetRowHeight("Sheet1", row, 20)
  1596. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "序号")
  1597. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), "产品名称")
  1598. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), "型号")
  1599. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), "单位")
  1600. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), "领用数量")
  1601. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), "备注")
  1602. // 设置列宽
  1603. f.SetColWidth("Sheet1", "A", "A", 5)
  1604. f.SetColWidth("Sheet1", "B", "B", 20)
  1605. f.SetColWidth("Sheet1", "C", "C", 12)
  1606. f.SetColWidth("Sheet1", "D", "D", 10)
  1607. f.SetColWidth("Sheet1", "E", "E", 10)
  1608. f.SetColWidth("Sheet1", "F", "F", 15)
  1609. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row), Style3)
  1610. sRow := row
  1611. // 循环写入数据
  1612. for j, v := range products {
  1613. row += 1
  1614. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1615. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), j+1)
  1616. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), product.T_name)
  1617. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), product.T_model)
  1618. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), product.T_spec)
  1619. if v.T_num > 0 {
  1620. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), v.T_num)
  1621. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), stockIn.T_remark)
  1622. }
  1623. }
  1624. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", sRow), fmt.Sprintf("F%d", row), Style4)
  1625. row += 1
  1626. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("C%d", row))
  1627. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("制单:%s", c.User.T_name))
  1628. f.MergeCell("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("F%d", row))
  1629. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("经办人:%s", stockInDetail.T_submit_name))
  1630. f.SetRowStyle("Sheet1", row, row, Style2)
  1631. f.SetRowHeight("Sheet1", row, 20)
  1632. }
  1633. // 保存文件
  1634. if err = f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  1635. fmt.Println(err)
  1636. }
  1637. var url string
  1638. //// 上传 OSS
  1639. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  1640. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  1641. if !is {
  1642. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  1643. c.ServeJSON()
  1644. return
  1645. }
  1646. //删除目录
  1647. err = os.Remove("ofile/" + filename + ".xlsx")
  1648. if err != nil {
  1649. logs.Error(lib.FuncName(), err)
  1650. }
  1651. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1652. c.ServeJSON()
  1653. return
  1654. }
  1655. // 批量导出出库单
  1656. func (c *StockController) StockIn_Excel_Batch() {
  1657. // 查询
  1658. T_number_list := c.GetString("T_number_list")
  1659. o := orm.NewOrm()
  1660. StockInDao := Stock.NewStockIn(o)
  1661. StockInProductDao := Stock.NewStockInProduct(o)
  1662. list := strings.Split(strings.Trim(T_number_list, "|"), "|")
  1663. userList, _ := NatsServer.Read_User_List_All()
  1664. Account.Read_User_All_Map(userList)
  1665. filename := fmt.Sprintf("冷链产品入库单(%s)", lib.GetRandstring(6, "0123456789", 0))
  1666. f := excelize.NewFile() // 设置单元格的值
  1667. Style1, _ := f.NewStyle(
  1668. &excelize.Style{
  1669. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  1670. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1671. })
  1672. Style2, _ := f.NewStyle(
  1673. &excelize.Style{
  1674. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1675. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  1676. })
  1677. Style3, _ := f.NewStyle(
  1678. &excelize.Style{
  1679. Font: &excelize.Font{Size: 10, Family: "宋体", Bold: true},
  1680. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1681. Border: []excelize.Border{
  1682. {Type: "left", Color: "000000", Style: 1},
  1683. {Type: "top", Color: "000000", Style: 1},
  1684. {Type: "bottom", Color: "000000", Style: 1},
  1685. {Type: "right", Color: "000000", Style: 1},
  1686. },
  1687. })
  1688. Style4, _ := f.NewStyle(
  1689. &excelize.Style{
  1690. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1691. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1692. Border: []excelize.Border{
  1693. {Type: "left", Color: "000000", Style: 1},
  1694. {Type: "top", Color: "000000", Style: 1},
  1695. {Type: "bottom", Color: "000000", Style: 1},
  1696. {Type: "right", Color: "000000", Style: 1},
  1697. },
  1698. })
  1699. var num int
  1700. for _, T_number := range list {
  1701. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  1702. if err != nil {
  1703. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1704. c.ServeJSON()
  1705. return
  1706. }
  1707. productList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  1708. var pList []Stock.StockInProduct_R
  1709. for _, v := range productList {
  1710. pList = append(pList, Stock.StockInProductToStockInProduct_R(v))
  1711. }
  1712. stockInDetail := Stock.StockInToStockIn_Detail(stockIn, pList)
  1713. T_date, _ := lib.DateStrToTime(stockIn.T_date)
  1714. productList2 := lib.ChunkBy(productList, 10)
  1715. for i, products := range productList2 {
  1716. if len(products) < 10 {
  1717. products = append(products, make([]Stock.StockInProduct, 10-len(products))...)
  1718. }
  1719. row := num*18 + 1
  1720. num += 1
  1721. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1722. f.SetRowStyle("Sheet1", row, row, Style1)
  1723. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("宝智达科技产品进销存统计表"))
  1724. f.SetRowHeight("Sheet1", row, 20)
  1725. row += 1
  1726. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1727. f.SetRowStyle("Sheet1", row, row, Style1)
  1728. title := "硬件入库单"
  1729. if len(productList2) > 1 {
  1730. title += fmt.Sprintf(" %d/%d", i+1, len(productList2))
  1731. }
  1732. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), title)
  1733. f.SetRowHeight("Sheet1", row, 20)
  1734. row += 1
  1735. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1736. f.SetRowStyle("Sheet1", row, row, Style2)
  1737. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("入库单号:%s", stockIn.T_number))
  1738. f.SetRowHeight("Sheet1", row, 20)
  1739. row += 1
  1740. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1741. f.SetRowStyle("Sheet1", row, row, Style2)
  1742. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("项目名称:%s", stockIn.T_remark))
  1743. f.SetRowHeight("Sheet1", row, 20)
  1744. row += 1
  1745. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1746. f.SetRowStyle("Sheet1", row, row, Style2)
  1747. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("入库时间:%s", T_date.Format("2006年01月02日")))
  1748. f.SetRowHeight("Sheet1", row, 20)
  1749. // 这里设置表头
  1750. row += 1
  1751. f.SetRowHeight("Sheet1", row, 20)
  1752. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "序号")
  1753. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), "产品名称")
  1754. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), "型号")
  1755. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), "单位")
  1756. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), "领用数量")
  1757. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), "备注")
  1758. // 设置列宽
  1759. f.SetColWidth("Sheet1", "A", "A", 5)
  1760. f.SetColWidth("Sheet1", "B", "B", 20)
  1761. f.SetColWidth("Sheet1", "C", "C", 12)
  1762. f.SetColWidth("Sheet1", "D", "D", 10)
  1763. f.SetColWidth("Sheet1", "E", "E", 10)
  1764. f.SetColWidth("Sheet1", "F", "F", 15)
  1765. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row), Style3)
  1766. sRow := row
  1767. // 循环写入数据
  1768. for j, v := range products {
  1769. row += 1
  1770. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1771. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), j+1)
  1772. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), product.T_name)
  1773. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), product.T_model)
  1774. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), product.T_spec)
  1775. if v.T_num > 0 {
  1776. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), v.T_num)
  1777. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), stockIn.T_remark)
  1778. }
  1779. }
  1780. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", sRow), fmt.Sprintf("F%d", row), Style4)
  1781. row += 1
  1782. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("C%d", row))
  1783. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("制单:%s", c.User.T_name))
  1784. f.MergeCell("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("F%d", row))
  1785. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("经办人:%s", stockInDetail.T_submit_name))
  1786. f.SetRowStyle("Sheet1", row, row, Style2)
  1787. f.SetRowHeight("Sheet1", row, 20)
  1788. // 空一行
  1789. row += 1
  1790. //f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1791. //f.SetRowStyle("Sheet1", row, row, Style1)
  1792. //f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "")
  1793. // 插入下一页分页符
  1794. f.InsertPageBreak("Sheet1", fmt.Sprintf("A%d", row))
  1795. }
  1796. }
  1797. // 保存文件
  1798. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  1799. fmt.Println(err)
  1800. }
  1801. var url string
  1802. //// 上传 OSS
  1803. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  1804. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  1805. if !is {
  1806. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  1807. c.ServeJSON()
  1808. return
  1809. }
  1810. //删除目录
  1811. err := os.Remove("ofile/" + filename + ".xlsx")
  1812. if err != nil {
  1813. logs.Error(lib.FuncName(), err)
  1814. }
  1815. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1816. c.ServeJSON()
  1817. return
  1818. }
  1819. func (c *StockController) StockOut_List() {
  1820. // 分页参数 初始化
  1821. page, _ := c.GetInt("page")
  1822. if page < 1 {
  1823. page = 1
  1824. }
  1825. page_z, _ := c.GetInt("page_z")
  1826. if page_z < 1 {
  1827. page_z = conf.Page_size
  1828. }
  1829. // 查询
  1830. T_depot_id, _ := c.GetInt("T_depot_id")
  1831. T_contract_number := c.GetString("T_contract_number")
  1832. T_start_date := c.GetString("T_start_date")
  1833. T_end_date := c.GetString("T_end_date")
  1834. T_name := c.GetString("T_name")
  1835. userList, _ := NatsServer.Read_User_List_All()
  1836. Account.Read_User_All_Map(userList)
  1837. Basic.Read_Depot_All_Map()
  1838. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  1839. 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)
  1840. var r_jsons lib.R_JSONS
  1841. r_jsons.Num = R_cnt
  1842. r_jsons.Data = R_List
  1843. r_jsons.Page = page
  1844. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  1845. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1846. c.ServeJSON()
  1847. return
  1848. }
  1849. func (c *StockController) StockOut_List_Product() {
  1850. // 分页参数 初始化
  1851. page, _ := c.GetInt("page")
  1852. if page < 1 {
  1853. page = 1
  1854. }
  1855. page_z, _ := c.GetInt("page_z")
  1856. if page_z < 1 {
  1857. page_z = conf.Page_size
  1858. }
  1859. // 查询
  1860. T_depot_id, _ := c.GetInt("T_depot_id")
  1861. T_name := c.GetString("T_name")
  1862. T_start_date := c.GetString("T_start_date")
  1863. T_end_date := c.GetString("T_end_date")
  1864. T_product_name := c.GetString("T_product_name") // 产品名称
  1865. T_product_model := c.GetString("T_product_model") // 产品型号
  1866. userList, _ := NatsServer.Read_User_List_All()
  1867. Account.Read_User_All_Map(userList)
  1868. Basic.Read_Depot_All_Map()
  1869. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  1870. 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)
  1871. var r_jsons lib.R_JSONS
  1872. r_jsons.Num = R_cnt
  1873. r_jsons.Data = R_List
  1874. r_jsons.Page = page
  1875. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  1876. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1877. c.ServeJSON()
  1878. return
  1879. }
  1880. func (c *StockController) StockOut_List_Product_Excel() {
  1881. // 查询
  1882. T_depot_id, _ := c.GetInt("T_depot_id")
  1883. T_name := c.GetString("T_name")
  1884. T_start_date := c.GetString("T_start_date")
  1885. T_end_date := c.GetString("T_end_date")
  1886. T_product_name := c.GetString("T_product_name") // 产品名称
  1887. T_product_model := c.GetString("T_product_model") // 产品型号
  1888. userList, _ := NatsServer.Read_User_List_All()
  1889. Account.Read_User_All_Map(userList)
  1890. Basic.Read_Depot_All_Map()
  1891. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  1892. 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)
  1893. filename := fmt.Sprintf("出库明细单(%s)", lib.GetRandstring(6, "0123456789", 0))
  1894. f := excelize.NewFile() // 设置单元格的值
  1895. Style1, _ := f.NewStyle(
  1896. &excelize.Style{
  1897. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  1898. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1899. })
  1900. Style2, _ := f.NewStyle(
  1901. &excelize.Style{
  1902. Font: &excelize.Font{Bold: true, Size: 11, Family: "宋体"},
  1903. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1904. Border: []excelize.Border{
  1905. {Type: "left", Color: "000000", Style: 1},
  1906. {Type: "top", Color: "000000", Style: 1},
  1907. {Type: "bottom", Color: "000000", Style: 1},
  1908. {Type: "right", Color: "000000", Style: 1},
  1909. },
  1910. })
  1911. Style3, _ := f.NewStyle(
  1912. &excelize.Style{
  1913. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1914. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  1915. Border: []excelize.Border{
  1916. {Type: "left", Color: "000000", Style: 1},
  1917. {Type: "top", Color: "000000", Style: 1},
  1918. {Type: "bottom", Color: "000000", Style: 1},
  1919. {Type: "right", Color: "000000", Style: 1},
  1920. },
  1921. })
  1922. f.MergeCell("Sheet1", "A1", "L1")
  1923. f.SetRowStyle("Sheet1", 1, 1, Style1)
  1924. f.SetCellValue("Sheet1", "A1", fmt.Sprintf("出库明细单"))
  1925. f.SetRowHeight("Sheet1", 1, 30)
  1926. // 这里设置表头
  1927. f.SetCellStyle("Sheet1", "A2", "L2", Style2)
  1928. f.SetRowHeight("Sheet1", 2, 20)
  1929. f.SetCellValue("Sheet1", "A2", "序号")
  1930. f.SetCellValue("Sheet1", "B2", "出库单号")
  1931. f.SetCellValue("Sheet1", "C2", "领取人")
  1932. f.SetCellValue("Sheet1", "D2", "出库日期")
  1933. f.SetCellValue("Sheet1", "E2", "出库仓库")
  1934. f.SetCellValue("Sheet1", "F2", "关联项目")
  1935. f.SetCellValue("Sheet1", "G2", "产品名称")
  1936. f.SetCellValue("Sheet1", "H2", "产品型号")
  1937. f.SetCellValue("Sheet1", "I2", "数量")
  1938. f.SetCellValue("Sheet1", "J2", "是否关联SN")
  1939. f.SetCellValue("Sheet1", "K2", "备注")
  1940. f.SetCellValue("Sheet1", "L2", "SN")
  1941. // 设置列宽
  1942. f.SetColWidth("Sheet1", "A", "A", 10)
  1943. f.SetColWidth("Sheet1", "B", "B", 15)
  1944. f.SetColWidth("Sheet1", "C", "D", 10)
  1945. f.SetColWidth("Sheet1", "D", "D", 10)
  1946. f.SetColWidth("Sheet1", "E", "E", 10)
  1947. f.SetColWidth("Sheet1", "F", "F", 20)
  1948. f.SetColWidth("Sheet1", "G", "G", 20)
  1949. f.SetColWidth("Sheet1", "H", "H", 15)
  1950. f.SetColWidth("Sheet1", "I", "I", 10)
  1951. f.SetColWidth("Sheet1", "J", "J", 12)
  1952. f.SetColWidth("Sheet1", "K", "K", 30)
  1953. f.SetColWidth("Sheet1", "L", "L", 100)
  1954. line := 2
  1955. for _, product := range R_List {
  1956. line += 1
  1957. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), line-2)
  1958. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), product.T_number)
  1959. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), product.T_receive_name)
  1960. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), product.T_date)
  1961. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), product.T_depot_name)
  1962. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), product.T_project)
  1963. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), product.T_product_name)
  1964. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), product.T_product_model)
  1965. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), product.T_num)
  1966. var relation_sn string
  1967. if product.T_product_relation_sn == 1 {
  1968. relation_sn = "是"
  1969. } else {
  1970. relation_sn = "否"
  1971. }
  1972. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), relation_sn)
  1973. f.SetCellValue("Sheet1", fmt.Sprintf("K%d", line), product.T_remark)
  1974. f.SetCellValue("Sheet1", fmt.Sprintf("L%d", line), product.T_relation_sn)
  1975. }
  1976. f.SetCellStyle("Sheet1", "A3", fmt.Sprintf("L%d", line), Style3)
  1977. // 保存文件
  1978. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  1979. fmt.Println(err)
  1980. }
  1981. var url string
  1982. //// 上传 OSS
  1983. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  1984. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  1985. if !is {
  1986. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  1987. c.ServeJSON()
  1988. return
  1989. }
  1990. //删除目录
  1991. err := os.Remove("ofile/" + filename + ".xlsx")
  1992. if err != nil {
  1993. logs.Error(lib.FuncName(), err)
  1994. }
  1995. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1996. c.ServeJSON()
  1997. return
  1998. }
  1999. func (c *StockController) StockOut_Get() {
  2000. // 查询
  2001. T_number := c.GetString("T_number")
  2002. o := orm.NewOrm()
  2003. StockOutDao := Stock.NewStockOut(o)
  2004. StockOutProductDao := Stock.NewStockOutProduct(o)
  2005. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  2006. if err != nil {
  2007. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  2008. c.ServeJSON()
  2009. return
  2010. }
  2011. productList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  2012. var pList []Stock.StockOutProduct_R
  2013. for _, v := range productList {
  2014. pList = append(pList, Stock.StockOutProductToStockOutProduct_R(v))
  2015. }
  2016. userList, _ := NatsServer.Read_User_List_All()
  2017. Account.Read_User_All_Map(userList)
  2018. Basic.Read_Depot_All_Map()
  2019. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Stock.StockOutToStockOut_Detail(stockOut, pList)}
  2020. c.ServeJSON()
  2021. return
  2022. }
  2023. func (c *StockController) StockOut_generate_number() {
  2024. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  2025. rand_x := 0
  2026. T_number := ""
  2027. for true {
  2028. T_number = "CK-" + lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", int64(rand_x))
  2029. _, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  2030. if err != nil && err.Error() == orm.ErrNoRows.Error() {
  2031. break
  2032. }
  2033. rand_x += 1
  2034. }
  2035. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  2036. c.ServeJSON()
  2037. return
  2038. }
  2039. func (c *StockController) StockOut_Add() {
  2040. o := orm.NewOrm()
  2041. o.Begin()
  2042. StockOutDao := Stock.NewStockOut(o)
  2043. // 检查前端传入的T_number是否已提交过
  2044. T_number := c.GetString("T_number")
  2045. _, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  2046. if err == nil {
  2047. // 入库编号已存在,不需要做任何处理
  2048. o.Rollback()
  2049. c.Data["json"] = lib.JSONS{Code: 200, Msg: "出库编号已存在,无需重复提交!", Data: T_number}
  2050. c.ServeJSON()
  2051. return
  2052. }
  2053. if err != nil && err.Error() != orm.ErrNoRows.Error() {
  2054. // 数据库查询错误
  2055. o.Rollback()
  2056. c.Data["json"] = lib.JSONS{Code: 203, Msg: "查询入库编号失败"}
  2057. c.ServeJSON()
  2058. return
  2059. }
  2060. T_contract_number := c.GetString("T_contract_number")
  2061. T_depot_id, _ := c.GetInt("T_depot_id")
  2062. T_type, _ := c.GetInt("T_type") // 出库类型 1-领料出库 2-销售出库
  2063. T_date := c.GetString("T_date")
  2064. T_receive := c.GetString("T_receive")
  2065. T_product := c.GetString("T_product")
  2066. T_remark := c.GetString("T_remark")
  2067. T_project := c.GetString("T_project")
  2068. T_delivery_type, _ := c.GetInt("T_delivery_type")
  2069. T_signer_unit := c.GetString("T_signer_unit")
  2070. T_signer := c.GetString("T_signer")
  2071. T_signer_phone := c.GetString("T_signer_phone")
  2072. T_signer_date := c.GetString("T_signer_date")
  2073. T_courier_number := c.GetString("T_courier_number")
  2074. date, is := lib.DateStrToTime(T_date)
  2075. if !is {
  2076. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  2077. c.ServeJSON()
  2078. return
  2079. }
  2080. var_ := Stock.StockOut{
  2081. T_number: T_number,
  2082. T_contract_number: T_contract_number,
  2083. T_depot_id: T_depot_id,
  2084. T_type: T_type,
  2085. T_date: T_date,
  2086. T_receive: T_receive,
  2087. T_remark: T_remark,
  2088. T_project: T_project,
  2089. T_submit: c.User.T_uuid,
  2090. // 销售出库
  2091. T_delivery_type: T_delivery_type, // 1-自送 2-自提 3-快递
  2092. T_signer_unit: T_signer_unit,
  2093. T_signer: T_signer,
  2094. T_signer_phone: T_signer_phone,
  2095. T_signer_date: T_signer_date,
  2096. T_courier_number: T_courier_number,
  2097. T_state: Stock.StockOutAlreadyOut,
  2098. }
  2099. StockOutProductDao := Stock.NewStockOutProduct(o)
  2100. StockDao := Stock.NewStock(o)
  2101. DeviceDao := Stock.NewDevice(o)
  2102. ContractDao := Contract.NewContract(o)
  2103. ContractProductDao := Contract.NewContractProduct(o)
  2104. _, err = StockOutDao.Add_StockOut(var_)
  2105. if err != nil {
  2106. o.Rollback()
  2107. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
  2108. c.ServeJSON()
  2109. return
  2110. }
  2111. // 1、添加出库单
  2112. contract, _ := ContractDao.Read_Contract_ByT_number(T_contract_number)
  2113. if contract.Id == 0 && T_type == 2 {
  2114. o.Rollback()
  2115. c.Data["json"] = lib.JSONS{Code: 203, Msg: "合同编号错误!"}
  2116. c.ServeJSON()
  2117. return
  2118. }
  2119. if len(T_product) == 0 {
  2120. o.Rollback()
  2121. c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
  2122. c.ServeJSON()
  2123. return
  2124. }
  2125. productList := lib.SplitString(T_product, "|")
  2126. allProductList := []int{}
  2127. for _, v := range productList {
  2128. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  2129. allProductList = append(allProductList, product_id)
  2130. product, _ := Basic.Read_Product_ById(product_id)
  2131. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  2132. T_relation_sn := strings.Split(v, "-")[2]
  2133. if T_relation_sn == "" && product.T_relation_sn == 1 {
  2134. o.Rollback()
  2135. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  2136. c.ServeJSON()
  2137. return
  2138. }
  2139. if len(T_relation_sn) > 0 {
  2140. snList := lib.SplitString(T_relation_sn, ",")
  2141. num = len(snList)
  2142. for _, sn := range snList {
  2143. mqtt := Stock.Read_MqttUser(sn)
  2144. // 根据IMEI查询上次入库的批次号
  2145. batchNumber, err := DeviceDao.Read_Device_BatchNumber_ByImei(mqtt.Imei)
  2146. if err != nil {
  2147. logs.Error(lib.FuncName(), err)
  2148. }
  2149. device := Stock.Device{
  2150. T_contract_number: T_contract_number,
  2151. T_product_id: product_id,
  2152. T_out_number: T_number,
  2153. T_sn: sn,
  2154. T_iccid: mqtt.Iccid,
  2155. T_imei: mqtt.Imei,
  2156. T_State: 1,
  2157. T_project: T_project,
  2158. T_batch_number: batchNumber,
  2159. CreateTime: date,
  2160. }
  2161. _, err = DeviceDao.AddOrUpdate_Device(device, 1)
  2162. if err != nil {
  2163. o.Rollback()
  2164. c.Data["json"] = lib.JSONS{Code: 202, Msg: "出库失败"}
  2165. c.ServeJSON()
  2166. return
  2167. }
  2168. }
  2169. }
  2170. // 3、添加出库产品清单
  2171. stockOutProduct := Stock.StockOutProduct{
  2172. T_number: T_number,
  2173. T_product_id: product_id,
  2174. T_depot_id: T_depot_id,
  2175. T_num: num, // 出库数量
  2176. T_date: T_date, // 出库日期
  2177. T_relation_sn: T_relation_sn,
  2178. T_state: 1,
  2179. }
  2180. _, err = StockOutProductDao.Add_StockOutProduct(stockOutProduct)
  2181. if err != nil {
  2182. o.Rollback()
  2183. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
  2184. c.ServeJSON()
  2185. return
  2186. }
  2187. // 4、更改合同产品列表清单
  2188. contractProduct, _ := ContractProductDao.Read_ContractProduct_ByT_number_T_product_id(T_contract_number, product_id)
  2189. contractProduct.T_product_out += num
  2190. if contractProduct.T_product_out > contractProduct.T_product_total && T_type == 2 {
  2191. o.Rollback()
  2192. p, _ := Basic.Read_Product_ById(product_id)
  2193. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】出库数量超过预计出库数量!", p.T_name)}
  2194. c.ServeJSON()
  2195. return
  2196. }
  2197. contractProduct.T_State = 2
  2198. if contractProduct.T_product_out == 0 {
  2199. contractProduct.T_State = 1
  2200. }
  2201. if contractProduct.T_product_out == contractProduct.T_product_total {
  2202. contractProduct.T_State = 3
  2203. }
  2204. err = ContractProductDao.Update_ContractProduct(contractProduct, "T_product_out", "T_State")
  2205. if err != nil {
  2206. o.Rollback()
  2207. c.Data["json"] = lib.JSONS{Code: 202, Msg: "更新合同产品清单失败"}
  2208. c.ServeJSON()
  2209. return
  2210. }
  2211. // 5、更新产品库存列表
  2212. _, err = StockDao.AddOrUpdate_Stock(T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num, 1)
  2213. if err != nil {
  2214. o.Rollback()
  2215. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存失败" + err.Error()}
  2216. c.ServeJSON()
  2217. return
  2218. }
  2219. }
  2220. o.Commit()
  2221. o2 := orm.NewOrm()
  2222. o2.Begin()
  2223. ContractDao2 := Contract.NewContract(o2)
  2224. ContractProductDao2 := Contract.NewContractProduct(o2)
  2225. // 5、更新合同出库状态
  2226. var T_out int
  2227. if contract.T_State == 1 {
  2228. T_out = 2
  2229. // 查询合同产品清单是否全部都为已出库
  2230. state := ContractProductDao2.Read_ContractProduct_T_State_List(contract.T_number)
  2231. if state == 1 {
  2232. T_out = 1
  2233. }
  2234. if state == 3 {
  2235. T_out = 3
  2236. }
  2237. }
  2238. if T_out != contract.T_out {
  2239. contract.T_out = T_out
  2240. err = ContractDao2.Update_Contract(contract, "T_out")
  2241. if err != nil {
  2242. o2.Rollback()
  2243. c.Data["json"] = lib.JSONS{Code: 202, Msg: "更新合同出库状态失败"}
  2244. c.ServeJSON()
  2245. return
  2246. }
  2247. }
  2248. o2.Commit()
  2249. StockOut_Edit_StockMonth(T_date, T_depot_id, allProductList)
  2250. NatsServer.AddUserLogs(c.User.T_uuid, "出库", "出库", var_)
  2251. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  2252. c.ServeJSON()
  2253. return
  2254. }
  2255. func (c *StockController) StockOut_Edit_Delivery() {
  2256. T_number := c.GetString("T_number")
  2257. T_remark := c.GetString("T_remark")
  2258. T_project := c.GetString("T_project")
  2259. T_delivery_type, _ := c.GetInt("T_delivery_type")
  2260. T_signer_unit := c.GetString("T_signer_unit")
  2261. T_signer := c.GetString("T_signer")
  2262. T_signer_phone := c.GetString("T_signer_phone")
  2263. T_signer_date := c.GetString("T_signer_date")
  2264. T_courier_number := c.GetString("T_courier_number")
  2265. o := orm.NewOrm()
  2266. StockOutDao := Stock.NewStockOut(o)
  2267. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  2268. if err != nil {
  2269. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库编号错误!"}
  2270. c.ServeJSON()
  2271. return
  2272. }
  2273. if len(T_remark) > 0 {
  2274. stockOut.T_remark = T_remark
  2275. }
  2276. if len(T_project) > 0 {
  2277. stockOut.T_project = T_project
  2278. }
  2279. if T_delivery_type > 0 {
  2280. stockOut.T_delivery_type = T_delivery_type
  2281. }
  2282. if len(T_signer_unit) > 0 {
  2283. stockOut.T_signer_unit = T_signer_unit
  2284. }
  2285. if len(T_signer) > 0 {
  2286. stockOut.T_signer = T_signer
  2287. }
  2288. if len(T_signer_phone) > 0 {
  2289. stockOut.T_signer_phone = T_signer_phone
  2290. }
  2291. if len(T_signer_date) > 0 {
  2292. stockOut.T_signer_date = T_signer_date
  2293. }
  2294. if len(T_courier_number) > 0 {
  2295. stockOut.T_courier_number = T_courier_number
  2296. }
  2297. 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")
  2298. if err != nil {
  2299. o.Rollback()
  2300. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
  2301. c.ServeJSON()
  2302. return
  2303. }
  2304. NatsServer.AddUserLogs(c.User.T_uuid, "出库", "修改发货单", stockOut)
  2305. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  2306. c.ServeJSON()
  2307. return
  2308. }
  2309. func (c *StockController) StockOut_Edit() {
  2310. T_number := c.GetString("T_number") // 出库单号
  2311. //T_depot_id, _ := c.GetInt("T_depot_id")
  2312. T_date := c.GetString("T_date")
  2313. T_receive := c.GetString("T_receive")
  2314. T_project := c.GetString("T_project")
  2315. T_product := c.GetString("T_product")
  2316. T_remark := c.GetString("T_remark")
  2317. date, is := lib.DateStrToTime(T_date)
  2318. if !is {
  2319. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  2320. c.ServeJSON()
  2321. return
  2322. }
  2323. if date.Year() != time.Now().Year() && date.Month() != time.Now().Month() {
  2324. T_date = ""
  2325. }
  2326. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "修改出库", T_product)
  2327. o := orm.NewOrm()
  2328. o.Begin()
  2329. StockOutDao := Stock.NewStockOut(o)
  2330. StockOutProductDao := Stock.NewStockOutProduct(o)
  2331. StockDao := Stock.NewStock(o)
  2332. DeviceDao := Stock.NewDevice(o)
  2333. IotCardDao := Property.NewIotCard(&o)
  2334. // 查询出库信息
  2335. StockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  2336. if err != nil {
  2337. o.Rollback()
  2338. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  2339. c.ServeJSON()
  2340. return
  2341. }
  2342. T_old_date := StockOut.T_date
  2343. // 查询出库产品信息
  2344. productOldList := StockOutProductDao.Read_StockOutProduct_List(StockOut.T_number)
  2345. var oldProductList []dto.StockProduct
  2346. var newProductList []dto.StockProduct
  2347. productOldMap := map[int]dto.StockProduct{}
  2348. allProductListMap := make(map[int]struct{})
  2349. for _, product := range productOldList {
  2350. stockProduct := dto.StockProduct{
  2351. T_product_id: product.T_product_id,
  2352. T_num: product.T_num,
  2353. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  2354. }
  2355. oldProductList = append(oldProductList, stockProduct)
  2356. productOldMap[product.T_product_id] = stockProduct
  2357. if _, ok := allProductListMap[product.T_product_id]; !ok {
  2358. allProductListMap[product.T_product_id] = struct{}{}
  2359. }
  2360. }
  2361. productNewList := lib.SplitString(T_product, "|")
  2362. productNewMap := map[int]dto.StockProduct{}
  2363. for _, v := range productNewList {
  2364. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  2365. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  2366. T_relation_sn := strings.Split(v, "-")[2]
  2367. snList := lib.SplitString(T_relation_sn, ",")
  2368. if len(snList) > 0 {
  2369. num = len(snList)
  2370. }
  2371. stockProduct := dto.StockProduct{
  2372. T_product_id: product_id,
  2373. T_num: num,
  2374. T_relation_sn: snList,
  2375. }
  2376. newProductList = append(newProductList, stockProduct)
  2377. productNewMap[product_id] = stockProduct
  2378. if _, ok := allProductListMap[product_id]; !ok {
  2379. allProductListMap[product_id] = struct{}{}
  2380. }
  2381. }
  2382. // 判断产品列表信息是否相同
  2383. StockProductListIsEqual, needDelete, needAdd, needEdit, snDiff := dto.StockProductListsEqual(oldProductList, newProductList)
  2384. // 两次提交的数据相同,则不用修改
  2385. if !StockProductListIsEqual {
  2386. if len(needDelete) > 0 {
  2387. // 删除入库产品列表
  2388. for _, v := range needDelete {
  2389. product, _ := Basic.Read_Product_ById(v.T_product_id)
  2390. // 删除入库产品列表
  2391. err = StockOutProductDao.Delete_StockOutProduct(StockOut.T_number, StockOut.T_depot_id, v.T_product_id)
  2392. if err != nil {
  2393. o.Rollback()
  2394. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  2395. c.ServeJSON()
  2396. return
  2397. }
  2398. if product.T_relation_sn == 1 {
  2399. // 删除该入库单关联sn
  2400. for _, sn := range v.T_relation_sn {
  2401. err = DeviceDao.Delete_Device_ByT_out_number(StockOut.T_number, sn)
  2402. if err != nil {
  2403. o.Rollback()
  2404. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库设备失败"}
  2405. c.ServeJSON()
  2406. return
  2407. }
  2408. }
  2409. }
  2410. // 增加库存
  2411. _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 2)
  2412. if err != nil {
  2413. o.Rollback()
  2414. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
  2415. c.ServeJSON()
  2416. return
  2417. }
  2418. }
  2419. }
  2420. if len(needAdd) > 0 {
  2421. // 新增入库产品列表
  2422. for _, v := range needAdd {
  2423. product, _ := Basic.Read_Product_ById(v.T_product_id)
  2424. if product.T_relation_sn == 1 && len(v.T_relation_sn) == 0 {
  2425. o.Rollback()
  2426. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  2427. c.ServeJSON()
  2428. return
  2429. }
  2430. // 添加该入库单关联sn
  2431. for _, sn := range v.T_relation_sn {
  2432. mqtt := Stock.Read_MqttUser(sn)
  2433. // 添加设备
  2434. device := Stock.Device{
  2435. T_contract_number: "",
  2436. T_out_number: StockOut.T_number,
  2437. T_product_id: v.T_product_id,
  2438. T_in_number: "",
  2439. T_sn: sn,
  2440. T_iccid: mqtt.Iccid,
  2441. T_imei: mqtt.Imei,
  2442. T_State: 1,
  2443. CreateTime: date,
  2444. UpdateTime: StockOut.UpdateTime,
  2445. }
  2446. _, err = DeviceDao.AddOrUpdate_Device(device, 1)
  2447. if err != nil {
  2448. o.Rollback()
  2449. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  2450. c.ServeJSON()
  2451. return
  2452. }
  2453. // 添加物联网卡
  2454. iotCard := Property.IotCard{
  2455. T_iccid: mqtt.Iccid,
  2456. T_sn: sn,
  2457. T_State: 2, //1-未使用 2-已使用 3-已作废
  2458. T_type: "4G",
  2459. }
  2460. _, err = IotCardDao.AddOrUpdate_IotCard(iotCard)
  2461. if err != nil {
  2462. o.Rollback()
  2463. c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库失败"}
  2464. c.ServeJSON()
  2465. return
  2466. }
  2467. }
  2468. StockOutProduct := Stock.StockOutProduct{
  2469. T_number: T_number,
  2470. T_product_id: v.T_product_id,
  2471. T_depot_id: StockOut.T_depot_id,
  2472. T_num: v.T_num, // 入库数量
  2473. T_date: StockOut.T_date, // 入库日期
  2474. T_relation_sn: strings.Join(v.T_relation_sn, ","),
  2475. T_state: 1,
  2476. }
  2477. _, err = StockOutProductDao.Add_StockOutProduct(StockOutProduct)
  2478. if err != nil {
  2479. o.Rollback()
  2480. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  2481. c.ServeJSON()
  2482. return
  2483. }
  2484. // 减少库存
  2485. _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
  2486. if err != nil {
  2487. o.Rollback()
  2488. c.Data["json"] = lib.JSONS{Code: 203, Msg: "增加库存失败"}
  2489. c.ServeJSON()
  2490. return
  2491. }
  2492. }
  2493. }
  2494. if len(needEdit) > 0 {
  2495. for _, v := range needEdit {
  2496. product, _ := Basic.Read_Product_ById(v.T_product_id)
  2497. StockOutProduct := Stock.StockOutProduct{
  2498. T_number: StockOut.T_number,
  2499. T_product_id: v.T_product_id,
  2500. T_depot_id: StockOut.T_depot_id,
  2501. T_num: productNewMap[v.T_product_id].T_num, // 入库数量
  2502. T_date: StockOut.T_date, // 入库日期
  2503. T_state: 1,
  2504. }
  2505. // 出库数量比之前多,减少库存
  2506. var T_type int
  2507. var T_num int
  2508. if productNewMap[v.T_product_id].T_num > productOldMap[v.T_product_id].T_num {
  2509. // 减少库存
  2510. T_type = 1
  2511. T_num = productNewMap[v.T_product_id].T_num - productOldMap[v.T_product_id].T_num
  2512. } else {
  2513. // 增加库存
  2514. T_type = 2
  2515. T_num = productOldMap[v.T_product_id].T_num - productNewMap[v.T_product_id].T_num
  2516. }
  2517. _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, T_num, T_type)
  2518. if err != nil {
  2519. o.Rollback()
  2520. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  2521. c.ServeJSON()
  2522. return
  2523. }
  2524. // 更新产品库存表
  2525. err = StockOutProductDao.Update_StockOutProduct(StockOutProduct)
  2526. if err != nil {
  2527. o.Rollback()
  2528. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改出库产品信息失败"}
  2529. c.ServeJSON()
  2530. return
  2531. }
  2532. }
  2533. }
  2534. for _, diff := range snDiff {
  2535. product, _ := Basic.Read_Product_ById(diff.T_product_id)
  2536. // 删除减少的sn
  2537. if len(diff.T_delete_relation_sn) > 0 {
  2538. for _, sn := range diff.T_delete_relation_sn {
  2539. err = DeviceDao.Delete_Device_ByT_out_number(StockOut.T_number, sn)
  2540. if err != nil {
  2541. o.Rollback()
  2542. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  2543. c.ServeJSON()
  2544. return
  2545. }
  2546. }
  2547. // 增加库存
  2548. _, 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)
  2549. if err != nil {
  2550. o.Rollback()
  2551. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  2552. c.ServeJSON()
  2553. return
  2554. }
  2555. }
  2556. if len(diff.T_add_relation_sn) > 0 {
  2557. for _, sn := range diff.T_add_relation_sn {
  2558. mqtt := Stock.Read_MqttUser(sn)
  2559. // 添加设备
  2560. device := Stock.Device{
  2561. T_contract_number: "",
  2562. T_out_number: StockOut.T_number,
  2563. T_product_id: diff.T_product_id,
  2564. T_in_number: "",
  2565. T_sn: sn,
  2566. T_iccid: mqtt.Iccid,
  2567. T_imei: mqtt.Imei,
  2568. T_State: 1,
  2569. CreateTime: date,
  2570. UpdateTime: StockOut.UpdateTime,
  2571. }
  2572. _, err = DeviceDao.AddOrUpdate_Device(device, 1)
  2573. if err != nil {
  2574. o.Rollback()
  2575. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  2576. c.ServeJSON()
  2577. return
  2578. }
  2579. // 添加物联网卡
  2580. iotCard := Property.IotCard{
  2581. T_iccid: mqtt.Iccid,
  2582. T_sn: sn,
  2583. T_State: 2, //1-未使用 2-已使用 3-已作废
  2584. T_type: "4G",
  2585. }
  2586. _, err = IotCardDao.AddOrUpdate_IotCard(iotCard)
  2587. if err != nil {
  2588. o.Rollback()
  2589. c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库失败"}
  2590. c.ServeJSON()
  2591. return
  2592. }
  2593. }
  2594. // 减少库存
  2595. _, 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)
  2596. if err != nil {
  2597. o.Rollback()
  2598. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  2599. c.ServeJSON()
  2600. return
  2601. }
  2602. }
  2603. StockOutProduct := Stock.StockOutProduct{
  2604. T_number: StockOut.T_number,
  2605. T_product_id: diff.T_product_id,
  2606. T_depot_id: StockOut.T_depot_id,
  2607. T_num: productNewMap[diff.T_product_id].T_num, // 入库数量
  2608. T_date: StockOut.T_date, // 入库日期
  2609. T_relation_sn: strings.Join(productNewMap[diff.T_product_id].T_relation_sn, ","),
  2610. T_state: 1,
  2611. }
  2612. // 更新产品库存表
  2613. err = StockOutProductDao.Update_StockOutProduct(StockOutProduct)
  2614. if err != nil {
  2615. o.Rollback()
  2616. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改出库产品信息失败"}
  2617. c.ServeJSON()
  2618. return
  2619. }
  2620. }
  2621. }
  2622. if len(T_remark) > 0 {
  2623. StockOut.T_remark = T_remark
  2624. }
  2625. if len(T_receive) > 0 {
  2626. StockOut.T_receive = T_receive
  2627. }
  2628. if len(T_project) > 0 {
  2629. StockOut.T_project = T_project
  2630. }
  2631. if len(T_date) > 0 {
  2632. StockOut.T_date = T_date
  2633. }
  2634. err = StockOutDao.Update_StockOut(StockOut, "T_remark", "T_receive", "T_project", "T_date")
  2635. if err != nil {
  2636. o.Rollback()
  2637. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库失败"}
  2638. c.ServeJSON()
  2639. return
  2640. }
  2641. o.Commit()
  2642. if len(T_date) > 0 && T_old_date != T_date {
  2643. // 修改出库产品日期
  2644. StockOutProductDao.Update_StockOutProduct_T_date(StockOut.T_number, T_date)
  2645. DeviceDao.Update_Device_CreateTimeByT_out_number(StockOut.T_number, T_date)
  2646. }
  2647. if len(T_project) > 0 {
  2648. DeviceDao.Update_Device_ByT_out_number_T_project(StockOut.T_number, T_project)
  2649. }
  2650. // 更新月份统计表
  2651. allProductList := []int{}
  2652. for i, _ := range allProductListMap {
  2653. allProductList = append(allProductList, i)
  2654. }
  2655. StockOut_Edit_StockMonth(StockOut.T_date, StockOut.T_depot_id, allProductList)
  2656. NatsServer.AddUserLogs(c.User.T_uuid, "出库", "修改", StockOut)
  2657. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  2658. c.ServeJSON()
  2659. return
  2660. }
  2661. // 删除出库
  2662. func (c *StockController) StockOut_Del() {
  2663. T_number := c.GetString("T_number") // 入库单号
  2664. o := orm.NewOrm()
  2665. o.Begin()
  2666. StockOutDao := Stock.NewStockOut(o)
  2667. StockOutProductDao := Stock.NewStockOutProduct(o)
  2668. StockDao := Stock.NewStock(o)
  2669. DeviceDao := Stock.NewDevice(o)
  2670. // 查询出库信息
  2671. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  2672. if err != nil {
  2673. o.Rollback()
  2674. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  2675. c.ServeJSON()
  2676. return
  2677. }
  2678. // 查询出库产品信息
  2679. productOldList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  2680. var oldProductList []dto.StockProduct
  2681. allProductList := []int{}
  2682. for _, product := range productOldList {
  2683. oldProductList = append(oldProductList, dto.StockProduct{
  2684. T_product_id: product.T_product_id,
  2685. T_num: product.T_num,
  2686. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  2687. })
  2688. allProductList = append(allProductList, product.T_product_id)
  2689. }
  2690. // 删除入库产品列表
  2691. for _, v := range oldProductList {
  2692. product, _ := Basic.Read_Product_ById(v.T_product_id)
  2693. // 删除入库产品列表
  2694. err = StockOutProductDao.Delete_StockOutProduct(stockOut.T_number, stockOut.T_depot_id, v.T_product_id)
  2695. if err != nil {
  2696. o.Rollback()
  2697. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品出库信息失败!"}
  2698. c.ServeJSON()
  2699. return
  2700. }
  2701. if product.T_relation_sn == 1 {
  2702. // 删除该入库单关联sn
  2703. for _, sn := range v.T_relation_sn {
  2704. err = DeviceDao.Delete_Device_ByT_out_number(stockOut.T_number, sn)
  2705. if err != nil {
  2706. o.Rollback()
  2707. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除出库设备失败"}
  2708. c.ServeJSON()
  2709. return
  2710. }
  2711. }
  2712. }
  2713. // 增加库存
  2714. _, err = StockDao.AddOrUpdate_Stock(stockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 2)
  2715. if err != nil {
  2716. o.Rollback()
  2717. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
  2718. c.ServeJSON()
  2719. return
  2720. }
  2721. }
  2722. err = StockOutDao.Delete_StockOut(stockOut)
  2723. if err != nil {
  2724. o.Rollback()
  2725. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除出库失败"}
  2726. c.ServeJSON()
  2727. return
  2728. }
  2729. o.Commit()
  2730. // 更新月份统计表
  2731. StockOut_Edit_StockMonth(stockOut.T_date, stockOut.T_depot_id, allProductList)
  2732. NatsServer.AddUserLogs(c.User.T_uuid, "出库", "删除", stockOut)
  2733. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  2734. c.ServeJSON()
  2735. return
  2736. }
  2737. // 导出出库单
  2738. func (c *StockController) StockOut_Excel() {
  2739. // 查询
  2740. T_number := c.GetString("T_number")
  2741. o := orm.NewOrm()
  2742. StockOutDao := Stock.NewStockOut(o)
  2743. StockOutProductDao := Stock.NewStockOutProduct(o)
  2744. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  2745. if err != nil {
  2746. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  2747. c.ServeJSON()
  2748. return
  2749. }
  2750. productList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  2751. //productList = append(productList, productList...)
  2752. //productList = append(productList, productList...)
  2753. //productList = append(productList, productList...)
  2754. var pList []Stock.StockOutProduct_R
  2755. for _, v := range productList {
  2756. pList = append(pList, Stock.StockOutProductToStockOutProduct_R(v))
  2757. }
  2758. userList, _ := NatsServer.Read_User_List_All()
  2759. Account.Read_User_All_Map(userList)
  2760. stockOutDetail := Stock.StockOutToStockOut_Detail(stockOut, pList)
  2761. T_date, _ := lib.DateStrToTime(stockOut.T_date)
  2762. filename := fmt.Sprintf("冷链产品出库单(%s)", lib.GetRandstring(6, "0123456789", 0))
  2763. f := excelize.NewFile() // 设置单元格的值
  2764. Style1, _ := f.NewStyle(
  2765. &excelize.Style{
  2766. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  2767. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  2768. })
  2769. Style2, _ := f.NewStyle(
  2770. &excelize.Style{
  2771. Font: &excelize.Font{Size: 11, Family: "宋体"},
  2772. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  2773. })
  2774. Style3, _ := f.NewStyle(
  2775. &excelize.Style{
  2776. Font: &excelize.Font{Size: 10, Family: "宋体", Bold: true},
  2777. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  2778. Border: []excelize.Border{
  2779. {Type: "left", Color: "000000", Style: 1},
  2780. {Type: "top", Color: "000000", Style: 1},
  2781. {Type: "bottom", Color: "000000", Style: 1},
  2782. {Type: "right", Color: "000000", Style: 1},
  2783. },
  2784. })
  2785. Style4, _ := f.NewStyle(
  2786. &excelize.Style{
  2787. Font: &excelize.Font{Size: 11, Family: "宋体"},
  2788. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  2789. Border: []excelize.Border{
  2790. {Type: "left", Color: "000000", Style: 1},
  2791. {Type: "top", Color: "000000", Style: 1},
  2792. {Type: "bottom", Color: "000000", Style: 1},
  2793. {Type: "right", Color: "000000", Style: 1},
  2794. },
  2795. })
  2796. productList2 := lib.ChunkBy(productList, 10)
  2797. for i, products := range productList2 {
  2798. if len(products) < 10 {
  2799. products = append(products, make([]Stock.StockOutProduct, 10-len(products))...)
  2800. }
  2801. row := i*17 + 1
  2802. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2803. f.SetRowStyle("Sheet1", row, row, Style1)
  2804. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("宝智达科技产品进销存统计表"))
  2805. f.SetRowHeight("Sheet1", row, 20)
  2806. row += 1
  2807. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2808. f.SetRowStyle("Sheet1", row, row, Style1)
  2809. title := "硬件出库单"
  2810. if len(productList2) > 1 {
  2811. title += fmt.Sprintf(" %d/%d", i+1, len(productList2))
  2812. }
  2813. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), title)
  2814. f.SetRowHeight("Sheet1", row, 20)
  2815. row += 1
  2816. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2817. f.SetRowStyle("Sheet1", row, row, Style2)
  2818. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("出库单号:%s", stockOut.T_number))
  2819. f.SetRowHeight("Sheet1", row, 20)
  2820. row += 1
  2821. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2822. f.SetRowStyle("Sheet1", row, row, Style2)
  2823. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("项目名称:%s", stockOut.T_project))
  2824. f.SetRowHeight("Sheet1", row, 20)
  2825. row += 1
  2826. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2827. f.SetRowStyle("Sheet1", row, row, Style2)
  2828. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("领用时间:%s", T_date.Format("2006年01月02日")))
  2829. f.SetRowHeight("Sheet1", row, 20)
  2830. // 这里设置表头
  2831. row += 1
  2832. f.SetRowHeight("Sheet1", row, 20)
  2833. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "序号")
  2834. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), "产品名称")
  2835. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), "型号")
  2836. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), "单位")
  2837. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), "领用数量")
  2838. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), "备注")
  2839. // 设置列宽
  2840. f.SetColWidth("Sheet1", "A", "A", 5)
  2841. f.SetColWidth("Sheet1", "B", "B", 20)
  2842. f.SetColWidth("Sheet1", "C", "C", 12)
  2843. f.SetColWidth("Sheet1", "D", "D", 10)
  2844. f.SetColWidth("Sheet1", "E", "E", 10)
  2845. f.SetColWidth("Sheet1", "F", "F", 15)
  2846. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row), Style3)
  2847. sRow := row
  2848. // 循环写入数据
  2849. for j, v := range products {
  2850. row += 1
  2851. product, _ := Basic.Read_Product_ById(v.T_product_id)
  2852. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), j+1)
  2853. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), product.T_name)
  2854. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), product.T_model)
  2855. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), product.T_spec)
  2856. if v.T_num > 0 {
  2857. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), v.T_num)
  2858. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), stockOut.T_remark)
  2859. }
  2860. }
  2861. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", sRow), fmt.Sprintf("F%d", row), Style4)
  2862. row += 1
  2863. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("C%d", row))
  2864. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("制单:%s", c.User.T_name))
  2865. f.MergeCell("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("F%d", row))
  2866. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("领用人:%s", stockOutDetail.T_receive_name))
  2867. f.SetRowStyle("Sheet1", row, row, Style2)
  2868. f.SetRowHeight("Sheet1", row, 20)
  2869. }
  2870. // 保存文件
  2871. if err = f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  2872. fmt.Println(err)
  2873. }
  2874. var url string
  2875. //// 上传 OSS
  2876. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  2877. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  2878. if !is {
  2879. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  2880. c.ServeJSON()
  2881. return
  2882. }
  2883. //删除目录
  2884. err = os.Remove("ofile/" + filename + ".xlsx")
  2885. if err != nil {
  2886. logs.Error(lib.FuncName(), err)
  2887. }
  2888. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  2889. c.ServeJSON()
  2890. return
  2891. }
  2892. func (c *StockController) StockOut_Excel_Batch() {
  2893. // 查询
  2894. T_number_list := c.GetString("T_number_list")
  2895. o := orm.NewOrm()
  2896. StockOutDao := Stock.NewStockOut(o)
  2897. StockOutProductDao := Stock.NewStockOutProduct(o)
  2898. list := strings.Split(strings.Trim(T_number_list, "|"), "|")
  2899. userList, _ := NatsServer.Read_User_List_All()
  2900. Account.Read_User_All_Map(userList)
  2901. filename := fmt.Sprintf("冷链产品出库单(%s)", lib.GetRandstring(6, "0123456789", 0))
  2902. f := excelize.NewFile() // 设置单元格的值
  2903. Style1, _ := f.NewStyle(
  2904. &excelize.Style{
  2905. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  2906. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  2907. })
  2908. Style2, _ := f.NewStyle(
  2909. &excelize.Style{
  2910. Font: &excelize.Font{Size: 11, Family: "宋体"},
  2911. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  2912. })
  2913. Style3, _ := f.NewStyle(
  2914. &excelize.Style{
  2915. Font: &excelize.Font{Size: 10, Family: "宋体", Bold: true},
  2916. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  2917. Border: []excelize.Border{
  2918. {Type: "left", Color: "000000", Style: 1},
  2919. {Type: "top", Color: "000000", Style: 1},
  2920. {Type: "bottom", Color: "000000", Style: 1},
  2921. {Type: "right", Color: "000000", Style: 1},
  2922. },
  2923. })
  2924. Style4, _ := f.NewStyle(
  2925. &excelize.Style{
  2926. Font: &excelize.Font{Size: 11, Family: "宋体"},
  2927. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  2928. Border: []excelize.Border{
  2929. {Type: "left", Color: "000000", Style: 1},
  2930. {Type: "top", Color: "000000", Style: 1},
  2931. {Type: "bottom", Color: "000000", Style: 1},
  2932. {Type: "right", Color: "000000", Style: 1},
  2933. },
  2934. })
  2935. var num int
  2936. for _, T_number := range list {
  2937. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  2938. if err != nil {
  2939. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  2940. c.ServeJSON()
  2941. return
  2942. }
  2943. productList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  2944. var pList []Stock.StockOutProduct_R
  2945. for _, v := range productList {
  2946. pList = append(pList, Stock.StockOutProductToStockOutProduct_R(v))
  2947. }
  2948. stockOutDetail := Stock.StockOutToStockOut_Detail(stockOut, pList)
  2949. T_date, _ := lib.DateStrToTime(stockOut.T_date)
  2950. productList2 := lib.ChunkBy(productList, 10)
  2951. for i, products := range productList2 {
  2952. if len(products) < 10 {
  2953. products = append(products, make([]Stock.StockOutProduct, 10-len(products))...)
  2954. }
  2955. row := num*18 + 1
  2956. num += 1
  2957. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2958. f.SetRowStyle("Sheet1", row, row, Style1)
  2959. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("宝智达科技产品进销存统计表"))
  2960. f.SetRowHeight("Sheet1", row, 20)
  2961. row += 1
  2962. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2963. f.SetRowStyle("Sheet1", row, row, Style1)
  2964. title := "硬件出库单"
  2965. if len(productList2) > 1 {
  2966. title += fmt.Sprintf(" %d/%d", i+1, len(productList2))
  2967. }
  2968. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), title)
  2969. f.SetRowHeight("Sheet1", row, 20)
  2970. row += 1
  2971. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2972. f.SetRowStyle("Sheet1", row, row, Style2)
  2973. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("出库单号:%s", stockOut.T_number))
  2974. f.SetRowHeight("Sheet1", row, 20)
  2975. row += 1
  2976. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2977. f.SetRowStyle("Sheet1", row, row, Style2)
  2978. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("项目名称:%s", stockOut.T_project))
  2979. f.SetRowHeight("Sheet1", row, 20)
  2980. row += 1
  2981. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2982. f.SetRowStyle("Sheet1", row, row, Style2)
  2983. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("领用时间:%s", T_date.Format("2006年01月02日")))
  2984. f.SetRowHeight("Sheet1", row, 20)
  2985. // 这里设置表头
  2986. row += 1
  2987. f.SetRowHeight("Sheet1", row, 20)
  2988. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "序号")
  2989. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), "产品名称")
  2990. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), "型号")
  2991. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), "单位")
  2992. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), "领用数量")
  2993. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), "备注")
  2994. // 设置列宽
  2995. f.SetColWidth("Sheet1", "A", "A", 5)
  2996. f.SetColWidth("Sheet1", "B", "B", 20)
  2997. f.SetColWidth("Sheet1", "C", "C", 12)
  2998. f.SetColWidth("Sheet1", "D", "D", 10)
  2999. f.SetColWidth("Sheet1", "E", "E", 10)
  3000. f.SetColWidth("Sheet1", "F", "F", 15)
  3001. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row), Style3)
  3002. sRow := row
  3003. // 循环写入数据
  3004. for j, v := range products {
  3005. row += 1
  3006. product, _ := Basic.Read_Product_ById(v.T_product_id)
  3007. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), j+1)
  3008. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), product.T_name)
  3009. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), product.T_model)
  3010. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), product.T_spec)
  3011. if v.T_num > 0 {
  3012. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), v.T_num)
  3013. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), stockOut.T_remark)
  3014. }
  3015. }
  3016. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", sRow), fmt.Sprintf("F%d", row), Style4)
  3017. row += 1
  3018. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("C%d", row))
  3019. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("制单:%s", c.User.T_name))
  3020. f.MergeCell("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("F%d", row))
  3021. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("领用人:%s", stockOutDetail.T_receive_name))
  3022. f.SetRowStyle("Sheet1", row, row, Style2)
  3023. f.SetRowHeight("Sheet1", row, 20)
  3024. // 空一行
  3025. row += 1
  3026. //f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  3027. //f.SetRowStyle("Sheet1", row, row, Style1)
  3028. //f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "")
  3029. // 插入下一页分页符
  3030. f.InsertPageBreak("Sheet1", fmt.Sprintf("A%d", row))
  3031. }
  3032. }
  3033. // 保存文件
  3034. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  3035. fmt.Println(err)
  3036. }
  3037. var url string
  3038. //// 上传 OSS
  3039. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  3040. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  3041. if !is {
  3042. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  3043. c.ServeJSON()
  3044. return
  3045. }
  3046. //删除目录
  3047. err := os.Remove("ofile/" + filename + ".xlsx")
  3048. if err != nil {
  3049. logs.Error(lib.FuncName(), err)
  3050. }
  3051. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  3052. c.ServeJSON()
  3053. return
  3054. }
  3055. func StockOut_Edit_StockMonth(T_date string, T_depot_id int, allProductList []int) error {
  3056. date, _ := lib.DateStrToTime(T_date)
  3057. //if date.Year() == time.Now().Year() && date.Month() == time.Now().Month() {
  3058. // // 修改本月数据不同步库存
  3059. // return nil
  3060. //}
  3061. //T_month := date.Format("2006-01")
  3062. months := generateMonthList(date, "out")
  3063. o := orm.NewOrm()
  3064. StockOutDao := Stock.NewStockOut(o)
  3065. StockMonthDao := Stock.NewStockMonth(o)
  3066. StockOutProductDao := Stock.NewStockOutProduct(o)
  3067. StockInProductDao := Stock.NewStockInProduct(o)
  3068. for _, T_product_id := range allProductList {
  3069. for _, T_month := range months {
  3070. // 获取当前产品本月出入库数量
  3071. lastMonth := StockMonthDao.Read_LastMonth_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
  3072. stockMonth := StockMonthDao.Read_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
  3073. if stockMonth.Id > 0 {
  3074. // 修改
  3075. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(T_depot_id, T_product_id, T_month)
  3076. if len(lastMonth) > 0 {
  3077. stockMonth.T_beginning = lastMonth[0].T_ending
  3078. } else {
  3079. stockMonth.T_beginning = 0
  3080. }
  3081. // 期末库存 = 期初库存+入库-出库
  3082. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  3083. err := StockMonthDao.Update_StockMonth(stockMonth, "T_out", "T_beginning", "T_ending")
  3084. if err != nil {
  3085. return err
  3086. }
  3087. } else {
  3088. product, _ := Basic.Read_Product_ById(T_product_id)
  3089. // 添加库存记录
  3090. stockMonth = Stock.StockMonth{
  3091. T_depot_id: T_depot_id,
  3092. T_product_id: T_product_id,
  3093. T_product_class: product.T_class,
  3094. T_month: T_month,
  3095. }
  3096. // 获取当前产品本月出入库数量
  3097. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(T_depot_id, T_product_id, T_month)
  3098. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(T_depot_id, T_product_id, T_month)
  3099. if len(lastMonth) > 0 {
  3100. stockMonth.T_beginning = lastMonth[0].T_ending
  3101. } else {
  3102. stockMonth.T_beginning = 0
  3103. }
  3104. // 期末库存 = 期初库存+入库-出库
  3105. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  3106. stockMonth.T_project = StockOutDao.Read_StockOut_T_contract_number(T_depot_id, T_product_id, T_month)
  3107. _, err := StockMonthDao.Add_StockMonth(stockMonth)
  3108. if err != nil {
  3109. return err
  3110. }
  3111. }
  3112. }
  3113. }
  3114. return nil
  3115. }
  3116. func Cron_StockMonth() {
  3117. //创建一个定时任务对象
  3118. c := cron.New(cron.WithSeconds())
  3119. //给对象增加定时任务
  3120. // @monthly 每月运行一次,每月第一天午夜 0 0 0 1 * *
  3121. //c.AddFunc("0 */1 * * * ?", Cron_StockMonth_Add)
  3122. c.AddFunc("@monthly", Cron_StockMonth_Add)
  3123. //启动定时任务
  3124. c.Start()
  3125. defer c.Stop()
  3126. //查询语句,阻塞,让main函数不退出,保持程序运行
  3127. select {}
  3128. }
  3129. // 保存每月入库出库明细
  3130. func Cron_StockMonth_Add() {
  3131. T_month := time.Now().Format("2006-01")
  3132. //T_month := time.Now().AddDate(0, -1, 0).Format("2006-01")
  3133. logs.Info("开始统计" + T_month + "库存明细数据")
  3134. o := orm.NewOrm()
  3135. StockDao := Stock.NewStock(o)
  3136. StockOutDao := Stock.NewStockOut(o)
  3137. StockMonthDao := Stock.NewStockMonth(o)
  3138. StockOutProductDao := Stock.NewStockOutProduct(o)
  3139. StockInProductDao := Stock.NewStockInProduct(o)
  3140. stockList, _ := StockDao.Read_Stock_List(0, 0, "", "", 0, 9999)
  3141. for _, stock := range stockList {
  3142. lastMonth := StockMonthDao.Read_LastMonth_StockMonth_ByT_depot_id_T_product_id(stock.T_depot_id, stock.T_product_id, T_month)
  3143. stockMonth := StockMonthDao.Read_StockMonth_ByT_depot_id_T_product_id(stock.T_depot_id, stock.T_product_id, T_month)
  3144. if stockMonth.Id > 0 {
  3145. // 修改
  3146. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(stock.T_depot_id, stock.T_product_id, T_month)
  3147. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(stock.T_depot_id, stock.T_product_id, T_month)
  3148. if len(lastMonth) > 0 {
  3149. stockMonth.T_beginning = lastMonth[0].T_ending
  3150. } else {
  3151. stockMonth.T_beginning = 0
  3152. }
  3153. // 期末库存 = 期初库存+入库-出库
  3154. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  3155. err := StockMonthDao.Update_StockMonth(stockMonth, "T_in", "T_out", "T_beginning", "T_ending")
  3156. if err != nil {
  3157. NatsServer.AddSysLogs("库存明细统计", fmt.Sprintf("库存明细统计失败%s:%d:%d", T_month, stock.T_depot_id, stock.T_product_id), stockMonth)
  3158. }
  3159. } else {
  3160. stockMonth = Stock.StockMonth{
  3161. T_depot_id: stock.T_depot_id,
  3162. T_product_id: stock.T_product_id,
  3163. T_product_class: stock.T_product_class,
  3164. T_month: T_month,
  3165. }
  3166. // 获取当前产品本月出入库数量
  3167. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(stock.T_depot_id, stock.T_product_id, T_month)
  3168. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(stock.T_depot_id, stock.T_product_id, T_month)
  3169. if len(lastMonth) > 0 {
  3170. stockMonth.T_beginning = lastMonth[0].T_ending
  3171. } else {
  3172. stockMonth.T_beginning = 0
  3173. }
  3174. // 期末库存 = 期初库存+入库-出库
  3175. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  3176. stockMonth.T_project = StockOutDao.Read_StockOut_T_contract_number(stock.T_depot_id, stock.T_product_id, T_month)
  3177. _, err := StockMonthDao.Add_StockMonth(stockMonth)
  3178. if err != nil {
  3179. NatsServer.AddSysLogs("库存明细统计", fmt.Sprintf("库存明细统计失败%s:%d:%d", T_month, stock.T_depot_id, stock.T_product_id), stockMonth)
  3180. }
  3181. }
  3182. }
  3183. }
  3184. func generateMonthList(startMonth time.Time, t_type string) []string {
  3185. var months []string
  3186. endMonth := time.Now()
  3187. if t_type == "in" {
  3188. endMonth = endMonth.AddDate(0, 1, 0)
  3189. }
  3190. for month := startMonth; month.Before(endMonth); month = month.AddDate(0, 1, 0) {
  3191. months = append(months, month.Format("2006-01"))
  3192. }
  3193. return months
  3194. }
  3195. // 我的申请列表
  3196. func (c *StockController) StockOut_Apply_List() {
  3197. // 分页参数 初始化
  3198. page, _ := c.GetInt("page")
  3199. if page < 1 {
  3200. page = 1
  3201. }
  3202. page_z, _ := c.GetInt("page_z")
  3203. if page_z < 1 {
  3204. page_z = conf.Page_size
  3205. }
  3206. // 查询
  3207. T_depot_id, _ := c.GetInt("T_depot_id")
  3208. T_state, _ := c.GetInt("T_state")
  3209. T_contract_number := c.GetString("T_contract_number")
  3210. T_start_date := c.GetString("T_start_date")
  3211. T_end_date := c.GetString("T_end_date")
  3212. T_name := c.GetString("T_name")
  3213. userList, _ := NatsServer.Read_User_List_All()
  3214. Account.Read_User_All_Map(userList)
  3215. Basic.Read_Depot_All_Map()
  3216. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  3217. R_List, R_cnt := StockOutDao.Read_StockOut_Audit_List(c.User.T_uuid, T_name, Stock.StockOutTypeApply, T_state, T_depot_id, T_contract_number, T_start_date, T_end_date, page, page_z)
  3218. var r_jsons lib.R_JSONS
  3219. r_jsons.Num = R_cnt
  3220. r_jsons.Data = R_List
  3221. r_jsons.Page = page
  3222. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  3223. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  3224. c.ServeJSON()
  3225. return
  3226. }
  3227. // 出库申请
  3228. func (c *StockController) StockOut_Apply() {
  3229. rand_x := 0
  3230. T_number := ""
  3231. o := orm.NewOrm()
  3232. StockOutDao := Stock.NewStockOut(o)
  3233. for true {
  3234. T_number = "CK-" + lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", int64(rand_x))
  3235. _, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  3236. if err != nil && err.Error() == orm.ErrNoRows.Error() {
  3237. break
  3238. }
  3239. rand_x += 1
  3240. }
  3241. T_contract_number := c.GetString("T_contract_number")
  3242. T_depot_id, _ := c.GetInt("T_depot_id")
  3243. T_product := c.GetString("T_product")
  3244. T_remark := c.GetString("T_remark")
  3245. T_project := c.GetString("T_project")
  3246. T_company_name := c.GetString("T_company_name")
  3247. T_payment_method := c.GetString("T_payment_method")
  3248. if len(T_company_name) == 0 {
  3249. c.Data["json"] = lib.JSONS{Code: 202, Msg: "公司名称不能为空"}
  3250. c.ServeJSON()
  3251. return
  3252. }
  3253. if len(T_payment_method) == 0 {
  3254. c.Data["json"] = lib.JSONS{Code: 202, Msg: "付款方式不能为空"}
  3255. c.ServeJSON()
  3256. return
  3257. }
  3258. var_ := Stock.StockOut{
  3259. T_number: T_number,
  3260. T_contract_number: T_contract_number,
  3261. T_depot_id: T_depot_id,
  3262. T_type: 1,
  3263. T_application_date: time.Now().Format("2006-01-02"),
  3264. T_receive: c.User.T_uuid,
  3265. T_remark: T_remark,
  3266. T_project: T_project,
  3267. T_submit: c.User.T_uuid,
  3268. T_state: Stock.StockOutWaitAudit,
  3269. T_company_name: T_company_name,
  3270. T_payment_method: T_payment_method,
  3271. }
  3272. o.Begin()
  3273. StockOutProductDao := Stock.NewStockOutProduct(o)
  3274. StockDao := Stock.NewStock(o)
  3275. _, err := StockOutDao.Add_StockOut(var_)
  3276. if err != nil {
  3277. o.Rollback()
  3278. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
  3279. c.ServeJSON()
  3280. return
  3281. }
  3282. // 1、添加出库单
  3283. if len(T_product) == 0 {
  3284. o.Rollback()
  3285. c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
  3286. c.ServeJSON()
  3287. return
  3288. }
  3289. productList := lib.SplitString(T_product, "|")
  3290. allProductList := []int{}
  3291. for _, v := range productList {
  3292. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  3293. allProductList = append(allProductList, product_id)
  3294. product, _ := Basic.Read_Product_ById(product_id)
  3295. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  3296. if num == 0 {
  3297. o.Rollback()
  3298. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s出库数量不能为0!", product.T_name)}
  3299. c.ServeJSON()
  3300. return
  3301. }
  3302. // 2、添加出库产品清单
  3303. stockOutProduct := Stock.StockOutProduct{
  3304. T_number: T_number,
  3305. T_product_id: product_id,
  3306. T_depot_id: T_depot_id,
  3307. T_num: num, // 出库数量
  3308. T_state: 2,
  3309. }
  3310. _, err = StockOutProductDao.Add_StockOutProduct(stockOutProduct)
  3311. if err != nil {
  3312. o.Rollback()
  3313. c.Data["json"] = lib.JSONS{Code: 203, Msg: "申请出库失败"}
  3314. c.ServeJSON()
  3315. return
  3316. }
  3317. // 3、增加占用库存
  3318. _, err = StockDao.AddOrUpdate_Occupy_Stock(T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num, 2)
  3319. if err != nil {
  3320. o.Rollback()
  3321. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新占用库存失败" + err.Error()}
  3322. c.ServeJSON()
  3323. return
  3324. }
  3325. }
  3326. o.Commit()
  3327. NatsServer.AddUserLogs(c.User.T_uuid, "出库申请", "出库申请", var_)
  3328. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  3329. c.ServeJSON()
  3330. return
  3331. }
  3332. // 修改出库申请
  3333. func (c *StockController) StockOut_Apply_Edit() {
  3334. T_number := c.GetString("T_number") // 出库单号
  3335. //T_depot_id, _ := c.GetInt("T_depot_id")
  3336. T_project := c.GetString("T_project")
  3337. T_product := c.GetString("T_product")
  3338. T_remark := c.GetString("T_remark")
  3339. T_company_name := c.GetString("T_company_name")
  3340. T_payment_method := c.GetString("T_payment_method")
  3341. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "修改出库", T_product)
  3342. o := orm.NewOrm()
  3343. o.Begin()
  3344. StockOutDao := Stock.NewStockOut(o)
  3345. StockOutProductDao := Stock.NewStockOutProduct(o)
  3346. StockDao := Stock.NewStock(o)
  3347. // 查询入库信息
  3348. StockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  3349. if err != nil {
  3350. o.Rollback()
  3351. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  3352. c.ServeJSON()
  3353. return
  3354. }
  3355. if StockOut.T_state == Stock.StockOutAlreadyOut {
  3356. o.Rollback()
  3357. c.Data["json"] = lib.JSONS{Code: 202, Msg: "当前出库单已出库,不能修改!"}
  3358. c.ServeJSON()
  3359. return
  3360. }
  3361. // 查询入库产品信息
  3362. productOldList := StockOutProductDao.Read_StockOutProduct_List(StockOut.T_number)
  3363. var oldProductList []dto.StockProduct
  3364. var newProductList []dto.StockProduct
  3365. productOldMap := map[int]dto.StockProduct{}
  3366. allProductListMap := make(map[int]struct{})
  3367. for _, product := range productOldList {
  3368. stockProduct := dto.StockProduct{
  3369. T_product_id: product.T_product_id,
  3370. T_num: product.T_num,
  3371. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  3372. }
  3373. oldProductList = append(oldProductList, stockProduct)
  3374. productOldMap[product.T_product_id] = stockProduct
  3375. if _, ok := allProductListMap[product.T_product_id]; !ok {
  3376. allProductListMap[product.T_product_id] = struct{}{}
  3377. }
  3378. if StockOut.T_state != Stock.StockOutAuditManagerUnPass && StockOut.T_state != Stock.StockOutAuditFinanceUnPass {
  3379. // 减少占用库存
  3380. product2, _ := Basic.Read_Product_ById(product.T_product_id)
  3381. _, err = StockDao.AddOrUpdate_Occupy_Stock(StockOut.T_depot_id, product2.Id, product2.T_class, product2.T_name, product2.T_model, product.T_num, 1)
  3382. if err != nil {
  3383. o.Rollback()
  3384. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新占用库存失败"}
  3385. c.ServeJSON()
  3386. return
  3387. }
  3388. }
  3389. }
  3390. productNewList := lib.SplitString(T_product, "|")
  3391. productNewMap := map[int]dto.StockProduct{}
  3392. for _, v := range productNewList {
  3393. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  3394. product, _ := Basic.Read_Product_ById(product_id)
  3395. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  3396. if num == 0 {
  3397. o.Rollback()
  3398. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s出库数量不能为0!", product.T_name)}
  3399. c.ServeJSON()
  3400. return
  3401. }
  3402. T_relation_sn := strings.Split(v, "-")[2]
  3403. stockProduct := dto.StockProduct{
  3404. T_product_id: product_id,
  3405. T_num: num,
  3406. T_relation_sn: lib.SplitString(T_relation_sn, ","),
  3407. }
  3408. newProductList = append(newProductList, stockProduct)
  3409. productNewMap[product_id] = stockProduct
  3410. if _, ok := allProductListMap[product_id]; !ok {
  3411. allProductListMap[product_id] = struct{}{}
  3412. }
  3413. // 增加占用库存
  3414. _, err = StockDao.AddOrUpdate_Occupy_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num, 2)
  3415. if err != nil {
  3416. o.Rollback()
  3417. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新占用库存失败"}
  3418. c.ServeJSON()
  3419. return
  3420. }
  3421. }
  3422. // 判断产品列表信息是否相同
  3423. StockProductListIsEqual, needDelete, needAdd, needEdit, _ := dto.StockProductListsEqual(oldProductList, newProductList)
  3424. // 两次提交的数据相同,则不用修改
  3425. if !StockProductListIsEqual {
  3426. if len(needDelete) > 0 {
  3427. // 删除入库产品列表
  3428. for _, v := range needDelete {
  3429. // 删除入库产品列表
  3430. err = StockOutProductDao.Delete_StockOutProduct(StockOut.T_number, StockOut.T_depot_id, v.T_product_id)
  3431. if err != nil {
  3432. o.Rollback()
  3433. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  3434. c.ServeJSON()
  3435. return
  3436. }
  3437. }
  3438. }
  3439. if len(needAdd) > 0 {
  3440. // 新增入库产品列表
  3441. for _, v := range needAdd {
  3442. StockOutProduct := Stock.StockOutProduct{
  3443. T_number: T_number,
  3444. T_product_id: v.T_product_id,
  3445. T_depot_id: StockOut.T_depot_id,
  3446. T_num: v.T_num, // 出库数量
  3447. T_state: 1,
  3448. }
  3449. _, err = StockOutProductDao.Add_StockOutProduct(StockOutProduct)
  3450. if err != nil {
  3451. o.Rollback()
  3452. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  3453. c.ServeJSON()
  3454. return
  3455. }
  3456. }
  3457. }
  3458. if len(needEdit) > 0 {
  3459. for _, v := range needEdit {
  3460. StockOutProduct := Stock.StockOutProduct{
  3461. T_number: StockOut.T_number,
  3462. T_product_id: v.T_product_id,
  3463. T_depot_id: StockOut.T_depot_id,
  3464. T_num: productNewMap[v.T_product_id].T_num, // 入库数量
  3465. T_state: 1,
  3466. }
  3467. // 更新产品表
  3468. err = StockOutProductDao.Update_StockOutProduct(StockOutProduct)
  3469. if err != nil {
  3470. o.Rollback()
  3471. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改出库产品信息失败"}
  3472. c.ServeJSON()
  3473. return
  3474. }
  3475. }
  3476. }
  3477. }
  3478. if len(T_remark) > 0 {
  3479. StockOut.T_remark = T_remark
  3480. }
  3481. if len(T_project) > 0 {
  3482. StockOut.T_project = T_project
  3483. }
  3484. if len(T_company_name) > 0 {
  3485. StockOut.T_company_name = T_company_name
  3486. }
  3487. if len(T_payment_method) > 0 {
  3488. StockOut.T_payment_method = T_payment_method
  3489. }
  3490. StockOut.T_state = Stock.StockOutWaitAudit
  3491. err = StockOutDao.Update_StockOut(StockOut, "T_remark", "T_receive", "T_project", "T_company_name", "T_payment_method", "T_state")
  3492. if err != nil {
  3493. o.Rollback()
  3494. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库失败"}
  3495. c.ServeJSON()
  3496. return
  3497. }
  3498. o.Commit()
  3499. NatsServer.AddUserLogs(c.User.T_uuid, "出库申请", "修改", StockOut)
  3500. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  3501. c.ServeJSON()
  3502. return
  3503. }
  3504. // 删除出库申请
  3505. func (c *StockController) StockOut_Apply_Del() {
  3506. T_number := c.GetString("T_number") // 入库单号
  3507. o := orm.NewOrm()
  3508. o.Begin()
  3509. StockOutDao := Stock.NewStockOut(o)
  3510. StockOutProductDao := Stock.NewStockOutProduct(o)
  3511. StockDao := Stock.NewStock(o)
  3512. // 查询出库信息
  3513. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  3514. if err != nil {
  3515. o.Rollback()
  3516. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  3517. c.ServeJSON()
  3518. return
  3519. }
  3520. // 查询出库产品信息
  3521. productOldList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  3522. var oldProductList []dto.StockProduct
  3523. allProductList := []int{}
  3524. for _, product := range productOldList {
  3525. oldProductList = append(oldProductList, dto.StockProduct{
  3526. T_product_id: product.T_product_id,
  3527. T_num: product.T_num,
  3528. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  3529. })
  3530. allProductList = append(allProductList, product.T_product_id)
  3531. }
  3532. // 删除入库产品列表
  3533. for _, v := range oldProductList {
  3534. product, _ := Basic.Read_Product_ById(v.T_product_id)
  3535. // 删除入库产品列表
  3536. err = StockOutProductDao.Delete_StockOutProduct(stockOut.T_number, stockOut.T_depot_id, v.T_product_id)
  3537. if err != nil {
  3538. o.Rollback()
  3539. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品出库信息失败!"}
  3540. c.ServeJSON()
  3541. return
  3542. }
  3543. // 减少占用库存
  3544. _, err = StockDao.AddOrUpdate_Occupy_Stock(stockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
  3545. if err != nil {
  3546. o.Rollback()
  3547. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新占用库存失败"}
  3548. c.ServeJSON()
  3549. return
  3550. }
  3551. }
  3552. err = StockOutDao.Delete_StockOut(stockOut)
  3553. if err != nil {
  3554. o.Rollback()
  3555. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除出库失败"}
  3556. c.ServeJSON()
  3557. return
  3558. }
  3559. o.Commit()
  3560. NatsServer.AddUserLogs(c.User.T_uuid, "出库申请", "删除", stockOut)
  3561. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  3562. c.ServeJSON()
  3563. return
  3564. }
  3565. // 财务
  3566. func (c *StockController) StockOut_Finance_List() {
  3567. // 分页参数 初始化
  3568. page, _ := c.GetInt("page")
  3569. if page < 1 {
  3570. page = 1
  3571. }
  3572. page_z, _ := c.GetInt("page_z")
  3573. if page_z < 1 {
  3574. page_z = conf.Page_size
  3575. }
  3576. // 查询
  3577. T_depot_id, _ := c.GetInt("T_depot_id")
  3578. T_state, _ := c.GetInt("T_state")
  3579. T_contract_number := c.GetString("T_contract_number")
  3580. T_start_date := c.GetString("T_start_date")
  3581. T_end_date := c.GetString("T_end_date")
  3582. T_name := c.GetString("T_name")
  3583. userList, _ := NatsServer.Read_User_List_All()
  3584. Account.Read_User_All_Map(userList)
  3585. Basic.Read_Depot_All_Map()
  3586. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  3587. R_List, R_cnt := StockOutDao.Read_StockOut_Audit_List("", T_name, Stock.StockOutTypeFinance, T_state, T_depot_id, T_contract_number, T_start_date, T_end_date, page, page_z)
  3588. var r_jsons lib.R_JSONS
  3589. r_jsons.Num = R_cnt
  3590. r_jsons.Data = R_List
  3591. r_jsons.Page = page
  3592. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  3593. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  3594. c.ServeJSON()
  3595. return
  3596. }
  3597. // 管理员
  3598. func (c *StockController) StockOut_Manager_List() {
  3599. // 分页参数 初始化
  3600. page, _ := c.GetInt("page")
  3601. if page < 1 {
  3602. page = 1
  3603. }
  3604. page_z, _ := c.GetInt("page_z")
  3605. if page_z < 1 {
  3606. page_z = conf.Page_size
  3607. }
  3608. // 查询
  3609. T_depot_id, _ := c.GetInt("T_depot_id")
  3610. T_state, _ := c.GetInt("T_state")
  3611. T_contract_number := c.GetString("T_contract_number")
  3612. T_start_date := c.GetString("T_start_date")
  3613. T_end_date := c.GetString("T_end_date")
  3614. T_name := c.GetString("T_name")
  3615. userList, _ := NatsServer.Read_User_List_All()
  3616. Account.Read_User_All_Map(userList)
  3617. Basic.Read_Depot_All_Map()
  3618. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  3619. R_List, R_cnt := StockOutDao.Read_StockOut_Audit_List("", T_name, Stock.StockOutTypeManager, T_state, T_depot_id, T_contract_number, T_start_date, T_end_date, page, page_z)
  3620. var r_jsons lib.R_JSONS
  3621. r_jsons.Num = R_cnt
  3622. r_jsons.Data = R_List
  3623. r_jsons.Page = page
  3624. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  3625. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  3626. c.ServeJSON()
  3627. return
  3628. }
  3629. // 仓管
  3630. func (c *StockController) StockOut_Warehouse_List() {
  3631. // 分页参数 初始化
  3632. page, _ := c.GetInt("page")
  3633. if page < 1 {
  3634. page = 1
  3635. }
  3636. page_z, _ := c.GetInt("page_z")
  3637. if page_z < 1 {
  3638. page_z = conf.Page_size
  3639. }
  3640. // 查询
  3641. T_depot_id, _ := c.GetInt("T_depot_id")
  3642. T_state, _ := c.GetInt("T_state")
  3643. T_contract_number := c.GetString("T_contract_number")
  3644. T_start_date := c.GetString("T_start_date")
  3645. T_end_date := c.GetString("T_end_date")
  3646. T_name := c.GetString("T_name")
  3647. userList, _ := NatsServer.Read_User_List_All()
  3648. Account.Read_User_All_Map(userList)
  3649. Basic.Read_Depot_All_Map()
  3650. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  3651. R_List, R_cnt := StockOutDao.Read_StockOut_Audit_List(c.User.T_uuid, T_name, Stock.StockOutTypeWarehouse, T_state, T_depot_id, T_contract_number, T_start_date, T_end_date, page, page_z)
  3652. var r_jsons lib.R_JSONS
  3653. r_jsons.Num = R_cnt
  3654. r_jsons.Data = R_List
  3655. r_jsons.Page = page
  3656. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  3657. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  3658. c.ServeJSON()
  3659. return
  3660. }
  3661. // 财务/总经理审核
  3662. func (c *StockController) StockOut_Audit() {
  3663. T_number := c.GetString("T_number") // 出库单号
  3664. //T_depot_id, _ := c.GetInt("T_depot_id")
  3665. T_audit, _ := c.GetInt("T_audit") // 审核 2财务通过 3财务不通过 4总经理通过 5总经理不通过 6已出库
  3666. T_approval_opinion := c.GetString("T_approval_opinion") // 审批意见
  3667. T_approval_img := c.GetString("T_approval_img") // 审批意见
  3668. T_type := c.GetString("T_type") // 类型 Finance Manager
  3669. o := orm.NewOrm()
  3670. o.Begin()
  3671. StockOutDao := Stock.NewStockOut(o)
  3672. StockDao := Stock.NewStock(o)
  3673. StockOutProductDao := Stock.NewStockOutProduct(o)
  3674. StockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  3675. if err != nil {
  3676. o.Rollback()
  3677. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  3678. c.ServeJSON()
  3679. return
  3680. }
  3681. StockOut.T_state = T_audit
  3682. if T_type == Stock.StockOutTypeFinance {
  3683. StockOut.T_finance_approval_opinion = T_approval_opinion
  3684. StockOut.T_finance_approval_img = T_approval_img
  3685. } else {
  3686. StockOut.T_manager_approval_opinion = T_approval_opinion
  3687. StockOut.T_manager_approval_img = T_approval_img
  3688. }
  3689. if T_audit == Stock.StockOutAuditFinanceUnPass || T_audit == Stock.StockOutAuditManagerUnPass {
  3690. productList := StockOutProductDao.Read_StockOutProduct_List(StockOut.T_number)
  3691. for _, v := range productList {
  3692. product, _ := Basic.Read_Product_ById(v.T_product_id)
  3693. _, err = StockDao.AddOrUpdate_Occupy_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
  3694. if err != nil {
  3695. o.Rollback()
  3696. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  3697. c.ServeJSON()
  3698. return
  3699. }
  3700. }
  3701. }
  3702. err = StockOutDao.Update_StockOut(StockOut, "T_state", "T_finance_approval_opinion", "T_manager_approval_opinion", "T_finance_approval_img", "T_manager_approval_img")
  3703. if err != nil {
  3704. o.Rollback()
  3705. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库审批失败"}
  3706. c.ServeJSON()
  3707. return
  3708. }
  3709. o.Commit()
  3710. NatsServer.AddUserLogs(c.User.T_uuid, "出库申请", "审批", StockOut)
  3711. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  3712. c.ServeJSON()
  3713. return
  3714. }
  3715. // 仓管出库
  3716. func (c *StockController) StockOut_Warehouse() {
  3717. T_number := c.GetString("T_number") // 出库单号
  3718. T_date := c.GetString("T_date")
  3719. date, is := lib.DateStrToTime(T_date)
  3720. if !is {
  3721. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  3722. c.ServeJSON()
  3723. return
  3724. }
  3725. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "确认出库", T_number)
  3726. o := orm.NewOrm()
  3727. o.Begin()
  3728. StockOutDao := Stock.NewStockOut(o)
  3729. StockOutProductDao := Stock.NewStockOutProduct(o)
  3730. DeviceDao := Stock.NewDevice(o)
  3731. StockDao := Stock.NewStock(o)
  3732. // 查询入库信息
  3733. StockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  3734. if err != nil {
  3735. o.Rollback()
  3736. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  3737. c.ServeJSON()
  3738. return
  3739. }
  3740. T_product := c.GetString("T_product")
  3741. productList := lib.SplitString(T_product, "|")
  3742. allProductList := []int{}
  3743. for _, v := range productList {
  3744. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  3745. allProductList = append(allProductList, product_id)
  3746. product, _ := Basic.Read_Product_ById(product_id)
  3747. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  3748. if num == 0 {
  3749. o.Rollback()
  3750. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s出库数量不能为0!", product.T_name)}
  3751. c.ServeJSON()
  3752. return
  3753. }
  3754. T_relation_sn := strings.Split(v, "-")[2]
  3755. if T_relation_sn == "" && product.T_relation_sn == 1 {
  3756. o.Rollback()
  3757. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  3758. c.ServeJSON()
  3759. return
  3760. }
  3761. if len(T_relation_sn) > 0 {
  3762. snList := strings.Split(T_relation_sn, ",")
  3763. if product.T_relation_sn == 1 && num != len(snList) {
  3764. o.Rollback()
  3765. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s SN数量与出库数量不一致,请核查!", product.T_name)}
  3766. c.ServeJSON()
  3767. return
  3768. }
  3769. if len(snList) > 0 {
  3770. for _, sn := range snList {
  3771. mqtt := Stock.Read_MqttUser(sn)
  3772. // 根据IMEI查询上次入库的批次号
  3773. batchNumber, err := DeviceDao.Read_Device_BatchNumber_ByImei(mqtt.Imei)
  3774. if err != nil {
  3775. logs.Error(lib.FuncName(), err)
  3776. }
  3777. device := Stock.Device{
  3778. T_contract_number: StockOut.T_contract_number,
  3779. T_product_id: product_id,
  3780. T_out_number: T_number,
  3781. T_sn: sn,
  3782. T_iccid: mqtt.Iccid,
  3783. T_imei: mqtt.Imei,
  3784. T_State: 1,
  3785. T_project: StockOut.T_project,
  3786. T_batch_number: batchNumber,
  3787. CreateTime: date,
  3788. }
  3789. _, err = DeviceDao.AddOrUpdate_Device(device, 1)
  3790. if err != nil {
  3791. o.Rollback()
  3792. c.Data["json"] = lib.JSONS{Code: 202, Msg: "出库失败"}
  3793. c.ServeJSON()
  3794. return
  3795. }
  3796. }
  3797. }
  3798. }
  3799. // 3、添加出库产品清单
  3800. stockOutProduct := Stock.StockOutProduct{
  3801. T_number: T_number,
  3802. T_product_id: product_id,
  3803. T_depot_id: StockOut.T_depot_id,
  3804. T_num: num, // 出库数量
  3805. T_date: T_date, // 出库日期
  3806. T_relation_sn: T_relation_sn,
  3807. T_state: 1,
  3808. }
  3809. // 更新产品表
  3810. err = StockOutProductDao.Update_StockOutProduct(stockOutProduct)
  3811. if err != nil {
  3812. o.Rollback()
  3813. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改出库产品信息失败"}
  3814. c.ServeJSON()
  3815. return
  3816. }
  3817. // 5、更新产品库存列表、占用库存
  3818. _, err = StockDao.Stock_Warehouse(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num)
  3819. if err != nil {
  3820. o.Rollback()
  3821. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  3822. c.ServeJSON()
  3823. return
  3824. }
  3825. }
  3826. StockOut.T_warehouse = c.User.T_uuid
  3827. StockOut.T_state = Stock.StockOutAlreadyOut
  3828. StockOut.T_date = T_date
  3829. err = StockOutDao.Update_StockOut(StockOut, "T_state", "T_date", "T_warehouse")
  3830. if err != nil {
  3831. o.Rollback()
  3832. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库审批失败"}
  3833. c.ServeJSON()
  3834. return
  3835. }
  3836. o.Commit()
  3837. StockOut_Edit_StockMonth(StockOut.T_date, StockOut.T_depot_id, allProductList)
  3838. NatsServer.AddUserLogs(c.User.T_uuid, "出库申请", "出库", StockOut)
  3839. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  3840. c.ServeJSON()
  3841. return
  3842. }