Refactor Slack notifications and enhance departmental progress calculation
- Replace `send_webhook_message` with `send_message` for improved Slack notification rendering using `goals_progress_alert_renderer`. - Add methods for calculating and retrieving departmental progress and distribution in `goals_criteria`. - Update `renderDanishPeriodSummary` and Slack cron logic to support departmental-specific summaries. - Include departmental progress in serialized goal objects for better reporting.
This commit is contained in:
@@ -369,6 +369,15 @@ class goals_criteria implements goals_criteria_i
|
||||
return $criteria->getProgress();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function calculateDepartmentalProgressFromArray(array $criteria_array): array
|
||||
{
|
||||
$criteria = self::fromJson(json_encode($criteria_array));
|
||||
return $criteria->getDepartmentalProgress();
|
||||
}
|
||||
|
||||
/**
|
||||
* Export the criteria as an associative array suitable for JSON encoding.
|
||||
* Ensures a canonical schema matching fromJson expectations.
|
||||
@@ -608,4 +617,28 @@ class goals_criteria implements goals_criteria_i
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function getDepartmentalProgress(): array
|
||||
{
|
||||
$results = [];
|
||||
$department_ids = $this->departments->listIDs();
|
||||
foreach ($department_ids as $dept_id) {
|
||||
$dept_criteria = clone $this;
|
||||
$dept_criteria->departments->set([(new \objects\departments_o())->select($dept_id)]);
|
||||
$results[$dept_id] = $dept_criteria->getProgress();
|
||||
}
|
||||
// reset the departments to original
|
||||
$this->departments->set(array_map(fn($id) => (new \objects\departments_o())->select($id), $department_ids));
|
||||
return $results;
|
||||
}
|
||||
|
||||
public function withDepartmentFilter(int $department_id): goals_criteria
|
||||
{
|
||||
$new_criteria = clone $this;
|
||||
$new_criteria->departments->set([(new \objects\departments_o())->select($department_id)]);
|
||||
return $new_criteria;
|
||||
}
|
||||
}
|
||||
@@ -9,10 +9,11 @@ use goals\formats\sms_progress_alert_format;
|
||||
use goals\helpers\goals_criteria_progress_alert_destination as Dest;
|
||||
use goals\helpers\goals_criteria_progress_alert_progress_type as PType;
|
||||
use goals\helpers\goals_criteria_progress_alert_style as Style;
|
||||
use objects\departments_o;
|
||||
|
||||
class goals_progress_alert_renderer
|
||||
{
|
||||
public static function render(goals_criteria $criteria): string
|
||||
public static function render(goals_criteria $criteria, ?departments_o $department = null): string
|
||||
{
|
||||
$label = (string)($criteria->label ?? 'Goal');
|
||||
$count = (int)self::getProgress($criteria);
|
||||
@@ -47,18 +48,18 @@ class goals_progress_alert_renderer
|
||||
|
||||
$body = match ($criteria->progress_alert_progress_type) {
|
||||
// New DK multi-line formats for COUNT_ONLY and ALL
|
||||
PType::COUNT_ONLY => self::renderDanishPeriodSummary($criteria, includeTargets: false,
|
||||
PType::COUNT_ONLY => self::renderDanishPeriodSummary($criteria, includeTargets: true,
|
||||
header: [
|
||||
$deptNames !== '' ? "$deptLabel: $deptNames" : null,
|
||||
$customerNames !== '' ? "$customerLabel: $customerNames" : null,
|
||||
$productNames !== '' ? "$productLabel: $productNames" : null,
|
||||
]),
|
||||
], department: $department),
|
||||
PType::ALL => self::renderDanishPeriodSummary($criteria, includeTargets: $target > 0,
|
||||
header: [
|
||||
$deptNames !== '' ? "$deptLabel: $deptNames" : null,
|
||||
$customerNames !== '' ? "$customerLabel: $customerNames" : null,
|
||||
$productNames !== '' ? "$productLabel: $productNames" : null,
|
||||
]),
|
||||
], department: $department),
|
||||
// Legacy single-line fallbacks
|
||||
PType::PERCENTAGE_ONLY => sprintf('Progress: %s%%', number_format($percent, 2)),
|
||||
PType::COUNT_AND_TARGET => sprintf('Progress: %d / %d', $count, $target),
|
||||
@@ -123,8 +124,25 @@ class goals_progress_alert_renderer
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static function renderDanishPeriodSummary(goals_criteria $criteria, bool $includeTargets, array $header = []): string
|
||||
private static function renderDanishPeriodSummary(goals_criteria $criteria, bool $includeTargets, array $header = [], ?departments_o $department = null): string
|
||||
{
|
||||
/**
|
||||
* > Afdelinger: Taulov, Taastrup
|
||||
* > Kunder: DHL FREIGHT (DENMARK ONLY) A/S, DHL FREIGHT (Trailer Danmark NTP), DHL FREIGHT (Trailere Finland NTP), DHL FREIGHT (Trailere Sverige NTP)
|
||||
* > Produkt: Trailer
|
||||
* > Igår: 4
|
||||
* > Heraf:
|
||||
* > - Taastrup: 2
|
||||
* > - Taulov: 2
|
||||
* > Ugen total: 9
|
||||
* > Heraf:
|
||||
* > - Taastrup: 7
|
||||
* > - Taulov: 2
|
||||
* > Måneden total: 15
|
||||
* > Heraf:
|
||||
* > - Taastrup: 10
|
||||
* > - Taulov: 5
|
||||
*/
|
||||
// Build period windows
|
||||
$now = new \DateTimeImmutable('now');
|
||||
$yesterdayStart = $now->modify('-1 day')->setTime(0, 0, 0);
|
||||
|
||||
Reference in New Issue
Block a user