Fix: pgsql compatibility

This commit is contained in:
Laurent Destailleur 2011-03-09 10:07:42 +00:00
parent 8b1b3ef1b3
commit 22a26b9e2b

View File

@ -1,5 +1,5 @@
<?php <?php
/* Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net> /* Copyright (C) 2004-2009 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2006-2007 Yannick Warnier <ywarnier@beeznest.org> * Copyright (C) 2006-2007 Yannick Warnier <ywarnier@beeznest.org>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
@ -26,7 +26,7 @@
/** /**
* \brief Look for collectable VAT clients in the chosen year * \brief Look for collectable VAT clients in the chosen year (and month)
* \param db Database handle * \param db Database handle
* \param y Year * \param y Year
* \param date_start Start date * \param date_start Start date
@ -35,7 +35,7 @@
* \param direction 'sell' or 'buy' * \param direction 'sell' or 'buy'
* \return array List of customers third parties with vat, -1 if no accountancy module, -2 if not yet developped, -3 if error * \return array List of customers third parties with vat, -1 if no accountancy module, -2 if not yet developped, -3 if error
*/ */
function vat_by_thirdparty($db, $y, $date_start, $date_end, $modetax, $direction) function vat_by_thirdparty($db, $y, $date_start, $date_end, $modetax, $direction, $m=0)
{ {
global $conf; global $conf;
@ -84,7 +84,16 @@ function vat_by_thirdparty($db, $y, $date_start, $date_end, $modetax, $direction
$sql.= " OR f.type = 1"; // Replacement $sql.= " OR f.type = 1"; // Replacement
$sql.= " OR f.type = 2)"; // Credit note $sql.= " OR f.type = 2)"; // Credit note
//$sql.= " OR f.type = 3"; // We do not include deposit //$sql.= " OR f.type = 3"; // We do not include deposit
if ($y) $sql.= " AND f.datef >= '".$y."0101000000' AND f.datef <= '".$y."1231235959'"; 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 ($date_start && $date_end) $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'"; if ($date_start && $date_end) $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
$sql.= " AND s.rowid = f.fk_soc AND f.rowid = fd.".$fk_facture; $sql.= " AND s.rowid = f.fk_soc AND f.rowid = fd.".$fk_facture;
$sql.= " GROUP BY s.rowid"; $sql.= " GROUP BY s.rowid";
@ -115,7 +124,16 @@ function vat_by_thirdparty($db, $y, $date_start, $date_end, $modetax, $direction
$sql.= " OR f.type = 1"; // Replacement $sql.= " OR f.type = 1"; // Replacement
$sql.= " OR f.type = 2)"; // Credit note $sql.= " OR f.type = 2)"; // Credit note
//$sql.= " OR f.type = 3"; // We do not include deposit //$sql.= " OR f.type = 3"; // We do not include deposit
if ($y) $sql.= " AND f.datef >= '".$y."0101000000' AND f.datef <= '".$y."1231235959'"; 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 ($date_start && $date_end) $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'"; if ($date_start && $date_end) $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
$sql.= " AND s.rowid = f.fk_soc AND f.rowid = fd.".$fk_facture; $sql.= " AND s.rowid = f.fk_soc AND f.rowid = fd.".$fk_facture;
$sql.= " GROUP BY s.rowid"; $sql.= " GROUP BY s.rowid";
@ -225,9 +243,17 @@ function vat_by_date($db, $y, $q, $date_start, $date_end, $modetax, $direction,
$sql.= " OR f.type = 2)"; // Credit note $sql.= " OR f.type = 2)"; // Credit note
//$sql.= " OR f.type = 3"; // We do not include deposit //$sql.= " OR f.type = 3"; // We do not include deposit
$sql.= " AND f.rowid = d.".$fk_facture; $sql.= " AND f.rowid = d.".$fk_facture;
if ($y) $sql.= " AND f.datef >= '".$y."0101000000' AND f.datef <= '".$y."1231235959'"; 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 ($q) $sql.= " AND (date_format(f.datef,'%m') > ".(($q-1)*3)." AND date_format(f.datef,'%m') <= ".($q*3).")";
if ($m) $sql.= " AND (date_format(f.datef,'%m') > ".($m-1)." AND date_format(f.datef,'%m') <= ".($m).")";
if ($date_start && $date_end) $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'"; 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.product_type = 0"; // Limit to products
$sql.= " AND d.date_start is null AND d.date_end IS NULL)"; // enhance detection of service $sql.= " AND d.date_start is null AND d.date_end IS NULL)"; // enhance detection of service
@ -270,9 +296,17 @@ function vat_by_date($db, $y, $q, $date_start, $date_end, $modetax, $direction,
// $sql.= " AND pa.rowid = pf.".$fk_payment; // $sql.= " AND pa.rowid = pf.".$fk_payment;
// $sql.= " AND pa.datep >= '".$y."0101000000' AND pa.datep <= '".$y."1231235959'"; // $sql.= " AND pa.datep >= '".$y."0101000000' AND pa.datep <= '".$y."1231235959'";
// $sql.= " AND (date_format(pa.datep,'%m') > ".(($q-1)*3)." AND date_format(pa.datep,'%m') <= ".($q*3).")"; // $sql.= " AND (date_format(pa.datep,'%m') > ".(($q-1)*3)." AND date_format(pa.datep,'%m') <= ".($q*3).")";
if ($y) $sql.= " AND f.datef >= '".$y."0101000000' AND f.datef <= '".$y."1231235959'"; 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 ($q) $sql.= " AND (date_format(f.datef,'%m') > ".(($q-1)*3)." AND date_format(f.datef,'%m') <= ".($q*3).")";
if ($m) $sql.= " AND (date_format(f.datef,'%m') > ".($m-1)." AND date_format(f.datef,'%m') <= ".($m).")";
if ($date_start && $date_end) $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'"; 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.product_type = 0"; // Limit to products
$sql.= " AND d.date_start is null AND d.date_end IS NULL)"; // enhance detection of service $sql.= " AND d.date_start is null AND d.date_end IS NULL)"; // enhance detection of service
@ -370,9 +404,17 @@ function vat_by_date($db, $y, $q, $date_start, $date_end, $modetax, $direction,
$sql.= " OR f.type = 2)"; // Credit note $sql.= " OR f.type = 2)"; // Credit note
//$sql.= " OR f.type = 3"; // We do not include deposit //$sql.= " OR f.type = 3"; // We do not include deposit
$sql.= " AND f.rowid = d.".$fk_facture; $sql.= " AND f.rowid = d.".$fk_facture;
if ($y) $sql.= " AND f.datef >= '".$y."0101000000' AND f.datef <= '".$y."1231235959'"; 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 ($q) $sql.= " AND (date_format(f.datef,'%m') > ".(($q-1)*3)." AND date_format(f.datef,'%m') <= ".($q*3).")";
if ($m) $sql.= " AND (date_format(f.datef,'%m') > ".($m-1)." AND date_format(f.datef,'%m') <= ".($m).")";
if ($date_start && $date_end) $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'"; 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.= " 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.= " OR d.date_start is NOT null OR d.date_end IS NOT NULL)"; // enhance detection of service
@ -413,9 +455,17 @@ function vat_by_date($db, $y, $q, $date_start, $date_end, $modetax, $direction,
$sql.= " AND f.rowid = d.".$fk_facture;; $sql.= " AND f.rowid = d.".$fk_facture;;
$sql.= " AND pf.".$fk_facture2." = f.rowid"; $sql.= " AND pf.".$fk_facture2." = f.rowid";
$sql.= " AND pa.rowid = pf.".$fk_payment; $sql.= " AND pa.rowid = pf.".$fk_payment;
if ($y) $sql.= " AND pa.datep >= '".$y."0101000000' AND pa.datep <= '".$y."1231235959'"; 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 ($q) $sql.= " AND (date_format(pa.datep,'%m') > ".(($q-1)*3)." AND date_format(pa.datep,'%m') <= ".($q*3).")";
if ($m) $sql.= " AND (date_format(pa.datep,'%m') > ".($m-1)." AND date_format(pa.datep,'%m') <= ".($m).")";
if ($date_start && $date_end) $sql.= " AND pa.datep >= ".$db->idate($date_start)." AND pa.datep <= ".$db->idate($date_end); 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.= " 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.= " OR d.date_start is NOT null OR d.date_end IS NOT NULL)"; // enhance detection of service