ValidarIVA.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using sage.ew.ewbase;
6 using System.Xml.Serialization;
7 using System.IO;
8 using System.Data;
9 using sage.ew.db;
10 using System.ComponentModel;
11 using sage.ew.listados.Clases;
12 using sage.ew.functions;
13 
14 namespace sage.addons.idatos.Negocio.Clases
15 {
16  #region CLASE_VALIDARIVA
17  public class ValidarIVA: Validaciones
21  {
22  #region PROPIEDADES
23 
27  public string valorCSV_col = string.Empty;
28 
32  public string mensaje = string.Empty;
33 
34  #endregion PROPIEDADES
35 
36  #region CONSTRUCTORES
37  public ValidarIVA()
41  {
42 
43  }
44 
48  public ValidarIVA(string lcCodigoGuia)
49  {
50  codigoGuia = lcCodigoGuia;
51  }
52 
53  #endregion CONSTRUCTORES
54 
55  #region METODOS PUBLICOS
56 
61  public override ValidationResult Validar()
62  {
63  ValidationResult lcResult = null;
64  return lcResult;
65  }
66 
71  public override ValidationResult Validar(string valorColumna, DataRow drEnImportacion)
72  {
73  dataRowEnImportacion = drEnImportacion;
74  valorCSV_col = valorColumna;
75  ValidationResult lcResult = ValidacionesValorCampo();
76 
77  return lcResult;
78  }
79 
84  public override ValidationResult Validar(string valorColumna, int columna)
85  {
86  ValidationResult lcResult = null;
87  return lcResult;
88  }
89 
93  private ValidationResult ValidacionesValorCampo()
94  {
95  ValidationResult lcResult = null;
96 
97  bool lbOk = true;
98  bool lbExcepcion = false;
99 
100  if (valorCSV_col.Contains("%"))
101  valorCSV_col = valorCSV_col.Replace("%", "");
102 
103  if (valorCSV_col.Contains(","))
104  valorCSV_col = valorCSV_col.Replace(",", ".");
105 
106  if (dyValidar.Keys.Contains(valorCSV_col))
107  {
108  lcResult = dyValidar.Where(p => p.Key == valorCSV_col).FirstOrDefault().Value;
109  return lcResult;
110  }
111 
112  //138456
113  string codigoS50 = ObtenerCodigoIvaS50();
114 
115  if (!string.IsNullOrEmpty(codigoS50))
116  {
117  lcResult = new ValidationResult(lbOk, mensaje);
118  dyValidar.Add(valorCSV_col, lcResult);
119 
120  return lcResult;
121  }
122 
123  bool incluirRecargo = IncluirRecargo();
124 
125  mensaje = string.Empty;
126 
127  string valorIVA = valorCSV_col;
128  if (!string.IsNullOrEmpty(valorCSV_col.Trim()))
129  {
130  DataTable tablaIva = new DataTable();
131  string lcSql = "SELECT * FROM " + DB.SQLDatabase("GESTION", "TIPO_IVA");
132 
133  if (string.IsNullOrWhiteSpace(valorIVA))
134  {
135  lcSql += " WHERE IVA = 21 AND ";
136  if (incluirRecargo)
137  lcSql += "RECARG <> 0 AND IVA_CEE != '' AND ";
138  else
139  lcSql += "RECARG = 0 AND ";
140 
141  lcSql += "DEDUCE = 1 AND EXENTO = 0";
142  }
143  else if (Convert.ToDecimal(valorIVA) == 0)
144  {
145  lcSql += " WHERE IVA = 0 AND DEDUCE = 1 AND EXENTO = 0";
146  }
147  else
148  {
149  lcSql += " WHERE IVA = " + DB.SQLString(valorIVA) + " AND ";
150 
151  if (incluirRecargo)
152  {
153  lcSql += "RECARG <> 0 AND IVA_CEE != '' AND ";
154  }
155  else
156  {
157  lcSql += "RECARG = 0 AND ";
158  lbExcepcion = true;
159  }
160 
161  lcSql += "DEDUCE = 1 AND EXENTO = 0";
162  }
163 
164  //138456
165  if (lbExcepcion)
166  lcSql += " AND CODIGO NOT IN ('11', '12', '13') AND CODIGO NOT IN (SELECT DISTINCT IVA_CEE FROM " + DB.SQLDatabase("GESTION", "TIPO_IVA") + " WHERE IVA_CEE != '') ";
167 
168  DB.SQLExec(lcSql, ref tablaIva);
169 
170  if (tablaIva.Rows.Count == 0)
171  {
172  lbOk = false;
173  mensaje = "Existe en la tabla tipos de IVA un valor " + valorIVA + ", pero no puede ser asignado por no existir en destino exactamente este tipo de IVA";
174  }
175 
176  // Comprobar si tiene equivalente comunitario
177  /*
178  DataTable dtMomen = new DataTable();
179 
180  if (Convert.ToDecimal(valorIVA) == 0)
181  {
182  lcSql = "SELECT count(*) as num FROM " + DB.SQLDatabase("GESTION", "TIPO_IVA") + " WHERE IVA = " + valorIVA;
183  }
184  else
185  {
186  lcSql = "SELECT count(*) as num FROM " + DB.SQLDatabase("GESTION", "TIPO_IVA") + " WHERE IVA = " + valorIVA + " AND NOMBRE LIKE '%CEE%' ";
187  }
188  DB.SQLExec(lcSql, ref dtMomen);
189  if (dtMomen != null && dtMomen.Rows.Count > 0)
190  {
191  if (Convert.ToInt32(dtMomen.Rows[0]["num"]) == 0)
192  {
193  lbOk = false;
194  mensaje = "Existe en la tabla tipos de IVA un valor " + valorIVA + ", pero no puede ser asignado por no llevar equivalente";
195  }
196  }
197  */
198  }
199 
200  lcResult = new ValidationResult(lbOk, mensaje);
201  dyValidar.Add(valorCSV_col, lcResult);
202 
203  return lcResult;
204  }
205 
206  #endregion METODOS PUBLICOS
207 
208  #region METODOS PRIVADOS
209  private bool IncluirRecargo()
214  {
215  Negocio.Mantes.plinter_c loConfigCSV = new Negocio.Mantes.plinter_c(codigoGuia);
216  CSVConfig cs = loConfigCSV._ObtenerConfiguracionCSV();
217 
218  string lcBooleanoTrue = cs._BooleanoTrue.Trim();
219  string lcBooleanoFalse = cs._BooleanoFalse.Trim();
220 
221  DataTable ldPlid = new DataTable();
222 
223  string lcSql = "SELECT * FROM " + DB.SQLDatabase("IDATOS", "PLINTER_D") +
224  " WHERE CODIGO = " + DB.SQLString(codigoGuia);
225  DB.SQLExec(lcSql, ref ldPlid);
226 
227  if (ldPlid != null || ldPlid.Rows.Count > 0)
228  {
229  string nombreColumnaRecargo;
230 
231  var columna = ldPlid.Rows.OfType<DataRow>().Where(F => (Convert.ToInt32(F["IDCAMPO"]) == 50029 ||
232  Convert.ToInt32(F["IDCAMPO"]) == 50056 ||
233  Convert.ToInt32(F["IDCAMPO"]) == 50054) &&
234  !string.IsNullOrWhiteSpace(F["columna"].ToString())).FirstOrDefault();
235 
236  if (columna == null)
237  {
238  return true;
239  }
240  nombreColumnaRecargo = columna["COLUMNA"].ToString().Trim();
241 
242  string valDefColumnaRecargo = columna["VAL_DEF"].ToString().Trim();
243  if (nombreColumnaRecargo != null)
244  {
245  int posicion = posicionColumna(nombreColumnaRecargo);
246  string rec = dataRowEnImportacion.ItemArray[posicion].ToString().Trim();
247 
248  if (string.IsNullOrEmpty(rec))
249  {
250  return valDefColumnaRecargo.ToUpper() == lcBooleanoTrue.ToUpper() || valDefColumnaRecargo.ToUpper() == ".T.";
251  }
252  else
253  {
254  if (rec.ToUpper() == lcBooleanoTrue.ToUpper() || rec.ToUpper() == ".T." || rec.ToUpper() == lcBooleanoFalse.ToUpper() || rec.ToUpper() == ".F.")
255  {
256  return rec.ToUpper() == lcBooleanoTrue.ToUpper() || rec.ToUpper() == ".T.";
257  }
258  else
259  {
260  return true;
261  }
262  }
263  }
264  }
265 
266  return true;
267  }
268 
273  private string ObtenerCodigoIvaS50()
274  {
275  DataTable ldPlid = new DataTable();
276 
277  string lcSql = "SELECT * FROM " + DB.SQLDatabase("IDATOS", "PLINTER_D") +
278  " WHERE CODIGO = " + DB.SQLString(codigoGuia);
279  DB.SQLExec(lcSql, ref ldPlid);
280 
281  if (ldPlid != null || ldPlid.Rows.Count > 0)
282  {
283  string nombreColumnaCodIva = "";
284  string valorDefectoCodIva = "";
285  var columna = ldPlid.Rows.OfType<DataRow>().Where(F => Convert.ToInt32(F["IDCAMPO"]) == 50030).FirstOrDefault();
286  if (columna != null)
287  {
288  nombreColumnaCodIva = columna["COLUMNA"].ToString().Trim();
289  valorDefectoCodIva = columna["VAL_DEF"].ToString().Trim();
290  }
291  else
292  return null;
293 
294  if (nombreColumnaCodIva != null)
295  {
296  int posicion = posicionColumna(nombreColumnaCodIva);
297  string rec = dataRowEnImportacion.ItemArray[posicion].ToString();
298 
299  if (string.IsNullOrEmpty(rec))
300  return null;
301  else
302  {
303  //139950
304  bool bValido = true;
305 
306  string lcSqlComprobar = "SELECT * FROM " + DB.SQLDatabase("GESTION", "TIPO_IVA") + " WHERE CODIGO = '" + rec + "'";
307  DataTable tablaIvaComprobar = new DataTable();
308  DB.SQLExec(lcSqlComprobar, ref tablaIvaComprobar);
309 
310  if (tablaIvaComprobar.Rows.Count == 0)
311  bValido = false;
312 
313  if (!bValido)
314  {
315  string lcMessage = "No se ha podido encontrar el tipo de IVA con código " + rec + " especificado en el fichero CSV-EXCEL." + Environment.NewLine +
316  "Por favor, revisa el fichero de datos, y si el código de IVA es correcto asegurate de tenerlo creado dentro de tu " + Convert.ToString(sage.ew.global.EW_GLOBAL._GetVariable("wc_nameproducto"));
317 
318  FUNCTIONS._MessageBox(lcMessage, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Stop, System.Windows.Forms.MessageBoxDefaultButton.Button1, System.Windows.Forms.DialogResult.OK);
319 
320  //si hay valDef, usamos ese, si no, buscamos el 21%
321  if (!string.IsNullOrEmpty(valorDefectoCodIva))
322  return valorDefectoCodIva;
323 
324  string lcSqlIvaDefecto = "SELECT * FROM " + DB.SQLDatabase("GESTION", "TIPO_IVA") + " WHERE IVA = 21 AND RECARG <> 0 AND IVA_CEE != '' AND DEDUCE = 1 AND EXENTO = 0";
325  DataTable tablaIvaDefecto = new DataTable();
326  DB.SQLExec(lcSqlIvaDefecto, ref tablaIvaDefecto);
327 
328  if (tablaIvaDefecto.Rows.Count > 0)
329  return tablaIvaDefecto.Rows[0]["CODIGO"].ToString();
330 
331  return "03";
332  }
333 
334  return rec;
335  }
336  }
337  }
338 
339  return null;
340  }
341 
347  private int posicionColumna(string nombreColumna)
348  {
349  int ret = 0;
350  if (nombreColumna.Length == 1)
351  {
352  ret = Encoding.ASCII.GetBytes(nombreColumna)[0] - 65;
353  }
354  else
355  {
356  if (!string.IsNullOrWhiteSpace(nombreColumna))
357  {
358  Int32 times = Convert.ToInt32(nombreColumna.Substring(0, 1));
359  ret = ((Encoding.ASCII.GetBytes("Z")[0] - 65) * times) + (Encoding.ASCII.GetBytes(nombreColumna)[1] - 65) + 1;
360  }
361  }
362 
363  return ret;
364  }
365  #endregion METODOS PRIVADOS
366  }
367  #endregion CLASE_VALIDARIVA
368 }
369 
370 
371 
372 
373 
374 
375 
376 
Es como el tipo de entrada asientos pero por negocio, sin formulario, pq quiero que me haga las propu...
ValidarIVA(string lcCodigoGuia)
Clase ValidationResult
Definition: ValidarIVA.cs:48
Clase EW_GLOBAL para gestionar las variables públicas de Eurowin, variables de empresa, variables de anchuras de campo, variables de mascaras, etc.
Definition: EW_GLOBAL.cs:46
override ValidationResult Validar(string valorColumna, DataRow drEnImportacion)
Definition: ValidarIVA.cs:71
override ValidationResult Validar(string valorColumna, int columna)
Definition: ValidarIVA.cs:84
override ValidationResult Validar()
Definition: ValidarIVA.cs:61