<?php
 namespace App\Entity\Utils; class PivotDocumentsList { private $pivot; private $idPivot; public function __construct($pivot, $idPivot) { if ($pivot!='order' and $pivot!='service' and $pivot!='invoice' and $pivot!='cashing' and $pivot!='payment' and $pivot != 'cashBookDoc' and $pivot != 'importedService' and $pivot != 'importedDocument' and $pivot != 'balance') { throw new \Exception('The document pivot is not valid - '.$pivot); } $this->pivot=$pivot; $this->idPivot=$idPivot; } public function getDocumentsList($selectForUpdate) { if (!is_numeric($this->idPivot) or $this->idPivot<=0) { return []; } $dlObj=new \App\EntityList\Documents(); $dlObj->setFilter('doc.pivot', '=', $this->pivot); $dlObj->setFilter('doc.idPivot', '=', $this->idPivot); if ($this->pivot == 'cashBookDoc') { $cashBookDate = \Entity\CashBookDocument::select($this->idPivot)->date; $tableMonth = \Engine\DateTime::createFromIsoDate($cashBookDate)->format('Ym'); $startIndex = \App\Utils\Db\TablesUtils::getInstance()->getStartIndexFromYm($tableMonth); $endIndex = \App\Utils\Db\TablesUtils::getInstance()->getEndIndexFromYm($tableMonth); $dlObj->setFilter('doc.id', 'BETWEEN', [$startIndex, $endIndex]); } if ($this->pivot == 'importedService') { $tableMonth = \Engine\DateTime::createFromIsoDatetime(\Entity\ImportedService::select($this->idPivot)->addTime)->format('Ym'); $startIndex = \App\Utils\Db\TablesUtils::getInstance()->getStartIndexFromYm($tableMonth); $endIndex = \App\Utils\Db\TablesUtils::getInstance()->getEndIndexFromYm($tableMonth); $dlObj->setFilter('doc.id', 'BETWEEN', [$startIndex, $endIndex]); } if ($this->pivot == 'importedDocument') { $tableMonth = \Engine\DateTime::createFromIsoDatetime(\Entity\ImportedDocument::select($this->idPivot)->addTime)->format('Ym'); $startIndex = \App\Utils\Db\TablesUtils::getInstance()->getStartIndexFromYm($tableMonth); $endIndex = \App\Utils\Db\TablesUtils::getInstance()->getEndIndexFromYm($tableMonth); $dlObj->setFilter('doc.id', 'BETWEEN', [$startIndex, $endIndex]); } $dlObj->applyUserRestriction(false); $dlObj->setColumns(['doc.docType', 'doc.idTemplate', 'doc.format', 'doc.docNumber', 'doc.docDate', 'doc.docFile', 'doc.preview', 'doc.generation', 'doc.comments', 'doc.fieldsDetails', 'doc.history', 'doc.extraInfo']); $dlObj->setForUpdate($selectForUpdate); $dlObj->setOrderBy('doc.id', 'ASC'); $docsList=$dlObj->getExecution(); $finalList=[]; $genCount=[]; foreach($docsList as $doc) { $idDoc=$doc['id']; $finalList[$idDoc]=$doc; if ($doc['fieldsDetails']) { $finalList[$idDoc]['fieldsDetails']=unserialize($doc['fieldsDetails']); } else { $finalList[$idDoc]['fieldsDetails']=[]; } if ($doc['extraInfo']) { $ei=unserialize($doc['extraInfo']); } else { $ei=[]; } $finalList[$idDoc]['extraInfo']=$ei; unset($finalList[$idDoc]['id']); $finalList[$idDoc]['history']=unserialize($doc['history']); if ($ei['servicesList']) { $finalList[$idDoc]['servicesList']=$ei['servicesList']; } $finalList[$idDoc]['voided']=(Boolean)$ei['voided']; if ($ei['idDocumentSeries']) { $finalList[$idDoc]['idDocumentSeries']=$ei['idDocumentSeries']; } if (!$doc['preview'] and $doc['generation']=='auto') { $genCount[$doc['docType']]++; } $finalList[$idDoc]['genCount']=$genCount[$doc['docType']]; $finalList[$idDoc]['printed']=false; if ($finalList[$idDoc]['history']) { foreach($finalList[$idDoc]['history'] as $item) { if ($item['op']=='p') { $finalList[$idDoc]['printed']=true; break; } } } $finalList[$idDoc]['idAddUser']=$finalList[$idDoc]['history'][0]['us']; $finalList[$idDoc]['addTime']=$finalList[$idDoc]['history'][0]['t']; } return $finalList; } }