jct-list-form-balance.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package controllers_join
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "net/http"
  6. "strconv"
  7. "syncscan-go/locals"
  8. models_join "syncscan-go/models/join"
  9. models_table "syncscan-go/models/table"
  10. "github.com/dabory/abango-rest"
  11. "github.com/labstack/echo"
  12. )
  13. type BalanceIdPageBase struct {
  14. Page []locals.IdBase
  15. }
  16. type ListFormBalancePageReq struct {
  17. QueryVars locals.QueryVars
  18. ListFormBalanceVars locals.ListFormBalanceVars
  19. PageVars locals.PageVars
  20. }
  21. func ListFormBalancePage(c echo.Context) error {
  22. v := c.Get("receiver").(ListFormBalancePageReq)
  23. y := c.Get("abango").(abango.Controller)
  24. vRet := &struct {
  25. models_join.ListFormBalancePageRet
  26. }{}
  27. vRet.QueryVars = v.QueryVars
  28. vRet.ListFormBalanceVars = v.ListFormBalanceVars
  29. vRet.PageVars = v.PageVars
  30. if err := vRet.GetListFormBalancePage(&y); err != nil {
  31. return c.String(607, err.Error())
  32. }
  33. ret, _ := json.Marshal(vRet)
  34. return c.JSONBlob(http.StatusOK, ret)
  35. }
  36. type ListFormBalanceMactReq struct {
  37. QueryVars locals.QueryVars
  38. ListFormBalanceVars locals.ListFormBalanceVars
  39. Page []models_join.ListFormBalance
  40. }
  41. func ListFormBalanceMact(c echo.Context) error {
  42. v := c.Get("receiver").(ListFormBalanceMactReq)
  43. y := c.Get("abango").(abango.Controller)
  44. vRet := &struct {
  45. BalanceIdPageBase
  46. }{}
  47. if v.QueryVars.QueryName == "item" {
  48. if status, err := vRet.ListFormBalanceMactItem(y, v); err != nil {
  49. return c.String(status, err.Error())
  50. }
  51. } else if v.QueryVars.QueryName == "supplier" {
  52. if status, err := vRet.ListFormBalanceMactSupplier(y, v); err != nil {
  53. return c.String(status, err.Error())
  54. }
  55. } else if v.QueryVars.QueryName == "buyer" {
  56. if status, err := vRet.ListFormBalanceMactBuyer(y, v); err != nil {
  57. return c.String(status, err.Error())
  58. }
  59. }
  60. ret, _ := json.Marshal(vRet)
  61. return c.JSONBlob(http.StatusOK, ret)
  62. }
  63. func (vRet *BalanceIdPageBase) ListFormBalanceMactBuyer(y abango.Controller, v ListFormBalanceMactReq) (int, error) {
  64. vIdRet := &locals.IdBase{}
  65. t := &struct {
  66. models_table.DbrBalBuyer
  67. }{}
  68. sess := y.Db.NewSession()
  69. defer sess.Close()
  70. if err := sess.Begin(); err == nil {
  71. for _, row := range v.Page {
  72. if !(row.Id < 0) {
  73. t.BranchId = y.Gtb.BranchId
  74. }
  75. t.YyyyMm = v.ListFormBalanceVars.YyyyMm
  76. t.Id = row.Id
  77. t.BuyerId = row.CodeId
  78. t.BalAmt = row.Balance2 //맨마지막 밸렌스 이용
  79. skipRow := false // Do NOT write row if Balaces are ZERO
  80. if bal, err := strconv.ParseFloat(row.Balance2, 32); err == nil {
  81. if bal == 0 {
  82. skipRow = true
  83. }
  84. }
  85. if t.Id == 0 && !skipRow {
  86. if err := t.AddaRow(&y); err != nil {
  87. sess.Rollback()
  88. return 603, err
  89. }
  90. } else if t.Id > 0 {
  91. if err := t.EditaRow(&y); err != nil {
  92. sess.Rollback()
  93. return 605, err
  94. }
  95. }
  96. vIdRet.Id = t.Id
  97. vRet.Page = append(vRet.Page, *vIdRet)
  98. }
  99. } else {
  100. return 608, errors.New("Transaction Action Failed")
  101. }
  102. if err := sess.Commit(); err != nil {
  103. return 608, errors.New("Transaction Commit Failed")
  104. }
  105. return 200, nil
  106. }
  107. func (vRet *BalanceIdPageBase) ListFormBalanceMactSupplier(y abango.Controller, v ListFormBalanceMactReq) (int, error) {
  108. vIdRet := &locals.IdBase{}
  109. t := &struct {
  110. models_table.DbrBalSupplier
  111. }{}
  112. sess := y.Db.NewSession()
  113. defer sess.Close()
  114. if err := sess.Begin(); err == nil {
  115. for _, row := range v.Page {
  116. if !(row.Id < 0) {
  117. t.BranchId = y.Gtb.BranchId
  118. }
  119. t.YyyyMm = v.ListFormBalanceVars.YyyyMm
  120. t.Id = row.Id
  121. t.SupplierId = row.CodeId
  122. t.BalAmt = row.Balance2 //맨마지막 밸렌스 이용
  123. skipRow := false // Do NOT write row if Balaces are ZERO
  124. if bal, err := strconv.ParseFloat(row.Balance2, 32); err == nil {
  125. if bal == 0 {
  126. skipRow = true
  127. }
  128. }
  129. if t.Id == 0 && !skipRow {
  130. if err := t.AddaRow(&y); err != nil {
  131. sess.Rollback()
  132. return 603, err
  133. }
  134. } else if t.Id > 0 {
  135. if err := t.EditaRow(&y); err != nil {
  136. sess.Rollback()
  137. return 605, err
  138. }
  139. }
  140. vIdRet.Id = t.Id
  141. vRet.Page = append(vRet.Page, *vIdRet)
  142. }
  143. } else {
  144. return 608, errors.New("Transaction Action Failed")
  145. }
  146. if err := sess.Commit(); err != nil {
  147. return 608, errors.New("Transaction Commit Failed")
  148. }
  149. return 200, nil
  150. }
  151. func (vRet *BalanceIdPageBase) ListFormBalanceMactItem(y abango.Controller, v ListFormBalanceMactReq) (int, error) {
  152. vIdRet := &locals.IdBase{}
  153. t := &struct {
  154. models_table.DbrBalItem
  155. }{}
  156. sess := y.Db.NewSession()
  157. defer sess.Close()
  158. if err := sess.Begin(); err == nil {
  159. for _, row := range v.Page {
  160. t.YyyyMm = v.ListFormBalanceVars.YyyyMm
  161. t.StorageId = v.ListFormBalanceVars.SelectedId
  162. t.Id = row.Id
  163. t.ItemId = row.CodeId
  164. t.BalQty = row.Balance
  165. t.BadBalQty = row.Balance2
  166. skipRow := false // Do NOT write row if Balaces are ZERO
  167. if bal, err := strconv.ParseFloat(row.Balance, 32); err == nil {
  168. if bal2, err := strconv.ParseFloat(row.Balance2, 32); err == nil {
  169. if bal == 0 && bal2 == 0 {
  170. skipRow = true
  171. }
  172. }
  173. }
  174. if t.Id == 0 && !skipRow {
  175. if err := t.AddaRow(&y); err != nil {
  176. sess.Rollback()
  177. return 603, err
  178. }
  179. } else if t.Id > 0 {
  180. if err := t.EditaRow(&y); err != nil {
  181. sess.Rollback()
  182. return 605, err
  183. }
  184. }
  185. vIdRet.Id = t.Id
  186. vRet.Page = append(vRet.Page, *vIdRet)
  187. }
  188. } else {
  189. return 608, errors.New("Transaction Action Failed")
  190. }
  191. if err := sess.Commit(); err != nil {
  192. return 608, errors.New("Transaction Commit Failed")
  193. }
  194. return 200, nil
  195. }