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'); }
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'); }
public function invoice_print($id) { $salesOrder = SalesOrder::where('id', $id)->where('status', '!=', 4)->with(['salesOrderItems', 'salesOrderItems.product'])->first(); return view('sales.salesOrder.invoice')->with(compact('salesOrder')); }
public function getReport(Request $request) { $this->validate($request, ['workspace_id' => 'required', 'customer_id' => 'required_if:customer_type,1,2,3'], ['customer_id.required_if' => 'This field is required']); $workspace_id = $request->input('workspace_id'); $from_date = strtotime($request->input('from_date')); $to_date = strtotime($request->input('to_date') . ' 11:59:59 PM'); $sales_type = $request->input('sales_type'); $customer_type = $request->input('customer_type'); if ($sales_type == Config::get('report.sales_type.All')) { if ($customer_type == 0) { $salesReturns = SalesReturn::with('workspaces')->where('workspace_id', '=', $workspace_id)->where('date', '>=', $from_date)->where('date', '<=', $to_date)->get(); $salesOrders = SalesOrder::with('workspaces')->where('workspace_id', '=', $workspace_id)->where('created_at', '>=', $from_date)->where('created_at', '<=', $to_date)->get(); } else { $customer_id = $request->input('customer_id'); $salesReturns = SalesReturn::with('workspaces')->where('workspace_id', '=', $workspace_id)->where('customer_id', '=', $customer_id)->where('customer_type', '=', $customer_type)->where('date', '>=', $from_date)->where('date', '<=', $to_date)->get(); $salesOrders = SalesOrder::with('workspaces')->where('workspace_id', '=', $workspace_id)->where('customer_id', '=', $customer_id)->where('customer_type', '=', $customer_type)->where('created_at', '>=', $from_date)->where('created_at', '<=', $to_date)->get(); } $results = []; $i = 0; foreach ($salesOrders as $salesOrder) { $results[$i]['date'] = date('d-m-Y', $salesOrder->created_at); $results[$i]['workspace'] = $salesOrder->workspaces->name; if ($salesOrder->status == 1) { $results[$i]['sales_type'] = 'Sales: Not yet delivered'; } elseif ($salesOrder->status == 2) { $results[$i]['sales_type'] = 'Sales: Partially delivered'; } elseif ($salesOrder->status == 4) { $results[$i]['sales_type'] = 'Sales: Fully delivered'; } $results[$i]['customer'] = CommonHelper::getCustomerName($salesOrder->customer_id, $salesOrder->customer_type); $results[$i]['total'] = $salesOrder->total; $results[$i]['discount'] = $salesOrder->discount; $results[$i]['net'] = $salesOrder->total + $salesOrder->transport_cost - $salesOrder->discount; $results[$i]['paid'] = $salesOrder->paid; $results[$i]['due'] = $salesOrder->due; $i++; } foreach ($salesReturns as $salesReturn) { $results[$i]['date'] = date('d-m-Y', $salesReturn->created_at); $results[$i]['workspace'] = $salesReturn->workspaces->name; $results[$i]['sales_type'] = 'Sales Returns'; $results[$i]['customer'] = CommonHelper::getCustomerName($salesReturn->customer_id, $salesReturn->customer_type); $results[$i]['total'] = $salesReturn->total_amount; $results[$i]['discount'] = 0; $results[$i]['net'] = $salesReturn->total_amount; $results[$i]['paid'] = $salesReturn->due_paid; $results[$i]['due'] = $salesReturn->due; $i++; } $view = view('reports.salesReport.report')->with(compact('results'))->render(); return response()->json($view); } elseif ($sales_type == Config::get('report.sales_type.Sales: Fully delivered')) { if ($customer_type == 0) { $salesOrders = SalesOrder::with('workspaces')->where('workspace_id', '=', $workspace_id)->where('created_at', '>=', $from_date)->where('created_at', '<=', $to_date)->where('status', '=', 4)->get(); } else { $customer_id = $request->input('customer_id'); $salesOrders = SalesOrder::with('workspaces')->where('workspace_id', '=', $workspace_id)->where('customer_id', '=', $customer_id)->where('customer_type', '=', $customer_type)->where('created_at', '>=', $from_date)->where('created_at', '<=', $to_date)->where('status', '=', 4)->get(); } // dd($salesOrders); $results = []; $i = 0; foreach ($salesOrders as $salesOrder) { $results[$i]['date'] = date('d-m-Y', $salesOrder->created_at); $results[$i]['workspace'] = $salesOrder->workspaces->name; if ($salesOrder->status == 4) { $results[$i]['sales_type'] = 'Sales: Fully delivered'; } $results[$i]['customer'] = CommonHelper::getCustomerName($salesOrder->customer_id, $salesOrder->customer_type); $results[$i]['total'] = $salesOrder->total; $results[$i]['discount'] = $salesOrder->discount; $results[$i]['net'] = $salesOrder->total + $salesOrder->transport_cost - $salesOrder->discount; $results[$i]['paid'] = $salesOrder->paid; $results[$i]['due'] = $salesOrder->due; $i++; } $view = view('reports.salesReport.report')->with(compact('results'))->render(); return response()->json($view); } elseif ($sales_type == Config::get('report.sales_type.Sales: Partially delivered')) { if ($customer_type == 0) { $salesOrders = SalesOrder::with('workspaces')->where('workspace_id', '=', $workspace_id)->where('created_at', '>=', $from_date)->where('created_at', '<=', $to_date)->where('status', '=', 2)->get(); } else { $customer_id = $request->input('customer_id'); $salesOrders = SalesOrder::with('workspaces')->where('workspace_id', '=', $workspace_id)->where('customer_id', '=', $customer_id)->where('customer_type', '=', $customer_type)->where('created_at', '>=', $from_date)->where('created_at', '<=', $to_date)->where('status', '=', 2)->get(); } $results = []; $i = 0; foreach ($salesOrders as $salesOrder) { $results[$i]['date'] = date('d-m-Y', $salesOrder->created_at); $results[$i]['workspace'] = $salesOrder->workspaces->name; if ($salesOrder->status == 2) { $results[$i]['sales_type'] = 'Sales: Partially delivered'; } $results[$i]['customer'] = CommonHelper::getCustomerName($salesOrder->customer_id, $salesOrder->customer_type); $results[$i]['total'] = $salesOrder->total; $results[$i]['discount'] = $salesOrder->discount; $results[$i]['net'] = $salesOrder->total + $salesOrder->transport_cost - $salesOrder->discount; $results[$i]['paid'] = $salesOrder->paid; $results[$i]['due'] = $salesOrder->due; $i++; } $view = view('reports.salesReport.report')->with(compact('results'))->render(); return response()->json($view); } elseif ($sales_type == Config::get('report.sales_type.Sales: Not yet delivered')) { if ($customer_type == 0) { $salesOrders = SalesOrder::with('workspaces')->where('workspace_id', '=', $workspace_id)->where('created_at', '>=', $from_date)->where('created_at', '<=', $to_date)->where('status', '=', 1)->get(); } else { $customer_id = $request->input('customer_id'); $salesOrders = SalesOrder::with('workspaces')->where('workspace_id', '=', $workspace_id)->where('customer_id', '=', $customer_id)->where('customer_type', '=', $customer_type)->where('created_at', '>=', $from_date)->where('created_at', '<=', $to_date)->where('status', '=', 1)->get(); } $results = []; $i = 0; foreach ($salesOrders as $salesOrder) { $results[$i]['date'] = date('d-m-Y', $salesOrder->created_at); $results[$i]['workspace'] = $salesOrder->workspaces->name; if ($salesOrder->status == 1) { $results[$i]['sales_type'] = 'Sales: Not yet delivered'; } $results[$i]['customer'] = CommonHelper::getCustomerName($salesOrder->customer_id, $salesOrder->customer_type); $results[$i]['total'] = $salesOrder->total; $results[$i]['discount'] = $salesOrder->discount; $results[$i]['net'] = $salesOrder->total + $salesOrder->transport_cost - $salesOrder->discount; $results[$i]['paid'] = $salesOrder->paid; $results[$i]['due'] = $salesOrder->due; $i++; } $view = view('reports.salesReport.report')->with(compact('results'))->render(); return response()->json($view); } elseif ($sales_type == Config::get('report.sales_type.Sales: All')) { if ($customer_type == 0) { $salesOrders = SalesOrder::with('workspaces')->where('workspace_id', '=', $workspace_id)->where('created_at', '>=', $from_date)->where('created_at', '<=', $to_date)->get(); } else { $customer_id = $request->input('customer_id'); $salesOrders = SalesOrder::with('workspaces')->where('workspace_id', '=', $workspace_id)->where('customer_id', '=', $customer_id)->where('customer_type', '=', $customer_type)->where('created_at', '>=', $from_date)->where('created_at', '<=', $to_date)->get(); } $results = []; $i = 0; foreach ($salesOrders as $salesOrder) { $results[$i]['date'] = date('d-m-Y', $salesOrder->created_at); $results[$i]['workspace'] = $salesOrder->workspaces->name; if ($salesOrder->status == 1) { $results[$i]['sales_type'] = 'Sales: Not yet delivered'; } elseif ($salesOrder->status == 2) { $results[$i]['sales_type'] = 'Sales: Partially delivered'; } elseif ($salesOrder->status == 4) { $results[$i]['sales_type'] = 'Sales: Fully delivered'; } $results[$i]['customer'] = CommonHelper::getCustomerName($salesOrder->customer_id, $salesOrder->customer_type); $results[$i]['total'] = $salesOrder->total; $results[$i]['discount'] = $salesOrder->discount; $results[$i]['net'] = $salesOrder->total + $salesOrder->transport_cost - $salesOrder->discount; $results[$i]['paid'] = $salesOrder->paid; $results[$i]['due'] = $salesOrder->due; $i++; } $view = view('reports.salesReport.report')->with(compact('results'))->render(); return response()->json($view); } elseif ($sales_type == Config::get('report.sales_type.Sales Returns')) { if ($customer_type == 0) { $salesReturns = SalesReturn::with('workspaces')->where('workspace_id', '=', $workspace_id)->where('date', '>=', $from_date)->where('date', '<=', $to_date)->get(); } else { $customer_id = $request->input('customer_id'); $salesReturns = SalesReturn::with('workspaces')->where('workspace_id', '=', $workspace_id)->where('customer_id', '=', $customer_id)->where('customer_type', '=', $customer_type)->where('date', '>=', $from_date)->where('date', '<=', $to_date)->get(); } $results = []; $i = 0; foreach ($salesReturns as $salesReturn) { $results[$i]['date'] = date('d-m-Y', $salesReturn->created_at); $results[$i]['workspace'] = $salesReturn->workspaces->name; $results[$i]['sales_type'] = 'Sales Returns'; $results[$i]['customer'] = CommonHelper::getCustomerName($salesReturn->customer_id, $salesReturn->customer_type); $results[$i]['total'] = $salesReturn->total_amount; $results[$i]['discount'] = 0; $results[$i]['net'] = $salesReturn->total_amount; $results[$i]['paid'] = $salesReturn->due_paid; $results[$i]['due'] = $salesReturn->due; $i++; } $view = view('reports.salesReport.report')->with(compact('results'))->render(); return response()->json($view); } }