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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace Zitec\RuleEngineBundle\Command;
  3. use Symfony\Component\Console\Command\Command as ContainerAwareCommand;;
  4. use Symfony\Component\Console\Input\InputArgument;
  5. use Symfony\Component\Console\Input\InputInterface;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use App\Entity\Contable;
  9. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  10. use PhpOffice\PhpSpreadsheet\IOFactory;
  11. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  12. use DateTime;
  13. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  14. use FOS\UserBundle\Model\UserInterface;
  15. use FOS\UserBundle\Model\UserManagerInterface;
  16. use App\Kernel;
  17. use Symfony\Component\Intl\Countries;
  18. class FAImporterCommand extends ContainerAwareCommand
  19. {
  20. private $em;
  21. private $output;
  22. private $sectores=[];
  23. private $userManager;
  24. private $tokenStorage;
  25. protected $projectDir;
  26. public function __construct(EntityManagerInterface $em,
  27. UserManagerInterface $userManager,
  28. TokenStorageInterface $tokenStorage,
  29. Kernel $kernel)
  30. {
  31. parent::__construct();
  32. $this->em = $em;
  33. $this->userManager = $userManager;
  34. $this->tokenStorage = $tokenStorage;
  35. $this->projectDir = $kernel->getProjectDir();
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function configure(): void
  41. {
  42. $this->setName('zitec:fa:importer');
  43. $this->addArgument(
  44. 'fileOrigin',
  45. InputArgument::REQUIRED,
  46. 'csv file to import'
  47. );
  48. $this->setDescription(
  49. 'Financial Actor Importer'
  50. );
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function execute(InputInterface $input, OutputInterface $output): int
  56. {
  57. $filename=$input->getArgument('fileOrigin');
  58. $type = IOFactory::identify($filename);
  59. $objReader = IOFactory::createReader($type);
  60. $objPHPExcel = $objReader->load($filename);
  61. foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
  62. $worksheets[$worksheet->getTitle()] = $worksheet->toArray();
  63. }
  64. $this->sectores = $this->em->getRepository('App:Sector')->findAll();
  65. $invalidos=[];
  66. foreach($worksheets as $user){
  67. $i = 0;
  68. foreach($user as $geuser){
  69. if($i==0){ $i++;continue;}
  70. if (!empty($geuser[0]) || !empty($geuser[1])) {
  71. $data=[];
  72. $data['firstname']=$geuser[0];
  73. $data['lastname']=$geuser[1];
  74. $data['email']=$geuser[2];
  75. $data['company_name']=$geuser[3];
  76. $data['address']=$geuser[4];
  77. $data['email_company']=$geuser[5];
  78. $data['phone']=$geuser[6];
  79. $data['website']=$geuser[7];
  80. $data['typology']=$this->getTypology($geuser[8]);
  81. $data['description']=$geuser[9];
  82. $data['locale']=$this->getLocale($geuser[10]);
  83. $data['geographical_scope']=$this->getLocales($geuser[11]);
  84. $data['experience']=$geuser[12];
  85. $data['instruments']=$geuser[13];
  86. $data['average_amount']=$geuser[14];
  87. $data['scope_sector']=$this->getSectors($geuser[15]);
  88. $data['stages']=$this->getStages($geuser[16]);
  89. $data['barriers']=$geuser[17];
  90. $data['minimum_invest']=$geuser[18];
  91. $data['products']=$geuser[19];
  92. $data['gender']='u';
  93. $data['dateOfBirth']=DateTime::createFromFormat('m/d/Y',$geuser[4]);
  94. $repetido=$this->em->getRepository('App:User')->findOneByEmail($data['email']);
  95. if(!empty($repetido) && $repetido->getType()!='financial_actor'){
  96. $data['email']='rep'.$data['email'];
  97. $repetido=$this->em->getRepository('App:User')->findOneByEmail($data['email']);
  98. }
  99. if(empty($repetido)){
  100. $repetido=$this->em->getRepository('App:Contable')->findOneByEmail('rep'.$data['email']);
  101. if(!empty($repetido)){
  102. $data['email']='rep'.$data['email'];
  103. }
  104. }
  105. echo (empty($repetido))?'No existe email':'Existe email';
  106. if($this->checkemail($data['email']) && empty($repetido)){
  107. $repetidoUser=$this->em->getRepository('App:User')->findOneByEmail($data['email']);
  108. $repetidoUser2=$this->em->getRepository('App:User')->findOneByEmail('rep'.$data['email']);
  109. echo 'No Repetido';
  110. $user=new Contable();
  111. $parts = explode("@", $data['email']);
  112. $data['email']=$data['email'];
  113. $data['username']=$data['email'];
  114. if(!empty($parts[1])){
  115. $data['plainPassword']=$parts[1];
  116. }else{
  117. die(print_r($data, true));
  118. }
  119. echo "\nCreando Usuario {$data['username']} \n";
  120. $this->updateUser($user, $data);
  121. }else{
  122. if (empty($repetido)){
  123. $repetido=new Contable();
  124. }
  125. $parts = explode("@", $data['email']);
  126. $data['username']=$data['email'];
  127. if(!empty($parts[1])){
  128. $data['plainPassword']=$parts[1];
  129. }else{
  130. die(print_r($data, true));
  131. }
  132. $this->updateUser($repetido, $data);
  133. echo "\nUsuario invalido {$data['firstname']} {$data['lastname']} \n";
  134. $invalidos[]=$data;
  135. }
  136. }
  137. }
  138. }
  139. /*
  140. $spreadsheet = new Spreadsheet();
  141. $spreadsheet->setActiveSheetIndex(0);
  142. $sheet = $spreadsheet->getActiveSheet();
  143. $keys= array_keys($invalidos[0]);
  144. foreach($keys as $index=>$key){
  145. $i=$index+1;
  146. $sheet->setCellValue('A'.$i, $key);
  147. }
  148. $fila='A';
  149. foreach($invalidos as $invalido){
  150. ++$fila;
  151. $i=1;
  152. foreach($invalido as $field){
  153. $sheet->setCellValue($fila.$i, $field);
  154. $i++;
  155. }
  156. }
  157. $writer = IOFactory::createWriter($spreadsheet, 'Xls');
  158. $writer->save('invalidos.xls');
  159. $this->output=$output;
  160. */
  161. exit(0);
  162. }
  163. private function getLocale($locale){
  164. $local='EN';
  165. switch($locale){
  166. case 'Algeria': $local='DZ';break;
  167. case 'Egypt':$local='EG';break;
  168. case 'Israel':$local='IL';break;
  169. case 'Jordan':$local='JO';break;
  170. case 'Lebanon':$local='LB';break;
  171. case 'Morocco':$local='MA';break;
  172. case 'Palestine':$local='PS';break;
  173. case 'Tunisia':$local='TN';break;
  174. case 'Italy':$local='IT';break;
  175. case 'Spain':$local='ES';break;
  176. }
  177. return $local;
  178. }
  179. private function getLocales($locale){
  180. \Locale::setDefault('en');
  181. $countries = Countries::getNames();
  182. $local='EN';
  183. $locales=explode(';', $locale);
  184. $arraylocales=[];
  185. foreach($locales as $localeitem){
  186. switch($localeitem){
  187. case 'Algeria': $arraylocales[]='DZ';break;
  188. case 'Egypt':$arraylocales[]='EG';break;
  189. case 'Israel':$arraylocales[]='IL';break;
  190. case 'Jordan':$arraylocales[]='JO';break;
  191. case 'Lebanon':$arraylocales[]='LB';break;
  192. case 'Morocco':$arraylocales[]='MA';break;
  193. case 'Palestine':$arraylocales[]='PS';break;
  194. case 'Tunisia':$arraylocales[]='TN';break;
  195. case 'Italy':$arraylocales[]='IT';break;
  196. case 'Spain':$arraylocales[]='ES';break;
  197. case 'MENA':$arraylocales[]='DZ';
  198. $arraylocales[]='BH';//Barehin
  199. $arraylocales[]='IR';//Iran
  200. $arraylocales[]='IQ';//Irak
  201. $arraylocales[]='KW';//Kuwait
  202. $arraylocales[]='LY';//Lybia
  203. $arraylocales[]='OM';// Oman
  204. $arraylocales[]='SA'; //Saudio Arabia
  205. $arraylocales[]='SY'; //Syria
  206. $arraylocales[]='AE';//United arabian emirates
  207. $arraylocales[]='YE';//Yemen
  208. $arraylocales[]='QA';// Qatar
  209. $arraylocales[]='EG';
  210. $arraylocales[]='IL';
  211. $arraylocales[]='JO';
  212. $arraylocales[]='LB';
  213. $arraylocales[]='MA';
  214. $arraylocales[]='TN';
  215. break;
  216. case 'Global': $arraylocales= array_keys($countries);
  217. break;
  218. case 'South East Asia':
  219. $arraylocales[]='BN';//Brunei,
  220. $arraylocales[]='MM';// Burma (Myanmar)
  221. $arraylocales[]='CV';// Cambodia,
  222. $arraylocales[]='TL';// Timor-Leste,
  223. $arraylocales[]='ID';// Indonesia,
  224. $arraylocales[]='LA';// Laos,
  225. $arraylocales[]='MY';// Malaysia,
  226. $arraylocales[]='PH';// the Philippines,
  227. $arraylocales[]='SG';// Singapore,
  228. $arraylocales[]='TH';// Thailand and
  229. $arraylocales[]='VN';// Vietnam
  230. break;
  231. }
  232. }
  233. return $arraylocales;
  234. }
  235. private function getSectors($sectorese){
  236. $array=[];
  237. $sectorsarray=explode(';',$sectorese);
  238. if (strpos($sectorese, 'All sectors') !== false){
  239. return $this->sectores;
  240. }
  241. foreach($sectorsarray as $sector){
  242. $sector=strtolower($sector);
  243. foreach($this->sectores as $s){
  244. if (strpos($sector, 'food') !== false && strpos($s->getNombre(), 'food') !== false) {
  245. $array[]=$s;
  246. }
  247. if(strpos($sector, 'enewable') !== false && strpos($s->getNombre(), 'Renewable') !== false) {
  248. $array[]=$s;
  249. }
  250. if(strpos($sector, 'waste') !== false && strpos($s->getNombre(), 'waste') !== false) {
  251. $array[]=$s;
  252. }
  253. if(strpos($sector, 'tourism') !== false && strpos($s->getNombre(), 'tourism') !== false) {
  254. $array[]=$s;
  255. }
  256. if(strpos($sector, 'construction') !== false && strpos($s->getNombre(), 'construction') !== false) {
  257. $array[]=$s;
  258. }
  259. if(strpos($sector, 'textile') !== false && strpos($s->getNombre(), 'textile') !== false) {
  260. $array[]=$s;
  261. }
  262. if(strpos($sector, 'mobility') !== false && strpos($s->getNombre(), 'mobility') !== false) {
  263. $array[]=$s;
  264. }
  265. if(strpos($sector, 'cleaning') !== false && strpos($s->getNombre(), 'cleaning') !== false) {
  266. $array[]=$s;
  267. }
  268. if(strpos($sector, 'furniture') !== false && strpos($s->getNombre(), 'furniture') !== false) {
  269. $array[]=$s;
  270. }
  271. if(strpos($sector, 'electrical') !== false && strpos($s->getNombre(), 'electrical') !== false) {
  272. $array[]=$s;
  273. }
  274. if(strpos($sector, 'Communication') !== false && strpos($s->getNombre(), 'Communication') !== false) {
  275. $array[]=$s;
  276. }
  277. }
  278. }
  279. return $array;
  280. }
  281. private function getTypology($str){
  282. $typologies=explode(';',$str);
  283. $arraytypologies=[];
  284. foreach($typologies as $typo){
  285. if (strpos($typo, 'Accelerator') !== false) {
  286. $arraytypologies[]='accel';
  287. }
  288. if (strpos($typo, 'Bank') !== false) {
  289. $arraytypologies[]='bank';
  290. }
  291. if (strpos($typo, 'Business Angels') !== false) {
  292. $arraytypologies[]='ba';
  293. }
  294. if (strpos($typo, 'Capital risque') !== false) {
  295. $arraytypologies[]='cr';
  296. }
  297. if (strpos($typo, 'Corporate Investment') !== false) {
  298. $arraytypologies[]='ci';
  299. }
  300. if (strpos($typo, 'Crowd Equity') !== false) {
  301. $arraytypologies[]='ce';
  302. }
  303. if (strpos($typo, 'Crowdfunding') !== false) {
  304. $arraytypologies[]='crow';
  305. }
  306. if (strpos($typo, 'Equity') !== false) {
  307. $arraytypologies[]='equity';
  308. }
  309. if (strpos($typo, 'Grants') !== false) {
  310. $arraytypologies[]='grants';
  311. }
  312. if (strpos($typo, 'Guarantee fund') !== false) {
  313. $arraytypologies[]='gf';
  314. }
  315. if (strpos($typo, 'Impact Investment') !== false) {
  316. $arraytypologies[]='ii';
  317. }
  318. if (strpos($typo, 'Incubator') !== false) {
  319. $arraytypologies[]='incubator';
  320. }
  321. if (strpos($typo, 'Loans') !== false) {
  322. $arraytypologies[]='loans';
  323. }
  324. if (strpos($typo, 'Microfinance Institution') !== false) {
  325. $arraytypologies[]='mi';
  326. }
  327. if (strpos($typo, 'Private Equity') !== false) {
  328. $arraytypologies[]='pi';
  329. }
  330. if (strpos($typo, 'Private Foundation') !== false) {
  331. $arraytypologies[]='pf';
  332. }
  333. if (strpos($typo, 'Public Opportunity') !== false || strpos($typo, 'Public opportunity') !== false) {
  334. $arraytypologies[]='po';
  335. }
  336. if (strpos($typo, 'Venture Capital') !== false) {
  337. $arraytypologies[]='vc';
  338. }
  339. if (strpos($typo, 'Capital Investissement') !== false) {
  340. $arraytypologies[]='ci';
  341. }
  342. if (strpos($typo, 'Venture Philanthropy') !== false) {
  343. $arraytypologies[]='vp';
  344. }
  345. }
  346. return $arraytypologies;
  347. }
  348. private function getStages($str){
  349. $typologies=explode(';',$str);
  350. $array=[];
  351. foreach($typologies as $typo){
  352. if (strpos($typo, 'Ideation') !== false) {
  353. $array[]='ideation_stage';
  354. }
  355. if (strpos($typo, 'Early') !== false) {
  356. $array[]='early_stage';
  357. }
  358. if (strpos($typo, 'Growth') !== false) {
  359. $array[]='growth';
  360. }
  361. if (strpos($typo, 'Scale') !== false) {
  362. $array[]='scale';
  363. }
  364. }
  365. return $array;
  366. }
  367. private function checkemail($str) {
  368. return (!preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
  369. }
  370. private function updateUser($user, $data){
  371. $user->setUsername($data['username']);
  372. $user->setUsernameCanonical($data['username']);
  373. $user->setEmail($data['email']);
  374. $user->setEmailCanonical($data['email']);
  375. $user->setPlainPassword($data['plainPassword']);
  376. $user->setFirstname($data['firstname']);
  377. $user->setLastname($data['lastname']);
  378. $user->setLocale($data['locale']);
  379. $user->setGender($data['gender']);
  380. $user->setCompanyName($data['company_name']);
  381. $user->setAddress($data['address']);
  382. $user->setPhone($data['phone']);
  383. $user->setWebsite($data['website']);
  384. $user->setTypology($data['typology']);
  385. $user->setBiography($data['description']);
  386. $user->setActivities($data['geographical_scope']);
  387. if(!empty($data['experience']) && $data['experience']!='Non' && $data['experience']!='N/A'){
  388. $user->setPreviousExperience(true);
  389. }else{
  390. $user->setPreviousExperience(false);
  391. }
  392. $user->setInstruments($data['instruments']);
  393. if(!empty($data['average_amount'])){
  394. $user->setAverageAmount($data['average_amount']);
  395. }
  396. foreach($data['scope_sector'] as $sector){
  397. $user->addScopeOperations($sector);
  398. }
  399. $user->setInvestmentStages($data['stages']);
  400. $user->setBarriers($data['barriers']);
  401. if(!empty($data['minimum_invest'])){
  402. $user->setMinimumInvestment($data['minimum_invest']);
  403. }
  404. $user->setAvailableProducts($data['products']);
  405. if(!empty($data['dateOfBirth'])){
  406. $user->setDateOfBirth($data['dateOfBirth']);
  407. }
  408. $user->setNew(1);
  409. $user->setEnabled(1);
  410. // $this->em->persist($user);
  411. // $this->em->flush();
  412. echo "\nUsuario {$data['username']} creado\n";
  413. $this->userManager->updateUser($user);
  414. $token = new UsernamePasswordToken($user, $data['plainPassword'], 'admin', $user->getRoles());
  415. $this->tokenStorage->setToken($token);
  416. //$this->container->get('security.token_storage')->setToken($token);
  417. // $this->container->get('session')->set('_security_main', serialize($token));
  418. if(!file_exists($this->projectDir.'/public/uploads/'.$user->getId())){
  419. mkdir($this->projectDir.'/public/uploads/'.$user->getId());
  420. }
  421. }
  422. }