Example #1
0
<?php

if (!array_key_exists('action', $_REQUEST)) {
    $_REQUEST['action'] = null;
}
$this->getTemplate()->setBlock('head', 'person/head.phtml');
$this->getTemplate()->setBlock('middle', 'person/middle.phtml');
$this->getTemplate()->setBlock('footer_a', 'person/footer_a.phtml');
$this->getTemplate()->setBlock('footer', 'person/footer.phtml');
switch ($_REQUEST['action']) {
    case 'jExponentList':
        $personColl = new \crm\PersonColl($GLOBALS['db']);
        $personColl->loadAll(array_merge(array('page' => 0, 'count' => 10), $_REQUEST));
        $result = array('count' => $personColl->countAll($_REQUEST), 'data' => array());
        foreach ($personColl->getItems() as $person) {
            $result['data'][] = $person->getData();
        }
        header('Content-Type: application/json');
        echo json_encode($result);
        exit;
        break;
    case 'edit':
        $person = new \crm\Person($GLOBALS['db']);
        $person->loadFromId($_REQUEST['codFisc']);
        $this->getTemplate()->setObjectData($person);
        $this->getTemplate()->setBlock('middle', 'person/edit.phtml');
        $this->getTemplate()->setBlock('footer', 'person/footer.phtml');
        $this->getTemplate()->setBlock('footer_a', 'person/empty.phtml');
        break;
}
Example #2
0
    public function import($statOnly = '')
    {
        $dateFilter = '`data_ora_rich` > CURDATE() - INTERVAL 2 MONTH';
        if ($statOnly != '') {
            $dateFilter = 'TRUE';
            if ($this->db->config->storeExpiryStatFile->Sksocio != '') {
                $statHandler = fopen($this->db->config->storeExpiryStatFile->Sksocio, 'w');
            }
        }
        $archiviRichieste = array(array('dir' => 'risposteBaseDirMass', 'sql' => '
SELECT aps_prod_backup.eps_richiesta_mass.id_richiesta,
aps_prod_backup.eps_richiesta_mass.id_mov_contab,
aps_prod_backup.eps_richiesta_mass.id_richiesta_cache,
aps_prod_backup.eps_richiesta_mass.stringa_richiesta,
aps_prod_backup.eps_richiesta_mass.data_ora_rich,
aps_prod_backup.eps_richiesta_mass.data_ora_evas,
aps_prod.eps_operazione.cod_operazione as cod_operazione ,
aps_prod_backup.eps_mov_contab_mass.cod_causale as cod_causale ,
aps_prod_backup.eps_mov_contab_mass.importo as importo ,
aps_prod_backup.eps_mov_contab_mass.id_mov_contab_gen as id_mov_contab_gen ,
aps_prod.eps_prodotto.cod_prodotto as cod_prodotto ,
aps_prod.eps_cliente.cod_cliente as cod_cliente ,
aps_prod.eps_istanza.cod_istanza as cod_istanza
FROM aps_prod_backup.eps_richiesta_mass
LEFT JOIN aps_prod.eps_operazione ON aps_prod.eps_operazione.id_operazione=aps_prod_backup.eps_richiesta_mass.id_operazione
LEFT JOIN aps_prod_backup.eps_mov_contab_mass ON aps_prod_backup.eps_mov_contab_mass.id_mov_contab=aps_prod_backup.eps_richiesta_mass.id_mov_contab
LEFT JOIN aps_prod.eps_prodotto ON aps_prod.eps_prodotto.id_prodotto =aps_prod_backup.eps_mov_contab_mass.id_prodotto
LEFT JOIN aps_prod.eps_cliente ON aps_prod.eps_cliente.id_cliente=aps_prod_backup.eps_mov_contab_mass.id_cliente
LEFT JOIN aps_prod.eps_istanza ON aps_prod.eps_istanza.id_istanza=aps_prod.eps_cliente.id_istanza
WHERE
' . $dateFilter . ' AND
aps_prod.eps_operazione.cod_operazione = "sksocio" OR aps_prod.eps_operazione.cod_operazione = "sksociosto"
                    '), array('dir' => 'risposteBaseDir', 'sql' => '
SELECT aps_prod_backup.eps_richiesta.id_richiesta,
aps_prod_backup.eps_richiesta.id_mov_contab,
aps_prod_backup.eps_richiesta.id_richiesta_cache,
aps_prod_backup.eps_richiesta.stringa_richiesta,
aps_prod_backup.eps_richiesta.data_ora_rich,
aps_prod_backup.eps_richiesta.data_ora_evas,
aps_prod.eps_operazione.cod_operazione as cod_operazione ,
aps_prod_backup.eps_mov_contab.cod_causale as cod_causale ,
aps_prod_backup.eps_mov_contab.importo as importo ,
aps_prod_backup.eps_mov_contab.id_mov_contab_gen as id_mov_contab_gen ,
aps_prod.eps_prodotto.cod_prodotto as cod_prodotto ,
aps_prod.eps_cliente.cod_cliente as cod_cliente ,
aps_prod.eps_istanza.cod_istanza as cod_istanza

FROM aps_prod_backup.eps_richiesta
LEFT JOIN aps_prod.eps_operazione ON aps_prod.eps_operazione.id_operazione=aps_prod_backup.eps_richiesta.id_operazione
LEFT JOIN aps_prod_backup.eps_mov_contab ON aps_prod_backup.eps_mov_contab.id_mov_contab=aps_prod_backup.eps_richiesta.id_mov_contab
LEFT JOIN aps_prod.eps_prodotto ON aps_prod.eps_prodotto.id_prodotto =aps_prod_backup.eps_mov_contab.id_prodotto
LEFT JOIN aps_prod.eps_cliente ON aps_prod.eps_cliente.id_cliente=aps_prod_backup.eps_mov_contab.id_cliente
LEFT JOIN aps_prod.eps_istanza ON aps_prod.eps_istanza.id_istanza=aps_prod.eps_cliente.id_istanza
WHERE
' . $dateFilter . ' AND
aps_prod.eps_operazione.cod_operazione = "sksocio" OR aps_prod.eps_operazione.cod_operazione = "sksociosto"
                    '));
        $totalCount = 0;
        foreach ($archiviRichieste as $archivioRichieste) {
            $richieste = $this->db->query($archivioRichieste['sql'], \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
            $count = 0;
            $groupCount = 0;
            $rCompany = new \crm\Company($this->db);
            $company = new \crm\Company($this->db);
            $person = new \crm\Person($this->db);
            while (is_object($richiesta = $richieste->current())) {
                echo "\r" . $count++ . '/' . ($richieste->count() - 1) . str_repeat(' ', 5);
                $dati = array();
                $imprese = array();
                $esponenti = array();
                $sId = str_pad($richiesta->id_richiesta, 9, '0', STR_PAD_LEFT);
                $dM = 'M' . substr($sId, 0, 3);
                $dK = 'K' . substr($sId, 3, 3);
                $nomeFile = $this->db->config->{$archivioRichieste['dir']} . "/{$dM}/{$dK}/F{$sId}.xml";
                if (!is_file($nomeFile)) {
                    $richieste->next();
                    continue;
                }
                $dati['id_mov_contab'] = $richiesta->id_mov_contab;
                $dati['massiva'] = $archivioRichieste['dir'] == 'risposteBaseDir' ? 0 : 1;
                $dati['id_richiesta'] = $richiesta->id_richiesta;
                $dati['test_nome_file'] = "\"" . str_replace('"', '\\"', basename($nomeFile)) . "\"";
                $dati['errore'] = '';
                $attesa = $this->db->query('
SELECT time FROM aps_stat.operaz WHERE
datetime="' . $richiesta->data_ora_evas . '" AND
operaz="daim.' . $richiesta->cod_operazione . '"
', \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
                $attesa = $attesa->current();
                $dati['data_ora_rich'] = $richiesta->data_ora_rich;
                $dati['data_ora_evas'] = $richiesta->data_ora_evas;
                $dati['cod_operazione'] = $richiesta->cod_operazione;
                $dati['cod_prodotto'] = $richiesta->cod_prodotto;
                $dati['cod_cliente'] = $richiesta->cod_cliente;
                $dati['cod_istanza'] = $richiesta->cod_istanza;
                $dati['importo'] = $richiesta->importo;
                $dati['differita'] = 1;
                if ($richiesta->importo == 0 && $richiesta->id_richiesta_cache == 0) {
                    $richieste->next();
                    continue;
                }
                $sXML = new \SimpleXMLElement($nomeFile, 0, true);
                $dati['dataCerved'] = '';
                $dateObject = date_create_from_format('Y m d H:i:s', (string) $sXML->xpath('/s2xResponse/s2xStatus/s2xTime')[0]);
                if (is_object($dateObject)) {
                    $dati['dataCerved'] = date_format($dateObject, 'Y-m-d H:i:s');
                }
                $sobject = $sXML->xpath('/s2xResponse/s2xData/Shareholder/Identification');
                if (!is_array($sobject) || sizeof($sobject) < 1) {
                    $richieste->next();
                    continue;
                }
                $sobject = $sobject[0];
                $dati['nome'] = '';
                $dati['cognome'] = '';
                preg_match("/^(\\w+)/", $sobject->LastNameFirstName, $matches);
                if (sizeof($matches) > 0) {
                    $cognome = $matches[1];
                    $nome = trim(str_replace($cognome, '', $sobject->LastNameFirstName));
                    $dati['nome'] = $nome;
                    $dati['cognome'] = $cognome;
                }
                $dati['codFisc'] = (string) $sobject->TaxCode;
                $dati['dataNas'] = '';
                if (property_exists($sobject, 'IndividualAddedValue')) {
                    $dateObject = date_create_from_format('Ymd', (string) $sobject->IndividualAddedValue->BirthInformation->BirthData);
                    if (is_object($dateObject)) {
                        $dati['dataNas'] = (string) date_format($dateObject, 'Y-m-d');
                    }
                }
                if (strlen($dati['codFisc']) == 16) {
                    $person->setData(array());
                    $person->loadFromId($dati['codFisc']);
                    $cDati = $person->getData();
                    if (is_array($cDati) && count($cDati) > 0) {
                        $person->setData(array_merge($cDati, array_filter($dati)));
                        $person->update();
                    } else {
                        $person->setData($dati);
                        $person->insert();
                    }
                } else {
                    if ($dati['codFisc'] != '') {
                        $dati['partIVA'] = $dati['codFisc'];
                        $dati['ragSoc'] = (string) $sobject->CompanyName;
                        $dati['provCCIAA'] = (string) $sobject->REAProvinceCode;
                        $dati['nCCIAA'] = (string) $sobject->REARegistrationDate;
                        $dati['formaGiurid'] = (string) $sobject->CompanyTitle;
                        $dati['capitaleSociale'] = (string) $sobject->EquityCapital->Subscribed;
                        if (property_exists($sobject, 'LegalFormAddedValue')) {
                            $dati['statoAttivita'] = (string) $sobject->LegalFormAddedValue->BusinessStatus->OtherDescription;
                            $dati['gruppo'] = (string) $sobject->LegalFormAddedValue->MemberOfGroup3->NameMemberOfGroup3;
                        }
                        $rCompany->setData(array());
                        $rCompany->loadFromId($dati['partIVA']);
                        $cDati = $rCompany->getData();
                        if (is_array($cDati) && count($cDati) > 0) {
                            $rCompany->setData(array_merge($cDati, array_filter($dati)));
                            $rCompany->update();
                        } else {
                            $rCompany->setData($dati);
                            $rCompany->insert();
                        }
                    }
                }
                restore_error_handler();
                if ($statOnly != '') {
                    array_walk($dati, create_function('&$item', '$item=str_replace("\\"","",$item);'));
                    if (isset($statHandler)) {
                        if (ftell($statHandler) == 0) {
                            fputcsv($statHandler, array_keys($dati));
                        }
                        fputcsv($statHandler, $dati);
                        fflush($statHandler);
                    }
                    $richieste->next();
                    continue;
                }
                foreach ($sXML->xpath('/s2xResponse/s2xData/ShareholderSection/ListLatestPartnership/LatestPartnership') as $related) {
                    $impresa = array();
                    $impresa['id_mov_contab'] = $richiesta->id_mov_contab;
                    $impresa['massiva'] = $archivioRichieste['dir'] == 'risposteBaseDir' ? 0 : 1;
                    $impresa['id_richiesta'] = $richiesta->id_richiesta;
                    $impresa['test_nome_file'] = "\"" . str_replace('"', '\\"', basename($nomeFile)) . "\"";
                    $impresa['errore'] = '';
                    $impresa['data_ora_rich'] = $richiesta->data_ora_rich;
                    $impresa['data_ora_evas'] = $richiesta->data_ora_evas;
                    $impresa['cod_operazione'] = $richiesta->cod_operazione;
                    $impresa['cod_prodotto'] = $richiesta->cod_prodotto;
                    $impresa['cod_cliente'] = $richiesta->cod_cliente;
                    $impresa['cod_istanza'] = $richiesta->cod_istanza;
                    $impresa['importo'] = $richiesta->importo;
                    $impresa['dataCerved'] = $dati['dataCerved'];
                    $impresa['ragSoc'] = (string) $related->CompanyName;
                    $impresa['formaGiurid'] = (string) $related->CompanyTitle;
                    $impresa['codFisc'] = (string) $related->TaxCode;
                    $impresa['partIVA'] = $impresa['codFisc'];
                    $impresa['capitaleSociale'] = (string) $related->EquityCapital->Subscribed;
                    if (property_exists($related, 'PartnershipAddedValue')) {
                        $impresa['provCCIAA'] = (string) $related->PartnershipAddedValue->REAProvinceCode;
                        $impresa['nCCIAA'] = (string) $related->PartnershipAddedValue->REANo;
                        $impresa['prov'] = (string) $related->PartnershipAddedValue->Address->ProvinceCode;
                        $impresa['desISTAT'] = (string) $related->OtherInformationCompany->CATECOADescription;
                        $impresa['indirizzo'] = (string) $related->PartnershipAddedValue->Address->ToponymCode . ' ' . (string) $related->PartnershipAddedValue->Address->StreetName . ' ' . (string) $related->PartnershipAddedValue->Address->StreetNo;
                        $impresa['CAP'] = (string) $related->PartnershipAddedValue->Address->Postcode;
                    }
                    if ($impresa['partIVA'] == '') {
                        continue;
                    }
                    $company->setData(array());
                    $company->loadFromId($impresa['partIVA']);
                    $cDati = $company->getData();
                    if (is_array($cDati) && count($cDati) > 0) {
                        $company->setData(array_merge($cDati, array_filter($impresa)));
                        $company->update();
                    } else {
                        $company->setData($impresa);
                        $company->insert();
                    }
                    if (strlen($dati['codFisc']) == 16) {
                        $company->addRelatedExponent($person);
                    } else {
                        if ($dati['codFisc'] != '') {
                            $company->addRelatedCompany($rCompany);
                        }
                    }
                }
                $richieste->next();
            }
        }
        if (isset($statHandler)) {
            fclose($statHandler);
        }
        echo PHP_EOL;
    }
Example #3
0
    public function import($statOnly = '')
    {
        $dateFilter = '`data_ora_rich` > CURDATE() - INTERVAL 1 MONTH';
        if ($statOnly != '') {
            $config = clone $this->db->config;
            $configDb = $this->db->config->database->toArray();
            $configDb['options']['buffer_results'] = false;
            $this->db = new \Zend\Db\Adapter\Adapter($configDb);
            $this->db->config = $config;
            $dateFilter = 'TRUE';
            if ($this->db->config->storeExpiryStatFile->VisSintRep != '') {
                $statHandler = fopen($this->db->config->storeExpiryStatFile->VisSintRep, 'w');
            }
            $manager = new \MongoDB\Driver\Manager("mongodb://localhost:27017");
            $bulk = new \MongoDB\Driver\BulkWrite();
        }
        $this->db->query('ALTER TABLE company DISABLE KEYS', \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
        $this->db->query('ALTER TABLE company_to_company DISABLE KEYS', \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
        $this->db->query('ALTER TABLE person DISABLE KEYS', \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
        $this->db->query('ALTER TABLE person_to_company DISABLE KEYS', \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
        $archiviRichieste = array(array('dir' => 'risposteBaseDirMass', 'sql' => '
SELECT aps_prod_backup.eps_richiesta_mass.id_richiesta,
aps_prod_backup.eps_richiesta_mass.id_mov_contab,
aps_prod_backup.eps_richiesta_mass.id_richiesta_cache,
aps_prod_backup.eps_richiesta_mass.stringa_richiesta,
aps_prod_backup.eps_richiesta_mass.data_ora_rich,
aps_prod_backup.eps_richiesta_mass.data_ora_evas,
aps_prod.eps_operazione.cod_operazione as cod_operazione ,
aps_prod_backup.eps_mov_contab_mass.cod_causale as cod_causale ,
aps_prod_backup.eps_mov_contab_mass.importo as importo ,
aps_prod_backup.eps_mov_contab_mass.id_mov_contab_gen as id_mov_contab_gen ,
aps_prod.eps_prodotto.cod_prodotto as cod_prodotto ,
aps_prod.eps_cliente.cod_cliente as cod_cliente ,
aps_prod.eps_istanza.cod_istanza as cod_istanza
FROM aps_prod_backup.eps_richiesta_mass
LEFT JOIN aps_prod.eps_operazione ON aps_prod.eps_operazione.id_operazione=aps_prod_backup.eps_richiesta_mass.id_operazione
LEFT JOIN aps_prod_backup.eps_mov_contab_mass ON aps_prod_backup.eps_mov_contab_mass.id_mov_contab=aps_prod_backup.eps_richiesta_mass.id_mov_contab
LEFT JOIN aps_prod.eps_prodotto ON aps_prod.eps_prodotto.id_prodotto =aps_prod_backup.eps_mov_contab_mass.id_prodotto
LEFT JOIN aps_prod.eps_cliente ON aps_prod.eps_cliente.id_cliente=aps_prod_backup.eps_mov_contab_mass.id_cliente
LEFT JOIN aps_prod.eps_istanza ON aps_prod.eps_istanza.id_istanza=aps_prod.eps_cliente.id_istanza
WHERE
' . $dateFilter . ' AND
aps_prod.eps_operazione.cod_operazione = "vissintrep"
                    ', 'sqlp' => '
SELECT aps_prod_backup.eps_richiesta.id_richiesta,
aps_prod_backup.eps_richiesta.id_mov_contab,
aps_prod_backup.eps_richiesta.id_richiesta_cache,
aps_prod_backup.eps_richiesta.stringa_richiesta,
aps_prod_backup.eps_richiesta.data_ora_rich,
aps_prod_backup.eps_richiesta.data_ora_evas,
aps_prod.eps_operazione.cod_operazione as cod_operazione ,
aps_prod_backup.eps_mov_contab.cod_causale as cod_causale ,
aps_prod_backup.eps_mov_contab.importo as importo ,
aps_prod.eps_prodotto.cod_prodotto as cod_prodotto ,
aps_prod.eps_cliente.cod_cliente as cod_cliente ,
aps_prod.eps_istanza.cod_istanza as cod_istanza

FROM aps_prod_backup.eps_richiesta
LEFT JOIN aps_prod.eps_operazione ON aps_prod.eps_operazione.id_operazione=aps_prod_backup.eps_richiesta.id_operazione
LEFT JOIN aps_prod_backup.eps_mov_contab ON aps_prod_backup.eps_mov_contab.id_mov_contab=aps_prod_backup.eps_richiesta.id_mov_contab
LEFT JOIN aps_prod.eps_prodotto ON aps_prod.eps_prodotto.id_prodotto =aps_prod_backup.eps_mov_contab.id_prodotto
LEFT JOIN aps_prod.eps_cliente ON aps_prod.eps_cliente.id_cliente=aps_prod_backup.eps_mov_contab.id_cliente
LEFT JOIN aps_prod.eps_istanza ON aps_prod.eps_istanza.id_istanza=aps_prod.eps_cliente.id_istanza
WHERE  
aps_prod_backup.eps_mov_contab.id_mov_contab = %d
'), array('dir' => 'risposteBaseDir', 'sql' => '
SELECT aps_prod_backup.eps_richiesta.id_richiesta,
aps_prod_backup.eps_richiesta.id_mov_contab,
aps_prod_backup.eps_richiesta.id_richiesta_cache,
aps_prod_backup.eps_richiesta.stringa_richiesta,
aps_prod_backup.eps_richiesta.data_ora_rich,
aps_prod_backup.eps_richiesta.data_ora_evas,
aps_prod.eps_operazione.cod_operazione as cod_operazione ,
aps_prod_backup.eps_mov_contab.cod_causale as cod_causale ,
aps_prod_backup.eps_mov_contab.importo as importo ,
aps_prod_backup.eps_mov_contab.id_mov_contab_gen as id_mov_contab_gen ,
aps_prod.eps_prodotto.cod_prodotto as cod_prodotto ,
aps_prod.eps_cliente.cod_cliente as cod_cliente ,
aps_prod.eps_istanza.cod_istanza as cod_istanza

FROM aps_prod_backup.eps_richiesta
LEFT JOIN aps_prod.eps_operazione ON aps_prod.eps_operazione.id_operazione=aps_prod_backup.eps_richiesta.id_operazione
LEFT JOIN aps_prod_backup.eps_mov_contab ON aps_prod_backup.eps_mov_contab.id_mov_contab=aps_prod_backup.eps_richiesta.id_mov_contab
LEFT JOIN aps_prod.eps_prodotto ON aps_prod.eps_prodotto.id_prodotto =aps_prod_backup.eps_mov_contab.id_prodotto
LEFT JOIN aps_prod.eps_cliente ON aps_prod.eps_cliente.id_cliente=aps_prod_backup.eps_mov_contab.id_cliente
LEFT JOIN aps_prod.eps_istanza ON aps_prod.eps_istanza.id_istanza=aps_prod.eps_cliente.id_istanza
WHERE  
' . $dateFilter . ' AND
aps_prod.eps_operazione.cod_operazione = "vissintrep"
                    ', 'sqlp' => '
SELECT aps_prod_backup.eps_richiesta.id_richiesta,
aps_prod_backup.eps_richiesta.id_mov_contab,
aps_prod_backup.eps_richiesta.id_richiesta_cache,
aps_prod_backup.eps_richiesta.stringa_richiesta,
aps_prod_backup.eps_richiesta.data_ora_rich,
aps_prod_backup.eps_richiesta.data_ora_evas,
aps_prod.eps_operazione.cod_operazione as cod_operazione ,
aps_prod_backup.eps_mov_contab.cod_causale as cod_causale ,
aps_prod_backup.eps_mov_contab.importo as importo ,
aps_prod.eps_prodotto.cod_prodotto as cod_prodotto ,
aps_prod.eps_cliente.cod_cliente as cod_cliente ,
aps_prod.eps_istanza.cod_istanza as cod_istanza

FROM aps_prod_backup.eps_richiesta
LEFT JOIN aps_prod.eps_operazione ON aps_prod.eps_operazione.id_operazione=aps_prod_backup.eps_richiesta.id_operazione
LEFT JOIN aps_prod_backup.eps_mov_contab ON aps_prod_backup.eps_mov_contab.id_mov_contab=aps_prod_backup.eps_richiesta.id_mov_contab
LEFT JOIN aps_prod.eps_prodotto ON aps_prod.eps_prodotto.id_prodotto =aps_prod_backup.eps_mov_contab.id_prodotto
LEFT JOIN aps_prod.eps_cliente ON aps_prod.eps_cliente.id_cliente=aps_prod_backup.eps_mov_contab.id_cliente
LEFT JOIN aps_prod.eps_istanza ON aps_prod.eps_istanza.id_istanza=aps_prod.eps_cliente.id_istanza
WHERE  
aps_prod_backup.eps_mov_contab.id_mov_contab = %d
'));
        $totalCount = 0;
        $asXML = array();
        foreach ($archiviRichieste as $archivioRichieste) {
            $richieste = $this->db->query($archivioRichieste['sql'], \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
            $count = 0;
            $groupCount = 0;
            $company = new \crm\Company($this->db);
            $rCompany = new \crm\Company($this->db);
            $person = new \crm\Person($this->db);
            while (is_object($richiesta = $richieste->current())) {
                echo "\r" . $count++ . '/' . ($richieste->count() - 1) . str_repeat(' ', 5);
                $dati = array();
                $imprese = array();
                $esponenti = array();
                $sId = str_pad($richiesta->id_richiesta, 9, '0', STR_PAD_LEFT);
                $dM = 'M' . substr($sId, 0, 3);
                $dK = 'K' . substr($sId, 3, 3);
                $nomeFile = $this->db->config->{$archivioRichieste['dir']} . "/{$dM}/{$dK}/F{$sId}.xml";
                if (!is_file($nomeFile)) {
                    $richieste->next();
                    continue;
                }
                $dati['id_mov_contab'] = $richiesta->id_mov_contab;
                $dati['massiva'] = $archivioRichieste['dir'] == 'risposteBaseDir' ? 0 : 1;
                $dati['id_richiesta'] = $richiesta->id_richiesta;
                $dati['test_nome_file'] = "\"" . str_replace('"', '\\"', basename($nomeFile)) . "\"";
                $dati['errore'] = '';
                $dati['data_ora_rich'] = $richiesta->data_ora_rich;
                $dati['data_ora_evas'] = $richiesta->data_ora_evas;
                $dati['cod_operazione'] = $richiesta->cod_operazione;
                $dati['cod_prodotto'] = $richiesta->cod_prodotto;
                $dati['cod_cliente'] = $richiesta->cod_cliente;
                $dati['cod_istanza'] = $richiesta->cod_istanza;
                $dati['importo'] = $richiesta->importo;
                $dati['differita'] = 1;
                if ($richiesta->importo == 0 && $richiesta->id_richiesta_cache == 0) {
                    $richieste->next();
                    continue;
                }
                $richiestaGen = $this->db->query(sprintf($archivioRichieste['sqlp'], $richiesta->id_mov_contab_gen), \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
                $richiestaGen = $richiestaGen->current();
                $nomeFileGen = null;
                if (is_object($richiestaGen)) {
                    $sIdGen = str_pad($richiestaGen->id_richiesta, 9, '0', STR_PAD_LEFT);
                    $dMG = 'M' . substr($sIdGen, 0, 3);
                    $dKG = 'K' . substr($sIdGen, 3, 3);
                    $nomeFileGen = $this->db->config->{$archivioRichieste['dir']} . "/{$dMG}/{$dKG}/F{$sIdGen}.xml";
                }
                $content = file_get_contents($nomeFile);
                preg_match('/\\-+=.*/', $content, $separator);
                if (sizeof($separator) == 0) {
                    $main = '';
                    $report = $content;
                } else {
                    $separator = trim($separator[0]);
                    $parts = explode($separator, $content);
                    $main = $parts[1];
                    $report = $parts[2];
                    $main = preg_replace('/.*\\r\\n\\r\\n/ms', '', $main);
                    $report = preg_replace('/.*\\r\\n\\r\\n/ms', '', $report);
                }
                set_error_handler(create_function('', ''));
                try {
                    $scXML = new \SimpleXMLElement($main);
                    $scXML->registerXPathNamespace('env', 'http://schemas.xmlsoap.org/soap/envelope/');
                    $scXML->registerXPathNamespace('ns2', 'http://www.lince.it/WSCorporate_1/RetrieveReportService');
                    $scXML->registerXPathNamespace('xsi', 'http://www.w3.org/2001/XMLSchema-instance');
                    $sXML = new \SimpleXMLElement($report);
                    $compmany = null;
                    if (is_file($nomeFileGen)) {
                        $sgXML = new \SimpleXMLElement($nomeFileGen, 0, true);
                        $sgXML->registerXPathNamespace('env', 'http://schemas.xmlsoap.org/soap/envelope/');
                        $sgXML->registerXPathNamespace('ns2', 'http://www.lince.it/WSCorporate_1/ThreeStepService');
                        $sgXML->registerXPathNamespace('xsi', 'http://www.w3.org/2001/XMLSchema-instance');
                        $companiesSearch = $sgXML->xpath('/env:Envelope/env:Body/ns2:searchCompanyResponse/searchResult/companies');
                        foreach ($companiesSearch as $companySearch) {
                            if ($companySearch->fiscalCode == (string) $sXML->xpath('/BasicAbbrevia/CompanyIdentificationSection/TaxCode')[0]) {
                                break;
                            }
                        }
                    }
                    /*$errore = $sXML->xpath('/env:Envelope/env:Body/env:Fault/faultstring');
                      if (is_array($errore)) {
                         $dati['errore'] = "\"".array_shift($errore)."\"";
                      }
                      $sContenuto = base64_decode($sXML->xpath('//document')[0]);
                      $sXML = new \SimpleXMLElement($sContenuto);*/
                } catch (\Exception $e) {
                }
                $companyData = $sXML->xpath('/BasicAbbrevia/CompanyIdentificationSection')[0];
                $companyAddress = $sXML->xpath('/BasicAbbrevia/CompanyIdentificationSection/RHOAddress')[0];
                $companyBusiness = $sXML->xpath('/BasicAbbrevia/CompanyIdentificationSection/RHOSlimActivityList/SlimBusinessActivity')[0];
                $dati['dataCerved'] = '';
                $dateObject = date_create_from_format('d/m/Y H:i:s.u', (string) $sXML->xpath('/BasicAbbrevia/Metadata/Response/Stats/ServiceResponseTime')[0]);
                if (!is_object($dateObject)) {
                    $dateObject = date_create_from_format('d/m/Y H:i:s,u', (string) $sXML->xpath('/BasicAbbrevia/Metadata/Response/Stats/ServiceResponseTime')[0]);
                }
                if (is_object($dateObject)) {
                    $dati['dataCerved'] = date_format($dateObject, 'Y-m-d H:i:s');
                }
                $dati['gruppo'] = '';
                $dati['gruppoRuolo'] = '';
                $dati['partIVA'] = (string) $companySearch->vatNumber;
                $dati['ragSoc'] = (string) $companyData->CompanyName;
                $dati['codFisc'] = (string) $companyData->TaxCode;
                $dati['provCCIAA'] = (string) $companyData->REAProvinceCode;
                $dati['nCCIAA'] = (string) $companyData->REANumber;
                $dati['prov'] = (string) $companyAddress->ProvinceCode;
                $dati['comune'] = (string) $companyAddress->City;
                $dati['indirizzo'] = (string) $companyAddress->StreetName . ' ' . (string) $companyAddress->StreetNo;
                $dati['CAP'] = (string) $companyAddress->PostCode;
                $dati['nazione'] = 'ITALIA';
                $dati['sitoWeb'] = '';
                $dati['email'] = (string) $companyData->CertifiedEmail;
                $dati['telefono'] = (string) $companySearch->phoneNumber;
                $dati['statoAttivita'] = (string) $companySearch->activityStatus;
                $dati['formaGiurid'] = (string) $companySearch->legalForm;
                $dati['tipoSoc'] = '';
                $dati['capitaleSociale'] = '';
                $dati['codISTAT'] = (string) $companyBusiness->Code;
                $dati['desISTAT'] = (string) $companySearch->cAtecoDescription;
                if (method_exists($companyBusiness->Code, 'attributes')) {
                    $dati['desISTAT'] = (string) $companyBusiness->Code->attributes()->description;
                }
                $dati['codNACE'] = '';
                $dati['dataInizioAtt'] = '';
                $dati['dataCostituzione'] = '';
                $dati['dataREA'] = date_format(date_create((string) $companySearch->registrationDate), 'Y-m-d H:i:s');
                if (strlen($dati['dataREA']) < 6) {
                    $dati['dataREA'] = '';
                }
                $dati['fidoAffidabilta'] = '';
                $dati['fidoAffidabiltaNote'] = '';
                $dati['fidoAccordabile'] = '';
                $dati['numUffSedi'] = '';
                $dati['desAttivita'] = '';
                $dati['eventi'] = '';
                $dati['protesti'] = '';
                $dati['fallimenti'] = '';
                $dati['pregiudizievoli'] = '';
                $dati['cassaIntegrazione'] = '';
                $dati['nDipendenti'] = '';
                $dati['fatturato'] = '';
                $dati['annoFatturato'] = '';
                $dati['trendFatturato'] = '';
                $dati['utilePerdita'] = '';
                $dati['patrimonioNetto'] = '';
                $dati['margineOperativoLordo'] = '';
                $dati['risultatoOperativoLordo'] = '';
                $dati['indiceIndebitamento'] = '';
                $dati['ROI'] = '';
                $dati['ROE'] = '';
                $dati['indiceLiquidita'] = '';
                $dati['dataChiusuraBilancio'] = '';
                foreach ($sXML->xpath('/BasicAbbrevia/RepresentativesSection/RepresentativeList/Representative') as $shareolder) {
                    if (property_exists($shareolder, 'Individual')) {
                        $esponente = array();
                        $esponente['nome'] = (string) $shareolder->Individual->FirstName;
                        $esponente['cognome'] = (string) $shareolder->Individual->LastName;
                        $esponente['codFisc'] = (string) $shareolder->TaxCode;
                        $esponente['dataNas'] = (string) date_format(date_create_from_format('d/m/Y', (string) $shareolder->Individual->Birth->BirthDate->Value), 'Y-m-d');
                        $esponente['luogoNas'] = (string) $shareolder->Individual->Birth->City;
                        $esponente['prov'] = (string) $shareolder->Individual->TaxDomicile->ProvinceCode;
                        $esponente['comune'] = (string) $shareolder->Individual->TaxDomicile->City;
                        $esponente['indirizzo'] = (string) $shareolder->Individual->TaxDomicile->StreetName . ' ' . (string) $shareolder->Individual->TaxDomicile->StreetNo;
                        $esponente['CAP'] = (string) $shareolder->Individual->TaxDomicile->PostCode;
                        $esponente['titolo'] = 'ESPONENTE';
                        $esponenti[] = $esponente;
                    } else {
                        if (property_exists($shareolder, 'CompanyEquivalent')) {
                            $impresa = array();
                            $impresa['ragSoc'] = (string) $shareolder->CompanyEquivalent->CompanyName;
                            $impresa['formaGiurid'] = '';
                            $impresa['partIVA'] = (string) $shareolder->TaxCode;
                            $impresa['capitaleSociale'] = '';
                            $impresa['statoAttivita'] = '';
                            $impresa['provCCIAA'] = (string) $shareolder->CompanyEquivalent->REAProvinceCode;
                            $impresa['nCCIAA'] = (string) $shareolder->CompanyEquivalent->REANo;
                            $impresa['prov'] = (string) $shareolder->CompanyEquivalent->Address->ProvinceCode;
                            $impresa['comune'] = (string) $shareolder->CompanyEquivalent->Address->City;
                            $impresa['indirizzo'] = (string) $shareolder->CompanyEquivalent->Address->StreetName . ' ' . (string) $shareolder->CompanyEquivalent->Address->StreetNo;
                            $impresa['CAP'] = (string) $shareolder->CompanyEquivalent->Address->PostCode;
                            $impresa['titolo'] = 'CORRELATA';
                            $imprese[] = $impresa;
                        }
                    }
                }
                restore_error_handler();
                if ($statOnly != '') {
                    array_walk($dati, create_function('&$item', '$item=str_replace("\\"","",$item);'));
                    if (isset($statHandler)) {
                        if (ftell($statHandler) == 0) {
                            fputcsv($statHandler, array_keys($dati));
                        }
                        fputcsv($statHandler, $dati);
                        fflush($statHandler);
                    }
                    $domXml = dom_import_simplexml($sXML);
                    $aXML = array('_id' => $dati['id_mov_contab']);
                    $this->xml2array($domXml, $aXML);
                    $this->singlexml2array($domXml, $asXML);
                    file_put_contents('/tmp/vissintrep.json', json_encode($asXML));
                    /*$xml = new \SimpleXMLElement('<root/>');
                    
                    
                                   $this->array_to_xml($aXML,$xml);*/
                    if (true || is_array($aXML) && $totalCount++ < 10000.0) {
                        $bulk->update(array('_id' => $dati['id_mov_contab']), $aXML, array('multi' => false, 'upsert' => true));
                    }
                }
                if ($dati['partIVA'] == '' || $dati['partIVA'] == 0 || $statOnly != '') {
                    $richieste->next();
                    continue;
                }
                $company->setData(array());
                $company->loadFromId($dati['partIVA']);
                $cDati = $company->getData();
                if (is_array($cDati) && count($cDati) > 0) {
                    $company->setData(array_merge($cDati, array_filter($dati)));
                    $company->update();
                } else {
                    $company->setData($dati);
                    $company->insert();
                }
                foreach ($imprese as $impresa) {
                    $rCompany->setData(array());
                    $rCompany->loadFromId($impresa['partIVA']);
                    $cDati = $rCompany->getData();
                    if (is_array($cDati) && count($cDati) > 0) {
                        $rCompany->setData(array_merge($cDati, array_filter($impresa)));
                        $rCompany->update();
                    } else {
                        $rCompany->setData($impresa);
                        $rCompany->insert();
                    }
                    $company->addRelatedCompany($rCompany);
                }
                foreach ($esponenti as $esponente) {
                    if (strlen($esponente['codFisc']) == 16) {
                        $person->setData(array());
                        $person->loadFromId($esponente['codFisc']);
                        $cDati = $person->getData();
                        if (is_array($cDati) && count($cDati) > 0) {
                            $person->setData(array_merge($cDati, array_filter($esponente)));
                            $person->update();
                        } else {
                            $person->setData($esponente);
                            $person->insert();
                        }
                        $company->addRelatedExponent($person);
                    } else {
                        $rCompany->setData(array());
                        $esponente['partIVA'] = $esponente['codFisc'];
                        $rCompany->loadFromId($esponente['partIVA']);
                        $cDati = $rCompany->getData();
                        if (is_array($cDati) && count($cDati) > 0) {
                            $rCompany->setData(array_merge($cDati, array_filter($esponente)));
                            $rCompany->update();
                        } else {
                            $rCompany->setData($esponente);
                            $rCompany->insert();
                        }
                        $company->addRelatedCompany($rCompany);
                    }
                }
                $richieste->next();
            }
        }
        $this->db->query('ALTER TABLE company ENABLE KEYS', \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
        $this->db->query('ALTER TABLE company_to_company ENABLE KEYS', \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
        $this->db->query('ALTER TABLE person ENABLE KEYS', \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
        $this->db->query('ALTER TABLE person_to_company ENABLE KEYS', \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
        if (isset($statHandler)) {
            fclose($statHandler);
            if ($bulk->count() > 0) {
                $manager->executeBulkWrite('report_impresa.vissintrep', $bulk);
            }
        }
        echo PHP_EOL;
    }
Example #4
0
    public function import($statOnly = '')
    {
        $dateFilter = '`data_ora_rich` > CURDATE() - INTERVAL 2 MONTH';
        if ($statOnly != '') {
            $config = clone $this->db->config;
            $configDb = $this->db->config->database->toArray();
            $configDb['options']['buffer_results'] = false;
            $this->db = new \Zend\Db\Adapter\Adapter($configDb);
            $this->db->config = $config;
            $dateFilter = 'TRUE';
            if ($this->db->config->storeExpiryStatFile->Skimp != '') {
                $statHandler = fopen($this->db->config->storeExpiryStatFile->Skimp, 'w');
            }
            $manager = new \MongoDB\Driver\Manager("mongodb://localhost:27017");
            $bulk = new \MongoDB\Driver\BulkWrite();
        }
        $this->db->query('ALTER TABLE company DISABLE KEYS', \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
        $this->db->query('ALTER TABLE company_to_company DISABLE KEYS', \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
        $this->db->query('ALTER TABLE person DISABLE KEYS', \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
        $this->db->query('ALTER TABLE person_to_company DISABLE KEYS', \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
        $archiviRichieste = array(array('dir' => 'risposteBaseDirMass', 'sql' => '
SELECT aps_prod_backup.eps_richiesta_mass.id_richiesta,
aps_prod_backup.eps_richiesta_mass.id_mov_contab,
aps_prod_backup.eps_richiesta_mass.id_richiesta_cache,
aps_prod_backup.eps_richiesta_mass.stringa_richiesta,
aps_prod_backup.eps_richiesta_mass.data_ora_rich,
aps_prod_backup.eps_richiesta_mass.data_ora_evas,
aps_prod.eps_operazione.cod_operazione as cod_operazione ,
aps_prod_backup.eps_mov_contab_mass.cod_causale as cod_causale ,
aps_prod_backup.eps_mov_contab_mass.importo as importo ,
aps_prod.eps_prodotto.cod_prodotto as cod_prodotto ,
aps_prod.eps_cliente.cod_cliente as cod_cliente ,
aps_prod.eps_istanza.cod_istanza as cod_istanza

FROM aps_prod_backup.eps_richiesta_mass
LEFT JOIN aps_prod.eps_operazione ON aps_prod.eps_operazione.id_operazione=aps_prod_backup.eps_richiesta_mass.id_operazione
LEFT JOIN aps_prod_backup.eps_mov_contab_mass ON aps_prod_backup.eps_mov_contab_mass.id_mov_contab=aps_prod_backup.eps_richiesta_mass.id_mov_contab
LEFT JOIN aps_prod.eps_prodotto ON aps_prod.eps_prodotto.id_prodotto =aps_prod_backup.eps_mov_contab_mass.id_prodotto
LEFT JOIN aps_prod.eps_cliente ON aps_prod.eps_cliente.id_cliente=aps_prod_backup.eps_mov_contab_mass.id_cliente
LEFT JOIN aps_prod.eps_istanza ON aps_prod.eps_istanza.id_istanza=aps_prod.eps_cliente.id_istanza
WHERE  
' . $dateFilter . ' AND (
   aps_prod.eps_operazione.cod_operazione = "skimpord" OR 
   aps_prod.eps_operazione.cod_operazione = "skimpsto"
)

          '), array('dir' => 'risposteBaseDir', 'sql' => '
SELECT aps_prod_backup.eps_richiesta.id_richiesta,
aps_prod_backup.eps_richiesta.id_mov_contab,
aps_prod_backup.eps_richiesta.id_richiesta_cache,
aps_prod_backup.eps_richiesta.stringa_richiesta,
aps_prod_backup.eps_richiesta.data_ora_rich,
aps_prod_backup.eps_richiesta.data_ora_evas,
aps_prod.eps_operazione.cod_operazione as cod_operazione ,
aps_prod_backup.eps_mov_contab.cod_causale as cod_causale ,
aps_prod_backup.eps_mov_contab.importo as importo ,
aps_prod.eps_prodotto.cod_prodotto as cod_prodotto ,
aps_prod.eps_cliente.cod_cliente as cod_cliente ,
aps_prod.eps_istanza.cod_istanza as cod_istanza

FROM aps_prod_backup.eps_richiesta
LEFT JOIN aps_prod.eps_operazione ON aps_prod.eps_operazione.id_operazione=aps_prod_backup.eps_richiesta.id_operazione
LEFT JOIN aps_prod_backup.eps_mov_contab ON aps_prod_backup.eps_mov_contab.id_mov_contab=aps_prod_backup.eps_richiesta.id_mov_contab
LEFT JOIN aps_prod.eps_prodotto ON aps_prod.eps_prodotto.id_prodotto =aps_prod_backup.eps_mov_contab.id_prodotto
LEFT JOIN aps_prod.eps_cliente ON aps_prod.eps_cliente.id_cliente=aps_prod_backup.eps_mov_contab.id_cliente
LEFT JOIN aps_prod.eps_istanza ON aps_prod.eps_istanza.id_istanza=aps_prod.eps_cliente.id_istanza
WHERE  
' . $dateFilter . ' AND (
   aps_prod.eps_operazione.cod_operazione = "skimpord" OR 
   aps_prod.eps_operazione.cod_operazione = "skimpsto"
)
'));
        foreach ($archiviRichieste as $archivioRichieste) {
            $richieste = $this->db->query($archivioRichieste['sql'], \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
            $count = 0;
            $totalCount = 0;
            $groupCount = 0;
            $company = new \crm\Company($this->db);
            $rCompany = new \crm\Company($this->db);
            $person = new \crm\Person($this->db);
            while (is_object($richiesta = $richieste->current())) {
                echo "\r" . $count++ . '/' . ($richieste->count() - 1) . str_repeat(' ', 5);
                $dati = array();
                $imprese = array();
                $esponenti = array();
                $sId = str_pad($richiesta->id_richiesta, 9, '0', STR_PAD_LEFT);
                $dM = 'M' . substr($sId, 0, 3);
                $dK = 'K' . substr($sId, 3, 3);
                $nomeFile = $this->db->config->{$archivioRichieste['dir']} . "/{$dM}/{$dK}/F{$sId}.xml";
                if (!is_file($nomeFile)) {
                    $richieste->next();
                    continue;
                }
                $dati['id_mov_contab'] = $richiesta->id_mov_contab;
                $dati['massiva'] = 0;
                $dati['id_richiesta'] = $richiesta->id_richiesta;
                $dati['test_nome_file'] = "\"" . str_replace('"', '\\"', basename($nomeFile)) . "\"";
                $dati['errore'] = '';
                $dati['data_ora_rich'] = $richiesta->data_ora_rich;
                $dati['data_ora_evas'] = $richiesta->data_ora_evas;
                $dati['cod_operazione'] = $richiesta->cod_operazione;
                $dati['cod_prodotto'] = $richiesta->cod_prodotto;
                $dati['cod_cliente'] = $richiesta->cod_cliente;
                $dati['cod_istanza'] = $richiesta->cod_istanza;
                $dati['importo'] = $richiesta->importo;
                $dati['differita'] = 0;
                if ($richiesta->importo == 0 && $richiesta->id_richiesta_cache == 0) {
                    $richieste->next();
                    continue;
                }
                set_error_handler(create_function('', ''));
                try {
                    $sXML = new \SimpleXMLElement($nomeFile, 0, true);
                } catch (\Exception $e) {
                    $dati['differita'] = 1;
                    $data = file_get_contents($nomeFile);
                    preg_match('/--(?<MIME_boundary>.+?)\\s/', $data, $match);
                    $mimeBoundary = $match['MIME_boundary'];
                    preg_match('/(.*Content-Id.+' . PHP_EOL . ')/', $data, $match);
                    $start = strpos($data, $match[1]) + strlen($match[1]);
                    $end = strpos($data, "--{$mimeBoundary}--");
                    $data = substr($data, $start, $end - $start);
                    try {
                        $sXML = new \SimpleXMLElement(trim($data));
                    } catch (\Exception $e) {
                        $dati['differita'] = 0;
                    }
                }
                $dati['dataCerved'] = '';
                $report = $sXML->xpath('/s:Envelope/s:Body/*')[0]->BasicReportDocument;
                $date = new \DateTime((string) $sXML->xpath('/s:Envelope/s:Body/*')[0]->TransactionResponse->Details->ResponseTimestamp[0]);
                if (is_object($date)) {
                    $dati['dataCerved'] = $date->format('Y-m-d H:i:s');
                }
                $dati['gruppo'] = '';
                $dati['gruppoRuolo'] = '';
                $dati['partIVA'] = (string) $report->CRR->ID->C->attributes()->VC[0];
                $dati['ragSoc'] = (string) $report->CRR->ID->C->attributes()->ND[0];
                $dati['codFisc'] = (string) $report->CRR->ID->C->attributes()->TC[0];
                $dati['provCCIAA'] = (string) $report->CRR->ID->C->attributes()->PCCIAA[0];
                $dati['nCCIAA'] = (string) $report->CRR->ID->C->attributes()->RC[0];
                $dati['prov'] = (string) $report->CRR->ID->C->AD->attributes()->PC[0];
                $dati['comune'] = (string) $report->CRR->ID->C->AD->attributes()->TD[0];
                $dati['indirizzo'] = (string) $report->CRR->ID->C->AD->attributes()->TP[0] . ' ' . (string) $report->CRR->ID->C->AD->attributes()->AD[0] . ' ' . (string) $report->CRR->ID->C->AD->attributes()->N[0];
                $dati['CAP'] = (string) $report->CRR->ID->C->AD->attributes()->ZC[0];
                $dati['nazione'] = 'IT';
                $dati['sitoWeb'] = '';
                $dati['email'] = '';
                $dati['telefono'] = '';
                foreach ($report->CRR->ID->C->C as $contact) {
                    switch ($contact->attributes()->TC) {
                        case 'U':
                            $dati['sitoWeb'] = (string) $contact->attributes()->CV[0];
                            break;
                        case 'T':
                            $dati['telefono'] = (string) $contact->attributes()->CV[0];
                            break;
                        case 'E':
                        case 'C':
                            $dati['email'] = (string) $contact->attributes()->CV[0];
                            break;
                    }
                }
                $dati['statoAttivita'] = (string) $report->CRR->ID->C->attributes()->BSD[0];
                $dati['formaGiurid'] = (string) $report->CRR->ID->C->attributes()->LFCD[0];
                $dati['tipoSoc'] = (string) $report->CRR->ID->C->attributes()->LFGCD[0];
                $dati['capitaleSociale'] = '';
                if (is_object($report->CRR->AF->LD->FA->SC->AC)) {
                    $dati['capitaleSociale'] = (string) $report->CRR->AF->LD->FA->SC->AC->attributes()->A[0];
                }
                $dati['codISTAT'] = '';
                $dati['desISTAT'] = '';
                if (is_object($report->CRR->AF->A->IA->PA07)) {
                    $dati['codISTAT'] = (string) $report->CRR->AF->A->IA->PA07->attributes()->C[0];
                    $dati['desISTAT'] = (string) $report->CRR->AF->A->IA->PA07->attributes()->D[0];
                }
                if (is_object($report->CRR->AF->A->NACE)) {
                    $dati['codNACE'] = (string) $report->CRR->AF->A->NACE->attributes()->C[0];
                }
                $dati['dataInizioAtt'] = '';
                $date = new \DateTime((string) $report->CRR->ID->C->attributes()->SD[0]);
                if (is_object($date)) {
                    $dati['dataInizioAtt'] = $date->format('Y-m-d H:i:s');
                }
                $dati['dataCostituzione'] = '';
                $dati['dataREA'] = '';
                $date = new \DateTime((string) $report->CRR->AF->LD->H->attributes()->SD[0]);
                if (is_object($date)) {
                    $dati['dataREA'] = $date->format('Y-m-d H:i:s');
                }
                $dati['fidoAffidabilta'] = '';
                $dati['fidoAffidabiltaNote'] = '';
                $dati['fidoAccordabile'] = '';
                $dati['numUffSedi'] = count($report->CRR->Bs->B);
                $dati['desAttivita'] = (string) $report->CRR->AF->LD->AA->attributes()->SO[0];
                $dati['eventi'] = count($report->CRR->RE->LE);
                $dati['protesti'] = count($report->CRR->RE->NE->PRTs);
                $dati['fallimenti'] = count($report->CRR->RE->NE->PE);
                $dati['pregiudizievoli'] = count($report->CRR->RE->NE->LPs) + count($report->CRR->RE->NE->PERER);
                $dati['cassaIntegrazione'] = '';
                if (is_object($report->CRR->AF->A->ED)) {
                    $dati['nDipendenti'] = $report->CRR->AF->A->ED->attributes()->N + $report->CRR->AF->A->ED->attributes()->SEN;
                }
                $dati['fatturato'] = '';
                $dati['annoFatturato'] = '';
                $dati['utilePerdita'] = '';
                $dati['patrimonioNetto'] = '';
                $dati['trendFatturato'] = '';
                $dati['margineOperativoLordo'] = '';
                $dati['risultatoOperativoLordo'] = '';
                $dati['indiceIndebitamento'] = '';
                $dati['ROI'] = '';
                $dati['ROE'] = '';
                $dati['indiceLiquidita'] = '';
                if (is_object($report->CRR->BSs->BSS->BSD)) {
                    $dati['fatturato'] = (string) $report->CRR->BSs->BSS->BSD->PLA->attributes()->TR[0];
                    $dati['annoFatturato'] = (string) $report->CRR->BSs->BSS->BSD->attributes()->Y[0];
                    $dati['utilePerdita'] = (string) $report->CRR->BSs->BSS->BSD->PLA->attributes()->PL[0];
                    $dati['patrimonioNetto'] = (string) $report->CRR->BSs->BSS->BSD->LB->attributes()->PL[0];
                }
                $dati['dataChiusuraBilancio'] = '';
                foreach ($report->CRR->Rs->R as $subsidiarie) {
                    if (is_object($subsidiarie->ID->P)) {
                        $esponente = array();
                        $esponente['nome'] = (string) $subsidiarie->ID->P->attributes()->N;
                        $esponente['cognome'] = (string) $subsidiarie->ID->P->attributes()->S;
                        $esponente['codFisc'] = (string) $subsidiarie->ID->P->attributes()->TC;
                        $esponente['dataNas'] = '';
                        if (is_object($subsidiarie->ID->P->BD)) {
                            $date = new \DateTime((string) $subsidiarie->ID->P->BD->attributes()->D);
                            if (is_object($date)) {
                                $esponente['dataNas'] = $date->format('Y-m-d');
                            }
                            $esponente['luogoNas'] = (string) $subsidiarie->ID->P->BD->attributes()->TD;
                        }
                        if (is_object($subsidiarie->ID->P->AD)) {
                            $esponente['prov'] = (string) $subsidiarie->ID->P->AD->attributes()->PC;
                            $esponente['comune'] = (string) $subsidiarie->ID->P->AD->attributes()->TD;
                            $esponente['indirizzo'] = (string) $subsidiarie->ID->P->AD->attributes()->TP . ' ' . (string) $subsidiarie->ID->P->AD->attributes()->AD . ' ' . (string) $subsidiarie->ID->P->AD->attributes()->N;
                            $esponente['CAP'] = (string) $subsidiarie->ID->P->AD->attributes()->ZC;
                        }
                        if (is_object($subsidiarie->RPs->RP->R)) {
                            $esponente['titolo'] = (string) $subsidiarie->RPs->RP->R->attributes()->RDES;
                        }
                        $esponenti[] = $esponente;
                    } else {
                        if (is_object($subsidiarie->ID->C)) {
                            $impresa = array();
                            $impresa['ragSoc'] = (string) $subsidiarie->ID->C->attributes()->ND;
                            $impresa['formaGiurid'] = '';
                            $impresa['partIVA'] = (string) $subsidiarie->ID->C->attributes()->TC;
                            $impresa['capitaleSociale'] = '';
                            $impresa['statoAttivita'] = '';
                            $impresa['provCCIAA'] = '';
                            $impresa['nCCIAA'] = '';
                            $impresa['prov'] = (string) $subsidiarie->ID->C->AD->attributes()->PC;
                            $impresa['comune'] = (string) $subsidiarie->ID->C->AD->attributes()->TD;
                            $impresa['indirizzo'] = (string) $subsidiarie->ID->C->AD->attributes()->TP . ' ' . (string) $subsidiarie->ID->C->AD->attributes()->AD . ' ' . (string) $subsidiarie->ID->C->AD->attributes()->N;
                            $impresa['CAP'] = (string) $subsidiarie->ID->C->AD->attributes()->ZC;
                            $impresa['titolo'] = (string) $subsidiarie->RPs->RP->R->attributes()->RDES;
                            $imprese[] = $impresa;
                        }
                    }
                }
                foreach ($report->CRR->Ss->S->SI as $subsidiarie) {
                    if (is_object($subsidiarie->ID->P)) {
                        $esponente = array();
                        $esponente['nome'] = (string) $subsidiarie->ID->P->attributes()->N;
                        $esponente['cognome'] = (string) $subsidiarie->ID->P->attributes()->S;
                        $esponente['codFisc'] = (string) $subsidiarie->ID->P->attributes()->TC;
                        $esponente['dataNas'] = '';
                        if (is_object($subsidiarie->ID->P->BD) && is_object($subsidiarie->ID->P->BD->attributes())) {
                            $date = new \DateTime((string) $subsidiarie->ID->P->BD->attributes()->D);
                            if (is_object($date)) {
                                $esponente['dataNas'] = $date->format('Y-m-d');
                            }
                            $esponente['luogoNas'] = (string) $subsidiarie->ID->P->BD->attributes()->TD;
                        }
                        if (is_object($subsidiarie->ID->P->AD)) {
                            $esponente['prov'] = (string) $subsidiarie->ID->P->AD->attributes()->PC;
                            $esponente['comune'] = (string) $subsidiarie->ID->P->AD->attributes()->TD;
                            $esponente['indirizzo'] = (string) $subsidiarie->ID->P->AD->attributes()->TP . ' ' . (string) $subsidiarie->ID->P->AD->attributes()->AD . ' ' . (string) $subsidiarie->ID->P->AD->attributes()->N;
                            $esponente['CAP'] = (string) $subsidiarie->ID->P->AD->attributes()->ZC;
                        }
                        $esponente['titolo'] = 'SOCIO';
                        $esponenti[] = $esponente;
                    } else {
                        if (is_object($subsidiarie->ID->C)) {
                            $impresa = array();
                            $impresa['ragSoc'] = (string) $subsidiarie->ID->C->attributes()->ND;
                            $impresa['formaGiurid'] = '';
                            $impresa['partIVA'] = (string) $subsidiarie->ID->C->attributes()->TC;
                            $impresa['capitaleSociale'] = '';
                            $impresa['statoAttivita'] = '';
                            $impresa['provCCIAA'] = '';
                            $impresa['nCCIAA'] = '';
                            $impresa['prov'] = (string) $subsidiarie->ID->C->AD->attributes()->PC;
                            $impresa['comune'] = (string) $subsidiarie->ID->C->AD->attributes()->TD;
                            $impresa['indirizzo'] = (string) $subsidiarie->ID->C->AD->attributes()->TP . ' ' . (string) $subsidiarie->ID->C->AD->attributes()->AD . ' ' . (string) $subsidiarie->ID->C->AD->attributes()->N;
                            $impresa['CAP'] = (string) $subsidiarie->ID->C->AD->attributes()->ZC;
                            $impresa['titolo'] = 'SOCIO';
                            $imprese[] = $impresa;
                        }
                    }
                }
                restore_error_handler();
                if ($statOnly != '') {
                    array_walk($dati, create_function('&$item', '$item=str_replace("\\"","",$item);'));
                    if (isset($statHandler)) {
                        if (ftell($statHandler) == 0) {
                            fputcsv($statHandler, array_keys($dati));
                        }
                        fputcsv($statHandler, $dati);
                        fflush($statHandler);
                    }
                    $domXml = dom_import_simplexml($report);
                    $aXML = array('_id' => $dati['id_mov_contab']);
                    $this->xml2array($domXml, $aXML);
                    /*$xml = new \SimpleXMLElement('<root/>');
                    
                    
                                $t*his->array_to_xml($aXML,$xml);*/
                    if (true || is_array($aXML) && $totalCount++ < 10000.0) {
                        $bulk->update(array('_id' => $dati['id_mov_contab']), $aXML, array('multi' => false, 'upsert' => true));
                    }
                }
                if ($dati['partIVA'] == '' || $dati['partIVA'] == 0 || $statOnly != '') {
                    $richieste->next();
                    continue;
                }
                $company->setData(array());
                $company->loadFromId($dati['partIVA']);
                $cDati = $company->getData();
                if (is_array($cDati) && count($cDati) > 0) {
                    $company->setData(array_merge($cDati, array_filter($dati)));
                    $company->update();
                } else {
                    $company->setData($dati);
                    $company->insert();
                }
                foreach ($imprese as $impresa) {
                    $rCompany->setData(array());
                    $rCompany->loadFromId($impresa['partIVA']);
                    $cDati = $rCompany->getData();
                    if (is_array($cDati) && count($cDati) > 0) {
                        $rCompany->setData(array_merge($cDati, array_filter($impresa)));
                        $rCompany->update();
                    } else {
                        $rCompany->setData($impresa);
                        $rCompany->insert();
                    }
                    $company->addRelatedCompany($rCompany);
                }
                foreach ($esponenti as $esponente) {
                    if (strlen($esponente['codFisc']) == 16) {
                        $person->setData(array());
                        $person->loadFromId($esponente['codFisc']);
                        $cDati = $person->getData();
                        if (is_array($cDati) && count($cDati) > 0) {
                            $person->setData(array_merge($cDati, array_filter($esponente)));
                            $person->update();
                        } else {
                            $person->setData($esponente);
                            $person->insert();
                        }
                        $company->addRelatedExponent($person);
                    } else {
                        $rCompany->setData(array());
                        $esponente['partIVA'] = $esponente['codFisc'];
                        $rCompany->loadFromId($esponente['partIVA']);
                        $cDati = $rCompany->getData();
                        if (is_array($cDati) && count($cDati) > 0) {
                            $rCompany->setData(array_merge($cDati, array_filter($esponente)));
                            $rCompany->update();
                        } else {
                            $rCompany->setData($esponente);
                            $rCompany->insert();
                        }
                        $company->addRelatedCompany($rCompany);
                    }
                }
                $richieste->next();
            }
        }
        $this->db->query('ALTER TABLE company ENABLE KEYS', \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
        $this->db->query('ALTER TABLE company_to_company ENABLE KEYS', \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
        $this->db->query('ALTER TABLE person ENABLE KEYS', \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
        $this->db->query('ALTER TABLE person_to_company ENABLE KEYS', \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
        if (isset($statHandler)) {
            fclose($statHandler);
            if ($bulk->count() > 0) {
                $manager->executeBulkWrite('report_impresa.skimp', $bulk);
            }
        }
        echo PHP_EOL;
    }
Example #5
0
    public function import($statOnly = '')
    {
        $dateFilter = '`data_ora_rich` > CURDATE() - INTERVAL 2 MONTH';
        if ($statOnly != '') {
            $config = clone $this->db->config;
            $configDb = $this->db->config->database->toArray();
            $configDb['options']['buffer_results'] = false;
            $this->db = new \Zend\Db\Adapter\Adapter($configDb);
            $this->db->config = $config;
            $dateFilter = 'TRUE';
            if ($this->db->config->storeExpiryStatFile->Sksocieta != '') {
                $statHandler = fopen($this->db->config->storeExpiryStatFile->Sksocieta, 'w');
            }
            $manager = new \MongoDB\Driver\Manager("mongodb://localhost:27017");
            $bulk = new \MongoDB\Driver\BulkWrite();
        }
        $archiviRichieste = array(array('dir' => 'risposteBaseDirMass', 'sql' => '
SELECT aps_prod_backup.eps_richiesta_mass.id_richiesta,
aps_prod_backup.eps_richiesta_mass.id_mov_contab,
aps_prod_backup.eps_richiesta_mass.id_richiesta_cache,
aps_prod_backup.eps_richiesta_mass.stringa_richiesta,
aps_prod_backup.eps_richiesta_mass.data_ora_rich,
aps_prod_backup.eps_richiesta_mass.data_ora_evas,
aps_prod.eps_operazione.cod_operazione as cod_operazione ,
aps_prod_backup.eps_mov_contab_mass.cod_causale as cod_causale ,
aps_prod_backup.eps_mov_contab_mass.importo as importo ,
aps_prod_backup.eps_mov_contab_mass.id_mov_contab_gen as id_mov_contab_gen ,
aps_prod.eps_prodotto.cod_prodotto as cod_prodotto ,
aps_prod.eps_cliente.cod_cliente as cod_cliente ,
aps_prod.eps_istanza.cod_istanza as cod_istanza
FROM aps_prod_backup.eps_richiesta_mass
LEFT JOIN aps_prod.eps_operazione ON aps_prod.eps_operazione.id_operazione=aps_prod_backup.eps_richiesta_mass.id_operazione
LEFT JOIN aps_prod_backup.eps_mov_contab_mass ON aps_prod_backup.eps_mov_contab_mass.id_mov_contab=aps_prod_backup.eps_richiesta_mass.id_mov_contab
LEFT JOIN aps_prod.eps_prodotto ON aps_prod.eps_prodotto.id_prodotto =aps_prod_backup.eps_mov_contab_mass.id_prodotto
LEFT JOIN aps_prod.eps_cliente ON aps_prod.eps_cliente.id_cliente=aps_prod_backup.eps_mov_contab_mass.id_cliente
LEFT JOIN aps_prod.eps_istanza ON aps_prod.eps_istanza.id_istanza=aps_prod.eps_cliente.id_istanza
WHERE
' . $dateFilter . ' AND
aps_prod.eps_operazione.cod_operazione = "sksocieta"
                    '), array('dir' => 'risposteBaseDir', 'sql' => '
SELECT aps_prod_backup.eps_richiesta.id_richiesta,
aps_prod_backup.eps_richiesta.id_mov_contab,
aps_prod_backup.eps_richiesta.id_richiesta_cache,
aps_prod_backup.eps_richiesta.stringa_richiesta,
aps_prod_backup.eps_richiesta.data_ora_rich,
aps_prod_backup.eps_richiesta.data_ora_evas,
aps_prod.eps_operazione.cod_operazione as cod_operazione ,
aps_prod_backup.eps_mov_contab.cod_causale as cod_causale ,
aps_prod_backup.eps_mov_contab.importo as importo ,
aps_prod_backup.eps_mov_contab.id_mov_contab_gen as id_mov_contab_gen ,
aps_prod.eps_prodotto.cod_prodotto as cod_prodotto ,
aps_prod.eps_cliente.cod_cliente as cod_cliente ,
aps_prod.eps_istanza.cod_istanza as cod_istanza

FROM aps_prod_backup.eps_richiesta
LEFT JOIN aps_prod.eps_operazione ON aps_prod.eps_operazione.id_operazione=aps_prod_backup.eps_richiesta.id_operazione
LEFT JOIN aps_prod_backup.eps_mov_contab ON aps_prod_backup.eps_mov_contab.id_mov_contab=aps_prod_backup.eps_richiesta.id_mov_contab
LEFT JOIN aps_prod.eps_prodotto ON aps_prod.eps_prodotto.id_prodotto =aps_prod_backup.eps_mov_contab.id_prodotto
LEFT JOIN aps_prod.eps_cliente ON aps_prod.eps_cliente.id_cliente=aps_prod_backup.eps_mov_contab.id_cliente
LEFT JOIN aps_prod.eps_istanza ON aps_prod.eps_istanza.id_istanza=aps_prod.eps_cliente.id_istanza
WHERE
' . $dateFilter . ' AND
aps_prod.eps_operazione.cod_operazione = "sksocieta"
                    '));
        $totalCount = 0;
        foreach ($archiviRichieste as $archivioRichieste) {
            $richieste = $this->db->query($archivioRichieste['sql'], \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
            $count = 0;
            $groupCount = 0;
            $rCompany = new \crm\Company($this->db);
            $company = new \crm\Company($this->db);
            $person = new \crm\Person($this->db);
            while (is_object($richiesta = $richieste->current())) {
                echo "\r" . $count++ . '/' . ($richieste->count() - 1) . str_repeat(' ', 5);
                $dati = array();
                $imprese = array();
                $esponenti = array();
                $sId = str_pad($richiesta->id_richiesta, 9, '0', STR_PAD_LEFT);
                $dM = 'M' . substr($sId, 0, 3);
                $dK = 'K' . substr($sId, 3, 3);
                $nomeFile = $this->db->config->{$archivioRichieste['dir']} . "/{$dM}/{$dK}/F{$sId}.xml";
                if (!is_file($nomeFile)) {
                    $richieste->next();
                    continue;
                }
                $dati['id_mov_contab'] = $richiesta->id_mov_contab;
                $dati['massiva'] = $archivioRichieste['dir'] == 'risposteBaseDir' ? 0 : 1;
                $dati['id_richiesta'] = $richiesta->id_richiesta;
                $dati['test_nome_file'] = "\"" . str_replace('"', '\\"', basename($nomeFile)) . "\"";
                $dati['errore'] = '';
                $dati['data_ora_rich'] = $richiesta->data_ora_rich;
                $dati['data_ora_evas'] = $richiesta->data_ora_evas;
                $dati['cod_operazione'] = $richiesta->cod_operazione;
                $dati['cod_prodotto'] = $richiesta->cod_prodotto;
                $dati['cod_cliente'] = $richiesta->cod_cliente;
                $dati['cod_istanza'] = $richiesta->cod_istanza;
                $dati['importo'] = $richiesta->importo;
                $dati['differita'] = 1;
                if ($richiesta->importo == 0 && $richiesta->id_richiesta_cache == 0) {
                    $richieste->next();
                    continue;
                }
                $sXML = new \SimpleXMLElement($nomeFile, 0, true);
                $dati['dataCerved'] = '';
                $dateObject = date_create_from_format('Y m d H:i:s', (string) $sXML->xpath('/s2xResponse/s2xStatus/s2xTime')[0]);
                if (is_object($dateObject)) {
                    $dati['dataCerved'] = date_format($dateObject, 'Y-m-d H:i:s');
                }
                $sobject = $sXML->xpath('/s2xResponse/s2xData/Shareholders/Identification');
                if (!is_array($sobject) || sizeof($sobject) < 1) {
                    $richieste->next();
                    continue;
                }
                $sobject = $sobject[0];
                $dati['partIVA'] = (string) $sobject->TaxCode;
                $dati['ragSoc'] = (string) $sobject->CompanyName;
                $dati['codFisc'] = (string) $sobject->TaxCode;
                $dati['provCCIAA'] = (string) $sobject->ProvinceCode;
                $dati['nCCIAA'] = (string) $sobject->REARegistrationNumber;
                $dati['formaGiurid'] = (string) $sobject->CompanyTitle;
                $dati['capitaleSociale'] = (string) $sobject->EquityCapital->Subscribed;
                $dati['dataREA'] = '';
                $dateObject = date_create_from_format('Ymd', (string) $sobject->REARegistrationDate);
                if (is_object($dateObject)) {
                    $dati['dataREA'] = date_format($dateObject, 'Y-m-d H:i:s');
                }
                if (property_exists($sobject, 'IdentificationAddedValue')) {
                    $dati['prov'] = (string) $sobject->IdentificationAddedValue->Address->ProvinceCode;
                    if (is_object($sobject->IdentificationAddedValue->Address->MunicipalityCode)) {
                        $dati['comune'] = (string) $sobject->IdentificationAddedValue->Address->MunicipalityCode->attributes()->Description;
                    }
                    $dati['indirizzo'] = (string) $sobject->IdentificationAddedValue->Address->ToponymCode . ' ' . (string) $sobject->IdentificationAddedValue->Address->StreetName . ' ' . (string) $sobject->IdentificationAddedValue->Address->StreetNo;
                    $dati['CAP'] = (string) $sobject->IdentificationAddedValue->Address->Postcode;
                    $dati['nazione'] = 'ITALIA';
                    $dati['sitoWeb'] = '';
                    $dati['email'] = '';
                    $dati['telefono'] = (string) $sobject->IdentificationAddedValue->Telephone->AreaCode . ' ' . (string) $sobject->IdentificationAddedValue->Telephone->TelephoneNumber;
                    $dati['statoAttivita'] = (string) $sobject->BusinessStatus->Code;
                    $dati['tipoSoc'] = '';
                    $dati['codISTAT'] = (string) $sobject->IdentificationAddedValue->Activity->CERVEDActivityCode;
                    $dati['desISTAT'] = '';
                    if (is_object($sobject->IdentificationAddedValue->Activity->CERVEDActivityCode) && $sobject->IdentificationAddedValue->Activity->CERVEDActivityCode->count() > 0) {
                        $dati['desISTAT'] = (string) $sobject->IdentificationAddedValue->Activity->CERVEDActivityCode->attributes()->Description;
                    }
                    $dati['fatturato'] = (string) $sobject->IdentificationAddedValue->DataBalanceSheet->Revenue;
                    $dati['annoFatturato'] = '';
                    if (is_object($sobject->IdentificationAddedValue->DataBalanceSheet->ClosingDate)) {
                        $dati['annoFatturato'] = (string) $sobject->IdentificationAddedValue->DataBalanceSheet->ClosingDate->attributes()->year;
                    }
                    $dati['gruppo'] = (string) $sobject->IdentificationAddedValue->MemberOfGroup->NameMemberOfGroup;
                    if ($dati['gruppo'] == '') {
                        $dati['gruppo'] = (string) $sobject->IdentificationAddedValue->MemberOfGroup3->NameMemberOfGroup3;
                    }
                    $dati['gruppoRuolo'] = '';
                    $dati['codNACE'] = '';
                    $dati['dataInizioAtt'] = '';
                    $dati['dataCostituzione'] = '';
                }
                $dati['fidoAffidabilta'] = '';
                $dati['fidoAffidabiltaNote'] = '';
                $dati['fidoAccordabile'] = '';
                $dati['numUffSedi'] = '';
                $dati['desAttivita'] = '';
                $dati['eventi'] = '';
                $dati['protesti'] = '';
                $dati['fallimenti'] = '';
                $dati['pregiudizievoli'] = '';
                $dati['cassaIntegrazione'] = '';
                $dati['nDipendenti'] = '';
                $dati['trendFatturato'] = '';
                $dati['utilePerdita'] = '';
                $dati['patrimonioNetto'] = '';
                $dati['margineOperativoLordo'] = '';
                $dati['risultatoOperativoLordo'] = '';
                $dati['indiceIndebitamento'] = '';
                $dati['ROI'] = '';
                $dati['ROE'] = '';
                $dati['indiceLiquidita'] = '';
                $dati['dataChiusuraBilancio'] = '';
                restore_error_handler();
                if ($statOnly != '') {
                    array_walk($dati, create_function('&$item', '$item=str_replace("\\"","",$item);'));
                    if (isset($statHandler)) {
                        if (ftell($statHandler) == 0) {
                            fputcsv($statHandler, array_keys($dati));
                        }
                        fputcsv($statHandler, $dati);
                        fflush($statHandler);
                    }
                    $domXml = dom_import_simplexml($sobject);
                    $aXML = array();
                    $this->xml2array($domXml, $aXML);
                    /*$xml = new \SimpleXMLElement('<root/>');
                    
                    
                                   $this->array_to_xml($aXML,$xml);*/
                    if (true || is_array($aXML) && $totalCount++ < 10000.0) {
                        $bulk->update(array('_id' => $dati['id_mov_contab']), $aXML, array('multi' => false, 'upsert' => true));
                    }
                    $richieste->next();
                    continue;
                }
                foreach ($sXML->xpath('/s2xResponse/s2xData/NotConsolidatedShareholdingsSection/SharesList/Shares') as $related) {
                    if (property_exists($related, 'Shareholdings_NotConsolidated')) {
                        $socio = array();
                        $socio['codFisc'] = (string) $related->Shareholdings_NotConsolidated->TaxCode;
                        if (strlen($socio['codFisc']) == 16) {
                            $socio['nome'] = (string) $related->Shareholdings_NotConsolidated->FirstName;
                            $socio['cognome'] = (string) $related->Shareholdings_NotConsolidated->LastName;
                            $socio['prov'] = (string) $related->Shareholdings_NotConsolidated->Domicile->ProvinceCode;
                            $socio['comune'] = (string) $related->Shareholdings_NotConsolidated->Domicile->Municipality;
                            $socio['indirizzo'] = (string) $related->Shareholdings_NotConsolidated->Domicile->Street . ' ' . (string) $related->Shareholdings_NotConsolidated->Domicile->StreetNo;
                            $socio['CAP'] = (string) $related->Shareholdings_NotConsolidated->Domicile->PostalCode;
                            $socio['dataNas'] = '';
                            if (is_object($related->Shareholdings_NotConsolidated->IndividualAddedValue->BirthInformation)) {
                                $dateObject = date_create_from_format('Ymd', (string) $related->Shareholdings_NotConsolidated->IndividualAddedValue->BirthInformation->BirthData);
                                if (is_object($dateObject)) {
                                    $socio['dataNas'] = date_format($dateObject, 'Y-m-d');
                                }
                            }
                            $esponenti[] = $socio;
                        } else {
                            if (strlen($socio['codFisc']) == 11) {
                                $socio['partIVA'] = $socio['codFisc'];
                                $socio['ragSoc'] = (string) $related->Shareholdings_NotConsolidated->CompanyName;
                                $socio['prov'] = (string) $related->Shareholdings_NotConsolidated->Domicile->ProvinceCode;
                                $socio['comune'] = (string) $related->Shareholdings_NotConsolidated->Domicile->Municipality;
                                $socio['indirizzo'] = (string) $related->Shareholdings_NotConsolidated->Domicile->Street . ' ' . (string) $related->Shareholdings_NotConsolidated->Domicile->StreetNo;
                                if (is_object($related->Shareholdings_NotConsolidated->PartnershipAddedValue)) {
                                    if (is_object($related->Shareholdings_NotConsolidated->PartnershipAddedValue->ReaKey)) {
                                        $socio['provCCIAA'] = (string) $related->Shareholdings_NotConsolidated->PartnershipAddedValue->ReaKey->REAProvinceCode;
                                        $socio['nCCIAA'] = (string) $related->Shareholdings_NotConsolidated->PartnershipAddedValue->ReaKey->REANo;
                                    }
                                    if (is_object($related->Shareholdings_NotConsolidated->PartnershipAddedValue->BusinessStatus)) {
                                        $socio['statoAttivita'] = (string) $related->Shareholdings_NotConsolidated->PartnershipAddedValue->BusinessStatus->Code;
                                    }
                                    $socio['gruppo'] = '';
                                    if (is_object($related->Shareholdings_NotConsolidated->PartnershipAddedValue->MemberOfGroup)) {
                                        $socio['gruppo'] = (string) $related->Shareholdings_NotConsolidated->PartnershipAddedValue->MemberOfGroup->NameMemberOfGroup;
                                    }
                                    if ($socio['gruppo'] == '' && is_object($related->Shareholdings_NotConsolidated->PartnershipAddedValue->MemberOfGroup3)) {
                                        $socio['gruppo'] = (string) $related->Shareholdings_NotConsolidated->PartnershipAddedValue->MemberOfGroup3->NameMemberOfGroup3;
                                    }
                                }
                                $imprese[] = $socio;
                            }
                        }
                    }
                    $company->setData(array());
                    $company->loadFromId($dati['partIVA']);
                    $cDati = $company->getData();
                    if (is_array($cDati) && count($cDati) > 0) {
                        $company->setData(array_merge($cDati, array_filter($dati)));
                        $company->update();
                    } else {
                        $company->setData($dati);
                        $company->insert();
                    }
                    foreach ($imprese as $impresa) {
                        $rCompany->setData(array());
                        $rCompany->loadFromId($impresa['partIVA']);
                        $cDati = $rCompany->getData();
                        if (is_array($cDati) && count($cDati) > 0) {
                            $rCompany->setData(array_merge($cDati, array_filter($impresa)));
                            $rCompany->update();
                        } else {
                            $rCompany->setData($impresa);
                            $rCompany->insert();
                        }
                        $company->addRelatedCompany($rCompany);
                    }
                    foreach ($esponenti as $esponente) {
                        if (strlen($esponente['codFisc']) == 16) {
                            $person->setData(array());
                            $person->loadFromId($esponente['codFisc']);
                            $cDati = $person->getData();
                            if (is_array($cDati) && count($cDati) > 0) {
                                $person->setData(array_merge($cDati, array_filter($esponente)));
                                $person->update();
                            } else {
                                $person->setData($esponente);
                                $person->insert();
                            }
                            $company->addRelatedExponent($person);
                        } else {
                            $rCompany->setData(array());
                            $esponente['partIVA'] = $esponente['codFisc'];
                            $rCompany->loadFromId($esponente['partIVA']);
                            $cDati = $rCompany->getData();
                            if (is_array($cDati) && count($cDati) > 0) {
                                $rCompany->setData(array_merge($cDati, array_filter($esponente)));
                                $rCompany->update();
                            } else {
                                $rCompany->setData($esponente);
                                $rCompany->insert();
                            }
                            $company->addRelatedCompany($rCompany);
                        }
                    }
                }
                $richieste->next();
            }
        }
        if (isset($statHandler)) {
            fclose($statHandler);
            if ($bulk->count() > 0) {
                $manager->executeBulkWrite('report_impresa.sksocieta', $bulk);
            }
        }
        echo PHP_EOL;
    }
Example #6
0
    public function import($statOnly = '')
    {
        $dateFilter = '`data_ora_rich` > CURDATE() - INTERVAL 2 MONTH';
        if ($statOnly != '') {
            $config = clone $this->db->config;
            $configDb = $this->db->config->database->toArray();
            $configDb['options']['buffer_results'] = false;
            $this->db = new \Zend\Db\Adapter\Adapter($configDb);
            $this->db->config = $config;
            $dateFilter = 'TRUE';
            if ($this->db->config->storeExpiryStatFile->Rep3P != '') {
                $statHandler = fopen($this->db->config->storeExpiryStatFile->Rep3P, 'w');
            }
            $manager = new \MongoDB\Driver\Manager("mongodb://*****:*****@year')[0];
            $dati['dataCerved'] .= '-';
            $dati['dataCerved'] .= str_pad((string) $sXML->xpath('/*/ns3:DeliveryAndSummaryDetails/DeliveredOn/@month')[0], 2, '0', STR_PAD_LEFT);
            $dati['dataCerved'] .= '-';
            $dati['dataCerved'] .= str_pad((string) $sXML->xpath('/*/ns3:DeliveryAndSummaryDetails/DeliveredOn/@day')[0], 2, '0', STR_PAD_LEFT);
            $dati['dataCerved'] .= ' ';
            $dati['dataCerved'] .= str_pad((string) $sXML->xpath('/*/ns3:DeliveryAndSummaryDetails/DeliveredOn/@hour24')[0], 2, '0', STR_PAD_LEFT);
            $dati['dataCerved'] .= ':';
            $dati['dataCerved'] .= str_pad((string) $sXML->xpath('/*/ns3:DeliveryAndSummaryDetails/DeliveredOn/@minutes')[0], 2, '0', STR_PAD_LEFT);
            $dati['dataCerved'] .= ':';
            $dati['dataCerved'] .= str_pad((string) $sXML->xpath('/*/ns3:DeliveryAndSummaryDetails/DeliveredOn/@seconds')[0], 2, '0', STR_PAD_LEFT);
            if (strlen($dati['dataCerved']) < 6) {
                $dati['dataCerved'] = '';
            }
            $dati['gruppo'] = (string) $sXML->xpath('/*/ns4:ItemInformation/MemberOfGroup')[0];
            $dati['gruppoRuolo'] = (string) $sXML->xpath('/*/ns4:ItemInformation/Role')[0];
            $dati['partIVA'] = (string) $sXML->xpath('/*/ns4:ItemInformation/CompanyInformation/VATRegistrationNo')[0];
            $dati['ragSoc'] = (string) $sXML->xpath('/*/ns4:ItemInformation/CompanyInformation/CompanyName')[0];
            $dati['codFisc'] = (string) $sXML->xpath('/*/ns4:ItemInformation/CompanyInformation/TaxCode')[0];
            $dati['provCCIAA'] = (string) $sXML->xpath('/*/ns4:ItemInformation/ReaCode/CoCProvinceCode')[0];
            $dati['nCCIAA'] = (string) $sXML->xpath('/*/ns4:ItemInformation/ReaCode/REANo')[0];
            $dati['prov'] = (string) $sXML->xpath('/*/ns4:ItemInformation/CompanyInformation/Address/Province/@Code')[0];
            $dati['comune'] = (string) $sXML->xpath('/*/ns4:ItemInformation/CompanyInformation/Address/Municipality')[0];
            $dati['indirizzo'] = (string) $sXML->xpath('/*/ns4:ItemInformation/CompanyInformation/Address/Street')[0];
            $dati['CAP'] = (string) $sXML->xpath('/*/ns4:ItemInformation/CompanyInformation/Address/PostCode')[0];
            $dati['nazione'] = (string) $sXML->xpath('/*/ns4:ItemInformation/CompanyInformation/Address/Country')[0];
            $dati['sitoWeb'] = (string) $sXML->xpath('/*/ns4:ItemInformation/WebSite')[0];
            $dati['email'] = (string) $sXML->xpath('/*/ns4:ItemInformation/Email')[0];
            $dati['telefono'] = (string) $sXML->xpath('/*/ns4:ItemInformation/Telephone')[0];
            $dati['statoAttivita'] = (string) $sXML->xpath('/*/ns4:ItemInformation/ActivityStatusDescription')[0];
            $dati['formaGiurid'] = (string) $sXML->xpath('/*/ns4:ItemInformation/CompanyInformation/CompanyForm')[0];
            $dati['tipoSoc'] = (string) $sXML->xpath('/*/ns4:ItemInformation/CompanyInformation/CompanyForm/@LegalFormType')[0];
            $dati['capitaleSociale'] = (string) $sXML->xpath('/*/ns4:ItemInformation/EquityCapital')[0];
            $dati['codISTAT'] = (string) $sXML->xpath('/*/ns4:ItemInformation/ISTATCode/@Code')[0];
            $dati['desISTAT'] = (string) $sXML->xpath('/*/ns4:ItemInformation/ISTATCode')[0];
            $dati['codNACE'] = (string) $sXML->xpath('/*/ns4:ItemInformation/NaceCode')[0];
            $dati['dataInizioAtt'] = (string) $sXML->xpath('/*/ns4:ItemInformation/ActivityStartDate/@year')[0];
            $dati['dataInizioAtt'] .= '-';
            $dati['dataInizioAtt'] .= str_pad((string) $sXML->xpath('/*/ns4:ItemInformation/ActivityStartDate/@month')[0], 2, '0', STR_PAD_LEFT);
            $dati['dataInizioAtt'] .= '-';
            $dati['dataInizioAtt'] .= str_pad((string) $sXML->xpath('/*/ns4:ItemInformation/ActivityStartDate/@day')[0], 2, '0', STR_PAD_LEFT);
            $dati['dataInizioAtt'] .= ' ';
            $dati['dataInizioAtt'] .= str_pad((string) $sXML->xpath('/*/ns4:ItemInformation/ActivityStartDate/@hour24')[0], 2, '0', STR_PAD_LEFT);
            $dati['dataInizioAtt'] .= ':';
            $dati['dataInizioAtt'] .= str_pad((string) $sXML->xpath('/*/ns4:ItemInformation/ActivityStartDate/@minutes')[0], 2, '0', STR_PAD_LEFT);
            $dati['dataInizioAtt'] .= ':';
            $dati['dataInizioAtt'] .= str_pad((string) $sXML->xpath('/*/ns4:ItemInformation/ActivityStartDate/@seconds')[0], 2, '0', STR_PAD_LEFT);
            if (strlen($dati['dataInizioAtt']) < 6) {
                $dati['dataInizioAtt'] = '';
            }
            $dati['dataCostituzione'] = (string) $sXML->xpath('/*/ns4:ItemInformation/IncorporationDate/@year')[0];
            $dati['dataCostituzione'] .= '-';
            $dati['dataCostituzione'] .= str_pad((string) $sXML->xpath('/*/ns4:ItemInformation/IncorporationDate/@month')[0], 2, '0', STR_PAD_LEFT);
            $dati['dataCostituzione'] .= '-';
            $dati['dataCostituzione'] .= str_pad((string) $sXML->xpath('/*/ns4:ItemInformation/IncorporationDate/@day')[0], 2, '0', STR_PAD_LEFT);
            $dati['dataCostituzione'] .= ' ';
            $dati['dataCostituzione'] .= str_pad((string) $sXML->xpath('/*/ns4:ItemInformation/IncorporationDate/@hour24')[0], 2, '0', STR_PAD_LEFT);
            $dati['dataCostituzione'] .= ':';
            $dati['dataCostituzione'] .= str_pad((string) $sXML->xpath('/*/ns4:ItemInformation/IncorporationDate/@minutes')[0], 2, '0', STR_PAD_LEFT);
            $dati['dataCostituzione'] .= ':';
            $dati['dataCostituzione'] .= str_pad((string) $sXML->xpath('/*/ns4:ItemInformation/IncorporationDate/@seconds')[0], 2, '0', STR_PAD_LEFT);
            if (strlen($dati['dataCostituzione']) < 6) {
                $dati['dataCostituzione'] = '';
            }
            $dati['dataREA'] = (string) $sXML->xpath('/*/ns4:ItemInformation/RegistrationDate/@year')[0];
            $dati['dataREA'] .= '-';
            $dati['dataREA'] .= str_pad((string) $sXML->xpath('/*/ns4:ItemInformation/RegistrationDate/@month')[0], 2, '0', STR_PAD_LEFT);
            $dati['dataREA'] .= '-';
            $dati['dataREA'] .= str_pad((string) $sXML->xpath('/*/ns4:ItemInformation/RegistrationDate/@day')[0], 2, '0', STR_PAD_LEFT);
            $dati['dataREA'] .= ' ';
            $dati['dataREA'] .= str_pad((string) $sXML->xpath('/*/ns4:ItemInformation/RegistrationDate/@hour24')[0], 2, '0', STR_PAD_LEFT);
            $dati['dataREA'] .= ':';
            $dati['dataREA'] .= str_pad((string) $sXML->xpath('/*/ns4:ItemInformation/RegistrationDate/@minutes')[0], 2, '0', STR_PAD_LEFT);
            $dati['dataREA'] .= ':';
            $dati['dataREA'] .= str_pad((string) $sXML->xpath('/*/ns4:ItemInformation/RegistrationDate/@seconds')[0], 2, '0', STR_PAD_LEFT);
            if (strlen($dati['dataREA']) < 6) {
                $dati['dataREA'] = '';
            }
            $codiceFido = (string) $sXML->xpath('/*/ns19_:AverageGrantableCredit/CreditLimit/Code')[0];
            if ($codiceFido == '') {
                $codiceFido = (string) $sXML->xpath('/*/ns19_:AverageGrantableCredit/BlockCode')[0];
            }
            $dati['fidoAffidabilta'] = '';
            if (key_exists($codiceFido, $aFido)) {
                $dati['fidoAffidabilta'] = $aFido[$codiceFido];
            }
            $codiceFido = (string) $sXML->xpath('/*/ns19_:AverageGrantableCredit/CreditLimit/Note')[0];
            $dati['fidoAffidabiltaNote'] = '';
            if (key_exists($codiceFido, $this->aFido)) {
                $dati['fidoAffidabiltaNote'] = $this->aFido[$codiceFido];
            }
            $dati['fidoAccordabile'] = (string) $sXML->xpath('/*/ns19_:AverageGrantableCredit/CreditLimit/Value')[0];
            $dati['numUffSedi'] = (string) $sXML->xpath('/*/ns4:ItemInformation/OfficesAndSubOrganizationsNumber')[0];
            $dati['desAttivita'] = (string) $sXML->xpath('/*/ns16:Activity/ActivityDescription')[0];
            $dati['eventi'] = count($sXML->xpath('/*/ns17:ExtraordinaryEvents/ns17:ExtraordinaryEventsList/EventItem'));
            $dati['protesti'] = count($sXML->xpath('/*/ns6:Protests/ns6:ProtestsBlock/ns6:SingleProtestBlock'));
            $dati['fallimenti'] = count($sXML->xpath('/*/ns7:Bankruptcy/ns7:BankruptcyBlock/ns7:SingleBankruptcyBlock'));
            $dati['pregiudizievoli'] = count($sXML->xpath('/*/ns8:AgreedWindingUpProcedures/ns8:ItemAgreeWindingUpProcedure/ns8:AgreedWindingUpProcedure'));
            $dati['cassaIntegrazione'] = count($sXML->xpath('/*/ns20:Layoffs/Decree'));
            $dati['nDipendenti'] = (string) $sXML->xpath('/*/ns9:CompanyEmployee/NumberOfEmployees')[0];
            $dati['fatturato'] = (string) $sXML->xpath('/*/ns40:BalanceSheet/BalanceSheetInfo/ProfitAndLoss/Sales/Value')[0];
            $dati['annoFatturato'] = (string) $sXML->xpath('/*/ns40:BalanceSheet/BalanceSheetInfo/ReferenceYear')[0];
            $dati['trendFatturato'] = (string) $sXML->xpath('/*/ns40:BalanceSheet/BalanceSheetInfo/ProfitAndLoss/Sales/Change')[0];
            $dati['utilePerdita'] = (string) $sXML->xpath('/*/ns40:BalanceSheet/BalanceSheetInfo/ProfitAndLoss/ProfitAndLoss/Value')[0];
            $dati['patrimonioNetto'] = (string) $sXML->xpath('/*/ns40:BalanceSheet/BalanceSheetInfo/Assets/TotalAssets/Value')[0];
            $dati['margineOperativoLordo'] = (string) $sXML->xpath('/*/ns40:BalanceSheet/BalanceSheetInfo/ProfitAndLoss/GrossProfit/Value')[0];
            $dati['risultatoOperativoLordo'] = (string) $sXML->xpath('/*/ns40:BalanceSheet/BalanceSheetInfo/ProfitAndLoss/OperatingProfit/Value')[0];
            $dati['indiceIndebitamento'] = (string) $sXML->xpath('/*/ns41:BalanceSheetAnalysis/BalanceSheetRatios/Voices/DebtRatio/Value')[0];
            $dati['ROI'] = (string) $sXML->xpath('/*/ns41:BalanceSheetAnalysis/BalanceSheetRatios/Voices/Roi/Value')[0];
            $dati['ROE'] = (string) $sXML->xpath('/*/ns41:BalanceSheetAnalysis/BalanceSheetRatios/Voices/Roe/Value')[0];
            $dati['indiceLiquidita'] = (string) $sXML->xpath('/*/ns41:BalanceSheetAnalysis/BalanceSheetRatios/Voices/CurrentAssetsRatio/Value')[0];
            $dati['fidoAccordabile201604'] = fidoRep3P201604($dati['fidoAccordabile'], $dati['tipoSoc'], $dati['fatturato']);
            $dati['dataChiusuraBilancio'] = (string) $sXML->xpath('/*/ns40:BalanceSheet/BalanceSheetInfo/ClosingDate/@year')[0];
            $dati['dataChiusuraBilancio'] .= '-';
            $dati['dataChiusuraBilancio'] .= str_pad((string) $sXML->xpath('/*/ns40:BalanceSheet/BalanceSheetInfo/ClosingDate/@month')[0], 2, '0', STR_PAD_LEFT);
            $dati['dataChiusuraBilancio'] .= '-';
            $dati['dataChiusuraBilancio'] .= str_pad((string) $sXML->xpath('/*/ns40:BalanceSheet/BalanceSheetInfo/ClosingDate/@day')[0], 2, '0', STR_PAD_LEFT);
            $dati['dataChiusuraBilancio'] .= ' ';
            $dati['dataChiusuraBilancio'] .= str_pad((string) $sXML->xpath('/*/ns40:BalanceSheet/BalanceSheetInfo/ClosingDate/@hour24')[0], 2, '0', STR_PAD_LEFT);
            $dati['dataChiusuraBilancio'] .= ':';
            $dati['dataChiusuraBilancio'] .= str_pad((string) $sXML->xpath('/*/ns40:BalanceSheet/BalanceSheetInfo/ClosingDate/@minutes')[0], 2, '0', STR_PAD_LEFT);
            $dati['dataChiusuraBilancio'] .= ':';
            $dati['dataChiusuraBilancio'] .= str_pad((string) $sXML->xpath('/*/ns40:BalanceSheet/BalanceSheetInfo/ClosingDate/@seconds')[0], 2, '0', STR_PAD_LEFT);
            if (strlen($dati['dataChiusuraBilancio']) < 6) {
                $dati['dataChiusuraBilancio'] = '';
            }
            foreach ($sXML->xpath('/*/ns5:Subsidiaries/ns5:Othersubsidiaries') as $subsidiarie) {
                $esponente = array();
                $esponente['nome'] = (string) $subsidiarie->xpath('OthersubsidiarieInfos')[0];
                $esponente['cognome'] = '';
                $esponente['codFisc'] = (string) $subsidiarie->xpath('TaxCode')[0];
                $esponente['dataNas'] = '';
                $esponente['luogoNas'] = '';
                $esponente['prov'] = '';
                $esponente['comune'] = '';
                $esponente['indirizzo'] = '';
                $esponente['CAP'] = '';
                $esponente['titolo'] = (string) $subsidiarie->xpath('MainPosition')[0];
                $esponenti[] = $esponente;
            }
            foreach ($sXML->xpath('/*/ns23:Shareholders/ShareholdersList/Shareholder/ShareholderItem') as $shareolder) {
                if (count($shareolder->xpath('ns22:Aziendaslim'))) {
                    $impresa = array();
                    $impresa['ragSoc'] = (string) $shareolder->xpath('ns22:Aziendaslim/CompanyInformation/CompanyName')[0];
                    $impresa['formaGiurid'] = (string) $shareolder->xpath('ns22:Aziendaslim/CompanyInformation/CompanyForm')[0];
                    $impresa['partIVA'] = (string) $shareolder->xpath('ns22:Aziendaslim/CompanyInformation/TaxCode')[0];
                    $impresa['capitaleSociale'] = (string) $shareolder->xpath('ns22:Aziendaslim/EquityCapital')[0];
                    $impresa['statoAttivita'] = (string) $shareolder->xpath('ns22:Aziendaslim/ActivityDescription')[0];
                    $impresa['provCCIAA'] = (string) $shareolder->xpath('ns22:Aziendaslim/ReaCode/CoCProvinceCode')[0];
                    $impresa['nCCIAA'] = (string) $shareolder->xpath('ns22:Aziendaslim/ReaCode/REANo')[0];
                    $impresa['prov'] = (string) $shareolder->xpath('ns22:Aziendaslim/CompanyInformation/Address/Province/@Code')[0];
                    $impresa['comune'] = (string) $shareolder->xpath('ns22:Aziendaslim/CompanyInformation/Address/Municipality')[0];
                    $impresa['indirizzo'] = (string) $shareolder->xpath('ns22:Aziendaslim/CompanyInformation/Address/Street')[0];
                    $impresa['CAP'] = (string) $shareolder->xpath('ns22:Aziendaslim/CompanyInformation/Address/PostCode')[0];
                    $impresa['titolo'] = 'SOCIO';
                    $imprese[] = $impresa;
                } else {
                    $esponente = array();
                    $esponente['nome'] = (string) $shareolder->xpath('IndividualInformation/FirstName')[0];
                    $esponente['cognome'] = (string) $shareolder->xpath('IndividualInformation/LastName')[0];
                    $esponente['codFisc'] = (string) $shareolder->xpath('IndividualInformation/TaxCode')[0];
                    $esponente['dataNas'] = '';
                    $esponente['luogoNas'] = '';
                    $esponente['prov'] = '';
                    $esponente['comune'] = '';
                    $esponente['indirizzo'] = '';
                    $esponente['CAP'] = '';
                    $esponente['titolo'] = 'SOCIO';
                    $esponenti[] = $esponente;
                }
            }
            foreach ($sXML->xpath('/*/ns24:Holdings/ControllatedCompany') as $controlledCompany) {
                $impresa = array();
                $impresa['ragSoc'] = (string) $controlledCompany->xpath('ns22:Aziendaslim/CompanyInformation/CompanyName')[0];
                $impresa['formaGiurid'] = (string) $controlledCompany->xpath('ns22:Aziendaslim/CompanyInformation/CompanyForm')[0];
                $impresa['partIVA'] = (string) $controlledCompany->xpath('ns22:Aziendaslim/CompanyInformation/TaxCode')[0];
                $impresa['capitaleSociale'] = (string) $controlledCompany->xpath('ns22:Aziendaslim/EquityCapital')[0];
                $impresa['statoAttivita'] = (string) $controlledCompany->xpath('ns22:Aziendaslim/ActivityDescription')[0];
                $impresa['provCCIAA'] = (string) $controlledCompany->xpath('ns22:Aziendaslim/ReaCode/CoCProvinceCode')[0];
                $impresa['nCCIAA'] = (string) $controlledCompany->xpath('ns22:Aziendaslim/ReaCode/REANo')[0];
                $impresa['prov'] = (string) $controlledCompany->xpath('ns22:Aziendaslim/CompanyInformation/Address/Province/@Code')[0];
                $impresa['comune'] = (string) $controlledCompany->xpath('ns22:Aziendaslim/CompanyInformation/Address/Municipality')[0];
                $impresa['indirizzo'] = (string) $controlledCompany->xpath('ns22:Aziendaslim/CompanyInformation/Address/Street')[0];
                $impresa['CAP'] = (string) $controlledCompany->xpath('ns22:Aziendaslim/CompanyInformation/Address/PostCode')[0];
                $impresa['titolo'] = 'CONTROLLATA';
                $imprese[] = $impresa;
            }
            foreach ($sXML->xpath('/*/ns24:Holdings/LinkedCompany') as $controlledCompany) {
                $impresa = array();
                $impresa['ragSoc'] = (string) $controlledCompany->xpath('ns22:Aziendaslim/CompanyInformation/CompanyName')[0];
                $impresa['formaGiurid'] = (string) $controlledCompany->xpath('ns22:Aziendaslim/CompanyInformation/CompanyForm')[0];
                $impresa['partIVA'] = (string) $controlledCompany->xpath('ns22:Aziendaslim/CompanyInformation/TaxCode')[0];
                $impresa['capitaleSociale'] = (string) $controlledCompany->xpath('ns22:Aziendaslim/EquityCapital')[0];
                $impresa['statoAttivita'] = (string) $controlledCompany->xpath('ns22:Aziendaslim/ActivityDescription')[0];
                $impresa['provCCIAA'] = (string) $controlledCompany->xpath('ns22:Aziendaslim/ReaCode/CoCProvinceCode')[0];
                $impresa['nCCIAA'] = (string) $controlledCompany->xpath('ns22:Aziendaslim/ReaCode/REANo')[0];
                $impresa['prov'] = (string) $controlledCompany->xpath('ns22:Aziendaslim/CompanyInformation/Address/Province/@Code')[0];
                $impresa['comune'] = (string) $controlledCompany->xpath('ns22:Aziendaslim/CompanyInformation/Address/Municipality')[0];
                $impresa['indirizzo'] = (string) $controlledCompany->xpath('ns22:Aziendaslim/CompanyInformation/Address/Street')[0];
                $impresa['CAP'] = (string) $controlledCompany->xpath('ns22:Aziendaslim/CompanyInformation/Address/PostCode')[0];
                $impresa['titolo'] = 'COLLEGATA';
                $imprese[] = $impresa;
            }
            foreach ($sXML->xpath('/*/ns25:OfficialDirectors/Director') as $director) {
                $esponente = array();
                $esponente['nome'] = (string) $director->xpath('Individual/FirstName')[0];
                $esponente['cognome'] = (string) $director->xpath('Individual/LastName')[0];
                $esponente['codFisc'] = (string) $director->xpath('Individual/TaxCode')[0];
                $esponente['dataNas'] = substr((string) $director->xpath('Individual/BirthDate')[0], 0, 10);
                $esponente['luogoNas'] = (string) $director->xpath('Individual/BirthPlace')[0];
                $esponente['prov'] = (string) $director->xpath('Individual/ResidenceAddress/Province/@Code')[0];
                $esponente['comune'] = (string) $director->xpath('Individual/ResidenceAddress/Municipality')[0];
                $esponente['indirizzo'] = (string) $director->xpath('Individual/ResidenceAddress/Street')[0];
                $esponente['CAP'] = (string) $director->xpath('Individual/ResidenceAddress/PostCode')[0];
                $esponente['titolo'] = (string) $director->xpath('IndividualPosition/Type')[0];
                $esponenti[] = $esponente;
            }
            foreach ($sXML->xpath('/*/ns26:PreviousOfficialDirectors/Director') as $director) {
                $esponente = array();
                $esponente['nome'] = (string) $director->xpath('Individual/FirstName')[0];
                $esponente['cognome'] = (string) $director->xpath('Individual/LastName')[0];
                $esponente['codFisc'] = (string) $director->xpath('Individual/TaxCode')[0];
                $esponente['dataNas'] = substr((string) $director->xpath('Individual/BirthDate')[0], 0, 10);
                $esponente['luogoNas'] = (string) $director->xpath('Individual/BirthPlace')[0];
                $esponente['prov'] = (string) $director->xpath('Individual/ResidenceAddress/Province/@Code')[0];
                $esponente['comune'] = (string) $director->xpath('Individual/ResidenceAddress/Municipality')[0];
                $esponente['indirizzo'] = (string) $director->xpath('Individual/ResidenceAddress/Street')[0];
                $esponente['CAP'] = (string) $director->xpath('Individual/ResidenceAddress/PostCode')[0];
                $esponente['titolo'] = 'PRECEDENTE ' . (string) $director->xpath('IndividualPosition/Type')[0];
                $esponenti[] = $esponente;
            }
            foreach ($sXML->xpath('/*/ns27:PositionCompany/ns27:PositionRelatedCompany/Director') as $director) {
                $esponente = array();
                $esponente['nome'] = (string) $director->xpath('Individual/FirstName')[0];
                $esponente['cognome'] = (string) $director->xpath('Individual/LastName')[0];
                $esponente['codFisc'] = (string) $director->xpath('Individual/TaxCode')[0];
                $esponente['dataNas'] = substr((string) $director->xpath('Individual/BirthDate')[0], 0, 10);
                $esponente['luogoNas'] = (string) $director->xpath('Individual/BirthPlace')[0];
                $esponente['prov'] = (string) $director->xpath('Individual/ResidenceAddress/Province/@Code')[0];
                $esponente['comune'] = (string) $director->xpath('Individual/ResidenceAddress/Municipality')[0];
                $esponente['indirizzo'] = (string) $director->xpath('Individual/ResidenceAddress/Street')[0];
                $esponente['CAP'] = (string) $director->xpath('Individual/ResidenceAddress/PostCode')[0];
                $esponente['titolo'] = (string) $director->xpath('Position/Type')[0];
                $esponenti[] = $esponente;
            }
            foreach ($sXML->xpath('/*/ns27:PositionCompany/ns27:PositionRelatedCompany/RelatedCompany') as $controlledCompany) {
                $impresa = array();
                $impresa['ragSoc'] = (string) $controlledCompany->xpath('ns22:Aziendaslim/CompanyInformation/CompanyName')[0];
                $impresa['formaGiurid'] = (string) $controlledCompany->xpath('ns22:Aziendaslim/CompanyInformation/CompanyForm')[0];
                $impresa['partIVA'] = (string) $controlledCompany->xpath('ns22:Aziendaslim/CompanyInformation/TaxCode')[0];
                $impresa['capitaleSociale'] = (string) $controlledCompany->xpath('ns22:Aziendaslim/EquityCapital')[0];
                $impresa['statoAttivita'] = (string) $controlledCompany->xpath('ns22:Aziendaslim/ActivityDescription')[0];
                $impresa['provCCIAA'] = (string) $controlledCompany->xpath('ns22:Aziendaslim/ReaCode/CoCProvinceCode')[0];
                $impresa['nCCIAA'] = (string) $controlledCompany->xpath('ns22:Aziendaslim/ReaCode/REANo')[0];
                $impresa['prov'] = (string) $controlledCompany->xpath('ns22:Aziendaslim/CompanyInformation/Address/Province/@Code')[0];
                $impresa['comune'] = (string) $controlledCompany->xpath('ns22:Aziendaslim/CompanyInformation/Address/Municipality')[0];
                $impresa['indirizzo'] = (string) $controlledCompany->xpath('ns22:Aziendaslim/CompanyInformation/Address/Street')[0];
                $impresa['CAP'] = (string) $controlledCompany->xpath('ns22:Aziendaslim/CompanyInformation/Address/PostCode')[0];
                $impresa['titolo'] = 'CORRELATA';
                $imprese[] = $impresa;
            }
            restore_error_handler();
            if ($statOnly != '') {
                array_walk($dati, create_function('&$item', '$item=str_replace("\\"","",$item);'));
                if (isset($statHandler)) {
                    if (ftell($statHandler) == 0) {
                        fputcsv($statHandler, array_keys($dati));
                    }
                    fputcsv($statHandler, $dati);
                    fflush($statHandler);
                }
                $domXml = dom_import_simplexml($sXML);
                $aXML = array('_id' => $dati['id_mov_contab']);
                $this->xml2array($domXml, $aXML);
                /*$xml = new \SimpleXMLElement('<root/>');
                
                
                         $t*his->array_to_xml($aXML,$xml);*/
                if (true || is_array($aXML) && $totalCount++ < 10000.0) {
                    $bulk->update(array('_id' => $dati['id_mov_contab']), $aXML, array('multi' => false, 'upsert' => true));
                }
            }
            if ($dati['partIVA'] == '' || $dati['partIVA'] == 0 || $statOnly != '') {
                $richieste->next();
                continue;
            }
            $company->setData(array());
            $company->loadFromId($dati['partIVA']);
            $cDati = $company->getData();
            if (is_array($cDati) && count($cDati) > 0) {
                $company->setData(array_merge($cDati, array_filter($dati)));
                $company->update();
            } else {
                $company->setData($dati);
                $company->insert();
            }
            foreach ($imprese as $impresa) {
                $rCompany->setData(array());
                $rCompany->loadFromId($impresa['partIVA']);
                $cDati = $rCompany->getData();
                if (is_array($cDati) && count($cDati) > 0) {
                    $rCompany->setData(array_merge($cDati, array_filter($impresa)));
                    $rCompany->update();
                } else {
                    $rCompany->setData($impresa);
                    $rCompany->insert();
                }
                $company->addRelatedCompany($rCompany);
            }
            foreach ($esponenti as $esponente) {
                if (strlen($esponente['codFisc']) == 16) {
                    $person->setData(array());
                    $person->loadFromId($esponente['codFisc']);
                    $cDati = $person->getData();
                    if (is_array($cDati) && count($cDati) > 0) {
                        $person->setData(array_merge($cDati, array_filter($esponente)));
                        $person->update();
                    } else {
                        $person->setData($esponente);
                        $person->insert();
                    }
                    $company->addRelatedExponent($person);
                } else {
                    $rCompany->setData(array());
                    $esponente['partIVA'] = $esponente['codFisc'];
                    $rCompany->loadFromId($esponente['partIVA']);
                    $cDati = $rCompany->getData();
                    if (is_array($cDati) && count($cDati) > 0) {
                        $rCompany->setData(array_merge($cDati, array_filter($esponente)));
                        $rCompany->update();
                    } else {
                        $rCompany->setData($esponente);
                        $rCompany->insert();
                    }
                    $company->addRelatedCompany($rCompany);
                }
            }
            $richieste->next();
        }
        $this->db->query('ALTER TABLE company ENABLE KEYS', \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
        $this->db->query('ALTER TABLE company_to_company ENABLE KEYS', \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
        $this->db->query('ALTER TABLE person ENABLE KEYS', \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
        $this->db->query('ALTER TABLE person_to_company ENABLE KEYS', \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
        if (isset($statHandler)) {
            fclose($statHandler);
            if ($bulk->count() > 0) {
                $manager->executeBulkWrite('report_impresa.rep3p', $bulk);
            }
        }
        echo PHP_EOL;
    }
Example #7
0
    public function import($statOnly = '')
    {
        $dateFilter = '`data_ora_rich` > CURDATE() - INTERVAL 2 MONTH';
        if ($statOnly != '') {
            $config = clone $this->db->config;
            $configDb = $this->db->config->database->toArray();
            $configDb['options']['buffer_results'] = false;
            $this->db = new \Zend\Db\Adapter\Adapter($configDb);
            $this->db->config = $config;
            $dateFilter = 'TRUE';
            if ($this->db->config->storeExpiryStatFile->Catasto != '') {
                $statHandler = fopen($this->db->config->storeExpiryStatFile->Catasto, 'w');
            }
        }
        $archiviRichieste = array(array('dir' => 'risposteBaseDirMass', 'sql' => '
SELECT aps_prod_backup.eps_richiesta_mass.id_richiesta,
aps_prod_backup.eps_richiesta_mass.id_mov_contab,
aps_prod_backup.eps_richiesta_mass.id_richiesta_cache,
aps_prod_backup.eps_richiesta_mass.stringa_richiesta,
aps_prod_backup.eps_richiesta_mass.data_ora_rich,
aps_prod_backup.eps_richiesta_mass.data_ora_evas,
aps_prod.eps_operazione.cod_operazione as cod_operazione ,
aps_prod_backup.eps_mov_contab_mass.cod_causale as cod_causale ,
aps_prod_backup.eps_mov_contab_mass.importo as importo ,
aps_prod_backup.eps_mov_contab_mass.id_mov_contab_gen as id_mov_contab_gen ,
aps_prod.eps_prodotto.cod_prodotto as cod_prodotto ,
aps_prod.eps_cliente.cod_cliente as cod_cliente ,
aps_prod.eps_istanza.cod_istanza as cod_istanza
FROM aps_prod_backup.eps_richiesta_mass
LEFT JOIN aps_prod.eps_operazione ON aps_prod.eps_operazione.id_operazione=aps_prod_backup.eps_richiesta_mass.id_operazione
LEFT JOIN aps_prod_backup.eps_mov_contab_mass ON aps_prod_backup.eps_mov_contab_mass.id_mov_contab=aps_prod_backup.eps_richiesta_mass.id_mov_contab
LEFT JOIN aps_prod.eps_prodotto ON aps_prod.eps_prodotto.id_prodotto =aps_prod_backup.eps_mov_contab_mass.id_prodotto
LEFT JOIN aps_prod.eps_cliente ON aps_prod.eps_cliente.id_cliente=aps_prod_backup.eps_mov_contab_mass.id_cliente
LEFT JOIN aps_prod.eps_istanza ON aps_prod.eps_istanza.id_istanza=aps_prod.eps_cliente.id_istanza
WHERE
' . $dateFilter . ' AND (
   aps_prod.eps_operazione.cod_operazione = "omicf" OR 
   aps_prod.eps_operazione.cod_operazione = "omi" OR 
   aps_prod.eps_operazione.cod_operazione = "sintecf" OR
   aps_prod.eps_operazione.cod_operazione = "sintesi" OR
   aps_prod.eps_operazione.cod_operazione = "sintesi3" OR
   aps_prod.eps_operazione.cod_operazione = "visucf" OR
   aps_prod.eps_operazione.cod_operazione = "visura" OR
   aps_prod.eps_operazione.cod_operazione = "visura3"
)
                    '), array('dir' => 'risposteBaseDir', 'sql' => '
SELECT aps_prod_backup.eps_richiesta.id_richiesta,
aps_prod_backup.eps_richiesta.id_mov_contab,
aps_prod_backup.eps_richiesta.id_richiesta_cache,
aps_prod_backup.eps_richiesta.stringa_richiesta,
aps_prod_backup.eps_richiesta.data_ora_rich,
aps_prod_backup.eps_richiesta.data_ora_evas,
aps_prod.eps_operazione.cod_operazione as cod_operazione ,
aps_prod_backup.eps_mov_contab.cod_causale as cod_causale ,
aps_prod_backup.eps_mov_contab.importo as importo ,
aps_prod_backup.eps_mov_contab.id_mov_contab_gen as id_mov_contab_gen ,
aps_prod.eps_prodotto.cod_prodotto as cod_prodotto ,
aps_prod.eps_cliente.cod_cliente as cod_cliente ,
aps_prod.eps_istanza.cod_istanza as cod_istanza

FROM aps_prod_backup.eps_richiesta
LEFT JOIN aps_prod.eps_operazione ON aps_prod.eps_operazione.id_operazione=aps_prod_backup.eps_richiesta.id_operazione
LEFT JOIN aps_prod_backup.eps_mov_contab ON aps_prod_backup.eps_mov_contab.id_mov_contab=aps_prod_backup.eps_richiesta.id_mov_contab
LEFT JOIN aps_prod.eps_prodotto ON aps_prod.eps_prodotto.id_prodotto =aps_prod_backup.eps_mov_contab.id_prodotto
LEFT JOIN aps_prod.eps_cliente ON aps_prod.eps_cliente.id_cliente=aps_prod_backup.eps_mov_contab.id_cliente
LEFT JOIN aps_prod.eps_istanza ON aps_prod.eps_istanza.id_istanza=aps_prod.eps_cliente.id_istanza
WHERE  
' . $dateFilter . ' AND (
   aps_prod.eps_operazione.cod_operazione = "sintecf" OR aps_prod.eps_operazione.cod_operazione = "omicf" OR 
   aps_prod.eps_operazione.cod_operazione = "sintesi" OR aps_prod.eps_operazione.cod_operazione = "omi" OR 
   aps_prod.eps_operazione.cod_operazione = "sintecf" OR aps_prod.eps_operazione.cod_operazione = "sintecf" OR 
   aps_prod.eps_operazione.cod_operazione = "sintesi" OR aps_prod.eps_operazione.cod_operazione = "sintesi" OR 
   aps_prod.eps_operazione.cod_operazione = "sintesi3" OR aps_prod.eps_operazione.cod_operazione = "sintesi3" OR
   aps_prod.eps_operazione.cod_operazione = "sintecf" OR aps_prod.eps_operazione.cod_operazione = "visucf" OR
   aps_prod.eps_operazione.cod_operazione = "sintesi" OR aps_prod.eps_operazione.cod_operazione = "visura" OR 
   aps_prod.eps_operazione.cod_operazione = "sintesi3" OR aps_prod.eps_operazione.cod_operazione = "visura3" 
)
                    '));
        foreach ($archiviRichieste as $archivioRichieste) {
            $richieste = $this->db->query($archivioRichieste['sql'], \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
            $count = 0;
            $rCompany = new \crm\Company($this->db);
            $company = new \crm\Company($this->db);
            $person = new \crm\Person($this->db);
            $rPerson = new \crm\Person($this->db);
            while (is_object($richiesta = $richieste->current())) {
                echo "\r" . $count++ . '/' . ($richieste->count() - 1) . str_repeat(' ', 5);
                $dati = array();
                $sId = str_pad($richiesta->id_richiesta, 9, '0', STR_PAD_LEFT);
                $dM = 'M' . substr($sId, 0, 3);
                $dK = 'K' . substr($sId, 3, 3);
                $nomeFile = $this->db->config->{$archivioRichieste['dir']} . "/{$dM}/{$dK}/F{$sId}.xml";
                if (!is_file($nomeFile)) {
                    $richieste->next();
                    continue;
                }
                $dati['id_mov_contab'] = $richiesta->id_mov_contab;
                $dati['massiva'] = $archivioRichieste['dir'] == 'risposteBaseDir' ? 0 : 1;
                $dati['id_richiesta'] = $richiesta->id_richiesta;
                $dati['test_nome_file'] = "\"" . str_replace('"', '\\"', basename($nomeFile)) . "\"";
                $dati['errore'] = '';
                $dati['data_ora_rich'] = $richiesta->data_ora_rich;
                $dati['data_ora_evas'] = $richiesta->data_ora_evas;
                $dati['cod_operazione'] = $richiesta->cod_operazione;
                $dati['cod_prodotto'] = $richiesta->cod_prodotto;
                $dati['cod_cliente'] = $richiesta->cod_cliente;
                $dati['cod_istanza'] = $richiesta->cod_istanza;
                $dati['importo'] = $richiesta->importo;
                $dati['differita'] = 1;
                $dati['stornata'] = 0;
                if ($richiesta->importo == 0 && $richiesta->id_richiesta_cache == 0) {
                    $dati['stornata'] = 1;
                }
                set_error_handler(create_function('', ''));
                $filecontent = file_get_contents($nomeFile);
                $filecontent = iconv('ISO-8859-1', 'UTF-8//IGNORE', $filecontent);
                libxml_use_internal_errors(true);
                try {
                    $sXML = new \SimpleXMLElement($filecontent);
                } catch (Exception $e) {
                    var_dump(libxml_get_errors());
                    var_dump(file_get_contents($nomeFile));
                }
                $dati['dataADTI'] = (string) $sXML->xpath('/ADTIReport/time/data/anno')[0];
                $dati['dataADTI'] .= '-';
                $dati['dataADTI'] .= str_pad((string) $sXML->xpath('/ADTIReport/time/data/mese')[0], 2, '0', STR_PAD_LEFT);
                $dati['dataADTI'] .= '-';
                $dati['dataADTI'] .= str_pad((string) $sXML->xpath('/ADTIReport/time/data/giorno')[0], 2, '0', STR_PAD_LEFT);
                $dati['dataADTI'] .= ' ';
                $dati['dataADTI'] .= str_pad((string) $sXML->xpath('/ADTIReport/time/ora/ore')[0], 2, '0', STR_PAD_LEFT);
                $dati['dataADTI'] .= ':';
                $dati['dataADTI'] .= str_pad((string) $sXML->xpath('/ADTIReport/time/ora/minuti')[0], 2, '0', STR_PAD_LEFT);
                $dati['dataADTI'] .= ':';
                $dati['dataADTI'] .= str_pad((string) $sXML->xpath('/ADTIReport/time/ora/secondi')[0], 2, '0', STR_PAD_LEFT);
                if (strlen($dati['dataADTI']) < 6) {
                    $dati['dataADTI'] = '';
                }
                restore_error_handler();
                if ($statOnly != '') {
                    array_walk($dati, create_function('&$item', '$item=str_replace("\\"","",$item);'));
                    if (isset($statHandler)) {
                        if (ftell($statHandler) == 0) {
                            fputcsv($statHandler, array_keys($dati));
                        }
                        fputcsv($statHandler, $dati);
                        fflush($statHandler);
                    }
                }
                $soggetto = $sXML->xpath('/ADTIReport/visuraCatastale/elencoInfoCatasto/infoCatasto/soggetto');
                if (sizeof($soggetto) < 1 || $statOnly != '') {
                    $richieste->next();
                    continue;
                }
                $soggetto = $soggetto[0];
                $dati['codFisc'] = (string) $soggetto->codiceFiscale;
                if (strlen($dati['codFisc']) == 16) {
                    preg_match("/^(\\w+)/", $soggetto->denominazione, $matches);
                    $cognome = $matches[1];
                    $nome = trim(str_replace($cognome, '', $soggetto->denominazione));
                    $dati['nome'] = $nome;
                    $dati['cognome'] = $cognome;
                    $person->setData(array());
                    $person->loadFromId($dati['codFisc']);
                    $cDati = $person->getData();
                    if (is_array($cDati) && count($cDati) > 0) {
                        $person->setData(array_merge($cDati, array_filter($dati)));
                        $person->update();
                    } else {
                        $person->setData($dati);
                        $person->insert();
                    }
                } else {
                    if ($dati['codFisc'] != '') {
                        $dati['partIVA'] = $dati['codFisc'];
                        $dati['ragSoc'] = (string) $soggetto->denominazione;
                        $company->setData(array());
                        $company->loadFromId($dati['codFisc']);
                        $cDati = $company->getData();
                        if (is_array($cDati) && count($cDati) > 0) {
                            $company->setData(array_merge($cDati, array_filter($dati)));
                            $company->update();
                        } else {
                            $company->setData($dati);
                            $company->insert();
                        }
                    }
                }
                $correlati = $sXML->xpath('/ADTIReport/visuraCatastale/elencoInfoCatasto/infoCatasto/elencoUnitaImmobiliari/unitaImmobiliari/elencoIntestatari/intestatari/soggetto');
                if (!is_array($correlati)) {
                    $richieste->next();
                    continue;
                }
                foreach ($correlati as $correlato) {
                    $rDati = array();
                    $rDati['codFisc'] = (string) $correlato->codiceFiscale;
                    if (strlen($rDati['codFisc']) == 16) {
                        $rDati['nome'] = (string) $correlato->nominativo->nome;
                        $rDati['cognome'] = (string) $correlato->nominativo->cognome;
                        $rDati['dataNas'] = '';
                        if (is_object($correlato->datiNascita->data) && !is_null($correlato->datiNascita->data->anno)) {
                            $dateObject = date_date_set(new \DateTime(), (string) $correlato->datiNascita->data->anno, (string) $correlato->datiNascita->data->mese, (string) $correlato->datiNascita->data->giorno);
                            if (is_object($dateObject)) {
                                $rDati['dataNas'] = (string) date_format($dateObject, 'Y-m-d');
                            }
                        }
                        $rDati['luogoNas'] = '';
                        if (is_object($correlato->datiNascita->comune) && !is_null($correlato->datiNascita->comune->descrComune)) {
                            $rDati['luogoNas'] = trim(preg_replace('/\\(.*\\)/', '', (string) $correlato->datiNascita->comune->descrComune));
                        }
                        $rPerson->setData(array());
                        $rPerson->loadFromId($rDati['codFisc']);
                        $cDati = $rPerson->getData();
                        if (is_array($cDati) && count($cDati) > 0) {
                            $rPerson->setData(array_merge($cDati, array_filter($rDati)));
                            $rPerson->update();
                        } else {
                            $rPerson->setData($rDati);
                            $rPerson->insert();
                        }
                        if ($dati['codFisc'] == $rDati['codFisc']) {
                            continue;
                        }
                        if (strlen($dati['codFisc']) == 16) {
                            $person->addRelatedPerson($rPerson);
                        } else {
                            if ($dati['codFisc'] != '') {
                                $company->addRelatedExponent($rPerson);
                            }
                        }
                    } else {
                        if ($rDati['codFisc'] != '') {
                            $rDati['partIVA'] = $rDati['codFisc'];
                            $rDati['ragSoc'] = (string) $correlato->denominazione;
                            $rCompany->setData(array());
                            $rCompany->loadFromId($rDati['partIVA']);
                            $cDati = $rCompany->getData();
                            if (is_array($cDati) && count($cDati) > 0) {
                                $rCompany->setData(array_merge($cDati, array_filter($rDati)));
                                $rCompany->update();
                            } else {
                                $rCompany->setData($rDati);
                                $rCompany->insert();
                            }
                            if ($dati['codFisc'] == $rDati['codFisc']) {
                                continue;
                            }
                            if (strlen($dati['codFisc']) == 16) {
                                $rCompany->addRelatedExponent($person);
                            } else {
                                if ($dati['codFisc'] != '') {
                                    $rCompany->addRelatedCompany($company);
                                }
                            }
                        }
                    }
                }
                $richieste->next();
            }
        }
        if (isset($statHandler)) {
            fclose($statHandler);
        }
        echo PHP_EOL;
    }
Example #8
0
    public function import($statOnly = '')
    {
        $dateFilter = '`data_ora_rich` > CURDATE() - INTERVAL 2 MONTH';
        if ($statOnly != '') {
            $config = clone $this->db->config;
            $configDb = $this->db->config->database->toArray();
            $configDb['options']['buffer_results'] = false;
            $this->db = new \Zend\Db\Adapter\Adapter($configDb);
            $this->db->config = $config;
            $dateFilter = 'TRUE';
            if ($this->db->config->storeExpiryStatFile->AnaimpVis != '') {
                $statHandler = fopen($this->db->config->storeExpiryStatFile->AnaimpVis, 'w');
            }
            $manager = new \MongoDB\Driver\Manager("mongodb://*****:*****@Description');
                if (sizeof($activity) > 1) {
                    $dati['desISTAT'] = (string) $activity[0];
                }
                $dati['codNACE'] = '';
                $dati['dataInizioAtt'] = '';
                $dateObject = date_create_from_format('Ymd', (string) $companyData->RegisteredHeadOfficeIdentification->CompanyRegistryRegistrationDate);
                if (is_object($dateObject)) {
                    $dati['dataInizioAtt'] = date_format($dateObject, 'Y-m-d H:i:s');
                }
                $dati['dataCostituzione'] = '';
                $dati['dataREA'] = '';
                $dateObject = date_create_from_format('Ymd', (string) $companyData->RegisteredHeadOfficeIdentification->REARegistrationDate);
                if (is_object($dateObject)) {
                    $dati['dataREA'] = date_format($dateObject, 'Y-m-d H:i:s');
                }
                $dati['fidoAffidabilta'] = '';
                $dati['fidoAffidabiltaNote'] = '';
                $dati['fidoAccordabile'] = '';
                $dati['numUffSedi'] = '';
                $sedi = $sXML->xpath('/s2xResponse/s2xData/BranchList');
                if (is_array($sedi) && sizeof($sedi) > 0) {
                    $dati['numUffSedi'] = sizeof($sedi[0]);
                }
                $dati['desAttivita'] = '';
                $desAttivita = $sXML->xpath('/s2xResponse/s2xData/CompanyIdentification/CompanyPurpose');
                if (sizeof($desAttivita) > 0) {
                    $dati['desAttivita'] = strip_tags($desAttivita[0]->asXML());
                }
                $dati['eventi'] = '';
                $dati['protesti'] = '';
                $dati['fallimenti'] = '';
                $dati['pregiudizievoli'] = '';
                $dati['cassaIntegrazione'] = '';
                $dati['nDipendenti'] = '';
                $nDipendenti = $sXML->xpath('/s2xResponse/s2xData/CERVEDAddedValue/Employees/Number');
                if (is_array($nDipendenti) && sizeof($nDipendenti) > 0) {
                    $dati['nDipendenti'] = (string) $nDipendenti[0];
                }
                $dati['fatturato'] = '';
                $fatturato = $sXML->xpath('/s2xResponse/s2xData/CERVEDAddedValue/Revenue/Value');
                if (is_array($nDipendenti) && sizeof($nDipendenti) > 0) {
                    $dati['fatturato'] = (string) $fatturato[0];
                }
                $annofatturato = $sXML->xpath('/s2xResponse/s2xData/CERVEDAddedValue/Revenue/FinancialStatementDate/@year');
                if (is_array($annofatturato) && sizeof($annofatturato) > 0) {
                    $dati['annoFatturato'] = (string) $annofatturato[0];
                }
                $dati['trendFatturato'] = '';
                $dati['utilePerdita'] = '';
                $dati['patrimonioNetto'] = '';
                $dati['margineOperativoLordo'] = '';
                $dati['risultatoOperativoLordo'] = '';
                $dati['indiceIndebitamento'] = '';
                $dati['ROI'] = '';
                $dati['ROE'] = '';
                $dati['indiceLiquidita'] = '';
                $dati['dataChiusuraBilancio'] = '';
                foreach ($sXML->xpath('/s2xResponse/s2xData/Representatives/IndividualList/Individual') as $shareolder) {
                    if (property_exists($shareolder, 'PersonalData')) {
                        $esponente = array();
                        $esponente['nome'] = (string) $shareolder->PersonalData->FirstName;
                        $esponente['cognome'] = (string) $shareolder->PersonalData->LastName;
                        $esponente['codFisc'] = (string) $shareolder->PersonalData->TaxCode;
                        $esponente['dataNas'] = '';
                        $dateObject = date_create_from_format('Ymd', (string) $shareolder->PersonalData->Birth->BirthDate);
                        if (is_object($dateObject)) {
                            $esponente['dataNas'] = (string) date_format($dateObject, 'Y-m-d');
                        }
                        $esponente['luogoNas'] = (string) $shareolder->PersonalData->Birth->City;
                        $esponente['prov'] = (string) $shareolder->PersonalData->TaxDomicile->ProvinceCode;
                        $esponente['comune'] = (string) $shareolder->PersonalData->TaxDomicile->City;
                        $esponente['indirizzo'] = (string) $shareolder->PersonalData->TaxDomicile->ToponymCode . ' ' . (string) $shareolder->PersonalData->TaxDomicile->StreetName . ' ' . (string) $shareolder->PersonalData->TaxDomicile->StreetNo;
                        $esponente['CAP'] = (string) $shareolder->PersonalData->TaxDomicile->Postcode;
                        $esponente['titolo'] = 'ESPONENTE';
                        $esponenti[] = $esponente;
                    }
                }
                foreach ($sXML->xpath('/s2xResponse/s2xData/Representatives/EquivalentCompanyList/EquivalentCompany') as $shareolder) {
                    if (property_exists($shareolder, 'CompanyIdentification')) {
                        $impresa = array();
                        $impresa['ragSoc'] = (string) $shareolder->CompanyIdentification->CompanyName;
                        $impresa['formaGiurid'] = '';
                        $impresa['partIVA'] = (string) $shareolder->CompanyIdentification->TaxCode;
                        $impresa['capitaleSociale'] = '';
                        $impresa['statoAttivita'] = '';
                        $impresa['provCCIAA'] = (string) $shareolder->CompanyIdentification->REAProvinceCode;
                        $impresa['nCCIAA'] = (string) $shareolder->CompanyIdentification->REANo;
                        $impresa['prov'] = (string) $shareolder->CompanyIdentification->Address->ProvinceCode;
                        $impresa['comune'] = (string) $shareolder->CompanyIdentification->Address->City;
                        $impresa['indirizzo'] = (string) $shareolder->CompanyIdentification->Address->ToponymCode . ' ' . (string) $shareolder->CompanyIdentification->Address->StreetName . ' ' . (string) $shareolder->CompanyIdentification->Address->StreetNo;
                        $impresa['CAP'] = (string) $shareolder->CompanyIdentification->Address->PostCode;
                        $impresa['titolo'] = 'CORRELATA';
                        $imprese[] = $impresa;
                    }
                }
                restore_error_handler();
                if ($statOnly != '') {
                    array_walk($dati, create_function('&$item', '$item=str_replace("\\"","",$item);'));
                    if (isset($statHandler)) {
                        if (ftell($statHandler) == 0) {
                            fputcsv($statHandler, array_keys($dati));
                        }
                        fputcsv($statHandler, $dati);
                        fflush($statHandler);
                    }
                    $domXml = dom_import_simplexml($sXML);
                    $aXML = array('_id' => $dati['id_mov_contab']);
                    $this->xml2array($domXml, $aXML);
                    /*$xml = new \SimpleXMLElement('<root/>');
                    
                    
                                   $t*his->array_to_xml($aXML,$xml);*/
                    if (true || is_array($aXML) && $totalCount++ < 10000.0) {
                        $bulk->update(array('_id' => $dati['id_mov_contab']), $aXML, array('multi' => false, 'upsert' => true));
                    }
                }
                if ($dati['partIVA'] == '' || $dati['partIVA'] == 0 || $statOnly != '') {
                    $richieste->next();
                    continue;
                }
                $company->setData(array());
                $company->loadFromId($dati['partIVA']);
                $cDati = $company->getData();
                if (is_array($cDati) && count($cDati) > 0) {
                    $company->setData(array_merge($cDati, array_filter($dati)));
                    $company->update();
                } else {
                    $company->setData($dati);
                    $company->insert();
                }
                foreach ($imprese as $impresa) {
                    $rCompany->setData(array());
                    $rCompany->loadFromId($impresa['partIVA']);
                    $cDati = $rCompany->getData();
                    if (is_array($cDati) && count($cDati) > 0) {
                        $rCompany->setData(array_merge($cDati, array_filter($impresa)));
                        $rCompany->update();
                    } else {
                        $rCompany->setData($impresa);
                        $rCompany->insert();
                    }
                    $company->addRelatedCompany($rCompany);
                }
                foreach ($esponenti as $esponente) {
                    if (strlen($esponente['codFisc']) == 16) {
                        $person->setData(array());
                        $person->loadFromId($esponente['codFisc']);
                        $cDati = $person->getData();
                        if (is_array($cDati) && count($cDati) > 0) {
                            $person->setData(array_merge($cDati, array_filter($esponente)));
                            $person->update();
                        } else {
                            $person->setData($esponente);
                            $person->insert();
                        }
                        $company->addRelatedExponent($person);
                    } else {
                        $rCompany->setData(array());
                        $esponente['partIVA'] = $esponente['codFisc'];
                        $rCompany->loadFromId($esponente['partIVA']);
                        $cDati = $rCompany->getData();
                        if (is_array($cDati) && count($cDati) > 0) {
                            $rCompany->setData(array_merge($cDati, array_filter($esponente)));
                            $rCompany->update();
                        } else {
                            $rCompany->setData($esponente);
                            $rCompany->insert();
                        }
                        $company->addRelatedCompany($rCompany);
                    }
                }
                $richieste->next();
            }
        }
        $this->db->query('ALTER TABLE company ENABLE KEYS', \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
        $this->db->query('ALTER TABLE company_to_company ENABLE KEYS', \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
        $this->db->query('ALTER TABLE person ENABLE KEYS', \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
        $this->db->query('ALTER TABLE person_to_company ENABLE KEYS', \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
        if (isset($statHandler)) {
            fclose($statHandler);
            if ($bulk->count() > 0) {
                $manager->executeBulkWrite('report_impresa.anaimpvis', $bulk);
            }
        }
        echo PHP_EOL;
    }