Generation of invoice for timespent works on all timesheet tabs

This commit is contained in:
Laurent Destailleur 2019-03-29 15:54:46 +01:00
parent ecc7afa4e3
commit 4e09867b01

View File

@ -315,114 +315,119 @@ elseif (GETPOST('project_ref', 'alpha'))
$projectidforalltimes=$projectstatic->id; $projectidforalltimes=$projectstatic->id;
$withproject=1; $withproject=1;
} }
elseif ($id > 0)
{
$object->fetch($id);
$result=$projectstatic->fetch($object->fk_project);
}
if ($action == 'confirm_generateinvoice') if ($action == 'confirm_generateinvoice')
{ {
if (! empty($projectstatic->socid)) $projectstatic->fetch_thirdparty(); if (! empty($projectstatic->socid)) $projectstatic->fetch_thirdparty();
//->fetch_thirdparty();
if (! ($projectstatic->thirdparty->id > 0)) if (! ($projectstatic->thirdparty->id > 0))
{
setEventMessages($langs->trans("ThirdPartyRequiredToGenerateInvoice"), null, 'errors');
}
else
{
include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
include_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
$tmpinvoice = new Facture($db);
$tmptimespent=new Task($db);
$tmpproduct=new Product($db);
$fuser = new User($db);
$db->begin();
$idprod = GETPOST('productid', 'int');
if ($idprod > 0)
{ {
setEventMessages($langs->trans("ThirdPartyRequiredToGenerateInvoice"), null, 'errors'); $tmpproduct->fetch($idprod);
$dataforprice = $tmpproduct->getSellPrice($mysoc, $projectstatic->thirdparty, 0);
$pu_ht = empty($dataforprice['pu_ht'])?0:$dataforprice['pu_ht'];
$txtva = $dataforprice['tva_tx'];
$localtax1 = $dataforprice['localtax1'];
$localtax2 = $dataforprice['localtax2'];
} }
else else
{ {
include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php'; $pu_ht = 0;
include_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php'; $txtva = get_default_tva($mysoc, $projectstatic->thirdparty);
include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; $localtax1 = get_default_localtax($mysoc, $projectstatic->thirdparty, 1);
$localtax2 = get_default_localtax($mysoc, $projectstatic->thirdparty, 2);
}
$tmpinvoice = new Facture($db); $tmpinvoice->socid = $projectstatic->thirdparty->id;
$tmptimespent=new Task($db); $tmpinvoice->date = dol_mktime(GETPOST('rehour', 'int'), GETPOST('remin', 'int'), GETPOST('resec', 'int'), GETPOST('remonth', 'int'), GETPOST('reday', 'int'), GETPOST('reyear', 'int'));
$tmpproduct=new Product($db); $tmpinvoice->fk_project = $projectstatic->id;
$fuser = new User($db);
$db->begin(); $result = $tmpinvoice->create($user);
$idprod = GETPOST('productid', 'int'); if ($result <= 0)
if ($idprod > 0) {
$error++;
setEventMessages($tmpinvoice->error, $tmpinvoice->errors, 'errors');
}
if (! $error)
{
$arrayoftasks=array();
foreach($toselect as $key => $value)
{ {
$tmpproduct->fetch($idprod); // Get userid, timepent
$object->fetchTimeSpent($value);
$dataforprice = $tmpproduct->getSellPrice($mysoc, $projectstatic->thirdparty, 0); $arrayoftasks[$object->timespent_fk_user]['timespent']+=$object->timespent_duration;
$pu_ht = empty($dataforprice['pu_ht'])?0:$dataforprice['pu_ht']; $arrayoftasks[$object->timespent_fk_user]['totalvaluetodivideby3600']+=($object->timespent_duration * $object->timespent_thm);
$txtva = $dataforprice['tva_tx'];
$localtax1 = $dataforprice['localtax1'];
$localtax2 = $dataforprice['localtax2'];
}
else
{
$pu_ht = 0;
$txtva = get_default_tva($mysoc, $projectstatic->thirdparty);
$localtax1 = get_default_localtax($mysoc, $projectstatic->thirdparty, 1);
$localtax2 = get_default_localtax($mysoc, $projectstatic->thirdparty, 2);
} }
$tmpinvoice->socid = $projectstatic->thirdparty->id; foreach($arrayoftasks as $userid => $value)
$tmpinvoice->date = dol_mktime(GETPOST('rehour', 'int'), GETPOST('remin', 'int'), GETPOST('resec', 'int'), GETPOST('remonth', 'int'), GETPOST('reday', 'int'), GETPOST('reyear', 'int'));
$tmpinvoice->fk_project = $projectstatic->id;
$result = $tmpinvoice->create($user);
if ($result <= 0)
{ {
$error++; $fuser->fetch($userid);
setEventMessages($tmpinvoice->error, $tmpinvoice->errors, 'errors'); //$pu_ht = $value['timespent'] * $fuser->thm;
} $username = $fuser->getFullName($langs);
if (! $error) // Define qty per hour
{ $qtyhour = round($value['timespent'] / 3600, 2);
$arrayoftasks=array(); $qtyhourtext = convertSecondToTime($value['timespent']);
foreach($toselect as $key => $value)
{
// Get userid, timepent
$object->fetchTimeSpent($value);
$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) // If no unit price known
{ if (empty($pu_ht))
$fuser->fetch($userid); {
//$pu_ht = $value['timespent'] * $fuser->thm; $pu_ht = price2num($value['totalvaluetodivideby3600'] / 3600, 'MU');
$username = $fuser->getFullName($langs); }
// Define qty per hour // Add lines
$qtyhour = round($value['timespent'] / 3600, 2); $lineid = $tmpinvoice->addline($langs->trans("TimeSpentForInvoice", $username).' : '.$qtyhourtext, $pu_ht, $qtyhour, $txtva, $localtax1, $localtax2, ($idprod > 0 ? $idprod : 0));
$qtyhourtext = convertSecondToTime($value['timespent']);
// If no unit price known // Update lineid into line of timespent
if (empty($pu_ht)) $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;
$pu_ht = price2num($value['totalvaluetodivideby3600'] / 3600, 'MU'); $result = $db->query($sql);
} if (! $result)
{
// Add lines $error++;
$lineid = $tmpinvoice->addline($langs->trans("TimeSpentForInvoice", $username).' : '.$qtyhourtext, $pu_ht, $qtyhour, $txtva, $localtax1, $localtax2, ($idprod > 0 ? $idprod : 0)); setEventMessages($db->lasterror(), null, 'errors');
break;
// 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;
}
}
}
if (! $error)
{
$urltoinvoice = $tmpinvoice->getNomUrl(0);
setEventMessages($langs->trans("InvoiceGeneratedFromTimeSpent", $urltoinvoice), null, 'mesgs');
//var_dump($tmpinvoice);
$db->commit();
}
else
{
$db->rollback();
} }
} }
if (! $error)
{
$urltoinvoice = $tmpinvoice->getNomUrl(0);
setEventMessages($langs->trans("InvoiceGeneratedFromTimeSpent", $urltoinvoice), null, 'mesgs');
//var_dump($tmpinvoice);
$db->commit();
}
else
{
$db->rollback();
}
}
} }
@ -802,7 +807,10 @@ if (($id > 0 || ! empty($ref)) || $projectidforalltimes > 0)
print $langs->trans('Mode'); print $langs->trans('Mode');
print '</td>'; print '</td>';
print '<td>'; print '<td>';
$tmparray=array('onelineperuser'=>'OneLinePerUser'); $tmparray=array(
'onelineperuser'=>'OneLinePerUser',
//'onelinepertask'=>'OneLinePerTask',
);
print $form->selectarray('generateinvoicemode', $tmparray, 'onelineperuser', 0, 0, 0, '', 1); print $form->selectarray('generateinvoicemode', $tmparray, 'onelineperuser', 0, 0, 0, '', 1);
print '</td>'; print '</td>';
print '</tr>'; print '</tr>';