mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-25 15:55:44 +00:00
fix perhitunga sapronak
This commit is contained in:
@@ -573,17 +573,21 @@ func (s closingService) getSapronakDateRange(ctx context.Context, projectFlockID
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
var minChickin *time.Time
|
||||
var firstChickin struct {
|
||||
ChickInDate time.Time `gorm:"column:chick_in_date"`
|
||||
}
|
||||
if err := db.Table("project_chickins").
|
||||
Select("MIN(chick_in_date)").
|
||||
Where("project_flock_kandang_id = ?", pfk.Id).
|
||||
Scan(&minChickin).Error; err != nil {
|
||||
Select("chick_in_date").
|
||||
Where("project_flock_kandang_id = ? AND chick_in_date IS NOT NULL", pfk.Id).
|
||||
Order("chick_in_date ASC").
|
||||
Limit(1).
|
||||
Scan(&firstChickin).Error; err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
start := pfk.CreatedAt
|
||||
if minChickin != nil && !minChickin.IsZero() {
|
||||
start = *minChickin
|
||||
if !firstChickin.ChickInDate.IsZero() {
|
||||
start = firstChickin.ChickInDate
|
||||
}
|
||||
startDate := dateOnlyUTC(start)
|
||||
|
||||
@@ -596,26 +600,34 @@ func (s closingService) getSapronakDateRange(ctx context.Context, projectFlockID
|
||||
return &startDate, endDate, nil
|
||||
}
|
||||
|
||||
var minCreated time.Time
|
||||
var firstPFK entity.ProjectFlockKandang
|
||||
if err := db.Model(&entity.ProjectFlockKandang{}).
|
||||
Select("MIN(created_at)").
|
||||
Select("created_at").
|
||||
Where("project_flock_id = ?", projectFlockID).
|
||||
Scan(&minCreated).Error; err != nil {
|
||||
Order("created_at ASC").
|
||||
First(&firstPFK).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil, nil
|
||||
}
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
var minChickin *time.Time
|
||||
var firstChickin struct {
|
||||
ChickInDate time.Time `gorm:"column:chick_in_date"`
|
||||
}
|
||||
if err := db.Table("project_chickins pc").
|
||||
Select("MIN(pc.chick_in_date)").
|
||||
Select("pc.chick_in_date").
|
||||
Joins("JOIN project_flock_kandangs pfk ON pfk.id = pc.project_flock_kandang_id").
|
||||
Where("pfk.project_flock_id = ?", projectFlockID).
|
||||
Scan(&minChickin).Error; err != nil {
|
||||
Where("pfk.project_flock_id = ? AND pc.chick_in_date IS NOT NULL", projectFlockID).
|
||||
Order("pc.chick_in_date ASC").
|
||||
Limit(1).
|
||||
Scan(&firstChickin).Error; err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
start := minCreated
|
||||
if minChickin != nil && !minChickin.IsZero() {
|
||||
start = *minChickin
|
||||
start := firstPFK.CreatedAt
|
||||
if !firstChickin.ChickInDate.IsZero() {
|
||||
start = firstChickin.ChickInDate
|
||||
}
|
||||
startDate := dateOnlyUTC(start)
|
||||
|
||||
@@ -627,15 +639,19 @@ func (s closingService) getSapronakDateRange(ctx context.Context, projectFlockID
|
||||
return nil, nil, err
|
||||
}
|
||||
if openCount == 0 {
|
||||
var maxClosed *time.Time
|
||||
var latestClosed entity.ProjectFlockKandang
|
||||
if err := db.Model(&entity.ProjectFlockKandang{}).
|
||||
Select("MAX(closed_at)").
|
||||
Where("project_flock_id = ?", projectFlockID).
|
||||
Scan(&maxClosed).Error; err != nil {
|
||||
Select("closed_at").
|
||||
Where("project_flock_id = ? AND closed_at IS NOT NULL", projectFlockID).
|
||||
Order("closed_at DESC").
|
||||
First(&latestClosed).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return &startDate, nil, nil
|
||||
}
|
||||
return nil, nil, err
|
||||
}
|
||||
if maxClosed != nil && !maxClosed.IsZero() {
|
||||
d := dateOnlyUTC(*maxClosed)
|
||||
if latestClosed.ClosedAt != nil && !latestClosed.ClosedAt.IsZero() {
|
||||
d := dateOnlyUTC(*latestClosed.ClosedAt)
|
||||
endDate = &d
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user