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