Fix generation of ODT (test if temporary directory is writable)

This commit is contained in:
Laurent Destailleur 2021-06-20 05:11:46 +02:00
parent 1a6647e690
commit e3e2dfd764
26 changed files with 162 additions and 57 deletions

View File

@ -489,7 +489,7 @@ abstract class CommonDocGenerator
$array_key.'_remain_to_pay'=>price2num($object->total_ttc - $remain_to_pay, 'MT')
);
if (method_exists($object, 'getTotalDiscount')) {
if (method_exists($object, 'getTotalDiscount') && in_array(get_class($object), array('Proposal', 'Commande', 'Facture', 'SupplierProposal', 'CommandeFournisseur', 'FactureFournisseur'))) {
$resarray[$array_key.'_total_discount_ht_locale'] = price($object->getTotalDiscount(), 0, $outputlangs);
$resarray[$array_key.'_total_discount_ht'] = price2num($object->getTotalDiscount());
} else {
@ -532,10 +532,10 @@ abstract class CommonDocGenerator
}
// @GS: Calculate total up and total discount percentage
// Note that this added fields correspond to nothing in Dolibarr (Dolibarr manage discount on lines not globally)
// Note that this added fields does not match a field into database in Dolibarr (Dolibarr manage discount on lines not as a global property of object)
$resarray['object_total_up'] = $totalUp;
$resarray['object_total_up_locale'] = price($resarray['object_total_up'], 0, $outputlangs);
if (method_exists($object, 'getTotalDiscount')) {
if (method_exists($object, 'getTotalDiscount') && in_array(get_class($object), array('Proposal', 'Commande', 'Facture', 'SupplierProposal', 'CommandeFournisseur', 'FactureFournisseur'))) {
$totalDiscount = $object->getTotalDiscount();
} else {
$totalDiscount = 0;

View File

@ -4334,37 +4334,41 @@ abstract class CommonObject
/**
* Function that returns the total amount HT of discounts applied for all lines.
*
* @return float
* @return float|string Total amout of discount
*/
public function getTotalDiscount()
{
$total_discount = 0.00;
if (!empty($this->table_element_line) ) {
$total_discount = 0.00;
$sql = "SELECT subprice as pu_ht, qty, remise_percent, total_ht";
$sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element."det";
$sql .= " WHERE ".$this->fk_element." = ".$this->id;
$sql = "SELECT subprice as pu_ht, qty, remise_percent, total_ht";
$sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element_line;
$sql .= " WHERE ".$this->fk_element." = ".$this->id;
dol_syslog(get_class($this).'::getTotalDiscount', LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql) {
$num = $this->db->num_rows($resql);
$i = 0;
while ($i < $num) {
$obj = $this->db->fetch_object($resql);
dol_syslog(get_class($this).'::getTotalDiscount', LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql) {
$num = $this->db->num_rows($resql);
$i = 0;
while ($i < $num) {
$obj = $this->db->fetch_object($resql);
$pu_ht = $obj->pu_ht;
$qty = $obj->qty;
$total_ht = $obj->total_ht;
$pu_ht = $obj->pu_ht;
$qty = $obj->qty;
$total_ht = $obj->total_ht;
$total_discount_line = floatval(price2num(($pu_ht * $qty) - $total_ht, 'MT'));
$total_discount += $total_discount_line;
$total_discount_line = floatval(price2num(($pu_ht * $qty) - $total_ht, 'MT'));
$total_discount += $total_discount_line;
$i++;
$i++;
}
}
//print $total_discount; exit;
return price2num($total_discount);
}
//print $total_discount; exit;
return price2num($total_discount);
return null;
}

View File

@ -186,6 +186,11 @@ class modPhpbarcode extends ModeleBarCode
global $conf, $filebarcode;
dol_mkdir($conf->barcode->dir_temp);
if (!is_writable($conf->barcode->dir_temp)) {
$this->error = "Failed to write in temp directory ".$conf->barcode->dir_temp;
dol_syslog('Error in write_file: '.$this->error, LOG_ERR);
return -1;
}
$file = $conf->barcode->dir_temp.'/barcode_'.$code.'_'.$encoding.'.png';

View File

@ -158,6 +158,12 @@ class modTcpdfbarcode extends ModeleBarCode
global $conf, $_GET;
dol_mkdir($conf->barcode->dir_temp);
if (!is_writable($conf->barcode->dir_temp)) {
$this->error = "Failed to write in temp directory ".$conf->barcode->dir_temp;
dol_syslog('Error in write_file: '.$this->error, LOG_ERR);
return -1;
}
$file = $conf->barcode->dir_temp.'/barcode_'.$code.'_'.$encoding.'.png';
$tcpdfEncoding = $this->getTcpdfEncodingType($encoding);

View File

@ -278,7 +278,11 @@ class doc_generic_bom_odt extends ModelePDFBom
//print "conf->societe->dir_temp=".$conf->societe->dir_temp;
dol_mkdir($conf->bom->dir_temp);
if (!is_writable($conf->bom->dir_temp)) {
$this->error = "Failed to write in temp directory ".$conf->bom->dir_temp;
dol_syslog('Error in write_file: '.$this->error, LOG_ERR);
return -1;
}
// If CUSTOMER contact defined on order, we use it
$usecontact = false;

View File

@ -290,7 +290,11 @@ class doc_generic_order_odt extends ModelePDFCommandes
//print "conf->societe->dir_temp=".$conf->societe->dir_temp;
dol_mkdir($conf->commande->dir_temp);
if (!is_writable($conf->commande->dir_temp)) {
$this->error = "Failed to write in temp directory ".$conf->commande->dir_temp;
dol_syslog('Error in write_file: '.$this->error, LOG_ERR);
return -1;
}
// If CUSTOMER contact defined on order, we use it
$usecontact = false;

View File

@ -278,7 +278,11 @@ class doc_generic_contract_odt extends ModelePDFContract
//print "conf->contrat->dir_temp=".$conf->contrat->dir_temp;
dol_mkdir($conf->contrat->dir_temp);
if (!is_writable($conf->contrat->dir_temp)) {
$this->error = "Failed to write in temp directory ".$conf->contrat->dir_temp;
dol_syslog('Error in write_file: '.$this->error, LOG_ERR);
return -1;
}
// If CUSTOMER contact defined on contract, we use it
$usecontact = false;

View File

@ -290,7 +290,11 @@ class doc_generic_shipment_odt extends ModelePdfExpedition
//print "conf->societe->dir_temp=".$conf->societe->dir_temp;
dol_mkdir($conf->expedition->dir_temp);
if (!is_writable($conf->expedition->dir_temp)) {
$this->error = "Failed to write in temp directory ".$conf->expedition->dir_temp;
dol_syslog('Error in write_file: '.$this->error, LOG_ERR);
return -1;
}
// If SHIPMENT contact defined on invoice, we use it
$usecontact = false;

View File

@ -291,7 +291,11 @@ class doc_generic_invoice_odt extends ModelePDFFactures
//print "conf->societe->dir_temp=".$conf->societe->dir_temp;
dol_mkdir($conf->facture->dir_temp);
if (!is_writable($conf->facture->dir_temp)) {
$this->error = "Failed to write in temp directory ".$conf->facture->dir_temp;
dol_syslog('Error in write_file: '.$this->error, LOG_ERR);
return -1;
}
// If BILLING contact defined on invoice, we use it
$usecontact = false;

View File

@ -279,7 +279,11 @@ class doc_generic_member_odt extends ModelePDFMember
//print "conf->adherent->dir_temp=".$conf->adherent->dir_temp;
dol_mkdir($conf->adherent->dir_temp);
if (!is_writable($conf->adherent->dir_temp)) {
$this->error = "Failed to write in temp directory ".$conf->adherent->dir_temp;
dol_syslog('Error in write_file: '.$this->error, LOG_ERR);
return -1;
}
// If CUSTOMER contact defined on member, we use it
$usecontact = false;

View File

@ -285,7 +285,11 @@ class doc_generic_mo_odt extends ModelePDFMo
//print "conf->societe->dir_temp=".$conf->societe->dir_temp;
dol_mkdir($conf->mrp->dir_temp);
if (!is_writable($conf->mrp->dir_temp)) {
$this->error = "Failed to write in temp directory ".$conf->mrp->dir_temp;
dol_syslog('Error in write_file: '.$this->error, LOG_ERR);
return -1;
}
// If CUSTOMER contact defined on order, we use it
$usecontact = false;
@ -342,11 +346,13 @@ class doc_generic_mo_odt extends ModelePDFMo
dol_syslog($e->getMessage(), LOG_INFO);
return -1;
}
// After construction $odfHandler->contentXml contains content and
// [!-- BEGIN row.lines --]*[!-- END row.lines --] has been replaced by
// [!-- BEGIN lines --]*[!-- END lines --]
//print html_entity_decode($odfHandler->__toString());
//print exit;
/*
// Make substitutions into odt of freetext
@ -394,6 +400,7 @@ class doc_generic_mo_odt extends ModelePDFMo
dol_syslog($e->getMessage(), LOG_INFO);
}
}
// Replace tags of lines
try {
$foundtagforlines = 1;
@ -404,6 +411,7 @@ class doc_generic_mo_odt extends ModelePDFMo
$foundtagforlines = 0;
dol_syslog($e->getMessage(), LOG_INFO);
}
if ($foundtagforlines) {
$linenumber = 0;
foreach ($object->lines as $line) {
@ -446,14 +454,14 @@ class doc_generic_mo_odt extends ModelePDFMo
$parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray);
$reshook = $hookmanager->executeHooks('beforeODTSave', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
*/
// Write new file
if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
try {
$odfHandler->exportAsAttachedPDF($file);
} catch (Exception $e) {
$this->error = $e->getMessage();
dol_syslog($e->getMessage(), LOG_INFO);
dol_syslog('Error in exportAsAttachedPDF: '.$e->getMessage(), LOG_INFO);
return -1;
}
} else {
@ -461,7 +469,7 @@ class doc_generic_mo_odt extends ModelePDFMo
$odfHandler->saveToDisk($file);
} catch (Exception $e) {
$this->error = $e->getMessage();
dol_syslog($e->getMessage(), LOG_INFO);
dol_syslog('Error in saveToDisk: '.$e->getMessage(), LOG_INFO);
return -1;
}
}

View File

@ -302,7 +302,11 @@ class doc_generic_product_odt extends ModelePDFProduct
//print "conf->product->dir_temp=".$conf->product->dir_temp;
dol_mkdir($conf->product->dir_temp);
if (!is_writable($conf->product->dir_temp)) {
$this->error = "Failed to write in temp directory ".$conf->product->dir_temp;
dol_syslog('Error in write_file: '.$this->error, LOG_ERR);
return -1;
}
// If CUSTOMER contact defined on product, we use it
$usecontact = false;

View File

@ -574,6 +574,11 @@ class doc_generic_project_odt extends ModelePDFProjects
//print "conf->societe->dir_temp=".$conf->societe->dir_temp;
dol_mkdir($conf->projet->dir_temp);
if (!is_writable($conf->projet->dir_temp)) {
$this->error = "Failed to write in temp directory ".$conf->projet->dir_temp;
dol_syslog('Error in write_file: '.$this->error, LOG_ERR);
return -1;
}
// If PROJECTLEADER contact defined on project, we use it
$usecontact = false;

View File

@ -524,6 +524,11 @@ class doc_generic_task_odt extends ModelePDFTask
//print "conf->societe->dir_temp=".$conf->societe->dir_temp;
dol_mkdir($conf->projet->dir_temp);
if (!is_writable($conf->task->dir_temp)) {
$this->error = "Failed to write in temp directory ".$conf->task->dir_temp;
dol_syslog('Error in write_file: '.$this->error, LOG_ERR);
return -1;
}
$socobject = $project->thirdparty;

View File

@ -318,7 +318,11 @@ class doc_generic_proposal_odt extends ModelePDFPropales
//print "conf->propal->dir_temp=".$conf->propal->dir_temp;
dol_mkdir($conf->propal->multidir_temp[$object->entity]);
if (!is_writable($conf->propal->dir_temp)) {
$this->error = "Failed to write in temp directory ".$conf->propal->dir_temp;
dol_syslog('Error in write_file: '.$this->error, LOG_ERR);
return -1;
}
// If CUSTOMER contact defined on proposal, we use it
$usecontact = false;

View File

@ -282,7 +282,11 @@ class doc_generic_reception_odt extends ModelePdfReception
//print "conf->societe->dir_temp=".$conf->societe->dir_temp;
dol_mkdir($conf->reception->dir_temp);
if (!is_writable($conf->reception->dir_temp)) {
$this->error = "Failed to write in temp directory ".$conf->reception->dir_temp;
dol_syslog('Error in write_file: '.$this->error, LOG_ERR);
return -1;
}
// If BILLING contact defined on invoice, we use it
$usecontact = false;

View File

@ -266,6 +266,11 @@ class doc_generic_odt extends ModeleThirdPartyDoc
//exit;
dol_mkdir($conf->societe->multidir_temp[$object->entity]);
if (!is_writable($conf->societe->multidir_temp[$object->entity])) {
$this->error = "Failed to write in temp directory ".$conf->societe->multidir_temp[$object->entity];
dol_syslog('Error in write_file: '.$this->error, LOG_ERR);
return -1;
}
// Open and load template
require_once ODTPHP_PATH.'odf.php';

View File

@ -291,7 +291,11 @@ class doc_generic_stock_odt extends ModelePDFStock
//print "conf->product->dir_temp=".$conf->product->dir_temp;
dol_mkdir($conf->product->dir_temp);
if (!is_writable($conf->product->dir_temp)) {
$this->error = "Failed to write in temp directory ".$conf->product->dir_temp;
dol_syslog('Error in write_file: '.$this->error, LOG_ERR);
return -1;
}
// If CUSTOMER contact defined on stock, we use it
$usecontact = false;

View File

@ -280,8 +280,12 @@ class doc_generic_supplier_order_odt extends ModelePDFSuppliersOrders
//print "file=".$file;
//print "conf->societe->dir_temp=".$conf->societe->dir_temp;
dol_mkdir($conf->commande->dir_temp);
dol_mkdir($conf->fournisseur->commande->dir_temp);
if (!is_writable($conf->fournisseur->commande->dir_temp)) {
$this->error = "Failed to write in temp directory ".$conf->fournisseur->commande->dir_temp;
dol_syslog('Error in write_file: '.$this->error, LOG_ERR);
return -1;
}
// If CUSTOMER contact defined on order, we use it
$usecontact = false;

View File

@ -315,7 +315,11 @@ class doc_generic_supplier_proposal_odt extends ModelePDFSupplierProposal
//print "conf->propal->dir_temp=".$conf->propal->dir_temp;
dol_mkdir($conf->supplier_proposal->dir_temp);
if (!is_writable($conf->supplier_proposal->dir_temp)) {
$this->error = "Failed to write in temp directory ".$conf->supplier_proposal->dir_temp;
dol_syslog('Error in write_file: '.$this->error, LOG_ERR);
return -1;
}
// If BILLING contact defined on invoice, we use it
$usecontact = false;

View File

@ -222,7 +222,7 @@ class doc_generic_ticket_odt extends ModelePDFTicket
// Load translation files required by the page
$outputlangs->loadLangs(array("main", "companies", "bills", "dict"));
if ($conf->user->dir_output) {
if ($conf->ticket->dir_output) {
// If $object is id instead of object
if (!is_object($object)) {
$id = $object;
@ -236,7 +236,7 @@ class doc_generic_ticket_odt extends ModelePDFTicket
$object->fetch_thirdparty();
$dir = $conf->user->dir_output;
$dir = $conf->ticket->dir_output;
$objectref = dol_sanitizeFileName($object->ref);
if (!preg_match('/specimen/i', $objectref)) {
$dir .= "/".$objectref;
@ -274,10 +274,14 @@ class doc_generic_ticket_odt extends ModelePDFTicket
//print "newdir=".$dir;
//print "newfile=".$newfile;
//print "file=".$file;
//print "conf->user->dir_temp=".$conf->user->dir_temp;
dol_mkdir($conf->user->dir_temp);
//print "conf->ticket->dir_temp=".$conf->ticket->dir_temp;
dol_mkdir($conf->ticket->dir_temp);
if (!is_writable($conf->ticket->dir_temp)) {
$this->error = "Failed to write in temp directory ".$conf->ticket->dir_temp;
dol_syslog('Error in write_file: '.$this->error, LOG_ERR);
return -1;
}
// If CUSTOMER contact defined on user, we use it
$usecontact = false;
@ -306,7 +310,7 @@ class doc_generic_ticket_odt extends ModelePDFTicket
$odfHandler = new odf(
$srctemplatepath,
array(
'PATH_TO_TMP' => $conf->user->dir_temp,
'PATH_TO_TMP' => $conf->ticket->dir_temp,
'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy.
'DELIMITER_LEFT' => '{',
'DELIMITER_RIGHT' => '}'

View File

@ -309,7 +309,11 @@ class doc_generic_user_odt extends ModelePDFUser
//print "conf->user->dir_temp=".$conf->user->dir_temp;
dol_mkdir($conf->user->dir_temp);
if (!is_writable($conf->user->dir_temp)) {
$this->error = "Failed to write in temp directory ".$conf->user->dir_temp;
dol_syslog('Error in write_file: '.$this->error, LOG_ERR);
return -1;
}
// If CUSTOMER contact defined on user, we use it
$usecontact = false;

View File

@ -299,7 +299,11 @@ class doc_generic_usergroup_odt extends ModelePDFUserGroup
//print "conf->user->dir_temp=".$conf->user->dir_temp;
dol_mkdir($conf->user->dir_temp);
if (!is_writable($conf->user->dir_temp)) {
$this->error = "Failed to write in temp directory ".$conf->user->dir_temp;
dol_syslog('Error in write_file: '.$this->error, LOG_ERR);
return -1;
}
// If CUSTOMER contact defined on user, we use it
$usecontact = false;

View File

@ -284,7 +284,11 @@ class doc_generic_myobject_odt extends ModelePDFMyObject
//print "conf->societe->dir_temp=".$conf->societe->dir_temp;
dol_mkdir($conf->mymodule->dir_temp);
if (!is_writable($conf->mymodule->dir_temp)) {
$this->error = "Failed to write in temp directory ".$conf->mymodule->dir_temp;
dol_syslog('Error in write_file: '.$this->error, LOG_ERR);
return -1;
}
// If CUSTOMER contact defined on order, we use it
$usecontact = false;

View File

@ -78,7 +78,7 @@ function moPrepareHead($object)
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
$upload_dir = $conf->mrp->dir_output."/mo/".dol_sanitizeFileName($object->ref);
$upload_dir = $conf->mrp->dir_output."/".dol_sanitizeFileName($object->ref);
$nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
$nbLinks = Link::count($db, $object->element, $object->id);
$head[$h][0] = dol_buildpath("/mrp/mo_document.php", 1).'?id='.$object->id;

View File

@ -138,6 +138,16 @@ if (empty($reshook)) {
// Actions when printing a doc from card
include DOL_DOCUMENT_ROOT.'/core/actions_printing.inc.php';
// Action to build doc
include DOL_DOCUMENT_ROOT.'/core/actions_builddoc.inc.php';
if ($action == 'set_thirdparty' && $permissiontoadd) {
$object->setValueFrom('fk_soc', GETPOST('fk_soc', 'int'), '', '', 'date', '', $user, 'MO_MODIFY');
}
if ($action == 'classin' && $permissiontoadd) {
$object->setProject(GETPOST('projectid', 'int'));
}
// Actions to send emails
$triggersendname = 'MO_SENTBYMAIL';
$autocopy = 'MAIN_MAIL_AUTOCOPY_MO_TO';
@ -147,13 +157,6 @@ if (empty($reshook)) {
// Action to move up and down lines of object
//include DOL_DOCUMENT_ROOT.'/core/actions_lineupdown.inc.php'; // Must be include, not include_once
if ($action == 'set_thirdparty' && $permissiontoadd) {
$object->setValueFrom('fk_soc', GETPOST('fk_soc', 'int'), '', '', 'date', '', $user, 'MO_MODIFY');
}
if ($action == 'classin' && $permissiontoadd) {
$object->setProject(GETPOST('projectid', 'int'));
}
// Action close produced
if ($action == 'confirm_produced' && $confirm == 'yes' && $permissiontoadd) {
$result = $object->setStatut($object::STATUS_PRODUCED, 0, '', 'MRP_MO_PRODUCED');