<?php
namespace App\Http\Controllers;
use App\Bank\Payment;
use App\Classes\TransactionGeneration;
use App\CloseTopUp;
use App\DayCloseStore;
use App\Imali\MerchantContract;
use App\PeriodCloseStore;
use App\StoreConfig;
use Illuminate\Http\Request;
use DB;
class CloseController extends Controller
{
public function getTopUpDailyClose($date1, $date2)
{
$top = CloseTopUp::query()
->leftJoin('admins', 'admins.id', '=', 'close_top_ups.user_id')
->orderByDesc('close_top_ups.created_at')
->where('close_top_ups.created_at', '>=', $date1)
->where('close_top_ups.created_at', '<=', $date2)
->select('close_top_ups.*', 'admins.name as username')
->get();
return response()->json(['data' => $top], 200);
}
public function closePeriodOLD(Request $request)
{
$paymentsAmount = Payment::query()
->where('store_id', $request->user()->id)
->whereDate('created_at', '=', date('Y-m-d', strtotime(' -1 day')))
->sum('payments.amount_credited');
$closes = PeriodCloseStore::query()
->where('store_id', $request->user()->id)
->where('date_reference', '=', date('Y-m-d', strtotime(' -1 day')))
->first();
if ($paymentsAmount > 0) {
$trasactionGeneration = new TransactionGeneration();
$transaction = $trasactionGeneration->generateTransaction();
$close = DayCloseStore::create([
'transaction' => $transaction,
'description' => 'Fecho ' . date('Y-m-d', strtotime(' -1 day')),
'nib' => $request->user()->nib,
'amount' => $paymentsAmount,
'date_reference' => date('Y-m-d', strtotime(' -1 day')),
'store_id' => $request->user()->id,
'user_id' => 1,
]);
if ($close) {
DB::table('stores')->where('id', $request->user()->id)->decrement('balance', $paymentsAmount);
$this->info('Fecho confirmado com Sucesso');
}
}
}
public function getPeriodCloses(Request $request)
{
$transactions = PeriodCloseStore::query()
->join('stores', 'stores.id', '=', 'period_close_stores.store_id')
->select('period_close_stores.*')
->where('period_close_stores.store_id', '=', $request->user()->id)
->where('period_close_stores.status', '=', 'fechado')
->orderByDesc('period_close_stores.created_at')
->get();
$lastPeriod = PeriodCloseStore::query()
->leftJoin('stores', 'stores.id', '=', 'period_close_stores.store_id')
->select('period_close_stores.*')
->where('period_close_stores.store_id', '=', $request->user()->id)
->get()->last();
$storeConfig = StoreConfig::query()
->where('store_id', '=', $request->user()->id)
->first();
if ($storeConfig) {
$storeConfig->makeHidden(['id', 'store_id']);
if (!$lastPeriod) {
return response()->json(['data' => $transactions, 'lastPeriod' => null, 'storeConfig' => $storeConfig]);
} else {
return response()->json(['data' => $transactions, 'lastPeriod' => $lastPeriod, 'storeConfig' => $storeConfig]);
}
} else {
if (!$lastPeriod) {
return response()->json(['data' => $transactions, 'lastPeriod' => null, 'storeConfig' => null]);
} else {
return response()->json(['data' => $transactions, 'lastPeriod' => $lastPeriod, 'storeConfig' => null]);
}
}
}
public function closePeriod(Request $request)
{
$this->validate($request, [
'open_period' => 'required',
'code' => 'required'
]);
$lastPeriod = PeriodCloseStore::query()
->join('stores', 'stores.id', '=', 'period_close_stores.store_id')
->select('period_close_stores.*')
->where('period_close_stores.store_id', '=', $request->user()->id)
->where('period_close_stores.code', '=', $request->code)
->first();
// ->get()->last();
$periodDate = date_create($request->open_period);
$paymentsAmount = Payment::query()
->where('store_id', '=', $request->user()->id)
->where('payment_type', '=', 'directo')
->where('created_at', '<=', date('Y-m-d H:i:s'))
->where('created_at', '>=', $request->open_period)
// ->whereDate('created_at', '<=', date('Y-m-d'))
// ->whereDate('created_at', '>=', date_format($periodDate, "Y-m-d"))
->sum('payments.amount_credited');
$paymentsAmountRefundCredited = Payment::query()
->where('store_id', '=', $request->user()->id)
->where('payment_type', '=', 'reembolso')
// ->whereDate('created_at', '<=', date('Y-m-d'))
// ->whereDate('created_at', '>=', date_format($periodDate, "Y-m-d"))
->where('created_at', '<=', date('Y-m-d H:i:s'))
->where('created_at', '>=', $request->open_period)
->sum('payments.amount_credited');
$amountTotal = Payment::query()
->where('store_id', '=', $request->user()->id)
->where('payment_type', '=', 'directo')
// ->whereDate('created_at', '<=', date('Y-m-d'))
// ->whereDate('created_at', '>=', date_format($periodDate, "Y-m-d"))
->where('created_at', '<=', date('Y-m-d H:i:s'))
->where('created_at', '>=', $request->open_period)
->sum('payments.amount');
$amountRefund = Payment::query()
->where('store_id', '=', $request->user()->id)
->where('payment_type', '=', 'reembolso')
// ->whereDate('created_at', '<=', date('Y-m-d'))
// ->whereDate('created_at', '>=', date_format($periodDate, "Y-m-d"))
->where('created_at', '<=', date('Y-m-d H:i:s'))
->where('created_at', '>=', $request->open_period)
->sum('payments.amount');
$fee = Payment::query()
->where('store_id', '=', $request->user()->id)
->where('payment_type', '=', 'directo')
// ->whereDate('created_at', '<=', date('Y-m-d'))
// ->whereDate('created_at', '>=', date_format($periodDate, "Y-m-d"))
->where('created_at', '<=', date('Y-m-d H:i:s'))
->where('created_at', '>=', $request->open_period)
->sum('payments.comissao');
$feeRefund = Payment::query()
->where('store_id', '=', $request->user()->id)
->where('payment_type', '=', 'reembolso')
// ->whereDate('created_at', '<=', date('Y-m-d'))
// ->whereDate('created_at', '>=', date_format($periodDate, "Y-m-d"))
->where('created_at', '<=', date('Y-m-d H:i:s'))
->where('created_at', '>=', $request->open_period)
->sum('payments.comissao');
$countRefund = Payment::query()
->where('store_id', '=', $request->user()->id)
->where('payment_type', '=', 'reembolso')
// ->whereDate('created_at', '<=', date('Y-m-d'))
// ->whereDate('created_at', '>=', date_format($periodDate, "Y-m-d"))
->where('created_at', '<=', date('Y-m-d H:i:s'))
->where('created_at', '>=', $request->open_period)
->count();
$countDirect = Payment::query()
->where('store_id', '=', $request->user()->id)
->where('payment_type', '=', 'directo')
// ->whereDate('created_at', '<=', date('Y-m-d'))
// ->whereDate('created_at', '>=', date_format($periodDate, "Y-m-d"))
->where('created_at', '<=', date('Y-m-d H:i:s'))
->where('created_at', '>=', $request->open_period)
->count();
$closes = PeriodCloseStore::query()
->where('store_id', '=', $request->user()->id)
->where('code', '=', $request->code)
->where('status', '=', 'fechado')
->first();
if ($closes) {
return response()->json(['message' => 'Este período já foi fechado'], 400);
} else {
$valorAPagar = $paymentsAmountRefundCredited;
$contractoComerciante = MerchantContract::query()->where('store_id', $request->user()->id)->first();
$taxaDesconto = $valorAPagar * ($contractoComerciante->taxa) / 100;
$period = PeriodCloseStore::query()
->where('store_id', '=', $request->user()->id)
->where('code', '=', $request->code)
->where('open_period', '=', $request->open_period)
->where('status', '=', 'aberto')
->first();
$period->update([
'amount' => $amountTotal,
'amount_credited' => $paymentsAmount,
'amount_credited_refund' => $paymentsAmountRefundCredited + $feeRefund,
'amount_total_credited' => $paymentsAmount - $paymentsAmountRefundCredited,
'fee' => $fee,
'fee_refund' => $feeRefund,
'total_payments' => $countDirect,
'total_refunds' => $countRefund,
'amount_refund' => $amountRefund,
'close_period' => now(),
'date_reference' => date('Y-m-d'),
'status' => 'fechado'
]);
$refund = $paymentsAmountRefundCredited + $feeRefund;
$total = $paymentsAmount - $refund;
// DB::table('stores')->where('id', $request->user()->id)->decrement('balance', $paymentsAmount - $paymentsAmountRefundCredited);
// DB::table('stores')->where('id', $request->user()->id)->decrement('balance', $total);
return response()->json(['message' => 'Período fechado com sucesso', 'dataPeriod' => $period], 200);
}
}
public function openPeriod(Request $request)
{
$this->validate($request, [
'period_time' => 'required',
'description' => 'required'
]);
$lastPeriod = PeriodCloseStore::query()
->join('stores', 'stores.id', '=', 'period_close_stores.store_id')
->select('period_close_stores.*')
->where('period_close_stores.store_id', '=', $request->user()->id)
->where('period_close_stores.status', '=', 'aberto')
->get()->last();
if ($lastPeriod) {
return response()->json(['message' => 'Ainda tens um período aberto'], 400);
}
$storeConfig = StoreConfig::query()->where('store_id', '=', $request->user()->id)->first();
if ($storeConfig) {
if ($storeConfig->use_period == 0) {
return response()->json(['message' => 'Está loja não aceita períodos'], 400);
}
} else {
return response()->json(['message' => 'Está loja ainda não foi configurada'], 400);
}
$trasactionGeneration = new TransactionGeneration();
$transaction = $trasactionGeneration->generateTransaction();
$codigo = $trasactionGeneration->generateCode();
PeriodCloseStore::create([
'transaction' => $transaction,
'code' => $trasactionGeneration->generateCode(),
'description' => $request->description,
'designation' => 'Período ' . $codigo,
'nib' => $request->user()->nib,
'open_period' => now(),
'period_time' => $request->period_time,
'date_reference' => date('Y-m-d'),
'store_id' => $request->user()->id,
'admin_user_id' => 1,
]);
return response()->json(['message' => 'Período aberto com sucesso'], 200);
}
}