fix correction fonction fec

This commit is contained in:
Tolotsoa 2025-09-19 16:17:59 +03:00
parent ebd98b58d2
commit 54f80a72f4
2 changed files with 236 additions and 227 deletions

View File

@ -2280,12 +2280,14 @@ class PageController extends Controller
* @NoAdminRequired * @NoAdminRequired
* @NoCSRFRequired * @NoCSRFRequired
*/ */
public function apiReloadFec() { public function apiReloadFec()
{
$this->reloadFec(); $this->reloadFec();
return new DataResponse("", 200, ['Content-Type' => 'application/json']); return new DataResponse("", 200, ['Content-Type' => 'application/json']);
} }
private function reloadFec(){ private function reloadFec()
{
$current_config = json_decode($this->myDb->getConfiguration($this->idNextcloud)); $current_config = json_decode($this->myDb->getConfiguration($this->idNextcloud));
$clean_folder = html_entity_decode($current_config[0]->path).'/'; $clean_folder = html_entity_decode($current_config[0]->path).'/';
@ -2297,6 +2299,11 @@ class PageController extends Controller
foreach ($factures as $key => $facture) { foreach ($factures as $key => $facture) {
$factureIsSingle = $facture->facture_type == FactureTypeConstant::TYPE_SINGLE; $factureIsSingle = $facture->facture_type == FactureTypeConstant::TYPE_SINGLE;
// Logique TVA : si tva = 0 alors 0%, si tva = 1 alors taux par défaut
$tva_rate = 0; // Par défaut
if (isset($facture->tva) && $facture->tva == 1) {
$tva_rate = isset($current_config[0]->tva_default) ? floatval($current_config[0]->tva_default) : 0;
}
$facture_temp = array( $facture_temp = array(
'num' => $facture->num, 'num' => $facture->num,
@ -2306,41 +2313,29 @@ class PageController extends Controller
'date_facture' => $facture->date_paiement, 'date_facture' => $facture->date_paiement,
'defunt' => $facture->nom_defunt, 'defunt' => $facture->nom_defunt,
'montant_htc' => 0, 'montant_htc' => 0,
'tva' => $current_config[0]->tva_default, 'tva' => $tva_rate, // Utiliser le taux calculé
'montant_tva' => 0, 'montant_tva' => 0,
'montant_ttc' => 0, 'montant_ttc' => 0,
); );
if($factureIsSingle) { if($factureIsSingle) {
$produits = json_decode($this->getProduitsById($facture->id_devis)); $produits = json_decode($this->getProduitsById($facture->id_devis));
if ($produits) {
foreach ($produits as $key => $produit) { foreach ($produits as $key => $produit) {
$htPrice = $produit->prix_unitaire; $htPrice = isset($produit->prix_unitaire) ? floatval($produit->prix_unitaire) : 0;
if($facture->fk_client_group_id != null || $facture->fk_client_group_id != 0) { if($facture->fk_client_group_id != null || $facture->fk_client_group_id != 0) {
$price = $this->myDb->getProductPriceByClientGroupId($facture->fk_client_group_id, $produit->id); $price = $this->myDb->getProductPriceByClientGroupId($facture->fk_client_group_id, $produit->id);
if($price != null) { if($price != null) {
$htPrice = $price; $htPrice = floatval($price);
}
}
$quantite = isset($produit->quantite) ? floatval($produit->quantite) : 0;
$facture_temp['montant_htc'] += $htPrice * $quantite;
} }
} }
$facture_temp['montant_htc'] += $htPrice * $produit->quantite;
};
$facture_temp['montant_tva'] = ($facture_temp['montant_htc'] * $facture_temp['tva']) / 100; $facture_temp['montant_tva'] = ($facture_temp['montant_htc'] * $facture_temp['tva']) / 100;
$facture_temp['montant_ttc'] = $facture_temp['montant_tva'] + $facture_temp['montant_htc']; $facture_temp['montant_ttc'] = $facture_temp['montant_tva'] + $facture_temp['montant_htc'];
} else { } else {
$devis = $this->myDb->getDevisByFkFactureId($facture->id); $devis = $this->myDb->getDevisByFkFactureId($facture->id);
$factureGroupIsRelatedToAnyDevis = $devis != null; $factureGroupIsRelatedToAnyDevis = $devis != null;
$mentionFilters = [ $mentionFilters = [
@ -2348,7 +2343,6 @@ class PageController extends Controller
DevisMentionConstant::FACTURED_FORMATTED DevisMentionConstant::FACTURED_FORMATTED
]; ];
$isFactureGroupForSingleClient = $facture->facture_client_id != null && $facture->facture_client_id != 0; $isFactureGroupForSingleClient = $facture->facture_client_id != null && $facture->facture_client_id != 0;
if($isFactureGroupForSingleClient) { if($isFactureGroupForSingleClient) {
$facture_temp["client"] = $facture->facture_client_entreprise; $facture_temp["client"] = $facture->facture_client_entreprise;
@ -2383,51 +2377,52 @@ class PageController extends Controller
$devisList = $this->myDb->getDevisByClientIdByFactureId($facture->id, $mentionFilters); $devisList = $this->myDb->getDevisByClientIdByFactureId($facture->id, $mentionFilters);
} }
if (isset($devisList)) {
foreach($devisList as $currentDevis) { foreach($devisList as $currentDevis) {
$produits = json_decode($this->getProduitsById($currentDevis['id'])); $produits = json_decode($this->getProduitsById($currentDevis['id']));
if ($produits) {
foreach ($produits as $key => $produit) { foreach ($produits as $key => $produit) {
$htPrice = $produit->prix_unitaire; $htPrice = isset($produit->prix_unitaire) ? floatval($produit->prix_unitaire) : 0;
if($currentDevis["fk_client_group_id"] != null || $currentDevis["fk_client_group_id"] != 0) { if($currentDevis["fk_client_group_id"] != null || $currentDevis["fk_client_group_id"] != 0) {
$price = $this->myDb->getProductPriceByClientGroupId($currentDevis["fk_client_group_id"], $produit->id); $price = $this->myDb->getProductPriceByClientGroupId($currentDevis["fk_client_group_id"], $produit->id);
if($price != null) { if($price != null) {
$htPrice = $price; $htPrice = floatval($price);
}
}
$quantite = isset($produit->quantite) ? floatval($produit->quantite) : 0;
$facture_temp['montant_htc'] += $htPrice * $quantite;
}
} }
} }
$facture_temp['montant_htc'] += $htPrice * $produit->quantite;
};
} }
$facture_temp['montant_tva'] = ($facture_temp['montant_htc'] * $facture_temp['tva']) / 100; $facture_temp['montant_tva'] = ($facture_temp['montant_htc'] * $facture_temp['tva']) / 100;
$facture_temp['montant_ttc'] = $facture_temp['montant_tva'] + $facture_temp['montant_htc']; $facture_temp['montant_ttc'] = $facture_temp['montant_tva'] + $facture_temp['montant_htc'];
} }
array_push($data_factures, $facture_temp); array_push($data_factures, $facture_temp);
}; }
$data_temp = array(); $data_temp = array();
foreach ($data_factures as $key => $facture) { foreach ($data_factures as $key => $facture) {
if (isset($facture['date_facture']) && !empty($facture['date_facture'])) {
$datesplit = explode('-', $facture['date_facture']); $datesplit = explode('-', $facture['date_facture']);
if($data_temp[strval($datesplit[0])] == null) { if (count($datesplit) >= 2) {
$data_temp[strval($datesplit[0])][strval($datesplit[1])] = array(0 => $facture); $annee = strval($datesplit[0]);
$mois = strval($datesplit[1]);
if($data_temp[$annee] == null) {
$data_temp[$annee][$mois] = array(0 => $facture);
} else { } else {
if($data_temp[strval($datesplit[0])][strval($datesplit[1])] == null) { if($data_temp[$annee][$mois] == null) {
$data_temp[strval($datesplit[0])][strval($datesplit[1])] = array(0 => $facture); $data_temp[$annee][$mois] = array(0 => $facture);
} else { } else {
array_push($data_temp[strval($datesplit[0])][strval($datesplit[1])], $facture); array_push($data_temp[$annee][$mois], $facture);
} }
} }
} }
}
}
$fec_headers_txt = [ $fec_headers_txt = [
utf8_decode('JOURNALCODE'), utf8_decode('JOURNALCODE'),
utf8_decode('JOURNALLIB'), utf8_decode('JOURNALLIB'),
@ -2448,16 +2443,18 @@ class PageController extends Controller
utf8_decode('MONTANTDEVISE'), utf8_decode('MONTANTDEVISE'),
utf8_decode('IDEVISE'), utf8_decode('IDEVISE'),
]; ];
//parcours annee //parcours annee
foreach ($data_temp as $key_annee => $annee) { foreach ($data_temp as $key_annee => $annee) {
//parcours annee //parcours annee
$_clean_folder = $clean_folder.'FEC/'.$key_annee.'/'; $_clean_folder = $clean_folder.'FEC/'.$key_annee.'/';
try { try {
$this->storage->newFolder($_clean_folder); $this->storage->newFolder($_clean_folder);
} catch(\OCP\Files\NotPermittedException $e) { } } catch(\OCP\Files\NotPermittedException $e) {
}
foreach ($annee as $key_mois => $mois) { foreach ($annee as $key_mois => $mois) {
// Initialize FEC headers // Initialize FEC headers
$fec_temp_txt = implode(TAB1, $fec_headers_txt) . PHP_EOL . PHP_EOL; $fec_temp_txt = implode(TAB1, $fec_headers_txt) . PHP_EOL . PHP_EOL;
$fec_temp = implode(';', $fec_headers_txt) . "\n\n"; $fec_temp = implode(';', $fec_headers_txt) . "\n\n";
@ -2559,13 +2556,14 @@ class PageController extends Controller
$this->storage->newFile($ff_txt); $this->storage->newFile($ff_txt);
$file_txt = $this->storage->get($ff_txt); $file_txt = $this->storage->get($ff_txt);
$file_txt->putContent($fec_temp_txt); $file_txt->putContent($fec_temp_txt);
// $file->putContent(implode(';', array('Jane Smith', 'janesmith@example.com', '555-5678')) . "\n");
} }
} }
} catch(\OCP\Files\NotFoundException $e) { } } catch(\OCP\Files\NotFoundException $e) {
}
} catch(\OCP\Files\NotPermittedException $e) { } } catch(\OCP\Files\NotPermittedException $e) {
}
} }
private function refreshFEC() private function refreshFEC()

View File

@ -488,6 +488,13 @@ class Bdd
client.adresse as adresse_cli,client.mail as mail_cli, client.adresse as adresse_cli,client.mail as mail_cli,
client.telephone as telephone_cli,client.legal_one as legalone_cli, client.telephone as telephone_cli,client.legal_one as legalone_cli,
client.fk_client_group_id as fk_client_group_id, client.fk_client_group_id as fk_client_group_id,
COALESCE(
client.is_tva,
CASE
WHEN facture.fk_client_group_facturation_id IS NOT NULL THEN client_group_tva.is_tva
ELSE facture_client.is_tva
END
) as tva,
defunt.id as id_defunt, defunt.id as id_defunt,
defunt.nom as nom_defunt, defunt.nom as nom_defunt,
lieu.id as lid, lieu.id as lid,
@ -502,6 +509,7 @@ class Bdd
LEFT JOIN ".$this->tableprefix."facture_status as facture_status on facture.fk_facture_status_key = facture_status.facture_status_key LEFT JOIN ".$this->tableprefix."facture_status as facture_status on facture.fk_facture_status_key = facture_status.facture_status_key
LEFT JOIN ".$this->tableprefix."client as facture_client on facture.fk_client_id = facture_client.id LEFT JOIN ".$this->tableprefix."client as facture_client on facture.fk_client_id = facture_client.id
LEFT JOIN ".$this->tableprefix."client_group_facturation as facture_client_group_facturation on facture.fk_client_group_facturation_id = facture_client_group_facturation.id LEFT JOIN ".$this->tableprefix."client_group_facturation as facture_client_group_facturation on facture.fk_client_group_facturation_id = facture_client_group_facturation.id
LEFT JOIN ".$this->tableprefix."client as client_group_tva ON facture.fk_client_group_facturation_id = client_group_tva.fk_client_group_facturation_id
ORDER BY facture.id DESC, facture.date_paiement DESC"; ORDER BY facture.id DESC, facture.date_paiement DESC";
$result = $this->execSQL($sql, array()); $result = $this->execSQL($sql, array());
return $result; return $result;
@ -5510,21 +5518,22 @@ COMMENTAIRES: ".$comment;
return $devisList; return $devisList;
} }
public function getDevisIdsGroupByFactureId($factureId,$mentionFilters = []){ public function getDevisIdsGroupByFactureId($factureId, $mentionFilters = [])
{
$sql = "SELECT devis.id $sql = "SELECT devis.id
FROM ".$this->tableprefix."devis as devis FROM ".$this->tableprefix."devis as devis
WHERE devis.fk_facture_id = ? "; WHERE devis.fk_facture_id = ? ";
$conditions = [$factureId]; $conditions = [$factureId];
if(!empty($mentionFilters)){ if(!empty($mentionFilters)) {
$mentionsFilterPlaceholders = implode(',', array_fill(0, count($mentionFilters), '?')); $mentionsFilterPlaceholders = implode(',', array_fill(0, count($mentionFilters), '?'));
$sql .= " AND devis.mentions IN ($mentionsFilterPlaceholders)"; $sql .= " AND devis.mentions IN ($mentionsFilterPlaceholders)";
$conditions = array_merge($conditions, $mentionFilters); $conditions = array_merge($conditions, $mentionFilters);
} }
$sql.= ";"; $sql .= ";";
$devisList = $this->execSQLNoJsonReturn($sql,$conditions); $devisList = $this->execSQLNoJsonReturn($sql, $conditions);
$devisIds = []; $devisIds = [];
foreach($devisList as $devis){ foreach($devisList as $devis) {
$devisIds[] = $devis['id']; $devisIds[] = $devis['id'];
} }
return $devisIds; return $devisIds;
@ -6109,11 +6118,12 @@ COMMENTAIRES: ".$comment;
]; ];
} }
private function getDevisIdsListByFactureId($factureId){ private function getDevisIdsListByFactureId($factureId)
{
$factureData = $this->getFactureByFactureId(factureId: $factureId); $factureData = $this->getFactureByFactureId(factureId: $factureId);
$isFactureGroupOfDevis = $factureData["facture_type"] == FactureTypeConstant::TYPE_GROUP; $isFactureGroupOfDevis = $factureData["facture_type"] == FactureTypeConstant::TYPE_GROUP;
$factureDevisIdsList = []; $factureDevisIdsList = [];
if($isFactureGroupOfDevis){ if($isFactureGroupOfDevis) {
$isFactureForSingleClient = $factureData['fk_client_id'] != null && $factureData['fk_client_id'] != 0; $isFactureForSingleClient = $factureData['fk_client_id'] != null && $factureData['fk_client_id'] != 0;
$devisMentionFilters = [ $devisMentionFilters = [
DevisMentionConstant::FACTURED_FORMATTED, DevisMentionConstant::FACTURED_FORMATTED,
@ -6122,24 +6132,25 @@ COMMENTAIRES: ".$comment;
$devis = $this->getDevisByFkFactureId($factureId); $devis = $this->getDevisByFkFactureId($factureId);
$factureGroupIsRelatedToAnyDevis = $devis != null; $factureGroupIsRelatedToAnyDevis = $devis != null;
if (!$factureGroupIsRelatedToAnyDevis) { if (!$factureGroupIsRelatedToAnyDevis) {
if($isFactureForSingleClient){ if($isFactureForSingleClient) {
$factureDevisIdsList = $this->getDevisIdsByClientIdAndMonthYear( $factureDevisIdsList = $this->getDevisIdsByClientIdAndMonthYear(
$factureData['fk_client_id'], $factureData['fk_client_id'],
$factureData['month'], $factureData['month'],
$factureData['year'],$devisMentionFilters $factureData['year'],
$devisMentionFilters
); );
}else{ } else {
$factureDevisIdsList = $this->getDevisIdsByClientGroupFacturationIdAndMonthYear( $factureDevisIdsList = $this->getDevisIdsByClientGroupFacturationIdAndMonthYear(
$factureData['fk_client_group_facturation_id'], $factureData['fk_client_group_facturation_id'],
$factureData['month'], $factureData['month'],
$factureData['year'],$devisMentionFilters $factureData['year'],
$devisMentionFilters
); );
} }
}else{ } else {
$factureDevisIdsList = $this->getDevisIdsGroupByFactureId($factureId, $devisMentionFilters ); $factureDevisIdsList = $this->getDevisIdsGroupByFactureId($factureId, $devisMentionFilters);
} }
} } else {
else{
$factureDevisIdsList = $factureData['id_devis'] ? [$factureData['id_devis']] : []; $factureDevisIdsList = $factureData['id_devis'] ? [$factureData['id_devis']] : [];
} }
return $factureDevisIdsList; return $factureDevisIdsList;
@ -6151,8 +6162,8 @@ COMMENTAIRES: ".$comment;
$totalHt = 0; $totalHt = 0;
$totalTtc = 0; $totalTtc = 0;
$tva = 0; $tva = 0;
foreach($factureDevisIds as $devisId){ foreach($factureDevisIds as $devisId) {
$clientTvaStatus = $this->getClientTvaStatus($devisId,BddConstant::DEFAULT_ADMIN_ID_NEXTCLOUD); $clientTvaStatus = $this->getClientTvaStatus($devisId, BddConstant::DEFAULT_ADMIN_ID_NEXTCLOUD);
$products = $this->getDevisProduits($devisId); $products = $this->getDevisProduits($devisId);
$totalPrices = $this->getProductsTotalPrices($products); $totalPrices = $this->getProductsTotalPrices($products);
$totalHt += $totalPrices["total_ht"]; $totalHt += $totalPrices["total_ht"];