194 lines
7.8 KiB
PHP
194 lines
7.8 KiB
PHP
<?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;
|
|
}
|
|
} |