From 695a681c14dffb1455fd5bbf952b6030b440e848 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sun, 3 Feb 2019 21:35:30 +0100 Subject: [PATCH] NEW Can generate invoices from timespent of a project --- htdocs/projet/class/task.class.php | 2 ++ htdocs/projet/tasks/time.php | 28 +++++++++++++++++++++------- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/htdocs/projet/class/task.class.php b/htdocs/projet/class/task.class.php index 3777edfeb0f..82f8c9ba169 100644 --- a/htdocs/projet/class/task.class.php +++ b/htdocs/projet/class/task.class.php @@ -1226,6 +1226,7 @@ class Task extends CommonObject $sql.= " t.task_date_withhour,"; $sql.= " t.task_duration,"; $sql.= " t.fk_user,"; + $sql.= " t.thm,"; $sql.= " t.note"; $sql.= " FROM ".MAIN_DB_PREFIX."projet_task_time as t"; $sql.= " WHERE t.rowid = ".$id; @@ -1245,6 +1246,7 @@ class Task extends CommonObject $this->timespent_withhour = $obj->task_date_withhour; $this->timespent_duration = $obj->task_duration; $this->timespent_fk_user = $obj->fk_user; + $this->timespent_thm = $obj->thm; // hourly rate $this->timespent_note = $obj->note; } diff --git a/htdocs/projet/tasks/time.php b/htdocs/projet/tasks/time.php index 0e68cc97680..be87a52fcd9 100644 --- a/htdocs/projet/tasks/time.php +++ b/htdocs/projet/tasks/time.php @@ -322,16 +322,24 @@ if ($action == 'confirm_generateinvoice') $fuser = new User($db); $db->begin(); - $idprod = GETPOST('productid', 'int'); if ($idprod > 0) { $tmpproduct->fetch($idprod); - } - $dataforprice = $tmpproduct->getSellPrice($mysoc, $projectstatic->thirdparty, 0); - $pu_ht = empty($dataforprice['pu_ht'])?0:$dataforprice['pu_ht']; - $txtva = $dataforprice['tva_tx']; + $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 + { + $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; $tmpinvoice->date = dol_mktime(GETPOST('rehour', 'int'), GETPOST('remin', 'int'), GETPOST('resec', 'int'), GETPOST('remonth', 'int'), GETPOST('reday', 'int'), GETPOST('reyear', 'int')); @@ -351,8 +359,8 @@ if ($action == 'confirm_generateinvoice') { // 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) @@ -365,8 +373,14 @@ if ($action == 'confirm_generateinvoice') $qtyhour = round($value['timespent'] / 3600, 2); $qtyhourtext = convertSecondToTime($value['timespent']); + // If no unit price known + if (empty($pu_ht)) + { + $pu_ht = price2num($value['totalvaluetodivideby3600'] / 3600, 'MU'); + } + // Add lines - $lineid = $tmpinvoice->addline($langs->trans("TimeSpentForInvoice", $username).' : '.$qtyhourtext, $pu_ht, $qtyhour, $txtva); + $lineid = $tmpinvoice->addline($langs->trans("TimeSpentForInvoice", $username).' : '.$qtyhourtext, $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;