public function store(TransactionRecorderRequest $request)
 {
     try {
         DB::transaction(function () use($request) {
             $recorder = new TransactionRecorder();
             $slice = substr($request->account_code, 0, 1);
             $currentYear = CommonHelper::get_current_financial_year();
             if ($slice == 1 || $slice == 2 || $slice == 3) {
                 $recorder->from_whom_type = $request->from_whom_type;
                 $recorder->from_whom = $request->from_whom;
                 $recorder->total_amount = $request->total_amount;
                 $recorder->amount = $request->amount;
                 $recorder->transaction_detail = $request->transaction_detail;
             } elseif ($slice == 4) {
                 $recorder->to_whom_type = $request->to_whom_type;
                 $recorder->to_whom = $request->to_whom;
                 $recorder->total_amount = $request->total_amount;
                 $recorder->amount = $request->amount;
                 $recorder->transaction_detail = $request->transaction_detail;
             } elseif ($slice == 5 || $slice == 6) {
                 $recorder->amount = $request->amount;
             }
             $recorder->date = $request->date;
             $recorder->year = $currentYear;
             $recorder->workspace_id = Auth::user()->workspace_id;
             $recorder->account_code = $request->account_code;
             $recorder->created_by = Auth::user()->id;
             $recorder->created_at = time();
             $recorder->save();
             // IMPACTS ON ACCOUNTING TABLES
             $workspace_id = Auth::user()->workspace_id;
             $cashCode = 11000;
             $accountReceivableCode = 12000;
             $accountPayableCode = 41000;
             $revenueCode = 30000;
             $expenseCode = 20000;
             $drawCode = 50000;
             $investmentCode = 60000;
             $officeSuppliesCode = 27000;
             if ($request->account_code == 12000) {
                 // ACCOUNT RECEIVABLE
                 // Workspace Ledger Account Receivable Credit(-)
                 $accountReceivableWorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => $accountReceivableCode, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
                 $accountReceivableWorkspaceData->balance -= $request->amount;
                 $accountReceivableWorkspaceData->update();
                 // Workspace Ledger Cash Debit(+)
                 $cashWorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => $cashCode, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
                 $cashWorkspaceData->balance += $request->amount;
                 $cashWorkspaceData->update();
                 // Personal Account Due(-)
                 $person_type = $request->from_whom_type;
                 $person_id = $request->from_whom;
                 $personData = PersonalAccount::where(['person_id' => $person_id, 'person_type' => $person_type])->first();
                 $personData->due -= $request->amount;
                 $personData->update();
                 // General Journals Account Receivable Credit
                 $generalJournal = new GeneralJournal();
                 $generalJournal->date = strtotime($request->date);
                 $generalJournal->transaction_type = Config::get('common.transaction_type.general');
                 $generalJournal->reference_id = $recorder->id;
                 $generalJournal->year = $currentYear;
                 $generalJournal->account_code = $accountReceivableCode;
                 $generalJournal->workspace_id = $workspace_id;
                 $generalJournal->amount = $request->amount;
                 $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                 $generalJournal->created_by = Auth::user()->id;
                 $generalJournal->created_at = time();
                 $generalJournal->save();
                 // General Journals Cash Debit
                 $generalJournal = new GeneralJournal();
                 $generalJournal->date = strtotime($request->date);
                 $generalJournal->transaction_type = Config::get('common.transaction_type.general');
                 $generalJournal->reference_id = $recorder->id;
                 $generalJournal->year = $currentYear;
                 $generalJournal->account_code = $cashCode;
                 $generalJournal->workspace_id = $workspace_id;
                 $generalJournal->amount = $request->amount;
                 $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                 $generalJournal->created_by = Auth::user()->id;
                 $generalJournal->created_at = time();
                 $generalJournal->save();
             } elseif ($request->account_code == 41000) {
                 // ACCOUNT PAYABLE
                 // Account Payable Debit(-)
                 $accountPayableWorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => $accountPayableCode, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
                 $accountPayableWorkspaceData->balance -= $request->amount;
                 $accountPayableWorkspaceData->update();
                 // Workspace Ledger Cash Credit(-)
                 $cashWorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => $cashCode, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
                 $cashWorkspaceData->balance -= $request->amount;
                 $cashWorkspaceData->update();
                 // Personal Account Balance(-)
                 $person_type = $request->to_whom_type;
                 $person_id = $request->to_whom;
                 $personData = PersonalAccount::where(['person_id' => $person_id, 'person_type' => $person_type])->first();
                 $personData->balance -= $request->amount;
                 $personData->update();
                 // General Journals Account Payable Debit
                 $generalJournal = new GeneralJournal();
                 $generalJournal->date = strtotime($request->date);
                 $generalJournal->transaction_type = Config::get('common.transaction_type.general');
                 $generalJournal->reference_id = $recorder->id;
                 $generalJournal->year = $currentYear;
                 $generalJournal->account_code = $accountPayableCode;
                 $generalJournal->workspace_id = $workspace_id;
                 $generalJournal->amount = $request->amount;
                 $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                 $generalJournal->created_by = Auth::user()->id;
                 $generalJournal->created_at = time();
                 $generalJournal->save();
                 // General Journals Cash Credit
                 $generalJournal = new GeneralJournal();
                 $generalJournal->date = strtotime($request->date);
                 $generalJournal->transaction_type = Config::get('common.transaction_type.general');
                 $generalJournal->reference_id = $recorder->id;
                 $generalJournal->year = $currentYear;
                 $generalJournal->account_code = $cashCode;
                 $generalJournal->workspace_id = $workspace_id;
                 $generalJournal->amount = $request->amount;
                 $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                 $generalJournal->created_by = Auth::user()->id;
                 $generalJournal->created_at = time();
                 $generalJournal->save();
             } elseif ($request->account_code == 30000) {
                 // REVENUE
                 $due = $request->total_amount - $request->amount;
                 // Revenue Credit(+) with Total Amount
                 $revenueWorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => $revenueCode, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
                 $revenueWorkspaceData->balance += $request->total_amount;
                 $revenueWorkspaceData->update();
                 // Workspace Ledger Cash Debit(+)
                 $cashWorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => $cashCode, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
                 $cashWorkspaceData->balance += $request->amount;
                 $cashWorkspaceData->update();
                 // Workspace Ledger Account Receivable Debit(+)
                 $accountReceivableWorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => $accountReceivableCode, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
                 $accountReceivableWorkspaceData->balance += $due;
                 $accountReceivableWorkspaceData->update();
                 if ($due > 0) {
                     // Personal Account Due(+)
                     $person_type = $request->from_whom_type;
                     $person_id = $request->from_whom;
                     $personData = PersonalAccount::where(['person_id' => $person_id, 'person_type' => $person_type])->first();
                     $personData->due += $due;
                     $personData->update();
                     // General Journals Account Receivable Debit with due
                     $generalJournal = new GeneralJournal();
                     $generalJournal->date = strtotime($request->date);
                     $generalJournal->transaction_type = Config::get('common.transaction_type.general');
                     $generalJournal->reference_id = $recorder->id;
                     $generalJournal->year = $currentYear;
                     $generalJournal->account_code = $accountReceivableCode;
                     $generalJournal->workspace_id = $workspace_id;
                     $generalJournal->amount = $due;
                     $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                     $generalJournal->created_by = Auth::user()->id;
                     $generalJournal->created_at = time();
                     $generalJournal->save();
                 }
                 // General Journals Revenue Credit with total amount
                 $generalJournal = new GeneralJournal();
                 $generalJournal->date = strtotime($request->date);
                 $generalJournal->transaction_type = Config::get('common.transaction_type.general');
                 $generalJournal->reference_id = $recorder->id;
                 $generalJournal->year = $currentYear;
                 $generalJournal->account_code = $revenueCode;
                 $generalJournal->workspace_id = $workspace_id;
                 $generalJournal->amount = $request->total_amount;
                 $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                 $generalJournal->created_by = Auth::user()->id;
                 $generalJournal->created_at = time();
                 $generalJournal->save();
                 // General Journals Cash Debit with paid
                 $generalJournal = new GeneralJournal();
                 $generalJournal->date = strtotime($request->date);
                 $generalJournal->transaction_type = Config::get('common.transaction_type.general');
                 $generalJournal->reference_id = $recorder->id;
                 $generalJournal->year = $currentYear;
                 $generalJournal->account_code = $cashCode;
                 $generalJournal->workspace_id = $workspace_id;
                 $generalJournal->amount = $request->amount;
                 $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                 $generalJournal->created_by = Auth::user()->id;
                 $generalJournal->created_at = time();
                 $generalJournal->save();
             } elseif ($request->account_code == 20000) {
                 // EXPENSE
                 // Workspace Ledger Expense Account Debit(+)
                 $expenseWorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => $expenseCode, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
                 $expenseWorkspaceData->balance += $request->total_amount;
                 $expenseWorkspaceData->update();
                 // Workspace Ledger Cash Credit(-)
                 $due = $request->total_amount - $request->amount;
                 $cashWorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => $cashCode, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
                 $cashWorkspaceData->balance -= $request->amount;
                 $cashWorkspaceData->update();
                 // Workspace Ledger Account Payable Credit with Due(+)
                 $accountPayableWorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => $accountPayableCode, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
                 $accountPayableWorkspaceData->balance += $due;
                 $accountPayableWorkspaceData->update();
                 if ($due > 0) {
                     // Personal Account Due(+)
                     $person_type = $request->from_whom_type;
                     $person_id = $request->from_whom;
                     $personData = PersonalAccount::where(['person_id' => $person_id, 'person_type' => $person_type])->first();
                     $personData->balance += $due;
                     $personData->update();
                     // General Journals Account Payable Credit
                     $generalJournal = new GeneralJournal();
                     $generalJournal->date = strtotime($request->date);
                     $generalJournal->transaction_type = Config::get('common.transaction_type.general');
                     $generalJournal->reference_id = $recorder->id;
                     $generalJournal->year = $currentYear;
                     $generalJournal->account_code = $accountPayableCode;
                     $generalJournal->workspace_id = $workspace_id;
                     $generalJournal->amount = $due;
                     $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                     $generalJournal->created_by = Auth::user()->id;
                     $generalJournal->created_at = time();
                     $generalJournal->save();
                 }
                 // General Journals Expense Debit
                 $generalJournal = new GeneralJournal();
                 $generalJournal->date = strtotime($request->date);
                 $generalJournal->transaction_type = Config::get('common.transaction_type.general');
                 $generalJournal->reference_id = $recorder->id;
                 $generalJournal->year = $currentYear;
                 $generalJournal->account_code = $expenseCode;
                 $generalJournal->workspace_id = $workspace_id;
                 $generalJournal->amount = $request->total_amount;
                 $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                 $generalJournal->created_by = Auth::user()->id;
                 $generalJournal->created_at = time();
                 $generalJournal->save();
                 // General Journals Cash Credit
                 $generalJournal = new GeneralJournal();
                 $generalJournal->date = strtotime($request->date);
                 $generalJournal->transaction_type = Config::get('common.transaction_type.general');
                 $generalJournal->reference_id = $recorder->id;
                 $generalJournal->year = $currentYear;
                 $generalJournal->account_code = $cashCode;
                 $generalJournal->workspace_id = $workspace_id;
                 $generalJournal->amount = $request->amount;
                 $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                 $generalJournal->created_by = Auth::user()->id;
                 $generalJournal->created_at = time();
                 $generalJournal->save();
             } elseif ($request->account_code == 50000) {
                 // DRAW
                 // Workspace Ledger Cash Credit(-)
                 $cashWorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => $cashCode, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
                 $cashWorkspaceData->balance -= $request->amount;
                 $cashWorkspaceData->update();
                 // Workspace Ledger Draw Debit(+)
                 $drawWorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => $drawCode, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
                 $drawWorkspaceData->balance += $request->amount;
                 $drawWorkspaceData->update();
                 // General Journals Draw Debit
                 $generalJournal = new GeneralJournal();
                 $generalJournal->date = strtotime($request->date);
                 $generalJournal->transaction_type = Config::get('common.transaction_type.general');
                 $generalJournal->reference_id = $recorder->id;
                 $generalJournal->year = $currentYear;
                 $generalJournal->account_code = $drawCode;
                 $generalJournal->workspace_id = $workspace_id;
                 $generalJournal->amount = $request->amount;
                 $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                 $generalJournal->created_by = Auth::user()->id;
                 $generalJournal->created_at = time();
                 $generalJournal->save();
                 // General Journals Cash Credit
                 $generalJournal = new GeneralJournal();
                 $generalJournal->date = strtotime($request->date);
                 $generalJournal->transaction_type = Config::get('common.transaction_type.general');
                 $generalJournal->reference_id = $recorder->id;
                 $generalJournal->year = $currentYear;
                 $generalJournal->account_code = $cashCode;
                 $generalJournal->workspace_id = $workspace_id;
                 $generalJournal->amount = $request->amount;
                 $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                 $generalJournal->created_by = Auth::user()->id;
                 $generalJournal->created_at = time();
                 $generalJournal->save();
             } elseif ($request->account_code == 60000) {
                 // OWNERS INVESTMENT
                 // Workspace Ledger Cash Debit(+)
                 $cashWorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => $cashCode, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
                 $cashWorkspaceData->balance += $request->amount;
                 $cashWorkspaceData->update();
                 // Workspace Ledger Investment Account Credit(+)
                 $investmentWorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => $investmentCode, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
                 $investmentWorkspaceData->balance += $request->amount;
                 $investmentWorkspaceData->update();
                 // General Journals Investment Credit
                 $generalJournal = new GeneralJournal();
                 $generalJournal->date = strtotime($request->date);
                 $generalJournal->transaction_type = Config::get('common.transaction_type.general');
                 $generalJournal->reference_id = $recorder->id;
                 $generalJournal->year = $currentYear;
                 $generalJournal->account_code = $investmentCode;
                 $generalJournal->workspace_id = $workspace_id;
                 $generalJournal->amount = $request->amount;
                 $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                 $generalJournal->created_by = Auth::user()->id;
                 $generalJournal->created_at = time();
                 $generalJournal->save();
                 // General Journals Cash Debit
                 $generalJournal = new GeneralJournal();
                 $generalJournal->date = strtotime($request->date);
                 $generalJournal->transaction_type = Config::get('common.transaction_type.general');
                 $generalJournal->reference_id = $recorder->id;
                 $generalJournal->year = $currentYear;
                 $generalJournal->account_code = $cashCode;
                 $generalJournal->workspace_id = $workspace_id;
                 $generalJournal->amount = $request->amount;
                 $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                 $generalJournal->created_by = Auth::user()->id;
                 $generalJournal->created_at = time();
                 $generalJournal->save();
             } elseif ($request->account_code == 27000) {
                 // OFFICE SUPPLIES
                 // Workspace Ledger Office Supplies Account Debit(+)
                 $due = $request->total_amount - $request->amount;
                 $ofcSuppliesWorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => $officeSuppliesCode, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
                 $ofcSuppliesWorkspaceData->balance += $request->total_amount;
                 $ofcSuppliesWorkspaceData->update();
                 // Workspace Ledger Cash Credit(-)
                 $cashWorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => $cashCode, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
                 $cashWorkspaceData->balance -= $request->amount;
                 $cashWorkspaceData->update();
                 // Workspace Ledger Account Payable Credit with Due(+)
                 $accountPayableWorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => $accountPayableCode, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
                 $accountPayableWorkspaceData->balance += $due;
                 $accountPayableWorkspaceData->update();
                 if ($due > 0) {
                     // Personal Account Due(+)
                     $person_type = $request->from_whom_type;
                     $person_id = $request->from_whom;
                     $personData = PersonalAccount::where(['person_id' => $person_id, 'person_type' => $person_type])->first();
                     $personData->balance += $due;
                     $personData->update();
                     // General Journals Account Payable Credit
                     $generalJournal = new GeneralJournal();
                     $generalJournal->date = strtotime($request->date);
                     $generalJournal->transaction_type = Config::get('common.transaction_type.general');
                     $generalJournal->reference_id = $recorder->id;
                     $generalJournal->year = $currentYear;
                     $generalJournal->account_code = $accountPayableCode;
                     $generalJournal->workspace_id = $workspace_id;
                     $generalJournal->amount = $due;
                     $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                     $generalJournal->created_by = Auth::user()->id;
                     $generalJournal->created_at = time();
                     $generalJournal->save();
                 }
                 // General Journals Office Supply Debit
                 $generalJournal = new GeneralJournal();
                 $generalJournal->date = strtotime($request->date);
                 $generalJournal->transaction_type = Config::get('common.transaction_type.general');
                 $generalJournal->reference_id = $recorder->id;
                 $generalJournal->year = $currentYear;
                 $generalJournal->account_code = $officeSuppliesCode;
                 $generalJournal->workspace_id = $workspace_id;
                 $generalJournal->amount = $request->total_amount;
                 $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                 $generalJournal->created_by = Auth::user()->id;
                 $generalJournal->created_at = time();
                 $generalJournal->save();
                 // General Journals Cash Credit
                 $generalJournal = new GeneralJournal();
                 $generalJournal->date = strtotime($request->date);
                 $generalJournal->transaction_type = Config::get('common.transaction_type.general');
                 $generalJournal->reference_id = $recorder->id;
                 $generalJournal->year = $currentYear;
                 $generalJournal->account_code = $cashCode;
                 $generalJournal->workspace_id = $workspace_id;
                 $generalJournal->amount = $request->amount;
                 $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                 $generalJournal->created_by = Auth::user()->id;
                 $generalJournal->created_at = time();
                 $generalJournal->save();
             } elseif ($request->account_code == 29000 || $request->account_code == 29100 || $request->account_code == 29200 || $request->account_code == 29300 || $request->account_code == 29400 || $request->account_code == 29500 || $request->account_code == 29600 || $request->account_code == 29700 || $request->account_code == 29800 || $request->account_code == 29910 || $request->account_code == 29950 || $request->account_code == 29980 || $request->account_code == 29990 || $request->account_code == 29991 || $request->account_code == 23000) {
                 $due = $request->total_amount - $request->amount;
                 $accountCode = $request->account_code;
                 // Workspace Ledger Account Debit(+)
                 $repairWorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => $accountCode, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
                 $repairWorkspaceData->balance += $request->total_amount;
                 $repairWorkspaceData->update();
                 // Workspace Ledger Cash Credit(-)
                 $cashWorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => $cashCode, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
                 $cashWorkspaceData->balance -= $request->amount;
                 $cashWorkspaceData->update();
                 // Workspace Ledger Account Payable Credit with Due(+)
                 $accountPayableWorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => $accountPayableCode, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
                 $accountPayableWorkspaceData->balance += $due;
                 $accountPayableWorkspaceData->update();
                 if ($due > 0) {
                     // Personal Account Due(+)
                     $person_type = $request->from_whom_type;
                     $person_id = $request->from_whom;
                     $personData = PersonalAccount::where(['person_id' => $person_id, 'person_type' => $person_type])->first();
                     $personData->balance += $due;
                     $personData->update();
                     // General Journals Account Payable Credit
                     $generalJournal = new GeneralJournal();
                     $generalJournal->date = strtotime($request->date);
                     $generalJournal->transaction_type = Config::get('common.transaction_type.general');
                     $generalJournal->reference_id = $recorder->id;
                     $generalJournal->year = $currentYear;
                     $generalJournal->account_code = $accountPayableCode;
                     $generalJournal->workspace_id = $workspace_id;
                     $generalJournal->amount = $due;
                     $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                     $generalJournal->created_by = Auth::user()->id;
                     $generalJournal->created_at = time();
                     $generalJournal->save();
                 }
                 // General Journals Debit
                 $generalJournal = new GeneralJournal();
                 $generalJournal->date = strtotime($request->date);
                 $generalJournal->transaction_type = Config::get('common.transaction_type.general');
                 $generalJournal->reference_id = $recorder->id;
                 $generalJournal->year = $currentYear;
                 $generalJournal->account_code = $accountCode;
                 $generalJournal->workspace_id = $workspace_id;
                 $generalJournal->amount = $request->total_amount;
                 $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                 $generalJournal->created_by = Auth::user()->id;
                 $generalJournal->created_at = time();
                 $generalJournal->save();
                 // General Journals Cash Credit
                 $generalJournal = new GeneralJournal();
                 $generalJournal->date = strtotime($request->date);
                 $generalJournal->transaction_type = Config::get('common.transaction_type.general');
                 $generalJournal->reference_id = $recorder->id;
                 $generalJournal->year = $currentYear;
                 $generalJournal->account_code = $cashCode;
                 $generalJournal->workspace_id = $workspace_id;
                 $generalJournal->amount = $request->amount;
                 $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                 $generalJournal->created_by = Auth::user()->id;
                 $generalJournal->created_at = time();
                 $generalJournal->save();
             } elseif ($request->account_code == 29930 || $request->account_code == 29960 || $request->account_code == 29996) {
                 // DONATION, JAKAT or Middleman Commission
                 $accountCode = $request->account_code;
                 // Workspace Ledger Cash Credit(-)
                 $cashWorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => $cashCode, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
                 $cashWorkspaceData->balance -= $request->amount;
                 $cashWorkspaceData->update();
                 // Workspace Ledger Account Debit(+)
                 $repairWorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => $accountCode, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
                 $repairWorkspaceData->balance += $request->amount;
                 $repairWorkspaceData->update();
                 // General Journals Debit
                 $generalJournal = new GeneralJournal();
                 $generalJournal->date = strtotime($request->date);
                 $generalJournal->transaction_type = Config::get('common.transaction_type.general');
                 $generalJournal->reference_id = $recorder->id;
                 $generalJournal->year = $currentYear;
                 $generalJournal->account_code = $accountCode;
                 $generalJournal->workspace_id = $workspace_id;
                 $generalJournal->amount = $request->amount;
                 $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                 $generalJournal->created_by = Auth::user()->id;
                 $generalJournal->created_at = time();
                 $generalJournal->save();
                 // General Journals Credit
                 $generalJournal = new GeneralJournal();
                 $generalJournal->date = strtotime($request->date);
                 $generalJournal->transaction_type = Config::get('common.transaction_type.general');
                 $generalJournal->reference_id = $recorder->id;
                 $generalJournal->year = $currentYear;
                 $generalJournal->account_code = $cashCode;
                 $generalJournal->workspace_id = $workspace_id;
                 $generalJournal->amount = $request->amount;
                 $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                 $generalJournal->created_by = Auth::user()->id;
                 $generalJournal->created_at = time();
                 $generalJournal->save();
             } elseif ($request->account_code == 29940) {
                 $accountCode = $request->cash_adjustment_type;
                 if ($accountCode == 29994) {
                     // Invisible Expense
                     // Workspace Ledger Invisible Expense Account Debit(+)
                     $WorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => $accountCode, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
                     $WorkspaceData->balance += $request->amount;
                     $WorkspaceData->update();
                     // Workspace Ledger Cash Credit(-)
                     $cashWorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => $cashCode, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
                     $cashWorkspaceData->balance -= $request->amount;
                     $cashWorkspaceData->update();
                     // General Journals Invisible Expense Debit
                     $generalJournal = new GeneralJournal();
                     $generalJournal->date = strtotime($request->date);
                     $generalJournal->transaction_type = Config::get('common.transaction_type.general');
                     $generalJournal->reference_id = $recorder->id;
                     $generalJournal->year = $currentYear;
                     $generalJournal->account_code = $accountCode;
                     $generalJournal->workspace_id = $workspace_id;
                     $generalJournal->amount = $request->amount;
                     $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                     $generalJournal->created_by = Auth::user()->id;
                     $generalJournal->created_at = time();
                     $generalJournal->save();
                     // General Journals Cash Credit
                     $generalJournal = new GeneralJournal();
                     $generalJournal->date = strtotime($request->date);
                     $generalJournal->transaction_type = Config::get('common.transaction_type.general');
                     $generalJournal->reference_id = $recorder->id;
                     $generalJournal->year = $currentYear;
                     $generalJournal->account_code = $cashCode;
                     $generalJournal->workspace_id = $workspace_id;
                     $generalJournal->amount = $request->amount;
                     $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                     $generalJournal->created_by = Auth::user()->id;
                     $generalJournal->created_at = time();
                     $generalJournal->save();
                 } elseif ($request->cash_adjustment_type == 37000) {
                     // Invisible Income
                     // Workspace Ledger Invisible Income Account Credit(+)
                     $WorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => $accountCode, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
                     $WorkspaceData->balance += $request->amount;
                     $WorkspaceData->update();
                     // Workspace Ledger Cash Debit(+)
                     $cashWorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => $cashCode, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
                     $cashWorkspaceData->balance += $request->amount;
                     $cashWorkspaceData->update();
                     // General Journals Invisible Income Credit
                     $generalJournal = new GeneralJournal();
                     $generalJournal->date = strtotime($request->date);
                     $generalJournal->transaction_type = Config::get('common.transaction_type.general');
                     $generalJournal->reference_id = $recorder->id;
                     $generalJournal->year = $currentYear;
                     $generalJournal->account_code = $accountCode;
                     $generalJournal->workspace_id = $workspace_id;
                     $generalJournal->amount = $request->amount;
                     $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                     $generalJournal->created_by = Auth::user()->id;
                     $generalJournal->created_at = time();
                     $generalJournal->save();
                     // General Journals Cash Debit
                     $generalJournal = new GeneralJournal();
                     $generalJournal->date = strtotime($request->date);
                     $generalJournal->transaction_type = Config::get('common.transaction_type.general');
                     $generalJournal->reference_id = $recorder->id;
                     $generalJournal->year = $currentYear;
                     $generalJournal->account_code = $cashCode;
                     $generalJournal->workspace_id = $workspace_id;
                     $generalJournal->amount = $request->amount;
                     $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                     $generalJournal->created_by = Auth::user()->id;
                     $generalJournal->created_at = time();
                     $generalJournal->save();
                 }
             } elseif ($request->account_code == 12100) {
                 // Loan Receive
                 $accountCode = 41400;
                 // Workspace Ledger Loan Payable Account Credit(+)
                 $WorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => $accountCode, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
                 $WorkspaceData->balance += $request->amount;
                 $WorkspaceData->update();
                 // Workspace Ledger Cash Debit(+)
                 $cashWorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => $cashCode, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
                 $cashWorkspaceData->balance += $request->amount;
                 $cashWorkspaceData->update();
                 // Personal Account balance(+)
                 $person_type = $request->from_whom_type;
                 $person_id = $request->from_whom;
                 $personData = PersonalAccount::where(['person_id' => $person_id, 'person_type' => $person_type])->first();
                 $personData->balance += $request->amount;
                 $personData->update();
                 // General Journals Loan Payable Credit
                 $generalJournal = new GeneralJournal();
                 $generalJournal->date = strtotime($request->date);
                 $generalJournal->transaction_type = Config::get('common.transaction_type.general');
                 $generalJournal->reference_id = $recorder->id;
                 $generalJournal->year = $currentYear;
                 $generalJournal->account_code = $accountCode;
                 $generalJournal->workspace_id = $workspace_id;
                 $generalJournal->amount = $request->amount;
                 $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                 $generalJournal->created_by = Auth::user()->id;
                 $generalJournal->created_at = time();
                 $generalJournal->save();
                 // General Journals Cash Debit
                 $generalJournal = new GeneralJournal();
                 $generalJournal->date = strtotime($request->date);
                 $generalJournal->transaction_type = Config::get('common.transaction_type.general');
                 $generalJournal->reference_id = $recorder->id;
                 $generalJournal->year = $currentYear;
                 $generalJournal->account_code = $cashCode;
                 $generalJournal->workspace_id = $workspace_id;
                 $generalJournal->amount = $request->amount;
                 $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                 $generalJournal->created_by = Auth::user()->id;
                 $generalJournal->created_at = time();
                 $generalJournal->save();
             } elseif ($request->account_code == 41400) {
                 // Loan Pay
                 $accountCode = $request->account_code;
                 // Workspace Ledger Loan Payable Account Debit(-)
                 $WorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => $accountCode, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
                 $WorkspaceData->balance -= $request->amount;
                 $WorkspaceData->update();
                 // Workspace Ledger Cash Credit(-)
                 $cashWorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => $cashCode, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
                 $cashWorkspaceData->balance -= $request->amount;
                 $cashWorkspaceData->update();
                 // Personal Account balance(-)
                 $person_type = $request->from_whom_type;
                 $person_id = $request->from_whom;
                 $personData = PersonalAccount::where(['person_id' => $person_id, 'person_type' => $person_type])->first();
                 $personData->balance -= $request->amount;
                 $personData->update();
                 // General Journals Loan Payable Debit
                 $generalJournal = new GeneralJournal();
                 $generalJournal->date = strtotime($request->date);
                 $generalJournal->transaction_type = Config::get('common.transaction_type.general');
                 $generalJournal->reference_id = $recorder->id;
                 $generalJournal->year = $currentYear;
                 $generalJournal->account_code = $accountCode;
                 $generalJournal->workspace_id = $workspace_id;
                 $generalJournal->amount = $request->amount;
                 $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                 $generalJournal->created_by = Auth::user()->id;
                 $generalJournal->created_at = time();
                 $generalJournal->save();
                 // General Journals Cash Credit
                 $generalJournal = new GeneralJournal();
                 $generalJournal->date = strtotime($request->date);
                 $generalJournal->transaction_type = Config::get('common.transaction_type.general');
                 $generalJournal->reference_id = $recorder->id;
                 $generalJournal->year = $currentYear;
                 $generalJournal->account_code = $cashCode;
                 $generalJournal->workspace_id = $workspace_id;
                 $generalJournal->amount = $request->amount;
                 $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                 $generalJournal->created_by = Auth::user()->id;
                 $generalJournal->created_at = time();
                 $generalJournal->save();
             }
         });
     } catch (\Exception $e) {
         //dd($e);
         Session()->flash('error_message', 'Transaction Recorder Creation Failed.');
         return redirect('recorders');
     }
     Session()->flash('flash_message', 'Transaction Recorder Creation Successful.');
     return redirect('recorders');
 }
 public function store(BankTransactionRequest $request)
 {
     try {
         DB::transaction(function () use($request) {
             $inputs = $request->input();
             $time = time();
             $user = Auth::user();
             $year = CommonHelper::get_current_financial_year();
             $bankTransaction = new BankTransaction();
             // Transaction Entry
             $bankTransaction->bank_id = $inputs['bank_id'];
             $bankTransaction->transaction_type = $inputs['transaction_type'];
             $bankTransaction->amount = $inputs['amount'];
             $bankTransaction->transaction_date = $inputs['transaction_date'];
             $bankTransaction->created_by = $user->id;
             $bankTransaction->created_at = $time;
             $bankTransaction->save();
             if ($inputs['amount'] > 0) {
                 if ($inputs['transaction_type'] == 1) {
                     // Bank Balance Update
                     $bank = Bank::findOrfail($inputs['bank_id']);
                     $bank->balance += $inputs['amount'];
                     $bank->updated_by = $user->id;
                     $bank->updated_by = $time;
                     $bank->save();
                     // Update Workspace Ledger Bank
                     $workspaceLedger = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => $bank->account_code, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $year])->first();
                     $workspaceLedger->balance += $inputs['amount'];
                     $workspaceLedger->updated_by = $user->id;
                     $workspaceLedger->updated_by = $time;
                     $workspaceLedger->save();
                     // Update Workspace Ledger Cash
                     $workspaceLedger = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => $bank->account_code, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $year])->first();
                     $workspaceLedger->balance -= $inputs['amount'];
                     $workspaceLedger->updated_by = $user->id;
                     $workspaceLedger->updated_by = $time;
                     $workspaceLedger->save();
                     // General Journals Cash Credit
                     $generalJournal = new GeneralJournal();
                     $generalJournal->date = strtotime($inputs['transaction_date']);
                     $generalJournal->transaction_type = Config::get('common.transaction_type.bank_deposit');
                     $generalJournal->reference_id = $bankTransaction->id;
                     $generalJournal->year = $year;
                     $generalJournal->account_code = 11000;
                     $generalJournal->workspace_id = $user->workspace_id;
                     $generalJournal->amount = $inputs['amount'];
                     $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                     $generalJournal->created_by = Auth::user()->id;
                     $generalJournal->created_at = time();
                     $generalJournal->save();
                     // Bank Account Debit
                     $generalJournal = new GeneralJournal();
                     $generalJournal->date = strtotime($inputs['transaction_date']);
                     $generalJournal->transaction_type = Config::get('common.transaction_type.bank_deposit');
                     $generalJournal->reference_id = $bankTransaction->id;
                     $generalJournal->year = $year;
                     $generalJournal->account_code = $bank->account_code;
                     $generalJournal->workspace_id = $user->workspace_id;
                     $generalJournal->amount = $inputs['amount'];
                     $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                     $generalJournal->created_by = Auth::user()->id;
                     $generalJournal->created_at = time();
                     $generalJournal->save();
                 } elseif ($inputs['transaction_type'] == 2) {
                     // Bank Balance Update
                     $bank = Bank::findOrfail($inputs['bank_id']);
                     $bank->balance -= $inputs['amount'];
                     $bank->updated_by = $user->id;
                     $bank->updated_by = $time;
                     $bank->save();
                     // Update Workspace Ledger Bank
                     $workspaceLedger = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => $bank->account_code, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $year])->first();
                     $workspaceLedger->balance -= $inputs['amount'];
                     $workspaceLedger->updated_by = $user->id;
                     $workspaceLedger->updated_by = $time;
                     $workspaceLedger->save();
                     // Update Workspace Ledger Cash
                     $workspaceLedger = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => 11000, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $year])->first();
                     $workspaceLedger->balance += $inputs['amount'];
                     $workspaceLedger->updated_by = $user->id;
                     $workspaceLedger->updated_by = $time;
                     $workspaceLedger->save();
                     // General Journals Cash Debit
                     $generalJournal = new GeneralJournal();
                     $generalJournal->date = strtotime($inputs['transaction_date']);
                     $generalJournal->transaction_type = Config::get('common.transaction_type.bank_withdraw');
                     $generalJournal->reference_id = $bankTransaction->id;
                     $generalJournal->year = $year;
                     $generalJournal->account_code = 11000;
                     $generalJournal->workspace_id = $user->workspace_id;
                     $generalJournal->amount = $inputs['amount'];
                     $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                     $generalJournal->created_by = Auth::user()->id;
                     $generalJournal->created_at = time();
                     $generalJournal->save();
                     // Bank Account Credit
                     $generalJournal = new GeneralJournal();
                     $generalJournal->date = strtotime($inputs['transaction_date']);
                     $generalJournal->transaction_type = Config::get('common.transaction_type.bank_withdraw');
                     $generalJournal->reference_id = $bankTransaction->id;
                     $generalJournal->year = $year;
                     $generalJournal->account_code = $bank->account_code;
                     $generalJournal->workspace_id = $user->workspace_id;
                     $generalJournal->amount = $inputs['amount'];
                     $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                     $generalJournal->created_by = Auth::user()->id;
                     $generalJournal->created_at = time();
                     $generalJournal->save();
                 } elseif ($inputs['transaction_type'] == 3) {
                     // Bank Balance Update
                     $bank = Bank::findOrfail($inputs['bank_id']);
                     $bank->balance += $inputs['amount'];
                     $bank->updated_by = $user->id;
                     $bank->updated_by = $time;
                     $bank->save();
                     // Update Workspace Ledger Bank
                     $workspaceLedger = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => $bank->account_code, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $year])->first();
                     $workspaceLedger->balance += $inputs['amount'];
                     $workspaceLedger->updated_by = $user->id;
                     $workspaceLedger->updated_by = $time;
                     $workspaceLedger->save();
                     // Update Workspace Ledger Interest
                     $workspaceLedger = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => 38000, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $year])->first();
                     $workspaceLedger->balance += $inputs['amount'];
                     $workspaceLedger->updated_by = $user->id;
                     $workspaceLedger->updated_by = $time;
                     $workspaceLedger->save();
                     // General Journals Cash Debit
                     $generalJournal = new GeneralJournal();
                     $generalJournal->date = strtotime($inputs['transaction_date']);
                     $generalJournal->transaction_type = Config::get('common.transaction_type.bank_interest');
                     $generalJournal->reference_id = $bankTransaction->id;
                     $generalJournal->year = $year;
                     $generalJournal->account_code = 11000;
                     $generalJournal->workspace_id = $user->workspace_id;
                     $generalJournal->amount = $inputs['amount'];
                     $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                     $generalJournal->created_by = Auth::user()->id;
                     $generalJournal->created_at = time();
                     $generalJournal->save();
                     // Bank Interest Credit
                     $generalJournal = new GeneralJournal();
                     $generalJournal->date = strtotime($inputs['transaction_date']);
                     $generalJournal->transaction_type = Config::get('common.transaction_type.bank_interest');
                     $generalJournal->reference_id = $bankTransaction->id;
                     $generalJournal->year = $year;
                     $generalJournal->account_code = 38000;
                     $generalJournal->workspace_id = $user->workspace_id;
                     $generalJournal->amount = $inputs['amount'];
                     $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                     $generalJournal->created_by = Auth::user()->id;
                     $generalJournal->created_at = time();
                     $generalJournal->save();
                 } elseif ($inputs['transaction_type'] == 4) {
                     // Bank Balance Update
                     $bank = Bank::findOrfail($inputs['bank_id']);
                     $bank->balance -= $inputs['amount'];
                     $bank->updated_by = $user->id;
                     $bank->updated_by = $time;
                     $bank->save();
                     // Update Workspace Ledger Bank
                     $workspaceLedger = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => $bank->account_code, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $year])->first();
                     $workspaceLedger->balance -= $inputs['amount'];
                     $workspaceLedger->updated_by = $user->id;
                     $workspaceLedger->updated_by = $time;
                     $workspaceLedger->save();
                     // Update Workspace Ledger Bank Charge
                     $workspaceLedger = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => 29995, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $year])->first();
                     $workspaceLedger->balance += $inputs['amount'];
                     $workspaceLedger->updated_by = $user->id;
                     $workspaceLedger->updated_by = $time;
                     $workspaceLedger->save();
                     // General Journals Cash Credit
                     $generalJournal = new GeneralJournal();
                     $generalJournal->date = strtotime($inputs['transaction_date']);
                     $generalJournal->transaction_type = Config::get('common.transaction_type.bank_charge');
                     $generalJournal->reference_id = $bankTransaction->id;
                     $generalJournal->year = $year;
                     $generalJournal->account_code = 11000;
                     $generalJournal->workspace_id = $user->workspace_id;
                     $generalJournal->amount = $inputs['amount'];
                     $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                     $generalJournal->created_by = Auth::user()->id;
                     $generalJournal->created_at = time();
                     $generalJournal->save();
                     // Bank Charge Debit
                     $generalJournal = new GeneralJournal();
                     $generalJournal->date = strtotime($inputs['transaction_date']);
                     $generalJournal->transaction_type = Config::get('common.transaction_type.bank_charge');
                     $generalJournal->reference_id = $bankTransaction->id;
                     $generalJournal->year = $year;
                     $generalJournal->account_code = 29995;
                     $generalJournal->workspace_id = $user->workspace_id;
                     $generalJournal->amount = $inputs['amount'];
                     $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                     $generalJournal->created_by = Auth::user()->id;
                     $generalJournal->created_at = time();
                     $generalJournal->save();
                 }
             }
         });
     } catch (\Exception $e) {
         Session::flash('error_message', 'Failed to do Bank Transaction. Please try again!');
         return Redirect::back();
     }
     Session::flash('flash_message', 'Bank Transaction done successfully!');
     return redirect('bank_transactions');
 }
Example #3
0
 public function store(AdjustmentRequest $request)
 {
     try {
         DB::transaction(function () use($request) {
             $currentYear = CommonHelper::get_current_financial_year();
             $workspace_id = Auth::user()->workspace_id;
             $adjustment = new Adjustment();
             $adjustment->year = $currentYear;
             $adjustment->account_from = $request->account_from;
             $adjustment->amount = $request->amount;
             if ($request->account_from == 25000) {
                 $adjustment->account_to = 14000;
             } elseif ($request->account_from == 27000) {
                 $adjustment->account_to = 13000;
             }
             $adjustment->created_by = Auth::user()->id;
             $adjustment->created_at = time();
             $adjustment->save();
             if ($request->account_from == 25000) {
                 // Workspace_id hardcoded for head office
                 // Workspace Ledger Purchase Credit(-)
                 $purchaseWorkspaceData = WorkspaceLedger::where(['workspace_id' => 1, 'account_code' => 25000, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
                 $purchaseWorkspaceData->balance = $purchaseWorkspaceData->balance - $request->amount;
                 $purchaseWorkspaceData->update();
                 // Workspace Ledger Inventory Raw Material Account Debit(+)
                 $assetWorkspaceData = WorkspaceLedger::where(['workspace_id' => 1, 'account_code' => 14000, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
                 $assetWorkspaceData->balance += $request->amount;
                 $assetWorkspaceData->update();
                 // General Journals Insert
                 $generalJournal = new GeneralJournal();
                 $generalJournal->date = strtotime(date('d-m-Y'));
                 $generalJournal->transaction_type = Config::get('common.transaction_type.purchase');
                 $generalJournal->reference_id = $adjustment->id;
                 $generalJournal->year = $currentYear;
                 $generalJournal->account_code = 14000;
                 $generalJournal->workspace_id = 1;
                 // Hard Coded 1 For Head Office (Raw Material)
                 $generalJournal->amount = $request->amount;
                 $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                 $generalJournal->created_by = Auth::user()->id;
                 $generalJournal->created_at = time();
                 $generalJournal->save();
             } elseif ($request->account_from == 27000) {
                 // Workspace Ledger Office Supply Credit(-)
                 $officeWorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => 27000, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
                 $officeWorkspaceData->balance = $officeWorkspaceData->balance - $request->amount;
                 $officeWorkspaceData->update();
                 // Workspace Ledger Inventory Office Supplies Account Debit(+)
                 $assetWorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => 13000, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
                 $assetWorkspaceData->balance += $request->amount;
                 $assetWorkspaceData->update();
                 // General Journals Insert
                 $generalJournal = new GeneralJournal();
                 $generalJournal->date = strtotime(date('d-m-Y'));
                 $generalJournal->transaction_type = Config::get('common.transaction_type.office_supply');
                 $generalJournal->reference_id = $adjustment->id;
                 $generalJournal->year = $currentYear;
                 $generalJournal->account_code = 13000;
                 $generalJournal->workspace_id = $workspace_id;
                 $generalJournal->amount = $request->amount;
                 $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                 $generalJournal->created_by = Auth::user()->id;
                 $generalJournal->created_at = time();
                 $generalJournal->save();
             }
         });
     } catch (\Exception $e) {
         Session()->flash('error_message', 'Adjustment not done!');
         return redirect('adjustments');
     }
     Session()->flash('flash_message', 'Adjustment has been done!');
     return redirect('adjustments');
 }
Example #4
0
 public function update($id, SalaryPaymentRequest $request)
 {
     try {
         DB::transaction(function () use($request, $id) {
             $inputs = $request->input();
             $user = Auth::user();
             $time = time();
             $date = strtotime(date('d-m-Y'));
             $balance_type = Config::get('common.balance_type_intermediate');
             $year = CommonHelper::get_current_financial_year();
             $salaryPayment = SalaryPayment::find($id);
             $salary = Salary::where(['id' => $salaryPayment->salary_id, 'employee_id' => $salaryPayment->employee_id, 'workspace_id' => $salaryPayment->workspace_id])->first();
             if ($salary->net > $inputs['salary'] && $salaryPayment->amount > $inputs['salary']) {
                 //Decrease amount from Salary payment table
                 $salaryPayment->amount -= $inputs['salary'];
                 $salaryPayment->updated_by = $user->id;
                 $salaryPayment->updated_at = $time;
                 $salaryPayment->update();
                 //Update salaries table
                 $salary->due = $salary->net - $inputs['salary'];
                 $salary->paid = $inputs['salary'];
                 $salary->payment_status = Config::get('common.salary_payment_status.partial');
                 $salary->update();
                 //Update Personal Account
                 $personalAccount = PersonalAccount::where(['person_id' => $salaryPayment->employee_id, 'person_type' => Config::get('common.person_type_employee')])->first();
                 $personalAccount->balance += $salaryPayment->amount - $inputs['salary'];
                 //ADD
                 $personalAccount->updated_by = $user->id;
                 $personalAccount->updated_at = $time;
                 $personalAccount->save();
                 //Update Workspace Ledger
                 $accountPayableWorkspaceData = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => 42000, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $accountPayableWorkspaceData->balance += $salaryPayment->amount - $inputs['salary'];
                 //Add Wage Payable
                 $accountPayableWorkspaceData->updated_by = $user->id;
                 $accountPayableWorkspaceData->updated_at = $time;
                 $accountPayableWorkspaceData->update();
                 $accountPayableWorkspaceData = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => 11000, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $accountPayableWorkspaceData->balance += $salaryPayment->amount - $inputs['salary'];
                 //Add Cash
                 $accountPayableWorkspaceData->updated_by = $user->id;
                 $accountPayableWorkspaceData->updated_at = $time;
                 $accountPayableWorkspaceData->update();
                 // General Journal Table Impact
                 $generalJournal = GeneralJournal::where(['transaction_type' => Config::get('common.transaction_type.salary_payment'), 'reference_id' => $id, 'account_code' => 42000, 'workspace_id' => $user->workspace_id, 'year' => $year])->first();
                 $generalJournal->amount -= $salaryPayment->amount - $inputs['salary'];
                 $generalJournal->updated_by = $user->id;
                 $generalJournal->updated_at = $time;
                 $generalJournal->save();
                 $generalJournal = GeneralJournal::where(['transaction_type' => Config::get('common.transaction_type.salary_payment'), 'reference_id' => $id, 'account_code' => 11000, 'workspace_id' => $user->workspace_id, 'year' => $year])->first();
                 $generalJournal->amount -= $salaryPayment->amount - $inputs['salary'];
                 $generalJournal->updated_by = $user->id;
                 $generalJournal->updated_at = $time;
                 $generalJournal->save();
             }
         });
     } catch (\Exception $e) {
         Session()->flash('error_message', 'Salary payment update cannot successful. Please Try again.');
         return Redirect::back();
     }
     Session()->flash('flash_message', 'Salary payment update successfully.');
     return redirect('salary_payment');
 }
Example #5
0
 public function update($id, SalesOrderRequest $request)
 {
     try {
         DB::transaction(function () use($request, $id) {
             $inputs = $request->input();
             $user = Auth::user();
             $time = time();
             $date = strtotime(date('d-m-Y'));
             $year = CommonHelper::get_current_financial_year();
             if (empty($inputs['transport_cost'])) {
                 $inputs['transport_cost'] = 0;
             }
             if (empty($inputs['labour_cost'])) {
                 $inputs['labour_cost'] = 0;
             }
             if (empty($inputs['discount'])) {
                 $inputs['discount'] = 0;
             }
             if (!isset($inputs['paid_from_personal_account'])) {
                 $inputs['paid_from_personal_account'] = 0;
             }
             $salesOrder = SalesOrder::find($id);
             $oldSalesOrder = clone $salesOrder;
             $salesOrder->total = $inputs['total'] - $inputs['discount'];
             $salesOrder->discount = $inputs['discount'];
             $salesOrder->paid = $inputs['paid'];
             $salesOrder->personal_account_paid = $inputs['paid_from_personal_account'];
             $salesOrder->due = $inputs['due'];
             $salesOrder->transport_cost = $inputs['transport_cost'];
             $salesOrder->labour_cost = $inputs['labour_cost'];
             $salesOrder->remarks = $inputs['remarks'];
             $salesOrder->updated_by = $user->id;
             $salesOrder->updated_at = $time;
             $salesOrder->update();
             if (isset($inputs['delete_product'])) {
                 foreach ($inputs['delete_product'] as $product) {
                     $salesOrderItem = SalesOrderItem::where('sales_order_id', '=', $id)->where('product_id', '=', $product['product_id'])->first();
                     if ($salesOrderItem) {
                         $salesOrderItem->delete();
                     }
                 }
             }
             $old_products = SalesOrderItem::where('sales_order_id', '=', $id)->where('status', '!=', 4)->get();
             $arrange_old_items = [];
             foreach ($old_products as $old_product) {
                 $arrange_old_items[$old_product['product_id']] = $old_product;
             }
             foreach ($inputs['product'] as $product) {
                 if (isset($arrange_old_items[$product['product_id']])) {
                     $salesOrderItem = SalesOrderItem::find($arrange_old_items[$product['product_id']]['id']);
                     $salesOrderItem->sales_quantity = $product['sales_quantity'];
                     $salesOrderItem->sales_unit_type = $product['sales_unit_type'];
                     $salesOrderItem->unit_price = $product['unit_price'];
                     $salesOrderItem->updated_by = $user->id;
                     $salesOrderItem->updated_at = $time;
                     $salesOrderItem->update();
                 } else {
                     $salesOrderItem = new SalesOrderItem();
                     $salesOrderItem->sales_order_id = $id;
                     $salesOrderItem->product_id = $product['product_id'];
                     $salesOrderItem->sales_quantity = $product['sales_quantity'];
                     $salesOrderItem->sales_unit_type = $product['sales_unit_type'];
                     $salesOrderItem->unit_price = $product['unit_price'];
                     $salesOrderItem->created_by = $user->id;
                     $salesOrderItem->created_at = $time;
                     $salesOrderItem->status = 1;
                     $salesOrderItem->save();
                 }
                 unset($arrange_old_items[$product['product_id']]);
             }
             $balance_type = Config::get('common.balance_type_intermediate');
             $transaction_type = Config::get('common.transaction_type.sales');
             $new_total_amount = $inputs['total'];
             $old_total_amount = $oldSalesOrder['total'];
             if ($new_total_amount > $old_total_amount) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 31000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $general_journal->amount = $new_total_amount - $inputs['discount'];
                 $general_journal->updated_by = $user->id;
                 $general_journal->updated_at = $time;
                 $general_journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 31000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->year = $year;
                 $workspace->balance += $new_total_amount - $old_total_amount - $inputs['discount'];
                 //Add Product Sales
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
             } elseif ($new_total_amount < $old_total_amount) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 31000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $general_journal->amount = $new_total_amount - $inputs['discount'];
                 $general_journal->updated_by = $user->id;
                 $general_journal->updated_at = $time;
                 $general_journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 31000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->year = $year;
                 $workspace->balance -= $old_total_amount - ($new_total_amount - $inputs['discount']);
                 //Subtract Product Sales
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
             }
             $new_paid_amount = $inputs['paid'];
             $old_paid_amount = $oldSalesOrder['paid'];
             if (!$new_paid_amount && $old_paid_amount) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 11000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $general_journal->delete();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 if ($workspace->balance < $old_paid_amount) {
                     Session()->flash('warning_message', 'Insufficient cash balance!');
                     throw new \Exception();
                 }
                 $workspace->balance -= $old_paid_amount;
                 //sub Cash
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
             } elseif (!$old_paid_amount && $new_paid_amount) {
                 //Insert data into General Journal
                 $journal = new GeneralJournal();
                 $journal->date = $date;
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $id;
                 $journal->year = $year;
                 $journal->account_code = 11000;
                 //Cash
                 $journal->workspace_id = $user->workspace_id;
                 $journal->amount = $new_paid_amount;
                 $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                 $journal->created_by = $user->id;
                 $journal->created_at = $time;
                 $journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->year = $year;
                 $workspace->balance += $new_paid_amount;
                 //Add Cash
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
             } elseif ($new_paid_amount > $old_paid_amount) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 11000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $general_journal->amount = $new_paid_amount;
                 $general_journal->updated_by = $user->id;
                 $general_journal->updated_at = $time;
                 $general_journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $new_paid_amount - $old_paid_amount;
                 //Add Cash
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
             } elseif ($new_paid_amount < $old_paid_amount) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 11000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $general_journal->amount = $new_paid_amount;
                 $general_journal->updated_by = $user->id;
                 $general_journal->updated_at = $time;
                 $general_journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 if ($workspace->balance < $old_paid_amount - $new_paid_amount) {
                     Session()->flash('warning_message', 'Insufficient cash balance!');
                     throw new \Exception();
                 }
                 $workspace->balance -= $old_paid_amount - $new_paid_amount;
                 //sub Cash
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 dd($workspace);
             }
             $new_account_paid = $inputs['paid_from_personal_account'];
             $old_account_paid = $oldSalesOrder->personal_account_paid;
             if (!$new_account_paid && $old_account_paid) {
                 $personal = PersonalAccount::where('person_id', $inputs['customer_id'])->where('person_type', $inputs['customer_type'])->first();
                 $personal->balance += $old_account_paid;
                 $personal->updated_by = $user->id;
                 $personal->updated_at = $time;
                 $personal->update();
                 $workspace = WorkspaceLedger::where(['account_code' => 41000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $old_account_paid;
                 //Add Account Payable
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 41000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $general_journal->delete();
             } elseif ($new_account_paid && !$old_account_paid) {
                 $personal = PersonalAccount::where('person_id', $inputs['customer_id'])->where('person_type', $inputs['customer_type'])->first();
                 $personal->balance -= $new_account_paid;
                 $personal->updated_by = $user->id;
                 $personal->updated_at = $time;
                 $personal->update();
                 $workspace = WorkspaceLedger::where(['account_code' => 41000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $new_account_paid;
                 //Add Account Payable
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
                 $journal = new GeneralJournal();
                 $journal->date = $date;
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $id;
                 $journal->year = $year;
                 $journal->account_code = 41000;
                 //Account Payable
                 $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                 $journal->workspace_id = $user->workspace_id;
                 $journal->amount = $new_account_paid;
                 $journal->created_by = $user->id;
                 $journal->created_at = $time;
                 $journal->save();
             } elseif ($new_account_paid > $old_account_paid) {
                 $personal = PersonalAccount::where('person_id', $salesOrder->customer_id)->where('person_type', $salesOrder->customer_type)->first();
                 $personal->balance -= $new_account_paid - $old_account_paid;
                 $personal->updated_by = $user->id;
                 $personal->updated_at = $time;
                 $personal->update();
                 $workspace = WorkspaceLedger::where(['account_code' => 41000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $new_account_paid - $old_account_paid;
                 //Sub Account Payable
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 41000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $general_journal->amount = $new_account_paid;
                 $general_journal->updated_by = $user->id;
                 $general_journal->updated_at = $time;
                 $general_journal->update();
             } elseif ($new_account_paid < $old_account_paid) {
                 $personal = PersonalAccount::where('person_id', $salesOrder->customer_id)->where('person_type', $salesOrder->customer_type)->first();
                 $personal->balance += $old_account_paid - $new_account_paid;
                 $personal->updated_by = $user->id;
                 $personal->updated_at = $time;
                 $personal->update();
                 $workspace = WorkspaceLedger::where(['account_code' => 41000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $old_account_paid - $new_account_paid;
                 //Add Account Payable
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 41000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $general_journal->amount = $new_account_paid;
                 $general_journal->updated_by = $user->id;
                 $general_journal->updated_at = $time;
                 $general_journal->update();
             }
             $new_due_amount = $inputs['due'];
             $old_due_amount = $oldSalesOrder['due'];
             if ($new_due_amount && !$old_due_amount) {
                 $journal = new GeneralJournal();
                 $journal->date = $date;
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $id;
                 $journal->year = $year;
                 $journal->account_code = 12000;
                 //Account Receivable
                 $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                 $journal->workspace_id = $user->workspace_id;
                 $journal->amount = $new_due_amount;
                 $journal->created_by = $user->id;
                 $journal->created_at = $time;
                 $journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 12000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $new_due_amount;
                 //add account receivable debit
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 // Update Personal Account
                 $personal = PersonalAccount::where('person_id', $oldSalesOrder['customer_id'])->where('person_type', $oldSalesOrder['customer_type'])->first();
                 $personal->due += $new_due_amount;
                 $personal->updated_by = $user->id;
                 $personal->updated_at = $time;
                 $personal->save();
             } elseif ($old_due_amount && !$new_due_amount) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 12000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $general_journal->delete();
                 $workspace = WorkspaceLedger::where(['account_code' => 12000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $old_due_amount;
                 //sub account receivable credit
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 // Update Personal Account
                 $personal = PersonalAccount::where('person_id', $oldSalesOrder['customer_id'])->where('person_type', $oldSalesOrder['customer_type'])->first();
                 $personal->due -= $old_due_amount;
                 $personal->updated_by = $user->id;
                 $personal->updated_at = $time;
                 $personal->save();
             } elseif ($new_due_amount > $old_due_amount) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 12000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $general_journal->amount = $new_due_amount;
                 $general_journal->updated_by = $user->id;
                 $general_journal->updated_at = $time;
                 $general_journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 12000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $new_due_amount - $old_due_amount;
                 //add account receivable debit
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 // Update Personal Account
                 $personal = PersonalAccount::where('person_id', $oldSalesOrder['customer_id'])->where('person_type', $oldSalesOrder['customer_type'])->first();
                 $personal->due += $new_due_amount - $old_due_amount;
                 $personal->updated_by = $user->id;
                 $personal->updated_at = $time;
                 $personal->save();
             } elseif ($new_due_amount < $old_due_amount) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 12000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $general_journal->amount = $new_due_amount;
                 $general_journal->updated_by = $user->id;
                 $general_journal->updated_at = $time;
                 $general_journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 12000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $old_due_amount - $new_due_amount;
                 //add account receivable credit
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 // Update Personal Account
                 $personal = PersonalAccount::where('person_id', $oldSalesOrder['customer_id'])->where('person_type', $oldSalesOrder['customer_type'])->first();
                 $personal->due -= $old_due_amount - $new_due_amount;
                 $personal->updated_by = $user->id;
                 $personal->updated_at = $time;
                 $personal->save();
             }
             $new_transportation_cost = $inputs['transport_cost'];
             $old_transportation_cost = $oldSalesOrder['transport_cost'];
             if ($new_transportation_cost <= 0 && $old_transportation_cost) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 35000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $general_journal->delete();
                 $workspace = WorkspaceLedger::where(['account_code' => 35000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $old_transportation_cost;
                 //sub transportation earning
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 if ($workspace->balance < $old_transportation_cost) {
                     Session()->flash('warning_message', 'Insufficient cash balance!');
                     throw new \Exception();
                 }
                 $workspace->balance -= $old_transportation_cost;
                 //Sub Cash
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
             } elseif ($new_transportation_cost && $old_transportation_cost <= 0) {
                 $journal = new GeneralJournal();
                 $journal->date = $date;
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $id;
                 $journal->year = $year;
                 $journal->account_code = 35000;
                 //Account Receivable
                 $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                 $journal->workspace_id = $user->workspace_id;
                 $journal->amount = $new_transportation_cost;
                 $journal->created_by = $user->id;
                 $journal->created_at = $time;
                 $journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 35000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $new_transportation_cost;
                 //add transportation earning
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $new_transportation_cost;
                 //Add Cash
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
             } elseif ($new_transportation_cost > $old_transportation_cost) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 35000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $general_journal->amount = $new_transportation_cost;
                 $general_journal->updated_by = $user->id;
                 $general_journal->updated_at = $time;
                 $general_journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 35000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $new_transportation_cost - $old_transportation_cost;
                 //Add transportation earning
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $new_transportation_cost - $old_transportation_cost;
                 //Add Cash
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
             } elseif ($new_transportation_cost < $old_transportation_cost) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 35000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $general_journal->amount = $new_transportation_cost;
                 $general_journal->updated_by = $user->id;
                 $general_journal->updated_at = $time;
                 $general_journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 35000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $old_transportation_cost - $new_transportation_cost;
                 //Add transportation earning
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 if ($workspace->balance < $old_transportation_cost - $new_transportation_cost) {
                     Session()->flash('warning_message', 'Insufficient cash balance!');
                     throw new \Exception();
                 }
                 $workspace->balance -= $old_transportation_cost - $new_transportation_cost;
                 //Sub Cash
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
             }
             $new_labour_cost = $inputs['labour_cost'];
             $old_labour_cost = $oldSalesOrder['labour_cost'];
             if ($new_labour_cost <= 0 && $old_labour_cost) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 34000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $general_journal->delete();
                 $workspace = WorkspaceLedger::where(['account_code' => 34000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $old_labour_cost;
                 //sub labour earning
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 if ($workspace->balance < $old_labour_cost) {
                     Session()->flash('warning_message', 'Insufficient cash balance!');
                     throw new \Exception();
                 }
                 $workspace->balance -= $old_labour_cost;
                 //Sub Cash
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
             } elseif ($new_labour_cost && $old_labour_cost <= 0) {
                 $journal = new GeneralJournal();
                 $journal->date = $date;
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $id;
                 $journal->year = $year;
                 $journal->account_code = 34000;
                 //labour earning
                 $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                 $journal->workspace_id = $user->workspace_id;
                 $journal->amount = $new_labour_cost;
                 $journal->created_by = $user->id;
                 $journal->created_at = $time;
                 $journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 34000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $new_labour_cost;
                 //add labour earning
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $new_labour_cost;
                 //Add Cash
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
             } elseif ($new_labour_cost > $old_labour_cost) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 34000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $general_journal->amount = $new_labour_cost;
                 $general_journal->updated_by = $user->id;
                 $general_journal->updated_at = $time;
                 $general_journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 34000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $new_labour_cost - $old_labour_cost;
                 //Add transportation earning
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $new_labour_cost - $old_labour_cost;
                 //Add Cash
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
             } elseif ($new_labour_cost < $old_labour_cost) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 34000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $general_journal->amount = $new_labour_cost;
                 $general_journal->updated_by = $user->id;
                 $general_journal->updated_at = $time;
                 $general_journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 34000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $old_labour_cost - $new_labour_cost;
                 //Add labour earning
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 if ($workspace->balance < $old_labour_cost - $new_labour_cost) {
                     Session()->flash('warning_message', 'Insufficient cash balance!');
                     throw new \Exception();
                 }
                 $workspace->balance -= $old_labour_cost - $new_labour_cost;
                 //Sub Cash
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
             }
         });
     } catch (\Exception $e) {
         Session()->flash('error_message', 'Sales Order not Updated!');
         return Redirect::back();
     }
     Session()->flash('flash_message', 'Sales Order has been Updated!');
     return redirect('salesOrder');
 }
Example #6
0
 public function store(Request $request)
 {
     $this->validate($request, ['customer_id' => 'required', 'product' => 'required|array', 'total' => 'required|numeric', 'return_type' => 'required']);
     try {
         DB::transaction(function () use($request) {
             $inputs = $request->input();
             $user_id = Auth::user()->id;
             $workspace_id = Auth::user()->workspace_id;
             $balance_type = Config::get('common.balance_type_intermediate');
             $transaction_type = Config::get('common.transaction_type.sales_return');
             $time = time();
             $date = strtotime(date('d-m-Y'));
             $year = CommonHelper::get_current_financial_year();
             $data['customer_id'] = $inputs['customer_id'];
             $data['workspace_id'] = $workspace_id;
             $data['customer_type'] = $inputs['customer_type'];
             $data['total_amount'] = $inputs['total'];
             if ($inputs['return_type'] == 3) {
                 $data['due'] = $inputs['total'];
             } elseif ($inputs['return_type'] == 4) {
                 $data['due'] = $inputs['total'] - $inputs['due_paid'];
             }
             if (isset($inputs['due_paid'])) {
                 $data['due_paid'] = $inputs['due_paid'];
             }
             $data['return_type'] = $inputs['return_type'];
             $data['date'] = $time;
             $data['created_by'] = $user_id;
             $data['created_at'] = $time;
             $sales_return_id = DB::table('sales_return')->insertGetId($data);
             unset($data);
             $data['sales_return_id'] = $sales_return_id;
             $data['created_by'] = $user_id;
             $data['created_at'] = $time;
             foreach ($inputs['product'] as $product) {
                 $data['product_id'] = $product['product_id'];
                 $data['quantity'] = $product['quantity_returned'];
                 $data['unit_price'] = $product['unit_price'];
                 $data['unit_type'] = $product['unit_type'];
                 DB::table('sales_return_details')->insert($data);
                 $quantity_returned = $product['quantity_returned'];
                 $stock = Stock::where('year', '=', $year)->where('stock_type', '=', $balance_type)->where('product_id', '=', $product['product_id'])->first();
                 if ($product['unit_type'] == 2) {
                     $quantity_returned = $product['quantity_returned'] / $product['weight'] * $product['length'];
                 }
                 $stock->quantity += $quantity_returned;
                 $stock->updated_by = $user_id;
                 $stock->updated_at = $time;
                 $stock->update();
             }
             if ($inputs['return_type'] == 1) {
                 //For Cash
                 // Update Workspace Ledger
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 if ($workspace->balance < $inputs['total']) {
                     Session()->flash('warning_message', 'Insufficient cash balance!');
                     throw new \Exception();
                 }
                 $workspace->balance -= $inputs['total'];
                 //Subtract Cash
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 32000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $inputs['total'];
                 //Add Product Sales Return
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 //Insert data into General Journal
                 $journal = new GeneralJournal();
                 $journal->date = $date;
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $sales_return_id;
                 $journal->year = $year;
                 $journal->account_code = 11000;
                 //Cash
                 $journal->workspace_id = $workspace_id;
                 $journal->amount = $inputs['total'];
                 $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                 $journal->created_by = $user_id;
                 $journal->created_at = $time;
                 $journal->save();
                 $journal = new GeneralJournal();
                 $journal->date = $date;
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $sales_return_id;
                 $journal->year = $year;
                 $journal->account_code = 32000;
                 //Product Sales Return
                 $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                 $journal->workspace_id = $workspace_id;
                 $journal->amount = $inputs['total'];
                 $journal->created_by = $user_id;
                 $journal->created_at = $time;
                 $journal->save();
             } elseif ($inputs['return_type'] == 2) {
                 // For Pay due
                 // Update Workspace Ledger
                 $workspace = WorkspaceLedger::where(['account_code' => 12000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $inputs['total'];
                 //Subtract Account Receivable
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 32000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $inputs['total'];
                 //Add Product Sales Return
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 //Insert data into General Journal
                 $journal = new GeneralJournal();
                 $journal->date = $date;
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $sales_return_id;
                 $journal->year = $year;
                 $journal->account_code = 12000;
                 //Account Receivable
                 $journal->workspace_id = $workspace_id;
                 $journal->amount = $inputs['total'];
                 $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                 $journal->created_by = $user_id;
                 $journal->created_at = $time;
                 $journal->save();
                 $journal = new GeneralJournal();
                 $journal->date = $date;
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $sales_return_id;
                 $journal->year = $year;
                 $journal->account_code = 32000;
                 //Product Sales Return
                 $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                 $journal->workspace_id = $workspace_id;
                 $journal->amount = $inputs['total'];
                 $journal->created_by = $user_id;
                 $journal->created_at = $time;
                 $journal->save();
                 // Update Personal Account
                 $personal = PersonalAccount::where('person_id', $inputs['customer_id'])->where('person_type', $inputs['customer_type'])->first();
                 $personal->due -= $inputs['due_paid'];
                 if ($inputs['total'] > $inputs['due_paid']) {
                     $personal->balance += $inputs['total'] - $inputs['due_paid'];
                 }
                 $personal->updated_by = $user_id;
                 $personal->updated_at = $time;
                 $personal->save();
             } elseif ($inputs['return_type'] == 3) {
                 //For Due
                 // Update Workspace Ledger
                 $workspace = WorkspaceLedger::where(['account_code' => 41000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $inputs['total'];
                 //Add Account Payable
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 32000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $inputs['total'];
                 //Add Product Sales Return
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 //Insert data into General Journal
                 $journal = new GeneralJournal();
                 $journal->date = $date;
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $sales_return_id;
                 $journal->year = $year;
                 $journal->account_code = 41000;
                 //Account Payable
                 $journal->workspace_id = $workspace_id;
                 $journal->amount = $inputs['total'];
                 $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                 $journal->created_by = $user_id;
                 $journal->created_at = $time;
                 $journal->save();
                 $journal = new GeneralJournal();
                 $journal->date = $date;
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $sales_return_id;
                 $journal->year = $year;
                 $journal->account_code = 32000;
                 //Product Sales Return
                 $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                 $journal->workspace_id = $workspace_id;
                 $journal->amount = $inputs['total'];
                 $journal->created_by = $user_id;
                 $journal->created_at = $time;
                 $journal->save();
                 //Update Personal Account
                 $personal = PersonalAccount::where('person_id', $inputs['customer_id'])->where('person_type', $inputs['customer_type'])->first();
                 $personal->balance += $inputs['total'];
                 $personal->updated_by = $user_id;
                 $personal->updated_at = $time;
                 $personal->save();
             } elseif ($inputs['return_type'] == 4) {
                 //For Pay Due & Cash Return
                 // Update Workspace Ledger
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 if ($workspace->balance < $inputs['total'] - $inputs['due_paid']) {
                     Session()->flash('warning_message', 'Insufficient cash balance!');
                     throw new \Exception();
                 }
                 $workspace->balance -= $inputs['total'] - $inputs['due_paid'];
                 //Subtract Cash
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 32000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $inputs['total'];
                 //Add Product Sales Return
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 12000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $inputs['due_paid'];
                 //Subtract Account Receivable
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 //Insert data into General Journal
                 $journal = new GeneralJournal();
                 $journal->date = $date;
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $sales_return_id;
                 $journal->year = $year;
                 $journal->account_code = 11000;
                 //Cash
                 $journal->workspace_id = $workspace_id;
                 $journal->amount = $inputs['total'] - $inputs['due_paid'];
                 $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                 $journal->created_by = $user_id;
                 $journal->created_at = $time;
                 $journal->save();
                 $journal = new GeneralJournal();
                 $journal->date = $date;
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $sales_return_id;
                 $journal->year = $year;
                 $journal->account_code = 32000;
                 //Product Sales Return
                 $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                 $journal->workspace_id = $workspace_id;
                 $journal->amount = $inputs['total'];
                 $journal->created_by = $user_id;
                 $journal->created_at = $time;
                 $journal->save();
                 $journal = new GeneralJournal();
                 $journal->date = $date;
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $sales_return_id;
                 $journal->year = $year;
                 $journal->account_code = 12000;
                 // Account Receivable
                 $journal->workspace_id = $workspace_id;
                 $journal->amount = $inputs['due_paid'];
                 $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                 $journal->created_by = $user_id;
                 $journal->created_at = $time;
                 $journal->save();
                 //Update Personal Account
                 $personal = PersonalAccount::where('person_id', $inputs['customer_id'])->where('person_type', $inputs['customer_type'])->first();
                 $personal->due -= $inputs['due_paid'];
                 $personal->updated_by = $user_id;
                 $personal->updated_at = $time;
                 $personal->save();
             }
         });
     } catch (\Exception $e) {
         Session()->flash('error_message', 'Sales Returned Failed.');
         return Redirect::back();
     }
     Session()->flash('flash_message', 'Sales Returned Successful.');
     return redirect('sales_return');
 }
 /**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request $request
  * @return \Illuminate\Http\Response
  */
 public function store(PurchasesReturnRequest $request)
 {
     if (!$request->input('items')) {
         Session()->flash('error_message', 'Purchases Return has not been Completed');
         return redirect('purchases');
     }
     $inputs = $request->input();
     DB::beginTransaction();
     try {
         $user_id = Auth::user()->id;
         $workspace_id = Auth::user()->workspace_id;
         $balance_type = Config::get('common.balance_type_intermediate');
         $transaction_type = Config::get('common.transaction_type.purchase_return');
         $person_type_supplier = Config::get('common.person_type_supplier');
         $year = CommonHelper::get_current_financial_year();
         $time = time();
         $purchase = new PurchasesReturn();
         $purchase->supplier_id = $request->input('supplier_id');
         $purchase->purchase_return_date = $request->input('purchase_return_date');
         $purchase->transportation_cost = $request->input('transportation_cost');
         $purchase->total_amout = $request->input('total');
         if ($inputs['return_type'] == 1) {
             $purchase->due = 0;
             $purchase->due_paid = 0;
         } elseif ($inputs['return_type'] == 2) {
             $purchase->due = 0;
             $purchase->due_paid = $request->input('total');
         } elseif ($inputs['return_type'] == 3) {
             $purchase->due = $request->input('total');
             $purchase->due_paid = 0;
         } elseif ($inputs['return_type'] == 4) {
             $purchase->due = 0;
             $purchase->due_paid = $request->input('pay_due_amount');
         }
         $purchase->return_type = $request->input('return_type');
         $purchase->created_at = time();
         $purchase->created_by = $user_id;
         $purchase->save();
         $purchase_return_id = $purchase->id;
         foreach ($request->input('items') as $item) {
             //purchase Return details
             $item['purchases_return_id'] = $purchase_return_id;
             $item['status'] = 1;
             $item['created_at'] = time();
             $item['created_by'] = $user_id;
             PurchasesReturnDetail::create($item);
             //update stock info
             RawStock::where(['material_id' => $item['material_id'], 'year' => $year, 'stock_type' => $balance_type])->decrement('quantity', $item['quantity'], ['updated_at' => $time, 'updated_by' => $user_id]);
         }
         if ($inputs['transportation_cost'] > 0) {
             // Update Workspace Ledger
             $workspace = WorkspaceLedger::where(['account_code' => 29997, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
             $workspace->balance += $inputs['transportation_cost'];
             $workspace->updated_by = $user_id;
             $workspace->updated_at = $time;
             $workspace->save();
             $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
             $workspace->balance -= $inputs['transportation_cost'];
             $workspace->updated_by = $user_id;
             $workspace->updated_at = $time;
             $workspace->save();
             //Insert data into General Journal
             $journal = new GeneralJournal();
             $journal->date = strtotime($request->input('purchase_return_date'));
             $journal->transaction_type = $transaction_type;
             $journal->reference_id = $purchase_return_id;
             $journal->year = $year;
             $journal->account_code = 11000;
             $journal->workspace_id = $workspace_id;
             $journal->amount = $inputs['transportation_cost'];
             $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
             $journal->created_by = $user_id;
             $journal->created_at = $time;
             $journal->save();
             $journal = new GeneralJournal();
             $journal->date = strtotime($request->input('purchase_return_date'));
             $journal->transaction_type = $transaction_type;
             $journal->reference_id = $purchase_return_id;
             $journal->year = $year;
             $journal->account_code = 29997;
             //purchase return transportation cost
             $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
             $journal->workspace_id = $workspace_id;
             $journal->amount = $inputs['transportation_cost'];
             $journal->created_by = $user_id;
             $journal->created_at = $time;
             $journal->save();
         }
         //general journal entry
         if ($inputs['return_type'] == 1) {
             // Update Workspace Ledger
             WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->increment('balance', $inputs['total'], ['updated_at' => $time, 'updated_by' => $user_id]);
             $workspace = WorkspaceLedger::where(['account_code' => 26000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
             $workspace->balance += $inputs['total'];
             //Add Purchase Return
             $workspace->updated_by = $user_id;
             $workspace->updated_at = $time;
             $workspace->save();
             //Insert data into General Journal
             $journal = new GeneralJournal();
             $journal->date = strtotime($request->input('purchase_return_date'));
             $journal->transaction_type = $transaction_type;
             $journal->reference_id = $purchase_return_id;
             $journal->year = $year;
             $journal->account_code = 11000;
             //Cash
             $journal->workspace_id = $workspace_id;
             $journal->amount = $inputs['total'];
             $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
             $journal->created_by = $user_id;
             $journal->created_at = $time;
             $journal->save();
             $journal = new GeneralJournal();
             $journal->date = strtotime($request->input('purchase_return_date'));
             $journal->transaction_type = $transaction_type;
             $journal->reference_id = $purchase_return_id;
             $journal->year = $year;
             $journal->account_code = 26000;
             //purchase Return
             $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
             $journal->workspace_id = $workspace_id;
             $journal->amount = $inputs['total'];
             $journal->created_by = $user_id;
             $journal->created_at = $time;
             $journal->save();
         } elseif ($inputs['return_type'] == 2) {
             // Update Workspace Ledger
             $workspace = WorkspaceLedger::where(['account_code' => 41000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
             $workspace->balance -= $inputs['total'];
             //Subtract Liabilities Account Payable
             $workspace->updated_by = $user_id;
             $workspace->updated_at = $time;
             $workspace->save();
             $workspace = WorkspaceLedger::where(['account_code' => 26000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
             $workspace->balance += $inputs['total'];
             //Add Purchase Return
             $workspace->updated_by = $user_id;
             $workspace->updated_at = $time;
             $workspace->save();
             //Insert data into General Journal
             $journal = new GeneralJournal();
             $journal->date = strtotime($request->input('purchase_return_date'));
             $journal->transaction_type = $transaction_type;
             $journal->reference_id = $purchase_return_id;
             $journal->year = $year;
             $journal->account_code = 41000;
             // Liabilities Account Payable
             $journal->workspace_id = $workspace_id;
             $journal->amount = $inputs['total'];
             $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
             $journal->created_by = $user_id;
             $journal->created_at = $time;
             $journal->save();
             $journal = new GeneralJournal();
             $journal->date = strtotime($request->input('purchase_return_date'));
             $journal->transaction_type = $transaction_type;
             $journal->reference_id = $purchase_return_id;
             $journal->year = $year;
             $journal->account_code = 26000;
             //Product Sales Return
             $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
             $journal->workspace_id = $workspace_id;
             $journal->amount = $inputs['total'];
             $journal->created_by = $user_id;
             $journal->created_at = $time;
             $journal->save();
             // Update Personal Account
             $personal = PersonalAccount::where('person_id', $inputs['supplier_id'])->where('person_type', $person_type_supplier)->first();
             $personal->balance -= $inputs['total'];
             $personal->updated_by = $user_id;
             $personal->updated_at = $time;
             $personal->save();
         } elseif ($inputs['return_type'] == 3) {
             // Update Workspace Ledger
             $workspace = WorkspaceLedger::where(['account_code' => 12000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
             $workspace->balance += $inputs['total'];
             //Account Receivable
             $workspace->updated_by = $user_id;
             $workspace->updated_at = $time;
             $workspace->save();
             $workspace = WorkspaceLedger::where(['account_code' => 26000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
             $workspace->balance += $inputs['total'];
             //Add Purchase Return
             $workspace->updated_by = $user_id;
             $workspace->updated_at = $time;
             $workspace->save();
             //Insert data into General Journal
             $journal = new GeneralJournal();
             $journal->date = strtotime($request->input('purchase_return_date'));
             $journal->transaction_type = $transaction_type;
             $journal->reference_id = $purchase_return_id;
             $journal->year = $year;
             $journal->account_code = 12000;
             //Account Receiveable
             $journal->workspace_id = $workspace_id;
             $journal->amount = $inputs['total'];
             $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
             $journal->created_by = $user_id;
             $journal->created_at = $time;
             $journal->save();
             $journal = new GeneralJournal();
             $journal->date = strtotime($request->input('purchase_return_date'));
             $journal->transaction_type = $transaction_type;
             $journal->reference_id = $purchase_return_id;
             $journal->year = $year;
             $journal->account_code = 26000;
             //Add Purchase Return
             $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
             $journal->workspace_id = $workspace_id;
             $journal->amount = $inputs['total'];
             $journal->created_by = $user_id;
             $journal->created_at = $time;
             $journal->save();
             //Update Personal Account
             $personal = PersonalAccount::where('person_id', $inputs['customer_id'])->where('person_type', $inputs['customer_type'])->first();
             $personal->due += $inputs['total'];
             $personal->updated_by = $user_id;
             $personal->updated_at = $time;
             $personal->save();
         } elseif ($inputs['return_type'] == 4) {
             // Update Workspace Ledger
             $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
             $workspace->balance += $inputs['cash_return_amount'];
             $workspace->updated_by = $user_id;
             $workspace->updated_at = $time;
             $workspace->save();
             $workspace = WorkspaceLedger::where(['account_code' => 26000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
             $workspace->balance += $inputs['total'];
             $workspace->updated_by = $user_id;
             $workspace->updated_at = $time;
             $workspace->save();
             $workspace = WorkspaceLedger::where(['account_code' => 41000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
             $workspace->balance -= $inputs['pay_due_amount'];
             //Account Payable
             $workspace->updated_by = $user_id;
             $workspace->updated_at = $time;
             $workspace->save();
             //Insert data into General Journal
             $journal = new GeneralJournal();
             $journal->date = strtotime($request->input('purchase_return_date'));
             $journal->transaction_type = $transaction_type;
             $journal->reference_id = $purchase_return_id;
             $journal->year = $year;
             $journal->account_code = 11000;
             //Cash
             $journal->workspace_id = $workspace_id;
             $journal->amount = $inputs['cash_return_amount'];
             $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
             $journal->created_by = $user_id;
             $journal->created_at = $time;
             $journal->save();
             $journal = new GeneralJournal();
             $journal->date = strtotime($request->input('purchase_return_date'));
             $journal->transaction_type = $transaction_type;
             $journal->reference_id = $purchase_return_id;
             $journal->year = $year;
             $journal->account_code = 26000;
             //Product Sales Return
             $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
             $journal->workspace_id = $workspace_id;
             $journal->amount = $inputs['total'];
             $journal->created_by = $user_id;
             $journal->created_at = $time;
             $journal->save();
             $journal = new GeneralJournal();
             $journal->date = strtotime($request->input('purchase_return_date'));
             $journal->transaction_type = $transaction_type;
             $journal->reference_id = $purchase_return_id;
             $journal->year = $year;
             $journal->account_code = 41000;
             // Account Payable
             $journal->workspace_id = $workspace_id;
             $journal->amount = $inputs['pay_due_amount'];
             $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
             $journal->created_by = $user_id;
             $journal->created_at = $time;
             $journal->save();
             //Update Personal Account
             $personal = PersonalAccount::where('person_id', $inputs['customer_id'])->where('person_type', $inputs['customer_type'])->first();
             $personal->blance -= $inputs['pay_due_amount'];
             $personal->updated_by = $user_id;
             $personal->updated_at = $time;
             $personal->save();
         }
         DB::commit();
         Session()->flash('flash_message', 'Purchases Return has been Completed');
     } catch (\Exception $e) {
         DB::rollBack();
         Session()->flash('flash_error', 'Purchases Return has not Completed!');
     }
     return redirect('purchases_return');
 }
Example #8
0
 public function store(MakePaymentRequest $request)
 {
     try {
         DB::transaction(function () use($request) {
             $payment = new Payment();
             $currentYear = CommonHelper::get_current_financial_year();
             $payment->to_whom_type = $request->to_whom_type;
             $payment->to_whom = $request->to_whom;
             $payment->total_amount = $request->total_amount;
             $payment->amount = $request->amount;
             $payment->transaction_detail = $request->transaction_detail;
             $payment->date = $request->date;
             $payment->year = $currentYear;
             $payment->workspace_id = Auth::user()->workspace_id;
             $payment->account_code = $request->account_code;
             $payment->voucher_no = $request->voucher_no;
             $payment->created_by = Auth::user()->id;
             $payment->created_at = time();
             $payment->save();
             // IMPACTS ON ACCOUNTING TABLES
             $workspace_id = Auth::user()->workspace_id;
             $cashCode = 11000;
             $accountPayableCode = 41000;
             $transaction_type = Config::get('common.transaction_type.payment');
             // Workspace Ledger Account Payable Debit(-)
             $accountReceivableWorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => $accountPayableCode, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
             $accountReceivableWorkspaceData->balance -= $request->amount;
             $accountReceivableWorkspaceData->update();
             // Workspace Ledger Cash Credit(-)
             $cashWorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => $cashCode, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
             $cashWorkspaceData->balance += $request->amount;
             $cashWorkspaceData->update();
             // Personal Account balance(-)
             $person_type = $request->to_whom_type;
             $person_id = $request->to_whom;
             $personData = PersonalAccount::where(['person_id' => $person_id, 'person_type' => $person_type])->first();
             $personData->balance -= $request->amount;
             $personData->update();
             // General Journals Account Payable Debit
             $generalJournal = new GeneralJournal();
             $generalJournal->date = strtotime($request->date);
             $generalJournal->transaction_type = $transaction_type;
             $generalJournal->reference_id = $payment->id;
             $generalJournal->year = $currentYear;
             $generalJournal->account_code = $accountPayableCode;
             $generalJournal->workspace_id = $workspace_id;
             $generalJournal->amount = $request->amount;
             $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
             $generalJournal->created_by = Auth::user()->id;
             $generalJournal->created_at = time();
             $generalJournal->save();
             // General Journals Cash Credit
             $generalJournal = new GeneralJournal();
             $generalJournal->date = strtotime($request->date);
             $generalJournal->transaction_type = $transaction_type;
             $generalJournal->reference_id = $payment->id;
             $generalJournal->year = $currentYear;
             $generalJournal->account_code = $cashCode;
             $generalJournal->workspace_id = $workspace_id;
             $generalJournal->amount = $request->amount;
             $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
             $generalJournal->created_by = Auth::user()->id;
             $generalJournal->created_at = time();
             $generalJournal->save();
         });
     } catch (\Exception $e) {
         //dd($e);
         Session()->flash('error_message', 'Payment Creation Failed!');
         return redirect('make_payments');
     }
     Session()->flash('flash_message', 'Payment Done Successfully!');
     return redirect('make_payments');
 }
Example #9
0
 public function update($id, Request $request)
 {
     $this->validate($request, ['product' => 'array', 'total' => 'required']);
     //        dd($request->input());
     try {
         DB::transaction(function () use($id, $request) {
             $inputs = $request->input();
             $user = Auth::user();
             $time = time();
             $date = strtotime(date('d-m-Y'));
             $year = CommonHelper::get_current_financial_year();
             $transaction_type = Config::get('common.transaction_type.defect_receive');
             $balance_type = Config::get('common.balance_type_intermediate');
             //Defect table affected.
             $defect = Defect::find($id);
             $oldDefect = clone $defect;
             $defect->total = $inputs['total'];
             $defect->cash = $inputs['cash'];
             $defect->due_paid = $inputs['due_paid'];
             $defect->due = $inputs['due'];
             if (isset($inputs['is_replacement'])) {
                 $defect->is_replacement = $inputs['is_replacement'];
                 $defect->replacement = $inputs['new_total'];
             }
             $defect->remarks = $inputs['remarks'];
             $defect->updated_by = $user->id;
             $defect->updated_at = $time;
             $defect->update();
             $defect_amount = $inputs['cash'] + $inputs['due_paid'] + $inputs['due'];
             if ($defect_amount > 0) {
                 $journal = GeneralJournal::where('account_code', '=', 36000)->where('workspace_id', '=', $user->workspace_id)->where('reference_id', '=', $id)->where('transaction_type', '=', $transaction_type)->where('year', '=', $year)->first();
                 $journal->amount = $defect_amount;
                 $journal->updated_by = $user->id;
                 $journal->updated_at = $time;
                 $journal->update();
                 $workspace = WorkspaceLedger::where(['account_code' => 36000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 if ($oldDefect->total > $defect_amount) {
                     $workspace->balance += $oldDefect->total - $defect_amount;
                     //sub defect receive
                 } elseif ($oldDefect->total < $defect_amount) {
                     $workspace->balance += $defect_amount - $oldDefect->total;
                     //sub defect receive
                 }
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
             } else {
                 $journal = GeneralJournal::where('account_code', '=', 36000)->where('workspace_id', '=', $user->workspace_id)->where('reference_id', '=', $id)->where('transaction_type', '=', $transaction_type)->where('year', '=', $year)->first();
                 $journal->delete();
                 $workspace = WorkspaceLedger::where(['account_code' => 36000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $oldDefect->cash + $oldDefect->due_paid + $oldDefect->due;
                 //sub defect receive
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
             }
             if (isset($inputs['delete_product'])) {
                 foreach ($inputs['delete_product'] as $product) {
                     $defectItem = DefectItem::where('defect_id', '=', $id)->where('product_id', '=', $product['product_id'])->first();
                     if ($defectItem) {
                         $defectItem->delete();
                     }
                 }
             }
             //Get Scrap Id
             $material = Material::where('name', '=', 'Scrap')->where('status', '=', 1)->first();
             //Old Defect Items
             foreach ($inputs['old_product'] as $product) {
                 $defectItem = DefectItem::where('defect_id', '=', $id)->where('product_id', '=', $product['product_id'])->first();
                 $oldDefectItem = clone $defectItem;
                 $defectItem->quantity = $product['receive_quantity'];
                 $defectItem->unit_type = $product['unit_type'];
                 $defectItem->unit_price = $product['unit_price'];
                 $defectItem->updated_by = $user->id;
                 $defectItem->updated_at = $time;
                 $defectItem->update();
                 //Material stock updated
                 $rawStock = RawStock::where('year', '=', $year)->where('stock_type', '=', Config::get('common.balance_type_intermediate'))->where('material_id', '=', $material->id)->first();
                 if ($product['unit_type'] == 1) {
                     if ($oldDefectItem->unit_type == 1) {
                         if ($oldDefectItem->quantity > $product['receive_quantity']) {
                             $rawStock->quantity -= ($oldDefectItem->quantity - $product['receive_quantity']) / $product['length'] * $product['weight'];
                         } elseif ($oldDefectItem->quantity < $product['receive_quantity']) {
                             $rawStock->quantity += ($product['receive_quantity'] - $oldDefectItem->quantity) / $product['length'] * $product['weight'];
                         }
                     } elseif ($oldDefectItem->unit_type == 2) {
                         $old_quantity = $oldDefectItem->quantity / $product['weight'] * $product['length'];
                         if ($old_quantity > $product['receive_quantity']) {
                             $rawStock->quantity -= ($old_quantity - $product['receive_quantity']) / $product['length'] * $product['weight'];
                         } elseif ($old_quantity < $product['receive_quantity']) {
                             $rawStock->quantity += ($product['receive_quantity'] - $old_quantity) / $product['length'] * $product['weight'];
                         }
                     }
                 } elseif ($product['unit_type'] == 2) {
                     if ($oldDefectItem->unit_type == 1) {
                         $old_quantity = $oldDefectItem->quantity / $product['length'] * $product['weight'];
                         if ($old_quantity > $product['receive_quantity']) {
                             $rawStock->quantity -= $old_quantity - $product['receive_quantity'];
                         } elseif ($old_quantity < $product['receive_quantity']) {
                             $rawStock->quantity += $product['receive_quantity'] - $old_quantity;
                         }
                     } elseif ($oldDefectItem->unit_type == 2) {
                         if ($oldDefectItem->quantity > $product['receive_quantity']) {
                             $rawStock->quantity -= $oldDefectItem->quantity - $product['receive_quantity'];
                         } elseif ($oldDefectItem->quantity < $product['receive_quantity']) {
                             $rawStock->quantity += $product['receive_quantity'] - $oldDefectItem->quantity;
                         }
                     }
                 }
                 $rawStock->updated_by = $user->id;
                 $rawStock->updated_at = $time;
                 $rawStock->update();
             }
             //New Defect Items
             if (!empty($inputs['product'])) {
                 foreach ($inputs['product'] as $product) {
                     $defectItem = new DefectItem();
                     $defectItem->defect_id = $id;
                     $defectItem->product_id = $product['product_id'];
                     $defectItem->quantity = $product['receive_quantity'];
                     $defectItem->unit_type = $product['unit_type'];
                     $defectItem->unit_price = $product['unit_price'];
                     $defectItem->created_by = $user->id;
                     $defectItem->created_at = $time;
                     $defectItem->save();
                     //Material stock updated
                     $rawStock = RawStock::where('year', '=', $year)->where('stock_type', '=', Config::get('common.balance_type_intermediate'))->where('material_id', '=', $material->id)->first();
                     if ($product['unit_type'] == 1) {
                         $rawStock->quantity += $product['receive_quantity'] / $product['length'] * $product['weight'];
                     } else {
                         $rawStock->quantity += $product['receive_quantity'];
                     }
                     $rawStock->updated_by = $user->id;
                     $rawStock->updated_at = $time;
                     $rawStock->update();
                 }
             }
             if ($inputs['cash'] && !$oldDefect->cash) {
                 //Cash
                 $journal = new GeneralJournal();
                 $journal->date = $date;
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $id;
                 $journal->year = $year;
                 $journal->account_code = 11000;
                 //Cash
                 $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                 $journal->workspace_id = $user->workspace_id;
                 $journal->amount = $inputs['cash'];
                 $journal->created_by = $user->id;
                 $journal->created_at = $time;
                 $journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $inputs['cash'] - $oldDefect->cash;
                 //sub cash
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
             } elseif (!$inputs['cash'] && $oldDefect->cash) {
                 //Cash
                 $journal = GeneralJournal::where('account_code', '=', 11000)->where('workspace_id', '=', $user->workspace_id)->where('reference_id', '=', $id)->where('transaction_type', '=', $transaction_type)->where('year', '=', $year)->first();
                 $journal->delete();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $oldDefect->cash;
                 //add cash
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
             } elseif ($inputs['cash'] > $oldDefect->cash) {
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 if ($workspace->balance < $inputs['cash'] - $oldDefect->cash) {
                     Session()->flash('warning_message', 'Insufficient cash balance!.');
                     throw new \Exception();
                 }
                 $workspace->balance -= $inputs['cash'] - $oldDefect->cash;
                 //sub cash
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
                 $journal = GeneralJournal::where('account_code', '=', 11000)->where('workspace_id', '=', $user->workspace_id)->where('reference_id', '=', $id)->where('transaction_type', '=', $transaction_type)->where('year', '=', $year)->first();
                 $journal->amount = $inputs['cash'];
                 $journal->updated_by = $user->id;
                 $journal->updated_at = $time;
                 $journal->update();
             } elseif ($inputs['cash'] < $oldDefect->cash) {
                 $journal = GeneralJournal::where('account_code', '=', 11000)->where('workspace_id', '=', $user->workspace_id)->where('reference_id', '=', $id)->where('transaction_type', '=', $transaction_type)->where('year', '=', $year)->first();
                 $journal->amount = $inputs['cash'];
                 $journal->updated_by = $user->id;
                 $journal->updated_at = $time;
                 $journal->update();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $oldDefect->cash - $inputs['cash'];
                 //add cash
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
             }
             if ($inputs['due_paid'] && !$oldDefect->due_paid) {
                 //Due Pay
                 $journal = new GeneralJournal();
                 $journal->date = $date;
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $defect->id;
                 $journal->year = $year;
                 $journal->account_code = 12000;
                 //Account Receivable
                 $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                 $journal->workspace_id = $user->workspace_id;
                 $journal->amount = $inputs['due_paid'];
                 $journal->created_by = $user->id;
                 $journal->created_at = $time;
                 $journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 12000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $inputs['due_paid'];
                 //sub account receivable
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
                 $personalAccount = PersonalAccount::where('person_type', '=', $oldDefect->customer_type)->where('person_id', '=', $inputs['customer_id'])->first();
                 $personalAccount->due -= $inputs['due_paid'];
                 //Sub due
                 $personalAccount->updated_by = $user->id;
                 $personalAccount->updated_at = $time;
                 $personalAccount->update();
             } elseif (!$inputs['due_paid'] && $oldDefect->due_paid) {
                 $journal = GeneralJournal::where('account_code', '=', 12000)->where('workspace_id', '=', $user->workspace_id)->where('reference_id', '=', $id)->where('transaction_type', '=', $transaction_type)->where('year', '=', $year)->first();
                 $journal->delete();
                 $workspace = WorkspaceLedger::where(['account_code' => 12000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $oldDefect->due_paid;
                 //add account receivable
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
                 $personalAccount = PersonalAccount::where('person_type', '=', $oldDefect->customer_type)->where('person_id', '=', $inputs['customer_id'])->first();
                 $personalAccount->due += $oldDefect->due_paid;
                 //add due
                 $personalAccount->updated_by = $user->id;
                 $personalAccount->updated_at = $time;
                 $personalAccount->update();
             } elseif ($inputs['due_paid'] > $oldDefect->due_paid) {
                 $journal = GeneralJournal::where('account_code', '=', 12000)->where('workspace_id', '=', $user->workspace_id)->where('reference_id', '=', $id)->where('transaction_type', '=', $transaction_type)->where('year', '=', $year)->first();
                 $journal->amount = $inputs['due_paid'];
                 $journal->updated_by = $user->id;
                 $journal->updated_at = $time;
                 $journal->update();
                 $workspace = WorkspaceLedger::where(['account_code' => 12000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $inputs['due_paid'] - $oldDefect->due_paid;
                 //sub account receivable
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
                 $personalAccount = PersonalAccount::where('person_type', '=', $oldDefect->customer_type)->where('person_id', '=', $inputs['customer_id'])->first();
                 $personalAccount->due -= $inputs['due_paid'] - $oldDefect->due_paid;
                 //Sub due
                 $personalAccount->updated_by = $user->id;
                 $personalAccount->updated_at = $time;
                 $personalAccount->update();
             } elseif ($inputs['due_paid'] < $oldDefect->due_paid) {
                 $journal = GeneralJournal::where('account_code', '=', 12000)->where('workspace_id', '=', $user->workspace_id)->where('reference_id', '=', $id)->where('transaction_type', '=', $transaction_type)->where('year', '=', $year)->first();
                 $journal->amount = $inputs['due_paid'];
                 $journal->updated_by = $user->id;
                 $journal->updated_at = $time;
                 $journal->update();
                 $workspace = WorkspaceLedger::where(['account_code' => 12000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $oldDefect->due_paid - $inputs['due_paid'];
                 //add account receivable
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
                 $personalAccount = PersonalAccount::where('person_type', '=', $oldDefect->customer_type)->where('person_id', '=', $oldDefect->customer_id)->first();
                 $personalAccount->due += $oldDefect->due_paid - $inputs['due_paid'];
                 //add due
                 $personalAccount->updated_by = $user->id;
                 $personalAccount->updated_at = $time;
                 $personalAccount->update();
             }
             if ($inputs['due'] && !$oldDefect->due) {
                 // Due
                 $journal = new GeneralJournal();
                 $journal->date = $date;
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $defect->id;
                 $journal->year = $year;
                 $journal->account_code = 41000;
                 //Account Payable
                 $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                 $journal->workspace_id = $user->workspace_id;
                 $journal->amount = $inputs['due'];
                 $journal->created_by = $user->id;
                 $journal->created_at = $time;
                 $journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 41000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $inputs['due'];
                 //add account payable
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
                 $personalAccount = PersonalAccount::where('person_type', '=', $oldDefect->customer_type)->where('person_id', '=', $inputs['customer_id'])->first();
                 $personalAccount->balance += $inputs['due'];
                 //Add Balance
                 $personalAccount->updated_by = $user->id;
                 $personalAccount->updated_at = $time;
                 $personalAccount->update();
             } elseif (!$inputs['due'] && $oldDefect->due) {
                 // Due
                 $journal = GeneralJournal::where('account_code', '=', 41000)->where('workspace_id', '=', $user->workspace_id)->where('reference_id', '=', $id)->where('transaction_type', '=', $transaction_type)->where('year', '=', $year)->first();
                 $journal->delete();
                 $workspace = WorkspaceLedger::where(['account_code' => 41000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $oldDefect->due;
                 //sub account payable
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
                 $personalAccount = PersonalAccount::where('person_type', '=', $oldDefect->customer_type)->where('person_id', '=', $inputs['customer_id'])->first();
                 $personalAccount->balance -= $oldDefect->due;
                 //Sub Balance
                 $personalAccount->updated_by = $user->id;
                 $personalAccount->updated_at = $time;
                 $personalAccount->update();
             } elseif ($inputs['due'] > $oldDefect->due) {
                 // Due
                 $journal = GeneralJournal::where('account_code', '=', 41000)->where('workspace_id', '=', $user->workspace_id)->where('reference_id', '=', $id)->where('transaction_type', '=', $transaction_type)->where('year', '=', $year)->first();
                 $journal->amount = $inputs['due'];
                 $journal->updated_by = $user->id;
                 $journal->updated_at = $time;
                 $journal->update();
                 $workspace = WorkspaceLedger::where(['account_code' => 41000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $inputs['due'] - $oldDefect->due;
                 //add account payable
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
                 $personalAccount = PersonalAccount::where('person_type', '=', $oldDefect->customer_type)->where('person_id', '=', $inputs['customer_id'])->first();
                 $personalAccount->balance += $inputs['due'] - $oldDefect->due;
                 //Add Balance
                 $personalAccount->updated_by = $user->id;
                 $personalAccount->updated_at = $time;
                 $personalAccount->update();
             } elseif ($inputs['due'] < $oldDefect->due) {
                 // Due
                 $journal = GeneralJournal::where('account_code', '=', 41000)->where('workspace_id', '=', $user->workspace_id)->where('reference_id', '=', $id)->where('transaction_type', '=', $transaction_type)->where('year', '=', $year)->first();
                 $journal->amount = $inputs['due'];
                 $journal->updated_by = $user->id;
                 $journal->updated_at = $time;
                 $journal->update();
                 $workspace = WorkspaceLedger::where(['account_code' => 41000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $oldDefect->due - $inputs['due'];
                 //sub account payable
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
                 $personalAccount = PersonalAccount::where('person_type', '=', $oldDefect->customer_type)->where('person_id', '=', $inputs['customer_id'])->first();
                 $personalAccount->balance -= $oldDefect->due - $inputs['due'];
                 //sub Balance
                 $personalAccount->updated_by = $user->id;
                 $personalAccount->updated_at = $time;
                 $personalAccount->update();
             }
             if (isset($inputs['is_replacement']) && !$oldDefect->is_replacement) {
                 //Replacement
                 $defectReplacement = new SalesOrder();
                 $defectReplacement->workspace_id = $user->workspace_id;
                 $defectReplacement->defect_id = $defect->id;
                 $defectReplacement->order_type = Config::get('common.sales_order_type.replacement');
                 $defectReplacement->customer_id = $inputs['customer_id'];
                 $defectReplacement->customer_type = $oldDefect->customer_type;
                 $defectReplacement->total = $inputs['new_total'];
                 $defectReplacement->date = $time;
                 $defectReplacement->delivery_status = 1;
                 $defectReplacement->created_by = $user->id;
                 $defectReplacement->created_at = $time;
                 $defectReplacement->save();
                 foreach ($inputs['new_product'] as $new_product) {
                     $defectReplacementItem = new SalesOrderItem();
                     $defectReplacementItem->sales_order_id = $defectReplacement->id;
                     $defectReplacementItem->product_id = $new_product['product_id'];
                     $defectReplacementItem->sales_quantity = $new_product['sales_quantity'];
                     $defectReplacementItem->sales_unit_type = $new_product['sales_unit_type'];
                     $defectReplacementItem->unit_price = $new_product['unit_price'];
                     $defectReplacementItem->created_by = $user->id;
                     $defectReplacementItem->created_at = $time;
                     $defectReplacementItem->save();
                     $stock = Stock::where('product_id', '=', $new_product['product_id'])->where('stock_type', '=', $balance_type)->where('year', '=', $year)->first();
                     if ($new_product['sales_unit_type'] == 1) {
                         if ($stock->quantity < $new_product['sales_quantity']) {
                             Session()->flash('warning_message', 'Insufficient stock!.');
                             throw new \Exception();
                         }
                         $stock->quantity -= $new_product['sales_quantity'];
                         //Sub stock
                     } elseif ($new_product['sales_unit_type'] == 2) {
                         $sales_quantity = $new_product['sales_quantity'] / $new_product['weight'] * $new_product['length'];
                         if ($stock->quantity < $sales_quantity) {
                             Session()->flash('warning_message', 'Insufficient stock!.');
                             throw new \Exception();
                         }
                         $stock->quantity -= $sales_quantity;
                         //Sub stock
                     }
                     $stock->updated_by = $user->id;
                     $stock->updated_at = $time;
                     $stock->update();
                 }
             } elseif (isset($inputs['is_replacement']) && $oldDefect->is_replacement) {
                 $defectReplacement = SalesOrder::where('defect_id', '=', $id)->first();
                 $oldDefectReplacement = clone $defectReplacement;
                 $defectReplacement->total = $inputs['new_total'];
                 $defectReplacement->updated_by = $user->id;
                 $defectReplacement->updated_at = $time;
                 $defectReplacement->update();
                 if (isset($inputs['delete_replacement_product'])) {
                     foreach ($inputs['delete_replacement_product'] as $product) {
                         $defectReplacementItem = SalesOrderItem::where('sales_order_id', '=', $defectReplacement->id)->where('product_id', '=', $product['product_id'])->first();
                         if ($defectReplacementItem) {
                             $defectReplacementItem->delete();
                         }
                     }
                 }
                 //Old Product
                 foreach ($inputs['old_replacement_product'] as $new_product) {
                     $defectReplacementItem = SalesOrderItem::where('sales_order_id', '=', $defectReplacement->id)->where('product_id', '=', $new_product['product_id'])->first();
                     $oldDefectReplacementItem = clone $defectReplacementItem;
                     $defectReplacementItem->sales_quantity = $new_product['sales_quantity'];
                     $defectReplacementItem->sales_unit_type = $new_product['sales_unit_type'];
                     $defectReplacementItem->unit_price = $new_product['unit_price'];
                     $defectReplacementItem->updated_by = $user->id;
                     $defectReplacementItem->updated_at = $time;
                     $defectReplacementItem->update();
                     $stock = Stock::where('product_id', '=', $new_product['product_id'])->where('stock_type', '=', $balance_type)->where('year', '=', $year)->first();
                     if ($oldDefectReplacementItem->sales_unit_type == 1) {
                         if ($new_product['sales_unit_type'] == 1) {
                             if ($oldDefectReplacementItem->sales_quantity > $new_product['sales_quantity']) {
                                 $stock->quantity += $oldDefectReplacementItem->sales_quantity - $new_product['sales_quantity'];
                                 //Add stock
                             } elseif ($oldDefectReplacementItem->sales_quantity < $new_product['sales_quantity']) {
                                 $sales_quantity = $new_product['sales_quantity'] - $oldDefectReplacementItem->sales_quantity;
                                 if ($stock->quantity < $sales_quantity) {
                                     Session()->flash('warning_message', 'Insufficient stock!.');
                                     throw new \Exception();
                                 }
                                 $stock->quantity -= $sales_quantity;
                                 //Sub stock
                             }
                         } elseif ($new_product['sales_unit_type'] == 2) {
                             $new_sales_quantity = $new_product['sales_quantity'] / $new_product['weight'] * $new_product['length'];
                             if ($oldDefectReplacementItem->sales_quantity > $new_sales_quantity) {
                                 $stock->quantity += $oldDefectReplacementItem->sales_quantity - $new_sales_quantity;
                                 //Add stock
                             } elseif ($oldDefectReplacementItem->sales_quantity < $new_sales_quantity) {
                                 $sales_quantity = $new_sales_quantity - $oldDefectReplacementItem->sales_quantity;
                                 if ($stock->quantity < $sales_quantity) {
                                     Session()->flash('warning_message', 'Insufficient stock!.');
                                     throw new \Exception();
                                 }
                                 $stock->quantity -= $sales_quantity;
                                 //Sub stock
                             }
                         }
                     } elseif ($oldDefectReplacementItem->sales_unit_type == 2) {
                         if ($new_product['sales_unit_type'] == 1) {
                             $new_sales_quantity = $oldDefectReplacementItem->sales_quantity / $new_product['weight'] * $new_product['length'];
                             if ($new_sales_quantity > $new_product['sales_quantity']) {
                                 $stock->quantity += $new_sales_quantity - $new_product['sales_quantity'];
                                 $stock->quantity += $new_sales_quantity - $new_product['sales_quantity'];
                                 //Add stock
                             } elseif ($new_sales_quantity < $new_product['sales_quantity']) {
                                 $sales_quantity = $new_product['sales_quantity'] - $new_sales_quantity;
                                 //Sub stock
                                 if ($stock->quantity < $sales_quantity) {
                                     Session()->flash('warning_message', 'Insufficient stock!.');
                                     throw new \Exception();
                                 }
                                 $stock->quantity -= $sales_quantity;
                                 //Sub stock
                             }
                         } elseif ($new_product['sales_unit_type'] == 2) {
                             $old_sales_quantity = $oldDefectReplacementItem->sales_quantity / $new_product['weight'] * $new_product['length'];
                             $new_sales_quantity = $new_product['sales_quantity'] / $new_product['weight'] * $new_product['length'];
                             if ($old_sales_quantity > $new_sales_quantity) {
                                 $stock->quantity += $old_sales_quantity - $new_sales_quantity;
                                 //Add stock
                             } elseif ($old_sales_quantity < $new_sales_quantity) {
                                 $sales_quantity = $new_sales_quantity - $old_sales_quantity;
                                 if ($stock->quantity < $sales_quantity) {
                                     Session()->flash('warning_message', 'Insufficient stock!.');
                                     throw new \Exception();
                                 }
                                 $stock->quantity -= $sales_quantity;
                                 //Sub stock
                             }
                         }
                     }
                     $stock->updated_by = $user->id;
                     $stock->updated_at = $time;
                     $stock->update();
                 }
                 //New Product
                 if (!empty($inputs['new_product'])) {
                     foreach ($inputs['new_product'] as $new_product) {
                         $defectReplacementItem = new SalesOrderItem();
                         $defectReplacementItem->sales_order_id = $defectReplacement->id;
                         $defectReplacementItem->product_id = $new_product['product_id'];
                         $defectReplacementItem->sales_quantity = $new_product['sales_quantity'];
                         $defectReplacementItem->sales_unit_type = $new_product['sales_unit_type'];
                         $defectReplacementItem->unit_price = $new_product['unit_price'];
                         $defectReplacementItem->created_by = $user->id;
                         $defectReplacementItem->created_at = $time;
                         $defectReplacementItem->save();
                         $stock = Stock::where('product_id', '=', $new_product['product_id'])->where('stock_type', '=', $balance_type)->where('year', '=', $year)->first();
                         $stock->quantity -= $new_product['sales_quantity'];
                         //Sub stock
                         $stock->updated_by = $user->id;
                         $stock->updated_at = $time;
                         $stock->update();
                     }
                 }
             } elseif (!isset($inputs['is_replacement']) && $oldDefect->is_replacement) {
                 $defectReplacement = SalesOrder::where('defect_id', '=', $id)->first();
                 $oldDefectReplacement = clone $defectReplacement;
                 $defectReplacement->total = $inputs['new_total'];
                 $defectReplacement->updated_by = $user->id;
                 $defectReplacement->updated_at = $time;
                 $defectReplacement->update();
                 if (isset($inputs['delete_replacement_product'])) {
                     foreach ($inputs['delete_replacement_product'] as $product) {
                         $defectReplacementItem = SalesOrderItem::where('sales_order_id', '=', $defectReplacement->id)->where('product_id', '=', $product['product_id'])->first();
                         if ($defectReplacementItem) {
                             $defectReplacementItem->delete();
                         }
                     }
                 }
                 //Old Product
                 foreach ($inputs['old_replacement_product'] as $new_product) {
                     $defectReplacementItem = SalesOrderItem::where('sales_order_id', '=', $defectReplacement->id)->where('product_id', '=', $new_product['product_id'])->first();
                     $oldDefectReplacementItem = clone $defectReplacementItem;
                     $defectReplacementItem->sales_quantity = $new_product['sales_quantity'];
                     $defectReplacementItem->sales_unit_type = $new_product['sales_unit_type'];
                     $defectReplacementItem->unit_price = $new_product['unit_price'];
                     $defectReplacementItem->updated_by = $user->id;
                     $defectReplacementItem->updated_at = $time;
                     $defectReplacementItem->update();
                     $stock = Stock::where('product_id', '=', $new_product['product_id'])->where('stock_type', '=', $balance_type)->where('year', '=', $year)->first();
                     if ($oldDefectReplacementItem->sales_unit_type == 1) {
                         if ($new_product['sales_unit_type'] == 1) {
                             if ($oldDefectReplacementItem->sales_quantity > $new_product['sales_quantity']) {
                                 $stock->quantity += $oldDefectReplacementItem->sales_quantity - $new_product['sales_quantity'];
                                 //Add stock
                             } elseif ($oldDefectReplacementItem->sales_quantity < $new_product['sales_quantity']) {
                                 $sales_quantity = $new_product['sales_quantity'] - $oldDefectReplacementItem->sales_quantity;
                                 if ($stock->quantity < $sales_quantity) {
                                     Session()->flash('warning_message', 'Insufficient stock!.');
                                     throw new \Exception();
                                 }
                                 $stock->quantity -= $sales_quantity;
                                 //Sub stock
                             }
                         } elseif ($new_product['sales_unit_type'] == 2) {
                             $new_sales_quantity = $new_product['sales_quantity'] / $new_product['weight'] * $new_product['length'];
                             if ($oldDefectReplacementItem->sales_quantity > $new_sales_quantity) {
                                 $stock->quantity += $oldDefectReplacementItem->sales_quantity - $new_sales_quantity;
                                 //Add stock
                             } elseif ($oldDefectReplacementItem->sales_quantity < $new_sales_quantity) {
                                 $sales_quantity = $new_sales_quantity - $oldDefectReplacementItem->sales_quantity;
                                 if ($stock->quantity < $sales_quantity) {
                                     Session()->flash('warning_message', 'Insufficient stock!.');
                                     throw new \Exception();
                                 }
                                 $stock->quantity -= $sales_quantity;
                                 //Sub stock
                             }
                         }
                     } elseif ($oldDefectReplacementItem->sales_unit_type == 2) {
                         if ($new_product['sales_unit_type'] == 1) {
                             $new_sales_quantity = $oldDefectReplacementItem->sales_quantity / $new_product['weight'] * $new_product['length'];
                             if ($new_sales_quantity > $new_product['sales_quantity']) {
                                 $stock->quantity += $new_sales_quantity - $new_product['sales_quantity'];
                                 $stock->quantity += $new_sales_quantity - $new_product['sales_quantity'];
                                 //Add stock
                             } elseif ($new_sales_quantity < $new_product['sales_quantity']) {
                                 $sales_quantity = $new_product['sales_quantity'] - $new_sales_quantity;
                                 //Sub stock
                                 if ($stock->quantity < $sales_quantity) {
                                     Session()->flash('warning_message', 'Insufficient stock!.');
                                     throw new \Exception();
                                 }
                                 $stock->quantity -= $sales_quantity;
                                 //Sub stock
                             }
                         } elseif ($new_product['sales_unit_type'] == 2) {
                             $old_sales_quantity = $oldDefectReplacementItem->sales_quantity / $new_product['weight'] * $new_product['length'];
                             $new_sales_quantity = $new_product['sales_quantity'] / $new_product['weight'] * $new_product['length'];
                             if ($old_sales_quantity > $new_sales_quantity) {
                                 $stock->quantity += $old_sales_quantity - $new_sales_quantity;
                                 //Add stock
                             } elseif ($old_sales_quantity < $new_sales_quantity) {
                                 $sales_quantity = $new_sales_quantity - $old_sales_quantity;
                                 if ($stock->quantity < $sales_quantity) {
                                     Session()->flash('warning_message', 'Insufficient stock!.');
                                     throw new \Exception();
                                 }
                                 $stock->quantity -= $sales_quantity;
                                 //Sub stock
                             }
                         }
                     }
                     $stock->updated_by = $user->id;
                     $stock->updated_at = $time;
                     $stock->update();
                 }
             }
         });
     } catch (\Exception $e) {
         Session()->flash('error_message', 'Defect receive update not success. Please try again.');
         return Redirect::back();
     }
     Session()->flash('flash_message', 'Defect receive updated successfully.');
     return redirect('receive_defect');
 }
Example #10
0
 public function update($id, SalaryGeneratorRequest $request)
 {
     try {
         DB::transaction(function () use($request, $id) {
             $user = Auth::user();
             $time = time();
             $date = strtotime(date('d-m-Y'));
             $year = CommonHelper::get_current_financial_year();
             $transaction_type = Config::get('common.transaction_type.salary');
             $balance_type = Config::get('common.balance_type_intermediate');
             $inputs = $request->input();
             $salary = Salary::find($id);
             $copy = clone $salary;
             $salary->cut = $inputs['cut'];
             $salary->net = $inputs['net'];
             $salary->net_due = $inputs['net'];
             if ($inputs['over_time'] > 0) {
                 $salary->over_time = $inputs['over_time'];
                 $salary->over_time_due = $inputs['overtime_amount'];
                 $salary->over_time_amount = $inputs['overtime_amount'];
             }
             if ($inputs['bonus'] > 0) {
                 $salary->bonus = $inputs['bonus'];
                 $salary->bonus_due = $inputs['bonus'];
             }
             $salary->update();
             $personalAccount = PersonalAccount::where(['person_id' => $copy->employee_id, 'person_type' => Config::get('common.person_type_employee')])->first();
             if ($inputs['net'] > $copy->net) {
                 $balance = $inputs['net'] - $copy->net;
                 $personalAccount->balance += $balance;
                 //Add
             } elseif ($inputs['net'] < $copy->net) {
                 $balance = $copy->net - $inputs['net'];
                 $personalAccount->balance -= $balance;
                 //Sub
             }
             if ($inputs['overtime_amount'] > $copy->over_time_amount) {
                 $personalAccount->overtime_balance += $inputs['overtime_amount'] - $copy->over_time_amount;
             } elseif ($inputs['overtime_amount'] < $copy->over_time_amount) {
                 $personalAccount->overtime_balance -= $copy->over_time_amount - $inputs['overtime_amount'];
             }
             if ($inputs['bonus'] > $copy->bonus) {
                 $personalAccount->bonus_balance += $inputs['bonus'] - $copy->bonus;
             } elseif ($inputs['bonus'] < $copy->bonus) {
                 $personalAccount->bonus_balance -= $copy->bonus - $inputs['bonus'];
             }
             $personalAccount->updated_by = $user->id;
             $personalAccount->updated_at = $time;
             $personalAccount->save();
             if ($inputs['net'] > $copy->net) {
                 //Update Workspace Ledger
                 $accountPayableWorkspaceData = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => 42000, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $accountPayableWorkspaceData->balance += $balance;
                 //Add Salary Payable
                 $accountPayableWorkspaceData->updated_by = $user->id;
                 $accountPayableWorkspaceData->updated_at = $time;
                 $accountPayableWorkspaceData->update();
                 $accountPayableWorkspaceData = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => 22000, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $accountPayableWorkspaceData->balance += $balance;
                 //Add Salary Expense
                 $accountPayableWorkspaceData->updated_by = $user->id;
                 $accountPayableWorkspaceData->updated_at = $time;
                 $accountPayableWorkspaceData->update();
                 // General Journal Table Impact
                 $generalJournal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 42000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $generalJournal->amount += $balance;
                 $generalJournal->updated_by = $user->id;
                 $generalJournal->updated_at = $time;
                 $generalJournal->save();
                 $generalJournal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 22000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $generalJournal->amount += $balance;
                 $generalJournal->updated_by = $user->id;
                 $generalJournal->updated_at = $time;
                 $generalJournal->save();
             } elseif ($inputs['net'] < $copy->net) {
                 //Update Workspace Ledger
                 $accountPayableWorkspaceData = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => 42000, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $accountPayableWorkspaceData->balance -= $balance;
                 //Add Salary Payable
                 $accountPayableWorkspaceData->updated_by = $user->id;
                 $accountPayableWorkspaceData->updated_at = $time;
                 $accountPayableWorkspaceData->update();
                 $accountPayableWorkspaceData = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => 22000, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $accountPayableWorkspaceData->balance -= $balance;
                 //Add Salary Expense
                 $accountPayableWorkspaceData->updated_by = $user->id;
                 $accountPayableWorkspaceData->updated_at = $time;
                 $accountPayableWorkspaceData->update();
                 // General Journal Table Impact
                 $generalJournal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 42000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $generalJournal->amount -= $balance;
                 $generalJournal->updated_by = $user->id;
                 $generalJournal->updated_at = $time;
                 $generalJournal->save();
                 $generalJournal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 22000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $generalJournal->amount -= $balance;
                 $generalJournal->updated_by = $user->id;
                 $generalJournal->updated_at = $time;
                 $generalJournal->save();
             }
             if ($inputs['over_time'] && !$copy->over_time) {
                 //Update Workspace Ledger
                 $accountPayableWorkspaceData = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => 44000, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $accountPayableWorkspaceData->balance += $inputs['overtime_amount'];
                 //Add Overtime Payable
                 $accountPayableWorkspaceData->updated_by = $user->id;
                 $accountPayableWorkspaceData->updated_at = $time;
                 $accountPayableWorkspaceData->update();
                 // General Journal Table Impact
                 $generalJournal = new GeneralJournal();
                 $generalJournal->date = $date;
                 $generalJournal->transaction_type = $transaction_type;
                 $generalJournal->reference_id = $salary->id;
                 $generalJournal->year = $year;
                 $generalJournal->account_code = 44000;
                 $generalJournal->workspace_id = $user->workspace_id;
                 $generalJournal->amount = $inputs['over_time'];
                 $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                 $generalJournal->created_by = $user->id;
                 $generalJournal->created_at = $time;
                 $generalJournal->save();
                 $accountPayableWorkspaceData = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => 29993, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $accountPayableWorkspaceData->balance += $inputs['overtime_amount'];
                 //Add Overtime Expense
                 $accountPayableWorkspaceData->updated_by = $user->id;
                 $accountPayableWorkspaceData->updated_at = $time;
                 $accountPayableWorkspaceData->update();
                 $generalJournal = new GeneralJournal();
                 $generalJournal->date = $date;
                 $generalJournal->transaction_type = $transaction_type;
                 $generalJournal->reference_id = $salary->id;
                 $generalJournal->year = $year;
                 $generalJournal->account_code = 29993;
                 $generalJournal->workspace_id = $user->workspace_id;
                 $generalJournal->amount = $inputs['over_time'];
                 $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                 $generalJournal->created_by = $user->id;
                 $generalJournal->created_at = $time;
                 $generalJournal->save();
             } else {
                 if (!$inputs['over_time'] && $copy->over_time) {
                     //Update Workspace Ledger
                     $accountPayableWorkspaceData = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => 44000, 'balance_type' => $balance_type, 'year' => $year])->first();
                     $accountPayableWorkspaceData->balance -= $copy->over_time;
                     //Add Overtime Payable
                     $accountPayableWorkspaceData->updated_by = $user->id;
                     $accountPayableWorkspaceData->updated_at = $time;
                     $accountPayableWorkspaceData->update();
                     // General Journal Table Impact
                     $generalJournal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 44000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                     $generalJournal->delete();
                     $accountPayableWorkspaceData = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => 29993, 'balance_type' => $balance_type, 'year' => $year])->first();
                     $accountPayableWorkspaceData->balance = $copy->over_time;
                     //Add Overtime Expense
                     $accountPayableWorkspaceData->updated_by = $user->id;
                     $accountPayableWorkspaceData->updated_at = $time;
                     $accountPayableWorkspaceData->update();
                     // General Journal Table Impact
                     $generalJournal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 29993, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                     $generalJournal->deleet();
                 } else {
                     if ($inputs['over_time'] > $copy->over_time) {
                         //Update Workspace Ledger
                         $accountPayableWorkspaceData = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => 44000, 'balance_type' => $balance_type, 'year' => $year])->first();
                         $accountPayableWorkspaceData->balance += $inputs['overtime_amount'] - $copy->overtime_amount;
                         //Add Overtime Payable
                         $accountPayableWorkspaceData->updated_by = $user->id;
                         $accountPayableWorkspaceData->updated_at = $time;
                         $accountPayableWorkspaceData->update();
                         $accountPayableWorkspaceData = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => 29993, 'balance_type' => $balance_type, 'year' => $year])->first();
                         $accountPayableWorkspaceData->balance += $inputs['overtime_amount'] - $copy->overtime_amount;
                         //Add Overtime Expense
                         $accountPayableWorkspaceData->updated_by = $user->id;
                         $accountPayableWorkspaceData->updated_at = $time;
                         $accountPayableWorkspaceData->update();
                         // General Journal Table Impact
                         $generalJournal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 44000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                         $generalJournal->amount += $inputs['overtime_amount'] - $copy->overtime_amount;
                         $generalJournal->updated_by = $user->id;
                         $generalJournal->updated_at = $time;
                         $generalJournal->save();
                         $generalJournal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 29993, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                         $generalJournal->amount += $inputs['overtime_amount'] - $copy->overtime_amount;
                         $generalJournal->updated_by = $user->id;
                         $generalJournal->updated_at = $time;
                         $generalJournal->save();
                     } elseif ($inputs['over_time'] < $copy->over_time) {
                         //Update Workspace Ledger
                         $accountPayableWorkspaceData = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => 44000, 'balance_type' => $balance_type, 'year' => $year])->first();
                         $accountPayableWorkspaceData->balance -= $copy->overtime_amount - $inputs['overtime_amount'];
                         //Sub Overtime Payable
                         $accountPayableWorkspaceData->updated_by = $user->id;
                         $accountPayableWorkspaceData->updated_at = $time;
                         $accountPayableWorkspaceData->update();
                         $accountPayableWorkspaceData = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => 29993, 'balance_type' => $balance_type, 'year' => $year])->first();
                         $accountPayableWorkspaceData->balance -= $copy->overtime_amount - $inputs['overtime_amount'];
                         //Sub Overtime Expense
                         $accountPayableWorkspaceData->updated_by = $user->id;
                         $accountPayableWorkspaceData->updated_at = $time;
                         $accountPayableWorkspaceData->update();
                         // General Journal Table Impact
                         $generalJournal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 44000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                         $generalJournal->amount -= $copy->overtime_amount - $inputs['overtime_amount'];
                         $generalJournal->updated_by = $user->id;
                         $generalJournal->updated_at = $time;
                         $generalJournal->save();
                         $generalJournal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 29993, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                         $generalJournal->amount -= $copy->overtime_amount - $inputs['overtime_amount'];
                         $generalJournal->updated_by = $user->id;
                         $generalJournal->updated_at = $time;
                         $generalJournal->save();
                     }
                 }
             }
             if ($inputs['bonus'] > $copy->bonus) {
                 //Update Workspace Ledger
                 $accountPayableWorkspaceData = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => 45000, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $accountPayableWorkspaceData->balance += $inputs['bonus'] - $copy->bonus;
                 //Add bonus Payable
                 $accountPayableWorkspaceData->updated_by = $user->id;
                 $accountPayableWorkspaceData->updated_at = $time;
                 $accountPayableWorkspaceData->update();
                 $accountPayableWorkspaceData = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => 29970, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $accountPayableWorkspaceData->balance += $inputs['bonus'] - $copy->bonus;
                 //Add bonus Expense
                 $accountPayableWorkspaceData->updated_by = $user->id;
                 $accountPayableWorkspaceData->updated_at = $time;
                 $accountPayableWorkspaceData->update();
                 // General Journal Table Impact
                 $generalJournal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 45000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 if ($generalJournal) {
                     $generalJournal->amount += $inputs['bonus'] - $copy->bonus;
                     $generalJournal->updated_by = $user->id;
                     $generalJournal->updated_at = $time;
                     $generalJournal->save();
                 } else {
                     $generalJournal = new GeneralJournal();
                     $generalJournal->date = $date;
                     $generalJournal->transaction_type = $transaction_type;
                     $generalJournal->reference_id = $salary->id;
                     $generalJournal->year = $year;
                     $generalJournal->account_code = 45000;
                     $generalJournal->workspace_id = $user->workspace_id;
                     $generalJournal->amount = $inputs['bonus'];
                     $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                     $generalJournal->created_by = $user->id;
                     $generalJournal->created_at = $time;
                     $generalJournal->save();
                 }
                 $generalJournal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 29970, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 if ($generalJournal) {
                     $generalJournal->amount += $inputs['bonus'] - $copy->bonus;
                     $generalJournal->updated_by = $user->id;
                     $generalJournal->updated_at = $time;
                     $generalJournal->save();
                 } else {
                     $generalJournal = new GeneralJournal();
                     $generalJournal->date = $date;
                     $generalJournal->transaction_type = $transaction_type;
                     $generalJournal->reference_id = $salary->id;
                     $generalJournal->year = $year;
                     $generalJournal->account_code = 29970;
                     $generalJournal->workspace_id = $user->workspace_id;
                     $generalJournal->amount = $inputs['bonus'];
                     $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                     $generalJournal->created_by = $user->id;
                     $generalJournal->created_at = $time;
                     $generalJournal->save();
                 }
             } elseif ($inputs['bonus'] < $copy->bonus) {
                 //Update Workspace Ledger
                 $accountPayableWorkspaceData = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => 45000, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $accountPayableWorkspaceData->balance -= $copy->bonus - $inputs['bonus'];
                 //Sub bonus Payable
                 $accountPayableWorkspaceData->updated_by = $user->id;
                 $accountPayableWorkspaceData->updated_at = $time;
                 $accountPayableWorkspaceData->update();
                 $accountPayableWorkspaceData = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => 29970, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $accountPayableWorkspaceData->balance -= $copy->bonus - $inputs['bonus'];
                 //Sub bonus Expense
                 $accountPayableWorkspaceData->updated_by = $user->id;
                 $accountPayableWorkspaceData->updated_at = $time;
                 $accountPayableWorkspaceData->update();
                 // General Journal Table Impact
                 $generalJournal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 45000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $generalJournal->amount -= $copy->bonus - $inputs['bonus'];
                 $generalJournal->updated_by = $user->id;
                 $generalJournal->updated_at = $time;
                 $generalJournal->save();
                 $generalJournal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 29970, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $generalJournal->amount -= $copy->bonus - $inputs['bonus'];
                 $generalJournal->updated_by = $user->id;
                 $generalJournal->updated_at = $time;
                 $generalJournal->save();
             }
         });
     } catch (\Exception $e) {
         dd($e);
         Session()->flash('error_message', 'Salary cannot Update. Please Try again.');
         return Redirect::back();
     }
     Session()->flash('flash_message', 'Salary updated successfully.');
     return redirect('salary_generator');
 }
 public function update($id, DiscardedSalesRequest $request)
 {
     try {
         DB::transaction(function () use($request, $id) {
             $workspace_id = Auth::user()->workspace_id;
             $customer_type = $request->customer_type;
             $customer_id = $request->customer_id;
             $total_amount = $request->total_amount;
             $paid_amount = $request->paid_amount;
             $due_amount = $total_amount - $paid_amount;
             $items = $request->items;
             if ($total_amount != $paid_amount && empty($customer_id)) {
                 Session()->flash('warning_message', 'Amount not fully paid. Customer required!');
                 throw new \Exception('error');
             } else {
                 $discardedSales = DiscardedSales::findOrFail($id);
                 $old_paid_amount = $discardedSales->paid_amount;
                 $old_due_amount = $discardedSales->due_amount;
                 $old_total_amount = $discardedSales->total_amount;
                 $discardedSales->year = CommonHelper::get_current_financial_year();
                 $discardedSales->date = $request->date;
                 $discardedSales->customer_type = $request->customer_type;
                 $discardedSales->customer_id = $request->customer_id;
                 $discardedSales->total_amount = $request->total_amount;
                 $discardedSales->paid_amount = $request->paid_amount;
                 $discardedSales->due_amount = $total_amount - $paid_amount;
                 $discardedSales->created_by = Auth::user()->id;
                 $discardedSales->created_at = time();
                 $discardedSales->update();
                 // Initial update sales details
                 DiscardedSalesDetail::where('discarded_sales_id', $id)->update(['status' => 0]);
                 foreach ($items as $key => $item) {
                     $existingSalesDetail = DiscardedSalesDetail::where(['discarded_sales_id' => $id, 'material_id' => $item['material_id']])->first();
                     if ($existingSalesDetail) {
                         $existingSalesDetail->material_id = $item['material_id'];
                         $existingSalesDetail->quantity = $item['sale_quantity'];
                         $existingSalesDetail->amount = $item['amount'];
                         $existingSalesDetail->status = 1;
                         $existingSalesDetail->updated_by = Auth::user()->id;
                         $existingSalesDetail->updated_at = time();
                         $existingSalesDetail->update();
                     } else {
                         $saleDetail = new DiscardedSalesDetail();
                         $saleDetail->discarded_sales_id = $discardedSales->id;
                         $saleDetail->material_id = $item['material_id'];
                         $saleDetail->quantity = $item['sale_quantity'];
                         $saleDetail->amount = $item['amount'];
                         $saleDetail->created_by = Auth::user()->id;
                         $saleDetail->created_at = time();
                         $saleDetail->save();
                     }
                 }
                 if ($old_total_amount != $total_amount) {
                     // Workspace Ledger Discarded Sale Account Debit(+)
                     $discardedWorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => 33000, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => CommonHelper::get_current_financial_year()])->first();
                     if ($old_paid_amount > $paid_amount) {
                         $update_total_amount = $old_total_amount - $total_amount;
                         $discardedWorkspaceData->balance -= $update_total_amount;
                     } else {
                         $update_total_amount = $total_amount - $old_total_amount;
                         $discardedWorkspaceData->balance += $update_total_amount;
                     }
                     $discardedWorkspaceData->update();
                     // General Journal Update
                     $generalJournal = GeneralJournal::where(['transaction_type' => Config::get('common.transaction_type.discarded_sale'), 'reference_id' => $id, 'workspace_id' => $workspace_id, 'account_code' => 33000, 'year' => CommonHelper::get_current_financial_year()])->first();
                     if ($old_total_amount > $total_amount) {
                         $update_total_amount = $old_total_amount - $total_amount;
                         $generalJournal->amount -= $update_total_amount;
                     } else {
                         $update_total_amount = $total_amount - $old_total_amount;
                         $generalJournal->amount += $update_total_amount;
                     }
                     $generalJournal->updated_by = Auth::user()->id;
                     $generalJournal->updated_at = time();
                     $generalJournal->update();
                 }
                 if ($old_paid_amount != $paid_amount) {
                     // Workspace Ledger Cash Account Debit(+)
                     $cashWorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => 11000, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => CommonHelper::get_current_financial_year()])->first();
                     if ($old_paid_amount > $paid_amount) {
                         $update_paid_amount = $old_paid_amount - $paid_amount;
                         $cashWorkspaceData->balance -= $update_paid_amount;
                     } else {
                         $update_paid_amount = $paid_amount - $old_paid_amount;
                         $cashWorkspaceData->balance += $update_paid_amount;
                     }
                     $cashWorkspaceData->update();
                     // General Journals Cash Update
                     $generalJournal = GeneralJournal::where(['transaction_type' => Config::get('common.transaction_type.discarded_sale'), 'reference_id' => $id, 'workspace_id' => $workspace_id, 'account_code' => 11000, 'year' => CommonHelper::get_current_financial_year()])->first();
                     if ($old_paid_amount > $paid_amount) {
                         $update_paid_amount = $old_paid_amount - $paid_amount;
                         $generalJournal->amount -= $update_paid_amount;
                     } else {
                         $update_paid_amount = $paid_amount - $old_paid_amount;
                         $generalJournal->amount += $update_paid_amount;
                     }
                     $generalJournal->updated_by = Auth::user()->id;
                     $generalJournal->updated_at = time();
                     $generalJournal->update();
                 }
                 if ($old_due_amount != $due_amount) {
                     // Workspace Ledger Account Receivable Account Debit(+)
                     $discardedWorkspaceData = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => 12000, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => CommonHelper::get_current_financial_year()])->first();
                     if ($old_due_amount > $due_amount) {
                         $update_due_amount = $old_due_amount - $due_amount;
                         $discardedWorkspaceData->balance -= $update_due_amount;
                     } else {
                         $update_due_amount = $due_amount - $old_due_amount;
                         $discardedWorkspaceData->balance += $update_due_amount;
                     }
                     $discardedWorkspaceData->update();
                     // Personal Account Table
                     $person_type = $customer_type;
                     $person_id = $request->customer_id;
                     $personData = PersonalAccount::where(['person_id' => $person_id, 'person_type' => $person_type])->first();
                     if ($old_due_amount > $due_amount) {
                         $update_due_amount = $old_due_amount - $due_amount;
                         $personData->balance -= $update_due_amount;
                     } else {
                         $update_due_amount = $due_amount - $old_due_amount;
                         $personData->balance += $update_due_amount;
                     }
                     $personData->update();
                     // General Journals Account Receivable Update
                     $generalJournal = GeneralJournal::where(['transaction_type' => Config::get('common.transaction_type.discarded_sale'), 'reference_id' => $id, 'workspace_id' => $workspace_id, 'account_code' => 12000, 'year' => CommonHelper::get_current_financial_year()])->first();
                     if ($old_due_amount > $due_amount) {
                         $update_due_amount = $old_due_amount - $due_amount;
                         $generalJournal->amount -= $update_due_amount;
                     } else {
                         $update_due_amount = $due_amount - $old_due_amount;
                         $generalJournal->amount += $update_due_amount;
                     }
                     $generalJournal->updated_by = Auth::user()->id;
                     $generalJournal->updated_at = time();
                     $generalJournal->update();
                 }
             }
         });
     } catch (\Exception $e) {
         Session()->flash('error_message', 'Discarded material sale not updated!');
         return redirect('discarded_sale');
     }
     Session()->flash('flash_message', 'Discarded material sale has been updated!');
     return redirect('discarded_sale');
 }
 public function update($id, Request $request)
 {
     //        dd($request->input());
     try {
         DB::transaction(function () use($id, $request) {
             $user = Auth::user();
             $time = time();
             $date = strtotime(date('d-m-Y'));
             $balance_type = Config::get('common.balance_type_intermediate');
             $year = CommonHelper::get_current_financial_year();
             $wage = Wage::find($id);
             $clone = clone $wage;
             $wage->wage = $request->wage;
             $wage->paid = $request->paid;
             $wage->due = $request->wage - $request->paid;
             $wage->updated_by = $user->id;
             $wage->updated_at = $time;
             $wage->update();
             if ($clone->paid && !$request->paid) {
                 $workspaceLedger = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => 11000, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspaceLedger->balance += $clone->paid;
                 //Add Cash
                 $workspaceLedger->updated_by = $user->id;
                 $workspaceLedger->updated_at = $time;
                 $workspaceLedger->update();
                 $generalJournal = GeneralJournal::where('transaction_type', '=', Config::get('common.transaction_type.wage_payment'))->where('reference_id', '=', $id)->where('year', '=', $year)->where('account_code', '=', 11000)->where('workspace_id', '=', $user->workspace_id)->first();
                 $generalJournal->delete();
                 $workspaceLedger = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => 29992, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspaceLedger->balance -= $clone->paid;
                 //sub Wage Expense
                 $workspaceLedger->updated_by = $user->id;
                 $workspaceLedger->updated_at = $time;
                 $workspaceLedger->update();
                 $generalJournal = GeneralJournal::where('transaction_type', '=', Config::get('common.transaction_type.wage_payment'))->where('reference_id', '=', $id)->where('year', '=', $year)->where('account_code', '=', 29992)->where('workspace_id', '=', $user->workspace_id)->first();
                 $workspaceLedger->delete();
             } elseif (!$clone->paid && $request->paid) {
                 $workspaceLedger = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => 11000, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspaceLedger->balance -= $request->paid;
                 //Sub Cash
                 $workspaceLedger->updated_by = $user->id;
                 $workspaceLedger->updated_at = $time;
                 $workspaceLedger->update();
                 $generalJournal = GeneralJournal::where('transaction_type', '=', Config::get('common.transaction_type.wage_payment'))->where('reference_id', '=', $id)->where('year', '=', $year)->where('account_code', '=', 11000)->where('workspace_id', '=', $user->workspace_id)->first();
                 $generalJournal->amount += $request->paid;
                 $generalJournal->updated_by = $user->id;
                 $generalJournal->updated_at = $time;
                 $generalJournal->update();
                 $workspaceLedger = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => 29992, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspaceLedger->balance += $request->paid;
                 //sub Wage Expense
                 $workspaceLedger->updated_by = $user->id;
                 $workspaceLedger->updated_at = $time;
                 $workspaceLedger->update();
                 $generalJournal = GeneralJournal::where('transaction_type', '=', Config::get('common.transaction_type.wage_payment'))->where('reference_id', '=', $id)->where('year', '=', $year)->where('account_code', '=', 29992)->where('workspace_id', '=', $user->workspace_id)->first();
                 $generalJournal->amount += $request->paid;
                 $workspaceLedger->updated_by = $user->id;
                 $workspaceLedger->updated_at = $time;
                 $workspaceLedger->update();
             } elseif ($clone->paid > $request->paid) {
                 $workspaceLedger = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => 11000, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspaceLedger->balance += $clone->paid - $request->paid;
                 //Add Cash
                 $workspaceLedger->updated_by = $user->id;
                 $workspaceLedger->updated_at = $time;
                 $workspaceLedger->update();
                 $generalJournal = GeneralJournal::where('transaction_type', '=', Config::get('common.transaction_type.wage_payment'))->where('reference_id', '=', $id)->where('year', '=', $year)->where('account_code', '=', 11000)->where('workspace_id', '=', $user->workspace_id)->first();
                 $generalJournal->amount -= $clone->paid - $request->paid;
                 $generalJournal->updated_by = $user->id;
                 $generalJournal->updated_at = $time;
                 $generalJournal->update();
                 $workspaceLedger = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => 29992, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspaceLedger->balance -= $clone->paid - $request->paid;
                 //sub Wage Expense
                 $workspaceLedger->updated_by = $user->id;
                 $workspaceLedger->updated_at = $time;
                 $workspaceLedger->update();
                 $generalJournal = GeneralJournal::where('transaction_type', '=', Config::get('common.transaction_type.wage_payment'))->where('reference_id', '=', $id)->where('year', '=', $year)->where('account_code', '=', 29992)->where('workspace_id', '=', $user->workspace_id)->first();
                 $generalJournal->amount -= $clone->paid - $request->paid;
                 $workspaceLedger->updated_by = $user->id;
                 $workspaceLedger->updated_at = $time;
                 $workspaceLedger->update();
             } elseif ($clone->paid < $request->paid) {
                 $workspaceLedger = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => 11000, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspaceLedger->balance -= $request->paid - $clone->paid;
                 //Sub Cash
                 $workspaceLedger->updated_by = $user->id;
                 $workspaceLedger->updated_at = $time;
                 $workspaceLedger->update();
                 $generalJournal = GeneralJournal::where('transaction_type', '=', Config::get('common.transaction_type.wage_payment'))->where('reference_id', '=', $id)->where('year', '=', $year)->where('account_code', '=', 11000)->where('workspace_id', '=', $user->workspace_id)->first();
                 $generalJournal->amount += $request->paid - $clone->paid;
                 $generalJournal->updated_by = $user->id;
                 $generalJournal->updated_at = $time;
                 $generalJournal->update();
                 $workspaceLedger = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => 29992, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspaceLedger->balance += $request->paid - $clone->paid;
                 //sub Wage Expense
                 $workspaceLedger->updated_by = $user->id;
                 $workspaceLedger->updated_at = $time;
                 $workspaceLedger->update();
                 $generalJournal = GeneralJournal::where('transaction_type', '=', Config::get('common.transaction_type.wage_payment'))->where('reference_id', '=', $id)->where('year', '=', $year)->where('account_code', '=', 29992)->where('workspace_id', '=', $user->workspace_id)->first();
                 $generalJournal->amount += $request->paid - $clone->paid;
                 $workspaceLedger->updated_by = $user->id;
                 $workspaceLedger->updated_at = $time;
                 $workspaceLedger->update();
             }
             $due = $request->wage - $request->paid;
             if (!$clone->due && $due) {
                 $workspaceLedger = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => 43000, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspaceLedger->balance += $due;
                 //Add wage payable
                 $workspaceLedger->updated_by = $user->id;
                 $workspaceLedger->updated_at = $time;
                 $workspaceLedger->update();
                 $generalJournal = new GeneralJournal();
                 $generalJournal->date = $date;
                 $generalJournal->transaction_type = Config::get('common.transaction_type.wage_payment');
                 $generalJournal->reference_id = $wage->id;
                 $generalJournal->year = $year;
                 $generalJournal->account_code = 43000;
                 $generalJournal->workspace_id = $user->workspace_id;
                 $generalJournal->amount = $due;
                 $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                 $generalJournal->created_by = $user->id;
                 $generalJournal->created_at = $time;
                 $generalJournal->save();
                 $personal = PersonalAccount::where('person_id', '=', $clone->employee_id)->where('person_type', '=', Config::get('common.person_type_employee'))->first();
                 $personal->balance += $due;
                 $personal->updated_by = $user->id;
                 $personal->updated_at = $time;
                 $personal->update();
             } elseif ($clone->due && !$due) {
                 $workspaceLedger = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => 43000, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspaceLedger->balance -= $clone->due;
                 //sub wage payable
                 $workspaceLedger->updated_by = $user->id;
                 $workspaceLedger->updated_at = $time;
                 $workspaceLedger->update();
                 $generalJournal = GeneralJournal::where('transaction_type', '=', Config::get('common.transaction_type.wage_payment'))->where('reference_id', '=', $id)->where('year', '=', $year)->where('account_code', '=', 43000)->where('workspace_id', '=', $user->workspace_id)->first();
                 $workspaceLedger->delete();
                 $personal = PersonalAccount::where('person_id', '=', $clone->employee_id)->where('person_type', '=', Config::get('common.person_type_employee'))->first();
                 $personal->balance -= $clone->due - $due;
                 $personal->updated_by = $user->id;
                 $personal->updated_at = $time;
                 $personal->update();
             } elseif ($clone->due > $due) {
                 $workspaceLedger = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => 43000, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspaceLedger->balance -= $clone->due - $due;
                 //sub wage payable
                 $workspaceLedger->updated_by = $user->id;
                 $workspaceLedger->updated_at = $time;
                 $workspaceLedger->update();
                 $generalJournal = GeneralJournal::where('transaction_type', '=', Config::get('common.transaction_type.wage_payment'))->where('reference_id', '=', $id)->where('year', '=', $year)->where('account_code', '=', 43000)->where('workspace_id', '=', $user->workspace_id)->first();
                 $generalJournal->amount -= $clone->due - $due;
                 $workspaceLedger->updated_by = $user->id;
                 $workspaceLedger->updated_at = $time;
                 $workspaceLedger->update();
                 $personal = PersonalAccount::where('person_id', '=', $clone->employee_id)->where('person_type', '=', Config::get('common.person_type_employee'))->first();
                 $personal->balance -= $clone->due - $due;
                 $personal->updated_by = $user->id;
                 $personal->updated_at = $time;
                 $personal->update();
             } elseif ($clone->due < $due) {
                 $workspaceLedger = WorkspaceLedger::where(['workspace_id' => $user->workspace_id, 'account_code' => 43000, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspaceLedger->balance += $due - $clone->due;
                 //Add wage payable
                 $workspaceLedger->updated_by = $user->id;
                 $workspaceLedger->updated_at = $time;
                 $workspaceLedger->update();
                 $generalJournal = GeneralJournal::where('transaction_type', '=', Config::get('common.transaction_type.wage_payment'))->where('reference_id', '=', $id)->where('year', '=', $year)->where('account_code', '=', 43000)->where('workspace_id', '=', $user->workspace_id)->first();
                 $generalJournal->amount += $due - $clone->due;
                 $workspaceLedger->updated_by = $user->id;
                 $workspaceLedger->updated_at = $time;
                 $workspaceLedger->update();
                 $personal = PersonalAccount::where('person_id', '=', $clone->employee_id)->where('person_type', '=', Config::get('common.person_type_employee'))->first();
                 $personal->balance += $due - $clone->due;
                 $personal->updated_by = $user->id;
                 $personal->updated_at = $time;
                 $personal->update();
             }
         });
     } catch (\Exception $e) {
         dd($e);
         Session()->flash('error_message', 'Wage payment update can not successfully. Please Try again.');
         return Redirect::back();
     }
     Session()->flash('flash_message', 'Successfully Updated.');
     return redirect('daily_wage_payment');
 }
Example #13
0
 public function edit($id)
 {
     try {
         DB::transaction(function () use($id) {
             $cashCode = 11000;
             $currentYear = CommonHelper::get_current_financial_year();
             $workspace_id = Auth::user()->workspace_id;
             $cashTransaction = CashTransaction::findOrFail($id);
             $cashTransaction->received = 1;
             $cashTransaction->receiving_date = date('d-m-Y');
             $cashTransaction->updated_by = Auth::user()->id;
             $cashTransaction->updated_at = time();
             $cashTransaction->update();
             // Workspace Ledger Impact
             // Workspace Ledger Cash Credit(-)
             $cashWorkspaceDataSender = WorkspaceLedger::where(['workspace_id' => $cashTransaction->workspace_from, 'account_code' => $cashCode, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
             $cashWorkspaceDataSender->balance -= $cashTransaction->amount;
             $cashWorkspaceDataSender->update();
             // Workspace Ledger Cash Debit(+)
             $cashWorkspaceDataReceiver = WorkspaceLedger::where(['workspace_id' => $workspace_id, 'account_code' => $cashCode, 'balance_type' => Config::get('common.balance_type_intermediate'), 'year' => $currentYear])->first();
             $cashWorkspaceDataReceiver->balance += $cashTransaction->amount;
             $cashWorkspaceDataReceiver->update();
             // General Journal Impact
             // Sender Cash Account Credit
             $generalJournal = new GeneralJournal();
             $generalJournal->date = strtotime(date('d-m-Y'));
             $generalJournal->transaction_type = Config::get('common.transaction_type.cash_transfer');
             $generalJournal->reference_id = $id;
             $generalJournal->year = $currentYear;
             $generalJournal->account_code = $cashCode;
             $generalJournal->workspace_id = $cashTransaction->workspace_from;
             $generalJournal->amount = $cashTransaction->amount;
             $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
             $generalJournal->created_by = Auth::user()->id;
             $generalJournal->created_at = time();
             $generalJournal->save();
             // Receiver Cash Account Debit
             $generalJournal = new GeneralJournal();
             $generalJournal->date = strtotime(date('d-m-Y'));
             $generalJournal->transaction_type = Config::get('common.transaction_type.cash_transfer');
             $generalJournal->reference_id = $id;
             $generalJournal->year = $currentYear;
             $generalJournal->account_code = $cashCode;
             $generalJournal->workspace_id = $workspace_id;
             $generalJournal->amount = $cashTransaction->amount;
             $generalJournal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
             $generalJournal->created_by = Auth::user()->id;
             $generalJournal->created_at = time();
             $generalJournal->save();
         });
     } catch (\Exception $e) {
         Session()->flash('error_message', 'Cash Not Received!');
         return redirect('cash_transaction');
     }
     Session()->flash('flash_message', 'Cash Received & Account Debited Successfully!');
     return redirect('cash_transaction');
 }
Example #14
0
 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');
 }
Example #15
0
 /**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request $request
  * @param  int $id
  * @return \Illuminate\Http\Response
  */
 public function update(Request $request, $id)
 {
     if (!$request->input('items')) {
         Session()->flash('error_message', 'Purchases Update has not been Completed');
         return redirect('purchases');
     }
     try {
         DB::transaction(function () use($request, $id) {
             $time = time();
             $workspace_id = Auth::user()->workspace_id;
             $balance_type = Config::get('common.balance_type_intermediate');
             $transaction_type = Config::get('common.transaction_type.purchase');
             $person_type_supplier = Config::get('common.person_type_supplier');
             $year = CommonHelper::get_current_financial_year();
             $user_id = Auth::user()->id;
             $old_main_purchase = Purchase::findOrFail($id);
             $purchase = Purchase::findOrFail($id);
             $purchase->supplier_id = $request->input('supplier_id');
             $purchase->purchase_date = $request->input('purchase_date');
             $purchase->transportation_cost = $request->input('transportation_cost');
             $purchase->paid = $request->input('paid');
             $purchase->total = $request->input('total');
             $purchase->updated_at = time();
             $purchase->updated_by = $user_id;
             $purchase->update();
             //get all old items
             $arrange_old_items = [];
             $old_purchases = PurchaseDetail::where('purchase_id', $id)->get();
             foreach ($old_purchases as $old_purchase) {
                 $arrange_old_items[$old_purchase['material_id']] = $old_purchase;
             }
             foreach ($request->input('items') as $item) {
                 if (isset($arrange_old_items[$item['material_id']])) {
                     // update old data
                     $PurchaseDetail = PurchaseDetail::findOrFail($arrange_old_items[$item['material_id']]['id']);
                     $PurchaseDetail->quantity = $item['quantity'];
                     $PurchaseDetail->received_quantity = $item['received_quantity'];
                     $PurchaseDetail->unit_price = $item['unit_price'];
                     $PurchaseDetail->status = 1;
                     $PurchaseDetail->updated_at = time();
                     $PurchaseDetail->updated_by = $user_id;
                     $PurchaseDetail->update();
                     //update stock info
                     if ($arrange_old_items[$item['material_id']]['received_quantity'] < $item['received_quantity']) {
                         $add_amount = $item['received_quantity'] - $arrange_old_items[$item['material_id']]['received_quantity'];
                         RawStock::where(['material_id' => $item['material_id'], 'year' => $year, 'stock_type' => $balance_type])->increment('quantity', $add_amount, ['updated_at' => $time, 'updated_by' => $user_id]);
                     } elseif ($arrange_old_items[$item['material_id']]['received_quantity'] > $item['received_quantity']) {
                         $sub_amount = $arrange_old_items[$item['material_id']]['received_quantity'] - $item['received_quantity'];
                         RawStock::where(['material_id' => $item['material_id'], 'year' => $year, 'stock_type' => $balance_type])->decrement('quantity', $sub_amount, ['updated_at' => $time, 'updated_by' => $user_id]);
                     }
                     unset($arrange_old_items[$item['material_id']]);
                 } else {
                     //purchase details
                     $item['purchase_id'] = $id;
                     $item['status'] = 1;
                     $item['created_at'] = time();
                     $item['created_by'] = $user_id;
                     PurchaseDetail::create($item);
                     //update stock info
                     RawStock::where(['material_id' => $item['material_id'], 'year' => $year, 'stock_type' => $balance_type])->increment('quantity', $item['received_quantity'], ['updated_at' => $time, 'updated_by' => $user_id]);
                 }
             }
             //delete old data
             foreach ($arrange_old_items as $old_item) {
                 //reduce the stock info
                 RawStock::where(['material_id' => $item['material_id'], 'year' => $year, 'stock_type' => $balance_type])->decrement('quantity', $old_item['received_quantity'], ['updated_at' => $time, 'updated_by' => $user_id]);
                 //update the purchase info
                 $PurchaseDetail = PurchaseDetail::findOrFail($old_item['id']);
                 $PurchaseDetail->delete();
             }
             /*
              * Account management
              *
              */
             $input = $request->input();
             if ($old_main_purchase['transportation_cost'] != $input['transportation_cost']) {
                 if ($old_main_purchase['transportation_cost'] > $input['transportation_cost']) {
                     $deviation = $old_main_purchase['transportation_cost'] - $input['transportation_cost'];
                     // Update Workspace Ledger
                     $workspace = WorkspaceLedger::where(['account_code' => 24000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                     $workspace->balance -= $input['transportation_cost'];
                     $workspace->updated_by = $user_id;
                     $workspace->updated_at = $time;
                     $workspace->save();
                     $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                     $workspace->balance += $input['transportation_cost'];
                     $workspace->updated_by = $user_id;
                     $workspace->updated_at = $time;
                     $workspace->save();
                     //Update General Journal
                     $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 24000, 'year' => $year, 'workspace_id' => $workspace_id])->first();
                     $general_journal->amount -= $deviation;
                     $general_journal->updated_by = $user_id;
                     $general_journal->updated_at = $time;
                     $general_journal->save();
                     $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 11000, 'year' => $year, 'workspace_id' => $workspace_id])->first();
                     $general_journal->amount -= $deviation;
                     $general_journal->updated_by = $user_id;
                     $general_journal->updated_at = $time;
                     $general_journal->save();
                 } elseif ($old_main_purchase['transportation_cost'] < $input['transportation_cost']) {
                     $deviation = $input['transportation_cost'] - $old_main_purchase['transportation_cost'];
                     // Update Workspace Ledger
                     $workspace = WorkspaceLedger::where(['account_code' => 24000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                     $workspace->balance += $input['transportation_cost'];
                     $workspace->updated_by = $user_id;
                     $workspace->updated_at = $time;
                     $workspace->save();
                     $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                     $workspace->balance -= $input['transportation_cost'];
                     $workspace->updated_by = $user_id;
                     $workspace->updated_at = $time;
                     $workspace->save();
                     //Update General Journal
                     $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 24000, 'year' => $year, 'workspace_id' => $workspace_id])->first();
                     $general_journal->amount += $deviation;
                     $general_journal->updated_by = $user_id;
                     $general_journal->updated_at = $time;
                     $general_journal->save();
                     $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 11000, 'year' => $year, 'workspace_id' => $workspace_id])->first();
                     $general_journal->amount += $deviation;
                     $general_journal->updated_by = $user_id;
                     $general_journal->updated_at = $time;
                     $general_journal->save();
                 }
             }
             if ($input['paid'] && $old_main_purchase['paid'] < $input['paid']) {
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $new_remain_paid_amount = $input['paid'] - $old_main_purchase['paid'];
                 if ($new_remain_paid_amount > $workspace->balance) {
                     Session()->flash('warning_message', 'Low Balance!! New Purchase paid amount(' . $new_remain_paid_amount . ') is greater than Balance (' . $workspace->balance . ')');
                     throw new \Exception('error');
                 }
             }
             //update the accounting tables
             $workspace_id = Auth::user()->workspace_id;
             $balance_type = Config::get('common.balance_type_intermediate');
             $transaction_type = Config::get('common.transaction_type.purchase');
             $person_type_supplier = Config::get('common.person_type_supplier');
             $user_id = Auth::user()->id;
             $time = time();
             //update general journal for raw material purchase
             $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 25000, 'year' => $year, 'workspace_id' => $workspace_id])->first();
             $general_journal->amount = $input['total'];
             $general_journal->updated_by = $user_id;
             $general_journal->updated_at = $time;
             $general_journal->save();
             //update Workspace for raw material purchase
             $new_total_amount = $input['total'];
             $old_total_amount = $old_main_purchase['total'];
             if ($new_total_amount > $old_total_amount) {
                 $workspace = WorkspaceLedger::where(['account_code' => 25000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $new_total_amount - $old_total_amount;
                 //add material purchase
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
             } elseif ($new_total_amount < $old_total_amount) {
                 $workspace = WorkspaceLedger::where(['account_code' => 25000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $old_total_amount - $new_total_amount;
                 //sub material purchase
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
             }
             //update cash amount
             $new_paid_amount = $input['paid'];
             $old_paid_amount = $old_main_purchase['paid'];
             //                throw new \Exception($new_paid_amount.'='.$old_paid_amount);
             if (!$new_paid_amount && $old_paid_amount) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 11000, 'year' => $year, 'workspace_id' => $workspace_id])->first();
                 $general_journal->delete();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $old_paid_amount;
                 //sub Cash
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
             } elseif (!$old_paid_amount && $new_paid_amount) {
                 //Insert data into General Journal
                 $journal = new GeneralJournal();
                 $journal->date = $time;
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $id;
                 $journal->year = $year;
                 $journal->account_code = 11000;
                 //Cash
                 $journal->workspace_id = $workspace_id;
                 $journal->amount = $new_paid_amount;
                 $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                 $journal->created_by = $user_id;
                 $journal->created_at = $time;
                 $journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $new_paid_amount;
                 //sub Cash
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
             } elseif ($new_paid_amount > $old_paid_amount) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 11000, 'year' => $year, 'workspace_id' => $workspace_id])->first();
                 $general_journal->amount = $new_paid_amount;
                 $general_journal->updated_by = $user_id;
                 $general_journal->updated_at = $time;
                 $general_journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $new_paid_amount - $old_paid_amount;
                 //sub Cash
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
             } elseif ($new_paid_amount < $old_paid_amount) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 11000, 'year' => $year, 'workspace_id' => $workspace_id])->first();
                 $general_journal->amount = $new_paid_amount;
                 $general_journal->updated_by = $user_id;
                 $general_journal->updated_at = $time;
                 $general_journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $old_paid_amount - $new_paid_amount;
                 //add Cash
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
             }
             //DUE management (liabilities)
             $new_due_amount = $input['total'] - $input['paid'];
             $old_due_amount = $old_main_purchase['total'] - $old_main_purchase['paid'];
             if ($new_due_amount && !$old_due_amount) {
                 $journal = new GeneralJournal();
                 $journal->date = $time;
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $id;
                 $journal->year = $year;
                 $journal->account_code = 41000;
                 //Account Payable
                 $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                 $journal->workspace_id = $workspace_id;
                 $journal->amount = $new_due_amount;
                 $journal->created_by = $user_id;
                 $journal->created_at = $time;
                 $journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 41000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $new_due_amount;
                 //add account payable credit
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 // Update Personal Account
                 $personal = PersonalAccount::where('person_id', $input['supplier_id'])->where('person_type', $person_type_supplier)->first();
                 $personal->balance += $new_due_amount;
                 $personal->updated_by = $user_id;
                 $personal->updated_at = $time;
                 $personal->save();
             } elseif ($old_due_amount && !$new_due_amount) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 41000, 'year' => $year, 'workspace_id' => $workspace_id])->first();
                 $general_journal->delete();
                 $workspace = WorkspaceLedger::where(['account_code' => 41000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $old_due_amount;
                 //add account payable debit
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 // Update Personal Account
                 $personal = PersonalAccount::where('person_id', $input['supplier_id'])->where('person_type', $person_type_supplier)->first();
                 $personal->balance -= $old_due_amount;
                 $personal->updated_by = $user_id;
                 $personal->updated_at = $time;
                 $personal->save();
             } elseif ($new_due_amount > $old_due_amount) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 41000, 'year' => $year, 'workspace_id' => $workspace_id])->first();
                 $general_journal->amount = $new_due_amount;
                 $general_journal->updated_by = $user_id;
                 $general_journal->updated_at = $time;
                 $general_journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 41000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $new_due_amount - $old_due_amount;
                 //add account payable debit
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 // Update Personal Account
                 $personal = PersonalAccount::where('person_id', $input['supplier_id'])->where('person_type', $person_type_supplier)->first();
                 $personal->balance += $new_due_amount - $old_due_amount;
                 $personal->updated_by = $user_id;
                 $personal->updated_at = $time;
                 $personal->save();
             } elseif ($new_due_amount < $old_due_amount) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 41000, 'year' => $year, 'workspace_id' => $workspace_id])->first();
                 $general_journal->amount = $new_due_amount;
                 $general_journal->updated_by = $user_id;
                 $general_journal->updated_at = $time;
                 $general_journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 41000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $old_due_amount - $new_due_amount;
                 //add account payable debit
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 // Update Personal Account
                 $personal = PersonalAccount::where('person_id', $input['supplier_id'])->where('person_type', $person_type_supplier)->first();
                 $personal->balance -= $old_due_amount - $new_due_amount;
                 $personal->updated_by = $user_id;
                 $personal->updated_at = $time;
                 $personal->save();
             }
         });
     } catch (\Exception $e) {
         Session()->flash('error_message', $e->getMessage());
         //            Session()->flash('error_message','Purchases Update has not been Completed');
         return Redirect::back();
     }
     Session()->flash('flash_message', 'Purchases Update has been Completed');
     return redirect('purchases');
 }