private async void btnCompraGuardar_Clicked(object sender, EventArgs e)
        {
            if (CrossConnectivity.Current.IsConnected)
            {
                if (!string.IsNullOrWhiteSpace(numero_facturaCompraEntry.Text) || (!string.IsNullOrEmpty(numero_facturaCompraEntry.Text)))
                {
                    if (!string.IsNullOrWhiteSpace(saldo_CompraEntry.Text) || (!string.IsNullOrEmpty(saldo_CompraEntry.Text)))
                    {
                        if (!string.IsNullOrWhiteSpace(totalCompraEntry.Text) || (!string.IsNullOrEmpty(totalCompraEntry.Text)))
                        {
                            try
                            {
                                if (App._detalleCData.Count() > 0)
                                {
                                    foreach (var item in App._detalleCData)
                                    {
                                        DetalleCompra detalleCompra = new DetalleCompra()
                                        {
                                            cantidad_compra    = item.cantidad,
                                            id_producto        = item.id_producto,
                                            precio_producto    = item.precio_producto,
                                            descuento_producto = item.descuento,
                                            sub_total          = item.sub_total,
                                            numero_factura     = Convert.ToInt32(numero_facturaCompraEntry.Text)
                                        };

                                        var        json1    = JsonConvert.SerializeObject(detalleCompra);
                                        var        content1 = new StringContent(json1, Encoding.UTF8, "application/json");
                                        HttpClient client1  = new HttpClient();
                                        var        result1  = await client1.PostAsync("https://dmrbolivia.com/api_distribuidora/compras/agregarDetalleCompra.php", content1);

                                        Models.Inventario _inventario = new Models.Inventario()
                                        {
                                            id_producto      = item.id_producto,
                                            fecha_inv        = fechaCompraEntry.Date,
                                            numero_factura   = Convert.ToInt32(numero_facturaCompraEntry.Text),
                                            detalle          = "Compra",
                                            precio_compra    = item.precio_producto - item.descuento,
                                            unidades         = item.cantidad,
                                            entrada_fisica   = item.cantidad,
                                            salida_fisica    = 0,
                                            saldo_fisica     = item.stock + item.cantidad,
                                            entrada_valorado = (item.precio_producto - item.descuento) * item.cantidad,
                                            salida_valorado  = 0,
                                            saldo_valorado   = item.stock_valorado + ((item.precio_producto - item.descuento) * item.cantidad),
                                            promedio         = ((item.stock_valorado + (item.precio_producto - item.descuento)) * item.cantidad) / item.stock + item.cantidad
                                        };

                                        var        json2    = JsonConvert.SerializeObject(_inventario);
                                        var        content2 = new StringContent(json2, Encoding.UTF8, "application/json");
                                        HttpClient client2  = new HttpClient();
                                        var        result2  = await client2.PostAsync("https://dmrbolivia.com/api_distribuidora/inventarios/agregarInventario.php", content2);

                                        Models.Producto producto = new Models.Producto()
                                        {
                                            id_producto    = item.id_producto,
                                            stock          = item.stock + item.cantidad,
                                            stock_valorado = item.stock_valorado + (item.cantidad * item.promedio),
                                            promedio       = (item.stock_valorado + ((item.precio_producto - item.descuento) * item.cantidad)) / (item.stock + item.cantidad)
                                        };
                                        var        json3    = JsonConvert.SerializeObject(producto);
                                        var        content3 = new StringContent(json3, Encoding.UTF8, "application/json");
                                        HttpClient client3  = new HttpClient();
                                        var        result3  = await client3.PostAsync("https://dmrbolivia.com/api_distribuidora/productos/editarProducto.php", content3);
                                    }
                                    Compras _compras = new Compras()
                                    {
                                        fecha_compra   = fechaCompraEntry.Date,
                                        numero_factura = Convert.ToInt32(numero_facturaCompraEntry.Text),
                                        id_proveedor   = idProveedorSelected,
                                        saldo          = Convert.ToDecimal(saldo_CompraEntry.Text),
                                        total          = Convert.ToDecimal(totalCompraEntry.Text)
                                    };

                                    var        json    = JsonConvert.SerializeObject(_compras);
                                    var        content = new StringContent(json, Encoding.UTF8, "application/json");
                                    HttpClient client  = new HttpClient();
                                    var        result  = await client.PostAsync("https://dmrbolivia.com/api_distribuidora/compras/agregarCompra.php", content);

                                    if (result.StatusCode == HttpStatusCode.OK)
                                    {
                                        await DisplayAlert("OK", "Se agrego correctamente", "OK");

                                        App._detalleCData.Clear();
                                        await Navigation.PopAsync();
                                    }
                                    else
                                    {
                                        await DisplayAlert("Error", "Algo salio mal, intentelo de nuevo por favor", "OK");

                                        await Navigation.PopAsync();
                                    }
                                }
                                else
                                {
                                    await DisplayAlert("Error", "Agregue productos a la lista", "OK");
                                }
                            }
                            catch (Exception error)
                            {
                                await DisplayAlert("Error", "Algo salio mal, intentelo de nuevo por favor", "OK");
                            }
                        }
                        else
                        {
                            await DisplayAlert("Campo vacio", "El campo de Total esta vacio", "Ok");
                        }
                    }
                    else
                    {
                        await DisplayAlert("Campo vacio", "El campo de Saldo esta vacio", "Ok");
                    }
                }
                else
                {
                    await DisplayAlert("Campo vacio", "El campo de Factura esta vacio", "Ok");
                }
            }
            else
            {
                await DisplayAlert("Error", "Necesitas estar conectado a internet", "OK");
            }
        }
        private async void BtnVentaGuardar_Clicked(object sender, EventArgs e)
        {
            if (App._detalleVData.Count() > 0)
            {
                string BusyReason = "Cargando...";
                try
                {
                    await PopupNavigation.Instance.PushAsync(new BusyPopup(BusyReason));

                    foreach (var item in App._detalleVData)
                    {
                        DetalleVenta detalleVenta = new DetalleVenta()
                        {
                            cantidad        = item.cantidad,
                            id_producto     = item.id_producto,
                            precio_producto = item.precio_producto,
                            descuento       = item.descuento,
                            sub_total       = item.sub_total,
                            factura         = Convert.ToInt32(numero_facturaVentaEntry.Text)
                        };

                        var        json1    = JsonConvert.SerializeObject(detalleVenta);
                        var        content1 = new StringContent(json1, Encoding.UTF8, "application/json");
                        HttpClient client1  = new HttpClient();
                        var        result1  = await client1.PostAsync("https://dmrbolivia.com/api_distribuidora/ventas/agregarDetalleVenta.php", content1);

                        Models.Inventario inventario = new Models.Inventario()
                        {
                            id_producto      = item.id_producto,
                            fecha_inv        = fechaVentaEntry.Date,
                            numero_factura   = Convert.ToInt32(numero_facturaVentaEntry.Text),
                            detalle          = "Venta",
                            precio_compra    = 0,
                            unidades         = item.cantidad,
                            entrada_fisica   = 0,
                            salida_fisica    = item.cantidad,
                            saldo_fisica     = item.stock - item.cantidad,
                            entrada_valorado = 0,
                            salida_valorado  = item.cantidad * item.promedio,
                            saldo_valorado   = item.stock_valorado - (item.cantidad * item.promedio),
                            promedio         = item.stock_valorado / item.stock
                        };

                        var        json2    = JsonConvert.SerializeObject(inventario);
                        var        content2 = new StringContent(json2, Encoding.UTF8, "application/json");
                        HttpClient client2  = new HttpClient();
                        var        result2  = await client2.PostAsync("https://dmrbolivia.com/api_distribuidora/inventarios/agregarInventario.php", content2);

                        Models.Producto producto = new Models.Producto()
                        {
                            id_producto    = item.id_producto,
                            stock          = item.stock - item.cantidad,
                            stock_valorado = item.stock_valorado - (item.cantidad * item.promedio),
                            promedio       = item.stock_valorado / item.stock
                        };
                        var        json3    = JsonConvert.SerializeObject(producto);
                        var        content3 = new StringContent(json3, Encoding.UTF8, "application/json");
                        HttpClient client3  = new HttpClient();
                        var        result3  = await client3.PostAsync("https://dmrbolivia.com/api_distribuidora/productos/editarProducto.php", content3);
                    }
                    Ventas ventas = new Ventas()
                    {
                        fecha          = fechaVentaEntry.Date,
                        numero_factura = Convert.ToInt32(numero_facturaVentaEntry.Text),
                        id_cliente     = idClienteSelected,
                        id_vendedor    = idVendedorSelected,
                        tipo_venta     = tipoVentaPick,
                        saldo          = Convert.ToDecimal(saldo_VentaEntry.Text),
                        total          = Convert.ToDecimal(totalVentaEntry.Text)
                    };

                    var        json    = JsonConvert.SerializeObject(ventas);
                    var        content = new StringContent(json, Encoding.UTF8, "application/json");
                    HttpClient client  = new HttpClient();
                    var        result  = await client.PostAsync("https://dmrbolivia.com/api_distribuidora/ventas/agregarVenta.php", content);

                    if (result.StatusCode == HttpStatusCode.OK)
                    {
                        await PopupNavigation.Instance.PopAsync();
                        await DisplayAlert("OK", "Se agrego correctamente", "OK");

                        App._detalleVData.Clear();
                        await Navigation.PopAsync();
                    }
                    else
                    {
                        await PopupNavigation.Instance.PopAsync();
                        await DisplayAlert("Error", result.StatusCode.ToString(), "OK");

                        await Navigation.PopAsync();
                    }
                }
                catch (Exception error)
                {
                    await PopupNavigation.Instance.PopAsync();
                    await DisplayAlert("ERROR", error.ToString(), "OK");
                }
            }
            else
            {
                await DisplayAlert("ERROR", "Agregue un producto a la lista", "OK");
            }
        }
Exemple #3
0
        private async void BtnVentaGuardar_Clicked(object sender, EventArgs e)
        {
            if (_FacturaExistente == "No")
            {
                if (!string.IsNullOrWhiteSpace(numero_facturaVentaEntry.Text) || (!string.IsNullOrEmpty(numero_facturaVentaEntry.Text)))
                {
                    if (!string.IsNullOrWhiteSpace(saldo_VentaEntry.Text) || (!string.IsNullOrEmpty(saldo_VentaEntry.Text)))
                    {
                        if (!string.IsNullOrWhiteSpace(totalVentaEntry.Text) || (!string.IsNullOrEmpty(totalVentaEntry.Text)))
                        {
                            if (!string.IsNullOrWhiteSpace(entryObs.Text) || (!string.IsNullOrEmpty(entryObs.Text)))
                            {
                                if (App._detalleVData.Count() > 0)
                                {
                                    string BusyReason = "Cargando...";
                                    try
                                    {
                                        await PopupNavigation.Instance.PushAsync(new BusyPopup(BusyReason));

                                        if (estadoPick == "Entregado")
                                        {
                                            foreach (var item in App._detalleVData)
                                            {
                                                DetalleVenta detalleVenta = new DetalleVenta()
                                                {
                                                    cantidad        = item.cantidad,
                                                    id_producto     = item.id_producto,
                                                    precio_producto = item.precio_producto,
                                                    descuento       = item.descuento,
                                                    sub_total       = item.sub_total,
                                                    envases         = item.envases,
                                                    factura         = Convert.ToInt32(numero_facturaVentaEntry.Text)
                                                };

                                                var        json1    = JsonConvert.SerializeObject(detalleVenta);
                                                var        content1 = new StringContent(json1, Encoding.UTF8, "application/json");
                                                HttpClient client1  = new HttpClient();
                                                var        result1  = await client1.PostAsync("https://dmrbolivia.com/api_distribuidora/ventas/agregarDetalleVenta.php", content1);

                                                Models.Inventario inventario = new Models.Inventario()
                                                {
                                                    id_producto      = item.id_producto,
                                                    fecha_inv        = fechaVentaEntry.Date,
                                                    numero_factura   = Convert.ToInt32(numero_facturaVentaEntry.Text),
                                                    detalle          = "Venta",
                                                    precio_compra    = 0,
                                                    unidades         = item.cantidad,
                                                    entrada_fisica   = 0,
                                                    salida_fisica    = item.cantidad,
                                                    saldo_fisica     = item.stock - item.cantidad,
                                                    entrada_valorado = 0,
                                                    salida_valorado  = item.cantidad * item.promedio,
                                                    saldo_valorado   = item.stock_valorado - (item.cantidad * item.promedio),
                                                    promedio         = item.stock_valorado / item.stock
                                                };

                                                var        json2    = JsonConvert.SerializeObject(inventario);
                                                var        content2 = new StringContent(json2, Encoding.UTF8, "application/json");
                                                HttpClient client2  = new HttpClient();
                                                var        result2  = await client2.PostAsync("https://dmrbolivia.com/api_distribuidora/inventarios/agregarInventario.php", content2);

                                                Models.Producto producto = new Models.Producto()
                                                {
                                                    id_producto    = item.id_producto,
                                                    stock          = item.stock - item.cantidad,
                                                    stock_valorado = item.stock_valorado - (item.cantidad * item.promedio),
                                                    promedio       = item.stock_valorado / item.stock
                                                };
                                                var        json3    = JsonConvert.SerializeObject(producto);
                                                var        content3 = new StringContent(json3, Encoding.UTF8, "application/json");
                                                HttpClient client3  = new HttpClient();
                                                var        result3  = await client3.PostAsync("https://dmrbolivia.com/api_distribuidora/productos/editarProducto.php", content3);
                                            }
                                            Ventas ventas = new Ventas()
                                            {
                                                fecha          = fechaVentaEntry.Date,
                                                numero_factura = Convert.ToInt32(numero_facturaVentaEntry.Text),
                                                id_cliente     = idClienteSelected,
                                                id_vendedor    = idVendedorSelected,
                                                tipo_venta     = tipoVentaPick,
                                                saldo          = Convert.ToDecimal(saldo_VentaEntry.Text),
                                                total          = Convert.ToDecimal(totalVentaEntry.Text),
                                                fecha_entrega  = _fechaHoy,
                                                estado         = estadoPick,
                                                observacion    = entryObs.Text
                                            };

                                            var        json    = JsonConvert.SerializeObject(ventas);
                                            var        content = new StringContent(json, Encoding.UTF8, "application/json");
                                            HttpClient client  = new HttpClient();
                                            var        result  = await client.PostAsync("https://dmrbolivia.com/api_distribuidora/ventas/agregarVenta.php", content);

                                            if (result.StatusCode == HttpStatusCode.OK)
                                            {
                                                await PopupNavigation.Instance.PopAsync();
                                                await DisplayAlert("OK", "Se agrego correctamente", "OK");

                                                App._detalleVData.Clear();
                                                await Shell.Current.Navigation.PopAsync();
                                            }
                                            else
                                            {
                                                await PopupNavigation.Instance.PopAsync();
                                                await DisplayAlert("Error", "Algo salio mal, intentelo de nuevo", "OK");

                                                await Shell.Current.Navigation.PopAsync();
                                            }
                                        }
                                        else if (estadoPick == "Pendiente")
                                        {
                                            foreach (var item in App._detalleVData)
                                            {
                                                DetalleVenta detalleVenta = new DetalleVenta()
                                                {
                                                    cantidad        = item.cantidad,
                                                    id_producto     = item.id_producto,
                                                    precio_producto = item.precio_producto,
                                                    descuento       = item.descuento,
                                                    sub_total       = item.sub_total,
                                                    envases         = item.envases,
                                                    factura         = Convert.ToInt32(numero_facturaVentaEntry.Text)
                                                };

                                                var        json1    = JsonConvert.SerializeObject(detalleVenta);
                                                var        content1 = new StringContent(json1, Encoding.UTF8, "application/json");
                                                HttpClient client1  = new HttpClient();
                                                var        result1  = await client1.PostAsync("https://dmrbolivia.com/api_distribuidora/ventas/agregarDetalleVenta.php", content1);
                                            }
                                            Ventas ventas5 = new Ventas()
                                            {
                                                fecha          = fechaVentaEntry.Date,
                                                numero_factura = Convert.ToInt32(numero_facturaVentaEntry.Text),
                                                id_cliente     = idClienteSelected,
                                                id_vendedor    = idVendedorSelected,
                                                tipo_venta     = tipoVentaPick,
                                                saldo          = Convert.ToDecimal(saldo_VentaEntry.Text),
                                                total          = Convert.ToDecimal(totalVentaEntry.Text),
                                                fecha_entrega  = _fechaHoy,
                                                estado         = "Pendiente",
                                                observacion    = entryObs.Text
                                            };

                                            var        json5    = JsonConvert.SerializeObject(ventas5);
                                            var        content5 = new StringContent(json5, Encoding.UTF8, "application/json");
                                            HttpClient client5  = new HttpClient();
                                            var        result5  = await client5.PostAsync("https://dmrbolivia.com/api_distribuidora/ventas/agregarVenta.php", content5);

                                            if (result5.StatusCode == HttpStatusCode.OK)
                                            {
                                                await PopupNavigation.Instance.PopAsync();
                                                await DisplayAlert("OK", "Se agrego correctamente", "OK");

                                                App._detalleVData.Clear();
                                                await Shell.Current.Navigation.PopAsync();
                                            }
                                            else
                                            {
                                                await PopupNavigation.Instance.PopAsync();
                                                await DisplayAlert("Error", "Algo salio mal, intentelo de nuevo", "OK");

                                                await Shell.Current.Navigation.PopAsync();
                                            }
                                        }
                                    }
                                    catch (Exception error)
                                    {
                                        await PopupNavigation.Instance.PopAsync();
                                        await DisplayAlert("Error", "Algo salio mal, intentelo de nuevo", "OK");
                                    }
                                }
                                else
                                {
                                    await DisplayAlert("Error", "Agregue un producto a la lista", "OK");
                                }
                            }
                            else
                            {
                                await DisplayAlert("Campo vacio", "El campo de Observacion esta vacio", "Ok");
                            }
                        }
                        else
                        {
                            await DisplayAlert("Campo vacio", "El campo de Total esta vacio", "Ok");
                        }
                    }
                    else
                    {
                        await DisplayAlert("Campo vacio", "El campo de Saldo esta vacio", "Ok");
                    }
                }
                else
                {
                    await DisplayAlert("Campo vacio", "El campo de Numero de factura esta vacio", "Ok");
                }
            }
            else if (_FacturaExistente == "Si")
            {
                await DisplayAlert("Factura", "El numero de Factura que desea ingresar ya existe", "Ok");
            }
        }