Last active
May 15, 2026 00:38
-
-
Save codigoconjuan/04865857890cdf44ffe5562cb9361180 to your computer and use it in GitHub Desktop.
Almacenar Gastos de Ticket
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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