mainqaq hace 3 años
padre
commit
352f3e27e2
Se han modificado 2 ficheros con 127 adiciones y 27 borrados
  1. 105 17
      controllers/supply_order_api_contorller.go
  2. 22 10
      service/supply_service.go

+ 105 - 17
controllers/supply_order_api_contorller.go Ver fichero

@@ -289,25 +289,72 @@ func (this *SupplyOrderApiController) UpdateSupply() {
289 289
 	bank := dataBody["bank"].(string) //纳税人识别号
290 290
 	//bankAccount := this.GetString("bankAccount")     //银行账号
291 291
 	bankAccount := dataBody["bankaccount"].(string)
292
+	tmodify := this.GetAdminUserInfo().AdminUser.Id //修改者
292 293
 	//保存联系人
293 294
 	if dataBody["contacts"] != nil && reflect.TypeOf(dataBody["contacts"]).String() == "[]interface {}" {
294 295
 		thisStockIn, _ := dataBody["contacts"].([]interface{})
295 296
 		if len(thisStockIn) > 0 {
296
-			if len(thisStockIn) == 1 {
297
+			for _, v := range thisStockIn { //这个循环是用来检测当填了手机号时联系人不能为空
298
+				vtem := v.(map[string]interface{})
299
+				if vtem["phone"].(string) != "" && vtem["name"].(string) == "" {
300
+					this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "联系人不能为空")
301
+					return
302
+				}
303
+			}
304
+			if len(thisStockIn) == 1 { //当只有一条联系人时强制设为首要联系人
297 305
 				for _, item := range thisStockIn {
298 306
 					items := item.(map[string]interface{})
299 307
 					items["is_first"] = 1
308
+					//如果电话和联系人都为空,不保存联系人
309
+					if items["phone"] == "" && items["name"] == "" {
310
+						upsupply := models.SpSupplierName{
311
+							ID:           supplierid,
312
+							SupplierCode: supplierCode,
313
+							SupplierName: supplierName,
314
+							SupplierType: supplierType,
315
+							VatRate:      vatRate,
316
+							Number:       number,
317
+							Bank:         bank,
318
+							BankAccount:  bankAccount,
319
+							Status:       1,
320
+							Mtime:        time.Now().Unix(),
321
+							Modify:       tmodify,
322
+						}
323
+						err = service.UpdateSupplyName(upsupply)
324
+						if err != nil {
325
+							this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
326
+							return
327
+						}
328
+					}
300 329
 				}
301 330
 			}
302
-			tmodify := this.GetAdminUserInfo().AdminUser.Id
303 331
 			err = service.UpdateSupplyAndContact(thisStockIn, supplierid, orgId, supplierType, tmodify, supplierCode, supplierName, number, bank, bankAccount, vatRate)
304 332
 			if err != nil {
305 333
 				this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
306 334
 				return
307 335
 			}
308 336
 		} else {
309
-			this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "联系人不能为空")
310
-			return
337
+			//如果联系人是空的,直接保存供应商
338
+			//更新供应商
339
+			upsupply := models.SpSupplierName{
340
+				ID:           supplierid,
341
+				SupplierCode: supplierCode,
342
+				SupplierName: supplierName,
343
+				SupplierType: supplierType,
344
+				VatRate:      vatRate,
345
+				Number:       number,
346
+				Bank:         bank,
347
+				BankAccount:  bankAccount,
348
+				Status:       1,
349
+				Mtime:        time.Now().Unix(),
350
+				Modify:       tmodify,
351
+			}
352
+			err = service.UpdateSupplyName(upsupply)
353
+			if err != nil {
354
+				this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
355
+				return
356
+			}
357
+
311 358
 		}
312 359
 	}
313 360
 	this.ServeSuccessJSON(map[string]interface{}{
@@ -520,7 +567,7 @@ func (this *SupplyOrderApiController) SaveSupply() {
520 567
 		dataBody["suppliertype"] = tmp
521 568
 	}
522 569
 	if dataBody["vatrate"] == "" {
523
-		dataBody["vatrate"] = 0
570
+		dataBody["vatrate"] = "0"
524 571
 	}
525 572
 	//supplierType, _ := this.GetInt64("suppliertype")
526 573
 	if dataBody["suppliertype"] == "" || dataBody["suppliertype"] == 0 {
@@ -544,33 +591,74 @@ func (this *SupplyOrderApiController) SaveSupply() {
544 591
 	bank := dataBody["bank"].(string)
545 592
 	//bankAccount := this.GetString("bankAccount")
546 593
 	bankAccount := dataBody["bankaccount"].(string)
547
-
548
-	//contacts := this.Get("contacts")//联系人
549
-	//err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody)
550
-	//if err != nil {
551
-	//	utils.ErrorLog(err.Error())
552
-	//	this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
553
-	//	return
554
-	//}
594
+	tcreater := this.GetAdminUserInfo().AdminUser.Id
555 595
 	//保存联系人
556 596
 	if dataBody["contacts"] != nil && reflect.TypeOf(dataBody["contacts"]).String() == "[]interface {}" {
557 597
 		thisStockIn, _ := dataBody["contacts"].([]interface{})
558 598
 		if len(thisStockIn) > 0 {
559
-			if len(thisStockIn) == 1 {
599
+			for _, v := range thisStockIn { //这个循环是用来检测当填了手机号时联系人不能为空
600
+				vtem := v.(map[string]interface{})
601
+				if vtem["phone"] != "" && vtem["name"] == nil {
602
+					this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "联系人不能为空")
603
+					return
604
+				}
605
+			}
606
+			if len(thisStockIn) == 1 { //当只有一条联系人时强制设为首要联系人,
560 607
 				for _, item := range thisStockIn {
561 608
 					items := item.(map[string]interface{})
562 609
 					items["is_first"] = 1
610
+					//如果电话和联系人都为空,不保存联系人
611
+					if items["phone"] == "" && items["name"] == "" {
612
+						supply := models.SpSupplierName{
613
+							SupplierCode: supplierCode,
614
+							SupplierName: supplierName,
615
+							SupplierType: supplierType,
616
+							VatRate:      vatRate,
617
+							Number:       number,
618
+							Bank:         bank,
619
+							BankAccount:  bankAccount,
620
+							UserOrgId:    orgId,
621
+							Status:       1,
622
+							Ctime:        time.Now().Unix(),
623
+							Mtime:        0,
624
+							Creater:      tcreater,
625
+							Modify:       tcreater,
626
+						}
627
+						err = service.SaveSupply(supply)
628
+						if err != nil {
629
+							this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
630
+							return
631
+						}
632
+					}
563 633
 				}
564 634
 			}
565
-			tcreater := this.GetAdminUserInfo().AdminUser.Id
566 635
 			err = service.SaveSupplyAndContact(thisStockIn, orgId, supplierType, tcreater, supplierCode, supplierName, number, bank, bankAccount, vatRate)
567 636
 			if err != nil {
568 637
 				this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
569 638
 				return
570 639
 			}
571 640
 		} else {
572
-			this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "联系人不能为空")
573
-			return
641
+			//如果联系人是空的,这里直接保存供应商
642
+			supply := models.SpSupplierName{
643
+				SupplierCode: supplierCode,
644
+				SupplierName: supplierName,
645
+				SupplierType: supplierType,
646
+				VatRate:      vatRate,
647
+				Number:       number,
648
+				Bank:         bank,
649
+				BankAccount:  bankAccount,
650
+				UserOrgId:    orgId,
651
+				Status:       1,
652
+				Ctime:        time.Now().Unix(),
653
+				Mtime:        0,
654
+				Creater:      tcreater,
655
+				Modify:       tcreater,
656
+			}
657
+			err = service.SaveSupply(supply)
658
+			if err != nil {
659
+				this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
660
+				return
661
+			}
574 662
 		}
575 663
 	}
576 664
 	this.ServeSuccessJSON(map[string]interface{}{

+ 22 - 10
service/supply_service.go Ver fichero

@@ -43,7 +43,7 @@ func UpdateSupplyAndContact(thisStockIn []interface{}, suid, orgId, supplierType
43 43
 	}()
44 44
 	for _, item := range thisStockIn {
45 45
 		items := item.(map[string]interface{})
46
-		//查询是否
46
+		//判断联系人是否为新加的
47 47
 		if items["id"] == "" || items["id"] == 0 {
48 48
 			var tmp float64 = 0
49 49
 			items["id"] = tmp
@@ -76,7 +76,7 @@ func UpdateSupplyAndContact(thisStockIn []interface{}, suid, orgId, supplierType
76 76
 			Ctime:        0,
77 77
 			Mtime:        time.Now().Unix(),
78 78
 		}
79
-		if id == 0 {
79
+		if id == 0 { //表示这是新加的
80 80
 			spcontacts := models.SpSupplierContacts{
81 81
 				Name:         name,
82 82
 				Phone:        phone,
@@ -87,7 +87,7 @@ func UpdateSupplyAndContact(thisStockIn []interface{}, suid, orgId, supplierType
87 87
 				Status:       1,
88 88
 				Ctime:        time.Now().Unix(),
89 89
 				Mtime:        0,
90
-			}
90
+			} //保存一条联系人的数据
91 91
 			err = SaveContacts(spcontacts, tx)
92 92
 			if err != nil {
93 93
 				return err
@@ -97,7 +97,7 @@ func UpdateSupplyAndContact(thisStockIn []interface{}, suid, orgId, supplierType
97 97
 		}
98 98
 		var tmpid int64
99 99
 		if isfirst == 1 {
100
-			if id == 0 {
100
+			if id == 0 { //新加的首要联系人
101 101
 				var spconid []*models.SpSupplierContacts
102 102
 				spconid, err = SaveContactsId(tx, orgId)
103 103
 				if err != nil {
@@ -123,7 +123,7 @@ func UpdateSupplyAndContact(thisStockIn []interface{}, suid, orgId, supplierType
123 123
 				Mtime:        time.Now().Unix(),
124 124
 				Modify:       tcreater,
125 125
 			}
126
-			err = UpdateSupplyName(upsupply, tx)
126
+			err = UpdateSupplyNameTX(upsupply, tx)
127 127
 			if err != nil {
128 128
 				return err
129 129
 			}
@@ -133,12 +133,18 @@ func UpdateSupplyAndContact(thisStockIn []interface{}, suid, orgId, supplierType
133 133
 	return err
134 134
 }
135 135
 
136
-//更新一条供应商的信息
137
-func UpdateSupplyName(upsupply models.SpSupplierName, tx *gorm.DB) error {
136
+//更新一条供应商的信息,添加了事务的
137
+func UpdateSupplyNameTX(upsupply models.SpSupplierName, tx *gorm.DB) error {
138 138
 	err := tx.Model(&models.SpSupplierName{}).Where("id = ? and status = 1", upsupply.ID).Updates(map[string]interface{}{"supplier_code": upsupply.SupplierCode, "supplier_name": upsupply.SupplierName, "supplier_type": upsupply.SupplierType, "vat_rate": upsupply.VatRate, "number": upsupply.Number, "bank": upsupply.Bank, "bank_account": upsupply.BankAccount, "user_org_id": upsupply.UserOrgId, "status": upsupply.Status, "contacts_id": upsupply.ContactsId, "mtime": upsupply.Mtime, "modify": upsupply.Modify}).Error
139 139
 	return err
140 140
 }
141 141
 
142
+//更新一条供应商的信息,没有添加事务
143
+func UpdateSupplyName(upsupply models.SpSupplierName) error {
144
+	err := XTWriteDB().Model(&models.SpSupplierName{}).Where("id = ? and status = 1", upsupply.ID).Updates(map[string]interface{}{"supplier_code": upsupply.SupplierCode, "supplier_name": upsupply.SupplierName, "supplier_type": upsupply.SupplierType, "vat_rate": upsupply.VatRate, "number": upsupply.Number, "bank": upsupply.Bank, "bank_account": upsupply.BankAccount, "user_org_id": upsupply.UserOrgId, "status": upsupply.Status, "contacts_id": upsupply.ContactsId, "mtime": upsupply.Mtime, "modify": upsupply.Modify}).Error
145
+	return err
146
+}
147
+
142 148
 //更新一条联系人的信息
143 149
 func UpdateContact(updatecontacts models.SpSupplierContacts, tx *gorm.DB) error {
144 150
 	err := tx.Model(&models.SpSupplierContacts{}).Where("id = ? and status = 1", updatecontacts.ID).Updates(map[string]interface{}{"name": updatecontacts.Name, "phone": updatecontacts.Phone, "address": updatecontacts.Address, "is_first": updatecontacts.IsFirst, "supplier_code": updatecontacts.SupplierCode, "user_org_id": updatecontacts.UserOrgId, "status": updatecontacts.Status, "mtime": updatecontacts.Mtime}).Error
@@ -255,7 +261,7 @@ func SaveSupplyAndContact(thisStockIn []interface{}, orgId, supplierType, tcreat
255 261
 				Creater:      tcreater,
256 262
 				Modify:       tcreater,
257 263
 			}
258
-			err = SaveSupply(supply, tx)
264
+			err = SaveSupplyTx(supply, tx)
259 265
 			if err != nil {
260 266
 				return err
261 267
 			}
@@ -309,12 +315,18 @@ func FindSupplierCodes(supplierCode string, orgid int64) (codebool bool, err err
309 315
 	return codebool, err
310 316
 }
311 317
 
312
-//保存一条供应商数据
313
-func SaveSupply(supply models.SpSupplierName, tx *gorm.DB) error {
318
+//保存一条供应商数据加事务的
319
+func SaveSupplyTx(supply models.SpSupplierName, tx *gorm.DB) error {
314 320
 	err := tx.Create(&supply).Error
315 321
 	return err
316 322
 }
317 323
 
324
+//保存一条供应商数据没有事务的
325
+func SaveSupply(supply models.SpSupplierName) error {
326
+	err := XTWriteDB().Create(&supply).Error
327
+	return err
328
+}
329
+
318 330
 //获取联系人的id
319 331
 func SaveContactsId(tx *gorm.DB, orgid int64) (spconid []*models.SpSupplierContacts, err error) {
320 332
 	err = tx.Model(&models.SpSupplierContacts{}).Select("id").Where("status = 1 and user_org_id = ?", orgid).Order("id desc").First(&spconid).Error