Skip to content

Commit 708e03e

Browse files
committed
Updated sort ordering on invoices
1 parent b290eba commit 708e03e

File tree

9 files changed

+47
-28
lines changed

9 files changed

+47
-28
lines changed

resources/views/invoice-lines/partials/fields.blade.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
<tr wire:key="select2-{{ $value }}" data-number="{{ $value }}" class="item-tr">
1+
<tr class="cursor-grab" id="{{ $value }}">
2+
<td colspan="5" class="m-0 p-0">
3+
<table class="table table-sm table-items mb-0">
4+
<tbody>
5+
<tr wire:key="select2-{{ $value }}" data-number="{{ $value }}" class="item-tr">
26
<td colspan="5" class="pt-3 bind-select2" style="position: relative;">
37
@include('laravel-crm::partials.form.hidden',[
48
'name' => 'invoiceLines['.$value.'][order_product_id]',
@@ -157,3 +161,7 @@
157161
@endif
158162
</td>
159163
</tr>
164+
</tbody>
165+
</table>
166+
</td>
167+
</tr>

resources/views/invoices/partials/card-show.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
</tr>
110110
</thead>
111111
<tbody>
112-
@foreach($invoice->invoiceLines()->whereNotNull('product_id')->get() as $invoiceLine)
112+
@foreach($invoice->invoiceLines()->whereNotNull('product_id')->orderBy('order', 'asc')->orderBy('created_at', 'asc')->get() as $invoiceLine)
113113
<tr>
114114
<td>
115115
{{ $invoiceLine->product->name }}

resources/views/invoices/pdf.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
</tr>
9191
</thead>
9292
<tbody>
93-
@foreach($invoice->invoiceLInes()->whereNotNull('product_id')->get() as $invoiceLine)
93+
@foreach($invoice->invoiceLInes()->whereNotNull('product_id')->orderBy('order', 'asc')->orderBy('created_at', 'asc')->get() as $invoiceLine)
9494
<tr>
9595
<td>
9696
{{ $invoiceLine->product->name ?? null }}

resources/views/livewire/invoice-lines.blade.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<th scope="col" class="col-3 border-0">{{ ucfirst(__('laravel-crm::lang.amount')) }}</th>
1616
</tr>
1717
</thead>--}}
18-
<tbody>
18+
<tbody id="sortableItems">
1919
@foreach($inputs as $key => $value)
2020
@include('laravel-crm::invoice-lines.partials.fields')
2121
@endforeach
@@ -96,6 +96,22 @@
9696
@push('livewire-js')
9797
<script>
9898
$(document).ready(function () {
99+
var el = document.getElementById('sortableItems');
100+
var sortable = Sortable.create(el, {
101+
ghostClass: 'bg-secondary',
102+
dragClass: "bg-white",
103+
onEnd: function (evt) {
104+
const orderChanged = evt.oldIndex !== evt.newIndex;
105+
106+
if (!orderChanged) {
107+
return;
108+
}
109+
110+
const fromOrderedIds = [].slice.call(evt.from.children).map(child => child.id);
111+
@this.call('onItemSorted', fromOrderedIds);
112+
}
113+
});
114+
99115
window.addEventListener('addedItem', event => {
100116
if($('meta[name=dynamic_products]').length > 0){
101117
var tags = JSON.parse($('meta[name=dynamic_products]').attr('content'));

resources/views/portal/invoices/show.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@
168168
</tr>
169169
</thead>
170170
<tbody>
171-
@foreach($invoice->invoiceLines()->whereNotNull('product_id')->get() as $invoiceLine)
171+
@foreach($invoice->invoiceLines()->whereNotNull('product_id')->orderBy('order', 'asc')->orderBy('created_at', 'asc')->get() as $invoiceLine)
172172
<tr>
173173
<td>{{ $invoiceLine->product->name ?? null }}</td>
174174
<td>{{ money($invoiceLine->price ?? null, $invoiceLine->currency) }}</td>

src/Http/Livewire/LiveInvoiceLines.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,14 @@ public function remove($id)
200200
$this->calculateAmounts();
201201
}
202202

203+
public function onItemSorted($orderedIds)
204+
{
205+
foreach ($orderedIds as $orderNumber => $i) {
206+
$key = $orderNumber + 1;
207+
$this->inputs[$orderNumber] = (int) $i;
208+
}
209+
}
210+
203211
protected function currencyFormat($number)
204212
{
205213
return number_format($number, 2, '.', '');

src/Http/Livewire/LiveOrderItems.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -222,18 +222,6 @@ public function remove($id)
222222

223223
public function onItemSorted($orderedIds)
224224
{
225-
$oldOrder = [
226-
'quote_product_id' => $this->quote_product_id,
227-
'order_product_id' => $this->quote_product_id,
228-
'product_id' => $this->product_id,
229-
'name' => $this->name,
230-
'quantity' => $this->quantity,
231-
'unit_price' => $this->unit_price,
232-
'tax_amount' => $this->tax_amount,
233-
'amount' => $this->amount,
234-
'comments' => $this->comments,
235-
];
236-
237225
foreach ($orderedIds as $orderNumber => $i) {
238226
$key = $orderNumber + 1;
239227
$this->inputs[$orderNumber] = (int) $i;

src/Http/Livewire/LiveQuoteItems.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -180,17 +180,6 @@ public function remove($id)
180180

181181
public function onItemSorted($orderedIds)
182182
{
183-
$oldOrder = [
184-
'quote_product_id' => $this->quote_product_id,
185-
'product_id' => $this->product_id,
186-
'name' => $this->name,
187-
'quantity' => $this->quantity,
188-
'unit_price' => $this->unit_price,
189-
'tax_amount' => $this->tax_amount,
190-
'amount' => $this->amount,
191-
'comments' => $this->comments,
192-
];
193-
194183
foreach ($orderedIds as $orderNumber => $i) {
195184
$key = $orderNumber + 1;
196185
$this->inputs[$orderNumber] = (int) $i;

src/Services/InvoiceService.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ public function create($request, $person = null, $organisation = null)
4646
]);
4747

4848
if (isset($request->invoiceLines)) {
49+
$invoiceLineOrder = 0;
50+
4951
foreach ($request->invoiceLines as $invoiceLine) {
52+
$invoiceLineOrder++;
53+
5054
if (isset($invoiceLine['product_id']) && $invoiceLine['quantity'] > 0) {
5155
if (! Product::find($invoiceLine['product_id'])) {
5256
$newProduct = $this->addProduct($invoiceLine, $request);
@@ -77,6 +81,7 @@ public function create($request, $person = null, $organisation = null)
7781
'currency' => $request->currency,
7882
'order_product_id' => $invoiceLine['order_product_id'] ?? null,
7983
'comments' => $invoiceLine['comments'],
84+
'order' => $invoiceLineOrder,
8085
]);
8186
}
8287
}
@@ -146,8 +151,11 @@ public function update($request, Invoice $invoice, $person = null, $organisation
146151

147152
if (isset($request->invoiceLines)) {
148153
$invoiceLineIds = [];
154+
$invoiceLineOrder = 0;
149155

150156
foreach ($request->invoiceLines as $line) {
157+
$invoiceLineOrder++;
158+
151159
if (isset($line['invoice_line_id']) && $invoiceLine = InvoiceLine::find($line['invoice_line_id'])) {
152160
if (! isset($line['product_id']) || $line['quantity'] == 0) {
153161
$invoiceLine->delete();
@@ -179,6 +187,7 @@ public function update($request, Invoice $invoice, $person = null, $organisation
179187
'tax_amount' => $line['amount'] * ($taxRate / 100),
180188
'currency' => $request->currency,
181189
'comments' => $line['comments'],
190+
'order' => $invoiceLineOrder,
182191
]);
183192

184193
$invoiceLineIds[] = $invoiceLine->id;
@@ -212,6 +221,7 @@ public function update($request, Invoice $invoice, $person = null, $organisation
212221
'tax_amount' => $line['amount'] * ($taxRate / 100),
213222
'currency' => $request->currency,
214223
'comments' => $line['comments'],
224+
'order' => $invoiceLineOrder,
215225
]);
216226

217227
$invoiceLineIds[] = $invoiceLine->id;

0 commit comments

Comments
 (0)