예제 #1
0
        private void btnGuardar_Click(object sender, EventArgs e)
        {
            TicketBL oTicketBL = new TicketBL();

            if (_TempTicketDetalleList == null)
            {
                _TempTicketDetalleList = new List <TicketDetalleList>();
            }
            OperationResult objOperationResult = new OperationResult();

            string[] componentIdFromProtocol = _TempTicketDetalleList.FindAll(p => p.i_RecordStatus != (int)RecordStatus.EliminadoLogico)
                                               .Select(p => p.v_IdProductoDetalle).ToArray();

            bool IsExists = _ticketlBL.IsExistsproductoInTicket(ref objOperationResult, componentIdFromProtocol, _ProductoId);

            if (IsExists)
            {
                var msj = string.Format("El examen producto puede agregar, ya existe", labelmensaje.Text);
                MessageBox.Show(msj, "Error de validación", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (_mode == "New")
            {
                // || cbExamen.Text == ""
                if (txtMedicamento.Tag == null)
                {
                    string mensage = "";
                    if (txtMedicamento.Tag == null)
                    {
                        mensage = @"Por favor seleccione un medicamento";
                    }
                    //else if (cbExamen.Text == ""){mensage = @"Por favor seleccione un examen";}
                    MessageBox.Show(mensage, @"Error de validación", MessageBoxButtons.OK);
                    txtMedicamento.Focus();
                    return;
                }

                var findResult = _TempTicketDetalleList.Find(p => p.v_IdProductoDetalle == _ProductoId);
                _objTicketDetalleList = new TicketDetalleList();

                if (findResult == null)
                {
                    _objTicketDetalleList.v_TicketDetalleId = Guid.NewGuid().ToString();

                    _objTicketDetalleList.v_IdProductoDetalle  = txtMedicamento.Tag.ToString();
                    _objTicketDetalleList.v_NombreProducto     = txtMedicamento.Text;
                    _objTicketDetalleList.v_CodInterno         = txtCodigo.Text;
                    _objTicketDetalleList.v_IdUnidadProductiva = txtUnidadProductiva.Text;
                    var precioTarifa = oTicketBL.ObtenerPrecioTarifario(_serviceId, _objTicketDetalleList.v_IdProductoDetalle);


                    decimal d;
                    _objTicketDetalleList.d_Cantidad = decimal.TryParse(txtCantidad.Text, out d) ? d : 0;

                    //nuevo
                    //int planId = 0;
                    //var servicioDet = oTicketBL.GetService(ref objOperationResult,_serviceId);
                    //if (servicioDet.i_PlanId != null)
                    //{
                    //    planId = (int)servicioDet.i_PlanId;
                    //}
                    //////


                    var tienePlan = false;
                    //var resultplan = oTicketBL.TienePlan_(_protocolId, planId);
                    var resultplan = oTicketBL.TienePlan(_protocolId, lineId);

                    if (resultplan.Count > 0)
                    {
                        tienePlan = true;
                    }
                    else
                    {
                        tienePlan = false;
                    }

                    if (tienePlan)
                    {
                        if (resultplan[0].i_EsCoaseguro == 1)
                        {
                            #region Conexion SIGESOFT verificar la unidad productiva del componente
                            ConexionSigesoft conectasam = new ConexionSigesoft();
                            conectasam.opensigesoft();
                            var cadena1 = "select PL.d_ImporteCo, PL.i_PlanId " +
                                          "from [dbo].[plan] PL " +
                                          "inner join protocol PR on PL.v_ProtocoloId=PR.v_ProtocolId " +
                                          "where PR.v_ProtocolId='" + _protocolId + "' and PL.v_IdUnidadProductiva='" + lineId + "'";
                            SqlCommand    comando = new SqlCommand(cadena1, connection: conectasam.conectarsigesoft);
                            SqlDataReader lector = comando.ExecuteReader();
                            string        ImporteCo = ""; string PlanId = "";
                            bool          lectorleido = false;
                            while (lector.Read())
                            {
                                ImporteCo   = lector.GetValue(0).ToString();
                                PlanId      = lector.GetValue(1).ToString();
                                lectorleido = true;
                            }

                            if (lectorleido == false)
                            {
                                MessageBox.Show(@"Elija un Examen que tenga Plan de Seguros", @"Error de validación", MessageBoxButtons.OK);
                                return;
                            }
                            lector.Close();

                            #endregion
                            _objTicketDetalleList.d_SaldoPaciente    = decimal.Round((decimal.Parse(txtPrecioVenta.Text)), 2) * decimal.Round((decimal.Parse(txtCantidad.Text)), 2) * decimal.Round((decimal.Parse(ImporteCo)), 2) / 100;
                            _objTicketDetalleList.d_SaldoAseguradora = decimal.Round((decimal.Parse(txtPrecioVenta.Text)), 2) * decimal.Round((decimal.Parse(txtCantidad.Text)), 2) - _objTicketDetalleList.d_SaldoPaciente;
                            _objTicketDetalleList.d_PrecioVenta      = decimal.Round((decimal.Parse(txtPrecioVenta.Text)), 2);

                            #region Update a service agrega el PlanId
                            var cadena2 = "update service set " +
                                          "i_PlanId = '" + PlanId + "' " +
                                          "where v_ServiceId = '" + _serviceId + "' ";
                            SqlCommand    comando_ = new SqlCommand(cadena2, connection: conectasam.conectarsigesoft);
                            SqlDataReader lector_  = comando_.ExecuteReader();
                            lector_.Close();

                            conectasam.closesigesoft();
                            #endregion
                        }
                        else if (resultplan[0].i_EsDeducible == 1)
                        {
                        }
                    }
                    else
                    {
                        _objTicketDetalleList.d_PrecioVenta = decimal.Round((decimal.Parse(txtPrecio.Text)), 2);   // decimal.Parse(txtPrecioVenta.Text);
                    }

                    _objTicketDetalleList.i_RecordStatus = (int)RecordStatus.Agregado;
                    _objTicketDetalleList.i_RecordType   = (int)RecordType.Temporal;


                    _TempTicketDetalleList.Add(_objTicketDetalleList);
                }
                else
                {
                    if (findResult.i_RecordStatus == (int)RecordStatus.EliminadoLogico)
                    {
                        if (findResult.i_RecordType == (int)RecordType.NoTemporal)
                        {
                            _objTicketDetalleList.v_IdProductoDetalle  = txtMedicamento.Tag.ToString();
                            _objTicketDetalleList.v_NombreProducto     = txtMedicamento.Text;
                            _objTicketDetalleList.v_CodInterno         = txtCodigo.Text;
                            _objTicketDetalleList.v_IdUnidadProductiva = txtUnidadProductiva.Text;
                            var precioTarifa = oTicketBL.ObtenerPrecioTarifario(_serviceId, _objTicketDetalleList.v_IdProductoDetalle);
                            _objTicketDetalleList.d_PrecioVenta = precioTarifa;    // decimal.Parse(txtPrecioVenta.Text);
                            decimal d;
                            _objTicketDetalleList.d_Cantidad = decimal.TryParse(txtCantidad.Text, out d) ? d : 0;

                            var tienePlan  = false;
                            var resultplan = oTicketBL.TienePlan(_protocolId, txtUnidadProductiva.Text);
                            if (resultplan.Count > 0)
                            {
                                tienePlan = true;
                            }
                            else
                            {
                                tienePlan = false;
                            }

                            if (tienePlan)
                            {
                                if (resultplan[0].i_EsCoaseguro == 1)
                                {
                                    _objTicketDetalleList.d_SaldoPaciente    = resultplan[0].d_Importe;
                                    _objTicketDetalleList.d_SaldoAseguradora = (decimal.Parse(_objTicketDetalleList.d_PrecioVenta.ToString()) * _objTicketDetalleList.d_Cantidad) - resultplan[0].d_Importe;
                                }
                                if (resultplan[0].i_EsDeducible == 1)
                                {
                                    _objTicketDetalleList.d_SaldoPaciente    = resultplan[0].d_Importe * decimal.Parse(_objTicketDetalleList.d_PrecioVenta.ToString()) * _objTicketDetalleList.d_Cantidad / 100;
                                    _objTicketDetalleList.d_SaldoAseguradora = (decimal.Parse(_objTicketDetalleList.d_PrecioVenta.ToString()) * _objTicketDetalleList.d_Cantidad) - _objTicketDetalleList.d_SaldoPaciente;
                                }
                            }

                            findResult.i_RecordStatus = (int)RecordStatus.Grabado;
                        }
                        else if (findResult.i_RecordType == (int)RecordType.Temporal)       // El registro tiene un ID temporal [GUID]
                        {
                            _objTicketDetalleList.v_IdProductoDetalle  = txtMedicamento.Tag.ToString();
                            _objTicketDetalleList.v_NombreProducto     = txtMedicamento.Text;
                            _objTicketDetalleList.v_CodInterno         = txtCodigo.Text;
                            _objTicketDetalleList.d_PrecioVenta        = decimal.Parse(txtPrecioVenta.Text);
                            _objTicketDetalleList.v_IdUnidadProductiva = txtUnidadProductiva.Text;
                            decimal d;
                            _objTicketDetalleList.d_Cantidad = decimal.TryParse(txtCantidad.Text, out d) ? d : 0;

                            var tienePlan  = false;
                            var resultplan = oTicketBL.TienePlan(_protocolId, txtUnidadProductiva.Text);
                            if (resultplan.Count > 0)
                            {
                                tienePlan = true;
                            }
                            else
                            {
                                tienePlan = false;
                            }

                            if (tienePlan)
                            {
                                if (resultplan[0].i_EsCoaseguro == 1)
                                {
                                    _objTicketDetalleList.d_SaldoPaciente    = resultplan[0].d_Importe;
                                    _objTicketDetalleList.d_SaldoAseguradora = (decimal.Parse(_objTicketDetalleList.d_PrecioVenta.ToString()) * _objTicketDetalleList.d_Cantidad) - resultplan[0].d_Importe;
                                }
                                if (resultplan[0].i_EsDeducible == 1)
                                {
                                    _objTicketDetalleList.d_SaldoPaciente    = resultplan[0].d_Importe * decimal.Parse(_objTicketDetalleList.d_PrecioVenta.ToString()) * _objTicketDetalleList.d_Cantidad / 100;
                                    _objTicketDetalleList.d_SaldoAseguradora = (decimal.Parse(_objTicketDetalleList.d_PrecioVenta.ToString()) * _objTicketDetalleList.d_Cantidad) - _objTicketDetalleList.d_SaldoPaciente;
                                }
                            }

                            _objTicketDetalleList.i_RecordType = (int)RecordType.Temporal;

                            findResult.i_RecordStatus = (int)RecordStatus.Agregado;
                        }
                    }
                    else
                    {
                        MessageBox.Show("Por favor seleccione otro medicamento. ya existe", "Error de validación", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                }
            }
            else if (_mode == "Edit")
            {
                var     result = _TempTicketDetalleList.Find(p => p.v_TicketDetalleId == _id);
                decimal d;
                result.d_Cantidad = decimal.Round((decimal.TryParse(txtCantidad.Text, out d) ? d : 0), 2);
                if ((result.d_SaldoAseguradora != null || result.d_SaldoAseguradora >= 0) || result.d_SaldoPaciente != null || result.d_SaldoPaciente >= 0)
                {
                    result.d_SaldoAseguradora =
                        decimal.Round((decimal.TryParse(txtCantidad.Text, out d) ? d : 0), 2) * decimal.Round((decimal)result.d_PrecioVenta, 2);
                    result.d_SaldoPaciente = 0;
                }
                result.v_CodInterno   = txtCodigo.Text;
                result.i_RecordStatus = (int)RecordStatus.Modificado;
            }
            MessageBox.Show("Se grabo correctamente.", "INFORMACION!", MessageBoxButtons.OK, MessageBoxIcon.Information);

            this.Close();
        }