facturation/www/admin/facture_modifier.php

264 lines
6.1 KiB
PHP
Raw Normal View History

2019-11-02 17:53:27 +01:00
<?php
namespace Garradin;
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
$db = DB::getInstance();
$step = false;
$liste = [];
$fields = $facture->recu_fields;
$tpl->assign('moyens_paiement', $facture->listMoyensPaiement());
$tpl->assign('moyen_paiement', f('moyen_paiement') ?: 'ES');
2019-11-02 17:53:27 +01:00
qv(['id' => 'required|numeric']);
$id = (int) qg('id');
if (!$f = $facture->get($id))
2019-11-02 17:53:27 +01:00
{
throw new UserException("Ce document n'existe pas.");
}
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-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'
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.');
}
$truc = [
'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,
'moyen_paiement' => f('moyen_paiement'),
'toto' => 0
];
if (in_array(f('type'), [DEVIS, FACT, CERFA]))
{
$truc['type_facture'] = f('type');
}
2019-11-03 17:51:31 +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']);
if (f('base_receveur') == 'client')
{
$truc['receveur_membre'] = 0;
2020-12-29 18:07:26 +01:00
$truc['receveur_id'] = f('client');
}
elseif (f('base_receveur') == 'membre')
{
$truc['receveur_membre'] = 1;
2020-12-29 18:07:26 +01:00
$truc['receveur_id'] = f('membre');
}
$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-12-29 18:07:26 +01:00
// Cotis
$fields = $facture->recu_fields;
$membre_id = f('membre') ?: $f->receveur_id;
$values['numero_facture'] = $f->numero;
$values['date_emission'] = $f->date_emission;
$radio = $liste = [];
if (f('select_cotis'))
{
$form->check('add_cotis_1',[
'numero_facture' => 'required|string',
'date_emission' => 'required|date_format:d/m/Y',
2021-11-06 03:56:49 +01:00
'membre_cotis' => 'required|numeric',
2020-12-29 18:07:26 +01:00
]);
$step = true;
}
elseif (f('add_cotis'))
{
$form->check('add_cotis_2',[
'numero_facture' => 'required|string',
'date_emission' => 'required|date_format:d/m/Y',
2021-11-06 03:56:49 +01:00
'membre_cotis' => 'required|numeric',
2020-12-29 18:07:26 +01:00
'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);
}
2021-11-06 03:56:49 +01:00
$r = $facture->getCotis(f('membre_cotis'), $cotis['id']);
2020-12-29 18:07:26 +01:00
$r = $r[0];
$data = [
'type_facture' => 3,
'numero' => f('numero_facture'),
'receveur_membre' => 1,
2021-11-06 03:56:49 +01:00
'receveur_id' => f('membre_cotis'),
2020-12-29 18:07:26 +01:00
'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'] ]
];
if($facture->edit($id, $data))
{
Utils::redirect(PLUGIN_URL . 'facture.php?id='.(int)$id);
}
throw new UserException('Erreur d\'édition du reçu');
}
catch (UserException $e)
{
$form->addError($e->getMessage());
}
}
$step = true;
}
if ($step)
{
try
{
2021-11-06 03:56:49 +01:00
$liste = $facture->getCotis((int)f('membre_cotis'));
2020-12-29 18:07:26 +01:00
}
catch (UserException $e)
{
$form->addError($e->getMessage());
}
}
2020-10-24 07:04:06 +02:00
// Affichage
2019-11-02 17:53:27 +01:00
2020-12-29 18:07:26 +01:00
$doc['moyens_paiement'] = $facture->listMoyensPaiement(true);
// $doc['moyen_paiement'] = $doc['moyens_paiement'][$f->moyen_paiement];
$doc['moyen_paiement'] = $f->moyen_paiement;
2020-12-29 18:07:26 +01:00
$doc['type'] = $f->type_facture;
$doc['numero_facture'] = $f->numero;
2020-12-29 18:07:26 +01:00
$doc['reglee'] = $f->reglee;
$doc['base_receveur'] = $f->receveur_membre?'membre':'client';
2020-12-29 18:07:26 +01:00
$doc['client'] = $f->receveur_id;
$doc['membre'] = $f->receveur_id;
$doc['date_emission'] = strtotime(f('date_emission')) ?: $f->date_emission;
$doc['date_echeance'] = strtotime(f('date_echeance')) ?: $f->date_echeance; // Smarty m'a saoulé pour utiliser form_field|date_fr:---
$tpl->assign('doc', $doc);
2020-12-29 18:07:26 +01:00
$radio['type'] = f('type')??$doc['type'];
$tpl->assign('types_details', $facture->types);
2020-10-24 07:04:06 +02:00
2020-12-29 18:07:26 +01:00
$tpl->assign('client_id', f('client') ?: -1);
$tpl->assign('membre_id', f('membre') ?: -1);
$tpl->assign(compact('liste', 'radio', 'step'));
2020-10-24 07:04:06 +02:00
$designations = [];
$prix = [];
2020-10-24 07:04:06 +02:00
// 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;
2020-12-29 18:07:26 +01:00
if ($f->type_facture != COTIS)
2019-11-02 17:53:27 +01:00
{
2020-12-29 18:07:26 +01:00
if (($d = f('designation')) && ($p = f('prix')))
2019-11-02 17:53:27 +01:00
{
2020-12-29 18:07:26 +01:00
foreach($d as $k=>$v)
2019-11-02 17:53:27 +01:00
{
if (empty($v) && empty($p[$k]))
2020-12-29 18:07:26 +01:00
{
continue;
}
$designations[] = $v;
$prix[] = $p[$k];
2019-11-02 17:53:27 +01:00
}
$from_user = true;
2019-11-02 17:53:27 +01:00
}
2020-12-29 18:07:26 +01:00
else
2019-11-02 17:53:27 +01:00
{
2020-12-29 18:07:26 +01:00
foreach($f->contenu as $k=>$v)
2019-11-02 17:53:27 +01:00
{
if (empty($v['designation']) && empty($v['prix']))
2020-12-29 18:07:26 +01:00
{
continue;
}
$designations[] = $v['designation'];
$prix[] = $v['prix'];
2019-11-02 17:53:27 +01:00
}
$from_user = false;
2019-11-02 17:53:27 +01:00
}
}
2020-12-29 18:07:26 +01:00
$tpl->assign(compact('designations', 'prix', 'from_user', 'identite'));
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;'));
$date = new \DateTime;
$date->setTimestamp(time());
$tpl->assign('date', $date->format('d/m/Y'));
2019-11-02 17:53:27 +01:00
$tpl->display(PLUGIN_ROOT . '/templates/facture_modifier.tpl');