Fix generation of invoice

This commit is contained in:
Laurent Destailleur 2021-05-18 13:36:07 +02:00
parent 74f35177ff
commit 7569275d28
3 changed files with 25 additions and 14 deletions

View File

@ -267,9 +267,11 @@ InvoiceToUse=Draft invoice to use
NewInvoice=New invoice NewInvoice=New invoice
OneLinePerTask=One line per task OneLinePerTask=One line per task
OneLinePerPeriod=One line per period OneLinePerPeriod=One line per period
OneLinePerTimeSpentLine=One line for each time spent declaration
RefTaskParent=Ref. Parent Task RefTaskParent=Ref. Parent Task
ProfitIsCalculatedWith=Profit is calculated using ProfitIsCalculatedWith=Profit is calculated using
AddPersonToTask=Add also to tasks AddPersonToTask=Add also to tasks
UsageOrganizeEvent=Usage: Event Organization UsageOrganizeEvent=Usage: Event Organization
PROJECT_CLASSIFY_CLOSED_WHEN_ALL_TASKS_DONE=Classify project as closed when all its tasks are completed (100%% progress) PROJECT_CLASSIFY_CLOSED_WHEN_ALL_TASKS_DONE=Classify project as closed when all its tasks are completed (100%% progress)
PROJECT_CLASSIFY_CLOSED_WHEN_ALL_TASKS_DONE_help=Note: existing projects with all tasks at 100 %% progress won't be affected: you will have to close them manually. This option only affects open projects. PROJECT_CLASSIFY_CLOSED_WHEN_ALL_TASKS_DONE_help=Note: existing projects with all tasks at 100 %% progress won't be affected: you will have to close them manually. This option only affects open projects.
SelectLinesOfTimeSpentToInvoice=Select lines of time spent that are unbilled, then bulk action "Generate Invoice" to bill them

View File

@ -1351,9 +1351,9 @@ class Task extends CommonObject
} }
/** /**
* Load one record of time spent * Load properties of timespent of a task from the time spent ID.
* *
* @param int $id Id object * @param int $id Id in time spent table
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
*/ */
public function fetchTimeSpent($id) public function fetchTimeSpent($id)

View File

@ -192,10 +192,10 @@ if ($action == 'addtimespent' && $user->rights->projet->lire) {
$object->timespent_duration = GETPOSTINT("timespent_durationhour") * 60 * 60; // We store duration in seconds $object->timespent_duration = GETPOSTINT("timespent_durationhour") * 60 * 60; // We store duration in seconds
$object->timespent_duration += (GETPOSTINT('timespent_durationmin') ? GETPOSTINT('timespent_durationmin') : 0) * 60; // We store duration in seconds $object->timespent_duration += (GETPOSTINT('timespent_durationmin') ? GETPOSTINT('timespent_durationmin') : 0) * 60; // We store duration in seconds
if (GETPOST("timehour") != '' && GETPOST("timehour") >= 0) { // If hour was entered if (GETPOST("timehour") != '' && GETPOST("timehour") >= 0) { // If hour was entered
$object->timespent_date = dol_mktime(GETPOST("timehour"), GETPOST("timemin"), 0, GETPOST("timemonth"), GETPOST("timeday"), GETPOST("timeyear")); $object->timespent_date = dol_mktime(GETPOST("timehour", 'int'), GETPOST("timemin", 'int'), 0, GETPOST("timemonth", 'int'), GETPOST("timeday", 'int'), GETPOST("timeyear", 'int'));
$object->timespent_withhour = 1; $object->timespent_withhour = 1;
} else { } else {
$object->timespent_date = dol_mktime(12, 0, 0, GETPOST("timemonth"), GETPOST("timeday"), GETPOST("timeyear")); $object->timespent_date = dol_mktime(12, 0, 0, GETPOST("timemonth", 'int'), GETPOST("timeday", 'int'), GETPOST("timeyear", 'int'));
} }
$object->timespent_fk_user = GETPOST("userid", 'int'); $object->timespent_fk_user = GETPOST("userid", 'int');
$result = $object->addTimeSpent($user); $result = $object->addTimeSpent($user);
@ -437,22 +437,27 @@ if ($action == 'confirm_generateinvoice') {
break; break;
} }
} }
} elseif ($generateinvoicemode == 'onelineperperiod') { } elseif ($generateinvoicemode == 'onelineperperiod') { // One line for each time spent line
$arrayoftasks = array(); $arrayoftasks = array();
foreach ($toselect as $key => $value) { foreach ($toselect as $key => $value) {
// Get userid, timepent // Get userid, timepent
$object->fetchTimeSpent($value); $object->fetchTimeSpent($value);
// $object->id is the task id
$ftask = new Task($db);
$ftask->fetch($object->id);
$fuser->fetch($object->timespent_fk_user);
$username = $fuser->getFullName($langs);
$arrayoftasks[$object->timespent_id]['timespent'] = $object->timespent_duration; $arrayoftasks[$object->timespent_id]['timespent'] = $object->timespent_duration;
$arrayoftasks[$object->timespent_id]['totalvaluetodivideby3600'] = $object->timespent_duration * $object->timespent_thm; $arrayoftasks[$object->timespent_id]['totalvaluetodivideby3600'] = $object->timespent_duration * $object->timespent_thm;
$arrayoftasks[$object->timespent_id]['note'] = $object->timespent_note; $arrayoftasks[$object->timespent_id]['note'] = $ftask->ref.' - '.$ftask->label.' - '.$username.($object->timespent_note ? ' - '.$object->timespent_note : ''); // TODO Add user name in note
$arrayoftasks[$object->timespent_id]['user'] = $object->timespent_fk_user; $arrayoftasks[$object->timespent_id]['user'] = $object->timespent_fk_user;
} }
foreach ($arrayoftasks as $timespent_id => $value) { foreach ($arrayoftasks as $timespent_id => $value) {
$userid = $value['user']; $userid = $value['user'];
$fuser->fetch($userid);
//$pu_ht = $value['timespent'] * $fuser->thm; //$pu_ht = $value['timespent'] * $fuser->thm;
$username = $fuser->getFullName($langs);
// Define qty per hour // Define qty per hour
$qtyhour = $value['timespent'] / 3600; $qtyhour = $value['timespent'] / 3600;
@ -465,6 +470,7 @@ if ($action == 'confirm_generateinvoice') {
// Add lines // Add lines
$lineid = $tmpinvoice->addline($value['note'], $pu_ht, round($qtyhour / $prodDurationHours, 2), $txtva, $localtax1, $localtax2, ($idprod > 0 ? $idprod : 0)); $lineid = $tmpinvoice->addline($value['note'], $pu_ht, round($qtyhour / $prodDurationHours, 2), $txtva, $localtax1, $localtax2, ($idprod > 0 ? $idprod : 0));
//var_dump($lineid);exit;
// Update lineid into line of timespent // Update lineid into line of timespent
$sql = 'UPDATE '.MAIN_DB_PREFIX.'projet_task_time SET invoice_line_id = '.((int) $lineid).', invoice_id = '.((int) $tmpinvoice->id); $sql = 'UPDATE '.MAIN_DB_PREFIX.'projet_task_time SET invoice_line_id = '.((int) $lineid).', invoice_id = '.((int) $tmpinvoice->id);
@ -517,7 +523,10 @@ if ($action == 'confirm_generateinvoice') {
if (!$error) { if (!$error) {
$urltoinvoice = $tmpinvoice->getNomUrl(0); $urltoinvoice = $tmpinvoice->getNomUrl(0);
setEventMessages($langs->trans("InvoiceGeneratedFromTimeSpent", $urltoinvoice), null, 'mesgs'); $mesg = $langs->trans("InvoiceGeneratedFromTimeSpent", '{s1}');
$mesg = str_replace('{s1}', $urltoinvoice, $mesg);
setEventMessages($mesg, null, 'mesgs');
//var_dump($tmpinvoice); //var_dump($tmpinvoice);
$db->commit(); $db->commit();
@ -969,7 +978,7 @@ if (($id > 0 || !empty($ref)) || $projectidforalltimes > 0) {
$tmparray = array( $tmparray = array(
'onelineperuser'=>'OneLinePerUser', 'onelineperuser'=>'OneLinePerUser',
'onelinepertask'=>'OneLinePerTask', 'onelinepertask'=>'OneLinePerTask',
'onelineperperiod'=>'OneLinePerPeriod', 'onelineperperiod'=>'OneLinePerTimeSpentLine',
); );
print $form->selectarray('generateinvoicemode', $tmparray, 'onelineperuser', 0, 0, 0, '', 1); print $form->selectarray('generateinvoicemode', $tmparray, 'onelineperuser', 0, 0, 0, '', 1);
print '</td>'; print '</td>';
@ -1315,7 +1324,7 @@ if (($id > 0 || !empty($ref)) || $projectidforalltimes > 0) {
print_liste_field_titre($arrayfields['value']['label'], $_SERVER['PHP_SELF'], '', '', $param, '', $sortfield, $sortorder, 'right '); print_liste_field_titre($arrayfields['value']['label'], $_SERVER['PHP_SELF'], '', '', $param, '', $sortfield, $sortorder, 'right ');
} }
if (!empty($arrayfields['valuebilled']['checked'])) { if (!empty($arrayfields['valuebilled']['checked'])) {
print_liste_field_titre($arrayfields['valuebilled']['label'], $_SERVER['PHP_SELF'], 'il.total_ht', '', $param, '', $sortfield, $sortorder, 'center '); print_liste_field_titre($arrayfields['valuebilled']['label'], $_SERVER['PHP_SELF'], 'il.total_ht', '', $param, '', $sortfield, $sortorder, 'center ', $langs->trans("SelectLinesOfTimeSpentToInvoice"));
} }
/* /*
// Extra fields // Extra fields