Fatturazione Importo netto + Tasse

  • Creatore Discussione Creatore Discussione Fabio90
  • Data di inizio Data di inizio

Fabio90

Utente Attivo
29 Feb 2012
507
0
16
Buonasera,
sto cercando di capire come poter modificare il seguente codice affichè le tasse vengano aggiunte all'importo netto e non sottratte..

PHP:
jimport('joomla.utilities.date');
		$jNow = new JDate();
		
		$gross_amount = 0;
		$net_amount= 0;
		foreach($items as $key => $item) {
			$item = (object) $item;
			$items[$key] = (object)$items[$key];
			if (isset($item->net_unit_price)) {
				$items[$key]->gross_unit_price = $item->net_unit_price * ((100 + $item->tax) / 100);
			} else if (isset($item->gross_unit_price)) {
				$items[$key]->net_unit_price = $item->gross_unit_price / ((100 + $item->tax) / 100);
			}
				
			$items[$key]->net_amount = $item->quantity * $items[$key]->net_unit_price;
			$items[$key]->gross_amount = $items[$key]->net_amount * (100 + $item->tax) / 100;
			
			$gross_amount += $items[$key]->gross_amount;
			$net_amount += $items[$key]->net_amount;
		}
		$tax_amount = $gross_amount - $net_amount;

Come posso modificare?
Grazie
 
scusa, magari ho smesso di capire ...
a me sembra che il calcolo sia corretto, le tasse sono sempre sommate al netto,
in realtà condivido poco il "codice" e la "logica" usati per calcolare netto e lordo (isset)
io avrei fatto controlli diversi e più attinenti, in ogni caso riposto il tuo script con dei commenti
PHP:
            $items[$key] = (object)$items[$key]; 

            if (isset($item->net_unit_price)) { 		// esiste ma potrebbe avere valore 0
								// nel caso calcolerebbe un lordo = 0
                $items[$key]->gross_unit_price = $item->net_unit_price * ((100 + $item->tax) / 100); 

            } else if (isset($item->gross_unit_price)) {  	// esiste ma potrebbe avere valore 0
								// nel caso calcolerebbe un netto = 0
                $items[$key]->net_unit_price = $item->gross_unit_price / ((100 + $item->tax) / 100); 
            } 
								// ciò che segue risente delle scelte precedenti
            $items[$key]->net_amount = $item->quantity * $items[$key]->net_unit_price; 
            $items[$key]->gross_amount = $items[$key]->net_amount * (100 + $item->tax) / 100; 
             
            $gross_amount += $items[$key]->gross_amount; 
            $net_amount += $items[$key]->net_amount; 
        } 
        $tax_amount = $gross_amount - $net_amount;
ciao
Marino
 
Ciao Marino,
grazie per la tua analisi. In realtà questo è il codice completo che gestisce la fatturazione:
Ricapitolando il calcolo è corretto però l'iva viene inglobata nel lordo. Quindi 4.90 - 22% = 4.02 mentre io vorrei riuscire a far si che sia aggiunta quindi 4.90 netto + IVA = 5.98

Devo cambiare qualcosa nel codice qui sotto ma non capisco dove..

vat.png


PHP:
<?php
/**
 * @package		Invoicing
 * @copyright	Copyright (C) 2010-2014 Juloa.com. All rights reserved.
 * @license GNU General Public License version 3, or later
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

include_once(JPATH_LIBRARIES.'/fof/include.php');
include_once(JPATH_ADMINISTRATOR.'/components/com_invoicing/helpers/select.php');
include_once(JPATH_ADMINISTRATOR.'/components/com_invoicing/helpers/format.php');
include_once(JPATH_ADMINISTRATOR.'/components/com_invoicing/helpers/cparams.php');
include_once(JPATH_ADMINISTRATOR.'/components/com_invoicing/helpers/mail.php');

// Merge the language overrides
$paths = array(JPATH_ADMINISTRATOR, JPATH_ROOT);
$jlang = JFactory::getLanguage();
$jlang->load("com_invoicing", $paths[0], 'en-GB', true);
$jlang->load("com_invoicing", $paths[0], null, true);
$jlang->load("com_invoicing", $paths[1], 'en-GB', true);
$jlang->load("com_invoicing", $paths[1], null, true);

class Invoicing {
	
	public static function getVendors() {
		
	}
	
    public static function formatPrice($price, $currency){
    	if ($currency == null)
    		return $price;
    	
        $number = number_format($price,$currency->number_decimals,$currency->decimal_separator,$currency->thousand_separator);    
            
        if($currency->symbol != ''){
            if ($currency->symbol_position == "after") {
                return $number."&nbsp;".$currency->symbol;
            } else {
                return $currency->symbol."&nbsp;".$number;
            }
        } else {
            $currencyLabel = $currency->code;
            if ($currency->symbol_position == "after") {
                return $number."&nbsp;".$currencyLabel;
            } else {
                return $currencyLabel."&nbsp;".$number;
            }
        }
        
		return "";
    }
    
	public static function getCurrencies($currencyId = 0) {
		if($currencyId == 0)
            return false;
        
        $db = JFactory::getDbo();
        
        $db->setQuery(" SELECT * FROM #__invoicing_currencies "
				     ." WHERE invoicing_currency_id='$currencyId'");
		$currency = $db->loadObject();
        
        return $currency;
	}
	
	public static function createInvoice($user_id,$items,$generator,$generator_key,
										 $currency_id=null,$notes=null,$vendor_id=null,
										 $language=null,$status='PENDING') {
		$db = &JFactory::getDbo();
		
		$db->setQuery(" SELECT invoicing_invoice_id,order_number FROM #__invoicing_invoices "
				     ." WHERE generator='$generator' AND generator_key='$generator_key' AND status = 'PENDING'");
		$invoice = $db->loadObject();
		if ($invoice != null) {
			$existing_invoice_id = $invoice->invoicing_invoice_id;
			$existing_order_number = $invoice->order_number;
		}else{
            $existing_invoice_id = false;
            $existing_order_number = null;
        }
		if ($existing_invoice_id != false) {
			$db->setQuery( "DELETE FROM #__invoicing_invoice_items WHERE invoice_id = $existing_invoice_id");
			$db->query();
		}
		
		jimport('joomla.utilities.date');
		$jNow = new JDate();
		
		$gross_amount = 0;
		$net_amount= 0;
		foreach($items as $key => $item) {
			$item = (object) $item;
			$items[$key] = (object)$items[$key];
			if (isset($item->net_unit_price)) {
				$items[$key]->gross_unit_price = $item->net_unit_price * ((100 + $item->tax) / 100);
			} else if (isset($item->gross_unit_price)) {
				$items[$key]->net_unit_price = $item->gross_unit_price / ((100 + $item->tax) / 100);
			}
				
			$items[$key]->net_amount = $item->quantity * $items[$key]->net_unit_price;
			$items[$key]->gross_amount = $items[$key]->net_amount * (100 + $item->tax) / 100;
			
			$gross_amount += $items[$key]->gross_amount;
			$net_amount += $items[$key]->net_amount;
		}
		$tax_amount = $gross_amount - $net_amount;
		
		$ip = $_SERVER['REMOTE_ADDR'];
		
		if ($language == null) {
			$lang = JFactory::getLanguage();
			$language = $lang->getTag();
		}
		
		if ($vendor_id == null) {
			$item = FOFModel::getTmpInstance('Vendors','InvoicingModel')
			->savestate(0)
			->getFirstItem();
			$vendor_id = $item->invoicing_vendor_id;
		}
		$vendor_id= 1;
		
		if ($currency_id == null) {
			$item = FOFModel::getTmpInstance('Currencies','InvoicingModel')
			->savestate(0)
			->getFirstItem();
			$currency_id = $item->invoicing_currency_id;
		}

		$sql = "SELECT invoicing_user_id FROM #__invoicing_users WHERE user_id = ".(int)$user_id;
		$db->setQuery($sql);
		$invoicing_user_id = $db->loadResult();
		if($invoicing_user_id == null) {
			FOFModel::getTmpInstance('Users','InvoicingModel')->createUser($user_id);
			$sql = "SELECT invoicing_user_id FROM #__invoicing_users WHERE user_id = ".(int)$user_id;
			$db->setQuery($sql);
			$invoicing_user_id = $db->loadResult();
		} 
	
        if ($existing_order_number != null) {
			$order_number = $existing_order_number;
		} else {
			$order_number = InvoicingHelperCparams::getParam('order_number_counter',1);
		}
		
        if(version_compare(JVERSION, '1.6.0', 'ge')) {
            $dateToSql = $jNow->toSql();
        }else{
            $dateToSql = $jNow->toMySql();
        }
        
		$user = JFactory::getUser();
		$invoice = (object)array(
				"order_number"    => $order_number,
				"user_id"   => $invoicing_user_id,
				"vendor_id" => $vendor_id,
				
				"status" => $status,
				
				"created_on" => $dateToSql,
				"created_by" => $user->id,
				"due_date" => $dateToSql,
				
				"notes" => $notes,
				
				"processor" => '',
				"processor_key" => '',
				
				"gross_amount" => $gross_amount,
				"tax_amount" => $tax_amount,
				"net_amount" => $net_amount,
				"gross_subamount" => $gross_amount,
				"tax_subamount" => $tax_amount,
				"net_subamount" => $net_amount,
				"gross_discount_amount" => 0,
				"net_discount_amount" => 0,
				"tax_discount_amount" => 0,
				"coupon_id" => 0,
				"discount_type" => '', 
        			"discount_value" => 0,

				"currency_id" => $currency_id,
				
				"language" => $language,
				
				"ip_address" => $ip,
				
				"generator" => $generator,
				"generator_key" => $generator_key
		);


		
		if ($existing_invoice_id != false) {
			$invoice->invoicing_invoice_id = $existing_invoice_id;
			$db->updateObject('#__invoicing_invoices', $invoice, 'invoicing_invoice_id');
			$invoice_id = $existing_invoice_id;
		} else {
			$db->insertObject('#__invoicing_invoices', $invoice);
			$invoice_id = $db->insertid();
		}
		if ($existing_order_number == null) {
			$order_number++;
			InvoicingHelperCparams::setParam($order_number,'order_number_counter');
		}
		
		$ordering = 0;
		foreach($items as $item) {		
			if ($item->name == "") {
				$item->name = $item->description;
			} 
			
			$invoiceitem = (object)array(
					"invoice_id" => $invoice_id,
					"name" => $item->name,
					"description" => $item->description,
					"quantity" => $item->quantity,
					
					"net_unit_price" => $item->net_unit_price,
					"tax" => $item->tax,
					"gross_unit_price" => $item->gross_unit_price,
					
					"gross_amount" => $item->gross_amount,
					"net_amount" => $item->net_amount,
					
					"source" => $item->source,
					"source_key" => @$item->source_type,
					
					"ordering" => $ordering);
			$ordering++;
			$db->insertObject('#__invoicing_invoice_items', $invoiceitem, 'invoicing_invoice_item_id');
		}
		
		/*if ($status == "PENDING") {	
			$invoice = FOFModel::getTmpInstance('Invoices','InvoicingModel')
			->getItem($invoice_id);
			InvoicingHelperMail::sendMailByStatus($invoice);
		}*/
		return $invoice_id;
	}
	
	function redirectToPayment($invoice_id) {
		$app = JFactory::getApplication();
        $uri	=& JURI::getInstance();
		$base	= $uri->toString( array('scheme', 'host', 'port'));
        //Le chemin doit petre complet $base.JROUTE car cette fonction peut être
        //appelé depuis une autre page que index.php (voir Buy Button)
		$app->redirect( $base.JRoute::_('index.php?option=com_invoicing&view=invoice&layout=payment&id='.$invoice_id) );
	}
}
 
nel codice che hai postato, ritrovo presente la parte del post precedente,
come dicevo il calcolo è corretto, ma ti propongo la seguente modifica,
PHP:
if (isset($item->net_unit_price)) {
	$items[$key]->net_unit_price = $item->net_unit_price;
	$items[$key]->gross_unit_price = $item->net_unit_price * ((100 + $item->tax) / 100);
} else if (isset($item->gross_unit_price)) {
	$items[$key]->gross_unit_price = $item->gross_unit_price;
	$items[$key]->net_unit_price = $item->gross_unit_price / ((100 + $item->tax) / 100);
}

se il risultato non cambia, come temo, posso solo suggerirti di controllare i dati che fornisci all'applicazione, la cui struttura ti riporto sotto,
da quello che posso capire, nei dati hai scambiato le colonne "net_unit_price" con "gross_unit_price"
PHP:
$invoiceitem = (object)array(
	"invoice_id" => $invoice_id,
	"name" => $item->name,
	"description" => $item->description,
	"quantity" => $item->quantity,
	
	"net_unit_price" => $item->net_unit_price,
	"tax" => $item->tax,
	"gross_unit_price" => $item->gross_unit_price,
	
	"gross_amount" => $item->gross_amount,
	"net_amount" => $item->net_amount,
	
	"source" => $item->source,
	"source_key" => @$item->source_type,
	
	"ordering" => $ordering);

se vuoi provare il calcolo puoi usare lo script seguente, ripreso dal calcolo originale
PHP:
<?php
$gross_unit_price = 0;		$net_unit_price = 4.90;		TaxCalc( $net_unit_price, $gross_unit_price );
$gross_unit_price = 5.98;	$net_unit_price = 0;		TaxCalc( $net_unit_price, $gross_unit_price );


function TaxCalc( $item_net_unit_price, $item_gross_unit_price )
{
	$item_tax = 22;
	$item_quantity = 1;

	if (!empty( $item_net_unit_price ))
	{
		$items_key_gross_unit_price = $item_net_unit_price * ((100 + $item_tax) / 100);
		$items_key_net_unit_price = $item_net_unit_price;
	}
	else if (!empty($item_gross_unit_price))
	{
		$items_key_net_unit_price = $item_gross_unit_price / ((100 + $item_tax) / 100);
		$items_key_gross_unit_price = $item_gross_unit_price;
	}
                
	$items_key_net_amount = $item_quantity * $items_key_net_unit_price;
	$items_key_gross_amount = $items_key_net_amount * (100 + $item_tax) / 100;

	$tax_amount = $items_key_gross_amount - $items_key_net_amount;

	echo 
"prezzo lordo : ".$item_gross_unit_price."<br />".
"prezzo netto : ".$item_net_unit_price."<br />".
"prezzo lordo calcolato : ".$items_key_gross_unit_price."<br />".
"prezzo netto calcolato : ".$items_key_net_unit_price."<br />".
"tasse calcolate : ".$tax_amount."<br /><br />";
}
?>
fai sapere, ciao
Marino
 
ciao forse dirò una stupidata ma prova a cambiare la sottrazione con l'addizione
PHP:
$tax_amount = $gross_amount - $net_amount;
//con questa
$tax_amount = $gross_amount + $net_amount;
 
ti posto il risultato del tuo esempio e la motivazione per cui ho scritto che hai scambiato le "colonne"
prezzo lordo : 0
prezzo netto : 4.9
prezzo lordo calcolato : 5.978
prezzo netto calcolato : 4.9
tasse calcolate : 1.078

prezzo lordo : 5.98
prezzo netto : 0
prezzo lordo calcolato : 5.98
prezzo netto calcolato : 4.9016393442623
tasse calcolate : 1.0783606557377
quando hai verificato, ricorda che esiste anche un problema di arrotondamenti .....
ciao
Marino
 

Discussioni simili