if($deliveryNote && $deliveryNote->status == Document::STATUS_VALID) { | if($deliveryNote && $deliveryNote->status == Document::STATUS_VALID) { | ||||
return [ | return [ | ||||
'return' => 'error', | 'return' => 'error', | ||||
'alert' => 'Vous ne pouvez pas modifier un bon de livraison déjà validé.', | |||||
'alert' => [ | |||||
'type' => 'danger', | |||||
'message' => 'Vous ne pouvez pas modifier un bon de livraison déjà validé.' | |||||
] | |||||
] ; | ] ; | ||||
} | } | ||||
namespace backend\controllers; | namespace backend\controllers; | ||||
use common\models\DeliveryNote; | |||||
use common\models\Product; | use common\models\Product; | ||||
use common\models\User; | use common\models\User; | ||||
use common\models\Document; | use common\models\Document; | ||||
$model->id_producer = GlobalParam::getCurrentProducerId(); | $model->id_producer = GlobalParam::getCurrentProducerId(); | ||||
if ($model->save()) { | if ($model->save()) { | ||||
$this->processInvoiceViaDeliveryNotes($model) ; | |||||
Yii::$app->getSession()->setFlash('success', $this->getFlashMessage('create', $model)); | Yii::$app->getSession()->setFlash('success', $this->getFlashMessage('create', $model)); | ||||
return $this->redirect(['/' . $model->getControllerUrlPath() . '/update', 'id' => $model->id]); | return $this->redirect(['/' . $model->getControllerUrlPath() . '/update', 'id' => $model->id]); | ||||
} else { | } else { | ||||
]); | ]); | ||||
} | } | ||||
public function processInvoiceViaDeliveryNotes($model) | |||||
{ | |||||
if($model->getClass() == 'Invoice') { | |||||
if($model->deliveryNotes && is_array($model->deliveryNotes) && count($model->deliveryNotes)) { | |||||
foreach($model->deliveryNotes as $key => $idDeliveryNote) { | |||||
Order::updateAll([ | |||||
'id_invoice' => $model->id | |||||
], [ | |||||
'id_delivery_note' => $idDeliveryNote | |||||
]) ; | |||||
} | |||||
} | |||||
} | |||||
} | |||||
/** | /** | ||||
* Modifie un modèle Produit existant. | * Modifie un modèle Produit existant. | ||||
* Si la modification réussit, le navigateur est redirigé vers la page 'index'. | * Si la modification réussit, le navigateur est redirigé vers la page 'index'. | ||||
} | } | ||||
if ($model && $model->load(Yii::$app->request->post()) && $model->save()) { | if ($model && $model->load(Yii::$app->request->post()) && $model->save()) { | ||||
Yii::$app->getSession()->setFlash('success', $this->getFlashMessage('update', $model)); | Yii::$app->getSession()->setFlash('success', $this->getFlashMessage('update', $model)); | ||||
} | } | ||||
]); | ]); | ||||
Yii::$app->getSession()->setFlash('success', $this->getFlashMessage('delete', $model)); | Yii::$app->getSession()->setFlash('success', $this->getFlashMessage('delete', $model)); | ||||
$this->redirect(['delivery-note/index']); | |||||
$this->redirect([$model->getControllerUrlPath().'/index']); | |||||
} | } | ||||
public function actionDownload($id) | public function actionDownload($id) | ||||
return $pdf->render(); | return $pdf->render(); | ||||
} | } | ||||
public function actionAjaxAddressUser($idUser) | |||||
public function actionAjaxUserInfos($typeAction, $idUser, $classDocument, $idDocument = false) | |||||
{ | { | ||||
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; | \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; | ||||
]); | ]); | ||||
if ($user) { | if ($user) { | ||||
return [ | |||||
$json = [ | |||||
'return' => 'success', | 'return' => 'success', | ||||
'address' => $user->getFullAddress() | 'address' => $user->getFullAddress() | ||||
]; | ]; | ||||
if($classDocument == 'Invoice') { | |||||
if($typeAction == 'create') { | |||||
$deliveryNotesArray = DeliveryNote::searchAll([ | |||||
'id_user' => $user->id, | |||||
'status' => Document::STATUS_VALID | |||||
]) ; | |||||
} | |||||
elseif($typeAction == 'update' && $idDocument > 0) { | |||||
$deliveryNotesArray = DeliveryNote::searchAll([ | |||||
'id_user' => $user->id, | |||||
'status' => Document::STATUS_VALID, | |||||
'order.id_invoice' => $idDocument | |||||
]) ; | |||||
} | |||||
if(isset($deliveryNotesArray)) { | |||||
$json['delivery_notes'] = [] ; | |||||
foreach($deliveryNotesArray as $deliveryNote) { | |||||
$json['delivery_notes'][] = array_merge( | |||||
$deliveryNote->getAttributes(), | |||||
[ | |||||
'total' => $deliveryNote->getAmountWithTax() | |||||
] | |||||
) ; | |||||
} | |||||
} | |||||
} | |||||
return $json ; | |||||
} | } | ||||
} | } | ||||
</div> | </div> | ||||
<div class="panel-body"> | <div class="panel-body"> | ||||
<?php $form = ActiveForm::begin(); ?> | <?php $form = ActiveForm::begin(); ?> | ||||
<?= Html::hiddenInput('classDocument',$model->getClass(), ['id' => 'class-document']) ?> | |||||
<?= Html::hiddenInput('typeAction',$action, ['id' => 'type-action']) ?> | |||||
<?php if ($action == 'update'): ?> | |||||
<?= Html::hiddenInput('idDocument',$model->id, ['id' => 'id-document']) ?> | |||||
<?php endif; ?> | |||||
<?= $form->field($model, 'name')->label('Nom du document') ?> | <?= $form->field($model, 'name')->label('Nom du document') ?> | ||||
<?php $usersArray = User::findBy()->all(); ?> | <?php $usersArray = User::findBy()->all(); ?> | ||||
<?= $form->field($model, 'id_user', [ | <?= $form->field($model, 'id_user', [ | ||||
<?php if ($action == 'update'): ?> | <?php if ($action == 'update'): ?> | ||||
<?= $form->field($model, 'comment')->textarea(['rows' => 2])->hint('Affiché en bas de la facture') ?> | <?= $form->field($model, 'comment')->textarea(['rows' => 2])->hint('Affiché en bas de la facture') ?> | ||||
<?php endif; ?> | <?php endif; ?> | ||||
<?php if($model->getClass() == 'Invoice'): ?> | |||||
<template v-if="idUser > 0"> | |||||
<strong>Bons de livraison</strong> | |||||
<table v-if="deliveryNotes && deliveryNotes.length > 0" class="table table-bordered"> | |||||
<thead> | |||||
<tr> | |||||
<?php if($action == 'create'): ?><th></th><?php endif; ?> | |||||
<th>Libellé</th> | |||||
<th>Montant (TTC)</th> | |||||
</tr> | |||||
</thead> | |||||
<tbody> | |||||
<tr v-for="deliveryNote in deliveryNotes"> | |||||
<?php if($action == 'create'): ?> | |||||
<td><input type="checkbox" name="Invoice[deliveryNotes][]" :value="deliveryNote.id" /></td> | |||||
<?php endif; ?> | |||||
<td>{{ deliveryNote.name }}</td> | |||||
<td>{{ formatPrice(deliveryNote.total) }}</td> | |||||
</tr> | |||||
</tbody> | |||||
</table> | |||||
<div v-else class="alert alert-warning">Aucun bon de livraison pour cet utilisateur.</div> | |||||
</template> | |||||
<?php endif; ?> | |||||
<div class="form-group"> | <div class="form-group"> | ||||
<?= Html::submitButton($model->isNewRecord ? 'Ajouter' : 'Modifier', ['class' => 'btn btn-primary']) ?> | <?= Html::submitButton($model->isNewRecord ? 'Ajouter' : 'Modifier', ['class' => 'btn btn-primary']) ?> | ||||
</div> | </div> |
'attribute' => 'id_user', | 'attribute' => 'id_user', | ||||
'header' => 'Utilisateur', | 'header' => 'Utilisateur', | ||||
'value' => function($model) { | 'value' => function($model) { | ||||
return $model->user->lastname.' '.$model->user->name ; | |||||
return $model->user->getUsername() ; | |||||
} | } | ||||
], | ], | ||||
[ | [ |
el: '#app-document-form', | el: '#app-document-form', | ||||
data: { | data: { | ||||
document: [], | document: [], | ||||
deliveryNotes: [], | |||||
idDocument: 0, | idDocument: 0, | ||||
typeDocument: '', | typeDocument: '', | ||||
idUser: '', | idUser: '', | ||||
methods: { | methods: { | ||||
formatPrice: formatPrice, | formatPrice: formatPrice, | ||||
init: function() { | init: function() { | ||||
var idDocument = $('#app-document-form').attr('data-id-document') ; | |||||
this.idDocument = idDocument ; | |||||
var classDocument = $('#app-document-form').attr('data-class-document') ; | |||||
this.classDocument = classDocument ; | |||||
if(idDocument) { | |||||
if(this.getDocumentId()) { | |||||
var app = this ; | var app = this ; | ||||
axios.get(UrlManager.getBaseUrlAbsolute()+"document/ajax-init",{params: { | axios.get(UrlManager.getBaseUrlAbsolute()+"document/ajax-init",{params: { | ||||
idDocument: idDocument, | |||||
classDocument: classDocument | |||||
idDocument: this.getDocumentId(), | |||||
classDocument: this.getDocumentClass() | |||||
}}) | }}) | ||||
.then(function(response) { | .then(function(response) { | ||||
if(response.data.return == 'success') { | if(response.data.return == 'success') { | ||||
app.ordersArray = response.data.orders ; | app.ordersArray = response.data.orders ; | ||||
app.total = response.data.total ; | app.total = response.data.total ; | ||||
app.total_with_tax = response.data.total_with_tax ; | app.total_with_tax = response.data.total_with_tax ; | ||||
if(app.idUser > 0) { | |||||
app.changeUser() ; | |||||
} | |||||
} | } | ||||
}) ; | }) ; | ||||
} | } | ||||
}, | }, | ||||
changeUser: function(event) { | |||||
getDocumentId: function() { | |||||
var documentId = $('#app-document-form').attr('data-id-document') ; | |||||
return documentId ; | |||||
}, | |||||
getDocumentClass: function() { | |||||
var documentClass = $('#app-document-form').attr('data-class-document') ; | |||||
return documentClass ; | |||||
}, | |||||
changeUser: function() { | |||||
var app = this ; | var app = this ; | ||||
axios.get(UrlManager.getBaseUrlAbsolute()+"document/ajax-address-user",{params: { | |||||
idUser: app.idUser | |||||
axios.get(UrlManager.getBaseUrlAbsolute()+"document/ajax-user-infos",{params: { | |||||
idUser: app.idUser, | |||||
classDocument: app.getDocumentClass(), | |||||
idDocument: app.getDocumentId(), | |||||
typeAction: $('#type-action').val(), | |||||
}}) | }}) | ||||
.then(function(response) { | .then(function(response) { | ||||
if(response.data.return == 'success') { | if(response.data.return == 'success') { | ||||
Vue.set(app.document, 'address', response.data.address); | Vue.set(app.document, 'address', response.data.address); | ||||
app.deliveryNotes = response.data.delivery_notes ; | |||||
} | } | ||||
else { | else { | ||||
app.document.address = '' ; | app.document.address = '' ; | ||||
validateDocument: function() { | validateDocument: function() { | ||||
var app = this ; | var app = this ; | ||||
axios.get(UrlManager.getBaseUrlAbsolute()+"document/ajax-validate-document",{params: { | axios.get(UrlManager.getBaseUrlAbsolute()+"document/ajax-validate-document",{params: { | ||||
idDocument: app.idDocument, | |||||
classDocument: app.classDocument, | |||||
idDocument: app.getDocumentId(), | |||||
classDocument: app.getDocumentClass(), | |||||
}}) | }}) | ||||
.then(function(response) { | .then(function(response) { | ||||
appAlerts.alertResponse(response) ; | appAlerts.alertResponse(response) ; | ||||
submitProductAdd: function() { | submitProductAdd: function() { | ||||
var app = this ; | var app = this ; | ||||
axios.get(UrlManager.getBaseUrlAbsolute()+"document/ajax-add-product",{params: { | axios.get(UrlManager.getBaseUrlAbsolute()+"document/ajax-add-product",{params: { | ||||
idDocument: app.idDocument, | |||||
classDocument: app.classDocument, | |||||
idDocument: this.getDocumentId(), | |||||
classDocument: this.getDocumentClass(), | |||||
idProduct: app.productAddId, | idProduct: app.productAddId, | ||||
quantity: app.productAddQuantity, | quantity: app.productAddQuantity, | ||||
price: app.productAddPrice, | price: app.productAddPrice, |
{ | { | ||||
const STATUS_DRAFT = 'draft' ; | const STATUS_DRAFT = 'draft' ; | ||||
const STATUS_VALID = 'valid' ; | const STATUS_VALID = 'valid' ; | ||||
/** | /** | ||||
* @inheritdoc | * @inheritdoc | ||||
*/ | */ | ||||
[['comment', 'address'], 'string'], | [['comment', 'address'], 'string'], | ||||
[['id_user','id_producer'], 'integer'], | [['id_user','id_producer'], 'integer'], | ||||
[['name', 'reference', 'status'], 'string', 'max' => 255], | [['name', 'reference', 'status'], 'string', 'max' => 255], | ||||
[['deliveryNotes'], 'safe'] | |||||
]; | ]; | ||||
} | } | ||||
class Invoice extends Document | class Invoice extends Document | ||||
{ | { | ||||
public $deliveryNotes ; | |||||
/** | /** | ||||
* @inheritdoc | * @inheritdoc | ||||
*/ | */ |
public function getUsername() | public function getUsername() | ||||
{ | { | ||||
return $this->lastname.' '.$this->name ; | |||||
$username = '' ; | |||||
if(isset($this->name_legal_person) && strlen($this->name_legal_person)) { | |||||
$username = $this->name_legal_person ; | |||||
} | |||||
else { | |||||
$username = $this->lastname.' '.$this->name ; | |||||
} | |||||
return $username ; | |||||
} | } | ||||
/** | /** |