2019-11-02 17:53:27 +01:00
< ? php
namespace Garradin ;
2021-12-17 21:10:54 +01:00
use const \Garradin\Plugin\Facturation\PATTERNS_LIST ;
2019-11-02 17:53:27 +01:00
require_once __DIR__ . '/_inc.php' ;
2021-04-11 14:25:59 +02:00
$session -> requireAccess ( $session :: SECTION_ACCOUNTING , $session :: ACCESS_WRITE );
2019-11-02 17:53:27 +01:00
use Garradin\DB ;
2020-12-29 18:07:26 +01:00
use stdClass ;
2019-11-02 17:53:27 +01:00
2020-12-29 18:07:26 +01:00
$db = DB :: getInstance ();
2023-04-14 19:48:07 +02:00
$step = false ;
$radio = $liste = [];
2021-12-17 14:49:03 +01:00
$designations = [];
$prix = [];
2020-12-29 18:07:26 +01:00
2021-12-17 14:35:12 +01:00
$csrf_key = 'ajout_facture' ;
2020-12-29 18:07:26 +01:00
$fields = $facture -> recu_fields ;
2021-12-17 14:35:12 +01:00
$moyens_paiement = $facture -> listMoyensPaiement ( true );
$doc = null ;
2021-12-17 21:10:54 +01:00
$require_number = $plugin -> getConfig ( 'pattern' ) ? false : true ;
2021-12-17 14:35:12 +01:00
2021-12-17 14:49:03 +01:00
if ( qg ( 'copy' ) !== null && $f = $facture -> get (( int ) qg ( 'copy' ))) {
2022-01-31 20:00:45 +01:00
$doc = ( array ) $f ;
2021-12-17 14:49:03 +01:00
2022-01-31 20:00:45 +01:00
// 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 ;
2022-01-07 01:24:32 +01:00
if ( $f -> type_facture == CERFA ) {
$doc [ 'forme_don' ] = $f -> contenu [ 'forme' ];
$doc [ 'nature_don' ] = $f -> contenu [ 'nature' ];
$doc [ 'texte_don' ] = $f -> contenu [ 'texte' ];
}
2021-12-17 14:49:03 +01:00
}
2021-12-17 21:10:54 +01:00
$tpl -> assign ( 'require_number' , $require_number );
$tpl -> assign ( 'number_pattern' , PATTERNS_LIST [ $plugin -> getConfig ( 'pattern' )]);
2021-12-17 14:35:12 +01:00
$tpl -> assign ( 'moyens_paiement' , $moyens_paiement );
2019-11-02 17:53:27 +01:00
$tpl -> assign ( 'moyen_paiement' , f ( 'moyen_paiement' ) ? : 'ES' );
2022-01-07 01:24:32 +01:00
$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' ,
'2' => 'Chèque' ,
'3' => 'Virement, CB; ...' ));
$tpl -> assign ( 'textes_don' , $facture -> listTextesCerfa ());
2021-12-17 14:49:03 +01:00
if ( f ( 'save' ))
2019-11-02 17:53:27 +01:00
{
2022-01-31 20:00:45 +01:00
$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' => '',
'moyen_paiement' => 'required|in:' . implode ( ',' , array_keys ( $moyens_paiement )),
'designation' => 'array|required' ,
'prix' => 'array|required'
]);
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.' );
}
$truc = [
'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
];
$truc [ 'type_facture' ] = f ( 'type' );
if ( in_array ( f ( 'type' ), [ DEVIS , FACT ]))
{
2022-01-07 01:24:32 +01:00
foreach ( f ( 'designation' ) as $k => $value )
{
$truc [ 'contenu' ][ $k ][ 'designation' ] = $value ;
$truc [ 'contenu' ][ $k ][ 'prix' ] = Utils :: moneyToInteger ( f ( 'prix' )[ $k ]);
$truc [ 'toto' ] += Utils :: moneyToInteger ( f ( 'prix' )[ $k ]);
}
$truc [ 'total' ] = $truc [ 'toto' ];
unset ( $truc [ 'toto' ]);
}
elseif ( f ( 'type' ) == CERFA )
{
2022-01-31 20:00:45 +01:00
$truc [ 'moyen_paiement' ] = f ( 'moyen_paiement2' );
2022-01-07 01:24:32 +01:00
$truc [ 'contenu' ] = [
'forme' => f ( 'forme_don' ),
'nature' => f ( 'nature_don' ),
'texte' => f ( 'texte_don' )];
unset ( $truc [ 'toto' ]);
$truc [ 'total' ] = Utils :: moneyToInteger ( f ( 'total' ));
}
2022-01-31 20:00:45 +01:00
if ( f ( 'base_receveur' ) == 'client' )
{
$truc [ 'receveur_membre' ] = 0 ;
$truc [ 'receveur_id' ] = f ( 'client' );
}
elseif ( f ( 'base_receveur' ) == 'membre' )
{
$truc [ 'receveur_membre' ] = 1 ;
$truc [ 'receveur_id' ] = f ( 'membre' );
}
$id = $facture -> add ( $truc , $plugin -> getConfig ( 'pattern' ));
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-12-29 18:07:26 +01:00
elseif ( f ( 'select_cotis' ))
{
2022-01-31 20:00:45 +01:00
$form -> check ( 'add_cotis_1' ,[
'numero_facture' => 'required|string' ,
'date_emission' => 'required|date_format:d/m/Y' ,
'membre_cotis' => 'required|numeric' ,
]);
2019-11-02 17:53:27 +01:00
2022-01-31 20:00:45 +01:00
$step = true ;
2020-12-29 18:07:26 +01:00
}
elseif ( f ( 'add_cotis' ))
2020-10-24 22:06:16 +02:00
{
2022-01-31 20:00:45 +01:00
$form -> check ( 'add_cotis_2' ,[
'numero_facture' => 'required|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' => f ( 'moyen_paiement' ),
'total' => $r -> paid_amount ? ? $r -> amount ,
'contenu' => [ 'id' => $cotis [ 'id' ],
'intitule' => $cotis [ 'label' ],
'souscription' => $cotis [ 'date' ],
'expiration' => $cotis [ 'expiry' ] ]
];
$id = $facture -> add ( $data , $plugin -> getConfig ( 'pattern' ));
Utils :: redirect ( PLUGIN_URL . 'facture.php?id=' . ( int ) $id );
}
catch ( UserException $e )
{
$form -> addError ( $e -> getMessage ());
}
}
$step = true ;
2020-10-24 22:06:16 +02:00
}
2020-12-29 18:07:26 +01:00
if ( $step )
{
2022-01-31 20:00:45 +01:00
try
{
$liste = $facture -> getCotis (( int ) f ( 'membre_cotis' ));
}
catch ( UserException $e )
{
$form -> addError ( $e -> getMessage ());
}
2020-12-29 18:07:26 +01:00
}
$type = qg ( 't' ) ? ( int ) qg ( 't' ) : null ;
if ( in_array ( $type , [ DEVIS , FACT , CERFA , COTIS ], true ))
{
2022-01-31 20:00:45 +01:00
$radio [ 'type' ] = $type ;
2020-12-29 18:07:26 +01:00
}
elseif ( null !== f ( 'type' ))
{
2022-01-31 20:00:45 +01:00
$radio [ 'type' ] = f ( 'type' );
2020-12-29 18:07:26 +01:00
}
2021-12-17 14:49:03 +01:00
elseif ( isset ( $doc [ 'type' ])) {
2022-01-31 20:00:45 +01:00
$radio [ 'type' ] = $doc [ 'type' ];
2021-12-17 14:49:03 +01:00
}
2020-12-29 18:07:26 +01:00
else
{
2022-01-31 20:00:45 +01:00
$radio [ 'type' ] = FACT ;
2020-12-29 18:07:26 +01:00
}
$tpl -> assign ( 'types_details' , $facture -> types );
2019-11-02 17:53:27 +01:00
$tpl -> assign ( 'client_id' , f ( 'client' ) ? : - 1 );
$tpl -> assign ( 'membre_id' , f ( 'membre' ) ? : - 1 );
2020-03-22 06:24:13 +01:00
2021-11-05 23:47:36 +01:00
$from_user = false ;
2020-10-24 07:28:31 +02:00
if (( $d = f ( 'designation' )) && ( $p = f ( 'prix' )) && implode ( $d ))
2019-11-02 17:53:27 +01:00
{
2022-01-31 20:00:45 +01:00
foreach ( $d as $k => $v )
{
if ( empty ( $v ) && empty ( $p [ $k ]))
{
continue ;
}
$designations [] = $v ;
$prix [] = $p [ $k ];
}
$from_user = true ;
2019-11-02 17:53:27 +01:00
}
2021-12-17 14:49:03 +01:00
else if ( ! empty ( $doc [ 'contenu' ])) {
2022-01-31 20:00:45 +01:00
foreach ( $doc [ 'contenu' ] as $k => $v )
{
if ( empty ( $v [ 'designation' ]) && empty ( $v [ 'prix' ]))
{
continue ;
}
$designations [] = $v [ 'designation' ];
$prix [] = $v [ 'prix' ];
}
2021-12-17 14:49:03 +01:00
}
2020-10-24 07:28:31 +02:00
else {
2022-01-31 20:00:45 +01:00
$designations = [ 'Exemple' ];
$prix = [ 250 ];
2020-10-24 07:28:31 +02:00
}
2020-12-29 18:07:26 +01:00
$tpl -> assign ( compact ( 'liste' , 'radio' , 'step' ));
$date = new \DateTime ;
$date -> setTimestamp ( time ());
$tpl -> assign ( 'date' , $date -> format ( 'd/m/Y' ));
2021-12-17 14:35:12 +01:00
$tpl -> assign ( compact ( 'designations' , 'prix' , 'from_user' , 'identite' , 'csrf_key' , 'doc' ));
2021-04-11 14:25:59 +02:00
$tpl -> assign ( 'membres' , $db -> getAssoc ( 'SELECT id, ' . $identite . ' FROM membres WHERE id_category != -2 NOT IN (SELECT id FROM users_categories WHERE hidden = 1);' ));
2020-12-29 18:07:26 +01:00
$tpl -> assign ( 'clients' , $db -> getAssoc ( 'SELECT id, nom FROM plugin_facturation_clients;' ));
2019-11-02 17:53:27 +01:00
2019-11-03 17:51:31 +01:00
$tpl -> display ( PLUGIN_ROOT . '/templates/facture_ajouter.tpl' );