remx.php 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. <?php
  2. /* Copyright (C) 2001-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2019 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2008 Raphael Bertrand (Resultic) <raphael.bertrand@resultic.fr>
  5. * Copyright (C) 2019 Frédéric France <frederic.france@netlogic.fr>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/comm/remx.php
  22. * \ingroup societe
  23. * \brief Page to edit absolute discounts for a customer
  24. */
  25. if (! defined('CSRFCHECK_WITH_TOKEN')) define('CSRFCHECK_WITH_TOKEN', '1'); // Force use of CSRF protection with tokens even for GET
  26. require '../main.inc.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
  31. // Load translation files required by the page
  32. $langs->loadLangs(array('orders', 'bills', 'companies'));
  33. $id = GETPOST('id', 'int');
  34. $action = GETPOST('action', 'aZ09');
  35. $backtopage = GETPOST('backtopage', 'alpha');
  36. // Security check
  37. $socid = GETPOST('id', 'int') ?GETPOST('id', 'int') : GETPOST('socid', 'int');
  38. if ($user->socid > 0) {
  39. $socid = $user->socid;
  40. }
  41. // Security check
  42. if ($user->socid > 0) {
  43. $id = $user->socid;
  44. }
  45. $result = restrictedArea($user, 'societe', $id, '&societe', '', 'fk_soc', 'rowid', 0);
  46. /*
  47. * Actions
  48. */
  49. if (GETPOST('cancel', 'alpha') && !empty($backtopage)) {
  50. header("Location: ".$backtopage);
  51. exit;
  52. }
  53. if ($action == 'confirm_split' && GETPOST("confirm", "alpha") == 'yes' && $user->rights->societe->creer) {
  54. //if ($user->rights->societe->creer)
  55. //if ($user->rights->facture->creer)
  56. $amount_ttc_1 = GETPOST('amount_ttc_1', 'alpha');
  57. $amount_ttc_1 = price2num($amount_ttc_1);
  58. $amount_ttc_2 = GETPOST('amount_ttc_2', 'alpha');
  59. $amount_ttc_2 = price2num($amount_ttc_2);
  60. $error = 0;
  61. $remid = (GETPOST("remid", 'int') ? GETPOST("remid", 'int') : 0);
  62. $discount = new DiscountAbsolute($db);
  63. $res = $discount->fetch($remid);
  64. if (!($res > 0)) {
  65. $error++;
  66. setEventMessages($langs->trans("ErrorFailedToLoadDiscount"), null, 'errors');
  67. }
  68. if (!$error && price2num($amount_ttc_1 + $amount_ttc_2) != $discount->amount_ttc) {
  69. $error++;
  70. setEventMessages($langs->trans("TotalOfTwoDiscountMustEqualsOriginal"), null, 'errors');
  71. }
  72. if (!$error && $discount->fk_facture_line) {
  73. $error++;
  74. setEventMessages($langs->trans("ErrorCantSplitAUsedDiscount"), null, 'errors');
  75. }
  76. if (!$error) {
  77. $newdiscount1 = new DiscountAbsolute($db);
  78. $newdiscount2 = new DiscountAbsolute($db);
  79. $newdiscount1->fk_facture_source = $discount->fk_facture_source;
  80. $newdiscount2->fk_facture_source = $discount->fk_facture_source;
  81. $newdiscount1->fk_facture = $discount->fk_facture;
  82. $newdiscount2->fk_facture = $discount->fk_facture;
  83. $newdiscount1->fk_facture_line = $discount->fk_facture_line;
  84. $newdiscount2->fk_facture_line = $discount->fk_facture_line;
  85. $newdiscount1->fk_invoice_supplier_source = $discount->fk_invoice_supplier_source;
  86. $newdiscount2->fk_invoice_supplier_source = $discount->fk_invoice_supplier_source;
  87. $newdiscount1->fk_invoice_supplier = $discount->fk_invoice_supplier;
  88. $newdiscount2->fk_invoice_supplier = $discount->fk_invoice_supplier;
  89. $newdiscount1->fk_invoice_supplier_line = $discount->fk_invoice_supplier_line;
  90. $newdiscount2->fk_invoice_supplier_line = $discount->fk_invoice_supplier_line;
  91. if ($discount->description == '(CREDIT_NOTE)' || $discount->description == '(DEPOSIT)') {
  92. $newdiscount1->description = $discount->description;
  93. $newdiscount2->description = $discount->description;
  94. } else {
  95. $newdiscount1->description = $discount->description.' (1)';
  96. $newdiscount2->description = $discount->description.' (2)';
  97. }
  98. $newdiscount1->fk_user = $discount->fk_user;
  99. $newdiscount2->fk_user = $discount->fk_user;
  100. $newdiscount1->fk_soc = $discount->fk_soc;
  101. $newdiscount2->fk_soc = $discount->fk_soc;
  102. $newdiscount1->discount_type = $discount->discount_type;
  103. $newdiscount2->discount_type = $discount->discount_type;
  104. $newdiscount1->datec = $discount->datec;
  105. $newdiscount2->datec = $discount->datec;
  106. $newdiscount1->tva_tx = $discount->tva_tx;
  107. $newdiscount2->tva_tx = $discount->tva_tx;
  108. $newdiscount1->vat_src_code = $discount->vat_src_code;
  109. $newdiscount2->vat_src_code = $discount->vat_src_code;
  110. $newdiscount1->amount_ttc = $amount_ttc_1;
  111. $newdiscount2->amount_ttc = price2num($discount->amount_ttc - $newdiscount1->amount_ttc);
  112. $newdiscount1->amount_ht = price2num($newdiscount1->amount_ttc / (1 + $newdiscount1->tva_tx / 100), 'MT');
  113. $newdiscount2->amount_ht = price2num($newdiscount2->amount_ttc / (1 + $newdiscount2->tva_tx / 100), 'MT');
  114. $newdiscount1->amount_tva = price2num($newdiscount1->amount_ttc - $newdiscount1->amount_ht);
  115. $newdiscount2->amount_tva = price2num($newdiscount2->amount_ttc - $newdiscount2->amount_ht);
  116. $newdiscount1->multicurrency_amount_ttc = $amount_ttc_1 * ($discount->multicurrency_amount_ttc / $discount->amount_ttc);
  117. $newdiscount2->multicurrency_amount_ttc = price2num($discount->multicurrency_amount_ttc - $newdiscount1->multicurrency_amount_ttc);
  118. $newdiscount1->multicurrency_amount_ht = price2num($newdiscount1->multicurrency_amount_ttc / (1 + $newdiscount1->tva_tx / 100), 'MT');
  119. $newdiscount2->multicurrency_amount_ht = price2num($newdiscount2->multicurrency_amount_ttc / (1 + $newdiscount2->tva_tx / 100), 'MT');
  120. $newdiscount1->multicurrency_amount_tva = price2num($newdiscount1->multicurrency_amount_ttc - $newdiscount1->multicurrency_amount_ht);
  121. $newdiscount2->multicurrency_amount_tva = price2num($newdiscount2->multicurrency_amount_ttc - $newdiscount2->multicurrency_amount_ht);
  122. $db->begin();
  123. $discount->fk_facture_source = 0; // This is to delete only the require record (that we will recreate with two records) and not all family with same fk_facture_source
  124. // This is to delete only the require record (that we will recreate with two records) and not all family with same fk_invoice_supplier_source
  125. $discount->fk_invoice_supplier_source = 0;
  126. $res = $discount->delete($user);
  127. $newid1 = $newdiscount1->create($user);
  128. $newid2 = $newdiscount2->create($user);
  129. if ($res > 0 && $newid1 > 0 && $newid2 > 0) {
  130. $db->commit();
  131. header("Location: ".$_SERVER["PHP_SELF"].'?id='.$id.($backtopage ? '&backtopage='.urlencode($backtopage) : '')); // To avoid pb whith back
  132. exit;
  133. } else {
  134. $db->rollback();
  135. }
  136. }
  137. }
  138. if ($action == 'setremise' && $user->rights->societe->creer) {
  139. //if ($user->rights->societe->creer)
  140. //if ($user->rights->facture->creer)
  141. $amount_ht = price2num(GETPOST('amount_ht', 'alpha'));
  142. $desc = GETPOST('desc', 'alpha');
  143. $tva_tx = GETPOST('tva_tx', 'alpha');
  144. $discount_type = !empty($_POST['discount_type']) ?GETPOST('discount_type', 'alpha') : 0;
  145. if ($amount_ht > 0) {
  146. $error = 0;
  147. if (empty($desc)) {
  148. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ReasonDiscount")), null, 'errors');
  149. $error++;
  150. }
  151. if (!$error) {
  152. $soc = new Societe($db);
  153. $soc->fetch($id);
  154. $discountid = $soc->set_remise_except($amount_ht, $user, $desc, $tva_tx, $discount_type);
  155. if ($discountid > 0) {
  156. if (!empty($backtopage)) {
  157. header("Location: ".$backtopage.'&discountid='.$discountid);
  158. exit;
  159. } else {
  160. header("Location: remx.php?id=".$id);
  161. exit;
  162. }
  163. } else {
  164. $error++;
  165. setEventMessages($soc->error, $soc->errors, 'errors');
  166. }
  167. }
  168. } else {
  169. setEventMessages($langs->trans("ErrorFieldFormat", $langs->transnoentitiesnoconv("AmountHT")), null, 'errors');
  170. }
  171. }
  172. if (GETPOST('action', 'aZ09') == 'confirm_remove' && GETPOST("confirm") == 'yes' && $user->rights->societe->creer) {
  173. //if ($user->rights->societe->creer)
  174. //if ($user->rights->facture->creer)
  175. $db->begin();
  176. $discount = new DiscountAbsolute($db);
  177. $result = $discount->fetch(GETPOST("remid"));
  178. $result = $discount->delete($user);
  179. if ($result > 0) {
  180. $db->commit();
  181. header("Location: ".$_SERVER["PHP_SELF"].'?id='.$id); // To avoid pb whith back
  182. exit;
  183. } else {
  184. setEventMessages($discount->error, $discount->errors, 'errors');
  185. $db->rollback();
  186. }
  187. }
  188. /*
  189. * View
  190. */
  191. $form = new Form($db);
  192. $facturestatic = new Facture($db);
  193. $facturefournstatic = new FactureFournisseur($db);
  194. llxHeader('', $langs->trans("GlobalDiscount"));
  195. if ($socid > 0) {
  196. // On recupere les donnees societes par l'objet
  197. $object = new Societe($db);
  198. $object->fetch($socid);
  199. $isCustomer = $object->client == 1 || $object->client == 3;
  200. $isSupplier = $object->fournisseur == 1;
  201. /*
  202. * Display tabs
  203. */
  204. $head = societe_prepare_head($object);
  205. print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'">';
  206. print '<input type="hidden" name="token" value="'.newToken().'">';
  207. print '<input type="hidden" name="action" value="setremise">';
  208. print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
  209. print dol_get_fiche_head($head, 'absolutediscount', $langs->trans("ThirdParty"), -1, 'company');
  210. dol_banner_tab($object, 'socid', '', ($user->socid ? 0 : 1), 'rowid', 'nom');
  211. print '<div class="fichecenter">';
  212. print '<div class="underbanner clearboth"></div>';
  213. if (!$isCustomer && !$isSupplier) {
  214. print '<p class="opacitymedium">'.$langs->trans('ThirdpartyIsNeitherCustomerNorClientSoCannotHaveDiscounts').'</p>';
  215. print dol_get_fiche_end();
  216. print '</form>';
  217. llxFooter();
  218. $db->close();
  219. exit;
  220. }
  221. print '<table class="border centpercent tableforfield borderbottom">';
  222. if ($isCustomer) { // Calcul avoirs client en cours
  223. $remise_all = $remise_user = 0;
  224. $sql = "SELECT SUM(rc.amount_ht) as amount, rc.fk_user";
  225. $sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as rc";
  226. $sql .= " WHERE rc.fk_soc = ".$object->id;
  227. $sql .= " AND rc.entity = ".$conf->entity;
  228. $sql .= " AND discount_type = 0"; // Exclude supplier discounts
  229. $sql .= " AND (fk_facture_line IS NULL AND fk_facture IS NULL)";
  230. $sql .= " GROUP BY rc.fk_user";
  231. $resql = $db->query($sql);
  232. if ($resql) {
  233. $obj = $db->fetch_object($resql);
  234. $remise_all += $obj->amount;
  235. if ($obj->fk_user == $user->id) {
  236. $remise_user += $obj->amount;
  237. }
  238. } else {
  239. dol_print_error($db);
  240. }
  241. print '<tr><td class="titlefield">'.$langs->trans("CustomerAbsoluteDiscountAllUsers").'</td>';
  242. print '<td>'.$remise_all.'&nbsp;'.$langs->trans("Currency".$conf->currency).' '.$langs->trans("HT").'</td></tr>';
  243. if (!empty($user->fk_soc)) { // No need to show this for external users
  244. print '<tr><td>'.$langs->trans("CustomerAbsoluteDiscountMy").'</td>';
  245. print '<td>'.$remise_user.'&nbsp;'.$langs->trans("Currency".$conf->currency).' '.$langs->trans("HT").'</td></tr>';
  246. }
  247. }
  248. if ($isSupplier) {
  249. // Calcul avoirs fournisseur en cours
  250. $remise_all = $remise_user = 0;
  251. $sql = "SELECT SUM(rc.amount_ht) as amount, rc.fk_user";
  252. $sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as rc";
  253. $sql .= " WHERE rc.fk_soc = ".$object->id;
  254. $sql .= " AND rc.entity = ".$conf->entity;
  255. $sql .= " AND discount_type = 1"; // Exclude customer discounts
  256. $sql .= " AND (fk_invoice_supplier_line IS NULL AND fk_invoice_supplier IS NULL)";
  257. $sql .= " GROUP BY rc.fk_user";
  258. $resql = $db->query($sql);
  259. if ($resql) {
  260. $obj = $db->fetch_object($resql);
  261. $remise_all += $obj->amount;
  262. if ($obj->fk_user == $user->id) {
  263. $remise_user += $obj->amount;
  264. }
  265. } else {
  266. dol_print_error($db);
  267. }
  268. print '<tr><td class="titlefield">'.$langs->trans("SupplierAbsoluteDiscountAllUsers").'</td>';
  269. print '<td>'.$remise_all.'&nbsp;'.$langs->trans("Currency".$conf->currency).' '.$langs->trans("HT").'</td></tr>';
  270. if (!empty($user->fk_soc)) { // No need to show this for external users
  271. print '<tr><td>'.$langs->trans("SupplierAbsoluteDiscountMy").'</td>';
  272. print '<td>'.$remise_user.'&nbsp;'.$langs->trans("Currency".$conf->currency).' '.$langs->trans("HT").'</td></tr>';
  273. }
  274. }
  275. print '</table>';
  276. print '</div>';
  277. print dol_get_fiche_end();
  278. if ($user->rights->societe->creer) {
  279. print '<br>';
  280. print load_fiche_titre($langs->trans("NewGlobalDiscount"), '', '');
  281. if ($isCustomer && !$isSupplier) {
  282. print '<input type="hidden" name="discount_type" value="0" />';
  283. }
  284. if (!$isCustomer && $isSupplier) {
  285. print '<input type="hidden" name="discount_type" value="1" />';
  286. }
  287. print dol_get_fiche_head();
  288. print '<table class="border centpercent">';
  289. if ($isCustomer && $isSupplier) {
  290. print '<tr><td class="titlefield fieldrequired">'.$langs->trans('DiscountType').'</td>';
  291. print '<td><input type="radio" name="discount_type" id="discount_type_0" checked="checked" value="0"/> <label for="discount_type_0">'.$langs->trans('Customer').'</label>';
  292. print ' &nbsp; <input type="radio" name="discount_type" id="discount_type_1" value="1"/> <label for="discount_type_1">'.$langs->trans('Supplier').'</label>';
  293. print '</td></tr>';
  294. }
  295. print '<tr><td class="titlefield fieldrequired">'.$langs->trans("AmountHT").'</td>';
  296. print '<td><input type="text" size="5" name="amount_ht" value="'.price2num(GETPOST("amount_ht")).'">';
  297. print '<span class="hideonsmartphone">&nbsp;'.$langs->trans("Currency".$conf->currency).'</span></td></tr>';
  298. print '<tr><td>'.$langs->trans("VAT").'</td>';
  299. print '<td>';
  300. print $form->load_tva('tva_tx', GETPOSTISSET('tva_tx') ? GETPOST('tva_tx', 'alpha') : 0, $mysoc, $object, 0, 0, '', 0, 1);
  301. print '</td></tr>';
  302. print '<tr><td class="fieldrequired" >'.$langs->trans("NoteReason").'</td>';
  303. print '<td><input type="text" class="quatrevingtpercent" name="desc" value="'.GETPOST('desc', 'alphanohtml').'"></td></tr>';
  304. print "</table>";
  305. print dol_get_fiche_end();
  306. }
  307. if ($user->rights->societe->creer) {
  308. print '<div class="center">';
  309. print '<input type="submit" class="button" name="submit" value="'.$langs->trans("AddGlobalDiscount").'">';
  310. if (!empty($backtopage)) {
  311. print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  312. print '<input type="submit" class="button button-cancel" name="cancel" value="'.$langs->trans("Cancel").'">';
  313. }
  314. print '</div>';
  315. }
  316. print '</form>';
  317. print '<br>';
  318. if ($_GET['action'] == 'remove') {
  319. print $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id.'&remid='.GETPOST('remid'), $langs->trans('RemoveDiscount'), $langs->trans('ConfirmRemoveDiscount'), 'confirm_remove', '', 0, 1);
  320. }
  321. /*
  322. * List not consumed available credits (= linked to no invoice and no invoice line)
  323. */
  324. print load_fiche_titre($langs->trans("DiscountStillRemaining"));
  325. if ($isCustomer) {
  326. if ($isSupplier) {
  327. print '<div class="fichecenter">';
  328. print '<div class="fichehalfleft fichehalfleft-lg">';
  329. print load_fiche_titre($langs->trans("CustomerDiscounts"), '', '');
  330. }
  331. $sql = "SELECT rc.rowid, rc.amount_ht, rc.amount_tva, rc.amount_ttc, rc.tva_tx, rc.vat_src_code,";
  332. $sql .= " rc.multicurrency_amount_ht, rc.multicurrency_amount_tva, rc.multicurrency_amount_ttc,";
  333. $sql .= " rc.datec as dc, rc.description,";
  334. $sql .= " rc.fk_facture_source,";
  335. $sql .= " u.login, u.rowid as user_id,";
  336. $sql .= " fa.ref as ref, fa.type as type";
  337. $sql .= " FROM ".MAIN_DB_PREFIX."user as u, ".MAIN_DB_PREFIX."societe_remise_except as rc";
  338. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as fa ON rc.fk_facture_source = fa.rowid";
  339. $sql .= " WHERE rc.fk_soc = ".$object->id;
  340. $sql .= " AND rc.entity = ".$conf->entity;
  341. $sql .= " AND u.rowid = rc.fk_user";
  342. $sql .= " AND rc.discount_type = 0"; // Eliminate supplier discounts
  343. $sql .= " AND (rc.fk_facture_line IS NULL AND rc.fk_facture IS NULL)";
  344. $sql .= " ORDER BY rc.datec DESC";
  345. $resql = $db->query($sql);
  346. if ($resql) {
  347. print '<div class="div-table-responsive-no-min">';
  348. print '<table width="100%" class="noborder">';
  349. print '<tr class="liste_titre">';
  350. print '<td class="widthdate">'.$langs->trans("Date").'</td>'; // Need 120+ for format with AM/PM
  351. print '<td>'.$langs->trans("ReasonDiscount").'</td>';
  352. print '<td class="nowrap">'.$langs->trans("ConsumedBy").'</td>';
  353. print '<td class="right">'.$langs->trans("AmountHT").'</td>';
  354. if (!empty($conf->multicurrency->enabled)) {
  355. print '<td class="right">'.$langs->trans("MulticurrencyAmountHT").'</td>';
  356. }
  357. print '<td class="right">'.$langs->trans("VATRate").'</td>';
  358. print '<td class="right">'.$langs->trans("AmountTTC").'</td>';
  359. if (!empty($conf->multicurrency->enabled)) {
  360. print '<td class="right">'.$langs->trans("MulticurrencyAmountTTC").'</td>';
  361. }
  362. print '<td width="100" class="center">'.$langs->trans("DiscountOfferedBy").'</td>';
  363. print '<td width="50">&nbsp;</td>';
  364. print '</tr>';
  365. $showconfirminfo = array();
  366. $i = 0;
  367. $num = $db->num_rows($resql);
  368. if ($num > 0) {
  369. while ($i < $num) {
  370. $obj = $db->fetch_object($resql);
  371. print '<tr class="oddeven">';
  372. print '<td>'.dol_print_date($db->jdate($obj->dc), 'dayhour').'</td>';
  373. if (preg_match('/\(CREDIT_NOTE\)/', $obj->description)) {
  374. print '<td class="minwidth100">';
  375. $facturestatic->id = $obj->fk_facture_source;
  376. $facturestatic->ref = $obj->ref;
  377. $facturestatic->type = $obj->type;
  378. print preg_replace('/\(CREDIT_NOTE\)/', $langs->trans("CreditNote"), $obj->description).' '.$facturestatic->getNomURl(1);
  379. print '</td>';
  380. } elseif (preg_match('/\(DEPOSIT\)/', $obj->description)) {
  381. print '<td class="minwidth100">';
  382. $facturestatic->id = $obj->fk_facture_source;
  383. $facturestatic->ref = $obj->ref;
  384. $facturestatic->type = $obj->type;
  385. print preg_replace('/\(DEPOSIT\)/', $langs->trans("InvoiceDeposit"), $obj->description).' '.$facturestatic->getNomURl(1);
  386. print '</td>';
  387. } elseif (preg_match('/\(EXCESS RECEIVED\)/', $obj->description)) {
  388. print '<td class="minwidth100">';
  389. $facturestatic->id = $obj->fk_facture_source;
  390. $facturestatic->ref = $obj->ref;
  391. $facturestatic->type = $obj->type;
  392. print preg_replace('/\(EXCESS RECEIVED\)/', $langs->trans("ExcessReceived"), $obj->description).' '.$facturestatic->getNomURl(1);
  393. print '</td>';
  394. } else {
  395. print '<td class="minwidth100">';
  396. print $obj->description;
  397. print '</td>';
  398. }
  399. print '<td class="nowrap">'.$langs->trans("NotConsumed").'</td>';
  400. print '<td class="right">'.price($obj->amount_ht).'</td>';
  401. if (!empty($conf->multicurrency->enabled)) {
  402. print '<td class="right">'.price($obj->multicurrency_amount_ht).'</td>';
  403. }
  404. print '<td class="right">'.vatrate($obj->tva_tx.($obj->vat_src_code ? ' ('.$obj->vat_src_code.')' : ''), true).'</td>';
  405. print '<td class="right">'.price($obj->amount_ttc).'</td>';
  406. if (!empty($conf->multicurrency->enabled)) {
  407. print '<td class="right">'.price($obj->multicurrency_amount_ttc).'</td>';
  408. }
  409. print '<td class="center">';
  410. print '<a href="'.DOL_URL_ROOT.'/user/card.php?id='.$obj->user_id.'">'.img_object($langs->trans("ShowUser"), 'user').' '.$obj->login.'</a>';
  411. print '</td>';
  412. if ($user->rights->societe->creer || $user->rights->facture->creer) {
  413. print '<td class="center nowrap">';
  414. print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=split&token='.newToken().'&remid='.$obj->rowid.($backtopage ? '&backtopage='.urlencode($backtopage) : '').'">'.img_split($langs->trans("SplitDiscount")).'</a>';
  415. print '<a class="reposition marginleftonly" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=remove&token='.newToken().'&remid='.$obj->rowid.($backtopage ? '&backtopage='.urlencode($backtopage) : '').'">'.img_delete($langs->trans("RemoveDiscount")).'</a>';
  416. print '</td>';
  417. } else {
  418. print '<td>&nbsp;</td>';
  419. }
  420. print '</tr>';
  421. if ($_GET["action"] == 'split' && GETPOST('remid') == $obj->rowid) {
  422. $showconfirminfo['rowid'] = $obj->rowid;
  423. $showconfirminfo['amount_ttc'] = $obj->amount_ttc;
  424. }
  425. $i++;
  426. }
  427. } else {
  428. $colspan = 8;
  429. if (!empty($conf->multicurrency->enabled)) {
  430. $colspan += 2;
  431. }
  432. print '<tr><td colspan="'.$colspan.'" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
  433. }
  434. $db->free($resql);
  435. print "</table>";
  436. print '</div>';
  437. if (count($showconfirminfo)) {
  438. $amount1 = price2num($showconfirminfo['amount_ttc'] / 2, 'MT');
  439. $amount2 = ($showconfirminfo['amount_ttc'] - $amount1);
  440. $formquestion = array(
  441. 'text' => $langs->trans('TypeAmountOfEachNewDiscount'),
  442. array('type' => 'text', 'name' => 'amount_ttc_1', 'label' => $langs->trans("AmountTTC").' 1', 'value' => $amount1, 'size' => '5'),
  443. array('type' => 'text', 'name' => 'amount_ttc_2', 'label' => $langs->trans("AmountTTC").' 2', 'value' => $amount2, 'size' => '5')
  444. );
  445. $langs->load("dict");
  446. print $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id.'&remid='.$showconfirminfo['rowid'].($backtopage ? '&backtopage='.urlencode($backtopage) : ''), $langs->trans('SplitDiscount'), $langs->trans('ConfirmSplitDiscount', price($showconfirminfo['amount_ttc']), $langs->transnoentities("Currency".$conf->currency)), 'confirm_split', $formquestion, '', 0);
  447. }
  448. } else {
  449. dol_print_error($db);
  450. }
  451. }
  452. if ($isSupplier) {
  453. if ($isCustomer) {
  454. print '</div>'; // class="fichehalfleft"
  455. print '<div class="fichehalfright fichehalfright-lg">';
  456. print '<div class="ficheaddleft">';
  457. print load_fiche_titre($langs->trans("SupplierDiscounts"), '', '');
  458. }
  459. /*
  460. * Liste remises fixes fournisseur restant en cours (= liees a aucune facture ni ligne de facture)
  461. */
  462. $sql = "SELECT rc.rowid, rc.amount_ht, rc.amount_tva, rc.amount_ttc, rc.tva_tx, rc.vat_src_code,";
  463. $sql .= " rc.multicurrency_amount_ht, rc.multicurrency_amount_tva, rc.multicurrency_amount_ttc,";
  464. $sql .= " rc.datec as dc, rc.description,";
  465. $sql .= " rc.fk_invoice_supplier_source,";
  466. $sql .= " u.login, u.rowid as user_id,";
  467. $sql .= " fa.ref, fa.type as type";
  468. $sql .= " FROM ".MAIN_DB_PREFIX."user as u, ".MAIN_DB_PREFIX."societe_remise_except as rc";
  469. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture_fourn as fa ON rc.fk_invoice_supplier_source = fa.rowid";
  470. $sql .= " WHERE rc.fk_soc = ".$object->id;
  471. $sql .= " AND rc.entity = ".$conf->entity;
  472. $sql .= " AND u.rowid = rc.fk_user";
  473. $sql .= " AND rc.discount_type = 1"; // Eliminate customer discounts
  474. $sql .= " AND (rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_line IS NULL)";
  475. $sql .= " ORDER BY rc.datec DESC";
  476. $resql = $db->query($sql);
  477. if ($resql) {
  478. print '<div class="div-table-responsive-no-min">';
  479. print '<table width="100%" class="noborder">';
  480. print '<tr class="liste_titre">';
  481. print '<td class="widthdate">'.$langs->trans("Date").'</td>'; // Need 120+ for format with AM/PM
  482. print '<td>'.$langs->trans("ReasonDiscount").'</td>';
  483. print '<td class="nowrap">'.$langs->trans("ConsumedBy").'</td>';
  484. print '<td class="right">'.$langs->trans("AmountHT").'</td>';
  485. if (!empty($conf->multicurrency->enabled)) {
  486. print '<td class="right">'.$langs->trans("MulticurrencyAmountHT").'</td>';
  487. }
  488. print '<td class="right">'.$langs->trans("VATRate").'</td>';
  489. print '<td class="right">'.$langs->trans("AmountTTC").'</td>';
  490. if (!empty($conf->multicurrency->enabled)) {
  491. print '<td class="right">'.$langs->trans("MulticurrencyAmountTTC").'</td>';
  492. }
  493. print '<td width="100" class="center">'.$langs->trans("DiscountOfferedBy").'</td>';
  494. print '<td width="50">&nbsp;</td>';
  495. print '</tr>';
  496. $showconfirminfo = array();
  497. $i = 0;
  498. $num = $db->num_rows($resql);
  499. if ($num > 0) {
  500. while ($i < $num) {
  501. $obj = $db->fetch_object($resql);
  502. print '<tr class="oddeven">';
  503. print '<td>'.dol_print_date($db->jdate($obj->dc), 'dayhour').'</td>';
  504. if (preg_match('/\(CREDIT_NOTE\)/', $obj->description)) {
  505. print '<td class="minwidth100">';
  506. $facturefournstatic->id = $obj->fk_invoice_supplier_source;
  507. $facturefournstatic->ref = $obj->ref;
  508. $facturefournstatic->type = $obj->type;
  509. print preg_replace('/\(CREDIT_NOTE\)/', $langs->trans("CreditNote"), $obj->description).' '.$facturefournstatic->getNomURl(1);
  510. print '</td>';
  511. } elseif (preg_match('/\(DEPOSIT\)/', $obj->description)) {
  512. print '<td class="minwidth100">';
  513. $facturefournstatic->id = $obj->fk_invoice_supplier_source;
  514. $facturefournstatic->ref = $obj->ref;
  515. $facturefournstatic->type = $obj->type;
  516. print preg_replace('/\(DEPOSIT\)/', $langs->trans("InvoiceDeposit"), $obj->description).' '.$facturefournstatic->getNomURl(1);
  517. print '</td>';
  518. } elseif (preg_match('/\(EXCESS PAID\)/', $obj->description)) {
  519. print '<td class="minwidth100">';
  520. $facturefournstatic->id = $obj->fk_invoice_supplier_source;
  521. $facturefournstatic->ref = $obj->ref;
  522. $facturefournstatic->type = $obj->type;
  523. print preg_replace('/\(EXCESS PAID\)/', $langs->trans("ExcessPaid"), $obj->description).' '.$facturefournstatic->getNomURl(1);
  524. print '</td>';
  525. } else {
  526. print '<td class="minwidth100">';
  527. print $obj->description;
  528. print '</td>';
  529. }
  530. print '<td class="nowrap">'.$langs->trans("NotConsumed").'</td>';
  531. print '<td class="right">'.price($obj->amount_ht).'</td>';
  532. if (!empty($conf->multicurrency->enabled)) {
  533. print '<td class="right">'.price($obj->multicurrency_amount_ht).'</td>';
  534. }
  535. print '<td class="right">'.vatrate($obj->tva_tx.($obj->vat_src_code ? ' ('.$obj->vat_src_code.')' : ''), true).'</td>';
  536. print '<td class="right">'.price($obj->amount_ttc).'</td>';
  537. if (!empty($conf->multicurrency->enabled)) {
  538. print '<td class="right">'.price($obj->multicurrency_amount_ttc).'</td>';
  539. }
  540. print '<td class="center">';
  541. print '<a href="'.DOL_URL_ROOT.'/user/card.php?id='.$obj->user_id.'">'.img_object($langs->trans("ShowUser"), 'user').' '.$obj->login.'</a>';
  542. print '</td>';
  543. if ($user->rights->societe->creer || $user->rights->facture->creer) {
  544. print '<td class="center nowrap">';
  545. print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=split&token='.newToken().'&remid='.$obj->rowid.($backtopage ? '&backtopage='.urlencode($backtopage) : '').'">'.img_split($langs->trans("SplitDiscount")).'</a>';
  546. print '<a class="reposition marginleftonly" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=remove&token='.newToken().'&remid='.$obj->rowid.($backtopage ? '&backtopage='.urlencode($backtopage) : '').'">'.img_delete($langs->trans("RemoveDiscount")).'</a>';
  547. print '</td>';
  548. } else {
  549. print '<td>&nbsp;</td>';
  550. }
  551. print '</tr>';
  552. if ($_GET["action"] == 'split' && GETPOST('remid') == $obj->rowid) {
  553. $showconfirminfo['rowid'] = $obj->rowid;
  554. $showconfirminfo['amount_ttc'] = $obj->amount_ttc;
  555. }
  556. $i++;
  557. }
  558. } else {
  559. $colspan = 8;
  560. if (!empty($conf->multicurrency->enabled)) {
  561. $colspan += 2;
  562. }
  563. print '<tr><td colspan="'.$colspan.'" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
  564. }
  565. $db->free($resql);
  566. print "</table>";
  567. print '</div>';
  568. if (count($showconfirminfo)) {
  569. $amount1 = price2num($showconfirminfo['amount_ttc'] / 2, 'MT');
  570. $amount2 = ($showconfirminfo['amount_ttc'] - $amount1);
  571. $formquestion = array(
  572. 'text' => $langs->trans('TypeAmountOfEachNewDiscount'),
  573. array('type' => 'text', 'name' => 'amount_ttc_1', 'label' => $langs->trans("AmountTTC").' 1', 'value' => $amount1, 'size' => '5'),
  574. array('type' => 'text', 'name' => 'amount_ttc_2', 'label' => $langs->trans("AmountTTC").' 2', 'value' => $amount2, 'size' => '5')
  575. );
  576. $langs->load("dict");
  577. print $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id.'&remid='.$showconfirminfo['rowid'].($backtopage ? '&backtopage='.urlencode($backtopage) : ''), $langs->trans('SplitDiscount'), $langs->trans('ConfirmSplitDiscount', price($showconfirminfo['amount_ttc']), $langs->transnoentities("Currency".$conf->currency)), 'confirm_split', $formquestion, 0, 0);
  578. }
  579. } else {
  580. dol_print_error($db);
  581. }
  582. if ($isCustomer) {
  583. print '</div>'; // class="ficheaddleft"
  584. print '</div>'; // class="fichehalfright"
  585. print '</div>'; // class="fichecenter"
  586. }
  587. }
  588. print '<div class="clearboth"></div><br>';
  589. /*
  590. * List discount consumed (=liees a une ligne de facture ou facture)
  591. */
  592. print load_fiche_titre($langs->trans("DiscountAlreadyCounted"));
  593. if ($isCustomer) {
  594. if ($isSupplier) {
  595. print '<div class="fichecenter">';
  596. print '<div class="fichehalfleft fichehalfleft-lg">';
  597. print load_fiche_titre($langs->trans("CustomerDiscounts"), '', '');
  598. }
  599. // Discount linked to invoice lines
  600. $sql = "SELECT rc.rowid, rc.amount_ht, rc.amount_tva, rc.amount_ttc, rc.tva_tx, rc.vat_src_code,";
  601. $sql .= " rc.multicurrency_amount_ht, rc.multicurrency_amount_tva, rc.multicurrency_amount_ttc,";
  602. $sql .= " rc.datec as dc, rc.description, rc.fk_facture_line, rc.fk_facture_source,";
  603. $sql .= " u.login, u.rowid as user_id,";
  604. $sql .= " f.rowid as invoiceid, f.ref,";
  605. $sql .= " fa.ref as invoice_source_ref, fa.type as type";
  606. $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
  607. $sql .= " , ".MAIN_DB_PREFIX."user as u";
  608. $sql .= " , ".MAIN_DB_PREFIX."facturedet as fc";
  609. $sql .= " , ".MAIN_DB_PREFIX."societe_remise_except as rc";
  610. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as fa ON rc.fk_facture_source = fa.rowid";
  611. $sql .= " WHERE rc.fk_soc =".$object->id;
  612. $sql .= " AND rc.fk_facture_line = fc.rowid";
  613. $sql .= " AND fc.fk_facture = f.rowid";
  614. $sql .= " AND rc.fk_user = u.rowid";
  615. $sql .= " AND rc.discount_type = 0"; // Eliminate supplier discounts
  616. $sql .= " ORDER BY dc DESC";
  617. //$sql.= " UNION ";
  618. // Discount linked to invoices
  619. $sql2 = "SELECT rc.rowid, rc.amount_ht, rc.amount_tva, rc.amount_ttc, rc.tva_tx, rc.vat_src_code,";
  620. $sql2 .= " rc.multicurrency_amount_ht, rc.multicurrency_amount_tva, rc.multicurrency_amount_ttc,";
  621. $sql2 .= " rc.datec as dc, rc.description, rc.fk_facture, rc.fk_facture_source,";
  622. $sql2 .= " u.login, u.rowid as user_id,";
  623. $sql2 .= " f.rowid as invoiceid, f.ref,";
  624. $sql2 .= " fa.ref as invoice_source_ref, fa.type as type";
  625. $sql2 .= " FROM ".MAIN_DB_PREFIX."facture as f";
  626. $sql2 .= " , ".MAIN_DB_PREFIX."user as u";
  627. $sql2 .= " , ".MAIN_DB_PREFIX."societe_remise_except as rc";
  628. $sql2 .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as fa ON rc.fk_facture_source = fa.rowid";
  629. $sql2 .= " WHERE rc.fk_soc =".$object->id;
  630. $sql2 .= " AND rc.fk_facture = f.rowid";
  631. $sql2 .= " AND rc.fk_user = u.rowid";
  632. $sql2 .= " AND rc.discount_type = 0"; // Eliminate supplier discounts
  633. $sql2 .= " ORDER BY dc DESC";
  634. $resql = $db->query($sql);
  635. $resql2 = null;
  636. if ($resql) {
  637. $resql2 = $db->query($sql2);
  638. }
  639. if ($resql2) {
  640. print '<div class="div-table-responsive-no-min">';
  641. print '<table class="noborder centpercent">';
  642. print '<tr class="liste_titre">';
  643. print '<td class="widthdate">'.$langs->trans("Date").'</td>'; // Need 120+ for format with AM/PM
  644. print '<td>'.$langs->trans("ReasonDiscount").'</td>';
  645. print '<td class="nowrap">'.$langs->trans("ConsumedBy").'</td>';
  646. print '<td class="right">'.$langs->trans("AmountHT").'</td>';
  647. if (!empty($conf->multicurrency->enabled)) {
  648. print '<td class="right">'.$langs->trans("MulticurrencyAmountHT").'</td>';
  649. }
  650. print '<td class="right">'.$langs->trans("VATRate").'</td>';
  651. print '<td class="right">'.$langs->trans("AmountTTC").'</td>';
  652. if (!empty($conf->multicurrency->enabled)) {
  653. print '<td class="right">'.$langs->trans("MulticurrencyAmountTTC").'</td>';
  654. }
  655. print '<td width="100" class="center">'.$langs->trans("Author").'</td>';
  656. print '<td width="50">&nbsp;</td>';
  657. print '</tr>';
  658. $tab_sqlobj = array();
  659. $tab_sqlobjOrder = array();
  660. $num = $db->num_rows($resql);
  661. if ($num > 0) {
  662. for ($i = 0; $i < $num; $i++) {
  663. $sqlobj = $db->fetch_object($resql);
  664. $tab_sqlobj[] = $sqlobj;
  665. $tab_sqlobjOrder[] = $db->jdate($sqlobj->dc);
  666. }
  667. }
  668. $db->free($resql);
  669. $num = $db->num_rows($resql2);
  670. for ($i = 0; $i < $num; $i++) {
  671. $sqlobj = $db->fetch_object($resql2);
  672. $tab_sqlobj[] = $sqlobj;
  673. $tab_sqlobjOrder[] = $db->jdate($sqlobj->dc);
  674. }
  675. $db->free($resql2);
  676. array_multisort($tab_sqlobjOrder, SORT_DESC, $tab_sqlobj);
  677. $num = count($tab_sqlobj);
  678. if ($num > 0) {
  679. $i = 0;
  680. while ($i < $num) {
  681. $obj = array_shift($tab_sqlobj);
  682. print '<tr class="oddeven">';
  683. print '<td>'.dol_print_date($db->jdate($obj->dc), 'dayhour').'</td>';
  684. if (preg_match('/\(CREDIT_NOTE\)/', $obj->description)) {
  685. print '<td class="minwidth100">';
  686. $facturestatic->id = $obj->fk_facture_source;
  687. $facturestatic->ref = $obj->invoice_source_ref;
  688. $facturestatic->type = $obj->type;
  689. print preg_replace('/\(CREDIT_NOTE\)/', $langs->trans("CreditNote"), $obj->description).' '.$facturestatic->getNomURl(1);
  690. print '</td>';
  691. } elseif (preg_match('/\(DEPOSIT\)/', $obj->description)) {
  692. print '<td class="minwidth100">';
  693. $facturestatic->id = $obj->fk_facture_source;
  694. $facturestatic->ref = $obj->invoice_source_ref;
  695. $facturestatic->type = $obj->type;
  696. print preg_replace('/\(DEPOSIT\)/', $langs->trans("InvoiceDeposit"), $obj->description).' '.$facturestatic->getNomURl(1);
  697. print '</td>';
  698. } elseif (preg_match('/\(EXCESS RECEIVED\)/', $obj->description)) {
  699. print '<td class="minwidth100">';
  700. $facturestatic->id = $obj->fk_facture_source;
  701. $facturestatic->ref = $obj->invoice_source_ref;
  702. $facturestatic->type = $obj->type;
  703. print preg_replace('/\(EXCESS RECEIVED\)/', $langs->trans("Invoice"), $obj->description).' '.$facturestatic->getNomURl(1);
  704. print '</td>';
  705. } else {
  706. print '<td class="minwidth100">';
  707. print $obj->description;
  708. print '</td>';
  709. }
  710. print '<td class="left nowrap">';
  711. if ($obj->invoiceid) {
  712. print '<a href="'.DOL_URL_ROOT.'/compta/facture/card.php?facid='.$obj->invoiceid.'">'.img_object($langs->trans("ShowBill"), 'bill').' '.$obj->ref.'</a>';
  713. }
  714. print '</td>';
  715. print '<td class="right">'.price($obj->amount_ht).'</td>';
  716. if (!empty($conf->multicurrency->enabled)) {
  717. print '<td class="right">'.price($obj->multicurrency_amount_ht).'</td>';
  718. }
  719. print '<td class="right">'.vatrate($obj->tva_tx.($obj->vat_src_code ? ' ('.$obj->vat_src_code.')' : ''), true).'</td>';
  720. print '<td class="right">'.price($obj->amount_ttc).'</td>';
  721. if (!empty($conf->multicurrency->enabled)) {
  722. print '<td class="right">'.price($obj->multicurrency_amount_ttc).'</td>';
  723. }
  724. print '<td class="center">';
  725. print '<a href="'.DOL_URL_ROOT.'/user/card.php?id='.$obj->user_id.'">'.img_object($langs->trans("ShowUser"), 'user').' '.$obj->login.'</a>';
  726. print '</td>';
  727. print '<td>&nbsp;</td>';
  728. print '</tr>';
  729. $i++;
  730. }
  731. } else {
  732. $colspan = 8;
  733. if (!empty($conf->multicurrency->enabled)) {
  734. $colspan += 2;
  735. }
  736. print '<tr><td colspan="'.$colspan.'" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
  737. }
  738. print "</table>";
  739. print '</div>';
  740. } else {
  741. dol_print_error($db);
  742. }
  743. }
  744. if ($isSupplier) {
  745. if ($isCustomer) {
  746. print '</div>'; // class="fichehalfleft"
  747. print '<div class="fichehalfright fichehalfright-lg">';
  748. print '<div class="ficheaddleft">';
  749. print load_fiche_titre($langs->trans("SupplierDiscounts"), '', '');
  750. }
  751. // Discount linked to invoice lines
  752. $sql = "SELECT rc.rowid, rc.amount_ht, rc.amount_tva, rc.amount_ttc, rc.tva_tx, rc.vat_src_code,";
  753. $sql .= " rc.multicurrency_amount_ht, rc.multicurrency_amount_tva, rc.multicurrency_amount_ttc,";
  754. $sql .= " rc.datec as dc, rc.description, rc.fk_invoice_supplier_line,";
  755. $sql .= " rc.fk_invoice_supplier_source,";
  756. $sql .= " u.login, u.rowid as user_id,";
  757. $sql .= " f.rowid as invoiceid, f.ref as ref,";
  758. $sql .= " fa.ref as invoice_source_ref, fa.type as type";
  759. $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
  760. $sql .= " , ".MAIN_DB_PREFIX."user as u";
  761. $sql .= " , ".MAIN_DB_PREFIX."facture_fourn_det as fc";
  762. $sql .= " , ".MAIN_DB_PREFIX."societe_remise_except as rc";
  763. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture_fourn as fa ON rc.fk_invoice_supplier_source = fa.rowid";
  764. $sql .= " WHERE rc.fk_soc =".$object->id;
  765. $sql .= " AND rc.fk_invoice_supplier_line = fc.rowid";
  766. $sql .= " AND fc.fk_facture_fourn = f.rowid";
  767. $sql .= " AND rc.fk_user = u.rowid";
  768. $sql .= " AND rc.discount_type = 1"; // Eliminate customer discounts
  769. $sql .= " ORDER BY dc DESC";
  770. //$sql.= " UNION ";
  771. // Discount linked to invoices
  772. $sql2 = "SELECT rc.rowid, rc.amount_ht, rc.amount_tva, rc.amount_ttc, rc.tva_tx, rc.vat_src_code,";
  773. $sql2 .= " rc.multicurrency_amount_ht, rc.multicurrency_amount_tva, rc.multicurrency_amount_ttc,";
  774. $sql2 .= " rc.datec as dc, rc.description, rc.fk_invoice_supplier,";
  775. $sql2 .= " rc.fk_invoice_supplier_source,";
  776. $sql2 .= " u.login, u.rowid as user_id,";
  777. $sql2 .= " f.rowid as invoiceid, f.ref as ref,";
  778. $sql2 .= " fa.ref as invoice_source_ref, fa.type as type";
  779. $sql2 .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
  780. $sql2 .= " , ".MAIN_DB_PREFIX."user as u";
  781. $sql2 .= " , ".MAIN_DB_PREFIX."societe_remise_except as rc";
  782. $sql2 .= " LEFT JOIN ".MAIN_DB_PREFIX."facture_fourn as fa ON rc.fk_invoice_supplier_source = fa.rowid";
  783. $sql2 .= " WHERE rc.fk_soc =".$object->id;
  784. $sql2 .= " AND rc.fk_invoice_supplier = f.rowid";
  785. $sql2 .= " AND rc.fk_user = u.rowid";
  786. $sql2 .= " AND rc.discount_type = 1"; // Eliminate customer discounts
  787. $sql2 .= " ORDER BY dc DESC";
  788. $resql = $db->query($sql);
  789. $resql2 = null;
  790. if ($resql) {
  791. $resql2 = $db->query($sql2);
  792. }
  793. if ($resql2) {
  794. print '<div class="div-table-responsive-no-min">';
  795. print '<table class="noborder centpercent">';
  796. print '<tr class="liste_titre">';
  797. print '<td class="widthdate">'.$langs->trans("Date").'</td>'; // Need 120+ for format with AM/PM
  798. print '<td>'.$langs->trans("ReasonDiscount").'</td>';
  799. print '<td class="nowrap">'.$langs->trans("ConsumedBy").'</td>';
  800. print '<td class="right">'.$langs->trans("AmountHT").'</td>';
  801. if (!empty($conf->multicurrency->enabled)) {
  802. print '<td class="right">'.$langs->trans("MulticurrencyAmountHT").'</td>';
  803. }
  804. print '<td class="right">'.$langs->trans("VATRate").'</td>';
  805. print '<td class="right">'.$langs->trans("AmountTTC").'</td>';
  806. if (!empty($conf->multicurrency->enabled)) {
  807. print '<td class="right">'.$langs->trans("MulticurrencyAmountTTC").'</td>';
  808. }
  809. print '<td width="100" class="center">'.$langs->trans("Author").'</td>';
  810. print '<td width="50">&nbsp;</td>';
  811. print '</tr>';
  812. $tab_sqlobj = array();
  813. $tab_sqlobjOrder = array();
  814. $num = $db->num_rows($resql);
  815. if ($num > 0) {
  816. for ($i = 0; $i < $num; $i++) {
  817. $sqlobj = $db->fetch_object($resql);
  818. $tab_sqlobj[] = $sqlobj;
  819. $tab_sqlobjOrder[] = $db->jdate($sqlobj->dc);
  820. }
  821. }
  822. $db->free($resql);
  823. $num = $db->num_rows($resql2);
  824. for ($i = 0; $i < $num; $i++) {
  825. $sqlobj = $db->fetch_object($resql2);
  826. $tab_sqlobj[] = $sqlobj;
  827. $tab_sqlobjOrder[] = $db->jdate($sqlobj->dc);
  828. }
  829. $db->free($resql2);
  830. array_multisort($tab_sqlobjOrder, SORT_DESC, $tab_sqlobj);
  831. $num = count($tab_sqlobj);
  832. if ($num > 0) {
  833. $i = 0;
  834. while ($i < $num) {
  835. $obj = array_shift($tab_sqlobj);
  836. print '<tr class="oddeven">';
  837. print '<td>'.dol_print_date($db->jdate($obj->dc), 'dayhour').'</td>';
  838. if (preg_match('/\(CREDIT_NOTE\)/', $obj->description)) {
  839. print '<td class="minwidth100">';
  840. $facturefournstatic->id = $obj->fk_invoice_supplier_source;
  841. $facturefournstatic->ref = $obj->invoice_source_ref;
  842. $facturefournstatic->type = $obj->type;
  843. print preg_replace('/\(CREDIT_NOTE\)/', $langs->trans("CreditNote"), $obj->description).' '.$facturefournstatic->getNomURl(1);
  844. print '</td>';
  845. } elseif (preg_match('/\(DEPOSIT\)/', $obj->description)) {
  846. print '<td class="minwidth100">';
  847. $facturefournstatic->id = $obj->fk_invoice_supplier_source;
  848. $facturefournstatic->ref = $obj->invoice_source_ref;
  849. $facturefournstatic->type = $obj->type;
  850. print preg_replace('/\(DEPOSIT\)/', $langs->trans("InvoiceDeposit"), $obj->description).' '.$facturefournstatic->getNomURl(1);
  851. print '</td>';
  852. } elseif (preg_match('/\(EXCESS PAID\)/', $obj->description)) {
  853. print '<td class="minwidth100">';
  854. $facturefournstatic->id = $obj->fk_invoice_supplier_source;
  855. $facturefournstatic->ref = $obj->invoice_source_ref;
  856. $facturefournstatic->type = $obj->type;
  857. print preg_replace('/\(EXCESS PAID\)/', $langs->trans("Invoice"), $obj->description).' '.$facturefournstatic->getNomURl(1);
  858. print '</td>';
  859. } else {
  860. print '<td class="minwidth100">';
  861. print $obj->description;
  862. print '</td>';
  863. }
  864. print '<td class="left nowrap">';
  865. if ($obj->invoiceid) {
  866. print '<a href="'.DOL_URL_ROOT.'/fourn/facture/card.php?facid='.$obj->invoiceid.'">'.img_object($langs->trans("ShowBill"), 'bill').' '.$obj->ref.'</a>';
  867. }
  868. print '</td>';
  869. print '<td class="right">'.price($obj->amount_ht).'</td>';
  870. if (!empty($conf->multicurrency->enabled)) {
  871. print '<td class="right">'.price($obj->multicurrency_amount_ht).'</td>';
  872. }
  873. print '<td class="right">'.vatrate($obj->tva_tx.($obj->vat_src_code ? ' ('.$obj->vat_src_code.')' : ''), true).'</td>';
  874. print '<td class="right">'.price($obj->amount_ttc).'</td>';
  875. if (!empty($conf->multicurrency->enabled)) {
  876. print '<td class="right">'.price($obj->multicurrency_amount_ttc).'</td>';
  877. }
  878. print '<td class="center">';
  879. print '<a href="'.DOL_URL_ROOT.'/user/card.php?id='.$obj->user_id.'">'.img_object($langs->trans("ShowUser"), 'user').' '.$obj->login.'</a>';
  880. print '</td>';
  881. print '<td>&nbsp;</td>';
  882. print '</tr>';
  883. $i++;
  884. }
  885. } else {
  886. $colspan = 8;
  887. if (!empty($conf->multicurrency->enabled)) {
  888. $colspan += 2;
  889. }
  890. print '<tr><td colspan="'.$colspan.'" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
  891. }
  892. print "</table>";
  893. print '</div>';
  894. } else {
  895. dol_print_error($db);
  896. }
  897. if ($isCustomer) {
  898. print '</div>'; // class="ficheaddleft"
  899. print '</div>'; // class="fichehalfright"
  900. print '</div>'; // class="fichecenter"
  901. }
  902. }
  903. }
  904. // End of page
  905. llxFooter();
  906. $db->close();