| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
#[AllowDynamicProperties] |
| 3 |
class SH4_Schedule_Html_View_Download |
| 4 |
{ |
| 5 |
public function __construct( |
| 6 |
HC3_Hooks $hooks, |
| 7 |
|
| 8 |
HC3_Ui $ui, |
| 9 |
HC3_Request $request, |
| 10 |
HC3_Time $t, |
| 11 |
HC3_Translate $translate, |
| 12 |
HC3_Settings $settings, |
| 13 |
|
| 14 |
SH4_Shifts_Presenter $shiftsPresenter, |
| 15 |
|
| 16 |
SH4_Shifts_Query $shiftsQuery, |
| 17 |
SH4_Calendars_Presenter $calendarsPresenter, |
| 18 |
SH4_Shifts_View_Widget $widget, |
| 19 |
SH4_Schedule_Html_View_Common $common |
| 20 |
) |
| 21 |
{ |
| 22 |
$this->ui = $ui; |
| 23 |
$this->t = $t; |
| 24 |
$this->request = $request; |
| 25 |
$this->translate = $translate; |
| 26 |
|
| 27 |
$this->calendarsPresenter = $hooks->wrap( $calendarsPresenter ); |
| 28 |
$this->shiftsPresenter = $hooks->wrap( $shiftsPresenter ); |
| 29 |
$this->shiftsQuery = $hooks->wrap( $shiftsQuery ); |
| 30 |
|
| 31 |
$this->widget = $hooks->wrap( $widget ); |
| 32 |
$this->common = $hooks->wrap( $common ); |
| 33 |
$this->settings = $hooks->wrap($settings); |
| 34 |
|
| 35 |
$this->self = $hooks->wrap( $this ); |
| 36 |
} |
| 37 |
|
| 38 |
private function report() |
| 39 |
{ |
| 40 |
$params = $this->request->getParams(); |
| 41 |
$startDate = $params['start']; |
| 42 |
|
| 43 |
$session = HC3_Session::instance(); |
| 44 |
$separator = $session->getUserdata('pref_csv_separator'); |
| 45 |
if (!$separator) { |
| 46 |
$separator = $this->settings->get('pref_csv_separator'); |
| 47 |
} |
| 48 |
if (!$separator) { |
| 49 |
$separator = ','; |
| 50 |
} |
| 51 |
|
| 52 |
$groupby = $session->getUserdata('scheduleViewGroupby'); |
| 53 |
if (!$groupby) { |
| 54 |
$groupby = $this->settings->get('default_schedule_groupby'); |
| 55 |
} |
| 56 |
if (!$groupby) { |
| 57 |
$groupby = 'employee'; |
| 58 |
} |
| 59 |
|
| 60 |
$shifts = $this->common->getShifts(); |
| 61 |
|
| 62 |
$dt1 = $this->shiftsQuery->getStart(); |
| 63 |
$dt2 = $this->shiftsQuery->getEnd(); |
| 64 |
// $d1 = $this->t->setDateTimeDb($dt1)->formatDateDb(); |
| 65 |
// $d2 = $this->t->setDateTimeDb($dt2)->formatDateDb(); |
| 66 |
|
| 67 |
$d1 = $this->t->setDateTimeDb($dt1)->formatDate(); |
| 68 |
$d1 = $this->translate->translate($d1); |
| 69 |
$d2 = $this->t->setDateTimeDb($dt2)->formatDate(); |
| 70 |
$d2 = $this->translate->translate($d2); |
| 71 |
|
| 72 |
$allEmployees = $this->common->findAllEmployees(); |
| 73 |
$allCalendars = $this->common->findAllCalendars(); |
| 74 |
|
| 75 |
$body = []; |
| 76 |
foreach ($shifts as $shift) { |
| 77 |
$shiftOut = $this->shiftsPresenter->export($shift); |
| 78 |
|
| 79 |
if ('employee' == $groupby) { |
| 80 |
$key = $shiftOut['employee_id']; |
| 81 |
} elseif ('calendar' == $groupby) { |
| 82 |
$key = $shiftOut['calendar_id']; |
| 83 |
} else { |
| 84 |
$key = 0; |
| 85 |
} |
| 86 |
|
| 87 |
if (!isset($body[$key])) { |
| 88 |
if ('employee' == $groupby) { |
| 89 |
$body[$key]['employee_id'] = $shiftOut['employee_id']; |
| 90 |
$body[$key]['employee'] = $shiftOut['employee']; |
| 91 |
} elseif ('calendar' == $groupby) { |
| 92 |
$body[$key]['calendar_id'] = $shiftOut['calendar_id']; |
| 93 |
$body[$key]['calendar'] = $shiftOut['calendar']; |
| 94 |
} else { |
| 95 |
$key = 0; |
| 96 |
} |
| 97 |
|
| 98 |
$body[$key]['duration'] = 0; |
| 99 |
$body[$key]['qty'] = 0; |
| 100 |
if (array_key_exists('salary', $shiftOut)) { |
| 101 |
$body[$key]['salary'] = 0; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
$body[$key]['qty'] += 1; |
| 106 |
$body[$key]['duration'] += $shiftOut['duration']; |
| 107 |
if (isset($shiftOut['salary'])) { |
| 108 |
$body[$key]['salary'] += $shiftOut['salary']; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
$out = array(); |
| 113 |
foreach ($body as $line) { |
| 114 |
$keys = array_keys($line); |
| 115 |
|
| 116 |
reset($keys); |
| 117 |
foreach ($keys as $k) { |
| 118 |
if (isset($line[$k])) { |
| 119 |
$line[$k] = $this->translate->translate($line[$k]); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
$out[] = HC3_Functions::buildCsv($line, $separator); |
| 124 |
} |
| 125 |
|
| 126 |
if ($out) { |
| 127 |
$header = array_keys(current($body)); |
| 128 |
$header = array_combine($header, $header); |
| 129 |
$header = HC3_Functions::buildCsv($header, $separator); |
| 130 |
array_unshift($out, $header); |
| 131 |
} |
| 132 |
|
| 133 |
$out = join("\n", $out); |
| 134 |
|
| 135 |
$fileName = 'report-' . $d1 . '-' . $d2; |
| 136 |
// $fileName .= '-' . date('Y-m-d_H-i') . '.csv'; |
| 137 |
$fileName .= '--' . date('YmdHi') . '.csv'; |
| 138 |
// echo $fileName; |
| 139 |
// exit; |
| 140 |
|
| 141 |
HC3_Functions::pushDownload( $fileName, $out ); |
| 142 |
exit; |
| 143 |
} |
| 144 |
|
| 145 |
public function render() |
| 146 |
{ |
| 147 |
$params = $this->request->getParams(); |
| 148 |
if (isset($params['report']) && $params['report']) { |
| 149 |
return $this->report(); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
$startDate = $params['start']; |
| 154 |
|
| 155 |
$shifts = $this->common->getShifts(); |
| 156 |
|
| 157 |
$iknow = array(); |
| 158 |
$hori = TRUE; |
| 159 |
|
| 160 |
$allEmployees = $this->common->findAllEmployees(); |
| 161 |
$allCalendars = $this->common->findAllCalendars(); |
| 162 |
|
| 163 |
$session = HC3_Session::instance(); |
| 164 |
$separator = $session->getUserdata( 'pref_csv_separator' ); |
| 165 |
|
| 166 |
if( ! $separator ){ |
| 167 |
$separator = $this->settings->get( 'pref_csv_separator' ); |
| 168 |
} |
| 169 |
if( ! $separator ){ |
| 170 |
$separator = ','; |
| 171 |
} |
| 172 |
|
| 173 |
$shiftsOut = array(); |
| 174 |
$header = array(); |
| 175 |
|
| 176 |
foreach( $shifts as $shift ){ |
| 177 |
$thisOut = $this->shiftsPresenter->export( $shift ); |
| 178 |
|
| 179 |
$thisHeader = array_keys( $thisOut ); |
| 180 |
foreach( $thisHeader as $th ){ |
| 181 |
if( ! isset($header[$th]) ){ |
| 182 |
$header[$th] = $th; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
$shiftsOut[] = $thisOut; |
| 187 |
} |
| 188 |
|
| 189 |
$keys = array_keys( $header ); |
| 190 |
$out = array(); |
| 191 |
reset( $shiftsOut ); |
| 192 |
foreach( $shiftsOut as $shiftOut ){ |
| 193 |
$thisOut = array(); |
| 194 |
|
| 195 |
reset( $keys ); |
| 196 |
foreach( $keys as $k ){ |
| 197 |
if( isset($shiftOut[$k]) ){ |
| 198 |
$thisOut[$k] = $this->translate->translate( $shiftOut[$k] ); |
| 199 |
} |
| 200 |
else { |
| 201 |
$thisOut[$k] = NULL; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
$thisOut = HC3_Functions::buildCsv( $thisOut, $separator ); |
| 206 |
$out[] = $thisOut; |
| 207 |
} |
| 208 |
|
| 209 |
if( $out ){ |
| 210 |
$header = HC3_Functions::buildCsv( $header, $separator ); |
| 211 |
$header = array_unshift( $out, $header ); |
| 212 |
} |
| 213 |
|
| 214 |
$out = join("\n", $out); |
| 215 |
|
| 216 |
$fileName = 'export'; |
| 217 |
$fileName .= '-' . date('Y-m-d_H-i') . '.csv'; |
| 218 |
|
| 219 |
HC3_Functions::pushDownload( $fileName, $out ); |
| 220 |
exit; |
| 221 |
} |
| 222 |
} |