]; | ]; | ||||
if ($classDocument == 'Invoice') { | 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) { | |||||
if (!$deliveryNote->isInvoiced()) { | |||||
$json['delivery_notes'][] = array_merge( | |||||
$deliveryNote->getAttributes(), | |||||
[ | |||||
'total' => $deliveryNote->getAmountWithTax(Order::INVOICE_AMOUNT_TOTAL) | |||||
] | |||||
); | |||||
} | |||||
} | |||||
} | |||||
$options = [ | |||||
'orderby' => 'distribution.date ASC' | |||||
]; | |||||
$deliveryNotesCreateArray = DeliveryNote::searchAll([ | |||||
'id_user' => $user->id, | |||||
'status' => Document::STATUS_VALID | |||||
], $options); | |||||
$deliveryNotesUpdateArray = DeliveryNote::searchAll([ | |||||
'id_user' => $user->id, | |||||
'status' => Document::STATUS_VALID, | |||||
'order.id_invoice' => $idDocument | |||||
], $options); | |||||
$json['delivery_note_create_array'] = $this->initDeliveryNoteArray('create', $deliveryNotesCreateArray); | |||||
$json['delivery_note_update_array'] = $this->initDeliveryNoteArray('update', $deliveryNotesUpdateArray); | |||||
} | } | ||||
return $json; | return $json; | ||||
return ['return' => 'error']; | return ['return' => 'error']; | ||||
} | } | ||||
public function initDeliveryNoteArray($type, $deliveryNoteArrayResults) | |||||
{ | |||||
$deliveryNoteArray = []; | |||||
$isCreate = false; | |||||
if($type == 'create') { | |||||
$isCreate = true; | |||||
} | |||||
if($deliveryNoteArrayResults) { | |||||
foreach ($deliveryNoteArrayResults as $deliveryNote) { | |||||
$deliveryNoteData = $this->addDeliveryNoteToArray($deliveryNote, $isCreate); | |||||
if($deliveryNoteData) { | |||||
$deliveryNoteArray[] = $deliveryNoteData; | |||||
} | |||||
} | |||||
} | |||||
return $deliveryNoteArray; | |||||
} | |||||
public function addDeliveryNoteToArray($deliveryNote, $isCreate = true) | |||||
{ | |||||
$deliveryNoteData = array_merge( | |||||
$deliveryNote->getAttributes(), | |||||
[ | |||||
'url' => Yii::$app->urlManager->createUrl(['delivery-note/update', 'id' => $deliveryNote->id]), | |||||
'total' => $deliveryNote->getAmountWithTax(Order::INVOICE_AMOUNT_TOTAL) | |||||
] | |||||
); | |||||
if (($isCreate && !$deliveryNote->isInvoiced()) || !$isCreate) { | |||||
return $deliveryNoteData; | |||||
} | |||||
return false; | |||||
} | |||||
public function actionValidate($id, $backUpdateForm = false) | public function actionValidate($id, $backUpdateForm = false) | ||||
{ | { | ||||
$classDocument = $this->getClass(); | $classDocument = $this->getClass(); | ||||
) == 'DeliveryNote') ? $document->getAmountWithTax( | ) == 'DeliveryNote') ? $document->getAmountWithTax( | ||||
Order::INVOICE_AMOUNT_TOTAL | Order::INVOICE_AMOUNT_TOTAL | ||||
) : $document->getAmountWithTax(Order::AMOUNT_TOTAL), | ) : $document->getAmountWithTax(Order::AMOUNT_TOTAL), | ||||
'invoice_url' => ($document->getClass() == 'DeliveryNote' && $document->getInvoice()) ? Yii::$app->urlManager->createUrl(['invoice/update', 'id' => $document->getInvoice()->id]) : null | |||||
]; | ]; | ||||
} | } | ||||
} | } |
namespace backend\controllers; | namespace backend\controllers; | ||||
use common\models\Order; | |||||
use Yii; | use Yii; | ||||
]); | ]); | ||||
} | } | ||||
public function actionAjaxDeleteDeliveryNote($idInvoice, $idDeliveryNote) | |||||
{ | |||||
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; | |||||
$invoice = Invoice::searchOne(['id' => $idInvoice]); | |||||
if($invoice && $invoice->isStatusDraft()) { | |||||
Order::updateAll([ | |||||
'id_invoice' => null | |||||
], [ | |||||
'id_delivery_note' => $idDeliveryNote | |||||
]); | |||||
return [ | |||||
'alert' => [ | |||||
'type' => 'success', | |||||
'message' => 'Bon de livraison supprimé de la facture.' | |||||
] | |||||
]; | |||||
} | |||||
else { | |||||
return [ | |||||
'alert' => [ | |||||
'type' => 'error', | |||||
'message' => 'Une erreur est survenue lors de la suppression du bon de livraison.' | |||||
] | |||||
]; | |||||
} | |||||
} | |||||
public function actionAjaxAddDeliveryNote($idInvoice, $idDeliveryNote) | |||||
{ | |||||
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; | |||||
$invoice = Invoice::searchOne(['id' => $idInvoice]); | |||||
$deliveryNote = DeliveryNote::searchOne(['id' => $idDeliveryNote]); | |||||
if($invoice && $invoice->isStatusDraft() && $deliveryNote) { | |||||
Order::updateAll([ | |||||
'id_invoice' => $idInvoice | |||||
], [ | |||||
'id_delivery_note' => $idDeliveryNote | |||||
]); | |||||
return [ | |||||
'alert' => [ | |||||
'type' => 'success', | |||||
'message' => 'Bon de livraison ajouté à la facture.' | |||||
] | |||||
]; | |||||
} | |||||
else { | |||||
return [ | |||||
'alert' => [ | |||||
'type' => 'error', | |||||
'message' => 'Une erreur est survenue lors de l\'ajout du bon de livraison.' | |||||
] | |||||
]; | |||||
} | |||||
} | |||||
} | } |
<?= $form->field($model, 'address')->textarea(['rows' => 2, 'v-model' => 'document.address']) ?> | <?= $form->field($model, 'address')->textarea(['rows' => 2, 'v-model' => 'document.address']) ?> | ||||
<?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 du document') ?> | |||||
<?php endif; ?> | <?php endif; ?> | ||||
<?php if ($model->getClass() == 'Invoice'): ?> | |||||
<?php if ($action == 'create' && $model->getClass() == 'Invoice'): ?> | |||||
<template v-if="idUser > 0"> | <template v-if="idUser > 0"> | ||||
<strong>Bons de livraison</strong> | <strong>Bons de livraison</strong> | ||||
<table v-if="deliveryNotes && deliveryNotes.length > 0" class="table table-bordered"> | |||||
<table v-if="deliveryNoteCreateArray && deliveryNoteCreateArray.length > 0" class="table table-bordered"> | |||||
<thead> | <thead> | ||||
<tr> | <tr> | ||||
<?php if ($action == 'create'): ?> | |||||
<th></th><?php endif; ?> | |||||
<th></th> | |||||
<th>Libellé</th> | <th>Libellé</th> | ||||
<th v-if="taxRateProducer != 0">Montant (TTC)</th> | <th v-if="taxRateProducer != 0">Montant (TTC)</th> | ||||
<th v-else>Montant</th> | <th v-else>Montant</th> | ||||
</tr> | </tr> | ||||
</thead> | </thead> | ||||
<tbody> | <tbody> | ||||
<tr v-for="deliveryNote in deliveryNotes"> | |||||
<?php if ($action == 'create'): ?> | |||||
<td><input type="checkbox" name="Invoice[deliveryNotes][]" | |||||
:value="deliveryNote.id"/></td> | |||||
<?php endif; ?> | |||||
<tr v-for="deliveryNote in deliveryNoteCreateArray"> | |||||
<td><input type="checkbox" name="Invoice[deliveryNotes][]" :value="deliveryNote.id"/></td> | |||||
<td>{{ deliveryNote.name }}</td> | <td>{{ deliveryNote.name }}</td> | ||||
<td>{{ formatPrice(deliveryNote.total) }}</td> | <td>{{ formatPrice(deliveryNote.total) }}</td> | ||||
</tr> | </tr> | ||||
<?php ActiveForm::end(); ?> | <?php ActiveForm::end(); ?> | ||||
</div> | </div> | ||||
</div> | </div> | ||||
<?php if ($action == 'update' && $model->getClass() == 'Invoice'): ?> | |||||
<div class="panel panel-default"> | |||||
<div class="panel-heading"> | |||||
Bons de livraison | |||||
</div> | |||||
<div class="panel-body"> | |||||
<table v-if="deliveryNoteUpdateArray && deliveryNoteUpdateArray.length > 0" class="table table-bordered"> | |||||
<thead> | |||||
<tr> | |||||
<th>Libellé</th> | |||||
<th v-if="taxRateProducer != 0">Montant (TTC)</th> | |||||
<th v-else>Montant</th> | |||||
<th v-if="document.status == 'draft'"></th> | |||||
</tr> | |||||
</thead> | |||||
<tbody> | |||||
<tr v-for="deliveryNote in deliveryNoteUpdateArray"> | |||||
<td><a :href="deliveryNote.url">{{ deliveryNote.name }}</a></td> | |||||
<td>{{ formatPrice(deliveryNote.total) }}</td> | |||||
<td v-if="document.status == 'draft'"><a class="btn btn-default" href="javascript:void(0);" @click="deleteDeliveryNoteOfInvoice" :data-id="deliveryNote.id"><span class="glyphicon glyphicon-trash"></span></a></td> | |||||
</tr> | |||||
</tbody> | |||||
</table> | |||||
<div v-else class="alert alert-warning">Aucun bon de livraison associé.</div> | |||||
<div v-if="document.status == 'draft'" id="delivery-note-add"> | |||||
<div class="col-md-8"> | |||||
<select class="form-control" v-model="deliveryNoteAddId"> | |||||
<option value="0" selected="selected">--</option> | |||||
<option v-for="deliveryNote in deliveryNoteCreateArray" :value="deliveryNote.id"> | |||||
{{ deliveryNote.name }} | |||||
</option> | |||||
</select> | |||||
</div> | |||||
<div class="col-md-4"> | |||||
<button class="btn btn-primary" value="Ajouter" @click="submitDeliveryNoteAddToInvoice">Ajouter</button> | |||||
</div> | |||||
</div> | |||||
</div> | |||||
</div> | |||||
<?php endif; ?> | |||||
</div> | </div> | ||||
<?php if ($action == 'update'): ?> | <?php if ($action == 'update'): ?> | ||||
<div class="info-box-content"> | <div class="info-box-content"> | ||||
<span class="info-box-text">Total<span v-if="taxRateProducer != 0"> (TTC)</span></span> | <span class="info-box-text">Total<span v-if="taxRateProducer != 0"> (TTC)</span></span> | ||||
<span class="info-box-number">{{ formatPrice(total_with_tax) }}</span> | <span class="info-box-number">{{ formatPrice(total_with_tax) }}</span> | ||||
<p v-if="invoiceUrl"> | |||||
<a class="btn btn-sm btn-default" :href="invoiceUrl"> | |||||
<span class="glyphicon glyphicon-eye-open"></span> Voir la facture | |||||
</a> | |||||
</p> | |||||
</div> | </div> | ||||
</div> | </div> | ||||
<div id="" class="info-box"> | <div id="" class="info-box"> |
taxRateSelected = 0; | taxRateSelected = 0; | ||||
} | } | ||||
var price = $('#product-price').val().replace(',', '.'); | |||||
if (price) { | |||||
$('#product-price-with-tax').val(getPriceWithTax(price, taxRateSelected)); | |||||
// formattage | |||||
$('#product-price').val(parseFloat(price).toFixed(3)); | |||||
if($('#product-price').length) { | |||||
var price = $('#product-price').val().replace(',', '.'); | |||||
if (price) { | |||||
$('#product-price-with-tax').val(getPriceWithTax(price, taxRateSelected)); | |||||
// formattage | |||||
$('#product-price').val(parseFloat(price).toFixed(3)); | |||||
} | |||||
} | } | ||||
} | } | ||||
data: { | data: { | ||||
taxRateProducer: null, | taxRateProducer: null, | ||||
document: [], | document: [], | ||||
deliveryNotes: [], | |||||
deliveryNoteCreateArray: [], | |||||
deliveryNoteUpdateArray: [], | |||||
deliveryNoteAddId: 0, | |||||
idDocument: 0, | idDocument: 0, | ||||
typeDocument: '', | typeDocument: '', | ||||
idUser: '', | idUser: '', | ||||
productAddPrice: '', | productAddPrice: '', | ||||
productAddQuantity: 1, | productAddQuantity: 1, | ||||
ordersArray: [], | ordersArray: [], | ||||
invoiceUrl: null, | |||||
total: 0, | total: 0, | ||||
total_with_tax: 0 | total_with_tax: 0 | ||||
}, | }, | ||||
app.idUser = response.data.id_user; | app.idUser = response.data.id_user; | ||||
app.productsArray = response.data.products; | app.productsArray = response.data.products; | ||||
app.ordersArray = response.data.orders; | app.ordersArray = response.data.orders; | ||||
app.invoiceUrl = response.data.invoice_url; | |||||
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; | ||||
.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; | |||||
app.deliveryNoteCreateArray = response.data.delivery_note_create_array; | |||||
app.deliveryNoteUpdateArray = response.data.delivery_note_update_array; | |||||
} else { | } else { | ||||
app.document.address = ''; | app.document.address = ''; | ||||
} | } | ||||
app.init(); | app.init(); | ||||
}); | }); | ||||
}, | }, | ||||
deleteDeliveryNoteOfInvoice: function(event) { | |||||
var app = this; | |||||
var idDeliveryNote = event.currentTarget.getAttribute('data-id'); | |||||
axios.get(UrlManager.getBaseUrlAbsolute() + "invoice/ajax-delete-delivery-note", { | |||||
params: { | |||||
idInvoice: app.getDocumentId(), | |||||
idDeliveryNote: idDeliveryNote | |||||
} | |||||
}) | |||||
.then(function (response) { | |||||
appAlerts.alertResponse(response); | |||||
app.init(); | |||||
}); | |||||
}, | |||||
submitDeliveryNoteAddToInvoice: function() { | |||||
var app = this; | |||||
axios.get(UrlManager.getBaseUrlAbsolute() + "invoice/ajax-add-delivery-note", { | |||||
params: { | |||||
idInvoice: this.getDocumentId(), | |||||
idDeliveryNote: app.deliveryNoteAddId, | |||||
} | |||||
}) | |||||
.then(function (response) { | |||||
appAlerts.alertResponse(response); | |||||
app.deliveryNoteAddId = 0; | |||||
app.init(); | |||||
}); | |||||
}, | |||||
getStepProductAdd: function () { | getStepProductAdd: function () { | ||||
var step = parseInt(this.getProductById(this.productAddId).step); | var step = parseInt(this.getProductById(this.productAddId).step); | ||||
if(!step) { | if(!step) { | ||||
var price = pricesArray[i].price; | var price = pricesArray[i].price; | ||||
var fromQuantity = pricesArray[i].from_quantity; | var fromQuantity = pricesArray[i].from_quantity; | ||||
console.log(pricesArray[i]); | |||||
console.log(price+" / "+thePrice+" / "+fromQuantity+" / "+theQuantity+" / "); | |||||
if (price < thePrice && fromQuantity <= theQuantity) { | if (price < thePrice && fromQuantity <= theQuantity) { | ||||
thePrice = price; | thePrice = price; | ||||
} | } |
]; | ]; | ||||
} | } | ||||
public function isInvoiced() | |||||
public function getInvoiceId() | |||||
{ | { | ||||
if($this->orders && count($this->orders) > 0) { | if($this->orders && count($this->orders) > 0) { | ||||
foreach($this->orders as $order) { | foreach($this->orders as $order) { | ||||
if($order->id_invoice) { | if($order->id_invoice) { | ||||
return true; | |||||
return $order->id_invoice; | |||||
} | } | ||||
} | } | ||||
} | } | ||||
return false; | return false; | ||||
} | } | ||||
public function isInvoiced() | |||||
{ | |||||
return (bool) $this->getInvoiceId(); | |||||
} | |||||
public function getInvoice() | |||||
{ | |||||
$invoice = null; | |||||
$idInvoice = $this->getInvoiceId(); | |||||
if($idInvoice) { | |||||
$invoice = Invoice::searchOne([ | |||||
'id' => $idInvoice | |||||
]); | |||||
} | |||||
return $invoice; | |||||
} | |||||
} | } |