redesign for mulituser
change validation concept (client and server) using entities
This commit is contained in:
@@ -1,33 +1,58 @@
|
||||
<?php namespace App\Models;
|
||||
|
||||
use App\Models\Cast\CastMyDate;
|
||||
use CodeIgniter\Model;
|
||||
use App\Models\Mbookingdetails;
|
||||
use App\Models\Mcategories;
|
||||
|
||||
class mBills extends Model {
|
||||
protected $table = 'budget_bills';
|
||||
|
||||
private $defaultTableName='_budget_bills';
|
||||
protected $primaryKey = 'id';
|
||||
protected $useAutoIncrement = true;
|
||||
|
||||
protected $returnType = 'object';
|
||||
protected $returnType = \App\Entities\eBooking::class;
|
||||
|
||||
protected $useSoftDeletes = true;
|
||||
protected $deletedField ="deleted_at";
|
||||
|
||||
protected $useSoftDeletes = false;
|
||||
//TODO timestamps aktivieren
|
||||
// protected $createdField = 'created_at';
|
||||
// protected $updatedField = 'updated_at';
|
||||
// protected $deletedField = 'deleted_at';
|
||||
|
||||
protected $allowedFields = ['renummer','datum','receiver','source_id','amount','type','comment','validate','lkz','rechnung'];
|
||||
protected $allowedFields = ['datum','receiver','source_id','amount','validate','deleted_at','rechnung'];
|
||||
|
||||
protected $useTimestamps = false;
|
||||
|
||||
// Bind the type to the handler
|
||||
protected array $castHandlers = [
|
||||
'myDate' => CastMyDate::class,
|
||||
];
|
||||
//protected $afterFind = ['checkTransfer'];
|
||||
protected array $casts = [
|
||||
'id' => 'int',
|
||||
'amount' => 'float',
|
||||
'datum' => 'myDate[dd.MM.yyyy]',
|
||||
'validate' => 'boolean',
|
||||
'source_id' => 'int',
|
||||
];
|
||||
|
||||
protected function initialize() {
|
||||
log_message('debug','mBills initialize');
|
||||
$this->table = auth()->user()->preFixDB.$this->defaultTableName;
|
||||
$this->checkOrCreateTable();
|
||||
}
|
||||
|
||||
public function getTable() : string {
|
||||
return $this->table;
|
||||
}
|
||||
// protected function checkTransfer(array $data){
|
||||
// $data['data']['transfer'] = ((count($this->attributes['details']) == 1) && $accounts->isAccount($this->attributes['details'][0]->account_id));
|
||||
// }
|
||||
|
||||
/**
|
||||
* liefert Buchungen für Aktivitätenliste
|
||||
* liefert Buchungen für Aktivitätenliste für alte datentabelle
|
||||
* @return mixed Datensätze für Tabelle
|
||||
*/
|
||||
public function getEntriesDet($start, $ende, $source){
|
||||
public function getEntriesDet(string $start, string $ende, int $source){
|
||||
$details = model('App\Models\mBillDetails');
|
||||
$quelle = model('App\Models\mAccounts');
|
||||
$this->where('datum >=', date( 'Y-m-d', strtotime($start)));
|
||||
@@ -51,7 +76,7 @@ class mBills extends Model {
|
||||
}
|
||||
}
|
||||
|
||||
$result = $this->orderBy('datum','desc')->where('lkz',null)->findAll();
|
||||
$result = $this->orderBy('datum','desc')->findAll(); //->where('lkz',null)
|
||||
if (0 == sizeof($result)){
|
||||
return $result;
|
||||
}
|
||||
@@ -64,7 +89,6 @@ class mBills extends Model {
|
||||
|
||||
foreach ($result as $value) {
|
||||
$value->details = $ergebnis[$value->id]??null;
|
||||
$value->datum = date( "d.m.Y", strtotime( $value->datum ));
|
||||
$value->source = $quelle->getDropDownEntry($value->source_id);
|
||||
// $value->amount = number_format( $value->amount , 2, ',', '.' );
|
||||
}
|
||||
@@ -72,72 +96,44 @@ class mBills extends Model {
|
||||
}
|
||||
|
||||
/**
|
||||
* liefert Buchungen für Aktivitätenliste
|
||||
* @return mixed Datensätze für Tabelle
|
||||
*/
|
||||
public function getActivities(string $start, string $ende, int $source){
|
||||
$quelle = model('App\Models\mAccounts');
|
||||
$this->where('datum >=', date( 'Y-m-d', strtotime($start)));
|
||||
$this->where('datum <=', date( 'Y-m-d', strtotime($ende)));
|
||||
if ($source > 0) {
|
||||
$this->where('source_id', $source );
|
||||
}
|
||||
$result = $this->orderBy('datum','desc')->findAll();
|
||||
if (0 == sizeof($result)) return $result;
|
||||
foreach ($result as $value) {
|
||||
$value->source = $quelle->getDropDownEntry($value->source_id);
|
||||
}
|
||||
return $result;
|
||||
|
||||
}
|
||||
/**
|
||||
* liefert Buchungen für Übersichtstabelle
|
||||
* @return mixed Datensätze für Tabelle
|
||||
*/
|
||||
function getEntries(){
|
||||
$details = model('App\Models\mBillDetails');
|
||||
$quelle = model('App\Models\mAccounts');
|
||||
$result = $this->orderBy('datum','desc')->where('validate',false)->where('lkz',null)->findAll(10);
|
||||
$keys=array();
|
||||
foreach ($result as $value) {
|
||||
$keys[] = intval($value->id);
|
||||
}
|
||||
//$ergebnis = $details->whereIn('bill_id', $keys)->asArray()->findAll();
|
||||
$ergebnis = $details->getDetails($keys);
|
||||
// print_r($ergebnis);
|
||||
|
||||
foreach ($result as $value) {
|
||||
$value->details = $ergebnis[$value->id]??null;//$details->getEntries($value->id);
|
||||
$value->datum = date( "d.m.Y", strtotime( $value->datum ));
|
||||
$value->quelle = $quelle->getDropDownEntry($value->source_id);
|
||||
// $value->amount = number_format( $value->amount , 2, ',', '.' );
|
||||
}
|
||||
public function getEntries(int $zeilen = 10){
|
||||
$result = $this->orderBy('datum','desc')->where('validate',false)->findAll($zeilen); //->where('lkz',null)
|
||||
if (0 == sizeof($result)) return $result;
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* liefert gegebene Buchung inkl. Details im Formularformat
|
||||
* @param int id der Buchung
|
||||
* @return mixed Datenstruktur für Formular
|
||||
*/
|
||||
function getEntry($id, &$result, $accounts){
|
||||
$details = model('App\Models\mBillDetails');
|
||||
$quelle = model('App\Models\mAccounts');
|
||||
$data = $this->orderBy('datum','desc')->find($id);
|
||||
$result['datum'] = $data->datum;//date( "d.m.Y", strtotime($data->datum));
|
||||
$result['receiver'] = $data->receiver;
|
||||
$result['billfile'] = $data->rechnung;
|
||||
$result['source'] = $data->source_id;
|
||||
$result['validate'] = $data->validate;
|
||||
$result['id'] = $id;
|
||||
$result = array_merge($result, $details->getEntriesForForm($id, $result, $accounts));
|
||||
$result['transfer'] = $result['transfer'] || ($data->type == 'transfer');
|
||||
}
|
||||
|
||||
public function getToDos(){
|
||||
$result = $this->orderBy('datum','desc')->where('validate',true)->findAll();
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
function getToDos(){
|
||||
$details = model('App\Models\mBillDetails');
|
||||
$quelle = model('App\Models\mAccounts');
|
||||
$result = $this->orderBy('datum','desc')->where('validate',true)->where('lkz',null)->findAll();
|
||||
$keys=array();
|
||||
foreach ($result as $value) {
|
||||
$keys[] = intval($value->id);
|
||||
}
|
||||
$ergebnis = $details->getDetails($keys);
|
||||
|
||||
foreach ($result as $value) {
|
||||
$value->details = $ergebnis[$value->id]??null;
|
||||
//$value->details = $details->getEntries($value->id);
|
||||
$value->datum = date( "d.m.Y", strtotime( $value->datum ));
|
||||
$value->quelle = $quelle->getDropDownEntry($value->source_id);;
|
||||
// $value->amount = number_format( $value->amount , 2, ',', '.' );
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
function getAccountsBalance(){
|
||||
$sums = $this->db->query("SELECT * FROM get_account_balance4()");
|
||||
return $sums->getResult();
|
||||
public function getAccountsBalance(){
|
||||
$sums = $this->db->query("SELECT * FROM finanzen.getbalances('".auth()->user()->preFixDB."'::text,ARRAY[]::integer[])"); // mit datensyntax ARRAY[210,216]
|
||||
// log_message('debug', $this->db->getLastQuery());
|
||||
// log_message('debug', print_r($sums->getResultArray(), true));
|
||||
return $sums->getResultArray();
|
||||
}
|
||||
|
||||
public function storeBillFile(int $billId, string $filename){
|
||||
@@ -148,52 +144,49 @@ class mBills extends Model {
|
||||
$this->update($billId);
|
||||
}
|
||||
|
||||
public function saveBill($data) : int {
|
||||
$details = model('App\Models\mBillDetails');
|
||||
$accounts = model('App\Models\mAccounts');
|
||||
$this->set('datum', date( "Y-m-d", strtotime($data['datum'] ) ));
|
||||
|
||||
$this->set('source_id', $data['source']);
|
||||
$total = floatval($data['total_in'])-floatval($data['total_out']);
|
||||
if ( !isset($data['multi']) )
|
||||
$total = floatval($data['input'][0])-floatval($data['output'][0]);
|
||||
if ( isset($data['rechnung']) )
|
||||
$this->set('rechnung', $data['rechnung']);
|
||||
$this->set('amount', $total);
|
||||
$this->set('validate', $data['validate']??false);
|
||||
if ($data['transfer']==1){
|
||||
$this->set('receiver', $accounts->getDropDownEntry($data['category'][0]));
|
||||
$this->set('type', 'transfer');
|
||||
}
|
||||
else{
|
||||
$this->set('receiver', $data['receiver']);
|
||||
$this->set('type', 'multiple');
|
||||
}
|
||||
//$tabdata['type']
|
||||
if ( 0 == $data['id'] ){
|
||||
$this->insert();
|
||||
$billId = $this->getInsertID();
|
||||
//insert
|
||||
}
|
||||
else{
|
||||
$billId = $data['id'];
|
||||
$this->update($data['id']);
|
||||
}
|
||||
$details->saveEntries($data, $billId);
|
||||
return $billId;
|
||||
}
|
||||
|
||||
/**
|
||||
* löscht gegebene Buchung inkl. Details
|
||||
* @param int id der Buchung
|
||||
* @return mixed Datenstruktur für Formular
|
||||
*/
|
||||
function deleteEntry($id){
|
||||
public function deleteEntry(int $id) : bool {
|
||||
$result = true;
|
||||
$details = model('App\Models\mBillDetails');
|
||||
if ($id > 0){
|
||||
$details->where('bill_id',$id)->delete();
|
||||
$this->delete($id);
|
||||
$result = $details->where('bill_id',$id)->delete();
|
||||
if ($result === true)
|
||||
$result = $this->delete($id);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function checkOrCreateTable(){
|
||||
log_message('debug',"Check or Create Bill Table");
|
||||
if ($this->db->tableExists('finanzen.'.$this->table)) return;
|
||||
log_message('debug','Create Bills Table '.$this->db->lastQuery);
|
||||
log_message('debug',"Create Bills Table ".$this->table);
|
||||
$accounts = model('App\Models\mAccounts'); /* @var \App\Models\mAccounts $accounts */
|
||||
$forge = \Config\Database::forge();
|
||||
$fields = array(
|
||||
'id' => array( 'type' => 'serial'),
|
||||
'datum' => array( 'type' => 'date'),
|
||||
'receiver' => array( 'type' => 'character varying','constraint' => '120'),
|
||||
'source_id' => array( 'type' => 'integer' ),
|
||||
'amount' => array( 'type' => 'numeric','constraint' => '9,2' ),
|
||||
'validate' => array( 'type' => 'boolean' ),
|
||||
'rechnung' => array( 'type' => 'character varying','constraint' => '50','null'=>true),
|
||||
'deleted_at'=> array( 'type' => 'timestamp without time zone','null'=>true),
|
||||
);
|
||||
$forge->addField( $fields );
|
||||
$forge->addPrimaryKey('id');
|
||||
$forge->addForeignKey('source_id', $accounts->getTable(), 'id','NO ACTION','NO ACTION','source_id_fk');
|
||||
// $this->db->query("CREATE TABLE $this->table (LIKE $this->defaultTableName INCLUDING DEFAULTS INCLUDING CONSTRAINTS) PARTITION BY RANGE (datum);");
|
||||
$forge->createTable($this->table,true,['PARTITION BY RANGE'=>'(datum)']);
|
||||
log_message('debug','Create Bills Table '.$this->db->lastQuery);
|
||||
// CREATE TABLE temperatures_y2025q1 PARTITION OF temperatures
|
||||
// FOR VALUES FROM ('2025-01-01') TO ('2025-04-01');
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
?>
|
||||
Reference in New Issue
Block a user