@@ -46,10 +46,10 @@ class BusinessLogic | |||
$this->getProductOrderContainer(), | |||
$this->getProductPriceContainer(), | |||
$this->getProductSubscriptionContainer(), | |||
$this->getUserContainer(), | |||
$this->getSubscriptionContainer(), | |||
$this->getProductContainer(), | |||
$this->getDistributionContainer(), | |||
$this->getUserContainer(), | |||
$this->getProducerContainer(), | |||
$this->getOrderContainer(), | |||
]; |
@@ -6,13 +6,14 @@ use common\helpers\GlobalParam; | |||
use common\helpers\Price; | |||
use common\logic\BaseService; | |||
use common\logic\BuilderInterface; | |||
use common\logic\Config\TaxRate\TaxRate; | |||
use common\logic\Distribution\Distribution\Distribution; | |||
use common\logic\Distribution\Distribution\DistributionRepository; | |||
use common\logic\Document\Document\Document; | |||
use common\logic\Order\OrderStatusHistory\OrderStatusHistoryBuilder; | |||
use common\logic\Order\ProductOrder\ProductOrder; | |||
use common\logic\Order\ProductOrder\ProductOrderBuilder; | |||
use common\logic\PointSale\PointSale\PointSale; | |||
use common\logic\Order\ProductOrder\ProductOrderSolver; | |||
use common\logic\PointSale\PointSale\PointSaleBuilder; | |||
use common\logic\PointSale\PointSale\PointSaleRepository; | |||
use common\logic\PointSale\PointSale\UserPointSaleRepository; | |||
@@ -20,6 +21,8 @@ use common\logic\Producer\Producer\Producer; | |||
use common\logic\Producer\Producer\ProducerRepository; | |||
use common\logic\Subscription\Subscription\Subscription; | |||
use common\logic\Subscription\Subscription\SubscriptionBuilder; | |||
use common\logic\Subscription\Subscription\SubscriptionRepository; | |||
use common\logic\Subscription\Subscription\SubscriptionSolver; | |||
use common\logic\User\CreditHistory\CreditHistory; | |||
use common\logic\User\CreditHistory\CreditHistoryBuilder; | |||
use common\logic\User\CreditHistory\CreditHistoryRepository; | |||
@@ -44,6 +47,9 @@ class OrderBuilder extends BaseService implements BuilderInterface | |||
protected UserProducerRepository $userProducerRepository; | |||
protected UserPointSaleRepository $userPointSaleRepository; | |||
protected PointSaleBuilder $pointSaleBuilder; | |||
protected ProductOrderSolver $productOrderSolver; | |||
protected SubscriptionRepository $subscriptionRepository; | |||
protected SubscriptionSolver $subscriptionSolver; | |||
public function __construct() | |||
{ | |||
@@ -61,6 +67,9 @@ class OrderBuilder extends BaseService implements BuilderInterface | |||
$this->userProducerRepository = $this->loadService(UserProducerRepository::class); | |||
$this->userPointSaleRepository = $this->loadService(UserPointSaleRepository::class); | |||
$this->pointSaleBuilder = $this->loadService(PointSaleBuilder::class); | |||
$this->productOrderSolver = $this->loadService(ProductOrderSolver::class); | |||
$this->subscriptionRepository = $this->loadService(SubscriptionRepository::class); | |||
$this->subscriptionSolver = $this->loadService(SubscriptionSolver::class); | |||
} | |||
public function instanciate(Distribution $distribution): Order | |||
@@ -73,6 +82,19 @@ class OrderBuilder extends BaseService implements BuilderInterface | |||
return $order; | |||
} | |||
public function create(Distribution $distribution): Order | |||
{ | |||
$order = $this->instanciate($distribution); | |||
$this->addUserPointSale($order); | |||
$this->initCommentPointSale($order); | |||
$this->generateReference($order); | |||
$order->save(); | |||
return $order; | |||
} | |||
/** | |||
* Ajoute l'abonnement' pour une date donnée. | |||
*/ | |||
@@ -89,45 +111,138 @@ class OrderBuilder extends BaseService implements BuilderInterface | |||
&& count($subscription->productSubscription) | |||
&& $subscription->pointSale) { | |||
$order = $this->instanciate($distribution); | |||
$order = $this->create($distribution); | |||
$this->updateFromSubscription($order, $subscription); | |||
} | |||
} | |||
$this->initFromSubscription($order, $subscription); | |||
/** | |||
* Ajoute les commandes pour une date donnée à partir des abonnements. | |||
*/ | |||
// addAll | |||
public function createAllFromSubscriptions(string $date, $force = false): void | |||
{ | |||
$distribution = $this->distributionRepository->getOneByDate(date('Y-m-d', strtotime($date))); | |||
if ($distribution) { | |||
$orderArray = $this->orderRepository->getByDistribution($distribution); | |||
$subscriptionArray = $this->subscriptionRepository->getByDate($date); | |||
foreach ($subscriptionArray as $subscription) { | |||
if (!$this->subscriptionSolver->hasOrderAlreadyExist($subscription, $orderArray)) { | |||
$this->createFromSubscription($subscription, $date, $force); | |||
} | |||
} | |||
} | |||
} | |||
$this->addUserPointSale($order); | |||
$this->initCommentPointSale($order); | |||
$this->generateReference($order); | |||
public function updateIncomingDistributions(Subscription $subscription, $update = false): void | |||
{ | |||
$matchedDistributionsArray = $this->distributionRepository->searchMatchedIncomingDistributions($subscription); | |||
$order->save(); | |||
if ($update) { | |||
$this->deleteOrdersIncomingDistributions($subscription); | |||
} | |||
$productsAdd = false; | |||
$user = $subscription->user ?? null; | |||
foreach ($subscription->productSubscription as $productSubscription) { | |||
$productOrder = new ProductOrder; | |||
$productOrder->id_order = $order->id; | |||
$productOrder->id_product = $productSubscription->product->id; | |||
$productOrder->quantity = $productSubscription->quantity; | |||
$productOrder->price = $productSubscription->product->getPrice([ | |||
'user' => $user, | |||
'point_sale' => $pointSale, | |||
'quantity' => $productSubscription->quantity | |||
]); | |||
$productOrder->unit = $productSubscription->product->unit; | |||
$productOrder->step = $productSubscription->product->step; | |||
$productOrder->id_tax_rate = $productSubscription->product->taxRate->id; | |||
$productOrder->save(); | |||
$productsAdd = true; | |||
if (count($matchedDistributionsArray)) { | |||
foreach ($matchedDistributionsArray as $distribution) { | |||
$this->createFromSubscription($subscription, $distribution->date); | |||
} | |||
} | |||
} | |||
if(!$productsAdd) { | |||
$order->delete(); | |||
public function deleteOrdersIncomingDistributions(Subscription $subscription, $deleteAfterDateEnd = false) | |||
{ | |||
$dateStart = $subscription->date_begin; | |||
$comparatorDateStart = '>='; | |||
if ($deleteAfterDateEnd) { | |||
$dateStart = $subscription->date_end; | |||
$comparatorDateStart = '>'; | |||
} | |||
$params = [ | |||
':id_producer' => GlobalParam::getCurrentProducerId(), | |||
':date_today' => date('Y-m-d'), | |||
':date_start' => $dateStart, | |||
':id_subscription' => $subscription->id | |||
]; | |||
$orderDeadline = $this->producerRepository->getConfig('order_deadline'); | |||
$hour = date('G'); | |||
if ($hour >= $orderDeadline) { | |||
$conditionDistributionDate = 'distribution.date > :date_today'; | |||
} else { | |||
$conditionDistributionDate = 'distribution.date >= :date_today'; | |||
} | |||
$orders = Order::find() | |||
->joinWith('distribution') | |||
->where('distribution.id_producer = :id_producer') | |||
->andWhere($conditionDistributionDate) | |||
->andWhere('distribution.date ' . $comparatorDateStart . ' :date_start') | |||
->andWhere('order.id_subscription = :id_subscription'); | |||
$orders->params($params); | |||
$ordersArray = $orders->all(); | |||
$configCredit = $this->producerRepository->getConfig('credit'); | |||
$countOrdersDeleted = 0; | |||
if ($ordersArray && count($ordersArray)) { | |||
foreach ($ordersArray as $order) { | |||
$theOrder = $this->orderRepository->getOneById($order->id); | |||
// remboursement de la commande | |||
if ($theOrder->id_user && $theOrder->getAmount(Order::AMOUNT_PAID) && $configCredit) { | |||
$this->creditHistoryBuilder->create( | |||
CreditHistory::TYPE_REFUND, | |||
$theOrder->getAmount(Order::AMOUNT_PAID), | |||
$theOrder->distribution->producer, | |||
$theOrder->user, | |||
GlobalParam::getCurrentUser() | |||
); | |||
} | |||
$this->delete($order, true); | |||
$countOrdersDeleted ++; | |||
} | |||
} | |||
return $countOrdersDeleted; | |||
} | |||
public function addProductOrdersFromSubscription(Order $order, Subscription $subscription): void | |||
{ | |||
$productsAdd = false; | |||
$user = $subscription->user ?? null; | |||
foreach ($subscription->productSubscription as $productSubscription) { | |||
$this->productOrderBuilder->create( | |||
$order, | |||
$productSubscription->product, | |||
$productSubscription->quantity, | |||
$productSubscription->product->getPrice([ | |||
'user' => $user, | |||
'point_sale' => $subscription->pointSale, | |||
'quantity' => $productSubscription->quantity | |||
]) | |||
); | |||
$productsAdd = true; | |||
} | |||
if(!$productsAdd) { | |||
$order->delete(); | |||
} | |||
} | |||
public function initFromSubscription(Order $order, Subscription $subscription): void | |||
public function updateFromSubscription(Order $order, Subscription $subscription): void | |||
{ | |||
$this->initBaseFromSubscription($order, $subscription); | |||
$this->initAutoPaymentFromSubscription($order, $subscription); | |||
$this->addProductOrdersFromSubscription($order, $subscription); | |||
} | |||
public function initBaseFromSubscription(Order $order, Subscription $subscription): void | |||
@@ -242,23 +357,28 @@ class OrderBuilder extends BaseService implements BuilderInterface | |||
} | |||
} | |||
public function addAmount(Order $order, $typeTotal, $productOrder, $taxCalculationMethod) | |||
public function addAmount(Order $order, string $typeTotal, ProductOrder $productOrder, string $taxCalculationMethod): void | |||
{ | |||
$fieldNameAmount = $this->getFieldNameAmount($typeTotal); | |||
$fieldNameAmountWithTax = $this->getFieldNameAmount($typeTotal, 'with_tax'); | |||
$price = $productOrder->getPriceByTypeTotal($typeTotal); | |||
$fieldNameAmount = $this->orderSolver->getFieldNameAmount($typeTotal); | |||
$fieldNameAmountWithTax = $this->orderSolver->getFieldNameAmount($typeTotal, 'with_tax'); | |||
$price = $this->productOrderSolver->getPriceByTypeTotal($productOrder, $typeTotal); | |||
$order->$fieldNameAmount += $price * $productOrder->quantity; | |||
$order->$fieldNameAmountWithTax += Price::getPriceWithTax( | |||
$price, | |||
$productOrder->taxRate->value, | |||
$taxCalculationMethod | |||
) * $productOrder->quantity; | |||
$order->addVat($order, $typeTotal, $price * $productOrder->quantity, $productOrder->taxRate, $taxCalculationMethod); | |||
$this->addVat($order, $typeTotal, $price * $productOrder->quantity, $productOrder->taxRate, $taxCalculationMethod); | |||
} | |||
public function addVat($typeTotal, $priceTotalWithoutTax, $taxRate, $taxCalculationMethod) | |||
public function addVat( | |||
Order $order, | |||
string $typeTotal, | |||
float $priceTotalWithoutTax, | |||
TaxRate $taxRate, | |||
string $taxCalculationMethod): void | |||
{ | |||
$fieldName = $this->getFieldNameAmount($typeTotal, 'vat'); | |||
$fieldName = $this->orderSolver->getFieldNameAmount($typeTotal, 'vat'); | |||
if (!isset($this->$fieldName[$taxRate->id])) { | |||
$this->$fieldName[$taxRate->id] = 0; |
@@ -42,6 +42,7 @@ use common\helpers\GlobalParam; | |||
use common\helpers\Price; | |||
use common\components\ActiveRecordCommon; | |||
use common\logic\Config\TaxRate\TaxRate; | |||
use common\logic\Order\Order\Order; | |||
use common\logic\Product\Product\Product; | |||
/** | |||
@@ -66,11 +67,31 @@ class ProductOrder extends ActiveRecordCommon | |||
return $this->hasOne(Product::class, ['id' => 'id_product']); | |||
} | |||
public function populateProduct(Product $product) | |||
{ | |||
$this->populateFieldObject('product', 'id_product', $product); | |||
} | |||
public function getOrder() | |||
{ | |||
return $this->hasOne(Order::class, ['id' => 'id_order']); | |||
} | |||
public function populateOrder(Order $order) | |||
{ | |||
$this->populateFieldObject('order', 'id_order', $order); | |||
} | |||
public function getTaxRate() | |||
{ | |||
return $this->hasOne(TaxRate::class, ['id' => 'id_tax_rate']); | |||
} | |||
public function populateTaxRate(TaxRate $taxRate) | |||
{ | |||
$this->populateFieldObject('taxRate', 'id_tax_rate', $taxRate); | |||
} | |||
/** | |||
* @inheritdoc | |||
*/ |
@@ -4,8 +4,10 @@ namespace common\logic\Order\ProductOrder; | |||
use common\logic\BaseService; | |||
use common\logic\BuilderInterface; | |||
use common\logic\Config\TaxRate\TaxRate; | |||
use common\logic\Order\Order\Order; | |||
use common\logic\PointSale\PointSale\PointSale; | |||
use common\logic\Product\Product\Product; | |||
use common\logic\Product\Product\ProductSolver; | |||
use common\logic\User\User\User; | |||
use common\logic\User\UserProducer\UserProducer; | |||
@@ -19,10 +21,25 @@ class ProductOrderBuilder extends BaseService implements BuilderInterface | |||
$this->productSolver = $this->loadService(ProductSolver::class); | |||
} | |||
public function instanciate(): ProductOrder | |||
public function instanciate(Order $order, Product $product, float $quantity, float $price): ProductOrder | |||
{ | |||
$productOrder = new ProductOrder(); | |||
$productOrder->populateOrder($order); | |||
$productOrder->populateProduct($product); | |||
$productOrder->populateTaxRate($product->taxRate); | |||
$productOrder->quantity = $quantity; | |||
$productOrder->price = $price; | |||
$productOrder->unit = $product->unit; | |||
$productOrder->step = $product->step; | |||
return $productOrder; | |||
} | |||
public function create(Order $order, Product $product, float $quantity, float $price): ProductOrder | |||
{ | |||
$productOrder = $this->instanciate($order, $product, $quantity, $price); | |||
$productOrder->save(); | |||
return $productOrder; | |||
} | |||
@@ -114,10 +114,7 @@ class Subscription extends ActiveRecordCommon | |||
public function getProducer() | |||
{ | |||
return $this->hasOne( | |||
Producer::class, | |||
['id' => 'id_producer'] | |||
); | |||
return $this->hasOne(Producer::class, ['id' => 'id_producer']); | |||
} | |||
public function getPointSale() | |||
@@ -130,234 +127,4 @@ class Subscription extends ActiveRecordCommon | |||
return $this->hasMany(ProductSubscription::class, ['id_subscription' => 'id']) | |||
->with('product'); | |||
} | |||
/** | |||
* Ajoute les commandes pour une date donnée à partir des abonnements. | |||
* | |||
* @param string $date | |||
* @param boolean $force | |||
*/ | |||
public static function addAll($date, $force = false) | |||
{ | |||
$distribution = DistributionModel::searchOne([ | |||
'date' => date('Y-m-d', strtotime($date)), | |||
'id_producer' => GlobalParam::getCurrentProducerId(), | |||
]); | |||
if ($distribution) { | |||
$arrayOrdersDistribution = Order::searchAll([ | |||
Order::tableName() . '.id_distribution' => $distribution->id | |||
]); | |||
$arraySubscriptions = self::searchByDate($date); | |||
foreach ($arraySubscriptions as $subscription) { | |||
if (!$subscription->hasOrderAlreadyExist($arrayOrdersDistribution)) { | |||
$subscription->add($date, $force); | |||
} | |||
} | |||
} | |||
} | |||
/** | |||
* Informe s'il existe une commande correspond à l'abonnement courant. | |||
* | |||
* @param array $arrayOrders | |||
* @return boolean | |||
*/ | |||
public function hasOrderAlreadyExist($arrayOrders) | |||
{ | |||
if (is_array($arrayOrders) && count($arrayOrders) > 0) { | |||
foreach ($arrayOrders as $order) { | |||
if ((($order->id_user > 0 && $order->id_user == $this->id_user) || | |||
(!$order->id_user && $order->username == $this->username)) && | |||
$order->id_point_sale == $this->id_point_sale) { | |||
return true; | |||
} | |||
} | |||
} | |||
return false; | |||
} | |||
/** | |||
* Retourne les abonnements pour une date donnée. | |||
* | |||
* @param string $date | |||
* @return array | |||
*/ | |||
public static function searchByDate($date) | |||
{ | |||
$date = date('Y-m-d', strtotime($date)); | |||
$subscriptions = Subscription::searchAll(); | |||
$arrSubscriptions = []; | |||
foreach ($subscriptions as $s) { | |||
if ($date >= $s->date_begin && | |||
(!$s->date_end || $date <= $s->date_end) && | |||
$s->matchWith($date)) { | |||
$arrSubscriptions[] = $s; | |||
} | |||
} | |||
return $arrSubscriptions; | |||
} | |||
/** | |||
* Valide le fait qu'un abonnement est bien compatible avec une date donnée. | |||
* | |||
* @param string $date | |||
* @return boolean | |||
*/ | |||
public function matchWith($date) | |||
{ | |||
$arrayDays = [ | |||
1 => 'monday', | |||
2 => 'tuesday', | |||
3 => 'wednesday', | |||
4 => 'thursday', | |||
5 => 'friday', | |||
6 => 'saturday', | |||
7 => 'sunday' | |||
]; | |||
$nbDays = (strtotime($date) - strtotime($this->date_begin)) / (24 * 60 * 60); | |||
if (round($nbDays) % ($this->week_frequency * 7) < 7) { | |||
$numDay = date('N', strtotime($date)); | |||
$day = $arrayDays[$numDay]; | |||
if ($this->$day) { | |||
return true; | |||
} | |||
} | |||
return false; | |||
} | |||
/** | |||
* Recherche les distributions futures où l'abonnement peut s'appliquer. | |||
* | |||
* @return array | |||
*/ | |||
public function searchMatchedIncomingDistributions() | |||
{ | |||
$params = [ | |||
':date_earliest_order' => date('Y-m-d'), | |||
':date_begin' => date('Y-m-d', strtotime($this->date_begin)), | |||
':id_producer' => GlobalParam::getCurrentProducerId() | |||
]; | |||
$incomingDistributions = DistributionModel::find() | |||
->where('id_producer = :id_producer') | |||
->andWhere('date >= :date_begin') | |||
->andWhere('date > :date_earliest_order'); | |||
if ($this->date_end) { | |||
$incomingDistributions->andWhere('date <= :date_end'); | |||
$params[':date_end'] = date('Y-m-d', strtotime($this->date_end)); | |||
} | |||
$incomingDistributions->orderBy('date ASC'); | |||
$incomingDistributions->params($params); | |||
$incomingDistributionsArray = $incomingDistributions->all(); | |||
DistributionModel::filterDistributionsByDateDelay($incomingDistributionsArray); | |||
$matchedIncomingDistributionsArray = []; | |||
foreach ($incomingDistributionsArray as $incomingDistribution) { | |||
if ($this->matchWith($incomingDistribution->date)) { | |||
$matchedIncomingDistributionsArray[] = $incomingDistribution; | |||
} | |||
} | |||
return $matchedIncomingDistributionsArray; | |||
} | |||
public function deleteOrdersIncomingDistributions($deleteAfterDateEnd = false) | |||
{ | |||
$dateStart = $this->date_begin; | |||
$comparatorDateStart = '>='; | |||
if ($deleteAfterDateEnd) { | |||
$dateStart = $this->date_end; | |||
$comparatorDateStart = '>'; | |||
} | |||
$params = [ | |||
':id_producer' => GlobalParam::getCurrentProducerId(), | |||
':date_today' => date('Y-m-d'), | |||
':date_start' => $dateStart, | |||
':id_subscription' => $this->id | |||
]; | |||
$orderDeadline = Producer::getConfig('order_deadline'); | |||
$hour = date('G'); | |||
if ($hour >= $orderDeadline) { | |||
$conditionDistributionDate = 'distribution.date > :date_today'; | |||
} else { | |||
$conditionDistributionDate = 'distribution.date >= :date_today'; | |||
} | |||
$orders = Order::find() | |||
->joinWith('distribution') | |||
->where('distribution.id_producer = :id_producer') | |||
->andWhere($conditionDistributionDate) | |||
->andWhere('distribution.date ' . $comparatorDateStart . ' :date_start') | |||
->andWhere('order.id_subscription = :id_subscription'); | |||
$orders->params($params); | |||
$ordersArray = $orders->all(); | |||
$configCredit = Producer::getConfig('credit'); | |||
$countOrdersDeleted = 0; | |||
if ($ordersArray && count($ordersArray)) { | |||
foreach ($ordersArray as $order) { | |||
$theOrder = Order::searchOne(['id' => $order->id]); | |||
// remboursement de la commande | |||
if ($theOrder->id_user && $theOrder->getAmount(Order::AMOUNT_PAID) && $configCredit) { | |||
$theOrder->saveCreditHistory( | |||
CreditHistory::TYPE_REFUND, | |||
$theOrder->getAmount(Order::AMOUNT_PAID), | |||
$theOrder->distribution->id_producer, | |||
$theOrder->id_user, | |||
User::getCurrentId() | |||
); | |||
} | |||
$order->delete(true); | |||
$countOrdersDeleted++; | |||
} | |||
} | |||
return $countOrdersDeleted; | |||
} | |||
public function updateIncomingDistributions($update = false) | |||
{ | |||
$matchedDistributionsArray = $this->searchMatchedIncomingDistributions(); | |||
if ($update) { | |||
$this->deleteOrdersIncomingDistributions(); | |||
} | |||
if (count($matchedDistributionsArray)) { | |||
foreach ($matchedDistributionsArray as $distribution) { | |||
$this->add($distribution->date); | |||
} | |||
} | |||
} | |||
public function getUsername() | |||
{ | |||
if ($this->user) { | |||
return $this->user->getUsername(); | |||
} | |||
return $this->username; | |||
} | |||
} |
@@ -2,14 +2,20 @@ | |||
namespace common\logic\Subscription\Subscription; | |||
use common\helpers\GlobalParam; | |||
use common\logic\BaseService; | |||
use common\logic\Distribution\Distribution\Distribution; | |||
use common\logic\RepositoryInterface; | |||
class SubscriptionRepository extends BaseService implements RepositoryInterface | |||
{ | |||
/** | |||
* Retourne les options de base nécessaires à la fonction de recherche. | |||
*/ | |||
protected SubscriptionSolver $subscriptionSolver; | |||
public function __construct() | |||
{ | |||
$this->subscriptionSolver = $this->loadService(SubscriptionSolver::class); | |||
} | |||
public function defaultOptionsSearch(): array | |||
{ | |||
return [ | |||
@@ -19,4 +25,67 @@ class SubscriptionRepository extends BaseService implements RepositoryInterface | |||
'attribute_id_producer' => 'subscription.id_producer' | |||
]; | |||
} | |||
public function get() | |||
{ | |||
return Subscription::searchAll(); | |||
} | |||
/** | |||
* Retourne les abonnements pour une date donnée. | |||
*/ | |||
// searchByDate | |||
public function getByDate(string $date) | |||
{ | |||
$date = date('Y-m-d', strtotime($date)); | |||
$subscriptionArray = $this->get(); | |||
$subscriptionReturnArray = []; | |||
foreach ($subscriptionArray as $subscription) { | |||
if ($date >= $subscription->date_begin && | |||
(!$subscription->date_end || $date <= $subscription->date_end) && | |||
$subscription->matchWith($date)) { | |||
$subscriptionReturnArray[] = $subscription; | |||
} | |||
} | |||
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; | |||
} | |||
} |
@@ -4,8 +4,68 @@ namespace common\logic\Subscription\Subscription; | |||
use common\logic\BaseService; | |||
use common\logic\SolverInterface; | |||
use common\logic\User\User\UserSolver; | |||
class SubscriptionSolver extends BaseService implements SolverInterface | |||
{ | |||
protected UserSolver $userSolver; | |||
public function __construct() | |||
{ | |||
$this->userSolver = $this->loadService(UserSolver::class); | |||
} | |||
/** | |||
* Informe s'il existe une commande correspond à l'abonnement courant. | |||
*/ | |||
public function hasOrderAlreadyExist(Subscription $subscription, bool $orderArray): bool | |||
{ | |||
if (is_array($orderArray) && count($orderArray) > 0) { | |||
foreach ($orderArray as $order) { | |||
if ((($order->id_user > 0 && $order->id_user == $subscription->id_user) || | |||
(!$order->id_user && $order->username == $subscription->username)) && | |||
$order->id_point_sale == $subscription->id_point_sale) { | |||
return true; | |||
} | |||
} | |||
} | |||
return false; | |||
} | |||
public function getUsername(Subscription $subscription): string | |||
{ | |||
if ($subscription->user) { | |||
return $this->userSolver->getUsername($subscription->user); | |||
} | |||
return $subscription->username; | |||
} | |||
/** | |||
* Valide le fait qu'un abonnement est bien compatible avec une date donnée. | |||
*/ | |||
public function matchWith(Subscription $subscription, string $date): bool | |||
{ | |||
$arrayDays = [ | |||
1 => 'monday', | |||
2 => 'tuesday', | |||
3 => 'wednesday', | |||
4 => 'thursday', | |||
5 => 'friday', | |||
6 => 'saturday', | |||
7 => 'sunday' | |||
]; | |||
$nbDays = (strtotime($date) - strtotime($subscription->date_begin)) / (24 * 60 * 60); | |||
if (round($nbDays) % ($subscription->week_frequency * 7) < 7) { | |||
$numDay = date('N', strtotime($date)); | |||
$day = $arrayDays[$numDay]; | |||
if ($subscription->$day) { | |||
return true; | |||
} | |||
} | |||
return false; | |||
} | |||
} |
@@ -1,45 +1,45 @@ | |||
<?php | |||
/** | |||
Copyright distrib (2018) | |||
contact@opendistrib.net | |||
Ce logiciel est un programme informatique servant à aider les producteurs | |||
à distribuer leur production en circuits courts. | |||
Ce logiciel est régi par la licence CeCILL soumise au droit français et | |||
respectant les principes de diffusion des logiciels libres. Vous pouvez | |||
utiliser, modifier et/ou redistribuer ce programme sous les conditions | |||
de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA | |||
sur le site "http://www.cecill.info". | |||
En contrepartie de l'accessibilité au code source et des droits de copie, | |||
de modification et de redistribution accordés par cette licence, il n'est | |||
offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons, | |||
seule une responsabilité restreinte pèse sur l'auteur du programme, le | |||
titulaire des droits patrimoniaux et les concédants successifs. | |||
A cet égard l'attention de l'utilisateur est attirée sur les risques | |||
associés au chargement, à l'utilisation, à la modification et/ou au | |||
développement et à la reproduction du logiciel par l'utilisateur étant | |||
donné sa spécificité de logiciel libre, qui peut le rendre complexe à | |||
manipuler et qui le réserve donc à des développeurs et des professionnels | |||
avertis possédant des connaissances informatiques approfondies. Les | |||
utilisateurs sont donc invités à charger et tester l'adéquation du | |||
logiciel à leurs besoins dans des conditions permettant d'assurer la | |||
sécurité de leurs systèmes et ou de leurs données et, plus généralement, | |||
à l'utiliser et l'exploiter dans les mêmes conditions de sécurité. | |||
Le fait que vous puissiez accéder à cet en-tête signifie que vous avez | |||
pris connaissance de la licence CeCILL, et que vous en avez accepté les | |||
termes. | |||
*/ | |||
/** | |||
* Copyright distrib (2018) | |||
* | |||
* contact@opendistrib.net | |||
* | |||
* Ce logiciel est un programme informatique servant à aider les producteurs | |||
* à distribuer leur production en circuits courts. | |||
* | |||
* Ce logiciel est régi par la licence CeCILL soumise au droit français et | |||
* respectant les principes de diffusion des logiciels libres. Vous pouvez | |||
* utiliser, modifier et/ou redistribuer ce programme sous les conditions | |||
* de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA | |||
* sur le site "http://www.cecill.info". | |||
* | |||
* En contrepartie de l'accessibilité au code source et des droits de copie, | |||
* de modification et de redistribution accordés par cette licence, il n'est | |||
* offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons, | |||
* seule une responsabilité restreinte pèse sur l'auteur du programme, le | |||
* titulaire des droits patrimoniaux et les concédants successifs. | |||
* | |||
* A cet égard l'attention de l'utilisateur est attirée sur les risques | |||
* associés au chargement, à l'utilisation, à la modification et/ou au | |||
* développement et à la reproduction du logiciel par l'utilisateur étant | |||
* donné sa spécificité de logiciel libre, qui peut le rendre complexe à | |||
* manipuler et qui le réserve donc à des développeurs et des professionnels | |||
* avertis possédant des connaissances informatiques approfondies. Les | |||
* utilisateurs sont donc invités à charger et tester l'adéquation du | |||
* logiciel à leurs besoins dans des conditions permettant d'assurer la | |||
* sécurité de leurs systèmes et ou de leurs données et, plus généralement, | |||
* à l'utiliser et l'exploiter dans les mêmes conditions de sécurité. | |||
* | |||
* Le fait que vous puissiez accéder à cet en-tête signifie que vous avez | |||
* pris connaissance de la licence CeCILL, et que vous en avez accepté les | |||
* termes. | |||
*/ | |||
namespace common\logic\User\UserGroup; | |||
use common\helpers\GlobalParam; | |||
use common\components\ActiveRecordCommon ; | |||
use common\components\ActiveRecordCommon; | |||
/** | |||
* This is the model class for table "user_group". | |||
@@ -51,7 +51,7 @@ class UserGroup extends ActiveRecordCommon | |||
/** | |||
* @inheritdoc | |||
*/ | |||
public static function tableName() | |||
public static function tableName() | |||
{ | |||
return 'user_group'; | |||
} | |||
@@ -59,7 +59,7 @@ class UserGroup extends ActiveRecordCommon | |||
/** | |||
* @inheritdoc | |||
*/ | |||
public function rules() | |||
public function rules() | |||
{ | |||
return [ | |||
[['name', 'id_producer'], 'required'], | |||
@@ -71,37 +71,10 @@ class UserGroup extends ActiveRecordCommon | |||
/** | |||
* @inheritdoc | |||
*/ | |||
public function attributeLabels() | |||
public function attributeLabels() | |||
{ | |||
return [ | |||
'name' => 'Nom', | |||
]; | |||
} | |||
/** | |||
* Retourne les options de base nécessaires à la fonction de recherche. | |||
* | |||
* @return array | |||
*/ | |||
public static function defaultOptionsSearch() { | |||
return [ | |||
'with' => [], | |||
'join_with' => [], | |||
'orderby' => '', | |||
'attribute_id_producer' => '' | |||
] ; | |||
} | |||
public static function populateDropdownList() | |||
{ | |||
$userGroupsArrayDropdown = ['' => '--'] ; | |||
$userGroupsArray = UserGroup::find()->where('id_producer = ' . GlobalParam::getCurrentProducerId())->all() ; | |||
foreach($userGroupsArray as $userGroup) { | |||
$userGroupsArrayDropdown[$userGroup['id']] = $userGroup['name'] ; | |||
} | |||
return $userGroupsArrayDropdown ; | |||
} | |||
} |
@@ -2,10 +2,31 @@ | |||
namespace common\logic\User\UserGroup; | |||
use common\helpers\GlobalParam; | |||
use common\logic\BaseService; | |||
use common\logic\RepositoryInterface; | |||
class UserGroupRepository extends BaseService implements RepositoryInterface | |||
{ | |||
public function defaultOptionsSearch() | |||
{ | |||
return [ | |||
'with' => [], | |||
'join_with' => [], | |||
'orderby' => '', | |||
'attribute_id_producer' => '' | |||
]; | |||
} | |||
public function populateDropdownList(): array | |||
{ | |||
$userGroupsArrayDropdown = ['' => '--']; | |||
$userGroupsArray = UserGroup::find()->where('id_producer = ' . GlobalParam::getCurrentProducerId())->all(); | |||
foreach ($userGroupsArray as $userGroup) { | |||
$userGroupsArrayDropdown[$userGroup['id']] = $userGroup['name']; | |||
} | |||
return $userGroupsArrayDropdown; | |||
} | |||
} |
@@ -38,6 +38,7 @@ termes. | |||
namespace common\logic\User\UserGroup; | |||
use common\logic\PointSale\PointSale\PointSale; | |||
use Yii; | |||
use common\components\ActiveRecordCommon ; | |||
@@ -47,7 +48,6 @@ use common\components\ActiveRecordCommon ; | |||
*/ | |||
class UserUserGroup extends ActiveRecordCommon | |||
{ | |||
/** | |||
* @inheritdoc | |||
*/ | |||
@@ -80,21 +80,6 @@ class UserUserGroup extends ActiveRecordCommon | |||
public function getPointSale() | |||
{ | |||
return $this->hasOne(PointSale::className(), ['id' => 'id_point_sale']); | |||
return $this->hasOne(PointSale::class, ['id' => 'id_point_sale']); | |||
} | |||
/** | |||
* Retourne les options de base nécessaires à la fonction de recherche. | |||
* | |||
* @return array | |||
*/ | |||
public static function defaultOptionsSearch() { | |||
return [ | |||
'with' => [], | |||
'join_with' => [], | |||
'orderby' => '', | |||
'attribute_id_producer' => '' | |||
] ; | |||
} | |||
} |
@@ -7,5 +7,13 @@ use common\logic\RepositoryInterface; | |||
class UserUserGroupRepository extends BaseService implements RepositoryInterface | |||
{ | |||
public function defaultOptionsSearch(): array | |||
{ | |||
return [ | |||
'with' => [], | |||
'join_with' => [], | |||
'orderby' => '', | |||
'attribute_id_producer' => '' | |||
] ; | |||
} | |||
} |