Browse Source

ReductionCredit

feature/export_comptable
Guillaume 4 years ago
parent
commit
7204653796
2 changed files with 160 additions and 118 deletions
  1. +11
    -0
      ShopBundle/Model/OrderShop.php
  2. +149
    -118
      ShopBundle/Services/PriceUtils.php

+ 11
- 0
ShopBundle/Model/OrderShop.php View File

@@ -475,6 +475,17 @@ abstract class OrderShop extends AbstractEntity implements FilterMerchantInterfa
return $this;
}

public function hasOrderReductionCartFreeShipping()
{
foreach($this->getOrderReductionCarts() as $orderReductionCart) {
if($orderReductionCart->getFreeShipping()) {
return true ;
}
}

return false ;
}

/**
* @return Collection|Document[]
*/

+ 149
- 118
ShopBundle/Services/PriceUtils.php View File

@@ -1,6 +1,6 @@
<?php

namespace Lc\ShopBundle\Services ;
namespace Lc\ShopBundle\Services;

use Doctrine\Common\Collections\Collection;
use Lc\ShopBundle\Context\OrderProductInterface;
@@ -15,22 +15,21 @@ class PriceUtils

public function getPrice($entity)
{
if($entity instanceof ProductPropertyInterface) {
if($entity->getBehaviorPriceInherited() == 'by-piece') {
return $entity->getPriceInherited() ;
}
elseif($entity->getBehaviorPriceInherited() == 'by-reference-unit') {
if($entity->getQuantityInherited() > 0) {
return $entity->getPriceByRefUnitInherited() * ($entity->getQuantityInherited() / $entity->getUnitInherited()->getCoefficient()) ;
if ($entity instanceof ProductPropertyInterface) {
if ($entity->getBehaviorPriceInherited() == 'by-piece') {
return $entity->getPriceInherited();
} elseif ($entity->getBehaviorPriceInherited() == 'by-reference-unit') {
if ($entity->getQuantityInherited() > 0) {
return $entity->getPriceByRefUnitInherited() * ($entity->getQuantityInherited() / $entity->getUnitInherited()->getCoefficient());
}
}
}

if($entity instanceof OrderProductInterface) {
return $entity->getPrice() ;
if ($entity instanceof OrderProductInterface) {
return $entity->getPrice();
}

return null ;
return null;
}

public function getPriceWithTax($entity)
@@ -38,7 +37,7 @@ class PriceUtils
return $this->applyTax(
$this->getPrice($entity),
$entity->getTaxRateInherited()->getValue()
) ;
);
}

public function getPriceWithTaxAndReduction($entity)
@@ -52,20 +51,19 @@ class PriceUtils

public function getPriceByRefUnit($entity)
{
if($entity instanceof ProductPropertyInterface) {
if($entity->getBehaviorPriceInherited() == 'by-piece') {
return ($this->getPrice($entity) * $entity->getUnitInherited()->getCoefficient()) / $entity->getQuantityInherited() ;
}
elseif($entity->getBehaviorPriceInherited() == 'by-reference-unit') {
return $entity->getPriceByRefUnitInherited() ;
if ($entity instanceof ProductPropertyInterface) {
if ($entity->getBehaviorPriceInherited() == 'by-piece') {
return ($this->getPrice($entity) * $entity->getUnitInherited()->getCoefficient()) / $entity->getQuantityInherited();
} elseif ($entity->getBehaviorPriceInherited() == 'by-reference-unit') {
return $entity->getPriceByRefUnitInherited();
}
}

if($entity instanceof OrderProductInterface) {
return ($this->getPrice($entity) * $entity->getUnitInherited()->getCoefficient()) / $entity->getQuantityProduct() ;
if ($entity instanceof OrderProductInterface) {
return ($this->getPrice($entity) * $entity->getUnitInherited()->getCoefficient()) / $entity->getQuantityProduct();
}

return null ;
return null;
}

public function getPriceByRefUnitWithTax($entity)
@@ -73,7 +71,7 @@ class PriceUtils
return $this->applyTax(
$this->getPriceByRefUnit($entity),
$entity->getTaxRateInherited()->getValue()
) ;
);
}

public function getPriceByRefUnitWithTaxAndReduction($entity)
@@ -87,169 +85,203 @@ class PriceUtils

public function getTotal($entity)
{
if($entity instanceof OrderProductInterface) {
return $entity->getQuantityOrder() * $this->getPrice($entity) ;
if ($entity instanceof OrderProductInterface) {
return $entity->getQuantityOrder() * $this->getPrice($entity);
}
if($entity instanceof OrderShopInterface) {
$total = 0 ;
foreach($entity->getOrderProducts() as $orderProduct) {
$total += $this->getTotal($orderProduct) ;
if ($entity instanceof OrderShopInterface) {
$total = 0;
foreach ($entity->getOrderProducts() as $orderProduct) {
$total += $this->getTotal($orderProduct);
}
return $total ;
return $total;
}
return null ;
return null;
}

public function getTotalWithTax($entity)
{
if($entity instanceof OrderProductInterface) {
if ($entity instanceof OrderProductInterface) {
return $this->applyTax(
$this->getTotal($entity),
$entity->getTaxRateInherited()->getValue()
) ;
);
}
if($entity instanceof OrderShopInterface) {
$total = 0 ;
foreach($entity->getOrderProducts() as $orderProduct) {
$total += $this->getTotalWithTax($orderProduct) ;
if ($entity instanceof OrderShopInterface) {
$total = 0;
foreach ($entity->getOrderProducts() as $orderProduct) {
$total += $this->getTotalWithTax($orderProduct);
}
return $total ;
return $total;
}

//C'est bizzare ce truc là
//if($entity instanceof OrderShopInterface) {
// return $this->getTotalOrderProducts($entity->getOrderProducts(), true) ;
//}
return null ;
return null;
}

public function getTotalWithTaxAndReduction($entity)
{
if($entity instanceof OrderProductInterface) {
if ($entity instanceof OrderProductInterface) {
return $this->getPriceWithTaxAndReductionCatalog(
$entity,
$this->getTotal($entity),
$this->getTotalWithTax($entity)
) ;
);
}

if($entity instanceof OrderShopInterface) {
return $this->getTotalOrderProductsWithTaxAndReduction($entity->getOrderProducts(), true, true) ;
if ($entity instanceof OrderShopInterface) {
return $this->getTotalOrderProductsWithTaxAndReduction($entity->getOrderProducts(), true, true);
}
}

public function getTotalWithReduction($entity)
{
if($entity instanceof OrderProductInterface) {
if ($entity instanceof OrderProductInterface) {
return $this->getPriceWithReductionCatalog(
$entity,
$this->getTotal($entity),
$this->getTotalWithTax($entity)
) ;
);
}

if($entity instanceof OrderShopInterface) {
return $this->getTotalOrderProductsWithReduction($entity->getOrderProducts(), true, true) ;
if ($entity instanceof OrderShopInterface) {
return $this->getTotalOrderProductsWithReduction($entity->getOrderProducts(), true, true);
}
}

public function getTotalOrderProductsWithReductionCart(OrderShopInterface $order)
{
$this->_getTotalOrderProductsWithReductionCartGeneric($order, false);
}

public function getTotalOrderProductsWithTaxAndReductionCart(OrderShopInterface $order)
{
$this->_getTotalOrderProductsWithReductionCartGeneric($order, true);
}

private function _getTotalOrderProductsWithReductionCartGeneric(OrderShopInterface $order, $withTax = true)
{

if ($withTax) {
$priceToReturn = $this->getTotalOrderProductsWithTaxAndReductionCatalog();
}
else {
$priceToReturn = $this->getTotalOrderProductsWithReductionCatalog();
}
foreach ($order->getOrderReductionCarts() as $orderReductionCart) {
if ($orderReductionCart->getUnit() == 'percent') {

$priceToReturn = $this->applyReductionPercent(
$priceToReturn,
$orderReductionCart->getValue
);
} else if ($orderReductionCart->getUnit() == 'amount') {
if ($orderReductionCart->getBehaviorTaxRate() == 'tax-inlcluded') {
$priceToReturn =
$this->applyReductionAmount(
$priceToReturn,
$orderReductionCart->getValue()
);
}
}
}
}


public function getTotalOrderProducts($entity)
{
return $this->getSumOrderProductsDispatch($entity) ;
return $this->getSumOrderProductsDispatch($entity);
}

public function getTotalOrderProductsWithTax($entity)
{
return $this->getSumOrderProductsDispatch($entity, true) ;
return $this->getSumOrderProductsDispatch($entity, true);
}

public function getTotalOrderProductsWithReduction($entity)
public function getTotalOrderProductsWithReductionCatalog($entity)
{
return $this->getSumOrderProductsDispatch($entity, false, true) ;
return $this->getSumOrderProductsDispatch($entity, false, true);
}

public function getTotalOrderProductsWithTaxAndReduction($entity)
public function getTotalOrderProductsWithTaxAndReductionCatalog($entity)
{
return $this->getSumOrderProductsDispatch($entity, true, true) ;
return $this->getSumOrderProductsDispatch($entity, true, true);
}

public function getSumOrderProductsDispatch($entity, $withTax = false, $withReduction = false)
public function getSumOrderProductsDispatch($entity, $withTax = false, $withReductionCatalog = false)
{
if($entity instanceof OrderShopInterface) {
return $this->getSumOrderProducts($entity->getOrderProducts(), $withTax, $withReduction) ;
if ($entity instanceof OrderShopInterface) {
return $this->getSumOrderProducts($entity->getOrderProducts(), $withTax, $withReductionCatalog);
}
if($entity instanceof Collection || is_array($entity)) {
return $this->getSumOrderProducts($entity, $withTax, $withReduction) ;
if ($entity instanceof Collection || is_array($entity)) {
return $this->getSumOrderProducts($entity, $withTax, $withReductionCatalog);
}
}

public function getSumOrderProducts($orderProducts, $withTax = false, $withReduction = false)
public function getSumOrderProducts($orderProducts, $withTax = false, $withReductionCatalog = false)
{
$total = 0 ;
foreach($orderProducts as $orderProduct) {
if($withTax && $withReduction) {
$total += $this->getTotalWithTaxAndReduction($orderProduct) ;
}
elseif($withTax) {
$total += $this->getTotalWithTax($orderProduct) ;
}
elseif($withReduction) {
$total += $this->getTotalWithReduction($orderProduct) ;
}
else {
$total += $this->getTotal($orderProduct) ;
$total = 0;
foreach ($orderProducts as $orderProduct) {
if ($withTax && $withReductionCatalog) {
$total += $this->getTotalWithTaxAndReduction($orderProduct);
} elseif ($withTax) {
$total += $this->getTotalWithTax($orderProduct);
} elseif ($withReductionCatalog) {
$total += $this->getTotalWithReduction($orderProduct);
} else {
$total += $this->getTotal($orderProduct);
}
}
return $total ;
return $total;
}

public function getPriceWithTaxAndReductionCatalog($entity, $price, $priceWithTax, $reductionCatalog = null)
{
return $this->getPriceWithReductionCatalogGeneric($entity, $price, $priceWithTax, $reductionCatalog, true) ;
return $this->getPriceWithReductionCatalogGeneric($entity, $price, $priceWithTax, $reductionCatalog, true);
}

public function getPriceWithReductionCatalog($entity, $price, $priceWithTax, $reductionCatalog = null)
{
return $this->getPriceWithReductionCatalogGeneric($entity, $price, $priceWithTax, $reductionCatalog, false) ;
return $this->getPriceWithReductionCatalogGeneric($entity, $price, $priceWithTax, $reductionCatalog, false);
}

public function getPriceWithReductionCatalogGeneric($entity, $price, $priceWithTax, $reductionCatalog = null, $withTax = true): ?float
{
if($reductionCatalog) {
$reductionCatalogValue = $reductionCatalog->getValue() ;
$reductionCatalogUnit = $reductionCatalog->getUnit() ;
$reductionCatalogBehaviorTaxRate = $reductionCatalog->getBehaviorTaxRate() ;
}
else {
if($entity instanceof ProductPropertyInterface) {
$reductionCatalog = $entity->getReductionCatalogInherited() ;

if($reductionCatalog) {
$reductionCatalogValue = $reductionCatalog->getValue() ;
$reductionCatalogUnit = $reductionCatalog->getUnit() ;
$reductionCatalogBehaviorTaxRate = $reductionCatalog->getBehaviorTaxRate() ;
if ($reductionCatalog) {
$reductionCatalogValue = $reductionCatalog->getValue();
$reductionCatalogUnit = $reductionCatalog->getUnit();
$reductionCatalogBehaviorTaxRate = $reductionCatalog->getBehaviorTaxRate();
} else {
if ($entity instanceof ProductPropertyInterface) {
$reductionCatalog = $entity->getReductionCatalogInherited();

if ($reductionCatalog) {
$reductionCatalogValue = $reductionCatalog->getValue();
$reductionCatalogUnit = $reductionCatalog->getUnit();
$reductionCatalogBehaviorTaxRate = $reductionCatalog->getBehaviorTaxRate();
}
}

if($entity instanceof OrderProductInterface) {
$orderProductReductionCatalog = $entity->getOrderProductReductionCatalog() ;
if($orderProductReductionCatalog) {
$reductionCatalogValue = $orderProductReductionCatalog->getValue() ;
$reductionCatalogUnit = $orderProductReductionCatalog->getUnit() ;
$reductionCatalogBehaviorTaxRate = $orderProductReductionCatalog->getBehaviorTaxRate() ;
if ($entity instanceof OrderProductInterface) {
$orderProductReductionCatalog = $entity->getOrderProductReductionCatalog();
if ($orderProductReductionCatalog) {
$reductionCatalogValue = $orderProductReductionCatalog->getValue();
$reductionCatalogUnit = $orderProductReductionCatalog->getUnit();
$reductionCatalogBehaviorTaxRate = $orderProductReductionCatalog->getBehaviorTaxRate();
}
}
}

if(isset($reductionCatalogValue) && isset($reductionCatalogUnit) && isset($reductionCatalogBehaviorTaxRate)) {
if (isset($reductionCatalogValue) && isset($reductionCatalogUnit) && isset($reductionCatalogBehaviorTaxRate)) {
if ($reductionCatalogUnit == 'percent') {
$priceWithTax = $this->applyReductionPercent(
$priceWithTax,
$reductionCatalogValue
);
}
elseif ($reductionCatalogUnit == 'amount') {
if($reductionCatalogBehaviorTaxRate == 'tax-excluded') {
} elseif ($reductionCatalogUnit == 'amount') {
if ($reductionCatalogBehaviorTaxRate == 'tax-excluded') {
$priceWithTax = $this->applyTax(
$this->applyReductionAmount(
$price,
@@ -257,8 +289,7 @@ class PriceUtils
),
$entity->getTaxRateInherited()->getValue()
);
}
elseif($reductionCatalogBehaviorTaxRate == 'tax-included') {
} elseif ($reductionCatalogBehaviorTaxRate == 'tax-included') {
$priceWithTax = $this->applyReductionAmount(
$priceWithTax,
$reductionCatalogValue
@@ -267,56 +298,55 @@ class PriceUtils
}
}

if($withTax) {
$priceReturn = $priceWithTax ;
}
else {
$priceReturn = $this->applyPercentNegative($priceWithTax, $entity->getTaxRateInherited()->getValue()) ;
if ($withTax) {
$priceReturn = $priceWithTax;
} else {
$priceReturn = $this->applyPercentNegative($priceWithTax, $entity->getTaxRateInherited()->getValue());
}

return $this->round($priceReturn) ;
return $this->round($priceReturn);
}

public function getTotalTaxes($entity)
{
if($entity instanceof OrderProductInterface) {
return $this->getTotalWithReduction($entity) * ($entity->getTaxRateInherited()->getValue() / 100) ;
if ($entity instanceof OrderProductInterface) {
return $this->getTotalWithReduction($entity) * ($entity->getTaxRateInherited()->getValue() / 100);
}

if($entity instanceof OrderShopInterface) {
$totalTaxes = 0 ;
foreach($entity->getOrderProducts() as $orderProduct) {
$totalTaxes += $this->getTotalTaxes($orderProduct) ;
if ($entity instanceof OrderShopInterface) {
$totalTaxes = 0;
foreach ($entity->getOrderProducts() as $orderProduct) {
$totalTaxes += $this->getTotalTaxes($orderProduct);
}
return $totalTaxes ;
return $totalTaxes;
}

return 0 ;
return 0;
}

public function applyTax($price, $taxRateValue)
{
return $this->round($this->applyPercent($price, $taxRateValue)) ;
return $this->round($this->applyPercent($price, $taxRateValue));
}

public function applyReductionPercent($price, $percentage)
{
return $this->applyPercent($price, -$percentage) ;
return $this->applyPercent($price, -$percentage);
}

public function applyReductionAmount($price, $amount)
{
return $price - $amount ;
return $price - $amount;
}

public function applyPercent($price, $percentage)
{
return $price * ($percentage / 100 + 1) ;
return $price * ($percentage / 100 + 1);
}

public function applyPercentNegative($price, $percentage)
{
return $price / ($percentage / 100 + 1) ;
return $price / ($percentage / 100 + 1);
}

public function round($price)
@@ -325,3 +355,4 @@ class PriceUtils
}

}


Loading…
Cancel
Save