147 lines
6.1 KiB
PHP
147 lines
6.1 KiB
PHP
<?php namespace App\Models;
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
class mBillDetails extends Model {
|
|
private $defaultTableName='_budget_billdetails';
|
|
protected $primaryKey = 'id';
|
|
|
|
protected $useAutoIncrement = true;
|
|
|
|
protected $returnType = \App\Entities\eBookingLine::class;
|
|
protected $useSoftDeletes = true;
|
|
protected $deletedField ="deleted_at";
|
|
|
|
protected $allowedFields = ['bill_id','comment','account_id','subamount','deleted_at'];
|
|
|
|
protected $useTimestamps = false;
|
|
|
|
protected array $casts = [
|
|
'subamount' => 'float',
|
|
'account_id' => 'int',
|
|
'bill_id' => 'int',
|
|
];
|
|
|
|
protected $validationRules = [
|
|
'bill_id' => 'required|numeric',
|
|
'account_id' => 'required|numeric',
|
|
];
|
|
|
|
protected $validationMessages = [
|
|
'bill_id' => [
|
|
'required' => 'Die Buchungsnummer muss angegeben sein.',
|
|
'numeric' => 'Die Buchungsnummer muss eine Zahl sein.',
|
|
],
|
|
'account_id' => [
|
|
'required' => 'Die Kategorie muss angegeben sein.',
|
|
'numeric' => 'Die Kategorie muss eine Zahl sein.',
|
|
],
|
|
];
|
|
|
|
protected function initialize() {
|
|
log_message('debug','mBillDetails initialize');
|
|
$this->table = auth()->user()->preFixDB.$this->defaultTableName;
|
|
$this->checkOrCreateTable();
|
|
}
|
|
|
|
public function getEntries(int $id){
|
|
$accounts = model('App\Models\mAccounts'); /* @var \App\Models\mAccounts $accounts */
|
|
$this->select($this->table.'.*,'.$accounts->getTable().'.description');
|
|
$this->join($accounts->getTable(), $accounts->getTable().'.id = '.$this->table.'.account_id', 'left');
|
|
$result = $this->orderBy('id','asc')->where('bill_id',$id)->findAll();
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* liefert für gegebene Buchung Details
|
|
* @param array<int> $ids id der Buchung
|
|
*/
|
|
public function getDetails(array $ids): array {
|
|
$quelle = model('App\Models\mAccounts');
|
|
$rows = $this->whereIn('bill_id', $ids)->findAll();
|
|
$result = array();
|
|
foreach($rows as $row){
|
|
$row->description = $quelle->getDropDownEntry($row->account_id);
|
|
$result[$row->bill_id][] = $row;
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* liefert für gegebene Buchung Details im Formularformat
|
|
* @param int $billId id der Buchung
|
|
* @param mixed $result Datenstruktur für Formular mit defaults
|
|
* @param \App\Models\mAccounts $accounts modelClass für Konten
|
|
*
|
|
*/
|
|
public function getEntriesForForm(int $billId, &$result){
|
|
$accounts = model('App\Models\mAccounts');
|
|
$this->select($this->table.'.*,'.$accounts->getTable().'.description');
|
|
$this->join($accounts->getTable(), $accounts->getTable().'.id = '.$this->table.'.account_id', 'left');
|
|
$data = $this->orderBy('id','asc')->where('bill_id',$billId)->findAll();
|
|
$amount = 0.0;
|
|
foreach ($data as $key => $row){
|
|
$result['sid'][$key]= $row->id;
|
|
$result['category'][$key]= $row->account_id;
|
|
$result['category_parent'][$key]= $accounts->getParent($result['category'][$key]);
|
|
$result['input'][$key]= ($row->subamount >= 0.0)?number_format( abs( $row->subamount ), 2, ',', '' ):'';
|
|
$result['output'][$key]= ($row->subamount < 0.0)?number_format( abs( $row->subamount ), 2, ',', '' ):'';
|
|
$result['comment'][$key]= $row->comment;
|
|
$amount += $row->subamount;
|
|
// $value->datum = date( "d.m.Y", strtotime( $value->datum ));
|
|
// $value->amount = number_format( $value->amount , 2, ',', '.' );
|
|
}
|
|
if (count($data) > 1){
|
|
$result['multi'] = 'on';
|
|
$result['total_in']= ($amount >= 0.0)?number_format( abs( $amount ), 2, ',', '' ):'';
|
|
$result['total_out']= ($amount < 0.0)?number_format( abs( $amount ), 2, ',', '' ):'';
|
|
}
|
|
else if ($accounts->isAccount($result['category'][0]))
|
|
$result['transfer'] = true;
|
|
return $result;
|
|
}
|
|
|
|
public function saveEntries(array $data, int $billId){
|
|
for ($i=0;$i<10;$i++){
|
|
if ($data['category'][$i] > 0){
|
|
$this->set('subamount', floatval($data['input'][$i])-floatval($data['output'][$i])); //floatval($data['total_in'])-floatval($data['total_out']));
|
|
$this->set('comment', $data['comment'][$i]);
|
|
$this->set('account_id', $data['category'][$i]);
|
|
$this->set('bill_id', $billId);
|
|
if ( 0 == $data['sid'][$i] ){
|
|
$this->insert();
|
|
}
|
|
else{
|
|
$this->update($data['sid'][$i]);
|
|
}
|
|
}
|
|
else if (0 < $data['sid'][$i]) {
|
|
$this->delete(['id' => $data['sid'][$i]]);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function checkOrCreateTable(){
|
|
log_message('debug',"Check or Create BillDetails Table");
|
|
if ($this->db->tableExists($this->table)) return;
|
|
log_message('debug',"Create BillDetails Table ".$this->table);
|
|
$accounts = model('App\Models\mAccounts'); /* @var \App\Models\mAccounts $accounts */
|
|
$bills = model('App\Models\mBills'); /* @var \App\Models\mBills $bills */
|
|
$forge = \Config\Database::forge();
|
|
$fields = array(
|
|
'id' => array( 'type' => 'serial'),
|
|
'bill_id' => array( 'type' => 'integer'),
|
|
'account_id' => array( 'type' => 'integer' ),
|
|
'subamount' => array( 'type' => 'numeric','constraint' => '9,2' ),
|
|
'comment' => array( 'type' => 'character varying','constraint' => '150'),
|
|
'deleted_at'=> array( 'type' => 'timestamp without time zone','null'=>true),
|
|
);
|
|
$forge->addField( $fields );
|
|
$forge->addPrimaryKey('id');
|
|
$forge->addForeignKey('bill_id', $bills->getTable(), 'id','NO ACTION','NO ACTION','bill_id_fk');
|
|
$forge->addForeignKey('account_id', $accounts->getTable(), 'id','NO ACTION','NO ACTION','account_id_fk');
|
|
$forge->createTable($this->table,true);
|
|
log_message('debug','Create BillDetails Table '.$this->db->lastQuery);
|
|
}
|
|
};
|
|
?>
|