public function update($id, InitializationRequest $request) { try { DB::transaction(function () use($request, $id) { $currentYear = CommonHelper::get_current_financial_year(); $balanceInput = $request->input('balance'); foreach ($balanceInput as $code => $amount) { $WorkspaceLedger = new WorkspaceLedger(); $GeneralLedger = new GeneralLedger(); $WorkspaceLedger->workspace_id = $id; $WorkspaceLedger->year = $currentYear; $WorkspaceLedger->account_code = $code; $WorkspaceLedger->balance_type = Config::get('common.balance_type_opening'); $WorkspaceLedger->balance = $amount; $WorkspaceLedger->created_by = Auth::user()->id; $WorkspaceLedger->created_at = time(); $WorkspaceLedger->save(); $WorkspaceLedger = new WorkspaceLedger(); // Intermediate row insert $WorkspaceLedger->workspace_id = $id; $WorkspaceLedger->year = $currentYear; $WorkspaceLedger->account_code = $code; $WorkspaceLedger->balance_type = Config::get('common.balance_type_intermediate'); $WorkspaceLedger->balance = $amount; $WorkspaceLedger->created_by = Auth::user()->id; $WorkspaceLedger->created_at = time(); $WorkspaceLedger->save(); $existingGeneralData = GeneralLedger::where(['account_code' => $code, 'balance_type' => Config::get('common.balance_type_opening'), 'year' => $currentYear])->first(); if ($existingGeneralData) { $existingGeneral = GeneralLedger::firstOrNew(['account_code' => $code, 'balance_type' => Config::get('common.balance_type_opening'), 'year' => $currentYear]); $existingGeneral->year = $currentYear; $existingGeneral->account_code = $code; $existingGeneral->balance_type = Config::get('common.balance_type_opening'); $existingGeneral->balance = $existingGeneralData->balance + $amount; $existingGeneral->updated_by = Auth::user()->id; $existingGeneral->updated_at = time(); $existingGeneral->update(); } else { $GeneralLedger->year = $currentYear; $GeneralLedger->account_code = $code; $GeneralLedger->balance_type = Config::get('common.balance_type_opening'); $GeneralLedger->balance = $amount; $GeneralLedger->created_by = Auth::user()->id; $GeneralLedger->created_at = time(); $GeneralLedger->save(); } // // General Intermediate Data Insert/ Update // $GeneralLedger = New GeneralLedger; // $existingGeneralIntermediateData = GeneralLedger::where(['account_code' => $code, 'balance_type' => Config::get('common.balance_type_intermediate')])->first(); // // if($existingGeneralIntermediateData) // { // $existingGeneralIntermediate = GeneralLedger::firstOrNew(['account_code' => $code, 'balance_type' => Config::get('common.balance_type_intermediate')]); // // $existingGeneralIntermediate->year = date('Y'); // $existingGeneralIntermediate->account_code = $code; // $existingGeneralIntermediate->balance_type = Config::get('common.balance_type_intermediate'); // $existingGeneralIntermediate->balance = $existingGeneralData->balance + $amount; // $existingGeneralIntermediate->updated_by = Auth::user()->id; // $existingGeneralIntermediate->updated_at = time(); // $existingGeneralIntermediate->update(); // } // else // { // $GeneralLedger->year = date('Y'); // $GeneralLedger->account_code = $code; // $GeneralLedger->balance_type = Config::get('common.balance_type_intermediate'); // $GeneralLedger->balance = $amount; // $GeneralLedger->created_by = Auth::user()->id; // $GeneralLedger->created_at = time(); // $GeneralLedger->save(); // } } }); } catch (\Exception $e) { Session()->flash('error_message', 'Accounts not Initialized!'); return redirect('initializations'); } Session()->flash('flash_message', 'Accounts Initialized!'); return redirect('initializations'); }
public function store(Request $request) { try { DB::transaction(function () use($request) { $workspace_id = $request->workspace_id; if ($workspace_id > 0) { $currentYear = CommonHelper::get_current_financial_year(); $closingStatus = DB::table('account_closings')->where(['year' => $currentYear, 'workspace_id' => $workspace_id])->value('status'); $existingYearDetail = DB::table('financial_years')->where('year', $currentYear)->first(); if ($closingStatus == 1) { if ($existingYearDetail->end_date > strtotime(date('Y-m-d'))) { // Delete Workspace Closing Balance Data of Current Year WorkspaceLedger::where(['workspace_id' => $workspace_id, 'balance_type' => Config::get('common.balance_type_closing'), 'year' => CommonHelper::get_current_financial_year()])->delete(); // Delete Next Year Opening Balance Data WorkspaceLedger::where(['workspace_id' => $workspace_id, 'balance_type' => Config::get('common.balance_type_opening'), 'year' => CommonHelper::get_next_financial_year()])->delete(); // Delete Next Year Intermediate Balance Data WorkspaceLedger::where(['workspace_id' => $workspace_id, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => CommonHelper::get_next_financial_year()])->delete(); // Delete Stock Table Current Year Closing Balance Data Stock::where(['workspace_id' => $workspace_id, 'stock_type' => Config::get('common.balance_type_closing'), 'year' => CommonHelper::get_current_financial_year()])->delete(); // Delete Next Year Opening Balance Data Stock::where(['workspace_id' => $workspace_id, 'stock_type' => Config::get('common.balance_type_opening'), 'year' => CommonHelper::get_next_financial_year()])->delete(); // Delete Stock Table Next Year Intermediate Balance Data Stock::where(['workspace_id' => $workspace_id, 'stock_type' => Config::get('common.balance_type_intermediate'), 'year' => CommonHelper::get_next_financial_year()])->delete(); // Delete Account Closing Data AccountClosing::where(['workspace_id' => $workspace_id, 'year' => CommonHelper::get_current_financial_year(), 'type' => 1])->delete(); } else { Session()->flash('warning_message', 'Alert: Time Over!'); throw new \Exception('error'); } } else { Session()->flash('warning_message', 'Alert: Workspace Not Closed Yet!'); throw new \Exception('error'); } } else { // Total System is being rolled back to the previous year // Delete General Ledger Current Year Closing Data GeneralLedger::where(['balance_type' => Config::get('common.balance_type_closing'), 'year' => CommonHelper::get_previous_financial_year()])->delete(); // Delete General Ledger Next Year Opening Data GeneralLedger::where(['balance_type' => Config::get('common.balance_type_opening'), 'year' => CommonHelper::get_current_financial_year()])->delete(); // Delete General Journal Table Current Year Data GeneralJournal::where(['year' => CommonHelper::get_current_financial_year()])->delete(); // Delete Account Closing Data AccountClosing::where(['year' => CommonHelper::get_previous_financial_year(), 'type' => 2])->delete(); // Delete Raw Stock Table Previous Year Closing Balance Data RawStock::where(['stock_type' => Config::get('common.balance_type_closing'), 'year' => CommonHelper::get_previous_financial_year()])->delete(); // Delete Raw Stock Table Current Year Opening Balance Data RawStock::where(['stock_type' => Config::get('common.balance_type_opening'), 'year' => CommonHelper::get_current_financial_year()])->delete(); // Delete Raw Stock Table Current Year Intermediate Balance Data RawStock::where(['stock_type' => Config::get('common.balance_type_intermediate'), 'year' => CommonHelper::get_current_financial_year()])->delete(); // Workspaces Rollback $workspaces = Workspace::where('status', '=', 1)->get(); foreach ($workspaces as $workspace) { $workspace_id = $workspace->id; // Delete Workspace Closing Balance Data of Current Year WorkspaceLedger::where(['workspace_id' => $workspace_id, 'balance_type' => Config::get('common.balance_type_closing'), 'year' => CommonHelper::get_previous_financial_year()])->delete(); // Delete Next Year Opening Balance Data WorkspaceLedger::where(['workspace_id' => $workspace_id, 'balance_type' => Config::get('common.balance_type_opening'), 'year' => CommonHelper::get_current_financial_year()])->delete(); // Delete Next Year Intermediate Balance Data WorkspaceLedger::where(['workspace_id' => $workspace_id, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => CommonHelper::get_current_financial_year()])->delete(); // Delete Stock Table Current Year Closing Balance Data Stock::where(['workspace_id' => $workspace_id, 'stock_type' => Config::get('common.balance_type_closing'), 'year' => CommonHelper::get_previous_financial_year()])->delete(); // Delete Next Year Opening Balance Data Stock::where(['workspace_id' => $workspace_id, 'stock_type' => Config::get('common.balance_type_opening'), 'year' => CommonHelper::get_current_financial_year()])->delete(); // Delete Stock Table Next Year Intermediate Balance Data Stock::where(['workspace_id' => $workspace_id, 'stock_type' => Config::get('common.balance_type_intermediate'), 'year' => CommonHelper::get_current_financial_year()])->delete(); // Delete Account Closing Data AccountClosing::where(['workspace_id' => $workspace_id, 'year' => CommonHelper::get_previous_financial_year(), 'type' => 1])->delete(); } // Fiscal Year Table Operations $previous = CommonHelper::get_previous_financial_year(); $current = CommonHelper::get_current_financial_year(); // Previous Year Activate DB::table('financial_years')->where('year', $previous)->update(['status' => 1]); // Delete Current Financial Year FinancialYear::where(['year' => $current])->delete(); } }); } catch (\Exception $e) { Session()->flash('error_message', 'Rollback not done!'); return redirect('rollback'); } Session()->flash('flash_message', 'Rollback Successfully done!'); return redirect('rollback'); }