dbxapp 4.1.3
CMS, Shop, Workflows und modulare Geschäftsanwendungen
Loading...
Searching...
No Matches
myInvoicesService.class.php
Go to the documentation of this file.
1<?php
2namespace dbx\myInvoices;
3
12{
13 private const INVOICE_DD = 'myInvoices|invoice';
14 private const ITEM_DD = 'myInvoices|invoiceItem';
15 private const FORM_FD = 'myInvoices|invoice-form';
16 private const REPORT_FD = 'myInvoices|rpt-invoice-selection';
17
18 private int $invoicePageTotalCents = 0;
19 private int $itemTotalCents = 0;
20
24 private function euro(int $cents): string
25 {
26 return number_format($cents / 100, 2, ',', '.') . ' EUR';
27 }
28
36 private function url(string $run, array $params = array()): string
37 {
38 $url = '?dbx_modul=myInvoices&dbx_run1=' . rawurlencode($run);
39 foreach ($params as $key => $value) {
40 $url .= '&' . rawurlencode((string)$key)
41 . '=' . rawurlencode((string)$value);
42 }
43 return $url;
44 }
45
54 public function form(): string
55 {
56 $ridValue = dbx()->get_modul_var(
57 'rid',
58 'new',
59 'parameter|max=24'
60 );
61 $rid = $ridValue === 'new' ? 0 : (int)$ridValue;
62 $isNew = $rid <= 0;
63
64 $form = dbx()->get_system_obj('dbxForm');
65 $form->init('invoice-form', 'myInvoices|invoice-form');
66 $form->_dd = self::INVOICE_DD;
67 $form->_fd = self::FORM_FD;
68 $form->load_fd_messages();
69
70 $data = $isNew
71 ? array('status' => 'draft')
72 : dbx()->get_system_obj('dbxDB')->select1(
73 self::INVOICE_DD,
74 array('id' => $rid)
75 );
76
77 if (!$isNew && (int)($data['id'] ?? 0) <= 0) {
78 return dbx()->get_system_obj('dbxTPL')->get_tpl(
79 'dbx|alert-warning',
80 array('msg' => $form->get_fd_message('not_found'))
81 );
82 }
83
84 $form->_data = is_array($data) ? $data : array();
85 $form->_rid = $rid;
86 $form->_action = $this->url('form', array(
87 'rid' => $isNew ? 'new' : $rid,
88 ));
89 $form->_msg_info = $form->get_fd_message('form_info');
90 $form->add_rep(
91 'form_title',
92 $form->get_fd_message(
93 $isNew ? 'form_title_new' : 'form_title_edit'
94 )
95 );
96 $form->add_rep(
97 'bar_title',
98 $form->get_fd_message('help_title')
99 );
100 $form->add_rep('list_url', $this->url('report'));
101 $form->add_flds();
102
103 if ($form->submit()) {
104 if (!$form->errors()) {
105 $ok = $form->save_post(
106 self::INVOICE_DD,
107 $isNew ? 'new' : $rid
108 );
109 $savedRid = (int)$form->_rid;
110 if ($ok && $isNew && $savedRid > 0) {
111 $form->_action = $this->url(
112 'form',
113 array('rid' => $savedRid)
114 );
115 $form->add_rep(
116 'form_title',
117 $form->get_fd_message('form_title_edit')
118 );
119 }
120 } else {
121 $form->_msg_error = $form->get_fd_message(
122 'validation_error'
123 );
124 }
125 }
126
127 return $form->run();
128 }
129
143 public function invoice_report_next_record($report, $record)
144 {
145 if (!is_array($record)) {
146 return $record;
147 }
148
149 $id = (int)($record['id'] ?? 0);
150 $totalCents = (int)round(
151 (float)($record['total_gross'] ?? 0) * 100
152 );
153
154 $this->invoicePageTotalCents += $totalCents;
155 $report->add_rep(
156 'invoice_report_total',
157 $this->euro($this->invoicePageTotalCents)
158 );
159 $record['action'] = dbx()->get_system_obj('dbxTPL')->get_tpl(
160 'myInvoices|invoice-row-action',
161 array(
162 'edit_url' => $this->url('form', array('rid' => $id)),
163 'delete_url' => dbx()->action_url(
164 $this->url('delete', array('rid' => $id))
165 ),
166 'delete_title' => $report->get_fd_message(
167 'delete_title'
168 ),
169 'delete_question' => $report->format_fd_message(
170 'delete_question',
171 array('id' => $id)
172 ),
173 'delete_hint' => $report->get_fd_message(
174 'delete_hint'
175 ),
176 )
177 );
178 $status = (string)($record['status'] ?? '');
179 if ($status !== '') {
180 $record['status'] = $report->get_fd_message(
181 'status_' . $status,
182 $status
183 );
184 }
185 $record['total_gross'] = $this->euro($totalCents);
186 $record['positions_call'] =
187 '[modul=myInvoices]dbx_run1=positions&invoice_id='
188 . $id
189 . '[/modul]';
190
191 return $record;
192 }
193
205 public function report(
206 string $success = '',
207 string $error = ''
208 ): string {
209 $db = dbx()->get_system_obj('dbxDB');
210 $report = dbx()->get_system_obj('dbxReport');
211 $report->init(
212 'invoice-report',
213 'myInvoices|invoice-report'
214 );
215 $report->_dd = self::INVOICE_DD;
216 $report->_mode = 'table';
217 $report->_action = $this->url('report');
218 $report->_pages = true;
219 $report->_but_pagination = 7;
220 $report->_create_row_select = false;
221 $report->_create_row_edit = false;
222 $report->_create_row_delete = false;
223 $report->add_rep('install_url', $this->url('install'));
224 $report->create_selection_fields(self::REPORT_FD);
225 $report->add_rep(
226 'bar_title',
227 $report->get_fd_message('report_title')
228 );
229 $report->_msg_success = $success === ''
230 ? ''
231 : $report->get_fd_message($success, $success);
232 $report->_msg_error = $error === ''
233 ? ''
234 : $report->get_fd_message($error, $error);
235
236 $this->invoicePageTotalCents = 0;
237
238 if ($report->submit() && $report->errors()) {
239 $report->_msg_error = $report->get_fd_message('filter_error');
240 }
241
242 $search = trim((string)$report->get_fld_val(
243 'dbx_rwhere',
244 '',
245 'sqlsearch|max=64'
246 ));
247 $status = (string)$report->get_fld_val(
248 'dbx_rstatus',
249 'all',
250 'parameter|max=24'
251 );
252 $pageSize = max(10, min(50, (int)$report->get_fld_val(
253 'dbx_rrows',
254 20,
255 'int'
256 )));
257 $position = max(0, (int)$report->get_fld_val(
258 'dbx_rpos',
259 0,
260 'int'
261 ));
262 $sort = (string)$report->get_fld_val(
263 'dbx_rsort',
264 'invoice_date',
265 'parameter'
266 );
267 $direction = strtoupper((string)$report->get_fld_val(
268 'dbx_rdesc',
269 'DESC',
270 'parameter'
271 ));
272
273 $allowedSort = array(
274 'invoice_no',
275 'invoice_date',
276 'customer',
277 'status',
278 'total_gross',
279 );
280 if (!in_array($sort, $allowedSort, true)) {
281 $sort = 'invoice_date';
282 }
283 if (!in_array($direction, array('ASC', 'DESC'), true)) {
284 $direction = 'DESC';
285 }
286
287 $where = array();
288 if (in_array($status, array('draft', 'open', 'paid'), true)) {
289 $where['status'] = $status;
290 }
291 if ($search !== '') {
292 $where['search'] = array(
293 'value' => $search,
294 'like' => array('invoice_no', 'customer'),
295 'mode' => 'contains',
296 );
297 }
298
299 $columns = array(
300 'id',
301 'invoice_no',
302 'invoice_date',
303 'customer',
304 'status',
305 'total_gross',
306 );
307 $rows = $db->select(
308 self::INVOICE_DD,
309 $where,
310 $columns,
311 $sort,
312 $direction,
313 '',
314 $pageSize,
315 $position
316 );
317
318 if (!is_array($rows)) {
319 return dbx()->get_system_obj('dbxTPL')->get_tpl(
320 'myInvoices|install-required',
321 array('install_url' => $this->url('install'))
322 );
323 }
324
325 $report->_rflds = array(
326 'invoice_no' => $report->get_fd_message('column_invoice_no'),
327 'invoice_date' => $report->get_fd_message(
328 'column_invoice_date'
329 ),
330 'customer' => $report->get_fd_message('column_customer'),
331 'status' => $report->get_fd_message('column_status'),
332 'action' => $report->get_fd_message('column_action'),
333 'total_gross' => $report->get_fd_message('column_total'),
334 );
335 $report->_rpt_format = array(
336 'invoice_date' => 'php-date-usr',
337 'action' => 'html',
338 );
339 $report->_rrows = $pageSize;
340 $report->_rpos = $position;
341 $report->_count_all = $db->count(self::INVOICE_DD);
342 $report->_rcount = $db->count(self::INVOICE_DD, $where);
343 $report->_rdata = $rows;
344
345 return $report->run();
346 }
347
358 {
359 if (!is_array($record)) {
360 return $record;
361 }
362
363 $quantity = (float)($record['quantity'] ?? 0);
364 $unitPrice = (float)($record['unit_price'] ?? 0);
365 $sumCents = (int)round($quantity * $unitPrice * 100);
366
367 $this->itemTotalCents += $sumCents;
368 $report->add_rep(
369 'report_total',
370 $this->euro($this->itemTotalCents)
371 );
372 $record['quantity'] = number_format($quantity, 2, ',', '.');
373 $record['unit_price'] = $this->euro(
374 (int)round($unitPrice * 100)
375 );
376 $record['sum'] = $this->euro($sumCents);
377
378 return $record;
379 }
380
390 public function positions(): string
391 {
392 $invoiceId = (int)dbx()->get_modul_var(
393 'invoice_id',
394 0,
395 'int'
396 );
397 if ($invoiceId <= 0) {
398 return '';
399 }
400
401 $db = dbx()->get_system_obj('dbxDB');
402 $invoice = $db->select1(
403 self::INVOICE_DD,
404 array('id' => $invoiceId),
405 array('id', 'invoice_no')
406 );
407 if ((int)($invoice['id'] ?? 0) <= 0) {
408 return '';
409 }
410
411 $rows = $db->select(
412 self::ITEM_DD,
413 array('invoice_id' => $invoiceId),
414 array(
415 'position_no',
416 'article_no',
417 'description',
418 'quantity',
419 'unit_price',
420 ),
421 'position_no',
422 'ASC'
423 );
424
425 $report = dbx()->get_system_obj('dbxReport');
426 $report->init(
427 'invoice-items-report',
428 'myInvoices|invoice-items-report'
429 );
430 $report->_dd = self::ITEM_DD;
431 $report->_fd = self::REPORT_FD;
432 $report->load_fd_messages();
433 $report->_mode = 'table';
434 $report->_pages = false;
435 $report->_create_row_select = false;
436 $report->_create_row_edit = false;
437 $report->_create_row_delete = false;
438 $report->add_rep(
439 'invoice_no',
440 (string)$invoice['invoice_no']
441 );
442
443 $this->itemTotalCents = 0;
444
445 $report->_rflds = array(
446 'position_no' => $report->get_fd_message(
447 'column_position_no'
448 ),
449 'article_no' => $report->get_fd_message(
450 'column_article_no'
451 ),
452 'description' => $report->get_fd_message('column_article'),
453 'quantity' => $report->get_fd_message('column_quantity'),
454 'unit_price' => $report->get_fd_message(
455 'column_unit_price'
456 ),
457 'sum' => $report->get_fd_message('column_total'),
458 );
459 $report->_rrows = is_array($rows) ? count($rows) : 0;
460 $report->_rpos = 0;
461 $report->_count_all = $report->_rrows;
462 $report->_rcount = $report->_rrows;
463 $report->_rdata = is_array($rows) ? $rows : array();
464
465 return $report->run();
466 }
467
477 public function delete(): string
478 {
479 $rid = (int)dbx()->get_modul_var('rid', 0, 'int');
480
481 if ($rid <= 0) {
482 return $this->report(
483 '',
484 'delete_invalid'
485 );
486 }
487
488 $db = dbx()->get_system_obj('dbxDB');
489 if ($db->begin(self::INVOICE_DD) !== 1) {
490 return $this->report(
491 '',
492 'transaction_error'
493 );
494 }
495
496 $itemsDeleted = $db->delete(
497 self::ITEM_DD,
498 array('invoice_id' => $rid)
499 );
500 $invoiceDeleted = in_array($itemsDeleted, array(0, 1), true)
501 ? $db->delete(self::INVOICE_DD, array('id' => $rid))
502 : -2;
503
504 if ($invoiceDeleted !== 1
505 || $db->commit(self::INVOICE_DD) !== 1
506 ) {
507 $db->rollback(self::INVOICE_DD);
508 return $this->report(
509 '',
510 'delete_error'
511 );
512 }
513
514 return $this->report('delete_success');
515 }
516
525 public function installation(): string
526 {
527 $form = dbx()->get_system_obj('dbxForm');
528 $form->init(
529 'myInvoices-install',
530 'myInvoices|invoice-install'
531 );
532 $form->_fd = 'myInvoices|invoice-install';
533 $form->load_fd_messages();
534 $form->add_rep(
535 'bar_title',
536 $form->get_fd_message('bar_title')
537 );
538
539 if (!dbx()->can('admin')) {
540 return dbx()->get_system_obj('dbxTPL')->get_tpl(
541 'dbx|alert-warning',
542 array('msg' => $form->get_fd_message('admin_required'))
543 );
544 }
545
546 $form->_action = $this->url('install');
547 $form->_msg_info = $form->get_fd_message('install_info');
548 $form->_data = array_merge(
549 $form->_data,
550 array('install_demo' => 1)
551 );
552 $form->add_rep('report_url', $this->url('report'));
553 $form->add_fld(
554 'install_demo',
555 'dbx|hidden',
556 rules: 'int',
557 dd: ''
558 );
559
560 if ($form->submit()) {
561 if ($form->errors()) {
562 $form->_msg_error = $form->get_fd_message(
563 'install_invalid'
564 );
565 } else {
566 $fixtures = dbx()->get_include_obj(
567 'myInvoicesFixtures',
568 'myInvoices'
569 );
570 $result = $fixtures->install(true);
571 if ((int)($result['ok'] ?? 0) === 1) {
572 $form->_msg_success = $form->format_fd_message(
573 'install_success',
574 array(
575 'invoices' => (int)$result['created_invoices'],
576 'items' => (int)$result['created_items'],
577 'existing' => (int)$result['existing_invoices'],
578 )
579 );
580 } else {
581 dbx()->sys_msg(
582 'error',
583 'myInvoices',
584 'install',
585 'Installation failed',
586 (string)($result['message'] ?? '')
587 );
588 $form->_msg_error = $form->get_fd_message(
589 'install_failed'
590 );
591 }
592 }
593 }
594
595 return $form->run();
596 }
597}
598
599?>
Fachservice des ausführbaren Rechnungs-Referenzmoduls.
invoice_items_report_next_record($report, $record)
Berechnet und formatiert die virtuelle Summenspalte einer Position.
installation()
Rendert den dbxForm-geschützten Installations- und Fixture-POST.
invoice_report_next_record($report, $record)
Bereitet eine Rechnungszeile unmittelbar vor dem Rendern auf.
report(string $success='', string $error='')
Rendert den paginierten Rechnungsreport.
form()
Rendert und verarbeitet das Rechnungskopfformular.
positions()
Rendert die zweite Listenstufe einer konkreten Rechnung.
if((string)($formActionPolicy['action'] ?? '') !=='dbxAction.save'||!dbx() ->check_action_token((string)($formActionPolicy['scope'] ?? ''),(string)($formActionRoute['params']['dbx_token'] ?? ''))) $report
action_url(string $url, string $action='', array $bindings=array())
Tokenisiert eine automatisch erkannte zustandsaendernde Route.
Definition dbxApi.php:1270
dbx()
Liefert die zentrale dbXapp-API als Singleton.
Definition dbxApi.php:2805
can($access_groups='', $user_groups='')
Prueft Gruppenrechte gegen den aktuellen oder uebergebenen Benutzer.
Definition dbxApi.php:1381
DBX schema administration.