consumption.php 30 KB

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