Пример #1
0
        private in_Ing_Egr_Inven_Info armar_movi_inven(cp_orden_giro_Info info, cp_orden_giro_x_in_Ing_Egr_Inven rel)
        {
            Entities_inventario db_inv = new Entities_inventario();
            try
            {
                #region Variables
                var motivo = db_inv.in_Motivo_Inven.Where(q => q.IdEmpresa == info.IdEmpresa && q.Tipo_Ing_Egr == "ING" && q.Genera_Movi_Inven == "S").FirstOrDefault();
                if (motivo == null)
                    return null;

                var param = db_inv.in_parametro.Where(q => q.IdEmpresa == info.IdEmpresa).FirstOrDefault();
                if (param == null)
                    return null;
                #endregion

                in_Ing_Egr_Inven_Info movi = new in_Ing_Egr_Inven_Info
                {
                    IdEmpresa = info.IdEmpresa,
                    IdSucursal = (int)info.IdSucursal,
                    IdBodega = info.IdBodega,
                    IdMotivo_Inv = motivo.IdMotivo_Inv,
                    IdNumMovi = rel == null ? 0 : rel.inv_IdNumMovi,
                    cm_fecha = info.co_fechaOg.Date,
                    cm_observacion = info.co_observacion,
                    CodMoviInven = "FXP-"+Convert.ToDecimal(info.co_factura).ToString(),
                    signo = "+",
                    IdMovi_inven_tipo = rel == null ? (int)param.P_IdMovi_inven_tipo_ingreso_x_compra : (rel.inv_IdMovi_inven_tipo),
                    Estado = "A",
                    IdUsuario = info.IdUsuario,
                    IdUsuarioUltModi = info.IdUsuario
                };

                foreach (var item in info.lst_det)
                {
                    movi.lst_in_Ing_Egr_Inven_det.Add(new in_Ing_Egr_Inven_det_Info
                    {
                        IdEmpresa = movi.IdEmpresa,
                        IdSucursal = movi.IdSucursal,
                        IdBodega = (int)movi.IdBodega,
                        IdMovi_inven_tipo = movi.IdMovi_inven_tipo,
                        IdNumMovi = movi.IdNumMovi,
                        dm_cantidad_sinConversion = item.Cantidad,
                        dm_cantidad = item.Cantidad,
                        mv_costo_sinConversion = item.CostoUniFinal,
                        mv_costo = item.CostoUniFinal,
                        IdUnidadMedida_sinConversion = item.IdUnidadMedida,
                        IdUnidadMedida = item.IdUnidadMedida,
                        IdProducto = item.IdProducto,                        
                    });
                }
                db_inv.Dispose();
                return movi;
            }
            catch (Exception)
            {
                db_inv.Dispose();
                throw;
            }
        }
Пример #2
0
        public bool EliminarContabilizacion(int IdEmpresa, int IdSucursal, int IdMovi_inven_tipo, decimal IdNumMovi)
        {
            Entities_inventario   db_i  = new Entities_inventario();
            Entities_contabilidad db_ct = new Entities_contabilidad();

            try
            {
                var lst_det = db_i.in_Ing_Egr_Inven_det.Where(q => q.IdEmpresa == IdEmpresa && q.IdSucursal == IdSucursal && q.IdMovi_inven_tipo == IdMovi_inven_tipo && q.IdNumMovi == IdNumMovi).ToList();
                if (lst_det.Where(q => q.IdNumMovi_inv == null).Count() == 0)
                {
                    var PK_movi = new
                    {
                        lst_det.First().IdEmpresa_inv,
                        lst_det.First().IdSucursal_inv,
                        lst_det.First().IdBodega_inv,
                        lst_det.First().IdMovi_inven_tipo_inv,
                        lst_det.First().IdNumMovi_inv
                    };

                    #region Obtengo relacion contable y la elimino
                    var PK_conta = db_i.in_movi_inve_x_ct_cbteCble.Where(q => q.IdEmpresa == PK_movi.IdEmpresa_inv &&
                                                                         q.IdSucursal == PK_movi.IdSucursal_inv &&
                                                                         q.IdBodega == PK_movi.IdBodega_inv &&
                                                                         q.IdMovi_inven_tipo == PK_movi.IdMovi_inven_tipo_inv &&
                                                                         q.IdNumMovi == PK_movi.IdNumMovi_inv
                                                                         ).FirstOrDefault();

                    #endregion
                    if (PK_conta != null)
                    {
                        #region Elimino diario contable
                        var lst_rel_det = db_i.in_movi_inve_detalle_x_ct_cbtecble_det.Where(q => q.IdEmpresa_inv == PK_movi.IdEmpresa_inv &&
                                                                                            q.IdSucursal_inv == PK_movi.IdSucursal_inv &&
                                                                                            q.IdBodega_inv == PK_movi.IdBodega_inv &&
                                                                                            q.IdMovi_inven_tipo_inv == PK_movi.IdMovi_inven_tipo_inv &&
                                                                                            q.IdNumMovi_inv == PK_movi.IdNumMovi_inv).ToList();
                        db_i.in_movi_inve_detalle_x_ct_cbtecble_det.RemoveRange(lst_rel_det);

                        var lst_conta = db_ct.ct_cbtecble_det.Where(q => q.IdEmpresa == PK_conta.IdEmpresa_ct &&
                                                                    q.IdTipoCbte == PK_conta.IdTipoCbte &&
                                                                    q.IdCbteCble == PK_conta.IdCbteCble
                                                                    ).ToList();
                        db_ct.ct_cbtecble_det.RemoveRange(lst_conta);

                        var Conta = db_ct.ct_cbtecble.Where(q => q.IdEmpresa == PK_conta.IdEmpresa &&
                                                            q.IdTipoCbte == PK_conta.IdTipoCbte &&
                                                            q.IdCbteCble == PK_conta.IdCbteCble
                                                            ).FirstOrDefault();
                        db_ct.ct_cbtecble.Remove(Conta);
                        #endregion
                        db_i.in_movi_inve_x_ct_cbteCble.Remove(PK_conta);
                    }
                }

                db_i.SaveChanges();
                db_ct.SaveChanges();

                db_ct.Dispose();
                db_i.Dispose();

                return(true);
            }
            catch (Exception)
            {
                db_ct.Dispose();
                db_i.Dispose();
                throw;
            }
        }
Пример #3
0
        public bool Contabilizar(int IdEmpresa, int IdSucursal, int IdMovi_inven_tipo, decimal IdNumMovi, string Observacion, DateTime Fecha)
        {
            Entities_inventario   db_i = new Entities_inventario();
            Entities_contabilidad db_c = new Entities_contabilidad();

            try
            {
                var tipo_movi = db_i.in_movi_inven_tipo.Where(q => q.IdEmpresa == IdEmpresa && q.IdMovi_inven_tipo == IdMovi_inven_tipo).FirstOrDefault();
                if (tipo_movi == null)
                {
                    return(false);
                }

                if (!(tipo_movi.Genera_Diario_Contable ?? false))
                {
                    return(false);
                }

                if (tipo_movi.IdTipoCbte == null)
                {
                    return(false);
                }

                var lst = db_i.vwin_Ing_Egr_Inven_PorContabilizar.Where(q => q.IdEmpresa == IdEmpresa && q.IdSucursal == IdSucursal && q.IdMovi_inven_tipo == IdMovi_inven_tipo && q.IdNumMovi == IdNumMovi).ToList();
                if (lst.Count == 0)
                {
                    return(false);
                }

                var lst_g = (from q in lst
                             group q by new
                {
                    q.IdCtaCble_Motivo,
                    q.IdCtaCtble_Costo,
                    q.IdCtaCtble_Inve,
                    q.P_IdCtaCble_transitoria_transf_inven,
                    q.EsTransferencia,

                    q.IdEmpresa_inv,
                    q.IdSucursal_inv,
                    q.IdMovi_inven_tipo_inv,
                    q.IdBodega_inv,
                    q.IdNumMovi_inv
                }
                             into g
                             select new
                {
                    g.Key.IdCtaCble_Motivo,
                    g.Key.IdCtaCtble_Costo,
                    g.Key.IdCtaCtble_Inve,
                    g.Key.P_IdCtaCble_transitoria_transf_inven,
                    g.Key.EsTransferencia,

                    g.Key.IdEmpresa_inv,
                    g.Key.IdSucursal_inv,
                    g.Key.IdMovi_inven_tipo_inv,
                    g.Key.IdBodega_inv,
                    g.Key.IdNumMovi_inv,

                    Valor = g.Sum(q => q.Valor)
                }).ToList();

                List <in_movi_inve_detalle_x_ct_cbtecble_det> lst_rel = new List <in_movi_inve_detalle_x_ct_cbtecble_det>();
                List <ct_cbtecble_det_Info> lst_ct = new List <ct_cbtecble_det_Info>();
                int Secuencia = 1;
                foreach (var item in lst_g)
                {
                    /*
                     * lst_rel.Add(new in_movi_inve_detalle_x_ct_cbtecble_det
                     * {
                     *  IdEmpresa_inv = (int)item.IdEmpresa_inv,
                     *  IdSucursal_inv = (int)item.IdSucursal_inv,
                     *  IdBodega_inv = (int)item.IdBodega_inv,
                     *  IdMovi_inven_tipo_inv = (int)item.IdMovi_inven_tipo_inv,
                     *  IdNumMovi_inv = (decimal)item.IdNumMovi_inv,
                     *  Secuencia_inv = (int)item.secuencia_inv,
                     *
                     *  secuencia_ct = Secuencia,
                     *  Secuencial_reg = Secuencia,
                     *  observacion = ""
                     * });*/
                    //Debe
                    lst_ct.Add(new ct_cbtecble_det_Info
                    {
                        secuencia = Secuencia++,
                        IdCtaCble = tipo_movi.cm_tipo_movi == "+" ? item.IdCtaCtble_Inve : ((bool)item.EsTransferencia ? item.P_IdCtaCble_transitoria_transf_inven : (string.IsNullOrEmpty(item.IdCtaCble_Motivo) ? item.IdCtaCtble_Costo : item.IdCtaCble_Motivo)),
                        dc_Valor  = Math.Abs(Math.Round(item.Valor, 2, MidpointRounding.AwayFromZero))
                    });

                    /*
                     * lst_rel.Add(new in_movi_inve_detalle_x_ct_cbtecble_det
                     * {
                     *  IdEmpresa_inv = (int)item.IdEmpresa_inv,
                     *  IdSucursal_inv = (int)item.IdSucursal_inv,
                     *  IdBodega_inv = (int)item.IdBodega_inv,
                     *  IdMovi_inven_tipo_inv = (int)item.IdMovi_inven_tipo_inv,
                     *  IdNumMovi_inv = (decimal)item.IdNumMovi_inv,
                     *  Secuencia_inv = (int)item.secuencia_inv,
                     *
                     *  secuencia_ct = Secuencia,
                     *  Secuencial_reg = Secuencia,
                     *  observacion = ""
                     * });*/
                    //Haber
                    lst_ct.Add(new ct_cbtecble_det_Info
                    {
                        secuencia = Secuencia++,
                        IdCtaCble = tipo_movi.cm_tipo_movi == "-" ? item.IdCtaCtble_Inve : ((bool)item.EsTransferencia ? item.P_IdCtaCble_transitoria_transf_inven : (string.IsNullOrEmpty(item.IdCtaCble_Motivo) ? item.IdCtaCtble_Costo : item.IdCtaCble_Motivo)),
                        dc_Valor  = Math.Abs(Math.Round(item.Valor, 2, MidpointRounding.AwayFromZero)) * -1
                    });
                }

                var diario = odata_ct.armar_info(lst_ct, IdEmpresa, IdSucursal, (int)tipo_movi.IdTipoCbte, 0, tipo_movi.tm_descripcion + " #" + IdNumMovi + " " + Observacion, Fecha);

                diario.lst_ct_cbtecble_det.RemoveAll(q => q.dc_Valor == 0);

                if (diario.lst_ct_cbtecble_det.Count == 0)
                {
                    return(false);
                }

                if (diario.lst_ct_cbtecble_det.Where(q => q.dc_Valor == 0).Count() > 0)
                {
                    return(false);
                }

                double descuadre = Math.Round(diario.lst_ct_cbtecble_det.Sum(q => q.dc_Valor), 2, MidpointRounding.AwayFromZero);
                if (descuadre < -0.01 || 0.01 <= descuadre)
                {
                    return(false);
                }

                if (descuadre <= 0.01 || -0.01 <= descuadre && descuadre != 0)
                {
                    if (descuadre > 0)
                    {
                        diario.lst_ct_cbtecble_det.Where(q => q.dc_Valor < 0).FirstOrDefault().dc_Valor -= descuadre;
                    }
                    else
                    {
                        diario.lst_ct_cbtecble_det.Where(q => q.dc_Valor > 0).FirstOrDefault().dc_Valor += (descuadre * -1);
                    }
                }

                descuadre = Math.Round(diario.lst_ct_cbtecble_det.Sum(q => q.dc_Valor), 2, MidpointRounding.AwayFromZero);
                if (descuadre != 0)
                {
                    return(false);
                }

                if (odata_ct.guardarDB(diario))
                {
                    /*
                     * lst_rel.ForEach(q =>
                     * {
                     *  q.IdEmpresa_ct = diario.IdEmpresa;
                     *  q.IdTipoCbte_ct = diario.IdTipoCbte;
                     *  q.IdCbteCble_ct = diario.IdCbteCble;
                     * });
                     */
                    var First = lst_g.First();
                    db_i.in_movi_inve_x_ct_cbteCble.Add(new in_movi_inve_x_ct_cbteCble
                    {
                        IdEmpresa         = (int)First.IdEmpresa_inv,
                        IdSucursal        = (int)First.IdSucursal_inv,
                        IdBodega          = (int)First.IdBodega_inv,
                        IdMovi_inven_tipo = (int)First.IdMovi_inven_tipo_inv,
                        IdNumMovi         = (decimal)First.IdNumMovi_inv,

                        IdEmpresa_ct = diario.IdEmpresa,
                        IdTipoCbte   = diario.IdTipoCbte,
                        IdCbteCble   = diario.IdCbteCble,

                        Observacion = ""
                    });
                    //db_i.in_movi_inve_detalle_x_ct_cbtecble_det.AddRange(lst_rel);
                    db_i.SaveChanges();
                }

                db_c.Dispose();
                db_i.Dispose();
                return(true);
            }
            catch (Exception)
            {
                db_c.Dispose();
                db_i.Dispose();
                throw;
            }
        }
Пример #4
0
        public bool guardarDB(in_Ajuste_Info info)
        {
            try
            {
                using (Entities_inventario Context = new Entities_inventario())
                {
                    int secuencia = 1;
                    in_parametro_Info info_parametro = data_parametro.get_info(info.IdEmpresa);

                    in_Ajuste Entity = new in_Ajuste
                    {
                        IdEmpresa             = info.IdEmpresa,
                        IdSucursal            = info.IdSucursal,
                        IdAjuste              = info.IdAjuste = get_id(info.IdEmpresa),
                        IdBodega              = info.IdBodega,
                        IdMovi_inven_tipo_ing = info.IdMovi_inven_tipo_ing = info_parametro.IdMovi_inven_tipo_ajuste_ing,
                        IdMovi_inven_tipo_egr = info.IdMovi_inven_tipo_egr = info_parametro.IdMovi_inven_tipo_ajuste_egr,
                        IdNumMovi_ing         = info.IdNumMovi_ing,
                        IdNumMovi_egr         = info.IdNumMovi_egr,
                        IdCatalogo_Estado     = info.IdCatalogo_Estado,
                        Fecha             = info.Fecha,
                        Observacion       = info.Observacion,
                        Estado            = info.Estado,
                        IdUsuarioCreacion = info.IdUsuarioCreacion,
                        FechaCreacion     = DateTime.Now
                    };

                    Context.in_Ajuste.Add(Entity);

                    foreach (var item in info.lst_detalle)
                    {
                        in_AjusteDet entity_det = new in_AjusteDet
                        {
                            IdEmpresa      = info.IdEmpresa,
                            IdAjuste       = info.IdAjuste,
                            Secuencia      = item.Secuencia = secuencia,
                            IdProducto     = item.IdProducto,
                            IdUnidadMedida = item.IdUnidadMedida,
                            StockFisico    = item.StockFisico,
                            StockSistema   = item.StockSistema,
                            Ajuste         = item.Ajuste,
                            Costo          = item.Costo
                        };
                        secuencia++;
                        Context.in_AjusteDet.Add(entity_det);
                    }

                    Context.SaveChanges();

                    #region Movimiento Inventario
                    Entities_inventario   dbi            = new Entities_inventario();
                    in_Ing_Egr_Inven_Data odata_i        = new in_Ing_Egr_Inven_Data();
                    int IdMovi_inven_tipo                = 0;
                    List <in_AjusteDet_Info> lst_ingreso = info.lst_detalle.Where(q => q.Ajuste > 0).ToList();
                    List <in_AjusteDet_Info> lst_egreso  = info.lst_detalle.Where(q => q.Ajuste < 0).ToList();

                    if (info_parametro == null)
                    {
                        return(false);
                    }

                    if (info.IdCatalogo_Estado == "APRO")
                    {
                        if (lst_ingreso.Count() > 0)
                        {
                            in_Ing_Egr_Inven_Info info_movi = new in_Ing_Egr_Inven_Info();
                            IdMovi_inven_tipo   = info_parametro.IdMovi_inven_tipo_ajuste_ing ?? 0;
                            info.lst_movimiento = lst_ingreso;

                            info_movi = GenerarMoviInven(info, "+", info_parametro.IdMotivo_Inv_ajuste_ing);
                            if (info_movi == null)
                            {
                                return(true);
                            }

                            if (info.IdNumMovi_ing == null && odata_i.guardarDB(info_movi, "+"))
                            {
                                info.IdNumMovi_ing   = info_movi.IdNumMovi;
                                Entity.IdNumMovi_ing = info.IdNumMovi_ing;

                                Context.SaveChanges();
                            }
                        }

                        if (lst_egreso.Count() > 0)
                        {
                            in_Ing_Egr_Inven_Info info_movi = new in_Ing_Egr_Inven_Info();
                            IdMovi_inven_tipo   = info_parametro.IdMovi_inven_tipo_ajuste_ing ?? 0;
                            info.lst_movimiento = lst_egreso;

                            info_movi = GenerarMoviInven(info, "-", info_parametro.IdMotivo_Inv_ajuste_egr);
                            if (info_movi == null)
                            {
                                return(true);
                            }

                            if (info.IdNumMovi_egr == null && odata_i.guardarDB(info_movi, "-"))
                            {
                                info.IdNumMovi_egr   = info_movi.IdNumMovi;
                                Entity.IdNumMovi_egr = info.IdNumMovi_egr;

                                Context.SaveChanges();
                            }
                        }
                    }
                    #endregion

                    Context.Dispose();
                }
                return(true);
            }
            catch (Exception)
            {
                throw;
            }
        }