1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263 |
- package final
- import (
- "fmt"
- "os"
- "strings"
- "sync/atomic"
- "time"
- "zhiyuan/models/budget"
- "zhiyuan/models/final"
- "zhiyuan/pkg/app"
- "zhiyuan/pkg/config"
- "zhiyuan/pkg/db"
- "zhiyuan/pkg/logger"
- "zhiyuan/pkg/utils"
- "zhiyuan/services/admin"
- budgetService "zhiyuan/services/budget"
- "github.com/w3liu/go-common/constant/timeformat"
- "github.com/xuri/excelize/v2"
- "github.com/gin-gonic/gin"
- )
- type MaterialCartAddFrom struct {
- MatId int64 `form:"mat_id" json:"mat_id" label:"材料ID" binding:"required"`
- Num float64 `form:"num" json:"num" label:"数量"`
- ItemId int64 `form:"item_id" json:"item_id" label:"项目类型"`
- }
- func MaterialCartAdd(c *gin.Context) {
- var form MaterialCartAddFrom
- if app.Bind(c, &form) != nil {
- return
- }
- adminId := c.GetInt("adminID")
- if form.Num <= 0 {
- form.Num = 1
- }
- var mat final.FinalMat
- db.GetModel(map[string]interface{}{
- "id": form.MatId,
- "deleted_at": 0,
- }, &mat)
- if mat.ID == 0 {
- app.ErrorMsg(c, "材料错误", nil)
- return
- }
- if form.ItemId == 0 {
- items := make([]final.FinalMatItem, 0)
- s := db.ModelQuery(db.Type(items), map[string]interface{}{}, true)
- db.WhereParse(&s, map[string]interface{}{
- "deleted_at": 0,
- })
- var mattype final.FinalMatType
- db.GetModel(map[string]interface{}{
- "id": mat.TypeId,
- }, &mattype)
- where := "1=2"
- where += fmt.Sprintf(" OR find_in_set(%s, `%s`.`mattype_ids`)", s.Param(mattype.ID), final.FinalMatItem{}.TableName())
- for _, v := range strings.Split(mattype.Path, ",") {
- if v != "" {
- i, _ := db.ToInt64(v)
- where += fmt.Sprintf(" OR find_in_set(%s, `%s`.`mattype_ids`)", s.Param(i), final.FinalMatItem{}.TableName())
- }
- }
- s.Where = append(s.Where, where)
- query, params := s.Query()
- db.Query(query, params, &items, final.FinalMatItem{}.DB())
- if len(items) == 1 {
- form.ItemId = items[0].ID
- }
- }
- var model final.FinalMaterialCart
- db.GetModel(map[string]interface{}{
- "item_id": form.ItemId,
- "admin_id": adminId,
- "process_id": 0,
- "mat_id": form.MatId,
- "deleted_at": 0,
- }, &model)
- total := utils.FloatMul(mat.Price, form.Num, 2)
- if model.ID == 0 {
- _, err := db.InsertModel(db.Type(model), map[string]interface{}{
- "item_id": form.ItemId,
- "admin_id": adminId,
- "process_id": 0,
- "mat_id": form.MatId,
- "price": mat.Price,
- "num": form.Num,
- "total": total,
- })
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- } else {
- err := db.UpdateModel(db.Type(model), model.ID, map[string]interface{}{
- "num": utils.FloatAdd(model.Num, form.Num, 2),
- "total": total,
- })
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- }
- app.Success(c, nil)
- }
- var num int64
- func Generate(t time.Time) string {
- s := t.Format(timeformat.Continuity)
- m := t.UnixNano()/1e6 - t.UnixNano()/1e9*1e3
- ms := sup(m, 3)
- p := os.Getpid() % 1000
- ps := sup(int64(p), 3)
- i := atomic.AddInt64(&num, 1)
- r := i % 10000
- rs := sup(r, 4)
- n := fmt.Sprintf("%s%s%s%s", s, ms, ps, rs)
- return n
- }
- // 对长度不足n的数字前面补0
- func sup(i int64, n int) string {
- m := fmt.Sprintf("%d", i)
- for len(m) < n {
- m = fmt.Sprintf("0%s", m)
- }
- return m
- }
- type MaterialOrderCreateFrom struct {
- Carts []int `form:"carts" json:"carts" label:"购物车" binding:"required"`
- SiteId int `form:"site_id" json:"site_id" label:"工地" binding:"required"`
- Attachments map[int64]string `form:"attachments" json:"attachments" label:"附件"`
- Material int `form:"material" json:"material" label:"项目"`
- }
- func MaterialOrderCreate(c *gin.Context) {
- var form MaterialOrderCreateFrom
- if app.Bind(c, &form) != nil {
- return
- }
- adminId := c.GetInt("adminID")
- if len(form.Carts) == 0 {
- app.ErrorMsg(c, "购物车错误", nil)
- return
- }
- carts, err := db.GetModelMap(db.Type(final.FinalMaterialCartMobile{}), map[string]interface{}{
- "admin_id": adminId,
- "id in": form.Carts,
- "finalmat.state": 1,
- "deleted_at": 0,
- }, nil)
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- if len(carts) != len(form.Carts) {
- app.ErrorMsg(c, "购物车错误", nil)
- return
- }
- var site final.FinalSite
- db.GetModel(map[string]interface{}{
- "id": form.SiteId,
- "deleted_at": 0,
- }, &site)
- if site.ID == 0 {
- app.ErrorMsg(c, "工地不存在", nil)
- return
- }
- var settle final.FinalSettle
- db.GetModel(map[string]interface{}{
- "site_id": site.ID,
- "deleted_at": 0,
- }, &settle)
- if settle.ID != 0 {
- app.ErrorMsg(c, "已结算,无法下单", nil)
- return
- }
- address := site.Address
- if site.Village != " " {
- address += " " + site.Village
- }
- if site.RoomNo != " " {
- address += " " + site.RoomNo
- }
- type FinalMaterialMobile struct {
- ID int64 `json:"id" prop:"add:false"`
- Residue float64 `json:"residue" label:"金额" prop:"select:budget-ifnull(sum(order.total),0)"`
- final.FinalMaterialMobile
- }
- var material FinalMaterialMobile
- if form.Material != 0 {
- db.GetModel(map[string]interface{}{
- "id": form.Material,
- "deleted_at": 0,
- }, &material)
- if material.ID == 0 {
- app.ErrorMsg(c, "下单状态错误", nil)
- return
- }
- }
- cTotal := float64(0)
- ctyp := int64(0)
- itemMap := make(map[int64][]final.FinalMaterialOrderItem, 0)
- citemMap := make(map[int64][]final.FinalMaterialOrderItem, 0)
- supplierAdminMap := make(map[int64]int64)
- supplierTotalMap := make(map[int64]float64)
- supplierReduceMap := make(map[int64]int64)
- for i, v := range carts {
- supplier_id, _ := db.ToInt64(v["supplier_id"])
- supplier_admin_id, _ := db.ToInt64(v["supplier_admin_id"])
- supplierAdminMap[supplier_id] = supplier_admin_id
- supplier_reduce, _ := db.ToInt64(v["supplier_reduce"])
- supplierReduceMap[supplier_id] = supplier_reduce
- id, _ := db.ToInt64(v["id"])
- typ, _ := db.ToInt64(v["type"])
- mat_id, _ := db.ToInt64(v["mat_id"])
- num, _ := db.ToFloat64(v["num"])
- remark := db.ToString(v["remark"])
- price, _ := db.ToFloat64(v["mat_price"])
- total := utils.FloatMul(price, num, 2)
- //total, _ := db.ToFloat64(v["total"])
- item_id, _ := db.ToInt64(v["item_id"])
- is_custom, _ := db.ToInt64(v["is_custom"])
- if num <= 0 {
- continue
- }
- if is_custom == 0 {
- is := make([]final.FinalMaterialOrderItem, 0)
- if s, ok := itemMap[supplier_id]; ok {
- is = s
- }
- is = append(is, final.FinalMaterialOrderItem{
- ID: id,
- ItemId: item_id,
- MatId: mat_id,
- Price: price,
- Num: num,
- Remark: remark,
- Total: total,
- })
- itemMap[supplier_id] = is
- } else {
- cs := make([]final.FinalMaterialOrderItem, 0)
- if s, ok := citemMap[supplier_id]; ok {
- cs = s
- }
- cs = append(cs, final.FinalMaterialOrderItem{
- ID: id,
- ItemId: item_id,
- MatId: mat_id,
- Price: price,
- Num: num,
- Remark: remark,
- Total: total,
- })
- citemMap[supplier_id] = cs
- }
- if i == 0 {
- ctyp = typ
- } else {
- if ctyp != typ {
- app.ErrorMsg(c, "下单类型错误", nil)
- return
- }
- }
- if _, ok := supplierTotalMap[supplier_id]; !ok {
- supplierTotalMap[supplier_id] = 0
- }
- supplierTotalMap[supplier_id] = utils.FloatAdd(supplierTotalMap[supplier_id], total, 2)
- cTotal = utils.FloatAdd(cTotal, total, 2)
- }
- if material.ID != 0 && cTotal > material.Residue {
- app.ErrorMsg(c, "下单金额大于控量", nil)
- return
- }
- supplierids := make([]int64, 0)
- for id, _ := range supplierAdminMap {
- supplierids = append(supplierids, id)
- }
- suppliers, err := db.GetModelMap(db.Type(final.FinalSupplierPrepaymentStatist{}), map[string]interface{}{
- "id in": supplierids,
- "prepayment": 1,
- "deleted_at": 0,
- }, nil)
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- for _, v := range suppliers {
- id, _ := db.ToInt64(v["id"])
- name := db.ToString(v["name"])
- warning_amount, _ := db.ToFloat64(v["warning_amount"])
- notice_ids := db.ToString(v["notice_ids"])
- total, _ := db.ToFloat64(v["total"])
- noticeIds := make([]int64, 0)
- for _, i := range strings.Split(notice_ids, ",") {
- if i != "" {
- if n, ok := db.ToInt64(i); ok {
- noticeIds = append(noticeIds, n)
- }
- }
- }
- if supplierTotalMap[id] > total {
- for _, i := range noticeIds {
- err := final.SendMaterialMessage(int(i), "", 0, address, "材料商"+name+"预付款金额不足,下单失败")
- logger.Sugar.Infof("SendMaterialMessage, err: %v", err)
- }
- app.ErrorMsg(c, "材料商"+name+"预付款金额不足,下单失败", nil)
- return
- }
- if total-supplierTotalMap[id] <= warning_amount {
- for _, i := range noticeIds {
- err := final.SendMaterialMessage(int(i), "", 0, address, "材料商"+name+"预付款金额仅剩"+db.ToString(total-supplierTotalMap[id]))
- logger.Sugar.Infof("SendMaterialMessage, err: %v", err)
- }
- }
- }
- orderNo := Generate(time.Now())
- n := 0
- supplierOrderMap := make(map[int64]final.FinalMaterialOrder)
- customOrdersMap := make(map[int64]final.FinalMaterialOrder)
- for is_custom, itemmap := range []map[int64][]final.FinalMaterialOrderItem{itemMap, citemMap} {
- for supplier_id, items := range itemmap {
- n++
- order_no := orderNo
- if len(itemMap)+len(citemMap) > 1 {
- order_no = order_no + "-" + db.ToString(n)
- }
- total := float64(0)
- cartids := make([]int64, 0)
- for _, v := range items {
- cartids = append(cartids, v.ID)
- total = utils.FloatAdd(total, v.Total, 2)
- }
- xs := total - float64(int64(total))
- total = float64(int64(total))
- if xs >= 0.6 {
- total += 1
- }
- datas := map[string]interface{}{
- "order_no": order_no,
- "site_id": form.SiteId,
- "type": ctyp,
- "is_custom": is_custom,
- "type_id": material.ID,
- "supplier_id": supplier_id,
- "attachment": form.Attachments[supplier_id],
- "total": total,
- "state": 0,
- "take_time": time.Now().Unix(),
- "created_id": adminId,
- }
- if is_custom == 1 {
- datas["state"] = 8
- if site.PkgId == 7 {
- datas["state"] = 9
- }
- } else {
- if supplierReduceMap[supplier_id] == 1 {
- datas["state"] = 2
- }
- }
- orderid, err := db.InsertModel(db.Type(final.FinalMaterialOrder{}), datas)
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- if is_custom == 0 {
- supplierOrderMap[supplier_id] = final.FinalMaterialOrder{
- ID: orderid,
- OrderNo: order_no,
- }
- } else {
- customOrdersMap[supplier_id] = final.FinalMaterialOrder{
- ID: orderid,
- OrderNo: order_no,
- }
- }
- for _, v := range items {
- _, err := db.InsertModel(db.Type(final.FinalMaterialOrderItem{}), map[string]interface{}{
- "item_id": v.ItemId,
- "order_id": orderid,
- "mat_id": v.MatId,
- "price": v.Price,
- "num": v.Num,
- "total": v.Total,
- "remark": v.Remark,
- })
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- }
- process_id, err := db.InsertModel(db.Type(final.FinalMaterialOrderProcess{}), map[string]interface{}{
- "order_id": orderid,
- "type": 0,
- "created_id": adminId,
- })
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- if is_custom == 1 && site.PkgId == 7 {
- _, err := db.InsertModel(db.Type(final.FinalMaterialOrderProcess{}), map[string]interface{}{
- "order_id": orderid,
- "type": 11,
- "created_id": adminId,
- })
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- }
- err = db.UpdateModels(db.Type(final.FinalMaterialCart{}), map[string]interface{}{
- "admin_id": adminId,
- "id in": cartids,
- "deleted_at": 0,
- }, map[string]interface{}{
- "process_id": process_id,
- })
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- }
- }
- if material.ID != 0 {
- db.UpdateModel(db.Type(material), material.ID, map[string]interface{}{
- "state": 1,
- })
- }
- for s, o := range supplierOrderMap {
- if supplierReduceMap[s] == 1 {
- err := final.SendMaterialMessage(int(supplierAdminMap[s]), o.OrderNo, o.ID, address, "订单已下单,请安排配送")
- logger.Sugar.Infof("SendMaterialMessage, err: %v", err)
- } else {
- err := final.SendMaterialMessage(int(supplierAdminMap[s]), o.OrderNo, o.ID, address, "订单已下单,请安排备货")
- logger.Sugar.Infof("SendMaterialMessage, err: %v", err)
- }
- }
- for s, o := range customOrdersMap {
- if site.PkgId == 7 {
- err := final.SendMaterialMessage(int(supplierAdminMap[s]), o.OrderNo, o.ID, address, "已通知测量,请及时安排")
- logger.Sugar.Infof("SendMaterialMessage, err: %v", err)
- } else {
- err := final.SendMaterialMessage(int(site.ManagerId), o.OrderNo, o.ID, address, "定制品已下单,请通知测量")
- logger.Sugar.Infof("SendMaterialMessage, err: %v", err)
- }
- }
- app.Success(c, nil)
- }
- type MaterialOrderProcessCheckFrom struct {
- ProcessId int64 `form:"process_id" json:"process_id" label:"流程ID" binding:"required"`
- Check int `form:"check" json:"check" label:"状态"`
- CheckMark string `form:"checkmark" json:"checkmark" label:"留言"`
- }
- func MaterialOrderProcessCheck(c *gin.Context) {
- var form MaterialOrderProcessCheckFrom
- if app.Bind(c, &form) != nil {
- return
- }
- adminId := c.GetInt("adminID")
- var process final.FinalMaterialOrderProcess
- db.GetModel(map[string]interface{}{
- "id": form.ProcessId,
- "deleted_at": 0,
- }, &process)
- if process.ID == 0 {
- app.ErrorMsg(c, "流程错误", nil)
- return
- }
- var order final.FinalMaterialOrder
- db.GetModel(map[string]interface{}{
- "id": process.OrderId,
- "deleted_at": 0,
- }, &order)
- if order.ID == 0 || order.ProcessId != process.ID {
- app.ErrorMsg(c, "流程错误", nil)
- return
- }
- var site final.FinalSite
- db.GetModel(map[string]interface{}{
- "id": order.SiteId,
- "deleted_at": 0,
- }, &site)
- if site.ID == 0 {
- app.ErrorMsg(c, "工地不存在", nil)
- return
- }
- address := site.Address
- if site.Village != " " {
- address += " " + site.Village
- }
- if site.RoomNo != " " {
- address += " " + site.RoomNo
- }
- var supplier final.FinalSupplier
- db.GetModel(map[string]interface{}{
- "id": order.SupplierId,
- "deleted_at": 0,
- }, &supplier)
- data := map[string]interface{}{
- "check": form.Check,
- "checkmark": form.CheckMark,
- "check_id": adminId,
- "check_at": time.Now().Unix(),
- }
- dataOrder := map[string]interface{}{
- "process_id": 0,
- }
- name := ""
- adminInfo, _ := admin.GetInfoByID(int(process.CreatedId), nil, nil)
- if adminInfo != nil {
- name = adminInfo.Username
- }
- if process.Type == 6 {
- data = map[string]interface{}{}
- dataOrder = map[string]interface{}{}
- /*if admin.CheckAuth([]string{"final:verify:audit"}, adminId) && process.CheckAt == 0 {
- data["check"] = form.Check
- data["checkmark"] = form.CheckMark
- data["check_id"] = adminId
- data["check_at"] = time.Now().Unix()
- process.CheckAt = time.Now().Unix()
- }*/
- if adminId == int(site.ManagerId) && process.CheckAt2 == 0 {
- data["check2"] = form.Check
- data["checkmark2"] = form.CheckMark
- data["check_id2"] = adminId
- data["check_at2"] = time.Now().Unix()
- process.CheckAt2 = time.Now().Unix()
- }
- if adminId == int(supplier.AdminId) && process.CheckAt3 == 0 {
- data["check3"] = form.Check
- data["checkmark3"] = form.CheckMark
- data["check_id3"] = adminId
- data["check_at3"] = time.Now().Unix()
- process.CheckAt3 = time.Now().Unix()
- }
- if len(data) == 0 {
- app.ErrorMsg(c, "没有权限", nil)
- return
- }
- /*if process.CreatedId == supplier.AdminId {
- if adminId != int(order.CreatedId) {
- app.ErrorMsg(c, "没有权限", nil)
- return
- }
- } else {
- if adminId != int(supplier.AdminId) {
- app.ErrorMsg(c, "没有权限", nil)
- return
- }
- }*/
- if form.Check == 0 {
- //if process.CheckAt != 0 && process.CheckAt2 != 0 && process.CheckAt3 != 0 {
- if (process.CheckAt2 != 0 || site.ManagerId == process.CreatedId) && process.CheckAt3 != 0 {
- dataOrder["state"] = -1
- dataOrder["process_id"] = 0
- final.SendMaterialMessage(int(process.CreatedId), order.OrderNo, order.ID, address, "订单已取消")
- }
- } else {
- dataOrder["process_id"] = 0
- final.SendMaterialMessage(int(process.CreatedId), order.OrderNo, order.ID, address, name+"不同意取消订单")
- }
- } else if process.Type == 7 {
- data = map[string]interface{}{}
- dataOrder = map[string]interface{}{}
- if admin.CheckAuth([]string{"final:verify:audit"}, adminId) && process.CheckAt == 0 {
- data["check"] = form.Check
- data["checkmark"] = form.CheckMark
- data["check_id"] = adminId
- data["check_at"] = time.Now().Unix()
- process.CheckAt = time.Now().Unix()
- }
- if adminId == int(order.CreatedId) && process.CheckAt2 == 0 {
- data["check2"] = form.Check
- data["checkmark2"] = form.CheckMark
- data["check_id2"] = adminId
- data["check_at2"] = time.Now().Unix()
- process.CheckAt2 = time.Now().Unix()
- }
- if len(data) == 0 {
- app.ErrorMsg(c, "没有权限", nil)
- return
- }
- /*if !admin.CheckAuth([]string{"final:verify:audit"}, adminId) {
- app.ErrorMsg(c, "没有权限", nil)
- return
- }*/
- if form.Check == 0 {
- if process.CheckAt != 0 && process.CheckAt2 != 0 {
- carts := make([]final.FinalMaterialCart, 0)
- db.GetModel(map[string]interface{}{
- "process_id": process.ID,
- "deleted_at": 0,
- }, &carts)
- _, err := db.BuildUpdate(final.FinalMaterialOrderItem{}.TableName(), map[string]interface{}{
- "order_id": order.ID,
- "deleted_at": 0,
- }, map[string]interface{}{
- "deleted_at": time.Now().Unix(),
- }, final.FinalMaterialOrderItem{}.DB())
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- total := float64(0)
- for _, v := range carts {
- _, err := db.InsertModel(db.Type(final.FinalMaterialOrderItem{}), map[string]interface{}{
- "order_id": order.ID,
- "mat_id": v.MatId,
- "price": v.Price,
- "num": v.Num,
- "total": v.Total,
- "remark": v.Remark,
- })
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- total = utils.FloatAdd(total, v.Total, 2)
- }
- dataOrder["total"] = total
- dataOrder["process_id"] = 0
- final.SendMaterialMessage(int(process.CreatedId), order.OrderNo, order.ID, address, "订单已修改")
- }
- } else {
- dataOrder["process_id"] = 0
- final.SendMaterialMessage(int(process.CreatedId), order.OrderNo, order.ID, address, name+"不同意修改订单")
- }
- } else if process.Type == 12 {
- data = map[string]interface{}{}
- dataOrder = map[string]interface{}{}
- if admin.CheckAuth([]string{"final:verify:audit"}, adminId) && process.CheckAt == 0 {
- data["check"] = form.Check
- data["checkmark"] = form.CheckMark
- data["check_id"] = adminId
- data["check_at"] = time.Now().Unix()
- process.CheckAt = time.Now().Unix()
- }
- if adminId == int(order.CreatedId) && process.CheckAt2 == 0 {
- data["check2"] = form.Check
- data["checkmark2"] = form.CheckMark
- data["check_id2"] = adminId
- data["check_at2"] = time.Now().Unix()
- process.CheckAt2 = time.Now().Unix()
- }
- if len(data) == 0 {
- app.ErrorMsg(c, "没有权限", nil)
- return
- }
- /*if !admin.CheckAuth([]string{"final:verify:audit"}, adminId) {
- app.ErrorMsg(c, "没有权限", nil)
- return
- }*/
- if form.Check == 0 {
- if process.CheckAt != 0 && process.CheckAt2 != 0 {
- carts := make([]final.FinalMaterialCart, 0)
- db.GetModel(map[string]interface{}{
- "process_id": process.ID,
- "deleted_at": 0,
- }, &carts)
- _, err := db.BuildUpdate(final.FinalMaterialOrderItem{}.TableName(), map[string]interface{}{
- "order_id": order.ID,
- "deleted_at": 0,
- }, map[string]interface{}{
- "deleted_at": time.Now().Unix(),
- }, final.FinalMaterialOrderItem{}.DB())
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- total := float64(0)
- for _, v := range carts {
- _, err := db.InsertModel(db.Type(final.FinalMaterialOrderItem{}), map[string]interface{}{
- "order_id": order.ID,
- "mat_id": v.MatId,
- "price": v.Price,
- "num": v.Num,
- "total": v.Total,
- "remark": v.Remark,
- })
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- total = utils.FloatAdd(total, v.Total, 2)
- }
- dataOrder["total"] = total
- dataOrder["state"] = 0
- dataOrder["take_time"] = time.Now().Unix()
- dataOrder["process_id"] = 0
- final.SendMaterialMessage(int(process.CreatedId), order.OrderNo, order.ID, address, "测量订单修改已通过审核,请及时安排备货")
- }
- } else {
- dataOrder["state"] = 9
- dataOrder["process_id"] = 0
- final.SendMaterialMessage(int(process.CreatedId), order.OrderNo, order.ID, address, "测量订单修改未通过审核")
- }
- } else if process.Type == 14 {
- data = map[string]interface{}{}
- dataOrder = map[string]interface{}{}
- if adminId == int(site.ManagerId) && process.CheckAt == 0 {
- data["check"] = form.Check
- data["checkmark"] = form.CheckMark
- data["check_id"] = adminId
- data["check_at"] = time.Now().Unix()
- process.CheckAt = time.Now().Unix()
- }
- if form.Check == 0 {
- //if process.CheckAt != 0 && process.CheckAt2 != 0 && process.CheckAt3 != 0 {
- if process.CheckAt != 0 {
- if process.CheckAt-process.CreatedAt > 2*24*60*60 {
- dataOrder["report_timeout"] = order.ReportTimeout + 1
- }
- dataOrder["state"] = 8
- dataOrder["process_id"] = 0
- final.SendMaterialMessage(int(process.CreatedId), order.OrderNo, order.ID, address, "不具备测量条件已通过审核")
- }
- } else {
- dataOrder["process_id"] = 0
- final.SendMaterialMessage(int(process.CreatedId), order.OrderNo, order.ID, address, name+"不同意不具备测量条件")
- }
- } else {
- app.ErrorMsg(c, "流程错误", nil)
- return
- }
- if len(dataOrder) != 0 {
- err := db.UpdateModel(db.Type(final.FinalMaterialOrder{}), order.ID, dataOrder)
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- }
- err := db.UpdateModel(db.Type(final.FinalMaterialOrderProcess{}), process.ID, data)
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- app.Success(c, nil)
- }
- type MaterialOrderCartAddFrom struct {
- OrderId int64 `form:"order_id" json:"order_id" label:"订单ID" binding:"required"`
- }
- func MaterialOrderCartAdd(c *gin.Context) {
- var form MaterialOrderCartAddFrom
- if app.Bind(c, &form) != nil {
- return
- }
- adminId := c.GetInt("adminID")
- items := make([]final.FinalMaterialOrderItem, 0)
- db.GetModel(map[string]interface{}{
- "order_id": form.OrderId,
- "deleted_at": 0,
- }, &items)
- var model final.FinalMaterialCart
- _, err := db.BuildUpdate(model.TableName(), map[string]interface{}{
- "admin_id": adminId,
- "process_id": 0,
- "deleted_at": 0,
- }, map[string]interface{}{
- "deleted_at": time.Now().Unix(),
- }, model.DB())
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- for _, v := range items {
- _, err := db.InsertModel(db.Type(model), map[string]interface{}{
- "admin_id": adminId,
- "process_id": 0,
- "item_id": v.ItemId,
- "mat_id": v.MatId,
- "price": v.Price,
- "num": v.Num,
- "remark": v.Remark,
- "total": v.Total,
- })
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- }
- app.Success(c, nil)
- }
- type Order struct {
- ID int64 `json:"id" prop:"add:false"`
- SiteId int64 `json:"site_id" label:"工地" type:"int" prop:"add:false" search:"="`
- Total float64 `json:"total" label:"金额" type:"float" prop:"add:false" default:"0"`
- SupplierId int64 `json:"supplier_id" label:"材料商ID" type:"int" prop:"add:false" search:"="`
- ShopId int64 `json:"shop_id" label:"门店" type:"int" prop:"select:finalsite.shop_id" search:"="`
- OverTime int64 `json:"over_time" label:"完成时间" type:"int" prop:"add:false" default:"0" search:"="`
- final.FinalMaterialOrderMobile
- }
- type ContractDeduction struct {
- ContractId int64 `json:"contractId"`
- Name string `json:"name"`
- Amount float64 `json:"amount"`
- }
- type PunishDeduction struct {
- PunishId int64 `json:"punishId"`
- Explain string `json:"explain"`
- Amount float64 `json:"amount"`
- }
- type OrderPunishDeduction struct {
- PunishId int64 `json:"punishId"`
- OrderId int64 `json:"orderId"`
- OrderNo string `json:"order_no"`
- Explain string `json:"explain"`
- Amount float64 `json:"amount"`
- }
- func materialSettleDeduction(supplierId int64, shopId int64, orders []Order) (amount float64, contractDeductions []ContractDeduction, punishDeductions []PunishDeduction, orderPunishDeduction []OrderPunishDeduction) {
- orderAmount := float64(0)
- for _, order := range orders {
- orderAmount = utils.FloatAdd(orderAmount, order.Total, 2)
- }
- amount = 0
- type FinalSupplierContractStatist struct {
- ID int64 `json:"id" prop:"add:false"`
- Name string `json:"name" label:"名称" type:"string" prop:"add edit"`
- StartDate int64 `json:"startdate" label:"开始日期" type:"int" prop:"add edit"`
- EndDate int64 `json:"enddate" label:"截止日期" type:"int" prop:"add edit"`
- Info string `json:"info" label:"返点" type:"string" prop:"edit"`
- AmountTotal float64 `json:"amount_total" type:"float" prop:"select:ifnull(deduction.amount_total,0)"`
- DeductionTotal float64 `json:"deduction_total" type:"float" prop:"select:ifnull(deduction.deduction_total,0)"`
- final.FinalSupplierContractStatist
- }
- contracts := make([]FinalSupplierContractStatist, 0)
- db.GetModel(map[string]interface{}{
- "supplierId": supplierId,
- "state": 1,
- "deleted_at": 0,
- }, &contracts)
- contractDeductions = make([]ContractDeduction, 0)
- for _, contract := range contracts {
- if orderAmount <= 0 {
- break
- }
- //contractOrders := make([]Order, 0)
- contractOrderTotal := float64(0)
- for _, order := range orders {
- if order.OverTime >= contract.StartDate && order.OverTime < contract.EndDate+24*3600 {
- //contractOrders = append(contractOrders, order)
- contractOrderTotal = utils.FloatAdd(contractOrderTotal, order.Total, 2)
- }
- }
- //if len(contractOrders) == 0 {
- // continue
- //}
- amountTotal := utils.FloatAdd(contract.AmountTotal, contractOrderTotal, 2)
- point := float64(0)
- points := final.FinalSupplierContractPoints(contract.Info)
- for _, v := range points {
- if amountTotal >= v.Amount && v.Point > point {
- point = v.Point
- }
- }
- pointAmount := utils.FloatSub(utils.FloatDiv(utils.FloatMul(amountTotal, point, 2), 100, 2), contract.DeductionTotal, 2)
- if pointAmount <= 0 {
- continue
- }
- if pointAmount > orderAmount {
- pointAmount = orderAmount
- }
- orderAmount = utils.FloatSub(orderAmount, pointAmount, 2)
- contractDeductions = append(contractDeductions, ContractDeduction{
- ContractId: contract.ID,
- Name: contract.Name,
- Amount: pointAmount,
- })
- amount = utils.FloatAdd(amount, pointAmount, 2)
- }
- type FinalSupplierPunishStatist struct {
- ID int64 `json:"id" prop:"add:false"`
- Amount float64 `json:"amount" label:"金额" type:"float" prop:"add edit" default:"0" search:"="`
- Explain string `json:"explain" label:"说明" type:"string" prop:"edit" search:"like"`
- Punished float64 `json:"punished" label:"已罚金额" type:"float" prop:"select:ifnull(deduction.deduction_total,0)"`
- final.FinalSupplierPunishStatist
- }
- punishs := make([]FinalSupplierPunishStatist, 0)
- s := db.ModelQuery(db.Type(punishs), map[string]interface{}{}, true)
- db.WhereParse(&s, map[string]interface{}{
- "supplierId": supplierId,
- "deleted_at": 0,
- })
- s.Where = append(s.Where, "`zy_final_supplier_punish`.`amount` > ifnull(`deduction`.`deduction_total`,0)")
- query, params := s.Query()
- db.Query(query, params, &punishs, FinalSupplierPunishStatist{}.DB())
- punishDeductions = make([]PunishDeduction, 0)
- for _, punish := range punishs {
- if orderAmount <= 0 {
- break
- }
- punishAmount := utils.FloatSub(punish.Amount, punish.Punished, 2)
- if punishAmount <= 0 {
- continue
- }
- if punishAmount > orderAmount {
- punishAmount = orderAmount
- }
- orderAmount = utils.FloatSub(orderAmount, punishAmount, 2)
- punishDeductions = append(punishDeductions, PunishDeduction{
- PunishId: punish.ID,
- Explain: punish.Explain,
- Amount: punishAmount,
- })
- amount = utils.FloatAdd(amount, punishAmount, 2)
- }
- type FinalOrderPunishStatist struct {
- ID int64 `json:"id" prop:"add:false"`
- OrderNo string `json:"order_no" label:"订单号" type:"string" prop:"select:order.order_no"`
- OrderId int64 `json:"orderId" label:"订单ID" type:"int" prop:"add" search:"="`
- Amount float64 `json:"amount" label:"金额" type:"float" prop:"add edit" default:"0" search:"="`
- Explain string `json:"explain" label:"说明" type:"string" prop:"edit" search:"like"`
- Punished float64 `json:"punished" label:"已罚金额" type:"float" prop:"select:ifnull(deduction.deduction_total,0)"`
- final.FinalOrderPunishStatist
- }
- orderpunishs := make([]FinalOrderPunishStatist, 0)
- s = db.ModelQuery(db.Type(orderpunishs), map[string]interface{}{}, true)
- db.WhereParse(&s, map[string]interface{}{
- "order.supplier_id": supplierId,
- "finalsite.shop_id": shopId,
- "deleted_at": 0,
- })
- s.Where = append(s.Where, "`zy_final_order_punish`.`amount` > ifnull(`deduction`.`deduction_total`,0)")
- query, params = s.Query()
- db.Query(query, params, &orderpunishs, FinalSupplierPunishStatist{}.DB())
- orderPunishDeduction = make([]OrderPunishDeduction, 0)
- for _, punish := range orderpunishs {
- if orderAmount <= 0 {
- break
- }
- punishAmount := utils.FloatSub(punish.Amount, punish.Punished, 2)
- if punishAmount <= 0 {
- continue
- }
- if punishAmount > orderAmount {
- punishAmount = orderAmount
- }
- orderAmount = utils.FloatSub(orderAmount, punishAmount, 2)
- orderPunishDeduction = append(orderPunishDeduction, OrderPunishDeduction{
- PunishId: punish.ID,
- OrderId: punish.OrderId,
- OrderNo: punish.OrderNo,
- Explain: punish.Explain,
- Amount: punishAmount,
- })
- amount = utils.FloatAdd(amount, punishAmount, 2)
- }
- return
- }
- type MaterialSettleDeductionFrom struct {
- Orders []int `form:"orders" json:"orders" label:"订单" binding:"required"`
- }
- func MaterialSettleDeduction(c *gin.Context) {
- var form MaterialSettleDeductionFrom
- if app.Bind(c, &form) != nil {
- return
- }
- if len(form.Orders) == 0 {
- app.ErrorMsg(c, "没有权限", nil)
- return
- }
- adminid := c.GetInt("adminID")
- suppliers := make([]final.FinalSupplier, 0)
- db.GetModel(map[string]interface{}{
- "adminId": adminid,
- "deleted_at": 0,
- }, &suppliers)
- supplierids := make([]int64, 0)
- for _, v := range suppliers {
- supplierids = append(supplierids, v.ID)
- }
- orders := make([]Order, 0)
- db.GetModel(map[string]interface{}{
- "id in": form.Orders,
- "supplier_id in": supplierids,
- "state": 11,
- "process_id": 0,
- "settle_id": 0,
- "deleted_at": 0,
- }, &orders)
- if len(orders) != len(form.Orders) {
- app.ErrorMsg(c, "订单错误", nil)
- return
- }
- amount := float64(0)
- shop_id := orders[0].ShopId
- supplier_id := orders[0].SupplierId
- orderMap := make(map[int64][]Order)
- for _, order := range orders {
- //amount = utils.FloatAdd(amount, order.Total, 2)
- if order.ShopId != shop_id {
- app.ErrorMsg(c, "没有权限", nil)
- return
- }
- if order.SupplierId != supplier_id {
- app.ErrorMsg(c, "没有权限", nil)
- return
- }
- if _, ok := orderMap[order.SiteId]; !ok {
- orderMap[order.SiteId] = make([]Order, 0)
- }
- orderMap[order.SiteId] = append(orderMap[order.SiteId], order)
- }
- for _, os := range orderMap {
- total := float64(0)
- for _, order := range os {
- total = utils.FloatAdd(total, order.Total, 2)
- }
- xs := total - float64(int64(total))
- total = float64(int64(total))
- if xs >= 0.6 {
- total += 1
- }
- amount = utils.FloatAdd(amount, total, 2)
- }
- deduction, contracts, punishs, orderPunishs := materialSettleDeduction(supplier_id, shop_id, orders)
- total := utils.FloatSub(amount, deduction, 2)
- app.Success(c, map[string]interface{}{
- "deduction": deduction,
- "contracts": contracts,
- "amount": amount,
- "punishs": punishs,
- "orderPunishs": orderPunishs,
- "total": total,
- })
- }
- type MaterialSettleAddFrom struct {
- Orders []int `form:"orders" json:"orders" label:"订单" binding:"required"`
- AccountId int `form:"account_id" json:"account_id" label:"账户" binding:"required"`
- Explain string `form:"explain" json:"explain" label:"说明"`
- Pictures string `form:"pictures" json:"pictures" label:"图片"`
- }
- func MaterialSettleAdd(c *gin.Context) {
- var form MaterialSettleAddFrom
- if app.Bind(c, &form) != nil {
- return
- }
- if len(form.Orders) == 0 {
- app.ErrorMsg(c, "没有权限", nil)
- return
- }
- adminid := c.GetInt("adminID")
- suppliers := make([]final.FinalSupplier, 0)
- db.GetModel(map[string]interface{}{
- "adminId": adminid,
- "deleted_at": 0,
- }, &suppliers)
- supplierids := make([]int64, 0)
- for _, v := range suppliers {
- supplierids = append(supplierids, v.ID)
- }
- orders := make([]Order, 0)
- db.GetModel(map[string]interface{}{
- "id in": form.Orders,
- "supplier_id in": supplierids,
- "state": 11,
- "process_id": 0,
- "settle_id": 0,
- "deleted_at": 0,
- }, &orders)
- if len(orders) != len(form.Orders) {
- app.ErrorMsg(c, "订单错误", nil)
- return
- }
- amount := float64(0)
- ids := make([]string, 0)
- shop_id := orders[0].ShopId
- supplier_id := orders[0].SupplierId
- orderMap := make(map[int64][]Order)
- for _, order := range orders {
- //amount = utils.FloatAdd(amount, order.Total, 2)
- ids = append(ids, db.ToString(order.ID))
- if order.ShopId != shop_id {
- app.ErrorMsg(c, "没有权限", nil)
- return
- }
- if order.SupplierId != supplier_id {
- app.ErrorMsg(c, "没有权限", nil)
- return
- }
- if _, ok := orderMap[order.SiteId]; !ok {
- orderMap[order.SiteId] = make([]Order, 0)
- }
- orderMap[order.SiteId] = append(orderMap[order.SiteId], order)
- }
- for _, os := range orderMap {
- total := float64(0)
- for _, order := range os {
- total = utils.FloatAdd(total, order.Total, 2)
- }
- xs := total - float64(int64(total))
- total = float64(int64(total))
- if xs >= 0.6 {
- total += 1
- }
- amount = utils.FloatAdd(amount, total, 2)
- }
- deduction, contractDeductions, punishDeductions, orderPunishDeductions := materialSettleDeduction(supplier_id, shop_id, orders)
- total := utils.FloatSub(amount, deduction, 2)
- id, err := db.InsertModel(db.Type(final.FinalMaterialSettle{}), map[string]interface{}{
- "supplier_id": supplier_id,
- "account_id": form.AccountId,
- "amount": amount,
- "deduction": deduction,
- "total": total,
- "shop_id": shop_id,
- "orders": strings.Join(ids, ","),
- "state": 2,
- "created_id": c.GetInt("adminID"),
- })
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- err = db.UpdateModels(db.Type(final.FinalMaterialOrder{}), map[string]interface{}{
- "id in": form.Orders,
- "supplier_id": supplier_id,
- "state": 11,
- "process_id": 0,
- "settle_id": 0,
- "deleted_at": 0,
- }, map[string]interface{}{
- "state": 12,
- "settle_id": id,
- })
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- _, err = db.InsertModel(db.Type(final.FinalMaterialSettleProcess{}), map[string]interface{}{
- "settle_id": id,
- "explain": form.Explain,
- "pictures": form.Pictures,
- "type": 0,
- "state": 0,
- "created_id": c.GetInt("adminID"),
- })
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- for _, deduction := range contractDeductions {
- _, err = db.InsertModel(db.Type(final.FinalSupplierContractDeduction{}), map[string]interface{}{
- "supplierId": supplier_id,
- "contractId": deduction.ContractId,
- "settleId": id,
- "amount": deduction.Amount,
- })
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- }
- for _, deduction := range punishDeductions {
- _, err = db.InsertModel(db.Type(final.FinalSupplierPunishDeduction{}), map[string]interface{}{
- "supplierId": supplier_id,
- "punishId": deduction.PunishId,
- "settleId": id,
- "amount": deduction.Amount,
- })
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- }
- for _, deduction := range orderPunishDeductions {
- _, err = db.InsertModel(db.Type(final.FinalOrderPunishDeduction{}), map[string]interface{}{
- "supplierId": supplier_id,
- "orderId": deduction.OrderId,
- "punishId": deduction.PunishId,
- "settleId": id,
- "amount": deduction.Amount,
- })
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- }
- app.Success(c, nil)
- }
- type MaterialCartSelectItemFrom struct {
- Id int64 `form:"id" json:"id" label:"ID" binding:"required"`
- ItemId int64 `form:"item_id" json:"item_id" label:"项目类型"`
- }
- func MaterialCartSelectItem(c *gin.Context) {
- var form MaterialCartSelectItemFrom
- if app.Bind(c, &form) != nil {
- return
- }
- adminId := c.GetInt("adminID")
- var model final.FinalMaterialCart
- db.GetModel(map[string]interface{}{
- "id": form.Id,
- "admin_id": adminId,
- "process_id": 0,
- "deleted_at": 0,
- }, &model)
- if model.ID == 0 {
- app.ErrorMsg(c, "没有权限", nil)
- return
- }
- var models final.FinalMaterialCart
- db.GetModel(map[string]interface{}{
- "id !=": model.ID,
- "item_id": form.ItemId,
- "admin_id": adminId,
- "process_id": 0,
- "mat_id": model.MatId,
- "deleted_at": 0,
- }, &models)
- if models.ID == 0 {
- err := db.UpdateModel(db.Type(model), model.ID, map[string]interface{}{
- "item_id": form.ItemId,
- })
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- } else {
- err := db.UpdateModel(db.Type(model), models.ID, map[string]interface{}{
- "num": utils.FloatAdd(model.Num, models.Num, 2),
- "total": utils.FloatAdd(model.Total, models.Total, 2),
- })
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- err = db.UpdateModel(db.Type(model), model.ID, map[string]interface{}{
- "deleted_at": time.Now().Unix(),
- })
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- }
- app.Success(c, nil)
- }
- type MaterialBrandCartAddFrom struct {
- BrandId int64 `form:"brandId" json:"brandId" label:"品牌" binding:"required"`
- PresetsId int64 `form:"presetsId" json:"presetsId" label:"预设"`
- }
- func MaterialBrandCartAdd(c *gin.Context) {
- var form MaterialBrandCartAddFrom
- if app.Bind(c, &form) != nil {
- return
- }
- adminId := c.GetInt("adminID")
- items := make([]final.FinalMatAuxiliaryItem, 0)
- db.GetModel(map[string]interface{}{
- "brandId": form.BrandId,
- "deleted_at": 0,
- }, &items)
- pitems := make([]final.FinalMatAuxiliaryPresetsItem, 0)
- db.GetModel(map[string]interface{}{
- "presetsId": form.PresetsId,
- "deleted_at": 0,
- }, &pitems)
- pitemMap := make(map[int64]final.FinalMatAuxiliaryPresetsItem)
- for _, v := range pitems {
- pitemMap[v.ItemId] = v
- }
- matitemids := make([]int64, 0)
- matitemMap := make(map[int64]final.FinalMatAuxiliaryItem)
- for _, v := range items {
- matitemids = append(matitemids, v.ItemId)
- matitemMap[v.ItemId] = v
- }
- matitems := make([]final.FinalMatItem, 0)
- db.GetModel(map[string]interface{}{
- "id in": matitemids,
- "deleted_at": 0,
- }, &matitems)
- itemss := make([]final.FinalMatAuxiliaryItem, 0)
- for _, v := range matitems {
- if item, ok := matitemMap[v.ID]; ok {
- itemss = append([]final.FinalMatAuxiliaryItem{item}, itemss...)
- }
- }
- var model final.FinalMaterialCart
- _, err := db.BuildUpdate(model.TableName(), map[string]interface{}{
- "admin_id": adminId,
- "process_id": 0,
- "deleted_at": 0,
- }, map[string]interface{}{
- "deleted_at": time.Now().Unix(),
- }, model.DB())
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- matids := make([]int64, 0)
- for _, v := range itemss {
- matids = append(matids, v.MatId)
- }
- mats := make([]final.FinalMat, 0)
- matMap := make(map[int64]final.FinalMat)
- db.GetModel(map[string]interface{}{
- "id in": matids,
- "deleted_at": 0,
- }, &mats)
- for _, v := range mats {
- matMap[v.ID] = v
- }
- for _, v := range itemss {
- num := int64(0)
- if i, ok := pitemMap[v.ItemId]; ok && i.Num > 0 {
- num = i.Num
- }
- total := utils.FloatMul(float64(num), matMap[v.MatId].Price, 2)
- _, err := db.InsertModel(db.Type(model), map[string]interface{}{
- "admin_id": adminId,
- "process_id": 0,
- "item_id": v.ItemId,
- "mat_id": v.MatId,
- "num": num,
- "price": matMap[v.MatId].Price,
- "total": total,
- })
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- }
- app.Success(c, nil)
- }
- type MaterialCartBatchAddFrom struct {
- Carts []MaterialCartAddFrom `form:"carts" json:"carts" binding:"required"`
- }
- func MaterialCartBatchAdd(c *gin.Context) {
- var form MaterialCartBatchAddFrom
- if app.Bind(c, &form) != nil {
- return
- }
- adminId := c.GetInt("adminID")
- var model final.FinalMaterialCart
- _, err := db.BuildUpdate(model.TableName(), map[string]interface{}{
- "admin_id": adminId,
- "process_id": 0,
- "deleted_at": 0,
- }, map[string]interface{}{
- "deleted_at": time.Now().Unix(),
- }, model.DB())
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- matids := make([]int64, 0)
- for _, v := range form.Carts {
- matids = append(matids, v.MatId)
- }
- mats := make([]final.FinalMat, 0)
- matMap := make(map[int64]final.FinalMat)
- db.GetModel(map[string]interface{}{
- "id in": matids,
- "deleted_at": 0,
- }, &mats)
- for _, v := range mats {
- matMap[v.ID] = v
- }
- for _, v := range form.Carts {
- if v.Num <= 0 {
- v.Num = 1
- }
- _, err := db.InsertModel(db.Type(model), map[string]interface{}{
- "admin_id": adminId,
- "process_id": 0,
- "item_id": v.ItemId,
- "mat_id": v.MatId,
- "num": v.Num,
- "total": utils.FloatMul(v.Num, matMap[v.MatId].Price, 2),
- })
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- }
- app.Success(c, nil)
- }
- type SelectMaterialAddFrom struct {
- Carts []MaterialCartAddFrom `form:"carts" json:"carts" binding:"required"`
- SiteId int64 `form:"site_id" json:"site_id" label:"ID" binding:"required"`
- }
- func SelectMaterialAdd(c *gin.Context) {
- var form SelectMaterialAddFrom
- if app.Bind(c, &form) != nil {
- return
- }
- adminId := c.GetInt("adminID")
- matids := make([]int64, 0)
- for _, v := range form.Carts {
- matids = append(matids, v.MatId)
- }
- mats := make([]final.FinalMat, 0)
- matMap := make(map[int64]final.FinalMat)
- db.GetModel(map[string]interface{}{
- "id in": matids,
- "deleted_at": 0,
- }, &mats)
- for _, v := range mats {
- matMap[v.ID] = v
- }
- id, err := db.InsertModel(db.Type(final.FinalSelectMaterial{}), map[string]interface{}{
- "site_id": form.SiteId,
- "state": 0,
- "created_id": adminId,
- })
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- for _, v := range form.Carts {
- if v.Num <= 0 {
- v.Num = 1
- }
- _, err := db.InsertModel(db.Type(final.FinalSelectMaterialItem{}), map[string]interface{}{
- "select_id": id,
- "item_id": v.ItemId,
- "mat_id": v.MatId,
- })
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- }
- app.Success(c, nil)
- }
- type SelectMaterialOrderCreateFrom struct {
- SelectId int `form:"select_id" json:"select_id" binding:"required"`
- }
- func SelectMaterialOrderCreate(c *gin.Context) {
- var form SelectMaterialOrderCreateFrom
- if app.Bind(c, &form) != nil {
- return
- }
- var selectMaterial final.FinalSelectMaterial
- db.GetModel(map[string]interface{}{
- "id": form.SelectId,
- "deleted_at": 0,
- }, &selectMaterial)
- if selectMaterial.ID == 0 {
- app.ErrorMsg(c, "选材单不存在", nil)
- return
- }
- if selectMaterial.State != 0 {
- app.ErrorMsg(c, "选材单状态错误", nil)
- return
- }
- adminId := c.GetInt("adminID")
- items, err := db.GetModelMap(db.Type(final.FinalSelectMaterialItemMobile{}), map[string]interface{}{
- "select_id": selectMaterial.ID,
- "deleted_at": 0,
- }, nil)
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- var site final.FinalSite
- db.GetModel(map[string]interface{}{
- "id": selectMaterial.SiteId,
- "deleted_at": 0,
- }, &site)
- if site.ID == 0 {
- app.ErrorMsg(c, "工地不存在", nil)
- return
- }
- var settle final.FinalSettle
- db.GetModel(map[string]interface{}{
- "site_id": site.ID,
- "deleted_at": 0,
- }, &settle)
- if settle.ID != 0 {
- app.ErrorMsg(c, "已结算,无法下单", nil)
- return
- }
- address := site.Address
- if site.Village != " " {
- address += " " + site.Village
- }
- if site.RoomNo != " " {
- address += " " + site.RoomNo
- }
- cTotal := float64(0)
- ctyp := int64(0)
- itemMap := make(map[int64][]final.FinalMaterialOrderItem, 0)
- citemMap := make(map[int64][]final.FinalMaterialOrderItem, 0)
- supplierAdminMap := make(map[int64]int64)
- supplierTotalMap := make(map[int64]float64)
- for i, v := range items {
- supplier_id, _ := db.ToInt64(v["supplier_id"])
- supplier_admin_id, _ := db.ToInt64(v["supplier_admin_id"])
- supplierAdminMap[supplier_id] = supplier_admin_id
- id, _ := db.ToInt64(v["id"])
- typ, _ := db.ToInt64(v["type"])
- mat_id, _ := db.ToInt64(v["mat_id"])
- num, _ := db.ToFloat64(v["num"])
- remark := db.ToString(v["remark"])
- price, _ := db.ToFloat64(v["mat_price"])
- total := utils.FloatMul(price, num, 2)
- item_id, _ := db.ToInt64(v["item_id"])
- is_custom, _ := db.ToInt64(v["is_custom"])
- if num <= 0 {
- continue
- }
- if is_custom == 0 {
- is := make([]final.FinalMaterialOrderItem, 0)
- if s, ok := itemMap[supplier_id]; ok {
- is = s
- }
- is = append(is, final.FinalMaterialOrderItem{
- ID: id,
- ItemId: item_id,
- MatId: mat_id,
- Price: price,
- Num: num,
- Remark: remark,
- Total: total,
- })
- itemMap[supplier_id] = is
- } else {
- cs := make([]final.FinalMaterialOrderItem, 0)
- if s, ok := citemMap[supplier_id]; ok {
- cs = s
- }
- cs = append(cs, final.FinalMaterialOrderItem{
- ID: id,
- ItemId: item_id,
- MatId: mat_id,
- Price: price,
- Num: num,
- Remark: remark,
- Total: total,
- })
- citemMap[supplier_id] = cs
- }
- if i == 0 {
- ctyp = typ
- } else {
- if ctyp != typ {
- app.ErrorMsg(c, "下单类型错误", nil)
- return
- }
- }
- if _, ok := supplierTotalMap[supplier_id]; !ok {
- supplierTotalMap[supplier_id] = 0
- }
- supplierTotalMap[supplier_id] = utils.FloatAdd(supplierTotalMap[supplier_id], total, 2)
- cTotal = utils.FloatAdd(cTotal, total, 2)
- }
- supplierids := make([]int64, 0)
- for id, _ := range supplierAdminMap {
- supplierids = append(supplierids, id)
- }
- suppliers, err := db.GetModelMap(db.Type(final.FinalSupplierPrepaymentStatist{}), map[string]interface{}{
- "id in": supplierids,
- "prepayment": 1,
- "deleted_at": 0,
- }, nil)
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- for _, v := range suppliers {
- id, _ := db.ToInt64(v["id"])
- name := db.ToString(v["name"])
- warning_amount, _ := db.ToFloat64(v["warning_amount"])
- notice_ids := db.ToString(v["notice_ids"])
- total, _ := db.ToFloat64(v["total"])
- noticeIds := make([]int64, 0)
- for _, i := range strings.Split(notice_ids, ",") {
- if i != "" {
- if n, ok := db.ToInt64(i); ok {
- noticeIds = append(noticeIds, n)
- }
- }
- }
- if supplierTotalMap[id] > total {
- for _, i := range noticeIds {
- err := final.SendMaterialMessage(int(i), "", 0, address, "材料商"+name+"预付款金额不足,下单失败")
- logger.Sugar.Infof("SendMaterialMessage, err: %v", err)
- }
- app.ErrorMsg(c, "材料商"+name+"预付款金额不足,下单失败", nil)
- return
- }
- if total-supplierTotalMap[id] <= warning_amount {
- for _, i := range noticeIds {
- err := final.SendMaterialMessage(int(i), "", 0, address, "材料商"+name+"预付款金额仅剩"+db.ToString(total-supplierTotalMap[id]))
- logger.Sugar.Infof("SendMaterialMessage, err: %v", err)
- }
- }
- }
- orderNo := Generate(time.Now())
- n := 0
- supplierOrderMap := make(map[int64]final.FinalMaterialOrder)
- customOrders := make([]final.FinalMaterialOrder, 0)
- for is_custom, itemmap := range []map[int64][]final.FinalMaterialOrderItem{itemMap, citemMap} {
- for supplier_id, items := range itemmap {
- n++
- order_no := orderNo
- if len(itemMap)+len(citemMap) > 1 {
- order_no = order_no + "-" + db.ToString(n)
- }
- total := float64(0)
- for _, v := range items {
- total = utils.FloatAdd(total, v.Total, 2)
- }
- xs := total - float64(int64(total))
- total = float64(int64(total))
- if xs >= 0.6 {
- total += 1
- }
- datas := map[string]interface{}{
- "order_no": order_no,
- "site_id": selectMaterial.SiteId,
- "type": ctyp,
- "is_custom": is_custom,
- "type_id": 0,
- "supplier_id": supplier_id,
- "total": total,
- "state": 0,
- "take_time": time.Now().Unix(),
- "created_id": selectMaterial.CreatedId,
- }
- if is_custom == 1 {
- datas["state"] = 8
- }
- orderid, err := db.InsertModel(db.Type(final.FinalMaterialOrder{}), datas)
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- if is_custom == 0 {
- supplierOrderMap[supplier_id] = final.FinalMaterialOrder{
- ID: orderid,
- OrderNo: order_no,
- }
- } else {
- customOrders = append(customOrders, final.FinalMaterialOrder{
- ID: orderid,
- OrderNo: order_no,
- })
- }
- for _, v := range items {
- _, err := db.InsertModel(db.Type(final.FinalMaterialOrderItem{}), map[string]interface{}{
- "item_id": v.ItemId,
- "order_id": orderid,
- "mat_id": v.MatId,
- "price": v.Price,
- "num": v.Num,
- "total": v.Total,
- "remark": v.Remark,
- })
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- }
- _, err = db.InsertModel(db.Type(final.FinalMaterialOrderProcess{}), map[string]interface{}{
- "order_id": orderid,
- "type": 0,
- "created_id": adminId,
- })
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- }
- }
- err = db.UpdateModel(db.Type(final.FinalSelectMaterial{}), selectMaterial.ID, map[string]interface{}{
- "check_id": adminId,
- "state": 1,
- })
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- for s, o := range supplierOrderMap {
- err := final.SendMaterialMessage(int(supplierAdminMap[s]), o.OrderNo, o.ID, address, "订单已下单,请安排备货")
- logger.Sugar.Infof("SendMaterialMessage, err: %v", err)
- }
- for _, o := range customOrders {
- err := final.SendMaterialMessage(int(site.ManagerId), o.OrderNo, o.ID, address, "定制品已下单,请通知测量")
- logger.Sugar.Infof("SendMaterialMessage, err: %v", err)
- }
- app.Success(c, nil)
- }
- type MaterialOrderCartBatchAddFrom struct {
- OrderId int64 `form:"orderId" json:"orderId" binding:"required"`
- }
- func MaterialOrderCartBatchAdd(c *gin.Context) {
- var form MaterialOrderCartBatchAddFrom
- if app.Bind(c, &form) != nil {
- return
- }
- adminId := c.GetInt("adminID")
- var model final.FinalMaterialCart
- _, err := db.BuildUpdate(model.TableName(), map[string]interface{}{
- "admin_id": adminId,
- "process_id": 0,
- "deleted_at": 0,
- }, map[string]interface{}{
- "deleted_at": time.Now().Unix(),
- }, model.DB())
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- items := make([]final.FinalMaterialOrderItem, 0)
- db.GetModel(map[string]interface{}{
- "order_id": form.OrderId,
- "deleted_at": 0,
- }, &items)
- matids := make([]int64, 0)
- for _, v := range items {
- matids = append(matids, v.MatId)
- }
- mats := make([]final.FinalMat, 0)
- matMap := make(map[int64]final.FinalMat)
- db.GetModel(map[string]interface{}{
- "id in": matids,
- "deleted_at": 0,
- }, &mats)
- for _, v := range mats {
- matMap[v.ID] = v
- }
- for _, v := range items {
- if v.Num <= 0 {
- v.Num = 1
- }
- _, err := db.InsertModel(db.Type(model), map[string]interface{}{
- "admin_id": adminId,
- "process_id": 0,
- "item_id": v.ItemId,
- "mat_id": v.MatId,
- "price": matMap[v.MatId].Price,
- "num": v.Num,
- "total": utils.FloatMul(v.Num, matMap[v.MatId].Price, 2),
- })
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- }
- app.Success(c, nil)
- }
- func QuoteOrderInfo(c *gin.Context) {
- id := utils.ToInt(c.Param("id"))
- if id <= 0 {
- app.ErrorMsg(c, "id must be a number", nil)
- return
- }
- var model budget.Order
- db.GetModel(map[string]interface{}{"id": id}, &model)
- if model.ID == 0 {
- app.ErrorMsg(c, "订单不存在", nil)
- return
- }
- order, err := budgetService.LoadOrder(model)
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- context := budgetService.NewContext(order, c)
- err = context.Eval()
- logger.Sugar.Infof("err: %v", err)
- if model.State == 0 {
- err = order.Save(context)
- logger.Sugar.Infof("err: %v", err)
- }
- model.Content = make([]byte, 0)
- app.Success(c, gin.H{
- "name": order.Models[budgetService.Quote][order.Form.ID].GetName(),
- "order": model,
- "result": order.ToMap(false),
- })
- }
- type MaterialLeaderCheckFrom struct {
- Orders []int `form:"orders" json:"orders" label:"订单" binding:"required"`
- Signature string `form:"signature" json:"signature" label:"签名" binding:"required"`
- Explain string `form:"explain" json:"explain" label:"说明"`
- Pictures string `form:"pictures" json:"pictures" label:"图片"`
- }
- func MaterialLeaderCheck(c *gin.Context) {
- var form MaterialLeaderCheckFrom
- if app.Bind(c, &form) != nil {
- return
- }
- if len(form.Orders) == 0 {
- app.ErrorMsg(c, "没有权限", nil)
- return
- }
- adminid := c.GetInt("adminID")
- orders := make([]final.FinalMaterialOrder, 0)
- db.GetModel(map[string]interface{}{
- "id in": form.Orders,
- "state": 7,
- "process_id": 0,
- "deleted_at": 0,
- }, &orders)
- if len(orders) != len(form.Orders) {
- app.ErrorMsg(c, "订单错误", nil)
- return
- }
- err := db.UpdateModels(db.Type(final.FinalMaterialOrder{}), map[string]interface{}{
- "id in": form.Orders,
- "state": 7,
- "process_id": 0,
- "deleted_at": 0,
- }, map[string]interface{}{
- "state": 11,
- })
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- for _, order := range orders {
- _, err = db.InsertModel(db.Type(final.FinalMaterialOrderProcess{}), map[string]interface{}{
- "order_id": order.ID,
- "type": 13,
- "explain": form.Explain,
- "pictures": form.Pictures,
- "signature": form.Signature,
- "created_id": adminid,
- })
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- }
- app.Success(c, nil)
- }
- func MaterialOrderSatic(c *gin.Context) {
- adminID := c.GetInt("adminID")
- orderModel := final.FinalMaterialOrderSupplier{}
- orderType := db.Type(orderModel)
- deleted := orderModel.DeletedField()
- s := db.ModelQuery(orderType, map[string]interface{}{}, false)
- if deleted != "" {
- s.Where = append(s.Where, fmt.Sprintf("`%s`.`%s` = 0", orderModel.TableName(), deleted))
- }
- if !orderModel.ListPrivilege(c, map[string]interface{}{}, &s) {
- app.ErrorMsg(c, "没有权限", nil)
- return
- }
- orderCount, err := db.GetCount(s, orderModel.DB())
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- where := s.Where
- adminIDParam := s.Param(adminID)
- s.Where = where
- dclWhere := fmt.Sprintf("(`finalsite`.`manager_id` = %s AND ((`%s`.`state` in (8,1,3,4,6) AND `%s`.`process_id` = 0) OR (`process`.`type` = 6 AND `process`.`check_at2` = 0) OR (`process`.`type` = 14 AND `process`.`check_at` = 0)))", adminIDParam, orderModel.TableName(), orderModel.TableName())
- dclWhere += fmt.Sprintf(" OR (`supplier`.`adminId` = %s AND ((`%s`.`state` in (9,0,2,5,11) AND `%s`.`process_id` = 0) OR (`process`.`type` = 6 AND `process`.`check_at3` = 0)))", adminIDParam, orderModel.TableName(), orderModel.TableName())
- dclWhere += fmt.Sprintf(" OR (`finalsite`.`project_leader_id` = %s AND `%s`.`state` = 7 AND `%s`.`process_id` = 0)", adminIDParam, orderModel.TableName(), orderModel.TableName())
- dclWhere += fmt.Sprintf(" OR (`%s`.`created_id` = %s AND `process`.`type` = 7 AND `process`.`check_at2` = 0)", orderModel.TableName(), adminIDParam)
- if admin.CheckAuth([]string{"final:verify:audit"}, adminID) {
- dclWhere += " OR (`process`.`type` = 7 AND `process`.`check_at` = 0) OR (`process`.`type` = 12 AND `process`.`check_at` = 0)"
- }
- s.Where = append(where, fmt.Sprintf("(%s)", dclWhere))
- dclCount, err := db.GetCount(s, orderModel.DB())
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- s.Where = append(where, fmt.Sprintf("`%s`.`process_id` != %s", orderModel.TableName(), s.Param(0)))
- wheres := fmt.Sprintf("(`finalsite`.`manager_id` = %s AND `process`.`type` = 6 AND `process`.`check_at2` = 0) OR (`supplier`.`adminId` = %s AND `process`.`type` = 6 AND `process`.`check_at3` = 0) OR (`%s`.`created_id` = %s AND `process`.`type` = 7 AND `process`.`check_at2` = 0)", adminIDParam, adminIDParam, orderModel.TableName(), adminIDParam)
- if admin.CheckAuth([]string{"final:verify:audit"}, adminID) {
- wheres += " OR (`process`.`type` = 7 AND `process`.`check_at` = 0) OR (`process`.`type` = 12 AND `process`.`check_at` = 0)"
- }
- s.Where = append(s.Where, "("+wheres+")")
- dshCount, err := db.GetCount(s, orderModel.DB())
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- stateConsts := []int64{}
- for i := 0; i <= 12; i++ {
- s.Where = append(where, fmt.Sprintf("`%s`.`state` = %s", orderModel.TableName(), s.Param(i)))
- count, err := db.GetCount(s, orderModel.DB())
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- stateConsts = append(stateConsts, count)
- }
- app.Success(c, gin.H{
- "orderCount": orderCount,
- "dclCount": dclCount,
- "dshCount": dshCount,
- "stateConsts": stateConsts,
- })
- }
- func FinalMaterialSettlePaymentExport(c *gin.Context) {
- id := utils.ToInt(c.Param("id"))
- if id <= 0 {
- app.ErrorMsg(c, "id must be a number", nil)
- return
- }
- type FinalMaterialSettle struct {
- ID int64 `json:"id" prop:"add:false"`
- ShopName string `json:"shop_name" prop:"select:shop.shop_name"`
- CreatedAt int64 `json:"created_at" prop:"add:false"`
- AccountName string `json:"account_name" prop:"select:account.account_name"`
- AccountBank string `json:"account_bank" prop:"select:account.account_bank"`
- AccountNo string `json:"account_no" prop:"select:account.account_no"`
- SupplierName string `json:"supplier_name" type:"string" prop:"select:supplier.name" search:"like"`
- Total float64 `json:"total" label:"合计金额" type:"float" prop:"add:false" search:"="`
- final.FinalMaterialSettleMobile
- }
- var model FinalMaterialSettle
- db.GetModel(map[string]interface{}{"id": id}, &model)
- if model.ID == 0 {
- app.ErrorMsg(c, "结算不存在", nil)
- return
- }
- template, err := excelize.OpenFile("fkspdTemplate.xlsx")
- //template, err := xlsx.OpenFile("fkspdTemplate.xlsx")
- if err != nil {
- app.ErrorMsg(c, "系统错误", nil)
- return
- }
- for _, sheet := range template.GetSheetList() {
- template.SetCellValue(sheet, "B3", model.ShopName)
- template.SetCellValue(sheet, "F3", time.Unix(model.CreatedAt, 0).Format("2006.01.02"))
- template.SetCellValue(sheet, "B4", model.AccountName)
- template.SetCellValue(sheet, "B5", model.AccountBank)
- template.SetCellValue(sheet, "E5", model.AccountNo)
- template.SetCellValue(sheet, "B6", model.SupplierName+"(材料款)")
- template.SetCellValue(sheet, "B8", model.Total)
- /*cell, err := sheet.Cell(2, 1)
- if err != nil {
- app.ErrorMsg(c, "系统错误", nil)
- return
- }
- cell.SetString(model.ShopName)
- cell, err = sheet.Cell(2, 5)
- if err != nil {
- app.ErrorMsg(c, "系统错误", nil)
- return
- }
- cell.SetString(time.Unix(model.CreatedAt, 0).Format("2006.01.02"))
- cell, err = sheet.Cell(3, 1)
- if err != nil {
- app.ErrorMsg(c, "系统错误", nil)
- return
- }
- cell.SetString(model.AccountName)
- cell, err = sheet.Cell(4, 1)
- if err != nil {
- app.ErrorMsg(c, "系统错误", nil)
- return
- }
- cell.SetString(model.AccountBank)
- cell, err = sheet.Cell(4, 4)
- if err != nil {
- app.ErrorMsg(c, "系统错误", nil)
- return
- }
- cell.SetString(model.AccountNo)
- cell, err = sheet.Cell(5, 1)
- if err != nil {
- app.ErrorMsg(c, "系统错误", nil)
- return
- }
- cell.SetString(model.SupplierName + "(材料款)")
- cell, err = sheet.Cell(7, 1)
- if err != nil {
- app.ErrorMsg(c, "系统错误", nil)
- return
- }
- cell.SetFloat(model.Total)*/
- }
- exportFileName := utils.ToStr(time.Now().UnixNano()) + ".xlsx"
- b, err := template.WriteToBuffer()
- if err != nil {
- app.ErrorMsg(c, "系统错误", nil)
- return
- }
- f, err := os.Create(config.Cfg.App.ExportPath + exportFileName)
- if err != nil {
- app.ErrorMsg(c, "系统错误", nil)
- return
- }
- defer f.Close()
- _, _ = b.WriteTo(f)
- /*if err := template.Save(config.Cfg.App.ExportPath + exportFileName); err != nil {
- app.ErrorMsg(c, "系统错误", nil)
- return
- }*/
- app.Success(c, gin.H{"path": "export/" + exportFileName, "filename": exportFileName})
- }
|