Skip to main content

Barcodetrennung mit ausschließen der Trenner-Seite

In folgendem Ordner der Squeeze Installation: SQUEEZE\htdocs\repository\<Mandantenname>\UserExits\Global\


muss die PHP-Datei BarcodeSeparation.php liegen.

Unter Barcode Types sind die möglichen Barcode-Typen aufgeführt welche verwendet werden können. Diese können dann über die Stapelklasseneigenschaften konfiguriert werden.

<?php

use Squeeze\SqueezeConfig;
use Squeeze\xBatchClass;
use Squeeze\xDoc;
use Squeeze\xQueueEntry;
use Squeeze\xReturnObject;
use Squeeze\xTools;

/**
 * @param xDoc $xDoc
 * @param string $nextStep
 * @param string $newStatus
 * @return xReturnObject
 * @throws Exception
 */
function BarcodeSeparation(xDoc $xDoc, string $nextStep, string $newStatus): xReturnObject
{
    $logger = Logger::getLogger("main");

    // =============================================
    // Get Barcode Engine from Batch Class Settings
    // =============================================
    $isBarcodeConfigured = true;
    $xQueueEntry = new xQueueEntry();
    $xQueueEntry->getByxDocId($xDoc->id);
    $batchClass = new xBatchClass();
    $batchClass->getById($xQueueEntry->batchclassid);
    $splitBarcodeType = strtolower(trim($batchClass->getSettingValue('SplitBarcodeType')));
    $splitBarcodePattern = $batchClass->getSettingValue('SplitBarcodePattern');
    $splitFixPages = intval(trim($batchClass->getSettingValue('SplitFixPages')));

    if ($splitBarcodeType === null or $splitBarcodeType == '') {
        $isBarcodeConfigured = false;
        //$logger->warn('DocId = '.$xDoc->id.' Barcode Split not possible because SplitBarcodeType is missing!');
    } else {
        if ($splitBarcodePattern === null or $splitBarcodePattern == '') {
            $isBarcodeConfigured = false;
            //$logger->warn('DocId = '.$xDoc->id.' Barcode Split not possible because SplitBarcodePattern is missing!');
        }
    }

    if ($isBarcodeConfigured === true) {
        // get splits by barcode and return the result
        return splitByBarcode($xDoc, $logger, $splitBarcodeType, $splitBarcodePattern);
    }

    if ($splitFixPages > 0) {
        // get splits by fixed pages and return the result
        return splitFixPages($xDoc, $logger, $splitFixPages);
    }

    // return an empty split result
    $emptySplit = array();
    return new xReturnObject(false, 400, 'No Split configured', $emptySplit);
}

/**
 * @param xDoc $xDoc
 * @param Logger $logger
 * @param int $fixedPageSize
 * @return xReturnObject
 * @throws Exception
 */
function splitFixPages(xDoc $xDoc, Logger $logger, int $fixedPageSize): xReturnObject
{
    $config = new SqueezeConfig();
    $repo = $config->get("repository.work");
    $splits = array();
    $keepFieldValues = array();
    $absolutePath = xTools::buildAbsolutePath($repo, $xDoc->repoPath, false);
    $files = array_diff(scandir($absolutePath . "Viewer"), array('.', '..'));
    $currentPage = 0;
    foreach ($files as $page => $file){
        $currentPage++;
        $modulo = $currentPage % $fixedPageSize;
        if ($modulo === 0) {
            $logger->debug('DocId = '.$xDoc->id.' New split at page '.$currentPage.' because of fixed page size');
            $splits[$currentPage][] = ['page' => $currentPage, 'type' => 'FixedPageSplit', 'value' => 'FixedPageSplit-' . $fixedPageSize, 'fields' => $keepFieldValues, 'suppressOcr' => false];
        }
    }
    return new xReturnObject(true,200, 'Fixed Page Split', $splits);
}

/**
 * @param xDoc $xDoc
 * @param Logger $logger
 * @param string $splitBarcodeType
 * @param string $splitBarcodePattern
 * @return xReturnObject
 */
function splitByBarcode(xDoc $xDoc, Logger $logger, string $splitBarcodeType, string $splitBarcodePattern): xReturnObject
{

    // ==========================================
    // Barcode Types
    // ==========================================
    //  AZTEC
    //  CODABAR
    //  CODE_39
    //  CODE_93
    //  CODE_128
    //  COMPOSITE
    //  DATABAR
    //  DATA_MATRIX
    //  DATABAR_EXP
    //  EAN_2
    //  EAN_5
    //  EAN_8
    //  EAN_13
    //  ITF
    //  ISBN_10
    //  ISBN_13
    //  MAXICODE
    //  PDF_417
    //  QR_CODE
    //  RSS_14
    //  RSS_EXPANDED
    //  UPC_A
    //  UPC_E
    //  UPC_EAN_EXTENSION
    //  UNKNOWN

    $currentSplit = 0;
    $suppressOCR = 0;
    $splits = array();
    $keepFieldValues = array();

    $splitBarcodeType = str_replace(' ', '', $splitBarcodeType);
    $splitBarcodeType = trim($splitBarcodeType, ',;');
    $arrSplitBarcodeTypes = explode(';', $splitBarcodeType);

    $logger->debug('DocId = '.$xDoc->id.' Execute Barcode-Split');

    foreach ($xDoc->barcodes as $page => $barcodes){

        $logger->debug('DocId = '.$xDoc->id.' Checking Barcodes for Page '. $page);
        $barcodeType = '';
        $barcodeValue = '';
		$isSplitPage = false;
		
        if (count($barcodes) == 0){
            if($page == 1){
                $logger->debug('DocId = '.$xDoc->id.' No Barcode on Page '. $page . '. Split because it is the first page');
                $currentSplit = $page;
            }
        }
        else {
            foreach ($barcodes as $barcode){
                foreach ($arrSplitBarcodeTypes as $splitBarcodeType){
                    if(strtolower($barcode['type']) == strtolower($splitBarcodeType)) {
                        $matches = null;
                        xTools::mb_preg_match_all("/$splitBarcodePattern/i", $barcode['value'], $matches, PREG_OFFSET_CAPTURE);
                        $hit = $matches[0];
                        foreach ($hit as $match) {
                            if ($match[0] == '') continue;
                            $logger->debug('DocId = '.$xDoc->id.' Start Split here! Barcode "'.$matchmatch[0].'" of type "'.$barcode['type'].'" found on page '. $page);
                            //$suppressOCR = 1;
                            $currentSplit = $page - 1;
                            $barcodeType = $barcode['type'];
                            $barcodeValue = $barcode['value'];
							$isSplitPage = true;

                            // Next Page
                            break 3;
                        }
                    }
                }
            }
        }
        // add page
        //$splits[$currentSplit][] = ['page' => $page, 'type' => $barcodeType, 'value' => $barcodeValue, 'fields' => $keepFieldValues, 'suppressOcr' => $suppressOCR];
		if(!$isSplitPage) {
			$splits[$currentSplit][] = ['page' => $page, 'type' =>$barcodeType, 'value' => $barcodeValue];
		} else {
			$logger->debug("IS SPLIT PAGE - DO NOT ADD PAGE $page");
		}
    }

    return new xReturnObject(true,200, 'Barcode Split', $splits);
}