Fix compatibility of recurring invoices with postgresql

This commit is contained in:
Laurent Destailleur 2016-09-10 05:26:31 +02:00
parent 7c9996c840
commit 41545e0d60

View File

@ -43,6 +43,7 @@ class FactureRec extends CommonInvoice
public $table_element_line='facturedet_rec';
public $fk_element='fk_facture';
var $entity;
var $number;
var $date;
var $amount;
@ -520,6 +521,7 @@ class FactureRec extends CommonInvoice
{
// Clean parameters
$remise_percent=price2num($remise_percent);
if (empty($remise_percent)) $remise_percent=0;
$qty=price2num($qty);
if (! $qty) $qty=1;
if (! $info_bits) $info_bits=0;
@ -580,12 +582,12 @@ class FactureRec extends CommonInvoice
$sql.= ", ".price2num($txtva);
$sql.= ", ".(! empty($fk_product)?"'".$fk_product."'":"null");
$sql.= ", ".$product_type;
$sql.= ", '".price2num($remise_percent)."'";
$sql.= ", '".price2num($pu_ht)."'";
$sql.= ", ".price2num($remise_percent);
$sql.= ", ".price2num($pu_ht);
$sql.= ", null";
$sql.= ", '".price2num($total_ht)."'";
$sql.= ", '".price2num($total_tva)."'";
$sql.= ", '".price2num($total_ttc)."'";
$sql.= ", ".price2num($total_ht);
$sql.= ", ".price2num($total_tva);
$sql.= ", ".price2num($total_ttc);
$sql.= ", ".$rang;
$sql.= ", ".$special_code;
$sql.= ", ".($fk_unit?"'".$this->db->escape($fk_unit)."'":"null").")";
@ -722,14 +724,16 @@ class FactureRec extends CommonInvoice
}
/**
* Create all recurrents invoices.
* A result may also be provided into this->output
* Create all recurrents invoices (for all entities if multicompany is used).
* A result may also be provided into this->output.
*
* WARNING: This method change context $conf->entity to be in correct context for each recurring invoice found.
*
* @return int 0 if OK, < 0 if KO (this function is used also by cron so only 0 is OK)
*/
function createRecurringInvoices()
{
global $langs, $db, $user;
global $conf, $langs, $db, $user;
$langs->load("bills");
@ -744,6 +748,7 @@ class FactureRec extends CommonInvoice
$sql.= ' WHERE frequency > 0'; // A recurring invoice is an invoice with a frequency
$sql.= " AND (date_when IS NULL OR date_when <= '".$db->idate($today)."')";
$sql.= ' AND (nb_gen_done < nb_gen_max OR nb_gen_max = 0)';
$sql.= $db->order('entity', 'ASC');
//print $sql;exit;
$resql = $db->query($sql);
@ -755,6 +760,8 @@ class FactureRec extends CommonInvoice
if ($num) $this->output.=$langs->trans("FoundXQualifiedRecurringInvoiceTemplate", $num)."\n";
else $this->output.=$langs->trans("NoQualifiedRecurringInvoiceTemplateFound");
$saventity = $conf->entity;
while ($i < $num) // Loop on each template invoice
{
$line = $db->fetch_object($resql);
@ -764,7 +771,10 @@ class FactureRec extends CommonInvoice
$facturerec = new FactureRec($db);
$facturerec->fetch($line->rowid);
dol_syslog("createRecurringInvoices Process invoice template id=".$facturerec->id.", ref=".$facturerec->ref);
// Set entity context
$conf->entity = $facturerec->entity;
dol_syslog("createRecurringInvoices Process invoice template id=".$facturerec->id.", ref=".$facturerec->ref.", entity=".$facturerec->entity);
$error=0;
@ -807,6 +817,8 @@ class FactureRec extends CommonInvoice
$i++;
}
$conf->entity = $saventity; // Restore entity context
}
else dol_print_error($db);
@ -1061,7 +1073,7 @@ class FactureRec extends CommonInvoice
return -1;
}
$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
$sql.= ' SET date_when = "'.$this->db->idate($date).'"';
$sql.= " SET date_when = ".($date ? "'".$this->db->idate($date)."'" : "null");
if ($increment_nb_gen_done>0) $sql.= ', nb_gen_done = nb_gen_done + 1';
$sql.= ' WHERE rowid = '.$this->id;