Élimination de form->check() pour version 1.3

This commit is contained in:
Jean-Christophe Engel 2023-10-19 10:24:49 +02:00
parent 0ed03eb362
commit 2454883ffc
4 changed files with 150 additions and 209 deletions

View File

@ -49,140 +49,108 @@ if ( !$target ) {
} }
// Traitement // Traitement
$data=[];
$form->runIf(f('save') && !$form->hasErrors(),
function () use ($client, &$data)
{
try
{
if ( count(f('designation')) !== count(f('prix')) )
{
throw new UserException('Nombre de désignations et de prix reçus différent.');
}
if (f('save')) $data = [
{ 'numero' => f('numero_facture'),
$str_moyen = 'moyen_paiement'. (f('type') == CERFA ? '_cerfa':''); 'date_emission' => f('date_emission'),
'date_echeance' => f('date_echeance'),
'reglee' => f('reglee') == 1?1:0,
'archivee' => f('archivee') == 1?1:0,
'moyen_paiement' => f('moyen_paiement'),
'toto' => 0
];
$data['type_facture'] = f('type');
if (in_array(f('type'), [DEVIS, FACT]))
{
foreach(f('designation') as $k=>$value)
{
$data['contenu'][$k]['designation'] = $value;
$data['contenu'][$k]['prix'] = Utils::moneyToInteger(f('prix')[$k]);
$data['toto'] += Utils::moneyToInteger(f('prix')[$k]);
}
$data['total'] = $data['toto'];
unset($data['toto']);
}
elseif ( f('type') == CERFA )
{
$data['moyen_paiement'] = f('moyen_paiement_cerfa');
$data['contenu'] = [
'forme' => f('forme_don'),
'nature' => f('nature_don'),
'texte' => f('texte_don')];
$data['total'] = Utils::moneyToInteger(f('total'));
unset($data['toto']);
}
if (f('base_receveur') == 'client')
{
$data['receveur_membre'] = 0;
$data['receveur_id'] = f('client');
}
elseif (f('base_receveur') == 'membre')
{
$data['receveur_membre'] = 1;
$data['receveur_id'] = f('membre');
}
$form->check($csrf_key, [ }
'type' => 'required|in:'.implode(',', [DEVIS, FACT, CERFA]), catch(UserException $e)
'numero_facture' => $require_number ? 'required|string' : 'string', {
'date_emission' => 'required|date_format:d/m/Y', $form->addError($e->getMessage());
'date_echeance' => 'required|date_format:d/m/Y', }
// 'reglee' => '',
// 'archivee' => '',
'base_receveur' => 'required|in:membre,client',
// 'client' => '',
// 'membre' => '',
$str_moyen => 'required|in:' . implode(',', array_keys($moyens_paiement)),
'designation' => 'array|required',
'prix' => 'array|required'
]);
if (!$form->hasErrors()) }, $csrf_key);
{
try
{
if ( count(f('designation')) !== count(f('prix')) )
{
throw new UserException('Nombre de désignations et de prix reçus différent.');
}
$data = [ $form->runIf(f('select_cotis') && !$form->hasErrors(),
'numero' => f('numero_facture'), function () use ($step)
'date_emission' => f('date_emission'), {
'date_echeance' => f('date_echeance'), $step = true;
'reglee' => f('reglee') == 1?1:0, }, 'add_cotis_1');
'archivee' => f('archivee') == 1?1:0,
'moyen_paiement' => f('moyen_paiement'),
'toto' => 0
];
$data['type_facture'] = f('type');
if (in_array(f('type'), [DEVIS, FACT]))
{
foreach(f('designation') as $k=>$value)
{
$data['contenu'][$k]['designation'] = $value;
$data['contenu'][$k]['prix'] = Utils::moneyToInteger(f('prix')[$k]);
$data['toto'] += Utils::moneyToInteger(f('prix')[$k]);
}
$data['total'] = $data['toto'];
unset($data['toto']);
}
elseif ( f('type') == CERFA )
{
$data['moyen_paiement'] = f('moyen_paiement_cerfa');
$data['contenu'] = [
'forme' => f('forme_don'),
'nature' => f('nature_don'),
'texte' => f('texte_don')];
$data['total'] = Utils::moneyToInteger(f('total'));
unset($data['toto']);
}
if (f('base_receveur') == 'client')
{
$data['receveur_membre'] = 0;
$data['receveur_id'] = f('client');
}
elseif (f('base_receveur') == 'membre')
{
$data['receveur_membre'] = 1;
$data['receveur_id'] = f('membre');
}
$form->runIf(f('add_cotis') && !$form->hasErrors(),
function () use ($radio, $fields, $facture)
{
$radio['type'] = f('cotisation');
try
{
$num = (int) str_replace('cotis_', '', $radio['type']);
foreach($fields as $field)
{
$cotis[$field] = f($field.'_'.$num);
}
} $r = $facture->getCotis(f('membre_cotis'), $cotis['id']);
catch(UserException $e) $r = $r[0];
{
$form->addError($e->getMessage());
}
}
}
elseif (f('select_cotis'))
{
$form->check('add_cotis_1',[
'numero_facture' => $require_number ? 'required|string' : 'string',
'date_emission' => 'required|date_format:d/m/Y',
'membre_cotis' => 'required|numeric',
]);
$step = true; $data = [
} 'type_facture' => COTIS,
elseif (f('add_cotis')) 'numero' => f('numero_facture'),
{ 'receveur_membre' => 1,
$form->check('add_cotis_2',[ 'receveur_id' => f('membre_cotis'),
'numero_facture' => $require_number ? 'required|string' : 'string', 'date_emission' => f('date_emission'),
'date_emission' => 'required|date_format:d/m/Y', 'moyen_paiement' => 'AU',
'membre_cotis' => 'required|numeric', 'total' => $r->paid_amount ?? $r->amount,
'cotisation' => 'required', 'contenu' => ['id' => $cotis['id'],
]); 'intitule' => $cotis['label'],
'souscription' => $cotis['date'],
'expiration' => $cotis['expiry'] ]
];
$radio['type'] = f('cotisation'); }
catch (UserException $e)
if (!$form->hasErrors()) {
{ $form->addError($e->getMessage());
try }
{ }, 'add_cotis_2');
$num = (int) str_replace('cotis_', '', $radio['type']);
foreach($fields as $field)
{
$cotis[$field] = f($field.'_'.$num);
}
$r = $facture->getCotis(f('membre_cotis'), $cotis['id']);
$r = $r[0];
$data = [
'type_facture' => COTIS,
'numero' => f('numero_facture'),
'receveur_membre' => 1,
'receveur_id' => f('membre_cotis'),
'date_emission' => f('date_emission'),
'moyen_paiement' => 'AU',
'total' => $r->paid_amount ?? $r->amount,
'contenu' => ['id' => $cotis['id'],
'intitule' => $cotis['label'],
'souscription' => $cotis['date'],
'expiration' => $cotis['expiry'] ]
];
}
catch (UserException $e)
{
$form->addError($e->getMessage());
}
}
}
if ($step) if ($step)
{ {
@ -195,7 +163,7 @@ if ($step)
$form->addError($e->getMessage()); $form->addError($e->getMessage());
} }
} }
elseif (isset($data)) elseif (count($data) > 0)
{ {
if ($target) if ($target)
{ {

View File

@ -16,38 +16,27 @@ if (!$c)
throw new UserException("Ce client n'existe pas."); throw new UserException("Ce client n'existe pas.");
} }
if(f('save')) $form->runIf(f('save') && !$form->hasErrors(),
{ function () use ($client, $id)
$form->check('edit_client', [ {
'nom' => 'required|string', try
'adresse' => 'required|string', {
'code_postal' => 'required|string', $r = $client->edit($id,[
'ville' => 'required|string', 'nom' => f('nom'),
'telephone' => 'string', 'adresse' => f('adresse'),
'email' => 'email' 'code_postal' => f('code_postal'),
]); 'ville' => f('ville'),
'telephone' => f('telephone'),
'email' => f('email')
]);
if (!$form->hasErrors()) $r ? Utils::redirect(PLUGIN_ADMIN_URL . 'client.php?id='.(int)$id):'';
{ }
try catch (UserException $e)
{ {
$r = $client->edit($id,[ $form->addError($e->getMessage());
'nom' => f('nom'), }
'adresse' => f('adresse'), }, 'edit_client');
'code_postal' => f('code_postal'),
'ville' => f('ville'),
'telephone' => f('telephone'),
'email' => f('email')
]);
$r ? Utils::redirect(PLUGIN_ADMIN_URL . 'client.php?id='.(int)$id):'';
}
catch (UserException $e)
{
$form->addError($e->getMessage());
}
}
}
$tpl->assign('client', $c); $tpl->assign('client', $c);

View File

@ -6,39 +6,27 @@ require_once __DIR__ . '/_inc.php';
$session->requireAccess($session::SECTION_ACCOUNTING, $session::ACCESS_READ); $session->requireAccess($session::SECTION_ACCOUNTING, $session::ACCESS_READ);
if(f('add')) $form->runIf(f('add') && !$form->hasErrors(),
{ function () use ($client)
$form->check('add_client', [ {
'nom' => 'required|string', try
'adresse' => 'required|string', {
'code_postal' => 'required|string', $id = $client->add([
'ville' => 'required|string', 'nom' => f('nom'),
'telephone' => 'string', 'adresse' => f('adresse'),
'email' => 'email' 'code_postal' => f('code_postal'),
]); 'ville' => f('ville'),
'telephone' => f('telephone'),
if (!$form->hasErrors()) 'email' => f('email')
{ ]);
try
{
$id = $client->add([
'nom' => f('nom'),
'adresse' => f('adresse'),
'code_postal' => f('code_postal'),
'ville' => f('ville'),
'telephone' => f('telephone'),
'email' => f('email')
]);
$id ? Utils::redirect(PLUGIN_ADMIN_URL . 'client.php?id='.(int)$id):'';
}
catch (UserException $e)
{
$form->addError($e->getMessage());
}
}
}
$id ? Utils::redirect(PLUGIN_ADMIN_URL . 'client.php?id='.(int)$id):'';
}
catch (UserException $e)
{
$form->addError($e->getMessage());
}
}, 'add_client');
$list = $client->list(); $list = $client->list();
$list->loadFromQueryString(); $list->loadFromQueryString();

View File

@ -17,22 +17,18 @@ if (!$client)
throw new UserException("Ce document n'existe pas."); throw new UserException("Ce document n'existe pas.");
} }
if (f('delete')) $form->runIf(f('delete') && !$form->hasErrors(),
{ function () use ($facture, $f)
$form->check('delete_doc_'.$f->id); {
try {
if (!$form->hasErrors()) $facture->delete($f->id);
{ Utils::redirect(PLUGIN_ADMIN_URL . 'index.php');
try { }
$facture->delete($f->id); catch (UserException $e)
Utils::redirect(PLUGIN_ADMIN_URL . 'index.php'); {
} $form->addError($e->getMessage());
catch (UserException $e) }
{ });
$form->addError($e->getMessage());
}
}
}
$tpl->assign('doc', $f); $tpl->assign('doc', $f);
$tpl->display(PLUGIN_ROOT . '/templates/facture_supprimer.tpl'); $tpl->display(PLUGIN_ROOT . '/templates/facture_supprimer.tpl');