Skip to content
Snippets Groups Projects
Commit 5ed79800 authored by aspangaro's avatar aspangaro
Browse files

Begin to wok on Vat expense report integration in reports

parent 7fa96924
Branches
Tags
No related merge requests found
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
* Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net> * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2005-2009 Regis Houssin <regis.houssin@capnetworks.com> * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@capnetworks.com>
* Copyright (C) 2014 Ferran Marcet <fmarcet@2byte.es> * Copyright (C) 2014 Ferran Marcet <fmarcet@2byte.es>
* Copyright (C) 2016 Alexandre Spangaro <aspangaro@zendsi.com>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
...@@ -147,9 +148,10 @@ for ($m = 1 ; $m < 13 ; $m++ ) ...@@ -147,9 +148,10 @@ for ($m = 1 ; $m < 13 ; $m++ )
{ {
$coll_listsell = vat_by_date($db, $y, 0, 0, 0, $modetax, 'sell', $m); $coll_listsell = vat_by_date($db, $y, 0, 0, 0, $modetax, 'sell', $m);
$coll_listbuy = vat_by_date($db, $y, 0, 0, 0, $modetax, 'buy', $m); $coll_listbuy = vat_by_date($db, $y, 0, 0, 0, $modetax, 'buy', $m);
$coll_listexpensereport = vat_by_date($db, $y, 0, 0, 0, $modetax, 'expensereport', $m);
$action = "tva"; $action = "tva";
$object = array(&$coll_listsell, &$coll_listbuy); $object = array(&$coll_listsell, &$coll_listbuy, &$coll_listexpensereport);
$parameters["mode"] = $modetax; $parameters["mode"] = $modetax;
$parameters["year"] = $y; $parameters["year"] = $y;
$parameters["month"] = $m; $parameters["month"] = $m;
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
* Copyright (C) 2004-2013 Laurent Destailleur <eldy@users.sourceforge.net> * Copyright (C) 2004-2013 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2006-2007 Yannick Warnier <ywarnier@beeznest.org> * Copyright (C) 2006-2007 Yannick Warnier <ywarnier@beeznest.org>
* Copyright (C) 2014 Ferran Marcet <fmarcet@2byte.es> * Copyright (C) 2014 Ferran Marcet <fmarcet@2byte.es>
* Copyright (C) 2016 Alexandre Spangaro <aspangaro@zendsi.com>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
...@@ -36,11 +37,14 @@ require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; ...@@ -36,11 +37,14 @@ require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php'; require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php'; require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
require_once DOL_DOCUMENT_ROOT.'/fourn/class/paiementfourn.class.php'; require_once DOL_DOCUMENT_ROOT.'/fourn/class/paiementfourn.class.php';
require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
require_once DOL_DOCUMENT_ROOT.'/expensereport/class/paymentexpensereport.class.php';
$langs->load("bills"); $langs->load("bills");
$langs->load("compta"); $langs->load("compta");
$langs->load("companies"); $langs->load("companies");
$langs->load("products"); $langs->load("products");
$langs->load("trips");
$langs->load("other"); $langs->load("other");
// Date range // Date range
...@@ -111,9 +115,11 @@ $form=new Form($db); ...@@ -111,9 +115,11 @@ $form=new Form($db);
$company_static=new Societe($db); $company_static=new Societe($db);
$invoice_customer=new Facture($db); $invoice_customer=new Facture($db);
$invoice_supplier=new FactureFournisseur($db); $invoice_supplier=new FactureFournisseur($db);
$expensereport=new ExpenseReport($db);
$product_static=new Product($db); $product_static=new Product($db);
$payment_static=new Paiement($db); $payment_static=new Paiement($db);
$paymentfourn_static=new PaiementFourn($db); $paymentfourn_static=new PaiementFourn($db);
$paymentexpensereport_static=new PaymentExpenseReport($db);
//print load_fiche_titre($langs->trans("VAT"),""); //print load_fiche_titre($langs->trans("VAT"),"");
...@@ -149,16 +155,26 @@ if ($modetax==1) // Calculate on invoice for goods and services ...@@ -149,16 +155,26 @@ if ($modetax==1) // Calculate on invoice for goods and services
$builddate=time(); $builddate=time();
//$exportlink=$langs->trans("NotYetAvailable"); //$exportlink=$langs->trans("NotYetAvailable");
// Customers invoices
$elementcust=$langs->trans("CustomersInvoices"); $elementcust=$langs->trans("CustomersInvoices");
$productcust=$langs->trans("ProductOrService"); $productcust=$langs->trans("ProductOrService");
$amountcust=$langs->trans("AmountHT"); $amountcust=$langs->trans("AmountHT");
$vatcust=$langs->trans("VATReceived"); $vatcust=$langs->trans("VATReceived");
if ($mysoc->tva_assuj) $vatcust.=' ('.$langs->trans("ToPay").')'; if ($mysoc->tva_assuj) $vatcust.=' ('.$langs->trans("ToPay").')';
// Suppliers invoices
$elementsup=$langs->trans("SuppliersInvoices"); $elementsup=$langs->trans("SuppliersInvoices");
$productsup=$langs->trans("ProductOrService"); $productsup=$langs->trans("ProductOrService");
$amountsup=$langs->trans("AmountHT"); $amountsup=$langs->trans("AmountHT");
$vatsup=$langs->trans("VATPaid"); $vatsup=$langs->trans("VATPaid");
if ($mysoc->tva_assuj) $vatsup.=' ('.$langs->trans("ToGetBack").')'; if ($mysoc->tva_assuj) $vatsup.=' ('.$langs->trans("ToGetBack").')';
// Expense reports
$elementexpensereport=$langs->trans("ExpenseReports");
$fees=$langs->trans("Fees");
$amountexpensereport=$langs->trans("AmountHT");
$vatexpensereport=$langs->trans("VATPaid");
if ($mysoc->tva_assuj) $vatexpensereport.=' ('.$langs->trans("ToGetBack").')';
} }
if ($modetax==0) // Invoice for goods, payment for services if ($modetax==0) // Invoice for goods, payment for services
{ {
...@@ -185,26 +201,35 @@ if ($modetax==0) // Invoice for goods, payment for services ...@@ -185,26 +201,35 @@ if ($modetax==0) // Invoice for goods, payment for services
$builddate=time(); $builddate=time();
//$exportlink=$langs->trans("NotYetAvailable"); //$exportlink=$langs->trans("NotYetAvailable");
// Customers invoices
$elementcust=$langs->trans("CustomersInvoices"); $elementcust=$langs->trans("CustomersInvoices");
$productcust=$langs->trans("ProductOrService"); $productcust=$langs->trans("ProductOrService");
$amountcust=$langs->trans("AmountHT"); $amountcust=$langs->trans("AmountHT");
$vatcust=$langs->trans("VATReceived"); $vatcust=$langs->trans("VATReceived");
if ($mysoc->tva_assuj) $vatcust.=' ('.$langs->trans("ToPay").')'; if ($mysoc->tva_assuj) $vatcust.=' ('.$langs->trans("ToPay").')';
// Suppliers invoices
$elementsup=$langs->trans("SuppliersInvoices"); $elementsup=$langs->trans("SuppliersInvoices");
$productsup=$langs->trans("ProductOrService"); $productsup=$langs->trans("ProductOrService");
$amountsup=$langs->trans("AmountHT"); $amountsup=$langs->trans("AmountHT");
$vatsup=$langs->trans("VATPaid"); $vatsup=$langs->trans("VATPaid");
if ($mysoc->tva_assuj) $vatsup.=' ('.$langs->trans("ToGetBack").')'; if ($mysoc->tva_assuj) $vatsup.=' ('.$langs->trans("ToGetBack").')';
// Expense reports
$elementexpensereport=$langs->trans("ExpenseReports");
$fees=$langs->trans("Fees");
$amountexpensereport=$langs->trans("AmountHT");
$vatexpensereport=$langs->trans("VATPaid");
if ($mysoc->tva_assuj) $vatexpensereport.=' ('.$langs->trans("ToGetBack").')';
} }
report_header($nom,$nomlink,$period,$periodlink,$description,$builddate,$exportlink,array(),$calcmode); report_header($nom,$nomlink,$period,$periodlink,$description,$builddate,$exportlink,array(),$calcmode);
$vatcust=$langs->trans("VATReceived"); $vatcust=$langs->trans("VATReceived");
$vatsup=$langs->trans("VATPaid"); $vatsup=$langs->trans("VATPaid");
$vatexpensereport=$langs->trans("VATPaid");
// VAT Received and paid // VAT Received and paid
print '<table class="noborder" width="100%">';
echo '<table class="noborder" width="100%">';
$y = $year_current; $y = $year_current;
$total = 0; $total = 0;
...@@ -625,6 +650,156 @@ else ...@@ -625,6 +650,156 @@ else
print '</tr>'; print '</tr>';
} }
// Blank line
print '<tr><td colspan="'.($span+1).'">&nbsp;</td></tr>';
// Expense reports
print '<tr class="liste_titre liste_titre_topborder">';
print '<td align="left">'.$elementexpensereport.'</td>';
print '<td align="left">'.$fees.'</td>';
if ($modetax == 0)
{
print '<td align="right">'.$amountexpensereport.'</td>';
print '<td align="right">'.$langs->trans("Payment").' ('.$langs->trans("PercentOfInvoice").')</td>';
}
print '<td align="right">'.$langs->trans("AmountHTVATRealPaid").'</td>';
print '<td align="right">'.$vatexpensereport.'</td>';
print '</tr>'."\n";
foreach(array_keys($x_paye) as $rate)
{
$subtot_paye_total_ht = 0;
$subtot_paye_vat = 0;
if(is_array($x_both[$rate]['paye']['detail']))
{
$var=true;
print "<tr>";
print '<td class="tax_rate">'.$langs->trans("Rate").': '.vatrate($rate).'%</td><td colspan="'.$span.'"></td>';
print '</tr>'."\n";
foreach($x_both[$rate]['paye']['detail'] as $index=>$fields)
{
// Define type
$type=($fields['dtype']?$fields['dtype']:$fields['ptype']);
// Try to enhance type detection using date_start and date_end for free lines where type
// was not saved.
if (! empty($fields['ddate_start'])) $type=1;
if (! empty($fields['ddate_end'])) $type=1;
$var=!$var;
print '<tr '.$bc[$var].'>';
// Ref
print '<td class="nowrap" align="left">'.$fields['link'].'</td>';
// Description
print '<td align="left">';
if ($fields['pid'])
{
$product_static->id=$fields['pid'];
$product_static->ref=$fields['pref'];
$product_static->type=$fields['ptype'];
print $product_static->getNomUrl(1);
if (dol_string_nohtmltag($fields['descr'])) print ' - '.dol_trunc(dol_string_nohtmltag($fields['descr']),16);
}
else
{
if ($type) $text = img_object($langs->trans('Service'),'service');
else $text = img_object($langs->trans('Product'),'product');
print $text.' '.dol_trunc(dol_string_nohtmltag($fields['descr']),16);
// Show range
print_date_range($fields['ddate_start'],$fields['ddate_end']);
}
print '</td>';
// Total HT
if ($modetax == 0)
{
print '<td class="nowrap" align="right">';
print price($fields['totalht']);
if (price2num($fields['ftotal_ttc']))
{
//print $fields['dtotal_ttc']."/".$fields['ftotal_ttc']." - ";
$ratiolineinvoice=($fields['dtotal_ttc']/$fields['ftotal_ttc']);
//print ' ('.round($ratiolineinvoice*100,2).'%)';
}
print '</td>';
}
// Payment
$ratiopaymentinvoice=1;
if ($modetax == 0)
{
if (isset($fields['payment_amount']) && $fields['ftotal_ttc']) $ratiopaymentinvoice=($fields['payment_amount']/$fields['ftotal_ttc']);
print '<td class="nowrap" align="right">';
if ($fields['payment_amount'] && $fields['ftotal_ttc'])
{
$paymentfourn_static->id=$fields['payment_id'];
print $paymentfourn_static->getNomUrl(2);
}
if ($type == 0)
{
print $langs->trans("NotUsedForGoods");
}
else
{
print price($fields['payment_amount']);
if (isset($fields['payment_amount'])) print ' ('.round($ratiopaymentinvoice*100,2).'%)';
}
print '</td>';
}
// VAT paid
print '<td class="nowrap" align="right">';
$temp_ht=$fields['totalht'];
if ($type == 1) $temp_ht=$fields['totalht']*$ratiopaymentinvoice;
print price(price2num($temp_ht,'MT'),1);
print '</td>';
// VAT
print '<td class="nowrap" align="right">';
$temp_vat=$fields['vat'];
if ($type == 1) $temp_vat=$fields['vat']*$ratiopaymentinvoice;
print price(price2num($temp_vat,'MT'),1);
//print price($fields['vat']);
print '</td>';
print '</tr>';
$subtot_paye_total_ht += $temp_ht;
$subtot_paye_vat += $temp_vat;
$x_paye_sum += $temp_vat;
}
}
// Total suppliers for this vat rate
print '<tr class="liste_total">';
print '<td>&nbsp;</td>';
print '<td align="right">'.$langs->trans("Total").':</td>';
if ($modetax == 0)
{
print '<td class="nowrap" align="right">&nbsp;</td>';
print '<td align="right">&nbsp;</td>';
}
print '<td align="right">'.price(price2num($subtot_paye_total_ht,'MT')).'</td>';
print '<td class="nowrap" align="right">'.price(price2num($subtot_paye_vat,'MT')).'</td>';
print '</tr>';
}
if (count($x_paye) == 0) // Show a total ine if nothing shown
{
print '<tr class="liste_total">';
print '<td>&nbsp;</td>';
print '<td align="right">'.$langs->trans("Total").':</td>';
if ($modetax == 0)
{
print '<td class="nowrap" align="right">&nbsp;</td>';
print '<td align="right">&nbsp;</td>';
}
print '<td align="right">'.price(price2num(0,'MT')).'</td>';
print '<td class="nowrap" align="right">'.price(price2num(0,'MT')).'</td>';
print '</tr>';
}
print '</table>'; print '</table>';
// Total to pay // Total to pay
......
...@@ -230,6 +230,17 @@ function vat_by_date($db, $y, $q, $date_start, $date_end, $modetax, $direction, ...@@ -230,6 +230,17 @@ function vat_by_date($db, $y, $q, $date_start, $date_end, $modetax, $direction,
$paymentfacturetable='paiementfourn_facturefourn'; $paymentfacturetable='paiementfourn_facturefourn';
$invoicefieldref='ref'; $invoicefieldref='ref';
} }
if ($direction == 'expensereport')
{
$expensereporttable='expensereport';
$expensereportdettable='expensereport_det';
$fk_expensereport='fk_expensereport';
$total_tva='total_tva';
$total_localtax1='total_localtax1';
$total_localtax2='total_localtax2';
$paymenttable='payment_expensereport';
$expensereportfieldref='ref';
}
// CAS DES BIENS // CAS DES BIENS
...@@ -515,6 +526,130 @@ function vat_by_date($db, $y, $q, $date_start, $date_end, $modetax, $direction, ...@@ -515,6 +526,130 @@ function vat_by_date($db, $y, $q, $date_start, $date_end, $modetax, $direction,
} }
} }
// Expense Report
// Define sql request
$sql='';
if ($modetax == 1) // Option vat on delivery for goods (payment) and debit invoice for services
{
// Count on invoice date
$sql = "SELECT erd.rowid, erd.fk_c_type_fees as fees_type, erd.".$fk_expensereport." as erid, erd.tva_tx as rate, erd.total_ht as total_ht, erd.total_ttc as total_ttc, erd.".$total_tva." as total_vat, erd.comments as descr, erd.date as dateexpensereport,";
$sql .=" erd.".$total_localtax1." as total_localtax1, erd.".$total_localtax2." as total_localtax2, ";
$sql.= " er.".$expensereportfieldref." as ernum, er.total_ttc as ertotal_ttc, u.lastname as user_lastname, u.firstname as user_firstname, u.rowid as user_id,";
// $sql.= " er.date_debut as date_start, er.date_fin as date_end,";
$sql.= " f.id as fid, f.label as flabel";
$sql.= " FROM ".MAIN_DB_PREFIX.$expensereporttable." as er,";
$sql.= " ".MAIN_DB_PREFIX.$expensereportdettable." as erd," ;
$sql.= " ".MAIN_DB_PREFIX."user as u";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_type_fees as f on erd.fk_c_type_fees = f.id";
$sql.= " WHERE er.entity = " . $conf->entity;
$sql.= " AND u.rowid = er.fk_user_author";
$sql.= " AND er.fk_statut in (5,6)"; // Validated or paid (partially or completely)
$sql.= " AND er.rowid = erd.".$fk_expensereport;
if ($y && $m)
{
$sql.= " AND erd.date >= '".$db->idate(dol_get_first_day($y,$m,false))."'";
$sql.= " AND erd.date <= '".$db->idate(dol_get_last_day($y,$m,false))."'";
}
else if ($y)
{
$sql.= " AND erd.date >= '".$db->idate(dol_get_first_day($y,1,false))."'";
$sql.= " AND erd.date <= '".$db->idate(dol_get_last_day($y,12,false))."'";
}
if ($q) $sql.= " AND (date_format(erd.date,'%m') > ".(($q-1)*3)." AND date_format(erd.date,'%m') <= ".($q*3).")";
if ($date_start && $date_end) $sql.= " AND erd.date >= '".$db->idate($date_start)."' AND erd.date <= '".$db->idate($date_end)."'";
$sql.= " ORDER BY erd.rowid, erd.".$fk_expensereport;
}
else // Option vat on delivery for goods (payments) and payments for services
{
// Count on payments date
$sql = "SELECT erd.rowid, erd.fk_c_type_fees as fees_type, erd.".$fk_expensereport." as erid, erd.tva_tx as rate, erd.total_ht as total_ht, erd.total_ttc as total_ttc, erd.".$total_tva." as total_vat, erd.comments as descr, erd.date as dateexpensereport,";
$sql .=" erd.".$total_localtax1." as total_localtax1, erd.".$total_localtax2." as total_localtax2, ";
$sql.= " er.".$expensereportfieldref." as ernum, er.total_ttc as ertotal_ttc, u.lastname as user_lastname, u.firstname as user_firstname, u.rowid as user_id,";
// $sql.= " er.date_debut as date_start, er.date_start as date_end,";
$sql.= " f.id as fid, f.label as flabel,";
$sql.= " pa.rowid as payment_id, pa.amount as payment_amount";
$sql.= " FROM ".MAIN_DB_PREFIX.$expensereporttable." as er,";
$sql.= " ".MAIN_DB_PREFIX.$paymenttable." as pa,";
$sql.= " ".MAIN_DB_PREFIX."user as u,";
$sql.= " ".MAIN_DB_PREFIX.$expensereportdettable." as erd";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_type_fees as f on erd.fk_c_type_fees = f.id";
$sql.= " WHERE er.entity = " . $conf->entity;
$sql.= " AND er.fk_statut in (5,6)"; // Paid (partially or completely)
$sql.= " AND er.rowid = erd.".$fk_expensereport;
$sql.= " AND u.rowid = er.fk_user_author";
$sql.= " AND pa.rowid = er.".$fk_expensereport;
if ($y && $m)
{
$sql.= " AND erd.date >= '".$db->idate(dol_get_first_day($y,$m,false))."'";
$sql.= " AND erd.date <= '".$db->idate(dol_get_last_day($y,$m,false))."'";
}
else if ($y)
{
$sql.= " AND erd.date >= '".$db->idate(dol_get_first_day($y,1,false))."'";
$sql.= " AND erd.date <= '".$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.= " ORDER BY er.rowid, erd.".$fk_expensereport;
}
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;
$list[$assoc['rate']]['erdtotal_ttc'][] = $assoc['total_ttc'];
$list[$assoc['rate']]['ftype'][] = $assoc['fees_type'];
$list[$assoc['rate']]['erdate'][] = $assoc['dateexpensereport'];
$list[$assoc['rate']]['ulastname'][] = $assoc['user_lastname'];
$list[$assoc['rate']]['ufirstname'][] = $assoc['user_firstname'];
$list[$assoc['rate']]['uid'][] = $assoc['user_id'];
// $list[$assoc['rate']]['erddate_start'][] = $db->jdate($assoc['date_start']);
// $list[$assoc['rate']]['erddate_end'][] = $db->jdate($assoc['date_end']);
$list[$assoc['rate']]['erid'][] = $assoc['erid'];
$list[$assoc['rate']]['ernum'][] = $assoc['ernum'];
$list[$assoc['rate']]['ertotal_ttc'][] = $assoc['ertotal_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']]['fid'][] = $assoc['fid'];
$list[$assoc['rate']]['flabel'][] = $assoc['flabel'];
$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; return $list;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment