diff --git a/sources/Afup/Comptabilite/Comptabilite.php b/sources/Afup/Comptabilite/Comptabilite.php index 0e05d59dd..9b0eafccf 100755 --- a/sources/Afup/Comptabilite/Comptabilite.php +++ b/sources/Afup/Comptabilite/Comptabilite.php @@ -9,10 +9,7 @@ namespace Afup\Site\Comptabilite; -use Afup\Site\Forum\Forum; use Afup\Site\Utils\Base_De_Donnees; -use AppBundle\Compta\Importer\AutoQualifier; -use AppBundle\Compta\Importer\Importer; class Comptabilite { @@ -26,197 +23,6 @@ class Comptabilite public function __construct(protected Base_De_Donnees $_bdd) {} - - /* affiche le journal de : - * courant = Compte courant - * Livret A - * Espece - * Paypal - * - */ - public function obtenirJournalBanque($compte = 1, - $periode_debut = '', - $periode_fin = '', - ) { - $periode_debut = $this->periodeDebutFin($debutFin = 'debut', $periode_debut); - $periode_fin = $this->periodeDebutFin($debutFin = 'fin', $periode_fin); - $requete = 'SELECT '; - $requete .= 'compta.date_regl, compta.description, compta.montant, compta.idoperation, '; - $requete .= 'MONTH(compta.date_regl) as mois, compta.id as idtmp, compta.comment,'; - $requete .= 'compta_reglement.reglement, '; - $requete .= 'compta_evenement.evenement, compta.idevenement, '; - $requete .= 'compta_categorie.categorie, compta.idcategorie, '; - $requete .= 'compta.attachment_required, compta.attachment_filename, '; - $requete .= 'compta_compte.nom_compte as compta_compte_nom_compte '; - $requete .= 'FROM '; - $requete .= 'compta '; - $requete .= 'LEFT JOIN '; - $requete .= 'compta_categorie on compta_categorie.id=compta.idcategorie '; - $requete .= 'LEFT JOIN '; - $requete .= 'compta_reglement on compta_reglement.id=compta.idmode_regl '; - $requete .= 'LEFT JOIN '; - $requete .= 'compta_evenement on compta_evenement.id=compta.idevenement '; - $requete .= 'LEFT JOIN '; - $requete .= 'compta_compte on compta_compte.id=compta.idcompte '; - $requete .= 'WHERE '; - $requete .= 'compta.date_regl >= \'' . $periode_debut . '\' '; - $requete .= 'AND compta.date_regl <= \'' . $periode_fin . '\' '; - $requete .= 'AND compta.montant != \'0.00\' '; - $requete .= 'AND compta.idmode_regl = compta_reglement.id '; - $requete .= 'AND idcompte = ' . (int) $compte . ' '; - $requete .= 'ORDER BY '; - $requete .= 'compta.date_regl '; - return $this->_bdd->obtenirTous($requete); - } - - - public function obtenirSousTotalJournalBanque($periode_debut, $periode_fin, $compte = 1) - { - $data = $this->obtenirJournalBanque($compte, $periode_debut, $periode_fin); - - for ($i = 1; $i <= 12; $i++) { - $credit[$i] = 0; - $debit[$i] = 0; - $nligne[$i] = 0; - } - foreach ($data as $row) { - if ($row['idoperation'] == "1") { - $debit[$row['mois']] += $row['montant']; - } - if ($row['idoperation'] == "2") { - $credit[$row['mois']] += $row['montant']; - } - if ($row['idoperation'] == "1" || $row['idoperation'] == "2") { - $nligne[$row['mois']]++; - } - } - - $dif_old = 0; - for ($i = 1; $i <= 12; $i++) { - $dif = $dif_old + $credit[$i] - $debit[$i]; - $tableau[$i] = ["mois" => $i, - "debit" => $debit[$i], - "credit" => $credit[$i], - "dif" => $dif, - "nligne" => $nligne[$i], - ]; - $dif_old = $dif; - } - - return $tableau; - } - - public function obtenirTotalJournalBanque($periode_debut, $periode_fin, $compte = 1): array - { - $data = $this->obtenirJournalBanque($compte, $periode_debut, $periode_fin); - /* echo "
";
-       print_r($data);
-       echo "
";*/ - $credit = 0; - $debit = 0; - - foreach ($data as $row) { - if ($row['idoperation'] == "1") { - $debit += $row['montant']; - } - if ($row['idoperation'] == "2") { - $credit += $row['montant']; - } - } - //print_r($credit); - //$dif_old=0; - //for ($i=1;$i<=12;$i++) - //{ - // $dif=$dif_old+$credit[$i]-$debit[$i]; - $tableau = [ - "debit" => $debit, - "credit" => $credit, - "dif" => $credit - $debit, - ]; - // $dif_old=$dif; - //} - - return $tableau; - /* $total=0; - foreach ($data as $id=>$row) - { - - if ($idoperation==$row['idoperation']) - $total += $row['montant']; - } - - return $total; - */ - } - - /* Journal des opération - * - */ - - public function obtenirJournal(string $debitCredit = '', - $periode_debut = '', - $periode_fin = '', - $onlyUnclasifedEntries = true, - ) { - $periode_debut = $this->periodeDebutFin($debutFin = 'debut', $periode_debut); - $periode_fin = $this->periodeDebutFin($debutFin = 'fin', $periode_fin); - - $filtre = $debitCredit == 1 || $debitCredit == 2 ? 'AND compta.idoperation =\'' . $debitCredit . '\' ' : ""; - - $requete = 'SELECT '; - $requete .= 'compta.date_ecriture, compta.description, compta.montant, compta.idoperation,compta.id as idtmp, '; - $requete .= 'compta.comment, compta.attachment_required, compta.attachment_filename, '; - $requete .= 'compta_reglement.reglement, '; - $requete .= 'compta_evenement.evenement, '; - $requete .= 'compta_categorie.categorie, '; - $requete .= 'compta_compte.nom_compte, '; - $requete .= '(COALESCE(compta.montant_ht_soumis_tva_0,0) + COALESCE(compta.montant_ht_soumis_tva_5_5,0) + COALESCE(compta.montant_ht_soumis_tva_10, 0) + COALESCE(compta.montant_ht_soumis_tva_20, 0)) as montant_ht, '; - $requete .= '((COALESCE(compta.montant_ht_soumis_tva_5_5, 0)*0.055) + (COALESCE(compta.montant_ht_soumis_tva_10, 0)*0.1) + (COALESCE(compta.montant_ht_soumis_tva_20, 0)*0.2)) as montant_tva, '; - $requete .= 'compta.montant_ht_soumis_tva_0 as montant_ht_0, '; - $requete .= 'compta.montant_ht_soumis_tva_5_5 as montant_ht_5_5, '; - $requete .= 'compta.montant_ht_soumis_tva_5_5*0.055 as montant_tva_5_5, '; - $requete .= 'compta.montant_ht_soumis_tva_10 as montant_ht_10, '; - $requete .= 'compta.montant_ht_soumis_tva_10*0.1 as montant_tva_10, '; - $requete .= 'compta.montant_ht_soumis_tva_20 as montant_ht_20, '; - $requete .= 'compta.montant_ht_soumis_tva_20*0.2 as montant_tva_20, '; - $requete .= 'compta.tva_zone '; - $requete .= 'FROM '; - $requete .= 'compta '; - $requete .= 'LEFT JOIN '; - $requete .= 'compta_categorie on compta_categorie.id=compta.idcategorie '; - $requete .= 'LEFT JOIN '; - $requete .= 'compta_reglement on compta_reglement.id=compta.idmode_regl '; - $requete .= 'LEFT JOIN '; - $requete .= 'compta_evenement on compta_evenement.id=compta.idevenement '; - $requete .= 'LEFT JOIN '; - $requete .= 'compta_compte on compta_compte.id=compta.idcompte '; - $requete .= 'WHERE '; - $requete .= ' compta.date_ecriture >= \'' . $periode_debut . '\' '; - $requete .= 'AND compta.date_ecriture <= \'' . $periode_fin . '\' '; - $requete .= $filtre; - if (true === $onlyUnclasifedEntries) { - $requete .= ' AND ( - compta_evenement.evenement = "A déterminer" - OR - compta_categorie.categorie = "A déterminer" - OR - compta_reglement.reglement = "A déterminer" - OR - (compta.attachment_required = 1 AND compta.attachment_filename IS NULL) - ) '; - } - $requete .= 'ORDER BY '; - $requete .= 'compta.date_ecriture, numero_operation'; - - return $this->_bdd->obtenirTous($requete); - } - - // mise en forme du montant - public function formatMontantCompta($valeur): string - { - return number_format($valeur, 2, ',', ' '); - } - public function periodeDebutFin($debutFin = 'debut', $date = '') { // echo "=>$debutFin*$date*
"; @@ -297,653 +103,4 @@ public function obtenirListPeriode(?string $date_debut = '', ?string $date_fin = return $this->_bdd->obtenirTous($requete); } - - public function obtenirListOperations($filtre = '', ?string $where = '') - { - $requete = 'SELECT '; - $requete .= 'id, operation '; - $requete .= 'FROM '; - $requete .= 'compta_operation '; - if ($where) { - $requete .= 'WHERE id=' . $where . ' '; - } - - $requete .= 'ORDER BY '; - $requete .= 'operation '; - - if ($where) { - return $this->_bdd->obtenirEnregistrement($requete); - } elseif ($filtre) { - return $this->_bdd->obtenirTous($requete); - } else { - $data = $this->_bdd->obtenirTous($requete); - $result[] = ""; - foreach ($data as $row) { - $result[$row['id']] = $row['operation']; - } - - return $result; - } - } - - public function obtenirListComptes($filtre = '', ?string $where = '') - { - $requete = 'SELECT '; - $requete .= 'id, nom_compte, archived_at '; - $requete .= 'FROM '; - $requete .= 'compta_compte '; - if ($where) { - $requete .= 'WHERE id=' . $where . ' '; - } - - $requete .= 'ORDER BY '; - $requete .= 'nom_compte '; - - if ($where) { - return $this->_bdd->obtenirEnregistrement($requete); - } elseif ($filtre) { - return $this->_bdd->obtenirTous($requete); - } else { - $data = $this->_bdd->obtenirTous($requete); - $result[] = ""; - foreach ($data as $row) { - $result[$row['id']] = $row['nom_compte']; - } - - return $result; - } - } - - /** - * @return array - */ - public function obtenirListeComptesActifs(): array - { - $requete = 'SELECT id, nom_compte FROM compta_compte '; - $requete .= 'WHERE archived_at IS NULL ORDER BY nom_compte '; - - $data = $this->_bdd->obtenirTous($requete); - $result = []; - foreach ($data as $row) { - $result[$row['id']] = $row['nom_compte']; - } - - return $result; - } - - public function obtenirListCategories($filtre = '', ?string $where = '', $usedInAccountingJournal = false) - { - $requete = 'SELECT '; - $requete .= 'id, idevenement, categorie '; - $requete .= 'FROM '; - $requete .= 'compta_categorie '; - $wheres = []; - if ($where) { - $wheres[] = 'id=' . $where . ' '; - } - if ($usedInAccountingJournal) { - $wheres[] = 'hide_in_accounting_journal_at IS NULL'; - } - - if ($wheres !== []) { - $requete .= sprintf('WHERE %s ',implode(' AND ', $wheres)); - } - - $requete .= 'ORDER BY '; - $requete .= 'categorie '; - - if ($where) { - return $this->_bdd->obtenirEnregistrement($requete); - } elseif ($filtre) { - return $this->_bdd->obtenirTous($requete); - } else { - $data = $this->_bdd->obtenirTous($requete); - $result[] = ""; - foreach ($data as $row) { - $result[$row['id']] = $row['categorie']; - } - - return $result; - } - } - - public function obtenirListCategoriesJournal() - { - $categories = $this->obtenirListCategories('', '', true); - unset($categories[0]); - - return $categories; - } - - public function obtenirListEvenements($filtre = '', ?string $where = '', $usedInAccountingJournal = false) - { - $requete = 'SELECT '; - $requete .= 'id, evenement '; - $requete .= 'FROM '; - $requete .= 'compta_evenement '; - $wheres = []; - if ($where) { - $wheres[] = 'id=' . $where . ' '; - } - if ($usedInAccountingJournal) { - $wheres[] = 'hide_in_accounting_journal_at IS NULL'; - } - - if ($wheres !== []) { - $requete .= sprintf('WHERE %s ',implode(' AND ', $wheres)); - } - - $requete .= 'ORDER BY '; - $requete .= 'evenement '; - - if ($where) { - return $this->_bdd->obtenirEnregistrement($requete); - } elseif ($filtre) { - return $this->_bdd->obtenirTous($requete); - } else { - $data = $this->_bdd->obtenirTous($requete); - $result[] = ""; - foreach ($data as $row) { - $result[$row['id']] = $row['evenement']; - } - - return $result; - } - } - - public function obtenirListEvenementsJournal() - { - $events = $this->obtenirListEvenements('', '', true); - unset($events[0]); - - return $events; - } - - public function obtenirListReglements($filtre = '', ?string $where = '', $usedInAccountingJournal = false) - { - $requete = 'SELECT '; - $requete .= 'id, reglement '; - $requete .= 'FROM '; - $requete .= 'compta_reglement '; - $wheres = []; - if ($where) { - $wheres[] = 'id=' . $where . ' '; - } - if ($usedInAccountingJournal) { - $wheres[] = 'hide_in_accounting_journal_at IS NULL'; - } - - if ($wheres !== []) { - $requete .= sprintf('WHERE %s ',implode(' AND ', $wheres)); - } - - $requete .= 'ORDER BY '; - $requete .= 'reglement '; - - if ($where) { - return $this->_bdd->obtenirEnregistrement($requete); - } elseif ($filtre) { - return $this->_bdd->obtenirTous($requete); - } else { - $data = $this->_bdd->obtenirTous($requete); - $result[] = ""; - foreach ($data as $row) { - $result[$row['id']] = $row['reglement']; - } - - return $result; - } - } - - public function obtenirListReglementsJournal() - { - $reglements = $this->obtenirListReglements('','', true); - unset($reglements[0]); - - return $reglements; - } - - public function ajouter($idoperation, $idcompte, $idcategorie, $date_ecriture, $nom_frs, $tva_intra, $montant, $description, - $numero, $idmode_regl, $date_regl, $obs_regl, $idevenement, $numero_operation = null, - $attachmentRequired = 0, $montantHtSoumisTva0 = null, $montantHtSoumisTva55 = null, $montantHtSoumisTva10 = null, $montantHtSoumisTva20 = null, $tvaZone = null) - { - $requete = 'INSERT INTO '; - $requete .= 'compta ('; - $requete .= 'idoperation,idcategorie,date_ecriture,nom_frs,tva_intra,montant,description,'; - $requete .= 'numero,idmode_regl,date_regl,obs_regl,idevenement, numero_operation,idcompte, attachment_required, - montant_ht_soumis_tva_0, montant_ht_soumis_tva_5_5, montant_ht_soumis_tva_10, montant_ht_soumis_tva_20, tva_zone - ) '; - $requete .= 'VALUES ('; - $requete .= $this->_bdd->echapper($idoperation) . ','; - $requete .= $this->_bdd->echapper($idcategorie) . ','; - $requete .= $this->_bdd->echapper($date_ecriture) . ','; - $requete .= $this->_bdd->echapper($nom_frs) . ','; - $requete .= $this->_bdd->echapper($tva_intra) . ','; - $requete .= $this->_bdd->echapper($montant) . ','; - $requete .= $this->_bdd->echapper($description) . ','; - $requete .= $this->_bdd->echapper($numero) . ','; - $requete .= $this->_bdd->echapper($idmode_regl) . ','; - $requete .= $this->_bdd->echapper($date_regl) . ','; - $requete .= $this->_bdd->echapper($obs_regl) . ','; - $requete .= $this->_bdd->echapper($idevenement) . ','; - $requete .= $this->_bdd->echapper($numero_operation) . ','; - $requete .= $this->_bdd->echapper($idcompte) . ','; - $requete .= $this->_bdd->echapper($attachmentRequired) . ','; - $requete .= ($montantHtSoumisTva0 ? $this->_bdd->echapper($montantHtSoumisTva0) : 'NULL') . ','; - $requete .= ($montantHtSoumisTva55 ? $this->_bdd->echapper($montantHtSoumisTva55) : 'NULL') . ','; - $requete .= ($montantHtSoumisTva10 ? $this->_bdd->echapper($montantHtSoumisTva10) : 'NULL') . ','; - $requete .= ($montantHtSoumisTva20 ? $this->_bdd->echapper($montantHtSoumisTva20) : 'NULL') . ','; - $requete .= ($tvaZone ? $this->_bdd->echapper($tvaZone) : 'NULL') . ''; - $requete .= ');'; - - $resultat = $this->_bdd->executer($requete); - if ($resultat) { - $this->lastId = $this->_bdd->obtenirDernierId(); - } - return $resultat; - } - - public function modifier(string $id, $idoperation, $idcompte, $idcategorie, $date_ecriture, $nom_frs, $tva_intra, $montant, $description, - $numero, $idmode_regl, $date_regl, $obs_regl, $idevenement, $comment, $numero_operation = null, $attachmentRequired = 0, - $montantHtSoumisTva0 = null, $montantHtSoumisTva55 = null, $montantHtSoumisTva10 = null, $montantHtSoumisTva20 = null, - $tvaZone = null, - ) { - $requete = 'UPDATE '; - $requete .= 'compta '; - $requete .= 'SET '; - $requete .= 'idoperation=' . $this->_bdd->echapper($idoperation) . ','; - $requete .= 'idcategorie=' . $this->_bdd->echapper($idcategorie) . ','; - $requete .= 'date_ecriture=' . $this->_bdd->echapper($date_ecriture) . ','; - $requete .= 'nom_frs=' . $this->_bdd->echapper($nom_frs) . ','; - $requete .= 'tva_intra=' . $this->_bdd->echapper($tva_intra) . ','; - $requete .= 'montant=' . $this->_bdd->echapper($montant) . ','; - $requete .= 'description=' . $this->_bdd->echapper($description) . ','; - $requete .= 'numero=' . $this->_bdd->echapper($numero) . ','; - $requete .= 'idmode_regl=' . $this->_bdd->echapper($idmode_regl) . ','; - $requete .= 'date_regl=' . $this->_bdd->echapper($date_regl) . ','; - $requete .= 'obs_regl=' . $this->_bdd->echapper($obs_regl) . ','; - $requete .= 'idcompte=' . $this->_bdd->echapper($idcompte) . ','; - $requete .= 'montant_ht_soumis_tva_0=' . ($montantHtSoumisTva0 ? $this->_bdd->echapper($montantHtSoumisTva0) : 'NULL') . ','; - $requete .= 'montant_ht_soumis_tva_5_5=' . ($montantHtSoumisTva55 ? $this->_bdd->echapper($montantHtSoumisTva55) : 'NULL') . ','; - $requete .= 'montant_ht_soumis_tva_10=' . ($montantHtSoumisTva10 ? $this->_bdd->echapper($montantHtSoumisTva10) : 'NULL') . ','; - $requete .= 'montant_ht_soumis_tva_20=' . ($montantHtSoumisTva20 ? $this->_bdd->echapper($montantHtSoumisTva20) : 'NULL') . ','; - $requete .= 'tva_zone=' . ($tvaZone ? $this->_bdd->echapper($tvaZone) : 'NULL') . ','; - $requete .= 'comment=' . ($comment ? $this->_bdd->echapper($comment) : 'NULL') . ','; - if ($numero_operation) { - $requete .= 'numero_operation=' . $this->_bdd->echapper($numero_operation) . ','; - } - $requete .= 'idevenement=' . $this->_bdd->echapper($idevenement) . ','; - $requete .= 'attachment_required=' . $this->_bdd->echapper($attachmentRequired) . ' '; - $requete .= 'WHERE '; - $requete .= 'id=' . $id . ' '; - - return $this->_bdd->executer($requete); - } - - /** - * Update one column value of a "compta" line. - * @param $id int "compta" identifier - * @param $columnName string Column name (whitelist: idcategorie, idmode_regl, idevenement) - * @param $columnValue string New value - * @return \mysqli_result|bool FALSE on failure - * @throws \Exception If bad column name - */ - public function modifierColonne($id, $columnName, $columnValue) - { - - // Check column using whitelist - if (!in_array($columnName, [ - 'idcategorie', - 'idmode_regl', - 'idevenement', - 'comment', - 'attachment_required', - 'attachment_filename', - ]) - ) { - throw new \Exception("Please provide a whitelisted column name."); - } - - $id = intval($id); - $requete = <<_bdd->echapper($columnValue)} - WHERE id = {$id}; -SQL; - - return $this->_bdd->executer($requete); - } - - public function ajouterConfig(string $table, string $champ, $valeur) - { - $requete = 'INSERT INTO '; - $requete .= '' . $table . ' ('; - $requete .= '' . $champ . ') '; - $requete .= 'VALUES ('; - $requete .= $this->_bdd->echapper($valeur) . ' '; - $requete .= ');'; - - return $this->_bdd->executer($requete); - } - - public function modifierConfig(string $table, string $id, string $champ, $valeur) - { - $requete = 'UPDATE '; - $requete .= '' . $table . ' '; - $requete .= 'SET '; - $requete .= '' . $champ . ' = ' . $this->_bdd->echapper($valeur) . ' '; - $requete .= 'WHERE '; - $requete .= 'id = ' . $id . ' '; - - return $this->_bdd->executer($requete); - } - - public function obtenir(int $id) - { - $requete = 'SELECT'; - $requete .= ' * '; - $requete .= 'FROM'; - $requete .= ' compta '; - $requete .= 'WHERE id=' . $id; - - return $this->_bdd->obtenirEnregistrement($requete); - } - - public function supprimerEcriture(string $id) - { - $requete = 'DELETE FROM compta WHERE id=' . $id; - return $this->_bdd->executer($requete); - } - - public function obtenirParNumeroOperation($numero_operation) - { - $requete = 'SELECT'; - $requete .= ' * '; - $requete .= 'FROM'; - $requete .= ' compta '; - $requete .= 'WHERE numero_operation=' . $this->_bdd->echapper($numero_operation); - - return $this->_bdd->obtenirEnregistrement($requete); - } - - public function obtenirTous() - { - $requete = 'SELECT'; - $requete .= ' * '; - $requete .= 'FROM'; - $requete .= ' compta '; - - return $this->_bdd->obtenirTous($requete); - } - - public function obtenirEvenementParIdForum($id) - { - $requete = 'SELECT '; - $requete .= ' compta_evenement.id '; - $requete .= 'FROM '; - $requete .= ' compta_evenement '; - $requete .= 'INNER JOIN '; - $requete .= ' afup_forum on afup_forum.titre = compta_evenement.evenement '; - $requete .= 'WHERE '; - $requete .= ' afup_forum.id = ' . (int) $id; - return $this->_bdd->obtenirUn($requete); - } - - public function extraireComptaDepuisCSVBanque(Importer $importer): bool - { - if (!$importer->validate()) { - return false; - } - - $qualifier = new AutoQualifier($this->obtenirListRegles(true)); - - foreach ($importer->extract() as $operation) { - $numero_operation = $operation->numeroOperation; - // On vérife si l'enregistrement existe déjà - $enregistrement = $this->obtenirParNumeroOperation($numero_operation); - - $operationQualified = $qualifier->qualify($operation); - if (!is_array($enregistrement)) { - $this->ajouter( - $operationQualified['idoperation'], - $importer->getCompteId(), - $operationQualified['categorie'], - $operationQualified['date_ecriture'], - '', - '', - $operationQualified['montant'], - $operationQualified['description'], - '', - $operationQualified['idModeReglement'], - $operationQualified['date_ecriture'], - '', - $operationQualified['evenement'], - $operationQualified['numero_operation'] ?? null, - $operationQualified['attachmentRequired'], - $operationQualified['montant_ht_soumis_tva_0'], - $operationQualified['montant_ht_soumis_tva_5_5'], - $operationQualified['montant_ht_soumis_tva_10'], - $operationQualified['montant_ht_soumis_tva_20'], - ); - } else { - $modifier = false; - if ($enregistrement['idcategorie'] == AutoQualifier::DEFAULT_CATEGORIE && $operationQualified['categorie'] != AutoQualifier::DEFAULT_CATEGORIE) { - $enregistrement['idcategorie'] = $operationQualified['categorie']; - $modifier = true; - } - if ($enregistrement['idevenement'] == AutoQualifier::DEFAULT_EVENEMENT && $operationQualified['evenement'] != AutoQualifier::DEFAULT_EVENEMENT) { - $enregistrement['idevenement'] = $operationQualified['evenement']; - $modifier = true; - } - if ($modifier) { - $this->modifier($enregistrement['id'], - $enregistrement['idoperation'], - $importer->getCompteId(), - $enregistrement['idcategorie'], - $enregistrement['date_ecriture'], - $enregistrement['nom_frs'], - $enregistrement['tva_intra'], - $enregistrement['montant'], - $enregistrement['description'], - $enregistrement['numero'], - $enregistrement['idmode_regl'], - $enregistrement['date_regl'], - $enregistrement['obs_regl'], - $enregistrement['idevenement'], - $enregistrement['numero_operation'], - $operationQualified['attachmentRequired'], - ); - } - } - } - return true; - } - - /** - * Search in whole database - *

We do multiple queries.

- * @param $query string String to search - * @return array Results sorted by type - */ - public function rechercher($query): array - { - $like = $this->_bdd->echapper("%$query%"); - $results = []; - - // "cotisations" for companies - $select = <<_bdd->obtenirTous($select, MYSQLI_ASSOC)) { - $results['cotisations_personnes_morales'] = $cotisations; - } - - // "cotisations" for people - $select = <<_bdd->obtenirTous($select, MYSQLI_ASSOC)) { - $results['cotisations_personnes_physiques'] = $cotisations; - } - - // Forum registrations - $select = <<_bdd->obtenirTous($select, MYSQLI_ASSOC)) { - $results['forum_inscriptions'] = $registrations; - } - - // Forum invoicing - $select = <<_bdd->obtenirTous($select, MYSQLI_ASSOC)) { - $results['forum_factures'] = $invoices; - } - - // Global invoicing - $select = << 0 - WHERE - inv.numero_devis LIKE $like - OR inv.numero_facture LIKE $like - OR inv.societe LIKE $like - OR inv.service LIKE $like - OR inv.email LIKE $like - OR inv.ref_clt1 LIKE $like - OR inv.ref_clt2 LIKE $like - OR inv.ref_clt3 LIKE $like - OR inv.observation LIKE $like - GROUP BY inv.id - ; -SQL; - if ($invoices = $this->_bdd->obtenirTous($select, MYSQLI_ASSOC)) { - $results['factures'] = $invoices; - } - - return $results; - } - - public function obtenirListRegles($filtre = '', $where = '') - { - $requete = 'SELECT '; - $requete .= '`id`, `label`, `condition`, `is_credit`, `vat`, `category_id`, `event_id`, `mode_regl_id`, `attachment_required` '; - $requete .= 'FROM '; - $requete .= 'compta_regle '; - $wheres = []; - if ($where) { - $wheres[] = 'id=' . intval($where) . ' '; - } - - if ($wheres !== []) { - $requete .= sprintf('WHERE %s ',implode(' AND ', $wheres)); - } - - $requete .= 'ORDER BY '; - $requete .= 'label '; - - if ($where) { - return $this->_bdd->obtenirEnregistrement($requete); - } elseif ($filtre) { - return $this->_bdd->obtenirTous($requete); - } - return null; - } - - public function ajouterRegle($label, $condition, $is_credit, $tva, $category_id, $event_id, $mode_regl_id, $attachment_required) - { - $requete = 'INSERT INTO '; - $requete .= 'compta_regle ('; - $requete .= '`label`, `condition`, `is_credit`, `vat`, `category_id`, `event_id`, `mode_regl_id`, `attachment_required`) '; - $requete .= 'VALUES ('; - $requete .= $this->_bdd->echapper($label) . ', '; - $requete .= $this->_bdd->echapper($condition) . ', '; - $requete .= $this->_bdd->echapper($is_credit) . ', '; - $requete .= $this->_bdd->echapper($tva) . ', '; - $requete .= $this->_bdd->echapper($category_id) . ', '; - $requete .= $this->_bdd->echapper($event_id) . ', '; - $requete .= $this->_bdd->echapper($mode_regl_id) . ', '; - $requete .= $this->_bdd->echapper($attachment_required) . ' '; - $requete .= ');'; - - return $this->_bdd->executer($requete); - } - - public function modifierRegle($id, $label, $condition, $is_credit, $tva, $category_id, $event_id, $mode_regl_id, $attachment_required) - { - $requete = 'UPDATE '; - $requete .= 'compta_regle '; - $requete .= 'SET '; - $requete .= '`label` = ' . $this->_bdd->echapper($label) . ', '; - $requete .= '`condition` = ' . $this->_bdd->echapper($condition) . ', '; - $requete .= '`is_credit` = ' . $this->_bdd->echapper($is_credit) . ', '; - $requete .= '`vat` = ' . $this->_bdd->echapper($tva) . ', '; - $requete .= '`category_id` = ' . $this->_bdd->echapper($category_id) . ', '; - $requete .= '`event_id` = ' . $this->_bdd->echapper($event_id) . ', '; - $requete .= '`mode_regl_id` = ' . $this->_bdd->echapper($mode_regl_id) . ', '; - $requete .= '`attachment_required` = ' . $this->_bdd->echapper($attachment_required) . ' '; - $requete .= 'WHERE '; - $requete .= 'id = ' . intval($id) . ' '; - - return $this->_bdd->executer($requete); - } } diff --git a/sources/AppBundle/Compta/CsvExtractor.php b/sources/AppBundle/Compta/CsvExtractor.php new file mode 100644 index 000000000..2a1e6cac5 --- /dev/null +++ b/sources/AppBundle/Compta/CsvExtractor.php @@ -0,0 +1,77 @@ +validate()) { + return false; + } + + $rules = $this->ruleRepository->findAll(); + $qualifier = new AutoQualifier($rules); + + foreach ($importer->extract() as $operation) { + $numero_operation = $operation->numeroOperation; + // On vérife si l'enregistrement existe déjà + $enregistrement = $this->transactionRepository->getOneBy(['operationNumber' => $numero_operation]); + + $operationQualified = $qualifier->qualify($operation); + if (!$enregistrement instanceof Transaction) { + $transaction = new Transaction(); + $transaction->setOperationId($operationQualified['idoperation']) + ->setAccountId($importer->getCompteId()) + ->setCategoryId($operationQualified['categorie']) + ->setAccountingDate(new \DateTime($operationQualified['date_ecriture'])) + ->setVendorName('') + ->setTvaIntra('') + ->setAmount($operationQualified['montant']) + ->setDescription($operationQualified['description']) + ->setNumber('') + ->setPaymentTypeId($operationQualified['idModeReglement']) + ->setAccountingDate(new \DateTime($operationQualified['date_ecriture'])) + ->setPaymentComment('') + ->setEventId($operationQualified['evenement']) + ->setOperationNumber($operationQualified['numero_operation'] ?? null) + ->setAttachmentRequired($operationQualified['attachmentRequired']) + ->setAmountTva0($operationQualified['montant_ht_soumis_tva_0']) + ->setAmountTva55($operationQualified['montant_ht_soumis_tva_5_5']) + ->setAmountTva10($operationQualified['montant_ht_soumis_tva_10']) + ->setAmountTva20($operationQualified['montant_ht_soumis_tva_20']); + $this->transactionRepository->save($transaction); + } else { + $modifier = false; + if ($enregistrement->getCategoryId() == AutoQualifier::DEFAULT_CATEGORIE && $operationQualified['categorie'] != AutoQualifier::DEFAULT_CATEGORIE) { + $enregistrement->setCategoryId($operationQualified['categorie']); + $modifier = true; + } + if ($enregistrement->getEventId() == AutoQualifier::DEFAULT_EVENEMENT && $operationQualified['evenement'] != AutoQualifier::DEFAULT_EVENEMENT) { + $enregistrement->setEventId($operationQualified['evenement']); + $modifier = true; + } + if ($modifier) { + $enregistrement->setAccountId($importer->getCompteId()) + ->setAttachmentRequired($operationQualified['attachmentRequired']); + $this->transactionRepository->save($enregistrement); + } + } + } + + return true; + } +} diff --git a/sources/AppBundle/Compta/Importer/AutoQualifier.php b/sources/AppBundle/Compta/Importer/AutoQualifier.php index 806798e6c..6d73d7014 100644 --- a/sources/AppBundle/Compta/Importer/AutoQualifier.php +++ b/sources/AppBundle/Compta/Importer/AutoQualifier.php @@ -5,6 +5,7 @@ namespace AppBundle\Compta\Importer; use Afup\Site\Utils\Vat; +use AppBundle\Accounting\Entity\Rule; use AppBundle\Model\ComptaModeReglement; class AutoQualifier @@ -14,6 +15,9 @@ class AutoQualifier public const DEFAULT_REGLEMENT = 9; public const DEFAULT_ATTACHMENT = 0; + /** + * @param array $rules + */ public function __construct(protected array $rules) {} public function qualify(Operation $operation): array @@ -54,22 +58,22 @@ public function qualify(Operation $operation): array $operationQualified['montant_ht_soumis_tva_20'] = null; foreach ($this->rules as $rule) { - if (($operation->isCredit() === (bool) $rule['is_credit'] || is_null($rule['is_credit'])) && str_contains($operationQualified['description'], (string) $rule['condition'])) { - if (null !== $rule['event_id']) { - $operationQualified['evenement'] = $rule['event_id']; + if (($operation->isCredit() === (bool) $rule->isCredit || is_null($rule->isCredit)) && str_contains($operationQualified['description'], (string) $rule->condition)) { + if (null !== $rule->event->id) { + $operationQualified['evenement'] = $rule->event->id; } - if (null !== $rule['category_id']) { - $operationQualified['categorie'] = $rule['category_id']; + if (null !== $rule->event->id) { + $operationQualified['categorie'] = $rule->category->id; } - if (null !== $rule['attachment_required']) { - $operationQualified['attachmentRequired'] = $rule['attachment_required']; + if (null !== $rule->attachmentRequired) { + $operationQualified['attachmentRequired'] = $rule->attachmentRequired; } - if (null !== $rule['mode_regl_id']) { - $operationQualified['idModeReglement'] = $rule['mode_regl_id']; + if (null !== $rule->paymentTypeId) { + $operationQualified['idModeReglement'] = $rule->paymentTypeId; } - if (null !== $rule['vat']) { + if (null !== $rule->vat) { $tx = ['0' => 0, '5_5' => 0.055, '10' => 0.1, '20' => 0.2]; - $operationQualified['montant_ht_soumis_tva_' . $rule['vat']] = Vat::getRoundedWithoutVatPriceFromPriceWithVat($operationQualified['montant'], $tx[$rule['vat']]); + $operationQualified['montant_ht_soumis_tva_' . $rule->vat] = Vat::getRoundedWithoutVatPriceFromPriceWithVat($operationQualified['montant'], $tx[$rule->vat]); } break; } diff --git a/sources/AppBundle/Controller/Admin/Accounting/Journal/ImportAction.php b/sources/AppBundle/Controller/Admin/Accounting/Journal/ImportAction.php index db81f3aac..3e9c7ca09 100644 --- a/sources/AppBundle/Controller/Admin/Accounting/Journal/ImportAction.php +++ b/sources/AppBundle/Controller/Admin/Accounting/Journal/ImportAction.php @@ -4,9 +4,9 @@ namespace AppBundle\Controller\Admin\Accounting\Journal; -use Afup\Site\Comptabilite\Comptabilite; use AppBundle\Accounting\Form\TransactionsImportType; use AppBundle\AuditLog\Audit; +use AppBundle\Compta\CsvExtractor; use AppBundle\Compta\Importer\Factory; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\DependencyInjection\Attribute\Autowire; @@ -18,8 +18,8 @@ class ImportAction extends AbstractController { public function __construct( private readonly Audit $audit, - private readonly Comptabilite $compta, private readonly Factory $importerFactory, + private readonly CsvExtractor $csvExtractor, #[Autowire('%kernel.project_dir%/../tmp/')] private readonly string $uploadDir, ) {} @@ -34,7 +34,7 @@ public function __invoke(Request $request): Response $uploadedFile->move($this->uploadDir, 'banque.csv'); $importer = $this->importerFactory->create($this->uploadDir . 'banque.csv', $form->get('bankAccount')->getData()); - if ($this->compta->extraireComptaDepuisCSVBanque($importer)) { + if ($this->csvExtractor->extract($importer)) { $this->audit->log('Chargement fichier banque'); $this->addFlash('notice', "Le fichier a été importé"); } else { diff --git a/tests/unit/AppBundle/Compta/Importer/AutoQualifierTest.php b/tests/unit/AppBundle/Compta/Importer/AutoQualifierTest.php index 1779b2c69..99163ee03 100644 --- a/tests/unit/AppBundle/Compta/Importer/AutoQualifierTest.php +++ b/tests/unit/AppBundle/Compta/Importer/AutoQualifierTest.php @@ -4,6 +4,9 @@ namespace AppBundle\Tests\Compta\Importer; +use AppBundle\Accounting\Entity\Category; +use AppBundle\Accounting\Entity\Event; +use AppBundle\Accounting\Entity\Rule; use AppBundle\Accounting\OperationType; use AppBundle\Compta\Importer\AutoQualifier; use AppBundle\Compta\Importer\Operation; @@ -99,7 +102,7 @@ public function testQualifier( ?string $expectedHTKey, ): void { $operation = new Operation('2022-02-22', $operationDescription, 100, $operationType, '1234'); - $qualifier = new AutoQualifier($this->fakeBD()); + $qualifier = new AutoQualifier($this->fakeRules()); $actual = $qualifier->qualify($operation); self::assertEquals($expectedCategorie, $actual['categorie']); @@ -112,152 +115,56 @@ public function testQualifier( } } - private function fakeBD(): array + /** + * @return array + */ + private function fakeRules(): array { return [ - [ - 'id' => 1, - 'label' => 'VIR sprd.net', - 'condition' => 'VIR SEPA sprd.net AG', - 'is_credit' => '1', - 'mode_regl_id' => ComptaModeReglement::VIREMENT, - 'vat' => '0', - 'category_id' => ComptaCategorie::GOODIES, - 'event_id' => ComptaEvenement::ASSOCIATION_AFUP, - 'attachment_required' => 1, - ], - [ - 'id' => 2, - 'label' => 'CB COM AFUP', - 'condition' => '*CB COM AFUP ', - 'is_credit' => 0, - 'mode_regl_id' => ComptaModeReglement::PRELEVEMENT, - 'vat' => '5_5', - 'category_id' => ComptaCategorie::FRAIS_DE_COMPTE, - 'event_id' => ComptaEvenement::GESTION, - 'attachment_required' => null, - ], - [ - 'id' => 3, - 'label' => 'COTIS ASSOCIATIS ESSENTIEL', - 'condition' => '* COTIS ASSOCIATIS ESSENTIEL', - 'is_credit' => 0, - 'mode_regl_id' => ComptaModeReglement::PRELEVEMENT, - 'vat' => '10', - 'category_id' => ComptaCategorie::FRAIS_DE_COMPTE, - 'event_id' => ComptaEvenement::GESTION, - 'attachment_required' => null, - ], - [ - 'id' => 4, - 'label' => 'URSSAF', - 'condition' => 'PRLV URSSAF', - 'is_credit' => 0, - 'mode_regl_id' => ComptaModeReglement::PRELEVEMENT, - 'vat' => '20', - 'category_id' => ComptaCategorie::CHARGES_SOCIALES, - 'event_id' => ComptaEvenement::GESTION, - 'attachment_required' => null, - ], - [ - 'id' => 5, - 'label' => 'DGFIP', - 'condition' => 'PRLV B2B DGFIP', - 'is_credit' => 0, - 'mode_regl_id' => ComptaModeReglement::PRELEVEMENT, - 'vat' => null, - 'category_id' => ComptaCategorie::PRELEVEMENT_SOURCE, - 'event_id' => ComptaEvenement::GESTION, - 'attachment_required' => null, - ], - [ - 'id' => 6, - 'label' => 'MALAKOFF HUMANIS', - 'condition' => 'PRLV A3M - RETRAITE - MALAKOFF HUMANIS', - 'is_credit' => 0, - 'mode_regl_id' => ComptaModeReglement::PRELEVEMENT, - 'vat' => null, - 'category_id' => ComptaCategorie::CHARGES_SOCIALES, - 'event_id' => ComptaEvenement::GESTION, - 'attachment_required' => null, - ], - [ - 'id' => 7, - 'label' => 'Online SAS', - 'condition' => 'PRLV Online SAS -', - 'is_credit' => 0, - 'mode_regl_id' => ComptaModeReglement::PRELEVEMENT, - 'vat' => null, - 'category_id' => ComptaCategorie::OUTILS, - 'event_id' => ComptaEvenement::ASSOCIATION_AFUP, - 'attachment_required' => 1, - ], - [ - 'id' => 8, - 'label' => 'meetup.org', - 'condition' => 'CB MEETUP ORG', - 'is_credit' => 0, - 'mode_regl_id' => ComptaModeReglement::CB, - 'vat' => null, - 'category_id' => ComptaCategorie::MEETUP, - 'event_id' => ComptaEvenement::ASSOCIATION_AFUP, - 'attachment_required' => 1, - ], - [ - 'id' => 9, - 'label' => 'POINT TRANSACTION SYSTEM', - 'condition' => 'PRLV POINT TRANSACTION SYSTEM -', - 'is_credit' => 0, - 'mode_regl_id' => ComptaModeReglement::PRELEVEMENT, - 'vat' => null, - 'category_id' => ComptaCategorie::FRAIS_DE_COMPTE, - 'event_id' => ComptaEvenement::GESTION, - 'attachment_required' => 1, - ], - [ - 'id' => 10, - 'label' => 'Mailchimp', - 'condition' => 'CB MAILCHIMP FACT', - 'is_credit' => 0, - 'mode_regl_id' => ComptaModeReglement::CB, - 'vat' => null, - 'category_id' => ComptaCategorie::MAILCHIMP, - 'event_id' => ComptaEvenement::ASSOCIATION_AFUP, - 'attachment_required' => 1, - ], - [ - 'id' => 11, - 'label' => 'AWS', - 'condition' => 'CB AWS EMEA FACT', - 'is_credit' => 0, - 'mode_regl_id' => ComptaModeReglement::CB, - 'vat' => null, - 'category_id' => ComptaCategorie::OUTILS, - 'event_id' => ComptaEvenement::ASSOCIATION_AFUP, - 'attachment_required' => 1, - ], - [ - 'id' => 12, - 'label' => 'gandi.net', - 'condition' => 'CB GANDI FACT', - 'is_credit' => 0, - 'mode_regl_id' => ComptaModeReglement::CB, - 'vat' => null, - 'category_id' => ComptaCategorie::GANDI, - 'event_id' => ComptaEvenement::ASSOCIATION_AFUP, - 'attachment_required' => 1, - ], - [ - 'id' => 13, - 'label' => 'Twilio', - 'condition' => 'CB Twilio', - 'is_credit' => 0, - 'mode_regl_id' => ComptaModeReglement::CB, - 'vat' => null, - 'category_id' => ComptaCategorie::OUTILS, - 'event_id' => ComptaEvenement::ASSOCIATION_AFUP, - 'attachment_required' => 1, - ], + $this->createRule(1, 'VIR sprd.net', 'VIR SEPA sprd.net AG', true, ComptaModeReglement::VIREMENT, '0', ComptaCategorie::GOODIES, ComptaEvenement::ASSOCIATION_AFUP, true), + $this->createRule(2, 'CB COM AFUP', '*CB COM AFUP ', false, ComptaModeReglement::PRELEVEMENT, '5_5', ComptaCategorie::FRAIS_DE_COMPTE, ComptaEvenement::GESTION, null), + $this->createRule(3, 'COTIS ASSOCIATIS ESSENTIEL', '* COTIS ASSOCIATIS ESSENTIEL', false, ComptaModeReglement::PRELEVEMENT, '10', ComptaCategorie::FRAIS_DE_COMPTE, ComptaEvenement::GESTION, null), + $this->createRule(4, 'URSSAF', 'PRLV URSSAF', false, ComptaModeReglement::PRELEVEMENT, '20', ComptaCategorie::CHARGES_SOCIALES, ComptaEvenement::GESTION, null), + $this->createRule(5, 'DGFIP', 'PRLV B2B DGFIP', false, ComptaModeReglement::PRELEVEMENT, null, ComptaCategorie::PRELEVEMENT_SOURCE, ComptaEvenement::GESTION, null), + $this->createRule(6, 'MALAKOFF HUMANIS', 'PRLV A3M - RETRAITE - MALAKOFF HUMANIS', false, ComptaModeReglement::PRELEVEMENT, null, ComptaCategorie::CHARGES_SOCIALES, ComptaEvenement::GESTION, null), + $this->createRule(7, 'Online SAS', 'PRLV Online SAS -', false, ComptaModeReglement::PRELEVEMENT, null, ComptaCategorie::OUTILS, ComptaEvenement::ASSOCIATION_AFUP, true), + $this->createRule(8, 'meetup.org', 'CB MEETUP ORG', false, ComptaModeReglement::CB, null, ComptaCategorie::MEETUP, ComptaEvenement::ASSOCIATION_AFUP, true), + $this->createRule(9, 'POINT TRANSACTION SYSTEM', 'PRLV POINT TRANSACTION SYSTEM -', false, ComptaModeReglement::PRELEVEMENT, null, ComptaCategorie::FRAIS_DE_COMPTE, ComptaEvenement::GESTION, true), + $this->createRule(10, 'Mailchimp', 'CB MAILCHIMP FACT', false, ComptaModeReglement::CB, null, ComptaCategorie::MAILCHIMP, ComptaEvenement::ASSOCIATION_AFUP, true), + $this->createRule(11, 'AWS', 'CB AWS EMEA FACT', false, ComptaModeReglement::CB, null, ComptaCategorie::OUTILS, ComptaEvenement::ASSOCIATION_AFUP, true), + $this->createRule(12, 'gandi.net', 'CB GANDI FACT', false, ComptaModeReglement::CB, null, ComptaCategorie::GANDI, ComptaEvenement::ASSOCIATION_AFUP, true), + $this->createRule(13, 'Twilio', 'CB Twilio', false, ComptaModeReglement::CB, null, ComptaCategorie::OUTILS, ComptaEvenement::ASSOCIATION_AFUP, true), ]; } + + private function createRule( + int $id, + string $label, + string $condition, + ?bool $isCredit, + ?int $paymentTypeId, + ?string $vat, + int $categoryId, + int $eventId, + ?bool $attachmentRequired, + ): Rule { + $category = new Category(); + $category->id = $categoryId; + + $event = new Event(); + $event->id = $eventId; + + $rule = new Rule(); + $rule->id = $id; + $rule->label = $label; + $rule->condition = $condition; + $rule->isCredit = $isCredit; + $rule->paymentTypeId = $paymentTypeId; + $rule->vat = $vat; + $rule->category = $category; + $rule->event = $event; + $rule->attachmentRequired = $attachmentRequired; + + return $rule; + } }