Work on expense report module

This commit is contained in:
Laurent Destailleur
2015-02-21 16:15:54 +01:00
parent 30a5111c68
commit 6d1324b463
11 changed files with 120 additions and 121 deletions

View File

@@ -210,19 +210,17 @@ if (($action == 'send' || $action == 'relance') && ! $_POST['addfile'] && ! $_PO
else
{
$langs->load("other");
$mesg='<div class="error">';
if ($mailfile->error)
{
$mesg='';
$mesg.=$langs->trans('ErrorFailedToSendMail',$from,$sendto);
$mesg.='<br>'.$mailfile->error;
setEventMessage($mesg,'errors');
}
else
{
$mesg.='No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS';
setEventMessage('No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS', 'warnings');
}
$mesg.='</div>';
setEventMessage($mesg,'warnings');
$action = 'presend';
}
}

View File

@@ -4189,15 +4189,15 @@ function get_date_range($date_start,$date_end,$format = '',$outputlangs='', $wit
if ($date_start && $date_end)
{
$out.= ($withparenthesis?' (':'').$outputlangs->trans('DateFromTo',dol_print_date($date_start, $format, false, $outputlangs),dol_print_date($date_end, $format, false, $outputlangs)).($withparenthesis?')':'');
$out.= ($withparenthesis?' (':'').$outputlangs->transnoentitiesnoconv('DateFromTo',dol_print_date($date_start, $format, false, $outputlangs),dol_print_date($date_end, $format, false, $outputlangs)).($withparenthesis?')':'');
}
if ($date_start && ! $date_end)
{
$out.= ($withparenthesis?' (':'').$outputlangs->trans('DateFrom',dol_print_date($date_start, $format, false, $outputlangs)).($withparenthesis?')':'');
$out.= ($withparenthesis?' (':'').$outputlangs->transnoentitiesnoconv('DateFrom',dol_print_date($date_start, $format, false, $outputlangs)).($withparenthesis?')':'');
}
if (! $date_start && $date_end)
{
$out.= ($withparenthesis?' (':'').$outputlangs->trans('DateUntil',dol_print_date($date_end, $format, false, $outputlangs)).($withparenthesis?')':'');
$out.= ($withparenthesis?' (':'').$outputlangs->transnoentitiesnoconv('DateUntil',dol_print_date($date_end, $format, false, $outputlangs)).($withparenthesis?')':'');
}
return $out;

View File

@@ -162,7 +162,7 @@ class modExpenseReport extends DolibarrModules
$this->rights[6][1] = 'Approve expense reports';
$this->rights[6][2] = 'w';
$this->rights[6][3] = 0;
$this->rights[6][4] = 'to_validate';
$this->rights[6][4] = 'approve';
$this->rights[7][0] = 776;
$this->rights[7][1] = 'Pay expense reports';
@@ -267,7 +267,7 @@ class modExpenseReport extends DolibarrModules
'langs'=>'trips', // Lang file to use (without .lang) by module. File must be in langs/code_CODE/ directory.
'position'=>100,
'enabled'=>'$conf->expensereport->enabled', // Define condition to show or hide menu entry. Use '$conf->mymodule->enabled' if entry must be visible if module is enabled.
'perms'=>'$user->rights->expensereport->lire', // Use 'perms'=>'$user->rights->mymodule->level1->level2' if you want your menu with a permission rules
'perms'=>'$user->rights->expensereport->approve', // Use 'perms'=>'$user->rights->mymodule->level1->level2' if you want your menu with a permission rules
'target'=>'',
'user'=>2); // 0=Menu for internal users, 1=external users, 2=both
$r++;

View File

@@ -70,6 +70,11 @@ if (! empty($conf->multicompany->enabled) && ! empty($conf->entity) && $conf->en
$conf->expensereport->dir_output = $rootfordata.'/expensereport';
$conf->expensereport->dir_output = $rootfordata.'/expensereport';
// Define $urlwithroot
$urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',trim($dolibarr_main_url_root));
$urlwithroot=$urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
//$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
/*
@@ -166,12 +171,9 @@ if ($action == "confirm_save" && GETPOST("confirm") == "yes" && $id > 0 && $user
$object = new ExpenseReport($db);
$object->fetch($id);
$result = $object->setValidate($user);
if ($result > 0)
if ($result > 0 && $object->fk_user_validator > 0)
{
// Send mail
if (! empty($conf->global->DEPLACEMENT_TO_CLEAN))
{
// Send mail
$langs->load("mails");
// TO
$destinataire = new User($db);
@@ -184,17 +186,14 @@ if ($action == "confirm_save" && GETPOST("confirm") == "yes" && $id > 0 && $user
$emailFrom = $expediteur->email;
// SUBJECT
$subject = "' ERP - Note de frais à valider";
$subject = $langs->trans("ExpenseReportWaitingForApproval");
// CONTENT
$message = "Bonjour {$destinataire->firstname},\n\n";
$message.= "Veuillez trouver en pièce jointe une nouvelle note de frais à valider.\n";
$message.= "- Déclarant : {$expediteur->firstname} {$expediteur->lastname}\n";
$message.= "- Période : du {$object->date_debut} au {$object->date_fin}\n";
$message.= "- Lien : {$dolibarr_main_url_root}/expensereport/card.php?id={$object->id}\n\n";
$message.= "Bien cordialement,\n' SI";
$link = $urlwithroot.'/expenserecord/card.php?id='.$object->id;
$message = $langs->trans("ExpenseReportWaitingForApprovalMessage", $expediteur->getFullName($langs), get_date_range($object->date_debut,$object->date_fin,'',$langs), $link);
// Génération du pdf avant attachement
// Rebuild pdf
/*
$object->setDocModel($user,"");
$resultPDF = expensereport_pdf_create($db,$id,'',"",$langs);
@@ -204,29 +203,42 @@ if ($action == "confirm_save" && GETPOST("confirm") == "yes" && $id > 0 && $user
array_push($filename,dol_sanitizeFileName($object->ref).".pdf");
array_push($filedir,$conf->expensereport->dir_output . "/" . dol_sanitizeFileName($object->ref) . "/" . dol_sanitizeFileName($object->ref).".pdf");
array_push($mimetype,"application/pdf");
*/
// PREPARE SEND
$mailfile = new CMailFile($subject,$emailTo,$emailFrom,$message,$filedir,$mimetype,$filename);
if(!$mailfile->error):
if ($mailfile)
{
// SEND
$result=$mailfile->sendfile();
if ($result):
Header("Location: ".$_SEVER["PHP_SELF"]."?id=".$id);
if ($result)
{
$mesg=$langs->trans('MailSuccessfulySent',$mailfile->getValidAddress($emailFrom,2),$mailfile->getValidAddress($emailTo,2));
setEventMessage($mesg);
header("Location: ".$_SEVER["PHP_SELF"]."?id=".$id);
exit;
endif;
else:
$mesg="Impossible d'envoyer l'email.";
endif;
// END - Send mail
else:
dol_print_error($db,$resultPDF);
exit;
endif;
}
else
{
$langs->load("other");
if ($mailfile->error)
{
$mesg='';
$mesg.=$langs->trans('ErrorFailedToSendMail',$from,$sendto);
$mesg.='<br>'.$mailfile->error;
setEventMessage($mesg,'errors');
}
else
{
setEventMessage('No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS', 'warnings');
}
}
}
else
{
setEventMessages($mailfile->error,$mailfile->errors,'errors');
$action='';
}
}
else
@@ -311,7 +323,7 @@ if ($action == "confirm_save_from_refuse" && GETPOST("confirm") == "yes" && $id
}
// Approve
if ($action == "confirm_validate" && GETPOST("confirm") == "yes" && $id > 0 && $user->rights->expensereport->to_validate)
if ($action == "confirm_approve" && GETPOST("confirm") == "yes" && $id > 0 && $user->rights->expensereport->approve)
{
$object = new ExpenseReport($db);
$object->fetch($id);
@@ -394,7 +406,7 @@ if ($action == "confirm_validate" && GETPOST("confirm") == "yes" && $id > 0 && $
}
}
if ($action == "confirm_refuse" && GETPOST('confirm')=="yes" && $id > 0 && $user->rights->expensereport->to_validate)
if ($action == "confirm_refuse" && GETPOST('confirm')=="yes" && $id > 0 && $user->rights->expensereport->approve)
{
$object = new ExpenseReport($db);
$object->fetch($id);
@@ -1120,7 +1132,7 @@ else
endif;
if ($action == 'validate'):
$ret=$form->form_confirm($_SEVER["PHP_SELF"]."?id=".$id,$langs->trans("ValideTrip"),$langs->trans("ConfirmValideTrip"),"confirm_validate","","",1);
$ret=$form->form_confirm($_SEVER["PHP_SELF"]."?id=".$id,$langs->trans("ValideTrip"),$langs->trans("ConfirmValideTrip"),"confirm_approve","","",1);
if ($ret == 'html') print '<br>';
endif;
@@ -1633,7 +1645,7 @@ if ($action != 'create' && $action != 'edit')
}
/* Si l'état est "En attente d'approbation"
* ET user à droit de "to_validate"
* ET user à droit de "approve"
* ET fk_user_validator == user courant
* Afficher : "Valider" / "Refuser" / "Supprimer"
*/
@@ -1646,7 +1658,7 @@ if ($action != 'create' && $action != 'edit')
}
}
if ($user->rights->expensereport->to_validate && $object->fk_c_expensereport_statuts == 2)
if ($user->rights->expensereport->approve && $object->fk_c_expensereport_statuts == 2)
{
//if($object->fk_user_validator==$user->id)
//{
@@ -1692,11 +1704,11 @@ if ($action != 'create' && $action != 'edit')
}
/* Si l'état est "Payée"
* ET user à droit "to_validate"
* ET user à droit "approve"
* ET user à droit "to_paid"
* Afficher : "Annuler"
*/
if ($user->rights->expensereport->to_validate && $user->rights->expensereport->to_paid && $object->fk_c_expensereport_statuts==6)
if ($user->rights->expensereport->approve && $user->rights->expensereport->to_paid && $object->fk_c_expensereport_statuts==6)
{
// Annuler
print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=cancel&id='.$id.'">'.$langs->trans('Cancel').'</a>';

View File

@@ -790,7 +790,7 @@ class ExpenseReport extends CommonObject
}
/**
* set_to_validate
* set_save_from_refuse
*
* @param User $user User
* @return int <0 if KO, >0 if OK
@@ -971,38 +971,6 @@ class ExpenseReport extends CommonObject
}
}
/**
* set_to_validate
*
* @param User $user User
* @return int <0 if KO, >0 if OK
*/
function set_to_valide($user)
{
if ($this->fk_c_expensereport_statuts != 2)
{
$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
$sql.= " SET fk_c_expensereport_statuts = 2, fk_user_validator = ".$this->fk_user_validator;
$sql.= ' WHERE rowid = '.$this->id;
dol_syslog(get_class($this)."::set_to_valide sql=".$sql, LOG_DEBUG);
if ($this->db->query($sql))
{
return 1;
}
else
{
$this->error=$this->db->error();
return -1;
}
}
else
{
dol_syslog(get_class($this)."::set_to_valide expensereport already with to-valide status", LOG_WARNING);
}
}
/**
* set_cancel
*
@@ -1312,7 +1280,7 @@ class ExpenseReport extends CommonObject
$sql = "SELECT fk_user";
$sql.= " FROM ".MAIN_DB_PREFIX."user_rights as ur, ".MAIN_DB_PREFIX."rights_def as rd";
$sql.= " WHERE ur.fk_id = rd.id and module = 'expensereport' AND perms = 'to_validate'"; // Permission 'Approve';
$sql.= " WHERE ur.fk_id = rd.id and module = 'expensereport' AND perms = 'approve'"; // Permission 'Approve';
dol_syslog(get_class($this)."::fetch_users_approver_expensereport sql=".$sql);
$result = $this->db->query($sql);

View File

@@ -49,7 +49,7 @@ class ExpenseReportStats extends Stats
*/
function __construct($db, $socid=0, $userid=0)
{
global $conf;
global $conf, $user;
$this->db = $db;
$this->socid = $socid;
@@ -65,6 +65,15 @@ class ExpenseReportStats extends Stats
{
$this->where.=" AND fk_soc = ".$this->socid;
}
// Only me and subordinates
if (empty($user->rights->expensereport->readall) && empty($user->rights->expensereport->lire_tous))
{
$childids = $user->getAllChildIds();
$childids[]=$user->id;
$this->where.=" AND fk_user_author IN (".(join(',',$childids)).")";
}
if ($this->userid > 0) $this->where.=' AND fk_user_author = '.$this->userid;
}

View File

@@ -69,7 +69,13 @@ $totalnb=$totalsum=0;
$sql = "SELECT tf.code, tf.label, count(de.rowid) as nb, sum(de.total_ht) as km";
$sql.= " FROM ".MAIN_DB_PREFIX."expensereport as d, ".MAIN_DB_PREFIX."expensereport_det as de, ".MAIN_DB_PREFIX."c_type_fees as tf";
$sql.= " WHERE de.fk_expensereport = d.rowid AND de.fk_c_type_fees = tf.id";
if (empty($user->rights->expensereport->readall) && empty($user->rights->expensereport->lire_tous)) $sql.=' AND d.fk_user_author IN ('.join(',',$childids).')';
// RESTRICT RIGHTS
if (empty($user->rights->expensereport->readall) && empty($user->rights->expensereport->lire_tous))
{
$sql.= " AND d.fk_user_author IN (".join(',',$childids).")\n";
}
$sql.= " GROUP BY tf.code, tf.label";
$result = $db->query($sql);

View File

@@ -48,7 +48,7 @@ $html = new Form($db);
$formother = new FormOther($db);
$expensereporttmp=new ExpenseReport($db);
llxHeader();
llxHeader('', $langs->trans("ListOfExpenseReports"));
$max_year = 5;
$min_year = 5;
@@ -84,13 +84,14 @@ $sql.= " u.rowid as id_user, u.firstname, u.lastname";
$sql.= " FROM ".MAIN_DB_PREFIX."expensereport d\n";
$sql.= " INNER JOIN ".MAIN_DB_PREFIX."user u ON d.fk_user_author = u.rowid\n";
// WHERE
if(!empty($search_ref)){
$sql.= " WHERE d.ref LIKE '%".$db->escape($search_ref)."%'\n";
}else{
$sql.= " WHERE 1 = 1\n";
}
// DATE START
if ($month_start > 0) {
if ($year_start > 0) {
@@ -148,13 +149,15 @@ if ($month_start > 0) {
}
}
}
if (!empty($search_user) && $search_user != -1) $sql.= " AND d.fk_user_author = '$search_user'\n";
if (!empty($search_user) && $search_user > 0) $sql.= " AND d.fk_user_author = ".$search_user."\n";
if($search_state != '') $sql.= " AND d.fk_c_expensereport_statuts = '$search_state'\n";
// RESTRICT RIGHTS
if (empty($user->rights->expensereport->readall) && empty($user->rights->expensereport->lire_tous)){
$sql.= " AND d.fk_user_author = '{$user->id}'\n";
if (empty($user->rights->expensereport->readall) && empty($user->rights->expensereport->lire_tous))
{
$childids = $user->getAllChildIds();
$childids[]=$user->id;
$sql.= " AND d.fk_user_author IN (".join(',',$childids).")\n";
}
$sql.= $db->order($sortfield,$sortorder);

View File

@@ -222,7 +222,9 @@ print '</td></tr>';
*/
// User
print '<tr><td>'.$langs->trans("User").'</td><td>';
print $form->select_dolusers($userid,'userid',1);
$include='';
if (empty($user->rights->expensereport->readall) && empty($user->rights->expensereport->lire_tous)) $include='hierarchy';
print $form->select_dolusers($userid,'userid',1,'',0,$include);
print '</td></tr>';
// Year
print '<tr><td>'.$langs->trans("Year").'</td><td>';

View File

@@ -20,7 +20,8 @@ ListToApprove=Waiting for approval
ExpensesArea=Expense reports area
SearchATripAndExpense=Search an expense report
ClassifyRefunded=Classify 'Refunded'
ExpenseReportWaitingForApproval=A new expense report has been submitted for approval
ExpenseReportWaitingForApprovalMessage=A new expense report has been submitted and is waiting for approval.\n- User: %s\n- Period: %s\nClick here to validate: %s
TripId=Id expense report
AnyOtherInThisListCanValidate=Person to inform for validation.
TripSociete=Information company

View File

@@ -2211,7 +2211,7 @@ class User extends CommonObject
/**
* Return and array with all instanciated children users of current user
* Return and array with all instanciated first level children users of current user
*
* @return void
*/
@@ -2365,7 +2365,7 @@ class User extends CommonObject
}
/**
* Return list of all childs users in herarchy.
* Return list of all child users id in herarchy (all sublevels).
*
* @return array Array of user id lower than user. This overwrite this->users.
*/