Browse Source

Documents : méthode de calcul TVA #478

refactoring
Guillaume Bourgeois 2 years ago
parent
commit
6105b99f3f
9 changed files with 109 additions and 12 deletions
  1. +8
    -1
      backend/controllers/DistributionController.php
  2. +1
    -0
      backend/controllers/DocumentController.php
  3. +1
    -0
      backend/controllers/QuotationController.php
  4. +2
    -0
      backend/views/producer/update.php
  5. +9
    -2
      common/helpers/Price.php
  6. +33
    -2
      common/models/Document.php
  7. +7
    -5
      common/models/Order.php
  8. +4
    -2
      common/models/Producer.php
  9. +44
    -0
      console/migrations/m220916_062206_add_column_document_tax_calculation_method.php

+ 8
- 1
backend/controllers/DistributionController.php View File

@@ -1475,6 +1475,7 @@ class DistributionController extends BackendController

if (!$deliveryNote) {
$deliveryNote = new DeliveryNote();
$deliveryNote->initTaxCalculationMethod();
$deliveryNote->id_producer = GlobalParam::getCurrentProducerId();
$deliveryNote->id_user = $order->id_user;
$deliveryNote->name = 'Bon de livraison ' . $order->getUsername() . ' (' . date(
@@ -1560,6 +1561,7 @@ class DistributionController extends BackendController
// génération du BL
if (!$deliveryNote) {
$deliveryNote = new DeliveryNote;
$deliveryNote->initTaxCalculationMethod();
$deliveryNote->name = 'Bon de livraison ' . $firstOrder->pointSale->name . ' (' . date(
'd/m/Y',
strtotime(
@@ -1573,6 +1575,11 @@ class DistributionController extends BackendController
$user = User::searchOne([
'id' => $deliveryNote->id_user
]);
$userProducer = UserProducer::searchOne([
'id_user' => $deliveryNote->id_user,
'id_producer' => GlobalParam::getCurrentProducerId(
)
]);
} else {
$user = new User;
$user->type = User::TYPE_LEGAL_PERSON;
@@ -1603,7 +1610,7 @@ class DistributionController extends BackendController
$deliveryNote->address = $user->getFullAddress();
$deliveryNote->save();
} else {
// réinitialisation des order.id_delivery_order
// réinitialisation des order.id_delivery_note
Order::updateAll([
'id_delivery_note' => null
], [

+ 1
- 0
backend/controllers/DocumentController.php View File

@@ -78,6 +78,7 @@ class DocumentController extends BackendController
{
$class = $this->getClass();
$model = new $class();
$model->initTaxCalculationMethod();

if ($model->load(Yii::$app->request->post())) {
$model->id_producer = GlobalParam::getCurrentProducerId();

+ 1
- 0
backend/controllers/QuotationController.php View File

@@ -90,6 +90,7 @@ class QuotationController extends DocumentController
if($quotation->isStatusValid()) {

$invoice = new Invoice ;
$invoice->initTaxCalculationMethod();
$invoice->id_producer = GlobalParam::getCurrentProducerId();
$invoice->id_user = $quotation->id_user ;
$invoice->address = $quotation->address ;

+ 2
- 0
backend/views/producer/update.php View File

@@ -432,6 +432,8 @@ $this->addBreadcrumb($this->getTitle()) ;
<?= $form->field($model, 'id_tax_rate_default')
->dropDownList(ArrayHelper::map(TaxRate::find()->all(), 'id', function($model) { return $model->name; }))
->label('TVA à appliquer par défaut'); ?>
<?= $form->field($model, 'option_tax_calculation_method')
->dropDownList(Document::$taxCalculationMethodArray); ?>
<?= $form->field($model, 'document_quotation_prefix') ; ?>
<?= $form->field($model, 'document_quotation_first_reference') ; ?>
<?= $form->field($model, 'document_quotation_duration') ; ?>

+ 9
- 2
common/helpers/Price.php View File

@@ -51,9 +51,16 @@ class Price
return floatval($priceWithTax) / ($taxRate + 1);
}

public static function getPriceWithTax($priceWithoutTax, $taxRate)
public static function getPriceWithTax($priceWithoutTax, $taxRate, $taxCalculationMethod = Document::TAX_CALCULATION_METHOD_DEFAULT)
{
return self::numberTwoDecimals(floatval($priceWithoutTax) * ($taxRate + 1)) ;
$priceWithTax = floatval($priceWithoutTax) * ($taxRate + 1);

if($taxCalculationMethod == Document::TAX_CALCULATION_METHOD_ROUNDING_OF_THE_SUM) {
return $priceWithTax;
}
else {
return self::numberTwoDecimals($priceWithTax);
}
}

public static function numberTwoDecimals($number)

+ 33
- 2
common/models/Document.php View File

@@ -45,6 +45,15 @@ class Document extends ActiveRecordCommon
const STATUS_DRAFT = 'draft';
const STATUS_VALID = 'valid';

const TAX_CALCULATION_METHOD_SUM_OF_ROUNDINGS = 'sum-roundings';
const TAX_CALCULATION_METHOD_ROUNDING_OF_THE_SUM = 'rounding-sum';
const TAX_CALCULATION_METHOD_DEFAULT = self::TAX_CALCULATION_METHOD_ROUNDING_OF_THE_SUM;

public static $taxCalculationMethodArray = [
self::TAX_CALCULATION_METHOD_ROUNDING_OF_THE_SUM => 'Arrondi de la somme des lignes',
self::TAX_CALCULATION_METHOD_SUM_OF_ROUNDINGS => 'Somme des arrondis de chaque ligne'
];

/**
* @inheritdoc
*/
@@ -53,7 +62,7 @@ class Document extends ActiveRecordCommon
return [
[['name', 'id_user'], 'required'],
[['date'], 'safe'],
[['comment', 'address'], 'string'],
[['comment', 'address', 'tax_calculation_method'], 'string'],
[['id_user', 'id_producer'], 'integer'],
[['name', 'reference', 'status'], 'string', 'max' => 255],
[['deliveryNotes'], 'safe']
@@ -75,6 +84,7 @@ class Document extends ActiveRecordCommon
'address' => 'Adresse',
'id_producer' => 'Producteur',
'status' => 'Statut',
'tax_calculation_method' => 'Méthode de calcul de la TVA'
];
}

@@ -123,7 +133,7 @@ class Document extends ActiveRecordCommon
$ordersArray = $this->orders;

foreach ($ordersArray as $order) {
$order->init();
$order->init($this->tax_calculation_method);

if ($withTax) {
$amount += $order->getAmountWithTax($type);
@@ -392,4 +402,25 @@ class Document extends ActiveRecordCommon
return $this->getClass() == 'Invoice' || $this->getClass() == 'DeliveryNote' ;
}

public function isTaxCalculationMethodSumOfRoundings()
{
return $this->tax_calculation_method == self::TAX_CALCULATION_METHOD_SUM_OF_ROUNDINGS;
}

public function isTaxCalculationMethodRoundingOfTheSum()
{
return $this->tax_calculation_method == self::TAX_CALCULATION_METHOD_ROUNDING_OF_THE_SUM;
}

public function initTaxCalculationMethod()
{
$producerTaxCalculationMethod = Producer::getConfig('option_tax_calculation_method');

if($producerTaxCalculationMethod) {
$this->tax_calculation_method = $producerTaxCalculationMethod;
}
else {
$this->tax_calculation_method = self::TAX_CALCULATION_METHOD_DEFAULT;
}
}
}

+ 7
- 5
common/models/Order.php View File

@@ -223,9 +223,9 @@ class Order extends ActiveRecordCommon
* Initialise le montant total, le montant déjà payé et le poids de la
* commande.
*/
public function init()
public function init($taxCalculationMethod = Document::TAX_CALCULATION_METHOD_DEFAULT)
{
$this->initAmount();
$this->initAmount($taxCalculationMethod);
$this->initPaidAmount();

return $this;
@@ -235,7 +235,7 @@ class Order extends ActiveRecordCommon
* Initialise le montant de la commande.
*
*/
public function initAmount()
public function initAmount($taxCalculationMethod = Document::TAX_CALCULATION_METHOD_DEFAULT)
{
$this->amount = 0;
$this->amount_with_tax = 0;
@@ -248,7 +248,8 @@ class Order extends ActiveRecordCommon
$this->amount += $productOrder->price * $productOrder->quantity;
$this->amount_with_tax += Price::getPriceWithTax(
$productOrder->price,
$productOrder->taxRate->value
$productOrder->taxRate->value,
$taxCalculationMethod
) * $productOrder->quantity;

if($productOrder->invoice_price) {
@@ -261,7 +262,8 @@ class Order extends ActiveRecordCommon
$this->invoice_amount += $invoicePrice * $productOrder->quantity;
$this->invoice_amount_with_tax += Price::getPriceWithTax(
$invoicePrice,
$productOrder->taxRate->value
$productOrder->taxRate->value,
$taxCalculationMethod
) * $productOrder->quantity;

if ($productOrder->unit == 'piece') {

+ 4
- 2
common/models/Producer.php View File

@@ -223,7 +223,8 @@ class Producer extends ActiveRecordCommon
'option_stripe_public_key',
'option_stripe_private_key',
'option_stripe_endpoint_secret',
'option_online_payment_type'
'option_online_payment_type',
'option_tax_calculation_method'
],
'string'
],
@@ -386,7 +387,8 @@ class Producer extends ActiveRecordCommon
'option_notify_producer_order_summary' => 'Recevoir les récapitulatifs de commande par email',
'option_billing_type' => 'Type de facturation',
'option_billing_frequency' => 'Fréquence de facturation',
'option_billing_reduction' => 'Réduction appliquée au moment de la facturation'
'option_billing_reduction' => 'Réduction appliquée au moment de la facturation',
'option_tax_calculation_method' => 'Méthode de calcul de la TVA'
];
}


+ 44
- 0
console/migrations/m220916_062206_add_column_document_tax_calculation_method.php View File

@@ -0,0 +1,44 @@
<?php

use yii\db\Migration;
use yii\db\Schema;
use common\models\Document;

/**
* Class m220916_062206_add_column_document_tax_calculation_method
*/
class m220916_062206_add_column_document_tax_calculation_method extends Migration
{
public static $tableDocumentArray = ['invoice', 'delivery_note', 'quotation'];

/**
* {@inheritdoc}
*/
public function safeUp()
{
$schemaTaxCalculationMethod = Schema::TYPE_STRING.' DEFAULT \''.Document::TAX_CALCULATION_METHOD_DEFAULT.'\'';

// producer
$this->addColumn('producer', 'option_tax_calculation_method', $schemaTaxCalculationMethod);

// documents
$columnTaxCalculationMethod = 'tax_calculation_method';
foreach(self::$tableDocumentArray as $tableName) {
$this->addColumn($tableName, $columnTaxCalculationMethod, $schemaTaxCalculationMethod);
// méthode appliquée jusqu'à maintenant
$this->execute('UPDATE `'.$tableName.'` SET `'.$columnTaxCalculationMethod.'` = \''.Document::TAX_CALCULATION_METHOD_SUM_OF_ROUNDINGS.'\'');
}
}

/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropColumn('producer', 'option_tax_calculation_method');

foreach(self::$tableDocumentArray as $tableName) {
$this->dropColumn($tableName, 'tax_calculation_method');
}
}
}

Loading…
Cancel
Save