PageRenderTime 57ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/modelo/MdlArticulo.java

https://gitlab.com/aynixpe/Bodega_Naty
Java | 408 lines | 288 code | 27 blank | 93 comment | 10 complexity | fcdd0ad875073f7edca2cf9e4b7899f5 MD5 | raw file
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package modelo;
  7. import clases.Articulo;
  8. import clases.Categoria;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.util.ArrayList;
  12. /**
  13. *
  14. * @author Edder
  15. */
  16. public class MdlArticulo{
  17. /**
  18. *
  19. * @param articulo
  20. * @param cnx
  21. * @throws SQLException
  22. */
  23. public static void insert(Articulo articulo) throws SQLException, ClassNotFoundException{
  24. Conexion.abrirTransaccion();
  25. String sql = "INSERT INTO articulo(nombre, presentacion, descripcion, "
  26. + "stockMinimo, tamanio, unidad, codigoBarras, idCategoria, "
  27. + "precioVenta, estado) "
  28. + "VALUES( '" + articulo.getNombre() + "', "
  29. + "'" + articulo.getPresentacion() + "', "
  30. + "'" + articulo.getDescripcion() + "', "
  31. + articulo.getStockMinimo() + ", "
  32. + "'" + articulo.getTamanio() + "', "
  33. + "'" + articulo.getUnidad() + "', "
  34. + "'" + articulo.getCodigoBarras() + "', "
  35. + articulo.getCategoria().getIdCategoria() + ", "
  36. + articulo.getPrecioVenta() + ", "
  37. + "'" + articulo.getEstado() + "')";
  38. Conexion.ejecutar(sql);
  39. }
  40. public static void fastInsert(Articulo articulo) throws SQLException, ClassNotFoundException{
  41. Conexion.abrirTransaccion();
  42. String sql = "INSERT INTO articulo(nombre, idCategoria, precioVenta, estado) "
  43. + "VALUES( '" + articulo.getNombre() + "', "
  44. + articulo.getCategoria().getIdCategoria() + ", "
  45. + articulo.getPrecioVenta() + ", "
  46. + "'" + articulo.getEstado() + "')";
  47. Conexion.ejecutar(sql);
  48. }
  49. /**
  50. *
  51. * @param cnx
  52. * @return
  53. * @throws SQLException
  54. */
  55. public static int getLastId() throws SQLException, ClassNotFoundException{
  56. Conexion.abrirTransaccion();
  57. String sql = "SELECT idArticulo FROM articulo ORDER BY idArticulo DESC LIMIT 1";
  58. ResultSet rs = Conexion.consultar(sql);
  59. int id = -1;
  60. if(rs.next()){
  61. id = rs.getInt(1);
  62. }
  63. return id;
  64. }
  65. /**
  66. *
  67. * @param articulo
  68. * @param cnx
  69. * @throws SQLException
  70. */
  71. public static void update(Articulo articulo) throws SQLException, ClassNotFoundException{
  72. Conexion.abrirTransaccion();
  73. String sql = "UPDATE articulo SET "
  74. + "nombre = '" + articulo.getNombre() + "', "
  75. + "presentacion = '" + articulo.getPresentacion() + "', "
  76. + "descripcion = '" + articulo.getDescripcion() + "', "
  77. + "stockMinimo = " + articulo.getStockMinimo() + ", "
  78. + "tamanio = '" + articulo.getTamanio() + "', "
  79. + "unidad = '" + articulo.getUnidad() + "', "
  80. + "codigoBarras = '" + articulo.getCodigoBarras() + "', "
  81. + "idCategoria = " + articulo.getCategoria().getIdCategoria() + ", "
  82. + "precioVenta = " + articulo.getPrecioVenta() + ", "
  83. + "estado = '" + articulo.getEstado() + "' "
  84. + "WHERE idArticulo = " + articulo.getIdArticulo();
  85. System.out.println(sql);
  86. Conexion.ejecutar(sql);
  87. }
  88. /**
  89. * Cambia estado del art铆culo a 'I' : Inactivo
  90. * @param articulo
  91. * @param cnx
  92. * @throws SQLException
  93. */
  94. public static void dar_baja(Articulo articulo) throws SQLException, ClassNotFoundException{
  95. Conexion.abrirTransaccion();
  96. String sql = "UPDATE articulo SET "
  97. + "estado = 'I' "
  98. + "WHERE idArticulo = " + articulo.getIdArticulo();
  99. Conexion.ejecutar(sql);
  100. }
  101. /**
  102. *
  103. * @param stock
  104. * @param idArticulo
  105. * @param idAlmacen
  106. * @param cnx
  107. * @throws SQLException
  108. */
  109. public static void updateStock(int stock, int idArticulo, int idAlmacen)
  110. throws SQLException, ClassNotFoundException{
  111. Conexion.abrirTransaccion();
  112. String sql = "UPDATE almacenarticulo SET "
  113. + "stock += " + stock + " "
  114. + "WHERE idAlmacen = " + idAlmacen + " "
  115. + "AND idArticulo = " + idArticulo;
  116. Conexion.ejecutar(sql);
  117. }
  118. /**
  119. *
  120. * @param idArticulo
  121. * @param idAlmacen
  122. * @param cnx
  123. * @throws SQLException
  124. * @throws ClassNotFoundException
  125. */
  126. public static void asignarAlmacen(int idArticulo, int idAlmacen) throws SQLException, ClassNotFoundException{
  127. Conexion.abrirTransaccion();
  128. String sql = "INSERT INTO almacenarticulo(idAlmacen, idArticulo, stock, stockMinimo) "
  129. + "VALUES(" + idAlmacen + ", " + idArticulo + ", 0, 0)";
  130. Conexion.ejecutar(sql);
  131. }
  132. /**
  133. * Devuelve una lista de articulos que coincidan con la busqueda a realizar
  134. * @param query
  135. * @param where
  136. * @param cnx
  137. * @return
  138. * @throws SQLException
  139. */
  140. public static ArrayList<Articulo> getArticulos(String query, String where)
  141. throws SQLException, ClassNotFoundException{
  142. String sql = "SELECT * FROM articulo "
  143. + "WHERE (UPPER(nombre) LIKE UPPER('%" + query + "%') "
  144. + "OR UPPER(descripcion) LIKE ('%" + query + "%') "
  145. + "OR codigobarras LIKE '" + query + "') "
  146. + "AND estado = 'A' "
  147. + ((where != null && !where.equals(""))? where : ";");
  148. return MdlArticulo.get(sql);
  149. }
  150. /**
  151. *
  152. * @param query
  153. * @param cnx
  154. * @return
  155. * @throws SQLException
  156. */
  157. public static ArrayList<Articulo> getArticulos() throws SQLException, ClassNotFoundException{
  158. String sql = "SELECT idArticulo, nombre FROM articulo;";
  159. return MdlArticulo.get(sql);
  160. }
  161. public static ArrayList<Articulo> getArticulos_almacen(int idAlmacen, String busqueda)
  162. throws SQLException, ClassNotFoundException{
  163. String sql = "SELECT a.idArticulo, a.nombre, a.presentacion, a.descripcion, a.stockMinimo, "
  164. + "a.tamanio, a.unidad, a.codigoBarras, a.idCategoria, a.precioVenta, a.urlImagen, a.estado "
  165. + "FROM articulo as a "
  166. + "INNER JOIN almacenarticulo as aa "
  167. + "ON a.idArticulo = aa.idArticulo "
  168. + "WHERE aa.idAlmacen = " + idAlmacen + " and "
  169. + "UPPER(a.nombre) LIKE ('%" + busqueda + "%');";
  170. return MdlArticulo.get(sql);
  171. }
  172. /**
  173. *
  174. * @param idCategoria
  175. * @param query
  176. * @param cnx
  177. * @return
  178. * @throws SQLException
  179. */
  180. public static ArrayList<Articulo> getArticulos_categoria(int idCategoria, String query)
  181. throws SQLException, ClassNotFoundException{
  182. String sql = "SELECT * FROM articulo "
  183. + "WHERE UPPER(nombre) LIKE UPPER('%" + query + "%') "
  184. + "OR UPPER(descripcion) LIKE UPPER('%" + query + "%') "
  185. + "AND idCategoria = " + idCategoria + "; ";
  186. return MdlArticulo.get(sql);
  187. }
  188. /**
  189. *
  190. * @param idArticulo
  191. * @param cnx
  192. * @return
  193. * @throws SQLException
  194. */
  195. public static Articulo get(int idArticulo) throws SQLException, ClassNotFoundException{
  196. Conexion.abrirTransaccion();
  197. String sql = "SELECT * FROM articulo "
  198. + "WHERE idArticulo = " + idArticulo + "; ";
  199. ResultSet rs = Conexion.consultar(sql);
  200. Articulo articulo = null;
  201. if(rs.next()){
  202. articulo = new Articulo(rs.getInt(1),
  203. rs.getString(2),
  204. rs.getString(3),
  205. rs.getString(4),
  206. rs.getInt(5),
  207. rs.getString(6),
  208. rs.getString(7),
  209. rs.getString(8),
  210. new clases.Categoria(){{
  211. setIdCategoria(rs.getInt(9));
  212. }},
  213. rs.getFloat(10),
  214. rs.getString(11).charAt(0));
  215. }
  216. return articulo;
  217. }
  218. /**
  219. *Devuelve un arreglo de Object de 3 columnas (idArticulo, nombre, stock)
  220. * @param idAlmacen
  221. * @param query
  222. * @param cnx
  223. * @return ArrayList<Object[]>
  224. * @throws SQLException
  225. */
  226. public static ArrayList<Object[]> getArticulosStock_almacen(int idAlmacen, String query)
  227. throws SQLException, ClassNotFoundException{
  228. Conexion.abrirTransaccion();
  229. String sql = "select a.idArticulo, CONCAT(a.nombre, ' ', "
  230. + "a.presentacion, ' ', a.tamanio, ' ', a.unidad) as nombre, aa.stock "
  231. + "from articulo as a "
  232. + "inner join almacenarticulo as aa "
  233. + "on a.idArticulo = aa.idArticulo "
  234. + "where (UPPER(a.nombre) LIKE UPPER('%" + query + "%') "
  235. + "OR UPPER(a.descripcion) LIKE UPPER('%" + query + "%') "
  236. + "OR a.codigobarras LIKE '" + query + "') "
  237. + "AND a.estado = 'A' AND aa.idAlmacen = " + idAlmacen;
  238. ResultSet rs = Conexion.consultar(sql);
  239. ArrayList<Object[]> articulosStock = new ArrayList<>();
  240. while(rs.next()){
  241. Object[] stock = new Object[]{
  242. rs.getInt("idArticulo"),
  243. rs.getString("nombre"),
  244. rs.getInt("stock")
  245. };
  246. articulosStock.add(stock);
  247. }
  248. return articulosStock;
  249. }
  250. /*
  251. * Devuelve informaciè´¸n para mostrar en la tabla de orden de compra.
  252. */
  253. public static ArrayList<Object[]> getArticulos_OrdenCompra(int idAlmacen, String query, boolean flag)
  254. throws SQLException, ClassNotFoundException{
  255. Conexion.abrirTransaccion();
  256. String where = "";
  257. if (flag) {
  258. where = "AND aa.stock = 0 ";
  259. }
  260. String sql = "select a.idArticulo, a.nombre, aa.stock, "
  261. + "(select sum(stock) from almacenarticulo where idArticulo = a.idArticulo) as total, "
  262. + "sum(case cantidad when null then 0 else cantidad end) as cantidad "
  263. + "from articulo as a "
  264. + "left join detalleordencompra as doc "
  265. + "on a.idArticulo = doc.idArticulo "
  266. + "right join almacenarticulo as aa "
  267. + "on a.idArticulo = aa.idArticulo "
  268. + "where (UPPER(a.nombre) LIKE UPPER('%" + query + "%') "
  269. + "OR UPPER(a.descripcion) LIKE UPPER('%" + query + "%')) "
  270. + "AND aa.idAlmacen = " + idAlmacen + " "
  271. + where
  272. + "group by a.idArticulo, a.nombre, a.stockMinimo, aa.stock "
  273. + "ORDER BY cantidad DESC, a.nombre ASC";
  274. ResultSet rs = Conexion.consultar(sql);
  275. ArrayList<Object[]> articulosStock = new ArrayList<>();
  276. while(rs.next()){
  277. Object[] stock = new Object[]{
  278. rs.getInt("idArticulo"),
  279. rs.getString("nombre"),
  280. rs.getInt("stock"),
  281. rs.getInt("total"),
  282. rs.getInt("cantidad")
  283. };
  284. articulosStock.add(stock);
  285. }
  286. return articulosStock;
  287. }
  288. public static ArrayList<Articulo> getCategoria(String query, ArrayList<Categoria> categorias, boolean inactivo) throws ClassNotFoundException, SQLException{
  289. ArrayList<Articulo> articulos;
  290. String where = "";
  291. if(categorias.size() > 0){
  292. where = " AND (";
  293. int i = 0;
  294. for(Categoria cat : categorias){
  295. if(i > 0){
  296. where += " OR ";
  297. }
  298. i++;
  299. where += "idCategoria = " + cat.getIdCategoria();
  300. }
  301. where += ")";
  302. }else
  303. where = "";
  304. where += inactivo ? "" : " AND estado NOT LIKE 'I'";
  305. String sql = "SELECT * FROM articulo where UPPER(nombre) like UPPER('%" + query + "%') "
  306. + where + " ORDER BY nombre asc";
  307. articulos = MdlArticulo.get(sql);
  308. return articulos;
  309. }
  310. public static ArrayList<Articulo> get(String sql) throws SQLException, ClassNotFoundException{
  311. Conexion.abrirTransaccion();
  312. ArrayList<Articulo> articulos = new ArrayList<>();
  313. ResultSet rs = Conexion.consultar(sql);
  314. while(rs.next()){
  315. articulos.add(new Articulo(rs.getInt(1),
  316. rs.getString(2),
  317. rs.getString(3),
  318. rs.getString(4),
  319. rs.getInt(5),
  320. rs.getString(6),
  321. rs.getString(7),
  322. rs.getString(8),
  323. new clases.Categoria(){{
  324. setIdCategoria(rs.getInt(9));
  325. }},
  326. rs.getFloat(10),
  327. rs.getString(11).charAt(0)));
  328. }
  329. return articulos;
  330. }
  331. /*
  332. * Metodo usado en anular movimiento para obtener el stock de articulos de un almacen
  333. */
  334. public static ArrayList<Object[]> articulosCantidad(int almacen) throws SQLException, ClassNotFoundException{
  335. Conexion.abrirTransaccion();
  336. ArrayList<Object[]> rows = new ArrayList<>();
  337. String sql = "SELECT idArticulo, stock "
  338. + "FROM almacenarticulo "
  339. + "WHERE idAlmacen = " + almacen;
  340. ResultSet rs = Conexion.consultar(sql);
  341. while(rs.next()){
  342. rows.add(new Object[]{
  343. rs.getInt("idArticulo"),
  344. rs.getInt("stock")
  345. });
  346. }
  347. return rows;
  348. }
  349. public static ArrayList<Object[]> getArticulos(int idAlmacen, Categoria categoria, String query)
  350. throws SQLException, ClassNotFoundException{
  351. Conexion.abrirTransaccion();
  352. ArrayList<Object[]> rows = new ArrayList<>();
  353. String sqlcategoria = "";
  354. if(categoria != null){
  355. sqlcategoria = "c.idCategoria = " + categoria.getIdCategoria() + " AND ";
  356. }
  357. String sql = "SELECT a.idArticulo, CONCAT(a.nombre, ' ', "
  358. + "a.presentacion, ' ', a.tamanio, ' ', a.unidad) as nombre, aa.stock, a.precioVenta as precio "
  359. + "FROM articulo as a "
  360. + "INNER JOIN almacenarticulo as aa "
  361. + "ON a.idArticulo = aa.idArticulo "
  362. + "INNER JOIN categoria as c "
  363. + "ON a.idCategoria = c.idCategoria "
  364. + "WHERE " + sqlcategoria
  365. + "aa.idAlmacen = " + idAlmacen + " "
  366. + "AND (a.codigoBarras LIKE '%" + query + "%' "
  367. + "OR UPPER(a.nombre) LIKE UPPER('%" + query + "%')) AND a.estado = 'A' "
  368. + "ORDER BY a.nombre asc, a.tamanio asc;";
  369. System.out.println(sql);
  370. ResultSet rs = Conexion.consultar(sql);
  371. while(rs.next()){
  372. rows.add(new Object[]{
  373. rs.getInt("idArticulo"),
  374. rs.getString("nombre"),
  375. rs.getInt("stock"),
  376. rs.getFloat("precio")
  377. });
  378. }
  379. return rows;
  380. }
  381. }