@@ -366,33 +366,20 @@ class DocumentController extends BackendController | |||
]; | |||
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; | |||
@@ -402,6 +389,43 @@ class DocumentController extends BackendController | |||
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) | |||
{ | |||
$classDocument = $this->getClass(); | |||
@@ -542,6 +566,7 @@ class DocumentController extends BackendController | |||
) == 'DeliveryNote') ? $document->getAmountWithTax( | |||
Order::INVOICE_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 | |||
]; | |||
} | |||
} |
@@ -38,6 +38,7 @@ termes. | |||
namespace backend\controllers; | |||
use common\models\Order; | |||
use Yii; | |||
@@ -59,4 +60,65 @@ class InvoiceController extends DocumentController | |||
]); | |||
} | |||
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.' | |||
] | |||
]; | |||
} | |||
} | |||
} |
@@ -94,28 +94,24 @@ use common\models\Producer; | |||
<?= $form->field($model, 'address')->textarea(['rows' => 2, 'v-model' => 'document.address']) ?> | |||
<?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 if ($model->getClass() == 'Invoice'): ?> | |||
<?php if ($action == 'create' && $model->getClass() == 'Invoice'): ?> | |||
<template v-if="idUser > 0"> | |||
<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> | |||
<tr> | |||
<?php if ($action == 'create'): ?> | |||
<th></th><?php endif; ?> | |||
<th></th> | |||
<th>Libellé</th> | |||
<th v-if="taxRateProducer != 0">Montant (TTC)</th> | |||
<th v-else>Montant</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; ?> | |||
<tr v-for="deliveryNote in deliveryNoteCreateArray"> | |||
<td><input type="checkbox" name="Invoice[deliveryNotes][]" :value="deliveryNote.id"/></td> | |||
<td>{{ deliveryNote.name }}</td> | |||
<td>{{ formatPrice(deliveryNote.total) }}</td> | |||
</tr> | |||
@@ -131,6 +127,49 @@ use common\models\Producer; | |||
<?php ActiveForm::end(); ?> | |||
</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> | |||
<?php if ($action == 'update'): ?> | |||
@@ -154,6 +193,11 @@ use common\models\Producer; | |||
<div class="info-box-content"> | |||
<span class="info-box-text">Total<span v-if="taxRateProducer != 0"> (TTC)</span></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 id="" class="info-box"> |
@@ -139,11 +139,13 @@ function opendistrib_products_event_price_with_tax() { | |||
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)); | |||
} | |||
} | |||
} | |||
@@ -39,7 +39,9 @@ var app = new Vue({ | |||
data: { | |||
taxRateProducer: null, | |||
document: [], | |||
deliveryNotes: [], | |||
deliveryNoteCreateArray: [], | |||
deliveryNoteUpdateArray: [], | |||
deliveryNoteAddId: 0, | |||
idDocument: 0, | |||
typeDocument: '', | |||
idUser: '', | |||
@@ -48,6 +50,7 @@ var app = new Vue({ | |||
productAddPrice: '', | |||
productAddQuantity: 1, | |||
ordersArray: [], | |||
invoiceUrl: null, | |||
total: 0, | |||
total_with_tax: 0 | |||
}, | |||
@@ -73,6 +76,7 @@ var app = new Vue({ | |||
app.idUser = response.data.id_user; | |||
app.productsArray = response.data.products; | |||
app.ordersArray = response.data.orders; | |||
app.invoiceUrl = response.data.invoice_url; | |||
app.total = response.data.total; | |||
app.total_with_tax = response.data.total_with_tax; | |||
@@ -129,7 +133,8 @@ var app = new Vue({ | |||
.then(function (response) { | |||
if (response.data.return == 'success') { | |||
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 { | |||
app.document.address = ''; | |||
} | |||
@@ -148,6 +153,35 @@ var app = new Vue({ | |||
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 () { | |||
var step = parseInt(this.getProductById(this.productAddId).step); | |||
if(!step) { | |||
@@ -178,8 +212,6 @@ var app = new Vue({ | |||
var price = pricesArray[i].price; | |||
var fromQuantity = pricesArray[i].from_quantity; | |||
console.log(pricesArray[i]); | |||
console.log(price+" / "+thePrice+" / "+fromQuantity+" / "+theQuantity+" / "); | |||
if (price < thePrice && fromQuantity <= theQuantity) { | |||
thePrice = price; | |||
} |
@@ -90,12 +90,12 @@ class DeliveryNote extends Document | |||
]; | |||
} | |||
public function isInvoiced() | |||
public function getInvoiceId() | |||
{ | |||
if($this->orders && count($this->orders) > 0) { | |||
foreach($this->orders as $order) { | |||
if($order->id_invoice) { | |||
return true; | |||
return $order->id_invoice; | |||
} | |||
} | |||
} | |||
@@ -103,4 +103,22 @@ class DeliveryNote extends Document | |||
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; | |||
} | |||
} |