/**
  * Return all batch detail records for given product and warehouse
  *
  *  @param	DoliDB		$db    				database object
  *  @param	int			$fk_product_stock	id product_stock for objet
  *  @param	int			$with_qty    		doesn't return line with 0 quantity
  *  @return int         					<0 if KO, >0 if OK
  */
 public static function findAll($db, $fk_product_stock, $with_qty = 0)
 {
     global $langs;
     $ret = array();
     $sql = "SELECT";
     $sql .= " t.rowid,";
     $sql .= " t.tms,";
     $sql .= " t.fk_product_stock,";
     $sql .= " t.sellby,";
     $sql .= " t.eatby,";
     $sql .= " t.batch,";
     $sql .= " t.qty,";
     $sql .= " t.import_key";
     $sql .= " FROM " . MAIN_DB_PREFIX . "product_batch as t";
     $sql .= " WHERE fk_product_stock=" . $fk_product_stock;
     if ($with_qty) {
         $sql .= " AND qty<>0";
     }
     dol_syslog("productbatch::findAll", LOG_DEBUG);
     $resql = $db->query($sql);
     if ($resql) {
         $num = $db->num_rows($resql);
         $i = 0;
         while ($i < $num) {
             $obj = $db->fetch_object($resql);
             $tmp = new productbatch($db);
             $tmp->id = $obj->rowid;
             $tmp->tms = $db->jdate($obj->tms);
             $tmp->fk_product_stock = $obj->fk_product_stock;
             $tmp->sellby = $db->jdate($obj->sellby);
             $tmp->eatby = $db->jdate($obj->eatby);
             $tmp->batch = $obj->batch;
             $tmp->qty = $obj->qty;
             $tmp->import_key = $obj->import_key;
             array_push($ret, $tmp);
             $i++;
         }
         $db->free($resql);
         return $ret;
     } else {
         $error = "Error " . $db->lasterror();
         return -1;
     }
 }
 /**
  *      Return a label for a key.
  *      Search into translation array, then into cache, then if still not found, search into database.
  *      Store key-label found into cache variable $this->cache_labels to save SQL requests to get labels.
  *
  * 		@param	DoliDB	$db				Database handler
  * 		@param	string	$key			Translation key to get label (key in language file)
  * 		@param	string	$tablename		Table name without prefix
  * 		@param	string	$fieldkey		Field for key
  * 		@param	string	$fieldlabel		Field for label
  *      @param	string	$keyforselect	Use another value than the translation key for the where into select
  *      @return string					Label in UTF8 (but without entities)
  *      @see dol_getIdFromCode
  */
 function getLabelFromKey($db, $key, $tablename, $fieldkey, $fieldlabel, $keyforselect = '')
 {
     // If key empty
     if ($key == '') {
         return '';
     }
     //print 'param: '.$key.'-'.$keydatabase.'-'.$this->trans($key); exit;
     // Check if a translation is available (this can call getTradFromKey)
     if ($this->transnoentitiesnoconv($key) != $key) {
         return $this->transnoentitiesnoconv($key);
         // Found in language array
     }
     // Check in cache
     if (isset($this->cache_labels[$tablename][$key])) {
         return $this->cache_labels[$tablename][$key];
         // Found in cache
     }
     $sql = "SELECT " . $fieldlabel . " as label";
     $sql .= " FROM " . MAIN_DB_PREFIX . $tablename;
     $sql .= " WHERE " . $fieldkey . " = '" . ($keyforselect ? $keyforselect : $key) . "'";
     dol_syslog(get_class($this) . '::getLabelFromKey', LOG_DEBUG);
     $resql = $db->query($sql);
     if ($resql) {
         $obj = $db->fetch_object($resql);
         if ($obj) {
             $this->cache_labels[$tablename][$key] = $obj->label;
         } else {
             $this->cache_labels[$tablename][$key] = $key;
         }
         $db->free($resql);
         return $this->cache_labels[$tablename][$key];
     } else {
         $this->error = $db->lasterror();
         return -1;
     }
 }
Exemple #3
0
 /**
  *  Return array of boxes qualified for area and user
  *
  *  @param	DoliDB		$db				Database handler
  *  @param	string		$mode			'available' or 'activated'
  *  @param	string		$zone			Name or area (-1 for all, 0 for Homepage, 1 for xxx, ...)
  *  @param  User|null   $user	  		Object user to filter
  *  @param	array		$excludelist	Array of box id (box.box_id = boxes_def.rowid) to exclude
  *  @return array       	        	Array of boxes
  */
 static function listBoxes($db, $mode, $zone, $user = null, $excludelist = array())
 {
     global $conf;
     $boxes = array();
     $confuserzone = 'MAIN_BOXES_' . $zone;
     if ($mode == 'activated') {
         $sql = "SELECT b.rowid, b.position, b.box_order, b.fk_user,";
         $sql .= " d.rowid as box_id, d.file, d.note, d.tms";
         $sql .= " FROM " . MAIN_DB_PREFIX . "boxes as b, " . MAIN_DB_PREFIX . "boxes_def as d";
         $sql .= " WHERE b.box_id = d.rowid";
         $sql .= " AND b.entity IN (0," . (!empty($conf->multicompany->enabled) && !empty($conf->multicompany->transverse_mode) ? "1," : "") . $conf->entity . ")";
         if ($zone >= 0) {
             $sql .= " AND b.position = " . $zone;
         }
         if (is_object($user)) {
             $sql .= " AND b.fk_user IN (0," . $user->id . ")";
         } else {
             $sql .= " AND b.fk_user = 0";
         }
         $sql .= " ORDER BY b.box_order";
     } else {
         $sql = "SELECT d.rowid as box_id, d.file, d.note, d.tms";
         $sql .= " FROM " . MAIN_DB_PREFIX . "boxes_def as d";
         $sql .= " WHERE d.entity IN (0," . (!empty($conf->multicompany->enabled) && !empty($conf->multicompany->transverse_mode) ? "1," : "") . $conf->entity . ")";
     }
     dol_syslog(get_class() . "::listBoxes get default box list for mode=" . $mode . " userid=" . (is_object($user) ? $user->id : '') . "", LOG_DEBUG);
     $resql = $db->query($sql);
     if ($resql) {
         $num = $db->num_rows($resql);
         $j = 0;
         while ($j < $num) {
             $obj = $db->fetch_object($resql);
             if (!in_array($obj->box_id, $excludelist)) {
                 if (preg_match('/^([^@]+)@([^@]+)$/i', $obj->file, $regs)) {
                     $boxname = preg_replace('/\\.php$/i', '', $regs[1]);
                     $module = $regs[2];
                     $relsourcefile = "/" . $module . "/core/boxes/" . $boxname . ".php";
                 } else {
                     $boxname = preg_replace('/\\.php$/i', '', $obj->file);
                     $relsourcefile = "/core/boxes/" . $boxname . ".php";
                 }
                 //print $obj->box_id.'-'.$boxname.'-'.$relsourcefile.'<br>';
                 // TODO PERF Do not make "dol_include_once" here, nor "new" later. This means, we must store a 'depends' field to store modules list, then
                 // the "enabled" condition for modules forbidden for external users and the depends condition can be done.
                 // Goal is to avoid making a "new" done for each boxes returned by select.
                 dol_include_once($relsourcefile);
                 if (class_exists($boxname)) {
                     $box = new $boxname($db, $obj->note);
                     // Constructor may set properties like box->enabled. obj->note is note into box def, not user params.
                     //$box=new stdClass();
                     // box properties
                     $box->rowid = empty($obj->rowid) ? '' : $obj->rowid;
                     $box->id = empty($obj->box_id) ? '' : $obj->box_id;
                     $box->position = $obj->position == '' ? '' : $obj->position;
                     // '0' must staty '0'
                     $box->box_order = empty($obj->box_order) ? '' : $obj->box_order;
                     $box->fk_user = empty($obj->fk_user) ? 0 : $obj->fk_user;
                     $box->sourcefile = $relsourcefile;
                     $box->class = $boxname;
                     if ($mode == 'activated' && !is_object($user)) {
                         if (is_numeric($box->box_order)) {
                             if ($box->box_order % 2 == 1) {
                                 $box->box_order = 'A' . $box->box_order;
                             } elseif ($box->box_order % 2 == 0) {
                                 $box->box_order = 'B' . $box->box_order;
                             }
                         }
                     }
                     // box_def properties
                     $box->box_id = empty($obj->box_id) ? '' : $obj->box_id;
                     $box->note = empty($obj->note) ? '' : $obj->note;
                     // Filter on box->enabled (used for example by box_comptes)
                     // Filter also on box->depends. Example: array("product|service") or array("contrat", "service")
                     $enabled = $box->enabled;
                     if (isset($box->depends) && count($box->depends) > 0) {
                         foreach ($box->depends as $moduleelem) {
                             $arrayelem = explode('|', $moduleelem);
                             $tmpenabled = 0;
                             // $tmpenabled is used for the '|' test (OR)
                             foreach ($arrayelem as $module) {
                                 $tmpmodule = preg_replace('/@[^@]+/', '', $module);
                                 if (!empty($conf->{$tmpmodule}->enabled)) {
                                     $tmpenabled = 1;
                                 }
                                 //print $boxname.'-'.$module.'-module enabled='.(empty($conf->$tmpmodule->enabled)?0:1).'<br>';
                             }
                             if (empty($tmpenabled)) {
                                 $enabled = 0;
                                 break;
                             }
                         }
                     }
                     //print '=>'.$boxname.'-enabled='.$enabled.'<br>';
                     //print 'xx module='.$module.' enabled='.$enabled;
                     if ($enabled) {
                         $boxes[] = $box;
                     } else {
                         unset($box);
                     }
                 } else {
                     dol_syslog("Failed to load box '" . $boxname . "' into file '" . $relsourcefile . "'", LOG_WARNING);
                 }
             }
             $j++;
         }
     } else {
         dol_syslog($db->lasterror(), LOG_ERR);
         return array('error' => $db->lasterror());
     }
     return $boxes;
 }
Exemple #4
0
/**
 *      Return an id or code from a code or id. Store Code-Id in a cache.
 *
 * 		@param	DoliDB	$db			Database handler
 * 		@param	string	$key		Code to get Id
 * 		@param	string	$tablename	Table name without prefix
 * 		@param	string	$fieldkey	Field for code
 * 		@param	string	$fieldid	Field for id
 *      @return int					Id of code
 */
function dol_getIdFromCode($db, $key, $tablename, $fieldkey = 'code', $fieldid = 'id')
{
    global $cache_codes;
    // If key empty
    if ($key == '') {
        return '';
    }
    // Check in cache
    if (isset($cache_codes[$tablename][$key])) {
        // Can be defined to 0 or ''
        return $cache_codes[$tablename][$key];
        // Found in cache
    }
    $sql = "SELECT " . $fieldid . " as id";
    $sql .= " FROM " . MAIN_DB_PREFIX . $tablename;
    $sql .= " WHERE " . $fieldkey . " = '" . $key . "'";
    dol_syslog('dol_getIdFromCode sql=' . $sql, LOG_DEBUG);
    $resql = $db->query($sql);
    if ($resql) {
        $obj = $db->fetch_object($resql);
        if ($obj) {
            $cache_codes[$tablename][$key] = $obj->id;
        } else {
            $cache_codes[$tablename][$key] = '';
        }
        $db->free($resql);
        return $cache_codes[$tablename][$key];
    } else {
        dol_syslog("dol_getIdFromCode error=" . $db->lasterror(), LOG_ERR);
        return -1;
    }
}