123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- package asuModels
- //go:generate reform --gofmt=false .
- import (
- "database/sql"
- "fmt"
- "github.com/xaionaro-go/extime"
- "html/template"
- "strings"
- )
- //reform:formular_view
- type Formular struct {
- EmpGUID int `reform:"EmpGUID" view:"readonly"`
- PersonId *int `reform:"person_id" view:"readonly"`
- StfCardId int `reform:"StfCardId" view:"readonly"`
- PersonnelNumber int `reform:"TabN" view:"readonly"`
- FormularType string `reform:"Tip_formular" view:"readonly"`
- StartDate *extime.Date `reform:"StartDate" view:"readonly"`
- EndDate *extime.Date `reform:"EndDate" view:"readonly"`
- AppointmentName string `reform:"Name" view:"readonly"`
- AppointmentShortName *string `reform:"Prof_BriefName" view:"readonly"`
- WorkStart *extime.Date `reform:"Start_work" view:"readonly"`
- Category string `reform:"Category" view:"readonly"`
- CategoryCode int `reform:"Cod_Catgory" view:"readonly"`
- UnitName string `reform:"Name_podr" view:"readonly"`
- UnitShortName *string `reform:"Podr_BriefName" view:"readonly"`
- UnitCode string `reform:"Code" view:"readonly"`
- ChiefName *string `reform:"ChiefName" view:"readonly"`
- Rate *float64 `reform:"RateAmount" view:"readonly"`
- IsHead *bool `reform:"IsHead" view:"readonly"`
- UnitId int `reform:"OrgDiv" view:"readonly"`
- PersNumber *int `reform:"PersNumber" view:"readonly"`
- unit Unit `reform:"-"`
- unitReady bool `reform:"-"`
- person Person `reform:"-"`
- personReady bool `reform:"-"`
- isActive bool `reform:"-"`
- isActiveReady bool `reform:"-"`
- //FormularTypeCode int `reform:"Cod_Tip_formular"`
- //WorkEnd *string `reform:"End_work"`
- //Profession int `reform:"Profession"`
- //Lastname string `reform:"SurName"`
- //Firstname string `reform:"FirstName"`
- //Patronymic string `reform:"MiddleName"`
- //OrgInn string `reform:"OrgInn"`
- //DivInn string `reform:"DivInn"`
- //Sex int `reform:"Sex"`
- //WorkState int `reform:"WorkState"`
- //ProfKvalLevel string `reform:"ProfKvalLevel"`
- //ProfKvalGroup int `reform:"ProfKvalGroup"`
- //EmpCardId string `reform:"EmpCardId"`
- //VacDays *int `reform:"VacDays"`
- //ExtraVacDays *int `reform:"ExtraVacDays"`
- }
- func (f *Formular) AfterFind() error {
- if f.UnitShortName != nil {
- *f.UnitShortName = strings.Trim(*f.UnitShortName, " ")
- }
- f.UnitName = strings.Trim(f.UnitName, " ")
- return nil
- }
- func (sql Formular) ActiveOnly() *FormularScope {
- return sql.Scope().ActiveOnly()
- }
- func (sql *FormularScope) ActiveOnly() *FormularScope {
- return sql.Where("(StartDate IS NULL OR StartDate < NOW()) AND (EndDate IS NULL OR EndDate+24*3600 > NOW())")
- }
- func (sql Formular) PPSOnly() *FormularScope {
- return sql.Scope().PPSOnly()
- }
- func (sql *FormularScope) PPSOnly() *FormularScope {
- return sql.Where("Cod_Catgory IN (1, 8)")
- }
- // TODO: Remove this "VIEW" from this model
- func (f Formular) View_readTag(fieldName string, parent interface{}, args []interface{}) template.HTML {
- unitName := f.UnitName
- if f.UnitShortName != nil {
- unitName = *f.UnitShortName + " (" + unitName + ")"
- }
- 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))
- }
- func (f *Formular) PrepareUnit() *Formular {
- var err error
- f.unit, err = UnitSQL.First(Unit{Id: f.UnitId})
- if err != nil && err != sql.ErrNoRows {
- //revel.ERROR.Printf("Got error: %v", err.Error())
- return f
- }
- f.unitReady = true
- return f
- }
- func (f Formular) IsUnitReady() bool {
- return f.unitReady
- }
- func (f Formular) GetUnit() Unit {
- if !f.unitReady {
- panic("formular is not ready for method GetUnit: it's required to call PrepareUnit method, first")
- }
- return f.unit
- }
- func (f *Formular) PreparePerson() *Formular {
- var err error
- f.person, err = PersonSQL.First(Person{EmpGUID: f.EmpGUID})
- if err != nil && err != sql.ErrNoRows {
- //revel.ERROR.Printf("Got error: %v", err.Error())
- return f
- }
- f.personReady = true
- return f
- }
- func (f Formular) IsPersonReady() bool {
- return f.personReady
- }
- func (f Formular) GetPerson() Person {
- if !f.personReady {
- panic("formular is not ready for method GetPerson: it's required to call PreparePerson method, first")
- }
- return f.person
- }
- func (f *Formular) PrepareIsActive() *Formular {
- now := extime.Now()
- startIsGood := false
- endIsGood := false
- if f.StartDate != nil {
- startIsGood = f.StartDate.UnixNano() <= now.UnixNano()
- }
- if f.EndDate == nil {
- endIsGood = true
- } else {
- endIsGood = f.EndDate.UnixNano()+24*3600*1000*1000*1000 >= now.UnixNano()
- }
- f.isActive = startIsGood && endIsGood
- f.isActiveReady = true
- return f
- }
- func (f Formular) IsActive() bool {
- if !f.isActiveReady {
- panic("formular is not ready for method IsActive: it's required to call PrepareIsActive method, first")
- }
- return f.isActive
- }
- type Formulars []Formular
- func (formulars Formulars) PrepareIsActive() Formulars {
- for idx := range formulars {
- formulars[idx].PrepareIsActive()
- }
- return formulars
- }
- func (formulars Formulars) ToMap() map[int]*Formular {
- formularMap := map[int]*Formular{}
- for idx, formular := range formulars {
- formularMap[formular.StfCardId] = &formulars[idx]
- }
- return formularMap
- }
- func (formulars Formulars) GetStfCardIds() (stfCardIds []int) {
- for _, formular := range formulars {
- stfCardIds = append(stfCardIds, formular.StfCardId)
- }
- return
- }
- func (formulars Formulars) GetEmpGUIDs() (empGUIDs []int) {
- empGUIDMap := map[int]bool{}
- for _, formular := range formulars {
- empGUIDMap[formular.EmpGUID] = true
- }
- for empGUID := range empGUIDMap {
- empGUIDs = append(empGUIDs, empGUID)
- }
- return
- }
- func (formulars Formulars) PreparePeople() Formulars {
- ids := formulars.GetEmpGUIDs()
- if len(ids) == 0 {
- return formulars
- }
- people, err := PersonSQL.Select("EmpGUID IN (?)", ids)
- if err != nil && err != sql.ErrNoRows {
- panic(err)
- }
- personMap := People(people).ToMap()
- for idx, formular := range formulars {
- formulars[idx].person = *personMap[formular.EmpGUID]
- formulars[idx].personReady = true
- }
- return formulars
- }
- func (formulars Formulars) GetPeople() (people People) {
- for _, formular := range formulars {
- people = append(people, formular.GetPerson())
- }
- return
- }
|