Added two new options when billing time spent on a project :
- Create one invoice line per timespent entry - Create one invoice line per task
This commit is contained in:
parent
ece8390277
commit
5b1ae96ce9
@ -342,6 +342,8 @@ if ($action == 'confirm_generateinvoice')
|
|||||||
|
|
||||||
$db->begin();
|
$db->begin();
|
||||||
$idprod = GETPOST('productid', 'int');
|
$idprod = GETPOST('productid', 'int');
|
||||||
|
$generateinvoicemode = GETPOST('generateinvoicemode', 'string');
|
||||||
|
|
||||||
if ($idprod > 0)
|
if ($idprod > 0)
|
||||||
{
|
{
|
||||||
$tmpproduct->fetch($idprod);
|
$tmpproduct->fetch($idprod);
|
||||||
@ -373,45 +375,132 @@ if ($action == 'confirm_generateinvoice')
|
|||||||
|
|
||||||
if (!$error)
|
if (!$error)
|
||||||
{
|
{
|
||||||
$arrayoftasks = array();
|
if ($generateinvoicemode=='onelineperuser') {
|
||||||
foreach ($toselect as $key => $value)
|
$arrayoftasks = array();
|
||||||
{
|
foreach ($toselect as $key => $value)
|
||||||
// Get userid, timepent
|
{
|
||||||
$object->fetchTimeSpent($value);
|
// Get userid, timepent
|
||||||
$arrayoftasks[$object->timespent_fk_user]['timespent'] += $object->timespent_duration;
|
$object->fetchTimeSpent($value);
|
||||||
$arrayoftasks[$object->timespent_fk_user]['totalvaluetodivideby3600'] += ($object->timespent_duration * $object->timespent_thm);
|
$arrayoftasks[$object->timespent_fk_user]['timespent'] += $object->timespent_duration;
|
||||||
}
|
$arrayoftasks[$object->timespent_fk_user]['totalvaluetodivideby3600'] += ($object->timespent_duration * $object->timespent_thm);
|
||||||
|
}
|
||||||
|
|
||||||
foreach ($arrayoftasks as $userid => $value)
|
foreach ($arrayoftasks as $userid => $value)
|
||||||
{
|
{
|
||||||
$fuser->fetch($userid);
|
$fuser->fetch($userid);
|
||||||
//$pu_ht = $value['timespent'] * $fuser->thm;
|
//$pu_ht = $value['timespent'] * $fuser->thm;
|
||||||
$username = $fuser->getFullName($langs);
|
$username = $fuser->getFullName($langs);
|
||||||
|
|
||||||
// Define qty per hour
|
// Define qty per hour
|
||||||
$qtyhour = round($value['timespent'] / 3600, 2);
|
$qtyhour = round($value['timespent'] / 3600, 2);
|
||||||
$qtyhourtext = convertSecondToTime($value['timespent'], 'all', $conf->global->MAIN_DURATION_OF_WORKDAY);
|
$qtyhourtext = convertSecondToTime($value['timespent'], 'all', $conf->global->MAIN_DURATION_OF_WORKDAY);
|
||||||
|
|
||||||
// If no unit price known
|
// If no unit price known
|
||||||
if (empty($pu_ht))
|
if (empty($pu_ht))
|
||||||
{
|
{
|
||||||
$pu_ht = price2num($value['totalvaluetodivideby3600'] / 3600, 'MU');
|
$pu_ht = price2num($value['totalvaluetodivideby3600'] / 3600, 'MU');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add lines
|
// Add lines
|
||||||
$lineid = $tmpinvoice->addline($langs->trans("TimeSpentForInvoice", $username).' : '.$qtyhourtext, $pu_ht, $qtyhour, $txtva, $localtax1, $localtax2, ($idprod > 0 ? $idprod : 0));
|
$lineid = $tmpinvoice->addline($langs->trans("TimeSpentForInvoice", $username).' : '.$qtyhourtext, $pu_ht, $qtyhour, $txtva, $localtax1, $localtax2, ($idprod > 0 ? $idprod : 0));
|
||||||
|
|
||||||
// Update lineid into line of timespent
|
// Update lineid into line of timespent
|
||||||
$sql = 'UPDATE '.MAIN_DB_PREFIX.'projet_task_time SET invoice_line_id = '.$lineid.', invoice_id = '.$tmpinvoice->id;
|
$sql = 'UPDATE '.MAIN_DB_PREFIX.'projet_task_time SET invoice_line_id = '.$lineid.', invoice_id = '.$tmpinvoice->id;
|
||||||
$sql .= ' WHERE rowid in ('.join(',', $toselect).') AND fk_user = '.$userid;
|
$sql .= ' WHERE rowid in ('.join(',', $toselect).') AND fk_user = '.$userid;
|
||||||
$result = $db->query($sql);
|
$result = $db->query($sql);
|
||||||
if (!$result)
|
if (!$result)
|
||||||
{
|
{
|
||||||
$error++;
|
$error++;
|
||||||
setEventMessages($db->lasterror(), null, 'errors');
|
setEventMessages($db->lasterror(), null, 'errors');
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
elseif ($generateinvoicemode=='onelineperperiod') {
|
||||||
|
$arrayoftasks = array();
|
||||||
|
foreach ($toselect as $key => $value)
|
||||||
|
{
|
||||||
|
// Get userid, timepent
|
||||||
|
$object->fetchTimeSpent($value);
|
||||||
|
$arrayoftasks[$object->timespent_id]['timespent'] = $object->timespent_duration;
|
||||||
|
$arrayoftasks[$object->timespent_id]['totalvaluetodivideby3600'] = $object->timespent_duration * $object->timespent_thm;
|
||||||
|
$arrayoftasks[$object->timespent_id]['note'] = $object->timespent_note;
|
||||||
|
$arrayoftasks[$object->timespent_id]['user'] = $object->timespent_fk_user;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($arrayoftasks as $timespent_id => $value)
|
||||||
|
{
|
||||||
|
$userid = $value['user'];
|
||||||
|
$fuser->fetch($userid);
|
||||||
|
//$pu_ht = $value['timespent'] * $fuser->thm;
|
||||||
|
$username = $fuser->getFullName($langs);
|
||||||
|
|
||||||
|
// Define qty per hour
|
||||||
|
$qtyhour = round($value['timespent'] / 3600, 2);
|
||||||
|
$qtyhourtext = convertSecondToTime($value['timespent'], 'all', $conf->global->MAIN_DURATION_OF_WORKDAY);
|
||||||
|
|
||||||
|
// If no unit price known
|
||||||
|
if (empty($pu_ht))
|
||||||
|
{
|
||||||
|
$pu_ht = price2num($value['totalvaluetodivideby3600'] / 3600, 'MU');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add lines
|
||||||
|
$lineid = $tmpinvoice->addline($value['note'], $pu_ht, $qtyhour, $txtva, $localtax1, $localtax2, ($idprod > 0 ? $idprod : 0));
|
||||||
|
|
||||||
|
// Update lineid into line of timespent
|
||||||
|
$sql = 'UPDATE '.MAIN_DB_PREFIX.'projet_task_time SET invoice_line_id = '.$lineid.', invoice_id = '.$tmpinvoice->id;
|
||||||
|
$sql .= ' WHERE rowid in ('.join(',', $toselect).') AND fk_user = '.$userid;
|
||||||
|
$result = $db->query($sql);
|
||||||
|
if (!$result)
|
||||||
|
{
|
||||||
|
$error++;
|
||||||
|
setEventMessages($db->lasterror(), null, 'errors');
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
elseif ($generateinvoicemode=='onelinepertask') {
|
||||||
|
$arrayoftasks = array();
|
||||||
|
foreach ($toselect as $key => $value)
|
||||||
|
{
|
||||||
|
// Get userid, timepent
|
||||||
|
$object->fetchTimeSpent($value);
|
||||||
|
// $object->id is the task id
|
||||||
|
$arrayoftasks[$object->id]['timespent'] += $object->timespent_duration;
|
||||||
|
$arrayoftasks[$object->id]['totalvaluetodivideby3600'] += $object->timespent_duration * $object->timespent_thm;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($arrayoftasks as $task_id => $value)
|
||||||
|
{
|
||||||
|
$ftask = new Task($db);
|
||||||
|
$ftask->fetch($task_id);
|
||||||
|
// Define qty per hour
|
||||||
|
$qtyhour = round($value['timespent'] / 3600, 2);
|
||||||
|
$qtyhourtext = convertSecondToTime($value['timespent'], 'all', $conf->global->MAIN_DURATION_OF_WORKDAY);
|
||||||
|
|
||||||
|
// If no unit price known
|
||||||
|
if (empty($pu_ht))
|
||||||
|
{
|
||||||
|
$pu_ht = price2num($value['totalvaluetodivideby3600'] / 3600, 'MU');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add lines
|
||||||
|
$lineName = $ftask->ref.' - '.$ftask->label;
|
||||||
|
$lineid = $tmpinvoice->addline($lineName, $pu_ht, $qtyhour, $txtva, $localtax1, $localtax2, ($idprod > 0 ? $idprod : 0));
|
||||||
|
|
||||||
|
// Update lineid into line of timespent
|
||||||
|
$sql = 'UPDATE '.MAIN_DB_PREFIX.'projet_task_time SET invoice_line_id = '.$lineid.', invoice_id = '.$tmpinvoice->id;
|
||||||
|
$sql .= ' WHERE rowid in ('.join(',', $toselect).')';
|
||||||
|
$result = $db->query($sql);
|
||||||
|
if (!$result)
|
||||||
|
{
|
||||||
|
$error++;
|
||||||
|
setEventMessages($db->lasterror(), null, 'errors');
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$error)
|
if (!$error)
|
||||||
@ -841,7 +930,8 @@ if (($id > 0 || !empty($ref)) || $projectidforalltimes > 0)
|
|||||||
print '<td>';
|
print '<td>';
|
||||||
$tmparray = array(
|
$tmparray = array(
|
||||||
'onelineperuser'=>'OneLinePerUser',
|
'onelineperuser'=>'OneLinePerUser',
|
||||||
//'onelinepertask'=>'OneLinePerTask',
|
'onelinepertask'=>'OneLinePerTask',
|
||||||
|
'onelineperperiod'=>'OneLinePerPeriod',
|
||||||
);
|
);
|
||||||
print $form->selectarray('generateinvoicemode', $tmparray, 'onelineperuser', 0, 0, 0, '', 1);
|
print $form->selectarray('generateinvoicemode', $tmparray, 'onelineperuser', 0, 0, 0, '', 1);
|
||||||
print '</td>';
|
print '</td>';
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user