]; | ]; | ||||
} | } | ||||
public function getOneById(int $id): ?PointSale | |||||
public function findOnePointSaleById(int $id): ?PointSale | |||||
{ | { | ||||
return PointSale::searchOne(['id' => $id]); | return PointSale::searchOne(['id' => $id]); | ||||
} | } | ||||
public function getByDistribution(Distribution $distribution) | |||||
public function findPointSalesByDistribution(Distribution $distribution) | |||||
{ | { | ||||
return PointSale::find() | return PointSale::find() | ||||
->with(['pointSaleDistribution' => function ($q) use ($distribution) { | ->with(['pointSaleDistribution' => function ($q) use ($distribution) { | ||||
/** | /** | ||||
* Retourne le nombre de points de vente pour le producteur courant. | * Retourne le nombre de points de vente pour le producteur courant. | ||||
*/ | */ | ||||
public static function count(): int | |||||
public static function countPointSales(): int | |||||
{ | { | ||||
return PointSale::searchCount(['id_producer' => GlobalParam::getCurrentProducerId()]); | return PointSale::searchCount(['id_producer' => GlobalParam::getCurrentProducerId()]); | ||||
} | } | ||||
public function get() | |||||
public function findPointSales() | |||||
{ | { | ||||
return PointSale::find() | return PointSale::find() | ||||
->where('id_producer = ' . GlobalParam::getCurrentProducerId()) | ->where('id_producer = ' . GlobalParam::getCurrentProducerId()) | ||||
->all();; | ->all();; | ||||
} | } | ||||
public function queryPublic(Producer $producer) | |||||
public function queryPointSalesPublic(Producer $producer) | |||||
{ | { | ||||
return PointSale::find() | return PointSale::find() | ||||
->where([ | ->where([ | ||||
public function populatePointSaleDropdownList(): array | public function populatePointSaleDropdownList(): array | ||||
{ | { | ||||
$pointSalesArrayDropdown = ['' => '--']; | $pointSalesArrayDropdown = ['' => '--']; | ||||
$pointSalesArray = $this->get(); | |||||
$pointSalesArray = $this->findPointSales(); | |||||
foreach ($pointSalesArray as $pointSale) { | foreach ($pointSalesArray as $pointSale) { | ||||
$pointSalesArrayDropdown[$pointSale['id']] = $pointSale['name']; | $pointSalesArrayDropdown[$pointSale['id']] = $pointSale['name']; |
$searchModel = new CreditHistorySearch(); | $searchModel = new CreditHistorySearch(); | ||||
$searchModel->id_user = GlobalParam::getCurrentUserId(); | $searchModel->id_user = GlobalParam::getCurrentUserId(); | ||||
$dataProvider = $searchModel->search(\Yii::$app->request->queryParams); | $dataProvider = $searchModel->search(\Yii::$app->request->queryParams); | ||||
$userProducer = $this->getLogic()->getUserProducerContainer()->getRepository()->getOne(GlobalParam::getCurrentUser(), $producer); | |||||
$userProducer = $this->getUserProducerManager()->findOneUserProducer(GlobalParam::getCurrentUser(), $producer); | |||||
if (strlen($returnPayment)) { | if (strlen($returnPayment)) { | ||||
if ($returnPayment == 'success') { | if ($returnPayment == 'success') { | ||||
$user = GlobalParam::getCurrentUser(); | $user = GlobalParam::getCurrentUser(); | ||||
\Stripe\Stripe::setApiKey( | \Stripe\Stripe::setApiKey( | ||||
$this->getLogic()->getProducerContainer()->getSolver()->getPrivateKeyApiStripe($producer) | |||||
$this->getProducerManager()->getPrivateKeyApiStripe($producer) | |||||
); | ); | ||||
$checkout_session = \Stripe\Checkout\Session::create([ | $checkout_session = \Stripe\Checkout\Session::create([ | ||||
public function actionStripeVerification() | public function actionStripeVerification() | ||||
{ | { | ||||
$creditHistoryBuilder = $this->getLogic()->getCreditHistoryContainer()->getBuilder(); | |||||
$producerSolver = $this->getLogic()->getProducerContainer()->getSolver(); | |||||
$userSolver = $this->getLogic()->getUserContainer()->getSolver(); | |||||
$creditHistoryManager = $this->getCreditHistoryManager(); | |||||
$producerManager = $this->getProducerManager(); | |||||
$userManager = $this->getUserManager(); | |||||
$producer = $this->getProducer(); | $producer = $this->getProducer(); | ||||
$contactProducer = $producer->getMainContact(); | |||||
$contactProducer = $producerManager->getMainContact($producer); | |||||
$payload = @file_get_contents('php://input'); | $payload = @file_get_contents('php://input'); | ||||
$sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE']; | $sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE']; | ||||
$event = \Stripe\Webhook::constructEvent( | $event = \Stripe\Webhook::constructEvent( | ||||
$payload, | $payload, | ||||
$sig_header, | $sig_header, | ||||
$producerSolver->getPrivateKeyEndpointStripe($producer) | |||||
$producerManager->getPrivateKeyEndpointStripe($producer) | |||||
); | ); | ||||
} catch (\UnexpectedValueException $e) { | } catch (\UnexpectedValueException $e) { | ||||
// Invalid payload | // Invalid payload | ||||
$paymentIntentMetadata = $paymentIntent->metadata; | $paymentIntentMetadata = $paymentIntent->metadata; | ||||
$amount = $paymentIntent->amount / 100; | $amount = $paymentIntent->amount / 100; | ||||
$idUser = $paymentIntentMetadata->user_id; | $idUser = $paymentIntentMetadata->user_id; | ||||
$user = $this->getLogic()->getUserContainer()->getRepository()->getOneById($paymentIntentMetadata->user_id); | |||||
$user = $userManager->findOneUserById($paymentIntentMetadata->user_id); | |||||
$idProducer = $paymentIntentMetadata->producer_id; | $idProducer = $paymentIntentMetadata->producer_id; | ||||
if (isset($paymentIntentMetadata->order_id)) { | if (isset($paymentIntentMetadata->order_id)) { | ||||
$order = $this->getOrderManager()->findOneById($paymentIntentMetadata->order_id); | $order = $this->getOrderManager()->findOneById($paymentIntentMetadata->order_id); | ||||
$pointSale = $this->getLogic()->getPointSaleContainer()->getRepostory()->getOneById($order->id_point_sale); | |||||
$pointSale = $this->getPointSaleManager()->findOnePointSaleById($order->id_point_sale); | |||||
$distribution = $this->getDistributionManager()->getOneById($order->id_distribution); | $distribution = $this->getDistributionManager()->getOneById($order->id_distribution); | ||||
} | } | ||||
if (!$creditHistoryExist) { | if (!$creditHistoryExist) { | ||||
$creditHistoryBuilder->create( | |||||
$creditHistoryManager->createCreditHistory( | |||||
CreditHistory::TYPE_CREDIT, | CreditHistory::TYPE_CREDIT, | ||||
$amount, | $amount, | ||||
$producer, | $producer, | ||||
if (isset($order) && $order) { | if (isset($order) && $order) { | ||||
$creditHistoryBuilder->create( | |||||
$creditHistoryManager->createCreditHistory( | |||||
CreditHistory::TYPE_PAYMENT, | CreditHistory::TYPE_PAYMENT, | ||||
$amount, | $amount, | ||||
$producer, | $producer, | ||||
// client : envoi d'un email de confirmation de paiement | // client : envoi d'un email de confirmation de paiement | ||||
$paramsEmail = [ | $paramsEmail = [ | ||||
'from_email' => $producerSolver->getEmailOpendistrib($producer), | |||||
'from_email' => $producerManager->getEmailOpendistrib($producer), | |||||
'from_name' => $producer->name, | 'from_name' => $producer->name, | ||||
'to_email' => $user->email, | 'to_email' => $user->email, | ||||
'to_name' => $userSolver->getUsername($user), | |||||
'to_name' => $userManager->getUsername($user), | |||||
'subject' => '[' . $producer->name . '] Confirmation de paiement', | 'subject' => '[' . $producer->name . '] Confirmation de paiement', | ||||
'content_view_text' => '@common/mail/paymentOrderConfirm-text.php', | 'content_view_text' => '@common/mail/paymentOrderConfirm-text.php', | ||||
'content_view_html' => '@common/mail/paymentOrderConfirm-html.php', | 'content_view_html' => '@common/mail/paymentOrderConfirm-html.php', | ||||
// producteur : mail de confirmation | // producteur : mail de confirmation | ||||
Mailjet::sendMail([ | Mailjet::sendMail([ | ||||
'from_email' => $producerSolver->getEmailOpendistrib($producer), | |||||
'from_email' => $producerManager->getEmailOpendistrib($producer), | |||||
'from_name' => $producer->name, | 'from_name' => $producer->name, | ||||
'to_email' => $contactProducer->email, | 'to_email' => $contactProducer->email, | ||||
'to_name' => $contactProducer->name, | 'to_name' => $contactProducer->name, | ||||
] | ] | ||||
]); | ]); | ||||
} else { | } else { | ||||
$userProducer = $this->getLogic()->getUserProducerContainer()->getRepository()->getOne($user, $producer); | |||||
$userProducer = $this->getUserProducerManager()->findOneUserProducer($user, $producer); | |||||
Mailjet::sendMail([ | Mailjet::sendMail([ | ||||
'from_email' => $producerSolver->getEmailOpendistrib($producer), | |||||
'from_email' => $producerManager->getEmailOpendistrib($producer), | |||||
'from_name' => $producer->name, | 'from_name' => $producer->name, | ||||
'to_email' => $user->email, | 'to_email' => $user->email, | ||||
'to_name' => $userSolver->getUsername($user), | |||||
'to_name' => $userManager->getUsername($user), | |||||
'subject' => '[' . $producer->name . '] Alimentation de votre crédit', | 'subject' => '[' . $producer->name . '] Alimentation de votre crédit', | ||||
'content_view_text' => '@common/mail/creditConfirm-text.php', | 'content_view_text' => '@common/mail/creditConfirm-text.php', | ||||
'content_view_html' => '@common/mail/creditConfirm-html.php', | 'content_view_html' => '@common/mail/creditConfirm-html.php', | ||||
// client | // client | ||||
Mailjet::sendMail([ | Mailjet::sendMail([ | ||||
'from_email' => $producerSolver->getEmailOpendistrib($producer), | |||||
'from_email' => $producerManager->getEmailOpendistrib($producer), | |||||
'from_name' => $producer->name, | 'from_name' => $producer->name, | ||||
'to_email' => $user->email, | 'to_email' => $user->email, | ||||
'to_name' => $userSolver->getUsername($user), | |||||
'to_name' => $userManager->getUsername($user), | |||||
'subject' => '[' . $producer->name . '] Erreur de paiement', | 'subject' => '[' . $producer->name . '] Erreur de paiement', | ||||
'content_view_text' => '@common/mail/paymentError-text.php', | 'content_view_text' => '@common/mail/paymentError-text.php', | ||||
'content_view_html' => '@common/mail/paymentError-html.php', | 'content_view_html' => '@common/mail/paymentError-html.php', | ||||
// producteur | // producteur | ||||
if (isset($order) && $order) { | if (isset($order) && $order) { | ||||
Mailjet::sendMail([ | Mailjet::sendMail([ | ||||
'from_email' => $producerSolver->getEmailOpendistrib($producer), | |||||
'from_email' => $producerManager->getEmailOpendistrib($producer), | |||||
'from_name' => $producer->name, | 'from_name' => $producer->name, | ||||
'to_email' => $contactProducer->email, | 'to_email' => $contactProducer->email, | ||||
'to_name' => $contactProducer->name, | 'to_name' => $contactProducer->name, |
public function actionAjaxProcess() | public function actionAjaxProcess() | ||||
{ | { | ||||
$subscriptionRepository = $this->getSubscriptionContainer()->getRepository(); | |||||
$subscriptionManager = $this->getSubscriptionManager(); | |||||
$model = new SubscriptionForm(); | $model = new SubscriptionForm(); | ||||
$model->id_producer = GlobalParam::getCurrentProducerId(); | $model->id_producer = GlobalParam::getCurrentProducerId(); | ||||
$model->id_user = GlobalParam::getCurrentUserId(); | $model->id_user = GlobalParam::getCurrentUserId(); | ||||
$idSubscription = (int)$posts['idSubscription']; | $idSubscription = (int)$posts['idSubscription']; | ||||
$isUpdate = false; | $isUpdate = false; | ||||
if ($idSubscription) { | if ($idSubscription) { | ||||
$subscription = $subscriptionRepository->getOneById($idSubscription); | |||||
$subscription = $subscriptionManager->findOneSubscriptionById($idSubscription); | |||||
if ($subscription) { | if ($subscription) { | ||||
$model->id = $idSubscription; | $model->id = $idSubscription; | ||||
if ($model->load($posts) && $model->validate() && $model->save()) { | if ($model->load($posts) && $model->validate() && $model->save()) { | ||||
$subscription = $subscriptionRepository->getOneById($model->id); | |||||
$subscription = $subscriptionManager->findOneSubscriptionById($model->id); | |||||
$this->getOrderManager()->updateOrdersIncomingDistributionsFromSubscription($subscription, $isUpdate); | $this->getOrderManager()->updateOrdersIncomingDistributionsFromSubscription($subscription, $isUpdate); | ||||
if ($isUpdate) { | if ($isUpdate) { | ||||
return $this->redirect($this->getUrlManagerFrontend()->createAbsoluteUrl(['site/producer', 'id' => $producer->id])); | return $this->redirect($this->getUrlManagerFrontend()->createAbsoluteUrl(['site/producer', 'id' => $producer->id])); | ||||
} | } | ||||
$subscriptionRepository = $this->getSubscriptionContainer()->getRepository(); | |||||
$subscriptionManager = $this->getSubscriptionManager(); | |||||
$model = new SubscriptionForm; | $model = new SubscriptionForm; | ||||
$subscription = $subscriptionRepository->getOneById($id); | |||||
$subscription = $subscriptionManager->findOneSubscriptionById($id); | |||||
if ($subscription) { | if ($subscription) { | ||||
$model->id = $id; | $model->id = $id; | ||||
throw new NotFoundHttpException('L\'abonnement est introuvable.', 404); | throw new NotFoundHttpException('L\'abonnement est introuvable.', 404); | ||||
} | } | ||||
$productsArray = $this->getProductContainer()->getRepository()->get(); | |||||
$productsArray = $this->getProductManager()->findProducts(); | |||||
if ($model->load(\Yii::$app->request->post()) && $model->validate()) { | if ($model->load(\Yii::$app->request->post()) && $model->validate()) { | ||||
if (!strlen($model->date_end)) { | if (!strlen($model->date_end)) { | ||||
if ($model->save()) { | if ($model->save()) { | ||||
$this->setFlash('success', 'Abonnement modifié'); | $this->setFlash('success', 'Abonnement modifié'); | ||||
$subscription = $subscriptionRepository->getOneById($model->id); | |||||
$matchedDistributionsArray = $subscriptionRepository->searchMatchedIncomingDistributions($subscription); | |||||
$subscription = $subscriptionManager->findOneSubscriptionById($model->id); | |||||
$matchedDistributionsArray = $this->getDistributionManager()->findDistributionsIncomingMatchWithSubscrtiption($subscription); | |||||
if (count($matchedDistributionsArray)) { | if (count($matchedDistributionsArray)) { | ||||
return $this->redirect( | return $this->redirect( | ||||
return $this->redirect($this->getUrlManagerFrontend()->createAbsoluteUrl(['site/producer', 'id' => $producer->id])); | return $this->redirect($this->getUrlManagerFrontend()->createAbsoluteUrl(['site/producer', 'id' => $producer->id])); | ||||
} | } | ||||
$subscriptionContainer = $this->getSubscriptionContainer(); | |||||
$subscription = $subscriptionContainer->getRepository()->getOneById($id); | |||||
$subscriptionContainer->getBuilder()->delete($subscription); | |||||
$subscriptionManager = $this->getSubscriptionManager(); | |||||
$subscription = $subscriptionManager->findOneSubscriptionById($id); | |||||
$subscriptionManager->deleteSubscription($subscription); | |||||
// @TODO : gérer via événements | // @TODO : gérer via événements | ||||
$this->getOrderManager()->deleteOrdersIncomingDistributionsFromSubscription($subscription); | $this->getOrderManager()->deleteOrdersIncomingDistributionsFromSubscription($subscription); | ||||
{ | { | ||||
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; | \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; | ||||
$params = []; | $params = []; | ||||
$productSolver = $this->getProductContainer()->getSolver(); | |||||
$subscriptionRepositpry = $this->getSubscriptionContainer()->getRepository(); | |||||
$productManager = $this->getProductManager(); | |||||
$subscriptionManager = $this->getSubscriptionManager(); | |||||
$user = GlobalParam::getCurrentUser(); | $user = GlobalParam::getCurrentUser(); | ||||
$userProducer = $this->getUserProducerContainer()->getRepository()->getOne( | |||||
$userProducer = $this->getUserProducerManager()->findOneUserProducer( | |||||
GlobalParam::getCurrentUser(), | GlobalParam::getCurrentUser(), | ||||
GlobalParam::getCurrentProducer() | GlobalParam::getCurrentProducer() | ||||
); | ); | ||||
'id_subscription' => $idSubscription | 'id_subscription' => $idSubscription | ||||
]); | ]); | ||||
$subscription = $subscriptionRepositpry->getOneById($idSubscription); | |||||
$subscription = $subscriptionManager->findOneSubscriptionById($idSubscription); | |||||
if ($subscription && $subscription->id_point_sale) { | if ($subscription && $subscription->id_point_sale) { | ||||
$pointSale = $this->getPointSaleContainer()->getRepostory() | |||||
->getOneById($subscription->id_point_sale); | |||||
$pointSale = $this->getPointSaleManager()->findOnePointSaleById($subscription->id_point_sale); | |||||
} | } | ||||
} | } | ||||
// Produits | // Produits | ||||
$productsArray = $this->getProductContainer()->getRepository()->get(); | |||||
$productsArray = $productManager->findProducts(); | |||||
$indexProduct = 0; | $indexProduct = 0; | ||||
foreach ($productsArray as &$product) { | foreach ($productsArray as &$product) { | ||||
$quantity = 0; | $quantity = 0; | ||||
'index' => $indexProduct++, | 'index' => $indexProduct++, | ||||
'quantity_form' => $quantity, | 'quantity_form' => $quantity, | ||||
'coefficient_unit' => $coefficientUnit, | 'coefficient_unit' => $coefficientUnit, | ||||
'wording_unit' => $productSolver->strUnit($product->unit, 'wording_unit', true), | |||||
'wording_short' => $productSolver->strUnit($product->unit, 'wording_short'), | |||||
'price_with_tax' => $productSolver->getPriceWithTax($product, [ | |||||
'wording_unit' => $productManager->strUnit($product->unit, 'wording_unit', true), | |||||
'wording_short' => $productManager->strUnit($product->unit, 'wording_short'), | |||||
'price_with_tax' => $productManager->getPriceWithTax($product, [ | |||||
'user' => $user, | 'user' => $user, | ||||
'user_producer' => $userProducer, | 'user_producer' => $userProducer, | ||||
'point_sale' => $pointSale | 'point_sale' => $pointSale | ||||
$params['products'] = $productsArray; | $params['products'] = $productsArray; | ||||
$pointsSaleArray = $this->getPointSaleContainer()->getRepostory()->get(); | |||||
$pointsSaleArray = $this->getPointSaleManager()->findPointSales(); | |||||
foreach ($pointsSaleArray as &$pointSale) { | foreach ($pointsSaleArray as &$pointSale) { | ||||
$pointSale = array_merge($pointSale->getAttributes(), [ | $pointSale = array_merge($pointSale->getAttributes(), [ | ||||
'userPointSale' => ($pointSale->userPointSale ? $pointSale->userPointSale[0] : '') | 'userPointSale' => ($pointSale->userPointSale ? $pointSale->userPointSale[0] : '') | ||||
$params['points_sale'] = $pointsSaleArray; | $params['points_sale'] = $pointsSaleArray; | ||||
if ($idSubscription > 0) { | if ($idSubscription > 0) { | ||||
$subscription = $subscriptionRepositpry->getOneById($idSubscription); | |||||
$subscription = $subscriptionManager->findOneSubscriptionById($idSubscription); | |||||
if (!$subscription || $subscription->id_user != GlobalParam::getCurrentUserId()) { | if (!$subscription || $subscription->id_user != GlobalParam::getCurrentUserId()) { | ||||
throw new UserException('Abonnement introuvable'); | throw new UserException('Abonnement introuvable'); | ||||
public function actionAjaxValidateCodePointSale($idPointSale, $code) | public function actionAjaxValidateCodePointSale($idPointSale, $code) | ||||
{ | { | ||||
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; | \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; | ||||
$pointSaleManager = $this->getPointSaleManager(); | |||||
$pointSale = $pointSaleManager->findOnePointSaleById($idPointSale); | |||||
$pointSale = $this->getPointSaleContainer()->getRepostory()->getOneById($idPointSale); | |||||
if ($pointSale) { | |||||
if ($this->getPointSaleContainer()->getSolver()->validateCode($pointSale, $code)) { | |||||
return 1; | |||||
} | |||||
if ($pointSale && $pointSaleManager->validateCode($pointSale, $code)) { | |||||
return 1; | |||||
} | } | ||||
return 0; | return 0; | ||||
} | } | ||||
} | } |