NEW Can create expense report for someone else (advanced permission)

NEW Can clone an expense report
This commit is contained in:
Laurent Destailleur 2016-11-13 22:00:57 +01:00
parent a369514e46
commit d197a0e0aa
4 changed files with 159 additions and 45 deletions

View File

@ -115,12 +115,6 @@ class modExpenseReport extends DolibarrModules
$this->rights[4][3] = 0;
$this->rights[4][4] = 'supprimer';
$this->rights[2][0] = 774;
$this->rights[2][1] = 'Read all expense reports';
$this->rights[2][2] = 'r';
$this->rights[2][3] = 1;
$this->rights[2][4] = 'readall';
$this->rights[6][0] = 775;
$this->rights[6][1] = 'Approve expense reports';
$this->rights[6][2] = 'w';
@ -133,20 +127,17 @@ class modExpenseReport extends DolibarrModules
$this->rights[7][3] = 0;
$this->rights[7][4] = 'to_paid';
if (! empty($conf->global->DEPLACEMENT_TO_CLEAN))
{
$this->rights[8][0] = 777;
$this->rights[8][1] = 'Synchroniser les NDF avec un compte courant';
$this->rights[8][2] = 'w';
$this->rights[8][3] = 0;
$this->rights[8][4] = 'synchro';
$this->rights[2][0] = 777;
$this->rights[2][1] = 'Read expense reports of everybody';
$this->rights[2][2] = 'r';
$this->rights[2][3] = 1;
$this->rights[2][4] = 'readall';
$this->rights[9][0] = 778;
$this->rights[9][1] = 'Exporter les NDF au format CSV';
$this->rights[9][2] = 'r';
$this->rights[9][3] = 0;
$this->rights[9][4] = 'export_csv';
}
$this->rights[2][0] = 778;
$this->rights[2][1] = 'Create expense reports for everybody';
$this->rights[2][2] = 'w';
$this->rights[2][3] = 0;
$this->rights[2][4] = 'writeall_advance';
$this->rights[5][0] = 779;
$this->rights[5][1] = 'Export expense reports';

View File

@ -46,6 +46,8 @@ $langs->load("mails");
$action=GETPOST('action');
$cancel=GETPOST('cancel');
$confirm = GETPOST('confirm', 'alpha');
$date_start = dol_mktime(0, 0, 0, GETPOST('date_debutmonth'), GETPOST('date_debutday'), GETPOST('date_debutyear'));
$date_end = dol_mktime(0, 0, 0, GETPOST('date_finmonth'), GETPOST('date_finday'), GETPOST('date_finyear'));
$date = dol_mktime(0, 0, 0, GETPOST('datemonth'), GETPOST('dateday'), GETPOST('dateyear'));
@ -192,6 +194,12 @@ if (empty($reshook))
$object->date_debut = $date_start;
$object->date_fin = $date_end;
$object->fk_user_author = GETPOST('fk_user_author','int');
if (! ($object->fk_user_author > 0)) $object->fk_user_author = $user->id;
$fuser=new User($db);
$fuser->fetch($object->fk_user_author);
$object->fk_statut = 1;
$object->fk_c_paiement = GETPOST('fk_c_paiement','int');
$object->fk_user_validator = GETPOST('fk_user_validator','int');
@ -204,7 +212,7 @@ if (empty($reshook))
if ($ret < 0) $error++;
}
if ($object->periode_existe($user,$object->date_debut,$object->date_fin))
if ($object->periode_existe($fuser,$object->date_debut,$object->date_fin))
{
$error++;
setEventMessages($langs->trans("ErrorDoubleDeclaration"), null, 'errors');
@ -1198,18 +1206,35 @@ if ($action == 'create')
print '<table class="border" width="100%">';
print '<tbody>';
// Date start
print '<tr>';
print '<td class="titlefieldcreate fieldrequired">'.$langs->trans("DateStart").'</td>';
print '<td>';
$form->select_date($date_start?$date_start:-1,'date_debut',0,0,0,'',1,1);
print '</td>';
print '</tr>';
// Date end
print '<tr>';
print '<td class="fieldrequired">'.$langs->trans("DateEnd").'</td>';
print '<td>';
$form->select_date($date_end?$date_end:-1,'date_fin',0,0,0,'',1,1);
print '</td>';
print '</tr>';
print '<tr>';
print '<td class="fieldrequired">'.$langs->trans("User").'</td>';
print '<td>';
$defaultselectuser=$user->id;
if (GETPOST('fk_user_author') > 0) $defaultselectuser=GETPOST('fk_user_author');
$include_users = array($user->id);
if (! empty($user->rights->expensereport->writeall)) $include_users=array();
$s=$form->select_dolusers($defaultselectuser, "fk_user_author", 0, "", 0, $include_users);
print $s;
print '</td>';
print '</tr>';
print '<tr>';
print '<td>'.$langs->trans("VALIDATOR").'</td>';
print '<td>';
@ -1222,6 +1247,8 @@ if ($action == 'create')
print $form->textwithpicto($s, $langs->trans("AnyOtherInThisListCanValidate"));
print '</td>';
print '</tr>';
// Payment mode
if (! empty($conf->global->EXPENSEREPORT_ASK_PAYMENTMODE_ON_CREATION))
{
print '<tr>';
@ -1284,7 +1311,8 @@ else
{
if ($object->fk_user_author != $user->id)
{
if (empty($user->rights->expensereport->readall) && empty($user->rights->expensereport->lire_tous))
if (empty($user->rights->expensereport->readall) && empty($user->rights->expensereport->lire_tous)
&& empty($user->rights->expensereport->writeall_advance))
{
print load_fiche_titre($langs->trans('TripCard'));
@ -1322,6 +1350,14 @@ else
$linkback = '<a href="'.DOL_URL_ROOT.'/expensereport/list.php'.(! empty($socid)?'?socid='.$socid:'').'">'.$langs->trans("BackToList").'</a>';
print '<tr>';
print '<td>'.$langs->trans("User").'</td>';
print '<td>';
$userfee=new User($db);
$userfee->fetch($object->fk_user_author);
print $userfee->getNomUrl(-1);
print '</td></tr>';
// Ref
print '<tr><td class="titlefieldcreate">'.$langs->trans("Ref").'</td><td>';
print $form->showrefnav($object, 'ref', $linkback, 1, 'ref', 'ref', '');
@ -1372,13 +1408,6 @@ else
print '</td></tr>';
}
print '<tr>';
print '<td>'.$langs->trans("AUTHOR").'</td>';
print '<td>';
$userfee=new User($db);
$userfee->fetch($object->fk_user_author);
print $userfee->getNomUrl(-1);
print '</td></tr>';
if ($object->fk_statut==6)
{
print '<tr>';
@ -1430,6 +1459,20 @@ else
{
dol_fiche_head($head, 'card', $langs->trans("ExpenseReport"), 0, 'trip');
// Clone confirmation
if ($action == 'clone') {
// Create an array for form
$formquestion = array(
// 'text' => $langs->trans("ConfirmClone"),
// array('type' => 'checkbox', 'name' => 'clone_content', 'label' => $langs->trans("CloneMainAttributes"), 'value' =>
// 1),
// array('type' => 'checkbox', 'name' => 'update_prices', 'label' => $langs->trans("PuttingPricesUpToDate"), 'value'
// => 1),
);
// Paiement incomplet. On demande si motif = escompte ou autre
$formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $object->id, $langs->trans('CloneExpenseReport'), $langs->trans('ConfirmCloneExpenseReport', $object->ref), 'confirm_clone', $formquestion, 'yes', 1);
}
if ($action == 'save')
{
$formconfirm=$form->form_confirm($_SERVER["PHP_SELF"]."?id=".$id,$langs->trans("SaveTrip"),$langs->trans("ConfirmSaveTrip"),"confirm_validate","","",1);
@ -1543,6 +1586,19 @@ else
print $form->showrefnav($object, 'ref', $linkback, 1, 'ref', 'ref', '');
print '</td></tr>';*/
// Author
print '<tr>';
print '<td class="titlefield">'.$langs->trans("User").'</td>';
print '<td>';
if ($object->fk_user_author > 0)
{
$userauthor=new User($db);
$result=$userauthor->fetch($object->fk_user_author);
if ($result < 0) dol_print_error('',$userauthor->error);
print $userauthor->getNomUrl(-1);
}
print '</td></tr>';
print '<tr>';
print '<td class="titlefield">'.$langs->trans("Period").'</td>';
print '<td>';
@ -1595,19 +1651,6 @@ else
print '<td>'.price($object->total_ttc).'</td>';
print '</tr>';
// Author
print '<tr>';
print '<td>'.$langs->trans("AUTHOR").'</td>';
print '<td>';
if ($object->fk_user_author > 0)
{
$userauthor=new User($db);
$result=$userauthor->fetch($object->fk_user_author);
if ($result < 0) dol_print_error('',$userauthor->error);
print $userauthor->getNomUrl(-1);
}
print '</td></tr>';
// Validation date
print '<tr>';
print '<td>'.$langs->trans("DATE_SAVE").'</td>';
@ -2201,6 +2244,10 @@ if ($action != 'create' && $action != 'edit')
print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=cancel&id='.$object->id.'">'.$langs->trans('Cancel').'</a></div>';
}
// Clone
if ($user->rights->expensereport->creer) {
print '<div class="inline-block divButAction"><a class="butAction" href="' . $_SERVER['PHP_SELF'] . '?id=' . $object->id . '&amp;action=clone">' . $langs->trans("ToClone") . '</a></div>';
}
/* If draft, validated, cancel, and user can create, he can always delete its card before it is approved */
if ($user->rights->expensereport->creer && $user->id == $object->fk_user_author && $object->fk_statut <= 4)

View File

@ -126,6 +126,9 @@ class ExpenseReport extends CommonObject
$now = dol_now();
$fuserid = $this->fk_user_author;
if (empty($fuserid)) $fuserid = $user->id;
$this->db->begin();
$sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element." (";
@ -153,7 +156,7 @@ class ExpenseReport extends CommonObject
$sql.= ", '".$this->db->idate($this->date_debut)."'";
$sql.= ", '".$this->db->idate($this->date_fin)."'";
$sql.= ", '".$this->db->idate($now)."'";
$sql.= ", ".($user->id > 0 ? $user->id:"null");
$sql.= ", ".$fuserid;
$sql.= ", ".($this->fk_user_validator > 0 ? $this->fk_user_validator:"null");
$sql.= ", ".($this->fk_user_modif > 0 ? $this->fk_user_modif:"null");
$sql.= ", ".($this->fk_statut > 1 ? $this->fk_statut:0);
@ -222,6 +225,78 @@ class ExpenseReport extends CommonObject
}
/**
* Load an object from its id and create a new one in database
*
* @param int $socid Id of thirdparty
* @return int New id of clone
*/
function createFromClone($socid=0)
{
global $user,$hookmanager;
$error=0;
$this->context['createfromclone'] = 'createfromclone';
$this->db->begin();
// get extrafields so they will be clone
foreach($this->lines as $line)
//$line->fetch_optionals($line->rowid);
// Load source object
$objFrom = clone $this;
$this->id=0;
$this->ref = '';
$this->statut=0;
// Clear fields
$this->fk_user_author = $user->id;
$this->fk_user_valid = '';
$this->date_create = '';
$this->date_creation = '';
$this->date_validation = '';
// Create clone
$result=$this->create($user);
if ($result < 0) $error++;
if (! $error)
{
// Hook of thirdparty module
if (is_object($hookmanager))
{
$parameters=array('objFrom'=>$objFrom);
$action='';
$reshook=$hookmanager->executeHooks('createFrom',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
if ($reshook < 0) $error++;
}
// Call trigger
$result=$this->call_trigger('EXPENSEREPORT_CLONE',$user);
if ($result < 0) $error++;
// End call triggers
}
unset($this->context['createfromclone']);
// End
if (! $error)
{
$this->db->commit();
return $this->id;
}
else
{
$this->db->rollback();
return -1;
}
}
/**
* update
*

View File

@ -100,7 +100,7 @@ if (empty($user->socid)) $fieldstosearchall["d.note_private"]="NotePrivate";
$arrayfields=array(
'd.ref'=>array('label'=>$langs->trans("Ref"), 'checked'=>1),
'user'=>array('label'=>$langs->trans("Person"), 'checked'=>1),
'user'=>array('label'=>$langs->trans("User"), 'checked'=>1),
'd.date_debut'=>array('label'=>$langs->trans("DateStart"), 'checked'=>1),
'd.date_fin'=>array('label'=>$langs->trans("DateEnd"), 'checked'=>1),
'd.total_ht'=>array('label'=>$langs->trans("AmountHT"), 'checked'=>1),
@ -248,7 +248,8 @@ if ($search_status != '' && $search_status >= 0)
else $sql.=" AND d.fk_statut = ".$search_status;
}
// RESTRICT RIGHTS
if (empty($user->rights->expensereport->readall) && empty($user->rights->expensereport->lire_tous))
if (empty($user->rights->expensereport->readall) && empty($user->rights->expensereport->lire_tous)
&& empty($user->rights->expensereport->writeall_advance))
{
$childids = $user->getAllChildIds();
$childids[]=$user->id;
@ -354,7 +355,7 @@ if ($resql)
print '<table class="tagtable liste'.($moreforfilter?" listwithfilterbefore":"").'">'."\n";
print "<tr class=\"liste_titre\">";
if (! empty($arrayfields['d.ref']['checked'])) print_liste_field_titre($langs->trans("Ref"),$_SERVER["PHP_SELF"],"d.ref","",$param,'',$sortfield,$sortorder);
if (! empty($arrayfields['user']['checked'])) print_liste_field_titre($langs->trans("Person"),$_SERVER["PHP_SELF"],"u.lastname","",$param,'',$sortfield,$sortorder);
if (! empty($arrayfields['user']['checked'])) print_liste_field_titre($langs->trans("User"),$_SERVER["PHP_SELF"],"u.lastname","",$param,'',$sortfield,$sortorder);
if (! empty($arrayfields['d.date_debut']['checked'])) print_liste_field_titre($langs->trans("DateStart"),$_SERVER["PHP_SELF"],"d.date_debut","",$param,'align="center"',$sortfield,$sortorder);
if (! empty($arrayfields['d.date_fin']['checked'])) print_liste_field_titre($langs->trans("DateEnd"),$_SERVER["PHP_SELF"],"d.date_fin","",$param,'align="center"',$sortfield,$sortorder);
if (! empty($arrayfields['d.total_ht']['checked'])) print_liste_field_titre($langs->trans("TotalHT"),$_SERVER["PHP_SELF"],"d.total_ht","",$param,'align="right"',$sortfield,$sortorder);