* Crée une commande via une requête AJAX. | * Crée une commande via une requête AJAX. | ||||
* | * | ||||
* @param string $date | * @param string $date | ||||
* @param integer $id_pv | |||||
* @param integer $id_user | |||||
* @param integer $idPointSale | |||||
* @param integer $idUser | |||||
* @param string $username | * @param string $username | ||||
* @param array $produits | * @param array $produits | ||||
* @param string $commentaire | * @param string $commentaire | ||||
* @param string $processCredit | |||||
*/ | */ | ||||
public function actionAjaxCreate( | 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; | \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; | ||||
$productOrder->save(); | $productOrder->save(); | ||||
} | } | ||||
} | } | ||||
$order = Order::searchOne(['id' => $order->id]) ; | |||||
if($order && $processCredit) { | |||||
$order->processCredit() ; | |||||
} | |||||
} | } | ||||
return ['success'] ; | return ['success'] ; | ||||
* @param string $comment | * @param string $comment | ||||
*/ | */ | ||||
public function actionAjaxUpdate( | 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; | \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; | ||||
} | } | ||||
$order->save(); | $order->save(); | ||||
$order = Order::searchOne(['id' => $order->id]) ; | |||||
if($order && $processCredit) { | |||||
$order->processCredit() ; | |||||
} | |||||
} | } | ||||
} | } | ||||
use common\models\User ; | use common\models\User ; | ||||
use common\models\Producer ; | use common\models\Producer ; | ||||
use common\models\Distribution ; | |||||
use backend\models\MailForm ; | use backend\models\MailForm ; | ||||
/** | /** | ||||
return $this->redirect(['mail','idPointSale' => $idPointSale]); | 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', [ | return $this->render('emails', [ | ||||
'usersArray' => $usersArray, | 'usersArray' => $usersArray, | ||||
'pointsSaleArray' => $pointsSaleArray, | 'pointsSaleArray' => $pointsSaleArray, | ||||
'pointSale' => $pointSale, | 'pointSale' => $pointSale, | ||||
'mailForm' => $mailForm, | 'mailForm' => $mailForm, | ||||
'idPointSaleActive' => $idPointSale | |||||
'idPointSaleActive' => $idPointSale, | |||||
'incomingDistributionsArray' => $incomingDistributionsArray, | |||||
]); | ]); | ||||
} | } | ||||
use Yii; | use Yii; | ||||
use yii\base\Model; | use yii\base\Model; | ||||
use common\helpers\Price ; | |||||
/** | /** | ||||
* ContactForm is the model behind the contact form. | * ContactForm is the model behind the contact form. | ||||
class MailForm extends Model | class MailForm extends Model | ||||
{ | { | ||||
public $id_distribution ; | |||||
public $subject; | public $subject; | ||||
public $message; | public $message; | ||||
{ | { | ||||
return [ | return [ | ||||
[['subject', 'message'], 'required', 'message' => 'Champs obligatoire'], | [['subject', 'message'], 'required', 'message' => 'Champs obligatoire'], | ||||
[['id_distribution'],'integer'] | |||||
]; | ]; | ||||
} | } | ||||
return [ | return [ | ||||
'subject' => 'Sujet', | 'subject' => 'Sujet', | ||||
'message' => 'Message', | 'message' => 'Message', | ||||
'id_distribution' => 'Distribution' | |||||
]; | ]; | ||||
} | } | ||||
$body = ['Messages' => []] ; | $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) { | foreach($usersArray as $user) { | ||||
$body['Messages'][] = [ | $body['Messages'][] = [ | ||||
'From' => [ | 'From' => [ | ||||
] | ] | ||||
], | ], | ||||
'Subject' => $this->subject, | 'Subject' => $this->subject, | ||||
'TextPart' => $this->message, | |||||
'HTMLPart' => nl2br($this->message) | |||||
'TextPart' => $this->message.$messageAutoText, | |||||
'HTMLPart' => nl2br($this->message).$messageAutoHtml | |||||
] ; | ] ; | ||||
} | } | ||||
</div> | </div> | ||||
<div slot="footer"> | <div slot="footer"> | ||||
<div class="actions-form"> | <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="submitFormUpdate" v-if="order.id">Modifier</button> | ||||
<button class="modal-default-button btn btn-primary" @click="submitFormCreate" v-else>Créer</button> | <button class="modal-default-button btn btn-primary" @click="submitFormCreate" v-else>Créer</button> | ||||
</div> | </div> | ||||
<div class="panel-body"> | <div class="panel-body"> | ||||
<?php $form = ActiveForm::begin(); ?> | <?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, 'subject')->textInput() ; ?> | ||||
<?= $form->field($mailForm, 'message')->textarea(['rows' => '15']) ; ?> | <?= $form->field($mailForm, 'message')->textarea(['rows' => '15']) ; ?> | ||||
<div class="form-group"> | <div class="form-group"> |
} | } | ||||
}, | }, | ||||
submitFormCreate: function() { | |||||
submitFormCreate: function(event) { | |||||
var app = this ; | var app = this ; | ||||
if(this.checkForm()) { | if(this.checkForm()) { | ||||
var processCredit = event.currentTarget.getAttribute('data-process-credit') ; | |||||
axios.get(UrlManager.getBaseUrlAbsolute()+"order/ajax-create",{params: { | axios.get(UrlManager.getBaseUrlAbsolute()+"order/ajax-create",{params: { | ||||
date: this.date.getFullYear() + '-' | date: this.date.getFullYear() + '-' | ||||
+ ('0' + (this.date.getMonth() +1)).slice(-2) + '-' | + ('0' + (this.date.getMonth() +1)).slice(-2) + '-' | ||||
idUser: this.order.id_user, | idUser: this.order.id_user, | ||||
username: this.order.username, | username: this.order.username, | ||||
products: JSON.stringify(this.order.productOrder), | products: JSON.stringify(this.order.productOrder), | ||||
comment: this.order.comment | |||||
comment: this.order.comment, | |||||
processCredit: processCredit | |||||
}}) | }}) | ||||
.then(function(response) { | .then(function(response) { | ||||
app.order.id_point_sale = 0 ; | app.order.id_point_sale = 0 ; | ||||
}) ; | }) ; | ||||
} | } | ||||
}, | }, | ||||
submitFormUpdate: function() { | |||||
submitFormUpdate: function(event) { | |||||
var app = this ; | var app = this ; | ||||
if(this.checkForm()) { | if(this.checkForm()) { | ||||
var processCredit = event.currentTarget.getAttribute('data-process-credit') ; | |||||
axios.get(UrlManager.getBaseUrlAbsolute()+"order/ajax-update",{params: { | axios.get(UrlManager.getBaseUrlAbsolute()+"order/ajax-update",{params: { | ||||
date: this.date.getFullYear() + '-' | date: this.date.getFullYear() + '-' | ||||
+ ('0' + (this.date.getMonth() +1)).slice(-2) + '-' | + ('0' + (this.date.getMonth() +1)).slice(-2) + '-' | ||||
idUser: this.order.id_user, | idUser: this.order.id_user, | ||||
username: ''+this.order.username, | username: ''+this.order.username, | ||||
products: JSON.stringify(this.order.productOrder), | products: JSON.stringify(this.order.productOrder), | ||||
comment: this.comment | |||||
comment: this.comment, | |||||
processCredit: processCredit | |||||
}}) | }}) | ||||
.then(function(response) { | .then(function(response) { | ||||
app.$emit('ordercreatedupdated') ; | app.$emit('ordercreatedupdated') ; |
public static function getIncomingDistributions() | public static function getIncomingDistributions() | ||||
{ | { | ||||
$distributionsArray = Distribution::find() | $distributionsArray = Distribution::find() | ||||
->where('date > ' . date('Y-m-d')) | |||||
->where('date > \'' . date('Y-m-d').'\'') | |||||
->andWhere(['id_producer' => Producer::getId()]) | ->andWhere(['id_producer' => Producer::getId()]) | ||||
->orderBy('date ASC') | |||||
->all(); | ->all(); | ||||
return $distributionsArray ; | return $distributionsArray ; |
$creditHistory->populateRelation('user', User::find()->where(['id' => $this->id_user])->one()) ; | $creditHistory->populateRelation('user', User::find()->where(['id' => $this->id_user])->one()) ; | ||||
$creditHistory->save(); | $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). | * Retourne le statut de paiement de la commande (payée, surplus, ou impayée). |
]; | ]; | ||||
} | } | ||||
public function actionOrder($id = 0) | |||||
public function actionOrder($id = 0, $date = '') | |||||
{ | { | ||||
$params = [] ; | $params = [] ; | ||||
if($id) { | if($id) { | ||||
$order = Order::searchOne([ | $order = Order::searchOne([ | ||||
'id' => $id | 'id' => $id | ||||
} | } | ||||
} | } | ||||
if(strlen($date)) { | |||||
$distribution = Distribution::searchOne([ | |||||
'date' => $date, | |||||
'id_producer' => Producer::getId() | |||||
]) ; | |||||
if($distribution) { | |||||
$params['date'] = $date ; | |||||
} | |||||
} | |||||
return $this->render('order', $params) ; | return $this->render('order', $params) ; | ||||
} | } | ||||
<?php if(isset($order)): ?> | <?php if(isset($order)): ?> | ||||
<span id="order-distribution-date"><?= $order->distribution->date; ?></span> | <span id="order-distribution-date"><?= $order->distribution->date; ?></span> | ||||
<?php endif; ?> | <?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"> | <div v-if="loadingInit && distributions.length == 0" class="alert alert-warning"> | ||||
Aucune distribution n'est prévue chez ce producteur. | Aucune distribution n'est prévue chez ce producteur. | ||||
</div> | </div> |
}, | }, | ||||
}, | }, | ||||
mounted: function() { | 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)+ '/' | this.dateFormat = ('0' + this.date.getDate()).slice(-2)+ '/' | ||||
+ ('0' + (this.date.getMonth() +1)).slice(-2) + '/' | + ('0' + (this.date.getMonth() +1)).slice(-2) + '/' | ||||
+ this.date.getFullYear() ; | + this.date.getFullYear() ; |