remx.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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 || $user->rights->facture->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 || $user->rights->facture->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 = GETPOSTISSET('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 || $user->rights->facture->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 = ".((int) $object->id);
  227. $sql .= " AND rc.entity = ".((int) $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 class="amount">'.price($remise_all, 1, $langs, 1, -1, -1, $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 class="amount">'.price($remise_user, 1, $langs, 1, -1, -1, $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 = ".((int) $object->id);
  254. $sql .= " AND rc.entity = ".((int) $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 class="amount">'.price($remise_all, 1, $langs, 1, -1, -1, $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 class="amount">'.price($remise_user, 1, $langs, 1, -1, -1, $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")).'" autofocus>';
  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 ($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 = ".((int) $object->id);
  340. $sql .= " AND rc.entity = ".((int) $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 tdoverflowmax125" title="'.dol_escape_htmltag($langs->trans("MulticurrencyAmountHT")).'">'.$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 tdoverflowmax125" title="'.dol_escape_htmltag($langs->trans("MulticurrencyAmountTTC")).'">'.$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', 'tzuserrel').'</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"><span class="opacitymedium">'.$langs->trans("NotConsumed").'</span></td>';
  400. print '<td class="right amount">'.price($obj->amount_ht).'</td>';
  401. if (!empty($conf->multicurrency->enabled)) {
  402. print '<td class="right amount">'.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 amount">'.price($obj->amount_ttc).'</td>';
  406. if (!empty($conf->multicurrency->enabled)) {
  407. print '<td class="right amount">'.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 load_fiche_titre($langs->trans("SupplierDiscounts"), '', '');
  457. }
  458. /*
  459. * Liste remises fixes fournisseur restant en cours (= liees a aucune facture ni ligne de facture)
  460. */
  461. $sql = "SELECT rc.rowid, rc.amount_ht, rc.amount_tva, rc.amount_ttc, rc.tva_tx, rc.vat_src_code,";
  462. $sql .= " rc.multicurrency_amount_ht, rc.multicurrency_amount_tva, rc.multicurrency_amount_ttc,";
  463. $sql .= " rc.datec as dc, rc.description,";
  464. $sql .= " rc.fk_invoice_supplier_source,";
  465. $sql .= " u.login, u.rowid as user_id,";
  466. $sql .= " fa.ref, fa.type as type";
  467. $sql .= " FROM ".MAIN_DB_PREFIX."user as u, ".MAIN_DB_PREFIX."societe_remise_except as rc";
  468. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture_fourn as fa ON rc.fk_invoice_supplier_source = fa.rowid";
  469. $sql .= " WHERE rc.fk_soc = ".((int) $object->id);
  470. $sql .= " AND rc.entity = ".((int) $conf->entity);
  471. $sql .= " AND u.rowid = rc.fk_user";
  472. $sql .= " AND rc.discount_type = 1"; // Eliminate customer discounts
  473. $sql .= " AND (rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_line IS NULL)";
  474. $sql .= " ORDER BY rc.datec DESC";
  475. $resql = $db->query($sql);
  476. if ($resql) {
  477. print '<div class="div-table-responsive-no-min">';
  478. print '<table width="100%" class="noborder">';
  479. print '<tr class="liste_titre">';
  480. print '<td class="widthdate">'.$langs->trans("Date").'</td>'; // Need 120+ for format with AM/PM
  481. print '<td>'.$langs->trans("ReasonDiscount").'</td>';
  482. print '<td class="nowrap">'.$langs->trans("ConsumedBy").'</td>';
  483. print '<td class="right">'.$langs->trans("AmountHT").'</td>';
  484. if (!empty($conf->multicurrency->enabled)) {
  485. print '<td class="right tdoverflowmax125" title="'.dol_escape_htmltag($langs->trans("MulticurrencyAmountHT")).'">'.$langs->trans("MulticurrencyAmountHT").'</td>';
  486. }
  487. print '<td class="right">'.$langs->trans("VATRate").'</td>';
  488. print '<td class="right">'.$langs->trans("AmountTTC").'</td>';
  489. if (!empty($conf->multicurrency->enabled)) {
  490. print '<td class="right tdoverflowmax125" title="'.dol_escape_htmltag($langs->trans("MulticurrencyAmountTTC")).'">'.$langs->trans("MulticurrencyAmountTTC").'</td>';
  491. }
  492. print '<td width="100" class="center">'.$langs->trans("DiscountOfferedBy").'</td>';
  493. print '<td width="50">&nbsp;</td>';
  494. print '</tr>';
  495. $showconfirminfo = array();
  496. $i = 0;
  497. $num = $db->num_rows($resql);
  498. if ($num > 0) {
  499. while ($i < $num) {
  500. $obj = $db->fetch_object($resql);
  501. print '<tr class="oddeven">';
  502. print '<td>'.dol_print_date($db->jdate($obj->dc), 'dayhour', 'tzuserrel').'</td>';
  503. if (preg_match('/\(CREDIT_NOTE\)/', $obj->description)) {
  504. print '<td class="minwidth100">';
  505. $facturefournstatic->id = $obj->fk_invoice_supplier_source;
  506. $facturefournstatic->ref = $obj->ref;
  507. $facturefournstatic->type = $obj->type;
  508. print preg_replace('/\(CREDIT_NOTE\)/', $langs->trans("CreditNote"), $obj->description).' '.$facturefournstatic->getNomURl(1);
  509. print '</td>';
  510. } elseif (preg_match('/\(DEPOSIT\)/', $obj->description)) {
  511. print '<td class="minwidth100">';
  512. $facturefournstatic->id = $obj->fk_invoice_supplier_source;
  513. $facturefournstatic->ref = $obj->ref;
  514. $facturefournstatic->type = $obj->type;
  515. print preg_replace('/\(DEPOSIT\)/', $langs->trans("InvoiceDeposit"), $obj->description).' '.$facturefournstatic->getNomURl(1);
  516. print '</td>';
  517. } elseif (preg_match('/\(EXCESS PAID\)/', $obj->description)) {
  518. print '<td class="minwidth100">';
  519. $facturefournstatic->id = $obj->fk_invoice_supplier_source;
  520. $facturefournstatic->ref = $obj->ref;
  521. $facturefournstatic->type = $obj->type;
  522. print preg_replace('/\(EXCESS PAID\)/', $langs->trans("ExcessPaid"), $obj->description).' '.$facturefournstatic->getNomURl(1);
  523. print '</td>';
  524. } else {
  525. print '<td class="minwidth100">';
  526. print $obj->description;
  527. print '</td>';
  528. }
  529. print '<td class="nowrap"><span class="opacitymedium">'.$langs->trans("NotConsumed").'</span></td>';
  530. print '<td class="right amount">'.price($obj->amount_ht).'</td>';
  531. if (!empty($conf->multicurrency->enabled)) {
  532. print '<td class="right amount">'.price($obj->multicurrency_amount_ht).'</td>';
  533. }
  534. print '<td class="right">'.vatrate($obj->tva_tx.($obj->vat_src_code ? ' ('.$obj->vat_src_code.')' : ''), true).'</td>';
  535. print '<td class="right amount">'.price($obj->amount_ttc).'</td>';
  536. if (!empty($conf->multicurrency->enabled)) {
  537. print '<td class="right amount">'.price($obj->multicurrency_amount_ttc).'</td>';
  538. }
  539. print '<td class="center">';
  540. print '<a href="'.DOL_URL_ROOT.'/user/card.php?id='.$obj->user_id.'">'.img_object($langs->trans("ShowUser"), 'user').' '.$obj->login.'</a>';
  541. print '</td>';
  542. if ($user->rights->societe->creer || $user->rights->facture->creer) {
  543. print '<td class="center nowrap">';
  544. 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>';
  545. 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>';
  546. print '</td>';
  547. } else {
  548. print '<td>&nbsp;</td>';
  549. }
  550. print '</tr>';
  551. if ($_GET["action"] == 'split' && GETPOST('remid') == $obj->rowid) {
  552. $showconfirminfo['rowid'] = $obj->rowid;
  553. $showconfirminfo['amount_ttc'] = $obj->amount_ttc;
  554. }
  555. $i++;
  556. }
  557. } else {
  558. $colspan = 8;
  559. if (!empty($conf->multicurrency->enabled)) {
  560. $colspan += 2;
  561. }
  562. print '<tr><td colspan="'.$colspan.'" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
  563. }
  564. $db->free($resql);
  565. print "</table>";
  566. print '</div>';
  567. if (count($showconfirminfo)) {
  568. $amount1 = price2num($showconfirminfo['amount_ttc'] / 2, 'MT');
  569. $amount2 = ($showconfirminfo['amount_ttc'] - $amount1);
  570. $formquestion = array(
  571. 'text' => $langs->trans('TypeAmountOfEachNewDiscount'),
  572. array('type' => 'text', 'name' => 'amount_ttc_1', 'label' => $langs->trans("AmountTTC").' 1', 'value' => $amount1, 'size' => '5'),
  573. array('type' => 'text', 'name' => 'amount_ttc_2', 'label' => $langs->trans("AmountTTC").' 2', 'value' => $amount2, 'size' => '5')
  574. );
  575. $langs->load("dict");
  576. 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);
  577. }
  578. } else {
  579. dol_print_error($db);
  580. }
  581. if ($isCustomer) {
  582. print '</div>'; // class="fichehalfright"
  583. print '</div>'; // class="fichecenter"
  584. }
  585. }
  586. print '<div class="clearboth"></div><br>';
  587. /*
  588. * List discount consumed (=liees a une ligne de facture ou facture)
  589. */
  590. print load_fiche_titre($langs->trans("DiscountAlreadyCounted"));
  591. if ($isCustomer) {
  592. if ($isSupplier) {
  593. print '<div class="fichecenter">';
  594. print '<div class="fichehalfleft fichehalfleft-lg">';
  595. print load_fiche_titre($langs->trans("CustomerDiscounts"), '', '');
  596. }
  597. // Discount linked to invoice lines
  598. $sql = "SELECT rc.rowid, rc.amount_ht, rc.amount_tva, rc.amount_ttc, rc.tva_tx, rc.vat_src_code,";
  599. $sql .= " rc.multicurrency_amount_ht, rc.multicurrency_amount_tva, rc.multicurrency_amount_ttc,";
  600. $sql .= " rc.datec as dc, rc.description, rc.fk_facture_line, rc.fk_facture_source,";
  601. $sql .= " u.login, u.rowid as user_id,";
  602. $sql .= " f.rowid as invoiceid, f.ref,";
  603. $sql .= " fa.ref as invoice_source_ref, fa.type as type";
  604. $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
  605. $sql .= " , ".MAIN_DB_PREFIX."user as u";
  606. $sql .= " , ".MAIN_DB_PREFIX."facturedet as fc";
  607. $sql .= " , ".MAIN_DB_PREFIX."societe_remise_except as rc";
  608. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as fa ON rc.fk_facture_source = fa.rowid";
  609. $sql .= " WHERE rc.fk_soc = ".((int) $object->id);
  610. $sql .= " AND rc.fk_facture_line = fc.rowid";
  611. $sql .= " AND fc.fk_facture = f.rowid";
  612. $sql .= " AND rc.fk_user = u.rowid";
  613. $sql .= " AND rc.discount_type = 0"; // Eliminate supplier discounts
  614. $sql .= " ORDER BY dc DESC";
  615. //$sql.= " UNION ";
  616. // Discount linked to invoices
  617. $sql2 = "SELECT rc.rowid, rc.amount_ht, rc.amount_tva, rc.amount_ttc, rc.tva_tx, rc.vat_src_code,";
  618. $sql2 .= " rc.multicurrency_amount_ht, rc.multicurrency_amount_tva, rc.multicurrency_amount_ttc,";
  619. $sql2 .= " rc.datec as dc, rc.description, rc.fk_facture, rc.fk_facture_source,";
  620. $sql2 .= " u.login, u.rowid as user_id,";
  621. $sql2 .= " f.rowid as invoiceid, f.ref,";
  622. $sql2 .= " fa.ref as invoice_source_ref, fa.type as type";
  623. $sql2 .= " FROM ".MAIN_DB_PREFIX."facture as f";
  624. $sql2 .= " , ".MAIN_DB_PREFIX."user as u";
  625. $sql2 .= " , ".MAIN_DB_PREFIX."societe_remise_except as rc";
  626. $sql2 .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as fa ON rc.fk_facture_source = fa.rowid";
  627. $sql2 .= " WHERE rc.fk_soc = ".((int) $object->id);
  628. $sql2 .= " AND rc.fk_facture = f.rowid";
  629. $sql2 .= " AND rc.fk_user = u.rowid";
  630. $sql2 .= " AND rc.discount_type = 0"; // Eliminate supplier discounts
  631. $sql2 .= " ORDER BY dc DESC";
  632. $resql = $db->query($sql);
  633. $resql2 = null;
  634. if ($resql) {
  635. $resql2 = $db->query($sql2);
  636. }
  637. if ($resql2) {
  638. print '<div class="div-table-responsive-no-min">';
  639. print '<table class="noborder centpercent">';
  640. print '<tr class="liste_titre">';
  641. print '<td class="widthdate">'.$langs->trans("Date").'</td>'; // Need 120+ for format with AM/PM
  642. print '<td>'.$langs->trans("ReasonDiscount").'</td>';
  643. print '<td class="nowrap">'.$langs->trans("ConsumedBy").'</td>';
  644. print '<td class="right">'.$langs->trans("AmountHT").'</td>';
  645. if (!empty($conf->multicurrency->enabled)) {
  646. print '<td class="right tdoverflowmax125" title="'.dol_escape_htmltag($langs->trans("MulticurrencyAmountHT")).'">'.$langs->trans("MulticurrencyAmountHT").'</td>';
  647. }
  648. print '<td class="right">'.$langs->trans("VATRate").'</td>';
  649. print '<td class="right">'.$langs->trans("AmountTTC").'</td>';
  650. if (!empty($conf->multicurrency->enabled)) {
  651. print '<td class="right tdoverflowmax125" title="'.dol_escape_htmltag($langs->trans("MulticurrencyAmountTTC")).'">'.$langs->trans("MulticurrencyAmountTTC").'</td>';
  652. }
  653. print '<td width="100" class="center">'.$langs->trans("Author").'</td>';
  654. print '<td width="50">&nbsp;</td>';
  655. print '</tr>';
  656. $tab_sqlobj = array();
  657. $tab_sqlobjOrder = array();
  658. $num = $db->num_rows($resql);
  659. if ($num > 0) {
  660. for ($i = 0; $i < $num; $i++) {
  661. $sqlobj = $db->fetch_object($resql);
  662. $tab_sqlobj[] = $sqlobj;
  663. $tab_sqlobjOrder[] = $db->jdate($sqlobj->dc);
  664. }
  665. }
  666. $db->free($resql);
  667. $num = $db->num_rows($resql2);
  668. for ($i = 0; $i < $num; $i++) {
  669. $sqlobj = $db->fetch_object($resql2);
  670. $tab_sqlobj[] = $sqlobj;
  671. $tab_sqlobjOrder[] = $db->jdate($sqlobj->dc);
  672. }
  673. $db->free($resql2);
  674. $array1_sort_order = SORT_DESC;
  675. array_multisort($tab_sqlobjOrder, $array1_sort_order, $tab_sqlobj);
  676. $num = count($tab_sqlobj);
  677. if ($num > 0) {
  678. $i = 0;
  679. while ($i < $num) {
  680. $obj = array_shift($tab_sqlobj);
  681. print '<tr class="oddeven">';
  682. print '<td>'.dol_print_date($db->jdate($obj->dc), 'dayhour').'</td>';
  683. if (preg_match('/\(CREDIT_NOTE\)/', $obj->description)) {
  684. print '<td class="minwidth100">';
  685. $facturestatic->id = $obj->fk_facture_source;
  686. $facturestatic->ref = $obj->invoice_source_ref;
  687. $facturestatic->type = $obj->type;
  688. print preg_replace('/\(CREDIT_NOTE\)/', $langs->trans("CreditNote"), $obj->description).' '.$facturestatic->getNomURl(1);
  689. print '</td>';
  690. } elseif (preg_match('/\(DEPOSIT\)/', $obj->description)) {
  691. print '<td class="minwidth100">';
  692. $facturestatic->id = $obj->fk_facture_source;
  693. $facturestatic->ref = $obj->invoice_source_ref;
  694. $facturestatic->type = $obj->type;
  695. print preg_replace('/\(DEPOSIT\)/', $langs->trans("InvoiceDeposit"), $obj->description).' '.$facturestatic->getNomURl(1);
  696. print '</td>';
  697. } elseif (preg_match('/\(EXCESS RECEIVED\)/', $obj->description)) {
  698. print '<td class="minwidth100">';
  699. $facturestatic->id = $obj->fk_facture_source;
  700. $facturestatic->ref = $obj->invoice_source_ref;
  701. $facturestatic->type = $obj->type;
  702. print preg_replace('/\(EXCESS RECEIVED\)/', $langs->trans("Invoice"), $obj->description).' '.$facturestatic->getNomURl(1);
  703. print '</td>';
  704. } else {
  705. print '<td class="minwidth100">';
  706. print $obj->description;
  707. print '</td>';
  708. }
  709. print '<td class="left nowrap">';
  710. if ($obj->invoiceid) {
  711. print '<a href="'.DOL_URL_ROOT.'/compta/facture/card.php?facid='.$obj->invoiceid.'">'.img_object($langs->trans("ShowBill"), 'bill').' '.$obj->ref.'</a>';
  712. }
  713. print '</td>';
  714. print '<td class="right">'.price($obj->amount_ht).'</td>';
  715. if (!empty($conf->multicurrency->enabled)) {
  716. print '<td class="right">'.price($obj->multicurrency_amount_ht).'</td>';
  717. }
  718. print '<td class="right">'.vatrate($obj->tva_tx.($obj->vat_src_code ? ' ('.$obj->vat_src_code.')' : ''), true).'</td>';
  719. print '<td class="right">'.price($obj->amount_ttc).'</td>';
  720. if (!empty($conf->multicurrency->enabled)) {
  721. print '<td class="right">'.price($obj->multicurrency_amount_ttc).'</td>';
  722. }
  723. print '<td class="center">';
  724. print '<a href="'.DOL_URL_ROOT.'/user/card.php?id='.$obj->user_id.'">'.img_object($langs->trans("ShowUser"), 'user').' '.$obj->login.'</a>';
  725. print '</td>';
  726. print '<td>&nbsp;</td>';
  727. print '</tr>';
  728. $i++;
  729. }
  730. } else {
  731. $colspan = 8;
  732. if (!empty($conf->multicurrency->enabled)) {
  733. $colspan += 2;
  734. }
  735. print '<tr><td colspan="'.$colspan.'" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
  736. }
  737. print "</table>";
  738. print '</div>';
  739. } else {
  740. dol_print_error($db);
  741. }
  742. }
  743. if ($isSupplier) {
  744. if ($isCustomer) {
  745. print '</div>'; // class="fichehalfleft"
  746. print '<div class="fichehalfright fichehalfright-lg">';
  747. print load_fiche_titre($langs->trans("SupplierDiscounts"), '', '');
  748. }
  749. // Discount linked to invoice lines
  750. $sql = "SELECT rc.rowid, rc.amount_ht, rc.amount_tva, rc.amount_ttc, rc.tva_tx, rc.vat_src_code,";
  751. $sql .= " rc.multicurrency_amount_ht, rc.multicurrency_amount_tva, rc.multicurrency_amount_ttc,";
  752. $sql .= " rc.datec as dc, rc.description, rc.fk_invoice_supplier_line,";
  753. $sql .= " rc.fk_invoice_supplier_source,";
  754. $sql .= " u.login, u.rowid as user_id,";
  755. $sql .= " f.rowid as invoiceid, f.ref as ref,";
  756. $sql .= " fa.ref as invoice_source_ref, fa.type as type";
  757. $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
  758. $sql .= " , ".MAIN_DB_PREFIX."user as u";
  759. $sql .= " , ".MAIN_DB_PREFIX."facture_fourn_det as fc";
  760. $sql .= " , ".MAIN_DB_PREFIX."societe_remise_except as rc";
  761. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture_fourn as fa ON rc.fk_invoice_supplier_source = fa.rowid";
  762. $sql .= " WHERE rc.fk_soc = ".((int) $object->id);
  763. $sql .= " AND rc.fk_invoice_supplier_line = fc.rowid";
  764. $sql .= " AND fc.fk_facture_fourn = f.rowid";
  765. $sql .= " AND rc.fk_user = u.rowid";
  766. $sql .= " AND rc.discount_type = 1"; // Eliminate customer discounts
  767. $sql .= " ORDER BY dc DESC";
  768. //$sql.= " UNION ";
  769. // Discount linked to invoices
  770. $sql2 = "SELECT rc.rowid, rc.amount_ht, rc.amount_tva, rc.amount_ttc, rc.tva_tx, rc.vat_src_code,";
  771. $sql2 .= " rc.multicurrency_amount_ht, rc.multicurrency_amount_tva, rc.multicurrency_amount_ttc,";
  772. $sql2 .= " rc.datec as dc, rc.description, rc.fk_invoice_supplier,";
  773. $sql2 .= " rc.fk_invoice_supplier_source,";
  774. $sql2 .= " u.login, u.rowid as user_id,";
  775. $sql2 .= " f.rowid as invoiceid, f.ref as ref,";
  776. $sql2 .= " fa.ref as invoice_source_ref, fa.type as type";
  777. $sql2 .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
  778. $sql2 .= " , ".MAIN_DB_PREFIX."user as u";
  779. $sql2 .= " , ".MAIN_DB_PREFIX."societe_remise_except as rc";
  780. $sql2 .= " LEFT JOIN ".MAIN_DB_PREFIX."facture_fourn as fa ON rc.fk_invoice_supplier_source = fa.rowid";
  781. $sql2 .= " WHERE rc.fk_soc = ".((int) $object->id);
  782. $sql2 .= " AND rc.fk_invoice_supplier = f.rowid";
  783. $sql2 .= " AND rc.fk_user = u.rowid";
  784. $sql2 .= " AND rc.discount_type = 1"; // Eliminate customer discounts
  785. $sql2 .= " ORDER BY dc DESC";
  786. $resql = $db->query($sql);
  787. $resql2 = null;
  788. if ($resql) {
  789. $resql2 = $db->query($sql2);
  790. }
  791. if ($resql2) {
  792. print '<div class="div-table-responsive-no-min">';
  793. print '<table class="noborder centpercent">';
  794. print '<tr class="liste_titre">';
  795. print '<td class="widthdate">'.$langs->trans("Date").'</td>'; // Need 120+ for format with AM/PM
  796. print '<td>'.$langs->trans("ReasonDiscount").'</td>';
  797. print '<td class="nowrap">'.$langs->trans("ConsumedBy").'</td>';
  798. print '<td class="right">'.$langs->trans("AmountHT").'</td>';
  799. if (!empty($conf->multicurrency->enabled)) {
  800. print '<td class="right toverflowmax125" title="'.dol_escape_htmltag($langs->trans("MulticurrencyAmountHT")).'">'.$langs->trans("MulticurrencyAmountHT").'</td>';
  801. }
  802. print '<td class="right">'.$langs->trans("VATRate").'</td>';
  803. print '<td class="right">'.$langs->trans("AmountTTC").'</td>';
  804. if (!empty($conf->multicurrency->enabled)) {
  805. print '<td class="right tdoverflowmax125" title="'.dol_escape_htmltag($langs->trans("MulticurrencyAmountTTC")).'">'.$langs->trans("MulticurrencyAmountTTC").'</td>';
  806. }
  807. print '<td width="100" class="center">'.$langs->trans("Author").'</td>';
  808. print '<td width="50">&nbsp;</td>';
  809. print '</tr>';
  810. $tab_sqlobj = array();
  811. $tab_sqlobjOrder = array();
  812. $num = $db->num_rows($resql);
  813. if ($num > 0) {
  814. for ($i = 0; $i < $num; $i++) {
  815. $sqlobj = $db->fetch_object($resql);
  816. $tab_sqlobj[] = $sqlobj;
  817. $tab_sqlobjOrder[] = $db->jdate($sqlobj->dc);
  818. }
  819. }
  820. $db->free($resql);
  821. $num = $db->num_rows($resql2);
  822. for ($i = 0; $i < $num; $i++) {
  823. $sqlobj = $db->fetch_object($resql2);
  824. $tab_sqlobj[] = $sqlobj;
  825. $tab_sqlobjOrder[] = $db->jdate($sqlobj->dc);
  826. }
  827. $db->free($resql2);
  828. $array1_sort_order = SORT_DESC;
  829. array_multisort($tab_sqlobjOrder, $array1_sort_order, $tab_sqlobj);
  830. $num = count($tab_sqlobj);
  831. if ($num > 0) {
  832. $i = 0;
  833. while ($i < $num) {
  834. $obj = array_shift($tab_sqlobj);
  835. print '<tr class="oddeven">';
  836. print '<td>'.dol_print_date($db->jdate($obj->dc), 'dayhour').'</td>';
  837. if (preg_match('/\(CREDIT_NOTE\)/', $obj->description)) {
  838. print '<td class="minwidth100">';
  839. $facturefournstatic->id = $obj->fk_invoice_supplier_source;
  840. $facturefournstatic->ref = $obj->invoice_source_ref;
  841. $facturefournstatic->type = $obj->type;
  842. print preg_replace('/\(CREDIT_NOTE\)/', $langs->trans("CreditNote"), $obj->description).' '.$facturefournstatic->getNomURl(1);
  843. print '</td>';
  844. } elseif (preg_match('/\(DEPOSIT\)/', $obj->description)) {
  845. print '<td class="minwidth100">';
  846. $facturefournstatic->id = $obj->fk_invoice_supplier_source;
  847. $facturefournstatic->ref = $obj->invoice_source_ref;
  848. $facturefournstatic->type = $obj->type;
  849. print preg_replace('/\(DEPOSIT\)/', $langs->trans("InvoiceDeposit"), $obj->description).' '.$facturefournstatic->getNomURl(1);
  850. print '</td>';
  851. } elseif (preg_match('/\(EXCESS PAID\)/', $obj->description)) {
  852. print '<td class="minwidth100">';
  853. $facturefournstatic->id = $obj->fk_invoice_supplier_source;
  854. $facturefournstatic->ref = $obj->invoice_source_ref;
  855. $facturefournstatic->type = $obj->type;
  856. print preg_replace('/\(EXCESS PAID\)/', $langs->trans("Invoice"), $obj->description).' '.$facturefournstatic->getNomURl(1);
  857. print '</td>';
  858. } else {
  859. print '<td class="minwidth100">';
  860. print $obj->description;
  861. print '</td>';
  862. }
  863. print '<td class="left nowrap">';
  864. if ($obj->invoiceid) {
  865. print '<a href="'.DOL_URL_ROOT.'/fourn/facture/card.php?facid='.$obj->invoiceid.'">'.img_object($langs->trans("ShowBill"), 'bill').' '.$obj->ref.'</a>';
  866. }
  867. print '</td>';
  868. print '<td class="right">'.price($obj->amount_ht).'</td>';
  869. if (!empty($conf->multicurrency->enabled)) {
  870. print '<td class="right">'.price($obj->multicurrency_amount_ht).'</td>';
  871. }
  872. print '<td class="right">'.vatrate($obj->tva_tx.($obj->vat_src_code ? ' ('.$obj->vat_src_code.')' : ''), true).'</td>';
  873. print '<td class="right">'.price($obj->amount_ttc).'</td>';
  874. if (!empty($conf->multicurrency->enabled)) {
  875. print '<td class="right">'.price($obj->multicurrency_amount_ttc).'</td>';
  876. }
  877. print '<td class="center">';
  878. print '<a href="'.DOL_URL_ROOT.'/user/card.php?id='.$obj->user_id.'">'.img_object($langs->trans("ShowUser"), 'user').' '.$obj->login.'</a>';
  879. print '</td>';
  880. print '<td>&nbsp;</td>';
  881. print '</tr>';
  882. $i++;
  883. }
  884. } else {
  885. $colspan = 8;
  886. if (!empty($conf->multicurrency->enabled)) {
  887. $colspan += 2;
  888. }
  889. print '<tr><td colspan="'.$colspan.'" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
  890. }
  891. print "</table>";
  892. print '</div>';
  893. } else {
  894. dol_print_error($db);
  895. }
  896. if ($isCustomer) {
  897. print '</div>'; // class="fichehalfright"
  898. print '</div>'; // class="fichecenter"
  899. }
  900. }
  901. }
  902. // End of page
  903. llxFooter();
  904. $db->close();