consumption.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. <?php
  2. /* Copyright (C) 2012-2013 Philippe Berthet <berthet@systune.be>
  3. * Copyright (C) 2004-2016 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2013-2015 Juanjo Menent <jmenent@2byte.es>
  5. * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
  6. * Copyright (C) 2015-2017 Ferran Marcet <fmarcet@2byte.es>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \file htdocs/contact/consumption.php
  23. * \ingroup societe
  24. * \brief Add a tab on thirdparty view to list all products/services bought or sells by thirdparty
  25. */
  26. // Load Dolibarr environment
  27. require "../main.inc.php";
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/contact.lib.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  31. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  33. $optioncss = GETPOST('optioncss', 'aZ'); // Option for the css output (always '' except when 'print')
  34. $contextpage = GETPOST('contextpage', 'aZ') ? GETPOST('contextpage', 'aZ') : str_replace('_', '', basename(dirname(__FILE__)).basename(__FILE__, '.php')); // To manage different context of search
  35. $id = GETPOST('id', 'int');
  36. $object = new Contact($db);
  37. if ($id > 0) {
  38. $object->fetch($id);
  39. }
  40. if (empty($object->thirdparty)) {
  41. $object->fetch_thirdparty();
  42. }
  43. $socid = $object->thirdparty->id;
  44. // Sort & Order fields
  45. $limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
  46. $sortfield = GETPOST('sortfield', 'aZ09comma');
  47. $sortorder = GETPOST('sortorder', 'aZ09comma');
  48. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  49. if (empty($page) || $page == -1) {
  50. $page = 0;
  51. } // If $page is not defined, or '' or -1
  52. $offset = $limit * $page;
  53. $pageprev = $page - 1;
  54. $pagenext = $page + 1;
  55. if (!$sortorder) {
  56. $sortorder = 'DESC';
  57. }
  58. if (!$sortfield) {
  59. $sortfield = 'dateprint';
  60. }
  61. // Search fields
  62. $sref = GETPOST("sref");
  63. $sprod_fulldescr = GETPOST("sprod_fulldescr");
  64. $month = GETPOST('month', 'int');
  65. $year = GETPOST('year', 'int');
  66. // Clean up on purge search criteria ?
  67. if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { // Both test are required to be compatible with all browsers
  68. $sref = '';
  69. $sprod_fulldescr = '';
  70. $year = '';
  71. $month = '';
  72. }
  73. // Customer or supplier selected in drop box
  74. $thirdTypeSelect = GETPOST("third_select_id");
  75. $type_element = GETPOSTISSET('type_element') ?GETPOST('type_element') : '';
  76. // Load translation files required by the page
  77. $langs->loadLangs(array("companies", "bills", "orders", "suppliers", "propal", "interventions", "contracts", "products"));
  78. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  79. $hookmanager->initHooks(array('consumptioncontact'));
  80. $result = restrictedArea($user, 'contact', $object->id, 'socpeople&societe');
  81. /*
  82. * Actions
  83. */
  84. $parameters = array('id'=>$id);
  85. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  86. if ($reshook < 0) {
  87. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  88. }
  89. /*
  90. * View
  91. */
  92. $form = new Form($db);
  93. $formother = new FormOther($db);
  94. $productstatic = new Product($db);
  95. $objsoc = new Societe($db);
  96. $title = (!empty($conf->global->SOCIETE_ADDRESSES_MANAGEMENT) ? $langs->trans("Contacts") : $langs->trans("ContactsAddresses"));
  97. $help_url = 'EN:Module_Third_Parties|FR:Module_Tiers|ES:Empresas';
  98. llxHeader('', $title, $help_url);
  99. if (empty($id)) {
  100. dol_print_error($db);
  101. exit;
  102. }
  103. $head = contact_prepare_head($object);
  104. print dol_get_fiche_head($head, 'consumption', $langs->trans("ContactsAddresses"), -1, 'contact');
  105. $linkback = '<a href="'.DOL_URL_ROOT.'/contact/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  106. $morehtmlref = '<a href="'.DOL_URL_ROOT.'/contact/vcard.php?id='.$object->id.'" class="refid">';
  107. $morehtmlref .= img_picto($langs->trans("Download").' '.$langs->trans("VCard"), 'vcard.png', 'class="valignmiddle marginleftonly paddingrightonly"');
  108. $morehtmlref .= '</a>';
  109. $morehtmlref .= '<div class="refidno">';
  110. if (empty($conf->global->SOCIETE_DISABLE_CONTACTS)) {
  111. $objsoc->fetch($socid);
  112. // Thirdparty
  113. $morehtmlref .= $langs->trans('ThirdParty').' : ';
  114. if ($objsoc->id > 0) {
  115. $morehtmlref .= $objsoc->getNomUrl(1, 'contact');
  116. } else {
  117. $morehtmlref .= $langs->trans("ContactNotLinkedToCompany");
  118. }
  119. }
  120. $morehtmlref .= '</div>';
  121. dol_banner_tab($object, 'id', $linkback, ($user->socid ? 0 : 1), 'rowid', 'nom', $morehtmlref);
  122. print '<div class="fichecenter">';
  123. print '<div class="underbanner clearboth"></div>';
  124. print '<table class="border centpercent tableforfield">';
  125. // Civility
  126. print '<tr><td class="titlefield">'.$langs->trans("UserTitle").'</td><td>';
  127. print $object->getCivilityLabel();
  128. print '</td></tr>';
  129. if ($object->thirdparty->client) {
  130. $thirdTypeArray['customer'] = $langs->trans("customer");
  131. if (isModEnabled("propal") && $user->rights->propal->lire) {
  132. $elementTypeArray['propal'] = $langs->transnoentitiesnoconv('Proposals');
  133. }
  134. if (isModEnabled('commande') && $user->rights->commande->lire) {
  135. $elementTypeArray['order'] = $langs->transnoentitiesnoconv('Orders');
  136. }
  137. if (isModEnabled('facture') && $user->rights->facture->lire) {
  138. $elementTypeArray['invoice'] = $langs->transnoentitiesnoconv('Invoices');
  139. }
  140. if (isModEnabled('contrat') && $user->rights->contrat->lire) {
  141. $elementTypeArray['contract'] = $langs->transnoentitiesnoconv('Contracts');
  142. }
  143. }
  144. if (isModEnabled('ficheinter') && $user->rights->ficheinter->lire) {
  145. $elementTypeArray['fichinter'] = $langs->transnoentitiesnoconv('Interventions');
  146. }
  147. if ($object->thirdparty->fournisseur) {
  148. $thirdTypeArray['supplier'] = $langs->trans("supplier");
  149. if ((isModEnabled("fournisseur") && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire) || (isModEnabled("supplier_invoice") && $user->rights->supplier_invoice->lire)) {
  150. $elementTypeArray['supplier_invoice'] = $langs->transnoentitiesnoconv('SuppliersInvoices');
  151. }
  152. if ((isModEnabled("fournisseur") && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->commande->lire) || (isModEnabled("supplier_order") && $user->rights->supplier_order->lire)) {
  153. $elementTypeArray['supplier_order'] = $langs->transnoentitiesnoconv('SuppliersOrders');
  154. }
  155. // There no contact type for supplier proposals
  156. // if ((isModEnabled("fournisseur") && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) || isModEnabled("supplier_order") || isModEnabled("supplier_invoice")) && $user->rights->supplier_proposal->lire) $elementTypeArray['supplier_proposal']=$langs->transnoentitiesnoconv('SupplierProposals');
  157. }
  158. print '</table>';
  159. print '</div>';
  160. print dol_get_fiche_end();
  161. print '<br>';
  162. print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'?id='.$id.'">';
  163. print '<input type="hidden" name="token" value="'.newToken().'">';
  164. $sql_select = '';
  165. if ($type_element == 'fichinter') { // Customer : show products from invoices
  166. require_once DOL_DOCUMENT_ROOT.'/fichinter/class/fichinter.class.php';
  167. $documentstatic = new Fichinter($db);
  168. $sql_select = 'SELECT f.rowid as doc_id, f.ref as doc_number, \'1\' as doc_type, f.datec as dateprint, f.fk_statut as status, tc.libelle as type_contact_label, ';
  169. $tables_from = MAIN_DB_PREFIX.'fichinterdet d';
  170. $tables_from .= ' LEFT JOIN '.MAIN_DB_PREFIX.'fichinter as f ON d.fk_fichinter=f.rowid';
  171. $tables_from .= ' INNER JOIN '.MAIN_DB_PREFIX.'element_contact ec ON ec.element_id=f.rowid AND ec.fk_socpeople = '.((int) $object->id);
  172. $tables_from .= ' INNER JOIN '.MAIN_DB_PREFIX."c_type_contact tc ON (ec.fk_c_type_contact=tc.rowid and tc.element='fichinter' and tc.source='external' and tc.active=1)";
  173. $where = ' WHERE f.entity IN ('.getEntity('ficheinter').')';
  174. $dateprint = 'f.datec';
  175. $doc_number = 'f.ref';
  176. } elseif ($type_element == 'invoice') { // Customer : show products from invoices
  177. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  178. $documentstatic = new Facture($db);
  179. $sql_select = 'SELECT f.rowid as doc_id, f.ref as doc_number, f.type as doc_type, f.datef as dateprint, f.fk_statut as status, f.paye as paid, tc.libelle as type_contact_label, ';
  180. $tables_from = MAIN_DB_PREFIX.'facturedet d';
  181. $tables_from .= ' LEFT JOIN '.MAIN_DB_PREFIX.'facture as f ON d.fk_facture=f.rowid';
  182. $tables_from .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product p ON d.fk_product=p.rowid';
  183. $tables_from .= ' INNER JOIN '.MAIN_DB_PREFIX.'element_contact ec ON ec.element_id=f.rowid AND ec.fk_socpeople = '.((int) $object->id);
  184. $tables_from .= ' INNER JOIN '.MAIN_DB_PREFIX."c_type_contact tc ON (ec.fk_c_type_contact=tc.rowid and tc.element='facture' and tc.source='external' and tc.active=1)";
  185. $where = " WHERE f.entity IN (".getEntity('invoice').")";
  186. $dateprint = 'f.datef';
  187. $doc_number = 'f.ref';
  188. $thirdTypeSelect = 'customer';
  189. } elseif ($type_element == 'propal') {
  190. require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
  191. $documentstatic = new Propal($db);
  192. $sql_select = 'SELECT c.rowid as doc_id, c.ref as doc_number, \'1\' as doc_type, c.datep as dateprint, c.fk_statut as status, tc.libelle as type_contact_label, ';
  193. $tables_from = MAIN_DB_PREFIX.'propaldet d';
  194. $tables_from .= ' LEFT JOIN '.MAIN_DB_PREFIX.'propal as c ON d.fk_propal=c.rowid';
  195. $tables_from .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product p ON d.fk_product=p.rowid';
  196. $tables_from .= ' INNER JOIN '.MAIN_DB_PREFIX.'element_contact ec ON ec.element_id=c.rowid AND ec.fk_socpeople = '.((int) $object->id);
  197. $tables_from .= ' INNER JOIN '.MAIN_DB_PREFIX."c_type_contact tc ON (ec.fk_c_type_contact=tc.rowid and tc.element='propal' and tc.source='external' and tc.active=1)";
  198. $where = ' WHERE c.entity IN ('.getEntity('propal').')';
  199. $dateprint = 'c.datep';
  200. $doc_number = 'c.ref';
  201. $thirdTypeSelect = 'customer';
  202. } elseif ($type_element == 'order') {
  203. require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
  204. $documentstatic = new Commande($db);
  205. $sql_select = 'SELECT c.rowid as doc_id, c.ref as doc_number, \'1\' as doc_type, c.date_commande as dateprint, c.fk_statut as status, tc.libelle as type_contact_label, ';
  206. $tables_from = MAIN_DB_PREFIX.'commandedet d';
  207. $tables_from .= ' LEFT JOIN '.MAIN_DB_PREFIX.'commande as c ON d.fk_commande=c.rowid';
  208. $tables_from .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product p ON d.fk_product=p.rowid';
  209. $tables_from .= ' INNER JOIN '.MAIN_DB_PREFIX.'element_contact ec ON ec.element_id=c.rowid AND ec.fk_socpeople = '.((int) $object->id);
  210. $tables_from .= ' INNER JOIN '.MAIN_DB_PREFIX."c_type_contact tc ON (ec.fk_c_type_contact=tc.rowid and tc.element='commande' and tc.source='external' and tc.active=1)";
  211. $where = ' WHERE c.entity IN ('.getEntity('order').')';
  212. $dateprint = 'c.date_commande';
  213. $doc_number = 'c.ref';
  214. $thirdTypeSelect = 'customer';
  215. } elseif ($type_element == 'supplier_invoice') { // Supplier : Show products from invoices.
  216. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
  217. $documentstatic = new FactureFournisseur($db);
  218. $sql_select = 'SELECT f.rowid as doc_id, f.ref as doc_number, \'1\' as doc_type, f.datef as dateprint, f.fk_statut as status, f.paye as paid, tc.libelle as type_contact_label, ';
  219. $tables_from = MAIN_DB_PREFIX.'facture_fourn_det d';
  220. $tables_from .= ' LEFT JOIN '.MAIN_DB_PREFIX.'facture_fourn as f ON d.fk_facture_fourn=f.rowid';
  221. $tables_from .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product p ON d.fk_product=p.rowid';
  222. $tables_from .= ' INNER JOIN '.MAIN_DB_PREFIX.'element_contact ec ON ec.element_id=f.rowid AND ec.fk_socpeople = '.((int) $object->id);
  223. $tables_from .= ' INNER JOIN '.MAIN_DB_PREFIX."c_type_contact tc ON (ec.fk_c_type_contact=tc.rowid and tc.element='invoice_supplier' and tc.source='external' and tc.active=1)";
  224. $where = ' WHERE f.entity IN ('.getEntity($documentstatic->element).')';
  225. $dateprint = 'f.datef';
  226. $doc_number = 'f.ref';
  227. $thirdTypeSelect = 'supplier';
  228. //} elseif ($type_element == 'supplier_proposal') {
  229. // require_once DOL_DOCUMENT_ROOT.'/supplier_proposal/class/supplier_proposal.class.php';
  230. // $documentstatic=new SupplierProposal($db);
  231. // $sql_select = 'SELECT c.rowid as doc_id, c.ref as doc_number, \'1\' as doc_type, c.date_valid as dateprint, c.fk_statut as status, ';
  232. // $tables_from = MAIN_DB_PREFIX."supplier_proposal as c,".MAIN_DB_PREFIX."supplier_proposaldet as d";
  233. // $where = " WHERE c.fk_soc = s.rowid AND s.rowid = ".((int) $socid);
  234. // $where.= " AND d.fk_supplier_proposal = c.rowid";
  235. // $where.= " AND c.entity = ".$conf->entity;
  236. // $dateprint = 'c.date_valid';
  237. // $doc_number='c.ref';
  238. // $thirdTypeSelect='supplier';
  239. //}
  240. } elseif ($type_element == 'supplier_order') { // Supplier : Show products from orders.
  241. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
  242. $documentstatic = new CommandeFournisseur($db);
  243. $sql_select = 'SELECT c.rowid as doc_id, c.ref as doc_number, \'1\' as doc_type, c.date_valid as dateprint, c.fk_statut as status, tc.libelle as type_contact_label, ';
  244. $tables_from = MAIN_DB_PREFIX.'commande_fournisseurdet d';
  245. $tables_from .= ' LEFT JOIN '.MAIN_DB_PREFIX.'commande_fournisseur as c ON d.fk_commande=c.rowid';
  246. $tables_from .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product p ON d.fk_product=p.rowid';
  247. $tables_from .= ' INNER JOIN '.MAIN_DB_PREFIX.'element_contact ec ON ec.element_id=c.rowid AND ec.fk_socpeople = '.((int) $object->id);
  248. $tables_from .= ' INNER JOIN '.MAIN_DB_PREFIX."c_type_contact tc ON (ec.fk_c_type_contact=tc.rowid and tc.element='order_supplier' and tc.source='external' and tc.active=1)";
  249. $where = ' WHERE c.entity IN ('.getEntity($documentstatic->element).')';
  250. $dateprint = 'c.date_valid';
  251. $doc_number = 'c.ref';
  252. $thirdTypeSelect = 'supplier';
  253. } elseif ($type_element == 'contract') { // Order
  254. require_once DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php';
  255. $documentstatic = new Contrat($db);
  256. $documentstaticline = new ContratLigne($db);
  257. $sql_select = 'SELECT c.rowid as doc_id, c.ref as doc_number, \'1\' as doc_type, c.date_contrat as dateprint, d.statut as status, tc.libelle as type_contact_label, ';
  258. $tables_from = MAIN_DB_PREFIX.'contratdet d';
  259. $tables_from .= ' LEFT JOIN '.MAIN_DB_PREFIX.'contrat as c ON d.fk_contrat=c.rowid';
  260. $tables_from .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product p ON d.fk_product=p.rowid';
  261. $tables_from .= ' INNER JOIN '.MAIN_DB_PREFIX.'element_contact ec ON ec.element_id=c.rowid AND ec.fk_socpeople = '.((int) $object->id);
  262. $tables_from .= ' INNER JOIN '.MAIN_DB_PREFIX."c_type_contact tc ON (ec.fk_c_type_contact=tc.rowid and tc.element='contrat' and tc.source='external' and tc.active=1)";
  263. $where = ' WHERE c.entity IN ('.getEntity('contrat').')';
  264. $dateprint = 'c.date_valid';
  265. $doc_number = 'c.ref';
  266. $thirdTypeSelect = 'customer';
  267. }
  268. $parameters = array();
  269. $reshook = $hookmanager->executeHooks('printFieldListSelect', $parameters); // Note that $action and $object may have been modified by hook
  270. if (!empty($sql_select)) {
  271. $sql = $sql_select;
  272. $sql .= ' d.description as description';
  273. if ($type_element != 'fichinter' && $type_element != 'contract' && $type_element != 'supplier_proposal') {
  274. $sql .= ', d.label, d.fk_product as product_id, d.fk_product as fk_product, d.info_bits, d.date_start, d.date_end, d.qty, d.qty as prod_qty, d.total_ht as total_ht, ';
  275. }
  276. if ($type_element == 'supplier_proposal') {
  277. $sql .= ', d.label, d.fk_product as product_id, d.fk_product as fk_product, d.info_bits, d.qty, d.qty as prod_qty, d.total_ht as total_ht, ';
  278. }
  279. if ($type_element == 'contract') {
  280. $sql .= ', d.label, d.fk_product as product_id, d.fk_product as fk_product, d.info_bits, d.date_ouverture as date_start, d.date_cloture as date_end, d.qty, d.qty as prod_qty, d.total_ht as total_ht, ';
  281. }
  282. if ($type_element != 'fichinter') {
  283. $sql .= ' p.ref as ref, p.rowid as prod_id, p.rowid as fk_product, p.fk_product_type as prod_type, p.fk_product_type as fk_product_type, p.entity as pentity';
  284. }
  285. $sql .= " ";
  286. if ($type_element != 'fichinter') {
  287. $sql .= ", p.ref as prod_ref, p.label as product_label";
  288. }
  289. $sql .= " FROM "/*.MAIN_DB_PREFIX."societe as s, "*/.$tables_from;
  290. // if ($type_element != 'fichinter') $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON d.fk_product = p.rowid ';
  291. $sql .= $where;
  292. $sql .= dolSqlDateFilter($dateprint, 0, $month, $year);
  293. if ($sref) {
  294. $sql .= " AND ".$doc_number." LIKE '%".$db->escape($sref)."%'";
  295. }
  296. if ($sprod_fulldescr) {
  297. $sql .= " AND (d.description LIKE '%".$db->escape($sprod_fulldescr)."%'";
  298. if (GETPOST('type_element') != 'fichinter') {
  299. $sql .= " OR p.ref LIKE '%".$db->escape($sprod_fulldescr)."%'";
  300. }
  301. if (GETPOST('type_element') != 'fichinter') {
  302. $sql .= " OR p.label LIKE '%".$db->escape($sprod_fulldescr)."%'";
  303. }
  304. $sql .= ")";
  305. }
  306. $sql .= $db->order($sortfield, $sortorder);
  307. $resql = $db->query($sql);
  308. $totalnboflines = $db->num_rows($resql);
  309. $sql .= $db->plimit($limit + 1, $offset);
  310. }
  311. $disabled = 0;
  312. $showempty = 2;
  313. if (empty($elementTypeArray) && !$object->thirdparty->client && !$object->thirdparty->fournisseur) {
  314. $showempty = $langs->trans("ThirdpartyNotCustomerNotSupplierSoNoRef");
  315. $disabled = 1;
  316. }
  317. // Define type of elements
  318. $typeElementString = $form->selectarray("type_element", $elementTypeArray, GETPOST('type_element'), $showempty, 0, 0, '', 0, 0, $disabled, '', 'maxwidth150onsmartphone');
  319. $button = '<input type="submit" class="button small" name="button_third" value="'.dol_escape_htmltag($langs->trans("Search")).'" title="'.dol_escape_htmltag($langs->trans("Search")).'">';
  320. $param = '';
  321. $param .= "&sref=".urlencode($sref);
  322. $param .= "&month=".urlencode($month);
  323. $param .= "&year=".urlencode($year);
  324. $param .= "&sprod_fulldescr=".urlencode($sprod_fulldescr);
  325. $param .= "&socid=".urlencode($socid);
  326. $param .= "&type_element=".urlencode($type_element);
  327. $total_qty = 0;
  328. $num=0;
  329. if ($sql_select) {
  330. $resql = $db->query($sql);
  331. if (!$resql) {
  332. dol_print_error($db);
  333. }
  334. $num = $db->num_rows($resql);
  335. $param = "&socid=".urlencode($socid)."&type_element=".urlencode($type_element);
  336. if (!empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) {
  337. $param .= '&contextpage='.urlencode($contextpage);
  338. }
  339. if ($limit > 0 && $limit != $conf->liste_limit) {
  340. $param .= '&limit='.urlencode($limit);
  341. }
  342. if ($sprod_fulldescr) {
  343. $param .= "&sprod_fulldescr=".urlencode($sprod_fulldescr);
  344. }
  345. if ($sref) {
  346. $param .= "&sref=".urlencode($sref);
  347. }
  348. if ($month) {
  349. $param .= "&month=".urlencode($month);
  350. }
  351. if ($year) {
  352. $param .= "&year=".urlencode($year);
  353. }
  354. if ($optioncss != '') {
  355. $param .= '&optioncss='.urlencode($optioncss);
  356. }
  357. print_barre_liste($langs->trans('ProductsIntoElements').' '.$typeElementString.' '.$button, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num, $totalnboflines, '', 0, '', '', $limit);
  358. print '<div class="div-table-responsive-no-min">';
  359. print '<table class="liste centpercent">'."\n";
  360. // Filters
  361. print '<tr class="liste_titre">';
  362. print '<td class="liste_titre left">';
  363. print '<input class="flat" type="text" name="sref" size="8" value="'.$sref.'">';
  364. print '</td>';
  365. print '<td class="liste_titre nowrap center">'; // date
  366. print $formother->select_month($month ? $month : -1, 'month', 1, 0, 'valignmiddle');
  367. print $formother->selectyear($year ? $year : -1, 'year', 1, 20, 1);
  368. print '</td>';
  369. print '<td class="liste_titre center">';
  370. print '</td>';
  371. print '<td class="liste_titre left">';
  372. print '<input class="flat" type="text" name="sprod_fulldescr" size="15" value="'.dol_escape_htmltag($sprod_fulldescr).'">';
  373. print '</td>';
  374. print '<td class="liste_titre center">'; // TODO: Add filters !
  375. print '</td>';
  376. print '<td class="liste_titre center">';
  377. print '</td>';
  378. print '<td class="liste_titre center">';
  379. print '</td>';
  380. print '<td class="liste_titre maxwidthsearch">';
  381. $searchpicto = $form->showFilterAndCheckAddButtons(0);
  382. print $searchpicto;
  383. print '</td>';
  384. print '</tr>';
  385. // Titles with sort buttons
  386. print '<tr class="liste_titre">';
  387. print_liste_field_titre('Ref', $_SERVER['PHP_SELF'], 'doc_number', '', $param, '', $sortfield, $sortorder, 'left ');
  388. print_liste_field_titre('Date', $_SERVER['PHP_SELF'], 'dateprint', '', $param, 'width="150"', $sortfield, $sortorder, 'center ');
  389. print_liste_field_titre('Status', $_SERVER['PHP_SELF'], 'fk_statut', '', $param, '', $sortfield, $sortorder, 'center ');
  390. print_liste_field_titre('Product', $_SERVER['PHP_SELF'], '', '', $param, '', $sortfield, $sortorder, 'left ');
  391. print_liste_field_titre('ContactType', $_SERVER['PHP_SELF'], '', '', $param, '', $sortfield, $sortorder, 'left ');
  392. print_liste_field_titre('Quantity', $_SERVER['PHP_SELF'], 'prod_qty', '', $param, '', $sortfield, $sortorder, 'right ');
  393. print_liste_field_titre('TotalHT', $_SERVER['PHP_SELF'], 'total_ht', '', $param, '', $sortfield, $sortorder, 'right ');
  394. print_liste_field_titre('UnitPrice', $_SERVER['PHP_SELF'], '', '', $param, '', $sortfield, $sortorder, 'right ');
  395. print "</tr>\n";
  396. $i = 0;
  397. $total_qty = 0;
  398. $total_ht = 0;
  399. while (($objp = $db->fetch_object($resql)) && $i < min($num, $limit)) {
  400. $documentstatic->id = $objp->doc_id;
  401. $documentstatic->ref = $objp->doc_number;
  402. $documentstatic->type = $objp->doc_type;
  403. $documentstatic->fk_statut = $objp->status;
  404. $documentstatic->fk_status = $objp->status;
  405. $documentstatic->statut = $objp->status;
  406. $documentstatic->status = $objp->status;
  407. $documentstatic->paye = $objp->paid;
  408. $documentstatic->paid = $objp->paid;
  409. if (is_object($documentstaticline)) {
  410. $documentstaticline->statut = $objp->status;
  411. }
  412. print '<tr class="oddeven">';
  413. print '<td class="nobordernopadding nowrap" width="100">';
  414. print $documentstatic->getNomUrl(1);
  415. print '</td>';
  416. print '<td class="center" width="80">'.dol_print_date($db->jdate($objp->dateprint), 'day').'</td>';
  417. // Status
  418. print '<td class="center">';
  419. if ($type_element == 'contract') {
  420. print $documentstaticline->getLibStatut(2);
  421. } else {
  422. print $documentstatic->getLibStatut(2);
  423. }
  424. print '</td>';
  425. print '<td>';
  426. // Define text, description and type
  427. $text = ''; $description = ''; $type = 0;
  428. // Code to show product duplicated from commonobject->printObjectLine
  429. if ($objp->fk_product > 0) {
  430. $product_static = new Product($db);
  431. $product_static->type = $objp->fk_product_type;
  432. $product_static->id = $objp->fk_product;
  433. $product_static->ref = $objp->ref;
  434. $product_static->entity = $objp->pentity;
  435. $text = $product_static->getNomUrl(1);
  436. }
  437. // Product
  438. if ($objp->fk_product > 0) {
  439. // Define output language
  440. if (getDolGlobalInt('MAIN_MULTILANGS') && !empty($conf->global->PRODUIT_TEXTS_IN_THIRDPARTY_LANGUAGE)) {
  441. $prod = new Product($db);
  442. $prod->fetch($objp->fk_product);
  443. $outputlangs = $langs;
  444. $newlang = '';
  445. if (empty($newlang) && GETPOST('lang_id', 'aZ09')) {
  446. $newlang = GETPOST('lang_id', 'aZ09');
  447. }
  448. if (empty($newlang)) {
  449. $newlang = $object->default_lang;
  450. }
  451. if (!empty($newlang)) {
  452. $outputlangs = new Translate("", $conf);
  453. $outputlangs->setDefaultLang($newlang);
  454. }
  455. $label = (!empty($prod->multilangs[$outputlangs->defaultlang]["label"])) ? $prod->multilangs[$outputlangs->defaultlang]["label"] : $objp->product_label;
  456. } else {
  457. $label = $objp->product_label;
  458. }
  459. $text .= ' - '.(!empty($objp->label) ? $objp->label : $label);
  460. $description = (getDolGlobalInt('PRODUIT_DESC_IN_FORM_ACCORDING_TO_DEVICE') ? '' : dol_htmlentitiesbr($objp->description));
  461. }
  462. if (($objp->info_bits & 2) == 2) {
  463. print '<a href="'.DOL_URL_ROOT.'/comm/remx.php?id='.$object->id.'">';
  464. $txt = '';
  465. print img_object($langs->trans("ShowReduc"), 'reduc').' ';
  466. if ($objp->description == '(DEPOSIT)') {
  467. $txt = $langs->trans("Deposit");
  468. } elseif ($objp->description == '(EXCESS RECEIVED)') {
  469. $txt = $langs->trans("ExcessReceived");
  470. } elseif ($objp->description == '(EXCESS PAID)') {
  471. $txt = $langs->trans("ExcessPaid");
  472. }
  473. //else $txt=$langs->trans("Discount");
  474. print $txt;
  475. print '</a>';
  476. if ($objp->description) {
  477. if ($objp->description == '(CREDIT_NOTE)' && $objp->fk_remise_except > 0) {
  478. $discount = new DiscountAbsolute($db);
  479. $discount->fetch($objp->fk_remise_except);
  480. echo ($txt ? ' - ' : '').$langs->transnoentities("DiscountFromCreditNote", $discount->getNomUrl(0));
  481. }
  482. if ($objp->description == '(EXCESS RECEIVED)' && $objp->fk_remise_except > 0) {
  483. $discount = new DiscountAbsolute($db);
  484. $discount->fetch($objp->fk_remise_except);
  485. echo ($txt ? ' - ' : '').$langs->transnoentities("DiscountFromExcessReceived", $discount->getNomUrl(0));
  486. } elseif ($objp->description == '(EXCESS PAID)' && $objp->fk_remise_except > 0) {
  487. $discount = new DiscountAbsolute($db);
  488. $discount->fetch($objp->fk_remise_except);
  489. echo ($txt ? ' - ' : '').$langs->transnoentities("DiscountFromExcessPaid", $discount->getNomUrl(0));
  490. } elseif ($objp->description == '(DEPOSIT)' && $objp->fk_remise_except > 0) {
  491. $discount = new DiscountAbsolute($db);
  492. $discount->fetch($objp->fk_remise_except);
  493. echo ($txt ? ' - ' : '').$langs->transnoentities("DiscountFromDeposit", $discount->getNomUrl(0));
  494. // Add date of deposit
  495. if (!empty($conf->global->INVOICE_ADD_DEPOSIT_DATE)) {
  496. echo ' ('.dol_print_date($discount->datec).')';
  497. }
  498. } else {
  499. echo ($txt ? ' - ' : '').dol_htmlentitiesbr($objp->description);
  500. }
  501. }
  502. } else {
  503. if ($objp->fk_product > 0) {
  504. echo $form->textwithtooltip($text, $description, 3, '', '', $i, 0, '');
  505. // Show range
  506. echo get_date_range($objp->date_start, $objp->date_end);
  507. // Add description in form
  508. if (getDolGlobalInt('PRODUIT_DESC_IN_FORM_ACCORDING_TO_DEVICE')) {
  509. print (!empty($objp->description) && $objp->description != $objp->product_label) ? '<br>'.dol_htmlentitiesbr($objp->description) : '';
  510. }
  511. } else {
  512. if (!empty($objp->label) || !empty($objp->description)) {
  513. if ($type == 1) {
  514. $text = img_object($langs->trans('Service'), 'service');
  515. } else {
  516. $text = img_object($langs->trans('Product'), 'product');
  517. }
  518. if (!empty($objp->label)) {
  519. $text .= ' <strong>'.$objp->label.'</strong>';
  520. echo $form->textwithtooltip($text, dol_htmlentitiesbr($objp->description), 3, '', '', $i, 0, '');
  521. } else {
  522. echo $text.' '.dol_htmlentitiesbr($objp->description);
  523. }
  524. }
  525. // Show range
  526. echo get_date_range($objp->date_start, $objp->date_end);
  527. }
  528. }
  529. /*
  530. $prodreftxt='';
  531. if ($objp->prod_id > 0)
  532. {
  533. $productstatic->id = $objp->prod_id;
  534. $productstatic->ref = $objp->prod_ref;
  535. $productstatic->status = $objp->prod_type;
  536. $prodreftxt = $productstatic->getNomUrl(0);
  537. if(!empty($objp->product_label)) $prodreftxt .= ' - '.$objp->product_label;
  538. }
  539. // Show range
  540. $prodreftxt .= get_date_range($objp->date_start, $objp->date_end);
  541. // Add description in form
  542. if (getDolGlobalInt('PRODUIT_DESC_IN_FORM_ACCORDING_TO_DEVICE'))
  543. {
  544. $prodreftxt .= (!empty($objp->description) && $objp->description!=$objp->product_label)?'<br>'.dol_htmlentitiesbr($objp->description):'';
  545. }
  546. */
  547. print '</td>';
  548. print '<td>'.$objp->type_contact_label.'</td>'; // Type of contact label
  549. print '<td class="right">'.$objp->prod_qty.'</td>';
  550. $total_qty += $objp->prod_qty;
  551. print '<td class="right">'.price($objp->total_ht).'</td>';
  552. $total_ht += $objp->total_ht;
  553. print '<td class="right">'.price($objp->total_ht / (empty($objp->prod_qty) ? 1 : $objp->prod_qty)).'</td>';
  554. print "</tr>\n";
  555. $i++;
  556. }
  557. print '<tr class="liste_total">';
  558. print '<td>'.$langs->trans('Total').'</td>';
  559. print '<td colspan="3"></td>';
  560. print '<td></td>';
  561. print '<td class="right">'.$total_qty.'</td>';
  562. print '<td class="right">'.price($total_ht).'</td>';
  563. print '<td class="right">'.price($total_ht / (empty($total_qty) ? 1 : $total_qty)).'</td>';
  564. print "</table>";
  565. print '</div>';
  566. if ($num > $limit) {
  567. print_barre_liste('', $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num);
  568. }
  569. $db->free($resql);
  570. } elseif (empty($type_element) || $type_element == -1) {
  571. print_barre_liste($langs->trans('ProductsIntoElements').' '.$typeElementString.' '.$button, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num, '', '');
  572. print '<table class="liste centpercent">'."\n";
  573. // Titles with sort buttons
  574. print '<tr class="liste_titre">';
  575. print_liste_field_titre('Ref', $_SERVER['PHP_SELF'], 'doc_number', '', $param, '', $sortfield, $sortorder, 'left ');
  576. print_liste_field_titre('Date', $_SERVER['PHP_SELF'], 'dateprint', '', $param, 'width="150"', $sortfield, $sortorder, 'center ');
  577. print_liste_field_titre('Status', $_SERVER['PHP_SELF'], 'fk_status', '', $param, '', $sortfield, $sortorder, 'center ');
  578. print_liste_field_titre('Product', $_SERVER['PHP_SELF'], '', '', $param, '', $sortfield, $sortorder, 'left ');
  579. print_liste_field_titre('Quantity', $_SERVER['PHP_SELF'], 'prod_qty', '', $param, '', $sortfield, $sortorder, 'right ');
  580. print "</tr>\n";
  581. print '<tr class="oddeven"><td colspan="5"><span class="opacitymedium">'.$langs->trans("SelectElementAndClick", $langs->transnoentitiesnoconv("Search")).'</span></td></tr>';
  582. print "</table>";
  583. } else {
  584. print_barre_liste($langs->trans('ProductsIntoElements').' '.$typeElementString.' '.$button, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num, '', '');
  585. print '<table class="liste centpercent">'."\n";
  586. print '<tr class="oddeven"><td colspan="5"><span class="opacitymedium">'.$langs->trans("FeatureNotYetAvailable").'</span></td></tr>';
  587. print "</table>";
  588. }
  589. print "</form>";
  590. // End of page
  591. llxFooter();
  592. $db->close();