Extend invoices and invoice items to keep more information.
This commit is contained in:
@@ -2,10 +2,12 @@
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Enum\InvoiceTypeCode;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Invoice;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class InvoiceController extends Controller
|
||||
{
|
||||
@@ -36,16 +38,21 @@ class InvoiceController extends Controller
|
||||
'customer_id' => 'required|integer|exists:customers,id',
|
||||
'address_id' => 'required|integer|exists:addresses,id',
|
||||
'delivery_id' => 'nullable|integer|exists:addresses,id',
|
||||
'project_id' => 'nullable|integer|exists:projects,id',
|
||||
'currency_code' => 'required|string',
|
||||
'type' => [Rule::enum(InvoiceTypeCode::class)],
|
||||
'project_count' => 'nullable|integer',
|
||||
'tax' => 'required|numeric',
|
||||
'sum' => 'required|numeric',
|
||||
'due_date' => 'required|date',
|
||||
'cash_discount' => 'nullable|numeric',
|
||||
'cash_discount_date' => 'nullable|date',
|
||||
]);
|
||||
|
||||
$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();
|
||||
|
||||
@@ -63,6 +70,9 @@ class InvoiceController extends Controller
|
||||
'delivery_id' => 'nullable|integer|exists:addresses,id',
|
||||
'tax' => 'required|numeric',
|
||||
'sum' => 'required|numeric',
|
||||
'due_date' => 'required|date',
|
||||
'cash_discount' => 'nullable|numeric',
|
||||
'cash_discount_date' => 'nullable|date',
|
||||
]);
|
||||
|
||||
$invoice->update($invoiceData);
|
||||
|
||||
@@ -31,6 +31,7 @@ class InvoiceitemController extends Controller
|
||||
'total' => 'required|numeric|min:0',
|
||||
'name' => 'required|string',
|
||||
'description' => 'nullable|string',
|
||||
'article_number' => 'nullable|string',
|
||||
|
||||
]);
|
||||
|
||||
|
||||
@@ -43,8 +43,8 @@ class EController extends Controller
|
||||
if (!isset($taxes[$item->tax])) {
|
||||
$taxes[$item->tax] = ['tax' => 0, 'taxable' => 0];
|
||||
}
|
||||
$taxes[$item->tax]['tax'] += round($item->price * $item->amount * $item->tax / 100, 2, PHP_ROUND_HALF_UP);
|
||||
$taxes[$item->tax]['taxable'] += $item->price * $item->amount;
|
||||
$taxes[$item->tax]['tax'] += round($item->price * $item->amount * ($item->tax / 100) * (100 - $item->discount) / 100, 2, PHP_ROUND_HALF_UP);
|
||||
$taxes[$item->tax]['taxable'] += $item->price * $item->amount * (100 - $item->discount) / 100;
|
||||
}
|
||||
|
||||
return $taxes;
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Option;
|
||||
use App\Models\Invoice;
|
||||
use Illuminate\Contracts\View\View;
|
||||
|
||||
@@ -20,7 +21,7 @@ class InvoiceController extends Controller
|
||||
*/
|
||||
public function create(): View
|
||||
{
|
||||
return view('invoice.create');
|
||||
return view('invoice.create', ['options' => Option::optionsAsObject()]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -23,6 +23,13 @@ class Invoice extends Model
|
||||
'status',
|
||||
'sum',
|
||||
'tax',
|
||||
'project_id',
|
||||
'project_count',
|
||||
'is_template',
|
||||
'currency_code',
|
||||
'due_date',
|
||||
'cash_discount',
|
||||
'cash_discount_date',
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -36,6 +43,25 @@ class Invoice extends Model
|
||||
'localized_state'
|
||||
];
|
||||
|
||||
/**
|
||||
* Set the project_count variable automatically, if a project is related to this invoice.
|
||||
*/
|
||||
public static function boot(): void
|
||||
{
|
||||
parent::boot();
|
||||
self::creating(function (Invoice $invoice) {
|
||||
if (is_null($invoice->project_id)) {
|
||||
return true;
|
||||
}
|
||||
if ($invoice->type != '326') {
|
||||
return true;
|
||||
}
|
||||
$projectMax = Invoice::where('project_id', '=', $invoice->project_id)->max('project_count') + 1;
|
||||
$invoice->project_count = $projectMax;
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the invoice state as translated string.
|
||||
*/
|
||||
@@ -107,4 +133,9 @@ class Invoice extends Model
|
||||
{
|
||||
return $this->hasMany(Payment::class);
|
||||
}
|
||||
|
||||
public function project(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Project::class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ class Invoiceitem extends Model
|
||||
*/
|
||||
protected $fillable = [
|
||||
'invoice_id',
|
||||
'article_number',
|
||||
'amount',
|
||||
'discount',
|
||||
'tax',
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace App\Models;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class Project extends Model
|
||||
@@ -33,8 +34,20 @@ class Project extends Model
|
||||
protected $appends = [
|
||||
'start',
|
||||
'end',
|
||||
'customer_email',
|
||||
'customer_name',
|
||||
];
|
||||
|
||||
|
||||
public function getCustomerNameAttribute(): string
|
||||
{
|
||||
return $this->customer->name;
|
||||
}
|
||||
public function getCustomerEmailAttribute(): string
|
||||
{
|
||||
return $this->customer->email;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the end_date attribute in local format.
|
||||
*/
|
||||
@@ -58,4 +71,9 @@ class Project extends Model
|
||||
{
|
||||
return $this->belongsTo(Customer::class);
|
||||
}
|
||||
|
||||
public function invoices(): HasMany
|
||||
{
|
||||
return $this->hasMany(Invoice::class);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user