%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/html/bbw/farmaci/kritik-portale/module/ReviewBase/src/ReviewBase/Model/
Upload File :
Create Path :
Current File : /var/www/html/bbw/farmaci/kritik-portale/module/ReviewBase/src/ReviewBase/Model/ContractFactory.php

<?php
namespace ReviewBase\Model;

use Basis\EventBus\EventBus;
use DateInterval;
use DateTime;
use DateTimeZone;
use Exception;
use ReviewBase\DB\TableGateway\Feature\BillingDeployFeature;
use ReviewBase\DB\TableGateway\Feature\BillingSyncFeature;
use ReviewBase\Entity\ContractEntity;
use ReviewBase\Repository\ContractRepository;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\HydratingResultSet;
use Zend\Db\Sql\Predicate\Expression;
use Zend\Db\Sql\Predicate\Predicate;
use Zend\Db\Sql\Predicate\PredicateSet;
use Zend\Db\Sql\Select;
use Zend\Db\TableGateway\TableGateway;
use Zend\Stdlib\Hydrator\ArraySerializable;

class ContractFactory
{
    protected $serviceLocator;

    protected $eventBus;
    
    protected $entitytypeName;

    protected $repository;
    
    protected $contractTableGateway;
    
    protected $viewsDBAdapter;
    
    protected $defaultDateFormat;
    protected $defaultDateTimeFormat;
    protected $defaultDateTimeStorageFormat;
    protected $utcTimezone;
    protected $applicationTimezone;

    public function __construct(EventBus $eventBus, Adapter $systemDBAdapter, Adapter $viewsDBAdapter)
    {
        //$this->serviceLocator = $serviceLocator;
        $this->eventBus = $eventBus;
        $this->entitytypeName = 'contract';
        $this->viewsDBAdapter = $viewsDBAdapter;
        $this->repository = new ContractRepository($eventBus, $systemDBAdapter, $this->entitytypeName);
        $this->contractTableGateway = new TableGateway($this->entitytypeName, $viewsDBAdapter,
            null,
            new HydratingResultSet(
                new ArraySerializable(),
                new ContractEntity()
            ));
        
        $this->defaultDateFormat = "d.m.Y";
        $this->defaultDateTimeFormat = "d.m.Y H:i:s";
        $this->defaultDateTimeStorageFormat = "Y-m-d H:i:s";
        $this->utcTimezone = new DateTimeZone('UTC');
        $applicationTimezone = date_default_timezone_get();
        $this->applicationTimezone = new DateTimeZone($applicationTimezone);
    }

    /**
     * @param $entityid
     * @return Contract
     */
    public function getContract($entityid)
    {
        $contract = $this->repository->load($entityid);
        return $contract;
    }
    
    public function getContractForObject($objectid)
    {
        $result = $this->contractTableGateway->select(['objectid' => $objectid]);
        $contractEntity = $result->current();
        if($contractEntity)
        {
            $contract = $this->repository->load($contractEntity->entityid);
            return $contract;
        }
        return false;
    }
    
    public function getContractByExternalHash($external_hash)
    {
        $result = $this->contractTableGateway->select(['external_subscription_hash' => $external_hash]);
        $contractEntity = $result->current();
        if($contractEntity)
        {
            $contract = $this->repository->load($contractEntity->entityid);
            return $contract;
        }
        return false;
    }
    
    public function getContractsForPlanUpdate()
    {
        $now = new DateTime();
        $interval1Day = new DateInterval('P1D');
        $oneDayAgo = $now->sub($interval1Day);
        $currentDate = new DateTime();
//        $predicate = new Predicate([new Expression("UNIX_TIMESTAMP(plan_start) BETWEEN UNIX_TIMESTAMP(CURDATE()) AND UNIX_TIMESTAMP(DATE_ADD(CURDATE(), INTERVAL + 86399 SECOND))")]);
//        echo "One day ago: ".$oneDayAgo->format('Y-m-d H:i:s').PHP_EOL;
//        echo "Now: ".$currentDate->format('Y-m-d H:i:s').PHP_EOL;
        //$predicate->between('plan_start', $oneDayAgo->format('Y-m-d H:i:s'), $currentDate->format('Y-m-d H:i:s'));
//        $startDate = new DateTime("today 00:00:00");
        $startDateUTC = new DateTime($this->convertDateTime("today 00:00:00", $this->applicationTimezone, $this->utcTimezone));
//        $endDate = new DateTime("today 23:59:59");
        $endDateUTC = new DateTime($this->convertDateTime("today 23:59:59", $this->applicationTimezone, $this->utcTimezone));
//        echo "Now: ".$currentDate->format('Y-m-d H:i:s').PHP_EOL;
//        echo "Now: utc ".$this->convertDateTime($currentDate->format('Y-m-d H:i:s'), $this->applicationTimezone, $this->utcTimezone).PHP_EOL;
//        echo "startDate: utc ".$startDateUTC->format('Y-m-d H:i:s').PHP_EOL;
//        echo "endDate: utc ".$endDateUTC->format('Y-m-d H:i:s').PHP_EOL;
//        echo "startDate: ".$startDate->format('Y-m-d H:i:s').PHP_EOL;
//        echo "endDate: ".$endDate->format('Y-m-d H:i:s').PHP_EOL;
        $predicate = new Predicate();
        $predicate->between('plan_start', $startDateUTC->format('Y-m-d H:i:s'), $endDateUTC->format('Y-m-d H:i:s'));
        
        $predicateSet = new PredicateSet();
        $predicate2 = new Predicate();
        $predicate2->greaterThan('plan_end', $now->format('Y-m-d H:i:s'));
        $predicateSet->andPredicate($predicate);
        $predicateSet->andPredicate($predicate2);
        
//        $sql = $this->contractTableGateway->getSql();
//        $exp = "UNIX_TIMESTAMP(plan_start) BETWEEN UNIX_TIMESTAMP(".$currentDate->format('Y-m-d').") AND UNIX_TIMESTAMP(DATE_ADD(".$currentDate->format('Y-m-d').", INTERVAL + 86399 SECOND))";
//        $select = $sql->select()->where(new Expression($exp));
//        echo $select->getSqlString($this->viewsDBAdapter->getPlatform());
        $contracts = $this->contractTableGateway->select($predicateSet);
        return $contracts;
    }
    
    public function getContractsForPlanDowngrade()
    {
        $currentDate = new DateTime();
        $startDateUTC = new DateTime($this->convertDateTime("9 days ago", $this->applicationTimezone, $this->utcTimezone));
        $endDateUTC = new DateTime($this->convertDateTime("7 days ago", $this->applicationTimezone, $this->utcTimezone));
//        echo "Now: ".$currentDate->format('Y-m-d H:i:s').PHP_EOL;
//        echo "Now: utc ".$this->convertDateTime($currentDate->format('Y-m-d H:i:s'), $this->applicationTimezone, $this->utcTimezone).PHP_EOL;
//        echo "startDate: utc ".$startDateUTC->format('Y-m-d H:i:s').PHP_EOL;
//        echo "endDate: utc ".$endDateUTC->format('Y-m-d H:i:s').PHP_EOL;
        $predicate = new Predicate();
        $predicate->between('plan_end', $startDateUTC->format('Y-m-d H:i:s'), $endDateUTC->format('Y-m-d H:i:s'));
        $contracts = $this->contractTableGateway->select($predicate);
        return $contracts;
    }
    
    public function getContracts()
    {
        $contracts = $this->contractTableGateway->select();
        return $contracts;
    }
    
    public function getContractsForSync()
    {
        $contracts = $this->contractTableGateway->select(['status' => BillingSyncFeature::STATUS_SYNC]);
        return $contracts;
    }
    
    public function getTaskContracts($status = BillingSyncFeature::STATUS_SYNC, $interval = "hourly")
    {
        //@todo select all tasks load contracts
        $select = $this->contractTableGateway->getSql()->select();
        $columns = [
            "cron_task_status" => "status",
            "cron_task_id" => "id",
            "cron_task_type" => "type",
            "cron_task_entityid" => "entityid",
            "cron_task_interval" => "interval"
        ];
        $select->join(['ct' => 'cron_task'], 'ct.entityid = contract.entityid', $columns, 'right');
        $predicateSet = new PredicateSet();
        $predicate = new Predicate();
        $predicate->equalTo("ct.status", $status);
        $predicate2 = new Predicate();
        $predicate2->equalTo("ct.interval", $interval);
        $predicate3 = new Predicate();
        $predicate3->equalTo('ct.type', 'contract');
        $predicateSet->andPredicate($predicate);
        $predicateSet->andPredicate($predicate2);
        $predicateSet->andPredicate($predicate3);
        $select->where($predicateSet);

        $resultSet = $this->contractTableGateway->selectWith($select);
//        foreach($resultSet as $contract)
//        {
//            var_dump($contract);
//        }
//        var_dump(count($resultSet));

        return $resultSet;
    }

    public function getContractsForDeploy()
    {
        $contracts = $this->contractTableGateway->select(['status' => BillingDeployFeature::STATUS_DEPLOY]);
        return $contracts;
    }
    
    public function getContractsForExternalCustomerID($customer_number)
    {
        $contractsForCustomer = $this->contractTableGateway->select(['customer_number' => $customer_number]);
        return $contractsForCustomer;
    }
    
    public function getCustomerHash($userid)
    {
        $customerHash = null;
        $contractsForUser = $this->contractTableGateway->select(['userid' => $userid]);
        foreach ($contractsForUser as $contract)
        {
            if(!is_null($customerHash) && $customerHash !== $contract->external_customer_hash)
            {
                throw new Exception("More than one Fastbill Customer Hash found. Can't recover.");
            }
            $customerHash = $contract->external_customer_hash;
        }
        return $customerHash;
    }
    
    public function getUserIDForCustomerHash($customerHash)
    {
        $userID = null;
        $contractsForUser = $this->contractTableGateway->select(['external_customer_hash' => $customerHash]);
        foreach ($contractsForUser as $contract)
        {
            if(!is_null($userID) && $userID !== $contract->userid)
            {
                throw new Exception("More than one UserID found for External Customer Hash. Can't recover.");
            }
            $userID = $contract->userid;
        }
        return $userID;
    }
    
    protected function convertDateTime($datetime, $fromTimezone, $toTimezone, $format = 'Y-m-d H:i:s')
    {
        $convertedDateTime = null;
        if ($datetime === "")
        {
            //catch the empty strings
            $datetime = null;
        }
        if (!is_null($datetime))
        {
            $dt = new DateTime($datetime, $fromTimezone);
            $dt->setTimezone($toTimezone);
            $convertedDateTime = $dt->format($format);
        }
        return $convertedDateTime;
    }
    
    /**
     * @param $entityid
     * @return Contract
     */
    public function delete($entityid)
    {
        $contract = $this->repository->load($entityid);
        $contract = $this->repository->delete($contract);
        return $contract;
    }

    /**
     * @param Contract $contract
     */
    public function save(Contract $contract)
    {
        $this->repository->save($contract);
    }
} 

Zerion Mini Shell 1.0