One Hat Cyber Team
Your IP :
3.17.129.242
Server IP :
104.21.16.1
Server :
Linux agrigation-prod 5.15.0-67-generic #74-Ubuntu SMP Wed Feb 22 14:14:39 UTC 2023 x86_64
Server Software :
nginx/1.24.0
PHP Version :
7.4.33
Buat File
|
Buat Folder
Eksekusi
Dir :
~
/
home
/
forge
/
app.gftag.com
/
app
/
Services
/
Edit File:
SaleReturnService.php
<?php namespace App\Services; use App\Models\Sale; use App\Models\User; use App\Models\SaleItem; use App\Models\SaleReturn; use App\Models\Organisation; use Illuminate\Support\Carbon; use App\Models\SalesReturnItem; use App\Models\DailyTransaction; use Illuminate\Support\Facades\DB; use App\Models\CashBookTransaction; use App\Services\TransactionService; use Illuminate\Support\Facades\Auth; use App\Models\CashTransactionHeader; class SaleReturnService { public function index() { $organisationService = new OrganisationService(); $org_id = $organisationService->getOrganisationByUser(); $saleReturns = SaleReturn::where('organisation_id', $org_id)->orderBy('id', 'desc')->get(); return $saleReturns; } public function getSaleItems($id) { $organisationService = new OrganisationService(); $org_id = $organisationService->getOrganisationByUser(); $sale_data_query = SaleItem::with('item')->where('organisation_id', $org_id)->where('sale_id', $id)->get(); $sale_data = $sale_data_query->map(function ($saleItem) { return [ 'item_name' => optional(optional($saleItem)->item)->name, 'item_id' => optional(optional($saleItem)->item)->id, 'quantity' => $saleItem->quantity, 'unit' => $saleItem->unit, 'sale_item_id' => $saleItem->id, 'price' => optional($saleItem)->price, ]; }); return $sale_data; } public function saveSaleReturn($requestData) { try { $todayDate = Carbon::now()->format('Y-m-d'); $organisationService = new OrganisationService(); $org_id = $organisationService->getOrganisationByUser(); $credit_note_number = $this->createCreditNoteNumber($org_id); $sales_id = $requestData['sale_id']; $sale = Sale::find($sales_id); $dailyTransactionsData = []; $total_amount = 0; $sale_return_data = [ 'sale_id' => $sales_id, 'organisation_id' => $org_id, 'credit_note_number' => $credit_note_number, 'return_date' => $requestData['return_date'], 'narration' => $requestData['narration'], 'sale_discount' => $sale->discount, 'return_amount' => 0 ]; $sale_return = SaleReturn::create($sale_return_data); for ($i = 0, $count = count($requestData['item']); $i < $count; $i++) { $return_quantity = $requestData['return_quantity'][$i]; $price = $requestData['price'][$i]; $amount = $price * $return_quantity; $total_amount += $amount; $sale_return_items = [ 'sale_return_id' => $sale_return->id, 'sale_item_id' => $requestData['sale_item_id'][$i], 'item_id' => $requestData['item_id'][$i], 'quantity' => $return_quantity, 'price' => $price, 'amount' => $amount, ]; $sales_return = SalesReturnItem::create($sale_return_items); $transaction = new TransactionService(); $transaction->createNewTransactionEntry( $org_id, "return", $requestData['item_id'][$i], $requestData['return_quantity'][$i], uniqid(), 'customer', 'organisation', $requestData['return_date'], get_class($sales_return), $sales_return->id ); $dailyTransactionsData[] = [ 'organisation_id' => $org_id, 'item_id' => $requestData['item_id'][$i], 'balance' => $requestData['return_quantity'][$i], 'transaction_date' => $todayDate ]; } $sales_return_data = SalesReturnItem::with( 'saleReturn', 'item', 'saleReturn.sales', )->where('sale_return_id', $sale_return->id)->get(); $this->updateDailyTransactions($dailyTransactionsData); $total = $this->calculateSaleReturnTotals($sales_return_data); $sale_return->return_amount = $total; $sale_return->save(); $cash_book_data = [ 'organisation_id' => $org_id, 'amount' => $total, 'transaction_date' => $requestData['return_date'], 'reference_id' => $sale_return->id, 'reference_type' => get_class($sales_return) ]; $this->updateCashBookTransactions($cash_book_data); DB::commit(); } catch (\Exception $e) { DB::rollback(); return $e; } catch (\Throwable $t) { DB::rollback(); return $t->getMessage(); } return true; } public function createCreditNoteNumber($org_id) { $return = SaleReturn::where('organisation_id', $org_id)->orderBy('id', 'DESC')->first(); $prefix = 'CR/'; $yearMonth = date('Y-m'); if ($return) { $lastInvoiceNumber = $return->credit_note_number; preg_match('/\d+$/', $lastInvoiceNumber, $matches); $incrementalPart = ($matches) ? intval($matches[0]) + 1 : 1; $newIncrementalPart = str_pad($incrementalPart, 4, '0', STR_PAD_LEFT); $newInvoiceNumber = $prefix . $yearMonth . '/' . $newIncrementalPart; } else { $newInvoiceNumber = $prefix . $yearMonth . '/' . '00001'; } return $newInvoiceNumber; } public function update($data) { try { $todayDate = Carbon::now()->format('Y-m-d'); $organisationService = new OrganisationService(); $org_id = $organisationService->getOrganisationByUser(); $total_amount = 0; $sale_return = SaleReturn::find($data['sale_return_id']); if ($data['narration'] != $sale_return->narration || $data['return_date'] != $sale_return->return_date) { $sale_return->update([ 'narration' => $data['narration'], 'return_date' => $data['return_date'], ]); } for ($i = 0, $count = count($data['return_item_id']); $i < $count; $i++) { $get_return_data = SalesReturnItem::find($data['return_item_id'][$i]); $new_return_quantity = $data['return_quantity'][$i] - $get_return_data->quantity; //to update daily Transactions and cash book table $dailyTransactionsData[] = [ 'organisation_id' => $org_id, 'item_id' => $data['item_id'][$i], 'balance' => $new_return_quantity, 'transaction_date' => $todayDate ]; $amount = $data['price'][$i] * $data['return_quantity'][$i]; $total_amount += $amount; $transaction = new TransactionService(); $get_return_data->update([ 'quantity' => $data['return_quantity'][$i], 'amount' => $amount ]); $transaction->updateTransactionOnEdit( $get_return_data->id, $get_return_data->quantity, 'return' ); } $this->updateDailyTransactions($dailyTransactionsData); $sale_return = SaleReturn::find($data['sale_return_id']); $sales_return_data = SalesReturnItem::with( 'saleReturn', 'item', 'saleReturn.sales', )->where('sale_return_id', $data['sale_return_id'])->get(); $total = $this->calculateSaleReturnTotals($sales_return_data); $sale_return->return_amount = $total; $sale_return->save(); $cash_book_data = [ 'organisation_id' => $org_id, 'amount' => $total, 'transaction_date' => $data['return_date'], 'reference_id' => $sale_return->id ]; $this->updateCashBookTransactions($cash_book_data); DB::commit(); } catch (\Exception $e) { DB::rollback(); return $e; } catch (\Throwable $t) { DB::rollback(); return $t->getMessage(); } return true; } public function updateDailyTransactions($dailyTransactionsData) { foreach ($dailyTransactionsData as $transaction) { $daily_transactions = DailyTransaction::where([ 'organisation_id' => $transaction['organisation_id'], 'item_id' => $transaction['item_id'], 'transaction_date' => $transaction['transaction_date'] ])->first(); if ($daily_transactions) { $new_balance = ($daily_transactions->balance + ($transaction['balance'])); $daily_transactions->update(['balance' => $new_balance]); } else { DailyTransaction::create($transaction); } } } public function existingSalesReturnData($id) { $sales_data = SaleReturn::with( 'saleReturnItems', 'saleReturnItems.item', 'saleReturnItems.saleItem', 'sales', )->where('sale_id', $id)->get(); if ($sales_data->isEmpty()) { return false; } return $sales_data; } public function creditNoteData($id) { $sale = SaleReturn::with('saleReturnItems', 'sales', 'saleReturnItems.saleItem')->where('sale_id', $id)->first(); if (!$sale) { return null; } return [ 'sale' => $sale, ]; } public function updateCashBookTransactions($cash_book_data) { $cash_header = CashTransactionHeader::select('id')->where('name', 'Credit Note')->first(); $cashbook = CashBookTransaction::where('reference_id', $cash_book_data['reference_id']) ->where('header_id', $cash_header->id) ->first(); if (!$cashbook) { $cash_book_data['type'] = 'expense'; $cash_book_data['comments'] = 'credit note'; $cash_book_data['header_id'] = $cash_header->id; CashBookTransaction::create($cash_book_data); } else { $cashbook->update($cash_book_data); } } public function calculateSaleReturnTotals($sale_return) { try { $base_total = 0; $gst_total = 0; $total_discount = 0; foreach ($sale_return as $return) { $base_price = $return->amount; $gst = ($return->saleReturn->sales->organisation->state_id == $return->saleReturn->sales->customer->state_id) ? ($return->item->sgst_rate + $return->item->cgst_rate) : $return->item->igst_rate; $line_discount = (int) ($base_price * $return->saleReturn->sale_discount) / 100; $total_discount += $line_discount; $price_after_discount = $base_price - $line_discount; $gst_amount = round(($price_after_discount * $gst) / 100); $gst_total += $gst_amount; $base_total += $price_after_discount + $gst_total; } } catch (\Throwable $t) { return $t->getMessage(); } return $base_total; } }
Simpan