Newer
Older
<?php
/**
* This file contains QUI\ERP\Process
*/
namespace QUI\ERP;
use QUI;
use QUI\ERP\Accounting\Offers\Handler as OfferHandler;
use QUI\ERP\Booking\Table as BookingTable;
use QUI\ERP\Purchasing\Processes\Handler as PurchasingHandler;
use QUI\ERP\SalesOrders\Handler as SalesOrdersHandler;
/**
* Class Process

Henning Leutz
committed
* - represents a complete erp process
*
* @package QUI\ERP
*/
class Process
{
/**
* @var string
*/
/**
* @var null|array
*/

Henning Leutz
committed
/**
* @var null|QUI\ERP\Comments
*/

Henning Leutz
committed
/**
* Process constructor.
*
* @param string $processId - the global process id
*/
{
$this->processId = $processId;
}
/**

Henning Leutz
committed
* Return the db table name
*

Henning Leutz
committed
* @return string
*/
{

Henning Leutz
committed
return QUI::getDBTableName('process');
}

Henning Leutz
committed
//region messages

Henning Leutz
committed
/**
* Add a comment to the history for the complete process
*
* @param string $message
* @param bool|int $time - optional, unix timestamp

Henning Leutz
committed
*/
public function addHistory(string $message, bool|int $time = false): void

Henning Leutz
committed
{
$this->getHistory()->addComment($message, $time);
try {
QUI::getDataBase()->update(
$this->table(),
['history' => $this->getHistory()->toJSON()],
['id' => $this->processId]
);
} catch (\QUI\Exception $Exception) {
QUI\System\Log::addError($Exception->getMessage());
}
}
/**

Henning Leutz
committed
* Return the history of the process
* This history only contains the process history
*
* If you want the complete history of all process objects, use getCompleteHistory()
*
* @return QUI\ERP\Comments
*/
{

Henning Leutz
committed
if ($this->History === null) {
$history = '';
'where' => [
'id' => $this->processId
],
'limit' => 1
if (isset($result[0]['history'])) {
$history = $result[0]['history'];
} else {
QUI::getDataBase()->insert($this->table(), [
'id' => $this->processId
]);
}
} catch (\QUI\Exception $Exception) {
QUI\System\Log::addError($Exception->getMessage());

Henning Leutz
committed
}
$this->History = QUI\ERP\Comments::unserialize($history);
}

Henning Leutz
committed
return $this->History;
}

Henning Leutz
committed
/**
* Return a complete history of all process objects

Henning Leutz
committed
*
* @return Comments
*/

Henning Leutz
committed
{
$History = $this->getHistory();
$this->parseInvoices($History);
$this->parseOffers($History);
$this->parseSalesOrders($History);

Henning Leutz
committed
try {
QUI::getEvents()->fireEvent('quiqqerErpGetCompleteHistory', [$this, $this->processId]);
} catch (\Exception $exception) {
QUI\System\Log::addError($exception->getMessage());
}
try {
QUI::getEvents()->fireEvent('quiqqerErpProcessHistory', [$this, $this->processId]);
} catch (\Exception $exception) {
QUI\System\Log::addError($exception->getMessage());
}

Henning Leutz
committed
return $History;
}
//endregion
//region invoice
protected function parseInvoices(Comments $History): void
{
$invoices = $this->getInvoices();
foreach ($invoices as $Invoice) {
$History->addComment(
QUI::getLocale()->get('quiqqer/erp', 'process.history.invoice.created', [
'hash' => $Invoice->getHash()
]),
strtotime($Invoice->getAttribute('date')),
'quiqqer/invoice',
'fa fa-file-text-o',
false,
$Invoice->getHash()
);
$history = $Invoice->getHistory()->toArray();
foreach ($history as $entry) {
if (empty($entry['source'])) {
$entry['source'] = 'quiqqer/invoice';
}
if (empty($entry['sourceIcon'])) {
$entry['sourceIcon'] = 'fa fa-file-text-o';
}
$History->addComment(
$entry['message'],
$entry['time'],
$entry['source'],
$entry['sourceIcon'],
$entry['id'],
$Invoice->getHash()
/**
* Return if the process has invoices or not
*
* @return bool
*/
{
$invoices = $this->getInvoices();
foreach ($invoices as $Invoice) {
if ($Invoice instanceof QUI\ERP\Accounting\Invoice\Invoice) {
return true;
}
}
return false;
}
/**
* Return if the process has temporary invoices or not
*
* @return bool
*/
{
$invoices = $this->getInvoices();
foreach ($invoices as $Invoice) {
if ($Invoice instanceof QUI\ERP\Accounting\Invoice\InvoiceTemporary) {
return true;
}
}
return false;
}
/**

Henning Leutz
committed
* @return Accounting\Invoice\Invoice[]|Accounting\Invoice\InvoiceTemporary[]
*/
{
if (!QUI::getPackageManager()->isInstalled('quiqqer/invoice')) {
return [];
}
try {
return QUI\ERP\Accounting\Invoice\Handler::getInstance()->getInvoicesByGlobalProcessId($this->processId);
} catch (\QUI\Exception $Exception) {
return [];
}
}
//endregion
//region order
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
protected function parseOrders(Comments $History): void
{
// orders
$orders = $this->getOrders();
foreach ($orders as $Order) {
/* order macht das schon selbst
$History->addComment(
QUI::getLocale()->get('quiqqer/erp', 'process.history.order.created'),
strtotime($Order->getCreateDate()),
'quiqqer/order',
'fa fa-shopping-basket'
);
*/
$history = $Order->getHistory()->toArray();
foreach ($history as $entry) {
if (empty($entry['source'])) {
$entry['source'] = 'quiqqer/order';
}
if (empty($entry['sourceIcon'])) {
$entry['sourceIcon'] = 'fa fa-shopping-basket';
}
$History->addComment(
$entry['message'],
$entry['time'],
$entry['source'],
$entry['sourceIcon'],
$entry['id'],
$Order->getHash()
/**
* @return bool
*/
{
return !($this->getOrder() === null);
}
/**

Henning Leutz
committed
* Return the first order of the process
*
*/
public function getOrder(): Order\OrderInProcess|Order\Order|null
{
if (!QUI::getPackageManager()->isInstalled('quiqqer/order')) {
return null;
}
$OrderHandler = QUI\ERP\Order\Handler::getInstance();
try {
return $OrderHandler->getOrderByGlobalProcessId($this->processId);
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeDebugException($Exception);
}
try {
return $OrderHandler->getOrderByHash($this->processId);
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeDebugException($Exception);
}
return null;
}

Henning Leutz
committed
/**
* Return all orders from the process
*
* @return array|Order\Order|Order\Order[]|Order\OrderInProcess[]

Henning Leutz
committed
*/
public function getOrders(): array

Henning Leutz
committed
{
if (!QUI::getPackageManager()->isInstalled('quiqqer/order')) {
return [];
}
return QUI\ERP\Order\Handler::getInstance()->getOrdersByGlobalProcessId($this->processId);

Henning Leutz
committed
} catch (QUI\Exception $Exception) {
return [];
}
}
//endregion
//region offers
protected function parseOffers(Comments $History): void
{
if (!QUI::getPackageManager()->isInstalled('quiqqer/offers')) {
return;
}
// orders
$offers = $this->getOffers();
foreach ($offers as $Offer) {
$History->addComment(
QUI::getLocale()->get('quiqqer/erp', 'process.history.offer.created', [
'hash' => $Offer->getHash()
]),
strtotime($Offer->getAttribute('date')),
'quiqqer/offer',
'fa fa-file-text-o',
false,
$Offer->getHash()
);
$history = $Offer->getHistory()->toArray();
foreach ($history as $entry) {
if (empty($entry['source'])) {
$entry['source'] = 'quiqqer/offer';
}
if (empty($entry['sourceIcon'])) {
$entry['sourceIcon'] = 'fa fa-file-text-o';
}
$History->addComment(
$entry['message'],
$entry['time'],
$entry['source'],
$entry['sourceIcon'],
$entry['id'],
$Offer->getHash()
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
);
}
}
}
/**
* @return QUI\ERP\Accounting\Offers\Offer[]
*/
public function getOffers(): array
{
if (!QUI::getPackageManager()->isInstalled('quiqqer/offers')) {
return [];
}
try {
$offers = QUI::getDatabase()->fetch([
'select' => 'id,hash,global_process_id,date',
'from' => OfferHandler::getInstance()->offersTable(),
'where_or' => [
'global_process_id' => $this->processId,
'hash' => $this->processId
]
]);
} catch (\Exception) {
return [];
}
$result = [];
$Offers = OfferHandler::getInstance();
foreach ($offers as $offer) {
try {
$result[] = $Offers->getOffer($offer['id']);
} catch (\Exception) {
}
}
return $result;
}
//endregion
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
//region booking
protected function parseBookings(Comments $History): void
{
// orders
$bookings = $this->getBookings();
foreach ($bookings as $Booking) {
$History->addComment(
QUI::getLocale()->get('quiqqer/erp', 'process.history.booking.created', [
'hash' => $Booking->getUuid()
]),
$Booking->getCreateDate()->getTimestamp(),
'quiqqer/booking',
'fa fa-ticket',
false,
$Booking->getUuid()
);
$history = $Booking->getHistory()->toArray();
foreach ($history as $entry) {
if (empty($entry['source'])) {
$entry['source'] = 'quiqqer/booking';
}
if (empty($entry['sourceIcon'])) {
$entry['sourceIcon'] = 'fa fa-ticket';
}
$History->addComment(
$entry['message'],
$entry['time'],
$entry['source'],
$entry['sourceIcon'],
$entry['id'],
$Booking->getUuid()
);
}
}
}
/**
* @return array
*/
public function getBookings(): array
{
if (!QUI::getPackageManager()->isInstalled('quiqqer/booking')) {
return [];
}
try {
$bookings = QUI::getDatabase()->fetch([
'select' => 'uuid,globalProcessId,createDate',
'from' => BookingTable::BOOKINGS->tableName(),
'where_or' => [
'globalProcessId' => $this->processId,
'uuid' => $this->processId
]
]);
} catch (\Exception) {
return [];
}
$result = [];
$BookingRepository = new QUI\ERP\Booking\Repository\BookingRepository();
foreach ($bookings as $booking) {
try {
$result[] = $BookingRepository->getByUuid($booking['uuid']);
} catch (\Exception) {
}
}
return $result;
}
//endregion
//region purchase / Einkauf
protected function parsePurchasing(Comments $History): void
{
// orders
$purchasing = $this->getPurchasing();
foreach ($purchasing as $Purchasing) {
$History->addComment(
QUI::getLocale()->get('quiqqer/erp', 'process.history.purchasing.created', [
'hash' => $Purchasing->getHash()
]),
strtotime($Purchasing->getAttribute('c_date')),
'quiqqer/purchasing',
'fa fa-cart-arrow-down',
false,
$Purchasing->getHash()
);
$history = $Purchasing->getHistory()->toArray();
foreach ($history as $entry) {
if (empty($entry['source'])) {
$entry['source'] = 'quiqqer/purchasing';
}
if (empty($entry['sourceIcon'])) {
$entry['sourceIcon'] = 'fa fa-cart-arrow-down';
}
$History->addComment(
$entry['message'],
$entry['time'],
$entry['source'],
$entry['sourceIcon'],
$entry['id'],
$Purchasing->getHash()
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
);
}
}
}
/**
* @return QUI\ERP\Purchasing\Processes\PurchasingProcess[]
*/
public function getPurchasing(): array
{
if (!QUI::getPackageManager()->isInstalled('quiqqer/purchasing')) {
return [];
}
try {
$purchasing = QUI::getDatabase()->fetch([
'select' => 'id,hash,global_process_id,date',
'from' => PurchasingHandler::getTablePurchasingProcesses(),
'where_or' => [
'global_process_id' => $this->processId,
'hash' => $this->processId
]
]);
} catch (\Exception) {
return [];
}
$result = [];
foreach ($purchasing as $process) {
try {
$result[] = PurchasingHandler::getPurchasingProcess($process['id']);
} catch (\Exception) {
}
}
return $result;
}
//endregion
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
//region sales orders / Aufträge
protected function parseSalesOrders(Comments $History): void
{
// orders
$salesOrders = $this->getSalesOrders();
foreach ($salesOrders as $SalesOrder) {
$History->addComment(
QUI::getLocale()->get('quiqqer/erp', 'process.history.salesorders.created', [
'hash' => $SalesOrder->getHash()
]),
strtotime($SalesOrder->getAttribute('c_date')),
'quiqqer/salesorders',
'fa fa-suitcase',
false,
$SalesOrder->getHash()
);
$history = $SalesOrder->getHistory()->toArray();
foreach ($history as $entry) {
if (empty($entry['source'])) {
$entry['source'] = 'quiqqer/salesorders';
}
if (empty($entry['sourceIcon'])) {
$entry['sourceIcon'] = 'fa fa-suitcase';
}
$History->addComment(
$entry['message'],
$entry['time'],
$entry['source'],
$entry['sourceIcon'],
$entry['id'],
$SalesOrder->getHash()
);
}
}
}
/**
* @return QUI\ERP\Purchasing\Processes\PurchasingProcess[]
*/
public function getSalesOrders(): array
{
if (!QUI::getPackageManager()->isInstalled('quiqqer/salesorders')) {
return [];
}
try {
$salesOrders = QUI::getDatabase()->fetch([
'select' => 'id,hash,global_process_id,date',
'from' => SalesOrdersHandler::getTableSalesOrders(),
'where_or' => [
'global_process_id' => $this->processId,
'hash' => $this->processId
]
]);
} catch (\Exception) {
return [];
}
$result = [];
foreach ($salesOrders as $salesOrder) {
try {
$result[] = SalesOrdersHandler::getSalesOrder($salesOrder['id']);
} catch (\Exception) {
}
}
return $result;
}
//endregion
//region transactions
/**
* @return bool
*/
{
$transactions = $this->getTransactions();
}
/**
* Return all related transactions
*
* @return QUI\ERP\Accounting\Payments\Transactions\Transaction[];
*/
{
try {
QUI::getPackage('quiqqer/payment-transactions');
} catch (QUI\Exception $Exception) {
return [];
}
if ($this->transactions !== null) {
return $this->transactions;
}
$Transactions = QUI\ERP\Accounting\Payments\Transactions\Handler::getInstance();
$this->transactions = $Transactions->getTransactionsByProcessId($this->processId);
return $this->transactions;
}
//endregion
}