charges.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. <?php
  2. /* Copyright (C) 2004-2014 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2005-2013 Regis Houssin <regis.houssin@capnetworks.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/compta/sociales/charges.php
  20. * \ingroup tax
  21. * \brief Social contribution car page
  22. */
  23. require '../../main.inc.php';
  24. require_once DOL_DOCUMENT_ROOT.'/compta/sociales/class/chargesociales.class.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formsocialcontrib.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/tax.lib.php';
  27. $langs->load("compta");
  28. $langs->load("bills");
  29. $id=GETPOST('id','int');
  30. $action=GETPOST("action");
  31. $confirm=GETPOST('confirm');
  32. // Security check
  33. $socid = GETPOST('socid','int');
  34. if ($user->societe_id) $socid=$user->societe_id;
  35. $result = restrictedArea($user, 'tax', $id, 'chargesociales','charges');
  36. /* *************************************************************************** */
  37. /* */
  38. /* Actions */
  39. /* */
  40. /* *************************************************************************** */
  41. // Classify paid
  42. if ($action == 'confirm_paid' && $confirm == 'yes')
  43. {
  44. $chargesociales = new ChargeSociales($db);
  45. $chargesociales->fetch($id);
  46. $result = $chargesociales->set_paid($user);
  47. }
  48. // Delete social contribution
  49. if ($action == 'confirm_delete' && $confirm == 'yes')
  50. {
  51. $chargesociales=new ChargeSociales($db);
  52. $chargesociales->fetch($id);
  53. $result=$chargesociales->delete($user);
  54. if ($result > 0)
  55. {
  56. header("Location: index.php");
  57. exit;
  58. }
  59. else
  60. {
  61. setEventMessage($chargesociales->error, 'errors');
  62. }
  63. }
  64. // Add social contribution
  65. if ($action == 'add' && $user->rights->tax->charges->creer)
  66. {
  67. $dateech=@dol_mktime($_POST["echhour"],$_POST["echmin"],$_POST["echsec"],$_POST["echmonth"],$_POST["echday"],$_POST["echyear"]);
  68. $dateperiod=@dol_mktime($_POST["periodhour"],$_POST["periodmin"],$_POST["periodsec"],$_POST["periodmonth"],$_POST["periodday"],$_POST["periodyear"]);
  69. if (! $dateech)
  70. {
  71. setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentities("DateDue")), 'errors');
  72. $action = 'create';
  73. }
  74. elseif (! $dateperiod)
  75. {
  76. setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentities("Period")), 'errors');
  77. $action = 'create';
  78. }
  79. elseif (! $_POST["actioncode"] > 0)
  80. {
  81. setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentities("Type")), 'errors');
  82. $action = 'create';
  83. }
  84. elseif (! $_POST["amount"])
  85. {
  86. setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentities("Amount")), 'errors');
  87. $action = 'create';
  88. }
  89. else
  90. {
  91. $chargesociales=new ChargeSociales($db);
  92. $chargesociales->type=$_POST["actioncode"];
  93. $chargesociales->lib=$_POST["label"];
  94. $chargesociales->date_ech=$dateech;
  95. $chargesociales->periode=$dateperiod;
  96. $chargesociales->amount=$_POST["amount"];
  97. $id=$chargesociales->create($user);
  98. if ($id <= 0)
  99. {
  100. setEventMessage($chargesociales->error, 'errors');
  101. }
  102. }
  103. }
  104. if ($action == 'update' && ! $_POST["cancel"] && $user->rights->tax->charges->creer)
  105. {
  106. $dateech=dol_mktime($_POST["echhour"],$_POST["echmin"],$_POST["echsec"],$_POST["echmonth"],$_POST["echday"],$_POST["echyear"]);
  107. $dateperiod=dol_mktime($_POST["periodhour"],$_POST["periodmin"],$_POST["periodsec"],$_POST["periodmonth"],$_POST["periodday"],$_POST["periodyear"]);
  108. if (! $dateech)
  109. {
  110. setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentities("DateDue")), 'errors');
  111. $action = 'edit';
  112. }
  113. elseif (! $dateperiod)
  114. {
  115. setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentities("Period")), 'errors');
  116. $action = 'edit';
  117. }
  118. else
  119. {
  120. $chargesociales=new ChargeSociales($db);
  121. $result=$chargesociales->fetch($_GET["id"]);
  122. $chargesociales->lib=$_POST["label"];
  123. $chargesociales->date_ech=$dateech;
  124. $chargesociales->periode=$dateperiod;
  125. $result=$chargesociales->update($user);
  126. if ($result <= 0)
  127. {
  128. setEventMessage($chargesociales->error, 'errors');
  129. }
  130. }
  131. }
  132. // Action clone object
  133. if ($action == 'confirm_clone' && $confirm != 'yes') { $action=''; }
  134. if ($action == 'confirm_clone' && $confirm == 'yes' && ($user->rights->tax->charges->creer))
  135. {
  136. $db->begin();
  137. $originalId = $id;
  138. $object = new ChargeSociales($db);
  139. $object->fetch($id);
  140. if ($object->id > 0)
  141. {
  142. $object->paye = 0;
  143. $object->id = $object->ref = null;
  144. if(GETPOST('clone_for_next_month') != '') {
  145. $object->date_ech = strtotime('+1month', $object->date_ech);
  146. $object->periode = strtotime('+1month', $object->periode);
  147. }
  148. if ($object->check())
  149. {
  150. $id = $object->create($user);
  151. if ($id > 0)
  152. {
  153. $db->commit();
  154. $db->close();
  155. header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
  156. exit;
  157. }
  158. else
  159. {
  160. $id=$originalId;
  161. $db->rollback();
  162. setEventMessages($object->error,$object->errors, 'errors');
  163. }
  164. }
  165. }
  166. else
  167. {
  168. $db->rollback();
  169. dol_print_error($db,$object->error);
  170. }
  171. }
  172. /*
  173. * View
  174. */
  175. $form = new Form($db);
  176. $formsocialcontrib = new FormSocialContrib($db);
  177. $help_url='EN:Module_Taxes_and_social_contributions|FR:Module Taxes et dividendes|ES:M&oacute;dulo Impuestos y cargas sociales (IVA, impuestos)';
  178. llxHeader("",$langs->trans("SocialContribution"),$help_url);
  179. // Mode creation
  180. if ($action == 'create')
  181. {
  182. print_fiche_titre($langs->trans("NewSocialContribution"));
  183. print "<br>\n";
  184. $var=false;
  185. print '<form name="charge" method="post" action="'.$_SERVER["PHP_SELF"].'">';
  186. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  187. print '<input type="hidden" name="action" value="add">';
  188. print "<table class=\"noborder\" width=\"100%\">";
  189. print "<tr class=\"liste_titre\">";
  190. print '<td>';
  191. print '&nbsp;';
  192. print '</td><td align="left">';
  193. print $langs->trans("Label");
  194. print '</td><td align="left">';
  195. print $langs->trans("Type");
  196. print '</td><td align="center">';
  197. print $langs->trans("PeriodEndDate");
  198. print '</td><td align="right">';
  199. print $langs->trans("Amount");
  200. print '</td><td align="center">';
  201. print $langs->trans("DateDue");
  202. print '</td><td align="left">';
  203. print '&nbsp;';
  204. print '</td>';
  205. print "</tr>\n";
  206. print '<tr '.$bc[$var].' valign="top">';
  207. print '<td>&nbsp;</td>';
  208. // Label
  209. print '<td align="left"><input type="text" size="34" name="label" class="flat" value="'.GETPOST('label').'"></td>';
  210. // Type
  211. print '<td align="left">';
  212. $formsocialcontrib->select_type_socialcontrib(GETPOST("actioncode")?GETPOST("actioncode"):'','actioncode',1);
  213. print '</td>';
  214. // Date end period
  215. print '<td align="center">';
  216. print $form->select_date(! empty($dateperiod)?$dateperiod:'-1', 'period', 0, 0, 0, 'charge', 1);
  217. print '</td>';
  218. print '<td align="right"><input type="text" size="6" name="amount" class="flat" value="'.GETPOST('amount').'"></td>';
  219. print '<td align="center">';
  220. print $form->select_date(! empty($dateech)?$dateech:'-1', 'ech', 0, 0, 0, 'charge', 1);
  221. print '</td>';
  222. print '<td align="center"><input type="submit" class="button" value="'.$langs->trans("Add").'"></td>';
  223. print '</tr>';
  224. print '</table>';
  225. print '</form>';
  226. }
  227. /* *************************************************************************** */
  228. /* */
  229. /* Mode fiche */
  230. /* */
  231. /* *************************************************************************** */
  232. if ($id > 0)
  233. {
  234. $object = new ChargeSociales($db);
  235. $result=$object->fetch($id);
  236. if ($result > 0)
  237. {
  238. $head=tax_prepare_head($object);
  239. dol_fiche_head($head, 'card', $langs->trans("SocialContribution"),0,'bill');
  240. // Clone confirmation
  241. if ($action === 'clone')
  242. {
  243. $formclone=array(
  244. array('type' => 'checkbox', 'name' => 'clone_for_next_month','label' => $langs->trans("CloneTaxForNextMonth"), 'value' => 1),
  245. );
  246. print $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id,$langs->trans('CloneTax'),$langs->trans('ConfirmCloneTax',$object->ref),'confirm_clone',$formclone,'yes');
  247. }
  248. // Confirmation de la suppression de la charge
  249. if ($action == 'paid')
  250. {
  251. $text=$langs->trans('ConfirmPaySocialContribution');
  252. print $form->formconfirm($_SERVER["PHP_SELF"]."?id=".$object->id,$langs->trans('PaySocialContribution'),$text,"confirm_paid",'','',2);
  253. }
  254. if ($action == 'delete')
  255. {
  256. $text=$langs->trans('ConfirmDeleteSocialContribution');
  257. print $form->formconfirm($_SERVER['PHP_SELF'].'?id='.$object->id,$langs->trans('DeleteSocialContribution'),$text,'confirm_delete','','',2);
  258. }
  259. if ($action == 'edit')
  260. {
  261. print "<form name=\"charge\" action=\"charges.php?id=$object->id&amp;action=update\" method=\"post\">";
  262. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  263. }
  264. print '<table class="border" width="100%">';
  265. // Ref
  266. print '<tr><td width="25%">'.$langs->trans("Ref").'</td><td colspan="2">';
  267. print $form->showrefnav($object,'id');
  268. print "</td></tr>";
  269. // Label
  270. if ($action == 'edit')
  271. {
  272. print '<tr><td>'.$langs->trans("Label").'</td><td colspan="2">';
  273. print '<input type="text" name="label" size="40" value="'.$object->lib.'">';
  274. print '</td></tr>';
  275. }
  276. else
  277. {
  278. print '<tr><td>'.$langs->trans("Label").'</td><td colspan="2">'.$object->lib.'</td></tr>';
  279. }
  280. // Type
  281. print "<tr><td>".$langs->trans("Type")."</td><td>".$object->type_libelle."</td>";
  282. $rowspan=5;
  283. print '<td rowspan="'.$rowspan.'" valign="top">';
  284. /*
  285. * Payments
  286. */
  287. $sql = "SELECT p.rowid, p.num_paiement, datep as dp, p.amount,";
  288. $sql.= "c.code as type_code,c.libelle as paiement_type";
  289. $sql.= " FROM ".MAIN_DB_PREFIX."paiementcharge as p";
  290. $sql.= ", ".MAIN_DB_PREFIX."c_paiement as c ";
  291. $sql.= ", ".MAIN_DB_PREFIX."chargesociales as cs";
  292. $sql.= " WHERE p.fk_charge = ".$id;
  293. $sql.= " AND p.fk_charge = cs.rowid";
  294. $sql.= " AND cs.entity = ".$conf->entity;
  295. $sql.= " AND p.fk_typepaiement = c.id";
  296. $sql.= " ORDER BY dp DESC";
  297. //print $sql;
  298. $resql = $db->query($sql);
  299. if ($resql)
  300. {
  301. $num = $db->num_rows($resql);
  302. $i = 0; $total = 0;
  303. echo '<table class="nobordernopadding" width="100%">';
  304. print '<tr class="liste_titre">';
  305. print '<td>'.$langs->trans("RefPayment").'</td>';
  306. print '<td>'.$langs->trans("Date").'</td>';
  307. print '<td>'.$langs->trans("Type").'</td>';
  308. print '<td align="right">'.$langs->trans("Amount").'</td>';
  309. print '<td>&nbsp;</td>';
  310. print '</tr>';
  311. $var=True;
  312. while ($i < $num)
  313. {
  314. $objp = $db->fetch_object($resql);
  315. $var=!$var;
  316. print "<tr ".$bc[$var]."><td>";
  317. print '<a href="'.DOL_URL_ROOT.'/compta/payment_sc/card.php?id='.$objp->rowid.'">'.img_object($langs->trans("Payment"),"payment").' '.$objp->rowid.'</a></td>';
  318. print '<td>'.dol_print_date($db->jdate($objp->dp),'day')."</td>\n";
  319. $labeltype=$langs->trans("PaymentType".$objp->type_code)!=("PaymentType".$objp->type_code)?$langs->trans("PaymentType".$objp->type_code):$objp->paiement_type;
  320. print "<td>".$labeltype.' '.$objp->num_paiement."</td>\n";
  321. print '<td align="right">'.price($objp->amount)."</td><td>&nbsp;".$langs->trans("Currency".$conf->currency)."</td>\n";
  322. print "</tr>";
  323. $totalpaye += $objp->amount;
  324. $i++;
  325. }
  326. if ($object->paye == 0)
  327. {
  328. print "<tr><td colspan=\"2\" align=\"right\">".$langs->trans("AlreadyPaid")." :</td><td align=\"right\"><b>".price($totalpaye)."</b></td><td>&nbsp;".$langs->trans("Currency".$conf->currency)."</td></tr>\n";
  329. print "<tr><td colspan=\"2\" align=\"right\">".$langs->trans("AmountExpected")." :</td><td align=\"right\" bgcolor=\"#d0d0d0\">".price($object->amount)."</td><td bgcolor=\"#d0d0d0\">&nbsp;".$langs->trans("Currency".$conf->currency)."</td></tr>\n";
  330. $resteapayer = $object->amount - $totalpaye;
  331. print "<tr><td colspan=\"2\" align=\"right\">".$langs->trans("RemainderToPay")." :</td>";
  332. print "<td align=\"right\" bgcolor=\"#f0f0f0\"><b>".price($resteapayer)."</b></td><td bgcolor=\"#f0f0f0\">&nbsp;".$langs->trans("Currency".$conf->currency)."</td></tr>\n";
  333. }
  334. print "</table>";
  335. $db->free($resql);
  336. }
  337. else
  338. {
  339. dol_print_error($db);
  340. }
  341. print "</td>";
  342. print "</tr>";
  343. // Period end date
  344. print "<tr><td>".$langs->trans("PeriodEndDate")."</td>";
  345. print "<td>";
  346. if ($action == 'edit')
  347. {
  348. print $form->select_date($object->periode, 'period', 0, 0, 0, 'charge', 1);
  349. }
  350. else
  351. {
  352. print dol_print_date($object->periode,"day");
  353. }
  354. print "</td></tr>";
  355. // Due date
  356. if ($action == 'edit')
  357. {
  358. print '<tr><td>'.$langs->trans("DateDue")."</td><td>";
  359. print $form->select_date($object->date_ech, 'ech', 0, 0, 0, 'charge', 1);
  360. print "</td></tr>";
  361. }
  362. else {
  363. print "<tr><td>".$langs->trans("DateDue")."</td><td>".dol_print_date($object->date_ech,'day')."</td></tr>";
  364. }
  365. // Amount
  366. print '<tr><td>'.$langs->trans("AmountTTC").'</td><td>'.price($object->amount,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';
  367. // Status
  368. print '<tr><td>'.$langs->trans("Status").'</td><td>'.$object->getLibStatut(4, $totalpaye).'</td></tr>';
  369. print '</table>';
  370. if ($action == 'edit')
  371. {
  372. print '<br><div align="center">';
  373. print '<input type="submit" class="button" name="save" value="'.$langs->trans("Save").'">';
  374. print ' &nbsp; ';
  375. print '<input type="submit" class="button" name="cancel" value="'.$langs->trans("Cancel").'">';
  376. print '</div';
  377. }
  378. if ($action == 'edit') print "</form>\n";
  379. dol_fiche_end();
  380. /*
  381. * Boutons actions
  382. */
  383. if ($action != 'edit')
  384. {
  385. print "<div class=\"tabsAction\">\n";
  386. // Edit
  387. if ($user->rights->tax->charges->creer)
  388. {
  389. print "<a class=\"butAction\" href=\"".DOL_URL_ROOT."/compta/sociales/charges.php?id=$object->id&amp;action=edit\">".$langs->trans("Modify")."</a>";
  390. }
  391. // Emettre paiement
  392. if ($object->paye == 0 && ((price2num($object->amount) < 0 && price2num($resteapayer, 'MT') < 0) || (price2num($object->amount) > 0 && price2num($resteapayer, 'MT') > 0)) && $user->rights->tax->charges->creer)
  393. {
  394. print "<a class=\"butAction\" href=\"".DOL_URL_ROOT."/compta/paiement_charge.php?id=$object->id&amp;action=create\">".$langs->trans("DoPayment")."</a>";
  395. }
  396. // Classify 'paid'
  397. if ($object->paye == 0 && round($resteapayer) <=0 && $user->rights->tax->charges->creer)
  398. {
  399. print "<a class=\"butAction\" href=\"".DOL_URL_ROOT."/compta/sociales/charges.php?id=$object->id&amp;action=paid\">".$langs->trans("ClassifyPaid")."</a>";
  400. }
  401. // Clone
  402. if ($user->rights->tax->charges->creer)
  403. {
  404. print "<a class=\"butAction\" href=\"".dol_buildpath("/compta/sociales/charges.php",1). "?id=$object->id&amp;action=clone\">".$langs->trans("ToClone")."</a>";
  405. }
  406. // Delete
  407. if ($user->rights->tax->charges->supprimer)
  408. {
  409. print "<a class=\"butActionDelete\" href=\"".DOL_URL_ROOT."/compta/sociales/charges.php?id=$object->id&amp;action=delete\">".$langs->trans("Delete")."</a>";
  410. }
  411. print "</div>";
  412. }
  413. }
  414. else
  415. {
  416. /* Charge non trouv� */
  417. dol_print_error('',$object->error);
  418. }
  419. }
  420. llxFooter();
  421. $db->close();