public function store(SalesOrderRequest $request) { $sales_order_id = ""; $inputs = $request->input(); try { DB::transaction(function () use($inputs, &$sales_order_id) { if (empty($inputs['transport_cost'])) { $inputs['transport_cost'] = 0; } if (empty($inputs['labour_cost'])) { $inputs['labour_cost'] = 0; } if (empty($inputs['discount'])) { $inputs['discount'] = 0; } if (empty($inputs['paid'])) { $inputs['paid'] = 0; } if (!isset($inputs['paid_from_personal_account'])) { $inputs['paid_from_personal_account'] = 0; } $salesOrder = new SalesOrder(); $salesOrder->workspace_id = Auth::user()->workspace_id; $salesOrder->customer_id = $inputs['customer_id']; $salesOrder->customer_type = $inputs['customer_type']; $salesOrder->total = $inputs['total'] - $inputs['discount']; $salesOrder->date = $inputs['date']; $salesOrder->discount = $inputs['discount']; $salesOrder->transport_cost = $inputs['transport_cost']; $salesOrder->labour_cost = $inputs['labour_cost']; $salesOrder->paid = $inputs['paid']; $salesOrder->personal_account_paid = $inputs['paid_from_personal_account']; $salesOrder->due = $inputs['due']; $salesOrder->delivery_status = 1; $salesOrder->created_by = Auth::user()->id; $salesOrder->created_at = time(); $salesOrder->status = 1; $salesOrder->save(); $sales_order_id = $salesOrder->id; unset($data); $total = 0; foreach ($inputs['product'] as $product) { $salesOderItems = new SalesOrderItem(); $salesOderItems->sales_order_id = $sales_order_id; $salesOderItems->product_id = $product['product_id']; $salesOderItems->sales_quantity = $product['sales_quantity']; $salesOderItems->sales_unit_type = $product['sales_unit_type']; $salesOderItems->unit_price = $product['unit_price']; $salesOderItems->created_by = Auth::user()->id; $salesOderItems->created_at = time(); $salesOderItems->status = 1; $total += $product['sales_quantity'] * $product['unit_price']; $salesOderItems->save(); unset($data); } $user_id = Auth::user()->id; $workspace_id = Auth::user()->workspace_id; $balance_type = Config::get('common.balance_type_intermediate'); $transaction_type = Config::get('common.transaction_type.sales'); $time = time(); $date = strtotime(date('d-m-Y')); $year = CommonHelper::get_current_financial_year(); // Update Workspace Ledger if ($inputs['transport_cost'] > 0) { $workspace = WorkspaceLedger::where(['account_code' => 35000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first(); $workspace->balance += $inputs['transport_cost']; //Add Transportation Earning $workspace->updated_by = $user_id; $workspace->updated_at = $time; $workspace->update(); $journal = new GeneralJournal(); $journal->date = $date; $journal->transaction_type = $transaction_type; $journal->reference_id = $sales_order_id; $journal->year = $year; $journal->account_code = 35000; // Transportation Earning $journal->workspace_id = $workspace_id; $journal->amount = $inputs['transport_cost']; $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit'); $journal->created_by = $user_id; $journal->created_at = $time; $journal->save(); } if ($inputs['labour_cost'] > 0) { $workspace = WorkspaceLedger::where(['account_code' => 34000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first(); $workspace->balance += $inputs['labour_cost']; //Add Labour Earning $workspace->updated_by = $user_id; $workspace->updated_at = $time; $workspace->update(); $journal = new GeneralJournal(); $journal->date = $date; $journal->transaction_type = $transaction_type; $journal->reference_id = $sales_order_id; $journal->year = $year; $journal->account_code = 34000; // Labour Earning $journal->workspace_id = $workspace_id; $journal->amount = $inputs['labour_cost']; $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit'); $journal->created_by = $user_id; $journal->created_at = $time; $journal->save(); } if ($inputs['paid'] > 0) { $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first(); $workspace->balance += $inputs['paid']; //Add Cash $workspace->updated_by = $user_id; $workspace->updated_at = $time; $workspace->update(); $journal = new GeneralJournal(); $journal->date = $date; $journal->transaction_type = $transaction_type; $journal->reference_id = $sales_order_id; $journal->year = $year; $journal->account_code = 11000; //Cash $journal->workspace_id = $workspace_id; $journal->amount = $inputs['paid']; $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit'); $journal->created_by = $user_id; $journal->created_at = $time; $journal->save(); } if ($inputs['due'] > 0) { $workspace = WorkspaceLedger::where(['account_code' => 12000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first(); $workspace->balance += $inputs['due']; //Add Account Receivable $workspace->updated_by = $user_id; $workspace->updated_at = $time; $workspace->update(); $journal = new GeneralJournal(); $journal->date = $date; $journal->transaction_type = $transaction_type; $journal->reference_id = $sales_order_id; $journal->year = $year; $journal->account_code = 12000; //Account Receivable $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit'); $journal->workspace_id = $workspace_id; $journal->amount = $inputs['due']; $journal->created_by = $user_id; $journal->created_at = $time; $journal->save(); } $workspace = WorkspaceLedger::where(['account_code' => 31000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first(); $workspace->balance += $inputs['total'] - $inputs['discount']; //Add Product Sales $workspace->updated_by = $user_id; $workspace->updated_at = $time; $workspace->update(); $journal = new GeneralJournal(); $journal->date = $date; $journal->transaction_type = $transaction_type; $journal->reference_id = $sales_order_id; $journal->year = $year; $journal->account_code = 31000; // Product Sales $journal->workspace_id = $workspace_id; $journal->amount = $inputs['total'] - $inputs['discount']; $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 if ($inputs['due'] > 0) { $personal = PersonalAccount::where('person_id', $inputs['customer_id'])->where('person_type', $inputs['customer_type'])->first(); $personal->due += $inputs['due']; $personal->updated_by = $user_id; $personal->updated_at = $time; $personal->update(); } if ($inputs['paid_from_personal_account'] > 0) { $personal = PersonalAccount::where('person_id', $inputs['customer_id'])->where('person_type', $inputs['customer_type'])->first(); $personal->balance -= $inputs['paid_from_personal_account']; $personal->updated_by = $user_id; $personal->updated_at = $time; $personal->update(); $workspace = WorkspaceLedger::where(['account_code' => 41000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first(); $workspace->balance -= $inputs['paid_from_personal_account']; //Sub Account Payable $workspace->updated_by = $user_id; $workspace->updated_at = $time; $workspace->update(); $journal = new GeneralJournal(); $journal->date = $date; $journal->transaction_type = $transaction_type; $journal->reference_id = $salesOrder->id; $journal->year = $year; $journal->account_code = 41000; // Account Payable $journal->workspace_id = $workspace_id; $journal->amount = $inputs['paid_from_personal_account']; $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit'); $journal->created_by = $user_id; $journal->created_at = $time; $journal->save(); } }); } catch (\Exception $e) { dd($e); Session()->flash('error_message', 'Sales Order not created!'); return Redirect::back(); } Session()->flash('flash_message', 'Sales Order has been created!'); return redirect()->action('Sales\\SalesOrderController@invoice_print', [$sales_order_id]); }
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'); }