Browse Source

Modifier un document : refactoring Order::getAmount() et gestion HT et TTC des documents #166

refactoring
Guillaume Bourgeois 5 years ago
parent
commit
ff7d3ff14f
15 changed files with 299 additions and 255 deletions
  1. +3
    -3
      backend/controllers/DistributionController.php
  2. +212
    -215
      backend/controllers/DocumentController.php
  3. +3
    -3
      backend/controllers/OrderController.php
  4. +1
    -1
      backend/views/delivery-note/index.php
  5. +22
    -9
      backend/views/document/_form.php
  6. +1
    -1
      backend/views/invoice/index.php
  7. +1
    -1
      backend/views/quotation/index.php
  8. +1
    -1
      backend/views/site/index.php
  9. +3
    -1
      backend/web/js/vuejs/document-form.js
  10. +1
    -1
      common/helpers/Tiller.php
  11. +1
    -1
      common/models/CreditHistory.php
  12. +17
    -2
      common/models/Document.php
  13. +30
    -13
      common/models/Order.php
  14. +2
    -2
      producer/controllers/OrderController.php
  15. +1
    -1
      producer/views/order/confirm.php

+ 3
- 3
backend/controllers/DistributionController.php View File

@@ -144,7 +144,7 @@ class DistributionController extends BackendController
if($ordersArray) {
foreach ($ordersArray as $order) {
if(is_null($order->date_delete)) {
$revenues += $order->amount;
$revenues += $order->getAmountWithTax();
$weight += $order->weight;
}
}
@@ -242,7 +242,7 @@ class DistributionController extends BackendController
}
$order = array_merge($order->getAttributes(), [
'amount' => $order->getAmount(Order::AMOUNT_TOTAL),
'amount' => $order->getAmountWithTax(Order::AMOUNT_TOTAL),
'amount_paid' => $order->getAmount(Order::AMOUNT_PAID),
'amount_remaining' => $order->getAmount(Order::AMOUNT_REMAINING),
'amount_surplus' => $order->getAmount(Order::AMOUNT_SURPLUS),
@@ -963,7 +963,7 @@ class DistributionController extends BackendController
'lines' => $lines,
'payments' => [[
'type' => $typePaymentTiller,
'amount' => $order->getAmount(Order::AMOUNT_TOTAL) * 100,
'amount' => $order->getAmountWithTax(Order::AMOUNT_TOTAL) * 100,
'status' => 'ACCEPTED',
'date' => $strDate
]]

+ 212
- 215
backend/controllers/DocumentController.php View File

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

/**
Copyright distrib (2018)
contact@opendistrib.net
Ce logiciel est un programme informatique servant à aider les producteurs
à distribuer leur production en circuits courts.
Ce logiciel est régi par la licence CeCILL soumise au droit français et
respectant les principes de diffusion des logiciels libres. Vous pouvez
utiliser, modifier et/ou redistribuer ce programme sous les conditions
de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA
sur le site "http://www.cecill.info".
En contrepartie de l'accessibilité au code source et des droits de copie,
de modification et de redistribution accordés par cette licence, il n'est
offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons,
seule une responsabilité restreinte pèse sur l'auteur du programme, le
titulaire des droits patrimoniaux et les concédants successifs.
A cet égard l'attention de l'utilisateur est attirée sur les risques
associés au chargement, à l'utilisation, à la modification et/ou au
développement et à la reproduction du logiciel par l'utilisateur étant
donné sa spécificité de logiciel libre, qui peut le rendre complexe à
manipuler et qui le réserve donc à des développeurs et des professionnels
avertis possédant des connaissances informatiques approfondies. Les
utilisateurs sont donc invités à charger et tester l'adéquation du
logiciel à leurs besoins dans des conditions permettant d'assurer la
sécurité de leurs systèmes et ou de leurs données et, plus généralement,
à l'utiliser et l'exploiter dans les mêmes conditions de sécurité.
Le fait que vous puissiez accéder à cet en-tête signifie que vous avez
pris connaissance de la licence CeCILL, et que vous en avez accepté les
termes.
* Copyright distrib (2018)
*
* contact@opendistrib.net
*
* Ce logiciel est un programme informatique servant à aider les producteurs
* à distribuer leur production en circuits courts.
*
* Ce logiciel est régi par la licence CeCILL soumise au droit français et
* respectant les principes de diffusion des logiciels libres. Vous pouvez
* utiliser, modifier et/ou redistribuer ce programme sous les conditions
* de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA
* sur le site "http://www.cecill.info".
*
* En contrepartie de l'accessibilité au code source et des droits de copie,
* de modification et de redistribution accordés par cette licence, il n'est
* offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons,
* seule une responsabilité restreinte pèse sur l'auteur du programme, le
* titulaire des droits patrimoniaux et les concédants successifs.
*
* A cet égard l'attention de l'utilisateur est attirée sur les risques
* associés au chargement, à l'utilisation, à la modification et/ou au
* développement et à la reproduction du logiciel par l'utilisateur étant
* donné sa spécificité de logiciel libre, qui peut le rendre complexe à
* manipuler et qui le réserve donc à des développeurs et des professionnels
* avertis possédant des connaissances informatiques approfondies. Les
* utilisateurs sont donc invités à charger et tester l'adéquation du
* logiciel à leurs besoins dans des conditions permettant d'assurer la
* sécurité de leurs systèmes et ou de leurs données et, plus généralement,
* à l'utiliser et l'exploiter dans les mêmes conditions de sécurité.
*
* Le fait que vous puissiez accéder à cet en-tête signifie que vous avez
* pris connaissance de la licence CeCILL, et que vous en avez accepté les
* termes.
*/

namespace backend\controllers;

use common\models\Product;
use common\models\User ;
use common\models\Document ;
use common\helpers\GlobalParam ;
use common\models\Order ;
use common\models\User;
use common\models\Document;
use common\helpers\GlobalParam;
use common\models\Order;
use yii\base\UserException;

class DocumentController extends BackendController
{
public function behaviors()
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
],
],
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'roles' => ['@'],
'matchCallback' => function ($rule, $action) {
return User::hasAccessBackend();
}
]
],
],
];
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
],
],
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'roles' => ['@'],
'matchCallback' => function ($rule, $action) {
return User::hasAccessBackend();
}
]
],
],
];
}

public function actionCreate()
@@ -76,18 +76,17 @@ class DocumentController extends BackendController
$model = new $class();

if ($model->load(Yii::$app->request->post())) {
$model->id_producer = GlobalParam::getCurrentProducerId() ;
if($model->save()) {
$model->id_producer = GlobalParam::getCurrentProducerId();
if ($model->save()) {
Yii::$app->getSession()->setFlash('success', $this->getFlashMessage('create', $model));
return $this->redirect(['/'.$model->getControllerUrlPath().'/update', 'id' => $model->id]);
}
else {
return $this->redirect(['/' . $model->getControllerUrlPath() . '/update', 'id' => $model->id]);
} else {
Yii::$app->getSession()->setFlash('error', 'Un problème est survenu lors de la création du document.');
}
}
return $this->render('/document/create', [
'title' => $this->getTitle('Ajouter'),
'typeDocument' => $this->getDocumentType(),
@@ -98,20 +97,20 @@ class DocumentController extends BackendController
/**
* Modifie un modèle Produit existant.
* Si la modification réussit, le navigateur est redirigé vers la page 'index'.
*
*
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id) ;
$model = $this->findModel($id);
$class = $this->getClass();
$model = $class::searchOne([
'id' => $id
]) ;
'id' => $id
]);

if($model->isStatusValid()) {
if ($model->isStatusValid()) {
throw new UserException('Vous ne pouvez pas modifier un document validé.');
}

@@ -119,7 +118,7 @@ class DocumentController extends BackendController

Yii::$app->getSession()->setFlash('success', $this->getFlashMessage('update', $model));
}
return $this->render('/document/update', [
'title' => $this->getTitle('Modifier'),
'typeDocument' => $this->getDocumentType(),
@@ -127,151 +126,152 @@ class DocumentController extends BackendController
]);
}

public function actionAjaxAddressUser($idUser)
public function actionAjaxAddressUser($idUser)
{
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
if($idUser > 0) {
if ($idUser > 0) {
$user = User::findOne($idUser);
if($user) {
$address = $user['lastname'].' '.$user['name']."\n" ;
$address .= $user['address'] ;
if ($user) {
$address = $user['lastname'] . ' ' . $user['name'] . "\n";
$address .= $user['address'];
return [
'return' => 'success',
'address' => $address
] ;
'return' => 'success',
'address' => $address
];
}
}
return ['return' => 'error'] ;
return ['return' => 'error'];
}

public function actionAjaxValidateDocument($idDocument, $classDocument)
{
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

if($idDocument > 0 && Document::isValidClass($classDocument)) {
if ($idDocument > 0 && Document::isValidClass($classDocument)) {
$document = $classDocument::searchOne([
'id' => $idDocument
]) ;
]);

if($document) {
$document->changeStatus(Document::STATUS_VALID) ;
$document->save() ;
return ['return' => 'success'] ;
if ($document) {
$document->changeStatus(Document::STATUS_VALID);
$document->save();
return ['return' => 'success'];
}
}

return ['return' => 'error'] ;
return ['return' => 'error'];
}
public function actionAjaxInit($idDocument, $classDocument)
public function actionAjaxInit($idDocument, $classDocument)
{
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
if($idDocument > 0 && Document::isValidClass($classDocument)) {
if ($idDocument > 0 && Document::isValidClass($classDocument)) {
$document = $classDocument::searchOne([
'id' => $idDocument
]) ;
if($document) {
$productsArray = Product::searchAll() ;
$ordersArray = [] ;
foreach($document->orders as $order) {
$order->init() ;
$productsOrderArray = [] ;
foreach($order->productOrder as $productOrder) {
$productsOrderArray[$productOrder->id] = $productOrder->getAttributes() ;
'id' => $idDocument
]);
if ($document) {
$productsArray = Product::searchAll();
$ordersArray = [];
foreach ($document->orders as $order) {
$order->init();
$productsOrderArray = [];
foreach ($order->productOrder as $productOrder) {
$productsOrderArray[$productOrder->id] = $productOrder->getAttributes();
}
$ordersArray[$order->id] = array_merge(
$order->getAttributes(),
[
'productOrder' => $productsOrderArray,
'productOrder' => $productsOrderArray,
]
);
}
return [
'return' => 'success',
'document' => array_merge($document->getAttributes(), [
'html_label' => $document->getHtmlLabel(),
'class' => $document->getClass()
]),
'idUser' => $document->user->id,
'products' => ArrayHelper::map($productsArray, 'id', function($product) {
return array_merge($product->getAttributes(),[
'price_with_tax' => $product->price_with_tax,
'wording_unit' => $product->wording_unit,
]) ;
}),
'orders' => $ordersArray,
'total' => $document->getAmount(Order::AMOUNT_TOTAL)
] ;
'return' => 'success',
'document' => array_merge($document->getAttributes(), [
'html_label' => $document->getHtmlLabel(),
'class' => $document->getClass()
]),
'idUser' => $document->user->id,
'products' => ArrayHelper::map($productsArray, 'id', function ($product) {
return array_merge($product->getAttributes(), [
'price_with_tax' => $product->price_with_tax,
'wording_unit' => $product->wording_unit,
'tax_rate' => $product->taxRate->value
]);
}),
'orders' => $ordersArray,
'total' => $document->getAmount(Order::AMOUNT_TOTAL),
'total_with_tax' => $document->getAmountWithTax(Order::AMOUNT_TOTAL),
];
}
}
return ['return' => 'error'] ;
return ['return' => 'error'];
}
public function actionAjaxAddProduct($idDocument, $classDocument, $idProduct, $quantity, $price)
public function actionAjaxAddProduct($idDocument, $classDocument, $idProduct, $quantity, $price)
{

\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
if(Document::isValidClass($classDocument)) {
if (Document::isValidClass($classDocument)) {
$document = $classDocument::searchOne([
'id' => $idDocument
]) ;
'id' => $idDocument
]);
$product = Product::searchOne([
'id' => $idProduct
]) ;
if($document && $product) {
if(count($document->orders) == 0) {
$order = new Order ;
$order->id_user = $document->id_user ;
$order->id_point_sale = null ;
$order->id_distribution = null ;
$order->origin = Order::ORIGIN_ADMIN ;
'id' => $idProduct
]);
if ($document && $product) {
if (count($document->orders) == 0) {
$order = new Order;
$order->id_user = $document->id_user;
$order->id_point_sale = null;
$order->id_distribution = null;
$order->origin = Order::ORIGIN_ADMIN;
$order->date = date('Y-m-d H:i:s');
$fieldIdDocument = 'id_'.$classDocument::tableName() ;
$order->$fieldIdDocument = $document->id ;
$order->save() ;
}
else {
$order = $document->orders[0] ;
$fieldIdDocument = 'id_' . $classDocument::tableName();
$order->$fieldIdDocument = $document->id;
$order->save();
} else {
$order = $document->orders[0];
}

if($order) {
$productOrder = new ProductOrder ;
$productOrder->id_order = $order->id ;
$productOrder->id_product = $idProduct ;
if ($order) {
$productOrder = new ProductOrder;
$productOrder->id_order = $order->id;
$productOrder->id_product = $idProduct;
$quantity = $quantity / Product::$unitsArray[$product->unit]['coefficient'];
$productOrder->quantity = $quantity ;
$productOrder->price = (float) $price ;
$productOrder->unit = $product->unit ;
$productOrder->step = $product->step ;
$productOrder->quantity = $quantity;
$productOrder->price = (float)$price;
$productOrder->unit = $product->unit;
$productOrder->step = $product->step;
$productOrder->id_tax_rate = $productOrder->id_tax_rate ?
$product->taxRate->id : GlobalParam::getCurrentProducer()->taxRate->id ;
$productOrder->save() ;
$product->taxRate->id : GlobalParam::getCurrentProducer()->taxRate->id;
$productOrder->save();
return [
'return' => 'success',
] ;
}
];
}
}
}
return [
'return' => 'error'
] ;
];
}

public function actionAjaxDeleteProductOrder($idProductOrder)
@@ -280,74 +280,71 @@ class DocumentController extends BackendController

$productOrder = ProductOrder::searchOne([
'id' => $idProductOrder
]) ;
]);

if($productOrder) {
$productOrder->delete() ;
if ($productOrder) {
$productOrder->delete();
return [
'return' => 'success'
] ;
];
}

return [
'return' => 'error'
] ;
];
}
protected function getClass()
{
$class = get_class($this);
$class = str_replace('Controller', '', $class) ;
$class = str_replace('backend\controllers\\', '', $class) ;
return $class ;
$class = str_replace('Controller', '', $class);
$class = str_replace('backend\controllers\\', '', $class);
return $class;
}
protected function getDocumentType()
protected function getDocumentType()
{
$class = $this->getClass();
if($class == 'Invoice') {
$documentType = 'Facture' ;
}
elseif($class == 'DeliveryNote') {
$documentType = 'Bon de livraison' ;
}
elseif($class == 'Quotation') {
$documentType = 'Devis' ;
$class = $this->getClass();

if ($class == 'Invoice') {
$documentType = 'Facture';
} elseif ($class == 'DeliveryNote') {
$documentType = 'Bon de livraison';
} elseif ($class == 'Quotation') {
$documentType = 'Devis';
}
if(isset($documentType)) {
return $documentType ;

if (isset($documentType)) {
return $documentType;
}
return '' ;
return '';
}
public function getFlashMessage($type = 'create', $model)
public function getFlashMessage($type = 'create', $model)
{
$class = $this->getClass();
$message = $this->getDocumentType() ;
$message .= ' <strong>'.Html::encode($model->name).'</strong> ' ;
if($type == 'create') {
$message .= 'ajouté' ;
}
elseif($type == 'update') {
$message .= 'modifié' ;

$message = $this->getDocumentType();
$message .= ' <strong>' . Html::encode($model->name) . '</strong> ';

if ($type == 'create') {
$message .= 'ajouté';
} elseif ($type == 'update') {
$message .= 'modifié';
}
if($class == 'Invoice') {
$message .= 'e' ;
if ($class == 'Invoice') {
$message .= 'e';
}
return $message ;
return $message;
}

protected function getTitle($prepend)
{
$class = $this->getClass();
switch ($class) {
case 'Invoice' :
$title = $prepend . ' une facture';
@@ -365,15 +362,15 @@ class DocumentController extends BackendController

/**
* Recherche un Document en fonction de son ID.
*
*
* @param integer $id
* @return Produit
* @return Produit
* @throws NotFoundHttpException si le modèle n'est pas trouvé
*/
protected function findModel($id)
{
$class = $this->getClass() ;
$class = $this->getClass();
if (($model = $class::findOne($id)) !== null) {
return $model;
} else {

+ 3
- 3
backend/controllers/OrderController.php View File

@@ -991,7 +991,7 @@ class OrderController extends BackendController

if (abs($order->amount - $amountPaid) < 0.0001) {
$html .= '<span class="label label-success">Payé</span>';
$buttonsCredit = Html::a('Rembourser ' . $order->getAmount(Order::AMOUNT_TOTAL, true), 'javascript:void(0);', ['class' => 'btn btn-default btn-xs rembourser', 'data-montant' => $order->amount, 'data-type' => 'refund']);
$buttonsCredit = Html::a('Rembourser ' . $order->getAmountWithTax(Order::AMOUNT_TOTAL, true), 'javascript:void(0);', ['class' => 'btn btn-default btn-xs rembourser', 'data-montant' => $order->amount, 'data-type' => 'refund']);
} elseif ($order->amount > $amountPaid) {
$amountToPay = $order->amount - $amountPaid;
$html .= '<span class="label label-danger">Non payé</span> reste <strong>' . number_format($amountToPay, 2) . ' €</strong> à payer';
@@ -1023,8 +1023,8 @@ class OrderController extends BackendController
. '<td>' . date('d/m/Y H:i:s', strtotime($h->date)) . '</td>'
. '<td>' . Html::encode($h->strUserAction()) . '</td>'
. '<td>' . $h->getStrWording() . '</td>'
. '<td>' . ($h->isTypeDebit() ? '- ' . $h->getAmount(Order::AMOUNT_TOTAL, true) : '') . '</td>'
. '<td>' . ($h->isTypeCredit() ? '+ ' . $h->getAmount(Order::AMOUNT_TOTAL, true) : '') . '</td>'
. '<td>' . ($h->isTypeDebit() ? '- ' . $h->getAmountWithTax(Order::AMOUNT_TOTAL, true) : '') . '</td>'
. '<td>' . ($h->isTypeCredit() ? '+ ' . $h->getAmountWithTax(Order::AMOUNT_TOTAL, true) : '') . '</td>'
. '</tr>';
}
} else {

+ 1
- 1
backend/views/delivery-note/index.php View File

@@ -95,7 +95,7 @@ $this->addButton(['label' => 'Nouveau bon de livraison <span class="glyphicon gl
'attribute' => 'amount',
'header' => 'Montant',
'value' => function($invoice) {
return $invoice->getAmount(Order::AMOUNT_TOTAL, true) ;
return $invoice->getAmountWithTax(Order::AMOUNT_TOTAL, true) ;
}
],
[

+ 22
- 9
backend/views/document/_form.php View File

@@ -100,7 +100,7 @@ use common\models\Producer;
<span class="info-box-icon bg-yellow"><i class="fa fa-euro"></i></span>
<div class="info-box-content">
<span class="info-box-text">Total (TTC)</span>
<span class="info-box-number">{{ formatPrice(total) }}</span>
<span class="info-box-number">{{ formatPrice(total_with_tax) }}</span>
</div>
</div>
<div id="" class="info-box">
@@ -190,23 +190,26 @@ use common\models\Producer;
<th>Nom</th>
<th>Prix (unité)</th>
<th>Quantité</th>
<th>Total</th>
<th>TVA</th>
<th>Total TTC</th>
<th>Supprimer</th>
</tr>
</thead>
<tbody>
<template v-for="order in ordersArray">
<tr v-for="productOrder in order.productOrder">
<td class="col-md-4">{{
productsArray[productOrder.id_product].name
}}
<td class="col-md-4">
{{ productsArray[productOrder.id_product].name }}
</td>
<td class="col-md-2">{{ formatPrice(productOrder.price) }}</td>
<td class="col-md-2">{{ productOrder.quantity }}</td>
<td class="col-md-2">{{ formatPrice(productOrder.quantity *
productOrder.price) }}
<td class="col-md-1">
{{ productsArray[productOrder.id_product].tax_rate * 100 }} %
</td>
<td class="col-md-2">
{{ formatPrice(productOrder.quantity * productOrder.price) }}
</td>
<td class="col-md-1">
<a class="btn btn-default" @click="deleteProductOrder(productOrder.id)">
<span class="glyphicon glyphicon-trash"></span>
</a>
@@ -214,8 +217,18 @@ use common\models\Producer;
</tr>
</template>
<tr>
<td colspan="3"><strong>Total</strong></td>
<td><strong>{{ formatPrice(total) }}</strong></td>
<td colspan="4"><strong>Total HT</strong></td>
<td><strong>{{ formatPrice(total) }} HT</strong></td>
<td></td>
</tr>
<tr>
<td colspan="4"><strong>Montant TVA</strong></td>
<td><strong>{{ formatPrice(total_with_tax - total) }}</strong></td>
<td></td>
</tr>
<tr>
<td colspan="4"><strong>Total TTC</strong></td>
<td><strong>{{ formatPrice(total_with_tax) }} TTC</strong></td>
<td></td>
</tr>
</tbody>

+ 1
- 1
backend/views/invoice/index.php View File

@@ -94,7 +94,7 @@ $this->addButton(['label' => 'Nouvelle facture <span class="glyphicon glyphicon-
'attribute' => 'amount',
'header' => 'Montant',
'value' => function($invoice) {
return $invoice->getAmount(Order::AMOUNT_TOTAL, true) ;
return $invoice->getAmountWithTax(Order::AMOUNT_TOTAL, true) ;
}
],
[

+ 1
- 1
backend/views/quotation/index.php View File

@@ -80,7 +80,7 @@ $this->addButton(['label' => 'Nouveau devis <span class="glyphicon glyphicon-plu
'attribute' => 'amount',
'header' => 'Montant',
'value' => function($invoice) {
return $invoice->getAmount(Order::AMOUNT_TOTAL, true) ;
return $invoice->getAmountWithTax(Order::AMOUNT_TOTAL, true) ;
}
],
[

+ 1
- 1
backend/views/site/index.php View File

@@ -174,7 +174,7 @@ $this->title = 'Tableau de bord';
</td>
<td><?= $order->getCartSummary() ; ?></td>
<td><?= $order->getPointSaleSummary() ; ?></td>
<td><?= $order->getAmount(Order::AMOUNT_TOTAL, true) ; ?></td>
<td><?= $order->getAmountWithTax(Order::AMOUNT_TOTAL, true) ; ?></td>
<td class="history"><?= $order->getStrHistory() ; ?></td>
</tr>
<?php endforeach; ?>

+ 3
- 1
backend/web/js/vuejs/document-form.js View File

@@ -47,7 +47,8 @@ var app = new Vue({
productAddPrice: '',
productAddQuantity: 1,
ordersArray: [],
total: 0
total: 0,
total_with_tax: 0
},
mounted: function() {
this.init() ;
@@ -73,6 +74,7 @@ var app = new Vue({
app.productsArray = response.data.products ;
app.ordersArray = response.data.orders ;
app.total = response.data.total ;
app.total_with_tax = response.data.total_with_tax ;
}
}) ;
}

+ 1
- 1
common/helpers/Tiller.php View File

@@ -87,7 +87,7 @@ class Tiller
$ordersOpendistribSynchro[$orderOpendistrib->id] = false ;
foreach($ordersTiller->orders as $orderTiller) {
if($orderOpendistrib->id == $orderTiller->externalId
&& ($orderOpendistrib->getAmount(Order::AMOUNT_TOTAL) * 100) == $orderTiller->currentBill) {
&& ($orderOpendistrib->getAmountWithTax(Order::AMOUNT_TOTAL) * 100) == $orderTiller->currentBill) {

$ordersOpendistribSynchro[$orderOpendistrib->id] = true ;
}

+ 1
- 1
common/models/CreditHistory.php View File

@@ -328,7 +328,7 @@ class CreditHistory extends ActiveRecordCommon
}
$str .= $this->getStrWording() ;
if(isset($this->order)) {
$str .= '<br />Montant de la commande : '.$this->order->getAmount(Order::AMOUNT_TOTAL, true) ;
$str .= '<br />Montant de la commande : '.$this->order->getAmountWithTax(Order::AMOUNT_TOTAL, true) ;
}
if(isset($this->user)) {
$str .= '<br />Client : '.Html::encode($this->user->name. ' '.$this->user->lastname) ;

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

@@ -102,15 +102,30 @@ class Document extends ActiveRecordCommon
/*
* Méthodes
*/
public function getAmount($type = Order::AMOUNT_TOTAL, $format = false)
{
return $this->_getAmountGeneric($type, false, $format) ;
}

public function getAmountWithTax($type = Order::AMOUNT_TOTAL, $format = false)
{
return $this->_getAmountGeneric($type, true, $format) ;
}

protected function _getAmountGeneric($type = Order::AMOUNT_TOTAL, $withTax = true, $format = false)
{
$amount = 0;
$ordersArray = $this->orders;

foreach ($ordersArray as $order) {
$order->init();
$amount += $order->getAmount($type);
if($withTax) {
$amount += $order->getAmountWithTax($type);
}
else {
$amount += $order->getAmount($type);
}
}

if ($format) {

+ 30
- 13
common/models/Order.php View File

@@ -62,6 +62,7 @@ use yii\web\NotFoundHttpException;
class Order extends ActiveRecordCommon
{
var $amount = 0;
var $amount_with_tax = 0;
var $paid_amount = 0;
var $weight = 0;

@@ -99,6 +100,7 @@ class Order extends ActiveRecordCommon
[['id_user', 'date', 'id_point_sale', 'id_distribution', 'status'], 'required', 'message' => ''],
[['id_user', 'id_point_sale', 'id_distribution', 'id_subscription', 'id_invoice', 'id_quotation', 'id_delivery_note'], 'integer'],
[['auto_payment', 'tiller_synchronization'], 'boolean'],
[['status'], 'string'],
[['date', 'date_update', 'comment', 'comment_point_sale', 'mean_payment'], 'safe']
];
}
@@ -210,11 +212,13 @@ class Order extends ActiveRecordCommon
public function initAmount()
{
$this->amount = 0;
$this->amount_with_tax = 0;
$this->weight = 0;

if (isset($this->productOrder)) {
foreach ($this->productOrder as $productOrder) {
$this->amount += Price::getPriceWithTax($productOrder->price, $productOrder->taxRate->value) * $productOrder->quantity;
$this->amount += $productOrder->price * $productOrder->quantity;
$this->amount_with_tax += Price::getPriceWithTax($productOrder->price, $productOrder->taxRate->value) * $productOrder->quantity;
if ($productOrder->unit == 'piece') {
if (isset($productOrder->product)) {
$this->weight += ($productOrder->quantity * $productOrder->product->weight) / 1000;
@@ -376,28 +380,41 @@ class Order extends ActiveRecordCommon
* @param boolean $format
* @return float
*/

public function getAmount($type = self::AMOUNT_TOTAL, $format = false)
{
return $this->_getAmountGeneric($type, $this->amount, $format) ;
}

public function getAmountWithTax($type = self::AMOUNT_TOTAL, $format = false)
{
return $this->_getAmountGeneric($type, $this->amount_with_tax, $format) ;
}

protected function _getAmountGeneric($type, $amountOrder, $format)
{
switch ($type) {
case self::AMOUNT_TOTAL :
$amount = $this->amount;
$amount = $amountOrder;
break;
case self::AMOUNT_PAID :
$this->initPaidAmount();
$amount = $this->paid_amount;
break;
case self::AMOUNT_REMAINING :
$amount = $this->getAmount(self::AMOUNT_TOTAL)
- $this->getAmount(self::AMOUNT_PAID);
$amount = $this->getAmountWithTax(self::AMOUNT_TOTAL)
- $this->getAmountWithTax(self::AMOUNT_PAID);
break;
case self::AMOUNT_SURPLUS :
$amount = $this->getAmount(self::AMOUNT_PAID)
- $this->getAmount(self::AMOUNT_TOTAL);
$amount = $this->getAmountWithTax(self::AMOUNT_PAID)
- $this->getAmountWithTax(self::AMOUNT_TOTAL);
break;
default:
throw new NotFoundHttpException('Type de montant inconnu.') ;
}

if ($format) {
return number_format($amount, 2) . ' €';
return Price::format($amount) ;
} else {
return $amount;
}
@@ -417,7 +434,7 @@ class Order extends ActiveRecordCommon
$jsonOrder = [
'products' => [],
'amount' => $order->amount,
'str_amount' => $order->getAmount(self::AMOUNT_TOTAL, true),
'str_amount' => $order->getAmountWithTax(self::AMOUNT_TOTAL, true),
'paid_amount' => $order->getAmount(self::AMOUNT_PAID),
'comment' => $order->comment,
];
@@ -491,14 +508,14 @@ class Order extends ActiveRecordCommon
public function getPaymentStatus()
{
// payé
if ($this->getAmount() - $this->getAmount(self::AMOUNT_PAID) < 0.01 &&
$this->getAmount() - $this->getAmount(self::AMOUNT_PAID) > -0.01) {
if ($this->getAmountWithtax() - $this->getAmount(self::AMOUNT_PAID) < 0.01 &&
$this->getAmountWithtax() - $this->getAmount(self::AMOUNT_PAID) > -0.01) {
return self::PAYMENT_PAID;
} // à rembourser
elseif ($this->getAmount() - $this->getAmount(self::AMOUNT_PAID) <= -0.01) {
elseif ($this->getAmountWithtax() - $this->getAmount(self::AMOUNT_PAID) <= -0.01) {
return self::PAYMENT_SURPLUS;
} // reste à payer
elseif ($this->getAmount() - $this->getAmount(self::AMOUNT_PAID) >= 0.01) {
elseif ($this->getAmountWithtax() - $this->getAmount(self::AMOUNT_PAID) >= 0.01) {
return self::PAYMENT_UNPAID;
}
}
@@ -567,7 +584,7 @@ class Order extends ActiveRecordCommon
{
$html = '';

$html .= $this->getAmount(self::AMOUNT_TOTAL, true) . '<br />';
$html .= $this->getAmountWithTax(self::AMOUNT_TOTAL, true) . '<br />';

if ($this->paid_amount) {
if ($this->getPaymentStatus() == Order::PAYMENT_PAID) {

+ 2
- 2
producer/controllers/OrderController.php View File

@@ -508,7 +508,7 @@ class OrderController extends ProducerBaseController
if (is_array($ordersUserArray) && count($ordersUserArray)) {
foreach ($ordersUserArray as &$order) {
$order = array_merge($order->getAttributes(), [
'amount_total' => $order->getAmount(Order::AMOUNT_TOTAL),
'amount_total' => $order->getAmountWithTax(Order::AMOUNT_TOTAL),
'date_distribution' => $order->distribution->date,
'pointSale' => $order->pointSale->getAttributes()
]);
@@ -537,7 +537,7 @@ class OrderController extends ProducerBaseController

if ($orderUser) {
$json['order'] = array_merge($orderUser->getAttributes(), [
'amount_total' => $orderUser->getAmount(Order::AMOUNT_TOTAL),
'amount_total' => $orderUser->getAmountWithTax(Order::AMOUNT_TOTAL),
'amount_paid' => $orderUser->getAmount(Order::AMOUNT_PAID),
]);
}

+ 1
- 1
producer/views/order/confirm.php View File

@@ -62,7 +62,7 @@ $this->setTitle('Confirmation de commande') ;
<li><span class="glyphicon glyphicon-time"></span><?= date('d/m/Y',strtotime($order->distribution->date)) ?></li>
<li><span class="glyphicon glyphicon-map-marker"></span><?= Html::encode($order->pointSale->name) ?><?php if(strlen($order->pointSale->name)): ?> <span class="locality">à <?= Html::encode($order->pointSale->locality) ?></span><?php endif; ?></li>
<li><span class="glyphicon glyphicon-th-list"></span><?= $order->countProducts(); ?> produit<?php if($order->countProducts() > 1): ?>s<?php endif; ?></li>
<li><span class="glyphicon glyphicon-chevron-right"></span><?= $order->getAmount(Order::AMOUNT_TOTAL, true); ?></li>
<li><span class="glyphicon glyphicon-chevron-right"></span><?= $order->getAmountWithTax(Order::AMOUNT_TOTAL, true); ?></li>
</ul>
</div>
<div class="clr"></div>

Loading…
Cancel
Save