card.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. /* Copyright (C) 2015 Alexandre Spangaro <aspangaro.dolibarr@gmail.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/don/payment/card.php
  19. * \ingroup donations
  20. * \brief Tab payment of a donation
  21. */
  22. require '../../main.inc.php';
  23. require_once DOL_DOCUMENT_ROOT.'/don/class/don.class.php';
  24. require_once DOL_DOCUMENT_ROOT.'/don/class/paymentdonation.class.php';
  25. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/modules/facture/modules_facture.php';
  27. if (! empty($conf->banque->enabled)) require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  28. $langs->load('bills');
  29. $langs->load('banks');
  30. $langs->load('companies');
  31. // Security check
  32. $id=GETPOST('rowid')?GETPOST('rowid','int'):GETPOST('id','int');
  33. $action=GETPOST("action");
  34. $confirm=GETPOST('confirm');
  35. if ($user->societe_id) $socid=$user->societe_id;
  36. // TODO Add rule to restrict access payment
  37. //$result = restrictedArea($user, 'facture', $id,'');
  38. $payment = new PaymentDonation($db);
  39. if ($id > 0)
  40. {
  41. $result=$payment->fetch($id);
  42. if (! $result) dol_print_error($db,'Failed to get payment id '.$id);
  43. }
  44. /*
  45. * Actions
  46. */
  47. // Delete payment
  48. if ($action == 'confirm_delete' && $confirm == 'yes' && $user->rights->don->supprimer)
  49. {
  50. $db->begin();
  51. $result = $payment->delete($user);
  52. if ($result > 0)
  53. {
  54. $db->commit();
  55. header("Location: ".DOL_URL_ROOT."/don/index.php");
  56. exit;
  57. }
  58. else
  59. {
  60. setEventMessage($payment->error, 'errors');
  61. $db->rollback();
  62. }
  63. }
  64. // Create payment
  65. if ($action == 'confirm_valide' && $confirm == 'yes' && $user->rights->don->creer)
  66. {
  67. $db->begin();
  68. $result=$payment->valide();
  69. if ($result > 0)
  70. {
  71. $db->commit();
  72. $factures=array(); // TODO Get all id of invoices linked to this payment
  73. foreach($factures as $id)
  74. {
  75. $fac = new Facture($db);
  76. $fac->fetch($id);
  77. $outputlangs = $langs;
  78. if (! empty($_REQUEST['lang_id']))
  79. {
  80. $outputlangs = new Translate("",$conf);
  81. $outputlangs->setDefaultLang($_REQUEST['lang_id']);
  82. }
  83. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
  84. $fac->generateDocument($fac->modelpdf, $outputlangs);
  85. }
  86. }
  87. header('Location: card.php?id='.$payment->id);
  88. exit;
  89. }
  90. else
  91. {
  92. setEventMessage($payment->error);
  93. $db->rollback();
  94. }
  95. }
  96. /*
  97. * View
  98. */
  99. llxHeader();
  100. $don = new Don($db);
  101. $form = new Form($db);
  102. $h=0;
  103. $head[$h][0] = DOL_URL_ROOT.'/don/payment/card.php?id='.$id;
  104. $head[$h][1] = $langs->trans("Card");
  105. $hselected = $h;
  106. $h++;
  107. dol_fiche_head($head, $hselected, $langs->trans("DonationPayment"), 0, 'payment');
  108. /*
  109. * Confirm deleting of the payment
  110. */
  111. if ($action == 'delete')
  112. {
  113. print $form->formconfirm('card.php?id='.$payment->id, $langs->trans("DeletePayment"), $langs->trans("ConfirmDeletePayment"), 'confirm_delete','',0,2);
  114. }
  115. /*
  116. * Confirm validation of the payment
  117. */
  118. if ($action == 'valide')
  119. {
  120. $facid = $_GET['facid'];
  121. print $form->formconfirm('card.php?id='.$payment->id.'&amp;facid='.$facid, $langs->trans("ValidatePayment"), $langs->trans("ConfirmValidatePayment"), 'confirm_valide','',0,2);
  122. }
  123. print '<table class="border" width="100%">';
  124. // Ref
  125. print '<tr><td valign="top" width="20%">'.$langs->trans('Ref').'</td>';
  126. print '<td colspan="3">';
  127. print $form->showrefnav($payment,'id','',1,'rowid','id');
  128. print '</td></tr>';
  129. // Date
  130. print '<tr><td valign="top">'.$langs->trans('Date').'</td><td colspan="3">'.dol_print_date($payment->datep,'day').'</td></tr>';
  131. // Mode
  132. print '<tr><td valign="top">'.$langs->trans('Mode').'</td><td colspan="3">'.$langs->trans("PaymentType".$payment->type_code).'</td></tr>';
  133. // Number
  134. print '<tr><td valign="top">'.$langs->trans('Number').'</td><td colspan="3">'.$payment->num_payment.'</td></tr>';
  135. // Amount
  136. print '<tr><td valign="top">'.$langs->trans('Amount').'</td><td colspan="3">'.price($payment->amount, 0, $outputlangs, 1, -1, -1, $conf->currency).'</td></tr>';
  137. // Note
  138. print '<tr><td valign="top">'.$langs->trans('Note').'</td><td colspan="3">'.nl2br($payment->note).'</td></tr>';
  139. // Bank account
  140. if (! empty($conf->banque->enabled))
  141. {
  142. if ($payment->bank_account)
  143. {
  144. $bankline=new AccountLine($db);
  145. $bankline->fetch($payment->bank_line);
  146. print '<tr>';
  147. print '<td>'.$langs->trans('BankTransactionLine').'</td>';
  148. print '<td colspan="3">';
  149. print $bankline->getNomUrl(1,0,'showall');
  150. print '</td>';
  151. print '</tr>';
  152. }
  153. }
  154. print '</table>';
  155. /*
  156. * List of donations paid
  157. */
  158. $disable_delete = 0;
  159. $sql = 'SELECT d.rowid as did, d.paid, d.amount as d_amount, pd.amount';
  160. $sql.= ' FROM '.MAIN_DB_PREFIX.'payment_donation as pd,'.MAIN_DB_PREFIX.'don as d';
  161. $sql.= ' WHERE pd.fk_donation = d.rowid';
  162. $sql.= ' AND d.entity = '.$conf->entity;
  163. $sql.= ' AND pd.rowid = '.$id;
  164. dol_syslog("don/payment/card.php", LOG_DEBUG);
  165. $resql=$db->query($sql);
  166. if ($resql)
  167. {
  168. $num = $db->num_rows($resql);
  169. $i = 0;
  170. $total = 0;
  171. print '<br><table class="noborder" width="100%">';
  172. print '<tr class="liste_titre">';
  173. print '<td>'.$langs->trans('Donation').'</td>';
  174. print '<td align="right">'.$langs->trans('ExpectedToPay').'</td>';
  175. print '<td align="center">'.$langs->trans('Status').'</td>';
  176. print '<td align="right">'.$langs->trans('PayedByThisPayment').'</td>';
  177. print "</tr>\n";
  178. if ($num > 0)
  179. {
  180. $var=True;
  181. while ($i < $num)
  182. {
  183. $objp = $db->fetch_object($resql);
  184. $var=!$var;
  185. print '<tr '.$bc[$var].'>';
  186. // Ref
  187. print '<td>';
  188. $don->fetch($objp->did);
  189. print $don->getNomUrl(1);
  190. print "</td>\n";
  191. // Expected to pay
  192. print '<td align="right">'.price($objp->d_amount).'</td>';
  193. // Status
  194. print '<td align="center">'.$don->getLibStatut(4,$objp->amount).'</td>';
  195. // Amount payed
  196. print '<td align="right">'.price($objp->amount).'</td>';
  197. print "</tr>\n";
  198. if ($objp->paid == 1) // If at least one invoice is paid, disable delete
  199. {
  200. $disable_delete = 1;
  201. }
  202. $total = $total + $objp->amount;
  203. $i++;
  204. }
  205. }
  206. $var=!$var;
  207. print "</table>\n";
  208. $db->free($resql);
  209. }
  210. else
  211. {
  212. dol_print_error($db);
  213. }
  214. print '</div>';
  215. /*
  216. * Actions buttons
  217. */
  218. print '<div class="tabsAction">';
  219. /*
  220. if (! empty($conf->global->BILL_ADD_PAYMENT_VALIDATION))
  221. {
  222. if ($user->societe_id == 0 && $payment->statut == 0 && $_GET['action'] == '')
  223. {
  224. if ($user->rights->facture->paiement)
  225. {
  226. print '<a class="butAction" href="card.php?id='.$_GET['id'].'&amp;facid='.$objp->facid.'&amp;action=valide">'.$langs->trans('Valid').'</a>';
  227. }
  228. }
  229. }
  230. */
  231. if ($_GET['action'] == '')
  232. {
  233. if ($user->rights->don->supprimer)
  234. {
  235. if (! $disable_delete)
  236. {
  237. print '<a class="butActionDelete" href="card.php?id='.$_GET['id'].'&amp;action=delete">'.$langs->trans('Delete').'</a>';
  238. }
  239. else
  240. {
  241. print '<a class="butActionRefused" href="#" title="'.dol_escape_htmltag($langs->trans("CantRemovePaymentWithOneInvoicePaid")).'">'.$langs->trans('Delete').'</a>';
  242. }
  243. }
  244. }
  245. print '</div>';
  246. llxFooter();
  247. $db->close();