First invoice implementation.
This commit is contained in:
70
app/Http/Controllers/Api/InvoiceController.php
Normal file
70
app/Http/Controllers/Api/InvoiceController.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Invoice;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class InvoiceController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index($from = null, $end = null): JsonResponse
|
||||
{
|
||||
$from = $from . ' 00:00:00';
|
||||
$end = $end . ' 23:59:59';
|
||||
return response()->json(Invoice::whereBetween('created_at', [$from, $end])->with('address')->orderBy('created_at', 'desc')->get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
$invoiceData = $request->validate([
|
||||
'customer_id' => 'required|integer|exists:customers,id',
|
||||
'address_id' => 'required|integer|exists:addresses,id',
|
||||
'delivery_id' => 'nullable|integer|exists:addresses,id',
|
||||
'tax' => 'required|numeric',
|
||||
'sum' => 'required|numeric',
|
||||
]);
|
||||
$invoiceData['user_id'] = auth()->id();
|
||||
$invoiceData['type'] = '380';
|
||||
$invoiceData['status'] = 'created';
|
||||
$invoiceData['invoice_number'] = Invoice::whereYear('created_at', now()->year)->count() + 1;
|
||||
|
||||
|
||||
|
||||
$invoice = new Invoice($invoiceData);
|
||||
$invoice->save();
|
||||
|
||||
return response()->json($invoice);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(Invoice $invoice)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, Invoice $invoice)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(Invoice $invoice)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
64
app/Http/Controllers/Api/InvoiceitemController.php
Normal file
64
app/Http/Controllers/Api/InvoiceitemController.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\Invoiceitem;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class InvoiceitemController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index(Invoice $invoice): JsonResponse
|
||||
{
|
||||
return response()->json($invoice->items);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request, Invoice $invoice): JsonResponse
|
||||
{
|
||||
$itemData = $request->validate([
|
||||
'amount' => 'required|numeric|min:0',
|
||||
'discount' => 'numeric|nullable',
|
||||
'tax' => 'required|numeric|min:0',
|
||||
'price' => 'required|numeric|min:0',
|
||||
'total' => 'required|numeric|min:0',
|
||||
'name' => 'required|string',
|
||||
'description' => 'nullable|string',
|
||||
|
||||
]);
|
||||
|
||||
$item = $invoice->items()->create($itemData);
|
||||
return response()->json($item);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(Invoiceitem $invoiceitem)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, Invoiceitem $invoiceitem)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(Invoiceitem $invoiceitem)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
41
app/Http/Controllers/InvoiceController.php
Normal file
41
app/Http/Controllers/InvoiceController.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Invoice;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class InvoiceController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
return view('invoice.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
return view('invoice.create');
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(Invoice $invoice)
|
||||
{
|
||||
return view('invoice.show', ['invoice' => $invoice]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit(Invoice $invoice)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
@@ -3,8 +3,77 @@
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class Invoice extends Model
|
||||
{
|
||||
//
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'customer_id',
|
||||
'address_id',
|
||||
'delivery_id',
|
||||
'invoice_number',
|
||||
'type',
|
||||
'status',
|
||||
'sum',
|
||||
'tax',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that are appended with attribute getters.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $appends = [
|
||||
'created',
|
||||
'number'
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the invoice number formatted
|
||||
*/
|
||||
public function getNumberAttribute(): string
|
||||
{
|
||||
return $this->created_at->format('Y') . '-' . str_pad($this->invoice_number, 5, '0', STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the created_at attribute in local time format.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getCreatedAttribute(): string
|
||||
{
|
||||
return $this->created_at->format('d.m.Y');
|
||||
}
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function customer(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Customer::class);
|
||||
}
|
||||
|
||||
public function address(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Address::class)->withTrashed();
|
||||
}
|
||||
|
||||
public function delivery(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Address::class)->withTrashed();
|
||||
}
|
||||
|
||||
public function items(): HasMany
|
||||
{
|
||||
return $this->hasMany(Invoiceitem::class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,5 +7,20 @@ use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class Invoiceitem extends Model
|
||||
{
|
||||
use SoftDeletes;
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'invoice_id',
|
||||
'amount',
|
||||
'discount',
|
||||
'tax',
|
||||
'price',
|
||||
'total',
|
||||
'name',
|
||||
'description',
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user