remise.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. /* Copyright (C) 2001-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2021 Laurent Destailleur <eldy@users.sourceforge.net>
  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 <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/comm/remise.php
  20. * \ingroup societe
  21. * \brief Page to edit relative discount of a customer
  22. */
  23. require '../main.inc.php';
  24. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  25. require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  26. // Load translation files required by the page
  27. $langs->loadLangs(array('companies', 'orders', 'bills'));
  28. $id = GETPOST("id", 'int');
  29. $socid = GETPOST('id', 'int') ?GETPOST('id', 'int') : GETPOST('socid', 'int');
  30. // Security check
  31. if ($user->socid > 0) {
  32. $socid = $user->socid;
  33. }
  34. $backtopage = GETPOST('backtopage', 'alpha');
  35. $cancel = GETPOST('cancel', 'aplha');
  36. $action = GETPOST('action', 'aZ09');
  37. // Security check
  38. if ($user->socid > 0) {
  39. $id = $user->socid;
  40. }
  41. $result = restrictedArea($user, 'societe', $id, '&societe', '', 'fk_soc', 'rowid', 0);
  42. /*
  43. * Actions
  44. */
  45. if ($cancel) {
  46. if (!empty($backtopage)) {
  47. header("Location: ".$backtopage);
  48. exit;
  49. } else {
  50. $action = '';
  51. }
  52. }
  53. if ($action == 'setremise') {
  54. $object = new Societe($db);
  55. $object->fetch($id);
  56. $discount_type = GETPOST('discount_type', 'int');
  57. if (!empty($discount_type)) {
  58. $result = $object->set_remise_supplier(price2num(GETPOST("remise")), GETPOST("note", "alphanohtml"), $user);
  59. } else {
  60. $result = $object->set_remise_client(price2num(GETPOST("remise")), GETPOST("note", "alphanohtml"), $user);
  61. }
  62. if ($result > 0) {
  63. if (!empty($backtopage)) {
  64. header("Location: ".$backtopage);
  65. exit;
  66. } else {
  67. header("Location: remise.php?id=".GETPOST("id", 'int'));
  68. exit;
  69. }
  70. } else {
  71. setEventMessages($object->error, $object->errors, 'errors');
  72. }
  73. }
  74. /*
  75. * View
  76. */
  77. $form = new Form($db);
  78. llxHeader();
  79. /*********************************************************************************
  80. *
  81. * Mode fiche
  82. *
  83. *********************************************************************************/
  84. if ($socid > 0) {
  85. // On recupere les donnees societes par l'objet
  86. $object = new Societe($db);
  87. $object->fetch($socid);
  88. $head = societe_prepare_head($object);
  89. $isCustomer = ($object->client == 1 || $object->client == 3);
  90. $isSupplier = $object->fournisseur == 1;
  91. print '<form method="POST" action="remise.php?id='.$object->id.'">';
  92. print '<input type="hidden" name="token" value="'.newToken().'">';
  93. print '<input type="hidden" name="action" value="setremise">';
  94. print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
  95. print dol_get_fiche_head($head, 'relativediscount', $langs->trans("ThirdParty"), -1, 'company');
  96. dol_banner_tab($object, 'socid', '', ($user->socid ? 0 : 1), 'rowid', 'nom');
  97. print '<div class="fichecenter">';
  98. print '<div class="underbanner clearboth"></div>';
  99. if (!$isCustomer && !$isSupplier) {
  100. print '<p class="opacitymedium">'.$langs->trans('ThirdpartyIsNeitherCustomerNorClientSoCannotHaveDiscounts').'</p>';
  101. print dol_get_fiche_end();
  102. print '</form>';
  103. // End of page
  104. llxFooter();
  105. $db->close();
  106. exit;
  107. }
  108. print '<table class="border centpercent">';
  109. if ($isCustomer) {
  110. // Customer discount
  111. print '<tr><td class="titlefield">';
  112. print $langs->trans("CustomerRelativeDiscount").'</td><td>'.price2num($object->remise_percent)."%</td></tr>";
  113. }
  114. if ($isSupplier) {
  115. // Supplier discount
  116. print '<tr><td class="titlefield">';
  117. print $langs->trans("SupplierRelativeDiscount").'</td><td>'.price2num($object->remise_supplier_percent)."%</td></tr>";
  118. }
  119. print '</table>';
  120. print '<br>';
  121. print load_fiche_titre($langs->trans("NewRelativeDiscount"), '', '');
  122. print '<div class="underbanner clearboth"></div>';
  123. /*if (! ($isCustomer && $isSupplier))
  124. {
  125. if ($isCustomer && ! $isSupplier) {
  126. print '<input type="hidden" name="discount_type" value="0" />';
  127. }
  128. if (! $isCustomer && $isSupplier) {
  129. print '<input type="hidden" name="discount_type" value="1" />';
  130. }
  131. }*/
  132. print '<table class="border centpercent">';
  133. if ($isCustomer || $isSupplier) {
  134. // Discount type
  135. print '<tr><td class="titlefield fieldrequired">'.$langs->trans('DiscountType').'</td><td>';
  136. if ($isCustomer) {
  137. print '<input type="radio" name="discount_type" id="discount_type_0" '.(GETPOSTISSET('discount_type') ? (GETPOST('discount_type', 'int') == 0 ? ' checked' : '') : ' checked').' value="0"> <label for="discount_type_0">'.$langs->trans('Customer').'</label>';
  138. }
  139. if ($isSupplier) {
  140. print ' <input type="radio" name="discount_type" id="discount_type_1"'.(GETPOSTISSET('discount_type') ? (GETPOST('discount_type', 'int') ? ' checked' : '') : ($isCustomer ? '' : ' checked')).' value="1"> <label for="discount_type_1">'.$langs->trans('Supplier').'</label>';
  141. }
  142. print '</td></tr>';
  143. }
  144. // New value
  145. print '<tr><td class="titlefield fieldrequired">';
  146. print $langs->trans("NewValue").'</td><td><input type="text" size="5" name="remise" value="'.dol_escape_htmltag(GETPOST("remise")).'">%</td></tr>';
  147. // Motif/Note
  148. print '<tr><td class="fieldrequired">';
  149. print $langs->trans("NoteReason").'</td><td><input type="text" size="60" name="note" value="'.dol_escape_htmltag(GETPOST("note", "alphanohtml")).'"></td></tr>';
  150. print "</table>";
  151. print '</div>';
  152. print dol_get_fiche_end();
  153. print $form->buttonsSaveCancel("Modify");
  154. print "</form>";
  155. print '<br>';
  156. if ($isCustomer) {
  157. if ($isSupplier) {
  158. print '<div class="fichecenter">';
  159. print '<div class="fichehalfleft">';
  160. print load_fiche_titre($langs->trans("CustomerDiscounts"), '', '');
  161. }
  162. /*
  163. * List log of all customer percent discounts
  164. */
  165. $sql = "SELECT rc.rowid, rc.remise_client as remise_percent, rc.note, rc.datec as dc,";
  166. $sql .= " u.login, u.rowid as user_id";
  167. $sql .= " FROM ".MAIN_DB_PREFIX."societe_remise as rc, ".MAIN_DB_PREFIX."user as u";
  168. $sql .= " WHERE rc.fk_soc = ".((int) $object->id);
  169. $sql .= " AND rc.entity IN (".getEntity('discount').")";
  170. $sql .= " AND u.rowid = rc.fk_user_author";
  171. $sql .= " ORDER BY rc.datec DESC";
  172. $resql = $db->query($sql);
  173. if ($resql) {
  174. print '<table class="noborder centpercent">';
  175. $tag = !$tag;
  176. print '<tr class="liste_titre">';
  177. print '<td width="160">'.$langs->trans("Date").'</td>';
  178. print '<td width="160" align="center">'.$langs->trans("CustomerRelativeDiscountShort").'</td>';
  179. print '<td class="left">'.$langs->trans("NoteReason").'</td>';
  180. print '<td class="center">'.$langs->trans("User").'</td>';
  181. print '</tr>';
  182. $num = $db->num_rows($resql);
  183. if ($num > 0) {
  184. $i = 0;
  185. while ($i < $num) {
  186. $obj = $db->fetch_object($resql);
  187. print '<tr class="oddeven">';
  188. print '<td>'.dol_print_date($db->jdate($obj->dc), "dayhour").'</td>';
  189. print '<td class="center">'.price2num($obj->remise_percent).'%</td>';
  190. print '<td class="left">'.$obj->note.'</td>';
  191. print '<td align="center"><a href="'.DOL_URL_ROOT.'/user/card.php?id='.$obj->user_id.'">'.img_object($langs->trans("ShowUser"), 'user').' '.$obj->login.'</a></td>';
  192. print '</tr>';
  193. $i++;
  194. }
  195. } else {
  196. print '<tr><td colspan="8" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
  197. }
  198. $db->free($resql);
  199. print "</table>";
  200. } else {
  201. dol_print_error($db);
  202. }
  203. }
  204. if ($isSupplier) {
  205. if ($isCustomer) {
  206. print '</div>'; // class="fichehalfleft"
  207. print '<div class="fichehalfright">';
  208. print load_fiche_titre($langs->trans("SupplierDiscounts"), '', '');
  209. }
  210. /*
  211. * List log of all supplier percent discounts
  212. */
  213. $sql = "SELECT rc.rowid, rc.remise_supplier as remise_percent, rc.note, rc.datec as dc,";
  214. $sql .= " u.login, u.rowid as user_id";
  215. $sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_supplier as rc, ".MAIN_DB_PREFIX."user as u";
  216. $sql .= " WHERE rc.fk_soc = ".((int) $object->id);
  217. $sql .= " AND rc.entity IN (".getEntity('discount').")";
  218. $sql .= " AND u.rowid = rc.fk_user_author";
  219. $sql .= " ORDER BY rc.datec DESC";
  220. $resql = $db->query($sql);
  221. if ($resql) {
  222. print '<table class="noborder centpercent">';
  223. $tag = !$tag;
  224. print '<tr class="liste_titre">';
  225. print '<td width="160">'.$langs->trans("Date").'</td>';
  226. print '<td width="160" align="center">'.$langs->trans("CustomerRelativeDiscountShort").'</td>';
  227. print '<td class="left">'.$langs->trans("NoteReason").'</td>';
  228. print '<td class="center">'.$langs->trans("User").'</td>';
  229. print '</tr>';
  230. $num = $db->num_rows($resql);
  231. if ($num > 0) {
  232. $i = 0;
  233. while ($i < $num) {
  234. $obj = $db->fetch_object($resql);
  235. print '<tr class="oddeven">';
  236. print '<td>'.dol_print_date($db->jdate($obj->dc), "dayhour").'</td>';
  237. print '<td class="center">'.price2num($obj->remise_percent).'%</td>';
  238. print '<td class="left">'.$obj->note.'</td>';
  239. print '<td align="center"><a href="'.DOL_URL_ROOT.'/user/card.php?id='.$obj->user_id.'">'.img_object($langs->trans("ShowUser"), 'user').' '.$obj->login.'</a></td>';
  240. print '</tr>';
  241. $i++;
  242. }
  243. } else {
  244. print '<tr><td colspan="8" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
  245. }
  246. $db->free($resql);
  247. print "</table>";
  248. } else {
  249. dol_print_error($db);
  250. }
  251. if ($isCustomer) {
  252. print '</div>'; // class="fichehalfright"
  253. print '</div>'; // class="fichecenter"
  254. }
  255. }
  256. }
  257. // End of page
  258. llxFooter();
  259. $db->close();