Stock.go 122 KB

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