Demo project
This commit is contained in:
1 parent
5e4f3540e8
commit
a68267d0f0
146 files changed
+41587
No files matched your search
@@ -0,0 +1,165 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Category extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
static $CACHED_VALUES = [];
|
||||
static $CACHED_ACTUAL = [];
|
||||
|
||||
public function posts() {
|
||||
return $this->hasMany(\App\Models\Post::class);
|
||||
}
|
||||
|
||||
public function move($moveUp = true) {
|
||||
$replaces = self::where("user_id", $this->user_id);
|
||||
|
||||
if($moveUp) {
|
||||
$replaces = $replaces->where("priority", "<", $this->priority)->orderBy("priority", "desc")->first();
|
||||
} else {
|
||||
$replaces = $replaces->where("priority", ">", $this->priority)->orderBy("priority", "asc")->first();
|
||||
}
|
||||
|
||||
if($replaces) {
|
||||
$tmp = $this->priority;
|
||||
$this->priority = $replaces->priority;
|
||||
$replaces->priority = $tmp;
|
||||
|
||||
$this->save();
|
||||
$replaces->save();
|
||||
}
|
||||
}
|
||||
|
||||
public function delete() {
|
||||
foreach($this->posts as $post) {
|
||||
$post->delete();
|
||||
}
|
||||
parent::delete();
|
||||
}
|
||||
|
||||
public static function ValueFromCache($id, $year, $month) {
|
||||
if(isset(Category::$CACHED_VALUES[$id])) {
|
||||
if(isset(Category::$CACHED_VALUES[$id][$year])) {
|
||||
if(isset(Category::$CACHED_VALUES[$id][$year][$month])) {
|
||||
return Category::$CACHED_VALUES[$id][$year][$month];
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function SetValueInCache($id, $year, $month, $value) {
|
||||
if(!isset(Category::$CACHED_VALUES[$id])) {
|
||||
Category::$CACHED_VALUES[$id] = [];
|
||||
}
|
||||
if(!isset(Category::$CACHED_VALUES[$id][$year])) {
|
||||
Category::$CACHED_VALUES[$id][$year] = [];
|
||||
}
|
||||
Category::$CACHED_VALUES[$id][$year][$month] = $value;
|
||||
}
|
||||
|
||||
public function monthValue($year, $month) {
|
||||
$value = Category::ValueFromCache($this->id, $year, $month);
|
||||
if($value !== null) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
$value = 0;
|
||||
foreach($this->posts->where("year", $year) as $post) {
|
||||
$value += $post->monthValue($month);
|
||||
}
|
||||
|
||||
Category::SetValueInCache($this->id, $year, $month, $value);
|
||||
return $value;
|
||||
}
|
||||
|
||||
public function totalValue($year) {
|
||||
$value = 0;
|
||||
for($i=1;$i<=12;$i++) {
|
||||
$value += $this->monthValue($year, $i);
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
public function avgValue($year, $count) {
|
||||
if($count == 0) { return 0; }
|
||||
return $this->totalValue($year) / $count;
|
||||
}
|
||||
|
||||
|
||||
public static function ActualFromCache($id, $year, $month) {
|
||||
if(isset(Category::$CACHED_ACTUAL[$id])) {
|
||||
if(isset(Category::$CACHED_ACTUAL[$id][$year])) {
|
||||
if(isset(Category::$CACHED_ACTUAL[$id][$year][$month])) {
|
||||
return Category::$CACHED_ACTUAL[$id][$year][$month];
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function SetActualInCache($id, $year, $month, $value) {
|
||||
if(!isset(Category::$CACHED_ACTUAL[$id])) {
|
||||
Category::$CACHED_ACTUAL[$id] = [];
|
||||
}
|
||||
if(!isset(Category::$CACHED_ACTUAL[$id][$year])) {
|
||||
Category::$CACHED_ACTUAL[$id][$year] = [];
|
||||
}
|
||||
Category::$CACHED_ACTUAL[$id][$year][$month] = $value;
|
||||
}
|
||||
|
||||
public function monthActualValue($year, $month, $differenceMode = false) {
|
||||
$value = Category::ActualFromCache($this->id, $year, $month);
|
||||
if($value === null) {
|
||||
$value = 0;
|
||||
foreach($this->posts->where("year", $year) as $post) {
|
||||
$value += $post->monthActualValue($month);
|
||||
}
|
||||
|
||||
Category::SetActualInCache($this->id, $year, $month, $value);
|
||||
}
|
||||
|
||||
if($differenceMode) {
|
||||
if(!\Auth::user()->activeActualMonths($year)[$month-1]) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$projected = $this->monthValue($year, $month);
|
||||
|
||||
return $value - $projected;
|
||||
} else {
|
||||
return $value;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
public function totalActualValue($year, $differenceMode = false) {
|
||||
$value = 0;
|
||||
for($i=1;$i<=12;$i++) {
|
||||
$value += $this->monthActualValue($year, $i);
|
||||
}
|
||||
|
||||
if($differenceMode) {
|
||||
$projected = 0;
|
||||
for($m = 1; $m<=12; $m++) {
|
||||
if(\Auth::user()->activeActualMonths($year)[$m-1]) {
|
||||
$projected += $this->monthValue($year, $m);
|
||||
}
|
||||
}
|
||||
|
||||
return $value - $projected;
|
||||
} else {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
public function avgActualValue($year, $count, $differenceMode = false) {
|
||||
if($count == 0) { return 0; }
|
||||
return $this->totalActualValue($year, $differenceMode) / $count;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Post extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
private $monthValueCache = null;
|
||||
private $monthActualValueCache = null;
|
||||
|
||||
public function category() {
|
||||
return $this->belongsTo(\App\Models\Category::class);
|
||||
}
|
||||
|
||||
public function delete() {
|
||||
foreach(\App\Models\PostValue::where('post_id', $this->id)->get() as $value) {
|
||||
$value->delete();
|
||||
}
|
||||
foreach(\App\Models\TransactionValue::where('post_id', $this->id)->get() as $value) {
|
||||
$value->delete();
|
||||
}
|
||||
parent::delete();
|
||||
}
|
||||
|
||||
public function setMonthValue($month, $value) {
|
||||
$postValue = \App\Models\PostValue::where("post_id", $this->id)->where("month", $month)->first();
|
||||
if($value != 0.00) {
|
||||
if($postValue == null) {
|
||||
$postValue = new \App\Models\PostValue();
|
||||
$postValue->post_id = $this->id;
|
||||
$postValue->month = $month;
|
||||
}
|
||||
$postValue->value = $value;
|
||||
$postValue->save();
|
||||
} else if($postValue != null) {
|
||||
$postValue->delete();
|
||||
}
|
||||
}
|
||||
|
||||
private function updateMonthValueCache() {
|
||||
$this->monthValueCache = [0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0 ,0];
|
||||
foreach(\App\Models\PostValue::where("post_id", $this->id)->get() as $value) {
|
||||
$this->monthValueCache[$value->month-1] = $value->value * ($this->is_expense ? -1.00 : 1.00);
|
||||
}
|
||||
}
|
||||
|
||||
public function monthValue($month) {
|
||||
if($this->monthValueCache == null) {
|
||||
$this->updateMonthValueCache();
|
||||
}
|
||||
return $this->monthValueCache[$month-1];
|
||||
}
|
||||
|
||||
public function totalValue() {
|
||||
$value = 0;
|
||||
for($i=1;$i<=12;$i++) {
|
||||
$value += $this->monthValue($i);
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
public function avgValue($count) {
|
||||
if($count == 0) { return 0; }
|
||||
return $this->totalValue() / $count;
|
||||
}
|
||||
|
||||
private function updateMonthActualValueCache() {
|
||||
$this->monthActualValueCache = [0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0 ,0];
|
||||
foreach(\App\Models\TransactionValue::where("post_id", $this->id)->get() as $value) {
|
||||
$this->monthActualValueCache[$value->month-1] += $value->value * ($this->is_expense ? -1.00 : 1.00);
|
||||
}
|
||||
}
|
||||
|
||||
public function monthActualValue($month, $differenceMode = false) {
|
||||
if($this->monthActualValueCache == null) {
|
||||
$this->updateMonthActualValueCache();
|
||||
}
|
||||
|
||||
if($differenceMode) {
|
||||
if(!\Auth::user()->activeActualMonths($this->year)[$month-1]) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$actual = $this->monthActualValueCache[$month-1];
|
||||
$projected = $this->monthValue($month);
|
||||
|
||||
return $actual - $projected;
|
||||
} else {
|
||||
return $this->monthActualValueCache[$month-1];
|
||||
}
|
||||
}
|
||||
|
||||
public function totalActualValue($differenceMode = false) {
|
||||
$value = 0;
|
||||
for($i=1;$i<=12;$i++) {
|
||||
$value += $this->monthActualValue($i);
|
||||
}
|
||||
|
||||
if($differenceMode) {
|
||||
$projected = 0;
|
||||
for($m = 1; $m<=12; $m++) {
|
||||
if(\Auth::user()->activeActualMonths($this->year)[$m-1]) {
|
||||
$projected += $this->monthValue($m);
|
||||
}
|
||||
}
|
||||
|
||||
return $value - $projected;
|
||||
} else {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
public function avgActualValue($count, $differenceMode = false) {
|
||||
if($count == 0) { return 0; }
|
||||
|
||||
return $this->totalActualValue($differenceMode) / $count;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class PostValue extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
public function post() {
|
||||
return $this->belongsTo(\App\Models\Post::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class TransactionValue extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
public function post() {
|
||||
return $this->belongsTo(\App\Models\Post::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
|
||||
class User extends Authenticatable
|
||||
{
|
||||
use HasFactory, Notifiable;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'email',
|
||||
'password',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be hidden for arrays.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $hidden = [
|
||||
'password',
|
||||
'remember_token',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast to native types.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [
|
||||
'email_verified_at' => 'datetime',
|
||||
];
|
||||
|
||||
static $ACTUAL_MONTH_CACHE = null;
|
||||
|
||||
public function hasPosts($year) {
|
||||
return \App\Models\Post::whereHas('category', function ($query) {
|
||||
return $query->where('user_id', \Auth::user()->id);
|
||||
})->where("year", $year)->count() > 0;
|
||||
}
|
||||
|
||||
public function monthValue($categories, $year, $month) {
|
||||
$value = 0;
|
||||
foreach($categories as $category) {
|
||||
$value += $category->monthValue($year, $month);
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
public function totalValue($categories, $year) {
|
||||
$value = 0;
|
||||
foreach($categories as $category) {
|
||||
$value += $category->totalValue($year);
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
public function avgValue($categories, $year, $count) {
|
||||
if($count == 0) { return 0; }
|
||||
return $this->totalValue($categories, $year) / $count;
|
||||
}
|
||||
|
||||
public function activeMonths($year) {
|
||||
$months = [false, false, false, false, false, false, false, false, false, false, false, false];
|
||||
|
||||
foreach(\App\Models\Category::where('user_id', $this->id)->get() as $category) {
|
||||
foreach($category->posts->where("year", $year) as $post) {
|
||||
for($i = 0; $i < 12; $i++) {
|
||||
if($months[$i]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if($post->monthValue($i+1) != 0) {
|
||||
$months[$i] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $months;
|
||||
}
|
||||
|
||||
public function monthActualValue($categories, $year, $month, $differenceMode = false) {
|
||||
$value = 0;
|
||||
foreach($categories as $category) {
|
||||
$value += $category->monthActualValue($year, $month);
|
||||
}
|
||||
|
||||
if($differenceMode) {
|
||||
if(!$this->activeActualMonths($year)[$month-1]) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$projected = $this->monthValue($categories, $year, $month);
|
||||
|
||||
return $value - $projected;
|
||||
} else {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
public function totalActualValue($categories, $year, $differenceMode = false) {
|
||||
$value = 0;
|
||||
foreach($categories as $category) {
|
||||
$value += $category->totalActualValue($year);
|
||||
}
|
||||
|
||||
if($differenceMode) {
|
||||
$projected = 0;
|
||||
for($m = 1; $m<=12; $m++) {
|
||||
if(\Auth::user()->activeActualMonths($this->year)[$m-1]) {
|
||||
$projected += $this->monthValue($categories, $year, $m);
|
||||
}
|
||||
}
|
||||
|
||||
return $value - $projected;
|
||||
} else {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
public function avgActualValue($categories, $year, $count, $differenceMode = false) {
|
||||
if($count == 0) { return 0; }
|
||||
return $this->totalActualValue($categories, $year, $differenceMode) / $count;
|
||||
}
|
||||
|
||||
public function activeActualMonths($year) {
|
||||
if(self::$ACTUAL_MONTH_CACHE === null) {
|
||||
self::$ACTUAL_MONTH_CACHE = [false, false, false, false, false, false, false, false, false, false, false, false];
|
||||
|
||||
foreach(\App\Models\Category::where('user_id', $this->id)->get() as $category) {
|
||||
foreach($category->posts->where("year", $year) as $post) {
|
||||
for($i = 0; $i < 12; $i++) {
|
||||
if(self::$ACTUAL_MONTH_CACHE[$i]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if($post->monthActualValue($i+1) != 0) {
|
||||
self::$ACTUAL_MONTH_CACHE[$i] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return self::$ACTUAL_MONTH_CACHE;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user