consumption.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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. if ($objsoc->id > 0) {
  114. $morehtmlref .= $objsoc->getNomUrl(1, 'contact');
  115. } else {
  116. $morehtmlref .= '<span class="opacitymedium">'.$langs->trans("ContactNotLinkedToCompany").'</span>';
  117. }
  118. }
  119. $morehtmlref .= '</div>';
  120. dol_banner_tab($object, 'id', $linkback, ($user->socid ? 0 : 1), 'rowid', 'nom', $morehtmlref);
  121. print '<div class="fichecenter">';
  122. print '<div class="underbanner clearboth"></div>';
  123. print '<table class="border centpercent tableforfield">';
  124. // Civility
  125. print '<tr><td class="titlefield">'.$langs->trans("UserTitle").'</td><td>';
  126. print $object->getCivilityLabel();
  127. print '</td></tr>';
  128. if ($object->thirdparty->client) {
  129. $thirdTypeArray['customer'] = $langs->trans("customer");
  130. if (isModEnabled("propal") && $user->hasRight('propal', 'lire')) {
  131. $elementTypeArray['propal'] = $langs->transnoentitiesnoconv('Proposals');
  132. }
  133. if (isModEnabled('commande') && $user->hasRight('commande', 'lire')) {
  134. $elementTypeArray['order'] = $langs->transnoentitiesnoconv('Orders');
  135. }
  136. if (isModEnabled('facture') && $user->hasRight('facture', 'lire')) {
  137. $elementTypeArray['invoice'] = $langs->transnoentitiesnoconv('Invoices');
  138. }
  139. if (isModEnabled('contrat') && $user->hasRight('contrat', 'lire')) {
  140. $elementTypeArray['contract'] = $langs->transnoentitiesnoconv('Contracts');
  141. }
  142. }
  143. if (isModEnabled('ficheinter') && $user->hasRight('ficheinter', 'lire')) {
  144. $elementTypeArray['fichinter'] = $langs->transnoentitiesnoconv('Interventions');
  145. }
  146. if ($object->thirdparty->fournisseur) {
  147. $thirdTypeArray['supplier'] = $langs->trans("supplier");
  148. if ((isModEnabled("fournisseur") && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire) || (isModEnabled("supplier_invoice") && $user->rights->supplier_invoice->lire)) {
  149. $elementTypeArray['supplier_invoice'] = $langs->transnoentitiesnoconv('SuppliersInvoices');
  150. }
  151. if ((isModEnabled("fournisseur") && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->commande->lire) || (isModEnabled("supplier_order") && $user->rights->supplier_order->lire)) {
  152. $elementTypeArray['supplier_order'] = $langs->transnoentitiesnoconv('SuppliersOrders');
  153. }
  154. // There no contact type for supplier proposals
  155. // 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');
  156. }
  157. print '</table>';
  158. print '</div>';
  159. print dol_get_fiche_end();
  160. print '<br>';
  161. print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'?id='.$id.'">';
  162. print '<input type="hidden" name="token" value="'.newToken().'">';
  163. $sql_select = '';
  164. if ($type_element == 'fichinter') { // Customer : show products from invoices
  165. require_once DOL_DOCUMENT_ROOT.'/fichinter/class/fichinter.class.php';
  166. $documentstatic = new Fichinter($db);
  167. $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, ';
  168. $tables_from = MAIN_DB_PREFIX.'fichinterdet d';
  169. $tables_from .= ' LEFT JOIN '.MAIN_DB_PREFIX.'fichinter as f ON d.fk_fichinter=f.rowid';
  170. $tables_from .= ' INNER JOIN '.MAIN_DB_PREFIX.'element_contact ec ON ec.element_id=f.rowid AND ec.fk_socpeople = '.((int) $object->id);
  171. $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)";
  172. $where = ' WHERE f.entity IN ('.getEntity('ficheinter').')';
  173. $dateprint = 'f.datec';
  174. $doc_number = 'f.ref';
  175. } elseif ($type_element == 'invoice') { // Customer : show products from invoices
  176. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  177. $documentstatic = new Facture($db);
  178. $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, ';
  179. $tables_from = MAIN_DB_PREFIX.'facturedet d';
  180. $tables_from .= ' LEFT JOIN '.MAIN_DB_PREFIX.'facture as f ON d.fk_facture=f.rowid';
  181. $tables_from .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product p ON d.fk_product=p.rowid';
  182. $tables_from .= ' INNER JOIN '.MAIN_DB_PREFIX.'element_contact ec ON ec.element_id=f.rowid AND ec.fk_socpeople = '.((int) $object->id);
  183. $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)";
  184. $where = " WHERE f.entity IN (".getEntity('invoice').")";
  185. $dateprint = 'f.datef';
  186. $doc_number = 'f.ref';
  187. $thirdTypeSelect = 'customer';
  188. } elseif ($type_element == 'propal') {
  189. require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
  190. $documentstatic = new Propal($db);
  191. $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, ';
  192. $tables_from = MAIN_DB_PREFIX.'propaldet d';
  193. $tables_from .= ' LEFT JOIN '.MAIN_DB_PREFIX.'propal as c ON d.fk_propal=c.rowid';
  194. $tables_from .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product p ON d.fk_product=p.rowid';
  195. $tables_from .= ' INNER JOIN '.MAIN_DB_PREFIX.'element_contact ec ON ec.element_id=c.rowid AND ec.fk_socpeople = '.((int) $object->id);
  196. $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)";
  197. $where = ' WHERE c.entity IN ('.getEntity('propal').')';
  198. $dateprint = 'c.datep';
  199. $doc_number = 'c.ref';
  200. $thirdTypeSelect = 'customer';
  201. } elseif ($type_element == 'order') {
  202. require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
  203. $documentstatic = new Commande($db);
  204. $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, ';
  205. $tables_from = MAIN_DB_PREFIX.'commandedet d';
  206. $tables_from .= ' LEFT JOIN '.MAIN_DB_PREFIX.'commande as c ON d.fk_commande=c.rowid';
  207. $tables_from .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product p ON d.fk_product=p.rowid';
  208. $tables_from .= ' INNER JOIN '.MAIN_DB_PREFIX.'element_contact ec ON ec.element_id=c.rowid AND ec.fk_socpeople = '.((int) $object->id);
  209. $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)";
  210. $where = ' WHERE c.entity IN ('.getEntity('order').')';
  211. $dateprint = 'c.date_commande';
  212. $doc_number = 'c.ref';
  213. $thirdTypeSelect = 'customer';
  214. } elseif ($type_element == 'supplier_invoice') { // Supplier : Show products from invoices.
  215. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
  216. $documentstatic = new FactureFournisseur($db);
  217. $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, ';
  218. $tables_from = MAIN_DB_PREFIX.'facture_fourn_det d';
  219. $tables_from .= ' LEFT JOIN '.MAIN_DB_PREFIX.'facture_fourn as f ON d.fk_facture_fourn=f.rowid';
  220. $tables_from .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product p ON d.fk_product=p.rowid';
  221. $tables_from .= ' INNER JOIN '.MAIN_DB_PREFIX.'element_contact ec ON ec.element_id=f.rowid AND ec.fk_socpeople = '.((int) $object->id);
  222. $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)";
  223. $where = ' WHERE f.entity IN ('.getEntity($documentstatic->element).')';
  224. $dateprint = 'f.datef';
  225. $doc_number = 'f.ref';
  226. $thirdTypeSelect = 'supplier';
  227. //} elseif ($type_element == 'supplier_proposal') {
  228. // require_once DOL_DOCUMENT_ROOT.'/supplier_proposal/class/supplier_proposal.class.php';
  229. // $documentstatic=new SupplierProposal($db);
  230. // $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, ';
  231. // $tables_from = MAIN_DB_PREFIX."supplier_proposal as c,".MAIN_DB_PREFIX."supplier_proposaldet as d";
  232. // $where = " WHERE c.fk_soc = s.rowid AND s.rowid = ".((int) $socid);
  233. // $where.= " AND d.fk_supplier_proposal = c.rowid";
  234. // $where.= " AND c.entity = ".$conf->entity;
  235. // $dateprint = 'c.date_valid';
  236. // $doc_number='c.ref';
  237. // $thirdTypeSelect='supplier';
  238. //}
  239. } elseif ($type_element == 'supplier_order') { // Supplier : Show products from orders.
  240. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
  241. $documentstatic = new CommandeFournisseur($db);
  242. $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, ';
  243. $tables_from = MAIN_DB_PREFIX.'commande_fournisseurdet d';
  244. $tables_from .= ' LEFT JOIN '.MAIN_DB_PREFIX.'commande_fournisseur as c ON d.fk_commande=c.rowid';
  245. $tables_from .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product p ON d.fk_product=p.rowid';
  246. $tables_from .= ' INNER JOIN '.MAIN_DB_PREFIX.'element_contact ec ON ec.element_id=c.rowid AND ec.fk_socpeople = '.((int) $object->id);
  247. $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)";
  248. $where = ' WHERE c.entity IN ('.getEntity($documentstatic->element).')';
  249. $dateprint = 'c.date_valid';
  250. $doc_number = 'c.ref';
  251. $thirdTypeSelect = 'supplier';
  252. } elseif ($type_element == 'contract') { // Order
  253. require_once DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php';
  254. $documentstatic = new Contrat($db);
  255. $documentstaticline = new ContratLigne($db);
  256. $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, ';
  257. $tables_from = MAIN_DB_PREFIX.'contratdet d';
  258. $tables_from .= ' LEFT JOIN '.MAIN_DB_PREFIX.'contrat as c ON d.fk_contrat=c.rowid';
  259. $tables_from .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product p ON d.fk_product=p.rowid';
  260. $tables_from .= ' INNER JOIN '.MAIN_DB_PREFIX.'element_contact ec ON ec.element_id=c.rowid AND ec.fk_socpeople = '.((int) $object->id);
  261. $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)";
  262. $where = ' WHERE c.entity IN ('.getEntity('contrat').')';
  263. $dateprint = 'c.date_valid';
  264. $doc_number = 'c.ref';
  265. $thirdTypeSelect = 'customer';
  266. }
  267. $parameters = array();
  268. $reshook = $hookmanager->executeHooks('printFieldListSelect', $parameters); // Note that $action and $object may have been modified by hook
  269. if (!empty($sql_select)) {
  270. $sql = $sql_select;
  271. $sql .= ' d.description as description';
  272. if ($type_element != 'fichinter' && $type_element != 'contract' && $type_element != 'supplier_proposal') {
  273. $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, ';
  274. }
  275. if ($type_element == 'supplier_proposal') {
  276. $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, ';
  277. }
  278. if ($type_element == 'contract') {
  279. $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, ';
  280. }
  281. if ($type_element != 'fichinter') {
  282. $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';
  283. }
  284. $sql .= " ";
  285. if ($type_element != 'fichinter') {
  286. $sql .= ", p.ref as prod_ref, p.label as product_label";
  287. }
  288. $sql .= " FROM "/*.MAIN_DB_PREFIX."societe as s, "*/.$tables_from;
  289. // if ($type_element != 'fichinter') $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON d.fk_product = p.rowid ';
  290. $sql .= $where;
  291. $sql .= dolSqlDateFilter($dateprint, 0, $month, $year);
  292. if ($sref) {
  293. $sql .= " AND ".$doc_number." LIKE '%".$db->escape($sref)."%'";
  294. }
  295. if ($sprod_fulldescr) {
  296. $sql .= " AND (d.description LIKE '%".$db->escape($sprod_fulldescr)."%'";
  297. if (GETPOST('type_element') != 'fichinter') {
  298. $sql .= " OR p.ref LIKE '%".$db->escape($sprod_fulldescr)."%'";
  299. }
  300. if (GETPOST('type_element') != 'fichinter') {
  301. $sql .= " OR p.label LIKE '%".$db->escape($sprod_fulldescr)."%'";
  302. }
  303. $sql .= ")";
  304. }
  305. $sql .= $db->order($sortfield, $sortorder);
  306. $resql = $db->query($sql);
  307. $totalnboflines = $db->num_rows($resql);
  308. $sql .= $db->plimit($limit + 1, $offset);
  309. }
  310. $disabled = 0;
  311. $showempty = 2;
  312. if (empty($elementTypeArray) && !$object->thirdparty->client && !$object->thirdparty->fournisseur) {
  313. $showempty = $langs->trans("ThirdpartyNotCustomerNotSupplierSoNoRef");
  314. $disabled = 1;
  315. }
  316. // Define type of elements
  317. $typeElementString = $form->selectarray("type_element", $elementTypeArray, GETPOST('type_element'), $showempty, 0, 0, '', 0, 0, $disabled, '', 'maxwidth150onsmartphone');
  318. $button = '<input type="submit" class="button small" name="button_third" value="'.dol_escape_htmltag($langs->trans("Search")).'" title="'.dol_escape_htmltag($langs->trans("Search")).'">';
  319. $param = '';
  320. $param .= "&sref=".urlencode($sref);
  321. $param .= "&month=".urlencode($month);
  322. $param .= "&year=".urlencode($year);
  323. $param .= "&sprod_fulldescr=".urlencode($sprod_fulldescr);
  324. $param .= "&socid=".urlencode($socid);
  325. $param .= "&type_element=".urlencode($type_element);
  326. $total_qty = 0;
  327. $num=0;
  328. if ($sql_select) {
  329. $resql = $db->query($sql);
  330. if (!$resql) {
  331. dol_print_error($db);
  332. }
  333. $num = $db->num_rows($resql);
  334. $param = "&socid=".urlencode($socid)."&type_element=".urlencode($type_element)."&id=".urlencode($id);
  335. if (!empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) {
  336. $param .= '&contextpage='.urlencode($contextpage);
  337. }
  338. if ($limit > 0 && $limit != $conf->liste_limit) {
  339. $param .= '&limit='.((int) $limit);
  340. }
  341. if ($sprod_fulldescr) {
  342. $param .= "&sprod_fulldescr=".urlencode($sprod_fulldescr);
  343. }
  344. if ($sref) {
  345. $param .= "&sref=".urlencode($sref);
  346. }
  347. if ($month) {
  348. $param .= "&month=".urlencode($month);
  349. }
  350. if ($year) {
  351. $param .= "&year=".urlencode($year);
  352. }
  353. if ($optioncss != '') {
  354. $param .= '&optioncss='.urlencode($optioncss);
  355. }
  356. print_barre_liste($langs->trans('ProductsIntoElements').' '.$typeElementString.' '.$button, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num, $totalnboflines, '', 0, '', '', $limit);
  357. print '<div class="div-table-responsive-no-min">';
  358. print '<table class="liste centpercent">'."\n";
  359. // Filters
  360. print '<tr class="liste_titre">';
  361. print '<td class="liste_titre left">';
  362. print '<input class="flat" type="text" name="sref" size="8" value="'.$sref.'">';
  363. print '</td>';
  364. print '<td class="liste_titre nowrap center">'; // date
  365. print $formother->select_month($month ? $month : -1, 'month', 1, 0, 'valignmiddle');
  366. print $formother->selectyear($year ? $year : -1, 'year', 1, 20, 1);
  367. print '</td>';
  368. print '<td class="liste_titre center">';
  369. print '</td>';
  370. print '<td class="liste_titre left">';
  371. print '<input class="flat" type="text" name="sprod_fulldescr" size="15" value="'.dol_escape_htmltag($sprod_fulldescr).'">';
  372. print '</td>';
  373. print '<td class="liste_titre center">'; // TODO: Add filters !
  374. print '</td>';
  375. print '<td class="liste_titre center">';
  376. print '</td>';
  377. print '<td class="liste_titre center">';
  378. print '</td>';
  379. print '<td class="liste_titre maxwidthsearch">';
  380. $searchpicto = $form->showFilterAndCheckAddButtons(0);
  381. print $searchpicto;
  382. print '</td>';
  383. print '</tr>';
  384. // Titles with sort buttons
  385. print '<tr class="liste_titre">';
  386. print_liste_field_titre('Ref', $_SERVER['PHP_SELF'], 'doc_number', '', $param, '', $sortfield, $sortorder, 'left ');
  387. print_liste_field_titre('Date', $_SERVER['PHP_SELF'], 'dateprint', '', $param, 'width="150"', $sortfield, $sortorder, 'center ');
  388. print_liste_field_titre('Status', $_SERVER['PHP_SELF'], 'fk_statut', '', $param, '', $sortfield, $sortorder, 'center ');
  389. print_liste_field_titre('Product', $_SERVER['PHP_SELF'], '', '', $param, '', $sortfield, $sortorder, 'left ');
  390. print_liste_field_titre('ContactType', $_SERVER['PHP_SELF'], '', '', $param, '', $sortfield, $sortorder, 'left ');
  391. print_liste_field_titre('Quantity', $_SERVER['PHP_SELF'], 'prod_qty', '', $param, '', $sortfield, $sortorder, 'right ');
  392. print_liste_field_titre('TotalHT', $_SERVER['PHP_SELF'], 'total_ht', '', $param, '', $sortfield, $sortorder, 'right ');
  393. print_liste_field_titre('UnitPrice', $_SERVER['PHP_SELF'], '', '', $param, '', $sortfield, $sortorder, 'right ');
  394. print "</tr>\n";
  395. $i = 0;
  396. $total_qty = 0;
  397. $total_ht = 0;
  398. while (($objp = $db->fetch_object($resql)) && $i < min($num, $limit)) {
  399. $documentstatic->id = $objp->doc_id;
  400. $documentstatic->ref = $objp->doc_number;
  401. $documentstatic->type = $objp->doc_type;
  402. $documentstatic->fk_statut = $objp->status;
  403. $documentstatic->fk_status = $objp->status;
  404. $documentstatic->statut = $objp->status;
  405. $documentstatic->status = $objp->status;
  406. $documentstatic->paye = $objp->paid;
  407. $documentstatic->paid = $objp->paid;
  408. if (is_object($documentstaticline)) {
  409. $documentstaticline->statut = $objp->status;
  410. }
  411. print '<tr class="oddeven">';
  412. print '<td class="nobordernopadding nowrap" width="100">';
  413. print $documentstatic->getNomUrl(1);
  414. print '</td>';
  415. print '<td class="center" width="80">'.dol_print_date($db->jdate($objp->dateprint), 'day').'</td>';
  416. // Status
  417. print '<td class="center">';
  418. if ($type_element == 'contract') {
  419. print $documentstaticline->getLibStatut(2);
  420. } else {
  421. print $documentstatic->getLibStatut(2);
  422. }
  423. print '</td>';
  424. print '<td>';
  425. // Define text, description and type
  426. $text = ''; $description = ''; $type = 0;
  427. // Code to show product duplicated from commonobject->printObjectLine
  428. if ($objp->fk_product > 0) {
  429. $product_static = new Product($db);
  430. $product_static->type = $objp->fk_product_type;
  431. $product_static->id = $objp->fk_product;
  432. $product_static->ref = $objp->ref;
  433. $product_static->entity = $objp->pentity;
  434. $text = $product_static->getNomUrl(1);
  435. }
  436. // Product
  437. if ($objp->fk_product > 0) {
  438. // Define output language
  439. if (getDolGlobalInt('MAIN_MULTILANGS') && !empty($conf->global->PRODUIT_TEXTS_IN_THIRDPARTY_LANGUAGE)) {
  440. $prod = new Product($db);
  441. $prod->fetch($objp->fk_product);
  442. $outputlangs = $langs;
  443. $newlang = '';
  444. if (empty($newlang) && GETPOST('lang_id', 'aZ09')) {
  445. $newlang = GETPOST('lang_id', 'aZ09');
  446. }
  447. if (empty($newlang)) {
  448. $newlang = $object->default_lang;
  449. }
  450. if (!empty($newlang)) {
  451. $outputlangs = new Translate("", $conf);
  452. $outputlangs->setDefaultLang($newlang);
  453. }
  454. $label = (!empty($prod->multilangs[$outputlangs->defaultlang]["label"])) ? $prod->multilangs[$outputlangs->defaultlang]["label"] : $objp->product_label;
  455. } else {
  456. $label = $objp->product_label;
  457. }
  458. $text .= ' - '.(!empty($objp->label) ? $objp->label : $label);
  459. $description = (getDolGlobalInt('PRODUIT_DESC_IN_FORM_ACCORDING_TO_DEVICE') ? '' : dol_htmlentitiesbr($objp->description));
  460. }
  461. if (($objp->info_bits & 2) == 2) {
  462. print '<a href="'.DOL_URL_ROOT.'/comm/remx.php?id='.$object->id.'">';
  463. $txt = '';
  464. print img_object($langs->trans("ShowReduc"), 'reduc').' ';
  465. if ($objp->description == '(DEPOSIT)') {
  466. $txt = $langs->trans("Deposit");
  467. } elseif ($objp->description == '(EXCESS RECEIVED)') {
  468. $txt = $langs->trans("ExcessReceived");
  469. } elseif ($objp->description == '(EXCESS PAID)') {
  470. $txt = $langs->trans("ExcessPaid");
  471. }
  472. //else $txt=$langs->trans("Discount");
  473. print $txt;
  474. print '</a>';
  475. if ($objp->description) {
  476. if ($objp->description == '(CREDIT_NOTE)' && $objp->fk_remise_except > 0) {
  477. $discount = new DiscountAbsolute($db);
  478. $discount->fetch($objp->fk_remise_except);
  479. echo ($txt ? ' - ' : '').$langs->transnoentities("DiscountFromCreditNote", $discount->getNomUrl(0));
  480. }
  481. if ($objp->description == '(EXCESS RECEIVED)' && $objp->fk_remise_except > 0) {
  482. $discount = new DiscountAbsolute($db);
  483. $discount->fetch($objp->fk_remise_except);
  484. echo ($txt ? ' - ' : '').$langs->transnoentities("DiscountFromExcessReceived", $discount->getNomUrl(0));
  485. } elseif ($objp->description == '(EXCESS PAID)' && $objp->fk_remise_except > 0) {
  486. $discount = new DiscountAbsolute($db);
  487. $discount->fetch($objp->fk_remise_except);
  488. echo ($txt ? ' - ' : '').$langs->transnoentities("DiscountFromExcessPaid", $discount->getNomUrl(0));
  489. } elseif ($objp->description == '(DEPOSIT)' && $objp->fk_remise_except > 0) {
  490. $discount = new DiscountAbsolute($db);
  491. $discount->fetch($objp->fk_remise_except);
  492. echo ($txt ? ' - ' : '').$langs->transnoentities("DiscountFromDeposit", $discount->getNomUrl(0));
  493. // Add date of deposit
  494. if (!empty($conf->global->INVOICE_ADD_DEPOSIT_DATE)) {
  495. echo ' ('.dol_print_date($discount->datec).')';
  496. }
  497. } else {
  498. echo ($txt ? ' - ' : '').dol_htmlentitiesbr($objp->description);
  499. }
  500. }
  501. } else {
  502. if ($objp->fk_product > 0) {
  503. echo $form->textwithtooltip($text, $description, 3, '', '', $i, 0, '');
  504. // Show range
  505. echo get_date_range($objp->date_start, $objp->date_end);
  506. // Add description in form
  507. if (getDolGlobalInt('PRODUIT_DESC_IN_FORM_ACCORDING_TO_DEVICE')) {
  508. print (!empty($objp->description) && $objp->description != $objp->product_label) ? '<br>'.dol_htmlentitiesbr($objp->description) : '';
  509. }
  510. } else {
  511. if (!empty($objp->label) || !empty($objp->description)) {
  512. if ($type == 1) {
  513. $text = img_object($langs->trans('Service'), 'service');
  514. } else {
  515. $text = img_object($langs->trans('Product'), 'product');
  516. }
  517. if (!empty($objp->label)) {
  518. $text .= ' <strong>'.$objp->label.'</strong>';
  519. echo $form->textwithtooltip($text, dol_htmlentitiesbr($objp->description), 3, '', '', $i, 0, '');
  520. } else {
  521. echo $text.' '.dol_htmlentitiesbr($objp->description);
  522. }
  523. }
  524. // Show range
  525. echo get_date_range($objp->date_start, $objp->date_end);
  526. }
  527. }
  528. /*
  529. $prodreftxt='';
  530. if ($objp->prod_id > 0)
  531. {
  532. $productstatic->id = $objp->prod_id;
  533. $productstatic->ref = $objp->prod_ref;
  534. $productstatic->status = $objp->prod_type;
  535. $prodreftxt = $productstatic->getNomUrl(0);
  536. if(!empty($objp->product_label)) $prodreftxt .= ' - '.$objp->product_label;
  537. }
  538. // Show range
  539. $prodreftxt .= get_date_range($objp->date_start, $objp->date_end);
  540. // Add description in form
  541. if (getDolGlobalInt('PRODUIT_DESC_IN_FORM_ACCORDING_TO_DEVICE'))
  542. {
  543. $prodreftxt .= (!empty($objp->description) && $objp->description!=$objp->product_label)?'<br>'.dol_htmlentitiesbr($objp->description):'';
  544. }
  545. */
  546. print '</td>';
  547. print '<td>'.$objp->type_contact_label.'</td>'; // Type of contact label
  548. print '<td class="right">'.$objp->prod_qty.'</td>';
  549. $total_qty += $objp->prod_qty;
  550. print '<td class="right">'.price($objp->total_ht).'</td>';
  551. $total_ht += $objp->total_ht;
  552. print '<td class="right">'.price($objp->total_ht / (empty($objp->prod_qty) ? 1 : $objp->prod_qty)).'</td>';
  553. print "</tr>\n";
  554. $i++;
  555. }
  556. print '<tr class="liste_total">';
  557. print '<td>'.$langs->trans('Total').'</td>';
  558. print '<td colspan="3"></td>';
  559. print '<td></td>';
  560. print '<td class="right">'.$total_qty.'</td>';
  561. print '<td class="right">'.price($total_ht).'</td>';
  562. print '<td class="right">'.price($total_ht / (empty($total_qty) ? 1 : $total_qty)).'</td>';
  563. print "</table>";
  564. print '</div>';
  565. if ($num > $limit) {
  566. print_barre_liste('', $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num);
  567. }
  568. $db->free($resql);
  569. } elseif (empty($type_element) || $type_element == -1) {
  570. print_barre_liste($langs->trans('ProductsIntoElements').' '.$typeElementString.' '.$button, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num, '', '');
  571. print '<table class="liste centpercent">'."\n";
  572. // Titles with sort buttons
  573. print '<tr class="liste_titre">';
  574. print_liste_field_titre('Ref', $_SERVER['PHP_SELF'], 'doc_number', '', $param, '', $sortfield, $sortorder, 'left ');
  575. print_liste_field_titre('Date', $_SERVER['PHP_SELF'], 'dateprint', '', $param, 'width="150"', $sortfield, $sortorder, 'center ');
  576. print_liste_field_titre('Status', $_SERVER['PHP_SELF'], 'fk_status', '', $param, '', $sortfield, $sortorder, 'center ');
  577. print_liste_field_titre('Product', $_SERVER['PHP_SELF'], '', '', $param, '', $sortfield, $sortorder, 'left ');
  578. print_liste_field_titre('Quantity', $_SERVER['PHP_SELF'], 'prod_qty', '', $param, '', $sortfield, $sortorder, 'right ');
  579. print "</tr>\n";
  580. print '<tr class="oddeven"><td colspan="5"><span class="opacitymedium">'.$langs->trans("SelectElementAndClick", $langs->transnoentitiesnoconv("Search")).'</span></td></tr>';
  581. print "</table>";
  582. } else {
  583. print_barre_liste($langs->trans('ProductsIntoElements').' '.$typeElementString.' '.$button, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num, '', '');
  584. print '<table class="liste centpercent">'."\n";
  585. print '<tr class="oddeven"><td colspan="5"><span class="opacitymedium">'.$langs->trans("FeatureNotYetAvailable").'</span></td></tr>';
  586. print "</table>";
  587. }
  588. print "</form>";
  589. // End of page
  590. llxFooter();
  591. $db->close();