diff --git a/admin/_facture_common.php b/admin/_facture_common.php index cfed2b3..2daeca1 100644 --- a/admin/_facture_common.php +++ b/admin/_facture_common.php @@ -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()); + } + + }, $csrf_key); - 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.'); - } +$form->runIf(f('select_cotis') && !$form->hasErrors(), + function () use ($step) + { + $step = true; + }, 'add_cotis_1'); - $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('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']); + $r = $r[0]; - } - 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', - ]); + $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'] ] + ]; - $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', - ]); - - $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) { @@ -333,4 +301,4 @@ if ($target) { $tpl->display(PLUGIN_ROOT . '/templates/facture_ajouter.tpl'); } else { $tpl->display(PLUGIN_ROOT . '/templates/facture_modifier.tpl'); -} \ No newline at end of file +} diff --git a/admin/client_modifier.php b/admin/client_modifier.php index cdfd0aa..1f81804 100644 --- a/admin/client_modifier.php +++ b/admin/client_modifier.php @@ -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); diff --git a/admin/clients.php b/admin/clients.php index f5adf78..b2193a3 100644 --- a/admin/clients.php +++ b/admin/clients.php @@ -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(); diff --git a/admin/facture_supprimer.php b/admin/facture_supprimer.php index 04e5918..4fc42cb 100644 --- a/admin/facture_supprimer.php +++ b/admin/facture_supprimer.php @@ -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'); \ No newline at end of file +$tpl->display(PLUGIN_ROOT . '/templates/facture_supprimer.tpl');