/[dolibarr]/dolibarr/htdocs/telephonie/script/commission.php
ViewVC logotype

Diff of /dolibarr/htdocs/telephonie/script/commission.php

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.13 by rodolphe, Mon Nov 14 10:25:02 2005 UTC revision 1.14 by rodolphe, Wed Nov 16 12:13:16 2005 UTC
# Line 119  else Line 119  else
119   * Calcul des avances   * Calcul des avances
120   *   *
121   *   *
122   *********************************************************/   ********************************************************/
123  dolibarr_syslog("Calcul avance");  dolibarr_syslog("Calcul avance");
124  $sql = "SELECT rowid, fk_distributeur, fk_contrat, datepo, montant";  $sql = "SELECT rowid, fk_distributeur, fk_contrat, datepo, montant";
125  $sql .= " , avance_pourcent, rem_pour_prev";  $sql .= " , avance_pourcent, rem_pour_prev, rem_pour_autr, mode_paiement";
126  $sql .= " FROM ".MAIN_DB_PREFIX."telephonie_contrat_priseordre";  $sql .= " FROM ".MAIN_DB_PREFIX."telephonie_contrat_priseordre";
127  $sql .= " WHERE date_format(datepo, '%Y%m') = '".$year_prev.$month_prev."'";  $sql .= " WHERE date_format(datepo, '%Y%m') = '".$year_prev.$month_prev."'";
128  $sql .= " AND fk_distributeur > 0";  $sql .= " AND fk_distributeur > 0";
# Line 138  if ( $resql ) Line 138  if ( $resql )
138      {      {
139        $obj = $db->fetch_object($resql);        $obj = $db->fetch_object($resql);
140                
141        $pourcent = $obj->rem_pour_prev;        if ($obj->mode_paiement == 'pre')
142            {
143              $pourcent = $obj->rem_pour_prev;
144            }
145          else
146            {
147              $pourcent = $obj->rem_pour_autr;
148            }
149        $avance_pourcent = $obj->avance_pourcent;        $avance_pourcent = $obj->avance_pourcent;
150                
151        $avance = $obj->montant * 12 * $avance_pourcent * $pourcent;        $avance = $obj->montant * 12 * $avance_pourcent * $pourcent;
# Line 151  if ( $resql ) Line 158  if ( $resql )
158    
159        $sqli = "INSERT INTO ".MAIN_DB_PREFIX."telephonie_commission_avance";        $sqli = "INSERT INTO ".MAIN_DB_PREFIX."telephonie_commission_avance";
160        $sqli .= " (date, fk_distributeur, fk_po,fk_contrat, montant, pourcentage, avance)";        $sqli .= " (date, fk_distributeur, fk_po,fk_contrat, montant, pourcentage, avance)";
161        $sqli .= " VALUES ('".$year.$month."'";        $sqli .= " VALUES ('".$year_prev.$month_prev."'";
162        $sqli .= ",".$obj->fk_distributeur.",".$obj->rowid.",".$obj->fk_contrat;        $sqli .= ",".$obj->fk_distributeur.",".$obj->rowid.",".$obj->fk_contrat;
163        $sqli .= ",".ereg_replace(",",".",$avance);        $sqli .= ",".ereg_replace(",",".",$avance);
164        $sqli .= ",".ereg_replace(",",".",$pourcent);        $sqli .= ",".ereg_replace(",",".",$pourcent);
# Line 209  if ( $resql ) Line 216  if ( $resql )
216      {      {
217        $obj = $db->fetch_object($resql);        $obj = $db->fetch_object($resql);
218        
219        $pourcent = $obj->rem_pour_prev;        if ($obj->mode_paiement == 'pre')
220                  {
221        $comm = round($obj->cout_vente * $pourcent * 0.01, 2) ;            $pourcent = $obj->rem_pour_prev;
222            }
223          else
224            {
225              $pourcent = $obj->rem_pour_autr;
226            }
227    
228          $comm = round($obj->cout_vente * $pourcent * 0.01, 2) ;
229            
230        $sqli = "INSERT INTO ".MAIN_DB_PREFIX."telephonie_commission_conso";        $sqli = "INSERT INTO ".MAIN_DB_PREFIX."telephonie_commission_conso";
231        $sqli .= " (date, fk_distributeur, fk_contrat, fk_ligne, montant, pourcentage, avance)";        $sqli .= " (date, fk_distributeur, fk_contrat, fk_ligne, montant, pourcentage, avance)";
232        $sqli .= " VALUES ('".$year.$month."'";        $sqli .= " VALUES ('".$year_prev.$month_prev."'";
233        $sqli .= ",".$obj->fk_distributeur.",".$obj->fk_contrat.",".$obj->ligne;        $sqli .= ",".$obj->fk_distributeur.",".$obj->fk_contrat.",".$obj->ligne;
234        $sqli .= ",".ereg_replace(",",".",$comm);        $sqli .= ",".ereg_replace(",",".",$comm);
235        $sqli .= ",".ereg_replace(",",".",$pourcent);        $sqli .= ",".ereg_replace(",",".",$pourcent);
236    
237        if ($obj->date_regul < $year.$month)        if ($obj->date_regul < $year_prev.$month_prev)
238          {          {
239            $sqli .= ",0)";            $sqli .= ",0)";
240            $avan = 0;            $avan = 0;
# Line 268  else Line 281  else
281   *   *
282   *   *
283   *********************************************************/   *********************************************************/
   
284  $distri_av = array();  $distri_av = array();
285    
286  $sql = "SELECT distinct fk_distributeur";  $sql = "SELECT distinct fk_distributeur";
# Line 279  $resql = $db->query($sql); Line 291  $resql = $db->query($sql);
291        
292  if ( $resql )  if ( $resql )
293  {  {
294    $num = $db->num_rows($resql);    while ($row = $db->fetch_row($resql))
   $i = 0;  
   
   while ($i < $num)  
295      {      {
       $row = $db->fetch_row($resql);  
   
296        array_push($distri_av, $row[0]);        array_push($distri_av, $row[0]);
       $i++;  
297      }      }
298    $db->free($resql);    $db->free($resql);
299  }  }
# Line 348  foreach ($distri_av as $distributeur_id) Line 354  foreach ($distri_av as $distributeur_id)
354                    fputs($fp, " av regul  po ".substr($space.$rowa[0],-4));                    fputs($fp, " av regul  po ".substr($space.$rowa[0],-4));
355                    fputs($fp, " : ".substr($space.$row[0],-8)."\n");                    fputs($fp, " : ".substr($space.$row[0],-8)."\n");
356    
357                      $sqlir = "INSERT INTO ".MAIN_DB_PREFIX."telephonie_commission_regul";
358                      $sqlir .= " (date, fk_distributeur, fk_contrat, montant, type)";
359                      $sqlir .= " VALUES ('".$year_prev.$month_prev."'";
360                      $sqlir .= ",'".$distributeur_id."','".$row[1];
361                      $sqlir .= "','-".ereg_replace(",",".",$row[0]);
362                      $sqlir .= "','avan')";
363                      $resqlir = $db->query($sqlir);
364                      if (!$resqlir)
365                        {
366                          $error = 32;
367                          dolibarr_syslog("Erreur insertion regul avances (error $error)");
368                          dolibarr_syslog($sqlir);
369                        }
370    
371                    dolibarr_syslog("* Avance ".$row[0] . " statut : ".$row[2]);                    dolibarr_syslog("* Avance ".$row[0] . " statut : ".$row[2]);
372                                        
373                    /* Communications relatives */                    /* Communications relatives */
# Line 368  foreach ($distri_av as $distributeur_id) Line 388  foreach ($distri_av as $distributeur_id)
388                            while ($rowc = $db->fetch_row($resqlc))                            while ($rowc = $db->fetch_row($resqlc))
389                              {                              {
390                                $comm_regul[$distributeur_id] = $comm_regul[$distributeur_id] + $rowc[0];                                $comm_regul[$distributeur_id] = $comm_regul[$distributeur_id] + $rowc[0];
                                 
391                                dolibarr_syslog("* Conso générée ".$rowc[0]);                                dolibarr_syslog("* Conso générée ".$rowc[0]);
392    
393                                  $sqlir = "INSERT INTO ".MAIN_DB_PREFIX."telephonie_commission_regul";
394                                  $sqlir .= " (date, fk_distributeur, fk_contrat, montant, type)";
395                                  $sqlir .= " VALUES ('".$year_prev.$month_prev."'";
396                                  $sqlir .= ",'".$distributeur_id."','".$row[1];
397                                  $sqlir .= "','".ereg_replace(",",".",$rowc[0]);
398                                  $sqlir .= "','comm')";
399                                  $resqlir = $db->query($sqlir);
400                                  if (!$resqlir)
401                                    {
402                                      $error = 31;
403                                      dolibarr_syslog("Erreur insertion regul avances conso (error $error)");
404                                      dolibarr_syslog($sqlir);
405                                    }
406                              }                              }
407                            $db->free($resqlc);                            $db->free($resqlc);
408                          }                          }
# Line 389  foreach ($distri_av as $distributeur_id) Line 422  foreach ($distri_av as $distributeur_id)
422                        fputs($fp, " CON : ".$row[1] . " ANNULE\n");                        fputs($fp, " CON : ".$row[1] . " ANNULE\n");
423    
424                        $sqlc = "UPDATE ".MAIN_DB_PREFIX."telephonie_commission_conso";                        $sqlc = "UPDATE ".MAIN_DB_PREFIX."telephonie_commission_conso";
425                        $sqlc .= " SET annul = '".$year.$month."'";                        $sqlc .= " SET annul = '".$year_prev.$month_prev."'";
426                        $sqlc .= " WHERE fk_contrat = ".$row[1];                        $sqlc .= " WHERE fk_contrat = ".$row[1];
427                        $sqlc .= " AND date <= '".$datup."' AND date >= '".$datdo."'";                        $sqlc .= " AND date <= '".$datup."' AND date >= '".$datdo."'";
428    
# Line 421  foreach ($distri_av as $distributeur_id) Line 454  foreach ($distri_av as $distributeur_id)
454      }      }
455  }  }
456    
   
457  /********************************************************  /********************************************************
458   *   *
459   * Calcul des consos   * Calcul des consos
460   *   *
461   *   *
462   *********************************************************/   *********************************************************/
   
463  $distri_co = array();  $distri_co = array();
464    
465  $sql = "SELECT distinct fk_distributeur";  $sql = "SELECT distinct fk_distributeur";
# Line 439  $resql = $db->query($sql); Line 470  $resql = $db->query($sql);
470        
471  if ( $resql )  if ( $resql )
472  {  {
473    $num = $db->num_rows($resql);    while ($row = $db->fetch_row($resql))
   $i = 0;  
   
   while ($i < $num)  
474      {      {
       $row = $db->fetch_row($resql);  
475        array_push($distri_co, $row[0]);        array_push($distri_co, $row[0]);
       $i++;  
476      }      }
477    $db->free($resql);    $db->free($resql);
478  }  }
# Line 497  foreach ($distri_co as $distributeur_id) Line 523  foreach ($distri_co as $distributeur_id)
523              {              {
524                if ( $rowc = $db->fetch_row($resqlc) )                if ( $rowc = $db->fetch_row($resqlc) )
525                  {                  {
526                    $comm_conso[$distributeur_id] = $comm_conso[$distributeur_id] + $rowc[0];                    $comm_conso[$distributeur_id] = $comm_conso[$distributeur_id] + $rowc[0];              
                     
527                    dolibarr_syslog("** Conso générée ".$rowc[0]);                    dolibarr_syslog("** Conso générée ".$rowc[0]);
528                  }                  }
529                else                else
# Line 523  foreach ($distri_co as $distributeur_id) Line 548  foreach ($distri_co as $distributeur_id)
548      }      }
549  }  }
550    
   
551  /********************************************************  /********************************************************
552   *   *
553   *   *
554   *   *
555   *********************************************************/   ********************************************************/
556    
557  foreach ($distributeurs as $distributeur_id)  foreach ($distributeurs as $distributeur_id)
558  {  {
# Line 552  foreach ($distributeurs as $distributeur Line 576  foreach ($distributeurs as $distributeur
576    fputs($fp, "DIS : ".$distributeur_id);    fputs($fp, "DIS : ".$distributeur_id);
577    fputs($fp, " Comm Conso : ".substr($space.$comm_conso[$distributeur_id],-15)."\n");    fputs($fp, " Comm Conso : ".substr($space.$comm_conso[$distributeur_id],-15)."\n");
578    
   
579    /********************************************************    /********************************************************
580     *     *
581     * Somme des commissions     * Somme des commissions
# Line 563  foreach ($distributeurs as $distributeur Line 586  foreach ($distributeurs as $distributeur
586    $sql = "SELECT sum(montant)";    $sql = "SELECT sum(montant)";
587    $sql .= " FROM ".MAIN_DB_PREFIX."telephonie_commission_avance";    $sql .= " FROM ".MAIN_DB_PREFIX."telephonie_commission_avance";
588    $sql .= " WHERE fk_distributeur = ".$distributeur->id;    $sql .= " WHERE fk_distributeur = ".$distributeur->id;
589    $sql .= " AND date = '".$year.$month."';";    $sql .= " AND date = '".$year_prev.$month_prev."';";
590    
591    $resql = $db->query($sql);    $resql = $db->query($sql);
592        
# Line 578  foreach ($distributeurs as $distributeur Line 601  foreach ($distributeurs as $distributeur
601            fputs($fp, " Avan Regul : ".substr($space."-".$avan_regul[$distributeur_id],-15)."\n");            fputs($fp, " Avan Regul : ".substr($space."-".$avan_regul[$distributeur_id],-15)."\n");
602    
603            $amount = $amount + $row[0];            $amount = $amount + $row[0];
604                        /* commission finale */
605            $sqli = "INSERT INTO ".MAIN_DB_PREFIX."telephonie_commission";            $sqli = "INSERT INTO ".MAIN_DB_PREFIX."telephonie_commission";
606            $sqli .= " (date, fk_distributeur, montant)";            $sqli .= " (date, fk_distributeur, montant)";
607            $sqli .= " VALUES ('".$year_prev.$month_prev."'";            $sqli .= " VALUES ('".$year_prev.$month_prev."'";

Legend:
Removed from v.1.13  
changed lines
  Added in v.1.14

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26