ソースを参照

Refactoring CreditHistory #881

refactoring
Guillaume Bourgeois 1年前
コミット
dc710a88bb
15個のファイルの変更927行の追加939行の削除
  1. +9
    -5
      backend/controllers/DistributionController.php
  2. +7
    -6
      backend/controllers/OrderController.php
  3. +4
    -1
      backend/models/CreditForm.php
  4. +2
    -1
      backend/views/user/credit.php
  5. +33
    -44
      common/models/CreditHistory.php
  6. +7
    -0
      common/models/Distribution.php
  7. +9
    -1
      common/models/Order.php
  8. +745
    -738
      common/models/User.php
  9. +0
    -1
      common/repositories/CreditHistoryRepository.php
  10. +40
    -79
      common/services/CreditHistoryService.php
  11. +9
    -2
      common/services/UserProducerService.php
  12. +1
    -1
      composer.json
  13. +2
    -2
      composer.lock
  14. +3
    -1
      producer/controllers/CreditController.php
  15. +56
    -57
      producer/views/credit/history.php

+ 9
- 5
backend/controllers/DistributionController.php ファイルの表示

@@ -109,6 +109,8 @@ class DistributionController extends BackendController
{
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

$creditHistoryService = \Yii::$app->logic->getCreditHistoryContainer()->getService();

$json = [
'distribution' => [],
'products' => []
@@ -271,14 +273,16 @@ class DistributionController extends BackendController
foreach ($order->creditHistory as $creditHistory) {
$creditHistoryArray[] = [
'date' => date('d/m/Y H:i:s', strtotime($creditHistory->date)),
'user' => $creditHistory->user->getUsername(),
'user_action' => $creditHistory->strUserAction(),
'wording' => $creditHistory->getStrWording(),
'debit' => ($creditHistory->isTypeDebit() ? '- ' . $creditHistory->getAmount(
'user' => $creditHistory->getUserObject()->getUsername(),
'user_action' => $creditHistoryService->getStrUserAction($creditHistory),
'wording' => $creditHistoryService->getStrWording($creditHistory),
'debit' => ($creditHistoryService->isTypeDebit($creditHistory) ? '- ' . $creditHistoryService->getAmount(
$creditHistory,
Order::AMOUNT_TOTAL,
true
) : ''),
'credit' => ($creditHistory->isTypeCredit() ? '+ ' . $creditHistory->getAmount(
'credit' => ($creditHistoryService->isTypeCredit($creditHistory) ? '+ ' . $creditHistoryService->getAmount(
$creditHistory,
Order::AMOUNT_TOTAL,
true
) : '')

+ 7
- 6
backend/controllers/OrderController.php ファイルの表示

@@ -1051,6 +1051,7 @@ class OrderController extends BackendController
*/
public function actionPaymentStatus($idOrder)
{
$creditHistoryService = \Yii::$app->logic->getCreditHistoryContainer()->getService();
$order = Order::searchOne(['id' => $idOrder]);

if ($order) {
@@ -1095,13 +1096,13 @@ class OrderController extends BackendController
. '<tbody>';

if ($history && is_array($history) && count($history)) {
foreach ($history as $h) {
foreach ($history as $creditHistory) {
$html .= '<tr>'
. '<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->getAmountWithTax(Order::AMOUNT_TOTAL, true) : '') . '</td>'
. '<td>' . ($h->isTypeCredit() ? '+ ' . $h->getAmountWithTax(Order::AMOUNT_TOTAL, true) : '') . '</td>'
. '<td>' . date('d/m/Y H:i:s', strtotime($creditHistoryService->getDate($creditHistory))) . '</td>'
. '<td>' . Html::encode($creditHistoryService->getStrUserAction($creditHistory)) . '</td>'
. '<td>' . $creditHistoryService->getStrWording($creditHistory) . '</td>'
. '<td>' . ($creditHistoryService->isTypeDebit($creditHistory) ? '- ' . $creditHistoryService->getAmountWithTax($creditHistory, Order::AMOUNT_TOTAL, true) : '') . '</td>'
. '<td>' . ($creditHistoryService->isTypeCredit($creditHistory) ? '+ ' . $creditHistoryService->getAmountWithTax($creditHistory, Order::AMOUNT_TOTAL, true) : '') . '</td>'
. '</tr>';
}
} else {

+ 4
- 1
backend/models/CreditForm.php ファイルの表示

@@ -101,6 +101,8 @@ class CreditForm extends Model
*/
public function save()
{
$creditHistoryService = Yii::$app->logic->getCreditHistoryContainer()->getService();

if ($this->validate()) {
$creditHistory = new CreditHistory;
$creditHistory->id_user = $this->id_user;
@@ -110,7 +112,8 @@ class CreditForm extends Model
$creditHistory->comment = $this->comment ;
$creditHistory->amount = $this->amount ;
$creditHistory->mean_payment = $this->mean_payment ;
$creditHistory->save();

$creditHistoryService->save($creditHistory);
// on prévient l'utilisateur que son compte vient d'être crédité
if($this->send_mail) {

+ 2
- 1
backend/views/user/credit.php ファイルの表示

@@ -128,8 +128,9 @@ $this->addBreadcrumb('Créditer') ;
<tbody>
<?php if(count($history)): ?>
<?php foreach($history as $creditHistory): ?>

<tr>
<td><?= $creditHistory->getDate(true) ; ?></td>
<td><?= $creditHistoryService->getDate($creditHistory, true) ; ?></td>
<td><?= Html::encode($creditHistoryService->getStrUserAction($creditHistory)); ?></td>
<td><?= $creditHistoryService->getStrWording($creditHistory); ?></td>
<td>

+ 33
- 44
common/models/CreditHistory.php ファイルの表示

@@ -39,19 +39,9 @@
namespace common\models;

use common\components\ActiveRecordCommon;
use yii\db\ActiveQuery;


/**
* This is the model class for table "credit_historique".
*
* @property integer $id
* @property integer $id_user
* @property integer $id_order
* @property string $date
* @property double $amount
* @property string $type
* @property integer $id_producer
* @property string $mean_payment
*/
class CreditHistory extends ActiveRecordCommon
{
const TYPE_INITIAL_CREDIT = 'initial-credit';
@@ -63,7 +53,7 @@ class CreditHistory extends ActiveRecordCommon
/**
* @inheritdoc
*/
public static function tableName()
public static function tableName(): string
{
return 'credit_history';
}
@@ -71,7 +61,7 @@ class CreditHistory extends ActiveRecordCommon
/**
* @inheritdoc
*/
public function rules()
public function rules(): array
{
return [
[['amount'], 'required'],
@@ -86,7 +76,7 @@ class CreditHistory extends ActiveRecordCommon
/**
* @inheritdoc
*/
public function attributeLabels()
public function attributeLabels(): array
{
return [
'id' => 'ID',
@@ -106,148 +96,147 @@ class CreditHistory extends ActiveRecordCommon
* Relations
*/

public function getUser()
public function getUser(): ActiveQuery
{
return $this->hasOne(User::className(), ['id' => 'id_user']);
return $this->hasOne(User::class, ['id' => 'id_user']);
}

public function getUserObject()
public function getUserObject(): ?User
{
return $this->user;
}

public function getUserAction()
public function getUserAction(): ActiveQuery
{
return $this->hasOne(User::className(), ['id' => 'id_user_action']);
return $this->hasOne(User::class, ['id' => 'id_user_action']);
}

public function getUserActionObject()
public function getUserActionObject(): ?User
{
return $this->userAction;
}

public function getOrder()
public function getOrder(): ActiveQuery
{
return $this->hasOne(Order::className(), ['id' => 'id_order']);
return $this->hasOne(Order::class, ['id' => 'id_order']);
}

public function getOrderObject()
public function getOrderObject(): ?Order
{
return $this->order;
}


/*
* Getters / setters
*/

public function getId()
public function getId(): int
{
return $this->id;
}

public function getIdUser()
public function getIdUser(): ?int
{
return $this->id_user;
}

public function setIdUser($idUser)
public function setIdUser(?int $idUser): self
{
$this->id_user = $idUser;

return $this;
}

public function getIdUserAction()
public function getIdUserAction(): ?int
{
return $this->id_user_action;
}

public function setIdUserAction($idUserAction)
public function setIdUserAction(?int $idUserAction): self
{
$this->id_user_action = $idUserAction;

return $this;
}

public function getIdOrder()
public function getIdOrder(): ?int
{
return $this->id_order;
}

public function setIdOrder($idOrder)
public function setIdOrder(?int $idOrder): self
{
$this->id_order = $idOrder;

return $this;
}

public function getDate()
public function getDate(): ?string
{
return $this->date;
}

public function setDate($date)
public function setDate(?string $date): self
{
$this->date = $date;

return $this;
}

public function getAmount()
public function getAmount(): ?float
{
return $this->amount;
}

public function setAmount($amount)
public function setAmount(?float $amount): self
{
$this->amount = $amount;

return $this;
}

public function getType()
public function getType(): ?string
{
return $this->type;
}

public function setType($type)
public function setType(?string $type): self
{
$this->type = $type;

return $this;
}

public function getIdProducer()
public function getIdProducer(): ?int
{
return $this->id_producer;
}

public function setIdProducer($idProducer)
public function setIdProducer(?int $idProducer): self
{
$this->id_producer = $idProducer;

return $this;
}

public function getMeanPayment()
public function getMeanPayment(): ?string
{
return $this->mean_payment;
}

public function setMeanPayment($meanPayment)
public function setMeanPayment(?string $meanPayment): self
{
$this->mean_payment = $meanPayment;

return $this;
}

public function getComment()
public function getComment(): ?string
{
return $this->comment;
}

public function setComment($comment)
public function setComment(?string $comment): self
{
$this->comment = $comment;


+ 7
- 0
common/models/Distribution.php ファイルの表示

@@ -123,6 +123,13 @@ class Distribution extends ActiveRecordCommon
];
}


public function getDate(): string
{
return $this->date;
}


/**
* Retourne si un produit est actif ou non.
*

+ 9
- 1
common/models/Order.php ファイルの表示

@@ -168,6 +168,11 @@ class Order extends ActiveRecordCommon
->with('producer');
}

public function getDistributionOject(): ?Distribution
{
return $this->distribution;
}

public function getPointSale()
{
return $this->hasOne(PointSale::className(), ['id' => 'id_point_sale'])
@@ -552,6 +557,8 @@ class Order extends ActiveRecordCommon
*/
public function saveCreditHistory($type, $amount, $idProducer, $idUser, $idUserAction)
{
$creditHistoryService = Yii::$app->logic->getCreditHistoryContainer()->getService();

$creditHistory = new CreditHistory;
$creditHistory->id_user = $idUser;
$creditHistory->id_order = $this->id;
@@ -561,7 +568,8 @@ class Order extends ActiveRecordCommon
$creditHistory->id_user_action = $idUserAction;
$creditHistory->populateRelation('order', $this);
$creditHistory->populateRelation('user', User::find()->where(['id' => $this->id_user])->one());
$creditHistory->save();

$creditHistoryService->save($creditHistory);
}

/**

+ 745
- 738
common/models/User.php
ファイル差分が大きすぎるため省略します
ファイルの表示


+ 0
- 1
common/repositories/CreditHistoryRepository.php ファイルの表示

@@ -3,7 +3,6 @@
namespace common\repositories;

use common\models\CreditHistory;
use common\models\User;

class CreditHistoryRepository
{

+ 40
- 79
common/services/CreditHistoryService.php ファイルの表示

@@ -9,33 +9,25 @@ use yii\helpers\Html;

class CreditHistoryService
{
/**
* Enregistre le modèle.
*
* @param boolean $runValidation
* @param array $attributeNames
*/
public function save($creditHistory)

public function save(CreditHistory $creditHistory): bool
{
if ($creditHistory->getAmount() > -0.01 && $creditHistory->getAmount() < 0.01) {
return false;
}

// initialisation du commentaire avant sauvegarde
// Initialisation du commentaire avant sauvegarde
$creditHistory->setComment($creditHistory->getComment() . $this->getStrComment($creditHistory));

$creditHistory->save();

// Mise à jour du crédit au niveau de UserProducer
Yii::$app->logic->getUserProducerContainer()->getService()->updateCredit($creditHistory);
\Yii::$app->logic->getUserProducerContainer()->getService()->updateCredit($creditHistory);

return true;
}

/**
* Retourne si le CreditHistorique est un débit ou non.
*
* @return boolean
*/
public function isTypeDebit($creditHistory)
public function isTypeDebit(CreditHistory $creditHistory): bool
{
return in_array($creditHistory->type, [
CreditHistory::TYPE_DEBIT,
@@ -43,12 +35,7 @@ class CreditHistoryService
]);
}

/**
* Retourne si le CreditHistorique est un crédit ou non.
*
* @return boolean
*/
public function isTypeCredit($creditHistory)
public function isTypeCredit(CreditHistory $creditHistory): bool
{
return in_array($creditHistory->type, [
CreditHistory::TYPE_CREDIT,
@@ -57,18 +44,11 @@ class CreditHistoryService
]);
}

/**
* Retourne le montant.
*
* @param boolean $format
* @return float
*/
public function getAmount($creditHistory, $format = false)
public function getAmount(CreditHistory $creditHistory, bool $format = false): string
{
if ($format) {
return number_format($creditHistory->amount, 2) . '&nbsp;€';
}
else {
} else {
return $creditHistory->amount;
}
}
@@ -77,33 +57,29 @@ class CreditHistoryService
* Retourne le libellé du CreditHistory informant de son type et
* éventuellement de la date de sa commande associée.
*
* @return string
*/
public function getStrWording($creditHistory)
public function getStrWording(CreditHistory $creditHistory): string
{
$str = '';
$type = $creditHistory->getType();

if ($creditHistory->type == CreditHistory::TYPE_INITIAL_CREDIT) {
if (CreditHistory::TYPE_INITIAL_CREDIT == $type) {
$str = 'Crédit initial';
}
elseif ($creditHistory->type == CreditHistory::TYPE_CREDIT) {
} elseif (CreditHistory::TYPE_CREDIT == $type) {
$str = 'Crédit';
}
elseif ($creditHistory->type == CreditHistory::TYPE_PAYMENT) {
} elseif (CreditHistory::TYPE_PAYMENT == $type) {
$str = 'Paiement';
}
elseif ($creditHistory->type == CreditHistory::TYPE_REFUND) {
} elseif (CreditHistory::TYPE_REFUND == $type) {
$str = 'Remboursement';
}
elseif ($creditHistory->type == CreditHistory::TYPE_DEBIT) {
} elseif (CreditHistory::TYPE_DEBIT == $type) {
$str = 'Débit';
}

if ($creditHistory->type == CreditHistory::TYPE_PAYMENT || $creditHistory->type == CreditHistory::TYPE_REFUND) {
if (isset($creditHistory->order) && isset($creditHistory->order->distribution)) {
$str .= '<br />Commande : ' . date('d/m/Y', strtotime($creditHistory->order->distribution->date));
}
else {
if (CreditHistory::TYPE_PAYMENT == $type || CreditHistory::TYPE_REFUND == $type) {
$order = $creditHistory->getOrderObject();
if ($order && $order->getDistributionOject()) {
$str .= '<br />Commande : ' . date('d/m/Y', strtotime($order->getDistributionOject()->getDate()));
} else {
$str .= '<br />Commande supprimée';
}
}
@@ -115,73 +91,58 @@ class CreditHistoryService
* Retourne les informations à ajouter au commentaire du CreditHistorique
* (libellé, montant, client, action) au format HTML.
*
* @return string
*/
public function getStrComment($creditHistory)
public function getStrComment(CreditHistory $creditHistory): string
{
$str = '';
if (strlen($creditHistory->getComment())) {
$str .= '<br />';
}

$str .= $this->getStrWording();
if ($creditHistory->getOrder()) {
$str .= '<br />Montant de la commande : ' . $creditHistory->getOrder()->getAmountWithTax(Order::AMOUNT_TOTAL, true);
$str .= $this->getStrWording($creditHistory);

$order = $creditHistory->getOrderObject();
if ($order) {
$str .= '<br />Montant de la commande : ' . $order->getAmountWithTax(Order::AMOUNT_TOTAL, true);
}

if ($creditHistory->getUser()) {
$str .= '<br />Client : ' . Html::encode($creditHistory->getUser()->name . ' ' . $creditHistory->user->lastname);
$user = $creditHistory->getUserObject();
if ($user) {
$str .= '<br />Client : ' . Html::encode($user->getName() . ' ' . $user->getLastname());
}

if ($creditHistory->getUserAction()) {
$str .= '<br />Action : ' . Html::encode($creditHistory->getUserAction()->name . ' ' . $creditHistory->userAction->lastname);
$userAction = $creditHistory->getUserActionObject();
if ($userAction) {
$str .= '<br />Action : ' . Html::encode($userAction->getName() . ' ' . $userAction->getLastname());
}

return $str;
}

/**
* Retourne la date.
*
* @param boolean $format
* @return string
*/
public function getDate($creditHistory, $format = false)
public function getDate(CreditHistory $creditHistory, bool $format = false): string
{
$date = $creditHistory->getDate();

if ($format) {
return date('d/m/Y à H:i:s', strtotime($date));
}
else {
} else {
return $date;
}
}

/**
* Retourne le libellé du moyen de paiement du CreditHistory courant.
*
* @return string
*/
public function getStrMeanPayment($creditHistory)
public function getStrMeanPayment(CreditHistory $creditHistory): string
{
return MeanPayment::getStrBy($creditHistory->getMeanPayment());
}

/**
* Retourne le libellé de l'utilisateur ayant initié l'action.
*
* @return string
*/
// strUserAction
public function getStrUserAction($creditHistory)
public function getStrUserAction(CreditHistory $creditHistory): string
{
$userAction = $creditHistory->getUserActionObject();

if ($userAction) {
return $userAction->name . ' ' . $userAction->lastname;
}
else {
return $userAction->getName() . ' ' . $userAction->getlastname();
} else {
return 'Système';
}
}

+ 9
- 2
common/services/UserProducerService.php ファイルの表示

@@ -8,6 +8,13 @@ use common\models\Producer;

class UserProducerService
{
protected CreditHistoryService $creditHistoryService;

public function __construct()
{
$this->creditHistoryService = \Yii::$app->logic->getCreditHistoryContainer()->getService();
}

public function updateCredit($creditHistory)
{
$userProducer = Yii::$app->logic->getUserProducerContainer()->getRepository()->getOne($creditHistory->id_user, $creditHistory->id_producer);
@@ -23,9 +30,9 @@ class UserProducerService

public function deductCredit($userProducer, $creditHistory)
{
if ($creditHistory->isTypeCredit()) {
if ($this->creditHistoryService->isTypeCredit($creditHistory)) {
$userProducer->credit += $creditHistory->amount;
} elseif ($creditHistory->isTypeDebit()) {
} elseif ($this->creditHistoryService->isTypeDebit($creditHistory)) {
$userProducer->credit -= $creditHistory->amount;
}


+ 1
- 1
composer.json ファイルの表示

@@ -14,7 +14,7 @@
},
"minimum-stability": "stable",
"require": {
"php": ">=5.4.0",
"php": ">=7.4",
"yiisoft/yii2": "*",
"yiisoft/yii2-bootstrap": "*",
"yiisoft/yii2-swiftmailer": "*",

+ 2
- 2
composer.lock ファイルの表示

@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "96d4dfea93de951d74c4d1d099ed27ed",
"content-hash": "78a16f604e3b8e2cb623f7204619682f",
"packages": [
{
"name": "2amigos/yii2-chartjs-widget",
@@ -6113,7 +6113,7 @@
"prefer-stable": false,
"prefer-lowest": false,
"platform": {
"php": ">=5.4.0"
"php": ">=7.4"
},
"platform-dev": [],
"plugin-api-version": "2.3.0"

+ 3
- 1
producer/controllers/CreditController.php ファイルの表示

@@ -193,6 +193,7 @@ class CreditController extends ProducerBaseController

public function actionStripeVerification()
{
$creditHistoryService = \Yii::$app->logic->getCreditHistoryContainer()->getService();
$producer = $this->getProducer();
$contactProducer = $producer->getMainContact();

@@ -255,7 +256,8 @@ class CreditController extends ProducerBaseController
$creditHistory->comment = null;
$creditHistory->amount = $amount;
$creditHistory->mean_payment = MeanPayment::CREDIT_CARD;
$creditHistory->save();

$creditHistoryService->save($creditHistory);

if (isset($order) && $order) {
// paiement de la commande

+ 56
- 57
producer/views/credit/history.php ファイルの表示

@@ -36,75 +36,74 @@
* termes.
*/

use yii\helpers\Html;
use yii\grid\GridView;
use ruskid\stripe\StripeCheckoutCustom;
use yii\web\JsExpression;

$creditHistoryService = Yii::$app->logic->getCreditHistoryContainer()->getService();
$producer = $this->context->getProducer();
$this->setTitle('Crédit : <span id="credit-user">' . number_format($creditUser, 2) . ' €</span>');
$this->setPageTitle('Crédit');

if ($this->context->getProducer()->online_payment) {
$this->addButton(
[
'label' => '<span class="glyphicon glyphicon-credit-card"></span> Créditer mon compte',
'url' => 'credit/add',
'class' => 'btn btn-primary'
]
[
'label' => '<span class="glyphicon glyphicon-credit-card"></span> Créditer mon compte',
'url' => 'credit/add',
'class' => 'btn btn-primary'
]
);
}

?>

<?= GridView::widget([
// 'filterModel' => $searchModel,
'dataProvider' => $dataProvider,
'columns' => [
[
'attribute' => 'date',
'value' => function ($model) {
return $model->getDate(true);
}
],
[
'attribute' => 'id_user_action',
'value' => function ($model) {
return $model->strUserAction();
}
],
[
'label' => 'Type',
'format' => 'raw',
'value' => function ($model) {
return $model->getStrWording();
}
],
[
'attribute' => 'mean_payment',
'value' => function ($model) {
return $model->getStrMeanPayment();
}
],
[
'label' => '- Débit',
'format' => 'raw',
'value' => function ($model) {
if ($model->isTypeDebit()) {
return '-&nbsp;' . $model->getAmount(true);
}
return '';
}
],
[
'label' => '+ Crédit',
'format' => 'raw',
'value' => function ($model) {
if ($model->isTypeCredit()) {
return '+&nbsp;' . $model->getAmount(true);
}
return '';
}
],
],
]);
'dataProvider' => $dataProvider,
'columns' => [
[
'attribute' => 'date',
'value' => function ($model) use ($creditHistoryService) {
return $creditHistoryService->getDate($model, true);
}
],
[
'attribute' => 'id_user_action',
'value' => function ($model) use ($creditHistoryService) {
return $creditHistoryService->getStrUserAction($model);
}
],
[
'label' => 'Type',
'format' => 'raw',
'value' => function ($model) use ($creditHistoryService) {
return $creditHistoryService->getStrWording($model);
}
],
[
'attribute' => 'mean_payment',
'value' => function ($model) use ($creditHistoryService) {
return $creditHistoryService->getStrMeanPayment($model);
}
],
[
'label' => '- Débit',
'format' => 'raw',
'value' => function ($model) use ($creditHistoryService) {
if ($creditHistoryService->isTypeDebit($model)) {
return '-&nbsp;' . $creditHistoryService->getAmount($model, true);
}
return '';
}
],
[
'label' => '+ Crédit',
'format' => 'raw',
'value' => function ($model) use ($creditHistoryService) {
if ($creditHistoryService->isTypeCredit($model)) {
return '+&nbsp;' . $creditHistoryService->getAmount($model, true);
}
return '';
}
],
],
]);
?>

読み込み中…
キャンセル
保存