Browse Source

[backend] Distributions : bouton de synchronisation avec Tiller

refactoring
Guillaume Bourgeois 5 years ago
parent
commit
5ee37c1f42
11 changed files with 333 additions and 41 deletions
  1. +61
    -1
      backend/controllers/DistributionController.php
  2. +9
    -3
      backend/views/distribution/index.php
  3. +6
    -11
      backend/web/css/screen.css
  4. +13
    -8
      backend/web/js/vuejs/distribution-index.js
  5. +9
    -9
      backend/web/sass/distribution/_index.scss
  6. +109
    -0
      common/helpers/Tiller.php
  7. +2
    -1
      composer.json
  8. +57
    -5
      composer.lock
  9. +1
    -0
      vendor/composer/autoload_psr4.php
  10. +57
    -3
      vendor/composer/installed.json
  11. +9
    -0
      vendor/yiisoft/extensions.php

+ 61
- 1
backend/controllers/DistributionController.php View File

@@ -107,7 +107,8 @@ class DistributionController extends BackendController
$producer = Producer::getCurrent() ;
$json['producer'] = [
'credit' => $producer->credit
'credit' => $producer->credit,
'tiller' => $producer->tiller
];
$distributionsArray = Distribution::searchAll([
@@ -327,6 +328,11 @@ class DistributionController extends BackendController
$json['one_distribution_week_active'] = $oneDistributionWeekActive ;
// tiller
if($producer->tiller) {
$tiller = new Tiller() ;
$json['tiller_is_synchro'] = (int) $tiller->isSynchro($date) ;
}
}
return $json ;
@@ -893,4 +899,58 @@ class DistributionController extends BackendController
return ['success'] ;
}
/**
* Synchronise les commandes avec la plateforme Tiller pour une date donnée.
*
* @param string $date
*/
public function actionAjaxProcessSynchroTiller($date)
{
$producerTiller = Producer::getConfig('tiller') ;
if($producerTiller) {
$tiller = new Tiller() ;
$isSynchro = $tiller->isSynchro($date) ;
if(!$isSynchro) {
$orders = Order::searchAll([
'distribution.date' => $date
]) ;

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

if($orders && count($orders)) {
foreach($orders as $order) {
$lines = [] ;
foreach($order->productOrder as $productOrder) {
$lines[] = [
'name' => $productOrder->product->name,
'price' => $productOrder->price * 100 * $productOrder->quantity,
'tax' => 5.5,
'date' => $strDate,
'quantity' => $productOrder->quantity
] ;
}

$tiller->postOrder([
'externalId' => $order->id,
'type' => 1,
'status' => "CLOSED",
'openDate' => $strDate,
'closeDate' => $strDate,
'lines' => $lines,
'payments' => [[
'type' => 'CASH',
'amount' => $order->getAmount(Order::AMOUNT_TOTAL) * 100,
'status' => 'ACCEPTED',
'date' => $strDate
]]
]) ;
}
}
}
}
}
}

+ 9
- 3
backend/views/distribution/index.php View File

@@ -215,10 +215,16 @@ $this->setPageTitle('Distributions') ;
<div id="orders" class="panel panel-default" v-if="date">
<div class="panel-heading">
<h3 class="panel-title">Commandes <label class="label label-success" v-if="orders.length">{{ orders.length }}</label><label class="label label-danger" v-else>0</label></h3>
<div class="buttons">
<button id="btn-add-order" @click="showModalFormOrderCreate = true" class="btn btn-primary btn-xs"><span class="glyphicon glyphicon-plus"></span> Ajouter une commande</button>
<button id="btn-add-subscriptions" @click="addSubscriptions" class="btn btn-default btn-xs"><span class="glyphicon glyphicon-plus"></span> Importer les abonnements</button>
<template v-if="producer.tiller == true">
<button v-if="tillerIsSynchro" id="btn-tiller" class="btn btn-xs btn-success" disabled><span class="glyphicon glyphicon-refresh"></span> Synchronisé avec Tiller</button>
<button v-else id="btn-tiller" class="btn btn-xs btn-default" @click="synchroTiller"><span class="glyphicon glyphicon-refresh"></span> Synchroniser avec Tiller</button>
</template>
</div>
</div>
<div class="panel-body">
<button id="btn-add-subscriptions" @click="addSubscriptions" class="btn btn-default btn-xs"><span class="glyphicon glyphicon-plus"></span> Importer les abonnements</button>
<button id="btn-add-order" @click="showModalFormOrderCreate = true" class="btn btn-primary btn-xs"><span class="glyphicon glyphicon-plus"></span> Ajouter une commande</button>
<order-form
v-if="showModalFormOrderCreate"
:date="date"
@@ -456,7 +462,7 @@ $this->setPageTitle('Distributions') ;
</div>
<div class="col-md-4">
<div class="form-group">
<a v-if="producer.credit && order.id_user > 0" class="btn btn-xs btn-primary btn-credit" :href="baseUrl+'/user/credit?id='+user.id_user" v-for="user in users" v-if="user.id_user == order.id_user">{{ parseFloat(user.credit).toFixed(2).replace(/(\d)(?=(\d{3})+(?:\.\d+)?$)/g, "$1,")+'&nbsp;€' }}</a>
<a v-if="producer.credit && order.id_user > 0 && user.id_user == order.id_user" class="btn btn-xs btn-primary btn-credit" :href="baseUrl+'/user/credit?id='+user.id_user" v-for="user in users">{{ parseFloat(user.credit).toFixed(2).replace(/(\d)(?=(\d{3})+(?:\.\d+)?$)/g, "$1,")+'&nbsp;€' }}</a>
<label class="control-label" for="select-id-user">
Utilisateur
</label>

+ 6
- 11
backend/web/css/screen.css View File

@@ -1952,17 +1952,12 @@ termes.
.distribution-index #orders {
position: relative;
}
/* line 167, ../sass/distribution/_index.scss */
.distribution-index #orders #btn-add-order {
position: absolute;
top: 9px;
right: 10px;
}
/* line 173, ../sass/distribution/_index.scss */
.distribution-index #orders #btn-add-subscriptions {
position: absolute;
top: 9px;
right: 180px;
/* line 170, ../sass/distribution/_index.scss */
.distribution-index #orders .panel-heading .buttons .btn {
position: relative;
top: -19px;
float: right;
margin-left: 10px;
}
/* line 179, ../sass/distribution/_index.scss */
.distribution-index #orders #wrapper-nav-points-sale {

+ 13
- 8
backend/web/js/vuejs/distribution-index.js View File

@@ -60,20 +60,16 @@ var app = new Vue({
users: [],
showModalProducts: false,
showModalPointsSale: false,
showModalFormOrderCreate: false,
orderCreate: null,
showModalFormOrderUpdate: false,
idOrderUpdate: 0,
showViewProduct: false,
idOrderView: 0,
showModalPayment: false,
idOrderPayment: 0,
showLoading: false,
tillerIsSynchro: false,
calendar: {
mode: 'single',
attrs: [],
@@ -187,6 +183,8 @@ var app = new Vue({
app.users = response.data.users ;
}
app.tillerIsSynchro = response.data.tiller_is_synchro ;
app.calendar.attrs = [] ;
var distributions = response.data.distributions ;
if(distributions.length) {
@@ -402,6 +400,16 @@ var app = new Vue({
.then(function(response) {
app.init(app.idActivePointSale) ;
}) ;
},
synchroTiller: function() {
var app = this ;
this.showLoading = true ;
axios.get(UrlManager.getBaseUrlAbsolute()+"distribution/ajax-process-synchro-tiller",{params: {
date: this.getDate()
}})
.then(function(response) {
app.init(app.idActivePointSale) ;
}) ;
}
},
});
@@ -419,7 +427,6 @@ Vue.component('order-form',{
idUser: 0,
username : '',
comment: '',
producer: null,
baseUrl: $('meta[name=baseurl]').attr('content'),
} ;
},
@@ -452,8 +459,6 @@ Vue.component('order-form',{
if(!countProducts) {
this.errors.push('Veuillez sélectionner au moins un produit') ;
}
console.log('id_user : '+this.order.id_user+' / username : '+this.order.username) ;
},
submitFormCreate: function(event) {
var app = this ;

+ 9
- 9
backend/web/sass/distribution/_index.scss View File

@@ -164,16 +164,16 @@ termes.
#orders {
position: relative ;
#btn-add-order {
position: absolute ;
top: 9px ;
right: 10px ;
}
#btn-add-subscriptions {
position: absolute ;
top: 9px ;
right: 180px ;
.panel-heading {
.buttons {
.btn {
position: relative ;
top: -19px ;
float: right ;
margin-left: 10px ;
}
}
}
#wrapper-nav-points-sale {

+ 109
- 0
common/helpers/Tiller.php View File

@@ -0,0 +1,109 @@
<?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 linslin\yii2\curl;

class Tiller
{
var $curl ;
var $provider_token ;
var $restaurant_token ;
var $url_api = 'https://developers.tillersystems.com/api/' ;
public function __construct()
{
$this->curl = new curl\Curl();
$this->provider_token = Producer::getConfig('tiller_provider_token') ;
$this->restaurant_token = Producer::getConfig('tiller_restaurant_token') ;
}
public function getOrders($date)
{
$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' => 'CLOSED',
])->get($this->url_api.'orders');
return json_decode($orders) ;
}
public function isSynchro($date)
{
$ordersTiller = $this->getOrders($date) ;
$ordersOpendistrib = Order::searchAll([
'distribution.date' => $date
]) ;
$ordersOpendistribSynchro = [] ;
if($ordersOpendistrib) {
foreach($ordersOpendistrib as $orderOpendistrib) {
$ordersOpendistribSynchro[$orderOpendistrib->id] = false ;
foreach($ordersTiller->orders as $orderTiller) {
if($orderOpendistrib->id == $orderTiller->externalId
&& ($orderOpendistrib->getAmount(Order::AMOUNT_TOTAL) * 100) == $orderTiller->currentBill) {

$ordersOpendistribSynchro[$orderOpendistrib->id] = true ;
}
}
}
}
foreach($ordersOpendistribSynchro as $idOrder => $isSynchro) {
if(!$isSynchro) {
return false ;
}
}
return true ;
}
public function postOrder($params)
{
return $this->curl->setPostParams(array_merge([
'provider_token' => $this->provider_token,
'restaurant_token' => $this->restaurant_token,
], $params))
->post($this->url_api.'orders') ;
}
}

+ 2
- 1
composer.json View File

@@ -25,7 +25,8 @@
"yurkinx/yii2-image": "dev-master",
"dmstr/yii2-adminlte-asset": "^2.1",
"payplug/payplug-php": "^2.5",
"mailjet/mailjet-apiv3-php": "^1.4"
"mailjet/mailjet-apiv3-php": "^1.4",
"linslin/yii2-curl": "*"
},
"require-dev": {
"yiisoft/yii2-codeception": "*",

+ 57
- 5
composer.lock View File

@@ -4,8 +4,8 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "2a564b71f3dac99e36518f58a67f4245",
"content-hash": "49af3d5f9b5f0cb2ef34661e33f27903",
"hash": "8cb0b3a549f436550d88d1d127bba13c",
"content-hash": "d61ba662c69c68021c4ffa9989d25774",
"packages": [
{
"name": "2amigos/yii2-chartjs-widget",
@@ -132,12 +132,12 @@
"version": "v2.4.8",
"source": {
"type": "git",
"url": "https://github.com/almasaeed2010/AdminLTE.git",
"url": "https://github.com/ColorlibHQ/AdminLTE.git",
"reference": "d9e68301848a95dff2e2dbef6569e617a9b3fa30"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/almasaeed2010/AdminLTE/zipball/d9e68301848a95dff2e2dbef6569e617a9b3fa30",
"url": "https://api.github.com/repos/ColorlibHQ/AdminLTE/zipball/d9e68301848a95dff2e2dbef6569e617a9b3fa30",
"reference": "d9e68301848a95dff2e2dbef6569e617a9b3fa30",
"shasum": ""
},
@@ -1027,7 +1027,7 @@
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/kartik-v/yii2-mpdf/zipball/beecfa00d5debfef1ccac6ff7b1abe8560b6ffd1",
"url": "https://api.github.com/repos/kartik-v/yii2-mpdf/zipball/e513d17e8b1a5e063475f332e726845dba8f32b5",
"reference": "17fda1cb084c58fee6014026d35af5faf73316ef",
"shasum": ""
},
@@ -1069,6 +1069,58 @@
],
"time": "2017-01-14 11:51:12"
},
{
"name": "linslin/yii2-curl",
"version": "1.3.0",
"source": {
"type": "git",
"url": "https://github.com/linslin/Yii2-Curl.git",
"reference": "734c40b520729c79af2937283864f8c999e86879"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/linslin/Yii2-Curl/zipball/734c40b520729c79af2937283864f8c999e86879",
"reference": "734c40b520729c79af2937283864f8c999e86879",
"shasum": ""
},
"require": {
"ext-curl": "*",
"ext-json": "*",
"yiisoft/yii2": "*"
},
"require-dev": {
"codeception/base": "^2.2.3",
"codeception/specify": "~0.4.3",
"codeception/verify": "~0.3.1",
"codeclimate/php-test-reporter": "dev-master",
"guzzlehttp/guzzle": ">=4.1.4 <7.0",
"mcustiel/phiremock-codeception-extension": "1.2.4"
},
"type": "yii2-extension",
"autoload": {
"psr-4": {
"linslin\\yii2\\curl\\": ""
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nils Gajsek",
"email": "info@linslin.org"
}
],
"description": "Easy and nice cURL extension with RESTful support for Yii2",
"keywords": [
" curl",
"extension",
"restful",
"yii2"
],
"time": "2019-02-25 10:56:34"
},
{
"name": "mailjet/mailjet-apiv3-php",
"version": "v1.4.1",

+ 1
- 0
vendor/composer/autoload_psr4.php View File

@@ -16,6 +16,7 @@ return array(
'yii\\' => array($vendorDir . '/yiisoft/yii2'),
'rmrevin\\yii\\fontawesome\\' => array($vendorDir . '/rmrevin/yii2-fontawesome'),
'phpDocumentor\\Reflection\\' => array($vendorDir . '/phpdocumentor/reflection-common/src', $vendorDir . '/phpdocumentor/type-resolver/src', $vendorDir . '/phpdocumentor/reflection-docblock/src'),
'linslin\\yii2\\curl\\' => array($vendorDir . '/linslin/yii2-curl'),
'kartik\\mpdf\\' => array($vendorDir . '/kartik-v/yii2-mpdf'),
'dosamigos\\leaflet\\' => array($vendorDir . '/2amigos/yii2-leaflet-extension/src'),
'dosamigos\\chartjs\\' => array($vendorDir . '/2amigos/yii2-chartjs-widget/src'),

+ 57
- 3
vendor/composer/installed.json View File

@@ -1325,7 +1325,7 @@
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/kartik-v/yii2-mpdf/zipball/beecfa00d5debfef1ccac6ff7b1abe8560b6ffd1",
"url": "https://api.github.com/repos/kartik-v/yii2-mpdf/zipball/e513d17e8b1a5e063475f332e726845dba8f32b5",
"reference": "17fda1cb084c58fee6014026d35af5faf73316ef",
"shasum": ""
},
@@ -4206,12 +4206,12 @@
"version_normalized": "2.4.8.0",
"source": {
"type": "git",
"url": "https://github.com/almasaeed2010/AdminLTE.git",
"url": "https://github.com/ColorlibHQ/AdminLTE.git",
"reference": "d9e68301848a95dff2e2dbef6569e617a9b3fa30"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/almasaeed2010/AdminLTE/zipball/d9e68301848a95dff2e2dbef6569e617a9b3fa30",
"url": "https://api.github.com/repos/ColorlibHQ/AdminLTE/zipball/d9e68301848a95dff2e2dbef6569e617a9b3fa30",
"reference": "d9e68301848a95dff2e2dbef6569e617a9b3fa30",
"shasum": ""
},
@@ -4452,5 +4452,59 @@
"php",
"v3"
]
},
{
"name": "linslin/yii2-curl",
"version": "1.3.0",
"version_normalized": "1.3.0.0",
"source": {
"type": "git",
"url": "https://github.com/linslin/Yii2-Curl.git",
"reference": "734c40b520729c79af2937283864f8c999e86879"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/linslin/Yii2-Curl/zipball/734c40b520729c79af2937283864f8c999e86879",
"reference": "734c40b520729c79af2937283864f8c999e86879",
"shasum": ""
},
"require": {
"ext-curl": "*",
"ext-json": "*",
"yiisoft/yii2": "*"
},
"require-dev": {
"codeception/base": "^2.2.3",
"codeception/specify": "~0.4.3",
"codeception/verify": "~0.3.1",
"codeclimate/php-test-reporter": "dev-master",
"guzzlehttp/guzzle": ">=4.1.4 <7.0",
"mcustiel/phiremock-codeception-extension": "1.2.4"
},
"time": "2019-02-25 10:56:34",
"type": "yii2-extension",
"installation-source": "dist",
"autoload": {
"psr-4": {
"linslin\\yii2\\curl\\": ""
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nils Gajsek",
"email": "info@linslin.org"
}
],
"description": "Easy and nice cURL extension with RESTful support for Yii2",
"keywords": [
" curl",
"extension",
"restful",
"yii2"
]
}
]

+ 9
- 0
vendor/yiisoft/extensions.php View File

@@ -129,4 +129,13 @@ return array (
'@dmstr' => $vendorDir . '/dmstr/yii2-adminlte-asset',
),
),
'linslin/yii2-curl' =>
array (
'name' => 'linslin/yii2-curl',
'version' => '1.3.0.0',
'alias' =>
array (
'@linslin/yii2/curl' => $vendorDir . '/linslin/yii2-curl',
),
),
);

Loading…
Cancel
Save