Parcourir la source

Refactoring services #885

refactoring
Guillaume Bourgeois il y a 1 an
Parent
révision
82c110eaf0
73 fichiers modifiés avec 547 ajouts et 372 suppressions
  1. +3
    -3
      common/components/ActiveRecordCommon.php
  2. +9
    -4
      common/logic/BaseBuilder.php
  3. +1
    -1
      common/logic/Config/TaxRate/TaxRateBuilder.php
  4. +1
    -1
      common/logic/Config/TaxRate/TaxRateRepository.php
  5. +1
    -1
      common/logic/Development/Development/Development.php
  6. +1
    -1
      common/logic/Development/DevelopmentPriority/DevelopmentPriority.php
  7. +12
    -0
      common/logic/Distribution/Distribution/Distribution.php
  8. +9
    -8
      common/logic/Distribution/Distribution/DistributionBuilder.php
  9. +43
    -1
      common/logic/Distribution/Distribution/DistributionRepository.php
  10. +7
    -8
      common/logic/Distribution/PointSaleDistribution/PointSaleDistributionBuilder.php
  11. +1
    -1
      common/logic/Distribution/PointSaleDistribution/PointSaleDistributionRepository.php
  12. +1
    -1
      common/logic/Distribution/ProductDistribution/ProductDistributionBuilder.php
  13. +1
    -1
      common/logic/Distribution/ProductDistribution/ProductDistributionRepository.php
  14. +1
    -1
      common/logic/Document/DeliveryNote/DeliveryNoteRepository.php
  15. +1
    -1
      common/logic/Document/DeliveryNote/DeliveryNoteSearch.php
  16. +3
    -3
      common/logic/Document/Document/Document.php
  17. +3
    -2
      common/logic/Document/Document/DocumentBuilder.php
  18. +1
    -1
      common/logic/Document/Document/DocumentSolver.php
  19. +1
    -1
      common/logic/Document/Invoice/InvoiceRepository.php
  20. +1
    -1
      common/logic/Document/Invoice/InvoiceSearch.php
  21. +1
    -1
      common/logic/Document/Quotation/QuotationRepository.php
  22. +1
    -1
      common/logic/Document/Quotation/QuotationSearch.php
  23. +11
    -46
      common/logic/Order/Order/OrderBuilder.php
  24. +10
    -8
      common/logic/Order/Order/OrderRepository.php
  25. +1
    -1
      common/logic/Order/Order/OrderSearch.php
  26. +6
    -9
      common/logic/Order/OrderStatusHistory/OrderStatusHistoryBuilder.php
  27. +1
    -1
      common/logic/Order/OrderStatusHistory/OrderStatusHistoryRepository.php
  28. +9
    -8
      common/logic/Order/ProductOrder/ProductOrderBuilder.php
  29. +2
    -2
      common/logic/Order/ProductOrder/ProductOrderRepository.php
  30. +21
    -16
      common/logic/PointSale/PointSale/PointSaleBuilder.php
  31. +2
    -2
      common/logic/PointSale/PointSale/PointSaleRepository.php
  32. +1
    -1
      common/logic/PointSale/PointSale/PointSaleSearch.php
  33. +11
    -0
      common/logic/PointSale/UserPointSale/UserPointSale.php
  34. +19
    -9
      common/logic/PointSale/UserPointSale/UserPointSaleBuilder.php
  35. +2
    -2
      common/logic/PointSale/UserPointSale/UserPointSaleRepository.php
  36. +12
    -10
      common/logic/Producer/Producer/ProducerBuilder.php
  37. +15
    -14
      common/logic/Producer/Producer/ProducerRepository.php
  38. +23
    -0
      common/logic/Producer/ProducerPriceRange/ProducerPriceRangeBuilder.php
  39. +1
    -0
      common/logic/Producer/ProducerPriceRange/ProducerPriceRangeManager.php
  40. +8
    -3
      common/logic/Producer/ProducerPriceRange/ProducerPriceRangeRepository.php
  41. +11
    -3
      common/logic/Product/Product/ProductBuilder.php
  42. +34
    -32
      common/logic/Product/Product/ProductRepository.php
  43. +1
    -1
      common/logic/Product/Product/ProductSearch.php
  44. +11
    -2
      common/logic/Product/ProductCategory/ProductCategoryBuilder.php
  45. +5
    -5
      common/logic/Product/ProductCategory/ProductCategoryRepository.php
  46. +1
    -1
      common/logic/Product/ProductCategory/ProductCategorySearch.php
  47. +11
    -3
      common/logic/Product/ProductPointSale/ProductPointSaleBuilder.php
  48. +1
    -1
      common/logic/Product/ProductPointSale/ProductPointSaleRepository.php
  49. +11
    -3
      common/logic/Product/ProductPrice/ProductPriceBuilder.php
  50. +1
    -1
      common/logic/Product/ProductPrice/ProductPriceRepository.php
  51. +1
    -1
      common/logic/Product/ProductPrice/ProductPriceSearch.php
  52. +11
    -3
      common/logic/Subscription/ProductSubscription/ProductSubscriptionBuilder.php
  53. +1
    -1
      common/logic/Subscription/ProductSubscription/ProductSubscriptionRepository.php
  54. +14
    -11
      common/logic/Subscription/Subscription/SubscriptionBuilder.php
  55. +19
    -0
      common/logic/Subscription/Subscription/SubscriptionEventSubscriber.php
  56. +9
    -46
      common/logic/Subscription/Subscription/SubscriptionRepository.php
  57. +1
    -1
      common/logic/Subscription/Subscription/SubscriptionSearch.php
  58. +1
    -1
      common/logic/Subscription/Subscription/SubscriptionSolver.php
  59. +24
    -0
      common/logic/User/CreditHistory/CreditHistory.php
  60. +13
    -21
      common/logic/User/CreditHistory/CreditHistoryBuilder.php
  61. +1
    -1
      common/logic/User/CreditHistory/CreditHistoryRepository.php
  62. +1
    -1
      common/logic/User/CreditHistory/CreditHistorySearch.php
  63. +4
    -4
      common/logic/User/User/UserBuilder.php
  64. +28
    -20
      common/logic/User/User/UserRepository.php
  65. +1
    -1
      common/logic/User/User/UserSearch.php
  66. +11
    -3
      common/logic/User/UserGroup/UserGroupBuilder.php
  67. +8
    -3
      common/logic/User/UserGroup/UserGroupRepository.php
  68. +1
    -1
      common/logic/User/UserGroup/UserGroupSearch.php
  69. +25
    -20
      common/logic/User/UserProducer/UserProducerBuilder.php
  70. +12
    -0
      common/logic/User/UserProducer/UserProducerEventSubscriber.php
  71. +3
    -3
      common/logic/User/UserProducer/UserProducerRepository.php
  72. +12
    -2
      common/logic/User/UserUserGroup/UserUserGroupBuilder.php
  73. +1
    -1
      common/logic/User/UserUserGroup/UserUserGroupRepository.php

+ 3
- 3
common/components/ActiveRecordCommon.php Voir le fichier

@@ -67,11 +67,11 @@ class ActiveRecordCommon extends \yii\db\ActiveRecord
$class = get_called_class();
$repositoryClass = $class.'Repository';

if (is_callable([$repositoryClass, 'defaultOptionsSearch'])) {
if (is_callable([$repositoryClass, 'getDefaultOptionsSearch'])) {
$repository = new $repositoryClass;
$default_options = $repository->defaultOptionsSearch();
$default_options = $repository->getDefaultOptionsSearch();
} else {
throw new \ErrorException('La méthode "defaultOptionsSearch" n\'est '
throw new \ErrorException('La méthode "getDefaultOptionsSearch" n\'est '
. 'pas définie dans la classe "' . $class . '"');
}


+ 9
- 4
common/logic/BaseBuilder.php Voir le fichier

@@ -6,13 +6,18 @@ use yii\db\ActiveRecord;

class BaseBuilder extends BaseService
{
public function create(ActiveRecord $model): void
public function saveCreate(ActiveRecord $model): bool
{
$model->save();
return $model->save();
}

public function update(ActiveRecord $model): void
public function saveUpdate(ActiveRecord $model): bool
{
$model->save();
return $model->save();
}

public function delete(ActiveRecord $model): bool
{
$model->delete();
}
}

+ 1
- 1
common/logic/Config/TaxRate/TaxRateBuilder.php Voir le fichier

@@ -18,7 +18,7 @@ class TaxRateBuilder extends BaseBuilder implements BuilderInterface
{
$taxRate = $this->instanciateTaxRate();

$this->create($taxRate);
$this->saveCreate($taxRate);

return $taxRate;
}

+ 1
- 1
common/logic/Config/TaxRate/TaxRateRepository.php Voir le fichier

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

class TaxRateRepository extends BaseService implements RepositoryInterface
{
public function defaultOptionsSearch(): array
public function getDefaultOptionsSearch(): array
{
return [
'with' => [],

+ 1
- 1
common/logic/Development/Development/Development.php Voir le fichier

@@ -120,7 +120,7 @@ class Development extends ActiveRecordCommon
*
* @return array
*/
public static function defaultOptionsSearch() {
public static function getDefaultOptionsSearch() {
return [
'with' => ['developmentPriority', 'developmentPriorityCurrentProducer'],
'join_with' => [],

+ 1
- 1
common/logic/Development/DevelopmentPriority/DevelopmentPriority.php Voir le fichier

@@ -97,7 +97,7 @@ class DevelopmentPriority extends ActiveRecordCommon
*
* @return array
*/
public static function defaultOptionsSearch() {
public static function getDefaultOptionsSearch() {
return [
'with' => [],
'join_with' => [],

+ 12
- 0
common/logic/Distribution/Distribution/Distribution.php Voir le fichier

@@ -43,9 +43,12 @@ use common\logic\Distribution\ProductDistribution\ProductDistribution;
use common\logic\Order\Order\Order;
use common\logic\Producer\Producer\Producer;
use common\components\ActiveRecordCommon;
use common\logic\Subscription\Subscription\SubscriptionEventSubscriber;

class Distribution extends ActiveRecordCommon
{
const EVENT_ACTIVE = 'distribution.active';

/**
* @inheritdoc
*/
@@ -78,6 +81,15 @@ class Distribution extends ActiveRecordCommon
];
}

public function init()
{
$this->on(Distribution::EVENT_ACTIVE, function($event) {
SubscriptionEventSubscriber::onActiveDistribution($event->distribution);
});

parent::init();
}

/*
* Relations
*/

+ 9
- 8
common/logic/Distribution/Distribution/DistributionBuilder.php Voir le fichier

@@ -17,6 +17,7 @@ use common\logic\Producer\Producer\Producer;
use common\logic\Product\Product\Product;
use common\logic\Product\Product\ProductRepository;
use common\logic\User\UserProducer\UserProducerRepository;
use yii\base\Event;

class DistributionBuilder extends BaseBuilder implements BuilderInterface
{
@@ -60,7 +61,7 @@ class DistributionBuilder extends BaseBuilder implements BuilderInterface
{
$distribution = $this->instanciateDistribution($producer, $date, $delivery);

$this->create($distribution);
$this->saveCreate($distribution);

$this->createPointSaleDistributions($distribution);
$this->createProductDistributions($distribution);
@@ -131,7 +132,7 @@ class DistributionBuilder extends BaseBuilder implements BuilderInterface
$pointSaleDistribution->delivery = 1;
}

$this->update($pointSaleDistribution);
$this->saveUpdate($pointSaleDistribution);

return $pointSaleDistribution;
}
@@ -141,7 +142,7 @@ class DistributionBuilder extends BaseBuilder implements BuilderInterface
*/
public function addPointSaleIncomingDistributions(PointSale $pointSale): void
{
$distributionArray = $this->distributionRepository->getIncoming();
$distributionArray = $this->distributionRepository->findDistributionsIncoming();
foreach ($distributionArray as $distribution) {
$this->addPointSale($distribution, $pointSale);
}
@@ -149,7 +150,7 @@ class DistributionBuilder extends BaseBuilder implements BuilderInterface

public function updateOrderProductPrices(Distribution $distribution, Product $product): void
{
$ordersArray = $this->orderRepository->getByDistribution($distribution, 'AND origin != "user"');
$ordersArray = $this->orderRepository->findOrdersByDistribution($distribution, 'AND origin != "user"');

if ($ordersArray) {
foreach ($ordersArray as $order) {
@@ -179,17 +180,17 @@ class DistributionBuilder extends BaseBuilder implements BuilderInterface
/**
* Active ou désactive la distribution.
*/
public function updateActive(Distribution $distribution, bool $active = true): void
// active
public function activeDistribution(Distribution $distribution, bool $active = true): void
{
$this->pointSaleDistributionBuilder->createAllPointSaleDistributions($distribution, true);

$distribution->active = (int) $active;

$this->update($distribution);
$this->saveUpdate($distribution);

if ($active) {
// @TODO : gérer avec les événements
//Subscription::addAll($distribution->date);
$distribution->trigger(Distribution::EVENT_ACTIVE, new Event(['distribution' => $distribution]));
}
}
}

+ 43
- 1
common/logic/Distribution/Distribution/DistributionRepository.php Voir le fichier

@@ -6,17 +6,21 @@ use common\helpers\GlobalParam;
use common\logic\BaseService;
use common\logic\Producer\Producer\Producer;
use common\logic\RepositoryInterface;
use common\logic\Subscription\Subscription\Subscription;
use common\logic\Subscription\Subscription\SubscriptionSolver;

class DistributionRepository extends BaseService implements RepositoryInterface
{
protected DistributionSolver $distributionSolver;
protected SubscriptionSolver $subscriptionSolver;

public function __construct()
{
$this->distributionSolver = $this->loadService(DistributionSolver::class);
$this->subscriptionSolver = $this->loadService(SubscriptionSolver::class);
}

public function defaultOptionsSearch(): array
public function getDefaultOptionsSearch(): array
{
return [
'with' => [],
@@ -67,6 +71,44 @@ class DistributionRepository extends BaseService implements RepositoryInterface
return $distributionsArray;
}

/**
* Recherche les distributions futures où l'abonnement peut s'appliquer.
*/
// searchMatchedIncomingDistributions
public function findDistributionsIncomingMatchWithSubscrtiption(Subscription $subscription): array
{
$params = [
':date_earliest_order' => date('Y-m-d'),
':date_begin' => date('Y-m-d', strtotime($subscription->date_begin)),
':id_producer' => GlobalParam::getCurrentProducerId()
];

$incomingDistributions = Distribution::find()
->where('id_producer = :id_producer')
->andWhere('date >= :date_begin')
->andWhere('date > :date_earliest_order');

if ($subscription->date_end) {
$incomingDistributions->andWhere('date <= :date_end');
$params[':date_end'] = date('Y-m-d', strtotime($subscription->date_end));
}

$incomingDistributions->orderBy('date ASC');

$incomingDistributions->params($params);
$incomingDistributionsArray = $incomingDistributions->all();
$this->subscriptionSolver->filterDistributionsByDateDelay($incomingDistributionsArray);

$matchedIncomingDistributionsArray = [];
foreach ($incomingDistributionsArray as $incomingDistribution) {
if ($this->subscriptionSolver->matchWith($subscription, $incomingDistribution->date)) {
$matchedIncomingDistributionsArray[] = $incomingDistribution;
}
}

return $matchedIncomingDistributionsArray;
}

// isDateAvailable
public function isDistributionDateAvailable(Producer $producer, string $date = null): bool
{

+ 7
- 8
common/logic/Distribution/PointSaleDistribution/PointSaleDistributionBuilder.php Voir le fichier

@@ -28,9 +28,8 @@ class PointSaleDistributionBuilder extends BaseBuilder implements BuilderInterfa
public function instanciatePointSaleDistribution(Distribution $distribution, PointSale $pointSale): PointSaleDistribution
{
$pointSaleDistribution = new PointSaleDistribution();

$pointSaleDistribution->id_distribution = $distribution->id;
$pointSaleDistribution->id_point_sale = $pointSale->id;
$pointSaleDistribution->populateFieldObject('id_distribution', 'distribution', $distribution);
$pointSaleDistribution->populateFieldObject('id_point_sale', 'pointSale', $pointSale);

return $pointSaleDistribution;
}
@@ -42,7 +41,7 @@ class PointSaleDistributionBuilder extends BaseBuilder implements BuilderInterfa
{
$pointSaleDistribution = $this->instanciatePointSaleDistribution($distribution, $pointSale);

$this->create($pointSaleDistribution);
$this->saveCreate($pointSaleDistribution);

return $pointSaleDistribution;
}
@@ -66,13 +65,13 @@ class PointSaleDistributionBuilder extends BaseBuilder implements BuilderInterfa
}
}

$pointSaleDistributionArray = $this->pointSaleDistributionRepository->getByDistribution($distribution);
$pointSaleDistributionArray = $this->pointSaleDistributionRepository->findPointSaleDistributionsByDistribution($distribution);
foreach ($pointSaleDistributionArray as $pointSaleDistribution) {
$this->initDelivery($pointSaleDistribution, $delivery);
$this->updateDelivery($pointSaleDistribution, $delivery);
}
}

public function initDelivery(PointSaleDistribution $pointSaleDistribution, bool $delivery): void
public function updateDelivery(PointSaleDistribution $pointSaleDistribution, bool $delivery): void
{
$day = date('N', strtotime($pointSaleDistribution->distribution->date));
$pointSale = $pointSaleDistribution->pointSale;
@@ -91,6 +90,6 @@ class PointSaleDistributionBuilder extends BaseBuilder implements BuilderInterfa
$pointSaleDistribution->delivery = 0;
}

$this->update($pointSaleDistribution);
$this->saveUpdate($pointSaleDistribution);
}
}

+ 1
- 1
common/logic/Distribution/PointSaleDistribution/PointSaleDistributionRepository.php Voir le fichier

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

class PointSaleDistributionRepository extends BaseService implements RepositoryInterface
{
public function defaultOptionsSearch(): array
public function getDefaultOptionsSearch(): array
{
return [
'with' => ['distribution', 'pointSale'],

+ 1
- 1
common/logic/Distribution/ProductDistribution/ProductDistributionBuilder.php Voir le fichier

@@ -39,7 +39,7 @@ class ProductDistributionBuilder extends BaseBuilder implements BuilderInterface
->initActive($productDistribution)
->initQuantityMax($productDistribution);

$this->create($productDistribution);
$this->saveCreate($productDistribution);

return $productDistribution;
}

+ 1
- 1
common/logic/Distribution/ProductDistribution/ProductDistributionRepository.php Voir le fichier

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

class ProductDistributionRepository extends BaseService implements RepositoryInterface
{
public function defaultOptionsSearch(): array
public function getDefaultOptionsSearch(): array
{
return [
'with' => ['product','distribution'],

+ 1
- 1
common/logic/Document/DeliveryNote/DeliveryNoteRepository.php Voir le fichier

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

class DeliveryNoteRepository extends BaseService implements RepositoryInterface
{
public function defaultOptionsSearch(): array
public function getDefaultOptionsSearch(): array
{
return [
'with' => [],

+ 1
- 1
common/logic/Document/DeliveryNote/DeliveryNoteSearch.php Voir le fichier

@@ -60,7 +60,7 @@ class DeliveryNoteSearch extends DeliveryNote
public function search($params)
{
$deliveryNoteRepository = new DeliveryNoteRepository();
$optionsSearch = $deliveryNoteRepository->defaultOptionsSearch();
$optionsSearch = $deliveryNoteRepository->getDefaultOptionsSearch();

$query = DeliveryNote::find()
->with($optionsSearch['with'])

+ 3
- 3
common/logic/Document/Document/Document.php Voir le fichier

@@ -124,11 +124,11 @@ class Document extends ActiveRecordCommon implements DocumentInterface
public function relationOrders($fieldIdDocument)
{
$orderRepository = new OrderRepository();
$defaultOptionsSearch = $orderRepository->defaultOptionsSearch();
$getDefaultOptionsSearch = $orderRepository->getDefaultOptionsSearch();

return $this->hasMany(Order::class, [$fieldIdDocument => 'id'])
->with($defaultOptionsSearch['with'])
->joinWith($defaultOptionsSearch['join_with'])
->with($getDefaultOptionsSearch['with'])
->joinWith($getDefaultOptionsSearch['join_with'])
->orderBy('distribution.date ASC');
}
}

+ 3
- 2
common/logic/Document/Document/DocumentBuilder.php Voir le fichier

@@ -22,7 +22,7 @@ class DocumentBuilder extends BaseService implements BuilderInterface

public function generateReference(DocumentInterface $document): void
{
$class = $document->getClass();
$class = $this->documentSolver->getClass($document);
$classLower = strtolower($class);
if ($classLower == 'deliverynote') {
$classLower = 'delivery_note';
@@ -55,8 +55,9 @@ class DocumentBuilder extends BaseService implements BuilderInterface

public function changeStatus(DocumentInterface $document, string $status): void
{
$document->status = $status;

if ($status == Document::STATUS_VALID) {
$document->status = $status;
$this->generateReference($document);
}
}

+ 1
- 1
common/logic/Document/Document/DocumentSolver.php Voir le fichier

@@ -2,10 +2,10 @@

namespace common\logic\Document\Document;

use common\helpers\Price;
use common\logic\BaseService;
use common\logic\Order\Order\Order;
use common\logic\PointSale\PointSale\PointSale;
use common\logic\Producer\Producer\Producer;
use common\logic\SolverInterface;

class DocumentSolver extends BaseService implements SolverInterface

+ 1
- 1
common/logic/Document/Invoice/InvoiceRepository.php Voir le fichier

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

class InvoiceRepository extends BaseService implements RepositoryInterface
{
public function defaultOptionsSearch(): array
public function getDefaultOptionsSearch(): array
{
return [
'with' => [],

+ 1
- 1
common/logic/Document/Invoice/InvoiceSearch.php Voir le fichier

@@ -55,7 +55,7 @@ class InvoiceSearch extends Invoice

public function search($params)
{
$optionsSearch = self::defaultOptionsSearch();
$optionsSearch = self::getDefaultOptionsSearch();

$query = Invoice::find()
->with($optionsSearch['with'])

+ 1
- 1
common/logic/Document/Quotation/QuotationRepository.php Voir le fichier

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

class QuotationRepository extends BaseService implements RepositoryInterface
{
public function defaultOptionsSearch(): array
public function getDefaultOptionsSearch(): array
{
return [
'with' => [],

+ 1
- 1
common/logic/Document/Quotation/QuotationSearch.php Voir le fichier

@@ -56,7 +56,7 @@ class QuotationSearch extends Quotation

public function search($params)
{
$optionsSearch = self::defaultOptionsSearch();
$optionsSearch = self::getDefaultOptionsSearch();

$query = Quotation::find()
->with($optionsSearch['with'])

+ 11
- 46
common/logic/Order/Order/OrderBuilder.php Voir le fichier

@@ -5,6 +5,7 @@ namespace common\logic\Order\Order;
use common\helpers\GlobalParam;
use common\helpers\MeanPayment;
use common\helpers\Price;
use common\logic\BaseBuilder;
use common\logic\BaseService;
use common\logic\BuilderInterface;
use common\logic\Config\TaxRate\TaxRate;
@@ -32,7 +33,7 @@ use common\logic\User\User\UserSolver;
use common\logic\User\UserProducer\UserProducerRepository;
use yii\web\NotFoundHttpException;

class OrderBuilder extends BaseService implements BuilderInterface
class OrderBuilder extends BaseBuilder implements BuilderInterface
{
protected UserSolver $userSolver;
protected OrderSolver $orderSolver;
@@ -87,7 +88,7 @@ class OrderBuilder extends BaseService implements BuilderInterface
{
$order = $this->instanciateOrder($distribution);

$this->addUserPointSale($order);
$this->createUserPointSale($order);
$this->initOrderCommentPointSale($order);
$this->generateOrderReference($order);

@@ -357,9 +358,9 @@ class OrderBuilder extends BaseService implements BuilderInterface

if (isset($order->productOrder)) {
foreach ($order->productOrder as $productOrder) {
$this->addAmount($order, Order::AMOUNT_TOTAL, $productOrder, $taxCalculationMethod);
$this->addAmount($order, Order::INVOICE_AMOUNT_TOTAL, $productOrder, $taxCalculationMethod);
$this->addWeight($order, $productOrder);
$this->addProductOrderAmount($order, Order::AMOUNT_TOTAL, $productOrder, $taxCalculationMethod);
$this->addProductOrderAmount($order, Order::INVOICE_AMOUNT_TOTAL, $productOrder, $taxCalculationMethod);
$this->addProductOrderWeight($order, $productOrder);
}
}
}
@@ -436,7 +437,7 @@ class OrderBuilder extends BaseService implements BuilderInterface
// remboursement si l'utilisateur a payé pour cette commande
$amountPaid = $this->orderSolver->getAmount($order, Order::AMOUNT_PAID);

if ($amountPaid > 0.01) {
if ($amountPaid >= 0.01) {
$this->creditHistoryBuilder->create(
CreditHistory::TYPE_REFUND,
$amountPaid,
@@ -455,13 +456,13 @@ class OrderBuilder extends BaseService implements BuilderInterface

$this->productOrderBuilder->deleteByOrder($order);

return $order->delete();
return $this->delete($order);
}
// status 'delete'
elseif ($this->producerRepository->getConfig('option_behavior_cancel_order') == Producer::BEHAVIOR_DELETE_ORDER_STATUS) {
$order->date_delete = date('Y-m-d H:i:s');

return $order->save();
return $this->saveUpdate($order);
}

return false;
@@ -509,7 +510,7 @@ class OrderBuilder extends BaseService implements BuilderInterface
$order->tiller_synchronization = 0;
}

$order->save();
$this->saveUpdate($order);
}

/**
@@ -528,47 +529,11 @@ class OrderBuilder extends BaseService implements BuilderInterface
$order->save();
break;
case 'waiting-paiement-on-delivery':
if (in_array($newStatus, $orderStatusArray[$order->status]['nextStatusAllow'])) {
$this->orderStatusHistoryBuilder->create($order, $userCurrent, $newStatus, $origin);
$order->status = $newStatus;
$order->save();
}
break;
case 'waiting-paiement-by-credit':
if (in_array($newStatus, $orderStatusArray[$order->status]['nextStatusAllow'])) {
$this->orderStatusHistoryBuilder->create($order, $userCurrent, $newStatus, $origin);
$order->status = $newStatus;
$order->save();
}
break;
case 'paid-by-credit':
if (in_array($newStatus, $orderStatusArray[$order->status]['nextStatusAllow'])) {
$this->orderStatusHistoryBuilder->create($order, $userCurrent, $newStatus, $origin);
$order->status = $newStatus;
$order->save();
}
break;
case 'waiting-delevery' :
if (in_array($newStatus, $orderStatusArray[$order->status]['nextStatusAllow'])) {
$this->orderStatusHistoryBuilder->create($order, $userCurrent, $newStatus, $origin);
$order->status = $newStatus;
$order->save();
}
break;
case 'delivered':
if (in_array($newStatus, $orderStatusArray[$order->status]['nextStatusAllow'])) {
$this->orderStatusHistoryBuilder->create($order, $userCurrent, $newStatus, $origin);
$order->status = $newStatus;
$order->save();
}
break;
case 'refunded':
if (in_array($newStatus, $orderStatusArray[$order->status]['nextStatusAllow'])) {
$this->orderStatusHistoryBuilder->create($order, $userCurrent, $newStatus, $origin);
$order->status = $newStatus;
$order->save();
}
break;
case 'cancel':
if (in_array($newStatus, $orderStatusArray[$order->status]['nextStatusAllow'])) {
$this->orderStatusHistoryBuilder->create($order, $userCurrent, $newStatus, $origin);
@@ -602,7 +567,7 @@ class OrderBuilder extends BaseService implements BuilderInterface
$order->reference = 'A' . date('y') . 'C0001';
}

$order->save();
$this->saveUpdate($order);
}
}


+ 10
- 8
common/logic/Order/Order/OrderRepository.php Voir le fichier

@@ -30,7 +30,7 @@ class OrderRepository extends BaseService implements RepositoryInterface
$this->productDistributionRepository = $this->loadService(ProductDistributionRepository::class);
}

public function defaultOptionsSearch(): array
public function getDefaultOptionsSearch(): array
{
return [
'with' => [
@@ -46,7 +46,7 @@ class OrderRepository extends BaseService implements RepositoryInterface
];
}

public function findOneById(int $id)
public function findOneOrderById(int $id)
{
return Order::searchOne(['order.id' => $id]);;
}
@@ -55,7 +55,8 @@ class OrderRepository extends BaseService implements RepositoryInterface
* Recherche et initialise des commandes.
*/
// searchBy
public function findBy(array $params = [], array $options = []): array
// findBy
public function findOrdersBy(array $params = [], array $options = []): mixed
{
$orders = Order::searchBy($params, $options);

@@ -96,8 +97,9 @@ class OrderRepository extends BaseService implements RepositoryInterface
'conditions' => 'date_delete IS NULL ' . $conditionAppend
]);
}
public function queryHistory(Producer $producer, User $user, string $type = 'incoming')

// queryHistory
public function queryOrdersHistory(Producer $producer, User $user)
{
$query = Order::find()
->with('productOrder', 'pointSale', 'creditHistory')
@@ -235,7 +237,7 @@ class OrderRepository extends BaseService implements RepositoryInterface
return Order::STATE_PREPARATION;
}

public function findOneLastOfYear(Producer $producer)
public function findOneOrderLastOfYear(Producer $producer)
{
return Order::find()->innerJoinWith('distribution', true)
->where(['>=', 'distribution.date', date('Y') . '-01-01'])
@@ -251,7 +253,7 @@ class OrderRepository extends BaseService implements RepositoryInterface
public function findProductDistributionsByDistribution(Distribution $distribution): array
{
$orderArray = $this->findOrdersByDistribution($distribution);
$productDistributionArray = $this->productDistributionRepository->findProductDistributionsByDistribution();
$productDistributionArray = $this->productDistributionRepository->findProductDistributionsByDistribution($distribution);

foreach ($productDistributionArray as $productDistribution) {
if (isset($productDistribution->product)) {
@@ -260,7 +262,7 @@ class OrderRepository extends BaseService implements RepositoryInterface
'unavailable' => (int) $productDistribution->product->unavailable,
'quantity_max' => $productDistribution->quantity_max,
'quantity_order' => $this->orderSolver->getProductQuantity($productDistribution->product, $orderArray),
'quantity_remaining' => $productDistribution->quantity_max - $this->orderSolver->getProductQuantity($productDistribution->product, $orders)
'quantity_remaining' => $productDistribution->quantity_max - $this->orderSolver->getProductQuantity($productDistribution->product, $orderArray)
];
}
}

+ 1
- 1
common/logic/Order/Order/OrderSearch.php Voir le fichier

@@ -42,7 +42,7 @@ class OrderSearch extends Order
{
public function search($params)
{
$optionsSearch = self::defaultOptionsSearch() ;
$optionsSearch = self::getDefaultOptionsSearch() ;

$paramsSearch = [];
if(isset($params['id_user'])) {

+ 6
- 9
common/logic/Order/OrderStatusHistory/OrderStatusHistoryBuilder.php Voir le fichier

@@ -2,13 +2,13 @@

namespace common\logic\Order\OrderStatusHistory;

use common\logic\BaseService;
use common\logic\BaseBuilder;
use common\logic\BuilderInterface;
use common\logic\Order\Order\Order;
use common\logic\User\User\User;
use common\logic\User\User\UserSolver;

class OrderStatusHistoryBuilder extends BaseService implements BuilderInterface
class OrderStatusHistoryBuilder extends BaseBuilder implements BuilderInterface
{
protected UserSolver $userSolver;

@@ -20,11 +20,8 @@ class OrderStatusHistoryBuilder extends BaseService implements BuilderInterface
public function instanciateOrderStatusHistory(Order $order, User $user, string $status, string $origin): OrderStatusHistory
{
$orderStatusHistory = new OrderStatusHistory();

$orderStatusHistory->id_order = $order->id;
$orderStatusHistory->populateRelation('order', $order);
$orderStatusHistory->id_user = $user->id;
$orderStatusHistory->populateRelation('user', $user);
$orderStatusHistory->populateOrder($order);
$orderStatusHistory->populateUser($user);
$orderStatusHistory->status = $status;
$orderStatusHistory->origin = $origin;
$orderStatusHistory->date = date('Y-m-d H:i:s');
@@ -35,8 +32,8 @@ class OrderStatusHistoryBuilder extends BaseService implements BuilderInterface
public function createOrderStatusHistory(Order $order, User $user, string $status, string $origin): OrderStatusHistory
{
$orderStatusHistory = $this->instanciateOrderStatusHistory($order, $user, $status, $origin);
$orderStatusHistory->save();
$this->saveCreate($orderStatusHistory);

return $orderStatusHistory
return $orderStatusHistory;
}
}

+ 1
- 1
common/logic/Order/OrderStatusHistory/OrderStatusHistoryRepository.php Voir le fichier

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

class OrderStatusHistoryRepository extends BaseService implements RepositoryInterface
{
public function defaultOptionsSearch(): array
public function getDefaultOptionsSearch(): array
{
return [
'with' => [],

+ 9
- 8
common/logic/Order/ProductOrder/ProductOrderBuilder.php Voir le fichier

@@ -2,6 +2,7 @@

namespace common\logic\Order\ProductOrder;

use common\logic\BaseBuilder;
use common\logic\BaseService;
use common\logic\BuilderInterface;
use common\logic\Config\TaxRate\TaxRate;
@@ -12,7 +13,7 @@ use common\logic\Product\Product\ProductSolver;
use common\logic\User\User\User;
use common\logic\User\UserProducer\UserProducer;

class ProductOrderBuilder extends BaseService implements BuilderInterface
class ProductOrderBuilder extends BaseBuilder implements BuilderInterface
{
protected ProductSolver $productSolver;

@@ -24,7 +25,6 @@ class ProductOrderBuilder extends BaseService implements BuilderInterface
public function instanciateProductOrder(Order $order, Product $product, float $quantity, float $price): ProductOrder
{
$productOrder = new ProductOrder();

$productOrder->populateOrder($order);
$productOrder->populateProduct($product);
$productOrder->populateTaxRate($product->taxRate);
@@ -39,11 +39,12 @@ class ProductOrderBuilder extends BaseService implements BuilderInterface
public function createProductOrder(Order $order, Product $product, float $quantity, float $price): ProductOrder
{
$productOrder = $this->instanciateProductOrder($order, $product, $quantity, $price);
$productOrder->save();
$this->saveCreate($productOrder);

return $productOrder;
}

public function updatePrice(
public function updateProductOrderPrice(
ProductOrder $productOrder,
User $user = null,
UserProducer $userProducer = null,
@@ -59,12 +60,12 @@ class ProductOrderBuilder extends BaseService implements BuilderInterface
'quantity' => $quantity
]);

$productOrder->save();
$this->saveUpdate($productOrder);

return $productOrder;
}

public function updateInvoicePrice(ProductOrder $productOrder, array $params = []): void
public function updateProductOrderInvoicePrice(ProductOrder $productOrder, array $params = []): void
{
$productOrder->invoice_price = $this->productSolver->getPrice($productOrder->product, [
'user' => isset($params['user']) ?? null,
@@ -72,10 +73,10 @@ class ProductOrderBuilder extends BaseService implements BuilderInterface
'point_sale' => isset($params['point_sale']) ?? null,
'quantity' => $productOrder->quantity
]);
$productOrder->save();
$this->saveUpdate($productOrder);
}

public function deleteByOrder(Order $order): void
public function deleteProductOrdersByOrder(Order $order): void
{
ProductOrder::deleteAll(['id_order' => $order->id]);
}

+ 2
- 2
common/logic/Order/ProductOrder/ProductOrderRepository.php Voir le fichier

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

class ProductOrderRepository extends BaseService implements RepositoryInterface
{
public function defaultOptionsSearch(): array
public function getDefaultOptionsSearch(): array
{
return [
'with' => ['taxRate'],
@@ -18,7 +18,7 @@ class ProductOrderRepository extends BaseService implements RepositoryInterface
];
}

public function getByOrder(Order $order)
public function findProductOrdersByOrder(Order $order): array
{
return ProductOrder::find()->where(['id_order' => $order->id])->all();
}

+ 21
- 16
common/logic/PointSale/PointSale/PointSaleBuilder.php Voir le fichier

@@ -2,6 +2,7 @@

namespace common\logic\PointSale\PointSale;

use common\logic\BaseBuilder;
use common\logic\BaseService;
use common\logic\BuilderInterface;
use common\logic\PointSale\UserPointSale\UserPointSale;
@@ -11,7 +12,7 @@ use common\logic\Producer\Producer\Producer;
use common\logic\User\User\User;
use common\logic\User\User\UserRepository;

class PointSaleBuilder extends BaseService implements BuilderInterface
class PointSaleBuilder extends BaseBuilder implements BuilderInterface
{
protected UserPointSaleBuilder $userPointSaleBuilder;
protected UserRepository $userRepository;
@@ -31,10 +32,18 @@ class PointSaleBuilder extends BaseService implements BuilderInterface
return $pointSale;
}

public function createPointSale(): PointSale
{
$pointSale = $this->instanciatePointSale();
$this->saveCreate($pointSale);

return $pointSale;
}

/**
* Initialise les commandes liées au point de vente.
*/
public function initOrders(PointSale $pointSale, array $ordersArray): void
public function initPointSaleOrders(PointSale $pointSale, array $ordersArray): void
{
$pointSale->orders = [];
$pointSale->revenues = 0;
@@ -54,7 +63,8 @@ class PointSaleBuilder extends BaseService implements BuilderInterface
}
}

public function resetPointProductions(Producer $producer): void
// resetPointProductions
public function resetPointSalePointProductions(Producer $producer): void
{
PointSale::updateAll(
['point_production' => 0],
@@ -66,12 +76,13 @@ class PointSaleBuilder extends BaseService implements BuilderInterface
* Traite la mise à jour de l'attribut 'point_production'.
*/
// processPointProduction
public function updatePointProduction(PointSale $pointSale): void
// updatePointProduction
public function updatePointSalePointProduction(PointSale $pointSale): void
{
if ($pointSale->point_production) {
$this->resetPointProductions($pointSale->producer);
$this->resetPointSalePointProductions($pointSale->producer);
$pointSale->point_production = 1;
$pointSale->save();
$this->saveUpdate($pointSale);
}
}

@@ -80,13 +91,13 @@ class PointSaleBuilder extends BaseService implements BuilderInterface
*/
public function processRestrictedAccess(PointSale $pointSale): void
{
$this->userPointSaleBuilder->deleteByPointSale($pointSale);
$this->userPointSaleBuilder->deleteUserPointSaleByPointSale($pointSale);

if (is_array($pointSale->users) && count($pointSale->users)) {
foreach ($pointSale->users as $key => $val) {
$user = $this->userRepository->getOneById($val);
$user = $this->userRepository->findOneUserById($val);
if ($user) {
$this->userPointSaleBuilder->create($user, $pointSale, $pointSale->users_comment[$val]);
$this->userPointSaleBuilder->createUserPointSale($user, $pointSale, $pointSale->users_comment[$val]);
}
}
}
@@ -98,12 +109,6 @@ class PointSaleBuilder extends BaseService implements BuilderInterface
// linkUser
public function addUser(User $user, PointSale $pointSale): UserPointSale
{
$userPointSale = $this->userPointSaleRepository->getOne($user, $pointSale);

if (!$userPointSale) {
$userPointSale = $this->userPointSaleBuilder->create($user, $pointSale);
}

return $userPointSale;
return $this->userPointSaleBuilder->createUserPointSaleIfNotExist($user, $pointSale);
}
}

+ 2
- 2
common/logic/PointSale/PointSale/PointSaleRepository.php Voir le fichier

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

class PointSaleRepository extends BaseService implements RepositoryInterface
{
public function defaultOptionsSearch(): array
public function getDefaultOptionsSearch(): array
{
return [
'with' => [],
@@ -65,7 +65,7 @@ class PointSaleRepository extends BaseService implements RepositoryInterface
);
}

public function populateDropdownList(): array
public function populatePointSaleDropdownList(): array
{
$pointSalesArrayDropdown = ['' => '--'];
$pointSalesArray = $this->get();

+ 1
- 1
common/logic/PointSale/PointSale/PointSaleSearch.php Voir le fichier

@@ -63,7 +63,7 @@ class PointSaleSearch extends PointSale
public function search($params)
{
$optionsSearch = self::defaultOptionsSearch() ;
$optionsSearch = self::getDefaultOptionsSearch() ;
$query = PointSale::find()
->with($optionsSearch['with'])

+ 11
- 0
common/logic/PointSale/UserPointSale/UserPointSale.php Voir le fichier

@@ -40,6 +40,7 @@ namespace common\logic\PointSale\UserPointSale;

use common\logic\PointSale\PointSale\PointSale;
use common\components\ActiveRecordCommon ;
use common\logic\User\User\User;

/**
* This is the model class for table "user_point_sale".
@@ -88,4 +89,14 @@ class UserPointSale extends ActiveRecordCommon
{
$this->populateFieldObject('id_point_sale', 'pointSale', $pointSale);
}

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

public function populateUser(User $user): void
{
$this->populateFieldObject('id_user', 'user', $user);
}
}

+ 19
- 9
common/logic/PointSale/UserPointSale/UserPointSaleBuilder.php Voir le fichier

@@ -2,21 +2,25 @@

namespace common\logic\PointSale\UserPointSale;

use common\logic\BaseService;
use common\logic\BaseBuilder;
use common\logic\BuilderInterface;
use common\logic\PointSale\PointSale\PointSale;
use common\logic\User\User\User;

class UserPointSaleBuilder extends BaseService implements BuilderInterface
class UserPointSaleBuilder extends BaseBuilder implements BuilderInterface
{
protected UserPointSaleRepository $userPointSaleRepository;

public function __construct()
{
$this->userPointSaleRepository = $this->loadService(UserPointSaleRepository::class);
}

public function instanciateUserPointSale(User $user, PointSale $pointSale, string $comment = null): UserPointSale
{
$userPointSale = new UserPointSale();

$userPointSale->id_user = $user->id;
$userPointSale->populateRelation('user', $user);
$userPointSale->id_point_sale = $pointSale->id;
$userPointSale->populateRelation('pointSale', $pointSale);
$userPointSale->populatePointSale($pointSale);
$userPointSale->populateUser($user);

if($comment) {
$userPointSale->comment = $comment;
@@ -28,12 +32,18 @@ class UserPointSaleBuilder extends BaseService implements BuilderInterface
public function createUserPointSale(User $user, PointSale $pointSale, string $comment = null): UserPointSale
{
$userPointSale = $this->instanciateUserPointSale($user, $pointSale, $comment);
$userPointSale->save();
$this->saveCreate($userPointSale);

return $userPointSale;
}

public function deleteByPointSale(PointSale $pointSale): void
public function createUserPointSaleIfNotExist(User $user, PointSale $pointSale, string $comment = null): UserPointSale
{
return $this->userPointSaleRepository->findOneUserPointSale($user, $pointSale)
?? $this->createUserPointSale($user, $pointSale);
}

public function deleteUserPointSalesByPointSale(PointSale $pointSale): void
{
UserPointSale::deleteAll(['id_point_sale' => $pointSale->id]);
}

+ 2
- 2
common/logic/PointSale/UserPointSale/UserPointSaleRepository.php Voir le fichier

@@ -11,7 +11,7 @@ use common\logic\User\User\User;

class UserPointSaleRepository extends BaseService implements RepositoryInterface
{
public function defaultOptionsSearch(): array
public function getDefaultOptionsSearch(): array
{
return [
'with' => [],
@@ -21,7 +21,7 @@ class UserPointSaleRepository extends BaseService implements RepositoryInterface
] ;
}

public function getOne(User $user, PointSale $pointSale)
public function findOneUserPointSale(User $user, PointSale $pointSale)
{
return UserPointSale::find()
->where([

+ 12
- 10
common/logic/Producer/Producer/ProducerBuilder.php Voir le fichier

@@ -4,6 +4,7 @@ namespace common\logic\Producer\Producer;

use common\helpers\Opendistrib;
use common\helpers\Password;
use common\logic\BaseBuilder;
use common\logic\BaseService;
use common\logic\BuilderInterface;
use common\logic\User\User\User;
@@ -12,7 +13,7 @@ use common\logic\User\UserProducer\UserProducerBuilder;
use common\logic\User\UserProducer\UserProducerRepository;
use common\helpers\Url;

class ProducerBuilder extends BaseService implements BuilderInterface
class ProducerBuilder extends BaseBuilder implements BuilderInterface
{
protected ProducerRepository $producerRepository;
protected UserProducerRepository $userProducerRepository;
@@ -30,20 +31,19 @@ class ProducerBuilder extends BaseService implements BuilderInterface
public function instanciateProducer(): Producer
{
$producer = new Producer();

$producer->order_deadline = Producer::ORDER_DEADLINE_DEFAULT;
$producer->order_delay = Producer::ORDER_DELAY_DEFAULT;

return $producer;
}

public function init(Producer $producer): void
public function initProducer(Producer $producer): void
{
$this->initSlug($producer);
$this->initCode($producer);
$this->initProducerSlug($producer);
$this->initProducerCode($producer);
}

public function initSlug(Producer $producer): void
public function initProducerSlug(Producer $producer): void
{
$cptSlug = 0 ;
do {
@@ -56,7 +56,7 @@ class ProducerBuilder extends BaseService implements BuilderInterface
} while($this->producerRepository->getOneBySlug($producer->slug)) ;
}

public function initCode(Producer $producer): void
public function initProducerCode(Producer $producer): void
{
$producer->code = Password::generate();
}
@@ -66,12 +66,13 @@ class ProducerBuilder extends BaseService implements BuilderInterface
*/
public function addUser(User $user, Producer $producer, int $bookmark = 1): UserProducer
{
$userProducer = $this->userProducerBuilder->createIfNotExist($user, $producer, $bookmark);
$userProducer = $this->userProducerBuilder->createUserProducerIfNotExist($user, $producer, $bookmark);

if (!$userProducer->getActive()) {
$userProducer->setActive(1);
}
$userProducer->save();

$this->saveUpdate($userProducer);

return $userProducer;
}
@@ -80,7 +81,8 @@ class ProducerBuilder extends BaseService implements BuilderInterface
{
$versionsArray = Opendistrib::getVersions();
$producer->latest_version_opendistrib = array_values($versionsArray)[0];
$producer->save();

$this->saveUpdate($producer);
}

public function savePrivateKeyStripe($filename, $value)

+ 15
- 14
common/logic/Producer/Producer/ProducerRepository.php Voir le fichier

@@ -7,6 +7,7 @@ use common\helpers\GlobalParam;
use common\helpers\Price;
use common\logic\BaseService;
use common\logic\Document\Document\DocumentInterface;
use common\logic\Document\Document\DocumentSolver;
use common\logic\PointSale\PointSale\PointSale;
use common\logic\Producer\ProducerPriceRange\ProducerPriceRange;
use common\logic\Producer\ProducerPriceRange\ProducerPriceRangeRepository;
@@ -18,18 +19,20 @@ class ProducerRepository extends BaseService implements RepositoryInterface
{
protected ProducerPriceRangeRepository $producerPriceRangeRepository;
protected ProducerSolver $producerSolver;
protected DocumentSolver $documentSolver;

public function __construct()
{
$this->producerPriceRangeRepository = $this->loadService(ProducerPriceRangeRepository::class);
$this->producerSolver = $this->loadService(ProducerSolver::class);
$this->documentSolver = $this->loadService(DocumentSolver::class);
}

/**
* Retourne les options de base nécessaires à la fonction de recherche.
*
*/
public function defaultOptionsSearch(): array
public function getDefaultOptionsSearch(): array
{
return [
'with' => ['taxRate'],
@@ -39,17 +42,17 @@ class ProducerRepository extends BaseService implements RepositoryInterface
];
}

public function getOneById(int $id)
public function findOneProducerById(int $id)
{
return Producer::searchOne(['id' => $id]);
}

public function getOneBySlug(string $slug)
public function findOneProducerBySlug(string $slug)
{
return Producer::searchOne(['slug' => $slug]);
}

public function queryActive()
public function queryProducerActive()
{
return Producer::find()
->where([
@@ -62,16 +65,13 @@ class ProducerRepository extends BaseService implements RepositoryInterface
* Retourne le compte producteur de démonstration.
*
*/
public function getOneDemoAccount()
public function findOneProducerDemoAccount()
{
return Producer::find()->where('name LIKE \'Démo\'')->one();
}

/**
* Retourne la liste des établissements pour l'initialisation d'une liste
* sélective.
*
* @return array
* Retourne la liste des établissements pour l'initialisation d'une listesélective.
*/
public static function getPopulateDropdown(): array
{
@@ -167,7 +167,7 @@ class ProducerRepository extends BaseService implements RepositoryInterface
$turnover = $this->getTurnover($producer, $month);

if ($turnover) {
$isBold = $this->isBillingTypeClassic() && !$this->option_billing_permanent_transfer;
$isBold = $this->producerSolver->isBillingTypeClassic($producer) && !$producer->option_billing_permanent_transfer;
if ($isBold) $text .= '<strong>';
$text .= $this->producerPriceRangeRepository->getAmountToBeBilledByTurnover($turnover, true);
if ($isBold) $text .= '</strong>';
@@ -218,7 +218,7 @@ class ProducerRepository extends BaseService implements RepositoryInterface
$idProducer = GlobalParam::getCurrentProducerId();
}

$producer = $this->getOneById($idProducer);
$producer = $this->findOneProducerById($idProducer);
if ($producer) {
return $producer->$config;
}
@@ -230,7 +230,8 @@ class ProducerRepository extends BaseService implements RepositoryInterface
/**
* Retourne les établissements liés à l'utilisateur.
*/
public function getBookmarked(User $user): array
// getBookmarked
public function findProducersBookmarked(User $user): array
{
$producers = (new \yii\db\Query())
->select('*')
@@ -245,13 +246,13 @@ class ProducerRepository extends BaseService implements RepositoryInterface

public function getNameProducer(User $user): string
{
$producer = $this->getOneById($user->id_producer);
$producer = $this->findOneProducerById($user->id_producer);
return $producer->getName();
}

public function isDocumentDisplayOrders(DocumentInterface $document): bool
{
return ($document->getClass() == 'Invoice') ?
return ($this->documentSolver->getClass($document) == 'Invoice') ?
$this->getConfig('document_display_orders_invoice') :
$this->getConfig('document_display_orders_delivery_note');
}

+ 23
- 0
common/logic/Producer/ProducerPriceRange/ProducerPriceRangeBuilder.php Voir le fichier

@@ -0,0 +1,23 @@
<?php

namespace common\logic\Producer\ProducerPriceRange;

use common\logic\BaseBuilder;

class ProducerPriceRangeBuilder extends BaseBuilder
{
public function instanciateProducerPriceRange(): ProducerPriceRange
{
$producerPriceRange = new ProducerPriceRange();

return $producerPriceRange;
}

public function createProducerPriceRange(): ProducerPriceRange
{
$producerPriceRange = $this->instanciateProducerPriceRange();
$this->saveCreate($producerPriceRange);

return $producerPriceRange;
}
}

+ 1
- 0
common/logic/Producer/ProducerPriceRange/ProducerPriceRangeManager.php Voir le fichier

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

/**
* @mixin ProducerPriceRangeRepository
* @mixin ProducerPriceRangeBuilder
*/
class ProducerPriceRangeManager extends BaseManager
{

+ 8
- 3
common/logic/Producer/ProducerPriceRange/ProducerPriceRangeRepository.php Voir le fichier

@@ -12,7 +12,7 @@ class ProducerPriceRangeRepository extends BaseService implements RepositoryInte
* Retourne les options de base nécessaires à la fonction de recherche.
*
*/
public function defaultOptionsSearch(): array
public function getDefaultOptionsSearch(): array
{
return [
'with' => [],
@@ -22,15 +22,20 @@ class ProducerPriceRangeRepository extends BaseService implements RepositoryInte
];
}

public function query()
public function queryProducerPriceRanges()
{
return ProducerPriceRange::find()->orderBy('range_begin ASC');
}

public function findProducerPriceRanges()
{
return $this->queryProducerPriceRanges()->all();
}

public function getAmountToBeBilledByTurnover(float $turnover, $format = false)
{
$amountToBeBilled = 0;
$producerPriceRangeArray = ProducerPriceRange::find()->all();
$producerPriceRangeArray = $this->findProducerPriceRanges();
foreach ($producerPriceRangeArray as $priceRange) {
if ($turnover >= $priceRange->range_begin && $turnover < $priceRange->range_end) {
$amountToBeBilled = $priceRange->price;

+ 11
- 3
common/logic/Product/Product/ProductBuilder.php Voir le fichier

@@ -2,15 +2,23 @@

namespace common\logic\Product\Product;

use common\logic\BaseService;
use common\logic\BaseBuilder;
use common\logic\BuilderInterface;

class ProductBuilder extends BaseService implements BuilderInterface
class ProductBuilder extends BaseBuilder implements BuilderInterface
{
public function instanciate(): Product
public function instanciateProduct(): Product
{
$product = new Product();

return $product;
}

public function createProduct(): Product
{
$product = $this->instanciateProduct();
$this->saveCreate($product);

return $product;
}
}

+ 34
- 32
common/logic/Product/Product/ProductRepository.php Voir le fichier

@@ -26,7 +26,7 @@ class ProductRepository extends BaseService implements RepositoryInterface
$this->userProducerRepository = $this->loadService(UserProducerRepository::class);
}

public function defaultOptionsSearch(): array
public function getDefaultOptionsSearch(): array
{
return [
'with' => ['taxRate', 'productPointSale'],
@@ -36,7 +36,7 @@ class ProductRepository extends BaseService implements RepositoryInterface
];
}

public function get(): array
public function findProducts(): array
{
return Product::searchAll();
}
@@ -44,7 +44,7 @@ class ProductRepository extends BaseService implements RepositoryInterface
/**
* Retourne le nombre de produits du producteur courant.
*/
public static function count(): int
public static function countProducts(): int
{
return Product::searchCount();
}
@@ -53,7 +53,8 @@ class ProductRepository extends BaseService implements RepositoryInterface
* Retourne les produits d'une production donnée.
*/
// searchByDistribution
public function getByDistribution(Distribution $distribution)
// getByDistribution
public function findProductsByDistribution(Distribution $distribution)
{
return Product::find()
->leftJoin('product_distribution', 'product.id = product_distribution.id_product')
@@ -65,12 +66,40 @@ class ProductRepository extends BaseService implements RepositoryInterface
->all();
}

// queryByProductCategory
public function queryProductsByProductCategory(ProductCategory $productCategory)
{
return Product::find()
->andWhere([
'id_producer' => $productCategory->id_producer,
'active' => true,
])
->andWhere(
'product.id_product_category = :id_product_category'
)
->params(
[':id_product_category' => $productCategory->id]
)
->orderBy(
'order ASC'
);
}

public function countProductsWithoutCategory(Producer $producer): int
{
return Product::searchCount([
'id_producer' => $producer->id,
'product.active' => 1,
'product.id_product_category' => null
]);
}

public function getPriceArray(Product $product, User $user, PointSale $pointSale): array
{
$priceArray = [];
$userProducer = null;
if ($user) {
$userProducer = $this->userProducerRepository->getOne($user, GlobalParam::getCurrentProducer());
$userProducer = $this->userProducerRepository->findOneUserProducer($user, GlobalParam::getCurrentProducer());
}

// specific prices
@@ -119,31 +148,4 @@ class ProductRepository extends BaseService implements RepositoryInterface

return $priceArray;
}

public function queryByProductCategory(ProductCategory $productCategory)
{
return Product::find()
->andWhere([
'id_producer' => $productCategory->id_producer,
'active' => true,
])
->andWhere(
'product.id_product_category = :id_product_category'
)
->params(
[':id_product_category' => $productCategory->id]
)
->orderBy(
'order ASC'
);
}

public function countProductsWithoutCategory(Producer $producer): int
{
return Product::searchCount([
'id_producer' => $producer->id,
'product.active' => 1,
'product.id_product_category' => null
]);
}
}

+ 1
- 1
common/logic/Product/Product/ProductSearch.php Voir le fichier

@@ -57,7 +57,7 @@ class ProductSearch extends Product
public function search($params)
{
$optionsSearch = self::defaultOptionsSearch() ;
$optionsSearch = self::getDefaultOptionsSearch() ;
$query = Product::find()
->with($optionsSearch['with'])

+ 11
- 2
common/logic/Product/ProductCategory/ProductCategoryBuilder.php Voir le fichier

@@ -2,15 +2,24 @@

namespace common\logic\Product\ProductCategory;

use common\logic\BaseBuilder;
use common\logic\BaseService;
use common\logic\BuilderInterface;

class ProductCategoryBuilder extends BaseService implements BuilderInterface
class ProductCategoryBuilder extends BaseBuilder implements BuilderInterface
{
public function instanciate(): ProductCategory
public function instanciateProductCategory(): ProductCategory
{
$productCategory = new ProductCategory();

return $productCategory;
}

public function createProductCategory(): ProductCategory
{
$productCategory = $this->instanciateProductCategory();
$this->saveCreate($productCategory);

return $productCategory;
}
}

+ 5
- 5
common/logic/Product/ProductCategory/ProductCategoryRepository.php Voir le fichier

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

class ProductCategoryRepository extends BaseService implements RepositoryInterface
{
public function defaultOptionsSearch(): array
public function getDefaultOptionsSearch(): array
{
return [
'with' => [],
@@ -18,12 +18,12 @@ class ProductCategoryRepository extends BaseService implements RepositoryInterfa
];
}

public function get()
public function findProductCategories()
{
return ProductCategory::searchAll([], ['orderby' => 'product_category.position ASC']);
}

public function getAsArray()
public function findProductCategoriesAsArray()
{
return ProductCategory::searchAll(
[],
@@ -34,10 +34,10 @@ class ProductCategoryRepository extends BaseService implements RepositoryInterfa
);
}

public function populateDropdownList()
public function populateProductCategoriesDropdownList()
{
$productCategoriesArrayDropdown = ['' => '--'];
$productCategoriesArray = $this->get();
$productCategoriesArray = $this->findProductCategories();

foreach ($productCategoriesArray as $productCategory) {
$productCategoriesArrayDropdown[$productCategory['id']] = $productCategory['name'];

+ 1
- 1
common/logic/Product/ProductCategory/ProductCategorySearch.php Voir le fichier

@@ -54,7 +54,7 @@ class ProductCategorySearch extends ProductCategory
public function search($params)
{
$optionsSearch = self::defaultOptionsSearch() ;
$optionsSearch = self::getDefaultOptionsSearch() ;
$query = ProductCategory::find()
->with($optionsSearch['with'])

+ 11
- 3
common/logic/Product/ProductPointSale/ProductPointSaleBuilder.php Voir le fichier

@@ -2,15 +2,23 @@

namespace common\logic\Product\ProductPointSale;

use common\logic\BaseService;
use common\logic\BaseBuilder;
use common\logic\BuilderInterface;

class ProductPointSaleBuilder extends BaseService implements BuilderInterface
class ProductPointSaleBuilder extends BaseBuilder implements BuilderInterface
{
public function instanciate(): ProductPointSale
public function instanciateProductPointSale(): ProductPointSale
{
$productPointSale = new ProductPointSale();

return $productPointSale;
}

public function createProductPointSale(): ProductPointSale
{
$productPointSale = $this->instanciateProductPointSale();
$this->saveCreate($productPointSale);

return $productPointSale;
}
}

+ 1
- 1
common/logic/Product/ProductPointSale/ProductPointSaleRepository.php Voir le fichier

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

class ProductPointSaleRepository extends BaseService implements RepositoryInterface
{
public function defaultOptionsSearch(): array
public function getDefaultOptionsSearch(): array
{
return [
'with' => ['product', 'pointSale'],

+ 11
- 3
common/logic/Product/ProductPrice/ProductPriceBuilder.php Voir le fichier

@@ -2,15 +2,23 @@

namespace common\logic\Product\ProductPrice;

use common\logic\BaseService;
use common\logic\BaseBuilder;
use common\logic\BuilderInterface;

class ProductPriceBuilder extends BaseService implements BuilderInterface
class ProductPriceBuilder extends BaseBuilder implements BuilderInterface
{
public function instanciate(): ProductPrice
public function instanciateProductPrice(): ProductPrice
{
$productPrice = new ProductPrice();

return $productPrice;
}

public function createProductPrice(): ProductPrice
{
$productPrice = $this->instanciateProductPrice();
$this->saveCreate($productPrice);

return $productPrice;
}
}

+ 1
- 1
common/logic/Product/ProductPrice/ProductPriceRepository.php Voir le fichier

@@ -10,7 +10,7 @@ class ProductPriceRepository extends BaseService implements RepositoryInterface
/**
* Retourne les options de base nécessaires à la fonction de recherche.
*/
public function defaultOptionsSearch(): array
public function getDefaultOptionsSearch(): array
{
return [
'with' => ['user', 'pointSale'],

+ 1
- 1
common/logic/Product/ProductPrice/ProductPriceSearch.php Voir le fichier

@@ -58,7 +58,7 @@ class ProductPriceSearch extends ProductPrice

public function search($params)
{
$optionsSearch = self::defaultOptionsSearch() ;
$optionsSearch = self::getDefaultOptionsSearch() ;

$query = ProductPrice::find()
->with($optionsSearch['with'])

+ 11
- 3
common/logic/Subscription/ProductSubscription/ProductSubscriptionBuilder.php Voir le fichier

@@ -2,15 +2,23 @@

namespace common\logic\Subscription\ProductSubscription;

use common\logic\BaseService;
use common\logic\BaseBuilder;
use common\logic\BuilderInterface;

class ProductSubscriptionBuilder extends BaseService implements BuilderInterface
class ProductSubscriptionBuilder extends BaseBuilder implements BuilderInterface
{
public function instanciate(): ProductSubscription
public function instanciateProductSubscription(): ProductSubscription
{
$productSubscription = new ProductSubscription();

return $productSubscription;
}

public function createProductSubscription(): ProductSubscription
{
$productSubscription = $this->instanciateProductSubscription();
$this->saveCreate($productSubscription);

return $productSubscription;
}
}

+ 1
- 1
common/logic/Subscription/ProductSubscription/ProductSubscriptionRepository.php Voir le fichier

@@ -10,7 +10,7 @@ class ProductSubscriptionRepository extends BaseService implements RepositoryInt
/**
* Retourne les options de base nécessaires à la fonction de recherche.
*/
public function defaultOptionsSearch(): array
public function getDefaultOptionsSearch(): array
{
return [
'with' => ['product'],

+ 14
- 11
common/logic/Subscription/Subscription/SubscriptionBuilder.php Voir le fichier

@@ -2,17 +2,12 @@

namespace common\logic\Subscription\Subscription;

use common\logic\BaseService;
use common\logic\BaseBuilder;
use common\logic\BuilderInterface;
use common\logic\Distribution\Distribution\Distribution;
use common\logic\Distribution\Distribution\DistributionRepository;
use common\logic\PointSale\PointSale\PointSale;
use common\logic\Producer\Producer\Producer;
use common\logic\Subscription\ProductSubscription\ProductSubscription;
use common\logic\User\User\User;
use common\logic\User\UserProducer\UserProducer;

class SubscriptionBuilder extends BaseService implements BuilderInterface
class SubscriptionBuilder extends BaseBuilder implements BuilderInterface
{
protected DistributionRepository $distributionRepository;

@@ -21,16 +16,24 @@ class SubscriptionBuilder extends BaseService implements BuilderInterface
$this->distributionRepository = $this->loadService(DistributionRepository::class);
}

public function instanciate(): Subscription
public function instanciateSubscription(): Subscription
{
$subscription = new Subscription();

return $subscription;
}

public function delete(Subscription $subscription): void
public function createSubscription(): Subscription
{
ProductSubscription::deleteAll(['id_subscription' => $id]);
$subscription->delete();
$subscription = $this->instanciateSubscription();
$this->saveCreate($subscription);

return $subscription;
}

public function deleteSubscription(Subscription $subscription): void
{
ProductSubscription::deleteAll(['id_subscription' => $subscription->id]);
$this->delete($subscription);
}
}

+ 19
- 0
common/logic/Subscription/Subscription/SubscriptionEventSubscriber.php Voir le fichier

@@ -0,0 +1,19 @@
<?php

namespace common\logic\Subscription\Subscription;

use common\logic\Distribution\Distribution\Distribution;
use common\logic\Order\Order\OrderManager;

class SubscriptionEventSubscriber
{
/**
* @param Distribution $distribution
* @return void
*/
public static function onActiveDistribution(Distribution $distribution): void
{
$orderManager = new OrderManager();
$orderManager->createAllOrdersFromSubscriptions($distribution->date);
}
}

+ 9
- 46
common/logic/Subscription/Subscription/SubscriptionRepository.php Voir le fichier

@@ -18,7 +18,7 @@ class SubscriptionRepository extends BaseService implements RepositoryInterface
$this->subscriptionSolver = $this->loadService(SubscriptionSolver::class);
}

public function defaultOptionsSearch(): array
public function getDefaultOptionsSearch(): array
{
return [
'with' => ['producer'],
@@ -28,12 +28,12 @@ class SubscriptionRepository extends BaseService implements RepositoryInterface
];
}

public function getOneById(int $id): ?Subscription
public function findOneSubscriptionById(int $id): ?Subscription
{
return Subscription::searchOne(['id' => $id]);
}

public function get()
public function findSubscriptions()
{
return Subscription::searchAll();
}
@@ -41,17 +41,17 @@ class SubscriptionRepository extends BaseService implements RepositoryInterface
/**
* Retourne les abonnements pour une date donnée.
*/
// searchByDate
public function getByDate(string $date)
// searchByDate, getByDate
public function findSubscriptionsByDate(string $date)
{
$date = date('Y-m-d', strtotime($date));
$subscriptionArray = $this->get();
$subscriptionArray = $this->findSubscriptions();
$subscriptionReturnArray = [];

foreach ($subscriptionArray as $subscription) {
if ($date >= $subscription->date_begin &&
(!$subscription->date_end || $date <= $subscription->date_end) &&
$subscription->matchWith($date)) {
(!$subscription->date_end || $date <= $subscription->date_end)
&& $this->subscriptionSolver->isSubscriptionMatchWith($subscription, $date)) {
$subscriptionReturnArray[] = $subscription;
}
}
@@ -59,44 +59,7 @@ class SubscriptionRepository extends BaseService implements RepositoryInterface
return $subscriptionReturnArray;
}

/**
* Recherche les distributions futures où l'abonnement peut s'appliquer.
*/
public function searchMatchedIncomingDistributions(Subscription $subscription): array
{
$params = [
':date_earliest_order' => date('Y-m-d'),
':date_begin' => date('Y-m-d', strtotime($subscription->date_begin)),
':id_producer' => GlobalParam::getCurrentProducerId()
];

$incomingDistributions = Distribution::find()
->where('id_producer = :id_producer')
->andWhere('date >= :date_begin')
->andWhere('date > :date_earliest_order');

if ($subscription->date_end) {
$incomingDistributions->andWhere('date <= :date_end');
$params[':date_end'] = date('Y-m-d', strtotime($subscription->date_end));
}

$incomingDistributions->orderBy('date ASC');

$incomingDistributions->params($params);
$incomingDistributionsArray = $incomingDistributions->all();
$this->subscriptionSolver->filterDistributionsByDateDelay($incomingDistributionsArray);

$matchedIncomingDistributionsArray = [];
foreach ($incomingDistributionsArray as $incomingDistribution) {
if ($this->subscriptionSolver->matchWith($subscription, $incomingDistribution->date)) {
$matchedIncomingDistributionsArray[] = $incomingDistribution;
}
}

return $matchedIncomingDistributionsArray;
}

public function count(User $user, Producer $producer)
public function countSubscriptionsByUser(User $user, Producer $producer)
{
return Subscription::find()
->where([

+ 1
- 1
common/logic/Subscription/Subscription/SubscriptionSearch.php Voir le fichier

@@ -57,7 +57,7 @@ class SubscriptionSearch extends Subscription
public function search($params) {
$optionsSearch = self::defaultOptionsSearch() ;
$optionsSearch = self::getDefaultOptionsSearch() ;
$query = Subscription::find()
->with($optionsSearch['with'])

+ 1
- 1
common/logic/Subscription/Subscription/SubscriptionSolver.php Voir le fichier

@@ -45,7 +45,7 @@ class SubscriptionSolver extends BaseService implements SolverInterface
/**
* Valide le fait qu'un abonnement est bien compatible avec une date donnée.
*/
public function matchWith(Subscription $subscription, string $date): bool
public function isSubscriptionMatchWith(Subscription $subscription, string $date): bool
{
$arrayDays = [
1 => 'monday',

+ 24
- 0
common/logic/User/CreditHistory/CreditHistory.php Voir le fichier

@@ -39,13 +39,18 @@
namespace common\logic\User\CreditHistory;

use common\components\ActiveRecordCommon;
use common\logic\Producer\Producer\Producer;
use common\logic\User\User\User;
use common\logic\Order\Order\Order;
use common\logic\User\UserProducer\UserProducerEventSubscriber;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;


class CreditHistory extends ActiveRecordCommon
{
const EVENT_CREATE = 'creditHistory.create';

const TYPE_INITIAL_CREDIT = 'initial-credit';
const TYPE_CREDIT = 'credit';
const TYPE_PAYMENT = 'payment';
@@ -94,10 +99,29 @@ class CreditHistory extends ActiveRecordCommon
];
}

public function init()
{
$this->on(CreditHistory::EVENT_CREATE, function($event) {
UserProducerEventSubscriber::onCreateCreditHistory($event->creditHistory);
});

parent::init();
}

/*
* Relations
*/

public function getProducer(): ActiveQuery
{
return $this->hasOne(Producer::class, ['id' => 'id_producer']);
}

public function populateProducer(Producer $producer): void
{
$this->populateFieldObject('id_producer', 'producer', $producer);
}

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

+ 13
- 21
common/logic/User/CreditHistory/CreditHistoryBuilder.php Voir le fichier

@@ -2,14 +2,15 @@

namespace common\logic\User\CreditHistory;

use common\logic\BaseService;
use common\logic\BaseBuilder;
use common\logic\BuilderInterface;
use common\logic\Order\Order\Order;
use common\logic\Producer\Producer\Producer;
use common\logic\User\User\User;
use common\logic\User\UserProducer\UserProducerBuilder;
use yii\base\Event;
use yii\db\ActiveRecord;

class CreditHistoryBuilder extends BaseService implements BuilderInterface
class CreditHistoryBuilder extends BaseBuilder implements BuilderInterface
{
protected CreditHistorySolver $creditHistorySolver;

@@ -18,7 +19,7 @@ class CreditHistoryBuilder extends BaseService implements BuilderInterface
$this->creditHistorySolver = $this->loadService(CreditHistorySolver::class);
}

public function instanciate(
public function instanciateCreditHistory(
string $type,
float $amount,
Producer $producer,
@@ -32,16 +33,12 @@ class CreditHistoryBuilder extends BaseService implements BuilderInterface

$creditHistory->type = $type;
$creditHistory->amount = round($amount, 2);
$creditHistory->id_producer = $producer->id;
$creditHistory->populateRelation('producer', $producer);
$creditHistory->id_user = $user->id;
$creditHistory->populateRelation('user', $user);
$creditHistory->id_user_action = $userAction->id;
$creditHistory->populateRelation('userAction', $userAction);
$creditHistory->populateProducer($producer);
$creditHistory->populateUser($user);
$creditHistory->populateUserAction($userAction);

if($order) {
$creditHistory->id_order = $order->id;
$creditHistory->populateRelation('order', $order);
$creditHistory->populateOrder($order);
}

if($meanPayment) {
@@ -52,7 +49,7 @@ class CreditHistoryBuilder extends BaseService implements BuilderInterface
}

// saveCreditHistory
public function create(
public function createCreditHistory(
string $type,
float $amount,
Producer $producer,
@@ -66,16 +63,11 @@ class CreditHistoryBuilder extends BaseService implements BuilderInterface
return null;
}

$creditHistory = $this->instanciate($type, $amount, $producer, $user, $userAction, $meanPayment, $order);

// Initialisation du commentaire avant sauvegarde
$creditHistory = $this->instanciateCreditHistory($type, $amount, $producer, $user, $userAction, $meanPayment, $order);
$creditHistory->setComment($creditHistory->getComment() . $this->creditHistorySolver->getStrComment($creditHistory));
$this->saveCreate($creditHistory);

$creditHistory->save();

// Mise à jour du crédit au niveau de UserProducer
// @TODO : à gérer avec les événements
//$this->userProducerBuilder->updateCredit($creditHistory);
$creditHistory->trigger(CreditHistory::EVENT_CREATE, new Event(['creditHistory' => $creditHistory]));

return $creditHistory;
}

+ 1
- 1
common/logic/User/CreditHistory/CreditHistoryRepository.php Voir le fichier

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

class CreditHistoryRepository extends BaseService implements RepositoryInterface
{
public function defaultOptionsSearch(): array
public function getDefaultOptionsSearch(): array
{
return [
'with' => [],

+ 1
- 1
common/logic/User/CreditHistory/CreditHistorySearch.php Voir le fichier

@@ -56,7 +56,7 @@ class CreditHistorySearch extends CreditHistory
public function search($params)
{
$optionsSearch = CreditHistoryRepository::defaultOptionsSearch() ;
$optionsSearch = CreditHistoryRepository::getDefaultOptionsSearch() ;
$query = CreditHistory::find()
->with($optionsSearch['with'])

+ 4
- 4
common/logic/User/User/UserBuilder.php Voir le fichier

@@ -2,13 +2,14 @@

namespace common\logic\User\User;

use common\logic\BaseBuilder;
use common\logic\BaseService;
use common\logic\BuilderInterface;
use common\logic\Producer\Producer\Producer;

class UserBuilder extends BaseService implements BuilderInterface
class UserBuilder extends BaseBuilder implements BuilderInterface
{
public function instanciate(): User
public function instanciateUser(): User
{
$user = new User();

@@ -30,7 +31,7 @@ class UserBuilder extends BaseService implements BuilderInterface
/**
* Met à jour la date de dernière connexion de l'utilisateur.
*/
public function updateLastConnection(User $user)
public function updateUserLastConnection(User $user)
{
$user->date_last_connection = date('Y-m-d H:i:s');
$user->save();
@@ -38,7 +39,6 @@ class UserBuilder extends BaseService implements BuilderInterface

/**
* Generates password hash from password and sets it to the model
*
*/
public function setPassword(User $user, string $password): void
{

+ 28
- 20
common/logic/User/User/UserRepository.php Voir le fichier

@@ -4,9 +4,14 @@ namespace common\logic\User\User;

use common\helpers\GlobalParam;
use common\logic\BaseService;
use common\logic\Order\Order\Order;
use common\logic\PointSale\PointSale\PointSale;
use common\logic\PointSale\UserPointSale\UserPointSale;
use common\logic\Producer\Producer\Producer;
use common\logic\RepositoryInterface;
use common\logic\User\UserGroup\UserGroup;
use common\logic\User\UserProducer\UserProducerRepository;
use common\logic\User\UserUserGroup\UserUserGroup;
use yii\db\Query;

class UserRepository extends BaseService implements RepositoryInterface
@@ -25,7 +30,7 @@ class UserRepository extends BaseService implements RepositoryInterface
*
* @return array
*/
public function defaultOptionsSearch()
public function getDefaultOptionsSearch(): array
{
return [
'with' => [],
@@ -35,12 +40,13 @@ class UserRepository extends BaseService implements RepositoryInterface
];
}

public function getOneById($id)
public function findOneUserById($id)
{
return User::searchOne(['id' => $id]);
}

public function belongsToUserGroup(User $user, int $userGroupId): bool
// belongsToUserGroup
public function isUserbelongsToUserGroup(User $user, UserGroup $userGroup): bool
{
if (!$user->userUserGroup) {
$user->populateRelation('userUserGroup', UserUserGroup::searchAll([
@@ -50,7 +56,7 @@ class UserRepository extends BaseService implements RepositoryInterface

if ($user->userUserGroup) {
foreach ($user->userUserGroup as $userUserGroup) {
if ($userUserGroup->id_user_group == $userGroupId) {
if ($userUserGroup->id_user_group == $userGroup->id) {
return true;
}
}
@@ -59,9 +65,14 @@ class UserRepository extends BaseService implements RepositoryInterface
return false;
}

public static function populateDropdownList()
public function findUsers(): array
{
$usersArray = User::findBy()->all();
return $this->queryUsersBy()->all();
}

public static function populateUserDropdownList()
{
$usersArray =
$usersArrayDropdown = ['' => '--'];

foreach ($usersArray as $user) {
@@ -76,7 +87,7 @@ class UserRepository extends BaseService implements RepositoryInterface
* le point de vente de la dernière commande sinon.
*
*/
public function getFavoritePointSale(User $user): ?PointSale
public function getUserFavoritePointSale(User $user): ?PointSale
{
$arrayUserPointSale = UserPointSale::find()
->innerJoinWith('pointSale', true)
@@ -114,13 +125,8 @@ class UserRepository extends BaseService implements RepositoryInterface
*/
public function getCredit(User $user, Producer $producer): float
{
$userProducer = $this->userProducerRepository->getOne($user, $producer);

if ($userProducer) {
return $userProducer->credit;
}

return 0;
$userProducer = $this->userProducerRepository->findOneUserProducer($user, $producer);
return $userProducer ? $userProducer->credit : 0;
}

/**
@@ -130,7 +136,8 @@ class UserRepository extends BaseService implements RepositoryInterface
* @param array $params
* @return Query
*/
public static function findBy($params = [])
// findBy
public function queryUsersBy(array $params = [])
{
if (!isset($params['id_producer'])) {
$params['id_producer'] = GlobalParam::getCurrentProducerId();
@@ -211,9 +218,9 @@ class UserRepository extends BaseService implements RepositoryInterface

/**
* Finds user by password reset token
*
*/
public function getByPasswordResetToken(string $token)
// getByPasswordResetToken
public function findOneUserByPasswordResetToken(string $token)
{
if (!$this->userSolver->isPasswordResetTokenValid($token)) {
return null;
@@ -226,14 +233,15 @@ class UserRepository extends BaseService implements RepositoryInterface

/**
* Recherche un utilisateur via son adresse email.
*
*/
public static function getOneByEmail(string $email): ?User
// getOneByEmail
public static function findOneUserByEmail(string $email): ?User
{
return User::searchOne(['email' => $email]);
}

public static function getOneByUsername($username): ?User
// getOneByUsername
public static function findOneUserByUsername(string $username): ?User
{
return User::searchOne(['username' => $username]);
}

+ 1
- 1
common/logic/User/User/UserSearch.php Voir le fichier

@@ -59,7 +59,7 @@ class UserSearch extends User

public function search($params = [])
{
$optionsSearch = self::defaultOptionsSearch();
$optionsSearch = self::getDefaultOptionsSearch();

$query = User::find()
->select(

+ 11
- 3
common/logic/User/UserGroup/UserGroupBuilder.php Voir le fichier

@@ -2,15 +2,23 @@

namespace common\logic\User\UserGroup;

use common\logic\BaseService;
use common\logic\BaseBuilder;
use common\logic\BuilderInterface;

class UserGroupBuilder extends BaseService implements BuilderInterface
class UserGroupBuilder extends BaseBuilder implements BuilderInterface
{
public function instanciate(): UserGroup
public function instanciateUserGroup(): UserGroup
{
$userGroup = new UserGroup();

return $userGroup;
}

public function createUserGroup(): UserGroup
{
$userGroup = $this->instanciateUserGroup();
$this->saveCreate($userGroup);

return $userGroup;
}
}

+ 8
- 3
common/logic/User/UserGroup/UserGroupRepository.php Voir le fichier

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

class UserGroupRepository extends BaseService implements RepositoryInterface
{
public function defaultOptionsSearch()
public function getDefaultOptionsSearch()
{
return [
'with' => [],
@@ -18,10 +18,15 @@ class UserGroupRepository extends BaseService implements RepositoryInterface
];
}

public function populateDropdownList(): array
public function findUserGroups()
{
return UserGroup::find()->where('id_producer = ' . GlobalParam::getCurrentProducerId())->all();
}

public function populateUserGroupDropdownList(): array
{
$userGroupsArrayDropdown = ['' => '--'];
$userGroupsArray = UserGroup::find()->where('id_producer = ' . GlobalParam::getCurrentProducerId())->all();
$userGroupsArray = $this->findUserGroups();

foreach ($userGroupsArray as $userGroup) {
$userGroupsArrayDropdown[$userGroup['id']] = $userGroup['name'];

+ 1
- 1
common/logic/User/UserGroup/UserGroupSearch.php Voir le fichier

@@ -55,7 +55,7 @@ class UserGroupSearch extends UserGroup
public function search($params)
{
$optionsSearch = self::defaultOptionsSearch() ;
$optionsSearch = self::getDefaultOptionsSearch() ;
$query = UserGroup::find()
->with($optionsSearch['with'])

+ 25
- 20
common/logic/User/UserProducer/UserProducerBuilder.php Voir le fichier

@@ -3,26 +3,33 @@
namespace common\logic\User\UserProducer;

use common\helpers\MeanPayment;
use common\logic\BaseBuilder;
use common\logic\BaseService;
use common\logic\BuilderInterface;
use common\logic\Order\Order\Order;
use common\logic\Order\Order\OrderRepository;
use common\logic\Producer\Producer\Producer;
use common\logic\Producer\Producer\ProducerRepository;
use common\logic\User\CreditHistory\CreditHistory;
use common\logic\User\CreditHistory\CreditHistorySolver;
use common\logic\User\User\User;

class UserProducerBuilder extends BaseService implements BuilderInterface
class UserProducerBuilder extends BaseBuilder implements BuilderInterface
{
protected CreditHistorySolver $creditHistorySolver;
protected UserProducerRepository $userProducerRepository;
protected OrderRepository $orderRepository;
protected ProducerRepository $producerRepository;

public function __construct()
{
$this->creditHistorySolver = $this->loadService(CreditHistorySolver::class);
$this->userProducerRepository = $this->loadService(UserProducerRepository::class);
$this->orderRepository = $this->loadService(OrderRepository::class);
$this->producerRepository = $this->loadService(ProducerRepository::class);
}

public function instanciate(User $user, Producer $producer, int $bookmark = 1)
public function instanciateUserProducer(User $user, Producer $producer, int $bookmark = 1)
{
$userProducer = new UserProducer();

@@ -35,26 +42,22 @@ class UserProducerBuilder extends BaseService implements BuilderInterface
return $userProducer;
}

public function create(User $user, Producer $producer, int $bookmark = 1): UserProducer
public function createUserProducer(User $user, Producer $producer, int $bookmark = 1): UserProducer
{
$userProducer = $this->instanciate($user, $producer, $bookmark);
$userProducer->save();
$userProducer = $this->instanciateUserProducer($user, $producer, $bookmark);
$this->saveCreate($userProducer);
return $userProducer;
}

public function createIfNotExist(User $user, Producer $producer, int $bookmark = 1): UserProducer
public function createUserProducerIfNotExist(User $user, Producer $producer, int $bookmark = 1): UserProducer
{
$userProducer = $this->userProducerRepository->getOne($user, $producer);
if (!$userProducer) {
$userProducer = $this->create($user, $producer, $bookmark);
}

return $userProducer;
return $this->userProducerRepository->findOneUserProducer($user, $producer)
?? $this->createUserProducer($user, $producer, $bookmark);
}

public function updateCredit(CreditHistory $creditHistory): void
{
$userProducer = $this->userProducerRepository->getOne($creditHistory->getIdUser(), $creditHistory->getIdProducer());
$userProducer = $this->userProducerRepository->findOneUserProducer($creditHistory->user, $creditHistory->producer);

if ($userProducer) {
$oldCredit = $userProducer->getCredit();
@@ -73,14 +76,13 @@ class UserProducerBuilder extends BaseService implements BuilderInterface
$userProducer->setCredit($userProducer->getCredit() - $creditHistory->getAmount());
}

$userProducer->save();
$this->saveUpdate($userProducer);
}

public function initMeanPaymentOrder($creditHistory)
{
// set mean payment
if ($creditHistory->id_order && $creditHistory->id_order > 0) {
$order = Order::searchOne(['id' => (int)$creditHistory->id_order]);
$order = $this->orderRepository->findOneOrderById((int) $creditHistory->id_order);

if ($order) {
$paymentStatus = $order->getPaymentStatus();
@@ -88,7 +90,8 @@ class UserProducerBuilder extends BaseService implements BuilderInterface
|| $paymentStatus == Order::PAYMENT_SURPLUS) {

$order->mean_payment = MeanPayment::CREDIT;
$order->save();

$this->saveUpdate($order);
}
}
}
@@ -125,7 +128,7 @@ class UserProducerBuilder extends BaseService implements BuilderInterface

public function isCreditLimitCrossed($oldCredit, $newCredit)
{
$creditLimitReminder = Producer::getConfig('credit_limit_reminder');
$creditLimitReminder = $this->producerRepository->getConfig('credit_limit_reminder');

return !is_null($creditLimitReminder) &&
$oldCredit > $creditLimitReminder
@@ -134,11 +137,13 @@ class UserProducerBuilder extends BaseService implements BuilderInterface

public function updateActive(User $user, Producer $producer, bool $active): void
{
$userProducer = $this->getUserProducerContainer()->getRepository()->getOne(
$userProducer = $this->createUserProducerIfNotExist(
$user,
$producer
);

$userProducer->active = $active;
$userProducer->save();

$this->saveUpdate($userProducer);
}
}

+ 12
- 0
common/logic/User/UserProducer/UserProducerEventSubscriber.php Voir le fichier

@@ -0,0 +1,12 @@
<?php

namespace common\logic\User\UserProducer;

class UserProducerEventSubscriber
{
public static function onCreateCreditHistory($event)
{
$userProducerManager = new UserProducerManager();
$userProducerManager->updateCredit($event->creditHistory);
}
}

+ 3
- 3
common/logic/User/UserProducer/UserProducerRepository.php Voir le fichier

@@ -9,7 +9,7 @@ use common\logic\User\User\User;

class UserProducerRepository extends BaseService implements RepositoryInterface
{
public function defaultOptionsSearch(): array
public function getDefaultOptionsSearch(): array
{
return [
'with' => [],
@@ -19,7 +19,7 @@ class UserProducerRepository extends BaseService implements RepositoryInterface
];
}

public function getOne(User $user, Producer $producer)
public function findOneUserProducer(User $user, Producer $producer)
{
return UserProducer::searchOne([
'id_user' => $user->id,
@@ -27,7 +27,7 @@ class UserProducerRepository extends BaseService implements RepositoryInterface
]);
}

public function getBy(User $user, $active = 1, $bookmark = 1)
public function findUserProducersByUser(User $user, bool $active = true, bool $bookmark = true)
{
return UserProducer::find()
->with(['producer'])

+ 12
- 2
common/logic/User/UserUserGroup/UserUserGroupBuilder.php Voir le fichier

@@ -2,15 +2,25 @@

namespace common\logic\User\UserGroup;

use common\logic\BaseBuilder;
use common\logic\BaseService;
use common\logic\BuilderInterface;
use common\logic\User\UserUserGroup\UserUserGroup;

class UserUserGroupBuilder extends BaseService implements BuilderInterface
class UserUserGroupBuilder extends BaseBuilder implements BuilderInterface
{
public function instanciate(): UserUserGroup
public function instanciateUserUserGroup(): UserUserGroup
{
$userUserGroup = new UserUserGroup();

return $userUserGroup;
}

public function createUserUserGroup(): UserUserGroup
{
$userUserGroup = $this->instanciateUserUserGroup();
$this->saveCreate($userUserGroup);

return $userUserGroup;
}
}

+ 1
- 1
common/logic/User/UserUserGroup/UserUserGroupRepository.php Voir le fichier

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

class UserUserGroupRepository extends BaseService implements RepositoryInterface
{
public function defaultOptionsSearch(): array
public function getDefaultOptionsSearch(): array
{
return [
'with' => [],

Chargement…
Annuler
Enregistrer