2019-11-02 17:53:27 +01:00
< ? php
namespace Garradin ;
require_once __DIR__ . '/_inc.php' ;
$session -> requireAccess ( 'compta' , Membres :: DROIT_ECRITURE );
use Garradin\DB ;
2020-10-24 07:04:06 +02:00
use stdClass ;
2019-11-02 17:53:27 +01:00
qv ([ 'id' => 'required|numeric' ]);
$id = ( int ) qg ( 'id' );
$f = $facture -> get ( $id );
if ( ! $f )
{
throw new UserException ( " Ce document n'existe pas. " );
}
$cats = new Compta\Categories ;
2020-10-24 07:04:06 +02:00
// Traitement
2019-11-02 17:53:27 +01:00
if ( f ( 'save' ))
{
$form -> check ( 'modifier_facture' , [
2020-10-24 05:40:42 +02:00
'type' => 'required|in:facture,devis,cerfa,cotis' ,
2019-11-03 17:51:31 +01:00
'numero_facture' => 'required|string' ,
'date_emission' => 'required|date' ,
'date_echeance' => 'required|date' ,
// 'reglee' => '',
// 'archivee' => '',
'base_receveur' => 'required|in:membre,client' ,
// 'client' => '',
// 'membre' => '',
'moyen_paiement' => 'required|in:' . implode ( ',' , array_keys ( $cats -> listMoyensPaiement ())),
'designation' => 'array|required' ,
'prix' => 'array|required'
2019-11-02 17:53:27 +01:00
]);
2019-11-03 17:51:31 +01:00
2019-11-02 17:53:27 +01:00
if ( ! $form -> hasErrors ())
{
2019-11-03 17:51:31 +01:00
try
{
if ( count ( f ( 'designation' )) !== count ( f ( 'prix' )) )
{
throw new UserException ( 'Nombre de désignations et de prix reçus différent.' );
2020-10-24 01:00:48 +02:00
}
$truc = [
'numero' => f ( 'numero_facture' ),
'date_emission' => f ( 'date_emission' ),
'date_echeance' => f ( 'date_echeance' ),
'reglee' => f ( 'reglee' ) == 'on' ? 1 : 0 ,
'archivee' => f ( 'archivee' ) == 'on' ? 1 : 0 ,
'moyen_paiement' => f ( 'moyen_paiement' ),
'toto' => 0
];
2020-10-24 05:40:42 +02:00
if ( f ( 'type' ) == 'devis' )
{
$truc [ 'type_facture' ] = 0 ;
}
elseif ( f ( 'type' ) == 'facture' )
2020-10-24 01:00:48 +02:00
{
$truc [ 'type_facture' ] = 1 ;
}
2020-10-24 05:40:42 +02:00
elseif ( f ( 'type' ) == 'cerfa' )
2020-10-24 01:00:48 +02:00
{
2020-10-24 05:40:42 +02:00
$truc [ 'type_facture' ] = 2 ;
2020-10-24 01:00:48 +02:00
}
2019-11-03 17:51:31 +01:00
2020-10-24 01:00:48 +02:00
foreach ( f ( 'designation' ) as $k => $value )
{
$truc [ 'contenu' ][ $k ][ 'designation' ] = $value ;
$truc [ 'contenu' ][ $k ][ 'prix' ] = f ( 'prix' )[ $k ];
$truc [ 'toto' ] += f ( 'prix' )[ $k ];
}
$truc [ 'total' ] = $truc [ 'toto' ];
unset ( $truc [ 'toto' ]);
if ( f ( 'base_receveur' ) == 'client' )
{
$truc [ 'receveur_membre' ] = 0 ;
2020-10-24 07:04:06 +02:00
$truc [ 'receveur_id' ] = f ( 'client_id' );
2020-10-24 01:00:48 +02:00
}
elseif ( f ( 'base_receveur' ) == 'membre' )
{
$truc [ 'receveur_membre' ] = 1 ;
2020-10-24 07:04:06 +02:00
$truc [ 'receveur_id' ] = f ( 'membre_id' );
2020-10-24 01:00:48 +02:00
}
$r = $facture -> edit ( $id , $truc );
Utils :: redirect ( PLUGIN_URL . 'facture.php?id=' . ( int ) $id );
}
catch ( UserException $e )
{
$form -> addError ( $e -> getMessage ());
}
2019-11-02 17:53:27 +01:00
}
}
2020-10-24 07:04:06 +02:00
// Affichage
2019-11-02 17:53:27 +01:00
2020-10-24 07:04:06 +02:00
$doc = new stdClass ();
$doc -> moyens_paiement = $cats -> listMoyensPaiement ();
$doc -> moyen_paiement = $f -> moyen_paiement ;
$doc -> type = $facture -> type [ $f -> type_facture ];
$doc -> numero_facture = $f -> numero ;
$doc -> reglee = $f -> reglee ? 'on' : 'off' ;
$doc -> base_receveur = $f -> receveur_membre ? 'membre' : 'client' ;
$doc -> client_id = $f -> receveur_id ;
$doc -> membre_id = $f -> receveur_id ;
2020-10-24 05:40:42 +02:00
2020-10-24 07:04:06 +02:00
$tpl -> assign ( 'date_emission' , strtotime ( f ( 'date_emission' )) ? : $f -> date_emission ); // Smarty m'a saoulé pour utiliser form_field|date_fr:---
$tpl -> assign ( 'date_echeance' , strtotime ( f ( 'date_echeance' )) ? : $f -> date_echeance ); // Du coup j'utilise form_field pour ces champs
$tpl -> assign ( 'doc' , $doc );
// C'est un peu l'équivalent de form_field, mais j'avais écrit ça avant
// et oulala, c'est un peu complexe, faudrait réfléchir keskivomieux
2019-11-02 17:53:27 +01:00
if (( $d = f ( 'designation' )) && ( $p = f ( 'prix' )))
{
foreach ( $d as $k => $v )
{
if ( $v == '' && $p [ $k ] == 0 )
{
continue ;
}
$designations [] = $v ;
$prix [] = $p [ $k ];
}
}
2020-10-24 07:04:06 +02:00
else
2019-11-02 17:53:27 +01:00
{
foreach ( $f -> contenu as $k => $v )
{
if ( $v [ 'designation' ] == '' && $v [ 'prix' ] == 0 )
{
continue ;
}
$designations [] = $v [ 'designation' ];
$prix [] = $v [ 'prix' ];
}
}
$tpl -> assign ( 'designations' , $designations );
$tpl -> assign ( 'prix' , $prix );
$tpl -> assign ( 'membres' , ( array ) DB :: getInstance () -> get ( 'SELECT id, nom FROM membres WHERE id_categorie != -2 NOT IN (SELECT id FROM membres_categories WHERE cacher = 1);' ));
$tpl -> assign ( 'clients' , $client -> listAll ());
$tpl -> display ( PLUGIN_ROOT . '/templates/facture_modifier.tpl' );