Procházet zdrojové kódy

Refactoring services #885

refactoring
Guillaume před 1 rokem
rodič
revize
a8422dcf1b
8 změnil soubory, kde provedl 95 přidání a 50 odebrání
  1. +0
    -1
      common/logic/Config/TaxRate/TaxRateRepository.php
  2. +33
    -19
      common/logic/Order/Order/OrderBuilder.php
  3. +45
    -8
      common/logic/Order/Order/OrderManager.php
  4. +5
    -2
      common/logic/Order/Order/OrderRepository.php
  5. +0
    -5
      frontend/controllers/SiteController.php
  6. +1
    -1
      producer/controllers/CreditController.php
  7. +9
    -11
      producer/controllers/OrderController.php
  8. +2
    -3
      producer/controllers/SubscriptionController.php

+ 0
- 1
common/logic/Config/TaxRate/TaxRateRepository.php Zobrazit soubor

@@ -7,7 +7,6 @@ use common\logic\RepositoryInterface;

class TaxRateRepository extends BaseService implements RepositoryInterface
{

public function defaultOptionsSearch(): array
{
return [

+ 33
- 19
common/logic/Order/Order/OrderBuilder.php Zobrazit soubor

@@ -32,6 +32,10 @@ use common\logic\User\User\UserSolver;
use common\logic\User\UserProducer\UserProducerRepository;
use yii\web\NotFoundHttpException;

/**
*
*/

class OrderBuilder extends BaseService implements BuilderInterface
{
protected UserSolver $userSolver;
@@ -100,7 +104,7 @@ class OrderBuilder extends BaseService implements BuilderInterface
* Ajoute l'abonnement' pour une date donnée.
*/
// add
public function createOrderFromSubscription(Subscription $subscription, string $date, $force = false)
public function createOrderFromSubscription(Subscription $subscription, string $date, bool $force = false): ?Order
{
$now = date('Y-m-d');
$distributionDate = date('Y-m-d', strtotime($date));
@@ -114,7 +118,11 @@ class OrderBuilder extends BaseService implements BuilderInterface

$order = $this->createOrder($distribution);
$this->updateOrderFromSubscription($order, $subscription);

return $order;
}

return null;
}

/**
@@ -122,8 +130,9 @@ class OrderBuilder extends BaseService implements BuilderInterface
*/
// addAll
// createAllFromSubscriptions
public function createAllOrdersFromSubscriptions(string $date, $force = false): void
public function createAllOrdersFromSubscriptions(string $date, bool $force = false): array
{
$orderArray = [];
$distribution = $this->distributionRepository->getOneByDate(date('Y-m-d', strtotime($date)));

if ($distribution) {
@@ -131,29 +140,34 @@ class OrderBuilder extends BaseService implements BuilderInterface
$subscriptionArray = $this->subscriptionRepository->getByDate($date);
foreach ($subscriptionArray as $subscription) {
if (!$this->subscriptionSolver->hasOrderAlreadyExist($subscription, $orderArray)) {
$this->createFromSubscription($subscription, $date, $force);
$this->createOrderFromSubscription($subscription, $date, $force);
}
}
}

return $orderArray;
}

// updateIncomingDistributions
public function updateOrdersIncomingDistributionsFromSubscription(Subscription $subscription, $update = false): void
public function updateOrdersIncomingDistributionsFromSubscription(Subscription $subscription, $update = false): array
{
$orderArray = [];
$matchedDistributionsArray = $this->distributionRepository->searchMatchedIncomingDistributions($subscription);

if ($update) {
$this->deleteOrdersIncomingDistributions($subscription);
$this->deleteOrdersIncomingDistributionsFromSubscription($subscription);
}

if (count($matchedDistributionsArray)) {
foreach ($matchedDistributionsArray as $distribution) {
$this->createOrderFromSubscription($subscription, $distribution->date);
$orderArray[] = $this->createOrderFromSubscription($subscription, $distribution->date);
}
}

return $orderArray;
}

public function deleteOrdersIncomingDistributionsFromSubscription(Subscription $subscription, $deleteAfterDateEnd = false)
public function deleteOrdersIncomingDistributionsFromSubscription(Subscription $subscription, bool $deleteAfterDateEnd = false): int
{
$dateStart = $subscription->date_begin;
$comparatorDateStart = '>=';
@@ -208,7 +222,7 @@ class OrderBuilder extends BaseService implements BuilderInterface
);
}

$this->delete($order, true);
$this->deleteOrder($order, true);

$countOrdersDeleted ++;
}
@@ -217,7 +231,7 @@ class OrderBuilder extends BaseService implements BuilderInterface
return $countOrdersDeleted;
}

public function addProductOrdersFromSubscription(Order $order, Subscription $subscription): void
public function addProductOrdersFromSubscription(Order $order, Subscription $subscription): bool
{
$productsAdd = false;
$user = $subscription->user ?? null;
@@ -237,8 +251,10 @@ class OrderBuilder extends BaseService implements BuilderInterface
}

if(!$productsAdd) {
$order->delete();
$this->deleteOrder($order);
}

return $productsAdd;
}

// updateFromSubscription
@@ -323,19 +339,17 @@ class OrderBuilder extends BaseService implements BuilderInterface
* Initialise le montant total, le montant déjà payé et le poids de la commande.
*/
// init
public function initOrder(Order $order, $taxCalculationMethod = Document::TAX_CALCULATION_METHOD_DEFAULT)
public function initOrder(Order $order, string $taxCalculationMethod = Document::TAX_CALCULATION_METHOD_DEFAULT): void
{
$this->initOrderAmount($order, $taxCalculationMethod);
$this->initOrderPaidAmount($order);

return $this;
}

/**
* Initialise le montant de la commande.
*/
// initAmount
public function initOrderAmount(Order $order, $taxCalculationMethod = Document::TAX_CALCULATION_METHOD_DEFAULT)
public function initOrderAmount(Order $order, string $taxCalculationMethod = Document::TAX_CALCULATION_METHOD_DEFAULT): void
{
$order->amount = 0;
$order->amount_with_tax = 0;
@@ -421,7 +435,7 @@ class OrderBuilder extends BaseService implements BuilderInterface
}

// delete
public function deleteOrder(Order $order, $force = false)
public function deleteOrder(Order $order, bool $force = false): bool
{
// remboursement si l'utilisateur a payé pour cette commande
$amountPaid = $this->orderSolver->getAmount($order, Order::AMOUNT_PAID);
@@ -453,6 +467,8 @@ class OrderBuilder extends BaseService implements BuilderInterface

return $order->save();
}

return false;
}

/**
@@ -487,7 +503,7 @@ class OrderBuilder extends BaseService implements BuilderInterface

// setTillerSynchronization
// updateTillerSynchronization
public function updateOrderTillerSynchronization(Order $order)
public function updateOrderTillerSynchronization(Order $order): void
{
$paymentStatus = $order->getPaymentStatus();

@@ -498,8 +514,6 @@ class OrderBuilder extends BaseService implements BuilderInterface
}

$order->save();

return $order;
}

/**
@@ -573,7 +587,7 @@ class OrderBuilder extends BaseService implements BuilderInterface

// initReference
// generateReference
public function generateOrderReference(Order $order)
public function generateOrderReference(Order $order): void
{
$producer = $order->producer;


+ 45
- 8
common/logic/Order/Order/OrderManager.php Zobrazit soubor

@@ -3,20 +3,27 @@
namespace common\logic\Order\Order;

use common\logic\BaseManager;
use common\logic\Config\TaxRate\TaxRate;
use common\logic\Distribution\Distribution\Distribution;
use common\logic\Document\Document\Document;
use common\logic\Order\ProductOrder\ProductOrder;
use common\logic\Producer\Producer\Producer;
use common\logic\Product\Product\Product;
use common\logic\Subscription\Subscription\Subscription;
use common\logic\User\User\User;
use yii\db\Query;

/**
* > Solver
* Solver :
* @method string getFieldNameAmount($typeTotal = Order::AMOUNT_TOTAL, string $typeField = '')
* @method float getOrderTotalVat(Order $order, string $typeTotal = Order::AMOUNT_TOTAL)
* @method ?string getPaymentStatus(Order $order)
* @method string|null getPaymentStatus(Order $order)
* @method string getPointSaleSummary(Order $order)
* @method string getOrderUsername(Order $order)
* @method bool isLinkedToValidDocument(Order $order)
* @method string getCommentReport(Order $order)
* @method string getDateAsHtml(Order $order)
* @method countProducts(Order $order): int
* @method int countProducts(Order $order)
* @method int getProductQuantityPieces(Product $product, array $orders)
* @method int getProductQuantity(Product $product, array $orders, bool $ignoreCancel = false, string $unit = null)
* @method string getHistoryClass(Order $order)
@@ -30,12 +37,42 @@ use common\logic\User\User\User;
* @method bool isOrderBelongsToUser(Order $order, User $user = null)
* @method string getDataJson(Order $order)
*
* Repository
*
* Builder
*
* Utils
* Repository :
* @method array defaultOptionsSearch()
* @method Order|null findOneById(int $id)
* @method array findBy(array $params = [], array $options = [])
* @method array findByDistribution(Distribution $distribution, string $conditionAppend = '')
* @method Order|null findOneLastOfYear(Producer $producer)
* @method Query queryHistory(Producer $producer, User $user, string $type = 'incoming')
* @method string getCartSummary(Order $order, $htmlFormat = true)
* @method string getAmountSummary(Order $order)
* @method string getState(Order $order)
*
* Builder :
* @method Order instanciateOrder(Distribution $distribution)
* @method Order createOrder(Distribution $distribution)
* @method Order|null createOrderFromSubscription(Subscription $subscription, string $date, bool $force = false)
* @method array createAllOrdersFromSubscriptions(string $date, bool $force = false)
* @method array updateOrdersIncomingDistributionsFromSubscription(Subscription $subscription, bool $update = false)
* @method int deleteOrdersIncomingDistributionsFromSubscription(Subscription $subscription, bool $deleteAfterDateEnd = false)
* @method bool addProductOrdersFromSubscription(Order $order, Subscription $subscription)
* @method void updateOrderFromSubscription(Order $order, Subscription $subscription)
* @method void initOrderBaseFromSubscription(Order $order, Subscription $subscription)
* @method void initOrderAutoPaymentFromSubscription(Order $order, Subscription $subscription)
* @method void createUserPointSale(Order $order)
* @method void initOrderCommentPointSale(Order $order)
* @method void initOrder(Order $order, string $taxCalculationMethod = Document::TAX_CALCULATION_METHOD_DEFAULT)
* @method void initOrderAmount(Order $order, string $taxCalculationMethod = Document::TAX_CALCULATION_METHOD_DEFAULT)
* @method void addProductOrderWeight(Order $order, ProductOrder $productOrder)
* @method void addProductOrderAmount(Order $order, string $typeTotal, ProductOrder $productOrder, string $taxCalculationMethod)
* @method void addProductOrderVat(Order $order, string $typeTotal, float $priceTotalWithoutTax, TaxRate $taxRate, string $taxCalculationMethod)
* @method void initOrderPaidAmount(Order $order)
* @method void deleteOrder(Order $order, bool $force = false): bool
* @method void processCredit(Order $order)
* @method void updateOrderTillerSynchronization(Order $order)
* @method void updateOrderStatus(Order $order, string $newStatus, string $origin)
* @method void generateOrderReference(Order $order)
* @method void updateOrderInvoicePrices(Order $order, array $params = [])
*/
class OrderManager extends BaseManager
{

+ 5
- 2
common/logic/Order/Order/OrderRepository.php Zobrazit soubor

@@ -51,7 +51,7 @@ class OrderRepository extends BaseService implements RepositoryInterface
* Recherche et initialise des commandes.
*/
// searchBy
public function findBy($params = [], $options = [])
public function findBy(array $params = [], array $options = []): array
{
$orders = Order::searchBy($params, $options);

@@ -118,6 +118,7 @@ class OrderRepository extends BaseService implements RepositoryInterface

/**
* Retourne le résumé du panier au format HTML.
* @TODO : à déplacer dans OrderSolver
*/
public function getCartSummary(Order $order, $htmlFormat = true): string
{
@@ -150,6 +151,7 @@ class OrderRepository extends BaseService implements RepositoryInterface

/**
* Retourne le résumé du paiement (montant, statut).
* @TODO : à déplacer dans OrderSolver
*/
public function getAmountSummary(Order $order): string
{
@@ -182,6 +184,7 @@ class OrderRepository extends BaseService implements RepositoryInterface

/**
* Retourne l'état de la commande (livrée, modifiable ou en préparation.
* @TODO : à déplacer dans OrderSolver
*/
public function getState(Order $order): string
{
@@ -227,7 +230,7 @@ class OrderRepository extends BaseService implements RepositoryInterface
return Order::STATE_PREPARATION;
}

public function getOneLastOfYear(Producer $producer)
public function findOneLastOfYear(Producer $producer)
{
return Order::find()->innerJoinWith('distribution', true)
->where(['>=', 'distribution.date', date('Y') . '-01-01'])

+ 0
- 5
frontend/controllers/SiteController.php Zobrazit soubor

@@ -126,11 +126,6 @@ class SiteController extends FrontendController
*/
public function actionIndex()
{
$distribution = $this->getDistributionContainer()->getRepository()->getOneById(1);
$orderManager = $this->getOrderManager();
$order = $orderManager->instanciateOrder($distribution);
$orderManager->getOrderTotalVat($order);

return $this->render('index', [
'producerDemoAccount' => $this->getLogic()->getProducerContainer()
->getRepository()->getOneDemoAccount(),

+ 1
- 1
producer/controllers/CreditController.php Zobrazit soubor

@@ -215,7 +215,7 @@ class CreditController extends ProducerBaseController
$idProducer = $paymentIntentMetadata->producer_id;

if (isset($paymentIntentMetadata->order_id)) {
$order = $this->getLogic()->getOrderContainer()->getRepository()->getOneById($paymentIntentMetadata->order_id);
$order = $this->getOrderManager()->findOneById($paymentIntentMetadata->order_id);
$pointSale = $this->getLogic()->getPointSaleContainer()->getRepostory()->getOneById($order->id_point_sale);
$distribution = $this->getLogic()->getDistributionContainer()->getRepository()->getOneById($order->id_distribution);
}

+ 9
- 11
producer/controllers/OrderController.php Zobrazit soubor

@@ -72,8 +72,8 @@ class OrderController extends ProducerBaseController
);
}

$order = $this->getOrderContainer()->getRepository()->getOneById($id);
if ($order && $this->getOrderContainer()->getSolver()->isStateOpen($order)) {
$order = $this->getOrderManager()->findOneById($id);
if ($order && $this->getOrderManager()->isOrderStateOpen($order)) {
$params['order'] = $order;
}

@@ -89,7 +89,7 @@ class OrderController extends ProducerBaseController
*/
public function actionHistory($type = 'incoming')
{
$queryHistoryArray = $this->getOrderContainer()->getRepository()
$queryHistoryArray = $this->getOrderManager()
->queryHistory($this->getProducer(), $this->getUserCurrent(), $type);
$queryHistoryIncoming = $queryHistoryArray['incoming'];
$queryHistoryPassed = $queryHistoryArray['passed'];
@@ -555,18 +555,17 @@ class OrderController extends ProducerBaseController
*/
public function actionCancel(int $id)
{
$orderContainer = $this->getOrderContainer();
$order = $orderContainer->getRepository()->getOneById($id);
$order = $this->getOrderManager()->findOneById($id);

if (!$order) {
throw new \yii\web\NotFoundHttpException('Commande introuvable');
}

if ($orderContainer->getSolver()->isStateOpen($order)) {
if ($this->getOrderManager()->isOrderStateOpen($order)) {
throw new UserException('Vous ne pouvez plus annuler cette commande.');
}

if ($orderContainer->getSolver()->belongsToUser($order, GlobalParam::getCurrentUser())) {
if ($this->getOrderManager()->isOrderbelongsToUser($order, GlobalParam::getCurrentUser())) {
$orderContainer->getBuilder()->delete($order);
\Yii::$app->session->setFlash('success', 'Votre commande a bien été annulée.');
}
@@ -579,11 +578,10 @@ class OrderController extends ProducerBaseController
*/
public function actionConfirm(int $idOrder, string $returnPayment = '')
{
$orderContainer = $this->getOrderContainer();
$order = $orderContainer->getRepository()->getOneById($idOrder);
$order = $this->getOrderManager()->findOneById($idOrder);
$producer = $this->getProducer();

if (!$order || (!$orderContainer->getSolver()->belongsToUser($order, GlobalParam::getCurrentUser()) && !$producer->option_allow_order_guest)) {
if (!$order || (!$this->getOrderManager()->isOrderBelongsToUser($order, GlobalParam::getCurrentUser()) && !$producer->option_allow_order_guest)) {
throw new \yii\base\UserException('Commande introuvable.');
}

@@ -878,7 +876,7 @@ class OrderController extends ProducerBaseController
Order $order = null
)
{
$ordersArray = $this->getOrderContainer()->getRepository()->getByDistribution($distribution);
$ordersArray = $this->getOrderManager()->findByDistribution($distribution);

$productsArray = Product::find()
->where([

+ 2
- 3
producer/controllers/SubscriptionController.php Zobrazit soubor

@@ -111,8 +111,7 @@ class SubscriptionController extends ProducerBaseController
if ($model->load($posts) && $model->validate() && $model->save()) {

$subscription = $subscriptionRepository->getOneById($model->id);
$this->getOrderContainer()->getBuilder()
->updateIncomingDistributions($subscription, $isUpdate);
$this->getOrderManager()->updateOrdersIncomingDistributionsFromSubscription($subscription, $isUpdate);

if ($isUpdate) {
$this->setFlash('success', 'Abonnement modifié');
@@ -231,7 +230,7 @@ class SubscriptionController extends ProducerBaseController
$subscriptionContainer->getBuilder()->delete($subscription);

// @TODO : gérer via événements
$this->getOrderContainer()->getBuilder()->deleteOrdersIncomingDistributions($subscription);
$this->getOrderManager()->deleteOrdersIncomingDistributionsFromSubscription($subscription);

$this->setFlash('success', 'Abonnement supprimé');


Načítá se…
Zrušit
Uložit