<?php
namespace App\Http\Controllers;
use App\Classes\GeneralGenerator;
use App\Classes\GeneralNotification;
use App\Models\AccountCardType;
use App\Models\CardType;
use App\Models\Company;
use App\Models\CustomerAccount;
use App\Models\CustomerCard;
use App\Models\Payment;
use App\Models\PaymentToken;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Storage;
use Ramsey\Uuid\Uuid;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
use Illuminate\Support\Facades\DB;
class CardController extends Controller
{
protected $request;
public function __construct(Request $request)
{
$this->request = $request;
}
public function generateCards(Request $request)
{
$this->validate($request, [
'expire_at' => 'required',
'qtd' => 'required',
'card_type_id' => 'required',
// 'card_type_id' => 'required',
'can_recharge' => 'required',
'can_receive_notification' => 'required'
]);
DB::transaction(function () {
$generate = new GeneralGenerator();
$cardType = DB::table('card_types')->where('id', '=', $this->request->card_type_id)->value('name');
$cardTypes = DB::table('card_types')->where('id', '=', $this->request->card_type_id)->first();
// $accountG = DB::table('account_types')->where('id', '=', $this->request->account_type_id)->first();
if ($cardType == 'Empresa' || $cardType == 'empresa') {
// $empresa = Company::create([
// 'name' => 'Amparo Empresa Card',
// 'nuit' => '1245245289',
// 'email' => 'miguelmacamo99@gmail.com',
// 'address' => 'Maputo, Rua cardoso 41',
// 'contact_phone_fixo' => '21841520',
// 'contact_phone_mobile' => '845030902',
// 'manager_name' => 'Miguel',
// 'password' => Hash::make('12345678'),
// 'pin' => Hash::make('1234'),
// 'status' => 'activo',
// 'user_id' => $this->request->user()->id,
// 'created_at' => now(),
// 'updated_at' => now()
// ]);
$uuid = Uuid::uuid4();
$conta = CustomerAccount::create([
'name' => 'Paytek',
// 'email' => 'info2@paytek-africa.com',
// 'mobile_phone1' => '845030900',
// 'mobile_phone2' => '821414550',
'account_number' => $generate->generateAccountNumberAndCardNumber(),
'reference' => $generate->generateReferenceAccount(),
'balance' => '1000',
'balance_cards' => '100',
'qrcode' => 'link',
'document_type' => 'Bi',
'document_number' => '121545423541565Q',
'address' => 'Maputo, Rua cardoso 41',
// 'company_id' => $empresa->id,
'user_id' => $this->request->user()->id,
'public_id' => $uuid,
// 'account_type_id' => $accountG->id,
'created_at' => now(),
'updated_at' => now()
]);
// Adicionar Tipo de Cartão
$validar = AccountCardType::query()
->where('customer_account_id', $conta->id)
->where('card_type_id', $this->request->card_type_id)
->count();
if (!($validar > 0)) {
AccountCardType::create([
'customer_account_id' => $conta->id,
'card_type_id' => $this->request->card_type_id
]);
}
for ($i = 0; $i < $this->request->qtd; $i++) {
$uuid = Uuid::uuid4();
QrCode::size(250)->format('svg')->mergeString(public_path('/images/geral/logo.png'))->generate($uuid, public_path('images/cards/' . $uuid . '.svg'));
DB::table('customer_cards')->insert([
'username' => 'Amparo Cartões',
'phone_number' => '845030902',
'card_number' => $generate->generateAccountNumberAndCardNumber(),
'reference' => $generate->generateReference(),
'expire_at' => $this->request->expire_at,
'can_pay' => $this->request->can_pay,
'can_recharge' => $this->request->can_recharge,
'can_receive_notification' => $this->request->can_receive_notification,
'type_user' => 'indefinido',
'public_id' => $uuid,
'card_type_id' => $cardTypes->id,
'customer_account_id' => $conta->id,
'user_id' => $this->request->user()->id,
'qrcode' => url('/') . '/images/cards/' . $uuid . '.svg',
'created_at' => now(),
'updated_at' => now()
]);
}
} else {
for ($i = 0; $i < $this->request->qtd; $i++) {
$uuid = Uuid::uuid4();
QrCode::size(250)->format('svg')->mergeString(public_path('/images/geral/logo.png'))->generate($uuid, public_path('images/cards/' . $uuid . '.svg'));
DB::table('customer_cards')->insert([
'username' => 'Amparo Cartões',
'phone_number' => '845030902',
'card_number' => $generate->generateAccountNumberAndCardNumber(),
'reference' => $generate->generateReference(),
'expire_at' => $this->request->expire_at,
'can_pay' => $this->request->can_pay,
'can_recharge' => $this->request->can_recharge,
'can_receive_notification' => $this->request->can_receive_notification,
'type_user' => 'indefinido',
'public_id' => $uuid,
'card_type_id' => $cardTypes->id,
'user_id' => $this->request->user()->id,
'qrcode' => url('/') . '/images/cards/' . $uuid . '.svg',
'created_at' => now(),
'updated_at' => now()
]);
}
}
});
return response()->json([
'message' => 'Cartões Gerados Com Sucesso!'
], 200);
}
public function getCard($reference)
{
// $data = CustomerAccount::query()
//// ->leftJoin('companies', 'companies.id', '=', 'customer_accounts.company_id')
// ->leftJoin('customer_cards', 'customer_cards.customer_account_id', '=', 'customer_accounts.id')
// ->select('customer_cards.*', 'customer_accounts.name as manager_name', 'customer_accounts.nuit as company_nuit', 'customer_accounts.name as company_name')
//// ->select('customer_cards.*', 'customer_accounts.name as manager_name')
// ->where('customer_cards.public_id', $reference)
// ->first();
$data = CustomerCard::query()
->leftJoin('customer_accounts', 'customer_cards.customer_account_id', '=', 'customer_accounts.id')
->select('customer_cards.*', 'customer_accounts.name as manager_name', 'customer_accounts.nuit as company_nuit', 'customer_accounts.name as company_name')
// ->select('customer_cards.*', 'customer_accounts.name as manager_name')
->where('customer_cards.public_id', $reference)
->first();
if ($data) {
$g = new GeneralGenerator();
$res = PaymentToken::create([
'phone' => $data->phone_number,
'expire_in' => date('Y-m-d'),
'duration' => 3,
'codigo' => $g->generatePaymentToken(),
'customer_card_id' => $data->id,
]);
$sms = new GeneralNotification();
$sms->tokenSms($res);
return response()->json($data);
} else {
return response()->json(['message' => 'Cartão inválido']);
}
// $sms = new GeneralNotification();
// $sms->tokenSms($res);
}
public function getCardByNumber($number)
{
$payments = Payment::query()
->join('stores', 'stores.id', '=', 'payments.store_id')
->join('customer_cards', 'customer_cards.id', '=', 'payments.customer_card_id')
->join('customer_accounts', 'customer_accounts.id', '=', 'payments.customer_account_id')
->leftJoin('operators', 'operators.id', '=', 'payments.user_id')
->where('customer_cards.card_number', $number)
->orderByDesc('payments.created_at')
->select('stores.store_name',
'customer_cards.card_number',
'customer_cards.username as card_username',
'customer_cards.phone_number as card_username_phone',
'customer_accounts.name as account_name',
'operators.name as operator_name',
'payments.*')
->get()->take(2);
$data = CustomerAccount::query()
// ->leftJoin('companies', 'companies.id', '=', 'customer_accounts.company_id')
->join('customer_cards', 'customer_cards.customer_account_id', '=', 'customer_accounts.id')
->select('customer_cards.*', 'customer_accounts.name as manager_name', 'customer_accounts.name as company_name'
, 'customer_accounts.nuit as company_nuit')
// ->select('customer_cards.*', 'customer_accounts.name as manager_name')
->where('customer_cards.card_number', $number)
->first();
if ($data) {
$card = CustomerCard::query()->where('card_number', $number)->first();
$totalPayments = Payment::query()->where('customer_card_id', $card->id)->count();
$totalAmount = Payment::query()->where('customer_card_id', $card->id)->sum('amount');
$dashboard = array(
'total_payment' => $totalPayments,
'total_amount' => $totalAmount
);
// $object = json_decode($data, true);
$object = json_decode($data, true);
$object['data'] = $payments;
$object['dashboard'] = $dashboard;
$json = json_encode($object);
return response()->json($object, 200);
} else {
return response()->json(['message' => 'Cartão não encontrado'], 400);
}
}
public function getCardTypes()
{
$data = CardType::query()->orderBy('name', 'asc')->get();
return response()->json(['data' => $data]);
}
public function getAllCards()
{
// $data = CustomerAccount::query()
//// ->leftJoin('companies', 'companies.id', '=', 'customer_accounts.company_id')
// ->join('customer_cards', 'customer_cards.customer_account_id', '=', 'customer_accounts.id')
//// ->select('customer_cards.*', 'customer_accounts.name as manager_name', 'companies.name as company_name')
// ->select('customer_cards.*', 'customer_accounts.name as manager_name')
// ->orderByDesc('customer_cards.created_at')
// ->get();
$data = CustomerCard::query()
->leftJoin('card_types', 'customer_cards.card_type_id', '=', 'card_types.id')
->leftJoin('customer_accounts', 'customer_cards.customer_account_id', '=', 'customer_accounts.id')
->select('customer_cards.*', 'customer_accounts.name as manager_name', 'card_types.name as card_type_name')
->orderByDesc('customer_cards.created_at')
->get();
return response()->json(['data' => $data], 200);
}
public function getCardsByAccount($id)
{
$data = CustomerCard::query()
->where('customer_account_id', $id)
->orderByDesc('created_at')
->get();
$cActivos = CustomerCard::query()->where('customer_account_id', $id)->where('status', '=', 'activo')->count();
$cNeverUsed = CustomerCard::query()->where('customer_account_id', $id)->where('session_status', '=', 0)->count();
$canRechargeCards = CustomerCard::query()->where('customer_account_id', $id)->where('can_recharge', '=', 0)->count();
$canReceiveNotificationCards = CustomerCard::query()->where('customer_account_id', $id)->where('can_receive_notification', '=', 1)->count();
$canPayCards = CustomerCard::query()->where('customer_account_id', $id)->where('can_pay', '=', 1)->count();
$totalCards = CustomerCard::query()->where('customer_account_id', $id)->count();
return response()->json([
'totalCards' => $totalCards,
'activeCards' => $cActivos,
'neverUsedCards' => $cNeverUsed,
'canRechargeCards' => $canRechargeCards,
'canPayCards' => $canPayCards,
'canReceiveNotificationCards' => $canReceiveNotificationCards,
'data' => $data,], 200);
}
public function getNeverUsedCards()
{
$data = CustomerCard::query()
// ->where('customer_account_id', $id)
->where('session_status', '=', 0)
->orderByDesc('created_at')
->get();
return response()->json(['data' => $data], 200);
}
public function updateCard(Request $request)
{
$this->validate($request, [
'public_id' => 'required'
]);
$data = CustomerCard::query()->where('public_id', $request->public_id)->first();
if ($data) {
$data->update([
'username' => $request->username,
'phone_number' => $request->phone_number,
'card_balance_limit' => $request->card_balance_limit,
'expire_at' => $request->expire_at,
// 'status' => 'su',
'can_pay' => $request->can_pay,
'can_receive_notification' => $request->can_receive_notification,
'can_recharge' => $request->can_recharge,
'card_type_id' => $request->card_type_id,
'customer_account_id' => $request->customer_account_id
]);
return response()->json(['message' => 'Parámetros do Cartão ' . $data->card_number . ' alterados com Sucesso'], 201);
} else {
return response()->json(['message' => 'Cartão inválido'], 402);
}
}
}