Licitator 1.0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

436 lines
19 KiB

<?php
namespace Zitec\RuleEngineBundle\Command;
use Symfony\Component\Console\Command\Command as ContainerAwareCommand;;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Contable;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\IOFactory;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use DateTime;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use FOS\UserBundle\Model\UserInterface;
use FOS\UserBundle\Model\UserManagerInterface;
use App\Kernel;
use Symfony\Component\Intl\Countries;
class FAImporterCommand extends ContainerAwareCommand
{
private $em;
private $output;
private $sectores=[];
private $userManager;
private $tokenStorage;
protected $projectDir;
public function __construct(EntityManagerInterface $em,
UserManagerInterface $userManager,
TokenStorageInterface $tokenStorage,
Kernel $kernel)
{
parent::__construct();
$this->em = $em;
$this->userManager = $userManager;
$this->tokenStorage = $tokenStorage;
$this->projectDir = $kernel->getProjectDir();
}
/**
* {@inheritdoc}
*/
public function configure(): void
{
$this->setName('zitec:fa:importer');
$this->addArgument(
'fileOrigin',
InputArgument::REQUIRED,
'csv file to import'
);
$this->setDescription(
'Financial Actor Importer'
);
}
/**
* {@inheritdoc}
*/
public function execute(InputInterface $input, OutputInterface $output): int
{
$filename=$input->getArgument('fileOrigin');
$type = IOFactory::identify($filename);
$objReader = IOFactory::createReader($type);
$objPHPExcel = $objReader->load($filename);
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$worksheets[$worksheet->getTitle()] = $worksheet->toArray();
}
$this->sectores = $this->em->getRepository('App:Sector')->findAll();
$invalidos=[];
foreach($worksheets as $user){
$i = 0;
foreach($user as $geuser){
if($i==0){ $i++;continue;}
if (!empty($geuser[0]) || !empty($geuser[1])) {
$data=[];
$data['firstname']=$geuser[0];
$data['lastname']=$geuser[1];
$data['email']=$geuser[2];
$data['company_name']=$geuser[3];
$data['address']=$geuser[4];
$data['email_company']=$geuser[5];
$data['phone']=$geuser[6];
$data['website']=$geuser[7];
$data['typology']=$this->getTypology($geuser[8]);
$data['description']=$geuser[9];
$data['locale']=$this->getLocale($geuser[10]);
$data['geographical_scope']=$this->getLocales($geuser[11]);
$data['experience']=$geuser[12];
$data['instruments']=$geuser[13];
$data['average_amount']=$geuser[14];
$data['scope_sector']=$this->getSectors($geuser[15]);
$data['stages']=$this->getStages($geuser[16]);
$data['barriers']=$geuser[17];
$data['minimum_invest']=$geuser[18];
$data['products']=$geuser[19];
$data['gender']='u';
$data['dateOfBirth']=DateTime::createFromFormat('m/d/Y',$geuser[4]);
$repetido=$this->em->getRepository('App:User')->findOneByEmail($data['email']);
if(!empty($repetido) && $repetido->getType()!='financial_actor'){
$data['email']='rep'.$data['email'];
$repetido=$this->em->getRepository('App:User')->findOneByEmail($data['email']);
}
if(empty($repetido)){
$repetido=$this->em->getRepository('App:Contable')->findOneByEmail('rep'.$data['email']);
if(!empty($repetido)){
$data['email']='rep'.$data['email'];
}
}
echo (empty($repetido))?'No existe email':'Existe email';
if($this->checkemail($data['email']) && empty($repetido)){
$repetidoUser=$this->em->getRepository('App:User')->findOneByEmail($data['email']);
$repetidoUser2=$this->em->getRepository('App:User')->findOneByEmail('rep'.$data['email']);
echo 'No Repetido';
$user=new Contable();
$parts = explode("@", $data['email']);
$data['email']=$data['email'];
$data['username']=$data['email'];
if(!empty($parts[1])){
$data['plainPassword']=$parts[1];
}else{
die(print_r($data, true));
}
echo "\nCreando Usuario {$data['username']} \n";
$this->updateUser($user, $data);
}else{
if (empty($repetido)){
$repetido=new Contable();
}
$parts = explode("@", $data['email']);
$data['username']=$data['email'];
if(!empty($parts[1])){
$data['plainPassword']=$parts[1];
}else{
die(print_r($data, true));
}
$this->updateUser($repetido, $data);
echo "\nUsuario invalido {$data['firstname']} {$data['lastname']} \n";
$invalidos[]=$data;
}
}
}
}
/*
$spreadsheet = new Spreadsheet();
$spreadsheet->setActiveSheetIndex(0);
$sheet = $spreadsheet->getActiveSheet();
$keys= array_keys($invalidos[0]);
foreach($keys as $index=>$key){
$i=$index+1;
$sheet->setCellValue('A'.$i, $key);
}
$fila='A';
foreach($invalidos as $invalido){
++$fila;
$i=1;
foreach($invalido as $field){
$sheet->setCellValue($fila.$i, $field);
$i++;
}
}
$writer = IOFactory::createWriter($spreadsheet, 'Xls');
$writer->save('invalidos.xls');
$this->output=$output;
*/
exit(0);
}
private function getLocale($locale){
$local='EN';
switch($locale){
case 'Algeria': $local='DZ';break;
case 'Egypt':$local='EG';break;
case 'Israel':$local='IL';break;
case 'Jordan':$local='JO';break;
case 'Lebanon':$local='LB';break;
case 'Morocco':$local='MA';break;
case 'Palestine':$local='PS';break;
case 'Tunisia':$local='TN';break;
case 'Italy':$local='IT';break;
case 'Spain':$local='ES';break;
}
return $local;
}
private function getLocales($locale){
\Locale::setDefault('en');
$countries = Countries::getNames();
$local='EN';
$locales=explode(';', $locale);
$arraylocales=[];
foreach($locales as $localeitem){
switch($localeitem){
case 'Algeria': $arraylocales[]='DZ';break;
case 'Egypt':$arraylocales[]='EG';break;
case 'Israel':$arraylocales[]='IL';break;
case 'Jordan':$arraylocales[]='JO';break;
case 'Lebanon':$arraylocales[]='LB';break;
case 'Morocco':$arraylocales[]='MA';break;
case 'Palestine':$arraylocales[]='PS';break;
case 'Tunisia':$arraylocales[]='TN';break;
case 'Italy':$arraylocales[]='IT';break;
case 'Spain':$arraylocales[]='ES';break;
case 'MENA':$arraylocales[]='DZ';
$arraylocales[]='BH';//Barehin
$arraylocales[]='IR';//Iran
$arraylocales[]='IQ';//Irak
$arraylocales[]='KW';//Kuwait
$arraylocales[]='LY';//Lybia
$arraylocales[]='OM';// Oman
$arraylocales[]='SA'; //Saudio Arabia
$arraylocales[]='SY'; //Syria
$arraylocales[]='AE';//United arabian emirates
$arraylocales[]='YE';//Yemen
$arraylocales[]='QA';// Qatar
$arraylocales[]='EG';
$arraylocales[]='IL';
$arraylocales[]='JO';
$arraylocales[]='LB';
$arraylocales[]='MA';
$arraylocales[]='TN';
break;
case 'Global': $arraylocales= array_keys($countries);
break;
case 'South East Asia':
$arraylocales[]='BN';//Brunei,
$arraylocales[]='MM';// Burma (Myanmar)
$arraylocales[]='CV';// Cambodia,
$arraylocales[]='TL';// Timor-Leste,
$arraylocales[]='ID';// Indonesia,
$arraylocales[]='LA';// Laos,
$arraylocales[]='MY';// Malaysia,
$arraylocales[]='PH';// the Philippines,
$arraylocales[]='SG';// Singapore,
$arraylocales[]='TH';// Thailand and
$arraylocales[]='VN';// Vietnam
break;
}
}
return $arraylocales;
}
private function getSectors($sectorese){
$array=[];
$sectorsarray=explode(';',$sectorese);
if (strpos($sectorese, 'All sectors') !== false){
return $this->sectores;
}
foreach($sectorsarray as $sector){
$sector=strtolower($sector);
foreach($this->sectores as $s){
if (strpos($sector, 'food') !== false && strpos($s->getNombre(), 'food') !== false) {
$array[]=$s;
}
if(strpos($sector, 'enewable') !== false && strpos($s->getNombre(), 'Renewable') !== false) {
$array[]=$s;
}
if(strpos($sector, 'waste') !== false && strpos($s->getNombre(), 'waste') !== false) {
$array[]=$s;
}
if(strpos($sector, 'tourism') !== false && strpos($s->getNombre(), 'tourism') !== false) {
$array[]=$s;
}
if(strpos($sector, 'construction') !== false && strpos($s->getNombre(), 'construction') !== false) {
$array[]=$s;
}
if(strpos($sector, 'textile') !== false && strpos($s->getNombre(), 'textile') !== false) {
$array[]=$s;
}
if(strpos($sector, 'mobility') !== false && strpos($s->getNombre(), 'mobility') !== false) {
$array[]=$s;
}
if(strpos($sector, 'cleaning') !== false && strpos($s->getNombre(), 'cleaning') !== false) {
$array[]=$s;
}
if(strpos($sector, 'furniture') !== false && strpos($s->getNombre(), 'furniture') !== false) {
$array[]=$s;
}
if(strpos($sector, 'electrical') !== false && strpos($s->getNombre(), 'electrical') !== false) {
$array[]=$s;
}
if(strpos($sector, 'Communication') !== false && strpos($s->getNombre(), 'Communication') !== false) {
$array[]=$s;
}
}
}
return $array;
}
private function getTypology($str){
$typologies=explode(';',$str);
$arraytypologies=[];
foreach($typologies as $typo){
if (strpos($typo, 'Accelerator') !== false) {
$arraytypologies[]='accel';
}
if (strpos($typo, 'Bank') !== false) {
$arraytypologies[]='bank';
}
if (strpos($typo, 'Business Angels') !== false) {
$arraytypologies[]='ba';
}
if (strpos($typo, 'Capital risque') !== false) {
$arraytypologies[]='cr';
}
if (strpos($typo, 'Corporate Investment') !== false) {
$arraytypologies[]='ci';
}
if (strpos($typo, 'Crowd Equity') !== false) {
$arraytypologies[]='ce';
}
if (strpos($typo, 'Crowdfunding') !== false) {
$arraytypologies[]='crow';
}
if (strpos($typo, 'Equity') !== false) {
$arraytypologies[]='equity';
}
if (strpos($typo, 'Grants') !== false) {
$arraytypologies[]='grants';
}
if (strpos($typo, 'Guarantee fund') !== false) {
$arraytypologies[]='gf';
}
if (strpos($typo, 'Impact Investment') !== false) {
$arraytypologies[]='ii';
}
if (strpos($typo, 'Incubator') !== false) {
$arraytypologies[]='incubator';
}
if (strpos($typo, 'Loans') !== false) {
$arraytypologies[]='loans';
}
if (strpos($typo, 'Microfinance Institution') !== false) {
$arraytypologies[]='mi';
}
if (strpos($typo, 'Private Equity') !== false) {
$arraytypologies[]='pi';
}
if (strpos($typo, 'Private Foundation') !== false) {
$arraytypologies[]='pf';
}
if (strpos($typo, 'Public Opportunity') !== false || strpos($typo, 'Public opportunity') !== false) {
$arraytypologies[]='po';
}
if (strpos($typo, 'Venture Capital') !== false) {
$arraytypologies[]='vc';
}
if (strpos($typo, 'Capital Investissement') !== false) {
$arraytypologies[]='ci';
}
if (strpos($typo, 'Venture Philanthropy') !== false) {
$arraytypologies[]='vp';
}
}
return $arraytypologies;
}
private function getStages($str){
$typologies=explode(';',$str);
$array=[];
foreach($typologies as $typo){
if (strpos($typo, 'Ideation') !== false) {
$array[]='ideation_stage';
}
if (strpos($typo, 'Early') !== false) {
$array[]='early_stage';
}
if (strpos($typo, 'Growth') !== false) {
$array[]='growth';
}
if (strpos($typo, 'Scale') !== false) {
$array[]='scale';
}
}
return $array;
}
private function checkemail($str) {
return (!preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
}
private function updateUser($user, $data){
$user->setUsername($data['username']);
$user->setUsernameCanonical($data['username']);
$user->setEmail($data['email']);
$user->setEmailCanonical($data['email']);
$user->setPlainPassword($data['plainPassword']);
$user->setFirstname($data['firstname']);
$user->setLastname($data['lastname']);
$user->setLocale($data['locale']);
$user->setGender($data['gender']);
$user->setCompanyName($data['company_name']);
$user->setAddress($data['address']);
$user->setPhone($data['phone']);
$user->setWebsite($data['website']);
$user->setTypology($data['typology']);
$user->setBiography($data['description']);
$user->setActivities($data['geographical_scope']);
if(!empty($data['experience']) && $data['experience']!='Non' && $data['experience']!='N/A'){
$user->setPreviousExperience(true);
}else{
$user->setPreviousExperience(false);
}
$user->setInstruments($data['instruments']);
if(!empty($data['average_amount'])){
$user->setAverageAmount($data['average_amount']);
}
foreach($data['scope_sector'] as $sector){
$user->addScopeOperations($sector);
}
$user->setInvestmentStages($data['stages']);
$user->setBarriers($data['barriers']);
if(!empty($data['minimum_invest'])){
$user->setMinimumInvestment($data['minimum_invest']);
}
$user->setAvailableProducts($data['products']);
if(!empty($data['dateOfBirth'])){
$user->setDateOfBirth($data['dateOfBirth']);
}
$user->setNew(1);
$user->setEnabled(1);
// $this->em->persist($user);
// $this->em->flush();
echo "\nUsuario {$data['username']} creado\n";
$this->userManager->updateUser($user);
$token = new UsernamePasswordToken($user, $data['plainPassword'], 'admin', $user->getRoles());
$this->tokenStorage->setToken($token);
//$this->container->get('security.token_storage')->setToken($token);
// $this->container->get('session')->set('_security_main', serialize($token));
if(!file_exists($this->projectDir.'/public/uploads/'.$user->getId())){
mkdir($this->projectDir.'/public/uploads/'.$user->getId());
}
}
}