remx.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  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->hasRight('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->hasRight('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->hasRight('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. $linkback = '<a href="'.DOL_URL_ROOT.'/societe/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  212. dol_banner_tab($object, 'socid', $linkback, ($user->socid ? 0 : 1), 'rowid', 'nom');
  213. print '<div class="fichecenter">';
  214. print '<div class="underbanner clearboth"></div>';
  215. if (!$isCustomer && !$isSupplier) {
  216. print '<p class="opacitymedium">'.$langs->trans('ThirdpartyIsNeitherCustomerNorClientSoCannotHaveDiscounts').'</p>';
  217. print dol_get_fiche_end();
  218. print '</form>';
  219. llxFooter();
  220. $db->close();
  221. exit;
  222. }
  223. print '<div class="div-table-responsive-no-min">';
  224. print '<table class="border centpercent tableforfield borderbottom">';
  225. if ($isCustomer) { // Calcul avoirs client en cours
  226. $remise_all = $remise_user = 0;
  227. $sql = "SELECT SUM(rc.amount_ht) as amount, rc.fk_user";
  228. $sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as rc";
  229. $sql .= " WHERE rc.fk_soc = ".((int) $object->id);
  230. $sql .= " AND rc.entity = ".((int) $conf->entity);
  231. $sql .= " AND discount_type = 0"; // Exclude supplier discounts
  232. $sql .= " AND (fk_facture_line IS NULL AND fk_facture IS NULL)";
  233. $sql .= " GROUP BY rc.fk_user";
  234. $resql = $db->query($sql);
  235. if ($resql) {
  236. $obj = $db->fetch_object($resql);
  237. $remise_all += (!empty($obj->amount) ? $obj->amount : 0);
  238. if (!empty($obj->fk_user) && $obj->fk_user == $user->id) {
  239. $remise_user += (!empty($obj->amount) ? $obj->amount : 0);
  240. }
  241. } else {
  242. dol_print_error($db);
  243. }
  244. print '<tr><td>'.$langs->trans("CustomerAbsoluteDiscountAllUsers").'</td>';
  245. print '<td class="amount">'.price($remise_all, 1, $langs, 1, -1, -1, $conf->currency).' '.$langs->trans("HT");
  246. if (empty($user->fk_soc)) { // No need to show this for external users
  247. print $form->textwithpicto('', $langs->trans("CustomerAbsoluteDiscountMy").': '.price($remise_user, 1, $langs, 1, -1, -1, $conf->currency).' '.$langs->trans("HT"));
  248. }
  249. print '</td></tr>';
  250. }
  251. if ($isSupplier) {
  252. // Calcul avoirs fournisseur en cours
  253. $remise_all = $remise_user = 0;
  254. $sql = "SELECT SUM(rc.amount_ht) as amount, rc.fk_user";
  255. $sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as rc";
  256. $sql .= " WHERE rc.fk_soc = ".((int) $object->id);
  257. $sql .= " AND rc.entity = ".((int) $conf->entity);
  258. $sql .= " AND discount_type = 1"; // Exclude customer discounts
  259. $sql .= " AND (fk_invoice_supplier_line IS NULL AND fk_invoice_supplier IS NULL)";
  260. $sql .= " GROUP BY rc.fk_user";
  261. $resql = $db->query($sql);
  262. if ($resql) {
  263. $obj = $db->fetch_object($resql);
  264. $remise_all += (!empty($obj->amount) ? $obj->amount : 0);
  265. if (!empty($obj->fk_user) && $obj->fk_user == $user->id) {
  266. $remise_user += (!empty($obj->amount) ? $obj->amount : 0);
  267. }
  268. } else {
  269. dol_print_error($db);
  270. }
  271. print '<tr><td>'.$langs->trans("SupplierAbsoluteDiscountAllUsers").'</td>';
  272. print '<td class="amount">'.price($remise_all, 1, $langs, 1, -1, -1, $conf->currency).' '.$langs->trans("HT");
  273. if (empty($user->fk_soc)) { // No need to show this for external users
  274. print $form->textwithpicto('', $langs->trans("SupplierAbsoluteDiscountMy").' : '.price($remise_user, 1, $langs, 1, -1, -1, $conf->currency).' '.$langs->trans("HT"));
  275. }
  276. print '</td></tr>';
  277. }
  278. print '</table>';
  279. print '</div>';
  280. print '</div>'; // close fichecenter
  281. print dol_get_fiche_end();
  282. if ($action == 'create_remise') {
  283. if ($user->rights->societe->creer) {
  284. print '<br>';
  285. $discount_type = GETPOSTISSET('discount_type') ? GETPOST('discount_type', 'alpha') : 0;
  286. if ($isCustomer && $isSupplier) {
  287. $discounttypelabel = $discount_type == 1 ? 'NewSupplierGlobalDiscount' : 'NewClientGlobalDiscount';
  288. } else {
  289. $discounttypelabel = 'NewGlobalDiscount';
  290. }
  291. print load_fiche_titre($langs->trans($discounttypelabel), '', '');
  292. if ($isSupplier && $discount_type == 1) {
  293. print '<input type="hidden" name="discount_type" value="1" />';
  294. } else {
  295. print '<input type="hidden" name="discount_type" value="0" />';
  296. }
  297. print dol_get_fiche_head();
  298. print '<div class="div-table-responsive-no-min">';
  299. print '<table class="border centpercent">';
  300. /*if ($isCustomer && $isSupplier) {
  301. print '<tr><td class="titlefield fieldrequired">'.$langs->trans('DiscountType').'</td>';
  302. 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>';
  303. 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>';
  304. print '</td></tr>';
  305. }*/
  306. // Amount
  307. print '<tr><td class="titlefield fieldrequired">'.$langs->trans("Amount").'</td>';
  308. print '<td><input type="text" size="5" name="amount" value="'.price2num(GETPOST("amount")).'" autofocus>';
  309. print '<span class="hideonsmartphone">&nbsp;'.$langs->trans("Currency".$conf->currency).'</span></td></tr>';
  310. // Price base (HT / TTC)
  311. print '<tr><td class="titlefield">'.$langs->trans("PriceBase").'</td>';
  312. print '<td>';
  313. print $form->selectPriceBaseType(GETPOST("price_base_type"), "price_base_type");
  314. print '</td></tr>';
  315. // VAT
  316. print '<tr><td>'.$langs->trans("VAT").'</td>';
  317. print '<td>';
  318. print $form->load_tva('tva_tx', GETPOSTISSET('tva_tx') ? GETPOST('tva_tx', 'alpha') : 0, $mysoc, $object, 0, 0, '', 0, 1);
  319. print '</td></tr>';
  320. print '<tr><td class="fieldrequired" >'.$langs->trans("NoteReason").'</td>';
  321. print '<td><input type="text" class="quatrevingtpercent" name="desc" value="'.GETPOST('desc', 'alphanohtml').'"></td></tr>';
  322. print "</table>";
  323. print '</div>';
  324. print dol_get_fiche_end();
  325. }
  326. if ($user->rights->societe->creer) {
  327. print '<div class="center">';
  328. print '<input type="submit" class="button" name="submit" value="'.$langs->trans("AddGlobalDiscount").'">';
  329. if (!empty($backtopage)) {
  330. print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  331. print '<input type="submit" class="button button-cancel" name="cancel" value="'.$langs->trans("Cancel").'">';
  332. }
  333. print '</div>';
  334. }
  335. }
  336. print '</form>';
  337. print '<br>';
  338. if ($action == 'remove') {
  339. print $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id.'&remid='.GETPOST('remid'), $langs->trans('RemoveDiscount'), $langs->trans('ConfirmRemoveDiscount'), 'confirm_remove', '', 0, 1);
  340. }
  341. /*
  342. * List not consumed available credits (= linked to no invoice and no invoice line)
  343. */
  344. if ($isCustomer && !$isSupplier) {
  345. $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());
  346. } elseif (!$isCustomer && $isSupplier) {
  347. $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());
  348. } else {
  349. $newcardbutton = '';
  350. }
  351. print load_fiche_titre($langs->trans("DiscountStillRemaining"), $newcardbutton);
  352. if ($isCustomer) {
  353. $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());
  354. if ($isSupplier) {
  355. print '<div class="fichecenter">';
  356. print '<div class="fichehalfleft fichehalfleft-lg">';
  357. print load_fiche_titre($langs->trans("CustomerDiscounts"), $newcardbutton, '');
  358. }
  359. $sql = "SELECT rc.rowid, rc.amount_ht, rc.amount_tva, rc.amount_ttc, rc.tva_tx, rc.vat_src_code,";
  360. $sql .= " rc.multicurrency_amount_ht, rc.multicurrency_amount_tva, rc.multicurrency_amount_ttc,";
  361. $sql .= " rc.datec as dc, rc.description,";
  362. $sql .= " rc.fk_facture_source,";
  363. $sql .= " u.login, u.rowid as user_id,";
  364. $sql .= " fa.ref as ref, fa.type as type";
  365. $sql .= " FROM ".MAIN_DB_PREFIX."user as u, ".MAIN_DB_PREFIX."societe_remise_except as rc";
  366. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as fa ON rc.fk_facture_source = fa.rowid";
  367. $sql .= " WHERE rc.fk_soc = ".((int) $object->id);
  368. $sql .= " AND rc.entity = ".((int) $conf->entity);
  369. $sql .= " AND u.rowid = rc.fk_user";
  370. $sql .= " AND rc.discount_type = 0"; // Eliminate supplier discounts
  371. $sql .= " AND (rc.fk_facture_line IS NULL AND rc.fk_facture IS NULL)";
  372. $sql .= " ORDER BY rc.datec DESC";
  373. $resql = $db->query($sql);
  374. if ($resql) {
  375. print '<div class="div-table-responsive-no-min">';
  376. print '<table class="noborder centpercent">';
  377. print '<tr class="liste_titre">';
  378. print '<td class="widthdate">'.$langs->trans("Date").'</td>'; // Need 120+ for format with AM/PM
  379. print '<td>'.$langs->trans("ReasonDiscount").'</td>';
  380. print '<td class="nowrap">'.$langs->trans("ConsumedBy").'</td>';
  381. print '<td class="right">'.$langs->trans("AmountHT").'</td>';
  382. if (isModEnabled('multicompany')) {
  383. print '<td class="right tdoverflowmax125" title="'.dol_escape_htmltag($langs->trans("MulticurrencyAmountHT")).'">'.$langs->trans("MulticurrencyAmountHT").'</td>';
  384. }
  385. print '<td class="right">'.$langs->trans("VATRate").'</td>';
  386. print '<td class="right">'.$langs->trans("AmountTTC").'</td>';
  387. if (isModEnabled('multicompany')) {
  388. print '<td class="right tdoverflowmax125" title="'.dol_escape_htmltag($langs->trans("MulticurrencyAmountTTC")).'">'.$langs->trans("MulticurrencyAmountTTC").'</td>';
  389. }
  390. print '<td width="100" class="center">'.$langs->trans("DiscountOfferedBy").'</td>';
  391. print '<td width="50">&nbsp;</td>';
  392. print '</tr>';
  393. $showconfirminfo = array();
  394. $i = 0;
  395. $num = $db->num_rows($resql);
  396. if ($num > 0) {
  397. while ($i < $num) {
  398. $obj = $db->fetch_object($resql);
  399. print '<tr class="oddeven">';
  400. print '<td>'.dol_print_date($db->jdate($obj->dc), 'dayhour', 'tzuserrel').'</td>';
  401. if (preg_match('/\(CREDIT_NOTE\)/', $obj->description)) {
  402. print '<td class="tdoverflowmax100">';
  403. $facturestatic->id = $obj->fk_facture_source;
  404. $facturestatic->ref = $obj->ref;
  405. $facturestatic->type = $obj->type;
  406. print preg_replace('/\(CREDIT_NOTE\)/', $langs->trans("CreditNote"), $obj->description).' '.$facturestatic->getNomURl(1);
  407. print '</td>';
  408. } elseif (preg_match('/\(DEPOSIT\)/', $obj->description)) {
  409. print '<td class="tdoverflowmax100">';
  410. $facturestatic->id = $obj->fk_facture_source;
  411. $facturestatic->ref = $obj->ref;
  412. $facturestatic->type = $obj->type;
  413. print preg_replace('/\(DEPOSIT\)/', $langs->trans("InvoiceDeposit"), $obj->description).' '.$facturestatic->getNomURl(1);
  414. print '</td>';
  415. } elseif (preg_match('/\(EXCESS RECEIVED\)/', $obj->description)) {
  416. print '<td class="tdoverflowmax100">';
  417. $facturestatic->id = $obj->fk_facture_source;
  418. $facturestatic->ref = $obj->ref;
  419. $facturestatic->type = $obj->type;
  420. print preg_replace('/\(EXCESS RECEIVED\)/', $langs->trans("ExcessReceived"), $obj->description).' '.$facturestatic->getNomURl(1);
  421. print '</td>';
  422. } else {
  423. print '<td class="tdoverflowmax100" title="'.dol_escape_htmltag($obj->description).'">';
  424. print dol_escape_htmltag($obj->description);
  425. print '</td>';
  426. }
  427. print '<td class="nowrap"><span class="opacitymedium">'.$langs->trans("NotConsumed").'</span></td>';
  428. print '<td class="right nowraponall amount">'.price($obj->amount_ht).'</td>';
  429. if (isModEnabled('multicompany')) {
  430. print '<td class="right nowraponall amount">'.price($obj->multicurrency_amount_ht).'</td>';
  431. }
  432. print '<td class="right nowraponall">'.vatrate($obj->tva_tx.($obj->vat_src_code ? ' ('.$obj->vat_src_code.')' : ''), true).'</td>';
  433. print '<td class="right nowraponall amount">'.price($obj->amount_ttc).'</td>';
  434. if (isModEnabled('multicompany')) {
  435. print '<td class="right nowraponall amount">'.price($obj->multicurrency_amount_ttc).'</td>';
  436. }
  437. print '<td class="center">';
  438. print '<a href="'.DOL_URL_ROOT.'/user/card.php?id='.$obj->user_id.'">'.img_object($langs->trans("ShowUser"), 'user').' '.$obj->login.'</a>';
  439. print '</td>';
  440. if ($user->rights->societe->creer || $user->rights->facture->creer) {
  441. print '<td class="center nowrap">';
  442. 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>';
  443. 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>';
  444. print '</td>';
  445. } else {
  446. print '<td>&nbsp;</td>';
  447. }
  448. print '</tr>';
  449. if ($action == 'split' && GETPOST('remid') == $obj->rowid) {
  450. $showconfirminfo['rowid'] = $obj->rowid;
  451. $showconfirminfo['amount_ttc'] = $obj->amount_ttc;
  452. }
  453. $i++;
  454. }
  455. } else {
  456. $colspan = 8;
  457. if (isModEnabled('multicompany')) {
  458. $colspan += 2;
  459. }
  460. print '<tr><td colspan="'.$colspan.'"><span class="opacitymedium">'.$langs->trans("None").'</span></td></tr>';
  461. }
  462. $db->free($resql);
  463. print "</table>";
  464. print '</div>';
  465. if (count($showconfirminfo)) {
  466. $amount1 = price2num($showconfirminfo['amount_ttc'] / 2, 'MT');
  467. $amount2 = ($showconfirminfo['amount_ttc'] - $amount1);
  468. $formquestion = array(
  469. 'text' => $langs->trans('TypeAmountOfEachNewDiscount'),
  470. array('type' => 'text', 'name' => 'amount_ttc_1', 'label' => $langs->trans("AmountTTC").' 1', 'value' => $amount1, 'size' => '5'),
  471. array('type' => 'text', 'name' => 'amount_ttc_2', 'label' => $langs->trans("AmountTTC").' 2', 'value' => $amount2, 'size' => '5')
  472. );
  473. $langs->load("dict");
  474. 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);
  475. }
  476. } else {
  477. dol_print_error($db);
  478. }
  479. }
  480. if ($isSupplier) {
  481. if ($isCustomer) {
  482. $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());
  483. print '</div>'; // class="fichehalfleft"
  484. print '<div class="fichehalfright fichehalfright-lg">';
  485. print load_fiche_titre($langs->trans("SupplierDiscounts"), $newcardbutton, '');
  486. }
  487. /*
  488. * Liste remises fixes fournisseur restant en cours (= liees a aucune facture ni ligne de facture)
  489. */
  490. $sql = "SELECT rc.rowid, rc.amount_ht, rc.amount_tva, rc.amount_ttc, rc.tva_tx, rc.vat_src_code,";
  491. $sql .= " rc.multicurrency_amount_ht, rc.multicurrency_amount_tva, rc.multicurrency_amount_ttc,";
  492. $sql .= " rc.datec as dc, rc.description,";
  493. $sql .= " rc.fk_invoice_supplier_source,";
  494. $sql .= " u.login, u.rowid as user_id,";
  495. $sql .= " fa.ref, fa.type as type";
  496. $sql .= " FROM ".MAIN_DB_PREFIX."user as u, ".MAIN_DB_PREFIX."societe_remise_except as rc";
  497. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture_fourn as fa ON rc.fk_invoice_supplier_source = fa.rowid";
  498. $sql .= " WHERE rc.fk_soc = ".((int) $object->id);
  499. $sql .= " AND rc.entity = ".((int) $conf->entity);
  500. $sql .= " AND u.rowid = rc.fk_user";
  501. $sql .= " AND rc.discount_type = 1"; // Eliminate customer discounts
  502. $sql .= " AND (rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_line IS NULL)";
  503. $sql .= " ORDER BY rc.datec DESC";
  504. $resql = $db->query($sql);
  505. if ($resql) {
  506. print '<div class="div-table-responsive-no-min">';
  507. print '<table class="noborder centpercent">';
  508. print '<tr class="liste_titre">';
  509. print '<td class="widthdate">'.$langs->trans("Date").'</td>'; // Need 120+ for format with AM/PM
  510. print '<td>'.$langs->trans("ReasonDiscount").'</td>';
  511. print '<td class="nowrap">'.$langs->trans("ConsumedBy").'</td>';
  512. print '<td class="right">'.$langs->trans("AmountHT").'</td>';
  513. if (isModEnabled('multicompany')) {
  514. print '<td class="right tdoverflowmax125" title="'.dol_escape_htmltag($langs->trans("MulticurrencyAmountHT")).'">'.$langs->trans("MulticurrencyAmountHT").'</td>';
  515. }
  516. print '<td class="right">'.$langs->trans("VATRate").'</td>';
  517. print '<td class="right">'.$langs->trans("AmountTTC").'</td>';
  518. if (isModEnabled('multicompany')) {
  519. print '<td class="right tdoverflowmax125" title="'.dol_escape_htmltag($langs->trans("MulticurrencyAmountTTC")).'">'.$langs->trans("MulticurrencyAmountTTC").'</td>';
  520. }
  521. print '<td width="100" class="center">'.$langs->trans("DiscountOfferedBy").'</td>';
  522. print '<td width="50">&nbsp;</td>';
  523. print '</tr>';
  524. $showconfirminfo = array();
  525. $i = 0;
  526. $num = $db->num_rows($resql);
  527. if ($num > 0) {
  528. while ($i < $num) {
  529. $obj = $db->fetch_object($resql);
  530. print '<tr class="oddeven">';
  531. print '<td>'.dol_print_date($db->jdate($obj->dc), 'dayhour', 'tzuserrel').'</td>';
  532. if (preg_match('/\(CREDIT_NOTE\)/', $obj->description)) {
  533. print '<td class="tdoverflowmax100">';
  534. $facturefournstatic->id = $obj->fk_invoice_supplier_source;
  535. $facturefournstatic->ref = $obj->ref;
  536. $facturefournstatic->type = $obj->type;
  537. print preg_replace('/\(CREDIT_NOTE\)/', $langs->trans("CreditNote"), $obj->description).' '.$facturefournstatic->getNomURl(1);
  538. print '</td>';
  539. } elseif (preg_match('/\(DEPOSIT\)/', $obj->description)) {
  540. print '<td class="tdoverflowmax100">';
  541. $facturefournstatic->id = $obj->fk_invoice_supplier_source;
  542. $facturefournstatic->ref = $obj->ref;
  543. $facturefournstatic->type = $obj->type;
  544. print preg_replace('/\(DEPOSIT\)/', $langs->trans("InvoiceDeposit"), $obj->description).' '.$facturefournstatic->getNomURl(1);
  545. print '</td>';
  546. } elseif (preg_match('/\(EXCESS PAID\)/', $obj->description)) {
  547. print '<td class="tdoverflowmax100">';
  548. $facturefournstatic->id = $obj->fk_invoice_supplier_source;
  549. $facturefournstatic->ref = $obj->ref;
  550. $facturefournstatic->type = $obj->type;
  551. print preg_replace('/\(EXCESS PAID\)/', $langs->trans("ExcessPaid"), $obj->description).' '.$facturefournstatic->getNomURl(1);
  552. print '</td>';
  553. } else {
  554. print '<td class="tdoverflowmax100" title="'.dol_escape_htmltag($obj->description).'">';
  555. print dol_escape_htmltag($obj->description);
  556. print '</td>';
  557. }
  558. print '<td class="nowrap"><span class="opacitymedium">'.$langs->trans("NotConsumed").'</span></td>';
  559. print '<td class="right nowraponall amount">'.price($obj->amount_ht).'</td>';
  560. if (isModEnabled('multicompany')) {
  561. print '<td class="right nowraponall amount">'.price($obj->multicurrency_amount_ht).'</td>';
  562. }
  563. print '<td class="right">'.vatrate($obj->tva_tx.($obj->vat_src_code ? ' ('.$obj->vat_src_code.')' : ''), true).'</td>';
  564. print '<td class="right nowraponall amount">'.price($obj->amount_ttc).'</td>';
  565. if (isModEnabled('multicompany')) {
  566. print '<td class="right nowraponall amount">'.price($obj->multicurrency_amount_ttc).'</td>';
  567. }
  568. print '<td class="center">';
  569. print '<a href="'.DOL_URL_ROOT.'/user/card.php?id='.$obj->user_id.'">'.img_object($langs->trans("ShowUser"), 'user').' '.$obj->login.'</a>';
  570. print '</td>';
  571. if ($user->rights->societe->creer || $user->rights->facture->creer) {
  572. print '<td class="center nowrap">';
  573. 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>';
  574. 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>';
  575. print '</td>';
  576. } else {
  577. print '<td>&nbsp;</td>';
  578. }
  579. print '</tr>';
  580. if ($action == 'split' && GETPOST('remid') == $obj->rowid) {
  581. $showconfirminfo['rowid'] = $obj->rowid;
  582. $showconfirminfo['amount_ttc'] = $obj->amount_ttc;
  583. }
  584. $i++;
  585. }
  586. } else {
  587. $colspan = 8;
  588. if (isModEnabled('multicompany')) {
  589. $colspan += 2;
  590. }
  591. print '<tr><td colspan="'.$colspan.'"><span class="opacitymedium">'.$langs->trans("None").'</span></td></tr>';
  592. }
  593. $db->free($resql);
  594. print "</table>";
  595. print '</div>';
  596. if (count($showconfirminfo)) {
  597. $amount1 = price2num($showconfirminfo['amount_ttc'] / 2, 'MT');
  598. $amount2 = ($showconfirminfo['amount_ttc'] - $amount1);
  599. $formquestion = array(
  600. 'text' => $langs->trans('TypeAmountOfEachNewDiscount'),
  601. array('type' => 'text', 'name' => 'amount_ttc_1', 'label' => $langs->trans("AmountTTC").' 1', 'value' => $amount1, 'size' => '5'),
  602. array('type' => 'text', 'name' => 'amount_ttc_2', 'label' => $langs->trans("AmountTTC").' 2', 'value' => $amount2, 'size' => '5')
  603. );
  604. $langs->load("dict");
  605. 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);
  606. }
  607. } else {
  608. dol_print_error($db);
  609. }
  610. if ($isCustomer) {
  611. print '</div>'; // class="fichehalfright"
  612. print '</div>'; // class="fichecenter"
  613. }
  614. }
  615. print '<div class="clearboth"></div><br><br>';
  616. /*
  617. * List discount consumed (=liees a une ligne de facture ou facture)
  618. */
  619. print load_fiche_titre($langs->trans("DiscountAlreadyCounted"));
  620. if ($isCustomer) {
  621. if ($isSupplier) {
  622. print '<div class="fichecenter">';
  623. print '<div class="fichehalfleft fichehalfleft-lg">';
  624. print load_fiche_titre($langs->trans("CustomerDiscounts"), '', '');
  625. }
  626. // Discount linked to invoice lines
  627. $sql = "SELECT rc.rowid, rc.amount_ht, rc.amount_tva, rc.amount_ttc, rc.tva_tx, rc.vat_src_code,";
  628. $sql .= " rc.multicurrency_amount_ht, rc.multicurrency_amount_tva, rc.multicurrency_amount_ttc,";
  629. $sql .= " rc.datec as dc, rc.description, rc.fk_facture_line, rc.fk_facture_source,";
  630. $sql .= " u.login, u.rowid as user_id,";
  631. $sql .= " f.rowid as invoiceid, f.ref,";
  632. $sql .= " fa.ref as invoice_source_ref, fa.type as type";
  633. $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
  634. $sql .= " , ".MAIN_DB_PREFIX."user as u";
  635. $sql .= " , ".MAIN_DB_PREFIX."facturedet as fc";
  636. $sql .= " , ".MAIN_DB_PREFIX."societe_remise_except as rc";
  637. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as fa ON rc.fk_facture_source = fa.rowid";
  638. $sql .= " WHERE rc.fk_soc = ".((int) $object->id);
  639. $sql .= " AND rc.fk_facture_line = fc.rowid";
  640. $sql .= " AND fc.fk_facture = f.rowid";
  641. $sql .= " AND rc.fk_user = u.rowid";
  642. $sql .= " AND rc.discount_type = 0"; // Eliminate supplier discounts
  643. $sql .= " ORDER BY dc DESC";
  644. //$sql.= " UNION ";
  645. // Discount linked to invoices
  646. $sql2 = "SELECT rc.rowid, rc.amount_ht, rc.amount_tva, rc.amount_ttc, rc.tva_tx, rc.vat_src_code,";
  647. $sql2 .= " rc.multicurrency_amount_ht, rc.multicurrency_amount_tva, rc.multicurrency_amount_ttc,";
  648. $sql2 .= " rc.datec as dc, rc.description, rc.fk_facture, rc.fk_facture_source,";
  649. $sql2 .= " u.login, u.rowid as user_id,";
  650. $sql2 .= " f.rowid as invoiceid, f.ref,";
  651. $sql2 .= " fa.ref as invoice_source_ref, fa.type as type";
  652. $sql2 .= " FROM ".MAIN_DB_PREFIX."facture as f";
  653. $sql2 .= " , ".MAIN_DB_PREFIX."user as u";
  654. $sql2 .= " , ".MAIN_DB_PREFIX."societe_remise_except as rc";
  655. $sql2 .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as fa ON rc.fk_facture_source = fa.rowid";
  656. $sql2 .= " WHERE rc.fk_soc = ".((int) $object->id);
  657. $sql2 .= " AND rc.fk_facture = f.rowid";
  658. $sql2 .= " AND rc.fk_user = u.rowid";
  659. $sql2 .= " AND rc.discount_type = 0"; // Eliminate supplier discounts
  660. $sql2 .= " ORDER BY dc DESC";
  661. $resql = $db->query($sql);
  662. $resql2 = null;
  663. if ($resql) {
  664. $resql2 = $db->query($sql2);
  665. }
  666. if ($resql2) {
  667. print '<div class="div-table-responsive-no-min">';
  668. print '<table class="noborder centpercent">';
  669. print '<tr class="liste_titre">';
  670. print '<td class="widthdate">'.$langs->trans("Date").'</td>'; // Need 120+ for format with AM/PM
  671. print '<td>'.$langs->trans("ReasonDiscount").'</td>';
  672. print '<td class="nowrap">'.$langs->trans("ConsumedBy").'</td>';
  673. print '<td class="right">'.$langs->trans("AmountHT").'</td>';
  674. if (isModEnabled('multicompany')) {
  675. print '<td class="right tdoverflowmax125" title="'.dol_escape_htmltag($langs->trans("MulticurrencyAmountHT")).'">'.$langs->trans("MulticurrencyAmountHT").'</td>';
  676. }
  677. print '<td class="right">'.$langs->trans("VATRate").'</td>';
  678. print '<td class="right">'.$langs->trans("AmountTTC").'</td>';
  679. if (isModEnabled('multicompany')) {
  680. print '<td class="right tdoverflowmax125" title="'.dol_escape_htmltag($langs->trans("MulticurrencyAmountTTC")).'">'.$langs->trans("MulticurrencyAmountTTC").'</td>';
  681. }
  682. print '<td width="100" class="center">'.$langs->trans("Author").'</td>';
  683. print '<td width="50">&nbsp;</td>';
  684. print '</tr>';
  685. $tab_sqlobj = array();
  686. $tab_sqlobjOrder = array();
  687. $num = $db->num_rows($resql);
  688. if ($num > 0) {
  689. for ($i = 0; $i < $num; $i++) {
  690. $sqlobj = $db->fetch_object($resql);
  691. $tab_sqlobj[] = $sqlobj;
  692. $tab_sqlobjOrder[] = $db->jdate($sqlobj->dc);
  693. }
  694. }
  695. $db->free($resql);
  696. $num = $db->num_rows($resql2);
  697. for ($i = 0; $i < $num; $i++) {
  698. $sqlobj = $db->fetch_object($resql2);
  699. $tab_sqlobj[] = $sqlobj;
  700. $tab_sqlobjOrder[] = $db->jdate($sqlobj->dc);
  701. }
  702. $db->free($resql2);
  703. $array1_sort_order = SORT_DESC;
  704. array_multisort($tab_sqlobjOrder, $array1_sort_order, $tab_sqlobj);
  705. $num = count($tab_sqlobj);
  706. if ($num > 0) {
  707. $i = 0;
  708. while ($i < $num) {
  709. $obj = array_shift($tab_sqlobj);
  710. print '<tr class="oddeven">';
  711. print '<td>'.dol_print_date($db->jdate($obj->dc), 'dayhour').'</td>';
  712. if (preg_match('/\(CREDIT_NOTE\)/', $obj->description)) {
  713. print '<td class="tdoverflowmax100">';
  714. $facturestatic->id = $obj->fk_facture_source;
  715. $facturestatic->ref = $obj->invoice_source_ref;
  716. $facturestatic->type = $obj->type;
  717. print preg_replace('/\(CREDIT_NOTE\)/', $langs->trans("CreditNote"), $obj->description).' '.$facturestatic->getNomURl(1);
  718. print '</td>';
  719. } elseif (preg_match('/\(DEPOSIT\)/', $obj->description)) {
  720. print '<td class="tdoverflowmax100">';
  721. $facturestatic->id = $obj->fk_facture_source;
  722. $facturestatic->ref = $obj->invoice_source_ref;
  723. $facturestatic->type = $obj->type;
  724. print preg_replace('/\(DEPOSIT\)/', $langs->trans("InvoiceDeposit"), $obj->description).' '.$facturestatic->getNomURl(1);
  725. print '</td>';
  726. } elseif (preg_match('/\(EXCESS RECEIVED\)/', $obj->description)) {
  727. print '<td class="tdoverflowmax100">';
  728. $facturestatic->id = $obj->fk_facture_source;
  729. $facturestatic->ref = $obj->invoice_source_ref;
  730. $facturestatic->type = $obj->type;
  731. print preg_replace('/\(EXCESS RECEIVED\)/', $langs->trans("Invoice"), $obj->description).' '.$facturestatic->getNomURl(1);
  732. print '</td>';
  733. } else {
  734. print '<td class="tdoverflowmax100" title="'.dol_escape_htmltag($obj->description).'">';
  735. print dol_escape_htmltag($obj->description);
  736. print '</td>';
  737. }
  738. print '<td class="left nowrap">';
  739. if ($obj->invoiceid) {
  740. print '<a href="'.DOL_URL_ROOT.'/compta/facture/card.php?facid='.$obj->invoiceid.'">'.img_object($langs->trans("ShowBill"), 'bill').' '.$obj->ref.'</a>';
  741. }
  742. print '</td>';
  743. print '<td class="right nowraponall amount">'.price($obj->amount_ht).'</td>';
  744. if (isModEnabled('multicompany')) {
  745. print '<td class="right nowraponall amount">'.price($obj->multicurrency_amount_ht).'</td>';
  746. }
  747. print '<td class="right nowraponall">'.vatrate($obj->tva_tx.($obj->vat_src_code ? ' ('.$obj->vat_src_code.')' : ''), true).'</td>';
  748. print '<td class="right nowraponall amount">'.price($obj->amount_ttc).'</td>';
  749. if (isModEnabled('multicompany')) {
  750. print '<td class="right">'.price($obj->multicurrency_amount_ttc).'</td>';
  751. }
  752. print '<td class="center">';
  753. print '<a href="'.DOL_URL_ROOT.'/user/card.php?id='.$obj->user_id.'">'.img_object($langs->trans("ShowUser"), 'user').' '.$obj->login.'</a>';
  754. print '</td>';
  755. print '<td>&nbsp;</td>';
  756. print '</tr>';
  757. $i++;
  758. }
  759. } else {
  760. $colspan = 8;
  761. if (isModEnabled('multicompany')) {
  762. $colspan += 2;
  763. }
  764. print '<tr><td colspan="'.$colspan.'"><span class="opacitymedium">'.$langs->trans("None").'</span></td></tr>';
  765. }
  766. print "</table>";
  767. print '</div>';
  768. } else {
  769. dol_print_error($db);
  770. }
  771. }
  772. if ($isSupplier) {
  773. if ($isCustomer) {
  774. print '</div>'; // class="fichehalfleft"
  775. print '<div class="fichehalfright fichehalfright-lg">';
  776. print load_fiche_titre($langs->trans("SupplierDiscounts"), '', '');
  777. }
  778. // Discount linked to invoice lines
  779. $sql = "SELECT rc.rowid, rc.amount_ht, rc.amount_tva, rc.amount_ttc, rc.tva_tx, rc.vat_src_code,";
  780. $sql .= " rc.multicurrency_amount_ht, rc.multicurrency_amount_tva, rc.multicurrency_amount_ttc,";
  781. $sql .= " rc.datec as dc, rc.description, rc.fk_invoice_supplier_line,";
  782. $sql .= " rc.fk_invoice_supplier_source,";
  783. $sql .= " u.login, u.rowid as user_id,";
  784. $sql .= " f.rowid as invoiceid, f.ref as ref,";
  785. $sql .= " fa.ref as invoice_source_ref, fa.type as type";
  786. $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
  787. $sql .= " , ".MAIN_DB_PREFIX."user as u";
  788. $sql .= " , ".MAIN_DB_PREFIX."facture_fourn_det as fc";
  789. $sql .= " , ".MAIN_DB_PREFIX."societe_remise_except as rc";
  790. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture_fourn as fa ON rc.fk_invoice_supplier_source = fa.rowid";
  791. $sql .= " WHERE rc.fk_soc = ".((int) $object->id);
  792. $sql .= " AND rc.fk_invoice_supplier_line = fc.rowid";
  793. $sql .= " AND fc.fk_facture_fourn = f.rowid";
  794. $sql .= " AND rc.fk_user = u.rowid";
  795. $sql .= " AND rc.discount_type = 1"; // Eliminate customer discounts
  796. $sql .= " ORDER BY dc DESC";
  797. //$sql.= " UNION ";
  798. // Discount linked to invoices
  799. $sql2 = "SELECT rc.rowid, rc.amount_ht, rc.amount_tva, rc.amount_ttc, rc.tva_tx, rc.vat_src_code,";
  800. $sql2 .= " rc.multicurrency_amount_ht, rc.multicurrency_amount_tva, rc.multicurrency_amount_ttc,";
  801. $sql2 .= " rc.datec as dc, rc.description, rc.fk_invoice_supplier,";
  802. $sql2 .= " rc.fk_invoice_supplier_source,";
  803. $sql2 .= " u.login, u.rowid as user_id,";
  804. $sql2 .= " f.rowid as invoiceid, f.ref as ref,";
  805. $sql2 .= " fa.ref as invoice_source_ref, fa.type as type";
  806. $sql2 .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
  807. $sql2 .= " , ".MAIN_DB_PREFIX."user as u";
  808. $sql2 .= " , ".MAIN_DB_PREFIX."societe_remise_except as rc";
  809. $sql2 .= " LEFT JOIN ".MAIN_DB_PREFIX."facture_fourn as fa ON rc.fk_invoice_supplier_source = fa.rowid";
  810. $sql2 .= " WHERE rc.fk_soc = ".((int) $object->id);
  811. $sql2 .= " AND rc.fk_invoice_supplier = f.rowid";
  812. $sql2 .= " AND rc.fk_user = u.rowid";
  813. $sql2 .= " AND rc.discount_type = 1"; // Eliminate customer discounts
  814. $sql2 .= " ORDER BY dc DESC";
  815. $resql = $db->query($sql);
  816. $resql2 = null;
  817. if ($resql) {
  818. $resql2 = $db->query($sql2);
  819. }
  820. if ($resql2) {
  821. print '<div class="div-table-responsive-no-min">';
  822. print '<table class="noborder centpercent">';
  823. print '<tr class="liste_titre">';
  824. print '<td class="widthdate">'.$langs->trans("Date").'</td>'; // Need 120+ for format with AM/PM
  825. print '<td>'.$langs->trans("ReasonDiscount").'</td>';
  826. print '<td class="nowrap">'.$langs->trans("ConsumedBy").'</td>';
  827. print '<td class="right">'.$langs->trans("AmountHT").'</td>';
  828. if (isModEnabled('multicompany')) {
  829. print '<td class="right toverflowmax125" title="'.dol_escape_htmltag($langs->trans("MulticurrencyAmountHT")).'">'.$langs->trans("MulticurrencyAmountHT").'</td>';
  830. }
  831. print '<td class="right">'.$langs->trans("VATRate").'</td>';
  832. print '<td class="right">'.$langs->trans("AmountTTC").'</td>';
  833. if (isModEnabled('multicompany')) {
  834. print '<td class="right tdoverflowmax125" title="'.dol_escape_htmltag($langs->trans("MulticurrencyAmountTTC")).'">'.$langs->trans("MulticurrencyAmountTTC").'</td>';
  835. }
  836. print '<td width="100" class="center">'.$langs->trans("Author").'</td>';
  837. print '<td width="50">&nbsp;</td>';
  838. print '</tr>';
  839. $tab_sqlobj = array();
  840. $tab_sqlobjOrder = array();
  841. $num = $db->num_rows($resql);
  842. if ($num > 0) {
  843. for ($i = 0; $i < $num; $i++) {
  844. $sqlobj = $db->fetch_object($resql);
  845. $tab_sqlobj[] = $sqlobj;
  846. $tab_sqlobjOrder[] = $db->jdate($sqlobj->dc);
  847. }
  848. }
  849. $db->free($resql);
  850. $num = $db->num_rows($resql2);
  851. for ($i = 0; $i < $num; $i++) {
  852. $sqlobj = $db->fetch_object($resql2);
  853. $tab_sqlobj[] = $sqlobj;
  854. $tab_sqlobjOrder[] = $db->jdate($sqlobj->dc);
  855. }
  856. $db->free($resql2);
  857. $array1_sort_order = SORT_DESC;
  858. array_multisort($tab_sqlobjOrder, $array1_sort_order, $tab_sqlobj);
  859. $num = count($tab_sqlobj);
  860. if ($num > 0) {
  861. $i = 0;
  862. while ($i < $num) {
  863. $obj = array_shift($tab_sqlobj);
  864. print '<tr class="oddeven">';
  865. print '<td>'.dol_print_date($db->jdate($obj->dc), 'dayhour').'</td>';
  866. if (preg_match('/\(CREDIT_NOTE\)/', $obj->description)) {
  867. print '<td class="tdoverflowmax100">';
  868. $facturefournstatic->id = $obj->fk_invoice_supplier_source;
  869. $facturefournstatic->ref = $obj->invoice_source_ref;
  870. $facturefournstatic->type = $obj->type;
  871. print preg_replace('/\(CREDIT_NOTE\)/', $langs->trans("CreditNote"), $obj->description).' '.$facturefournstatic->getNomURl(1);
  872. print '</td>';
  873. } elseif (preg_match('/\(DEPOSIT\)/', $obj->description)) {
  874. print '<td class="tdoverflowmax100">';
  875. $facturefournstatic->id = $obj->fk_invoice_supplier_source;
  876. $facturefournstatic->ref = $obj->invoice_source_ref;
  877. $facturefournstatic->type = $obj->type;
  878. print preg_replace('/\(DEPOSIT\)/', $langs->trans("InvoiceDeposit"), $obj->description).' '.$facturefournstatic->getNomURl(1);
  879. print '</td>';
  880. } elseif (preg_match('/\(EXCESS PAID\)/', $obj->description)) {
  881. print '<td class="tdoverflowmax100">';
  882. $facturefournstatic->id = $obj->fk_invoice_supplier_source;
  883. $facturefournstatic->ref = $obj->invoice_source_ref;
  884. $facturefournstatic->type = $obj->type;
  885. print preg_replace('/\(EXCESS PAID\)/', $langs->trans("Invoice"), $obj->description).' '.$facturefournstatic->getNomURl(1);
  886. print '</td>';
  887. } else {
  888. print '<td class="tdoverflowmax100" title="'.dol_escape_htmltag($obj->description).'">';
  889. print dol_escape_htmltag($obj->description);
  890. print '</td>';
  891. }
  892. print '<td class="left nowrap">';
  893. if ($obj->invoiceid) {
  894. print '<a href="'.DOL_URL_ROOT.'/fourn/facture/card.php?facid='.$obj->invoiceid.'">'.img_object($langs->trans("ShowBill"), 'bill').' '.$obj->ref.'</a>';
  895. }
  896. print '</td>';
  897. print '<td class="right nowraponall amount">'.price($obj->amount_ht).'</td>';
  898. if (isModEnabled('multicompany')) {
  899. print '<td class="right nowraponall amount">'.price($obj->multicurrency_amount_ht).'</td>';
  900. }
  901. print '<td class="right">'.vatrate($obj->tva_tx.($obj->vat_src_code ? ' ('.$obj->vat_src_code.')' : ''), true).'</td>';
  902. print '<td class="right nowraponall amount">'.price($obj->amount_ttc).'</td>';
  903. if (isModEnabled('multicompany')) {
  904. print '<td class="right nowraponall amount">'.price($obj->multicurrency_amount_ttc).'</td>';
  905. }
  906. print '<td class="center">';
  907. print '<a href="'.DOL_URL_ROOT.'/user/card.php?id='.$obj->user_id.'">'.img_object($langs->trans("ShowUser"), 'user').' '.$obj->login.'</a>';
  908. print '</td>';
  909. print '<td>&nbsp;</td>';
  910. print '</tr>';
  911. $i++;
  912. }
  913. } else {
  914. $colspan = 8;
  915. if (isModEnabled('multicompany')) {
  916. $colspan += 2;
  917. }
  918. print '<tr><td colspan="'.$colspan.'"><span class="opacitymedium">'.$langs->trans("None").'</span></td></tr>';
  919. }
  920. print "</table>";
  921. print '</div>';
  922. } else {
  923. dol_print_error($db);
  924. }
  925. if ($isCustomer) {
  926. print '</div>'; // class="fichehalfright"
  927. print '</div>'; // class="fichecenter"
  928. }
  929. }
  930. }
  931. // End of page
  932. llxFooter();
  933. $db->close();