public function importarArticulos()
 {
     $reqs = Req::whereBetween('req', $this->arr_rango)->get(['id', 'req', 'estatus']);
     foreach ($reqs as $req) {
         $articulos_legacy = $this->consultarArticulosLegacy($req->req);
         foreach ($articulos_legacy as $art_legacy) {
             if (!empty($art_legacy->oc)) {
                 $oc_id = Oc::whereOc($art_legacy->oc)->pluck('id');
                 if (empty($oc_id)) {
                     $oc_id = 0;
                 }
             } else {
                 $oc_id = 0;
             }
             $articulo = new Articulo();
             $articulo->req_id = $req->id;
             $articulo->articulo = $art_legacy->art . ' ' . $art_legacy->esp;
             $articulo->cantidad = $art_legacy->cantidad;
             $articulo->impuesto = $art_legacy->impuesto;
             $articulo->monto = $art_legacy->monto;
             $articulo->oc_id = $oc_id;
             $articulo->unidad = $art_legacy->unidad;
             $articulo->inventariable = $art_legacy->alta;
             $articulo->save();
             if (!empty($art_legacy->rm)) {
                 $rm_id = Rm::whereRm($art_legacy->rm)->value('id');
                 $articulo->rms()->attach($rm_id, ['monto' => $art_legacy->monto]);
             }
         }
     }
 }
 public function importarCompensaciones()
 {
     $cambios_cuenta = $this->consultaCambioCuenta();
     foreach ($cambios_cuenta as $cambio) {
         $cambio_origenes = $this->consultaCambioOrigen($cambio->cambio_id);
         $cambio_destinos = $this->consultaCambioDestino($cambio->cambio_id);
         $compensa_rm = CompensaRm::create(['fecha' => $cambio->fecha, 'tipo' => $cambio->tipo]);
         if (count($cambio_origenes) > 0) {
             foreach ($cambio_origenes as $origen) {
                 $origen_rm_id = Rm::whereRm($origen->rm_origen)->value('id');
                 $compensa_origen = new CompensaOrigen(['rm_id' => $origen_rm_id, 'monto' => $origen->monto]);
                 $compensa_rm->compensaOrigenes()->save($compensa_origen);
             }
         }
         if (count($cambio_destinos) > 0) {
             foreach ($cambio_destinos as $destino) {
                 $destino_rm_id = Rm::whereRm($destino->rm_destino)->value('id');
                 $compensa_destino = new CompensaDestino(['rm_id' => $destino_rm_id, 'monto' => $destino->monto]);
                 $compensa_rm->compensaDestinos()->save($compensa_destino);
             }
         }
         if ($cambio->tipo == 'ExternaIngreso' || $cambio->tipo == 'ExternaEgreso') {
             $legacy_comp_ext = $this->consultaCompensaExterna($cambio->cambio_id);
             $urg_externa_id = UrgExterna::whereUrgExterna($legacy_comp_ext->ures_ext)->pluck('id');
             if (empty($urg_externa_id)) {
                 $urg_externa_id = UrgExterna::whereUrgExterna('2.2')->pluck('id');
             }
             $compensa_externa = new CompensaExterna(['urg_externa_id' => $urg_externa_id, 'tipo' => $legacy_comp_ext->tipo, 'concepto' => $legacy_comp_ext->concepto]);
             $compensa_rm->compensaExternas()->save($compensa_externa);
         }
     }
 }
 private function crearEgreso($e_legacy)
 {
     $benef_id = $this->getBenefId($e_legacy->benef_id);
     $cuenta_id = $this->getCuentaId($e_legacy->concepto);
     $user_id = $this->getUserId($e_legacy->responsable);
     $egreso = Egreso::create(['cuenta_bancaria_id' => $this->cuenta_bancaria->id, 'poliza' => $e_legacy->egreso_id, 'cheque' => 0, 'fecha' => $e_legacy->fecha, 'benef_id' => $benef_id, 'cuenta_id' => $cuenta_id, 'concepto' => '(' . $e_legacy->concepto . ') ' . $e_legacy->cmt, 'monto' => round($e_legacy->monto, 2), 'estatus' => $e_legacy->estatus, 'user_id' => $user_id]);
     //Presupuesto: Importar RMs
     if ($cuenta_id == 1 && $this->db_origen == 'legacy') {
         $egreso_rms_legacy = $this->getEgresoRmLegacy($e_legacy->egreso_id);
         foreach ($egreso_rms_legacy as $eg_rm) {
             $rm_id = Rm::whereRm($eg_rm->rm)->pluck('id');
             $egreso->rms()->attach($rm_id, ['monto' => $eg_rm->monto]);
         }
     }
 }
 private function crearCheque($ch_legacy)
 {
     $benef_id = $this->getBenefId($ch_legacy->benef_id);
     $cuenta_id = $this->getCuentaId($ch_legacy->concepto);
     $user_id = $this->getUserId($ch_legacy->responsable);
     //Cancelado: Crear Poliza de cancelación
     $cuenta_id == 27 ? $estatus = 'CANCELADO' : ($estatus = $ch_legacy->estatus);
     $egreso = Egreso::create(['cuenta_bancaria_id' => $this->cuenta_bancaria->id, 'poliza' => 0, 'cheque' => $ch_legacy->cheque, 'fecha' => $ch_legacy->fecha, 'benef_id' => $benef_id, 'cuenta_id' => $cuenta_id, 'concepto' => '(' . $ch_legacy->concepto . ') ' . $ch_legacy->cmt, 'monto' => round($ch_legacy->monto, 2), 'estatus' => $estatus, 'user_id' => $user_id, 'fecha_cobro' => $ch_legacy->fecha_cobro]);
     //Presupuesto: Importar RMs
     if ($cuenta_id == 1 && $this->db_origen == 'legacy') {
         $ch_rms_legacy = $this->getChequeRmLegacy($egreso->cheque);
         foreach ($ch_rms_legacy as $ch_rm) {
             $rm_id = Rm::whereRm($ch_rm->rm)->pluck('id');
             $egreso->rms()->attach($rm_id, ['monto' => $ch_rm->monto]);
         }
     }
 }
 public function importarReembolsosFaltantes()
 {
     $legacy_reembolsos = \DB::connection($this->db_origen)->table('tbl_reembolsos')->where('cta_b', $this->cuenta_bancaria->cuenta_bancaria)->whereNotIn('ingreso_id', $this->reembolsos_importados)->get();
     foreach ($legacy_reembolsos as $legacy_reembolso) {
         $cuenta_bancaria_id_egreso = $this->getCuentaBancariaId($legacy_reembolso->cta_b_cheg);
         $legacy_ingreso = \DB::connection($this->db_origen)->table('tbl_ingresos')->where('cta_b', $legacy_reembolso->cta_b)->where('ingreso_id', $legacy_reembolso->ingreso_id)->first();
         if (!empty($legacy_ingreso->ingreso_id)) {
             //Crear No Identificado
             $no_identificado = NoIdentificado::create(['cuenta_bancaria_id' => $this->cuenta_bancaria->id, 'fecha' => $legacy_ingreso->fecha, 'monto' => $legacy_ingreso->monto, 'no_deposito' => '', 'identificado' => 0]);
             $poliza = Poliza::create(['fecha' => $legacy_ingreso->fecha, 'tipo' => 'Ingreso', 'concepto' => $legacy_ingreso->cmt, 'user_id' => \Auth::user()->id]);
             $poliza->polizaCargos()->create(['cuenta_id' => 10, 'monto' => $legacy_ingreso->monto, 'origen_id' => $no_identificado->id, 'origen_type' => 'Guia\\Models\\NoIdentificado']);
             $egreso_query = Egreso::where('cuenta_bancaria_id', $cuenta_bancaria_id_egreso);
             if ($legacy_reembolso->tipo == 'ch') {
                 $egreso_query->where('cheque', $legacy_reembolso->ch_eg);
             } else {
                 $egreso_query->where('poliza', $legacy_reembolso->ch_eg);
             }
             $egreso_id = $egreso_query->pluck('id');
             $origen_id = $egreso_id;
             $origen_type = 'Guia\\Models\\Egreso';
             $cuenta_id = $this->getCuentaId($legacy_ingreso->concepto);
             $poliza_abono = $poliza->polizaAbonos()->create(['cuenta_id' => $cuenta_id, 'monto' => $legacy_ingreso->monto, 'origen_id' => $origen_id, 'origen_type' => $origen_type]);
             //Registrar RMs del Ingreso
             $legacy_ingreso_rms = \DB::connection($this->db_origen)->table('tbl_ingresos_rm')->where('cta_b', $legacy_ingreso->cta_b)->where('ingreso_id', $legacy_ingreso->ingreso_id)->get();
             if (count($legacy_ingreso_rms) > 0) {
                 foreach ($legacy_ingreso_rms as $legacy_ingreso_rm) {
                     $rm_id = Rm::whereRm($legacy_ingreso_rm->rm)->pluck('id');
                     $poliza_abono->rms()->attach($rm_id, ['monto' => $legacy_ingreso_rm->monto]);
                 }
             }
         }
     }
 }
 public function importarSolicitudes()
 {
     $solicitudes_legacy = $this->consultarSolicitudesLegacy();
     foreach ($solicitudes_legacy as $sol_legacy) {
         $benef = \DB::connection('legacy_benef')->table('tbl_benef')->where('benef_id', '=', $sol_legacy->benef_id)->value('benef');
         $benef_id = Benef::whereBenef($benef)->pluck('id');
         $proyecto = Proyecto::whereProyecto($sol_legacy->proy)->first(['id', 'urg_id']);
         //Determinar el ID del usuario solicitante
         $solicita_id = User::whereLegacyUsername($sol_legacy->solicita)->pluck('id');
         //Determinar el usuario responsable
         if ($sol_legacy->responsable == 'Presupuesto') {
             $usuario = \InfoDirectivos::getResponsable('PRESU');
             $user_id = $usuario->id;
         } elseif ($sol_legacy->responsable == 'Contabilidad') {
             $usuario = \InfoDirectivos::getResponsable('CONTA');
             $user_id = $usuario->id;
         } elseif ($sol_legacy->responsable == 'Recepcion') {
             //Buscar primer usuario con rol recepcion
             $user_id = 2;
         } else {
             $user_id = User::whereLegacyUsername($sol_legacy->responsable)->pluck('id');
         }
         if (empty($sol_legacy->inventariable)) {
             $inventariable = 0;
         } else {
             $inventariable = 1;
         }
         $sol_nueva = new Solicitud();
         $sol_nueva->fecha = $sol_legacy->fecha;
         $sol_nueva->benef_id = $benef_id;
         $sol_nueva->tipo_solicitud = $sol_legacy->tipo_solicitud;
         $sol_nueva->urg_id = $proyecto->urg_id;
         $sol_nueva->proyecto_id = $proyecto->id;
         $sol_nueva->concepto = $sol_legacy->concepto;
         $sol_nueva->obs = $sol_legacy->obs . ' #SIGI: ' . $sol_legacy->solicitud_id;
         $sol_nueva->no_documento = $sol_legacy->no_documento;
         $sol_nueva->no_afin = $sol_legacy->id_afin;
         $sol_nueva->monto = $sol_legacy->monto;
         $sol_nueva->solicita = $solicita_id;
         $sol_nueva->estatus = $sol_legacy->estatus;
         $sol_nueva->user_id = $user_id;
         //responsable
         $sol_nueva->monto_pagado = $sol_legacy->monto_pagado;
         $sol_nueva->viaticos = $sol_legacy->viaticos;
         $sol_nueva->inventariable = $inventariable;
         $sol_nueva->save();
         if ($sol_nueva->estatus != 'Cancelada') {
             if ($sol_legacy->tipo_solicitud != 'Vale') {
                 $rms_solicitud = $this->consutlarRMs($sol_legacy->solicitud_id);
                 foreach ($rms_solicitud as $rm_legacy) {
                     $rm_id = Rm::whereRm($rm_legacy->rm)->value('id');
                     $sol_nueva->rms()->attach($rm_id, ['monto' => $rm_legacy->monto]);
                 }
             } else {
                 //Buscar si tiene capturado el objetivo
                 $objs_solicitud = $this->consultarObjetivos($sol_legacy->solicitud_id);
                 if (count($objs_solicitud) > 0) {
                     foreach ($objs_solicitud as $obj_legacy) {
                         $objetivo_id = Objetivo::whereObjetivo($obj_legacy->objetivo)->pluck('id');
                         if (empty($obj_legacy)) {
                             dd($obj_legacy);
                         }
                         $sol_nueva->objetivos()->attach($objetivo_id, ['monto' => $obj_legacy->monto]);
                     }
                 } else {
                     //Si no, asigna primer objetivo que encuentre en el proyecto
                     $objetivo_id = Rm::whereProyectoId($sol_nueva->proyecto_id)->pluck('objetivo_id');
                     if (empty($objetivo_id->objetivo_id)) {
                         $objetivo_id = 1;
                     }
                     $sol_nueva->objetivos()->attach($objetivo_id, ['monto' => $sol_nueva->monto]);
                 }
             }
         }
     }
 }
 private function getRm($rm)
 {
     $rm = Rm::whereRm($rm)->first();
     return $rm;
 }