tommy2805

programma fatture

Dec 5th, 2021 (edited)
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.94 KB | None | 0 0
  1. local button1 = "Nuova Fattura"
  2. local button2 = "Riepilogo Fatture"
  3. local button3 = "Stampa fattura"
  4. local button4 = "Inserisci da capo"
  5. local button5 = "Annulla operazione"
  6. local button6 = "Home"
  7. local button7 = "Cancella storico"
  8. local button8 = "<--"
  9. local button9 = "-->"
  10. local button10 = "Annulla"
  11. local button11 = "Conferma"
  12. local x, y = 15,8
  13. local x1, y1 = 15, 12
  14. local x2, y2 = 1,14
  15. local x3, y3 = 1,16
  16. local x4, y4 = 1,18
  17. local x5, y5 = 1,19
  18. local x6, y6 = 10,19
  19. local x7, y7 = 35,19
  20. local x8, y8 = 47,19
  21. local x9, y9 = 15,10
  22. local x10, y10 = 30,10
  23. local array = {}
  24. local printer = peripheral.wrap("right")
  25.  
  26. --menu functions
  27. function menu()
  28.     drawMenu()
  29.     --check if .Index file exist
  30.     if fs.exists(".Index")==false then
  31.         setData(0,".Index")
  32.     end
  33.     --check if .Storico file exist
  34.     if fs.exists(".Storico")==false then
  35.         count = #array
  36.         for i=0, count do array[i]=nil end
  37.         array[0]="0"
  38.         setData(array,".Storico")
  39.     end
  40.     while true do
  41.         local event, button, cx, cy = os.pullEvent()
  42.             if event == "mouse_click" then
  43.                 if cx>=x-3 and cx <= (x+button2:len()+3) and ((cy==y-1) or (cy==y) or (cy==y+1)) then newBill()
  44.                 else if cx>=x1-3 and cx<= (x1+button2:len()+3) and ((cy==y1-1) or (cy==y1) or (cy==y1+1)) and button==1 then showBillList(1) end
  45.             end
  46.         end
  47.         sleep(0.05)
  48.     end
  49. end
  50.  
  51. --draw the menu of the program
  52. function drawMenu()
  53.     term.clear()
  54.     term.setTextColor(colors.lightBlue)
  55.     --blue box for button
  56.     paintutils.drawLine(x-3, y-1, ((x+button2:len())+3), y-1, colors.blue)
  57.     paintutils.drawLine(x-3, y, ((x+button2:len())+3), y, colors.blue)
  58.     paintutils.drawLine(x-3, y+1, ((x+button2:len())+3), y+1, colors.blue)
  59.     term.setCursorPos(x,y)
  60.     term.write(button1)
  61.     --blue box for button
  62.     paintutils.drawLine(x1-3, y1-1, ((x1+button2:len())+3), y1-1, colors.blue)
  63.     paintutils.drawLine(x1-3, y1, ((x1+button2:len())+3), y1, colors.blue)
  64.     paintutils.drawLine(x1-3, y1+1, ((x1+button2:len())+3), y1+1, colors.blue)
  65.     term.setCursorPos(x1,y1)
  66.     term.write(button2)
  67.     term.setBackgroundColor(colors.black)
  68. end
  69.  
  70. --bill list functions
  71.  
  72. --clear the bill list(y/n buttons)
  73. function clearList()
  74.     term.clear()
  75.     term.setTextColor(colors.lightBlue)
  76.     term.setCursorPos(5,8)
  77.     term.write("Cancellare tutto lo storico delle fatture??")
  78.     term.setBackgroundColor(colors.blue)
  79.     term.setCursorPos(x9,y9)
  80.     term.write(button10)
  81.     term.setCursorPos(x10,y10)
  82.     term.write(button11)
  83.     term.setBackgroundColor(colors.black)
  84.     while true do
  85.         local event, button, cx, cy = os.pullEvent()
  86.         if event == "mouse_click" then
  87.             if cx>=x9 and cx <= (x9+button10:len()) and cy == y9  then menu() end
  88.             if cx>=x10 and cx <= (x10+button11:len()) and cy == y10  then
  89.                 fs.delete(".Index")
  90.                 fs.delete(".Storico")
  91.                 term.clear()
  92.                 term.setCursorPos(10,10)
  93.                 term.write("Cancellazione in corso attendere...")
  94.                 sleep(1)
  95.                 menu()
  96.             end
  97.         end
  98.     end
  99. end
  100.  
  101. --draw the buttons for the bill list page
  102. function drawBillListPage(page)
  103.     term.setTextColor(colors.lightBlue)
  104.     paintutils.drawLine(0, y5, 60, y5, colors.blue)
  105.     term.setCursorPos(x5,y5)
  106.     term.write(button6)
  107.     term.setCursorPos(x6,y6)
  108.     term.write(button7)
  109.     term.setCursorPos(x7,y7)
  110.     term.write(button8)
  111.     tmp = page*6
  112.     if page~=1 then term.write(" "..(tmp-5).."-"..tmp)
  113.     else term.write(" "..(page).."-"..tmp) end
  114.     term.setCursorPos(x8,y8)
  115.     term.write(button9)
  116.     term.setBackgroundColor(colors.black)
  117. end
  118.  
  119. --print bill(max 6 for page)
  120. function showBillList(page)
  121.     term.clear()
  122.     array = getData(".Storico")
  123.     i = page*12
  124.     k = (((page-1)*12)+1)
  125.     f = 0
  126.     row = 1
  127.     for k=k,i,1 do
  128.         term.setCursorPos(1,row)
  129.         term.write(array[k])
  130.         row = row+1
  131.         f = f+1
  132.         if f==2 then
  133.             row = row+1
  134.             f=0
  135.         end
  136.     end
  137.     drawBillListPage(page)
  138.     while true do
  139.         local event, button, cx, cy = os.pullEvent()
  140.         if event == "mouse_click" then
  141.             if cx>=x5 and cx <= (x5+button6:len()) and cy == y5  then menu()
  142.             else if cx>=x6 and cx <= (x6+button7:len()) and cy == y6  then
  143.                 clearList()
  144.             else if cx>=x7 and cx <= (x7+button8:len()) and cy == y7  then
  145.                 if page-1>0 then
  146.                     showBillList(page-1)
  147.                 end
  148.             else if cx>=x8 and cx <= (x8+button8:len()) and cy == y8  then
  149.                 if (page*12)<#array then
  150.                     showBillList(page+1)
  151.                 end
  152.             end
  153.         end
  154.     end
  155.         sleep(0.05)
  156.     end
  157. end
  158. end
  159. end
  160.  
  161. --add a new bill to the bill array and update the bill list file
  162. function addToIndex(bill)
  163.     i = getData(".Index")
  164.     array = getData(".Storico")
  165.     i = i+1
  166.     array[i] = bill
  167.     setData(array,".storico")
  168.     setData(i,".Index")
  169. end
  170.  
  171. --set a data on a file
  172. function setData(data,name)
  173.     local file = fs.open(name,"w")
  174.     file.write(textutils.serialize(data))
  175.     file.close()
  176. end
  177.  
  178. --get data from a file
  179. function getData(name)
  180.     local file = fs.open(name,"r")
  181.     local data = file.readAll()
  182.     file.close()
  183.     return textutils.unserialize(data)
  184. end
  185.  
  186.  
  187. --new bill functions
  188. --get parameter of new bill
  189. function newBill()
  190.     term.clear()
  191.     term.setCursorPos(1,1)
  192.     term.setTextColor(colors.lightBlue)
  193.     term.write("Inserire il nome del destinatario: ")
  194.     term.setCursorPos(1,2)
  195.     dest = read()
  196.     term.setCursorPos(1,4)
  197.     term.write("Inserire il nome del prodotto: ")
  198.     term.setCursorPos(1,5)
  199.     prod = read()
  200.     term.setCursorPos(1,7)
  201.     term.write("Inserire la quantita del prodotto: ")
  202.     tmpbool = false
  203.     t = 0
  204.     while tmpbool==false do
  205.         term.setCursorPos(1,8)
  206.         term.clearLine()
  207.         if(t ~= 0) then
  208.             term.write("Deve essere un numero")
  209.             sleep(0.5)
  210.             term.clearLine()
  211.             term.setCursorPos(1,8)
  212.         end
  213.         t=t+1
  214.         qta = tonumber(read())
  215.         if qta~=nil then tmpbool=true end
  216.     end
  217.     term.setCursorPos(1,10)
  218.     term.write("Inserire il prezzo al pezzo del prodotto: ")
  219.     tmpbool = false
  220.     t = 0
  221.     while tmpbool==false do
  222.         term.setCursorPos(1,11)
  223.         term.clearLine()
  224.         if(t ~= 0) then
  225.             term.write("Deve essere un numero")
  226.             sleep(0.5)
  227.             term.clearLine()
  228.             term.setCursorPos(1,11)
  229.         end
  230.         t=t+1
  231.         pPz = tonumber(read())
  232.         if pPz~=nil then tmpbool=true end
  233.     end
  234.     tot = qta*pPz
  235.     billPreview()
  236. end
  237.  
  238. --show a preview before print(with options buttons)
  239. function billPreview()
  240.     term.clear()
  241.     term.setCursorPos(10,1)
  242.     term.write("Anteprima Fattura")
  243.     term.setCursorPos(1,2)
  244.     term.write("Destinatario: ")
  245.     term.setCursorPos(1,3)
  246.     term.write(dest)
  247.     term.setCursorPos(1,4)
  248.     term.write("Prodotto: ")   
  249.     term.setCursorPos(1,5)
  250.     term.write(prod)
  251.     term.setCursorPos(1,6)
  252.     term.write("Quantita: ")   
  253.     term.setCursorPos(1,7)
  254.     term.write(qta)
  255.     term.setCursorPos(1,8)
  256.     term.write("Prezzo al pz: ")   
  257.     term.setCursorPos(1,9)
  258.     term.write(pPz)
  259.     term.setCursorPos(1,10)
  260.     term.write("---------------------------------------------------")
  261.     term.setCursorPos(1,11)
  262.     term.write("Totale: ")
  263.     term.setCursorPos(1,12)
  264.     term.write(tot)
  265.     term.setCursorPos(1,13)
  266.     term.write("---------------------------------------------------")
  267.     drawbutton()
  268.     while true do
  269.         local event, button, cx, cy = os.pullEvent()
  270.             if event == "mouse_click" then
  271.                 --print and add the bill to the list
  272.                 if cx>=x2 and cx <= button2:len() and cy == y2  then
  273.                     prodt = string.sub(prod,0,15)
  274.                     tmp = "dest:"..dest
  275.                     addToIndex(tmp)
  276.                     tmp ="Prod:"..prodt.." qta:"..qta.." tot:"..tot
  277.                     addToIndex(tmp)
  278.                     print()
  279.                 --restart a new bill
  280.                 else if cx>=x3 and cx<= (x3+button3:len()) and cy == y3 and button==1 then newBill()
  281.                 --return to menu without print
  282.                 else if cx>=x4 and cx<= (x4+button4:len()) and cy == y4 and button==1 then menu() end
  283.             end
  284.         end
  285.         sleep(0.05)
  286.     end
  287. end
  288. end
  289.  
  290. --print functions
  291. function print()
  292.     --check if ink level is ok
  293.     while printer.getInkLevel() == 0 do
  294.         term.clear()
  295.         term.setCursorPos(1,1)
  296.         term.write("Errore! Inserire l'inchiostro nella stampante")
  297.         sleep(0.10)
  298.     end
  299.     --check if paper level is ok
  300.     while printer.getPaperLevel() == 0 do
  301.         term.clear()
  302.         term.setCursorPos(1,1)
  303.         term.write("Errore! Inserire la carta nella stampante")
  304.         sleep(0.10)
  305.     end
  306.     --set the new page for print
  307.     term.clear()
  308.     printer.newPage()
  309.     term.clear()
  310.     printer.setCursorPos(10,1)
  311.     printer.write("Fattura")
  312.     printer.setCursorPos(1,2)
  313.     printer.write("Destinatario: ")
  314.     printer.setCursorPos(1,3)
  315.     printer.write(dest)
  316.     printer.setCursorPos(1,5)
  317.     printer.write("Prodotto: ")
  318.     printer.setCursorPos(1,6)
  319.     printer.write(prod)
  320.     printer.setCursorPos(1,8)
  321.     printer.write("Quantita: ")
  322.     printer.setCursorPos(1,9)
  323.     printer.write(qta)
  324.     printer.setCursorPos(1,11)
  325.     printer.write("Prezzo al pz: ")
  326.     printer.setCursorPos(1,12)
  327.     printer.write(pPz)
  328.     printer.setCursorPos(1,14)
  329.     printer.write("---------------------------------------------------")
  330.     printer.setCursorPos(1,15)
  331.     printer.write("Totale: ")
  332.     printer.setCursorPos(1,16)
  333.     printer.write(tot)
  334.     printer.endPage()
  335.     menu()
  336. end
  337.  
  338. --draw the bill option buttons
  339. function drawbutton()
  340.     term.setTextColor(colors.lightBlue)
  341.     term.setBackgroundColor(colors.blue)
  342.     term.setCursorPos(x2,y2)
  343.     term.write(button3)
  344.     term.setCursorPos(x3,y3)
  345.     term.write(button4)
  346.     term.setCursorPos(x4,y4)
  347.     term.write(button5)
  348.     term.setBackgroundColor(colors.black)
  349. end
  350. menu()
Add Comment
Please, Sign In to add comment