remx.php 43 KB

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