* 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() ; | |||||
} | |||||
} | } | ||||
} | } | ||||
</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> | ||||
} | } | ||||
}, | }, | ||||
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') ; |
$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). |