Advertisement
Guest User

Untitled

a guest
May 28th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. DELIMITER $$
  2.  
  3. CREATE FUNCTION `get_stoc_by_date`(p_data date, p_id_gestiune int, p_id_produs int) RETURNS decimal(18,4)
  4. BEGIN
  5. declare p_stoc decimal(18,4) default 0;
  6. SET p_stoc=
  7. /*niruri*/
  8. COALESCE((SELECT
  9. SUM(nc.cantitate_unitara)
  10. FROM niruri n
  11. INNER JOIN niruri_componente nc ON n.id_nir = nc.id_nir
  12. WHERE n.data <= p_data and n.id_status = 4 and
  13. n.id_gestiune = p_id_gestiune and nc.id_produs = p_id_produs),0)
  14. +
  15. /*transferuri intrare*/
  16. COALESCE( (SELECT
  17. SUM(tc.cantitate)
  18. FROM transferuri t
  19. INNER JOIN transferuri_componente tc ON t.id_transfer = tc.id_transfer
  20. WHERE t.data <= p_data and t.id_status = 4 and
  21. t.id_gestiune_destinatie = p_id_gestiune and tc.id_produs = p_id_produs),0)
  22. +
  23.  
  24. COALESCE( (SELECT
  25. SUM(l.cantitate_initiala)
  26. FROM inventare i
  27. INNER JOIN inventare_componente ic ON i.id_inventar = ic.id_inventar
  28. INNER JOIN loturi l ON ic.id_componenta = l.id_inventar_componenta
  29. WHERE i.data<= p_data and i.id_status = 4 and
  30. i.id_gestiune = p_id_gestiune and ic.id_produs = p_id_produs),0)
  31. -
  32. /*transferuri iesire*/
  33. COALESCE((SELECT
  34. SUM(tc.cantitate)
  35. FROM transferuri t
  36. INNER JOIN transferuri_componente tc ON t.id_transfer = tc.id_transfer
  37. WHERE t.data <= p_data and t.id_status = 4 and
  38. t.id_gestiune_sursa = p_id_gestiune and tc.id_produs = p_id_produs),0)
  39. -
  40. /*bonuri consum*/
  41. COALESCE((SELECT
  42. SUM(bcc.cantitate)
  43. FROM bonuri_consum bc
  44. INNER JOIN bonuri_consum_componente bcc ON bc.id_bon_consum = bcc.id_bon_consum
  45. WHERE bc.data <= p_data and bc.id_status = 4 and
  46. bc.id_gestiune = p_id_gestiune and bcc.id_produs = p_id_produs),0)
  47. -
  48. /*vanzari */
  49. COALESCE((SELECT
  50. SUM(vc.cantitate)
  51. FROM vanzari v
  52. INNER JOIN vanzari_componente vc ON v.id_vanzare = vc.id_vanzare
  53. WHERE v.data < date_add( p_data,INTERVAL 1 DAY) and
  54. v.id_gestiune = p_id_gestiune and vc.id_produs = p_id_produs),0)
  55.  
  56. -
  57. COALESCE( (SELECT
  58. SUM(lic.cantitate)
  59. FROM inventare i
  60. INNER JOIN inventare_componente ic ON i.id_inventar = ic.id_inventar
  61. INNER JOIN inventare_componente_loturi lic ON ic.id_componenta = lic.id_componenta
  62. WHERE i.data<= p_data and i.id_status = 4 and
  63. i.id_gestiune = p_id_gestiune and ic.id_produs = p_id_produs),0);
  64.  
  65. return p_stoc;
  66.  
  67. END $$
  68.  
  69. DELIMITER ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement