Files
finanzen/app/Models/mScheduled.php
Markus 995e2ec111 redesign for mulituser
change validation concept (client and server)
using entities
2026-10-06 08:55:35 +02:00

110 lines
4.4 KiB
PHP

<?php namespace App\Models;
use CodeIgniter\Model;
class mScheduled extends Model {
private $defaultTableName='_budget_scheduled';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = 'array';
protected $useSoftDeletes = true;
protected $deletedField ="deleted_at";
protected $allowedFields = ['creat_date','next_date','data'];
protected $useTimestamps = false;
protected $skipValidation = false;
protected function initialize() {
$this->table = auth()->user()->preFixDB.$this->defaultTableName;
$this->checkOrCreateTable();
}
public function calcNextDate(string $date, string $num, string $type)
{
$start = strtotime( $date );
$period = $num .' '. $type;
$future = strtotime( '+' . $period, $start );
$check = strtotime( '-' . $num .' '. $type, $future );
if ( $check > $start )
$future = strtotime( 'last day of previous month', $future );
return date( "Y-m-d", $future );
}
public function getEntry(int $id){
$data = $this->asObject()->find($id);
$result = (array)json_decode($data->data);
$result['id'] = $id;
$result['datum'] = $data->next_date; //date( "d.m.Y", strtotime($data->next_date));
return $result;
}
public function getEntries(){
$quelle = model('App\Models\mAccounts');
$result = $this->asObject()->findAll();
log_message('error', 'Get Entries Schedules: {msg}', ['msg'=>(string)$this->db->getLastQuery()]);
foreach ($result as $value) {
$details = json_decode($value->data);
foreach ($details->input as $key => $line){
if ((floatval($line) == 0.0) && (floatval($details->output[$key]) == 0.0))
continue;
// $account_id = (!isset($details->account_id))?$details->category:$details->account_id;
$value->details[] = ['comment'=>$details->comment[$key], 'description'=>$quelle->getDropDownEntry($details->account_id[$key]), 'subamount'=>(floatval($details->input[$key])-floatval($details->output[$key]))];
}
//$value->details = null;
$value->datum = date( "d.m.Y", strtotime( $value->next_date ));
$source_id = (!isset($details->source_id))?$details->source:$details->source_id;
$value->source = $quelle->getDropDownEntry($source_id);
$value->receiver = $details->receiver;
// if (!isset($details->amount)){
// $value->amount = floatval($details->total_in)-floatval($details->total_out);
// $value->amount = number_format( floatval($value->amount) , 2, ',', '.' );
// }
// else
$value->amount = $details->amount; //number_format( floatval($details->amount) , 2, ',', '.' );
}
return $result;
}
public function saveIt(array $data){
$accounts = model('App\Models\mAccounts');
$data['validate'] = true;
$data['scheduled'] = true;
unset($data['openamount']);
unset($data['category_parent']);
unset($data['total_in']);
unset($data['total_out']);
// $data['datum'] = date( "Y-m-d", strtotime($data['datum'] ) );
$this->set('next_date', $data['datum']);
$this->set('data',json_encode($data));
log_message("debug","save Scheduled".json_encode($data));
if ( 0 == $data['id']) $this->insert();
else $this->update($data['id']);
}
public function checkOrCreateTable(){
log_message('debug',"Check or Create Scheduled Table");
if ($this->db->tableExists($this->table)) return;
log_message('debug','Create Scheduled Table '.$this->db->lastQuery);
log_message('debug',"Create Scheduled Table ".$this->table);
$forge = \Config\Database::forge();
$fields = array(
'id' => array( 'type' => 'serial'),
'creat_date' => array( 'type' => 'date','default'=>new \CodeIgniter\Database\RawSql('CURRENT_TIMESTAMP')),
'next_date' => array( 'type' => 'date'),
'data' => array( 'type' => 'text'),
'deleted_at'=> array( 'type' => 'timestamp without time zone','null'=>true),
);
$forge->addField( $fields );
$forge->addPrimaryKey('id');
$forge->createTable($this->table,true);
log_message('debug','Create Scheduled Table '.$this->db->lastQuery);
}
}