Skip to content

Commit

Permalink
Merge pull request #423 from jesusantguerrero/release/alpha-3
Browse files Browse the repository at this point in the history
Release/alpha 3
  • Loading branch information
jesusantguerrero authored Jun 25, 2024
2 parents ed21b77 + 78274ef commit 423645f
Show file tree
Hide file tree
Showing 51 changed files with 1,484 additions and 384 deletions.
51 changes: 51 additions & 0 deletions app/Console/Commands/GenerateBillingCycles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use App\Domains\Loans\Actions\UpdateLatePayments;
use Tests\Feature\CreditCard\Helpers\CreditCardBase;
use App\Domains\Transaction\Services\CreditCardReportService;

class GenerateBillingCycles extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'bg:generate-billing-cycles {teamId} {date?}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';

/**
* Execute the console command.
*
* @return int
*/
public function handle(CreditCardReportService $service)
{
$teamId = $this->argument('teamId');
$date = $this->argument('date');

$monthsWithTransactions = $this->getFirstTransaction($teamId);
return $service->generateBillingCycles($teamId, $date ?? $monthsWithTransactions->date);
}

private function getFirstTransaction(int $teamId) {
return DB::table('transaction_lines')
->where([
"team_id" => $teamId
])
->selectRaw("date_format(transaction_lines.date, '%Y-%m') AS date")
->groupBy(DB::raw("date_format(transaction_lines.date, '%Y-%m')"))
->orderBy('date')
->first();
}
}
2 changes: 1 addition & 1 deletion app/Domains/AppCore/Policies/FinanceAccountPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public function create(User $user)
public function delete(User $user, Account $account)
{

return $user->id == $account->user_id;
return $user->current_team_id == $account->team_id;
}
}
21 changes: 19 additions & 2 deletions app/Domains/Automation/Services/LogerAutomationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
use App\Domains\Integration\Actions\BHD;
use App\Domains\Automation\Models\Automation;
use App\Domains\Integration\Actions\BHDAlert;
use App\Domains\Transaction\Services\BHDService;
use App\Domains\Integration\Actions\APAP\APAP;
use App\Domains\Automation\Models\AutomationTask;
use App\Domains\Integration\Actions\MealPlanAutomation;
use App\Domains\Integration\Actions\OccurrenceAutomation;
use App\Domains\Integration\Actions\TransactionCreateEntry;

Expand Down Expand Up @@ -145,7 +146,7 @@ public static function services()
],
'BHD' => [
'name' => 'BHD',
'entity' => BHDService::class,
'entity' => BHD::class,
'logo' => '/images/bhd.png',
'description' => 'BHD bank',
'components' => [
Expand All @@ -159,7 +160,23 @@ public static function services()
],
],
'type' => 'internal',
],
'APAP' => [
'name' => 'BHD',
'entity' => APAP::class,
'logo' => '/images/bhd.png',
'description' => 'BHD bank',
'components' => [
[
'label' => 'Parse Alert',
'name' => 'parseMessage',
'entity' => BHD::class,
'description' => 'Parse an email alert',
'config' => json_encode(BHDAlert::getSchema()),

],
],
'type' => 'internal',
],
'transactions' => [
'name' => 'transactions',
Expand Down
11 changes: 11 additions & 0 deletions app/Domains/Budget/Http/Controllers/BudgetCategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ protected function index(Request $request) {
]);
}

protected function budgetAlerts() {
$queryParams = request()->query();
$teamId = auth()->user()->current_team_id;
$settings = Setting::getByTeam($teamId);
$timeZone = $settings['team_timezone'] ?? config('app.timezone');
$filters = isset($queryParams['filter']) ? $queryParams['filter'] : [];
[$startDate] = $this->getFilterDates($filters, $timeZone);

return BudgetMonth::getMonthOverspendingCategories($teamId, $startDate);
}

public function show(int $categoryId)
{
$queryParams = request()->query();
Expand Down
24 changes: 24 additions & 0 deletions app/Domains/Budget/Models/BudgetMonth.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,30 @@ public static function getMonthAssignmentByGroup($teamId, $date, $field = 'budge
return $balance;
}

public static function getMonthOverspendingCategories($teamId, $date, $field = 'budgeted')
{
$yearMonth = (new DateTime($date))->format('Y-m').'-01';

$balance = self::selectRaw("
sum(budgeted) as total, categories.name
")
->where('budget_months.team_id', $teamId)
->where('month', $yearMonth)
->join('categories', fn ($q) => $q->on('categories.id', 'category_id')
->whereNot('categories.name', BudgetReservedNames::READY_TO_ASSIGN->value)
)
->join(DB::raw('categories g'), fn ($q) => $q->on('g.id', 'categories.parent_id'))
->leftJoin('budget_targets', 'budget_targets.category_id', 'budget_months.category_id')
->orderBy('g.index')
->groupBy('g.name')
->where("budget_months.available", '<', 0)
->get();

$balance = $balance->mapWithKeys(fn ($item) => [$item['name'] => $item['total']])->toArray();

return $balance;
}

public static function createBudget($data, $startingBalance = true)
{
$month = self::updateOrCreate([
Expand Down
1 change: 0 additions & 1 deletion app/Domains/Budget/Services/BudgetRolloverService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Insane\Journal\Models\Core\Account;
use Insane\Journal\Models\Core\Category;
use App\Domains\Budget\Models\BudgetMonth;
use App\Domains\Budget\Data\BudgetAssignData;
use App\Domains\Budget\Data\BudgetReservedNames;
use Insane\Journal\Models\Core\AccountDetailType;

Expand Down
1 change: 1 addition & 0 deletions app/Domains/Budget/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

Route::middleware(['auth:sanctum', 'atmosphere.teamed', 'verified'])->group(function () {
Route::resource('/budgets', BudgetCategoryController::class);
Route::get('/budget-alerts', [BudgetCategoryController::class, 'budgetAlerts'])->name('budget-alerts');
Route::controller(BudgetTargetController::class)->group(function () {
Route::post('/budgets/{category}/targets/', 'store')->name('budget.target.store');
Route::put('/budgets/{category}/targets/{budgetTarget}', 'update')->name('budget.target.update');
Expand Down
133 changes: 133 additions & 0 deletions app/Domains/Integration/Actions/APAP/APAP.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

namespace App\Domains\Integration\Actions\APAP;

use Exception;
use Illuminate\Support\Facades\Log;
use Symfony\Component\DomCrawler\Crawler;
use App\Domains\Automation\Models\Automation;
use App\Domains\Integration\Actions\GmailReceived;
use App\Domains\Automation\Models\AutomationTaskAction;
use App\Domains\Automation\Concerns\AutomationActionContract;

class APAP implements AutomationActionContract
{
const EMAIL_ALERT = '[email protected]';
const EMAIL_NOTIFICATION = '[email protected]';
const EMAIL_NOTIFICATION_SUBJECT = "APAP, Notificaciones";
const EMAIL_BCRD_SUBJECT = "APAP, Pago Al Instante BCRD";

public static function handle(
Automation $automation,
mixed $payload,
AutomationTaskAction $task,
AutomationTaskAction $previousTask,
AutomationTaskAction $trigger
) {
$type = self::getMessageType($payload);
try {
$transaction = match ($type) {
'alert' => self::parseAlert($automation, $payload, $task, $previousTask, $trigger),
'notification' => self::parseNotification($automation, $payload, $task, $previousTask, $trigger),
};

return $transaction?->toArray();
} catch (Exception $e) {
dd('hola', $e);
}

}

public function getName(): string
{
return 'APAPMessage';
}

public function label(): string
{
return 'APAP Message';
}

public function getDescription(): string
{
return 'Parse an email alert or notification from Asociacion Popular de Ahorros y Prestamos';
}

/**
* Validate and create a new team for the given user.
*
* @param Google_Calendar_Events $calendarEvents
* @return void
*/
public static function parseAlert(
Automation $automation,
mixed $payload,
AutomationTaskAction $task,
AutomationTaskAction $previousTask,
AutomationTaskAction $trigger
) {

return (new APAPAlert())->handle($automation, $payload);
}

/**
* Validate and create a new team for the given user.
*
* @param Google_Calendar_Events $calendarEvents
* @return void
*/
public static function parseNotification(
Automation $automation,
mixed $payload,
AutomationTaskAction $task,
AutomationTaskAction $previousTask,
AutomationTaskAction $trigger
) {

return (new APAPAlert())->handle($automation, $payload);
}

public static function getMessageType($mail)
{
try {
$body = new Crawler($mail['message']);
$tdValues = $body->filter('[class*=table_trans_body] td')->each(function (Crawler $node) {
return $node->text();
});

return empty($tdValues) ? 'notification' : 'alert';
} catch (Exception) {
return 'notification';
}
}

public static function getConditions() {
return [[
'conditionType' => GmailReceived::CONDITION_FROM,
'value' => self::EMAIL_ALERT,
], [
'conditionType' => GmailReceived::CONDITION_SUBJECT,
'value' => self::EMAIL_NOTIFICATION_SUBJECT,
]
];
}

public static function parseCurrency($bhdCurrencyCode)
{
try {
$BHD_CURRENCY_CODES = [
'USD dólar estadounidense' => 'USD',
'RD' => 'DOP',
];

return $BHD_CURRENCY_CODES[$bhdCurrencyCode];
} catch (Exception $e) {
Log::error($e->getMessage(), [$bhdCurrencyCode]);
}
}

public static function parseTypes($type)
{
return 1;
}
}
46 changes: 46 additions & 0 deletions app/Domains/Integration/Actions/APAP/APAPAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace App\Domains\Integration\Actions\APAP;

trait APAPAction
{
public static function getSchema(): mixed
{
return [
'description' => [
'type' => 'string',
'required' => true,
],
'date' => [
'type' => 'date',
'label' => 'Date',

],
'currencyCode' => [
'type' => 'string',
'label' => 'currencyCode',
'required' => true,
],
'amount' => [
'type' => 'money',
'label' => 'Amount',
'required' => true,
],
'payee' => [
'type' => 'string',
'label' => 'Payee',
'required' => true,
],
'categoryGroup' => [
'type' => 'string',
'label' => 'categoryGroup',
'required' => true,
],
'category' => [
'type' => 'string',
'label' => 'Category',
'required' => true,
],
];
}
}
Loading

0 comments on commit 423645f

Please sign in to comment.