Store full datetime in post_date; recalculate finances on every pirep

This commit is contained in:
Nabeel Shahzad 2018-03-17 22:20:08 -05:00
parent 6fa724d7b7
commit 6b002f24a8
10 changed files with 52 additions and 17 deletions

View File

@ -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)

View File

@ -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

View File

@ -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)
{

View File

@ -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');

View File

@ -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
{

View File

@ -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',

View File

@ -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);

View File

@ -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;
}

View File

@ -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;

View File

@ -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);