Skip to content

Instantly share code, notes, and snippets.

@codigoconjuan
Last active May 15, 2026 00:38
Show Gist options
  • Select an option

  • Save codigoconjuan/04865857890cdf44ffe5562cb9361180 to your computer and use it in GitHub Desktop.

Select an option

Save codigoconjuan/04865857890cdf44ffe5562cb9361180 to your computer and use it in GitHub Desktop.
Almacenar Gastos de Ticket
private function createExpenses(Budget $budget, string $store, string $category, array $items): array
{
$created = [];
foreach ($items as $item) {
$expense = Expense::create([
'budget_id' => $budget->id,
'name' => $store . ' - ' . $item['name'],
'amount' => $item['amount'],
'category' => $budget->isGeneral() ? $category : null,
]);
$cat = $expense->category ? $expense->category->label() : 'Sin categoría';
$created[] = "- {$expense->name}: \${$expense->amount} ({$cat})";
}
$total = array_sum(array_column($items, 'amount'));
return [
'success' => true,
'message' => "Se registraron " . count($created) . " gastos del ticket:\n" .
implode("\n", $created) .
"\nTotal: \${$total}",
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment