remx.php 47 KB

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