products.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /* Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr>
  3. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
  4. * Copyright (C) 2007-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/product/ajax/products.php
  21. * \brief File to return Ajax response on product list request
  22. */
  23. if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL',1); // Disables token renewal
  24. if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1');
  25. if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1');
  26. if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1');
  27. if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1');
  28. if (! defined('NOCSRFCHECK')) define('NOCSRFCHECK','1');
  29. if (empty($_GET['keysearch']) && ! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1');
  30. require '../../main.inc.php';
  31. $htmlname=GETPOST('htmlname','alpha');
  32. $socid=GETPOST('socid','int');
  33. $type=GETPOST('type','int');
  34. $mode=GETPOST('mode','int');
  35. $status=((GETPOST('status','int') >= 0) ? GETPOST('status','int') : -1);
  36. $outjson=(GETPOST('outjson','int') ? GETPOST('outjson','int') : 0);
  37. $price_level=GETPOST('price_level','int');
  38. $action=GETPOST('action', 'alpha');
  39. $id=GETPOST('id', 'int');
  40. $price_by_qty_rowid=GETPOST('pbq', 'int');
  41. /*
  42. * View
  43. */
  44. //print '<!-- Ajax page called with url '.$_SERVER["PHP_SELF"].'?'.$_SERVER["QUERY_STRING"].' -->'."\n";
  45. dol_syslog(join(',',$_GET));
  46. //print_r($_GET);
  47. if (! empty($action) && $action == 'fetch' && ! empty($id))
  48. {
  49. require DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
  50. $outjson=array();
  51. $object = new Product($db);
  52. $ret=$object->fetch($id);
  53. if ($ret > 0)
  54. {
  55. $outref=$object->ref;
  56. $outlabel=$object->label;
  57. $outdesc=$object->description;
  58. $outtype=$object->type;
  59. $outqty=1;
  60. $outdiscount=0;
  61. $found=false;
  62. // Price by qty
  63. if (!empty($price_by_qty_rowid) && $price_by_qty_rowid >= 1) // If we need a particular price related to qty
  64. {
  65. $sql = "SELECT price, unitprice, quantity, remise_percent";
  66. $sql.= " FROM ".MAIN_DB_PREFIX."product_price_by_qty ";
  67. $sql.= " WHERE rowid=".$price_by_qty_rowid."";
  68. $result = $db->query($sql);
  69. if ($result)
  70. {
  71. $objp = $db->fetch_object($result);
  72. if ($objp)
  73. {
  74. $found=true;
  75. $outprice_ht=price($objp->unitprice);
  76. $outprice_ttc=price($objp->unitprice * (1 + ($object->tva_tx / 100)));
  77. $outpricebasetype=$object->price_base_type;
  78. $outtva_tx=$object->tva_tx;
  79. $outqty=$objp->quantity;
  80. $outdiscount=$objp->remise_percent;
  81. }
  82. }
  83. }
  84. // Multiprice
  85. if (! $found && isset($price_level) && $price_level >= 1) // If we need a particular price level (from 1 to 6)
  86. {
  87. $sql = "SELECT price, price_ttc, price_base_type, tva_tx";
  88. $sql.= " FROM ".MAIN_DB_PREFIX."product_price ";
  89. $sql.= " WHERE fk_product='".$id."'";
  90. $sql.= " AND price_level=".$price_level;
  91. $sql.= " ORDER BY date_price";
  92. $sql.= " DESC LIMIT 1";
  93. $result = $db->query($sql);
  94. if ($result)
  95. {
  96. $objp = $db->fetch_object($result);
  97. if ($objp)
  98. {
  99. $found=true;
  100. $outprice_ht=price($objp->price);
  101. $outprice_ttc=price($objp->price_ttc);
  102. $outpricebasetype=$objp->price_base_type;
  103. $outtva_tx=$objp->tva_tx;
  104. }
  105. }
  106. }
  107. if (! $found)
  108. {
  109. $outprice_ht=price($object->price);
  110. $outprice_ttc=price($object->price_ttc);
  111. $outpricebasetype=$object->price_base_type;
  112. $outtva_tx=$object->tva_tx;
  113. }
  114. $outjson = array('ref'=>$outref, 'label'=>$outlabel, 'desc'=>$outdesc, 'type'=>$outtype, 'price_ht'=>$outprice_ht, 'price_ttc'=>$outprice_ttc, 'pricebasetype'=>$outpricebasetype, 'tva_tx'=>$outtva_tx, 'qty'=>$outqty, 'discount'=>$outdiscount);
  115. }
  116. echo json_encode($outjson);
  117. }
  118. else
  119. {
  120. require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
  121. $langs->load("products");
  122. $langs->load("main");
  123. top_httphead();
  124. if (empty($htmlname)) return;
  125. $match = preg_grep('/('.$htmlname.'[0-9]+)/',array_keys($_GET));
  126. sort($match);
  127. $idprod = (! empty($match[0]) ? $match[0] : '');
  128. if (! GETPOST($htmlname) && ! GETPOST($idprod)) return;
  129. // When used from jQuery, the search term is added as GET param "term".
  130. $searchkey=(GETPOST($idprod)?GETPOST($idprod):(GETPOST($htmlname)?GETPOST($htmlname):''));
  131. $form = new Form($db);
  132. if (empty($mode) || $mode == 1)
  133. {
  134. $arrayresult=$form->select_produits_list("",$htmlname,$type,"",$price_level,$searchkey,$status,2,$outjson);
  135. }
  136. elseif ($mode == 2)
  137. {
  138. $arrayresult=$form->select_produits_fournisseurs_list($socid,"",$htmlname,$type,"",$searchkey,$status,$outjson);
  139. }
  140. $db->close();
  141. if ($outjson) print json_encode($arrayresult);
  142. }
  143. ?>