<?php

namespace App\Http\Controllers;

use App\Models\Currency;
use App\Models\Voucher;
use Illuminate\Http\Request;

class VoucherController extends Controller
{
    public function index() { return view('vouchers.index', ['vouchers'=>Voucher::latest('voucher_date')->paginate(15),'currencies'=>Currency::where('is_active',true)->get()]); }
    public function store(Request $request) { $data=$request->validate(['type'=>['required','in:payment,receipt,journal'],'voucher_date'=>['required','date'],'party_name'=>['nullable','string','max:120'],'account_name'=>['nullable','string','max:100'],'amount'=>['required','numeric','gt:0'],'currency_code'=>['required','exists:currencies,code'],'description'=>['nullable','string']]); $data['voucher_number']='VCH-'.now()->format('ymd').'-'.str_pad((string)(Voucher::whereDate('created_at',today())->count()+1),3,'0',STR_PAD_LEFT); Voucher::create($data); return back()->with('success', __('Voucher saved.')); }
}
