Récupérer le CA hors taxe

  • Posts: 24
  • Thank you received: 0
  • Hikashop Multisite
1 year 2 months ago #354885

-- HikaShop version -- : 4.7.5
-- Joomla version -- : 4.3.4
-- PHP version -- : 8.2

Bonjour,

Je suis actuellement en train de développer un module complémentaire pour Hikashop afin de réaliser des exports clients sur mesure, au format CSV. Cependant, je rencontre des difficultés pour récupérer le chiffre d'affaires hors taxe d'un client sur une période donnée. Pourriez-vous m'indiquer s'il existe des méthodes déjà mises en place qui pourraient faciliter cette tâche et m'indiquer comment les utiliser ?

Cordialement

Last edit: 10 months 1 week ago by lapinrouge.

Please Log in or Create an account to join the conversation.

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
1 year 2 months ago #354892

Bonjour,

Lorsque HikaShop fourni les montants hors taxes pour les commandes (dans le gestionnaire des taux de taxes par exemple), il calcule dynamiquement cette valeur.
Vous pouvez vous inspirez du code dans la fonction listing du fichier administrator/components/com_hikashop/views/tax/view.html.php :

		//load the taxes for the orders
		$database->setQuery('SELECT order_tax_info, order_currency_id, order_discount_price, order_discount_tax, order_full_price, order_number FROM '.hikashop_table('order').$filters_txt);
		$orders_taxes = $database->loadObjectList();

		//load the currencies
		$config = hikashop_config();
		$main_currency = $config->get('main_currency');
		$currencyClass = hikashop_get('class.currency');
		$currency_ids = array($main_currency=>$main_currency);
		$currencies = array();
		if(count($orders_taxes)){
			foreach($orders_taxes as $k => $v){
				$currency_ids[$v->order_currency_id] = $v->order_currency_id;
			}
		}
		$null = null;
		$currencies = $currencyClass->getCurrencies($currency_ids,$null);

		//convert the currency of the tax amounts if not in the main currency of the store and round the tax amounts
		if(count($orders_taxes)){
			foreach($orders_taxes as $k => $v){
				$orders_taxes[$k]->order_tax_info = hikashop_unserialize($v->order_tax_info);
				$info =& $orders_taxes[$k]->order_tax_info;
				if(!$info) continue;

				foreach($info as $k2 => $taxes_info){
					$tax_amount = $taxes_info->tax_amount;
					if(!isset($taxes_info->tax_rate)) {
						if(!isset($rows[$taxes_info->tax_namekey])) {
							if(!empty($taxes_info->tax_namekey))
								$app->enqueueMessage(JText::sprintf('THE_ORDER_X_HAS_A_TAX_RATE_WHICH_COULD_NOT_BE_FOUND', $v->order_number, $taxes_info->tax_namekey));
							continue;
						}
						$taxes_info->tax_rate = $rows[$taxes_info->tax_namekey]->tax_rate;
					}
					// for the amount we either take:
					//	- the pre calculated amount
					//	- we recalculate it from the tax amount and the tax rate when the tax rate is not 0
					//	- we use the order full price when the order doesn't have other tax rates and the tax rate is 0
					//	- we set it to 0 in other cases
					if(isset($taxes_info->amount)) {
						$info[$k2]->amount = $taxes_info->amount;
					} else {
						if($taxes_info->tax_rate != 0)
							$info[$k2]->amount = $currencyClass->round($tax_amount/$taxes_info->tax_rate,$currencyClass->getRounding($v->order_currency_id));
						elseif(count($info) == 1)
							$info[$k2]->amount = $v->order_full_price;
						else
							$info[$k2]->amount = 0;
					}
					$info[$k2]->tax_amount = $currencyClass->round($tax_amount,$currencyClass->getRounding($v->order_currency_id));
					if($main_currency!=$v->order_currency_id){
						$info[$k2]->tax_amount_main_currency = $currencyClass->convertUniquePrice($info[$k2]->tax_amount,$v->order_currency_id,$main_currency);
						$info[$k2]->amount_main_currency = $currencyClass->convertUniquePrice($info[$k2]->amount,$v->order_currency_id,$main_currency);
					}else{
						$info[$k2]->tax_amount_main_currency = $info[$k2]->tax_amount;
						$info[$k2]->amount_main_currency = $info[$k2]->amount;
					}
				}
			}
		}

		if($pageInfo->elements->page){
			foreach($rows as $k => $tax){
				//fill the taxes with the tax amounts for each currency
				$tax_amounts = array();
				$amounts = array();
				foreach($currencies as $currency_id => $currency){
					$tax_amount = 0;
					$amount = 0;
					if(count($orders_taxes)){
						foreach($orders_taxes as $order_taxes){
							if($order_taxes->order_currency_id != $currency_id || !$order_taxes->order_tax_info) continue;
							foreach($order_taxes->order_tax_info as $order_tax){
								if($order_tax->tax_namekey != $tax->tax_namekey) continue;
								$tax_amount += $order_tax->tax_amount;
								$amount += $order_tax->amount;
							}
						}
					}
					$tax_amounts[$currency_id] = $tax_amount;
					$amounts[$currency_id] = $amount;
				}
				$rows[$k]->tax_amounts = $tax_amounts;
				$rows[$k]->amounts = $amounts;

				//fill the taxes with the tax amounts converted in the main currency
				$tax_amount_main_currency = 0;
				$amount_main_currency = 0;
				if(count($orders_taxes)){
					foreach($orders_taxes as $order_taxes){
						if(!$order_taxes->order_tax_info) continue;
						foreach($order_taxes->order_tax_info as $order_tax){
							if($order_tax->tax_namekey != $tax->tax_namekey) continue;
							$tax_amount_main_currency += $order_tax->tax_amount_main_currency;
							$amount_main_currency += $order_tax->amount_main_currency;
						}
					}
				}
				$rows[$k]->tax_amount = $tax_amount_main_currency;
				$rows[$k]->amount = $amount_main_currency;
			}
		}
Comme vous pouvez le voir, il faut récupérer le order_full_price et le order_tax_info de chaque commande.
Le order_full_price contient directement le montant total TTC.
Et le order_tax_info contient les données sérializées des taxes appliquées à la commande.
A partir d'elles, il est possible de récupérer le montant total des taxes (il peut y avoir plusieurs taux des taxes pour une même commande et différentes devises pour différentes commandes donc le code ci-dessus est surement plus complexe que ce dont vous avez besoin) et du coup de soustraire ce montant au total TTC pour obtenir le total HT et ensuite il ne reste qu'à faire la somme pour obtenir le CA hors taxes.

Last edit: 1 year 2 months ago by nicolas.
The following user(s) said Thank You: lapinrouge

Please Log in or Create an account to join the conversation.

Time to create page: 0.055 seconds
Powered by Kunena Forum