formular.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package asuModels
  2. //go:generate reform --gofmt=false .
  3. import (
  4. "database/sql"
  5. "fmt"
  6. "github.com/xaionaro-go/extime"
  7. "html/template"
  8. "strings"
  9. )
  10. //reform:formular_view
  11. type Formular struct {
  12. EmpGUID int `reform:"EmpGUID" view:"readonly"`
  13. PersonId *int `reform:"person_id" view:"readonly"`
  14. StfCardId int `reform:"StfCardId" view:"readonly"`
  15. PersonnelNumber int `reform:"TabN" view:"readonly"`
  16. FormularType string `reform:"Tip_formular" view:"readonly"`
  17. StartDate *extime.Date `reform:"StartDate" view:"readonly"`
  18. EndDate *extime.Date `reform:"EndDate" view:"readonly"`
  19. AppointmentName string `reform:"Name" view:"readonly"`
  20. AppointmentShortName *string `reform:"Prof_BriefName" view:"readonly"`
  21. WorkStart *extime.Date `reform:"Start_work" view:"readonly"`
  22. Category string `reform:"Category" view:"readonly"`
  23. CategoryCode int `reform:"Cod_Catgory" view:"readonly"`
  24. UnitName string `reform:"Name_podr" view:"readonly"`
  25. UnitShortName *string `reform:"Podr_BriefName" view:"readonly"`
  26. UnitCode string `reform:"Code" view:"readonly"`
  27. ChiefName *string `reform:"ChiefName" view:"readonly"`
  28. Rate *float64 `reform:"RateAmount" view:"readonly"`
  29. IsHead *bool `reform:"IsHead" view:"readonly"`
  30. UnitId int `reform:"OrgDiv" view:"readonly"`
  31. PersNumber *int `reform:"PersNumber" view:"readonly"`
  32. unit Unit `reform:"-"`
  33. unitReady bool `reform:"-"`
  34. person Person `reform:"-"`
  35. personReady bool `reform:"-"`
  36. isActive bool `reform:"-"`
  37. isActiveReady bool `reform:"-"`
  38. //FormularTypeCode int `reform:"Cod_Tip_formular"`
  39. //WorkEnd *string `reform:"End_work"`
  40. //Profession int `reform:"Profession"`
  41. //Lastname string `reform:"SurName"`
  42. //Firstname string `reform:"FirstName"`
  43. //Patronymic string `reform:"MiddleName"`
  44. //OrgInn string `reform:"OrgInn"`
  45. //DivInn string `reform:"DivInn"`
  46. //Sex int `reform:"Sex"`
  47. //WorkState int `reform:"WorkState"`
  48. //ProfKvalLevel string `reform:"ProfKvalLevel"`
  49. //ProfKvalGroup int `reform:"ProfKvalGroup"`
  50. //EmpCardId string `reform:"EmpCardId"`
  51. //VacDays *int `reform:"VacDays"`
  52. //ExtraVacDays *int `reform:"ExtraVacDays"`
  53. }
  54. func (f *Formular) AfterFind() error {
  55. if f.UnitShortName != nil {
  56. *f.UnitShortName = strings.Trim(*f.UnitShortName, " ")
  57. }
  58. f.UnitName = strings.Trim(f.UnitName, " ")
  59. return nil
  60. }
  61. func (sql Formular) ActiveOnly() *FormularScope {
  62. return sql.Scope().ActiveOnly()
  63. }
  64. func (sql *FormularScope) ActiveOnly() *FormularScope {
  65. return sql.Where("(StartDate IS NULL OR StartDate < NOW()) AND (EndDate IS NULL OR EndDate+24*3600 > NOW())")
  66. }
  67. func (sql Formular) PPSOnly() *FormularScope {
  68. return sql.Scope().PPSOnly()
  69. }
  70. func (sql *FormularScope) PPSOnly() *FormularScope {
  71. return sql.Where("Cod_Catgory IN (1, 8)")
  72. }
  73. // TODO: Remove this "VIEW" from this model
  74. func (f Formular) View_readTag(fieldName string, parent interface{}, args []interface{}) template.HTML {
  75. unitName := f.UnitName
  76. if f.UnitShortName != nil {
  77. unitName = *f.UnitShortName + " (" + unitName + ")"
  78. }
  79. return template.HTML(fmt.Sprintf(`<a href="/asu/formulars/%v?fullscreen=true">%v — %v: [%v] %v — %v</a>`, f.StfCardId, f.StartDate, f.EndDate, f.UnitCode, f.AppointmentName, unitName))
  80. }
  81. func (f *Formular) PrepareUnit() *Formular {
  82. var err error
  83. f.unit, err = UnitSQL.First(Unit{Id: f.UnitId})
  84. if err != nil && err != sql.ErrNoRows {
  85. //revel.ERROR.Printf("Got error: %v", err.Error())
  86. return f
  87. }
  88. f.unitReady = true
  89. return f
  90. }
  91. func (f Formular) IsUnitReady() bool {
  92. return f.unitReady
  93. }
  94. func (f Formular) GetUnit() Unit {
  95. if !f.unitReady {
  96. panic("formular is not ready for method GetUnit: it's required to call PrepareUnit method, first")
  97. }
  98. return f.unit
  99. }
  100. func (f *Formular) PreparePerson() *Formular {
  101. var err error
  102. f.person, err = PersonSQL.First(Person{EmpGUID: f.EmpGUID})
  103. if err != nil && err != sql.ErrNoRows {
  104. //revel.ERROR.Printf("Got error: %v", err.Error())
  105. return f
  106. }
  107. f.personReady = true
  108. return f
  109. }
  110. func (f Formular) IsPersonReady() bool {
  111. return f.personReady
  112. }
  113. func (f Formular) GetPerson() Person {
  114. if !f.personReady {
  115. panic("formular is not ready for method GetPerson: it's required to call PreparePerson method, first")
  116. }
  117. return f.person
  118. }
  119. func (f *Formular) PrepareIsActive() *Formular {
  120. now := extime.Now()
  121. startIsGood := false
  122. endIsGood := false
  123. if f.StartDate != nil {
  124. startIsGood = f.StartDate.UnixNano() <= now.UnixNano()
  125. }
  126. if f.EndDate == nil {
  127. endIsGood = true
  128. } else {
  129. endIsGood = f.EndDate.UnixNano()+24*3600*1000*1000*1000 >= now.UnixNano()
  130. }
  131. f.isActive = startIsGood && endIsGood
  132. f.isActiveReady = true
  133. return f
  134. }
  135. func (f Formular) IsActive() bool {
  136. if !f.isActiveReady {
  137. panic("formular is not ready for method IsActive: it's required to call PrepareIsActive method, first")
  138. }
  139. return f.isActive
  140. }
  141. type Formulars []Formular
  142. func (formulars Formulars) PrepareIsActive() Formulars {
  143. for idx := range formulars {
  144. formulars[idx].PrepareIsActive()
  145. }
  146. return formulars
  147. }
  148. func (formulars Formulars) ToMap() map[int]*Formular {
  149. formularMap := map[int]*Formular{}
  150. for idx, formular := range formulars {
  151. formularMap[formular.StfCardId] = &formulars[idx]
  152. }
  153. return formularMap
  154. }
  155. func (formulars Formulars) GetStfCardIds() (stfCardIds []int) {
  156. for _, formular := range formulars {
  157. stfCardIds = append(stfCardIds, formular.StfCardId)
  158. }
  159. return
  160. }
  161. func (formulars Formulars) GetEmpGUIDs() (empGUIDs []int) {
  162. empGUIDMap := map[int]bool{}
  163. for _, formular := range formulars {
  164. empGUIDMap[formular.EmpGUID] = true
  165. }
  166. for empGUID := range empGUIDMap {
  167. empGUIDs = append(empGUIDs, empGUID)
  168. }
  169. return
  170. }
  171. func (formulars Formulars) PreparePeople() Formulars {
  172. ids := formulars.GetEmpGUIDs()
  173. if len(ids) == 0 {
  174. return formulars
  175. }
  176. people, err := PersonSQL.Select("EmpGUID IN (?)", ids)
  177. if err != nil && err != sql.ErrNoRows {
  178. panic(err)
  179. }
  180. personMap := People(people).ToMap()
  181. for idx, formular := range formulars {
  182. formulars[idx].person = *personMap[formular.EmpGUID]
  183. formulars[idx].personReady = true
  184. }
  185. return formulars
  186. }
  187. func (formulars Formulars) GetPeople() (people People) {
  188. for _, formular := range formulars {
  189. people = append(people, formular.GetPerson())
  190. }
  191. return
  192. }