Stock.go 119 KB

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