diff --git a/CHANGELOG.md b/CHANGELOG.md index d658203e..269da447 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ !! Please do a complete reinstall, with a new database - Finances! The finance portions have been implemented, you can [read about them here](http://docs.phpvms.net/concepts/finances) +- Awards! Added the award plugin system. [see docs](http://docs.phpvms.net/customizing/awards) - Changed theme system to using [laravel-theme](https://github.com/igaster/laravel-theme), there are changes to making theming much simpler with much more flexibility. - Fixed several security vulnerabilities (thanks magicflyer!) - Fuel units changed to lbs/kgs [#193](https://github.com/nabeelio/phpvms/issues/193) diff --git a/Procfile b/Procfile index e37f7486..251e55a7 100644 --- a/Procfile +++ b/Procfile @@ -1,5 +1,5 @@ dnsmasq: /usr/local/sbin/dnsmasq --keep-in-foreground php-fpm: /usr/local/sbin/php-fpm --nodaemonize nginx: /usr/local/bin/nginx -g 'daemon off;' -#mysql: /usr/local/bin/mysqld +mysql: /usr/local/bin/mysqld #mailhog: /usr/local/bin/mailhog diff --git a/app/Http/Controllers/Api/PirepController.php b/app/Http/Controllers/Api/PirepController.php index c6ea4a0e..fd3ec117 100644 --- a/app/Http/Controllers/Api/PirepController.php +++ b/app/Http/Controllers/Api/PirepController.php @@ -354,7 +354,7 @@ class PirepController extends RestController $pirep = $this->pirepRepo->find($id); $this->checkCancelled($pirep); - Log::info( + Log::debug( 'Posting ACARS update (user: '.Auth::user()->pilot_id.', pirep id :'.$id.'): ', $request->post() ); @@ -394,7 +394,7 @@ class PirepController extends RestController $pirep = $this->pirepRepo->find($id); $this->checkCancelled($pirep); - Log::info('Posting ACARS log, PIREP: '.$id, $request->post()); + Log::debug('Posting ACARS log, PIREP: '.$id, $request->post()); $count = 0; $logs = $request->post('logs'); @@ -426,7 +426,7 @@ class PirepController extends RestController $pirep = $this->pirepRepo->find($id); $this->checkCancelled($pirep); - Log::info('Posting ACARS event, PIREP: ' . $id, $request->post()); + Log::debug('Posting ACARS event, PIREP: ' . $id, $request->post()); $count = 0; $logs = $request->post('events'); @@ -468,7 +468,7 @@ class PirepController extends RestController $pirep = $this->pirepRepo->find($id); $this->checkCancelled($pirep); - Log::info('Posting comment, PIREP: '.$id, $request->post()); + Log::debug('Posting comment, PIREP: '.$id, $request->post()); # Add it $comment = new PirepComment($request->post()); @@ -483,6 +483,8 @@ class PirepController extends RestController * @param $id * @param Request $request * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection + * @throws \UnexpectedValueException + * @throws \InvalidArgumentException */ public function finances_get($id, Request $request) { diff --git a/app/Listeners/Cron/Nightly/RecalculateBalances.php b/app/Listeners/Cron/Nightly/RecalculateBalances.php index b643c9fa..f33d75c0 100644 --- a/app/Listeners/Cron/Nightly/RecalculateBalances.php +++ b/app/Listeners/Cron/Nightly/RecalculateBalances.php @@ -38,18 +38,14 @@ class RecalculateBalances $journals = Journal::all(); foreach ($journals as $journal) { + $old_balance = $journal->balance; - $where = ['journal_id' => $journal->id]; - $credits = Money::create(JournalTransaction::where($where)->sum('credit') ?: 0); - $debits = Money::create(JournalTransaction::where($where)->sum('debit') ?: 0); - $balance = $credits->subtract($debits); + $this->journalRepo->recalculateBalance($journal); + $journal->refresh(); Log::info('Adjusting balance on ' . $journal->morphed_type . ':' . $journal->morphed_id - . ' from ' . $journal->balance . ' to ' . $balance); - - $journal->balance = $balance->getAmount(); - $journal->save(); + . ' from ' . $old_balance . ' to ' . $journal->balance); } Log::info('Done calculating balances'); diff --git a/app/Models/Journal.php b/app/Models/Journal.php index 682c9ad8..5e5d2f51 100644 --- a/app/Models/Journal.php +++ b/app/Models/Journal.php @@ -18,6 +18,8 @@ use Carbon\Carbon; * @property Carbon $post_date * @property Carbon $created_at * @property \App\Models\Enums\JournalType type + * @property mixed morphed_type + * @property mixed morphed_id */ class Journal extends BaseModel { diff --git a/app/Models/JournalTransaction.php b/app/Models/JournalTransaction.php index fd7d7c3b..b71ecbc5 100644 --- a/app/Models/JournalTransaction.php +++ b/app/Models/JournalTransaction.php @@ -41,7 +41,7 @@ class JournalTransaction extends BaseModel 'tags' => 'array', ]; - protected $dateFormat = 'Y-m-d'; + //protected $dateFormat = 'Y-m-d'; protected $dates = [ 'created_at', 'updated_at', diff --git a/app/Repositories/JournalRepository.php b/app/Repositories/JournalRepository.php index d1573114..1e6e74e7 100644 --- a/app/Repositories/JournalRepository.php +++ b/app/Repositories/JournalRepository.php @@ -41,6 +41,29 @@ class JournalRepository extends BaseRepository implements CacheableInterface return $date->setTimezone('UTC')->toDateString(); } + /** + * Recalculate the balance of the given journal + * @param Journal $journal + * @return Journal + * @throws \UnexpectedValueException + * @throws \InvalidArgumentException + */ + public function recalculateBalance(Journal $journal) + { + $where = [ + 'journal_id' => $journal->id + ]; + + $credits = Money::create($this->findWhere($where)->sum('credit') ?: 0); + $debits = Money::create($this->findWhere($where)->sum('debit') ?: 0); + $balance = $credits->subtract($debits); + + $journal->balance = $balance->getAmount(); + $journal->save(); + + return $journal; + } + /** * Post a new transaction to a journal, and also adjust the balance * on the transaction itself. A cron will run to reconcile the journal @@ -67,7 +90,6 @@ class JournalRepository extends BaseRepository implements CacheableInterface $transaction_group = null, $tags = null ) { - # tags can be passed in a list if ($tags && \is_array($tags)) { $tags = implode(',', $tags); diff --git a/app/Services/Finance/PirepFinanceService.php b/app/Services/Finance/PirepFinanceService.php index d5ad0c20..78cdd530 100644 --- a/app/Services/Finance/PirepFinanceService.php +++ b/app/Services/Finance/PirepFinanceService.php @@ -80,6 +80,10 @@ class PirepFinanceService extends BaseService $pirep->airline->journal->refresh(); $pirep->user->journal->refresh(); + // Recalculate balances... + $this->journalRepo->recalculateBalance($pirep->airline->journal); + $this->journalRepo->recalculateBalance($pirep->user->journal); + return $pirep; } diff --git a/app/Services/Finance/RecurringFinanceService.php b/app/Services/Finance/RecurringFinanceService.php index 18ab7135..a91cd0db 100644 --- a/app/Services/Finance/RecurringFinanceService.php +++ b/app/Services/Finance/RecurringFinanceService.php @@ -112,10 +112,12 @@ class RecurringFinanceService extends BaseService 'journal_id' => $journal->id, 'ref_class' => Expense::class, 'ref_class_id' => $expense->id, - 'post_date' => \Carbon::now('UTC')->format('Y-m-d'), ]; - $found = JournalTransaction::where($w)->count(['id']); + $found = JournalTransaction::where($w) + ->whereDate('post_date', '=', \Carbon::now('UTC')->toDateString()) + ->count(['id']); + if($found > 0) { Log::info('Expense "'.$expense->name.'" already charged for today, skipping'); continue; diff --git a/tests/UtilsTest.php b/tests/UtilsTest.php index 26c5efab..f90931bb 100644 --- a/tests/UtilsTest.php +++ b/tests/UtilsTest.php @@ -7,6 +7,12 @@ class UtilsTest extends TestCase public function setUp() { } + public function testDates() + { + $carbon = new \Carbon\Carbon('2018-03-18 00:20:43'); + //echo $carbon; + } + public function testSecondsToTimeParts() { $t = Utils::secondsToTimeParts(3600);