Skip to content

Commit

Permalink
fix in archiviazione bilanci
Browse files Browse the repository at this point in the history
  • Loading branch information
madbob committed Aug 14, 2018
1 parent 6359fa5 commit 333ff69
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions code/app/CreditableTrait.php
Expand Up @@ -3,6 +3,7 @@
namespace App;

use DB;
use Log;

trait CreditableTrait
{
Expand All @@ -17,9 +18,15 @@ public function balances()

private function fixFirstBalance()
{
Log::debug('Fix bilancio per ' . $this->id . ' ' . get_class($this));

$proxy = $this->getBalanceProxy();
if (is_null($proxy))
$proxy = $this;

$balance = new Balance();
$balance->target_id = $this->id;
$balance->target_type = get_class($this);
$balance->target_id = $proxy->id;
$balance->target_type = get_class($proxy);
$balance->bank = 0;
$balance->cash = 0;
$balance->gas = 0;
Expand Down Expand Up @@ -117,6 +124,8 @@ public static function resetAllCurrentBalances()

public static function duplicateAllCurrentBalances($latest_date)
{
$current_status = [];

$classes = DB::table('balances')->select('target_type')->distinct()->get();
foreach($classes as $c) {
$class = $c->target_type;
Expand All @@ -127,12 +136,24 @@ public static function duplicateAllCurrentBalances($latest_date)
$objects = $class::all();

foreach($objects as $obj) {
$latest = $obj->current_balance;
$new = $latest->replicate();
$latest->date = $latest_date;
$latest->current = false;
$latest->save();
$new->save();
$proxy = $obj->getBalanceProxy();
if ($proxy != null)
$obj = $proxy;

if (!isset($current_status[$class]))
$current_status[$class] = [];

if (!isset($current_status[$class][$obj->id])) {
$latest = $obj->current_balance;
$new = $latest->replicate();

$latest->date = $latest_date;
$latest->current = false;
$latest->save();
$new->save();

$current_status[$class][$obj->id] = true;
}
}
}
}
Expand Down

0 comments on commit 333ff69

Please sign in to comment.