<?php use App\Core\Csrf;$errors=$_SESSION['form_errors']??[];unset($_SESSION['form_errors']);$groups=[];foreach($items as $item)$groups[$item['client_id']][]=$item; ?>
<link rel="stylesheet" href="/public/assets/css/cda-accounting-ui-972.css?v=9.6.72">
<main class="accounting-page">
<section class="cda-hero accounting-hero"><div><span class="cda-eyebrow">AXIOM Studio · Automatisation</span><h2>Inscriptions à facturer</h2><p>Synchronisez les inscriptions validées, contrôlez les lignes puis complétez le brouillon unique de chaque adhérent payeur.</p></div><div class="cda-actionbar billing-main-actions"><a class="cda-btn cda-btn-secondary billing-action-large" href="/public/facturation">← Retour à la facturation</a><form method="post" action="/public/facturation/a-preparer/synchroniser"><?=Csrf::field()?><button class="cda-btn cda-btn-primary billing-action-large">↻ Synchroniser les inscriptions</button></form></div></section>
<?php if($errors):?><div class="accounting-message is-error"><?php foreach($errors as $error):?><p><?=htmlspecialchars($error)?></p><?php endforeach?></div><?php endif?><?php if(!empty($_SESSION['success'])):?><div class="accounting-message is-success"><?=htmlspecialchars($_SESSION['success'])?></div><?php unset($_SESSION['success']);endif?>
<section class="accounting-kpis"><article class="accounting-kpi"><span class="accounting-kpi-icon">▤</span><div><small>Lignes à préparer</small><strong><?=count($items)?></strong></div></article><article class="accounting-kpi"><span class="accounting-kpi-icon">◎</span><div><small>Adhérents payeurs</small><strong><?=count($groups)?></strong></div></article><article class="accounting-kpi"><span class="accounting-kpi-icon">€</span><div><small>Total net proposé</small><strong><?=number_format(array_sum(array_map(static fn(array $i):float=>(float)$i['net_amount'],$items)),2,',',' ')?> €</strong></div></article><article class="accounting-kpi"><span class="accounting-kpi-icon">!</span><div><small>Lignes à compléter</small><strong><?=count(array_filter($items,static fn(array $i):bool=>(float)$i['gross_amount']<=0||empty($i['assignment_id'])))?></strong></div></article></section>
<form method="post" action="/public/facturation/a-preparer/generer"><?=Csrf::field()?>
<?php if(!$groups):?><section class="cda-panel accounting-empty"><span>✓</span><h3>Aucun élément à facturer</h3><p>Lancez une synchronisation après avoir validé de nouvelles inscriptions.</p></section><?php endif?>
<?php foreach($groups as $clientId=>$rows):?><section class="cda-panel accounting-panel billing-prepared-group"><div class="cda-panel-head"><div><span class="cda-eyebrow">Compte <?=htmlspecialchars($rows[0]['code_client'])?></span><h3><?=htmlspecialchars($rows[0]['prenom'].' '.$rows[0]['nom'])?></h3></div><label class="select-client"><input type="checkbox" data-select-client="<?=$clientId?>"> Sélectionner le dossier</label></div><div class="accounting-table-wrap"><table class="accounting-table prepared-table"><thead><tr><th></th><th>Élément</th><th>Pôle / compte</th><th>Période</th><th>Brut</th><th>Réduction</th><th>Net proposé</th><th>État</th><th>Action</th></tr></thead><tbody><?php foreach($rows as $item):$ready=(float)$item['gross_amount']>0&&!empty($item['assignment_id']);?><tr class="<?=$ready?'':'is-incomplete'?>"><td><input type="checkbox" name="item_ids[]" value="<?=$item['id']?>" data-client-item="<?=$clientId?>"></td><td><strong><?=htmlspecialchars($item['libelle'])?></strong><small class="accounting-subline"><?=htmlspecialchars($item['type_element'])?> · <?=htmlspecialchars($item['school_year']??'')?></small></td><td><select name="assignment_id[<?=$item['id']?>]" required><option value="">À choisir</option><?php foreach($assignments as $assignment):?><option value="<?=$assignment['id']?>" <?=(int)$item['assignment_id']===(int)$assignment['id']?'selected':''?>><?=htmlspecialchars($assignment['label'].' · '.$assignment['revenue_account'])?></option><?php endforeach?></select></td><td><div class="period-fields"><input type="date" name="service_start[<?=$item['id']?>]" value="<?=htmlspecialchars($item['service_start']??'')?>"><input type="date" name="service_end[<?=$item['id']?>]" value="<?=htmlspecialchars($item['service_end']??'')?>"></div></td><td><input class="money-input" type="number" name="gross_amount[<?=$item['id']?>]" min="0" step="0.01" value="<?=htmlspecialchars((string)($item['gross_amount']??0))?>"></td><td><input class="money-input" type="number" name="discount_amount[<?=$item['id']?>]" min="0" step="0.01" value="<?=htmlspecialchars((string)($item['discount_amount']??0))?>"><?php if(!empty($item['auto_discount_rule_id'])):?><small class="auto-discount">Répartie automatiquement</small><?php endif?></td><td><strong><?=number_format((float)$item['net_amount'],2,',',' ')?> €</strong></td><td><span class="studio-badge <?=$ready?'':'needs-data'?>"><?=$ready?'Prête':'À compléter'?></span></td><td><?php if(\App\Core\Permission::can('facturation.archive')):?><button class="cda-btn cda-btn-danger prepared-delete" type="submit" name="id" value="<?=$item['id']?>" formaction="/public/facturation/a-preparer/supprimer" formmethod="post" formnovalidate onclick="return confirm('Supprimer cet élément et annuler la demande correspondante dans le module Famille ?');">Supprimer</button><?php endif?></td></tr><?php endforeach?></tbody><tfoot><tr><th colspan="6">Total du dossier</th><th><?=number_format(array_sum(array_map(static fn(array $i):float=>(float)$i['net_amount'],$rows)),2,',',' ')?> €</th><th colspan="2"></th></tr></tfoot></table></div></section><?php endforeach?>
<?php if($groups):?><div class="prepared-action-bar"><div><strong><span data-prepared-count>0</span> ligne(s) sélectionnée(s)</strong><small>Les lignes seront ajoutées au brouillon existant de l’adhérent, ou créeront son premier brouillon.</small></div><button class="cda-btn accounting-btn-primary" onclick="return confirm('Regrouper les lignes sélectionnées dans les brouillons des adhérents ?')">Regrouper dans les brouillons</button></div><?php endif?>
</form></main><script>document.addEventListener('DOMContentLoaded',()=>{const items=[...document.querySelectorAll('[data-client-item]')],count=document.querySelector('[data-prepared-count]');const refresh=()=>{if(count)count.textContent=items.filter(x=>x.checked).length};document.querySelectorAll('[data-select-client]').forEach(master=>master.addEventListener('change',()=>{items.filter(x=>x.dataset.clientItem===master.dataset.selectClient).forEach(x=>x.checked=master.checked);refresh()}));items.forEach(x=>x.addEventListener('change',refresh));refresh()});</script>
