Import/Export for Transfers
This commit is contained in:
@@ -5,6 +5,7 @@ namespace App\BulkActions\Banking;
|
||||
use App\Abstracts\BulkAction;
|
||||
use App\Jobs\Banking\DeleteTransfer;
|
||||
use App\Models\Banking\Transfer;
|
||||
use App\Exports\Banking\Transfers as Export;
|
||||
|
||||
class Transfers extends BulkAction
|
||||
{
|
||||
@@ -16,6 +17,11 @@ class Transfers extends BulkAction
|
||||
'message' => 'bulk_actions.message.delete',
|
||||
'permission' => 'delete-banking-transfers',
|
||||
],
|
||||
'export' => [
|
||||
'name' => 'general.export',
|
||||
'message' => 'bulk_actions.message.export',
|
||||
'type' => 'download',
|
||||
],
|
||||
];
|
||||
|
||||
public function destroy($request)
|
||||
@@ -30,4 +36,11 @@ class Transfers extends BulkAction
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function export($request)
|
||||
{
|
||||
$selected = $this->getSelectedInput($request);
|
||||
|
||||
return \Excel::download(new Export($selected), \Str::filename(trans_choice('general.transfers', 2)) . '.xlsx');
|
||||
}
|
||||
}
|
||||
|
||||
60
app/Exports/Banking/Transfers.php
Normal file
60
app/Exports/Banking/Transfers.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exports\Banking;
|
||||
|
||||
use App\Abstracts\Export;
|
||||
use App\Models\Banking\Transfer as Model;
|
||||
use App\Utilities\Date;
|
||||
|
||||
class Transfers extends Export
|
||||
{
|
||||
public function collection()
|
||||
{
|
||||
$model = Model::with(
|
||||
'expense_transaction',
|
||||
'expense_transaction.account',
|
||||
'income_transaction',
|
||||
'income_transaction.account'
|
||||
)->usingSearchString(request('search'));
|
||||
|
||||
if (!empty($this->ids)) {
|
||||
$model->whereIn('id', (array) $this->ids);
|
||||
}
|
||||
|
||||
return $model->cursor();
|
||||
}
|
||||
|
||||
public function map($model): array
|
||||
{
|
||||
$model->transferred_at = Date::parse($model->expense_transaction->paid_at)->format('Y-m-d');
|
||||
$model->amount = $model->expense_transaction->amount;
|
||||
$model->from_account_name = $model->expense_transaction->account->name;
|
||||
$model->from_currency_code = $model->expense_transaction->currency_code;
|
||||
$model->from_currency_rate = $model->expense_transaction->currency_rate;
|
||||
$model->to_account_name = $model->income_transaction->account->name;
|
||||
$model->to_currency_code = $model->income_transaction->currency_code;
|
||||
$model->to_currency_rate = $model->income_transaction->currency_rate;
|
||||
$model->description = $model->income_transaction->description;
|
||||
$model->payment_method = $model->income_transaction->payment_method;
|
||||
$model->reference = $model->income_transaction->reference;
|
||||
|
||||
return parent::map($model);
|
||||
}
|
||||
|
||||
public function fields(): array
|
||||
{
|
||||
return [
|
||||
'transferred_at',
|
||||
'amount',
|
||||
'from_currency_code',
|
||||
'from_currency_rate',
|
||||
'from_account_name',
|
||||
'to_currency_code',
|
||||
'to_currency_rate',
|
||||
'to_account_name',
|
||||
'description',
|
||||
'payment_method',
|
||||
'reference',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,9 @@ namespace App\Http\Controllers\Banking;
|
||||
|
||||
use App\Abstracts\Http\Controller;
|
||||
use App\Http\Requests\Banking\Transfer as Request;
|
||||
use App\Exports\Banking\Transfers as Export;
|
||||
use App\Http\Requests\Common\Import as ImportRequest;
|
||||
use App\Imports\Banking\Transfers as Import;
|
||||
use App\Jobs\Banking\CreateTransfer;
|
||||
use App\Jobs\Banking\UpdateTransfer;
|
||||
use App\Jobs\Banking\DeleteTransfer;
|
||||
@@ -218,4 +221,32 @@ class Transfers extends Controller
|
||||
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Import the specified resource.
|
||||
*
|
||||
* @param ImportRequest $request
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function import(ImportRequest $request)
|
||||
{
|
||||
\Excel::import(new Import(), $request->file('import'));
|
||||
|
||||
$message = trans('messages.success.imported', ['type' => trans_choice('general.transfers', 2)]);
|
||||
|
||||
flash($message)->success();
|
||||
|
||||
return redirect()->route('transfers.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Export the specified resource.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function export()
|
||||
{
|
||||
return \Excel::download(new Export(), \Str::filename(trans_choice('general.transfers', 2)) . '.xlsx');
|
||||
}
|
||||
}
|
||||
|
||||
118
app/Imports/Banking/Transfers.php
Normal file
118
app/Imports/Banking/Transfers.php
Normal file
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
namespace App\Imports\Banking;
|
||||
|
||||
use App\Abstracts\Import;
|
||||
use App\Models\Banking\Transaction;
|
||||
use App\Models\Banking\Transfer as Model;
|
||||
use App\Models\Setting\Category;
|
||||
use App\Traits\Currencies;
|
||||
|
||||
class Transfers extends Import
|
||||
{
|
||||
use Currencies;
|
||||
|
||||
public function model(array $row)
|
||||
{
|
||||
return new Model($row);
|
||||
}
|
||||
|
||||
public function map($row): array
|
||||
{
|
||||
$row = parent::map($row);
|
||||
|
||||
$row['from_account_id'] = $this->getFromAccountId($row);
|
||||
$row['to_account_id'] = $this->getToAccountId($row);
|
||||
$row['expense_transaction_id'] = $this->getExpenseTransactionId($row);
|
||||
$row['income_transaction_id'] = $this->getIncomeTransactionId($row);
|
||||
|
||||
return $row;
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'from_account_id' => 'required|integer',
|
||||
'from_currency_code' => 'required|string|currency',
|
||||
'from_currency_rate' => 'required',
|
||||
'to_account_id' => 'required|integer',
|
||||
'to_currency_code' => 'required|string|currency',
|
||||
'to_currency_rate' => 'required',
|
||||
'amount' => 'required|amount',
|
||||
'transferred_at' => 'required|date_format:Y-m-d',
|
||||
'payment_method' => 'required|string',
|
||||
];
|
||||
}
|
||||
|
||||
private function getExpenseTransactionId($row)
|
||||
{
|
||||
$expense_transaction = Transaction::create([
|
||||
'company_id' => session('company_id'),
|
||||
'type' => 'expense',
|
||||
'account_id' => $row['from_account_id'],
|
||||
'paid_at' => $row['transferred_at'],
|
||||
'currency_code' => $row['from_currency_code'],
|
||||
'currency_rate' => $row['from_currency_rate'],
|
||||
'amount' => $row['amount'],
|
||||
'contact_id' => 0,
|
||||
'description' => $row['description'],
|
||||
'category_id' => Category::transfer(), // Transfer Category ID
|
||||
'payment_method' => $row['payment_method'],
|
||||
'reference' => $row['reference'],
|
||||
]);
|
||||
|
||||
return $expense_transaction->id;
|
||||
}
|
||||
|
||||
private function getIncomeTransactionId($row)
|
||||
{
|
||||
$amount = $row['amount'];
|
||||
// Convert amount if not same currency
|
||||
if ($row['from_currency_code'] !== $row['to_currency_code']) {
|
||||
$amount = $this->convertBetween(
|
||||
$amount,
|
||||
$row['from_currency_code'],
|
||||
$row['from_currency_rate'],
|
||||
$row['to_currency_code'],
|
||||
$row['to_currency_rate']
|
||||
);
|
||||
}
|
||||
|
||||
$income_transaction = Transaction::create([
|
||||
'company_id' => session('company_id'),
|
||||
'type' => 'income',
|
||||
'account_id' => $row['to_account_id'],
|
||||
'paid_at' => $row['transferred_at'],
|
||||
'currency_code' => $row['to_currency_code'],
|
||||
'currency_rate' => $row['to_currency_rate'],
|
||||
'amount' => $amount,
|
||||
'contact_id' => 0,
|
||||
'description' => $row['description'],
|
||||
'category_id' => Category::transfer(), // Transfer Category ID
|
||||
'payment_method' => $row['payment_method'],
|
||||
'reference' => $row['reference'],
|
||||
]);
|
||||
|
||||
return $income_transaction->id;
|
||||
}
|
||||
|
||||
private function getFromAccountId($row)
|
||||
{
|
||||
$row['account_id'] = $row['from_account_id'] ?? null;
|
||||
$row['account_name'] = $row['from_account_name'] ?? null;
|
||||
$row['account_number'] = $row['from_account_number'] ?? null;
|
||||
$row['currency_code'] = $row['from_currency_code'] ?? null;
|
||||
|
||||
return $this->getAccountId($row);
|
||||
}
|
||||
|
||||
private function getToAccountId($row)
|
||||
{
|
||||
$row['account_id'] = $row['to_account_id'] ?? null;
|
||||
$row['account_name'] = $row['to_account_name'] ?? null;
|
||||
$row['account_number'] = $row['to_account_number'] ?? null;
|
||||
$row['currency_code'] = $row['to_currency_code'] ?? null;
|
||||
|
||||
return $this->getAccountId($row);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user