-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInvoice.php
95 lines (79 loc) · 2.99 KB
/
Invoice.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
declare(strict_types=1);
namespace Tipoff\Invoices\Nova;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Fields\Currency;
use Laravel\Nova\Fields\Date;
use Laravel\Nova\Fields\DateTime;
use Laravel\Nova\Fields\HasMany;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Panel;
use Tipoff\Support\Nova\BaseResource;
class Invoice extends BaseResource
{
public static $model = \Tipoff\Invoices\Models\Invoice::class;
public static $title = 'invoice_number';
public static $search = [
'invoice_number',
];
public static $group = 'Operations Units';
public static function indexQuery(NovaRequest $request, $query)
{
if ($request->user()->hasPermissionTo('all locations')) {
return $query;
}
return $query->whereHas('order', function ($orderlocation) use ($request) {
return $orderlocation->whereIn('location_id', $request->user()->locations->pluck('id'));
});
}
public function fieldsForIndex(NovaRequest $request)
{
return array_filter([
ID::make()->sortable(),
Text::make('Invoice Number')->sortable(),
nova('order') ? BelongsTo::make('Order', 'order', nova('order'))->sortable() : null,
nova('user') ? BelongsTo::make('Customer', 'user', nova('user'))->sortable() : null,
Currency::make('Amount')->asMinorUnits()->sortable(),
Date::make('Invoiced At', 'invoiced_at')->sortable(),
]);
}
public function fields(Request $request)
{
return array_filter([
Text::make('Invoice Number')->exceptOnForms(),
nova('order') ? BelongsTo::make('Order', 'order', nova('order'))->hideWhenUpdating() : null,
nova('user') ? BelongsTo::make('Customer', 'user', nova('user'))->exceptOnForms() : null,
Currency::make('Amount')->asMinorUnits()
->step('0.01')
->resolveUsing(function ($value) {
return $value / 100;
})
->fillUsing(function ($request, $model, $attribute) {
$model->$attribute = $request->$attribute * 100;
})->rules('required'),
Text::make('Note'),
DateTime::make('Invoiced At', 'invoiced_at')->exceptOnForms(),
Date::make('Due Date', 'due_at'),
// Will need a 'Send Invoice' button
nova('payment') ? HasMany::make('Payments', 'payments', nova('payment')) : null,
new Panel('Data Fields', $this->dataFields()),
]);
}
protected function dataFields(): array
{
return array_merge(
parent::dataFields(),
$this->creatorDataFields(),
$this->updaterDataFields(),
);
}
public function filters(Request $request)
{
return [
// new Tipoff\LocationsFilters\OrderLocation,
];
}
}