79 lines
2.4 KiB
PHP
79 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Inertia\Inertia;
|
|
use App\Models\Customer;
|
|
use App\Support\ApiDataTransformer;
|
|
|
|
class CustomerController extends Controller
|
|
{
|
|
|
|
public function show()
|
|
{
|
|
return Inertia::render('Customers', [
|
|
'customersData' => $this->index()
|
|
]);
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$customers = Customer::with(['contacts' => function ($query) {
|
|
$query->orderBy('is_primary', 'desc');
|
|
}, 'paymentTerms'])->get();
|
|
|
|
return $customers->map(function ($customer) {
|
|
$customerArray = $customer->toArray();
|
|
$customerArray['payment_terms'] = $customer->paymentTerms->toArray();
|
|
unset($customerArray['payment_terms_id']);
|
|
return ApiDataTransformer::snakeToCamel($customerArray);
|
|
});
|
|
}
|
|
|
|
public function single($id)
|
|
{
|
|
$customer = Customer::with(['contacts' => function ($query) {
|
|
$query->orderBy('is_primary', 'desc');
|
|
}, 'paymentTerms'])->findOrFail($id);
|
|
|
|
$customerArray = $customer->toArray();
|
|
if ($customer->paymentTerms) {
|
|
$customerArray['payment_terms'] = $customer->paymentTerms->toArray();
|
|
unset($customerArray['payment_terms_id']);
|
|
}
|
|
return ApiDataTransformer::snakeToCamel($customerArray);
|
|
}
|
|
|
|
/**
|
|
* Generate a random available customer number
|
|
*/
|
|
public static function generateCustomerNumber()
|
|
{
|
|
$newNumber = null;
|
|
while (!$newNumber) {
|
|
// DATEV standard
|
|
$randomNumber = rand(10000, 69999);
|
|
$customer = Customer::firstWhere('customer_nr', $randomNumber);
|
|
if ($customer) {
|
|
return $randomNumber;
|
|
}
|
|
}
|
|
}
|
|
|
|
// // Beispiel für das Hochladen eines Kundenlogos
|
|
// if ($request->hasFile('customer_logo')) {
|
|
// $path = $request->file('customer_logo')->store('customer_logos', 'public');
|
|
// // Speichere den Pfad in der Datenbank
|
|
// $customer->logo_path = $path;
|
|
// $customer->save();
|
|
// }
|
|
|
|
// // Beispiel für das Hochladen eines Kontakt-Avatars
|
|
// if ($request->hasFile('contact_avatar')) {
|
|
// $path = $request->file('contact_avatar')->store('contact_avatars', 'public');
|
|
// // Speichere den Pfad in der Datenbank
|
|
// $contact->avatar_path = $path;
|
|
// $contact->save();
|
|
// }
|
|
}
|