Browse Source

Considered PersNumbers, fixed nil-bug in timesheet_row.go

Dmitry Yu Okunev 6 years ago
parent
commit
27c78d1064
3 changed files with 45 additions and 5 deletions
  1. 1 1
      formular.go
  2. 1 1
      timesheet_row.go
  3. 43 3
      unit.go

+ 1 - 1
formular.go

@@ -30,6 +30,7 @@ type Formular struct {
 	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:"-"`
@@ -40,7 +41,6 @@ type Formular struct {
 	isActive      bool `reform:"-"`
 	isActiveReady bool `reform:"-"`
 
-	//PersNumber           *int         `reform:"PersNumber"`
 	//FormularTypeCode   int     `reform:"Cod_Tip_formular"`
 	//CategoryCode       int     `reform:"Cod_Catgory"`
 	//WorkEnd           *string  `reform:"End_work"`

+ 1 - 1
timesheet_row.go

@@ -21,7 +21,7 @@ type TimesheetRow struct {
 	Hours         *int        `reform:"Hours"`
 	Author        int         `reform:"Author"`
 	ParentId      int         `reform:"ParentId"`
-	TableId       int         `reform:"TableId"`
+	TableId       *int        `reform:"TableId"`
 	TableState    int         `reform:"TableState"`
 	State         int         `reform:"State"`
 	AstDate       int         `reform:"AstDate"`

+ 43 - 3
unit.go

@@ -103,6 +103,19 @@ func (unit Unit) IsChildrenReady() bool {
 
 type Units []Unit
 
+func (units Units) ToPersNumberMap() map[int]*Unit {
+	unitMap := map[int]*Unit{}
+
+	for idx, unit := range units {
+		if unit.PersNumber == nil {
+			continue
+		}
+		unitMap[*unit.PersNumber] = &units[idx]
+	}
+
+	return unitMap
+}
+
 func (units Units) ToMap() map[int]*Unit {
 	unitMap := map[int]*Unit{}
 
@@ -175,7 +188,15 @@ func (u Unit) GetChildrenPtrs() []*Unit {
 
 func (u *Unit) PrepareFormulars() *Unit {
 	var err error
-	u.formulars, err = FormularSQL.Select(Formular{UnitId: u.Id})
+
+	if u.PersNumber == nil {
+		u.formulars, err = FormularSQL.Select(Formular{UnitId: u.Id})
+	} else {
+		u.formulars, err = FormularSQL.Select(Formular{PersNumber: u.PersNumber})
+		for idx, _ := range u.formulars {
+			u.formulars[idx].UnitId = u.Id
+		}
+	}
 	if err != nil && err != sql.ErrNoRows {
 		panic(err)
 	}
@@ -201,6 +222,7 @@ func (u Unit) GetFormulars() Formulars {
 
 func (units Units) PrepareFormulars(activeOnly bool) Units {
 	ids := units.GetUnitIds()
+	nums := units.GetPersNumbers()
 
 	scope := FormularSQL.Scope()
 
@@ -208,15 +230,22 @@ func (units Units) PrepareFormulars(activeOnly bool) Units {
 		scope = scope.ActiveOnly()
 	}
 
-	formulars, err := scope.Select("OrgDiv IN (?)", ids)
+	formulars, err := scope.Select("PersNumber IN (?) OR OrgDiv IN (?)", nums, ids)
 	if err != nil && err != sql.ErrNoRows {
 		panic(err)
 	}
 
 	unitsMap := units.ToMap()
+	unitsPMap := units.ToPersNumberMap()
 
 	for _, formular := range formulars {
-		(*unitsMap[formular.UnitId]).formulars = append((*unitsMap[formular.UnitId]).formulars, formular)
+		if formular.PersNumber == nil {
+			(*unitsMap[formular.UnitId]).formulars = append((*unitsMap[formular.UnitId]).formulars, formular)
+			continue
+		}
+		unitPtr := unitsPMap[*formular.PersNumber]
+		formular.UnitId = (*unitPtr).Id
+		(*unitPtr).formulars = append((*unitPtr).formulars, formular)
 	}
 
 	for idx, _ := range units {
@@ -333,6 +362,17 @@ func GetUnitsHeadedBy(empGUID int) (units Units) {
 	return
 }
 
+func (units Units) GetPersNumbers() (persNumbers []int) {
+	for _, unit := range units {
+		if unit.PersNumber == nil {
+			continue
+		}
+		persNumbers = append(persNumbers, *unit.PersNumber)
+	}
+
+	return
+}
+
 func (units Units) GetUnitIds() (unitIds []int) {
 	for _, unit := range units {
 		unitIds = append(unitIds, unit.Id)