public function buildAjaxInfosResponseTiller(Producer $producer, string $date) | public function buildAjaxInfosResponseTiller(Producer $producer, string $date) | ||||
{ | { | ||||
if ($producer->tiller) { | |||||
$tiller = new Tiller(); | |||||
return (int) $tiller->isSynchro($date); | |||||
} | |||||
return false; | |||||
return $this->getOrderModule()->getTillerManager()->isSynchronized($date); | |||||
} | } | ||||
public function buildAjaxInfosResponseMissingSubscriptions(string $date, Distribution $distribution, array $ordersArray) | public function buildAjaxInfosResponseMissingSubscriptions(string $date, Distribution $distribution, array $ordersArray) | ||||
* | * | ||||
* @param string $date | * @param string $date | ||||
*/ | */ | ||||
public function actionAjaxProcessSynchroTiller($date) | |||||
public function actionAjaxProcessSynchroTiller(string $date) | |||||
{ | { | ||||
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; | \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; | ||||
$producerModule = $this->getProducerModule(); | |||||
$productOrderModule = $this->getProductOrderModule(); | |||||
$orderModule = $this->getOrderModule(); | |||||
$return = []; | |||||
$producerTiller = $producerModule->getConfig('tiller'); | |||||
if ($producerTiller) { | |||||
$tiller = new Tiller(); | |||||
$isSynchro = $tiller->isSynchro($date); | |||||
if (!$isSynchro) { | |||||
$orders = Order::searchAll([ | |||||
'distribution.date' => $date, | |||||
'order.tiller_synchronization' => 1 | |||||
], [ | |||||
'conditions' => 'date_delete IS NULL' | |||||
]); | |||||
$strDate = date('Y-m-d\T12:i:s+0000', strtotime($date) + 1); | |||||
if ($orders && count($orders)) { | |||||
foreach ($orders as $order) { | |||||
$orderModule->initOrder($order); | |||||
$lines = []; | |||||
foreach ($order->productOrder as $productOrder) { | |||||
$lines[] = [ | |||||
'name' => $productOrder->product->name, | |||||
'price' => $productOrderModule->getPriceWithTax($productOrder) * 100 * $productOrder->quantity, | |||||
'tax' => $productOrder->taxRate->value * 100, | |||||
'date' => $strDate, | |||||
'quantity' => $productOrder->quantity | |||||
]; | |||||
} | |||||
$typePaymentTiller = ''; | |||||
if ($order->mean_payment == MeanPayment::MONEY) { | |||||
$typePaymentTiller = 'CASH'; | |||||
} | |||||
if ($order->mean_payment == MeanPayment::CREDIT_CARD | |||||
|| $order->mean_payment == MeanPayment::CREDIT | |||||
|| $order->mean_payment == MeanPayment::TRANSFER | |||||
|| $order->mean_payment == MeanPayment::OTHER) { | |||||
$typePaymentTiller = 'CARD'; | |||||
} | |||||
if ($order->mean_payment == MeanPayment::CHEQUE) { | |||||
$typePaymentTiller = 'BANK_CHECK'; | |||||
} | |||||
if (!strlen($typePaymentTiller) || !$order->mean_payment) { | |||||
$typePaymentTiller = 'CASH'; | |||||
} | |||||
if(!$tiller->isSynchro($date, $order->id)) { | |||||
$returnTiller = $tiller->postOrder([ | |||||
'externalId' => $order->id, | |||||
'type' => 1, | |||||
'status' => 'IN_PROGRESS', | |||||
'openDate' => $strDate, | |||||
'closeDate' => $strDate, | |||||
'lines' => $lines, | |||||
'payments' => [ | |||||
[ | |||||
'type' => $typePaymentTiller, | |||||
'amount' => $orderModule->getOrderAmountWithTax( | |||||
$order, | |||||
Order::AMOUNT_PAID | |||||
) * 100, | |||||
'status' => 'ACCEPTED', | |||||
'date' => $strDate | |||||
] | |||||
] | |||||
]); | |||||
$returnTillerObject = json_decode($returnTiller); | |||||
$order->tiller_external_id = '' . $returnTillerObject->id; | |||||
$order->save(); | |||||
$return[] = $returnTiller; | |||||
} | |||||
} | |||||
} | |||||
} | |||||
} | |||||
return $return; | |||||
return $this->getOrderModule()->getTillerManager()->synchronizeDistribution($date); | |||||
} | } | ||||
public function actionAjaxGenerateDeliveryNotePointSale(string $idOrders) | public function actionAjaxGenerateDeliveryNotePointSale(string $idOrders) |
<?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. | |||||
*/ | |||||
namespace common\components; | |||||
use linslin\yii2\curl; | |||||
class TillerClient | |||||
{ | |||||
var $curl; | |||||
var $providerToken; | |||||
var $restaurantToken; | |||||
var $urlApi = 'https://app.tillersystems.com/api/'; | |||||
public function __construct(string $providerToken, string $restaurantToken) | |||||
{ | |||||
$this->curl = new curl\Curl(); | |||||
$this->providerToken = $providerToken; | |||||
$this->restaurantToken = $restaurantToken; | |||||
} | |||||
public function getOrders($date) | |||||
{ | |||||
$orders = $this->curl->setGetParams([ | |||||
'provider_token' => $this->providerToken, | |||||
'restaurant_token' => $this->restaurantToken, | |||||
'dateFrom' => date('Y-m-d H-i-s', strtotime($date)), | |||||
'dateTo' => date( | |||||
'Y-m-d H-i-s', | |||||
strtotime($date) + 24 * 60 * 60 - 1 | |||||
), | |||||
'status' => 'IN_PROGRESS', | |||||
])->get($this->urlApi . 'orders'); | |||||
return json_decode($orders); | |||||
} | |||||
public function postOrder($params) | |||||
{ | |||||
return $this->curl->setPostParams( | |||||
array_merge([ | |||||
'provider_token' => $this->providerToken, | |||||
'restaurant_token' => $this->restaurantToken, | |||||
], $params) | |||||
) | |||||
->post($this->urlApi . 'orders'); | |||||
} | |||||
} |
<?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. | |||||
*/ | |||||
namespace common\helpers; | |||||
use common\logic\Order\Order\Model\Order; | |||||
use common\logic\Order\Order\Module\OrderModule; | |||||
use common\logic\Producer\Producer\Module\ProducerModule; | |||||
use linslin\yii2\curl; | |||||
class Tiller | |||||
{ | |||||
var $curl; | |||||
var $producer_tiller; | |||||
var $provider_token; | |||||
var $restaurant_token; | |||||
//var $url_api = 'https://developers.tillersystems.com/api/'; | |||||
var $url_api = 'https://app.tillersystems.com/api/'; | |||||
public function __construct() | |||||
{ | |||||
$producerModule = ProducerModule::getInstance(); | |||||
$this->curl = new curl\Curl(); | |||||
$this->producer_tiller = $producerModule->getConfig('tiller'); | |||||
$this->provider_token = $producerModule->getConfig('tiller_provider_token'); | |||||
$this->restaurant_token = $producerModule->getConfig('tiller_restaurant_token'); | |||||
} | |||||
public function getOrders($date) | |||||
{ | |||||
if ($this->producer_tiller) { | |||||
$orders = $this->curl->setGetParams([ | |||||
'provider_token' => $this->provider_token, | |||||
'restaurant_token' => $this->restaurant_token, | |||||
'dateFrom' => date('Y-m-d H-i-s', strtotime($date)), | |||||
'dateTo' => date( | |||||
'Y-m-d H-i-s', | |||||
strtotime($date) + 24 * 60 * 60 - 1 | |||||
), | |||||
'status' => 'IN_PROGRESS', | |||||
])->get($this->url_api . 'orders'); | |||||
return json_decode($orders); | |||||
} | |||||
} | |||||
public function isSynchro($date, $idOrder = null) | |||||
{ | |||||
$orderModule = OrderModule::getInstance(); | |||||
if ($this->producer_tiller) { | |||||
$ordersTiller = $this->getOrders($date); | |||||
$ordersOpendistrib = Order::searchAll([ | |||||
'distribution.date' => $date, | |||||
'order.tiller_synchronization' => 1 | |||||
], [ | |||||
'conditions' => 'date_delete IS NULL' | |||||
]); | |||||
$ordersOpendistribSynchro = []; | |||||
if ($ordersOpendistrib) { | |||||
foreach ($ordersOpendistrib as $orderOpendistrib) { | |||||
$orderModule->initOrder($orderOpendistrib); | |||||
$ordersOpendistribSynchro[$orderOpendistrib->id] = false; | |||||
if (isset($ordersTiller->orders)) { | |||||
foreach ($ordersTiller->orders as $orderTiller) { | |||||
if ($orderOpendistrib->tiller_external_id == $orderTiller->id) { | |||||
$amountTotalPaidOrderOpendistrib = (int)round( | |||||
$orderModule->getOrderAmountWithTax($orderOpendistrib, Order::AMOUNT_PAID) * 100 | |||||
); | |||||
if ($amountTotalPaidOrderOpendistrib >= (int)$orderTiller->currentPayedAmount | |||||
|| $amountTotalPaidOrderOpendistrib >= (int)$orderTiller->currentBill) { | |||||
$ordersOpendistribSynchro[$orderOpendistrib->id] = true; | |||||
} | |||||
} | |||||
} | |||||
} | |||||
} | |||||
} | |||||
if($idOrder && isset($ordersOpendistribSynchro[$idOrder])) { | |||||
return $ordersOpendistribSynchro[$idOrder]; | |||||
} | |||||
else { | |||||
foreach ($ordersOpendistribSynchro as $idOrder => $isSynchro) { | |||||
if (!$isSynchro) { | |||||
return false; | |||||
} | |||||
} | |||||
} | |||||
return true; | |||||
} | |||||
} | |||||
public function postOrder($params) | |||||
{ | |||||
if ($this->producer_tiller) { | |||||
return $this->curl->setPostParams( | |||||
array_merge([ | |||||
'provider_token' => $this->provider_token, | |||||
'restaurant_token' => $this->restaurant_token, | |||||
], $params) | |||||
) | |||||
->post($this->url_api . 'orders'); | |||||
} | |||||
} | |||||
} |
use common\logic\Order\Order\Service\OrderDocumentManager; | use common\logic\Order\Order\Service\OrderDocumentManager; | ||||
use common\logic\Order\Order\Service\OrderSolver; | use common\logic\Order\Order\Service\OrderSolver; | ||||
use common\logic\Order\Order\Service\OrderManager; | use common\logic\Order\Order\Service\OrderManager; | ||||
use common\logic\Order\Order\Service\TillerManager; | |||||
/** | /** | ||||
* @mixin OrderDefinition | * @mixin OrderDefinition | ||||
OrderBuilder::class, | OrderBuilder::class, | ||||
OrderManager::class, | OrderManager::class, | ||||
OrderDocumentManager::class, | OrderDocumentManager::class, | ||||
TillerManager::class, | |||||
]; | ]; | ||||
} | } | ||||
{ | { | ||||
return OrderDocumentManager::getInstance(); | return OrderDocumentManager::getInstance(); | ||||
} | } | ||||
public function getTillerManager(): TillerManager | |||||
{ | |||||
return TillerManager::getInstance(); | |||||
} | |||||
} | } |
<?php | |||||
namespace common\logic\Order\Order\Service; | |||||
use common\components\TillerClient; | |||||
use common\helpers\MeanPayment; | |||||
use common\logic\AbstractManager; | |||||
use common\logic\Order\Order\Model\Order; | |||||
use common\logic\Order\ProductOrder\Service\ProductOrderSolver; | |||||
use common\logic\Producer\Producer\Service\ProducerSolver; | |||||
class TillerManager extends AbstractManager | |||||
{ | |||||
protected bool $tillerActivated; | |||||
protected ProducerSolver $producerSolver; | |||||
protected TillerClient $tillerClient; | |||||
protected OrderSolver $orderSolver; | |||||
protected OrderBuilder $orderBuilder; | |||||
protected ProductOrderSolver $productOrderSolver; | |||||
public function loadDependencies(): void | |||||
{ | |||||
$this->producerSolver = $this->loadService(ProducerSolver::class); | |||||
$this->tillerActivated = $this->producerSolver->getConfig('tiller'); | |||||
$this->tillerClient = $this->getClient(); | |||||
$this->orderSolver = $this->loadService(OrderSolver::class); | |||||
$this->orderBuilder = $this->loadService(OrderBuilder::class); | |||||
$this->productOrderSolver = $this->loadService(ProductOrderSolver::class); | |||||
} | |||||
public function getClient(): TillerClient | |||||
{ | |||||
return new TillerClient( | |||||
$this->producerSolver->getConfig('tiller_provider_token'), | |||||
$this->producerSolver->getConfig('tiller_restaurant_token') | |||||
); | |||||
} | |||||
public function synchronizeDistribution(string $date): array | |||||
{ | |||||
$return = []; | |||||
if($this->tillerActivated) { | |||||
$isSynchro = $this->isSynchronized($date); | |||||
if (!$isSynchro) { | |||||
$orders = Order::searchAll([ | |||||
'distribution.date' => $date, | |||||
'order.tiller_synchronization' => 1 | |||||
], [ | |||||
'conditions' => 'date_delete IS NULL' | |||||
]); | |||||
$strDate = date('Y-m-d\T12:i:s+0000', strtotime($date) + 1); | |||||
if ($orders && count($orders)) { | |||||
foreach ($orders as $order) { | |||||
$this->orderBuilder->initOrder($order); | |||||
$lines = []; | |||||
foreach ($order->productOrder as $productOrder) { | |||||
$lines[] = [ | |||||
'name' => $productOrder->product->name, | |||||
'price' => $this->productOrderSolver->getPriceWithTax($productOrder) * 100 * $productOrder->quantity, | |||||
'tax' => $productOrder->taxRate->value * 100, | |||||
'date' => $strDate, | |||||
'quantity' => $productOrder->quantity | |||||
]; | |||||
} | |||||
$typePaymentTiller = ''; | |||||
if ($order->mean_payment == MeanPayment::MONEY) { | |||||
$typePaymentTiller = 'CASH'; | |||||
} | |||||
if ($order->mean_payment == MeanPayment::CREDIT_CARD | |||||
|| $order->mean_payment == MeanPayment::CREDIT | |||||
|| $order->mean_payment == MeanPayment::TRANSFER | |||||
|| $order->mean_payment == MeanPayment::OTHER) { | |||||
$typePaymentTiller = 'CARD'; | |||||
} | |||||
if ($order->mean_payment == MeanPayment::CHEQUE) { | |||||
$typePaymentTiller = 'BANK_CHECK'; | |||||
} | |||||
if (!strlen($typePaymentTiller) || !$order->mean_payment) { | |||||
$typePaymentTiller = 'CASH'; | |||||
} | |||||
if (!$this->isSynchronized($date, $order->id)) { | |||||
$returnTiller = $this->tillerClient->postOrder([ | |||||
'externalId' => $order->id, | |||||
'type' => 1, | |||||
'status' => 'IN_PROGRESS', | |||||
'openDate' => $strDate, | |||||
'closeDate' => $strDate, | |||||
'lines' => $lines, | |||||
'payments' => [ | |||||
[ | |||||
'type' => $typePaymentTiller, | |||||
'amount' => $this->orderSolver->getOrderAmountWithTax( | |||||
$order, | |||||
Order::AMOUNT_PAID | |||||
) * 100, | |||||
'status' => 'ACCEPTED', | |||||
'date' => $strDate | |||||
] | |||||
] | |||||
]); | |||||
$returnTillerObject = json_decode($returnTiller); | |||||
$order->tiller_external_id = '' . $returnTillerObject->id; | |||||
$order->save(); | |||||
$return[] = $returnTiller; | |||||
} | |||||
} | |||||
} | |||||
} | |||||
} | |||||
return $return; | |||||
} | |||||
public function isSynchronized($date, $idOrder = null): bool | |||||
{ | |||||
if ($this->tillerActivated) { | |||||
$ordersTiller = $this->tillerClient->getOrders($date); | |||||
$ordersOpendistrib = Order::searchAll([ | |||||
'distribution.date' => $date, | |||||
'order.tiller_synchronization' => 1 | |||||
], [ | |||||
'conditions' => 'date_delete IS NULL' | |||||
]); | |||||
$ordersOpendistribSynchro = []; | |||||
if ($ordersOpendistrib) { | |||||
foreach ($ordersOpendistrib as $orderOpendistrib) { | |||||
$this->orderBuilder->initOrder($orderOpendistrib); | |||||
$ordersOpendistribSynchro[$orderOpendistrib->id] = false; | |||||
if (isset($ordersTiller->orders)) { | |||||
foreach ($ordersTiller->orders as $orderTiller) { | |||||
if ($orderOpendistrib->tiller_external_id == $orderTiller->id) { | |||||
$amountTotalPaidOrderOpendistrib = (int)round( | |||||
$this->orderSolver->getOrderAmountWithTax($orderOpendistrib, Order::AMOUNT_PAID) * 100 | |||||
); | |||||
if ($amountTotalPaidOrderOpendistrib >= (int)$orderTiller->currentPayedAmount | |||||
|| $amountTotalPaidOrderOpendistrib >= (int)$orderTiller->currentBill) { | |||||
$ordersOpendistribSynchro[$orderOpendistrib->id] = true; | |||||
} | |||||
} | |||||
} | |||||
} | |||||
} | |||||
} | |||||
if($idOrder && isset($ordersOpendistribSynchro[$idOrder])) { | |||||
return $ordersOpendistribSynchro[$idOrder]; | |||||
} | |||||
else { | |||||
foreach ($ordersOpendistribSynchro as $idOrder => $isSynchro) { | |||||
if (!$isSynchro) { | |||||
return false; | |||||
} | |||||
} | |||||
} | |||||
return true; | |||||
} | |||||
return false; | |||||
} | |||||
} |