paiement_salary.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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->id = $id;
  92. $paiement->chid = $id; // deprecated
  93. $paiement->datep = $datepaye;
  94. $paiement->amounts = $amounts; // Tableau de montant
  95. $paiement->paiementtype = GETPOST("paiementtype", 'alphanohtml');
  96. $paiement->num_payment = GETPOST("num_payment", 'alphanohtml');
  97. $paiement->note = GETPOST("note", 'restricthtml');
  98. $paiement->note_private = GETPOST("note", 'restricthtml');
  99. if (!$error) {
  100. $paymentid = $paiement->create($user, (GETPOST('closepaidsalary') == 'on' ? 1 : 0));
  101. if ($paymentid < 0) {
  102. $error++;
  103. setEventMessages($paiement->error, null, 'errors');
  104. $action = 'create';
  105. }
  106. }
  107. if (!$error) {
  108. $result = $paiement->addPaymentToBank($user, 'payment_salary', '(SalaryPayment)', GETPOST('accountid', 'int'), '', '');
  109. if (!($result > 0)) {
  110. $error++;
  111. setEventMessages($paiement->error, null, 'errors');
  112. $action = 'create';
  113. }
  114. }
  115. if (!$error) {
  116. $db->commit();
  117. $loc = DOL_URL_ROOT.'/salaries/card.php?id='.$id;
  118. header('Location: '.$loc);
  119. exit;
  120. } else {
  121. $db->rollback();
  122. }
  123. }
  124. }
  125. }
  126. /*
  127. * View
  128. */
  129. $form = new Form($db);
  130. $help_url = '';
  131. llxHeader('', '', $help_url);
  132. $salary = $object;
  133. // Formulaire de creation d'un paiement de charge
  134. if ($action == 'create') {
  135. $salary->accountid = $salary->fk_account ? $salary->fk_account : $salary->accountid;
  136. $salary->paiementtype = $salary->mode_reglement_id ? $salary->mode_reglement_id : $salary->paiementtype;
  137. $total = $salary->amount;
  138. if (!empty($conf->use_javascript_ajax)) {
  139. print "\n".'<script type="text/javascript">';
  140. //Add js for AutoFill
  141. print ' $(document).ready(function () {';
  142. print ' $(".AutoFillAmount").on(\'click touchstart\', function(){
  143. var amount = $(this).data("value");
  144. document.getElementById($(this).data(\'rowid\')).value = amount ;
  145. });';
  146. print ' });'."\n";
  147. print ' </script>'."\n";
  148. }
  149. print load_fiche_titre($langs->trans("DoPayment"));
  150. print "<br>\n";
  151. print '<form name="add_payment" action="'.$_SERVER['PHP_SELF'].'" method="post">';
  152. print '<input type="hidden" name="token" value="'.newToken().'">';
  153. print '<input type="hidden" name="id" value="'.$id.'">';
  154. print '<input type="hidden" name="chid" value="'.$id.'">';
  155. print '<input type="hidden" name="action" value="add_payment">';
  156. print dol_get_fiche_head();
  157. print '<table class="border centpercent">';
  158. print '<tr><td class="titlefieldcreate">'.$langs->trans("Ref").'</td><td><a href="'.DOL_URL_ROOT.'/salaries/card.php?id='.$id.'">'.$id.'</a></td></tr>';
  159. print '<tr><td>'.$langs->trans("DateStart")."</td><td>".dol_print_date($salary->datesp, 'day')."</td></tr>\n";
  160. print '<tr><td>'.$langs->trans("DateEnd")."</td><td>".dol_print_date($salary->dateep, 'day')."</td></tr>\n";
  161. print '<tr><td>'.$langs->trans("Label").'</td><td>'.$salary->label."</td></tr>\n";
  162. /*print '<tr><td>'.$langs->trans("DateDue")."</td><td>".dol_print_date($salary->date_ech,'day')."</td></tr>\n";
  163. print '<tr><td>'.$langs->trans("Amount")."</td><td>".price($salary->amount,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';*/
  164. $sql = "SELECT sum(p.amount) as total";
  165. $sql .= " FROM ".MAIN_DB_PREFIX."payment_salary as p";
  166. $sql .= " WHERE p.fk_salary = ".((int) $id);
  167. $resql = $db->query($sql);
  168. if ($resql) {
  169. $obj = $db->fetch_object($resql);
  170. $sumpaid = $obj->total;
  171. $db->free($resql);
  172. }
  173. /*print '<tr><td>'.$langs->trans("AlreadyPaid").'</td><td>'.price($sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';
  174. print '<tr><td class="tdtop">'.$langs->trans("RemainderToPay").'</td><td>'.price($total-$sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';*/
  175. print '<tr><td class="fieldrequired">'.$langs->trans("Date").'</td><td>';
  176. $datepaye = dol_mktime(GETPOST("rehour", 'int'), GETPOST("remin", 'int'), GETPOST("resec", 'int'), GETPOST("remonth", 'int'), GETPOST("reday", 'int'), GETPOST("reyear", 'int'));
  177. $datepayment = !getDolGlobalString('MAIN_AUTOFILL_DATE') ? (GETPOST("remonth") ? $datepaye : -1) : '';
  178. print $form->selectDate($datepayment, '', 1, 1, 0, "add_payment", 1, 1, 0, '', '', $salary->dateep, '', 1, $langs->trans("DateEnd"));
  179. print "</td>";
  180. print '</tr>';
  181. print '<tr><td class="fieldrequired">'.$langs->trans("PaymentMode").'</td><td>';
  182. $form->select_types_paiements(GETPOSTISSET("paiementtype") ? GETPOST("paiementtype") : $salary->type_payment, "paiementtype");
  183. print "</td>\n";
  184. print '</tr>';
  185. print '<tr>';
  186. print '<td class="fieldrequired">'.$langs->trans('AccountToDebit').'</td>';
  187. print '<td>';
  188. print img_picto('', 'bank_account', 'class="pictofixedwidth"');
  189. $form->select_comptes(GETPOSTISSET("accountid") ? GETPOST("accountid", 'int') : $salary->accountid, "accountid", 0, '', 1); // Show opend bank account list
  190. print '</td></tr>';
  191. // Number
  192. print '<tr><td>'.$langs->trans('Numero');
  193. print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
  194. print '</td>';
  195. print '<td><input name="num_payment" type="text" value="'.GETPOST('num_payment', 'alphanohtml').'"></td></tr>'."\n";
  196. print '<tr>';
  197. print '<td class="tdtop">'.$langs->trans("Comments").'</td>';
  198. print '<td class="tdtop"><textarea name="note" wrap="soft" cols="60" rows="'.ROWS_3.'"></textarea></td>';
  199. print '</tr>';
  200. print '</table>';
  201. print dol_get_fiche_end();
  202. /*
  203. * Autres charges impayees
  204. */
  205. $num = 1;
  206. $i = 0;
  207. print '<table class="noborder centpercent">';
  208. print '<tr class="liste_titre">';
  209. //print '<td>'.$langs->trans("SocialContribution").'</td>';
  210. print '<td class="left">'.$langs->trans("DateEnd").'</td>';
  211. print '<td class="right">'.$langs->trans("Amount").'</td>';
  212. print '<td class="right">'.$langs->trans("AlreadyPaid").'</td>';
  213. print '<td class="right">'.$langs->trans("RemainderToPay").'</td>';
  214. print '<td class="center">'.$langs->trans("Amount").'</td>';
  215. print "</tr>\n";
  216. $total = 0;
  217. $totalrecu = 0;
  218. while ($i < $num) {
  219. $objp = $salary;
  220. print '<tr class="oddeven">';
  221. if ($objp->dateep > 0) {
  222. print '<td class="left">'.dol_print_date($objp->dateep, 'day').'</td>'."\n";
  223. } else {
  224. print '<td align="center"><b>!!!</b></td>'."\n";
  225. }
  226. print '<td class="right">'.price($objp->amount)."</td>";
  227. print '<td class="right">'.price($sumpaid)."</td>";
  228. print '<td class="right">'.price($objp->amount - $sumpaid)."</td>";
  229. print '<td class="center">';
  230. if ($sumpaid < $objp->amount) {
  231. $namef = "amount_".$objp->id;
  232. $nameRemain = "remain_".$objp->id;
  233. /* Disabled, we autofil the amount with remain to pay by default
  234. if (!empty($conf->use_javascript_ajax)) {
  235. print img_picto("Auto fill", 'rightarrow', "class='AutoFillAmount' data-rowid='".$namef."' data-value='".($objp->amount - $sumpaid)."'");
  236. } */
  237. $valuetoshow = GETPOSTISSET($namef) ? GETPOST($namef) : ($objp->amount - $sumpaid);
  238. print '<input type=hidden class="sum_remain" name="'.$nameRemain.'" value="'.$valuetoshow.'">';
  239. print '<input type="text" class="right width75" name="'.$namef.'" id="'.$namef.'" value="'.$valuetoshow.'">';
  240. } else {
  241. print '-';
  242. }
  243. print "</td>";
  244. print "</tr>\n";
  245. $total += $objp->total;
  246. $total_ttc += $objp->total_ttc;
  247. $totalrecu += $objp->am;
  248. $i++;
  249. }
  250. if ($i > 1) {
  251. // Print total
  252. print '<tr class="oddeven">';
  253. print '<td colspan="2" class="left">'.$langs->trans("Total").':</td>';
  254. print '<td class="right"><b>'.price($total_ttc).'</b></td>';
  255. print '<td class="right"><b>'.price($totalrecu).'</b></td>';
  256. print '<td class="right"><b>'.price($total_ttc - $totalrecu).'</b></td>';
  257. print '<td align="center">&nbsp;</td>';
  258. print "</tr>\n";
  259. }
  260. print "</table>";
  261. print '<br>';
  262. // Bouton Save payment
  263. print '<div class="center">';
  264. print '<div class="paddingbottom"><input type="checkbox" checked name="closepaidsalary" id="closepaidsalary"><label for="closepaidsalary">'.$langs->trans("ClosePaidSalaryAutomatically").'</label></div>';
  265. print $form->buttonsSaveCancel("ToMakePayment", "Cancel", '', true);
  266. print '</div>';
  267. print "</form>\n";
  268. }
  269. llxFooter();
  270. $db->close();