paiement_salary.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. /* Copyright (C) 2004-2014 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2016-2018 Frédéric France <frederic.france@netlogic.fr>
  4. * Copyright (C) 2021 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/salaries/paiement_salary.php
  21. * \ingroup salary
  22. * \brief Page to add payment of a salary
  23. */
  24. // Load Dolibarr environment
  25. require '../main.inc.php';
  26. require_once DOL_DOCUMENT_ROOT.'/salaries/class/salary.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/salaries/class/paymentsalary.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  29. // Load translation files required by the page
  30. $langs->loadLangs(array("banks", "bills"));
  31. $action = GETPOST('action', 'alpha');
  32. $cancel = GETPOST('cancel', 'alpha');
  33. $confirm = GETPOST('confirm', 'alpha');
  34. $id = GETPOSTINT('id');
  35. $ref = GETPOST('ref', 'alpha');
  36. $amounts = array();
  37. $object = new Salary($db);
  38. if ($id > 0 || !empty($ref)) {
  39. $object->fetch($id, $ref);
  40. }
  41. // Security check
  42. $socid = GETPOST("socid", "int");
  43. if ($user->socid > 0) {
  44. $socid = $user->socid;
  45. }
  46. restrictedArea($user, 'salaries', $object->id, 'salary', '');
  47. /*
  48. * Actions
  49. */
  50. if (($action == 'add_payment' || ($action == 'confirm_paiement' && $confirm == 'yes')) && $user->hasRight('salaries', 'write')) {
  51. $error = 0;
  52. if ($cancel) {
  53. $loc = DOL_URL_ROOT.'/salaries/card.php?id='.$id;
  54. header("Location: ".$loc);
  55. exit;
  56. }
  57. $datepaye = dol_mktime(GETPOST("rehour", 'int'), GETPOST("remin", 'int'), GETPOST("resec", 'int'), GETPOST("remonth", 'int'), GETPOST("reday", 'int'), GETPOST("reyear", 'int'), 'tzuserrel');
  58. if (!(GETPOST("paiementtype", 'int') > 0)) {
  59. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode")), null, 'errors');
  60. $error++;
  61. $action = 'create';
  62. }
  63. if ($datepaye == '') {
  64. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Date")), null, 'errors');
  65. $error++;
  66. $action = 'create';
  67. }
  68. if (isModEnabled("banque") && !(GETPOST("accountid", 'int') > 0)) {
  69. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountToDebit")), null, 'errors');
  70. $error++;
  71. $action = 'create';
  72. }
  73. // Read possible payments
  74. foreach ($_POST as $key => $value) {
  75. if (substr($key, 0, 7) == 'amount_') {
  76. $other_chid = substr($key, 7);
  77. $amounts[$other_chid] = price2num(GETPOST($key));
  78. }
  79. }
  80. if ($amounts[key($amounts)] <= 0) {
  81. $error++;
  82. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount")), null, 'errors');
  83. $action = 'create';
  84. }
  85. if (!$error) {
  86. $paymentid = 0;
  87. if (!$error) {
  88. $db->begin();
  89. // Create a line of payments
  90. $paiement = new PaymentSalary($db);
  91. $paiement->chid = $id;
  92. $paiement->datepaye = $datepaye;
  93. $paiement->amounts = $amounts; // Tableau de montant
  94. $paiement->paiementtype = GETPOST("paiementtype", 'alphanohtml');
  95. $paiement->num_payment = GETPOST("num_payment", 'alphanohtml');
  96. $paiement->note = GETPOST("note", 'restricthtml');
  97. $paiement->note_private = GETPOST("note", 'restricthtml');
  98. if (!$error) {
  99. $paymentid = $paiement->create($user, (GETPOST('closepaidsalary') == 'on' ? 1 : 0));
  100. if ($paymentid < 0) {
  101. $error++;
  102. setEventMessages($paiement->error, null, 'errors');
  103. $action = 'create';
  104. }
  105. }
  106. if (!$error) {
  107. $result = $paiement->addPaymentToBank($user, 'payment_salary', '(SalaryPayment)', GETPOST('accountid', 'int'), '', '');
  108. if (!($result > 0)) {
  109. $error++;
  110. setEventMessages($paiement->error, null, 'errors');
  111. $action = 'create';
  112. }
  113. }
  114. if (!$error) {
  115. $db->commit();
  116. $loc = DOL_URL_ROOT.'/salaries/card.php?id='.$id;
  117. header('Location: '.$loc);
  118. exit;
  119. } else {
  120. $db->rollback();
  121. }
  122. }
  123. }
  124. }
  125. /*
  126. * View
  127. */
  128. $form = new Form($db);
  129. $help_url = '';
  130. llxHeader('', '', $help_url);
  131. $salary = $object;
  132. // Formulaire de creation d'un paiement de charge
  133. if ($action == 'create') {
  134. $salary->accountid = $salary->fk_account ? $salary->fk_account : $salary->accountid;
  135. $salary->paiementtype = $salary->mode_reglement_id ? $salary->mode_reglement_id : $salary->paiementtype;
  136. $total = $salary->amount;
  137. if (!empty($conf->use_javascript_ajax)) {
  138. print "\n".'<script type="text/javascript">';
  139. //Add js for AutoFill
  140. print ' $(document).ready(function () {';
  141. print ' $(".AutoFillAmount").on(\'click touchstart\', function(){
  142. var amount = $(this).data("value");
  143. document.getElementById($(this).data(\'rowid\')).value = amount ;
  144. });';
  145. print ' });'."\n";
  146. print ' </script>'."\n";
  147. }
  148. print load_fiche_titre($langs->trans("DoPayment"));
  149. print "<br>\n";
  150. print '<form name="add_payment" action="'.$_SERVER['PHP_SELF'].'" method="post">';
  151. print '<input type="hidden" name="token" value="'.newToken().'">';
  152. print '<input type="hidden" name="id" value="'.$id.'">';
  153. print '<input type="hidden" name="chid" value="'.$id.'">';
  154. print '<input type="hidden" name="action" value="add_payment">';
  155. print dol_get_fiche_head();
  156. print '<table class="border centpercent">';
  157. print '<tr><td class="titlefieldcreate">'.$langs->trans("Ref").'</td><td><a href="'.DOL_URL_ROOT.'/salaries/card.php?id='.$id.'">'.$id.'</a></td></tr>';
  158. print '<tr><td>'.$langs->trans("DateStart")."</td><td>".dol_print_date($salary->datesp, 'day')."</td></tr>\n";
  159. print '<tr><td>'.$langs->trans("DateEnd")."</td><td>".dol_print_date($salary->dateep, 'day')."</td></tr>\n";
  160. print '<tr><td>'.$langs->trans("Label").'</td><td>'.$salary->label."</td></tr>\n";
  161. /*print '<tr><td>'.$langs->trans("DateDue")."</td><td>".dol_print_date($salary->date_ech,'day')."</td></tr>\n";
  162. print '<tr><td>'.$langs->trans("Amount")."</td><td>".price($salary->amount,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';*/
  163. $sql = "SELECT sum(p.amount) as total";
  164. $sql .= " FROM ".MAIN_DB_PREFIX."payment_salary as p";
  165. $sql .= " WHERE p.fk_salary = ".((int) $id);
  166. $resql = $db->query($sql);
  167. if ($resql) {
  168. $obj = $db->fetch_object($resql);
  169. $sumpaid = $obj->total;
  170. $db->free($resql);
  171. }
  172. /*print '<tr><td>'.$langs->trans("AlreadyPaid").'</td><td>'.price($sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';
  173. print '<tr><td class="tdtop">'.$langs->trans("RemainderToPay").'</td><td>'.price($total-$sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';*/
  174. print '<tr><td class="fieldrequired">'.$langs->trans("Date").'</td><td>';
  175. $datepaye = dol_mktime(GETPOST("rehour", 'int'), GETPOST("remin", 'int'), GETPOST("resec", 'int'), GETPOST("remonth", 'int'), GETPOST("reday", 'int'), GETPOST("reyear", 'int'));
  176. $datepayment = empty($conf->global->MAIN_AUTOFILL_DATE) ? (GETPOST("remonth") ? $datepaye : -1) : '';
  177. print $form->selectDate($datepayment, '', 1, 1, 0, "add_payment", 1, 1, 0, '', '', $salary->dateep, '', 1, $langs->trans("DateEnd"));
  178. print "</td>";
  179. print '</tr>';
  180. print '<tr><td class="fieldrequired">'.$langs->trans("PaymentMode").'</td><td>';
  181. $form->select_types_paiements(GETPOSTISSET("paiementtype") ? GETPOST("paiementtype") : $salary->type_payment, "paiementtype");
  182. print "</td>\n";
  183. print '</tr>';
  184. print '<tr>';
  185. print '<td class="fieldrequired">'.$langs->trans('AccountToDebit').'</td>';
  186. print '<td>';
  187. print img_picto('', 'bank_account', 'class="pictofixedwidth"');
  188. $form->select_comptes(GETPOSTISSET("accountid") ? GETPOST("accountid", 'int') : $salary->accountid, "accountid", 0, '', 1); // Show opend bank account list
  189. print '</td></tr>';
  190. // Number
  191. print '<tr><td>'.$langs->trans('Numero');
  192. print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
  193. print '</td>';
  194. print '<td><input name="num_payment" type="text" value="'.GETPOST('num_payment', 'alphanohtml').'"></td></tr>'."\n";
  195. print '<tr>';
  196. print '<td class="tdtop">'.$langs->trans("Comments").'</td>';
  197. print '<td class="tdtop"><textarea name="note" wrap="soft" cols="60" rows="'.ROWS_3.'"></textarea></td>';
  198. print '</tr>';
  199. print '</table>';
  200. print dol_get_fiche_end();
  201. /*
  202. * Autres charges impayees
  203. */
  204. $num = 1;
  205. $i = 0;
  206. print '<table class="noborder centpercent">';
  207. print '<tr class="liste_titre">';
  208. //print '<td>'.$langs->trans("SocialContribution").'</td>';
  209. print '<td class="left">'.$langs->trans("DateEnd").'</td>';
  210. print '<td class="right">'.$langs->trans("Amount").'</td>';
  211. print '<td class="right">'.$langs->trans("AlreadyPaid").'</td>';
  212. print '<td class="right">'.$langs->trans("RemainderToPay").'</td>';
  213. print '<td class="center">'.$langs->trans("Amount").'</td>';
  214. print "</tr>\n";
  215. $total = 0;
  216. $totalrecu = 0;
  217. while ($i < $num) {
  218. $objp = $salary;
  219. print '<tr class="oddeven">';
  220. if ($objp->dateep > 0) {
  221. print '<td class="left">'.dol_print_date($objp->dateep, 'day').'</td>'."\n";
  222. } else {
  223. print '<td align="center"><b>!!!</b></td>'."\n";
  224. }
  225. print '<td class="right">'.price($objp->amount)."</td>";
  226. print '<td class="right">'.price($sumpaid)."</td>";
  227. print '<td class="right">'.price($objp->amount - $sumpaid)."</td>";
  228. print '<td class="center">';
  229. if ($sumpaid < $objp->amount) {
  230. $namef = "amount_".$objp->id;
  231. $nameRemain = "remain_".$objp->id;
  232. /* Disabled, we autofil the amount with remain to pay by default
  233. if (!empty($conf->use_javascript_ajax)) {
  234. print img_picto("Auto fill", 'rightarrow', "class='AutoFillAmount' data-rowid='".$namef."' data-value='".($objp->amount - $sumpaid)."'");
  235. } */
  236. $valuetoshow = GETPOSTISSET($namef) ? GETPOST($namef) : ($objp->amount - $sumpaid);
  237. print '<input type=hidden class="sum_remain" name="'.$nameRemain.'" value="'.$valuetoshow.'">';
  238. print '<input type="text" class="right width75" name="'.$namef.'" id="'.$namef.'" value="'.$valuetoshow.'">';
  239. } else {
  240. print '-';
  241. }
  242. print "</td>";
  243. print "</tr>\n";
  244. $total += $objp->total;
  245. $total_ttc += $objp->total_ttc;
  246. $totalrecu += $objp->am;
  247. $i++;
  248. }
  249. if ($i > 1) {
  250. // Print total
  251. print '<tr class="oddeven">';
  252. print '<td colspan="2" class="left">'.$langs->trans("Total").':</td>';
  253. print '<td class="right"><b>'.price($total_ttc).'</b></td>';
  254. print '<td class="right"><b>'.price($totalrecu).'</b></td>';
  255. print '<td class="right"><b>'.price($total_ttc - $totalrecu).'</b></td>';
  256. print '<td align="center">&nbsp;</td>';
  257. print "</tr>\n";
  258. }
  259. print "</table>";
  260. print '<br>';
  261. // Bouton Save payment
  262. print '<div class="center">';
  263. print '<div class="paddingbottom"><input type="checkbox" checked name="closepaidsalary" id="closepaidsalary"><label for="closepaidsalary">'.$langs->trans("ClosePaidSalaryAutomatically").'</label></div>';
  264. print $form->buttonsSaveCancel("ToMakePayment", "Cancel", '', true);
  265. print '</div>';
  266. print "</form>\n";
  267. }
  268. llxFooter();
  269. $db->close();