$this->getProductOrderContainer(), | $this->getProductOrderContainer(), | ||||
$this->getProductPriceContainer(), | $this->getProductPriceContainer(), | ||||
$this->getProductSubscriptionContainer(), | $this->getProductSubscriptionContainer(), | ||||
$this->getUserContainer(), | |||||
$this->getSubscriptionContainer(), | $this->getSubscriptionContainer(), | ||||
$this->getProductContainer(), | $this->getProductContainer(), | ||||
$this->getDistributionContainer(), | $this->getDistributionContainer(), | ||||
$this->getUserContainer(), | |||||
$this->getProducerContainer(), | $this->getProducerContainer(), | ||||
$this->getOrderContainer(), | $this->getOrderContainer(), | ||||
]; | ]; |
use common\helpers\Price; | use common\helpers\Price; | ||||
use common\logic\BaseService; | use common\logic\BaseService; | ||||
use common\logic\BuilderInterface; | use common\logic\BuilderInterface; | ||||
use common\logic\Config\TaxRate\TaxRate; | |||||
use common\logic\Distribution\Distribution\Distribution; | use common\logic\Distribution\Distribution\Distribution; | ||||
use common\logic\Distribution\Distribution\DistributionRepository; | use common\logic\Distribution\Distribution\DistributionRepository; | ||||
use common\logic\Document\Document\Document; | use common\logic\Document\Document\Document; | ||||
use common\logic\Order\OrderStatusHistory\OrderStatusHistoryBuilder; | use common\logic\Order\OrderStatusHistory\OrderStatusHistoryBuilder; | ||||
use common\logic\Order\ProductOrder\ProductOrder; | use common\logic\Order\ProductOrder\ProductOrder; | ||||
use common\logic\Order\ProductOrder\ProductOrderBuilder; | 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\PointSaleBuilder; | ||||
use common\logic\PointSale\PointSale\PointSaleRepository; | use common\logic\PointSale\PointSale\PointSaleRepository; | ||||
use common\logic\PointSale\PointSale\UserPointSaleRepository; | use common\logic\PointSale\PointSale\UserPointSaleRepository; | ||||
use common\logic\Producer\Producer\ProducerRepository; | use common\logic\Producer\Producer\ProducerRepository; | ||||
use common\logic\Subscription\Subscription\Subscription; | use common\logic\Subscription\Subscription\Subscription; | ||||
use common\logic\Subscription\Subscription\SubscriptionBuilder; | 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\CreditHistory; | ||||
use common\logic\User\CreditHistory\CreditHistoryBuilder; | use common\logic\User\CreditHistory\CreditHistoryBuilder; | ||||
use common\logic\User\CreditHistory\CreditHistoryRepository; | use common\logic\User\CreditHistory\CreditHistoryRepository; | ||||
protected UserProducerRepository $userProducerRepository; | protected UserProducerRepository $userProducerRepository; | ||||
protected UserPointSaleRepository $userPointSaleRepository; | protected UserPointSaleRepository $userPointSaleRepository; | ||||
protected PointSaleBuilder $pointSaleBuilder; | protected PointSaleBuilder $pointSaleBuilder; | ||||
protected ProductOrderSolver $productOrderSolver; | |||||
protected SubscriptionRepository $subscriptionRepository; | |||||
protected SubscriptionSolver $subscriptionSolver; | |||||
public function __construct() | public function __construct() | ||||
{ | { | ||||
$this->userProducerRepository = $this->loadService(UserProducerRepository::class); | $this->userProducerRepository = $this->loadService(UserProducerRepository::class); | ||||
$this->userPointSaleRepository = $this->loadService(UserPointSaleRepository::class); | $this->userPointSaleRepository = $this->loadService(UserPointSaleRepository::class); | ||||
$this->pointSaleBuilder = $this->loadService(PointSaleBuilder::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 | public function instanciate(Distribution $distribution): Order | ||||
return $order; | 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. | * Ajoute l'abonnement' pour une date donnée. | ||||
*/ | */ | ||||
&& count($subscription->productSubscription) | && count($subscription->productSubscription) | ||||
&& $subscription->pointSale) { | && $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->initBaseFromSubscription($order, $subscription); | ||||
$this->initAutoPaymentFromSubscription($order, $subscription); | $this->initAutoPaymentFromSubscription($order, $subscription); | ||||
$this->addProductOrdersFromSubscription($order, $subscription); | |||||
} | } | ||||
public function initBaseFromSubscription(Order $order, Subscription $subscription): void | public function initBaseFromSubscription(Order $order, Subscription $subscription): void | ||||
} | } | ||||
} | } | ||||
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->$fieldNameAmount += $price * $productOrder->quantity; | ||||
$order->$fieldNameAmountWithTax += Price::getPriceWithTax( | $order->$fieldNameAmountWithTax += Price::getPriceWithTax( | ||||
$price, | $price, | ||||
$productOrder->taxRate->value, | $productOrder->taxRate->value, | ||||
$taxCalculationMethod | $taxCalculationMethod | ||||
) * $productOrder->quantity; | ) * $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])) { | if (!isset($this->$fieldName[$taxRate->id])) { | ||||
$this->$fieldName[$taxRate->id] = 0; | $this->$fieldName[$taxRate->id] = 0; |
use common\helpers\Price; | use common\helpers\Price; | ||||
use common\components\ActiveRecordCommon; | use common\components\ActiveRecordCommon; | ||||
use common\logic\Config\TaxRate\TaxRate; | use common\logic\Config\TaxRate\TaxRate; | ||||
use common\logic\Order\Order\Order; | |||||
use common\logic\Product\Product\Product; | use common\logic\Product\Product\Product; | ||||
/** | /** | ||||
return $this->hasOne(Product::class, ['id' => 'id_product']); | 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() | public function getTaxRate() | ||||
{ | { | ||||
return $this->hasOne(TaxRate::class, ['id' => 'id_tax_rate']); | return $this->hasOne(TaxRate::class, ['id' => 'id_tax_rate']); | ||||
} | } | ||||
public function populateTaxRate(TaxRate $taxRate) | |||||
{ | |||||
$this->populateFieldObject('taxRate', 'id_tax_rate', $taxRate); | |||||
} | |||||
/** | /** | ||||
* @inheritdoc | * @inheritdoc | ||||
*/ | */ |
use common\logic\BaseService; | use common\logic\BaseService; | ||||
use common\logic\BuilderInterface; | use common\logic\BuilderInterface; | ||||
use common\logic\Config\TaxRate\TaxRate; | |||||
use common\logic\Order\Order\Order; | use common\logic\Order\Order\Order; | ||||
use common\logic\PointSale\PointSale\PointSale; | use common\logic\PointSale\PointSale\PointSale; | ||||
use common\logic\Product\Product\Product; | |||||
use common\logic\Product\Product\ProductSolver; | use common\logic\Product\Product\ProductSolver; | ||||
use common\logic\User\User\User; | use common\logic\User\User\User; | ||||
use common\logic\User\UserProducer\UserProducer; | use common\logic\User\UserProducer\UserProducer; | ||||
$this->productSolver = $this->loadService(ProductSolver::class); | $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 = 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; | return $productOrder; | ||||
} | } | ||||
public function getProducer() | public function getProducer() | ||||
{ | { | ||||
return $this->hasOne( | |||||
Producer::class, | |||||
['id' => 'id_producer'] | |||||
); | |||||
return $this->hasOne(Producer::class, ['id' => 'id_producer']); | |||||
} | } | ||||
public function getPointSale() | public function getPointSale() | ||||
return $this->hasMany(ProductSubscription::class, ['id_subscription' => 'id']) | return $this->hasMany(ProductSubscription::class, ['id_subscription' => 'id']) | ||||
->with('product'); | ->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; | |||||
} | |||||
} | } |
namespace common\logic\Subscription\Subscription; | namespace common\logic\Subscription\Subscription; | ||||
use common\helpers\GlobalParam; | |||||
use common\logic\BaseService; | use common\logic\BaseService; | ||||
use common\logic\Distribution\Distribution\Distribution; | |||||
use common\logic\RepositoryInterface; | use common\logic\RepositoryInterface; | ||||
class SubscriptionRepository extends BaseService implements 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 | public function defaultOptionsSearch(): array | ||||
{ | { | ||||
return [ | return [ | ||||
'attribute_id_producer' => 'subscription.id_producer' | '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; | |||||
} | |||||
} | } |
use common\logic\BaseService; | use common\logic\BaseService; | ||||
use common\logic\SolverInterface; | use common\logic\SolverInterface; | ||||
use common\logic\User\User\UserSolver; | |||||
class SubscriptionSolver extends BaseService implements SolverInterface | 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; | |||||
} | |||||
} | } |
<?php | <?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; | namespace common\logic\User\UserGroup; | ||||
use common\helpers\GlobalParam; | use common\helpers\GlobalParam; | ||||
use common\components\ActiveRecordCommon ; | |||||
use common\components\ActiveRecordCommon; | |||||
/** | /** | ||||
* This is the model class for table "user_group". | * This is the model class for table "user_group". | ||||
/** | /** | ||||
* @inheritdoc | * @inheritdoc | ||||
*/ | */ | ||||
public static function tableName() | |||||
public static function tableName() | |||||
{ | { | ||||
return 'user_group'; | return 'user_group'; | ||||
} | } | ||||
/** | /** | ||||
* @inheritdoc | * @inheritdoc | ||||
*/ | */ | ||||
public function rules() | |||||
public function rules() | |||||
{ | { | ||||
return [ | return [ | ||||
[['name', 'id_producer'], 'required'], | [['name', 'id_producer'], 'required'], | ||||
/** | /** | ||||
* @inheritdoc | * @inheritdoc | ||||
*/ | */ | ||||
public function attributeLabels() | |||||
public function attributeLabels() | |||||
{ | { | ||||
return [ | return [ | ||||
'name' => 'Nom', | '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 ; | |||||
} | |||||
} | } |
namespace common\logic\User\UserGroup; | namespace common\logic\User\UserGroup; | ||||
use common\helpers\GlobalParam; | |||||
use common\logic\BaseService; | use common\logic\BaseService; | ||||
use common\logic\RepositoryInterface; | use common\logic\RepositoryInterface; | ||||
class UserGroupRepository extends BaseService implements 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; | |||||
} | |||||
} | } |
namespace common\logic\User\UserGroup; | namespace common\logic\User\UserGroup; | ||||
use common\logic\PointSale\PointSale\PointSale; | |||||
use Yii; | use Yii; | ||||
use common\components\ActiveRecordCommon ; | use common\components\ActiveRecordCommon ; | ||||
*/ | */ | ||||
class UserUserGroup extends ActiveRecordCommon | class UserUserGroup extends ActiveRecordCommon | ||||
{ | { | ||||
/** | /** | ||||
* @inheritdoc | * @inheritdoc | ||||
*/ | */ | ||||
public function getPointSale() | 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' => '' | |||||
] ; | |||||
} | |||||
} | } |
class UserUserGroupRepository extends BaseService implements RepositoryInterface | class UserUserGroupRepository extends BaseService implements RepositoryInterface | ||||
{ | { | ||||
public function defaultOptionsSearch(): array | |||||
{ | |||||
return [ | |||||
'with' => [], | |||||
'join_with' => [], | |||||
'orderby' => '', | |||||
'attribute_id_producer' => '' | |||||
] ; | |||||
} | |||||
} | } |