Qual: Add phpunit test before working on different rounding modes

This commit is contained in:
Laurent Destailleur 2012-01-11 16:35:12 +01:00
parent c773e25c21
commit a57295cc73
2 changed files with 127 additions and 75 deletions

View File

@ -3070,9 +3070,10 @@ class Facture extends CommonObject
* Used to build previews or test instances. * Used to build previews or test instances.
* id must be 0 if object instance is a specimen. * id must be 0 if object instance is a specimen.
* *
* @param string $option ''=Create a specimen invoice with lines, 'nolines'=No lines
* @return void * @return void
*/ */
function initAsSpecimen() function initAsSpecimen($option='')
{ {
global $user,$langs,$conf; global $user,$langs,$conf;
@ -3110,82 +3111,86 @@ class Facture extends CommonObject
$this->mode_reglement_code = 'CHQ'; $this->mode_reglement_code = 'CHQ';
$this->note_public='This is a comment (public)'; $this->note_public='This is a comment (public)';
$this->note='This is a comment (private)'; $this->note='This is a comment (private)';
// Lines
$nbp = 5; if (empty($option) || $option != 'nolines')
$xnbp = 0;
while ($xnbp < $nbp)
{ {
// Lines
$nbp = 5;
$xnbp = 0;
while ($xnbp < $nbp)
{
$line=new FactureLigne($this->db);
$line->desc=$langs->trans("Description")." ".$xnbp;
$line->qty=1;
$line->subprice=100;
//$line->price=100;
$line->tva_tx=19.6;
$line->localtax1_tx=0;
$line->localtax2_tx=0;
$line->remise_percent=0;
if ($xnbp == 1) // Qty is negative (product line)
{
$prodid = rand(1, $num_prods);
$line->fk_product=$prodids[$prodid];
$line->qty=-1;
$line->total_ht=-100;
$line->total_ttc=-119.6;
$line->total_tva=-19.6;
}
else if ($xnbp == 2) // UP is negative (free line)
{
$line->subprice=-100;
$line->total_ht=-100;
$line->total_ttc=-119.6;
$line->total_tva=-19.6;
$line->remise_percent=0;
}
else if ($xnbp == 3) // Discount is 50% (product line)
{
$prodid = rand(1, $num_prods);
$line->fk_product=$prodids[$prodid];
$line->total_ht=50;
$line->total_ttc=59.8;
$line->total_tva=9.8;
$line->remise_percent=50;
}
else // (product line)
{
$prodid = rand(1, $num_prods);
$line->fk_product=$prodids[$prodid];
$line->total_ht=100;
$line->total_ttc=119.6;
$line->total_tva=19.6;
$line->remise_percent=00;
}
$this->lines[$xnbp]=$line;
$xnbp++;
$this->total_ht += $line->total_ht;
$this->total_tva += $line->total_tva;
$this->total_ttc += $line->total_ttc;
}
// Add a line "offered"
$line=new FactureLigne($this->db); $line=new FactureLigne($this->db);
$line->desc=$langs->trans("Description")." ".$xnbp; $line->desc=$langs->trans("Description")." (offered line)";
$line->qty=1; $line->qty=1;
$line->subprice=100; $line->subprice=100;
//$line->price=100; //$line->price=100;
$line->tva_tx=19.6; $line->tva_tx=19.6;
$line->localtax1_tx=0; $line->localtax1_tx=0;
$line->localtax2_tx=0; $line->localtax2_tx=0;
$line->remise_percent=0; $line->remise_percent=100;
if ($xnbp == 1) // Qty is negative (product line) $line->total_ht=0;
{ $line->total_ttc=0; // 90 * 1.196
$prodid = rand(1, $num_prods); $line->total_tva=0;
$line->fk_product=$prodids[$prodid]; $prodid = rand(1, $num_prods);
$line->qty=-1; $line->fk_product=$prodids[$prodid];
$line->total_ht=-100;
$line->total_ttc=-119.6;
$line->total_tva=-19.6;
}
else if ($xnbp == 2) // UP is negative (free line)
{
$line->subprice=-100;
$line->total_ht=-100;
$line->total_ttc=-119.6;
$line->total_tva=-19.6;
$line->remise_percent=0;
}
else if ($xnbp == 3) // Discount is 50% (product line)
{
$prodid = rand(1, $num_prods);
$line->fk_product=$prodids[$prodid];
$line->total_ht=50;
$line->total_ttc=59.8;
$line->total_tva=9.8;
$line->remise_percent=50;
}
else // (product line)
{
$prodid = rand(1, $num_prods);
$line->fk_product=$prodids[$prodid];
$line->total_ht=100;
$line->total_ttc=119.6;
$line->total_tva=19.6;
$line->remise_percent=00;
}
$this->lines[$xnbp]=$line; $this->lines[$xnbp]=$line;
$xnbp++; $xnbp++;
$this->total_ht += $line->total_ht;
$this->total_tva += $line->total_tva;
$this->total_ttc += $line->total_ttc;
} }
// Add a line "offered"
$line=new FactureLigne($this->db);
$line->desc=$langs->trans("Description")." (offered line)";
$line->qty=1;
$line->subprice=100;
//$line->price=100;
$line->tva_tx=19.6;
$line->localtax1_tx=0;
$line->localtax2_tx=0;
$line->remise_percent=100;
$line->total_ht=0;
$line->total_ttc=0; // 90 * 1.196
$line->total_tva=0;
$prodid = rand(1, $num_prods);
$line->fk_product=$prodids[$prodid];
$this->lines[$xnbp]=$line;
$xnbp++;
} }
/** /**

View File

@ -28,17 +28,15 @@ global $conf,$user,$langs,$db;
require_once 'PHPUnit/Autoload.php'; require_once 'PHPUnit/Autoload.php';
require_once dirname(__FILE__).'/../../htdocs/master.inc.php'; require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
require_once dirname(__FILE__).'/../../htdocs/core/lib/price.lib.php'; require_once dirname(__FILE__).'/../../htdocs/core/lib/price.lib.php';
require_once dirname(__FILE__).'/../../htdocs/compta/facture/class/facture.class.php';
if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER','1'); if (empty($user->id))
if (! defined('NOREQUIREDB')) define('NOREQUIREDB','1'); {
if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1'); print "Load permissions for admin user nb 1\n";
if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1'); $user->fetch(1);
if (! defined('NOCSRFCHECK')) define('NOCSRFCHECK','1'); $user->getrights();
if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL','1'); }
if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1'); // If there is no menu to show $conf->global->MAIN_DISABLE_ALL_MAILS=1;
if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1'); // If we don't need to load the html.form.class.php
if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1');
if (! defined("NOLOGIN")) define("NOLOGIN",'1'); // If this page is public (can be called outside logged session)
/** /**
@ -139,5 +137,54 @@ class PricesTest extends PHPUnit_Framework_TestCase
return true; return true;
} }
/**
* Test function addline and update_price
*
* @return boolean
* @see http://wiki.dolibarr.org/index.php/Draft:VAT_calculation_and_rounding#Standard_usage
*/
public function testUpdatePrice()
{
//$this->sharedFixture
global $conf,$user,$langs,$db;
$this->savconf=$conf;
$this->savuser=$user;
$this->savlangs=$langs;
$this->savdb=$db;
// Two lines of 1.24 give 2.48 HT and 2.72 TTC with standard vat rounding mode
$localobject=new Facture($this->savdb);
$localobject->initAsSpecimen('nolines');
$invoiceid=$localobject->create($user);
$localobject->addline($invoiceid,'Desc',1.24,1,10,0,0,0,0,'','',0,0,0,'HT');
$localobject->addline($invoiceid,'Desc',1.24,1,10,0,0,0,0,'','',0,0,0,'HT');
$newlocalobject=new Facture($this->savdb);
$newlocalobject->fetch($invoiceid);
$this->assertEquals(2.48,$newlocalobject->total_ht);
$this->assertEquals(0.24,$newlocalobject->total_tva);
$this->assertEquals(2.72,$newlocalobject->total_ttc);
// Two lines of 1.24 give 2.48 HT and 2.73 TTC with global vat rounding mode
$localobject=new Facture($this->savdb);
$localobject->initAsSpecimen('nolines');
$invoiceid=$localobject->create($user);
$localobject->addline($invoiceid,'Desc',1.24,1,10,0,0,0,0,'','',0,0,0,'HT');
$localobject->addline($invoiceid,'Desc',1.24,1,10,0,0,0,0,'','',0,0,0,'HT');
$newlocalobject=new Facture($this->savdb);
$newlocalobject->fetch($invoiceid);
$this->assertEquals(2.48,$newlocalobject->total_ht);
//$this->assertEquals(0.25,$newlocalobject->total_tva);
//$this->assertEquals(2.73,$newlocalobject->total_ttc);
}
} }
?> ?>