lego11

Fatture eRGB

Oct 19th, 2017
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- CONFIGURAZIONE
  2. -- Lato stampante
  3. LPT = "right"
  4.  
  5. -- DICHIARAZIONE VARIABILI
  6. -- contatori totali
  7. consumoTotale = 0
  8. immissioneTotale = 0
  9.  
  10. cliente = nil
  11.  
  12. -- spese fisse totali
  13. speseFisseConsTotali = 0
  14. speseFisseImmTotali = 0
  15.  
  16. -- IMPLEMENTAZIONE DEL DRAWFILLEDBOX
  17. local function drawPixelInternal(xPos, yPos)
  18.     term.setCursorPos(xPos, yPos)
  19.     term.write(" ")
  20. end
  21. local tColourLookup = {}
  22. for n = 1, 16 do
  23.     tColourLookup[string.byte("0123456789abcdef", n, n)] = 2 ^ (n - 1)
  24. end
  25. function drawFilledBox(startX, startY, endX, endY, nColour)
  26.     if type(startX) ~= "number" or type(startX) ~= "number" or type(endX) ~=
  27.         "number" or type(endY) ~= "number" or
  28.         (nColour ~= nil and type(nColour) ~= "number") then
  29.         error("Expected startX, startY, endX, endY, colour", 2)
  30.     end
  31.  
  32.     startX = math.floor(startX)
  33.     startY = math.floor(startY)
  34.     endX = math.floor(endX)
  35.     endY = math.floor(endY)
  36.  
  37.     if nColour then term.setBackgroundColor(nColour) end
  38.     if startX == endX and startY == endY then
  39.         drawPixelInternal(startX, startY)
  40.         return
  41.     end
  42.  
  43.     local minX = math.min(startX, endX)
  44.     if minX == startX then
  45.         minY = startY
  46.         maxX = endX
  47.         maxY = endY
  48.     else
  49.         minY = endY
  50.         maxX = startX
  51.         maxY = startY
  52.     end
  53.  
  54.     for x = minX, maxX do for y = minY, maxY do drawPixelInternal(x, y) end end
  55. end
  56. -- Icone
  57. function disegnaAvviso()
  58.     drawFilledBox(5, 6, 13, 13, colours.yellow)
  59.     for i = 7, 12, 1 do
  60.         paintutils.drawPixel(9, i, colours.black)
  61.         paintutils.drawPixel(9, 11, colours.yellow)
  62.     end
  63. end
  64. function disegnaFloppy()
  65.     drawFilledBox(4, 5, 18, 15, colours.lime)
  66.     drawFilledBox(7, 5, 15, 8, colours.lightGrey)
  67.     drawFilledBox(13, 6, 14, 7, colours.lime)
  68.     drawFilledBox(5, 10, 17, 15, colours.white)
  69.     paintutils.drawPixel(18, 5, colours.black)
  70. end
  71. function disegnaFoglio()
  72.     drawFilledBox(4, 5, 18, 15, colours.white)
  73.     paintutils.drawPixel(6, 12, colours.green)
  74.     paintutils.drawPixel(7, 13, colours.green)
  75.     paintutils.drawPixel(8, 14, colours.green)
  76.     px = 9
  77.     py = 13
  78.     for i = 1, 8, 1 do
  79.         paintutils.drawPixel(px, py, colours.green)
  80.         px = px + 1
  81.         py = py - 1
  82.     end
  83. end
  84.  
  85. function disegnaStampante()
  86.     drawFilledBox(4, 10, 18, 15, colours.lightGrey)
  87.     drawFilledBox(6, 6, 16, 10, colours.white)
  88.     drawFilledBox(6, 14, 16, 14, colours.grey)
  89. end
  90.  
  91. -- Funzione pulisci
  92. function clear()
  93.     term.clear()
  94.     term.setCursorPos(1, 1)
  95. end
  96.  
  97. -- Funzioni colori
  98. function colore(sfumatura) term.setTextColour(sfumatura) end
  99. function fineColore() term.setTextColour(colours.white) end
  100. function sfondo(sfumaturaSfondo) term.setBackgroundColour(sfumaturaSfondo) end
  101. function fineSfondo() term.setBackgroundColour(colours.black) end
  102.  
  103. -- Disegna il Titolo
  104. function disegnaTitolo()
  105.     local tx, ty = term.getCursorPos()
  106.     term.setCursorPos(1, 1)
  107.     colore(colours.black)
  108.     sfondo(colours.yellow)
  109.     print("            Creazione Guidata Bolletta               ")
  110.     fineColore()
  111.     fineSfondo()
  112.     term.setCursorPos(tx, ty)
  113. end
  114.  
  115. -- Schermata Consumo
  116. function finestraConsumo()
  117.     -- Schermata iniziale
  118.     clear()
  119.     sfondo(colours.black)
  120.     term.clear()
  121.     disegnaTitolo()
  122.  
  123.     term.setCursorPos(1, 3)
  124.     print(
  125.         "Inserire i dati richiesti. Se il campo è corretto, diventerà verde. In caso contrario il computer chiederà di reinserire il dato. Per annullare premere CTRL+T.")
  126.     -- Intestatario del contratto
  127.     term.setCursorPos(1, 9)
  128.     term.write("Intestatario:")
  129.     term.setCursorPos(15, 9)
  130.     fineColore()
  131.     if cliente == nil or cliente == "" then cliente = read() end
  132.     term.setCursorPos(15, 9)
  133.     colore(colours.green)
  134.     term.write(cliente)
  135.     fineColore()
  136.  
  137.     -- Lettura contatore consumo
  138.     colore(colours.white)
  139.     term.setCursorPos(1, 11)
  140.     term.write("Consumo (Wh):")
  141.     term.setCursorPos(15, 11)
  142.     fineColore()
  143.     consumoAccettato = false
  144.     conteggioErrori = 0
  145.     while consumoAccettato == false do
  146.         consumo = read()
  147.         colore(colours.white)
  148.         term.setCursorPos(14, 11)
  149.         fineColore()
  150.         if tonumber(consumo) == "" or tonumber(consumo) == nil or
  151.             tonumber(consumo) < 0 then
  152.             term.setCursorPos(15, 11)
  153.             colore(colours.red)
  154.             term.write("Il dato immesso non corrisponde    ")
  155.             fineColore()
  156.             sleep(2)
  157.             term.setCursorPos(15, 11)
  158.             term.write("                                   ")
  159.             term.setCursorPos(15, 11)
  160.         else
  161.             term.setCursorPos(15, 11)
  162.             colore(colours.green)
  163.             term.write(consumo .. " Wh")
  164.             consumoAccettato = true
  165.  
  166.         end
  167.     end
  168.  
  169.     -- Lettura spese fisse
  170.     colore(colours.white)
  171.     term.setCursorPos(1, 13)
  172.     term.write("Spese fisse:")
  173.     term.setCursorPos(15, 13)
  174.     fineColore()
  175.     local sfAccettate = false
  176.     conteggioErrori = 0
  177.     while sfAccettate == false do
  178.         speseFisseCons = read()
  179.         colore(colours.white)
  180.         term.setCursorPos(14, 13)
  181.         fineColore()
  182.         if tonumber(speseFisseCons) == "" or tonumber(speseFisseCons) == nil or
  183.             tonumber(speseFisseCons) < 0 then
  184.             term.setCursorPos(15, 13)
  185.             colore(colours.red)
  186.             term.write("Il dato immesso non corrisponde    ")
  187.             fineColore()
  188.             sleep(2)
  189.             term.setCursorPos(15, 13)
  190.             term.write("                                   ")
  191.             term.setCursorPos(15, 13)
  192.         else
  193.             term.setCursorPos(15, 13)
  194.             colore(colours.green)
  195.             term.write(speseFisseCons .. " IC")
  196.             sfAccettate = true
  197.         end
  198.     end
  199.  
  200.     -- Calcolo totale contatore consumo
  201.     consumoTotale = consumoTotale + math.floor(consumo / 1000000)
  202.     speseFisseConsTotali = speseFisseConsTotali + speseFisseCons
  203. end
  204.  
  205. -- Schermata immissione
  206.  
  207. function finestraImmissione()
  208.     -- Schermata iniziale
  209.     clear()
  210.     sfondo(colours.black)
  211.     term.clear()
  212.     disegnaTitolo()
  213.  
  214.     term.setCursorPos(1, 3)
  215.     print(
  216.         "Inserire i dati richiesti. Se il campo è corretto, diventerà verde. In caso contrario il computer chiederà di reinserire il dato. Per annullare premere CTRL+T.")
  217.     -- Intestatario del contratto
  218.     term.setCursorPos(1, 9)
  219.     term.write("Intestatario:")
  220.     fineColore()
  221.     term.setCursorPos(15, 9)
  222.     colore(colours.green)
  223.     term.write(cliente)
  224.     fineColore()
  225.  
  226.     -- Lettura contatore immissione
  227.     colore(colours.white)
  228.     term.setCursorPos(1, 11)
  229.     term.write("Immiss. (Wh):")
  230.     term.setCursorPos(15, 11)
  231.     fineColore()
  232.     immissioneAccettata = false
  233.     conteggioErrori = 0
  234.     while immissioneAccettata == false do
  235.         immissione = read()
  236.         colore(colours.white)
  237.         term.setCursorPos(14, 11)
  238.         fineColore()
  239.         if tonumber(immissione) == "" or tonumber(immissione) == nil or
  240.             tonumber(immissione) < 0 then
  241.             term.setCursorPos(15, 11)
  242.             colore(colours.red)
  243.             term.write("Il dato immesso non corrisponde    ")
  244.             fineColore()
  245.             sleep(2)
  246.             term.setCursorPos(15, 11)
  247.             term.write("                                   ")
  248.             term.setCursorPos(15, 11)
  249.         else
  250.             term.setCursorPos(15, 11)
  251.             colore(colours.green)
  252.             term.write(immissione .. " Wh")
  253.             immissioneAccettata = true
  254.  
  255.         end
  256.     end
  257.  
  258.     -- Lettura spese fisse
  259.     colore(colours.white)
  260.     term.setCursorPos(1, 13)
  261.     term.write("Spese fisse:")
  262.     term.setCursorPos(15, 13)
  263.     fineColore()
  264.     local sfImmAccettate = false
  265.     conteggioErrori = 0
  266.     while sfImmAccettate == false do
  267.         speseFisseImm = read()
  268.         colore(colours.white)
  269.         term.setCursorPos(14, 13)
  270.         fineColore()
  271.         if tonumber(speseFisseImm) == "" or tonumber(speseFisseImm) == nil or
  272.             tonumber(speseFisseImm) < 0 then
  273.             term.setCursorPos(15, 13)
  274.             colore(colours.red)
  275.             term.write("Il dato immesso non corrisponde    ")
  276.             fineColore()
  277.             sleep(2)
  278.             term.setCursorPos(15, 13)
  279.             term.write("                                   ")
  280.             term.setCursorPos(15, 13)
  281.         else
  282.             term.setCursorPos(15, 13)
  283.             colore(colours.green)
  284.             term.write(speseFisseImm .. " IC")
  285.             sfImmAccettate = true
  286.         end
  287.     end
  288.  
  289.     -- Calcolo totale contatore immissione
  290.     immissioneTotale = immissioneTotale + math.floor(immissione / 1000000)
  291.     speseFisseImmTotali = speseFisseImmTotali + speseFisseImm
  292. end
  293.  
  294. -- MAIN LOOP
  295. -- DEL PROGRAMMA
  296.  
  297. -- Stampante
  298. lpt = peripheral.wrap(LPT)
  299.  
  300. sfondo(colors.black)
  301. clear()
  302.  
  303. -- Disegna la finestra per i dati del consumo
  304.  
  305. local dialogoAltroCont = false
  306. while dialogoAltroCont == false do
  307.  
  308.     finestraConsumo()
  309.  
  310.     -- Question box per un altro contatore
  311.     clear()
  312.     disegnaTitolo()
  313.     colore(colors.black)
  314.     sfondo(colors.yellow)
  315.     term.setCursorPos(14, 7)
  316.     term.write("  Consumo  ")
  317.     drawFilledBox(14, 8, 37, 14, colors.lightBlue)
  318.     term.setCursorPos(15, 9)
  319.     term.write("Inserire un altro")
  320.     term.setCursorPos(15, 10)
  321.     term.write("contatore consumo?")
  322.     colore(colors.black)
  323.     sfondo(colors.red)
  324.     term.setCursorPos(16, 13)
  325.     term.write("   No   ")
  326.     colore(colors.black)
  327.     sfondo(colors.green)
  328.     term.setCursorPos(28, 13)
  329.     term.write("   Si   ")
  330.     fineSfondo()
  331.     fineColore()
  332.  
  333.     local scelta = false
  334.     while scelta == false do
  335.         event, key, x, y = os.pullEvent("mouse_click")
  336.         if event == "mouse_click" and x > 16 and x < 24 and y == 13 then
  337.             scelta = true
  338.             dialogoAltroCont = true
  339.         elseif event == "mouse_click" and x > 28 and x < 36 and y == 13 then
  340.             scelta = true
  341.             dialogoAltroCont = false
  342.         end
  343.     end
  344. end
  345.  
  346. clear()
  347. disegnaTitolo()
  348. sleep(0.5)
  349.  
  350. -- Question box per il Doppio Contatore
  351. colore(colors.black)
  352. sfondo(colors.yellow)
  353. term.setCursorPos(14, 7)
  354. term.write("  Immissione  ")
  355. drawFilledBox(14, 8, 37, 14, colors.lightBlue)
  356. term.setCursorPos(15, 9)
  357. term.write("Il cliente possiede")
  358. term.setCursorPos(15, 10)
  359. term.write("il Doppio Contatore?")
  360. colore(colors.black)
  361. sfondo(colors.red)
  362. term.setCursorPos(16, 13)
  363. term.write("   No   ")
  364. colore(colors.black)
  365. sfondo(colors.green)
  366. term.setCursorPos(28, 13)
  367. term.write("   Si   ")
  368. fineColore()
  369. fineSfondo()
  370.  
  371. local dialogoDC = false
  372. while dialogoDC == false do
  373.     event, key, x, y = os.pullEvent("mouse_click")
  374.     if event == "mouse_click" and x > 16 and x < 24 and y == 13 then
  375.         utenteConDC = false
  376.         dialogoDC = true
  377.     elseif event == "mouse_click" and x > 28 and x < 36 and y == 13 then
  378.         utenteConDC = true
  379.         dialogoDC = true
  380.     end
  381. end
  382.  
  383. -- debug only
  384.  
  385. if utenteConDC == true then
  386.     print("ACC-M")
  387.     finDC = false
  388.     while finDC == false do
  389.  
  390.         finestraImmissione()
  391.  
  392.         clear()
  393.         disegnaTitolo()
  394.         sleep(0.5)
  395.  
  396.         -- Question box per il Doppio Contatore
  397.         colore(colors.black)
  398.         sfondo(colors.yellow)
  399.         term.setCursorPos(14, 7)
  400.         term.write("  Immissione  ")
  401.         drawFilledBox(14, 8, 37, 14, colors.lightBlue)
  402.         term.setCursorPos(15, 9)
  403.         term.write("Inserire un altro")
  404.         term.setCursorPos(15, 10)
  405.         term.write("contatore immissione?")
  406.         colore(colors.black)
  407.         sfondo(colors.red)
  408.         term.setCursorPos(16, 13)
  409.         term.write("   No   ")
  410.         colore(colors.black)
  411.         sfondo(colors.green)
  412.         term.setCursorPos(28, 13)
  413.         term.write("   Si   ")
  414.         local dialogoAltroDC = false
  415.         while dialogoAltroDC == false do
  416.             event, key, x, y = os.pullEvent("mouse_click")
  417.             if event == "mouse_click" and x > 16 and x < 24 and y == 13 then
  418.                 dialogoAltroDC = true
  419.                 finDC = true
  420.             elseif event == "mouse_click" and x > 28 and x < 36 and y == 13 then
  421.                 finDC = false
  422.                 dialogoAltroDC = true
  423.             end
  424.         end
  425.     end
  426. end
  427. fineSfondo()
  428. fineColore()
  429. clear()
  430. disegnaTitolo()
  431. term.setCursorPos(1, 3)
  432.  
  433. print("Riepilogo:\n")
  434.  
  435. -- Controllo variabili
  436.  
  437. if consumoTotale == nil or consumoTotale == "" then consumoTotale = 0 end
  438.  
  439. if immissioneTotale == nil or immissioneTotale == "" then immissioneTotale = 0 end
  440.  
  441. if speseFisseConsTotali == nil or speseFisseConsTotali == "" then
  442.     speseFisseConsTotali = 0
  443. end
  444.  
  445. if speseFisseImmTotali == nil or speseFisseImmTotali == "" then
  446.     speseFisseImmTotali = 0
  447. end
  448.  
  449. colore(colors.lightGray)
  450. print("Energia")
  451. fineColore()
  452. print(" Consumo       : " .. consumoTotale .. " IC")
  453. print(" Immissione    : " .. immissioneTotale .. " IC\n")
  454.  
  455. colore(colors.lightGray)
  456. term.setCursorPos(26, 5)
  457. print("Spese Fisse")
  458. fineColore()
  459. term.setCursorPos(27, 6)
  460. print(" S.F. consumo  : " .. speseFisseConsTotali .. " IC")
  461. term.setCursorPos(27, 7)
  462. print(" S.F. immi     : " .. speseFisseImmTotali .. " IC\n")
  463.  
  464. -- Calcoli
  465. subtEnergia = consumoTotale - immissioneTotale
  466. subtSF = speseFisseConsTotali + speseFisseImmTotali
  467. granTotale = subtEnergia + subtSF
  468.  
  469. colore(colors.lightGray)
  470. print("Subtotali")
  471. fineColore()
  472. print(" Subt. energia : " .. subtEnergia .. " IC")
  473. print(" Subt. S.F.    : " .. subtSF .. " IC\n")
  474.  
  475. colore(colors.yellow)
  476. print("Gran Totale    : " .. granTotale .. " IC")
  477.  
  478. term.setCursorPos(9, 17)
  479. sfondo(colors.red)
  480. colore(colors.black)
  481. term.write("   Annulla    ")
  482. term.setCursorPos(28, 17)
  483. sfondo(colors.green)
  484. colore(colors.black)
  485. term.write("   Conferma   ")
  486. fineColore()
  487. fineSfondo()
  488.  
  489. local scelta = false
  490. while scelta == false do
  491.     event, key, x, y = os.pullEvent("mouse_click")
  492.     if event == "mouse_click" and x > 9 and x < 23 and y == 17 then
  493.         scelta = true
  494.         continua = false
  495.     elseif event == "mouse_click" and x > 28 and x < 42 and y == 17 then
  496.         scelta = true
  497.         continua = true
  498.     end
  499. end
  500.  
  501. clear()
  502. if continua == true then
  503.     -- Recupera data
  504.     local phpdate = http.get("http://172.16.20.220/date.php")
  505.     local date = phpdate.readAll()
  506.     phpdate.close()
  507.  
  508.     -- Stampa
  509.     stampantePronta = false
  510.     while stampantePronta == false do
  511.         if lpt.getPaperLevel() == 0 then
  512.             clear()
  513.             disegnaTitolo()
  514.             disegnaAvviso()
  515.             fineColore()
  516.             fineSfondo()
  517.             term.setCursorPos(22, 9)
  518.             term.write("Inserire")
  519.             term.setCursorPos(22, 10)
  520.             term.write("carta")
  521.             term.setCursorPos(22, 11)
  522.             term.write("nella stampante")
  523.             term.setCursorPos(22, 13)
  524.             sfondo(colors.green)
  525.             term.write("   OK   ")
  526.             fineSfondo()
  527.             attesa = false
  528.             while attesa == false do
  529.                 event, key, x, y = os.pullEvent("mouse_click")
  530.                 if event == "mouse_click" and x > 22 and x < 31 and y == 13 then
  531.                     attesa = true
  532.                 end
  533.             end
  534.  
  535.         elseif lpt.getInkLevel() == 0 then
  536.             clear()
  537.             disegnaTitolo()
  538.             disegnaAvviso()
  539.             fineColore()
  540.             fineSfondo()
  541.             term.setCursorPos(22, 9)
  542.             term.write("Inserire")
  543.             term.setCursorPos(22, 10)
  544.             term.write("inchiostro")
  545.             term.setCursorPos(22, 11)
  546.             term.write("nella stampante")
  547.             term.setCursorPos(22, 13)
  548.             sfondo(colors.green)
  549.             term.write("   OK   ")
  550.             fineSfondo()
  551.             attesa = false
  552.             while attesa == false do
  553.                 event, key, x, y = os.pullEvent("mouse_click")
  554.                 if event == "mouse_click" and x > 22 and x < 31 and y == 13 then
  555.                     attesa = true
  556.                 end
  557.             end
  558.  
  559.         else
  560.             stampantePronta = true
  561.         end
  562.     end
  563.  
  564.     clear()
  565.     disegnaStampante()
  566.     fineColore()
  567.     fineSfondo()
  568.     term.setCursorPos(22, 9)
  569.     term.write("Stampa")
  570.     term.setCursorPos(22, 10)
  571.     term.write("bolletta")
  572.     term.setCursorPos(22, 11)
  573.     term.write("in corso")
  574.     term.setCursorPos(22, 13)
  575.     lpt.newPage()
  576.     lpt.setPageTitle("Bolletta eRGB")
  577.     lpt.write("Bolletta eRGB")
  578.     lpt.setCursorPos(1, 3)
  579.     lpt.write("Cliente: ")
  580.     lpt.setCursorPos(15, 3)
  581.     lpt.write(cliente)
  582.     lpt.setCursorPos(1, 4)
  583.     lpt.write("Bolletta del: ")
  584.     lpt.setCursorPos(15, 4)
  585.     lpt.setCursorPos(1, 6)
  586.     lpt.write("Cliente: ")
  587.     lpt.setCursorPos(16, 6)
  588.     lpt.write(UICnumber)
  589.     lpt.setCursorPos(1, 7)
  590.     lpt.write("Totale immissione:")
  591.     lpt.setCursorPos(1, 8)
  592.     lpt.write(energy)
  593.     lpt.setCursorPos(1, 10)
  594.     lpt.write("Totale immissione:")
  595.     lpt.setCursorPos(1, 11)
  596.     lpt.write(tassa)
  597.     lpt.setCursorPos(4, 11)
  598.     lpt.setCursorPos(1, 13)
  599.     lpt.write("Totale PAGATO:")
  600.     lpt.setCursorPos(1, 14)
  601.     lpt.write(totale)
  602.     lpt.setCursorPos(6, 14)
  603.     lpt.write("IC")
  604.     lpt.setCursorPos(1, 16)
  605.     lpt.write("Il denaro pagato e'")
  606.     lpt.setCursorPos(1, 17)
  607.     lpt.write("allegato a questa busta.")
  608.     lpt.setCursorPos(1, 18)
  609.     lpt.write("Grazie per aver scelto")
  610.     lpt.setCursorPos(1, 19)
  611.     lpt.write("eRGB!")
  612.     lpt.endPage()
  613.     sleep(2)
  614.  
  615.     -- tabella disco
  616.     local be = {}
  617.     be.UICnumber = cliente
  618.     be.date = date
  619.     be.totale = granTotale
  620.     bedisco = textutils.serialize(be)
  621.  
  622.     -- Messaggio inserire il disco
  623.     clear()
  624.     disegnaTitolo()
  625.     disegnaFloppy()
  626.     fineColore()
  627.     fineSfondo()
  628.     term.setCursorPos(22, 9)
  629.     term.write("Inserire il")
  630.     term.setCursorPos(22, 10)
  631.     term.write("Disco Bolletta Elettronica")
  632.     term.setCursorPos(22, 11)
  633.     term.write("nel lettore floppy")
  634.     event, latoFloppy = os.pullEvent("disk")
  635.     disk.setLabel(latoFloppy, "DBE")
  636.     local handle = assert(fs.open("disk/be", "w"), "TOC ERROR")
  637.     handle.write(bedisco)
  638.     handle.close()
  639.     clear()
  640.     print("Ritirare il disco")
  641.     disk.eject(latoFloppy)
  642.  
  643. end
Advertisement
Add Comment
Please, Sign In to add comment