Exemplo n.º 1
0
 public function update($id, SalesOrderRequest $request)
 {
     try {
         DB::transaction(function () use($request, $id) {
             $inputs = $request->input();
             $user = Auth::user();
             $time = time();
             $date = strtotime(date('d-m-Y'));
             $year = CommonHelper::get_current_financial_year();
             if (empty($inputs['transport_cost'])) {
                 $inputs['transport_cost'] = 0;
             }
             if (empty($inputs['labour_cost'])) {
                 $inputs['labour_cost'] = 0;
             }
             if (empty($inputs['discount'])) {
                 $inputs['discount'] = 0;
             }
             if (!isset($inputs['paid_from_personal_account'])) {
                 $inputs['paid_from_personal_account'] = 0;
             }
             $salesOrder = SalesOrder::find($id);
             $oldSalesOrder = clone $salesOrder;
             $salesOrder->total = $inputs['total'] - $inputs['discount'];
             $salesOrder->discount = $inputs['discount'];
             $salesOrder->paid = $inputs['paid'];
             $salesOrder->personal_account_paid = $inputs['paid_from_personal_account'];
             $salesOrder->due = $inputs['due'];
             $salesOrder->transport_cost = $inputs['transport_cost'];
             $salesOrder->labour_cost = $inputs['labour_cost'];
             $salesOrder->remarks = $inputs['remarks'];
             $salesOrder->updated_by = $user->id;
             $salesOrder->updated_at = $time;
             $salesOrder->update();
             if (isset($inputs['delete_product'])) {
                 foreach ($inputs['delete_product'] as $product) {
                     $salesOrderItem = SalesOrderItem::where('sales_order_id', '=', $id)->where('product_id', '=', $product['product_id'])->first();
                     if ($salesOrderItem) {
                         $salesOrderItem->delete();
                     }
                 }
             }
             $old_products = SalesOrderItem::where('sales_order_id', '=', $id)->where('status', '!=', 4)->get();
             $arrange_old_items = [];
             foreach ($old_products as $old_product) {
                 $arrange_old_items[$old_product['product_id']] = $old_product;
             }
             foreach ($inputs['product'] as $product) {
                 if (isset($arrange_old_items[$product['product_id']])) {
                     $salesOrderItem = SalesOrderItem::find($arrange_old_items[$product['product_id']]['id']);
                     $salesOrderItem->sales_quantity = $product['sales_quantity'];
                     $salesOrderItem->sales_unit_type = $product['sales_unit_type'];
                     $salesOrderItem->unit_price = $product['unit_price'];
                     $salesOrderItem->updated_by = $user->id;
                     $salesOrderItem->updated_at = $time;
                     $salesOrderItem->update();
                 } else {
                     $salesOrderItem = new SalesOrderItem();
                     $salesOrderItem->sales_order_id = $id;
                     $salesOrderItem->product_id = $product['product_id'];
                     $salesOrderItem->sales_quantity = $product['sales_quantity'];
                     $salesOrderItem->sales_unit_type = $product['sales_unit_type'];
                     $salesOrderItem->unit_price = $product['unit_price'];
                     $salesOrderItem->created_by = $user->id;
                     $salesOrderItem->created_at = $time;
                     $salesOrderItem->status = 1;
                     $salesOrderItem->save();
                 }
                 unset($arrange_old_items[$product['product_id']]);
             }
             $balance_type = Config::get('common.balance_type_intermediate');
             $transaction_type = Config::get('common.transaction_type.sales');
             $new_total_amount = $inputs['total'];
             $old_total_amount = $oldSalesOrder['total'];
             if ($new_total_amount > $old_total_amount) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 31000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $general_journal->amount = $new_total_amount - $inputs['discount'];
                 $general_journal->updated_by = $user->id;
                 $general_journal->updated_at = $time;
                 $general_journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 31000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->year = $year;
                 $workspace->balance += $new_total_amount - $old_total_amount - $inputs['discount'];
                 //Add Product Sales
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
             } elseif ($new_total_amount < $old_total_amount) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 31000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $general_journal->amount = $new_total_amount - $inputs['discount'];
                 $general_journal->updated_by = $user->id;
                 $general_journal->updated_at = $time;
                 $general_journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 31000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->year = $year;
                 $workspace->balance -= $old_total_amount - ($new_total_amount - $inputs['discount']);
                 //Subtract Product Sales
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
             }
             $new_paid_amount = $inputs['paid'];
             $old_paid_amount = $oldSalesOrder['paid'];
             if (!$new_paid_amount && $old_paid_amount) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 11000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $general_journal->delete();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 if ($workspace->balance < $old_paid_amount) {
                     Session()->flash('warning_message', 'Insufficient cash balance!');
                     throw new \Exception();
                 }
                 $workspace->balance -= $old_paid_amount;
                 //sub Cash
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
             } elseif (!$old_paid_amount && $new_paid_amount) {
                 //Insert data into General Journal
                 $journal = new GeneralJournal();
                 $journal->date = $date;
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $id;
                 $journal->year = $year;
                 $journal->account_code = 11000;
                 //Cash
                 $journal->workspace_id = $user->workspace_id;
                 $journal->amount = $new_paid_amount;
                 $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                 $journal->created_by = $user->id;
                 $journal->created_at = $time;
                 $journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->year = $year;
                 $workspace->balance += $new_paid_amount;
                 //Add Cash
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
             } elseif ($new_paid_amount > $old_paid_amount) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 11000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $general_journal->amount = $new_paid_amount;
                 $general_journal->updated_by = $user->id;
                 $general_journal->updated_at = $time;
                 $general_journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $new_paid_amount - $old_paid_amount;
                 //Add Cash
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
             } elseif ($new_paid_amount < $old_paid_amount) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 11000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $general_journal->amount = $new_paid_amount;
                 $general_journal->updated_by = $user->id;
                 $general_journal->updated_at = $time;
                 $general_journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 if ($workspace->balance < $old_paid_amount - $new_paid_amount) {
                     Session()->flash('warning_message', 'Insufficient cash balance!');
                     throw new \Exception();
                 }
                 $workspace->balance -= $old_paid_amount - $new_paid_amount;
                 //sub Cash
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 dd($workspace);
             }
             $new_account_paid = $inputs['paid_from_personal_account'];
             $old_account_paid = $oldSalesOrder->personal_account_paid;
             if (!$new_account_paid && $old_account_paid) {
                 $personal = PersonalAccount::where('person_id', $inputs['customer_id'])->where('person_type', $inputs['customer_type'])->first();
                 $personal->balance += $old_account_paid;
                 $personal->updated_by = $user->id;
                 $personal->updated_at = $time;
                 $personal->update();
                 $workspace = WorkspaceLedger::where(['account_code' => 41000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $old_account_paid;
                 //Add Account Payable
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 41000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $general_journal->delete();
             } elseif ($new_account_paid && !$old_account_paid) {
                 $personal = PersonalAccount::where('person_id', $inputs['customer_id'])->where('person_type', $inputs['customer_type'])->first();
                 $personal->balance -= $new_account_paid;
                 $personal->updated_by = $user->id;
                 $personal->updated_at = $time;
                 $personal->update();
                 $workspace = WorkspaceLedger::where(['account_code' => 41000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $new_account_paid;
                 //Add Account Payable
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
                 $journal = new GeneralJournal();
                 $journal->date = $date;
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $id;
                 $journal->year = $year;
                 $journal->account_code = 41000;
                 //Account Payable
                 $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                 $journal->workspace_id = $user->workspace_id;
                 $journal->amount = $new_account_paid;
                 $journal->created_by = $user->id;
                 $journal->created_at = $time;
                 $journal->save();
             } elseif ($new_account_paid > $old_account_paid) {
                 $personal = PersonalAccount::where('person_id', $salesOrder->customer_id)->where('person_type', $salesOrder->customer_type)->first();
                 $personal->balance -= $new_account_paid - $old_account_paid;
                 $personal->updated_by = $user->id;
                 $personal->updated_at = $time;
                 $personal->update();
                 $workspace = WorkspaceLedger::where(['account_code' => 41000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $new_account_paid - $old_account_paid;
                 //Sub Account Payable
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 41000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $general_journal->amount = $new_account_paid;
                 $general_journal->updated_by = $user->id;
                 $general_journal->updated_at = $time;
                 $general_journal->update();
             } elseif ($new_account_paid < $old_account_paid) {
                 $personal = PersonalAccount::where('person_id', $salesOrder->customer_id)->where('person_type', $salesOrder->customer_type)->first();
                 $personal->balance += $old_account_paid - $new_account_paid;
                 $personal->updated_by = $user->id;
                 $personal->updated_at = $time;
                 $personal->update();
                 $workspace = WorkspaceLedger::where(['account_code' => 41000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $old_account_paid - $new_account_paid;
                 //Add Account Payable
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 41000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $general_journal->amount = $new_account_paid;
                 $general_journal->updated_by = $user->id;
                 $general_journal->updated_at = $time;
                 $general_journal->update();
             }
             $new_due_amount = $inputs['due'];
             $old_due_amount = $oldSalesOrder['due'];
             if ($new_due_amount && !$old_due_amount) {
                 $journal = new GeneralJournal();
                 $journal->date = $date;
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $id;
                 $journal->year = $year;
                 $journal->account_code = 12000;
                 //Account Receivable
                 $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                 $journal->workspace_id = $user->workspace_id;
                 $journal->amount = $new_due_amount;
                 $journal->created_by = $user->id;
                 $journal->created_at = $time;
                 $journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 12000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $new_due_amount;
                 //add account receivable debit
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 // Update Personal Account
                 $personal = PersonalAccount::where('person_id', $oldSalesOrder['customer_id'])->where('person_type', $oldSalesOrder['customer_type'])->first();
                 $personal->due += $new_due_amount;
                 $personal->updated_by = $user->id;
                 $personal->updated_at = $time;
                 $personal->save();
             } elseif ($old_due_amount && !$new_due_amount) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 12000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $general_journal->delete();
                 $workspace = WorkspaceLedger::where(['account_code' => 12000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $old_due_amount;
                 //sub account receivable credit
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 // Update Personal Account
                 $personal = PersonalAccount::where('person_id', $oldSalesOrder['customer_id'])->where('person_type', $oldSalesOrder['customer_type'])->first();
                 $personal->due -= $old_due_amount;
                 $personal->updated_by = $user->id;
                 $personal->updated_at = $time;
                 $personal->save();
             } elseif ($new_due_amount > $old_due_amount) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 12000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $general_journal->amount = $new_due_amount;
                 $general_journal->updated_by = $user->id;
                 $general_journal->updated_at = $time;
                 $general_journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 12000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $new_due_amount - $old_due_amount;
                 //add account receivable debit
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 // Update Personal Account
                 $personal = PersonalAccount::where('person_id', $oldSalesOrder['customer_id'])->where('person_type', $oldSalesOrder['customer_type'])->first();
                 $personal->due += $new_due_amount - $old_due_amount;
                 $personal->updated_by = $user->id;
                 $personal->updated_at = $time;
                 $personal->save();
             } elseif ($new_due_amount < $old_due_amount) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 12000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $general_journal->amount = $new_due_amount;
                 $general_journal->updated_by = $user->id;
                 $general_journal->updated_at = $time;
                 $general_journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 12000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $old_due_amount - $new_due_amount;
                 //add account receivable credit
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 // Update Personal Account
                 $personal = PersonalAccount::where('person_id', $oldSalesOrder['customer_id'])->where('person_type', $oldSalesOrder['customer_type'])->first();
                 $personal->due -= $old_due_amount - $new_due_amount;
                 $personal->updated_by = $user->id;
                 $personal->updated_at = $time;
                 $personal->save();
             }
             $new_transportation_cost = $inputs['transport_cost'];
             $old_transportation_cost = $oldSalesOrder['transport_cost'];
             if ($new_transportation_cost <= 0 && $old_transportation_cost) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 35000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $general_journal->delete();
                 $workspace = WorkspaceLedger::where(['account_code' => 35000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $old_transportation_cost;
                 //sub transportation earning
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 if ($workspace->balance < $old_transportation_cost) {
                     Session()->flash('warning_message', 'Insufficient cash balance!');
                     throw new \Exception();
                 }
                 $workspace->balance -= $old_transportation_cost;
                 //Sub Cash
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
             } elseif ($new_transportation_cost && $old_transportation_cost <= 0) {
                 $journal = new GeneralJournal();
                 $journal->date = $date;
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $id;
                 $journal->year = $year;
                 $journal->account_code = 35000;
                 //Account Receivable
                 $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                 $journal->workspace_id = $user->workspace_id;
                 $journal->amount = $new_transportation_cost;
                 $journal->created_by = $user->id;
                 $journal->created_at = $time;
                 $journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 35000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $new_transportation_cost;
                 //add transportation earning
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $new_transportation_cost;
                 //Add Cash
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
             } elseif ($new_transportation_cost > $old_transportation_cost) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 35000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $general_journal->amount = $new_transportation_cost;
                 $general_journal->updated_by = $user->id;
                 $general_journal->updated_at = $time;
                 $general_journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 35000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $new_transportation_cost - $old_transportation_cost;
                 //Add transportation earning
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $new_transportation_cost - $old_transportation_cost;
                 //Add Cash
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
             } elseif ($new_transportation_cost < $old_transportation_cost) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 35000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $general_journal->amount = $new_transportation_cost;
                 $general_journal->updated_by = $user->id;
                 $general_journal->updated_at = $time;
                 $general_journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 35000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $old_transportation_cost - $new_transportation_cost;
                 //Add transportation earning
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 if ($workspace->balance < $old_transportation_cost - $new_transportation_cost) {
                     Session()->flash('warning_message', 'Insufficient cash balance!');
                     throw new \Exception();
                 }
                 $workspace->balance -= $old_transportation_cost - $new_transportation_cost;
                 //Sub Cash
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
             }
             $new_labour_cost = $inputs['labour_cost'];
             $old_labour_cost = $oldSalesOrder['labour_cost'];
             if ($new_labour_cost <= 0 && $old_labour_cost) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 34000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $general_journal->delete();
                 $workspace = WorkspaceLedger::where(['account_code' => 34000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $old_labour_cost;
                 //sub labour earning
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 if ($workspace->balance < $old_labour_cost) {
                     Session()->flash('warning_message', 'Insufficient cash balance!');
                     throw new \Exception();
                 }
                 $workspace->balance -= $old_labour_cost;
                 //Sub Cash
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
             } elseif ($new_labour_cost && $old_labour_cost <= 0) {
                 $journal = new GeneralJournal();
                 $journal->date = $date;
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $id;
                 $journal->year = $year;
                 $journal->account_code = 34000;
                 //labour earning
                 $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                 $journal->workspace_id = $user->workspace_id;
                 $journal->amount = $new_labour_cost;
                 $journal->created_by = $user->id;
                 $journal->created_at = $time;
                 $journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 34000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $new_labour_cost;
                 //add labour earning
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $new_labour_cost;
                 //Add Cash
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
             } elseif ($new_labour_cost > $old_labour_cost) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 34000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $general_journal->amount = $new_labour_cost;
                 $general_journal->updated_by = $user->id;
                 $general_journal->updated_at = $time;
                 $general_journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 34000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $new_labour_cost - $old_labour_cost;
                 //Add transportation earning
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $new_labour_cost - $old_labour_cost;
                 //Add Cash
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
             } elseif ($new_labour_cost < $old_labour_cost) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 34000, 'year' => $year, 'workspace_id' => $user->workspace_id])->first();
                 $general_journal->amount = $new_labour_cost;
                 $general_journal->updated_by = $user->id;
                 $general_journal->updated_at = $time;
                 $general_journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 34000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $old_labour_cost - $new_labour_cost;
                 //Add labour earning
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $user->workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 if ($workspace->balance < $old_labour_cost - $new_labour_cost) {
                     Session()->flash('warning_message', 'Insufficient cash balance!');
                     throw new \Exception();
                 }
                 $workspace->balance -= $old_labour_cost - $new_labour_cost;
                 //Sub Cash
                 $workspace->updated_by = $user->id;
                 $workspace->updated_at = $time;
                 $workspace->update();
             }
         });
     } catch (\Exception $e) {
         Session()->flash('error_message', 'Sales Order not Updated!');
         return Redirect::back();
     }
     Session()->flash('flash_message', 'Sales Order has been Updated!');
     return redirect('salesOrder');
 }
Exemplo n.º 2
0
 public function store(Request $request)
 {
     try {
         DB::transaction(function () use($request) {
             $inputs = $request->input();
             $user = Auth::user();
             $time = time();
             $year = CommonHelper::get_current_financial_year();
             $order_quantity = 0;
             $deliver_quantity = 0;
             $products = Product::whereIn('id', array_keys($inputs['quantity']))->get()->toArray();
             //                dd($products);
             foreach ($inputs['quantity'] as $key => $value) {
                 if ($value > 0) {
                     $salesDelivery = SalesDeliveryDetail::firstOrNew(['sales_order_id' => $inputs['sales_order_id'], 'product_id' => $key]);
                     if ($salesDelivery->id) {
                         $salesDelivery->updated_by = $user->id;
                         $salesDelivery->updated_at = $time;
                         $d_quantity = $salesDelivery->delivered_quantity += $inputs['deliver_now'][$key];
                         $salesDelivery->last_delivered_quantity = $inputs['deliver_now'][$key];
                         $salesDelivery->save();
                     } else {
                         $salesDelivery->sales_order_id = $inputs['sales_order_id'];
                         $salesDelivery->created_by = $user->id;
                         $salesDelivery->created_at = $time;
                         $salesDelivery->status = 2;
                         //Partial Delivery
                         $salesDelivery->product_id = $key;
                         $salesDelivery->order_quantity = $value;
                         $d_quantity = $salesDelivery->delivered_quantity = $inputs['deliver_now'][$key];
                         $salesDelivery->last_delivered_quantity = $inputs['deliver_now'][$key];
                         $salesDelivery->save();
                     }
                     if ($d_quantity == $value) {
                         $salesDelivery->status = 4;
                         //product delivery completed
                         $salesDelivery->save();
                         $salesOrderItem = SalesOrderItem::firstOrNew(['sales_order_id' => $inputs['sales_order_id'], 'product_id' => $key]);
                         $salesOrderItem->status = 4;
                         // Sales item Delivery Completed
                         $salesOrderItem->save();
                     } else {
                         $salesDelivery->status = 2;
                         //Partial Delivery
                         $salesDelivery->save();
                         $salesOrderItem = SalesOrderItem::firstOrNew(['sales_order_id' => $inputs['sales_order_id'], 'product_id' => $key]);
                         $salesOrderItem->status = 2;
                         //Partial Delivery
                         $salesOrderItem->save();
                     }
                     $deliver_quantity += $d_quantity;
                     $order_quantity += $value;
                     $quantity_delivered = $inputs['deliver_now'][$key];
                     $stocks = Stock::where(['year' => $year, 'stock_type' => Config::get('common.balance_type_intermediate'), 'workspace_id' => $user->workspace_id, 'product_id' => $key])->first();
                     if ($inputs['unit_type'][$key] == 2) {
                         foreach ($products as $item) {
                             if ($item['id'] == $key) {
                                 $quantity_delivered = $quantity_delivered / $item['weight'] * $item['length'];
                                 break;
                             }
                         }
                     }
                     if ($stocks->quantity < $quantity_delivered) {
                         Session()->flash('warning_message', 'Insufficient stock.');
                         throw new \Exception();
                     }
                     $stocks->quantity -= $quantity_delivered;
                     $stocks->updated_by = $user->id;
                     $stocks->updated_at = $time;
                     $stocks->update();
                 }
                 if ($deliver_quantity == $order_quantity) {
                     $salesOrder = SalesOrder::find($inputs['sales_order_id']);
                     $salesOrder->status = 4;
                     // Sales Delivery Completed
                     $salesOrder->save();
                 } else {
                     $salesOrder = SalesOrder::find($inputs['sales_order_id']);
                     $salesOrder->status = 2;
                     //Partial Delivery
                     $salesOrder->save();
                 }
             }
         });
     } catch (\Exception $e) {
         Session()->flash('error_message', 'Products delivered failed.');
         return Redirect::back();
     }
     Session()->flash('flash_message', 'Products delivered successfully.');
     return redirect('salesDelivery');
 }
Exemplo n.º 3
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');
 }