Browse Source

Refactoring services #885

refactoring
Guillaume 1 year ago
parent
commit
81689d6e0f
8 changed files with 60 additions and 41 deletions
  1. +1
    -1
      backend/controllers/DistributionController.php
  2. +10
    -6
      backend/controllers/OrderController.php
  3. +1
    -1
      common/logic/Distribution/PointSaleDistribution/Service/PointSaleDistributionBuilder.php
  4. +1
    -1
      common/logic/Distribution/ProductDistribution/Service/ProductDistributionBuilder.php
  5. +12
    -7
      common/logic/Order/Order/Service/OrderBuilder.php
  6. +13
    -5
      common/logic/Product/ProductPrice/Service/ProductPriceSolver.php
  7. +0
    -20
      common/logic/User/User/Repository/UserRepository.php
  8. +22
    -0
      common/logic/User/User/Service/UserSolver.php

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

if ($ordersArray) { if ($ordersArray) {
foreach ($ordersArray as $order) { foreach ($ordersArray as $order) {
if (is_null($order->date_delete)) { if (is_null($order->date_delete)) {
$revenues += $orderManager->getAmountWithTax($order);
$revenues += $orderManager->getOrderAmountWithTax($order);
$weight += $order->weight; $weight += $order->weight;
} }
} }

+ 10
- 6
backend/controllers/OrderController.php View File

use common\forms\SubscriptionForm; use common\forms\SubscriptionForm;
use common\helpers\CSV; use common\helpers\CSV;
use common\helpers\GlobalParam; use common\helpers\GlobalParam;
use common\helpers\Price;
use common\logic\Distribution\Distribution\Model\Distribution; use common\logic\Distribution\Distribution\Model\Distribution;
use common\logic\Distribution\PointSaleDistribution\Model\PointSaleDistribution; use common\logic\Distribution\PointSaleDistribution\Model\PointSaleDistribution;
use common\logic\Distribution\ProductDistribution\Model\ProductDistribution; use common\logic\Distribution\ProductDistribution\Model\ProductDistribution;
$products = json_decode($products); $products = json_decode($products);
$pointSale = $pointSaleManager->findOnePointSaleById($idPointSale); $pointSale = $pointSaleManager->findOnePointSaleById($idPointSale);
$distribution = $distributionManager->findOneDistributionByDate($date); $distribution = $distributionManager->findOneDistributionByDate($date);
$producerCurrent = $this->getProducerCurrent();


if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $date) if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $date)
&& ($idUser || strlen($username)) && ($idUser || strlen($username))
} }
} }


$order = $orderManager->findOneOrderById($order);
$order = $orderManager->findOneOrderById($order->id);
if ($order && $processCredit) { if ($order && $processCredit) {
$orderManager->processCredit($order); $orderManager->processCredit($order);
} }


// lien utilisateur / point de vente // lien utilisateur / point de vente
if ($idUser && $pointSale) { if ($idUser && $pointSale) {
$pointSaleManager->addUser($pointSale, $user);
$pointSaleManager->addUser($user, $pointSale);
} }
} }


*/ */
public function actionPaymentStatus(int $idOrder) public function actionPaymentStatus(int $idOrder)
{ {
$orderManager = $this->getOrderManager();
$creditHistoryManager = $this->getCreditHistoryManager(); $creditHistoryManager = $this->getCreditHistoryManager();
$order = Order::searchOne(['id' => $idOrder]);

$order = $orderManager->findOneOrderById($idOrder);


if ($order) { if ($order) {
$html = ''; $html = '';


if (abs($order->amount - $amountPaid) < 0.0001) { if (abs($order->amount - $amountPaid) < 0.0001) {
$html .= '<span class="label label-success">Payé</span>'; $html .= '<span class="label label-success">Payé</span>';
$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']);
$buttonsCredit = Html::a('Rembourser ' . $orderManager->getOrderAmountWithTax($order, 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) { } elseif ($order->amount > $amountPaid) {
$amountToPay = $order->amount - $amountPaid; $amountToPay = $order->amount - $amountPaid;
$html .= '<span class="label label-danger">Non payé</span> reste <strong>' . number_format($amountToPay, 2) . ' €</strong> à payer'; $html .= '<span class="label label-danger">Non payé</span> reste <strong>' . number_format($amountToPay, 2) . ' €</strong> à payer';
. '<td>' . date('d/m/Y H:i:s', strtotime($creditHistoryManager->getDate($creditHistory))) . '</td>' . '<td>' . date('d/m/Y H:i:s', strtotime($creditHistoryManager->getDate($creditHistory))) . '</td>'
. '<td>' . Html::encode($creditHistoryManager->getStrUserAction($creditHistory)) . '</td>' . '<td>' . Html::encode($creditHistoryManager->getStrUserAction($creditHistory)) . '</td>'
. '<td>' . $creditHistoryManager->getStrWording($creditHistory) . '</td>' . '<td>' . $creditHistoryManager->getStrWording($creditHistory) . '</td>'
. '<td>' . ($creditHistoryManager->isTypeDebit($creditHistory) ? '- ' . $creditHistoryManager->getAmountWithTax($creditHistory, Order::AMOUNT_TOTAL, true) : '') . '</td>'
. '<td>' . ($creditHistoryManager->isTypeCredit($creditHistory) ? '+ ' . $creditHistoryManager->getAmountWithTax($creditHistory, Order::AMOUNT_TOTAL, true) : '') . '</td>'
. '<td>' . ($creditHistoryManager->isTypeDebit($creditHistory) ? '- ' . Price::getPriceWithTax($creditHistory->amount) : '') . '</td>'
. '<td>' . ($creditHistoryManager->isTypeCredit($creditHistory) ? '+ ' . Price::getPriceWithTax($creditHistory->amount) : '') . '</td>'
. '</tr>'; . '</tr>';
} }
} else { } else {

+ 1
- 1
common/logic/Distribution/PointSaleDistribution/Service/PointSaleDistributionBuilder.php View File

// setAll // setAll
public function createAllPointSaleDistributions(Distribution $distribution, bool $delivery): void public function createAllPointSaleDistributions(Distribution $distribution, bool $delivery): void
{ {
$pointSaleArray = $this->pointSaleDistributionRepository->findPointSaleDistributionsByDistribution($distribution);
$pointSaleArray = $this->pointSaleRepository->findPointSalesByDistribution($distribution);
foreach ($pointSaleArray as $pointSale) { foreach ($pointSaleArray as $pointSale) {
if (!$pointSale->pointSaleDistribution) { if (!$pointSale->pointSaleDistribution) {
$this->createPointSaleDistribution($distribution, $pointSale); $this->createPointSaleDistribution($distribution, $pointSale);

+ 1
- 1
common/logic/Distribution/ProductDistribution/Service/ProductDistributionBuilder.php View File



public function initActive(ProductDistribution $productDistribution): ProductDistribution public function initActive(ProductDistribution $productDistribution): ProductDistribution
{ {
$productDistribution->active = $this->productSolver->isProductActiveByDay(
$productDistribution->active = (int) $this->productSolver->isProductActiveByDay(
$productDistribution->product, $productDistribution->product,
$this->distributionSolver->getDistributionDayAsString($productDistribution->distribution) $this->distributionSolver->getDistributionDayAsString($productDistribution->distribution)
); );

+ 12
- 7
common/logic/Order/Order/Service/OrderBuilder.php View File

use common\helpers\Price; use common\helpers\Price;
use common\logic\AbstractBuilder; use common\logic\AbstractBuilder;
use common\logic\Config\TaxRate\Model\TaxRate; use common\logic\Config\TaxRate\Model\TaxRate;
use common\logic\Distribution\Distribution\Model\Distribution;
use common\logic\Distribution\Distribution\Repository\DistributionRepository; use common\logic\Distribution\Distribution\Repository\DistributionRepository;
use common\logic\Document\Document\Model\Document; use common\logic\Document\Document\Model\Document;
use common\logic\Order\Order\Model\Order; use common\logic\Order\Order\Model\Order;
use common\logic\PointSale\UserPointSale\Repository\UserPointSaleRepository; use common\logic\PointSale\UserPointSale\Repository\UserPointSaleRepository;
use common\logic\Producer\Producer\Model\Producer; use common\logic\Producer\Producer\Model\Producer;
use common\logic\Producer\Producer\Repository\ProducerRepository; use common\logic\Producer\Producer\Repository\ProducerRepository;
use common\logic\Product\Product\Service\ProductSolver;
use common\logic\Subscription\Subscription\Model\Subscription; use common\logic\Subscription\Subscription\Model\Subscription;
use common\logic\Subscription\Subscription\Service\SubscriptionBuilder; use common\logic\Subscription\Subscription\Service\SubscriptionBuilder;
use common\logic\Subscription\Subscription\Repository\SubscriptionRepository; use common\logic\Subscription\Subscription\Repository\SubscriptionRepository;
protected ProductOrderSolver $productOrderSolver; protected ProductOrderSolver $productOrderSolver;
protected SubscriptionRepository $subscriptionRepository; protected SubscriptionRepository $subscriptionRepository;
protected SubscriptionSolver $subscriptionSolver; protected SubscriptionSolver $subscriptionSolver;
protected ProductSolver $productSolver;


public function loadDependencies(): void public function loadDependencies(): void
{ {
$this->productOrderSolver = $this->loadService(ProductOrderSolver::class); $this->productOrderSolver = $this->loadService(ProductOrderSolver::class);
$this->subscriptionRepository = $this->loadService(SubscriptionRepository::class); $this->subscriptionRepository = $this->loadService(SubscriptionRepository::class);
$this->subscriptionSolver = $this->loadService(SubscriptionSolver::class); $this->subscriptionSolver = $this->loadService(SubscriptionSolver::class);
$this->productSolver = $this->loadService(ProductSolver::class);
} }


public function instanciateOrder(): Order
public function instanciateOrder(Distribution $distribution): Order
{ {
$order = new Order(); $order = new Order();
//$order->populateDistribution($distribution);
$order->populateDistribution($distribution);
$order->date = date('Y-m-d H:i:s'); $order->date = date('Y-m-d H:i:s');
$order->status = 'tmp-order'; $order->status = 'tmp-order';


return $order; return $order;
} }


public function createOrder(): Order
public function createOrder(Distribution $distribution): Order
{ {
$order = $this->instanciateOrder();
$order = $this->instanciateOrder($distribution);


$this->createUserPointSale($order); $this->createUserPointSale($order);
$this->initOrderCommentPointSale($order); $this->initOrderCommentPointSale($order);
$order, $order,
$productSubscription->product, $productSubscription->product,
$productSubscription->quantity, $productSubscription->quantity,
$productSubscription->product->getPrice([
$this->productSolver->getPrice($productSubscription->product, [
'user' => $user, 'user' => $user,
'point_sale' => $subscription->pointSale, 'point_sale' => $subscription->pointSale,
'quantity' => $productSubscription->quantity 'quantity' => $productSubscription->quantity
$order->tiller_synchronization = $synchroTiller; $order->tiller_synchronization = $synchroTiller;
} }
else { else {
$paymentStatus = $order->getPaymentStatus();
$paymentStatus = $this->orderSolver->getPaymentStatus($order);


if ($paymentStatus == Order::PAYMENT_PAID) { if ($paymentStatus == Order::PAYMENT_PAID) {
$order->tiller_synchronization = 1; $order->tiller_synchronization = 1;
// generateReference // generateReference
public function generateOrderReference(Order $order): void public function generateOrderReference(Order $order): void
{ {
$producer = $this->producerRepository->findOneProducerById($order->id_producer);
$distribution = $this->distributionRepository->findOneDistributionById($order->id_distribution);
$producer = $this->producerRepository->findOneProducerById($distribution->id_producer);


if (!$order->reference && $producer->option_order_reference_type == Producer::ORDER_REFERENCE_TYPE_YEARLY) { if (!$order->reference && $producer->option_order_reference_type == Producer::ORDER_REFERENCE_TYPE_YEARLY) {
$lastOrder = $this->orderRepository->findOneOrderLastOfYear($producer); $lastOrder = $this->orderRepository->findOneOrderLastOfYear($producer);

+ 13
- 5
common/logic/Product/ProductPrice/Service/ProductPriceSolver.php View File

use common\logic\Product\ProductPrice\Model\ProductPrice; use common\logic\Product\ProductPrice\Model\ProductPrice;
use common\logic\SolverInterface; use common\logic\SolverInterface;
use common\logic\User\User\Model\User; use common\logic\User\User\Model\User;
use common\logic\User\User\Service\UserSolver;


class ProductPriceSolver extends AbstractService implements SolverInterface class ProductPriceSolver extends AbstractService implements SolverInterface
{ {
protected UserSolver $userSolver;

public function loadDependencies(): void
{
$this->userSolver = $this->loadService(UserSolver::class);
}

// getSpecificPricesFilterByPriorityMatch // getSpecificPricesFilterByPriorityMatch
public function filterByPriorityMatch( public function filterByPriorityMatch(
array $specificPrices, array $specificPrices,
$specificPricesFilter = []; $specificPricesFilter = [];


foreach ($specificPrices as $keySpecificPrice => $specificPrice) { foreach ($specificPrices as $keySpecificPrice => $specificPrice) {
if (($priorityMatchSpecificPrice && $specificPrice->$priorityMatchSpecificPrice($user, $pointSale))
|| $specificPrice->matchFromQuantityOnly()) {
if (($priorityMatchSpecificPrice && $this->$priorityMatchSpecificPrice($specificPrice, $user, $pointSale))
|| $this->matchFromQuantityOnly($specificPrice)) {


$specificPricesFilter[] = $specificPrice; $specificPricesFilter[] = $specificPrice;
} }
PointSale $pointSale = null): bool PointSale $pointSale = null): bool
{ {
foreach($specificPriceArray as $specificPrice) { foreach($specificPriceArray as $specificPrice) {
if($specificPrice->$typeMatch($user, $pointSale)) {
if($this->$typeMatch($specificPrice, $user, $pointSale)) {
return true; return true;
} }
} }
&& $productPrice->id_user_group && $productPrice->id_user_group
&& !$productPrice->id_point_sale && !$productPrice->id_point_sale
&& !$productPrice->id_user && !$productPrice->id_user
&& $user->belongsToUserGroup($productPrice->id_user_group);
&& $this->userSolver->isUserBelongsToUserGroup($user, $productPrice->userGroup);
} }


public function matchPointSale(ProductPrice $productPrice, User $user = null, PointSale $pointSale = null): bool public function matchPointSale(ProductPrice $productPrice, User $user = null, PointSale $pointSale = null): bool
&& $productPrice->id_user_group && $productPrice->id_user_group
&& $productPrice->id_point_sale && $productPrice->id_point_sale
&& !$productPrice->id_user && !$productPrice->id_user
&& $user->belongsToUserGroup($productPrice->id_user_group)
&& $this->userSolver->isUserBelongsToUserGroup($user, $productPrice->userGroup)
&& $productPrice->id_point_sale == $pointSale->id; && $productPrice->id_point_sale == $pointSale->id;
} }



+ 0
- 20
common/logic/User/User/Repository/UserRepository.php View File

return User::searchOne(['id' => $id]); return User::searchOne(['id' => $id]);
} }


// belongsToUserGroup
public function isUserbelongsToUserGroup(User $user, UserGroup $userGroup): bool
{
if (!$user->userUserGroup) {
$user->populateRelation('userUserGroup', UserUserGroup::searchAll([
'id_user' => $user->id
]));
}

if ($user->userUserGroup) {
foreach ($user->userUserGroup as $userUserGroup) {
if ($userUserGroup->id_user_group == $userGroup->id) {
return true;
}
}
}

return false;
}

public function findUsers(): array public function findUsers(): array
{ {
return $this->queryUsersBy()->all(); return $this->queryUsersBy()->all();

+ 22
- 0
common/logic/User/User/Service/UserSolver.php View File

use common\logic\AbstractService; use common\logic\AbstractService;
use common\logic\SolverInterface; use common\logic\SolverInterface;
use common\logic\User\User\Model\User; use common\logic\User\User\Model\User;
use common\logic\User\UserGroup\Model\UserGroup;
use common\logic\User\UserUserGroup\Model\UserUserGroup;


class UserSolver extends AbstractService implements SolverInterface class UserSolver extends AbstractService implements SolverInterface
{ {
|| $userCurrentStatus == User::STATUS_PRODUCER; || $userCurrentStatus == User::STATUS_PRODUCER;
} }


// belongsToUserGroup
public function isUserBelongsToUserGroup(User $user, UserGroup $userGroup): bool
{
if (!$user->userUserGroup) {
$user->populateRelation('userUserGroup', UserUserGroup::searchAll([
'id_user' => $user->id
]));
}

if ($user->userUserGroup) {
foreach ($user->userUserGroup as $userUserGroup) {
if ($userUserGroup->id_user_group == $userGroup->id) {
return true;
}
}
}

return false;
}

} }

Loading…
Cancel
Save