예제 #1
0
        public string Delete(SaleData Sale)
        {
            string        res    = "";
            SqlConnection SqlCon = new SqlConnection();

            try
            {
                // Connection
                SqlCon.ConnectionString = Connection.Con;
                SqlCon.Open();

                // Comand
                SqlCommand SqlCmd = new SqlCommand();
                SqlCmd.Connection  = SqlCon;
                SqlCmd.CommandText = "peliminar_venta";
                SqlCmd.CommandType = CommandType.StoredProcedure;

                // Parameters
                SqlParameter ParId = new SqlParameter();
                ParId.ParameterName = "@id_venta";
                ParId.SqlDbType     = SqlDbType.Int;
                ParId.Value         = Sale.IdSale;
                SqlCmd.Parameters.Add(ParId);

                // Execute
                res = SqlCmd.ExecuteNonQuery() == 1 ? "Correcto" : "Correcto"; // Igual por el tema de los triggers
            }
            catch (Exception e)
            {
                res = e.Message;
            }
            finally
            {
                // Close connection
                if (SqlCon.State == ConnectionState.Open)
                {
                    SqlCon.Close();
                }
            }

            return(res);
        }
예제 #2
0
        public string Insert(SaleData Sale, List <SaleDetailData> ListSaleDetail)
        {
            string        res    = "";
            SqlConnection SqlCon = new SqlConnection();

            try
            {
                // Connection
                SqlCon.ConnectionString = Connection.Con;
                SqlCon.Open();

                // Transaction
                SqlTransaction SqlTst = SqlCon.BeginTransaction();

                // Comand
                SqlCommand SqlCmd = new SqlCommand();
                SqlCmd.Connection  = SqlCon;
                SqlCmd.Transaction = SqlTst;
                SqlCmd.CommandText = "pinsertar_venta";
                SqlCmd.CommandType = CommandType.StoredProcedure;

                // Parameters
                SqlParameter ParId = new SqlParameter();
                ParId.ParameterName = "@id_venta";
                ParId.SqlDbType     = SqlDbType.Int;
                ParId.Direction     = ParameterDirection.Output;
                SqlCmd.Parameters.Add(ParId);

                SqlParameter ParIdCustomer = new SqlParameter();
                ParIdCustomer.ParameterName = "@id_cliente";
                ParIdCustomer.SqlDbType     = SqlDbType.Int;
                ParIdCustomer.Value         = Sale.IdCustomer;
                SqlCmd.Parameters.Add(ParIdCustomer);

                SqlParameter ParIdEmployee = new SqlParameter();
                ParIdEmployee.ParameterName = "@id_empleado";
                ParIdEmployee.SqlDbType     = SqlDbType.Int;
                ParIdEmployee.Value         = Sale.IdEmployee;
                SqlCmd.Parameters.Add(ParIdEmployee);

                SqlParameter ParDate = new SqlParameter();
                ParDate.ParameterName = "@fecha";
                ParDate.SqlDbType     = SqlDbType.Date;
                ParDate.Value         = Sale.DateSale;
                SqlCmd.Parameters.Add(ParDate);

                SqlParameter ParReceiptType = new SqlParameter();
                ParReceiptType.ParameterName = "@tipo_recibo";
                ParReceiptType.SqlDbType     = SqlDbType.VarChar;
                ParReceiptType.Size          = 50;
                ParReceiptType.Value         = Sale.ReceiptType;
                SqlCmd.Parameters.Add(ParReceiptType);

                SqlParameter ParSerie = new SqlParameter();
                ParSerie.ParameterName = "@serie";
                ParSerie.SqlDbType     = SqlDbType.VarChar;
                ParSerie.Size          = 4;
                ParSerie.Value         = Sale.Serie;
                SqlCmd.Parameters.Add(ParSerie);

                SqlParameter ParSequent = new SqlParameter();
                ParSequent.ParameterName = "@consecutivo";
                ParSequent.SqlDbType     = SqlDbType.VarChar;
                ParSequent.Size          = 8;
                ParSequent.Value         = Sale.Sequent;
                SqlCmd.Parameters.Add(ParSequent);

                SqlParameter ParIva = new SqlParameter();
                ParIva.ParameterName = "@iva";
                ParIva.SqlDbType     = SqlDbType.Decimal;
                ParIva.Precision     = 4;
                ParIva.Scale         = 2;
                ParIva.Value         = Sale.Iva;
                SqlCmd.Parameters.Add(ParIva);

                // Execute
                res = SqlCmd.ExecuteNonQuery() == 1 ? "Correcto" : "No se ha podido insertar el registro";

                if (res.Equals("Correcto"))
                {
                    // Get Sale ID
                    this.IdSale = Convert.ToInt32(SqlCmd.Parameters["@id_venta"].Value);
                    foreach (SaleDetailData detail in ListSaleDetail)
                    {
                        detail.IdSale = this.IdSale;
                        res           = detail.Insert(detail, ref SqlCon, ref SqlTst);
                        if (!res.Equals("Correcto"))
                        {
                            break;
                        }
                        else
                        {
                            // Update Stock
                            res = ReduceStock(detail.IdEntryDetail, detail.Amount);
                            if (!res.Equals("Correcto"))
                            {
                                break;
                            }
                        }
                    }
                }

                if (res.Equals("Correcto"))
                {
                    SqlTst.Commit();
                }
                else
                {
                    SqlTst.Rollback();
                }
            }
            catch (Exception e)
            {
                res = e.Message;
            }
            finally
            {
                // Close connection
                if (SqlCon.State == ConnectionState.Open)
                {
                    SqlCon.Close();
                }
            }

            return(res);
        }