Untitled

mail@pastecode.io avatar
unknown
plain_text
11 days ago
4.2 kB
4
Indexable
Never
<?php

namespace App\Http\Controllers;

use App\Models\accountcodes;
use App\Models\offices;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use App\Models\Cheque;
use App\Models\Disbursement;
use App\Models\particulars;
use Illuminate\Support\Facades\Auth;
use Barryvdh\DomPDF\Facade\Pdf;
use DB;
class ChequeController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function PushCheque(Request $request)
{
    if (Auth::check()) {
        $validator = Validator::make($request->all(), [
            'cheque.*.ChequeNumber' => 'required|string',
            'cheque.*.OrderNumber' => 'required|string',
            'cheque.*.Particular' => 'required|string',
            'cheque.*.Amount' => 'required|string',
            'cheque.*.OrderDate' => 'nullable|date',
            'cheque.*.BIRWithHoldings' => 'required|string',
            'cheque.*.Signatory' => 'nullable|string',
            'disbursement.*.AccountCode' => 'required|string',
            'disbursement.*.AccountName' => 'required|string',
            'disbursement.*.OfficeCode' => 'required|string',
            'disbursement.*.Particular2' => 'required|string',
            'disbursement.*.Amount2' => 'required|string',
        ]);

        if ($validator->fails()) {
            return response()->json(['errors' => $validator->errors()], 422);
        }

        try {
            foreach ($request->cheque as $chequeData) {
                if (!isset($chequeData['Amount'])) {
                    Log::error('Missing Amount in cheque data: ' . json_encode($chequeData));
                    continue;
                }

                $amount = $chequeData['Amount'];
                $formattedAmount = floatval(str_replace(',', '', $amount));

                $cheque = Cheque::create([
                    'ChequeNumber' => $chequeData['ChequeNumber'],
                    'OrderNumber' => $chequeData['OrderNumber'],
                    'Particular' => $chequeData['Particular'],
                    'Amount' => $formattedAmount,
                    'OrderDate' => $chequeData['OrderDate'],
                ]);

                Log::info('Cheque created: ' . json_encode($cheque->toArray()));
            }

            foreach ($request->disbursement as $disbursementData) {
                $formattedAmount = floatval(str_replace(',', '', $disbursementData['Amount2']));

                Disbursement::create([
                    'AccountCode' => $disbursementData['AccountCode'],
                    'AccountName' => $disbursementData['AccountName'],
                    'OfficeCode' => $disbursementData['OfficeCode'],
                    'Particular' => $disbursementData['Particular2'],
                    'Amount' => $formattedAmount,
                    'ChequeRef' => $cheque->id,
                ]);
            }

        } catch (\Exception $exception) {
            Log::error('Error in processing cheque and disbursement: ' . $exception->getMessage());
            return response()->json(['error' => 'Failed to process data'], 500);
        }
    } else {
        return view('403.unauthorized');
    }
}

    public function getAccounts(){
        if (Auth::check()) {
            $offices = offices::all();
            $accounts = accountcodes::all();
            $particulars = particulars::all();
            $cheque = Cheque::select('cheques.ChequeNumber', 'cheques.OrderNumber',
                                     'cheques.Particular', 'cheques.Amount',
                                     'disbursements.AccountCode','disbursements.AccountName',
                                     'disbursements.Particular','disbursements.Amount', 'disbursements.ChequeRef')
            ->leftJoin('disbursements', 'disbursements.ChequeRef', '=', 'cheques.id')
            ->get();
            $cheques = Cheque::all();
            return view('index', compact('offices','accounts','particulars', 'cheque', 'cheques'));
        }else{
            return view('403.unauthorized');
        }
    }
}
Leave a Comment