This commit is contained in:
2026-09-25 16:18:53 +05:30
parent 7fdcc92528
commit 090e9c0c2f
3 changed files with 124 additions and 10 deletions

View File

@@ -36,21 +36,42 @@ func normaliseShiftTime(raw string) (string, error) {
return t, nil
}
// ListStaffShifts returns an outlet's shifts, newest last so a picker reads in
// the order they were created rather than alphabetically by name.
// ListStaffShifts returns the shifts a tenant's staff can be put on, newest
// last so a picker reads in the order they were created rather than
// alphabetically by name.
//
// ── Why the outlet is optional ──────────────────────────────────────────────
//
// A shift is a fact about how a BUSINESS runs, not about one shop: a tenant
// that works 07:00–15:00 and 15:00–23:00 works those hours at every branch it
// owns, and making somebody re-enter them per outlet guarantees the third
// branch gets 07:00–15:30 and nobody notices. `locationid = 0` is a shift that
// belongs to the whole tenant.
//
// Branch-specific rows are still honoured, because they already exist and a
// tenant may genuinely run one outlet differently. Asking for an outlet returns
// the tenant's shifts AND that outlet's own; asking for none returns everything
// the tenant has.
func (r *posRepository) ListStaffShifts(tenantID, locationID int, includeInactive bool) ([]models.StaffShifts, error) {
if tenantID <= 0 || locationID <= 0 {
return nil, fmt.Errorf("tenantid and locationid are required")
if tenantID <= 0 {
return nil, fmt.Errorf("tenantid is required")
}
shifts := make([]models.StaffShifts, 0)
query := `SELECT * FROM staffshifts WHERE tenantid = ? AND locationid = ?`
args := []any{tenantID}
query := `SELECT * FROM staffshifts WHERE tenantid = ?`
if locationID > 0 {
// The tenant-wide ones and this outlet's, never another outlet's.
query += ` AND (COALESCE(locationid, 0) = 0 OR locationid = ?)`
args = append(args, locationID)
}
if !includeInactive {
query += ` AND LOWER(COALESCE(status,'active')) <> 'inactive'`
}
query += ` ORDER BY staffshiftid`
if err := r.db.Raw(query, tenantID, locationID).Scan(&shifts).Error; err != nil {
if err := r.db.Raw(query, args...).Scan(&shifts).Error; err != nil {
return nil, err
}
return shifts, nil
@@ -58,8 +79,14 @@ func (r *posRepository) ListStaffShifts(tenantID, locationID int, includeInactiv
// CreateStaffShift adds a window at one outlet.
func (r *posRepository) CreateStaffShift(tenantID, locationID int, req models.StaffShifts) (*models.StaffShifts, error) {
if tenantID <= 0 || locationID <= 0 {
return nil, fmt.Errorf("tenantid and locationid are required")
if tenantID <= 0 {
return nil, fmt.Errorf("tenantid is required")
}
// `locationid = 0` is deliberate and is now the ordinary case: the shift
// belongs to the tenant and every branch it owns can use it. An outlet is
// only named when one shop really does run different hours.
if locationID < 0 {
locationID = 0
}
name := strings.TrimSpace(req.Name)