Stock.go 124 KB

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