[Feature] Import LineItems from from CSV and products
This commit is contained in:
@@ -57,10 +57,9 @@ public function index($invoiceId)
|
||||
* ;"Hosting";"Annual hosting";1.500,50;"lump sum";1.200,00
|
||||
*
|
||||
* @param Request $request The HTTP request with the CSV file
|
||||
* @param int $invoiceId The ID of the invoice for which the items will be imported
|
||||
* @return \Illuminate\Http\JsonResponse A JSON response with the imported items or an error message
|
||||
*/
|
||||
public function importFromCsv(Request $request, $invoiceId)
|
||||
public function importFromCsv(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'csv' => 'required|file|mimes:csv,txt'
|
||||
@@ -133,16 +132,22 @@ public function importFromCsv(Request $request, $invoiceId)
|
||||
}
|
||||
|
||||
$lineItems[] = [
|
||||
'invoice_id' => $invoiceId,
|
||||
'id' => 0,
|
||||
'invoice_id' => 0,
|
||||
'position' => $position,
|
||||
'is_section' => false,
|
||||
'title' => $itemData['title'],
|
||||
'description' => $itemData['description'] ?? '',
|
||||
'quantity' => (float)$quantity,
|
||||
'unit_id' => $unit->id,
|
||||
'unit' => [
|
||||
'id' => $unit->id,
|
||||
'name' => $unit->name,
|
||||
'symbol' => $unit->symbol,
|
||||
'created_at' => $unit->created_at,
|
||||
'updated_at' => $unit->updated_at,
|
||||
],
|
||||
'price' => (float)$price,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now()
|
||||
];
|
||||
}
|
||||
|
||||
@@ -150,28 +155,9 @@ public function importFromCsv(Request $request, $invoiceId)
|
||||
return response()->json(['message' => 'No valid items found in the CSV file'], 400);
|
||||
}
|
||||
|
||||
// Delete existing items for this invoice
|
||||
LineItem::where('invoice_id', $invoiceId)->delete();
|
||||
|
||||
// Insert new items
|
||||
LineItem::insert($lineItems);
|
||||
|
||||
// Return the newly created items
|
||||
$items = LineItem::with('unit')
|
||||
->where('invoice_id', $invoiceId)
|
||||
->orderBy('position', 'asc')
|
||||
->get();
|
||||
|
||||
return $items->map(function ($item) {
|
||||
$itemArray = $item->toArray();
|
||||
|
||||
if ($item->unit) {
|
||||
$itemArray['unit_name'] = $item->unit->name;
|
||||
$itemArray['unit_symbol'] = $item->unit->symbol;
|
||||
}
|
||||
|
||||
return ApiDataTransformer::snakeToCamel($itemArray);
|
||||
});
|
||||
return response()->json(
|
||||
ApiDataTransformer::snakeToCamel($lineItems)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user