ctr_contract_invoice.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. package service
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. dao "dashoo.cn/micro/app/dao/contract"
  7. model "dashoo.cn/micro/app/model/contract"
  8. "dashoo.cn/opms_libary/micro_srv"
  9. "dashoo.cn/opms_libary/myerrors"
  10. "dashoo.cn/opms_libary/request"
  11. "github.com/gogf/gf/database/gdb"
  12. "github.com/gogf/gf/os/gtime"
  13. "github.com/gogf/gf/util/gvalid"
  14. )
  15. type CtrContractInvoiceService struct {
  16. Dao *dao.CtrContractInvoiceDao
  17. ContractDao *dao.CtrContractDao
  18. ctrSrv *CtrContractService
  19. Tenant string
  20. userInfo request.UserInfo
  21. }
  22. func NewCtrContractInvoiceService(ctx context.Context) (*CtrContractInvoiceService, error) {
  23. tenant, err := micro_srv.GetTenant(ctx)
  24. if err != nil {
  25. return nil, myerrors.TipsError(fmt.Sprintf("获取组合码异常:%s", err.Error())) //fmt.Errorf("获取组合码异常:%s", err.Error())
  26. }
  27. // 获取用户信息
  28. userInfo, err := micro_srv.GetUserInfo(ctx)
  29. if err != nil {
  30. return nil, myerrors.TipsError(fmt.Sprintf("获取用户信息异常:%s", err.Error())) //fmt.Errorf("获取用户信息异常:%s", err.Error())
  31. }
  32. ctrSrv, err := NewCtrContractService(ctx)
  33. if err != nil {
  34. return nil, err
  35. }
  36. return &CtrContractInvoiceService{
  37. Dao: dao.NewCtrContractInvoiceDao(tenant),
  38. ContractDao: dao.NewCtrContractDao(tenant),
  39. ctrSrv: ctrSrv,
  40. Tenant: tenant,
  41. userInfo: userInfo,
  42. }, nil
  43. }
  44. func (s CtrContractInvoiceService) List(ctx context.Context, req *model.CtrContractInvoiceListReq) (int, []*model.CtrContractInvoice, error) {
  45. dao := &s.Dao.CtrContractInvoiceDao
  46. if req.SearchText != "" {
  47. likestr := fmt.Sprintf("%%%s%%", req.SearchText)
  48. dao = dao.Where("(cust_name LIKE ? || contract_code LIKE ?)", likestr, likestr)
  49. }
  50. if req.CustId != 0 {
  51. dao = dao.Where("cust_id = ?", req.CustId)
  52. }
  53. if req.CustName != "" {
  54. likestr := fmt.Sprintf("%%%s%%", req.CustName)
  55. dao = dao.Where("cust_name like ?", likestr)
  56. }
  57. if req.ContractId != 0 {
  58. dao = dao.Where("contract_id = ?", req.ContractId)
  59. }
  60. if req.ContractCode != "" {
  61. likestr := fmt.Sprintf("%%%s%%", req.ContractCode)
  62. dao = dao.Where("contract_code like ?", likestr)
  63. }
  64. if req.InvoiceType != "" {
  65. dao = dao.Where("invoice_type = ?", req.InvoiceType)
  66. }
  67. if req.ApproStatus != "" {
  68. dao = dao.Where("appro_status = ?", req.ApproStatus)
  69. }
  70. if req.InvoiceCode != "" {
  71. dao = dao.Where("invoice_code = ?", req.InvoiceCode)
  72. }
  73. if req.CourierCode != "" {
  74. dao = dao.Where("courier_code = ?", req.CourierCode)
  75. }
  76. if req.BeginTime != "" {
  77. dao = dao.Where("created_time > ?", req.BeginTime)
  78. }
  79. if req.EndTime != "" {
  80. dao = dao.Where("created_time < ?", req.EndTime)
  81. }
  82. total, err := dao.Count()
  83. if err != nil {
  84. return 0, nil, err
  85. }
  86. if req.PageNum != 0 {
  87. dao = dao.Page(req.GetPage())
  88. }
  89. orderby := "created_time desc"
  90. if req.OrderBy != "" {
  91. orderby = req.OrderBy
  92. }
  93. dao = dao.Order(orderby)
  94. ents := []*model.CtrContractInvoice{}
  95. err = dao.Structs(&ents)
  96. if err != nil && err != sql.ErrNoRows {
  97. return 0, nil, err
  98. }
  99. return total, ents, err
  100. }
  101. func (s CtrContractInvoiceService) Add(ctx context.Context, req *model.CtrContractInvoiceAddReq) (int, error) {
  102. validErr := gvalid.CheckStruct(ctx, req, nil)
  103. if validErr != nil {
  104. return 0, myerrors.TipsError(validErr.Current().Error())
  105. }
  106. c, err := s.ContractDao.Where("id = ?", req.ContractId).One()
  107. if err != nil {
  108. return 0, err
  109. }
  110. if c == nil {
  111. return 0, myerrors.TipsError(fmt.Sprintf("合同:%d 不存在", req.ContractId))
  112. }
  113. if req.InvoiceCode != "" {
  114. ent, err := s.Dao.Where("invoice_code = ?", req.InvoiceCode).One()
  115. if err != nil {
  116. return 0, err
  117. }
  118. if ent != nil {
  119. return 0, myerrors.TipsError(fmt.Sprintf("发票号码:%s 已存在", req.InvoiceCode))
  120. }
  121. }
  122. if req.CourierCode != "" {
  123. ent, err := s.Dao.Where("courier_code = ?", req.CourierCode).One()
  124. if err != nil {
  125. return 0, err
  126. }
  127. if ent != nil {
  128. return 0, myerrors.TipsError(fmt.Sprintf("快递单号:%s 已存在", req.CourierCode))
  129. }
  130. }
  131. ent := model.CtrContractInvoice{
  132. CustId: c.CustId,
  133. CustName: c.CustName,
  134. ContractId: req.ContractId,
  135. ContractCode: c.ContractCode,
  136. ContractAmount: c.ContractAmount,
  137. InvoiceAmount: req.InvoiceAmount,
  138. InvoiceDate: req.InvoiceDate,
  139. InvoiceType: req.InvoiceType,
  140. ApproStatus: "10",
  141. InvoiceCode: req.InvoiceCode,
  142. ActualInvoiceDate: req.ActualInvoiceDate,
  143. CourierCode: req.CourierCode,
  144. Remark: req.Remark,
  145. CreatedBy: int(s.userInfo.Id),
  146. CreatedName: s.userInfo.NickName,
  147. CreatedTime: gtime.Now(),
  148. UpdatedBy: int(s.userInfo.Id),
  149. UpdatedName: s.userInfo.NickName,
  150. UpdatedTime: gtime.Now(),
  151. DeletedTime: gtime.Now(),
  152. }
  153. var id int
  154. txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  155. invoiceId, err := tx.InsertAndGetId("ctr_contract_invoice", ent)
  156. if err != nil {
  157. return err
  158. }
  159. err = s.ctrSrv.AddDynamicsByCurrentUser(tx, req.ContractId, "创建发票", map[string]interface{}{
  160. "id": invoiceId,
  161. "invoiceAmount": req.InvoiceAmount,
  162. "invoiceDate": req.InvoiceDate,
  163. "invoiceType": req.InvoiceType,
  164. "invoiceCode": req.InvoiceCode,
  165. "actualInvoiceDate": req.ActualInvoiceDate,
  166. "courierCode": req.CourierCode,
  167. })
  168. if err != nil {
  169. return err
  170. }
  171. id = int(invoiceId)
  172. return nil
  173. })
  174. return id, txerr
  175. }
  176. func (s CtrContractInvoiceService) Update(ctx context.Context, req *model.CtrContractInvoiceUpdateReq) error {
  177. validErr := gvalid.CheckStruct(ctx, req, nil)
  178. if validErr != nil {
  179. return myerrors.TipsError(validErr.Current().Error())
  180. }
  181. ent, err := s.Dao.Where("id = ?", req.Id).One()
  182. if err != nil {
  183. return err
  184. }
  185. if ent == nil {
  186. return myerrors.TipsError(fmt.Sprintf("发票不存在: %d", req.Id))
  187. }
  188. if req.InvoiceCode != "" {
  189. exist, err := s.Dao.Where("invoice_code = ?", req.InvoiceCode).One()
  190. if err != nil {
  191. return err
  192. }
  193. if exist != nil && exist.Id != req.Id {
  194. return myerrors.TipsError(fmt.Sprintf("发票号码: %s 已存在", req.InvoiceCode))
  195. }
  196. }
  197. if req.CourierCode != "" {
  198. exist, err := s.Dao.Where("courier_code = ?", req.CourierCode).One()
  199. if err != nil {
  200. return err
  201. }
  202. if exist != nil && exist.Id != req.Id {
  203. return myerrors.TipsError(fmt.Sprintf("快递单号: %s 已存在", req.CourierCode))
  204. }
  205. }
  206. toupdate := map[string]interface{}{}
  207. // if req.CustId != 0 {
  208. // toupdate["cust_id"] = req.CustId
  209. // }
  210. // if req.CustName != 0 {
  211. // toupdate["cust_name"] = req.CustName
  212. // }
  213. // if req.ContractId != 0 {
  214. // toupdate["contract_id"] = req.ContractId
  215. // }
  216. // if req.ContractCode != 0 {
  217. // toupdate["contract_code"] = req.ContractCode
  218. // }
  219. // if req.ContractAmount != 0 {
  220. // toupdate["contract_amount"] = req.ContractAmount
  221. // }
  222. if req.InvoiceAmount != nil {
  223. toupdate["invoice_amount"] = *req.InvoiceAmount
  224. }
  225. if req.InvoiceDate != nil {
  226. toupdate["invoice_date"] = req.InvoiceDate
  227. }
  228. if req.InvoiceType != "" {
  229. toupdate["invoice_type"] = req.InvoiceType
  230. }
  231. if req.ApproStatus != "" {
  232. toupdate["appro_status"] = req.ApproStatus
  233. }
  234. if req.InvoiceCode != "" {
  235. toupdate["invoice_code"] = req.InvoiceCode
  236. }
  237. if req.ActualInvoiceDate != nil {
  238. toupdate["actual_invoice_date"] = req.ActualInvoiceDate
  239. }
  240. if req.CourierCode != "" {
  241. toupdate["courier_code"] = req.CourierCode
  242. }
  243. if req.Remark != nil {
  244. toupdate["remark"] = *req.Remark
  245. }
  246. if len(toupdate) != 0 {
  247. toupdate["updated_by"] = int(s.userInfo.Id)
  248. toupdate["updated_name"] = s.userInfo.NickName
  249. toupdate["updated_time"] = gtime.Now()
  250. txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  251. _, err = tx.Update("ctr_contract_invoice", toupdate, "id= ?", req.Id)
  252. if err != nil {
  253. return err
  254. }
  255. if req.InvoiceAmount != nil || req.ApproStatus != "" {
  256. err = s.ctrSrv.UpdateInvoiceAmount(tx, ent.ContractId)
  257. if err != nil {
  258. return err
  259. }
  260. }
  261. return nil
  262. })
  263. if txerr != nil {
  264. return txerr
  265. }
  266. }
  267. return nil
  268. }
  269. func (s CtrContractInvoiceService) Delete(ctx context.Context, id []int) error {
  270. if len(id) == 0 {
  271. return nil
  272. }
  273. return s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  274. contractId := map[int]bool{}
  275. for _, i := range id {
  276. ent := model.CtrContractInvoice{}
  277. err := tx.GetStruct(&ent, "select * from ctr_contract_invoice where id = ?", i)
  278. if err == sql.ErrNoRows {
  279. continue
  280. }
  281. if err != nil {
  282. return err
  283. }
  284. if ent.ApproStatus != "20" {
  285. continue
  286. }
  287. contractId[ent.ContractId] = true
  288. }
  289. _, err := tx.Delete("ctr_contract_invoice", "id in (?)", id)
  290. if err != nil {
  291. return err
  292. }
  293. for cid := range contractId {
  294. err = s.ctrSrv.UpdateInvoiceAmount(tx, cid)
  295. if err != nil {
  296. return err
  297. }
  298. }
  299. return nil
  300. })
  301. }