2023-04-16 00:08:10 +02:00
< ? php
2023-07-21 23:28:54 +02:00
namespace Paheko ;
2023-04-16 00:08:10 +02:00
require_once __DIR__ . '/_inc.php' ;
2024-09-10 15:37:22 +02:00
function toArray ( $array , $cle , $sep = " , " )
{
$result = array ();
foreach ( $array as $elem )
{
$ro = new \ReflectionObject ( $elem );
$proprietes = $ro -> getProperties ();
$ligne = " " ;
foreach ( $proprietes as $p )
{
if ( $p -> getName () == $cle ) {
$key = $p -> getValue ( $elem );
}
else {
$ligne .= $sep . $p -> getValue ( $elem );
}
}
$result [ $key ] = substr ( $ligne , strlen ( $sep ));
}
return $result ;
}
2023-04-16 00:08:10 +02:00
if ( ! isset ( $target ) || ! in_array ( $target , [ 'new' , 'edit' ])) {
throw new Exception ( 'blabla illegal call' ); // Fix: exception type?
} else {
$target = ( $target === 'new' ? true : false );
}
$session -> requireAccess ( $session :: SECTION_ACCOUNTING , $session :: ACCESS_WRITE );
2023-07-21 23:28:54 +02:00
use Paheko\DB ;
2023-04-16 00:08:10 +02:00
use stdClass ;
2023-08-01 22:56:38 +02:00
use const \Paheko\Plugin\Facturation\PATTERNS_LIST ;
2023-04-16 00:08:10 +02:00
$db = DB :: getInstance ();
$step = false ;
$radio = $liste = $designations = $prix = [];
$moyens_paiement = $facture -> listMoyensPaiement ( true );
$tpl -> assign ( 'moyens_paiement' , $moyens_paiement );
$tpl -> assign ( 'moyen_paiement' , f ( 'moyen_paiement' ) ? : 'ES' );
$tpl -> assign ( 'formes_don' , array ( '1' => 'Acte authentique' ,
'2' => 'Acte sous seing privé' ,
'3' => 'Don manuel' ,
'4' => 'Autres' ));
$tpl -> assign ( 'natures_don' , array ( '1' => 'Numéraire' ,
2024-03-29 11:57:36 +01:00
'2' => 'Chèque' ,
'3' => 'Virement, CB; ...' ));
2023-04-16 00:08:10 +02:00
if ( ! $target ) {
2023-08-01 22:56:38 +02:00
f ([ 'id' => 'required|numeric' ]);
2023-04-16 00:08:10 +02:00
$id = ( int ) qg ( 'id' );
2024-03-29 11:57:36 +01:00
2023-04-16 00:08:10 +02:00
if ( ! $f = $facture -> get ( $id ))
{
throw new UserException ( " Ce document n'existe pas. " );
}
}
// Traitement
2023-10-19 10:24:49 +02:00
$data = [];
$form -> runIf ( f ( 'save' ) && ! $form -> hasErrors (),
2024-01-10 18:43:23 +01:00
function () use ( $client , & $data , $form )
2024-03-29 11:57:36 +01:00
{
try
{
if ( count ( f ( 'designation' )) !== count ( f ( 'prix' )) )
{
throw new UserException ( 'Nombre de désignations et de prix reçus différent.' );
}
$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 )
{
if ( $value != '' && f ( 'prix' )[ $k ] == null ) {
throw new UserException ( 'Il manque le prix sur la ligne ' . $k + 1 . ' !!' );
}
$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' ]);
}
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' );
}
else
{
throw new UserException ( 'Vous devez indiquer si le receveur est un client ou un membre' );
}
}
catch ( UserException $e )
{
$form -> addError ( $e -> getMessage ());
}
}, $csrf_key );
2023-10-19 10:24:49 +02:00
2024-01-10 18:43:23 +01:00
if ( ! $form -> hasErrors ())
2023-04-16 00:08:10 +02:00
{
2024-09-06 21:40:27 +02:00
if ( count ( $data ) > 0 )
2023-04-16 00:08:10 +02:00
{
2024-01-10 18:43:23 +01:00
if ( $target )
{
$id = $facture -> add ( $data , $plugin -> getConfig ( 'pattern' ));
Utils :: redirect ( PLUGIN_ADMIN_URL . 'facture.php?id=' . ( int ) $id );
}
else
{
if ( $facture -> edit ( $id , $data ))
{
Utils :: redirect ( PLUGIN_ADMIN_URL . 'facture.php?id=' . ( int ) $id );
}
throw new UserException ( 'Erreur d\'édition du reçu' );
}
2023-04-16 00:08:10 +02:00
}
}
// Affichage
if ( $target )
{
$doc = null ;
if ( qg ( 'copy' ) !== null && $f = $facture -> get (( int ) qg ( 'copy' ))) {
$doc = ( array ) $f ;
// Copié depuis facture_modifier.php
$doc [ 'type' ] = $f -> type_facture ;
$doc [ 'numero_facture' ] = '' ;
$doc [ 'base_receveur' ] = $f -> receveur_membre ? 'membre' : 'client' ;
$doc [ 'client' ] = $f -> receveur_id ;
$doc [ 'membre' ] = $f -> receveur_id ;
}
// Type du document:
$type = qg ( 't' ) ? ( int ) qg ( 't' ) : null ;
// Si le type est défini dans l'URL
2024-09-06 21:40:27 +02:00
if ( in_array ( $type , [ DEVIS , FACT ], true ))
2023-04-16 00:08:10 +02:00
{
$radio [ 'type' ] = $type ;
} // ... s'il a été rempli dans le formulaire envoyé
elseif ( null !== f ( 'type' ))
{
$radio [ 'type' ] = f ( 'type' );
} // ... s'il est défini dans le document copié
elseif ( isset ( $doc [ 'type' ])) {
$radio [ 'type' ] = $doc [ 'type' ];
} // ... ou par défaut
2024-03-29 11:57:36 +01:00
else
2023-04-16 00:08:10 +02:00
{
$radio [ 'type' ] = FACT ;
}
}
2024-03-29 11:57:36 +01:00
else
2023-04-16 00:08:10 +02:00
{
$doc [ 'moyen_paiement' ] = $f -> moyen_paiement ;
$doc [ 'type' ] = $f -> type_facture ;
$doc [ 'numero_facture' ] = $f -> numero ;
$doc [ 'reglee' ] = $f -> reglee ;
$doc [ 'base_receveur' ] = $f -> receveur_membre ? 'membre' : 'client' ;
$doc [ 'client' ] = $f -> receveur_id ;
$doc [ 'membre' ] = $f -> receveur_id ;
$doc [ 'contenu' ] = $f -> contenu ;
2024-03-29 11:57:36 +01:00
2023-04-16 00:08:10 +02:00
$doc [ 'date_emission' ] = f ( 'date_emission' ) ? : $f -> date_emission ;
$doc [ 'date_echeance' ] = f ( 'date_echeance' ) ? : $f -> date_echeance ; // Smarty m'a saoulé pour utiliser form_field|date_fr:---
$radio [ 'type' ] = f ( 'type' ) ? ? $doc [ 'type' ];
}
$tpl -> assign ( 'types_details' , $facture -> types );
$tpl -> assign ( 'client_id' , f ( 'client' ) ? : - 1 );
$tpl -> assign ( 'membre_id' , f ( 'membre' ) ? : - 1 );
// 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
$from_user = false ;
if ( in_array ( $radio [ 'type' ], [ DEVIS , FACT ]))
{
if (( $d = f ( 'designation' )) && ( $p = f ( 'prix' )) && implode ( $d ))
{
foreach ( $d as $k => $v )
{
if ( empty ( $v ) && empty ( $p [ $k ]))
{
continue ;
}
$designations [] = $v ;
$prix [] = $p [ $k ];
}
$from_user = true ;
}
else if ( ! empty ( $doc [ 'contenu' ])) {
foreach ( $doc [ 'contenu' ] as $k => $v )
{
if ( empty ( $v [ 'designation' ]) && empty ( $v [ 'prix' ]))
{
continue ;
}
$designations [] = $v [ 'designation' ];
$prix [] = $v [ 'prix' ];
}
}
else {
$designations = [ 'Exemple' ];
$prix = [ 250 ];
}
}
$date = new \DateTime ;
$date -> setTimestamp ( time ());
$tpl -> assign ( 'date' , $date -> format ( 'd/m/Y' ));
$tpl -> assign ( compact ( 'liste' , 'radio' , 'step' , 'designations' , 'prix' , 'from_user' , 'identite' , 'csrf_key' , 'doc' ));
2024-09-10 15:37:22 +02:00
$tpl -> assign ( 'users' , toArray ( $db -> get ( 'SELECT id, ' . $identite . ' FROM users WHERE id_category != -2 NOT IN (SELECT id FROM users_categories WHERE hidden = 1) ORDER BY ' . $identite . ';' ), 'id' , " " ));
2023-04-16 00:08:10 +02:00
$tpl -> assign ( 'clients' , $db -> getAssoc ( 'SELECT id, nom FROM plugin_facturation_clients;' ));
$tpl -> assign ( 'require_number' , $require_number );
$tpl -> assign ( 'number_pattern' , PATTERNS_LIST [ $plugin -> getConfig ( 'pattern' )]);
if ( $target ) {
$tpl -> display ( PLUGIN_ROOT . '/templates/facture_ajouter.tpl' );
} else {
$tpl -> display ( PLUGIN_ROOT . '/templates/facture_modifier.tpl' );
2023-10-19 10:24:49 +02:00
}