[ task #1063 ] Allow edit localtaxes rate for thirds. Works with company

This commit is contained in:
Juanjo Menent 2014-07-03 17:39:31 +02:00
parent cb4c3a4431
commit e30649dcd2
5 changed files with 292 additions and 15 deletions

View File

@ -2,7 +2,7 @@
/* Copyright (C) 2001-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2004-2013 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
* Copyright (C) 2010-2013 Juanjo Menent <jmenent@2byte.es>
* Copyright (C) 2010-2014 Juanjo Menent <jmenent@2byte.es>
* Copyright (C) 2011 Philippe Grand <philippe.grand@atoo-net.com>
*
* This program is free software; you can redistribute it and/or modify
@ -158,6 +158,31 @@ if ( ($action == 'update' && empty($_POST["cancel"]))
// Local taxes
dolibarr_set_const($db, "FACTURE_LOCAL_TAX1_OPTION",$_POST["optionlocaltax1"],'chaine',0,'',$conf->entity);
dolibarr_set_const($db, "FACTURE_LOCAL_TAX2_OPTION",$_POST["optionlocaltax2"],'chaine',0,'',$conf->entity);
if($_POST["optionlocaltax1"]=="localtax1on")
{
if(!isset($_REQUEST['lt1']))
{
dolibarr_set_const($db, "MAIN_INFO_VALUE_LOCALTAX1", 0,'chaine',0,'',$conf->entity);
}
else
{
dolibarr_set_const($db, "MAIN_INFO_VALUE_LOCALTAX1", GETPOST('lt1'),'chaine',0,'',$conf->entity);
}
dolibarr_set_const($db,"MAIN_INFO_LOCALTAX_CALC1", $_POST["clt1"],'chaine',0,'',$conf->entity);
}
if($_POST["optionlocaltax2"]=="localtax2on")
{
if(!isset($_REQUEST['lt2']))
{
dolibarr_set_const($db, "MAIN_INFO_VALUE_LOCALTAX2", 0,'chaine',0,'',$conf->entity);
}
else
{
dolibarr_set_const($db, "MAIN_INFO_VALUE_LOCALTAX2", GETPOST('lt2'),'chaine',0,'',$conf->entity);
}
dolibarr_set_const($db,"MAIN_INFO_LOCALTAX_CALC2", $_POST["clt2"],'chaine',0,'',$conf->entity);
}
if ($action != 'updateedit' && ! $error)
{
@ -570,6 +595,17 @@ if ($action == 'edit' || $action == 'updateedit')
print "<tr><td>".$langs->transcountry("LocalTax1IsUsedDesc",$mysoc->country_code)."</td></tr>";
$example=$langs->transcountry("LocalTax1IsUsedExample",$mysoc->country_code);
print ($example!="LocalTax1IsUsedExample"?"<tr><td><i>".$langs->trans("Example").': '.$langs->transcountry("LocalTax1IsUsedExample",$mysoc->country_code)."</i></td></tr>\n":"");
if(! isOnlyOneLocalTax(1))
{
print '<tr><td align="left">'.$langs->transcountry("LocalTax1Management",$mysoc->country_code).': ';
$formcompany->select_localtax(1,$conf->global->MAIN_INFO_VALUE_LOCALTAX1, "lt1");
}
print '</td></tr>';
print '<tr><td align="left">'.$langs->trans("CalcLocaltax").': ';
$opcions=array($langs->transcountry("CalcLocaltax1",$mysoc->country_code),$langs->transcountry("CalcLocaltax2",$mysoc->country_code),$langs->transcountry("CalcLocaltax3",$mysoc->country_code));
print $form->selectarray("clt1", $opcions, $conf->global->MAIN_INFO_LOCALTAX_CALC1);
print '</td></tr>';
print "</table>";
print "</td></tr>\n";
@ -603,6 +639,16 @@ if ($action == 'edit' || $action == 'updateedit')
print "<tr><td>".$langs->transcountry("LocalTax2IsUsedDesc",$mysoc->country_code)."</td></tr>";
$example=$langs->transcountry("LocalTax2IsUsedExample",$mysoc->country_code);
print ($example!="LocalTax2IsUsedExample"?"<tr><td><i>".$langs->trans("Example").': '.$langs->transcountry("LocalTax2IsUsedExample",$mysoc->country_code)."</i></td></tr>\n":"");
print '<tr><td align="left">'.$langs->transcountry("LocalTax2Management",$mysoc->country_code).': ';
if(! isOnlyOneLocalTax(2))
{
$formcompany->select_localtax(2,$conf->global->MAIN_INFO_VALUE_LOCALTAX2, "lt2");
print '</td></tr>';
}
print '<tr><td align="left">'.$langs->trans("CalcLocaltax").': ';
$opcions=array($langs->transcountry("CalcLocaltax1",$mysoc->country_code),$langs->transcountry("CalcLocaltax2",$mysoc->country_code),$langs->transcountry("CalcLocaltax3",$mysoc->country_code));
print $form->selectarray("clt2", $opcions, $conf->global->MAIN_INFO_LOCALTAX_CALC2);
print '</td></tr>';
print "</table>";
print "</td></tr>\n";
@ -954,6 +1000,19 @@ else
print "<tr><td>".$langs->transcountry("LocalTax1IsUsedDesc",$mysoc->country_code)."</td></tr>";
$example=$langs->transcountry("LocalTax1IsUsedExample",$mysoc->country_code);
print ($example!="LocalTax1IsUsedExample"?"<tr><td><i>".$langs->trans("Example").': '.$langs->transcountry("LocalTax1IsUsedExample",$mysoc->country_code)."</i></td></tr>\n":"");
if($conf->global->MAIN_INFO_VALUE_LOCALTAX1!=0){
print '<tr><td>'.$langs->transcountry("LocalTax1Management",$mysoc->country_code).': '. $conf->global->MAIN_INFO_VALUE_LOCALTAX1 .'</td></tr>';
}
print '<tr><td align="left">'.$langs->trans("CalcLocaltax").': ';
if($conf->global->MAIN_INFO_LOCALTAX_CALC1==0){
print $langs->transcountry("CalcLocaltax1",$mysoc->country_code);
}else if($conf->global->MAIN_INFO_LOCALTAX_CALC1==1){
print $langs->transcountry("CalcLocaltax2",$mysoc->country_code);
}else if($conf->global->MAIN_INFO_LOCALTAX_CALC1==2){
print $langs->transcountry("CalcLocaltax3",$mysoc->country_code);
}
print '</td></tr>';
print "</table>";
print "</td></tr>\n";
@ -987,6 +1046,19 @@ else
print "<tr><td>".$langs->transcountry("LocalTax2IsUsedDesc",$mysoc->country_code)."</td></tr>";
$example=$langs->transcountry("LocalTax2IsUsedExample",$mysoc->country_code);
print ($example!="LocalTax2IsUsedExample"?"<tr><td><i>".$langs->trans("Example").': '.$langs->transcountry("LocalTax2IsUsedExample",$mysoc->country_code)."</i></td></tr>\n":"");
if($conf->global->MAIN_INFO_VALUE_LOCALTAX2!=0){
print '<tr><td>'.$langs->transcountry("LocalTax2Management",$mysoc->country_code).': '. $conf->global->MAIN_INFO_VALUE_LOCALTAX2 .'</td></tr>';
}
print '<tr><td align="left">'.$langs->trans("CalcLocaltax").': ';
if($conf->global->MAIN_INFO_LOCALTAX_CALC2==0){
print $langs->transcountry("CalcLocaltax1",$mysoc->country_code);
}else if($conf->global->MAIN_INFO_LOCALTAX_CALC2==1){
print $langs->transcountry("CalcLocaltax2",$mysoc->country_code);
}else if($conf->global->MAIN_INFO_LOCALTAX_CALC2==2){
print $langs->transcountry("CalcLocaltax3",$mysoc->country_code);
}
print '</td></tr>';
print "</table>";
print "</td></tr>\n";

View File

@ -1,6 +1,7 @@
<?php
/* Copyright (C) 2008-2012 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2008-2012 Regis Houssin <regis.houssin@capnetworks.com>
* Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
*
* 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
@ -775,6 +776,43 @@ class FormCompany
return $out;
}
/**
* Return a select with localtax values for thirds
*
* @param int $local LocalTax
* @param int $selected Preselected value
* @param varchar $htmlname HTML select name
*/
function select_localtax($local, $selected, $htmlname)
{
$tax=get_localtax_by_third($local);
$num = $this->db->num_rows($tax);
$i = 0;
if ($num)
{
$valors=explode(":", $tax);
if(sizeof($valors)>1)
{
//montar select
print '<select class="flat" name="'.$htmlname.'">';
while($i <= (sizeof($valors))-1){
if ($selected == $valors[$i]){
print '<option value="'.$valors[$i].'" selected="selected">';
}else{
print '<option value="'.$valors[$i].'">';
}
print $valors[$i];
print '</option>';
$i++;
}
print'</select>';
}
}
}
}

View File

@ -7,7 +7,7 @@
* Copyright (C) 2004 Christophe Combelles <ccomb@free.fr>
* Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
* Copyright (C) 2008 Raphael Bertrand (Resultic) <raphael.bertrand@resultic.fr>
* Copyright (C) 2010-2011 Juanjo Menent <jmenent@2byte.es>
* Copyright (C) 2010-2014 Juanjo Menent <jmenent@2byte.es>
* Copyright (C) 2013 Cédric Salvador <csalvador@gpcsolutions.fr>
* Copyright (C) 2013 Alexandre Spangaro <alexandre.spangaro@gmail.com>
*
@ -2886,7 +2886,11 @@ function get_localtax($tva, $local, $thirdparty_buyer="", $thirdparty_seller="")
if (empty($thirdparty_seller) || ! is_object($thirdparty_seller)) $thirdparty_seller=$mysoc;
dol_syslog("get_localtax tva=".$tva." local=".$local." thirdparty_buyer id=".(is_object($thirdparty_buyer)?$thirdparty_buyer->id:'')."/country_code=".(is_object($thirdparty_buyer)?$thirdparty_buyer->country_code:'')." thirdparty_seller id=".$thirdparty_seller->id."/country_code=".$thirdparty_seller->country_code." thirdparty_seller localtax1_assuj=".$thirdparty_seller->localtax1_assuj." thirdparty_seller localtax2_assuj=".$thirdparty_seller->localtax2_assuj);
if($thirdparty_buyer->country_code!=$thirdparty_seller->country_code)
{
return 0;
}
// Some test to guess with no need to make database access
if ($mysoc->country_code == 'ES') // For spain localtaxes 1 and 2, tax is qualified if buyer use local taxe
{
@ -2898,14 +2902,14 @@ function get_localtax($tva, $local, $thirdparty_buyer="", $thirdparty_seller="")
}
else
{
if (! $thirdparty_seller->localtax1_assuj) return 0;
if (! $thirdparty_seller->localtax1_assuj) return 0;
}
}
if ($local == 2)
{
if ($thirdparty_seller->id==$mysoc->id)
if ($thirdparty_seller->id==$mysoc->id )
{
if (! $thirdparty_buyer->localtax2_assuj) return 0;
}
@ -2931,6 +2935,43 @@ function get_localtax($tva, $local, $thirdparty_buyer="", $thirdparty_seller="")
}*/
// Search local taxes
if ($local==1)
{
if($thirdparty_seller!=$mysoc )
{
if(!isOnlyOneLocalTax($local))
{
return $thirdparty_seller->localtax1_value;
}
}
else
{
if(!isOnlyOneLocalTax($local))
{
return $conf->global->MAIN_INFO_VALUE_LOCALTAX1;
}
}
}
if ($local==2)
{
if($thirdparty_seller!=$mysoc)
{
if(!isOnlyOneLocalTax($local))
{
return $thirdparty_seller->localtax2_value;
}
}
else
{
if(!isOnlyOneLocalTax($local))
{
return $conf->global->MAIN_INFO_VALUE_LOCALTAX2;
}
}
}
$sql = "SELECT t.localtax1, t.localtax2, t.localtax1_type, t.localtax2_type";
$sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_pays as p";
$sql .= " WHERE t.fk_pays = p.rowid AND p.code = '".$thirdparty_seller->country_code."'";
@ -2948,6 +2989,57 @@ function get_localtax($tva, $local, $thirdparty_buyer="", $thirdparty_seller="")
return 0;
}
/**
* Return true if LocalTax is unique
*
* @param int $local Local taxt to test
* @return boolean True if LocalTax have multiple values, False if not
*/
function isOnlyOneLocalTax($local)
{
$tax=get_localtax_by_third($local);
$valors=explode(":", $tax);
if(sizeof($valors)>1)
{
return false;
}
else
{
return true;
}
}
/**
* Get values of localtaxes
*
* @param int $local LocalTax to get
* @return number Values of localtax
*/
function get_localtax_by_third($local)
{
global $db, $mysoc;
$sql="SELECT t.localtax1, t.localtax2 ";
$sql.=" FROM ".MAIN_DB_PREFIX."c_tva as t inner join ".MAIN_DB_PREFIX."c_pays as p ON p.rowid=t.fk_pays";
$sql.=" WHERE p.code = '".$mysoc->country_code."' AND t.taux=(SELECT max(tt.taux)";
$sql.=" FROM ".MAIN_DB_PREFIX."c_tva as tt inner join ".MAIN_DB_PREFIX."c_pays as p ON p.rowid=tt.fk_pays";
$sql.= " WHERE p.code = '".$mysoc->country_code."')";
$resql=$db->query($sql);
if ($resql)
{
$obj = $db->fetch_object($resql);
if ($local==1) return $obj->localtax1;
elseif ($local==2) return $obj->localtax2;
}
return 0;
}
/**
* Get type and rate of localtaxes for a particular vat rate/country fo thirdparty
* TODO
@ -2960,7 +3052,7 @@ function get_localtax($tva, $local, $thirdparty_buyer="", $thirdparty_seller="")
* @param int $thirdparty Company object
* @return array array(localtax_type1(1-6 / 0 if not found), rate of localtax1, ...)
*/
function getLocalTaxesFromRate($vatrate, $local, $thirdparty)
function getLocalTaxesFromRate($vatrate, $local, $buyer, $seller)
{
global $db;
@ -2969,16 +3061,84 @@ function getLocalTaxesFromRate($vatrate, $local, $thirdparty)
// Search local taxes
$sql = "SELECT t.localtax1, t.localtax1_type, t.localtax2, t.localtax2_type, t.accountancy_code_sell, t.accountancy_code_buy";
$sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_pays as p";
$sql .= " WHERE t.fk_pays = p.rowid AND p.code = '".$thirdparty->country_code."'";
$sql .= " WHERE t.fk_pays = p.rowid AND p.code = '".$buyer->country_code."'";
$sql .= " AND t.taux = ".$vatrate." AND t.active = 1";
$resql=$db->query($sql);
if ($resql)
{
$obj = $db->fetch_object($resql);
if ($local == 1) return array($obj->localtax1_type, $obj->localtax1,$obj->accountancy_code_sell,$obj->accountancy_code_buy);
elseif ($local == 2) return array($obj->localtax2_type, $obj->localtax2,$obj->accountancy_code_sell,$obj->accountancy_code_buy);
else return array($obj->localtax1_type, $obj->localtax1, $obj->localtax2_type, $obj->localtax2,$obj->accountancy_code_sell,$obj->accountancy_code_buy);
if ($local == 1)
{
if (! isOnlyOneLocalTax(1))
{
return array($obj->localtax1_type, get_localtax($vatrate, $local, $buyer, $seller), $obj->accountancy_code_sell,$obj->accountancy_code_buy);
}
else
{
return array($obj->localtax1_type, $obj->localtax1,$obj->accountancy_code_sell,$obj->accountancy_code_buy);
}
}
elseif ($local == 2)
{
if (! isOnlyOneLocalTax(2))
{
return array($obj->localtax2_type, get_localtax($vatrate, $local, $buyer, $seller),$obj->accountancy_code_sell,$obj->accountancy_code_buy);
}
else
{
return array($obj->localtax2_type, $obj->localtax2,$obj->accountancy_code_sell,$obj->accountancy_code_buy);
}
}
else
{
if(! isOnlyOneLocalTax(1))
{
if(! isOnlyOneLocalTax(2))
{
return array($obj->localtax1_type, get_localtax($vatrate, 1, $buyer, $seller), $obj->localtax2_type, get_localtax($vatrate, 2, $buyer, $seller),$obj->accountancy_code_sell,$obj->accountancy_code_buy);
}
else
{
return array($obj->localtax1_type, get_localtax($vatrate, 1, $buyer, $seller), $obj->localtax2_type, $obj->localtax2,$obj->accountancy_code_sell,$obj->accountancy_code_buy);
}
}
else
{
if(! isOnlyOneLocalTax(2))
{
return array($obj->localtax1_type, $obj->localtax1, $obj->localtax2_type,get_localtax($vatrate, 2, $buyer, $seller) ,$obj->accountancy_code_sell,$obj->accountancy_code_buy);
}
else
{
return array($obj->localtax1_type, $obj->localtax1, $obj->localtax2_type, $obj->localtax2,$obj->accountancy_code_sell,$obj->accountancy_code_buy);
}
}
if(! isOnlyOneLocalTax(2))
{
if(! isOnlyOneLocalTax(1))
{
return array($obj->localtax2_type, get_localtax($vatrate, 1, $buyer, $seller), $obj->localtax2_type, get_localtax($vatrate, 2, $buyer, $seller),$obj->accountancy_code_sell,$obj->accountancy_code_buy);
}
else
{
return array($obj->localtax2_type, get_localtax($vatrate, 1, $buyer, $seller), $obj->localtax2_type, $obj->localtax2,$obj->accountancy_code_sell,$obj->accountancy_code_buy);
}
}
else
{
if(! isOnlyOneLocalTax(1))
{
return array($obj->localtax2_type, $obj->localtax2, $obj->localtax1_type,get_localtax($vatrate, 1, $buyer, $seller) ,$obj->accountancy_code_sell,$obj->accountancy_code_buy);
}
else
{
return array($obj->localtax2_type, $obj->localtax2, $obj->localtax1_type, $obj->localtax1,$obj->accountancy_code_sell,$obj->accountancy_code_buy);
}
}
}
}
return 0;

View File

@ -216,10 +216,10 @@ INSERT INTO llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active) VALUES (20
INSERT INTO llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active) VALUES (2023, 202, '0', '0', 'VAT Rate 0', 1);
-- SPAIN (id country=4)
insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,localtax1,localtax1_type,localtax2,localtax2_type,note,active) values ( 41, 4,'21','0','5.2','3','-21','1','VAT standard rate',1);
insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,localtax1,localtax1_type,localtax2,localtax2_type,note,active) values ( 42, 4,'10','0','1.4','3','-21','1','VAT reduced rate',1);
insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,localtax1,localtax1_type,localtax2,localtax2_type,note,active) values ( 43, 4, '4','0','0.5','3','-21','1','VAT super-reduced rate',1);
insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,localtax1,localtax1_type,localtax2,localtax2_type,note,active) values ( 44, 4, '0','0', '0','3','-21','1','VAT Rate 0',1);
insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,localtax1,localtax1_type,localtax2,localtax2_type,note,active) values ( 41, 4,'21','0','5.2','3','-21:-9:-2','1','VAT standard rate',1);
insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,localtax1,localtax1_type,localtax2,localtax2_type,note,active) values ( 42, 4,'10','0','1.4','3','-21:-9:-2','1','VAT reduced rate',1);
insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,localtax1,localtax1_type,localtax2,localtax2_type,note,active) values ( 43, 4, '4','0','0.5','3','-21:-9:-2','1','VAT super-reduced rate',1);
insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,localtax1,localtax1_type,localtax2,localtax2_type,note,active) values ( 44, 4, '0','0', '0','3','-21:-9:-2','1','VAT Rate 0',1);
-- SWEDEN (id country=20)
insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active) values (201,20, '25','0','VAT standard rate',1);

View File

@ -807,6 +807,13 @@ LocalTax2IsUsedDescES= The RE rate by default when creating prospects, invoices,
LocalTax2IsNotUsedDescES= By default the proposed IRPF is 0. End of rule.
LocalTax2IsUsedExampleES= In Spain, freelancers and independent professionals who provide services and companies who have chosen the tax system of modules.
LocalTax2IsNotUsedExampleES= In Spain they are bussines not subject to tax system of modules.
CalcLocaltax=Reports
CalcLocaltax1ES=Sales - Purchases
CalcLocaltax1Desc=Local Taxes reports are calculated with the difference between localtaxes sales and localtaxes purchases
CalcLocaltax2ES=Purchases
CalcLocaltax2Desc=Local Taxes reports are the total of localtaxes purchases
CalcLocaltax3ES=Sales
CalcLocaltax3Desc=Local Taxes reports are the total of localtaxes sales
LabelUsedByDefault=Label used by default if no translation can be found for code
LabelOnDocuments=Label on documents
NbOfDays=Nb of days