| Server IP : 188.114.96.3 / Your IP : 216.73.216.143 Web Server : Apache System : Linux sv058.home.net.pl 6.8.2 husak_dirty_pages_fix #1 SMP PREEMPT_DYNAMIC Tue Apr 2 22:06:51 CEST 2024 x86_64 User : ( 3194769) PHP Version : 8.5.12 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /polana/admin/ |
Upload File : |
<?php
declare(strict_types=1);
require_once dirname(__DIR__).'/includes/functions.php';
$user=require_tips_manager();ensure_schedule_schema();ensure_tips_schema();start_session();
$fallback='all_day';$preview=$_SESSION['tips_import_preview']??null;
if($_SERVER['REQUEST_METHOD']==='POST'){
verify_csrf();$action=(string)($_POST['action']??'upload');
try{
if($action==='upload'){
$file=$_FILES['import_file']??null;if(!$file||(int)($file['error']??UPLOAD_ERR_NO_FILE)!==UPLOAD_ERR_OK)throw new RuntimeException('Wybierz plik XLSX lub CSV.');if((int)($file['size']??0)>10*1024*1024)throw new RuntimeException('Plik jest za duży.');
$name=(string)($file['name']??'import.xlsx');$rows=tips_import_parse((string)$file['tmp_name'],$name,$fallback);if(!$rows)throw new RuntimeException('Nie znaleziono żadnych danych.');
$_SESSION['tips_import_preview']=['name'=>$name,'rows'=>$rows,'created'=>time()];$preview=$_SESSION['tips_import_preview'];
}elseif($action==='confirm'){
if(!$preview||!is_array($preview['rows']??null))throw new RuntimeException('Podgląd importu wygasł. Wgraj plik ponownie.');
$ok=0;$errors=[];
foreach($preview['rows'] as $row){
if(!empty($row['errors'])){$errors[]='Wiersz '.$row['row'].': '.implode(', ',$row['errors']);continue;}
try{
$ids=schedule_user_ids_for_date((string)$row['date']);
$existing=tip_pool_by_date((string)$row['date'],(string)$row['shift_code']);
tip_pool_save((string)$row['date'],(float)$row['cash'],(float)$row['card'],(float)$row['other'],(string)$row['note'],$ids,(int)$user['id'],$existing?'Import z pliku '.(string)$preview['name']:'',(string)$row['shift_code'],['type'=>'import','original_name'=>(string)$preview['name']]);$ok++;
}catch(Throwable $e){$errors[]='Wiersz '.$row['row'].': '.$e->getMessage();}
}
unset($_SESSION['tips_import_preview']);flash('success','Zaimportowano rozliczeń: '.$ok.'.'.($errors?' Błędy: '.count($errors):''));if($errors)$_SESSION['tips_import_errors']=$errors;redirect('/admin/tips-import.php');
}elseif($action==='cancel'){unset($_SESSION['tips_import_preview']);redirect('/admin/tips-import.php');}
}catch(Throwable $e){flash('error',$e->getMessage());}
}
$importErrors=$_SESSION['tips_import_errors']??[];unset($_SESSION['tips_import_errors']);
render_admin_header('Import napiwków'); ?>
<div class="admin-title-row"><div><h1>Import Excel / CSV</h1><p>Wiersz = jeden dzień. Obsada jest pobierana z grafiku.</p></div><div class="actions"><a class="btn btn-light" href="/admin/tips-template.php">Pobierz szablon CSV</a><a class="btn btn-light" href="/admin/tips.php">← Napiwki</a></div></div>
<?php if($importErrors): ?><div class="form-card"><h3>Błędy importu</h3><ul><?php foreach($importErrors as $e): ?><li><?= e($e) ?></li><?php endforeach; ?></ul></div><?php endif; ?>
<?php if(!$preview): ?><form class="form-card form-grid" method="post" enctype="multipart/form-data"><?= csrf_field() ?><input type="hidden" name="action" value="upload"><label class="full">Plik<input type="file" name="import_file" accept=".xlsx,.csv,text/csv,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" required></label><div class="full muted">Kolumny: Data, Gotówka, Karta, Inne, Notatka. Starsza kolumna „Zmiana” może zostać w pliku – będzie ignorowana.</div><div class="full"><button class="btn btn-primary">Wczytaj i pokaż podgląd</button></div></form>
<?php else: ?><section class="form-card"><div class="admin-section-title"><h2>Podgląd · <?= e($preview['name']) ?></h2></div><div class="table-wrap"><table class="admin-table"><thead><tr><th>Wiersz</th><th>Data</th><th>Gotówka</th><th>Karta</th><th>Inne</th><th>Obsada</th><th>Status</th></tr></thead><tbody><?php foreach($preview['rows'] as $r): ?><tr><td><?= (int)$r['row'] ?></td><td><?= e((string)$r['date']) ?></td><td><?= e(tip_money((float)$r['cash'])) ?></td><td><?= e(tip_money((float)$r['card'])) ?></td><td><?= e(tip_money((float)$r['other'])) ?></td><td><?= (int)$r['staff_count'] ?></td><td><?= $r['errors']?'<span class="tip-status is-open">'.e(implode(', ',$r['errors'])).'</span>':'<span class="tip-status is-settled">OK</span>' ?></td></tr><?php endforeach; ?></tbody></table></div><div class="actions" style="margin-top:16px"><form method="post"><?= csrf_field() ?><input type="hidden" name="action" value="confirm"><button class="btn btn-green">Importuj poprawne wiersze</button></form><form method="post"><?= csrf_field() ?><input type="hidden" name="action" value="cancel"><button class="btn btn-light">Anuluj</button></form></div></section><?php endif; ?>
<?php render_admin_footer(); ?>