Deprecated : Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 58
Deprecated : Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler
<?php
namespace App\Service ;
use App\Entity\BaseInvoice ;
use App\Entity\Batch ;
use App\Entity\Booking ;
use App\Entity\DeliveryNote ;
use App\Entity\Invoice ;
use App\Entity\InvoiceCredit ;
use App\Entity\InvoiceRowType ;
use App\Entity\Payment ;
use App\Entity\Shipment ;
use App\Entity\SupportRequest ;
use App\Entity\User ;
use App\Twig\FormatExtension ;
use DateTime ;
use DateTimeInterface ;
use Doctrine\Common\Collections\Collection ;
use Doctrine\ORM\EntityManagerInterface ;
use Exception ;
use Symfony\Component\HttpFoundation\JsonResponse ;
use Symfony\Component\HttpFoundation\Response ;
use Symfony\Component\Serializer\Exception\ExceptionInterface ;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer ;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer ;
use Symfony\Component\Serializer\SerializerInterface ;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface ;
use Symfony\Contracts\HttpClient\HttpClientInterface ;
use Throwable ;
use App\Service\Auctionis\InvoiceService as AuctionisInvoiceService ;
class InvoiceService
{
public const REF_GUARDCHARGE = 'GARD' ;
public const REF_EXPEDITION = 'EXP' ;
public const REF_INTERENCHERES = 'IE' ;
public const REMOTE_INVOICING = [
BrandService :: CODE_NPCJ => BrandService :: CODE_ADN
];
private float $vat ;
public function __construct (
private GuardChargeService $guardChargeService ,
private BrandService $brandService ,
private EntityManagerInterface $em ,
private SerializerInterface $serializer ,
private AuctionisInvoiceService $auctionisInvoiceService ,
)
{
$this -> vat = 100 * ( StripeService :: VAT - 1 );
}
public function getNoteItems ( DeliveryNote $deliveryNote ): array
{
$items = array();
/** @var Batch $batch */
foreach ( $deliveryNote -> getBatches () as $batch ) {
$items [] = array(
'reference' => $batch -> getBarcode (),
'designation' => $batch -> getDesignation (),
'quantity' => 1 ,
'unit_price_vat_exclude' => round ( $batch -> getTotalAmount () / StripeService :: VAT , 2 ),
'vat_rate' => $this -> vat ,
'total_price_vat_exclude' => round ( $deliveryNote -> getTotalAmount () / StripeService :: VAT , 2 )
);
}
if ($deliveryNote -> getTierCharge () != 0 ) {
$items [] = array(
'reference' => self :: REF_INTERENCHERES ,
'designation' => 'Frais Interencheres' ,
'quantity' => 1 ,
'unit_price_vat_exclude' => round ( $deliveryNote -> computeTierCharge () / StripeService :: VAT , 2 ),
'vat_rate' => $this -> vat ,
'total_price_vat_exclude' => round ( $deliveryNote -> computeTierCharge () / StripeService :: VAT , 2 )
);
}
return $items ;
}
public function getAdminShippingItems ( Shipment $shipment ): array
{
$items = array(
array(
'reference' => $shipment -> getCarrier ()-> getInvoiceRowType ()-> getCode (),
'designation' => 'Expédition - ' . $shipment -> getCarrier ()-> getName (),
'quantity' => 1 ,
'unit_price_vat_exclude' => array(
'numericValue' => round ( $shipment -> getTotalAmountShipping () / StripeService :: VAT , 2 ),
'value' => FormatExtension :: formatPrice ( round ( $shipment -> getTotalAmountShipping () / StripeService :: VAT , 2 ), false ),
),
'vat_rate' => array(
'numericValue' => $this -> vat ,
'value' => FormatExtension :: formatPercentage ( $this -> vat )
),
'total_price_vat_exclude' => array(
'numericValue' => round ( $shipment -> getTotalAmountShipping () / StripeService :: VAT , 2 ),
'value' => FormatExtension :: formatPrice ( round ( $shipment -> getTotalAmountShipping () / StripeService :: VAT , 2 ), false )
),
'type' => array( 'type' => 'hidden' , 'value' => $shipment -> getCarrier ()-> getInvoiceRowType ()-> getId ())
),
);
if ($shipment -> getAmountGuardCharge () != 0 ) {
$amountHT = $shipment -> getAmountGuardCharge () / 1.2 ;
$ref = $this -> em -> getRepository ( InvoiceRowType ::class)-> findOneByCode ( self :: REF_GUARDCHARGE );
$guardchargeItems = array(
array(
'reference' => $ref -> getName (),
'designation' => 'Frais de gardiennage' ,
'quantity' => 1 ,
'unit_price_vat_exclude' => array(
'numericValue' => $amountHT ,
'value' => FormatExtension :: formatPrice ( $amountHT , false )
),
'vat_rate' => array(
'numericValue' => $this -> vat ,
'value' => FormatExtension :: formatPercentage ( $this -> vat )
),
'total_price_vat_exclude' => array(
'numericValue' => $amountHT ,
'value' => FormatExtension :: formatPrice ( $amountHT , false )
),
'type' => array( 'type' => 'hidden' , 'value' => $ref -> getId ())
)
);
$items = array_merge ( $items , $guardchargeItems );
}
return $items ;
}
private function getInvoiceData ( BaseInvoice $invoice ): array
{
return $this -> getDefaultInvoiceData ( $invoice -> getUser (), $invoice -> getObject ());
}
private function getDefaultInvoiceData ( User $user , string $object = null ): array
{
return array(
'customer' => array(
'name' => $user -> getFullname (),
'address' => $user -> getFallbackAddress ()-> getAddressLines (),
'zipcodecountry' => $user -> getFallbackAddress ()-> getCityZipcodeCountry (),
'company' => $user -> getCompany (),
'company_number' => $user -> getCompanyNumber (),
'company_vat_number' => $user -> getCompanyVatNumber ()
),
'company' => array(
'name' => $this -> brandService -> getBrand ()-> getName (),
'address' => $this -> brandService -> getBrand ()-> getAddress ()-> getAddressLines (),
'zipcodecountry' => $this -> brandService -> getBrand ()-> getAddress ()-> getCityZipcodeCountry (),
'phone' => $this -> brandService -> getBrand ()-> getPhone (),
'contact' => $this -> brandService -> getBrand ()-> getContact ()
),
'metadata' => array(
'document_width' => '755px'
),
'object' => $object ?? '' ,
'columns' => array(
array('name' => 'reference' , 'label' => 'RÉFÉRENCE' , 'type' => 'text' , 'classList' => 'w-15' ),
array('name' => 'designation' , 'label' => 'DÉSIGNATION' , 'type' => 'text' , 'classList' => 'w-35' ),
array('name' => 'quantity' , 'label' => 'QUANTITÉ' , 'type' => 'text' , 'classList' => 'w-15 tar' ),
array('name' => 'unit_price_vat_exclude' , 'label' => 'PU HT' , 'type' => 'price' , 'classList' => 'w-10 tar' ),
array('name' => 'vat_rate' , 'label' => 'TVA' , 'type' => 'text' , 'classList' => 'w-10 tar' ),
array('name' => 'total_price_vat_exclude' , 'label' => 'TOTAL HT' , 'type' => 'price' , 'classList' => 'w-15 tar' ),
),
'rows' => array(),
'vat_data' => array(
'columns' => array(
array('label' => 'BASE HT' , 'type' => 'price' , 'classList' => 'w-15' ),
array('label' => 'TAUX' , 'type' => 'text' , 'classList' => 'w-15' ),
array('label' => 'TVA' , 'type' => 'price' , 'classList' => 'w-15' )
),
'rows' => array()
),
'total_data' => array()
);
}
public function getShippingItems ( Shipment $shipment ): array
{
$priceExcludedTaxes = $shipment -> hasDiscount () ? $shipment -> getPriceExcludedTaxes () : round ( $shipment -> getTotalAmountShipping () / StripeService :: VAT , 2 );
$items = array(
array(
'reference' => $shipment -> getCarrier ()-> getInvoiceRowType ()-> getCode (),
'designation' => 'Expédition - ' . $shipment -> getCarrier ()-> getName (),
'quantity' => 1 ,
'unit_price_vat_exclude' => array(
'numericValue' => $priceExcludedTaxes ,
'value' => FormatExtension :: formatPrice ( $priceExcludedTaxes , false ),
),
'vat_rate' => array(
'numericValue' => $this -> vat ,
'value' => FormatExtension :: formatPercentage ( $this -> vat )
),
'total_price_vat_exclude' => array(
'numericValue' => $priceExcludedTaxes ,
'value' => FormatExtension :: formatPrice ( $priceExcludedTaxes , false )
),
'type' => array( 'type' => 'hidden' , 'value' => $shipment -> getCarrier ()-> getInvoiceRowType ()-> getId ())
)
);
if ($shipment -> hasDiscount ()) {
$items [] = [
'reference' => $shipment -> getDiscountCode (),
'designation' => 'Remise' ,
'quantity' => 1 ,
'unit_price_vat_exclude' => [
'numericValue' => $shipment -> getDiscountAmount () * - 1 ,
'value' => FormatExtension :: formatPrice ( $shipment -> getDiscountAmount () * - 1 , false ),
],
'vat_rate' => [
'numericValue' => $this -> vat ,
'value' => FormatExtension :: formatPercentage ( $this -> vat )
],
'total_price_vat_exclude' => [
'numericValue' => $shipment -> getDiscountAmount () * - 1 ,
'value' => FormatExtension :: formatPrice ( $shipment -> getDiscountAmount () * - 1 , false )
],
'type' => [ 'type' => 'hidden' , 'value' => BaseInvoice :: ROW_TYPE_DISCOUNT ]
];
}
if ($shipment -> getAmountGuardCharge () != 0 ) {
$items = array_merge ( $items , $this -> getNoteGuardchargeItems ( $shipment -> getDeliveryNote (), end : $shipment -> getPayment ()-> getDate ()));
}
return $items ;
}
/**
* @param DeliveryNote $note
* @param DateTimeInterface|null $start
* @param DateTimeInterface|null $end
* @return array
*/
public function getNoteGuardchargeItems ( DeliveryNote $note , ? DateTimeInterface $start = null , ? DateTimeInterface $end = null ): array
{
$items = array();
/** @var Batch $batch */
foreach ( $note -> getBatches () as $batch ) {
$guardChargeReference = $this -> em -> getRepository ( InvoiceRowType ::class)-> findOneBy (array( 'flag' => $batch -> isOnlyPalletShipment () ? InvoiceRowType :: FLAG_GUARD_P : InvoiceRowType :: FLAG_GUARD_S ));
$items [] = array(
'reference' => $guardChargeReference -> getCode (),
'designation' => 'Lot n°' . $batch -> getSaleOrder () . ' - ' . $guardChargeReference -> getName (),
'quantity' => $this -> guardChargeService -> computeBatchDaysDiff ( $batch , $start , $end ),
'unit_price_vat_exclude' => array(
'numericValue' => $this -> guardChargeService -> getBatchPriceUnitExludingVAT ( $batch ),
'value' => FormatExtension :: formatPrice ( $this -> guardChargeService -> getBatchPriceUnitExludingVAT ( $batch ), false )
),
'vat_rate' => array(
'numericValue' => $this -> vat ,
'value' => FormatExtension :: formatPercentage ( $this -> vat )
),
'total_price_vat_exclude' => array(
'numericValue' => $this -> guardChargeService -> computeBatchDaysDiff ( $batch , $start , $end ) * $this -> guardChargeService -> getBatchPriceUnitExludingVAT ( $batch ),
'value' => FormatExtension :: formatPrice ( $this -> guardChargeService -> computeBatchDaysDiff ( $batch , $start , $end ) * $this -> guardChargeService -> getBatchPriceUnitExludingVAT ( $batch ), false )
),
'type' => array( 'type' => 'hidden' , 'value' => $guardChargeReference -> getId ())
);
}
return $items ;
}
/**
* @param Payment $payment
* @return array
* @throws Exception
*/
public function getBookingItems ( Payment $payment ): array
{
return $this -> getNoteGuardchargeItems ( $payment -> getBooking ()-> getDeliveryNote (), start : $payment -> getBooking ()-> getPreviousBookingDate (), end : $payment -> getBooking ()-> getSlot ());
}
public function createCustomInvoice ( BaseInvoice $invoice , bool $isUpdate = false ): BaseInvoice
{
$invoice -> setDate (new DateTime ());
$invoice -> setAmount ( $invoice -> computeAmount ());
if (!$isUpdate ) {
$invoice -> setNumber ( 'FB' );
}
$taxesTable = array();
foreach ($invoice -> getRows () as $row ) {
if (!isset($taxesTable [ $row [ 'vat_rate' ]])) {
$taxesTable [ $row [ 'vat_rate' ]] = array(
'total_price_vat_exclude' => 0 ,
'vat_rate' => $row [ 'vat_rate' ],
'total_vat' => 0
);
}
$taxesTable [ $row [ 'vat_rate' ]][ 'total_price_vat_exclude' ] += ( $row [ 'quantity' ] * $row [ 'unit_price_vat_exclude' ]);
$taxesTable [ $row [ 'vat_rate' ]][ 'total_vat' ] = round ( $taxesTable [ $row [ 'vat_rate' ]][ 'total_price_vat_exclude' ] * $row [ 'vat_rate' ] / 100 , 2 );
}
usort ( $taxesTable , fn( $a , $b ) => $a [ 'vat_rate' ] < $b [ 'vat_rate' ]);
$data = $this -> getInvoiceData ( $invoice );
$data [ 'rows' ] = $invoice -> getRows ()-> map (function (array $row ) {
return array(
'reference' => $row [ 'reference' ]-> getCode (),
'designation' => $row [ 'designation' ],
'quantity' => $row [ 'quantity' ],
'unit_price_vat_exclude' => array( 'numericValue' => $row [ 'unit_price_vat_exclude' ], 'value' => FormatExtension :: formatPrice ( $row [ 'unit_price_vat_exclude' ], false )),
'vat_rate' => array( 'numericValue' => $row [ 'vat_rate' ], 'value' => FormatExtension :: formatPercentage ( $row [ 'vat_rate' ])),
'total_price_vat_exclude' => array( 'numericValue' => $row [ 'unit_price_vat_exclude' ] * $row [ 'quantity' ], 'value' => FormatExtension :: formatPrice ( $row [ 'unit_price_vat_exclude' ] * $row [ 'quantity' ], false )),
'type' => array( 'type' => 'hidden' , 'value' => $row [ 'reference' ]-> getId ()));
})->toArray ();
$data [ 'vat_data' ][ 'rows' ] = array_map (fn( $taxesRow ) => array(
FormatExtension :: formatPrice ( $taxesRow [ 'total_price_vat_exclude' ], false ),
FormatExtension :: formatPercentage ( $taxesRow [ 'vat_rate' ]),
FormatExtension :: formatPrice ( $taxesRow [ 'total_vat' ], false ),
), $taxesTable );
$data [ 'total_data' ] = array(
array('label' => 'TOTAL HT' , 'value' => FormatExtension :: formatPrice ( round ( array_sum ( array_map (fn( $taxesRow ) => $taxesRow [ 'total_price_vat_exclude' ], $taxesTable )), 2 ), false )),
array('label' => 'TOTAL TVA' , 'value' => FormatExtension :: formatPrice ( round ( array_sum ( array_map (fn( $taxesRow ) => $taxesRow [ 'total_vat' ], $taxesTable )), 2 ), false )),
array('label' => 'TOTAL TTC' , 'value' => FormatExtension :: formatPrice ( $invoice -> getAmount (), false )),
);
$invoice -> setData ( $data );
$this -> em -> persist ( $invoice );
$this -> em -> flush ();
$this -> em -> refresh ( $invoice );
return $invoice ;
}
public function createInvoiceCredit ( InvoiceCredit $credit ): InvoiceCredit
{
$credit -> setDate (new DateTime ());
$credit -> setNumber ( 'A' );
$data = $this -> getInvoiceData ( $credit -> getInvoice ());
unset($data [ 'columns' ][ 2 ]);
unset($data [ 'columns' ][ 3 ]);
$data [ 'columns' ] = array_values ( $data [ 'columns' ]);
$data [ 'columns' ][ 1 ][ 'classList' ] = 'w-50' ;
$data [ 'columns' ][ 2 ][ 'classList' ] = 'w-15' ;
$data [ 'columns' ][ 3 ][ 'classList' ] = 'w-20' ;
$taxesTable = array();
$data [ 'customer' ] = $credit -> getInvoice ()-> getData ()[ 'customer' ];
$data [ 'company' ] = $credit -> getInvoice ()-> getData ()[ 'company' ];
$data [ 'rows' ] = array();
foreach ($credit -> getInvoice ()-> getData ()[ 'rows' ] as $key => $invoiceRow ) {
$creditRowData = array_filter ( $credit -> getRows ()[ $key ]);
if ($creditRowData ) {
$creditRow = $invoiceRow ;
$creditRow [ 'total_price_vat_exclude' ] = array(
'numericValue' => $credit -> getRows ()[ $key ][ 'amount_without_taxes' ],
'value' => FormatExtension :: formatPrice ((float)( $credit -> getRows ()[ $key ][ 'amount_without_taxes' ]), false )
);
unset($creditRow [ 'quantity' ]);
unset($creditRow [ 'unit_price_vat_exclude' ]);
$data [ 'rows' ][ "invoice_row_ $key " ] = $creditRow ;
if (!isset($taxesTable [ $invoiceRow [ 'vat_rate' ][ 'numericValue' ]])) {
$taxesTable [ $invoiceRow [ 'vat_rate' ][ 'numericValue' ]] = array(
'total_price_vat_exclude' => 0 ,
'vat_rate' => $invoiceRow [ 'vat_rate' ][ 'numericValue' ],
'total_vat' => 0
);
}
$taxesTable [ $invoiceRow [ 'vat_rate' ][ 'numericValue' ]][ 'total_price_vat_exclude' ] += (float)( $credit -> getRows ()[ $key ][ 'amount_without_taxes' ]);
$taxesTable [ $invoiceRow [ 'vat_rate' ][ 'numericValue' ]][ 'total_vat' ] = round ( $taxesTable [ $invoiceRow [ 'vat_rate' ][ 'numericValue' ]][ 'total_price_vat_exclude' ] * $invoiceRow [ 'vat_rate' ][ 'numericValue' ] / 100 , 2 );
}
}
usort ( $taxesTable , fn( $a , $b ) => $a [ 'vat_rate' ] < $b [ 'vat_rate' ]);
$data [ 'vat_data' ][ 'rows' ] = array_map (fn( $taxesRow ) => array(
FormatExtension :: formatPrice ( $taxesRow [ 'total_price_vat_exclude' ], false ),
FormatExtension :: formatPercentage ( $taxesRow [ 'vat_rate' ]),
FormatExtension :: formatPrice ( $taxesRow [ 'total_vat' ], false ),
), $taxesTable );
$data [ 'total_data' ] = array(
array('label' => 'TOTAL HT' , 'value' => FormatExtension :: formatPrice ( round ( array_sum ( array_map (fn( $taxesRow ) => $taxesRow [ 'total_price_vat_exclude' ], $taxesTable )), 2 ), false )),
array('label' => 'TOTAL TVA' , 'value' => FormatExtension :: formatPrice ( round ( array_sum ( array_map (fn( $taxesRow ) => $taxesRow [ 'total_vat' ], $taxesTable )), 2 ), false )),
array('label' => 'TOTAL TTC' , 'value' => FormatExtension :: formatPrice ( $credit -> getAmount (), false )),
);
$credit -> setData ( $data );
return $credit ;
}
public function createInvoiceCreditFromScratch ( InvoiceCredit $credit , User $user , string $object ): InvoiceCredit
{
$credit -> setDate (new DateTime ());
$credit -> setNumber ( 'A' );
$data = $this -> getDefaultInvoiceData ( $user , $object );
$taxesTable = [];
$data [ 'rows' ] = [];
foreach ($credit -> getRows () as $key => $creditRow ) {
$creditRow [ 'reference' ] = $creditRow [ 'reference' ]-> getCode ();
$total_price_vat_exclude = $creditRow [ 'quantity' ] * $creditRow [ 'unit_price_vat_exclude' ];
$creditRow [ 'total_price_vat_exclude' ] = [
'numericValue' => $total_price_vat_exclude ,
'value' => FormatExtension :: formatPrice ( $total_price_vat_exclude , false )
];
$creditRow [ 'unit_price_vat_exclude' ] = [
'numericValue' => $creditRow [ 'unit_price_vat_exclude' ],
'value' => FormatExtension :: formatPrice ( $creditRow [ 'unit_price_vat_exclude' ], false )
];
$creditRow [ 'vat_rate' ] = [
'numericValue' => $creditRow [ 'vat_rate' ],
'value' => FormatExtension :: formatPercentage ( $creditRow [ 'vat_rate' ])
];
$creditRow [ 'type' ] = [
'type' => 'hidden' ,
'value' => $credit -> getRows ()[ $key ][ 'reference' ]-> getId ()
];
$data [ 'rows' ][ "invoice_row_ $key " ] = $creditRow ;
if (!isset($taxesTable [ $creditRow [ 'vat_rate' ][ 'numericValue' ]])) {
$taxesTable [ $creditRow [ 'vat_rate' ][ 'numericValue' ]] = [
'total_price_vat_exclude' => 0 ,
'vat_rate' => $creditRow [ 'vat_rate' ][ 'numericValue' ],
'total_vat' => 0
];
}
$taxesTable [ $creditRow [ 'vat_rate' ][ 'numericValue' ]][ 'total_price_vat_exclude' ] += $creditRow [ 'total_price_vat_exclude' ][ 'numericValue' ];
$taxesTable [ $creditRow [ 'vat_rate' ][ 'numericValue' ]][ 'total_vat' ] = round ( $taxesTable [ $creditRow [ 'vat_rate' ][ 'numericValue' ]][ 'total_price_vat_exclude' ] * $creditRow [ 'vat_rate' ][ 'numericValue' ] / 100 , 2 );
}
usort ( $taxesTable , fn( $a , $b ) => $a [ 'vat_rate' ] < $b [ 'vat_rate' ]);
$data [ 'vat_data' ][ 'rows' ] = array_map (fn( $taxesRow ) => [
FormatExtension :: formatPrice ( $taxesRow [ 'total_price_vat_exclude' ], false ),
FormatExtension :: formatPercentage ( $taxesRow [ 'vat_rate' ]),
FormatExtension :: formatPrice ( $taxesRow [ 'total_vat' ], false ),
], $taxesTable );
$data [ 'total_data' ] = [
['label' => 'TOTAL HT' , 'value' => FormatExtension :: formatPrice ( round ( array_sum ( array_map (fn( $taxesRow ) => $taxesRow [ 'total_price_vat_exclude' ], $taxesTable )), 2 ), false )],
['label' => 'TOTAL TVA' , 'value' => FormatExtension :: formatPrice ( round ( array_sum ( array_map (fn( $taxesRow ) => $taxesRow [ 'total_vat' ], $taxesTable )), 2 ), false )],
['label' => 'TOTAL TTC' , 'value' => FormatExtension :: formatPrice ( $credit -> getAmount (), false )],
];
$credit -> setData ( $data );
return $credit ;
}
public function createSupportRequestCredit ( SupportRequest $supportRequest ): InvoiceCredit
{
$invoiceCredit = new InvoiceCredit ();
$invoiceCredit -> setAmount ( $supportRequest -> getRefundAmount ());
$invoiceCredit -> setDate (new DateTime ());
$invoiceCredit -> setNumber ( 'A' );
$data = $this -> getDefaultInvoiceData ( $supportRequest -> getUser ());
unset($data [ 'columns' ][ 2 ]);
unset($data [ 'columns' ][ 3 ]);
$data [ 'columns' ] = array_values ( $data [ 'columns' ]);
$data [ 'columns' ][ 1 ][ 'classList' ] = 'w-50' ;
$data [ 'columns' ][ 2 ][ 'classList' ] = 'w-15' ;
$data [ 'columns' ][ 3 ][ 'classList' ] = 'w-20' ;
$taxesTable = array();
$rowType = $this -> em -> getRepository ( InvoiceRowType ::class)-> findOneByflag ( InvoiceRowType :: FLAG_REFOUND_SAV );
$data [ 'rows' ] = array(
array(
'reference' => $rowType -> getCode (),
'designation' => $rowType -> getName (),
'quantity' => 1 ,
'unit_price_vat_exclude' => array( 'numericValue' => round ( $supportRequest -> getRefundAmount () / 1.2 , 2 ), 'value' => FormatExtension :: formatPrice ( round ( $supportRequest -> getRefundAmount () / 1.2 , 2 ), false )),
'vat_rate' => array( 'numericValue' => 20 , 'value' => FormatExtension :: formatPercentage ( 20 )),
'total_price_vat_exclude' => array( 'numericValue' => round ( $supportRequest -> getRefundAmount () / 1.2 , 2 ), 'value' => FormatExtension :: formatPrice ( round ( $supportRequest -> getRefundAmount () / 1.2 , 2 ), false )),
'type' => array( 'type' => 'hidden' , 'value' => $rowType -> getId ())
)
);
$data [ 'vat_data' ][ 'rows' ] = array_map (fn( $taxesRow ) => array(
FormatExtension :: formatPrice ( round ( $supportRequest -> getRefundAmount () / 1.2 , 2 ), false ),
FormatExtension :: formatPercentage ( 20 ),
FormatExtension :: formatPrice ( $supportRequest -> getRefundAmount () / 1.2 * 0.2 , false ),
), $taxesTable );
$data [ 'total_data' ] = array(
array('label' => 'TOTAL HT' , 'value' => FormatExtension :: formatPrice ( round ( $supportRequest -> getRefundAmount () / 1.2 , 2 ), false )),
array('label' => 'TOTAL TVA' , 'value' => FormatExtension :: formatPrice ( round (( $supportRequest -> getRefundAmount () / 1.2 ) * 0.2 , 2 ), false )),
array('label' => 'TOTAL TTC' , 'value' => FormatExtension :: formatPrice ( $supportRequest -> getRefundAmount (), false )),
);
$invoiceCredit -> setData ( $data );
return $invoiceCredit ;
}
public function mapInvoiceRows ( BaseInvoice $invoice ): array
{
$array = $invoice -> getData ()[ 'rows' ];
$array = array_map (function ( mixed $row ) {
$row [ 'reference' ] = $this -> em -> getRepository ( InvoiceRowType ::class)-> findOneByCode ( $row [ 'reference' ]);
$row [ 'unit_price_vat_exclude' ] = $row [ 'unit_price_vat_exclude' ][ 'numericValue' ];
$row [ 'vat_rate' ] = $row [ 'total_price_vat_exclude' ][ 'numericValue' ];
$row [ 'total_price_vat_exclude' ] = $row [ 'total_price_vat_exclude' ][ 'numericValue' ];
return $row ;
}, $array );
return $array ;
}
/**
* @param DeliveryNote $deliveryNote
* @param string $type
* @param string $brandOrigin
* @return JsonResponse
* @throws TransportExceptionInterface
*/
public function postInvoiceToAPI ( DeliveryNote $deliveryNote , string $type , string $brandOrigin ): JsonResponse
{
$shipment = $deliveryNote -> getShipment ();
$matchTypes = [
BaseInvoice :: TYPE_SHIPPING => 'shipment' ,
BaseInvoice :: TYPE_GUARD => 'guard' ,
];
$matchCarrierTypes = [
'EXP_COLI' => 'EXP_COLISSIMO' ,
'EXP_BOLLO' => 'EXP_BOLLORE' ,
'EXP_CHRONO' => 'EXP_CHRONOPOST' ,
'EXP_GEO' => 'EXP_GEODIS' ,
'EXP_COLI_PR' => 'EXP_COLISSIMO_PR' ,
];
$amount = $shipment -> getTotalAmountShipping ( true ) + $shipment -> getAmountGuardCharge ();
$invoiceData = [
"document_type" => "invoice" ,
"auction_house" => "adn" ,
"object" => "Facture NPCJ - Bordereaux " . $deliveryNote -> getCode (),
"number" => "FF" ,
"type" => $matchTypes [ $type ],
"date" => (new DateTime ())-> format ( 'Y-m-d H:i:s' ),
"amount_vat_exclude" => ( $amount * 100 ) / 1.2 ,
"amount" => $amount * 100 ,
"company_name" => null ,
"civility" => User :: CIVILITIES [ $deliveryNote -> getUser ()-> getCivility ()],
"firstname" => $deliveryNote -> getUser ()-> getFirstname (),
"lastname" => $deliveryNote -> getUser ()-> getLastname (),
"address" => [
"region" => null ,
"country" => null ,
"zipcode" => $shipment -> getBillingAddress ()[ 'zipcode' ],
"city" => $shipment -> getBillingAddress ()[ 'city' ],
"street" => $shipment -> getBillingAddress ()[ 'address' ],
"line1" => $shipment -> getBillingAddress ()[ 'address_line1' ],
"line2" => null
],
"rows" => [
[
"type" => $matchCarrierTypes [ $shipment -> getCarrier ()-> getInvoiceRowType ()-> getCode ()],
"designation" => 'Expédition ' . $shipment -> getCarrier ()-> getName (),
"quantity" => 1 ,
"unit_price_vat_exclude" => ( $shipment -> hasDiscount () ? $shipment -> getPriceExcludedTaxes () : round ( $shipment -> getTotalAmountShipping () / StripeService :: VAT , 2 )) * 100 ,
"vat_rate" => 2000 ,
"reduced_unit_price" => null
]
]
];
if ($shipment -> hasDiscount ()) {
$invoiceData [ 'rows' ][] = [
"type" => "DISCOUNT" ,
'designation' => 'Remise (code ' . $shipment -> getDiscountCode () . ')' ,
"quantity" => 1 ,
"unit_price_vat_exclude" => ( $shipment -> getDiscountAmount () * - 1 ) * 100 ,
"vat_rate" => 2000 ,
"reduced_unit_price" => null
];
}
if ($shipment -> getAmountGuardCharge () != 0 ) {
foreach ($deliveryNote -> getBatches () as $batch ) {
$invoiceData [ 'rows' ][] = [
"type" => "GUARD_" . ( $batch -> isOnlyPalletShipment () ? 'P' : 'S' ),
'designation' => 'Lot n°' . $batch -> getSaleOrder () . ' - Gardiennage' ,
"quantity" => $this -> guardChargeService -> computeBatchDaysDiff ( $batch , null , $shipment -> getPayment ()-> getDate ()),
"unit_price_vat_exclude" => $this -> guardChargeService -> getBatchPriceUnitExludingVAT ( $batch ) * 100 ,
"vat_rate" => 2000 ,
"reduced_unit_price" => null
];
}
}
$this -> auctionisInvoiceService -> post ( $invoiceData );
return new JsonResponse ( null , Response :: HTTP_OK );
}
/**
* @param array $deliveryNoteData
* @param string $type
* @return Invoice
* @throws Exception
*/
public function createInvoiceFromApi (array $deliveryNoteData , string $type , string $brandOrigin ): Invoice
{
try {
$deliveryNote = $this -> denormalizeDeliveryNote ( $deliveryNoteData );
if ($type === BaseInvoice :: TYPE_GUARD ) {
$invoice = $this -> createGuardchargeInvoiceRemote ( $deliveryNote , $brandOrigin , date : $deliveryNote -> getBooking ()-> getSlot (), dateStart : $deliveryNote -> getBooking ()-> getPreviousBookingDate ());
$payments = $deliveryNote -> getBooking ()-> getPayments ()-> toArray ();
} else if ($type === BaseInvoice :: TYPE_SHIPPING ) {
$invoice = $this -> createShippingInvoiceRemote ( $deliveryNote -> getShipment (), $brandOrigin );
$payments = [ $deliveryNote -> getShipment ()-> getPayment ()];
} else throw new Exception ();
foreach ($payments as $payment ) {
if (!$payment ) continue;
$payment -> setInvoice ( $invoice );
$payment -> setBooking ( null );
$payment -> setDeliveryNote ( null );
$payment -> setShipment ( null );
$this -> em -> persist ( $payment );
}
$invoice -> setDeliveryNote ( null );
$invoice -> setUser ( null );
$this -> em -> persist ( $invoice );
$this -> em -> flush ();
$this -> em -> refresh ( $invoice );
return $invoice ;
} catch (Throwable $e ) {
throw new Exception ( "Error while creating invoice : " . $e -> getMessage ());
}
}
/**
* @param array $data
* @return DeliveryNote
* @throws ExceptionInterface
*/
public function denormalizeDeliveryNote (array $data ): DeliveryNote
{
$deliveryNote = $this -> serializer -> denormalize ( $data , DeliveryNote ::class, context : [
AbstractObjectNormalizer :: DISABLE_TYPE_ENFORCEMENT => true ,
AbstractNormalizer :: CALLBACKS => [
'booking' => fn( $object ) => $object ?: new Booking ()
]
]);
$deliveryNote -> getShipment ()?-> setDeliveryNote ( $deliveryNote );
$deliveryNote -> getBooking ()?-> setDeliveryNote ( $deliveryNote );
array_map (fn( Batch $b ) => $b -> setDeliveryNote ( $deliveryNote ), $deliveryNote -> getBatches ()-> toArray ());
return $deliveryNote ;
}
public function createGuardchargeInvoiceRemote ( DeliveryNote $note , string $brandOrigin , ? Collection $batches = null , ? DateTimeInterface $date = null , ? DateTimeInterface $dateStart = null ): Invoice
{
$invoice = $this -> createGuardchargeInvoice ( $note , $batches , $date , $dateStart , persist : false );
$data = $invoice -> getData ();
$data [ 'object' ] = strtoupper ( $brandOrigin ) . ' - ' . $invoice -> getData ()[ 'object' ];
$invoice -> setData ( $data );
$invoice -> setObject ( strtoupper ( $brandOrigin ) . ' - ' . $invoice -> getObject ());
return $invoice ;
}
public function createGuardchargeInvoice ( DeliveryNote $note , ? Collection $batches = null , ? DateTimeInterface $date = null , ? DateTimeInterface $dateStart = null , ? bool $persist = true ): Invoice
{
$invoice = new Invoice ();
$invoice -> setUser ( $note -> getUser ());
$invoice -> setDeliveryNote ( $note );
$invoice -> setDate (new DateTime ());
$invoice -> setAmount ( $this -> guardChargeService -> computeGuardcharge ( $note , date : $date , batches : $batches , includePayments : false ));
$invoice -> setType ( BaseInvoice :: TYPE_GUARD );
$invoice -> setNumber ( 'F' );
$invoice -> setObject ( $note -> getCode () . ' - ' . $note -> getSaleDate ()-> format ( 'd/m/Y' ));
$data = $this -> getInvoiceData ( $invoice );
$data [ 'rows' ] = array_map (function ( Batch $batch ) use ( $date , $dateStart , $note ) {
$guardChargeReference = $this -> em -> getRepository ( InvoiceRowType ::class)-> findOneBy (array( 'flag' => $batch -> isOnlyPalletShipment () ? InvoiceRowType :: FLAG_GUARD_P : InvoiceRowType :: FLAG_GUARD_S ));
return array(
'reference' => $guardChargeReference -> getCode (),
'designation' => 'Lot n°' . $batch -> getSaleOrder () . ' - ' . $guardChargeReference -> getName (),
'quantity' => $this -> guardChargeService -> computeBatchDaysDiff ( $batch , $dateStart , $date , deliveryNote : $note ),
'unit_price_vat_exclude' => array(
'numericValue' => $this -> guardChargeService -> getBatchPriceUnitExludingVAT ( $batch ),
'value' => FormatExtension :: formatPrice ( $this -> guardChargeService -> getBatchPriceUnitExludingVAT ( $batch ), false )
),
'vat_rate' => array(
'numericValue' => ConstantsService :: GUARDCHARGE_VAT * 100 ,
'value' => FormatExtension :: formatPercentage ( ConstantsService :: GUARDCHARGE_VAT )
),
'total_price_vat_exclude' => array(
'numericValue' => $this -> guardChargeService -> computeBatchDaysDiff ( $batch , $dateStart , $date , deliveryNote : $note ) * $this -> guardChargeService -> getBatchPriceUnitExludingVAT ( $batch ),
'value' => FormatExtension :: formatPrice ( $this -> guardChargeService -> computeBatchDaysDiff ( $batch , $dateStart , $date , deliveryNote : $note ) * $this -> guardChargeService -> getBatchPriceUnitExludingVAT ( $batch ), false )
),
'type' => array( 'type' => 'hidden' , 'value' => $guardChargeReference -> getId ())
);
}, ($batches ?? $note -> getBatches ())-> toArray ());
$totalExcludedTaxes = $this -> guardChargeService -> computeGuardcharge ( deliveryNote : $note , date : $date , includeVAT : false , batches : $batches , includePayments : false );
$totalIncludedTaxes = $this -> guardChargeService -> computeGuardcharge ( deliveryNote : $note , date : $date , includeVAT : true , batches : $batches , includePayments : false );
$totalTaxes = $this -> guardChargeService -> computeGuardcharge ( deliveryNote : $note , date : $date , vatOnly : true , batches : $batches , includePayments : false );
$data [ 'vat_data' ][ 'rows' ] = array(
array(
FormatExtension :: formatPrice ( $totalExcludedTaxes , false ),
FormatExtension :: formatPercentage ( ConstantsService :: GUARDCHARGE_VAT ),
FormatExtension :: formatPrice ( $totalTaxes , false )
)
);
$data [ 'total_data' ] = array(
array('label' => 'TOTAL HT' , 'value' => FormatExtension :: formatPrice ( $totalExcludedTaxes , false )),
array('label' => 'TOTAL TVA' , 'value' => FormatExtension :: formatPrice ( $totalTaxes , false )),
array('label' => 'TOTAL TTC' , 'value' => FormatExtension :: formatPrice ( $totalIncludedTaxes , false )),
);
$invoice -> setData ( $data );
if ($persist ) {
$this -> em -> persist ( $invoice );
$this -> em -> flush ();
$this -> em -> refresh ( $invoice );
}
return $invoice ;
}
/**
* @param Shipment $shipment
* @return Invoice
*/
public function createShippingInvoiceRemote ( Shipment $shipment , string $brandOrigin ): Invoice
{
$invoice = $this -> createShippingInvoice ( $shipment , persist : false );
$data = $invoice -> getData ();
$data [ 'object' ] = strtoupper ( $brandOrigin ) . ' - ' . $invoice -> getData ()[ 'object' ];
$invoice -> setData ( $data );
$invoice -> setObject ( strtoupper ( $brandOrigin ) . ' - ' . $invoice -> getObject ());
return $invoice ;
}
public function createShippingInvoice ( Shipment $shipment , ?array $rows = null , ? bool $persist = true ): Invoice
{
$invoice = new Invoice ();
$invoice -> setDate (new DateTime ());
$invoice -> setNumber ( 'F' );
$invoice -> setObject ( 'Expédition ' . $shipment -> getDeliveryNote ()-> getCode ());
$invoice -> setUser ( $shipment -> getDeliveryNote ()-> getUser ());
$invoice -> setDeliveryNote ( $shipment -> getDeliveryNote ());
$invoice -> setType ( BaseInvoice :: TYPE_SHIPPING );
$invoice -> setAmount ( $shipment -> getTotalAmountShipping ( true ) + $shipment -> getAmountGuardCharge ());
$data = $this -> getInvoiceData ( $invoice );
$data [ 'rows' ] = $rows ?? $this -> getShippingItems ( $shipment );
$data [ 'vat_data' ][ 'rows' ] = array(
array(
FormatExtension :: formatPrice ( round ( $invoice -> getAmount () / StripeService :: VAT , 2 ), false ),
FormatExtension :: formatPercentage ( ConstantsService :: GUARDCHARGE_VAT ),
FormatExtension :: formatPrice ( $invoice -> getAmount () - round ( $invoice -> getAmount () / StripeService :: VAT , 2 ), false )
)
);
$data [ 'total_data' ] = array(
array('label' => 'TOTAL HT' , 'value' => FormatExtension :: formatPrice ( round ( $invoice -> getAmount () / StripeService :: VAT , 2 ), false )),
array('label' => 'TOTAL TVA' , 'value' => FormatExtension :: formatPrice ( $invoice -> getAmount () - round ( $invoice -> getAmount () / StripeService :: VAT , 2 ), false )),
array('label' => 'TOTAL TTC' , 'value' => FormatExtension :: formatPrice ( $invoice -> getAmount (), false )),
);
$invoice -> setData ( $data );
if ($persist ) {
$this -> em -> persist ( $invoice );
$this -> em -> flush ();
$this -> em -> refresh ( $invoice );
}
return $invoice ;
}
}