|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers; |
| 4 | + |
| 5 | +use App\Models\BlCuentaCobro; |
| 6 | +use App\Models\BlCuentaCobroItem; |
| 7 | +use App\Models\BLMarcacion; |
| 8 | +use Illuminate\Http\Request; |
| 9 | + |
| 10 | +use Illuminate\Support\Facades\DB; |
| 11 | + |
| 12 | +class BlCuentaCobroController extends Controller |
| 13 | +{ |
| 14 | + public function pasarPagados(Request $request) |
| 15 | + { |
| 16 | + $items = $request->input('items', []); |
| 17 | + if (empty($items)) { |
| 18 | + return back()->withErrors(['items' => 'No hay ítems seleccionados']); |
| 19 | + } |
| 20 | + $trabajadorId = $items[0]['user_id']; |
| 21 | + $total = collect($items)->sum('total'); |
| 22 | + DB::transaction(function () use ($trabajadorId, $total, $items) { |
| 23 | + $cuenta = BLCuentaCobro::create([ |
| 24 | + 'user_id' => $trabajadorId, |
| 25 | + 'fecha' => now(), |
| 26 | + 'total' => $total, |
| 27 | + ]); |
| 28 | + foreach ($items as $item) { |
| 29 | + BLCuentaCobroItem::create([ |
| 30 | + 'cuenta_cobro_id' => $cuenta->id, |
| 31 | + 'marcacion_id' => $item['marcacion_id'], |
| 32 | + ]); |
| 33 | + BLMarcacion::where('id', $item['marcacion_id'])->update(['pagado' => 1]); |
| 34 | + } |
| 35 | + }); |
| 36 | + return back()->with('success', 'Cuenta de cobro creada y items marcados como pagados.'); |
| 37 | + } |
| 38 | + // public function update($items) |
| 39 | + // { |
| 40 | + // foreach ($items as $item) { |
| 41 | + // $registro = BLMarcacion::findOrFail($item['marcacion_id']); |
| 42 | + // $registro->validate([ |
| 43 | + // 'marcacion_id' => 'required|integer|min:1' |
| 44 | + // ]); |
| 45 | + // $registro->update(['pagado' => 1]); |
| 46 | + // } |
| 47 | + // } |
| 48 | +} |
0 commit comments