where('datum >=', date( 'Y-m-d', strtotime($start))); $this->where('datum <=', date( 'Y-m-d', strtotime($ende))); if ($source > 0) { $subs = $details->select('bill_id')->where('account_id',$source)->findAll(); $keys=array(); foreach ($subs as $value) { $keys[] = intval($value->bill_id); } $this->groupStart(); $this->where('source_id', $source ); $this->orWhereIn('id', $keys ); $this->groupEnd(); } $result = $this->orderBy('datum','desc')->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->datum = date( "d.m.Y", strtotime( $value->datum )); $value->source = $quelle->getDropDownEntry($value->source_id); // $value->amount = number_format( $value->amount , 2, ',', '.' ); } 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',0)->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, ',', '.' ); } 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'] = date( "d.m.Y", strtotime($data->datum)); $result['receiver'] = $data->receiver; $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'); return $result; } function getToDos(){ $details = model('App\Models\mBillDetails'); $quelle = model('App\Models\mAccounts'); $result = $this->orderBy('datum','desc')->where('validate',1)->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(){ $this->select('SUM(amount) as amount, source_id as idd, budget_accounts.description',FALSE); $this->groupby('source_id')->asArray(); $this->join('budget_accounts', 'budget_accounts.id = budget_bills.source_id', 'left'); $this->where('budget_accounts.type','account'); $this->where('budget_accounts.LKZ',null); $this->where('budget_bills.validate',0); $result1 = $this->where('budget_bills.LKZ',null)->findAll(); $assoc1 = array(); foreach ($result1 as $value) { $assoc1[$value['idd']] = $value; } // $this->select('SUM((-1)*amount) as amount, target_id as idd, mw_budget_categories.description',FALSE); // $this->groupby('target_id')->asArray(); // $this->join('mw_budget_categories', 'mw_budget_categories.id = mw_budget_bookings.target_id', 'left'); // $this->where('mw_budget_categories.type','account'); // $this->where('mw_budget_categories.LKZ',NULL); // $result2 = $this->where('mw_budget_bookings.LKZ',NULL)->findAll(); // $assoc2 = array(); // foreach ($result2 as $value) { // $assoc2[$value['idd']] = $value; // } $details = model('App\Models\mBillDetails'); $details->select('SUM((-1)*subamount) as amount, account_id as idd, budget_accounts.description',FALSE); $details->groupby('account_id')->asArray(); $details->join('budget_accounts', 'budget_accounts.id = budget_billdetails.account_id', 'left'); $details->join('budget_bills', 'budget_bills.id = budget_billdetails.bill_id', 'left'); $details->where('budget_accounts.type','account'); $details->where('budget_bills.validate',0); $details->where('budget_accounts.LKZ',NULL); $result3 = $details->where('budget_billdetails.LKZ',NULL)->findAll(); $assoc3 = array(); foreach ($result3 as $value) { $assoc3[$value['idd']] = $value; } $sums = array(); foreach (array_keys($assoc1 + $assoc3) as $key) { $obj = new \stdClass; $obj->amount = (isset($assoc1[$key]) ? floatval($assoc1[$key]['amount']) : 0) + (isset($assoc3[$key]) ? floatval($assoc3[$key]['amount']) : 0); $obj->description = isset($assoc1[$key]) ? $assoc1[$key]['description'] : $assoc3[$key]['description']; $sums[] = $obj; } return $sums; } public function saveBill($data){ $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 ($total == 0) $total = floatval($data['input'][0])-floatval($data['output'][0]); $this->set('amount', $total); $this->set('validate', $data['validate']??0); 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); } /** * löscht gegebene Buchung inkl. Details * @param int id der Buchung * @return mixed Datenstruktur für Formular */ function deleteEntry($id){ $details = model('App\Models\mBillDetails'); if ($id > 0){ $this->delete($id); $details->where('bill_id',$id)->delete(); } } }; ?>