@@ -826,14 +826,15 @@ class OrderController extends BackendController | |||
* Crée une commande via une requête AJAX. | |||
* | |||
* @param string $date | |||
* @param integer $id_pv | |||
* @param integer $id_user | |||
* @param integer $idPointSale | |||
* @param integer $idUser | |||
* @param string $username | |||
* @param array $produits | |||
* @param string $commentaire | |||
* @param string $processCredit | |||
*/ | |||
public function actionAjaxCreate( | |||
$date, $idPointSale, $idUser, $username, $products, $comment) | |||
$date, $idPointSale, $idUser, $username, $products, $comment, $processCredit = 0) | |||
{ | |||
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; | |||
@@ -886,6 +887,11 @@ class OrderController extends BackendController | |||
$productOrder->save(); | |||
} | |||
} | |||
$order = Order::searchOne(['id' => $order->id]) ; | |||
if($order && $processCredit) { | |||
$order->processCredit() ; | |||
} | |||
} | |||
return ['success'] ; | |||
@@ -900,7 +906,7 @@ class OrderController extends BackendController | |||
* @param string $comment | |||
*/ | |||
public function actionAjaxUpdate( | |||
$date, $idOrder, $idPointSale, $idUser, $username, $products, $comment) | |||
$date, $idOrder, $idPointSale, $idUser, $username, $products, $comment, $processCredit = 0) | |||
{ | |||
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; | |||
@@ -962,6 +968,11 @@ class OrderController extends BackendController | |||
} | |||
$order->save(); | |||
$order = Order::searchOne(['id' => $order->id]) ; | |||
if($order && $processCredit) { | |||
$order->processCredit() ; | |||
} | |||
} | |||
} | |||
@@ -40,6 +40,7 @@ namespace backend\controllers; | |||
use common\models\User ; | |||
use common\models\Producer ; | |||
use common\models\Distribution ; | |||
use backend\models\MailForm ; | |||
/** | |||
@@ -249,13 +250,20 @@ class UserController extends BackendController | |||
return $this->redirect(['mail','idPointSale' => $idPointSale]); | |||
} | |||
$incomingDistributions = Distribution::getIncomingDistributions() ; | |||
$incomingDistributionsArray = ['0' => '--'] ; | |||
foreach($incomingDistributions as $distribution) { | |||
$incomingDistributionsArray[$distribution->id] = strftime('%A %d %B %Y', strtotime($distribution->date)) ; | |||
} | |||
return $this->render('emails', [ | |||
'usersArray' => $usersArray, | |||
'pointsSaleArray' => $pointsSaleArray, | |||
'pointSale' => $pointSale, | |||
'mailForm' => $mailForm, | |||
'idPointSaleActive' => $idPointSale | |||
'idPointSaleActive' => $idPointSale, | |||
'incomingDistributionsArray' => $incomingDistributionsArray, | |||
]); | |||
} | |||
@@ -40,6 +40,7 @@ namespace backend\models; | |||
use Yii; | |||
use yii\base\Model; | |||
use common\helpers\Price ; | |||
/** | |||
* ContactForm is the model behind the contact form. | |||
@@ -47,6 +48,7 @@ use yii\base\Model; | |||
class MailForm extends Model | |||
{ | |||
public $id_distribution ; | |||
public $subject; | |||
public $message; | |||
@@ -57,6 +59,7 @@ class MailForm extends Model | |||
{ | |||
return [ | |||
[['subject', 'message'], 'required', 'message' => 'Champs obligatoire'], | |||
[['id_distribution'],'integer'] | |||
]; | |||
} | |||
@@ -68,6 +71,7 @@ class MailForm extends Model | |||
return [ | |||
'subject' => 'Sujet', | |||
'message' => 'Message', | |||
'id_distribution' => 'Distribution' | |||
]; | |||
} | |||
@@ -90,6 +94,63 @@ class MailForm extends Model | |||
$body = ['Messages' => []] ; | |||
$messageAutoText = '' ; | |||
$messageAutoHtml = '' ; | |||
if($this->id_distribution) { | |||
$messageAutoText = ' | |||
' ; | |||
$messageAutoHtml = '<br /><br />' ; | |||
$distribution = Distribution::searchOne(['id' => $this->id_distribution]) ; | |||
if($distribution) { | |||
$linkOrder = Yii::$app->urlManagerProducer->createAbsoluteUrl(['order/order','slug_producer' => Producer::getCurrent()->slug, 'date' => $distribution->date]) ; | |||
$dateOrder = strftime('%A %d %B %Y', strtotime($distribution->date)) ; | |||
$messageAutoHtml .= '<a href="'.$linkOrder.'">Passer ma commande du '.$dateOrder.'</a>' ; | |||
$messageAutoText .= 'Suivez ce lien pour passer votre commande du '.$dateOrder.' : | |||
'.$linkOrder ; | |||
$productsArray = Product::find() | |||
->where([ | |||
'id_producer' => Producer::getId(), | |||
]) | |||
->innerJoinWith(['productDistribution' => function($query) use($distribution) { | |||
$query->andOnCondition([ | |||
'product_distribution.id_distribution' => $distribution->id, | |||
'product_distribution.active' => 1 | |||
]); | |||
}]) | |||
->orderBy('product.name ASC') | |||
->all(); | |||
if(count($productsArray) > 1) { | |||
$messageAutoHtml .= '<br /><br />Produits disponibles : <br /><ul>' ; | |||
$messageAutoText .= ' | |||
Produits disponibles : | |||
' ; | |||
foreach($productsArray as $product) { | |||
$productDescription = $product->name ; | |||
if(strlen($product->description)) { | |||
$productDescription .= ' / '.$product->description ; | |||
} | |||
if($product->price) { | |||
$productDescription .= ' / '.Price::format($product->price) ; | |||
} | |||
$messageAutoText .= '- '.$productDescription.' | |||
' ; | |||
$messageAutoHtml .= '<li>'.Html::encode($productDescription).'</li>' ; | |||
} | |||
$messageAutoHtml .= '</ul>' ; | |||
} | |||
} | |||
} | |||
foreach($usersArray as $user) { | |||
$body['Messages'][] = [ | |||
'From' => [ | |||
@@ -103,8 +164,8 @@ class MailForm extends Model | |||
] | |||
], | |||
'Subject' => $this->subject, | |||
'TextPart' => $this->message, | |||
'HTMLPart' => nl2br($this->message) | |||
'TextPart' => $this->message.$messageAutoText, | |||
'HTMLPart' => nl2br($this->message).$messageAutoHtml | |||
] ; | |||
} | |||
@@ -486,6 +486,9 @@ $this->setPageTitle('Distributions') ; | |||
</div> | |||
<div slot="footer"> | |||
<div class="actions-form"> | |||
<button class="modal-default-button btn btn-primary" @click="submitFormCreate" v-if="!order.id && order.id_user" data-process-credit="1">Créer et payer</button> | |||
<button class="modal-default-button btn btn-primary" @click="submitFormUpdate" v-if="order.id && order.id_user" data-process-credit="1">Modifier et payer</button> | |||
<button class="modal-default-button btn btn-primary" @click="submitFormUpdate" v-if="order.id">Modifier</button> | |||
<button class="modal-default-button btn btn-primary" @click="submitFormCreate" v-else>Créer</button> | |||
@@ -64,6 +64,8 @@ $this->render('_menu',[ | |||
</div> | |||
<div class="panel-body"> | |||
<?php $form = ActiveForm::begin(); ?> | |||
<?= $form->field($mailForm, 'id_distribution')->dropDownList($incomingDistributionsArray) | |||
->hint("Sélectionnez une distribution pour ajouter automatiquement au message :<br /><ul><li>un lien vers la prise de commande de cette distribution</li><li>la liste des produits disponibles pour cette distribution</li></ul>"); ?> | |||
<?= $form->field($mailForm, 'subject')->textInput() ; ?> | |||
<?= $form->field($mailForm, 'message')->textarea(['rows' => '15']) ; ?> | |||
<div class="form-group"> |
@@ -396,9 +396,10 @@ Vue.component('order-form',{ | |||
} | |||
}, | |||
submitFormCreate: function() { | |||
submitFormCreate: function(event) { | |||
var app = this ; | |||
if(this.checkForm()) { | |||
var processCredit = event.currentTarget.getAttribute('data-process-credit') ; | |||
axios.get(UrlManager.getBaseUrlAbsolute()+"order/ajax-create",{params: { | |||
date: this.date.getFullYear() + '-' | |||
+ ('0' + (this.date.getMonth() +1)).slice(-2) + '-' | |||
@@ -407,7 +408,8 @@ Vue.component('order-form',{ | |||
idUser: this.order.id_user, | |||
username: this.order.username, | |||
products: JSON.stringify(this.order.productOrder), | |||
comment: this.order.comment | |||
comment: this.order.comment, | |||
processCredit: processCredit | |||
}}) | |||
.then(function(response) { | |||
app.order.id_point_sale = 0 ; | |||
@@ -422,9 +424,10 @@ Vue.component('order-form',{ | |||
}) ; | |||
} | |||
}, | |||
submitFormUpdate: function() { | |||
submitFormUpdate: function(event) { | |||
var app = this ; | |||
if(this.checkForm()) { | |||
var processCredit = event.currentTarget.getAttribute('data-process-credit') ; | |||
axios.get(UrlManager.getBaseUrlAbsolute()+"order/ajax-update",{params: { | |||
date: this.date.getFullYear() + '-' | |||
+ ('0' + (this.date.getMonth() +1)).slice(-2) + '-' | |||
@@ -434,7 +437,8 @@ Vue.component('order-form',{ | |||
idUser: this.order.id_user, | |||
username: ''+this.order.username, | |||
products: JSON.stringify(this.order.productOrder), | |||
comment: this.comment | |||
comment: this.comment, | |||
processCredit: processCredit | |||
}}) | |||
.then(function(response) { | |||
app.$emit('ordercreatedupdated') ; |
@@ -203,8 +203,9 @@ class Distribution extends ActiveRecordCommon | |||
public static function getIncomingDistributions() | |||
{ | |||
$distributionsArray = Distribution::find() | |||
->where('date > ' . date('Y-m-d')) | |||
->where('date > \'' . date('Y-m-d').'\'') | |||
->andWhere(['id_producer' => Producer::getId()]) | |||
->orderBy('date ASC') | |||
->all(); | |||
return $distributionsArray ; |
@@ -307,6 +307,38 @@ class Order extends ActiveRecordCommon | |||
$creditHistory->populateRelation('user', User::find()->where(['id' => $this->id_user])->one()) ; | |||
$creditHistory->save(); | |||
} | |||
/** | |||
* Ajuste le crédit pour que la commande soit payée. | |||
* | |||
* @return boolean | |||
*/ | |||
public function processCredit() | |||
{ | |||
if($this->id_user) { | |||
$paymentStatus = $this->getPaymentStatus() ; | |||
if($paymentStatus == self::PAYMENT_PAID) { | |||
return true; | |||
} | |||
elseif($paymentStatus == self::PAYMENT_SURPLUS) { | |||
$type = CreditHistory::TYPE_REFUND ; | |||
$amount = $this->getAmount(self::AMOUNT_SURPLUS) ; | |||
} | |||
elseif($paymentStatus == self::PAYMENT_UNPAID) { | |||
$type = CreditHistory::TYPE_PAYMENT ; | |||
$amount = $this->getAmount(self::AMOUNT_REMAINING) ; | |||
} | |||
$this->saveCreditHistory( | |||
$type, | |||
$amount, | |||
Producer::getId(), | |||
$this->id_user, | |||
User::getCurrentId() | |||
); | |||
} | |||
} | |||
/** | |||
* Retourne le statut de paiement de la commande (payée, surplus, ou impayée). |
@@ -63,9 +63,10 @@ class OrderController extends ProducerBaseController | |||
]; | |||
} | |||
public function actionOrder($id = 0) | |||
public function actionOrder($id = 0, $date = '') | |||
{ | |||
$params = [] ; | |||
if($id) { | |||
$order = Order::searchOne([ | |||
'id' => $id | |||
@@ -77,6 +78,16 @@ class OrderController extends ProducerBaseController | |||
} | |||
} | |||
if(strlen($date)) { | |||
$distribution = Distribution::searchOne([ | |||
'date' => $date, | |||
'id_producer' => Producer::getId() | |||
]) ; | |||
if($distribution) { | |||
$params['date'] = $date ; | |||
} | |||
} | |||
return $this->render('order', $params) ; | |||
} | |||
@@ -46,6 +46,9 @@ $this->setTitle('Commander') ; | |||
<?php if(isset($order)): ?> | |||
<span id="order-distribution-date"><?= $order->distribution->date; ?></span> | |||
<?php endif; ?> | |||
<?php if(isset($date)): ?> | |||
<span id="distribution-date"><?= $date; ?></span> | |||
<?php endif; ?> | |||
<div v-if="loadingInit && distributions.length == 0" class="alert alert-warning"> | |||
Aucune distribution n'est prévue chez ce producteur. | |||
</div> |
@@ -60,8 +60,14 @@ var app = new Vue({ | |||
}, | |||
}, | |||
mounted: function() { | |||
if($('#order-distribution-date').size()) { | |||
this.date = new Date($('#order-distribution-date').html()) ; | |||
if($('#order-distribution-date').size() || $('#distribution-date').size()) { | |||
if($('#order-distribution-date').size()) { | |||
this.date = new Date($('#order-distribution-date').html()) ; | |||
} | |||
else { | |||
this.date = new Date($('#distribution-date').html()) ; | |||
} | |||
this.dateFormat = ('0' + this.date.getDate()).slice(-2)+ '/' | |||
+ ('0' + (this.date.getMonth() +1)).slice(-2) + '/' | |||
+ this.date.getFullYear() ; |