NEW Introduce mass action "delete" on sales orders.

This commit is contained in:
Laurent Destailleur 2016-07-23 12:17:47 +02:00
parent 02c573b3cc
commit 08d23b482b
4 changed files with 195 additions and 25 deletions

View File

@ -48,6 +48,8 @@ $langs->load('companies');
$langs->load('compta');
$langs->load('bills');
$action=GETPOST('action','alpha');
$massaction=GETPOST('massaction','alpha');
$orderyear=GETPOST("orderyear","int");
$ordermonth=GETPOST("ordermonth","int");
$orderday=GETPOST("orderday","int");
@ -70,6 +72,7 @@ $search_sale=GETPOST('search_sale','int');
$search_total_ht=GETPOST('search_total_ht','alpha');
$optioncss = GETPOST('optioncss','alpha');
$billed = GETPOST('billed','int');
$toselect = GETPOST('toselect', 'array');
// Security check
$id = (GETPOST('orderid')?GETPOST('orderid','int'):GETPOST('id','int'));
@ -183,6 +186,7 @@ if (GETPOST("button_removefilter_x") || GETPOST("button_removefilter.x") || GETP
$deliveryyear='';
$viewstatut='';
$billed='';
$toselect='';
$search_array_options=array();
}
@ -201,6 +205,148 @@ if (empty($reshook))
$error++;
}
// TODO Use a common inc.php file
if (! $error && $massaction == 'delete' && $user->rights->commande->supprimer)
{
$db->begin();
$objecttmp=new Commande($db);
$nbok = 0;
foreach($toselect as $toselectid)
{
$result=$objecttmp->fetch($toselectid);
if ($result > 0)
{
$result = $objecttmp->delete($user);
if ($result <= 0)
{
setEventMessages($objecttmp->error, $objecttmp->errors, 'errors');
$error++;
break;
}
else $nbok++;
}
else
{
setEventMessages($objecttmp->error, $objecttmp->errors, 'errors');
$error++;
break;
}
}
if (! $error)
{
if ($nbok > 1) setEventMessages($langs->trans("RecordsDeleted", $nbok), null, 'mesgs');
else setEventMessages($langs->trans("RecordDeleted", $nbok), null, 'mesgs');
$db->commit();
}
else
{
$db->rollback();
}
//var_dump($listofobjectthirdparties);exit;
}
if (! $error && $massaction == "builddoc" && $user->rights->commande->lire && ! GETPOST('button_search'))
{
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
$objecttmp=new Commande($db);
$listofobjectid=array();
$listofobjectthirdparties=array();
$listofobjectref=array();
foreach($toselect as $toselectid)
{
$objecttmp=new Commande($db); // must create new instance because instance is saved into $listofobjectref array for future use
$result=$objecttmp->fetch($toselectid);
if ($result > 0)
{
$listoinvoicesid[$toselectid]=$toselectid;
$thirdpartyid=$objecttmp->fk_soc?$objecttmp->fk_soc:$objecttmp->socid;
$listofobjectthirdparties[$thirdpartyid]=$thirdpartyid;
$listofobjectref[$toselectid]=$objecttmp->ref;
}
}
$arrayofinclusion=array();
foreach($listofobjectref as $tmppdf) $arrayofinclusion[]=preg_quote($tmppdf.'.pdf','/');
$listoffiles = dol_dir_list($conf->commande->dir_output,'all',1,implode('|',$arrayofinclusion),'\.meta$|\.png','date',SORT_DESC,0,true);
// build list of files with full path
$files = array();
foreach($listofobjectref as $basename)
{
foreach($listoffiles as $filefound)
{
if (strstr($filefound["name"],$basename))
{
$files[] = $conf->commande->dir_output.'/'.$basename.'/'.$filefound["name"];
break;
}
}
}
// Define output language (Here it is not used because we do only merging existing PDF)
$outputlangs = $langs;
$newlang='';
if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang=GETPOST('lang_id');
if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang=$object->thirdparty->default_lang;
if (! empty($newlang))
{
$outputlangs = new Translate("",$conf);
$outputlangs->setDefaultLang($newlang);
}
// Create empty PDF
$pdf=pdf_getInstance();
if (class_exists('TCPDF'))
{
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
}
$pdf->SetFont(pdf_getPDFFont($outputlangs));
if (! empty($conf->global->MAIN_DISABLE_PDF_COMPRESSION)) $pdf->SetCompression(false);
// Add all others
foreach($files as $file)
{
// Charge un document PDF depuis un fichier.
$pagecount = $pdf->setSourceFile($file);
for ($i = 1; $i <= $pagecount; $i++)
{
$tplidx = $pdf->importPage($i);
$s = $pdf->getTemplatesize($tplidx);
$pdf->AddPage($s['h'] > $s['w'] ? 'P' : 'L');
$pdf->useTemplate($tplidx);
}
}
// Create output dir if not exists
dol_mkdir($diroutputmassaction);
// Save merged file
$filename=strtolower(dol_sanitizeFileName($langs->transnoentities("Orders")));
if ($year) $filename.='_'.$year;
if ($month) $filename.='_'.$month;
if ($pagecount)
{
$now=dol_now();
$file=$diroutputmassaction.'/'.$filename.'_'.dol_print_date($now,'dayhourlog').'.pdf';
$pdf->Output($file,'F');
if (! empty($conf->global->MAIN_UMASK))
@chmod($file, octdec($conf->global->MAIN_UMASK));
$langs->load("exports");
setEventMessages($langs->trans('FileSuccessfullyBuilt',$filename.'_'.dol_print_date($now,'dayhourlog')), null, 'mesgs');
}
else
{
setEventMessages($langs->trans('NoPDFAvailableForDocGenAmongChecked'), null, 'errors');
}
}
}
@ -380,6 +526,8 @@ if ($resql)
$num = $db->num_rows($resql);
$arrayofselected=is_array($toselect)?$toselect:array();
$param='';
if (! empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) $param.='&contextpage='.$contextpage;
if ($limit > 0 && $limit != $conf->liste_limit) $param.='&limit='.$limit;
@ -408,8 +556,14 @@ if ($resql)
if ($val != '') $param.='&search_options_'.$tmpkey.'='.urlencode($val);
}
//$massactionbutton=$form->selectMassAction('', $massaction == 'presend' ? array() : array('presend'=>$langs->trans("SendByMail"), 'builddoc'=>$langs->trans("PDFMerge")));
$arrayofmassactions = array(
//'presend'=>$langs->trans("SendByMail"),
//'builddoc'=>$langs->trans("PDFMerge"),
);
if ($user->rights->commande->supprimer) $arrayofmassactions['delete']=$langs->trans("Delete");
if ($massaction == 'presend') $arrayofmassactions=array();
$massactionbutton=$form->selectMassAction('', $arrayofmassactions);
// Lignes des champs de filtre
print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
if ($optioncss != '') print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
@ -420,7 +574,7 @@ if ($resql)
print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
print '<input type="hidden" name="viewstatut" value="'.$viewstatut.'">';
print_barre_liste($title, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num, $nbtotalofrecords, 'title_commercial.png', 0, '', '', $limit);
print_barre_liste($title, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num, $nbtotalofrecords, 'title_commercial.png', 0, '', '', $limit);
if ($sall)
{
@ -657,7 +811,7 @@ if ($resql)
}
// Action column
print '<td class="liste_titre" align="middle">';
$searchpitco=$form->showFilterAndCheckAddButtons(0);
$searchpitco=$form->showFilterAndCheckAddButtons($massactionbutton?1:0, 'checkforselect', 1);
print $searchpitco;
print '</td>';
@ -1001,7 +1155,14 @@ if ($resql)
}
// Action column
print '<td></td>';
print '<td class="nowrap" align="center">';
if ($massactionbutton)
{
$selected=0;
if (in_array($obj->rowid, $arrayofselected)) $selected=1;
print '<input id="cb'.$obj->rowid.'" class="flat checkforselect" type="checkbox" name="toselect[]" value="'.$obj->rowid.'"'.($selected?' checked="checked"':'').'>';
}
print '</td>';
if (! $i) $totalarray['nbfield']++;
print '</tr>';

View File

@ -59,7 +59,6 @@ $id=(GETPOST('id','int')?GETPOST('id','int'):GETPOST('facid','int')); // For ba
$ref=GETPOST('ref','alpha');
$socid=GETPOST('socid','int');
$action=GETPOST('action','alpha');
$massaction=GETPOST('massaction','alpha');
$show_files=GETPOST('show_files','int');
$confirm=GETPOST('confirm','alpha');
$lineid=GETPOST('lineid','int');
@ -205,12 +204,12 @@ if (GETPOST("button_removefilter_x") || GETPOST("button_removefilter") || GETPOS
$day='';
$year='';
$month='';
$toselect='';
$option='';
$filter='';
$day_lim='';
$year_lim='';
$month_lim='';
$toselect='';
$search_array_options=array();
}
@ -229,6 +228,7 @@ if (empty($reshook))
$error++;
}
// TODO Use a common inc.php file
if (! $error && $massaction == 'confirm_presend')
{
$resaction = '';
@ -522,17 +522,17 @@ if (empty($reshook))
$arrayofinclusion=array();
foreach($listofobjectref as $tmppdf) $arrayofinclusion[]=preg_quote($tmppdf.'.pdf','/');
$factures = dol_dir_list($conf->facture->dir_output,'all',1,implode('|',$arrayofinclusion),'\.meta$|\.png','date',SORT_DESC,0,true);
$listoffiles = dol_dir_list($conf->facture->dir_output,'all',1,implode('|',$arrayofinclusion),'\.meta$|\.png','date',SORT_DESC,0,true);
// liste les fichiers
// build list of files with full path
$files = array();
foreach($listofobjectref as $basename)
{
foreach($factures as $facture)
foreach($listoffiles as $filefound)
{
if (strstr($facture["name"],$basename))
if (strstr($filefound["name"],$basename))
{
$files[] = $conf->facture->dir_output.'/'.$basename.'/'.$facture["name"];
$files[] = $conf->facture->dir_output.'/'.$basename.'/'.$filefound["name"];
break;
}
}
@ -831,7 +831,12 @@ if ($resql)
if ($val != '') $param.='&search_options_'.$tmpkey.'='.urlencode($val);
}
$massactionbutton=$form->selectMassAction('', $massaction == 'presend' ? array() : array('presend'=>$langs->trans("SendByMail"), 'builddoc'=>$langs->trans("PDFMerge")));
$arrayofmassactions=array(
'presend'=>$langs->trans("SendByMail"),
'builddoc'=>$langs->trans("PDFMerge")
);
if ($massaction == 'presend') $arrayofmassactions=array();
$massactionbutton=$form->selectMassAction('', $arrayofmassactions);
$i = 0;
print '<form method="POST" name="searchFormList" action="'.$_SERVER["PHP_SELF"].'">'."\n";
@ -843,7 +848,7 @@ if ($resql)
print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
print '<input type="hidden" name="viewstatut" value="'.$viewstatut.'">';
print_barre_liste($langs->trans('BillsCustomers').' '.($socid?' '.$soc->name:''),$page,$_SERVER["PHP_SELF"],$param,$sortfield,$sortorder,$massactionbutton,$num,$nbtotalofrecords,'title_accountancy.png',0,'','',$limit);
print_barre_liste($langs->trans('BillsCustomers').' '.($socid?' '.$soc->name:''), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num, $nbtotalofrecords, 'title_accountancy.png', 0, '', '', $limit);
if ($massaction == 'presend')
{
@ -1183,7 +1188,7 @@ if ($resql)
}
// Action column
print '<td class="liste_titre" align="middle">';
$searchpitco=$form->showFilterAndCheckAddButtons(1, 'checkforselect', 1);
$searchpitco=$form->showFilterAndCheckAddButtons($massactionbutton?1:0, 'checkforselect', 1);
print $searchpitco;
print '</td>';
print "</tr>\n";
@ -1423,9 +1428,12 @@ if ($resql)
// Action column
print '<td class="nowrap" align="center">';
$selected=0;
if (in_array($obj->facid, $arrayofselected)) $selected=1;
print '<input id="cb'.$obj->facid.'" class="flat checkforselect" type="checkbox" name="toselect[]" value="'.$obj->facid.'"'.($selected?' checked="checked"':'').'>';
if ($massactionbutton)
{
$selected=0;
if (in_array($obj->facid, $arrayofselected)) $selected=1;
print '<input id="cb'.$obj->facid.'" class="flat checkforselect" type="checkbox" name="toselect[]" value="'.$obj->facid.'"'.($selected?' checked="checked"':'').'>';
}
print '</td>' ;
if (! $i) $totalarray['nbfield']++;

View File

@ -5929,7 +5929,7 @@ class Form
/**
* Return HTML to show the search and clear seach button
*
* @param int $addcheckuncheckall Add the check all uncheck all button
* @param int $addcheckuncheckall Add the check all/uncheck all checkbox (use javascript) and code to manage this
* @param string $cssclass CSS class
* @param int $calljsfunction 0=default. 1=call function initCheckForSelect() after changing status of checkboxes
* @return string
@ -5944,8 +5944,7 @@ class Form
if ($addcheckuncheckall)
{
if (! empty($conf->use_javascript_ajax)) $out.='<input type="checkbox" id="checkallactions" name="checkallactions" class="checkallactions">';
}
$out.='<script type="text/javascript">
$out.='<script type="text/javascript">
$(document).ready(function() {
$("#checkallactions").click(function() {
if($(this).is(\':checked\')){
@ -5957,10 +5956,11 @@ class Form
console.log("We uncheck all");
$(".'.$cssclass.'").prop(\'checked\', false);
}'."\n";
if ($calljsfunction) $out.='if (typeof initCheckForSelect == \'function\') { initCheckForSelect(); } else { console.log("No function initCheckForSelect found. Call won\'t done."); }';
$out.=' });
});
</script>';
if ($calljsfunction) $out.='if (typeof initCheckForSelect == \'function\') { initCheckForSelect(); } else { console.log("No function initCheckForSelect found. Call won\'t be done."); }';
$out.=' });
});
</script>';
}
return $out;
}
}

View File

@ -567,6 +567,7 @@ CanBeModifiedIfOk=Can be modified if valid
CanBeModifiedIfKo=Can be modified if not valid
RecordModifiedSuccessfully=Record modified successfully
RecordsModified=%s records modified
RecordsDeleted=%s records deleted
AutomaticCode=Automatic code
FeatureDisabled=Feature disabled
MoveBox=Move widget