facturation/www/admin/facture_ajouter.php

230 lines
5.0 KiB
PHP
Raw Normal View History

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-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();
$step = $radio = false;
$liste = [];
$fields = $facture->recu_fields;
$tpl->assign('moyens_paiement', $facture->listMoyensPaiement());
2019-11-02 17:53:27 +01:00
$tpl->assign('moyen_paiement', f('moyen_paiement') ?: 'ES');
if (f('add'))
{
2019-11-03 17:51:31 +01:00
$form->check('ajout_facture', [
2020-12-29 18:07:26 +01:00
'type' => 'required|in:'.implode(',', [DEVIS, FACT, CERFA]),
2019-11-03 17:51:31 +01:00
'numero_facture' => 'required|string',
2020-12-29 18:07:26 +01:00
'date_emission' => 'required|date_format:d/m/Y',
'date_echeance' => 'required|date_format:d/m/Y',
2019-11-03 17:51:31 +01:00
// 'reglee' => '',
// 'archivee' => '',
'base_receveur' => 'required|in:membre,client',
// 'client' => '',
// 'membre' => '',
2020-12-29 18:07:26 +01:00
'moyen_paiement' => 'required|in:' . implode(',', array_keys($facture->listMoyensPaiement())),
2019-11-03 17:51:31 +01:00
'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.');
2020-12-29 18:07:26 +01:00
}
$truc = [
2019-11-03 17:51:31 +01:00
'numero' =>f('numero_facture'),
'date_emission' => f('date_emission'),
'date_echeance' => f('date_echeance'),
2020-12-29 18:07:26 +01:00
'reglee' => f('reglee') == 1?1:0,
'archivee' => f('archivee') == 1?1:0,
2019-11-03 17:51:31 +01:00
'moyen_paiement' => f('moyen_paiement'),
'toto' => 0
];
2020-12-29 18:07:26 +01:00
if (f('type') == DEVIS)
{
2020-12-29 18:07:26 +01:00
$truc['type_facture'] = DEVIS;
}
2020-12-29 18:07:26 +01:00
elseif (f('type') == FACT)
{
2020-12-29 18:07:26 +01:00
$truc['type_facture'] = FACT;
}
2020-12-29 18:07:26 +01:00
elseif (f('type') == CERFA)
{
2020-12-29 18:07:26 +01:00
$truc['type_facture'] = CERFA;
}
2019-11-03 17:51:31 +01: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;
$truc['receveur_id'] = f('client');
}
elseif (f('base_receveur') == 'membre')
{
$truc['receveur_membre'] = 1;
$truc['receveur_id'] = f('membre');
}
$id = $facture->add($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-12-29 18:07:26 +01:00
elseif (f('select_cotis'))
{
$form->check('add_cotis_1',[
'numero_facture' => 'required|string',
'date_emission' => 'required|date_format:d/m/Y',
'membre' => 'required|numeric',
]);
2019-11-02 17:53:27 +01:00
2020-12-29 18:07:26 +01:00
$step = true;
}
elseif (f('add_cotis'))
{
2020-12-29 18:07:26 +01:00
$form->check('add_cotis_2',[
'numero_facture' => 'required|string',
'date_emission' => 'required|date_format:d/m/Y',
'membre' => 'required|numeric',
'cotisation' => 'required',
]);
$radio['type'] = f('cotisation');
if (!$form->hasErrors())
{
2020-12-29 18:07:26 +01:00
try
{
$num = (int) str_replace('cotis_', '', $radio['type']);
foreach($fields as $field)
{
$cotis[$field] = f($field.'_'.$num);
}
$r = $facture->getCotis(f('membre'), $cotis['id']);
$r = $r[0];
$data = [
'type_facture' => COTIS,
'numero' => f('numero_facture'),
'receveur_membre' => 1,
'receveur_id' => f('membre'),
'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);
Utils::redirect(PLUGIN_URL . 'facture.php?id='.(int)$id);
}
catch (UserException $e)
{
$form->addError($e->getMessage());
}
}
2020-12-29 18:07:26 +01:00
$step = true;
}
2020-12-29 18:07:26 +01:00
if ($step)
{
try
{
$liste = $facture->getCotis((int)f('membre'));
}
catch (UserException $e)
{
$form->addError($e->getMessage());
}
}
$type = qg('t') ? (int) qg('t') : null;
if (in_array($type, [DEVIS, FACT, CERFA, COTIS], true))
{
$radio['type'] = $type;
}
elseif (null !== f('type'))
{
$radio['type'] = f('type');
}
else
{
$radio['type'] = FACT;
}
$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);
2019-11-02 17:53:27 +01:00
$designations = [];
$prix = [];
if (($d = f('designation')) && ($p = f('prix')) && implode($d))
2019-11-02 17:53:27 +01:00
{
foreach($d as $k=>$v)
{
if ($v == '' && $p[$k] == 0)
{
continue;
}
$designations[] = $v;
$prix[] = $p[$k];
}
}
else {
$designations = ['Exemple'];
$prix = [1.5];
}
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'));
2019-11-02 17:53:27 +01:00
$tpl->assign('designations', $designations);
$tpl->assign('prix', $prix);
$tpl->assign('identite', $identite);
2020-12-29 18:07:26 +01:00
$tpl->assign('membres', $db->getAssoc('SELECT id, '.$identite.' FROM membres WHERE id_categorie != -2 NOT IN (SELECT id FROM membres_categories WHERE cacher = 1);'));
$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');