Add LineItem CSV import and fix Unit API
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Inertia\Inertia;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\LineItem;
|
||||
use App\Models\Setting;
|
||||
use App\Models\Unit;
|
||||
use App\Mail\Reminder;
|
||||
use App\Support\ApiDataTransformer;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -107,6 +109,16 @@ public static function single($id)
|
||||
])->findOrFail($id);
|
||||
|
||||
$invoiceArray = $invoice->toArray();
|
||||
|
||||
$unitIds = array_unique(array_column($invoiceArray['items'], 'unit_id'));
|
||||
$units = Unit::whereIn('id', $unitIds)->get()->keyBy('id');
|
||||
$invoiceArray['items'] = array_map(function ($item) use ($units) {
|
||||
if (isset($item['unit_id']) && $units->has($item['unit_id'])) {
|
||||
$item['unit'] = $units->get($item['unit_id'])->toArray();
|
||||
}
|
||||
return $item;
|
||||
}, $invoiceArray['items']);
|
||||
|
||||
$invoiceArray['customer']['payment_terms'] = $invoice->customer->paymentTerms->toArray();
|
||||
unset($invoiceArray['customer']['payment_terms_id']);
|
||||
|
||||
@@ -211,7 +223,9 @@ public function exportPdf($id)
|
||||
'isPDF' => true,
|
||||
'fontPath' => public_path('storage/fonts/'),
|
||||
'giroCode' => $giroCode,
|
||||
'totalPages' => 1
|
||||
'totalPages' => 1,
|
||||
'companyName' => Setting::get('company.name'),
|
||||
'companyAddress' => json_decode(Setting::get('company.address'), true)
|
||||
];
|
||||
|
||||
$options = [
|
||||
@@ -476,7 +490,7 @@ public function store(Request $request)
|
||||
'items.*.title' => 'nullable|string',
|
||||
'items.*.description' => 'nullable|string',
|
||||
'items.*.quantity' => 'nullable|numeric',
|
||||
'items.*.unit' => 'nullable|string',
|
||||
'items.*.unitId' => 'nullable|integer',
|
||||
'items.*.price' => 'nullable|numeric',
|
||||
]);
|
||||
|
||||
@@ -563,7 +577,7 @@ public function update(Request $request, $id)
|
||||
'items.*.title' => 'nullable|string',
|
||||
'items.*.description' => 'nullable|string',
|
||||
'items.*.quantity' => 'nullable|numeric',
|
||||
'items.*.unit' => 'nullable|string',
|
||||
'items.*.unitId' => 'nullable|integer',
|
||||
'items.*.price' => 'nullable|numeric',
|
||||
]);
|
||||
|
||||
@@ -594,8 +608,8 @@ public function update(Request $request, $id)
|
||||
if (!empty($snakeCaseData['items'])) {
|
||||
foreach ($snakeCaseData['items'] as $item) {
|
||||
// Remove unit from item data if it's empty to let the database use the default value
|
||||
if (empty($item['unit'])) {
|
||||
unset($item['unit']);
|
||||
if (empty($item['unitId'])) {
|
||||
unset($item['unitId']);
|
||||
}
|
||||
if (isset($item['id']) && $item['id'] > 0) {
|
||||
// Update existing item
|
||||
|
||||
Reference in New Issue
Block a user