diff --git a/htdocs/langs/en_US/projects.lang b/htdocs/langs/en_US/projects.lang index 349ed87153e..97eddffc26e 100644 --- a/htdocs/langs/en_US/projects.lang +++ b/htdocs/langs/en_US/projects.lang @@ -237,4 +237,8 @@ SendProjectRef=Information project %s ModuleSalaryToDefineHourlyRateMustBeEnabled=Module 'Salaries' must be enabled to define employee hourly rate to have time spent valorized NewTaskRefSuggested=Task ref already used, a new task ref is required TimeSpentInvoiced=Time spent billed -GenerateInvoice=Generate invoice \ No newline at end of file +TimeSpentForInvoice=Time spent +GenerateInvoice=Generate invoice +OneLinePerUser=One line per user +ServiceToUseOnLines=Service to use on lines +InvoiceGeneratedFromTimeSpent=Invoice %s has been generated from time spent on project \ No newline at end of file diff --git a/htdocs/projet/tasks/time.php b/htdocs/projet/tasks/time.php index bbb04cc3d76..36ee794ce15 100644 --- a/htdocs/projet/tasks/time.php +++ b/htdocs/projet/tasks/time.php @@ -301,12 +301,11 @@ elseif (GETPOST('project_ref','alpha')) $withproject=1; } -if ($massaction == 'confirm_generateinvoice') +if ($action == 'confirm_generateinvoice') { if (! empty($projectstatic->socid)) $projectstatic->fetch_thirdparty(); //->fetch_thirdparty(); - if (! ($projectstatic->thirdparty->id > 0)) { setEventMessages($langs->trans("ThirdPartyRequiredToGenerateInvoice"), null, 'errors'); @@ -324,43 +323,71 @@ if ($massaction == 'confirm_generateinvoice') $db->begin(); - $idprod = GETPOST('idprod', 'int'); + $idprod = GETPOST('productid', 'int'); if ($idprod > 0) { $tmpproduct->fetch($idprod); } $dataforprice = $tmpproduct->getSellPrice($mysoc, $projectstatic->thirdparty, 0); - $pu_ht = $dataforprice['pu_ht']; + $pu_ht = empty($dataforprice['pu_ht'])?0:$dataforprice['pu_ht']; $txtva = $dataforprice['tva_tx']; - $tmpinvoice->fk_soc = $projectstatic->thirdparty->id; - $tmpinvoice->create($user); + $tmpinvoice->socid = $projectstatic->thirdparty->id; + $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; - $arrayoftasks=array(); - foreach($toselect as $key => $value) + $result = $tmpinvoice->create($user); + if ($result <= 0) { - // Get userid, timepent - $object->fetchTimeSpent($value); - - $arrayoftasks[$object->timespent_fk_user]['timespent']+=$object->timespent_duration; + $error++; + setEventMessages($tmpinvoice->error, $tmpinvoice->errors, 'errors'); } - foreach($arrayoftasks as $userid => $value) - { - $fuser->fetch($userid); - //$pu_ht = $value['timespent'] * $fuser->thm; - $username = $fuser->getFullName($langs); - - // Add lines - $tmpinvoice->addline($langs->trans("TotalOfTimeSpentBy", $username).' : '.$value['timespent'], $pu_ht, 1, $txtva); - } - - setEventMessages($langs->trans("InvoiceGeneratedFromTimeSpent", $tmpinvoice->ref), null, 'mesgs'); - //var_dump($tmpinvoice); if (! $error) { - $db->commit(); + $arrayoftasks=array(); + foreach($toselect as $key => $value) + { + // Get userid, timepent + $object->fetchTimeSpent($value); + + $arrayoftasks[$object->timespent_fk_user]['timespent']+=$object->timespent_duration; + } + + foreach($arrayoftasks as $userid => $value) + { + $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']); + + // Add lines + $lineid = $tmpinvoice->addline($langs->trans("TimeSpentForInvoice", $username).' : '.$qtyhourtext, $pu_ht, $qtyhour, $txtva); + + // 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 { @@ -802,6 +829,7 @@ if (($id > 0 || ! empty($ref)) || $projectidforalltimes > 0) if ($action == 'editline') print ''; elseif ($action == 'splitline') print ''; elseif ($action == 'createtime' && empty($id) && $user->rights->projet->lire) print ''; + elseif ($massaction == 'generateinvoice' && empty($id) && $user->rights->facture->lire) print ''; else print ''; print ''; print ''; @@ -827,11 +855,20 @@ if (($id > 0 || ! empty($ref)) || $projectidforalltimes > 0) print $form->selectDate('', '', '', '', '', '', 1, 1); print ''; print ''; + print ''; + print ''; + print $langs->trans('Mode'); + print ''; + print ''; + $tmparray=array('onelineperuser'=>'OneLinePerUser'); + print $form->selectarray('generateinvoicemode', $tmparray, 'onelineperuser', 0, 0, 0, '', 1); + print ''; + print ''; if ($conf->service->enabled) { print ''; print ''; - print $langs->trans('Service'); + print $langs->trans('ServiceToUseOnLines'); print ''; print ''; print $form->select_produits('', 'productid', '1', 0, 0, 1, 2, '', 0, array(), 0, 'None', 0, 'maxwidth500'); @@ -1125,6 +1162,7 @@ if (($id > 0 || ! empty($ref)) || $projectidforalltimes > 0) print "\n"; $tasktmp = new Task($db); + $tmpinvoice = new Facture($db); $i = 0; @@ -1281,8 +1319,11 @@ if (($id > 0 || ! empty($ref)) || $projectidforalltimes > 0) { if ($task_time->invoice_id) { - $tmpinvoice->fetch($task_time->invoice_id); - print $tmpinvoice->getNomUrl(1); + $result = $tmpinvoice->fetch($task_time->invoice_id); + if ($result > 0) + { + print $tmpinvoice->getNomUrl(1); + } } else {