É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
$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'))
{
$str_moyen = 'moyen_paiement'. (f('type') == CERFA ? '_cerfa':'');
$data = [
'numero' => f('numero_facture'),
'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]),
'numero_facture' => $require_number ? 'required|string' : 'string',
'date_emission' => 'required|date_format:d/m/Y',
'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'
]);
}
catch(UserException $e)
{
$form->addError($e->getMessage());
}
if (!$form->hasErrors())
{
try
{
if ( count(f('designation')) !== count(f('prix')) )
{
throw new UserException('Nombre de désignations et de prix reçus différent.');
}
}, $csrf_key);
$data = [
'numero' => f('numero_facture'),
'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->runIf(f('select_cotis') && !$form->hasErrors(),
function () use ($step)
{
$step = true;
}, 'add_cotis_1');
$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);
}
}
catch(UserException $e)
{
$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',
]);
$r = $facture->getCotis(f('membre_cotis'), $cotis['id']);
$r = $r[0];
$step = true;
}
elseif (f('add_cotis'))
{
$form->check('add_cotis_2',[
'numero_facture' => $require_number ? 'required|string' : 'string',
'date_emission' => 'required|date_format:d/m/Y',
'membre_cotis' => 'required|numeric',
'cotisation' => 'required',
]);
$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'] ]
];
$radio['type'] = f('cotisation');
if (!$form->hasErrors())
{
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']);
$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());
}
}
}
}
catch (UserException $e)
{
$form->addError($e->getMessage());
}
}, 'add_cotis_2');
if ($step)
{
@ -195,7 +163,7 @@ if ($step)
$form->addError($e->getMessage());
}
}
elseif (isset($data))
elseif (count($data) > 0)
{
if ($target)
{

View File

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

View File

@ -6,39 +6,27 @@ require_once __DIR__ . '/_inc.php';
$session->requireAccess($session::SECTION_ACCOUNTING, $session::ACCESS_READ);
if(f('add'))
{
$form->check('add_client', [
'nom' => 'required|string',
'adresse' => 'required|string',
'code_postal' => 'required|string',
'ville' => 'required|string',
'telephone' => 'string',
'email' => 'email'
]);
if (!$form->hasErrors())
{
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());
}
}
}
$form->runIf(f('add') && !$form->hasErrors(),
function () use ($client)
{
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());
}
}, 'add_client');
$list = $client->list();
$list->loadFromQueryString();

View File

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