Browse Source

[Administration] Distributions > Tiller : synchronisation forcée d'une seule commande

feature/rotating_product
Guillaume Bourgeois 6 months ago
parent
commit
7a325ebc87
5 changed files with 148 additions and 119 deletions
  1. +7
    -0
      backend/controllers/DistributionController.php
  2. +1
    -0
      backend/views/distribution/index.php
  3. +12
    -0
      backend/web/js/vuejs/distribution-index.js
  4. +5
    -6
      common/components/Tiller/TillerClientV2.php
  5. +123
    -113
      domain/Order/Order/TillerManager.php

+ 7
- 0
backend/controllers/DistributionController.php View File

@@ -725,6 +725,13 @@ class DistributionController extends BackendController
return $this->getOrderModule()->getTillerManager()->synchronizeDistribution($date);
}

public function actionAjaxForceSynchronizeOrderTiller(int $idOrder)
{
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$order = $this->getOrderModule()->getRepository()->findOneOrderById($idOrder);
return $this->getOrderModule()->getTillerManager()->synchronizeOrder($order, true);
}

public function actionAjaxGenerateDeliveryNotePointSale(string $idOrders)
{
if (strlen($idOrders)) {

+ 1
- 0
backend/views/distribution/index.php View File

@@ -513,6 +513,7 @@ $this->setPageTitle('Distributions') ;
</button>
<ul class="dropdown-menu">
<li><a href="javascript:void(0);" class="" :data-id-order="order.id" @click="deleteOrderClick"><span class="glyphicon glyphicon-trash"></span> {{ order.labelDeleteAction }}</a></li>
<li v-if="producer && producer.tiller"><a href="javascript:void(0);" @click="forceSynchronizeOrderTiller(order.id)"><span class="glyphicon glyphicon-transfer"></span> Synchroniser avec Tiller</a></li>
<li v-if="order.id_subscription > 0"><a class="" :href="baseUrl+'/subscription/update?id='+order.id_subscription"><span class="glyphicon glyphicon-repeat"></span> Modifier l'abonnement lié</a></li>
<li v-else><a class="add-subscription" :href="baseUrl+'/subscription/create?idOrder='+order.id"><span class="glyphicon glyphicon-plus"></span><span class="glyphicon glyphicon-repeat"></span>Créer un abonnement</a></li>
</ul>

+ 12
- 0
backend/web/js/vuejs/distribution-index.js View File

@@ -643,6 +643,18 @@ if($(selector).length) {
app.init(app.idActivePointSale);
});
},
forceSynchronizeOrderTiller: function(idOrder) {
var app = this;
this.showLoading = true;
axios.get(UrlManager.getBaseUrlAbsolute() + "distribution/ajax-force-synchronize-order-tiller", {
params: {
idOrder: idOrder
}
})
.then(function (response) {
app.init(app.idActivePointSale);
});
},
authTiller: function () {

},

+ 5
- 6
common/components/Tiller/TillerClientV2.php View File

@@ -83,11 +83,10 @@ class TillerClientV2 implements TillerClientInterface
public function postOrder($params)
{
return $this->curl->setPostParams(
array_merge([
'provider_token' => $this->providerToken,
'restaurant_token' => $this->restaurantToken,
], $params)
)
->post($this->urlApi . 'orders');
array_merge([
'provider_token' => $this->providerToken,
'restaurant_token' => $this->restaurantToken,
], $params)
)->post($this->urlApi . 'orders');
}
}

+ 123
- 113
domain/Order/Order/TillerManager.php View File

@@ -66,7 +66,6 @@ class TillerManager extends AbstractManager

public function synchronizeDistribution(string $date): array
{
$apiVersion = $this->producerSolver->getConfig('tiller_api_version');
$return = [];

if($this->tillerActivated) {
@@ -80,130 +79,141 @@ class TillerManager extends AbstractManager
'conditions' => OrderRepositoryQuery::getSqlFilterIsValid()
]);

$strDate = date('Y-m-d\T12:i:s+0000', strtotime($date) + 1);
$datetime = new \DateTime(date('Y-m-d 12:i:s', strtotime($date)));

if ($orders && count($orders)) {
foreach ($orders as $order) {
$this->orderBuilder->initOrder($order);
$lines = [];
foreach ($order->productOrder as $productOrder) {
// v3
if($apiVersion == 'v3') {
$amount = round($this->productOrderSolver->getPriceWithTax($productOrder) * 100);

// classique
if(is_int($productOrder->quantity)) {
$quantity = $productOrder->quantity;
}
// vrac
else {
$amount = $amount * $productOrder->quantity;
$quantity = 1;
}

$lines[] = [
'name' => $productOrder->product->name,
'unitPrice' => [
'amount' => $amount,
],
'taxRate' => $productOrder->taxRate->value * 100,
'quantity' => $quantity
];
}
// v2
else {
$lines[] = [
'name' => $productOrder->product->name,
'price' => $this->productOrderSolver->getPriceWithTax($productOrder) * 100 * $productOrder->quantity,
'tax' => $productOrder->taxRate->value * 100,
'date' => $strDate,
'quantity' => $productOrder->quantity
];
}
$res = $this->synchronizeOrder($order);
if($res) {
$return[] = $res;
}
}
}
}
}

$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';
}
return $return;
}

if (!$this->isSynchronized($date, $order->id)) {
// v3
if($apiVersion == 'v3') {

$payments = [];
$amountPayment = round($this->orderSolver->getOrderAmountWithTax($order, Order::AMOUNT_PAID) * 100);
if($amountPayment) {
$payments[] = [
'externalId' => ''.$order->id,
'amount' => $amountPayment
];
}
public function synchronizeOrder(Order $order, bool $force = false)
{
$apiVersion = $this->producerSolver->getConfig('tiller_api_version');
$date = $order->distribution->date;
$strDate = date('Y-m-d\T12:i:s+0000', strtotime($date) + 1);
$datetime = new \DateTime(date('Y-m-d 12:i:s', strtotime($date)));
$this->orderBuilder->initOrder($order);
$lines = [];

$returnTiller = $this->tillerClient->postOrder([
'externalId' => ''.$order->id,
'purchaseRequestType' => 'clickAndCollect',
'date' => $datetime->format(\DateTime::ATOM),
'order' => [
'currencyCode' => 'EUR',
'itemLines' => $lines,
'payments' => $payments
]
]);
}
// v2
else {
$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
]
]
]);
}
foreach ($order->productOrder as $productOrder) {
// v3
if($apiVersion == 'v3') {
$amount = round($this->productOrderSolver->getPriceWithTax($productOrder) * 100);

$returnTillerObject = json_decode($returnTiller);
if($apiVersion == 'v3') {
$order->tiller_external_id = '' . $returnTillerObject->referenceId;
}
else {
$order->tiller_external_id = '' . $returnTillerObject->id;
}
$order->save();
// classique
if(is_int($productOrder->quantity)) {
$quantity = $productOrder->quantity;
}
// vrac
else {
$amount = $amount * $productOrder->quantity;
$quantity = 1;
}

$return[] = $returnTiller;
}
}
$lines[] = [
'name' => $productOrder->product->name,
'unitPrice' => [
'amount' => $amount,
],
'taxRate' => $productOrder->taxRate->value * 100,
'quantity' => $quantity
];
}
// v2
else {
$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) || $force) {
// v3
if($apiVersion == 'v3') {
$payments = [];
$amountPayment = round($this->orderSolver->getOrderAmountWithTax($order, Order::AMOUNT_PAID) * 100);
if($amountPayment) {
$payments[] = [
'externalId' => ''.$order->id,
'amount' => $amountPayment
];
}

$returnTiller = $this->tillerClient->postOrder([
'externalId' => ''.$order->id,
'purchaseRequestType' => 'clickAndCollect',
'date' => $datetime->format(\DateTime::ATOM),
'order' => [
'currencyCode' => 'EUR',
'itemLines' => $lines,
'payments' => $payments
]
]);
}
// v2
else {
$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);
if($apiVersion == 'v3') {
$order->tiller_external_id = '' . $returnTillerObject->referenceId;
}
else {
$order->tiller_external_id = '' . $returnTillerObject->id;
}
$order->save();

return $returnTiller;
}

return $return;
return null;
}

public function isSynchronized($date, $idOrder = null): bool

Loading…
Cancel
Save