if (!$deliveryNote) { | if (!$deliveryNote) { | ||||
$deliveryNote = new DeliveryNote(); | $deliveryNote = new DeliveryNote(); | ||||
$deliveryNote->initTaxCalculationMethod(); | |||||
$deliveryNote->id_producer = GlobalParam::getCurrentProducerId(); | $deliveryNote->id_producer = GlobalParam::getCurrentProducerId(); | ||||
$deliveryNote->id_user = $order->id_user; | $deliveryNote->id_user = $order->id_user; | ||||
$deliveryNote->name = 'Bon de livraison ' . $order->getUsername() . ' (' . date( | $deliveryNote->name = 'Bon de livraison ' . $order->getUsername() . ' (' . date( | ||||
// génération du BL | // génération du BL | ||||
if (!$deliveryNote) { | if (!$deliveryNote) { | ||||
$deliveryNote = new DeliveryNote; | $deliveryNote = new DeliveryNote; | ||||
$deliveryNote->initTaxCalculationMethod(); | |||||
$deliveryNote->name = 'Bon de livraison ' . $firstOrder->pointSale->name . ' (' . date( | $deliveryNote->name = 'Bon de livraison ' . $firstOrder->pointSale->name . ' (' . date( | ||||
'd/m/Y', | 'd/m/Y', | ||||
strtotime( | strtotime( | ||||
$user = User::searchOne([ | $user = User::searchOne([ | ||||
'id' => $deliveryNote->id_user | 'id' => $deliveryNote->id_user | ||||
]); | ]); | ||||
$userProducer = UserProducer::searchOne([ | |||||
'id_user' => $deliveryNote->id_user, | |||||
'id_producer' => GlobalParam::getCurrentProducerId( | |||||
) | |||||
]); | |||||
} else { | } else { | ||||
$user = new User; | $user = new User; | ||||
$user->type = User::TYPE_LEGAL_PERSON; | $user->type = User::TYPE_LEGAL_PERSON; | ||||
$deliveryNote->address = $user->getFullAddress(); | $deliveryNote->address = $user->getFullAddress(); | ||||
$deliveryNote->save(); | $deliveryNote->save(); | ||||
} else { | } else { | ||||
// réinitialisation des order.id_delivery_order | |||||
// réinitialisation des order.id_delivery_note | |||||
Order::updateAll([ | Order::updateAll([ | ||||
'id_delivery_note' => null | 'id_delivery_note' => null | ||||
], [ | ], [ |
{ | { | ||||
$class = $this->getClass(); | $class = $this->getClass(); | ||||
$model = new $class(); | $model = new $class(); | ||||
$model->initTaxCalculationMethod(); | |||||
if ($model->load(Yii::$app->request->post())) { | if ($model->load(Yii::$app->request->post())) { | ||||
$model->id_producer = GlobalParam::getCurrentProducerId(); | $model->id_producer = GlobalParam::getCurrentProducerId(); |
if($quotation->isStatusValid()) { | if($quotation->isStatusValid()) { | ||||
$invoice = new Invoice ; | $invoice = new Invoice ; | ||||
$invoice->initTaxCalculationMethod(); | |||||
$invoice->id_producer = GlobalParam::getCurrentProducerId(); | $invoice->id_producer = GlobalParam::getCurrentProducerId(); | ||||
$invoice->id_user = $quotation->id_user ; | $invoice->id_user = $quotation->id_user ; | ||||
$invoice->address = $quotation->address ; | $invoice->address = $quotation->address ; |
<?= $form->field($model, 'id_tax_rate_default') | <?= $form->field($model, 'id_tax_rate_default') | ||||
->dropDownList(ArrayHelper::map(TaxRate::find()->all(), 'id', function($model) { return $model->name; })) | ->dropDownList(ArrayHelper::map(TaxRate::find()->all(), 'id', function($model) { return $model->name; })) | ||||
->label('TVA à appliquer par défaut'); ?> | ->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_prefix') ; ?> | ||||
<?= $form->field($model, 'document_quotation_first_reference') ; ?> | <?= $form->field($model, 'document_quotation_first_reference') ; ?> | ||||
<?= $form->field($model, 'document_quotation_duration') ; ?> | <?= $form->field($model, 'document_quotation_duration') ; ?> |
return floatval($priceWithTax) / ($taxRate + 1); | 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) | public static function numberTwoDecimals($number) |
const STATUS_DRAFT = 'draft'; | const STATUS_DRAFT = 'draft'; | ||||
const STATUS_VALID = 'valid'; | 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 | * @inheritdoc | ||||
*/ | */ | ||||
return [ | return [ | ||||
[['name', 'id_user'], 'required'], | [['name', 'id_user'], 'required'], | ||||
[['date'], 'safe'], | [['date'], 'safe'], | ||||
[['comment', 'address'], 'string'], | |||||
[['comment', 'address', 'tax_calculation_method'], 'string'], | |||||
[['id_user', 'id_producer'], 'integer'], | [['id_user', 'id_producer'], 'integer'], | ||||
[['name', 'reference', 'status'], 'string', 'max' => 255], | [['name', 'reference', 'status'], 'string', 'max' => 255], | ||||
[['deliveryNotes'], 'safe'] | [['deliveryNotes'], 'safe'] | ||||
'address' => 'Adresse', | 'address' => 'Adresse', | ||||
'id_producer' => 'Producteur', | 'id_producer' => 'Producteur', | ||||
'status' => 'Statut', | 'status' => 'Statut', | ||||
'tax_calculation_method' => 'Méthode de calcul de la TVA' | |||||
]; | ]; | ||||
} | } | ||||
$ordersArray = $this->orders; | $ordersArray = $this->orders; | ||||
foreach ($ordersArray as $order) { | foreach ($ordersArray as $order) { | ||||
$order->init(); | |||||
$order->init($this->tax_calculation_method); | |||||
if ($withTax) { | if ($withTax) { | ||||
$amount += $order->getAmountWithTax($type); | $amount += $order->getAmountWithTax($type); | ||||
return $this->getClass() == 'Invoice' || $this->getClass() == 'DeliveryNote' ; | 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; | |||||
} | |||||
} | |||||
} | } |
* Initialise le montant total, le montant déjà payé et le poids de la | * Initialise le montant total, le montant déjà payé et le poids de la | ||||
* commande. | * commande. | ||||
*/ | */ | ||||
public function init() | |||||
public function init($taxCalculationMethod = Document::TAX_CALCULATION_METHOD_DEFAULT) | |||||
{ | { | ||||
$this->initAmount(); | |||||
$this->initAmount($taxCalculationMethod); | |||||
$this->initPaidAmount(); | $this->initPaidAmount(); | ||||
return $this; | return $this; | ||||
* Initialise le montant de la commande. | * Initialise le montant de la commande. | ||||
* | * | ||||
*/ | */ | ||||
public function initAmount() | |||||
public function initAmount($taxCalculationMethod = Document::TAX_CALCULATION_METHOD_DEFAULT) | |||||
{ | { | ||||
$this->amount = 0; | $this->amount = 0; | ||||
$this->amount_with_tax = 0; | $this->amount_with_tax = 0; | ||||
$this->amount += $productOrder->price * $productOrder->quantity; | $this->amount += $productOrder->price * $productOrder->quantity; | ||||
$this->amount_with_tax += Price::getPriceWithTax( | $this->amount_with_tax += Price::getPriceWithTax( | ||||
$productOrder->price, | $productOrder->price, | ||||
$productOrder->taxRate->value | |||||
$productOrder->taxRate->value, | |||||
$taxCalculationMethod | |||||
) * $productOrder->quantity; | ) * $productOrder->quantity; | ||||
if($productOrder->invoice_price) { | if($productOrder->invoice_price) { | ||||
$this->invoice_amount += $invoicePrice * $productOrder->quantity; | $this->invoice_amount += $invoicePrice * $productOrder->quantity; | ||||
$this->invoice_amount_with_tax += Price::getPriceWithTax( | $this->invoice_amount_with_tax += Price::getPriceWithTax( | ||||
$invoicePrice, | $invoicePrice, | ||||
$productOrder->taxRate->value | |||||
$productOrder->taxRate->value, | |||||
$taxCalculationMethod | |||||
) * $productOrder->quantity; | ) * $productOrder->quantity; | ||||
if ($productOrder->unit == 'piece') { | if ($productOrder->unit == 'piece') { |
'option_stripe_public_key', | 'option_stripe_public_key', | ||||
'option_stripe_private_key', | 'option_stripe_private_key', | ||||
'option_stripe_endpoint_secret', | 'option_stripe_endpoint_secret', | ||||
'option_online_payment_type' | |||||
'option_online_payment_type', | |||||
'option_tax_calculation_method' | |||||
], | ], | ||||
'string' | 'string' | ||||
], | ], | ||||
'option_notify_producer_order_summary' => 'Recevoir les récapitulatifs de commande par email', | 'option_notify_producer_order_summary' => 'Recevoir les récapitulatifs de commande par email', | ||||
'option_billing_type' => 'Type de facturation', | 'option_billing_type' => 'Type de facturation', | ||||
'option_billing_frequency' => 'Fréquence 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' | |||||
]; | ]; | ||||
} | } | ||||
<?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'); | |||||
} | |||||
} | |||||
} |