浏览代码

[Administration] Producteurs : création facture Dolibarr et initialisation Alwaysdata via ajax #1298

feature/souke
Guillaume Bourgeois 1年前
父节点
当前提交
87e695d2de
共有 5 个文件被更改,包括 184 次插入219 次删除
  1. +7
    -12
      backend/controllers/ProducerAdminController.php
  2. +142
    -195
      backend/views/producer-admin/index.php
  3. +14
    -1
      backend/web/js/backend.js
  4. +18
    -11
      common/components/AlwaysdataClient.php
  5. +3
    -0
      common/config/main.php

+ 7
- 12
backend/controllers/ProducerAdminController.php 查看文件

@@ -38,19 +38,18 @@

namespace backend\controllers;

use common\helpers\Ajax;
use common\helpers\Alwaysdata;
use common\logic\Order\Order\Model\Order;
use common\logic\Producer\Producer\Model\Producer;
use common\logic\Product\Product\Model\Product;
use Yii;
use yii\helpers\Html;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use yii\data\ActiveDataProvider;

/**
* UserController implements the CRUD actions for User model.
*/
class ProducerAdminController extends BackendController
{
public function behaviors()
@@ -160,14 +159,12 @@ class ProducerAdminController extends BackendController
$producer = $this->findProducer($id);

if($producer->contact_email) {
Alwaysdata::createProducerSiteShortUrlRedirection($producer);
Alwaysdata::createProducerEmailRedirection($producer);
Yii::$app->alwaysdataClient->initRedirections($producer);
return Ajax::responseSuccess('Redirections initialisées chez Alwaysdata');
}
else {
$this->addFlash('error', "L'adresse email de contact du producteur n'est pas définie.");
return Ajax::responseError("L'adresse email de contact du producteur n'est pas définie");
}

return $this->redirect(['index']);
}

public function actionDolibarr(int $id)
@@ -176,13 +173,11 @@ class ProducerAdminController extends BackendController
$producer = $this->findProducer($id);
if($producer->dolibarr_socid) {
$producerManager->generateDolibarrProducerInvoice($producer);
$this->setFlash('success', "Facture générée sur Dolibarr pour le producteur <strong>".$producer->name."</strong>");
return Ajax::responseSuccess("Facture générée sur Dolibarr pour le producteur \"".Html::encode($producer->name)."\"");
}
else {
$this->setFlash('error', "Dolibarr : l'id user du producteur doit être défini");
return Ajax::responseError("Dolibarr : l'id user du producteur doit être défini");
}

return $this->redirect(['index']);
}

public function actionUserTransfer($fromProducerId, $toProducerId, $withOrders = 1)

+ 142
- 195
backend/views/producer-admin/index.php 查看文件

@@ -49,220 +49,167 @@ $this->addBreadcrumb($this->getTitle()) ;
$this->addButton(['label' => 'Nouveau producteur <span class="glyphicon glyphicon-plus"></span>', 'url' => 'producer-admin/create', 'class' => 'btn btn-primary']) ;

?>
<div class="producer-admin-index">

<div class="alert alert-info">
Facturé le mois dernier : <strong><?= $sumPrices ?> €</strong><br />
</div>

<?= GridView::widget([
'dataProvider' => $dataProviderProducer,
'columns' => [
[
'attribute' => 'active',
'format' => 'raw',
'value' => function($model) {
$html = '' ;
if($model->active) {
$html .= '<span class="label label-success">En ligne</span>' ;
}
else {
$html .= '<span class="label label-danger">Hors-ligne</span>' ;
}

<div class="alert alert-info">
Facturé le mois dernier : <strong><?= $sumPrices ?> €</strong><br />
</div>

<?= GridView::widget([
'dataProvider' => $dataProviderProducer,
'columns' => [
[
'attribute' => 'active',
'format' => 'raw',
'value' => function($model) {
$html = '' ;
if($model->active) {
$html .= '<span class="label label-success">En ligne</span>' ;
}
else {
$html .= '<span class="label label-danger">Hors-ligne</span>' ;
}

if(strlen($model->code))
{
$html .= ' <span class="glyphicon glyphicon-lock" data-toggle="tooltip" data-placement="bottom" data-original-title="'.Html::encode($model->code).'"></span>' ;
}

return $html ;
}
],
'name',
/*[
'attribute' => 'date_creation',
'format' => 'raw',
'value' => function($model) {
return date('d/m/Y', strtotime($model->date_creation)) ;
}
],*/
/*[
'attribute' => 'Lieu',
'format' => 'raw',
'value' => function($model) {
return Html::encode($model->city.' ('.$model->postcode.')') ;
}
],*/
/*[
'attribute' => 'Utilisateurs',
'format' => 'raw',
'value' => function($model) {
if(!$model->userProducer || !count($model->userProducer))
{
return 'Aucun utilisateur' ;
}
else {
$users = count($model->userProducer).' client' ;
if(count($model->userProducer) > 1) {
$users .= 's' ;
if(strlen($model->code))
{
$html .= ' <span class="glyphicon glyphicon-lock" data-toggle="tooltip" data-placement="bottom" data-original-title="'.Html::encode($model->code).'"></span>' ;
}
return $users ;
}
}
],*/
[
'attribute' => 'Contact',
'format' => 'raw',
'value' => function($model) {
if(!isset($model->user) || (isset($model->user) && count($model->user) == 0))
{
return 'Aucun contact' ;

return $html ;
}
else {
foreach($model->user as $u)
],
'name',
[
'attribute' => 'Contact',
'format' => 'raw',
'value' => function($model) {
if(!isset($model->user) || (isset($model->user) && count($model->user) == 0))
{
if($u->status == User::STATUS_PRODUCER)
return 'Aucun contact' ;
}
else {
foreach($model->user as $u)
{
return Html::encode($u->lastname.' '.$u->name)
.'<br />'.Html::encode($u->email)
.'<br />'.Html::encode($u->phone) ;
if($u->status == User::STATUS_PRODUCER)
{
return Html::encode($u->lastname.' '.$u->name)
.'<br />'.Html::encode($u->email)
.'<br />'.Html::encode($u->phone) ;
}
}
}
}
}
],
/*[
'attribute' => 'Prix libre',
'label' => 'Prix libre',
'format' => 'raw',
'value' => function($model) {
if(is_null($model->free_price)) {
return '' ;
}
else {
$str = '';
if($model->isBillingTypeFreePrice()) {
$str .= '<strong>';
}

$str .= $model->getFreePrice();

if($model->isBillingTypeFreePrice()) {
$str .= '</strong>';
}

return $str;
}
}
],*/
[
'label' => 'Facturation',
'format' => 'raw',
'value' => function($producer) use ($producerManager) {

$str = '';
$str .= '<ul style="margin: 0px;padding-left: 15px;">';
],
[
'label' => 'Facturation',
'format' => 'raw',
'value' => function($producer) use ($producerManager) {

$isBillingFrequencyMonthly = $producerManager->isBillingFrequencyMonthly($producer);
$summaryMonthly = $producerManager->getSummaryAmountsToBeBilled($producer, 'Mois dernier', 1);
$isBillingFrequencyQuaterly = $producerManager->isBillingFrequencyQuarterly($producer);
$summaryQuaterly = $producerManager->getSummaryAmountsToBeBilled($producer, '3 derniers mois', 3);
$isBillingFrequencyBiannual = $producerManager->isBillingFrequencyBiannual($producer);
$summaryBiannual = $producerManager->getSummaryAmountsToBeBilled($producer, '6 derniers mois', 6);
$str = '';
$str .= '<ul style="margin: 0px;padding-left: 15px;">';

$isBillingFrequencyMonthly = $producerManager->isBillingFrequencyMonthly($producer);
$summaryMonthly = $producerManager->getSummaryAmountsToBeBilled($producer, 'Mois dernier', 1);
$isBillingFrequencyQuaterly = $producerManager->isBillingFrequencyQuarterly($producer);
$summaryQuaterly = $producerManager->getSummaryAmountsToBeBilled($producer, '3 derniers mois', 3);
$isBillingFrequencyBiannual = $producerManager->isBillingFrequencyBiannual($producer);
$summaryBiannual = $producerManager->getSummaryAmountsToBeBilled($producer, '6 derniers mois', 6);

if(($isBillingFrequencyMonthly && $summaryMonthly)
|| ($isBillingFrequencyQuaterly && $summaryQuaterly)
|| ($isBillingFrequencyBiannual && $summaryBiannual)) {

$str .= '<li>';
if ($isBillingFrequencyMonthly && $summaryMonthly) {
$str .= $summaryMonthly;
} elseif ($isBillingFrequencyQuaterly && $summaryQuaterly) {
$str .= $summaryQuaterly;
} elseif ($isBillingFrequencyBiannual && $summaryBiannual) {
$str .= $summaryBiannual;
}

if(($isBillingFrequencyMonthly && $summaryMonthly)
|| ($isBillingFrequencyQuaterly && $summaryQuaterly)
|| ($isBillingFrequencyBiannual && $summaryBiannual)) {
if ($producer->option_billing_reduction && strlen($str)) {
$str .= '<br /><u>Avec réduction</u> : ';
if ($producer->option_billing_reduction_percentage) {
$str .= ' ' . $producer->option_billing_reduction_percentage . '&nbsp;%';
}
}
$str .= '</li>';

$str .= '<li>';
if ($isBillingFrequencyMonthly && $summaryMonthly) {
$str .= $summaryMonthly;
} elseif ($isBillingFrequencyQuaterly && $summaryQuaterly) {
$str .= $summaryQuaterly;
} elseif ($isBillingFrequencyBiannual && $summaryBiannual) {
$str .= $summaryBiannual;
$str .= '<li>Facturation '.strtolower(Producer::$billingFrequencyArray[$producer->option_billing_frequency]).'</li>';
}

if ($producer->option_billing_reduction && strlen($str)) {
$str .= '<br /><u>Avec réduction</u> : ';
if ($producer->option_billing_reduction_percentage) {
$str .= ' ' . $producer->option_billing_reduction_percentage . '&nbsp;%';
}
if($producer->option_billing_permanent_transfer) {
$str .= '<li>Virement permanent : <strong>'.Price::format($producer->option_billing_permanent_transfer_amount).'</strong></li>';
}
$str .= '</li>';

$str .= '<li>Facturation '.strtolower(Producer::$billingFrequencyArray[$producer->option_billing_frequency]).'</li>';
}
$str .= '</ul>';

if($producer->option_billing_permanent_transfer) {
$str .= '<li>Virement permanent : <strong>'.Price::format($producer->option_billing_permanent_transfer_amount).'</strong></li>';
return $str;
}
],
[
'attribute' => 'admin_comment',
'label' => 'Commentaire',
'format' => 'raw',
'value' => function($producer) {
if($producer->admin_comment) {
return $producer->admin_comment;
}

$str .= '</ul>';

return $str;
}
],
[
'attribute' => 'admin_comment',
'label' => 'Commentaire',
'format' => 'raw',
'value' => function($producer) {
if($producer->admin_comment) {
return $producer->admin_comment;
return '';
}

return '';
}
],
[
'class' => 'yii\grid\ActionColumn',
'template' => '{update} {dolibarr} {billing} {alwaysdata}',
'headerOptions' => ['class' => 'column-actions'],
'contentOptions' => ['class' => 'column-actions'],
'buttons' => [
'update' => function ($url, $model) {
return Html::a(
'<span class="glyphicon glyphicon-pencil"></span>',
$url,
[
'title' => 'Modifier',
'class' => 'btn btn-default'
]
);
},
'dolibarr' => function ($url, $model) {
return Html::a(
'<span class="glyphicon glyphicon-paste"></span>',
$url,
[
'title' => 'Générer la facture sur Dolibarr',
'class' => 'btn btn-default'
]
);
},
'billing' => function ($url, $model) {
return Html::a(
'<span class="glyphicon glyphicon-euro"></span>',
$url,
[
'title' => 'Facturation',
'class' => 'btn btn-default'
]
);
},
'alwaysdata' => function ($url, $model) {
return Html::a(
'<span class="glyphicon glyphicon-road"></span>',
$url,
[
'title' => 'Redirections Alwaysdata (email et url courte)',
'class' => 'btn btn-default'
]
);
},
],
[
'class' => 'yii\grid\ActionColumn',
'template' => '{update} {billing} {dolibarr} {alwaysdata}',
'headerOptions' => ['class' => 'column-actions'],
'contentOptions' => ['class' => 'column-actions'],
'buttons' => [
'update' => function ($url, $model) {
return Html::a(
'<span class="glyphicon glyphicon-pencil"></span>',
$url,
[
'title' => 'Modifier',
'class' => 'btn btn-default'
]
);
},
'dolibarr' => function ($url, $model) {
return Html::a(
'<span class="glyphicon glyphicon-paste"></span>',
$url,
[
'title' => 'Générer la facture sur Dolibarr',
'class' => 'btn btn-default btn-dolibarr'
]
);
},
'billing' => function ($url, $model) {
return Html::a(
'<span class="glyphicon glyphicon-euro"></span>',
$url,
[
'title' => 'Facturation',
'class' => 'btn btn-default'
]
);
},
'alwaysdata' => function ($url, $model) {
return Html::a(
'<span class="glyphicon glyphicon-road"></span>',
$url,
[
'title' => 'Redirections Alwaysdata (email et url courte)',
'class' => 'btn btn-default btn-alwaysdata'
]
);
},
],
],
],
],
]); ?>
]); ?>

</div>

+ 14
- 1
backend/web/js/backend.js 查看文件

@@ -52,6 +52,7 @@ $(document).ready(function () {
opendistrib_select2();
opendistrib_dropdown_producers();
opendistrib_gridview_pagesize();
opendistrib_producers_admin();
});

var UrlManager = {
@@ -64,13 +65,25 @@ var UrlManager = {
}
};

function opendistrib_producers_admin() {
$('.producer-admin-index .btn-alwaysdata, .producer-admin-index .btn-dolibarr').click(function() {
var $button = $(this);
$button.attr('disabled', 'disabled');
axios.get($button.attr('href'), {})
.then(function (response) {
appAlerts.alertResponse(response);
$button.removeAttr('disabled');
});
return false;
});
}

function opendistrib_gridview_pagesize() {
$('.gridview-pagesize select').change(function() {
$(this).parent().parent().submit();
});
}


function opendistrib_dropdown_producers() {

$('.producer-menu .dropdown-toggle').click(function() {

common/helpers/Alwaysdata.php → common/components/AlwaysdataClient.php 查看文件

@@ -1,16 +1,23 @@
<?php

namespace common\helpers;
namespace common\components;

use common\helpers\Password;
use common\logic\Producer\Producer\Model\Producer;
use GuzzleHttp\Client;
use yii\base\ErrorException;

class Alwaysdata
class AlwaysdataClient
{
public static function createProducerEmailRedirection(Producer $producer)
public function initRedirections(Producer $producer)
{
return self::post('mailbox',[
$this->createProducerSiteShortUrlRedirection($producer);
$this->createProducerEmailRedirection($producer);
}

public function createProducerEmailRedirection(Producer $producer)
{
return $this->post('mailbox',[
'domain' => \Yii::$app->parameterBag->get('alwaysdataDomainOpendistribId'),
'name' => $producer->slug,
'password' => Password::generate(13),
@@ -19,9 +26,9 @@ class Alwaysdata
]);
}

public static function createProducerSiteShortUrlRedirection(Producer $producer)
public function createProducerSiteShortUrlRedirection(Producer $producer)
{
return self::post('site', [
return $this->post('site', [
'addresses' => [$producer->slug.'.opendistrib.net'],
'type' => 'redirect',
'url' => 'https://producteurs.opendistrib.net/'.$producer->slug,
@@ -30,18 +37,18 @@ class Alwaysdata
]);
}

private static function post(string $resource, array $data)
private function post(string $resource, array $data)
{
$client = self::getClient($resource);
$client = $this->getClient($resource);

return $client->request('POST', '', [
'body' => json_encode($data)
]);
}

private static function getClient(string $resource)
private function getClient(string $resource)
{
self::checkConfiguration();
$this->checkConfiguration();

return new Client([
'base_uri' => \Yii::$app->parameterBag->get('alwaysdataApiUrl').$resource.'/',
@@ -49,7 +56,7 @@ class Alwaysdata
]);
}

private static function checkConfiguration()
private function checkConfiguration()
{
if(!\Yii::$app->parameterBag->has('alwaysdataApiUrl')
|| !\Yii::$app->parameterBag->has('alwaysdataAccount')

+ 3
- 0
common/config/main.php 查看文件

@@ -69,6 +69,9 @@ return [
'pageSizer' => [
'class' => 'common\components\PageSizer'
],
'alwaysdataClient' => [
'class' => 'common\components\AlwaysdataClient'
],
'assetManager' => [
'linkAssets' => YII_ENV == "dev" ? 'true' : false,
],

正在加载...
取消
保存