public function store(DiscardedStockRequest $request)
 {
     try {
         DB::transaction(function () use($request) {
             $materialInput = $request->input('material_id');
             $quantityInput = $request->input('quantity');
             $rawStock = RawStock::where(['material_id' => $materialInput, 'year' => CommonHelper::get_current_financial_year(), 'stock_type' => Config::get('common.balance_type_intermediate')])->first();
             $rawStock->quantity += $quantityInput;
             $rawStock->updated_by = Auth::user()->id;
             $rawStock->updated_at = time();
             $rawStock->update();
         });
     } catch (\Exception $e) {
         Session()->flash('warning_message', 'Discarded Material Entry Not Done!');
         return redirect('discarded_stock');
     }
     Session()->flash('flash_message', 'Discarded Material Entry Successful!');
     return redirect('discarded_stock');
 }
Ejemplo n.º 2
0
 public function update($id, UsageRegisterRequest $request)
 {
     try {
         DB::transaction(function () use($request, $id) {
             $existingRegister = UsageRegister::where('id', $id)->first();
             $UsageRegister = UsageRegister::findOrFail($id);
             $existingStock = RawStock::where(['year' => CommonHelper::get_current_financial_year(), 'stock_type' => Config::get('common.balance_type_intermediate'), 'material_id' => $existingRegister->material_id])->first();
             if ($existingStock->quantity + $existingRegister->usage >= $request->input('usage')) {
                 $UsageRegister->date = $request->input('date');
                 $UsageRegister->material_id = $request->input('material_id');
                 $UsageRegister->usage = $request->input('usage');
                 $UsageRegister->status = $request->input('status');
                 $UsageRegister->updated_by = Auth::user()->id;
                 $UsageRegister->updated_at = time();
                 $UsageRegister->update();
                 if ($existingRegister->usage != $request->input('usage')) {
                     if ($existingRegister->usage > $request->input('usage')) {
                         $difference = $existingRegister->usage - $request->input('usage');
                         $stock = RawStock::findOrFail($existingStock->id);
                         $stock->quantity = $existingStock->quantity + $difference;
                         $stock->updated_by = Auth::user()->id;
                         $stock->updated_at = time();
                         $stock->update();
                     } elseif ($existingRegister->usage < $request->input('usage')) {
                         $difference = $request->input('usage') - $existingRegister->usage;
                         $stock = RawStock::findOrFail($existingStock->id);
                         $stock->quantity = $existingStock->quantity - $difference;
                         $stock->updated_by = Auth::user()->id;
                         $stock->updated_at = time();
                         $stock->update();
                     }
                 }
             } else {
                 Session()->flash('warning_message', 'Alert: Not Enough Stock! Usage Quantity ' . $request->input('usage') . ' is greater than Raw Material Stock ' . ($existingStock->quantity + $existingRegister->usage) . '');
                 throw new \Exception('error');
             }
         });
     } catch (\Exception $e) {
         Session()->flash('error_message', 'Usage Register not updated!');
         return redirect('usageRegisters');
     }
     Session()->flash('flash_message', 'Usage Register has been updated!');
     return redirect('usageRegisters');
 }
Ejemplo n.º 3
0
 public function store(Request $request)
 {
     $yearClosingStatus = AccountClosing::where(['year' => CommonHelper::get_current_financial_year(), 'type' => 2])->lists('id');
     if (sizeof($yearClosingStatus) == 0) {
         $workspaces = Workspace::where('status', 1)->lists('id');
         $closedWorkspaces = AccountClosing::where(['year' => CommonHelper::get_current_financial_year(), 'type' => 1])->lists('workspace_id');
         if (sizeof($workspaces) == sizeof($closedWorkspaces)) {
             try {
                 DB::transaction(function () use($request) {
                     $heads = ChartOfAccount::where('status', 1)->lists('code');
                     $currentYear = CommonHelper::get_current_financial_year();
                     foreach ($heads as $head) {
                         $headTotal = WorkspaceLedger::where(['account_code' => $head, 'year' => CommonHelper::get_current_financial_year(), 'balance_type' => Config::get('common.balance_type_closing')])->sum('balance');
                         // Closing Balance Set
                         $generalLedger = new GeneralLedger();
                         $generalLedger->year = $currentYear;
                         $generalLedger->account_code = $head;
                         $generalLedger->balance_type = Config::get('common.balance_type_closing');
                         $generalLedger->balance = $headTotal;
                         $generalLedger->created_by = Auth::user()->id;
                         $generalLedger->created_at = time();
                         $generalLedger->save();
                         // Opening Balance Set for Next Financial Year
                         if (substr($head, 0, 1) == 1 || substr($head, 0, 1) == 4 || substr($head, 0, 1) == 6) {
                             $generalLedger = new GeneralLedger();
                             $generalLedger->year = CommonHelper::get_next_financial_year();
                             $generalLedger->account_code = $head;
                             $generalLedger->balance_type = Config::get('common.balance_type_opening');
                             $generalLedger->balance = $headTotal;
                             $generalLedger->created_by = Auth::user()->id;
                             $generalLedger->created_at = time();
                             $generalLedger->save();
                         } else {
                             $generalLedger = new GeneralLedger();
                             $generalLedger->year = CommonHelper::get_next_financial_year();
                             $generalLedger->account_code = $head;
                             $generalLedger->balance_type = Config::get('common.balance_type_opening');
                             $generalLedger->balance = 0;
                             $generalLedger->created_by = Auth::user()->id;
                             $generalLedger->created_at = time();
                             $generalLedger->save();
                         }
                     }
                     // Account Closing table Impact
                     $accountClosing = new AccountClosing();
                     $accountClosing->type = 2;
                     // Year Closing type=2 and Workspace Closing type=1;
                     $accountClosing->year = $currentYear;
                     $accountClosing->save();
                     // Raw Stock Table Impact
                     $rawMaterials = RawStock::where(['year' => CommonHelper::get_current_financial_year(), 'stock_type' => Config::get('common.balance_type_intermediate')])->get();
                     foreach ($rawMaterials as $rawMaterial) {
                         // Current Year Opening Balance
                         $rawStock = new RawStock();
                         $rawStock->year = $currentYear;
                         $rawStock->stock_type = Config::get('common.balance_type_closing');
                         $rawStock->material_id = $rawMaterial->material_id;
                         $rawStock->quantity = $rawMaterial->quantity;
                         $rawStock->created_by = Auth::user()->id;
                         $rawStock->created_at = time();
                         // Next Year Opening Balance
                         $rawStock = new RawStock();
                         $rawStock->year = CommonHelper::get_next_financial_year();
                         $rawStock->stock_type = Config::get('common.balance_type_opening');
                         $rawStock->material_id = $rawMaterial->material_id;
                         $rawStock->quantity = $rawMaterial->quantity;
                         $rawStock->created_by = Auth::user()->id;
                         $rawStock->created_at = time();
                         // Next Year Intermediate Balance
                         $rawStock = new RawStock();
                         $rawStock->year = CommonHelper::get_next_financial_year();
                         $rawStock->stock_type = Config::get('common.balance_type_intermediate');
                         $rawStock->material_id = $rawMaterial->material_id;
                         $rawStock->quantity = $rawMaterial->quantity;
                         $rawStock->created_by = Auth::user()->id;
                         $rawStock->created_at = time();
                     }
                     // Current Year Data Fetch
                     $existingYearDetail = DB::table('financial_years')->where('year', $currentYear)->first();
                     $nextYear = CommonHelper::get_next_financial_year();
                     // Current Year Inactive
                     DB::table('financial_years')->where('year', $currentYear)->update(['status' => 0]);
                     // New Year Insert
                     DB::table('financial_years')->insert(['year' => $nextYear, 'start_date' => strtotime(date("Y-m-d", $existingYearDetail->start_date) . " + 1 year"), 'end_date' => strtotime(date("Y-m-d", $existingYearDetail->end_date) . " + 1 year"), 'created_by' => Auth::user()->id, 'created_at' => time()]);
                 });
             } catch (\Exception $e) {
                 Session()->flash('error_message', 'Year Closing Not Done!');
                 return redirect('year_closing');
             }
             Session()->flash('flash_message', 'Year Closed And New Year Opened Successfully!');
             return redirect('year_closing');
         } else {
             Session()->flash('warning_message', 'Warning: All Workspace Account Not Closed Yet!');
             return redirect('year_closing');
         }
     } else {
         Session()->flash('warning_message', 'Warning: Year Closing Done For This Year!');
         return redirect('year_closing');
     }
 }
Ejemplo n.º 4
0
 /**
  * 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');
 }
Ejemplo n.º 5
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');
 }
Ejemplo n.º 6
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');
 }
Ejemplo n.º 7
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');
 }
Ejemplo n.º 8
0
 public function getAdjustmentAmounts(Request $request)
 {
     $workspace_id = Auth::user()->workspace_id;
     $account = $request->input('account');
     $year_str = strtotime(date('Y'));
     if ($account == 25000) {
         $purchaseDetail = PurchaseDetail::where(['status' => 1], ['created_at', '>', $year_str])->get(['unit_price', 'quantity']);
         $total_amount = 0;
         $total_quantity = 0;
         foreach ($purchaseDetail as $detail) {
             $total_amount += $detail->quantity * $detail->unit_price;
             $total_quantity += $detail->quantity;
         }
         $unit_price = $total_amount / $total_quantity;
         $stocks = RawStock::where('status', 1)->sum('quantity');
         $remaining_amount = $stocks * $unit_price;
         $return = new stdClass();
         $return->total_amount = $total_amount;
         $return->remaining_amount = $remaining_amount;
         return response()->json($return);
     } elseif ($account == 27000) {
         $supply_amount = TransactionRecorder::where(['workspace_id' => $workspace_id, 'account_code' => $account, 'status' => 1, 'year' => date('Y')])->sum('total_amount');
         return response()->json($supply_amount);
     }
 }