Example #1
0
/**
 *  Gets VAT to collect for the given year (and given quarter or month)
 *  The function gets the VAT in split results, as the VAT declaration asks
 *  to report the amounts for different VAT rates as different lines.
 *  This function also accounts recurrent invoices.
 *
 *  @param	DoliDB	$db          	Database handler object
 *  @param  int		$y           	Year
 *  @param  int		$q           	Quarter
 *  @param  string	$date_start  	Start date
 *  @param  string	$date_end    	End date
 *  @param  int		$modetax     	0 or 1 (option vat on debit)
 *  @param  int		$direction   	'sell' (customer invoice) or 'buy' (supplier invoices)
 *  @param  int		$m           	Month
 *  @return array       			List of quarters with vat
 */
function vat_by_date($db, $y, $q, $date_start, $date_end, $modetax, $direction, $m = 0)
{
    global $conf;
    $list = array();
    if ($direction == 'sell') {
        $invoicetable = 'facture';
        $invoicedettable = 'facturedet';
        $fk_facture = 'fk_facture';
        $fk_facture2 = 'fk_facture';
        $fk_payment = 'fk_paiement';
        $total_tva = 'total_tva';
        $total_localtax1 = 'total_localtax1';
        $total_localtax2 = 'total_localtax2';
        $paymenttable = 'paiement';
        $paymentfacturetable = 'paiement_facture';
        $invoicefieldref = 'facnumber';
    }
    if ($direction == 'buy') {
        $invoicetable = 'facture_fourn';
        $invoicedettable = 'facture_fourn_det';
        $fk_facture = 'fk_facture_fourn';
        $fk_facture2 = 'fk_facturefourn';
        $fk_payment = 'fk_paiementfourn';
        $total_tva = 'tva';
        $total_localtax1 = 'total_localtax1';
        $total_localtax2 = 'total_localtax2';
        $paymenttable = 'paiementfourn';
        $paymentfacturetable = 'paiementfourn_facturefourn';
        $invoicefieldref = 'ref';
    }
    // CAS DES BIENS
    // Define sql request
    $sql = '';
    if ($modetax == 1) {
        // Count on delivery date (use invoice date as delivery is unknown)
        $sql = "SELECT d.rowid, d.product_type as dtype, d." . $fk_facture . " as facid, d.tva_tx as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d." . $total_tva . " as total_vat, d.description as descr,";
        $sql .= " d." . $total_localtax1 . " as total_localtax1, d." . $total_localtax2 . " as total_localtax2, ";
        $sql .= " d.date_start as date_start, d.date_end as date_end,";
        $sql .= " f." . $invoicefieldref . " as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef, s.nom as company_name, s.rowid as company_id,";
        $sql .= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
        $sql .= " 0 as payment_id, 0 as payment_amount";
        $sql .= " FROM " . MAIN_DB_PREFIX . $invoicetable . " as f,";
        $sql .= " " . MAIN_DB_PREFIX . "societe as s,";
        $sql .= " " . MAIN_DB_PREFIX . $invoicedettable . " as d";
        $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "product as p on d.fk_product = p.rowid";
        $sql .= " WHERE f.entity = " . $conf->entity;
        $sql .= " AND f.fk_statut in (1,2)";
        // Validated or paid (partially or completely)
        if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
            $sql .= " AND f.type IN (0,1,2,5)";
        } else {
            $sql .= " AND f.type IN (0,1,2,3,5)";
        }
        $sql .= " AND f.rowid = d." . $fk_facture;
        $sql .= " AND s.rowid = f.fk_soc";
        if ($y && $m) {
            $sql .= " AND f.datef >= '" . $db->idate(dol_get_first_day($y, $m, false)) . "'";
            $sql .= " AND f.datef <= '" . $db->idate(dol_get_last_day($y, $m, false)) . "'";
        } else {
            if ($y) {
                $sql .= " AND f.datef >= '" . $db->idate(dol_get_first_day($y, 1, false)) . "'";
                $sql .= " AND f.datef <= '" . $db->idate(dol_get_last_day($y, 12, false)) . "'";
            }
        }
        if ($q) {
            $sql .= " AND (date_format(f.datef,'%m') > " . ($q - 1) * 3 . " AND date_format(f.datef,'%m') <= " . $q * 3 . ")";
        }
        if ($date_start && $date_end) {
            $sql .= " AND f.datef >= '" . $db->idate($date_start) . "' AND f.datef <= '" . $db->idate($date_end) . "'";
        }
        $sql .= " AND (d.product_type = 0";
        // Limit to products
        $sql .= " AND d.date_start is null AND d.date_end IS NULL)";
        // enhance detection of service
        $sql .= " ORDER BY d.rowid, d." . $fk_facture;
    } else {
        // Count on delivery date (use invoice date as delivery is unknown)
        $sql = "SELECT d.rowid, d.product_type as dtype, d." . $fk_facture . " as facid, d.tva_tx as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d." . $total_tva . " as total_vat, d.description as descr,";
        $sql .= " d." . $total_localtax1 . " as total_localtax1, d." . $total_localtax2 . " as total_localtax2, ";
        $sql .= " d.date_start as date_start, d.date_end as date_end,";
        $sql .= " f." . $invoicefieldref . " as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef as date_f, s.nom as company_name, s.rowid as company_id,";
        $sql .= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
        $sql .= " 0 as payment_id, 0 as payment_amount";
        $sql .= " FROM " . MAIN_DB_PREFIX . $invoicetable . " as f,";
        $sql .= " " . MAIN_DB_PREFIX . "societe as s,";
        $sql .= " " . MAIN_DB_PREFIX . $invoicedettable . " as d";
        $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "product as p on d.fk_product = p.rowid";
        $sql .= " WHERE f.entity = " . $conf->entity;
        $sql .= " AND f.fk_statut in (1,2)";
        // Validated or paid (partially or completely)
        if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
            $sql .= " AND f.type IN (0,1,2,5)";
        } else {
            $sql .= " AND f.type IN (0,1,2,3,5)";
        }
        $sql .= " AND f.rowid = d." . $fk_facture;
        $sql .= " AND s.rowid = f.fk_soc";
        if ($y && $m) {
            $sql .= " AND f.datef >= '" . $db->idate(dol_get_first_day($y, $m, false)) . "'";
            $sql .= " AND f.datef <= '" . $db->idate(dol_get_last_day($y, $m, false)) . "'";
        } else {
            if ($y) {
                $sql .= " AND f.datef >= '" . $db->idate(dol_get_first_day($y, 1, false)) . "'";
                $sql .= " AND f.datef <= '" . $db->idate(dol_get_last_day($y, 12, false)) . "'";
            }
        }
        if ($q) {
            $sql .= " AND (date_format(f.datef,'%m') > " . ($q - 1) * 3 . " AND date_format(f.datef,'%m') <= " . $q * 3 . ")";
        }
        if ($date_start && $date_end) {
            $sql .= " AND f.datef >= '" . $db->idate($date_start) . "' AND f.datef <= '" . $db->idate($date_end) . "'";
        }
        $sql .= " AND (d.product_type = 0";
        // Limit to products
        $sql .= " AND d.date_start is null AND d.date_end IS NULL)";
        // enhance detection of service
        $sql .= " ORDER BY d.rowid, d." . $fk_facture;
        //print $sql;
    }
    //print $sql.'<br>';
    if (!$sql) {
        return -1;
    }
    if ($sql == 'TODO') {
        return -2;
    }
    if ($sql != 'TODO') {
        dol_syslog("Tax.lib.php::vat_by_date", LOG_DEBUG);
        $resql = $db->query($sql);
        if ($resql) {
            $rate = -1;
            $oldrowid = '';
            while ($assoc = $db->fetch_array($resql)) {
                if (!isset($list[$assoc['rate']]['totalht'])) {
                    $list[$assoc['rate']]['totalht'] = 0;
                }
                if (!isset($list[$assoc['rate']]['vat'])) {
                    $list[$assoc['rate']]['vat'] = 0;
                }
                if (!isset($list[$assoc['rate']]['localtax1'])) {
                    $list[$assoc['rate']]['localtax1'] = 0;
                }
                if (!isset($list[$assoc['rate']]['localtax2'])) {
                    $list[$assoc['rate']]['localtax2'] = 0;
                }
                if ($assoc['rowid'] != $oldrowid) {
                    $oldrowid = $assoc['rowid'];
                    $list[$assoc['rate']]['totalht'] += $assoc['total_ht'];
                    $list[$assoc['rate']]['vat'] += $assoc['total_vat'];
                    $list[$assoc['rate']]['localtax1'] += $assoc['total_localtax1'];
                    $list[$assoc['rate']]['localtax2'] += $assoc['total_localtax2'];
                }
                $list[$assoc['rate']]['dtotal_ttc'][] = $assoc['total_ttc'];
                $list[$assoc['rate']]['dtype'][] = $assoc['dtype'];
                $list[$assoc['rate']]['datef'][] = $assoc['datef'];
                $list[$assoc['rate']]['company_name'][] = $assoc['company_name'];
                $list[$assoc['rate']]['company_id'][] = $assoc['company_id'];
                $list[$assoc['rate']]['ddate_start'][] = $db->jdate($assoc['date_start']);
                $list[$assoc['rate']]['ddate_end'][] = $db->jdate($assoc['date_end']);
                $list[$assoc['rate']]['facid'][] = $assoc['facid'];
                $list[$assoc['rate']]['facnum'][] = $assoc['facnum'];
                $list[$assoc['rate']]['type'][] = $assoc['type'];
                $list[$assoc['rate']]['ftotal_ttc'][] = $assoc['ftotal_ttc'];
                $list[$assoc['rate']]['descr'][] = $assoc['descr'];
                $list[$assoc['rate']]['totalht_list'][] = $assoc['total_ht'];
                $list[$assoc['rate']]['vat_list'][] = $assoc['total_vat'];
                $list[$assoc['rate']]['localtax1_list'][] = $assoc['total_localtax1'];
                $list[$assoc['rate']]['localtax2_list'][] = $assoc['total_localtax2'];
                $list[$assoc['rate']]['pid'][] = $assoc['pid'];
                $list[$assoc['rate']]['pref'][] = $assoc['pref'];
                $list[$assoc['rate']]['ptype'][] = $assoc['ptype'];
                $list[$assoc['rate']]['payment_id'][] = $assoc['payment_id'];
                $list[$assoc['rate']]['payment_amount'][] = $assoc['payment_amount'];
                $rate = $assoc['rate'];
            }
        } else {
            dol_print_error($db);
            return -3;
        }
    }
    // CAS DES SERVICES
    // Define sql request
    $sql = '';
    if ($modetax == 1) {
        // Count on invoice date
        $sql = "SELECT d.rowid, d.product_type as dtype, d." . $fk_facture . " as facid, d.tva_tx as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d." . $total_tva . " as total_vat, d.description as descr,";
        $sql .= " d." . $total_localtax1 . " as total_localtax1, d." . $total_localtax2 . " as total_localtax2, ";
        $sql .= " d.date_start as date_start, d.date_end as date_end,";
        $sql .= " f." . $invoicefieldref . " as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef, s.nom as company_name, s.rowid as company_id,";
        $sql .= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
        $sql .= " 0 as payment_id, 0 as payment_amount";
        $sql .= " FROM " . MAIN_DB_PREFIX . $invoicetable . " as f,";
        $sql .= " " . MAIN_DB_PREFIX . "societe as s,";
        $sql .= " " . MAIN_DB_PREFIX . $invoicedettable . " as d";
        $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "product as p on d.fk_product = p.rowid";
        $sql .= " WHERE f.entity = " . $conf->entity;
        $sql .= " AND f.fk_statut in (1,2)";
        // Validated or paid (partially or completely)
        if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
            $sql .= " AND f.type IN (0,1,2,5)";
        } else {
            $sql .= " AND f.type IN (0,1,2,3,5)";
        }
        $sql .= " AND f.rowid = d." . $fk_facture;
        $sql .= " AND s.rowid = f.fk_soc";
        if ($y && $m) {
            $sql .= " AND f.datef >= '" . $db->idate(dol_get_first_day($y, $m, false)) . "'";
            $sql .= " AND f.datef <= '" . $db->idate(dol_get_last_day($y, $m, false)) . "'";
        } else {
            if ($y) {
                $sql .= " AND f.datef >= '" . $db->idate(dol_get_first_day($y, 1, false)) . "'";
                $sql .= " AND f.datef <= '" . $db->idate(dol_get_last_day($y, 12, false)) . "'";
            }
        }
        if ($q) {
            $sql .= " AND (date_format(f.datef,'%m') > " . ($q - 1) * 3 . " AND date_format(f.datef,'%m') <= " . $q * 3 . ")";
        }
        if ($date_start && $date_end) {
            $sql .= " AND f.datef >= '" . $db->idate($date_start) . "' AND f.datef <= '" . $db->idate($date_end) . "'";
        }
        $sql .= " AND (d.product_type = 1";
        // Limit to services
        $sql .= " OR d.date_start is NOT null OR d.date_end IS NOT NULL)";
        // enhance detection of service
        $sql .= " ORDER BY d.rowid, d." . $fk_facture;
    } else {
        // Count on payments date
        $sql = "SELECT d.rowid, d.product_type as dtype, d." . $fk_facture . " as facid, d.tva_tx as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d." . $total_tva . " as total_vat, d.description as descr,";
        $sql .= " d." . $total_localtax1 . " as total_localtax1, d." . $total_localtax2 . " as total_localtax2, ";
        $sql .= " d.date_start as date_start, d.date_end as date_end,";
        $sql .= " f." . $invoicefieldref . " as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef, s.nom as company_name, s.rowid as company_id,";
        $sql .= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
        $sql .= " pf." . $fk_payment . " as payment_id, pf.amount as payment_amount";
        $sql .= " FROM " . MAIN_DB_PREFIX . $invoicetable . " as f,";
        $sql .= " " . MAIN_DB_PREFIX . $paymentfacturetable . " as pf,";
        $sql .= " " . MAIN_DB_PREFIX . $paymenttable . " as pa,";
        $sql .= " " . MAIN_DB_PREFIX . "societe as s,";
        $sql .= " " . MAIN_DB_PREFIX . $invoicedettable . " as d";
        $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "product as p on d.fk_product = p.rowid";
        $sql .= " WHERE f.entity = " . $conf->entity;
        $sql .= " AND f.fk_statut in (1,2)";
        // Paid (partially or completely)
        if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
            $sql .= " AND f.type IN (0,1,2,5)";
        } else {
            $sql .= " AND f.type IN (0,1,2,3,5)";
        }
        $sql .= " AND f.rowid = d." . $fk_facture;
        $sql .= " AND s.rowid = f.fk_soc";
        $sql .= " AND pf." . $fk_facture2 . " = f.rowid";
        $sql .= " AND pa.rowid = pf." . $fk_payment;
        if ($y && $m) {
            $sql .= " AND pa.datep >= '" . $db->idate(dol_get_first_day($y, $m, false)) . "'";
            $sql .= " AND pa.datep <= '" . $db->idate(dol_get_last_day($y, $m, false)) . "'";
        } else {
            if ($y) {
                $sql .= " AND pa.datep >= '" . $db->idate(dol_get_first_day($y, 1, false)) . "'";
                $sql .= " AND pa.datep <= '" . $db->idate(dol_get_last_day($y, 12, false)) . "'";
            }
        }
        if ($q) {
            $sql .= " AND (date_format(pa.datep,'%m') > " . ($q - 1) * 3 . " AND date_format(pa.datep,'%m') <= " . $q * 3 . ")";
        }
        if ($date_start && $date_end) {
            $sql .= " AND pa.datep >= '" . $db->idate($date_start) . "' AND pa.datep <= '" . $db->idate($date_end) . "'";
        }
        $sql .= " AND (d.product_type = 1";
        // Limit to services
        $sql .= " OR d.date_start is NOT null OR d.date_end IS NOT NULL)";
        // enhance detection of service
        $sql .= " ORDER BY d.rowid, d." . $fk_facture . ", pf.rowid";
    }
    if (!$sql) {
        dol_syslog("Tax.lib.php::vat_by_date no accountancy module enabled" . $sql, LOG_ERR);
        return -1;
        // -1 = Not accountancy module enabled
    }
    if ($sql == 'TODO') {
        return -2;
    }
    // -2 = Feature not yet available
    if ($sql != 'TODO') {
        dol_syslog("Tax.lib.php::vat_by_date", LOG_DEBUG);
        $resql = $db->query($sql);
        if ($resql) {
            $rate = -1;
            $oldrowid = '';
            while ($assoc = $db->fetch_array($resql)) {
                if (!isset($list[$assoc['rate']]['totalht'])) {
                    $list[$assoc['rate']]['totalht'] = 0;
                }
                if (!isset($list[$assoc['rate']]['vat'])) {
                    $list[$assoc['rate']]['vat'] = 0;
                }
                if (!isset($list[$assoc['rate']]['localtax1'])) {
                    $list[$assoc['rate']]['localtax1'] = 0;
                }
                if (!isset($list[$assoc['rate']]['localtax2'])) {
                    $list[$assoc['rate']]['localtax2'] = 0;
                }
                if ($assoc['rowid'] != $oldrowid) {
                    $oldrowid = $assoc['rowid'];
                    $list[$assoc['rate']]['totalht'] += $assoc['total_ht'];
                    $list[$assoc['rate']]['vat'] += $assoc['total_vat'];
                    $list[$assoc['rate']]['localtax1'] += $assoc['total_localtax1'];
                    $list[$assoc['rate']]['localtax2'] += $assoc['total_localtax2'];
                }
                $list[$assoc['rate']]['dtotal_ttc'][] = $assoc['total_ttc'];
                $list[$assoc['rate']]['dtype'][] = $assoc['dtype'];
                $list[$assoc['rate']]['datef'][] = $assoc['datef'];
                $list[$assoc['rate']]['company_name'][] = $assoc['company_name'];
                $list[$assoc['rate']]['company_id'][] = $assoc['company_id'];
                $list[$assoc['rate']]['ddate_start'][] = $db->jdate($assoc['date_start']);
                $list[$assoc['rate']]['ddate_end'][] = $db->jdate($assoc['date_end']);
                $list[$assoc['rate']]['facid'][] = $assoc['facid'];
                $list[$assoc['rate']]['facnum'][] = $assoc['facnum'];
                $list[$assoc['rate']]['type'][] = $assoc['type'];
                $list[$assoc['rate']]['ftotal_ttc'][] = $assoc['ftotal_ttc'];
                $list[$assoc['rate']]['descr'][] = $assoc['descr'];
                $list[$assoc['rate']]['totalht_list'][] = $assoc['total_ht'];
                $list[$assoc['rate']]['vat_list'][] = $assoc['total_vat'];
                $list[$assoc['rate']]['localtax1_list'][] = $assoc['total_localtax1'];
                $list[$assoc['rate']]['localtax2_list'][] = $assoc['total_localtax2'];
                $list[$assoc['rate']]['pid'][] = $assoc['pid'];
                $list[$assoc['rate']]['pref'][] = $assoc['pref'];
                $list[$assoc['rate']]['ptype'][] = $assoc['ptype'];
                $list[$assoc['rate']]['payment_id'][] = $assoc['payment_id'];
                $list[$assoc['rate']]['payment_amount'][] = $assoc['payment_amount'];
                $rate = $assoc['rate'];
            }
        } else {
            dol_print_error($db);
            return -3;
        }
    }
    return $list;
}
 /**
  * Retrieve all batch number details link to a shipment line
  *
  * @param	DoliDB	$db					Database object
  * @param	int		$id_line_expdet		id of shipment line
  * @return	variant						-1 if KO, array of ExpeditionLineBatch if OK
  */
 static function fetchAll($db, $id_line_expdet)
 {
     $sql = "SELECT rowid,";
     $sql .= "fk_expeditiondet";
     $sql .= ", sellby";
     $sql .= ", eatby";
     $sql .= ", batch";
     $sql .= ", qty";
     $sql .= ", fk_origin_stock";
     $sql .= " FROM " . MAIN_DB_PREFIX . self::$_table_element;
     $sql .= " WHERE fk_expeditiondet=" . (int) $id_line_expdet;
     dol_syslog(__METHOD__ . "", LOG_DEBUG);
     $resql = $db->query($sql);
     if ($resql) {
         $num = $db->num_rows($resql);
         $i = 0;
         while ($i < $num) {
             $tmp = new self($db);
             $obj = $db->fetch_object($resql);
             $tmp->sellby = $db->jdate($obj->sellby);
             $tmp->eatby = $db->jdate($obj->eatby);
             $tmp->batch = $obj->batch;
             $tmp->id = $obj->rowid;
             $tmp->fk_origin_stock = $obj->fk_origin_stock;
             $tmp->fk_expeditiondet = $obj->fk_expeditiondet;
             $tmp->dluo_qty = $obj->qty;
             $ret[] = $tmp;
             $i++;
         }
         $db->free($resql);
         return $ret;
     } else {
         return -1;
     }
 }
Example #3
0
 /**
  * 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;
     }
 }
/**
 *    	Show html area with actions done
 *
 * 		@param	Conf		$conf		Object conf
 * 		@param	Translate	$langs		Object langs
 * 		@param	DoliDB		$db			Object db
 * 		@param	Object		$object		Object third party or member
 * 		@param	Contact		$objcon		Object contact
 *      @param  int			$noprint    Return string but does not output it
 *      @return	mixed					Return html part or void if noprint is 1
 * TODO change function to be able to list event linked to an object.
 */
function show_actions_done($conf, $langs, $db, $object, $objcon = '', $noprint = 0)
{
    global $bc, $user;
    // Check parameters
    if (!is_object($object)) {
        dol_print_error('', 'BadParameter');
    }
    $out = '';
    $histo = array();
    $numaction = 0;
    $now = dol_now('tzuser');
    if (!empty($conf->agenda->enabled)) {
        // Recherche histo sur actioncomm
        $sql = "SELECT a.id, a.label,";
        $sql .= " a.datep as dp,";
        $sql .= " a.datep2 as dp2,";
        $sql .= " a.note, a.percent,";
        $sql .= " a.fk_element, a.elementtype,";
        $sql .= " a.fk_user_author, a.fk_contact,";
        $sql .= " c.code as acode, c.libelle,";
        $sql .= " u.login, u.rowid as user_id";
        if (get_class($object) == 'Adherent') {
            $sql .= ", m.lastname, m.firstname";
        }
        if (get_class($object) == 'Societe') {
            $sql .= ", sp.lastname, sp.firstname";
        }
        $sql .= " FROM " . MAIN_DB_PREFIX . "c_actioncomm as c, " . MAIN_DB_PREFIX . "user as u, " . MAIN_DB_PREFIX . "actioncomm as a";
        if (get_class($object) == 'Adherent') {
            $sql .= ", " . MAIN_DB_PREFIX . "adherent as m";
        }
        if (get_class($object) == 'Societe') {
            $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "socpeople as sp ON a.fk_contact = sp.rowid";
        }
        $sql .= " WHERE u.rowid = a.fk_user_author";
        $sql .= " AND a.entity IN (" . getEntity('agenda', 1) . ")";
        if (get_class($object) == 'Adherent') {
            $sql .= " AND a.fk_element = m.rowid AND a.elementtype = 'member'";
        }
        if (get_class($object) == 'Adherent' && $object->id) {
            $sql .= " AND a.fk_element = " . $object->id;
        }
        if (get_class($object) == 'Societe' && $object->id) {
            $sql .= " AND a.fk_soc = " . $object->id;
        }
        if (is_object($objcon) && $objcon->id) {
            $sql .= " AND a.fk_contact = " . $objcon->id;
        }
        $sql .= " AND c.id=a.fk_action";
        $sql .= " AND (a.percent = 100 OR (a.percent = -1 AND a.datep <= '" . $db->idate($now) . "'))";
        $sql .= " ORDER BY a.datep DESC, a.id DESC";
        dol_syslog("company.lib::show_actions_done sql=" . $sql, LOG_DEBUG);
        $resql = $db->query($sql);
        if ($resql) {
            $i = 0;
            $num = $db->num_rows($resql);
            $var = true;
            while ($i < $num) {
                $obj = $db->fetch_object($resql);
                $histo[$numaction] = array('type' => 'action', 'id' => $obj->id, 'datestart' => $db->jdate($obj->dp), 'date' => $db->jdate($obj->dp2), 'note' => $obj->label, 'percent' => $obj->percent, 'acode' => $obj->acode, 'libelle' => $obj->libelle, 'userid' => $obj->user_id, 'login' => $obj->login, 'contact_id' => $obj->fk_contact, 'lastname' => $obj->lastname, 'firstname' => $obj->firstname, 'fk_element' => $obj->fk_element, 'elementtype' => $obj->elementtype);
                $numaction++;
                $i++;
            }
        } else {
            dol_print_error($db);
        }
    }
    if (!empty($conf->mailing->enabled) && !empty($objcon->email)) {
        $langs->load("mails");
        // Recherche histo sur mailing
        $sql = "SELECT m.rowid as id, mc.date_envoi as da, m.titre as note, '100' as percentage,";
        $sql .= " 'AC_EMAILING' as acode,";
        $sql .= " u.rowid as user_id, u.login";
        // User that valid action
        $sql .= " FROM " . MAIN_DB_PREFIX . "mailing as m, " . MAIN_DB_PREFIX . "mailing_cibles as mc, " . MAIN_DB_PREFIX . "user as u";
        $sql .= " WHERE mc.email = '" . $db->escape($objcon->email) . "'";
        // Search is done on email.
        $sql .= " AND mc.statut = 1";
        $sql .= " AND u.rowid = m.fk_user_valid";
        $sql .= " AND mc.fk_mailing=m.rowid";
        $sql .= " ORDER BY mc.date_envoi DESC, m.rowid DESC";
        dol_syslog("company.lib::show_actions_done sql=" . $sql, LOG_DEBUG);
        $resql = $db->query($sql);
        if ($resql) {
            $i = 0;
            $num = $db->num_rows($resql);
            $var = true;
            while ($i < $num) {
                $obj = $db->fetch_object($resql);
                $histo[$numaction] = array('type' => 'mailing', 'id' => $obj->id, 'date' => $db->jdate($obj->da), 'note' => $obj->note, 'percent' => $obj->percentage, 'acode' => $obj->acode, 'userid' => $obj->user_id, 'login' => $obj->login);
                $numaction++;
                $i++;
            }
            $db->free($resql);
        } else {
            dol_print_error($db);
        }
    }
    if (!empty($conf->agenda->enabled) || !empty($conf->mailing->enabled) && !empty($objcon->email)) {
        require_once DOL_DOCUMENT_ROOT . '/comm/action/class/actioncomm.class.php';
        require_once DOL_DOCUMENT_ROOT . '/comm/propal/class/propal.class.php';
        require_once DOL_DOCUMENT_ROOT . '/commande/class/commande.class.php';
        require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php';
        $actionstatic = new ActionComm($db);
        $userstatic = new User($db);
        $contactstatic = new Contact($db);
        // TODO uniformize
        $propalstatic = new Propal($db);
        $orderstatic = new Commande($db);
        $facturestatic = new Facture($db);
        $out .= "\n";
        $out .= '<table class="noborder" width="100%">';
        $out .= '<tr class="liste_titre">';
        $out .= '<td colspan="2">';
        if (get_class($object) == 'Societe') {
            $out .= '<a href="' . DOL_URL_ROOT . '/comm/action/listactions.php?socid=' . $object->id . '&amp;status=done">';
        }
        $out .= $langs->trans("ActionsDoneShort");
        if (get_class($object) == 'Societe') {
            $out .= '</a>';
        }
        $out .= '</td>';
        $out .= '<td colspan="5" align="right">';
        $permok = $user->rights->agenda->myactions->create;
        if ((!empty($object->id) || !empty($objcon->id)) && $permok) {
            $out .= '<a href="' . DOL_URL_ROOT . '/comm/action/fiche.php?action=create';
            if (get_class($object) == 'Societe') {
                $out .= '&amp;socid=' . $object->id;
            }
            $out .= (!empty($objcon->id) ? '&amp;contactid=' . $objcon->id : '') . '&amp;backtopage=1&amp;percentage=-1">';
            $out .= $langs->trans("AddAnAction") . ' ';
            $out .= img_picto($langs->trans("AddAnAction"), 'filenew');
            $out .= "</a>";
        }
        $out .= '</td>';
        $out .= '</tr>';
        foreach ($histo as $key => $value) {
            $var = !$var;
            $out .= "<tr " . $bc[$var] . ">";
            // Champ date
            $out .= '<td width="120" class="nowrap">';
            if ($histo[$key]['date']) {
                $out .= dol_print_date($histo[$key]['date'], 'dayhour');
            } else {
                if ($histo[$key]['datestart']) {
                    $out .= dol_print_date($histo[$key]['datestart'], 'dayhour');
                }
            }
            $out .= "</td>\n";
            // Picto
            $out .= '<td width="16">&nbsp;</td>';
            // Action
            $out .= '<td>';
            if (isset($histo[$key]['type']) && $histo[$key]['type'] == 'action') {
                $actionstatic->type_code = $histo[$key]['acode'];
                $transcode = $langs->trans("Action" . $histo[$key]['acode']);
                $libelle = $transcode != "Action" . $histo[$key]['acode'] ? $transcode : $histo[$key]['libelle'];
                //$actionstatic->libelle=$libelle;
                $actionstatic->libelle = $histo[$key]['note'];
                $actionstatic->id = $histo[$key]['id'];
                $out .= $actionstatic->getNomUrl(1, 40);
            }
            if (isset($histo[$key]['type']) && $histo[$key]['type'] == 'mailing') {
                $out .= '<a href="' . DOL_URL_ROOT . '/comm/mailing/fiche.php?id=' . $histo[$key]['id'] . '">' . img_object($langs->trans("ShowEMailing"), "email") . ' ';
                $transcode = $langs->trans("Action" . $histo[$key]['acode']);
                $libelle = $transcode != "Action" . $histo[$key]['acode'] ? $transcode : 'Send mass mailing';
                $out .= dol_trunc($libelle, 40);
            }
            $out .= '</td>';
            // Title of event
            //$out.='<td>'.dol_trunc($histo[$key]['note'], 40).'</td>';
            // Objet lie
            // TODO uniformize
            $out .= '<td>';
            if (isset($histo[$key]['elementtype'])) {
                if ($histo[$key]['elementtype'] == 'propal' && !empty($conf->propal->enabled)) {
                    $propalstatic->ref = $langs->trans("ProposalShort");
                    $propalstatic->id = $histo[$key]['fk_element'];
                    $out .= $propalstatic->getNomUrl(1);
                } elseif ($histo[$key]['elementtype'] == 'commande' && !empty($conf->commande->enabled)) {
                    $orderstatic->ref = $langs->trans("Order");
                    $orderstatic->id = $histo[$key]['fk_element'];
                    $out .= $orderstatic->getNomUrl(1);
                } elseif ($histo[$key]['elementtype'] == 'facture' && !empty($conf->facture->enabled)) {
                    $facturestatic->ref = $langs->trans("Invoice");
                    $facturestatic->id = $histo[$key]['fk_element'];
                    $facturestatic->type = $histo[$key]['ftype'];
                    $out .= $facturestatic->getNomUrl(1, 'compta');
                } else {
                    $out .= '&nbsp;';
                }
            } else {
                $out .= '&nbsp;';
            }
            $out .= '</td>';
            // Contact pour cette action
            if (!empty($objcon->id) && isset($histo[$key]['contact_id']) && $histo[$key]['contact_id'] > 0) {
                $contactstatic->lastname = $histo[$key]['lastname'];
                $contactstatic->firstname = $histo[$key]['firstname'];
                $contactstatic->id = $histo[$key]['contact_id'];
                $out .= '<td width="120">' . $contactstatic->getNomUrl(1, '', 10) . '</td>';
            } else {
                $out .= '<td>&nbsp;</td>';
            }
            // Auteur
            $out .= '<td class="nowrap" width="80">';
            $userstatic->id = $histo[$key]['userid'];
            $userstatic->login = $histo[$key]['login'];
            $out .= $userstatic->getLoginUrl(1);
            $out .= '</td>';
            // Statut
            $out .= '<td class="nowrap" width="20">' . $actionstatic->LibStatut($histo[$key]['percent'], 3) . '</td>';
            $out .= "</tr>\n";
            $i++;
        }
        $out .= "</table>\n";
        $out .= "<br>\n";
    }
    if ($noprint) {
        return $out;
    } else {
        print $out;
    }
}