@@ -202,15 +202,7 @@ class CronController extends BackendController | |||
$orderManager->initOrder($order); | |||
if ($order->auto_payment && $configCredit) { | |||
if ($orderManager->getOrderAmount($order, Order::AMOUNT_REMAINING) > 0) { | |||
$creditHistoryManager->createCreditHistory( | |||
CreditHistory::TYPE_PAYMENT, | |||
$orderManager->getOrderAmount($order, Order::AMOUNT_REMAINING), | |||
$order->distribution->producer, | |||
$order->user, | |||
$userManager->findOneUserById(User::ID_USER_SYSTEM), | |||
MeanPayment::CREDIT, | |||
$order | |||
); | |||
$creditHistoryManager->payOrder($order, $userManager->findOneUserById(User::ID_USER_SYSTEM), false); | |||
$countOrders++; | |||
} | |||
} |
@@ -1096,19 +1096,11 @@ class OrderController extends BackendController | |||
$orderManager = $this->getOrderManager(); | |||
$creditHistoryManager = $this->getCreditHistoryManager(); | |||
$order = $orderManager->findOneOrderById($idOrder); | |||
$orderManager->initOrder($order); | |||
if ($order) { | |||
$creditHistoryManager->createCreditHistory( | |||
$type, | |||
$amount, | |||
GlobalParam::getCurrentProducer(), | |||
$order->user, | |||
GlobalParam::getCurrentUser(), | |||
MeanPayment::CREDIT, | |||
$order | |||
); | |||
$creditHistoryManager->payOrRefundOrder($type, $order, $this->getUserCurrent()); | |||
} | |||
return ['success']; |
@@ -46,6 +46,7 @@ use common\logic\User\CreditHistory\Wrapper\CreditHistoryManager; | |||
use common\logic\User\User\Model\User; | |||
use common\logic\User\User\Wrapper\UserManager; | |||
use common\logic\User\UserProducer\Model\UserProducer; | |||
use common\logic\User\UserProducer\Wrapper\UserProducerManager; | |||
use Yii; | |||
use yii\base\Model; | |||
@@ -103,27 +104,18 @@ class CreditForm extends Model | |||
public function save() | |||
{ | |||
$userManager = UserManager::getInstance(); | |||
$userProducerManager = UserProducerManager::getInstance(); | |||
$creditHistoryManager = CreditHistoryManager::getInstance(); | |||
$producerManager = ProducerManager::getInstance(); | |||
if ($this->validate()) { | |||
$user = $userManager->findOneUserById($this->id_user); | |||
$creditHistoryManager->createCreditHistory( | |||
$this->type, | |||
$this->amount, | |||
GlobalParam::getCurrentProducer(), | |||
$user, | |||
Yii::$app->user->identity, | |||
$this->mean_payment | |||
); | |||
$creditHistoryManager->creditOrDebitUser($this->type, $user, $this->amount, $this->mean_payment, $userManager->getCurrent()); | |||
// on prévient l'utilisateur que son compte vient d'être crédité | |||
if($this->send_mail) { | |||
$user = User::findOne($this->id_user) ; | |||
$producer = GlobalParam::getCurrentProducer() ; | |||
$userProducer = UserProducer::searchOne([ | |||
'id_user' => $this->id_user | |||
]); | |||
$userProducer = $userProducerManager->findOneUserProducer($user); | |||
$paramsEmail = [ | |||
'from_email' => $producerManager->getEmailOpendistrib($producer), |
@@ -6,22 +6,29 @@ use common\helpers\MeanPayment; | |||
use common\logic\AbstractService; | |||
use common\logic\Order\Order\Model\Order; | |||
use common\logic\Order\Order\Service\OrderSolver; | |||
use common\logic\Producer\Producer\Repository\ProducerRepository; | |||
use common\logic\User\CreditHistory\Model\CreditHistory; | |||
use common\logic\User\User\Model\User; | |||
use common\logic\User\User\Repository\UserRepository; | |||
use common\logic\UtilsInterface; | |||
use yii\base\ErrorException; | |||
class CreditUtils extends AbstractService implements UtilsInterface | |||
{ | |||
protected CreditHistoryBuilder $creditHistoryBuilder; | |||
protected OrderSolver $orderSolver; | |||
protected ProducerRepository $producerRepository; | |||
protected UserRepository $userRepository; | |||
public function loadDependencies(): void | |||
{ | |||
$this->creditHistoryBuilder = $this->loadService(CreditHistoryBuilder::class); | |||
$this->orderSolver = $this->loadService(OrderSolver::class); | |||
$this->producerRepository = $this->loadService(ProducerRepository::class); | |||
$this->userRepository = $this->loadService(UserRepository::class); | |||
} | |||
public function creditCredit(float $amount, User $user, User $userAction, string $meanPayment): void | |||
public function creditUser(User $user, float $amount, string $meanPayment, User $userAction): void | |||
{ | |||
$this->creditHistoryBuilder->createCreditHistory( | |||
CreditHistory::TYPE_CREDIT, | |||
@@ -33,7 +40,7 @@ class CreditUtils extends AbstractService implements UtilsInterface | |||
); | |||
} | |||
public function debitCredit(float $amount, User $user, User $userAction, string $meanPayment): void | |||
public function debitUser(User $user, float $amount, string $meanPayment, User $userAction): void | |||
{ | |||
$this->creditHistoryBuilder->createCreditHistory( | |||
CreditHistory::TYPE_DEBIT, | |||
@@ -45,11 +52,50 @@ class CreditUtils extends AbstractService implements UtilsInterface | |||
); | |||
} | |||
public function payOrder(Order $order, User $userAction): void | |||
public function creditOrDebitUser(string $type, User $user, float $amount, string $meanPayment, User $userAction): void | |||
{ | |||
if($type == CreditHistory::TYPE_CREDIT) { | |||
$this->creditUser($user, $amount, $meanPayment, $userAction); | |||
} | |||
elseif($type == CreditHistory::TYPE_DEBIT) { | |||
$this->debitUser($user, $amount, $meanPayment, $userAction); | |||
} | |||
else { | |||
throw new ErrorException('$type a une valeur incorrect'); | |||
} | |||
} | |||
public function payOrder(Order $order, User $userAction, bool $checkCreditLimit): void | |||
{ | |||
$amountRemaining = $this->orderSolver->getOrderAmountWithTax($order, Order::AMOUNT_REMAINING); | |||
if($checkCreditLimit) { | |||
$creditLimit = $this->producerRepository->getConfig('credit_limit'); | |||
$creditUser = $this->userRepository->getCredit($order->user); | |||
if (!is_null($creditLimit) && $amountRemaining > $creditUser - $creditLimit) { | |||
$amountRemaining = $creditUser - $creditLimit; | |||
} | |||
} | |||
if($amountRemaining > 0) { | |||
$this->creditHistoryBuilder->createCreditHistory( | |||
CreditHistory::TYPE_PAYMENT, | |||
$amountRemaining, | |||
$this->getProducerContext(), | |||
$order->user, | |||
$userAction, | |||
MeanPayment::CREDIT, | |||
$order | |||
); | |||
} | |||
} | |||
public function refundOrder(Order $order, User $userAction): void | |||
{ | |||
$this->creditHistoryBuilder->createCreditHistory( | |||
CreditHistory::TYPE_PAYMENT, | |||
$this->orderSolver->getOrderAmountWithTax($order), | |||
CreditHistory::TYPE_REFUND, | |||
$this->orderSolver->getOrderAmountWithTax($order, Order::AMOUNT_PAID), | |||
$this->getProducerContext(), | |||
$order->user, | |||
$userAction, | |||
@@ -58,11 +104,11 @@ class CreditUtils extends AbstractService implements UtilsInterface | |||
); | |||
} | |||
public function refundOrder(Order $order, User $userAction): void | |||
public function refundSurplusOrder(Order $order, User $userAction): void | |||
{ | |||
$this->creditHistoryBuilder->createCreditHistory( | |||
CreditHistory::TYPE_REFUND, | |||
$this->orderSolver->getOrderAmountWithTax($order, Order::AMOUNT_PAID), | |||
$this->orderSolver->getOrderAmountWithTax($order, Order::AMOUNT_SURPLUS), | |||
$this->getProducerContext(), | |||
$order->user, | |||
$userAction, | |||
@@ -70,4 +116,17 @@ class CreditUtils extends AbstractService implements UtilsInterface | |||
$order | |||
); | |||
} | |||
public function payOrRefundOrder(string $type, Order $order, User $userAction, bool $checkCreditLimit = false): void | |||
{ | |||
if($type == CreditHistory::TYPE_PAYMENT) { | |||
$this->payOrder($order, $userAction, $checkCreditLimit); | |||
} | |||
elseif($type == CreditHistory::TYPE_REFUND) { | |||
$this->refundOrder($order, $userAction); | |||
} | |||
else { | |||
throw new ErrorException('$type a une valeur incorrect'); | |||
} | |||
} | |||
} |
@@ -7,14 +7,14 @@ use common\logic\User\CreditHistory\Repository\CreditHistoryRepository; | |||
use common\logic\User\CreditHistory\Service\CreditHistoryBuilder; | |||
use common\logic\User\CreditHistory\Service\CreditHistoryDefinition; | |||
use common\logic\User\CreditHistory\Service\CreditHistorySolver; | |||
use common\logic\User\CreditHistory\Service\CreditService; | |||
use common\logic\User\CreditHistory\Service\CreditUtils; | |||
/** | |||
* @mixin CreditHistoryDefinition | |||
* @mixin CreditHistorySolver | |||
* @mixin CreditHistoryRepository | |||
* @mixin CreditHistoryBuilder | |||
* @mixin CreditService | |||
* @mixin CreditUtils | |||
*/ | |||
class CreditHistoryManager extends AbstractManager | |||
{ |
@@ -180,6 +180,7 @@ class CreditController extends ProducerBaseController | |||
public function actionStripeVerification() | |||
{ | |||
$orderManager = $this->getOrderManager(); | |||
$creditHistoryManager = $this->getCreditHistoryManager(); | |||
$producerManager = $this->getProducerManager(); | |||
$userManager = $this->getUserManager(); | |||
@@ -214,6 +215,7 @@ class CreditController extends ProducerBaseController | |||
if (isset($paymentIntentMetadata->order_id)) { | |||
$order = $this->getOrderManager()->findOneOrderById($paymentIntentMetadata->order_id); | |||
$orderManager->initOrder($order); | |||
$pointSale = $this->getPointSaleManager()->findOnePointSaleById($order->id_point_sale); | |||
$distribution = $this->getDistributionManager()->findOneDistributionById($order->id_distribution); | |||
} | |||
@@ -233,26 +235,11 @@ class CreditController extends ProducerBaseController | |||
if (!$creditHistoryExist) { | |||
$creditHistoryManager->createCreditHistory( | |||
CreditHistory::TYPE_CREDIT, | |||
$amount, | |||
$producer, | |||
$user, | |||
$user, | |||
MeanPayment::CREDIT_CARD | |||
); | |||
$creditHistoryManager->creditUser($user, $amount, MeanPayment::CREDIT_CARD, $user); | |||
if (isset($order) && $order) { | |||
$creditHistoryManager->createCreditHistory( | |||
CreditHistory::TYPE_PAYMENT, | |||
$amount, | |||
$producer, | |||
$user, | |||
$user, | |||
MeanPayment::CREDIT, | |||
$order | |||
); | |||
$creditHistoryManager->payOrder($order, $user, true); | |||
// client : envoi d'un email de confirmation de paiement | |||
$paramsEmail = [ |
@@ -443,55 +443,22 @@ class OrderController extends ProducerBaseController | |||
// credit | |||
$credit = $producerManager->getConfig('credit'); | |||
$creditLimit = $producerManager->getConfig('credit_limit'); | |||
$creditFunctioning = $producerManager->getPointSaleCreditFunctioning($pointSale); | |||
$creditUser = $userManager->getCredit($user); | |||
$order = $orderManager->findOneOrderById($order->id); | |||
$orderManager->initOrder($order); | |||
$amountRemaining = $orderManager->getOrderAmount($order, Order::AMOUNT_REMAINING); | |||
if ($credit && $pointSale->credit && | |||
(($creditFunctioning == Producer::CREDIT_FUNCTIONING_OPTIONAL && $posts['use_credit']) || | |||
$creditFunctioning == Producer::CREDIT_FUNCTIONING_MANDATORY || | |||
($creditFunctioning == Producer::CREDIT_FUNCTIONING_USER && $userProducer->credit_active) | |||
)) { | |||
$orderManager->updateOrderStatus($order, 'waiting-paiement-by-credit', 'user'); | |||
// à payer | |||
if ($orderManager->getPaymentStatus($order) == Order::PAYMENT_UNPAID) { | |||
if (!is_null($creditLimit) && $amountRemaining > $creditUser - $creditLimit) { | |||
$amountRemaining = $creditUser - $creditLimit; | |||
} | |||
if ($amountRemaining > 0) { | |||
$creditHistoryManager->createCreditHistory( | |||
CreditHistory::TYPE_PAYMENT, | |||
$amountRemaining, | |||
$producer, | |||
GlobalParam::getCurrentUser(), | |||
GlobalParam::getCurrentUser(), | |||
MeanPayment::CREDIT, | |||
$order | |||
); | |||
$orderManager->updateOrderStatus($order, 'paid-by-credit', 'user'); | |||
} else { | |||
$orderManager->updateOrderStatus($order, 'waiting-paiement-on-delivery', 'user'); | |||
} | |||
$creditHistoryManager->payOrder($order, $this->getUserCurrent(), true); | |||
} // surplus à rembourser | |||
elseif ($orderManager->getPaymentStatus($order) == Order::PAYMENT_SURPLUS) { | |||
$amountSurplus = $orderManager->getOrderAmount($order, Order::AMOUNT_SURPLUS); | |||
$creditHistoryManager->createCreditHistory( | |||
CreditHistory::TYPE_REFUND, | |||
$amountSurplus, | |||
$producer, | |||
GlobalParam::getCurrentUser(), | |||
GlobalParam::getCurrentUser(), | |||
MeanPayment::CREDIT, | |||
$order | |||
); | |||
$creditHistoryManager->refundSurplusOrder($order, $this->getUserCurrent()); | |||
} | |||
} else { | |||
$orderManager->updateOrderStatus($order, 'waiting-paiement-on-delivery', 'user'); | |||
} | |||
$paramsEmail = [ |