redesign for mulituser
change validation concept (client and server) using entities
This commit is contained in:
194
app/Entities/eBooking.php
Normal file
194
app/Entities/eBooking.php
Normal file
@@ -0,0 +1,194 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entities;
|
||||
|
||||
use App\Models\mBills;
|
||||
use App\Models\mBillDetails;
|
||||
use App\Models\mScheduled;
|
||||
use CodeIgniter\Entity\Entity;
|
||||
use CodeIgniter\I18n\Time;
|
||||
|
||||
class eBooking extends Entity{
|
||||
protected $file = null; /* @var \CodeIgniter\HTTP\Files\UploadedFile|null $file */
|
||||
protected $attributes =[
|
||||
'id' => 0,
|
||||
'datum' => '',
|
||||
'source_id' => 0,
|
||||
'receiver' => '',
|
||||
'amount' => null,
|
||||
'validate' => false,
|
||||
'transfer' => false,
|
||||
'scheduled' => false,
|
||||
'details' => [],
|
||||
'deleted_at' => null,
|
||||
'rechnung' => null,
|
||||
'scheduledNum' => null,
|
||||
'scheduledType' => null
|
||||
];
|
||||
|
||||
public function fillDetails(){
|
||||
$billD = model(mBillDetails::class);
|
||||
$accounts = model('App\Models\mAccounts'); /* @var \App\Models\mAccounts $accounts */
|
||||
$this->attributes['details'] = $billD->where('bill_id',$this->attributes['id'])->findAll();
|
||||
$this->attributes['scheduled'] = false;
|
||||
$this->attributes['transfer'] = ((count($this->attributes['details']) == 1) && $accounts->isAccount($this->attributes['details'][0]->account_id));
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setInvoice(\CodeIgniter\HTTP\Files\UploadedFile $file){
|
||||
$this->file = $file;
|
||||
}
|
||||
|
||||
public function getNew(){
|
||||
$this->attributes['datum'] = date('Y-m-d');
|
||||
$this->attributes['details'][] = new \App\Entities\eBookingLine();
|
||||
log_message('debug','eBooking::getNew: '.json_encode($this->attributes));
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getFormData(){
|
||||
$billDet = new \App\Entities\eBookingLine();
|
||||
$data = $this->toArray();
|
||||
$data['template'] = $billDet->toArray();
|
||||
log_message('debug','eBooking::getFormData:template '.json_encode($data['template']));
|
||||
$accounts = model('App\Models\mAccounts'); /* @var \App\Models\mAccounts $accounts */
|
||||
$data['category'] = $accounts->getDropDownLists($this->attributes['transfer']?'Acc':'InOut');
|
||||
$data['sourcelist'] = $accounts->getDropDownList( type:'Acc', defaultKey:$data['source_id'] );
|
||||
// log_message('debug','eBooking::getFormData: '.json_encode($data));
|
||||
//$billDet->createDropDownLists($data);
|
||||
|
||||
if (is_null($this->attributes['amount'])){
|
||||
$data['total_in'] = '';
|
||||
$data['total_out'] = '';
|
||||
}
|
||||
else {
|
||||
$data['total_in'] = ($this->attributes['amount'] >= 0.0)? abs($this->attributes['amount']):'';
|
||||
$data['total_out'] = ($this->attributes['amount'] < 0.0)? abs($this->attributes['amount']):'';
|
||||
}
|
||||
|
||||
if ($this->attributes['scheduled']??false){
|
||||
// $data['scheduledNum'] = 0;
|
||||
// $data['scheduledType'] = 0;
|
||||
$data['schedule_lists'] = array('NumList' => array('1'=>'1','2'=>'2','3'=>'3','4'=>'4','5'=>'5','6'=>'6'),
|
||||
'TypeList' => array('week'=>'Woche(n)','month'=>'Monat(e)','year'=>'Jahr(e)'));
|
||||
}
|
||||
|
||||
if (count($this->attributes['details'])>0){
|
||||
foreach ($this->attributes['details'] as $i => $line){
|
||||
$line->createFormData($data,$i);
|
||||
}
|
||||
}else{
|
||||
foreach ($this->attributes['sid'] as $i => $line){
|
||||
$billDet->createDropDownLists($data,$i);
|
||||
}
|
||||
}
|
||||
log_message('debug','eBooking::getFormData: '.json_encode($data));
|
||||
return $data;
|
||||
}
|
||||
|
||||
// public function convertFromPost(array $data){
|
||||
// $this->fill($data);
|
||||
// foreach ($data['sid'] as $i => $value){
|
||||
// $sub = array('id' => $value,
|
||||
// 'bill_id' => $data['id'],
|
||||
// 'comment' => $data['comment'][$i],
|
||||
// 'account_id' => $data['account_id'][$i],
|
||||
// 'subamount' => floatval($data['input'][$i])-floatval($data['output'][$i]),
|
||||
// 'deleted_at' => null);
|
||||
// $details[] = new \App\Entities\eBookingLine($sub);
|
||||
// }
|
||||
// }
|
||||
|
||||
private function calcAmount(){
|
||||
$amount = floatval($data['total_in'])-floatval($data['total_out']);
|
||||
$subamounts = 0.0;
|
||||
foreach ($this->attributes['input'] as $i => $value){
|
||||
$subamounts = $subamounts + floatval($value) - floatval($this->attributes['output'][$i]);
|
||||
}
|
||||
}
|
||||
|
||||
private function saveScheduled() {
|
||||
$scheduledModel = model(mScheduled::class);
|
||||
$data = $this->toArray();
|
||||
for ($i=(count($data['account_id'])-1); $i>0; $i--){
|
||||
if ($data['account_id'][$i] == "0") {
|
||||
unset($data['account_id'][$i]);
|
||||
unset($data['sid'][$i]);
|
||||
unset($data['input'][$i]);
|
||||
unset($data['output'][$i]);
|
||||
unset($data['comment'][$i]);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return $scheduledModel->saveIt($data);
|
||||
|
||||
}
|
||||
//switch scheduled or bill
|
||||
|
||||
public function saveIt() {
|
||||
if ($this->attributes['scheduled'] != 0) return $this->saveScheduled();
|
||||
|
||||
$billModel = model(mBills::class);
|
||||
|
||||
if ($this->attributes['id'] == 0) {
|
||||
|
||||
$billId = $billModel->insert($this, true);
|
||||
log_message('debug','SaveIt with insert result: '.json_encode($billId));
|
||||
if ($billId === false)
|
||||
return $billModel->errors();
|
||||
else{
|
||||
$this->attributes['id'] = $billId;
|
||||
}
|
||||
//$billId = $$billModel->getInsertID();
|
||||
} else {
|
||||
if ($this->attributes['removeBillFile']??false){
|
||||
$this->attributes['rechnung'] = null;
|
||||
unlink(WRITEPATH . 'uploads/'.auth()->user()->preFixDB.'/'.$this->attributes['removeBillFile']);
|
||||
}
|
||||
if ($billModel->update($this->attributes['id'],$this) === false)
|
||||
return $billModel->errors();
|
||||
}
|
||||
|
||||
$billDetailsModel = model(mBillDetails::class);
|
||||
$details = new \App\Entities\eBookingLine;
|
||||
foreach ($this->attributes['sid'] as $i => $value){
|
||||
if ($this->attributes['account_id'][$i] == 0) { // leere Zeile ignorieren bzw wenn id (also bereits in DB) dann löschen
|
||||
if ($value == 0) {
|
||||
continue;
|
||||
} else {
|
||||
$billDetailsModel->delete($value);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
$sub = array('sid' => ($value == 0)? NULL: $value,
|
||||
'bill_id' => $this->attributes['id'],
|
||||
'comment' => $this->attributes['comment'][$i],
|
||||
'account_id' => $this->attributes['account_id'][$i],
|
||||
'subamount' => floatval($this->attributes['input'][$i])-floatval($this->attributes['output'][$i]),
|
||||
'deleted_at' => null);
|
||||
$details->fill($sub);
|
||||
$billDetailsModel->save($details);
|
||||
}
|
||||
|
||||
if ($this->file) {
|
||||
if (! $this->file->isValid() && ($this->file->getError() != UPLOAD_ERR_NO_FILE)) {
|
||||
throw new \RuntimeException($this->file->getErrorString() . '(' . $this->file->getError() . ')');
|
||||
} else if ($this->file->isValid()) {
|
||||
helper('url');
|
||||
$date = Time::parse($this->attributes['datum']);
|
||||
$name = url_title($date->toLocalizedString('yyyyMMdd')." ".$this->attributes['receiver']." ".$this->attributes['id']);
|
||||
$this->file->move(WRITEPATH . 'uploads/'.auth()->user()->preFixDB,$name.".".$this->file->guessExtension());
|
||||
log_message('debug', $this->file->getName());
|
||||
$this->attributes['rechnung'] = $this->file->getName();
|
||||
if ($billModel->update($this->attributes['id'],$this) === false)
|
||||
return $billModel->errors();
|
||||
}
|
||||
}
|
||||
|
||||
// $details = model('App\Models\mBillDetails');
|
||||
// return $details->getDetails([$this->id]);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
69
app/Entities/eBookingLine.php
Normal file
69
app/Entities/eBookingLine.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entities;
|
||||
|
||||
use CodeIgniter\Entity\Entity;
|
||||
use App\Models\mAccounts;
|
||||
|
||||
class eBookingLine extends Entity{
|
||||
protected $attributes =[
|
||||
'id' => 0,
|
||||
'bill_id' => 0,
|
||||
'comment' => '',
|
||||
'account_id' => 0,
|
||||
'subamount' => null,
|
||||
'deleted_at' => null,
|
||||
];
|
||||
|
||||
protected $datamap = [
|
||||
// property_name => db_column_name
|
||||
'sid' => 'id',
|
||||
];
|
||||
private function checkCreateParentDefault(array &$list, int $parentId) : void {
|
||||
if ($parentId == 0) return;
|
||||
if (!array_key_exists($parentId, $list)){
|
||||
log_message('debug',"Parent not exists: $parentId in List: ".json_encode(array_keys($list)));
|
||||
$list[$parentId] = $this->accounts->getDropDownEntry($parentId);
|
||||
}
|
||||
}
|
||||
|
||||
private function checkCreateSubDefault(array &$list, int $accountId) : void {
|
||||
if (!array_key_exists($accountId, $list)){
|
||||
if ($accountId > 0)
|
||||
$list[$accountId] = $this->accounts->getDropDownEntry($accountId);
|
||||
else
|
||||
$list[0] = '-----';
|
||||
}
|
||||
}
|
||||
|
||||
public function createFormData(array &$data, int $i){
|
||||
if (!array_key_exists('sid', $data)) $data['sid'] = [];
|
||||
if (!array_key_exists('comment', $data)) $data['comment'] = [];
|
||||
if (!array_key_exists('account_id', $data)) $data['account_id'] = [];
|
||||
if (!array_key_exists('deleted_at', $data)) $data['deleted_at'] = [];
|
||||
if (!array_key_exists('input', $data)) $data['input'] = [];
|
||||
if (!array_key_exists('output', $data)) $data['output'] = [];
|
||||
|
||||
$data['sid'][$i] = $this->attributes['id'];
|
||||
$data['comment'][$i] = $this->attributes['comment'];
|
||||
$data['account_id'][$i] = $this->attributes['account_id'];
|
||||
$data['deleted_at'][$i] = $this->attributes['deleted_at'];
|
||||
$data['input'][$i] = (!is_null($this->attributes['subamount']) && $this->attributes['subamount'] > 0.0) ? $this->attributes['subamount'] :'';
|
||||
$data['output'][$i] = (!is_null($this->attributes['subamount']) && $this->attributes['subamount'] <= 0.0) ? abs($this->attributes['subamount']) :'';
|
||||
$this->createDropDownLists($data,$i);
|
||||
}
|
||||
|
||||
public function createDropDownLists(array &$data, int $i){
|
||||
$accounts = model('App\Models\mAccounts'); /* @var \App\Models\mAccounts $accounts */
|
||||
$lists = $data['category'] ?? [];
|
||||
if (!array_key_exists('parent_id', $data)) $data['parent_id'] = [];
|
||||
if (!array_key_exists('cat_parent', $data)) $data['cat_parent'] = [];
|
||||
if (!array_key_exists('cat_sub', $data)) $data['cat_sub'] = [];
|
||||
|
||||
$data['parent_id'][$i] = $accounts->getParent($data['account_id'][$i]);
|
||||
$data['cat_parent'][$i] = $lists['parent'];
|
||||
$this->checkCreateParentDefault($data['cat_parent'][$i],$data['parent_id'][$i]);
|
||||
$data['cat_sub'][$i] = $lists['subs']->{$data['parent_id'][$i]} ?? [];
|
||||
$this->checkCreateSubDefault($data['cat_sub'][$i],$data['account_id'][$i]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user