commit 95dddb0555860da693f11a08992bf10ddf753102
parent 2fa1012114104ff5744454e5c1f610bc45b68957
Author: rob <rob@tarina.org>
Date:   Thu,  9 Mar 2023 22:42:12 +0200
mlinvoice basics implementerade
Diffstat:
5 files changed, 95 insertions(+), 36 deletions(-)
diff --git a/public_html/html/admin.html b/public_html/html/admin.html
@@ -10,6 +10,8 @@ $def with (anslagstavla, saysomething, bokningar, doordebug, str, log, getDorrko
     <br>
     $if doordebug.doorstate == 'False':
         Dörren är stängd. <br>
+    $if doordebug.doorstate == 'True':
+        Dörren är öppen. <br>
     $doordebug.datum.strftime('%d-%m-%y %H:%M')
     <br>
     Senaste i dörrloggen<br>
@@ -36,7 +38,7 @@ $def with (anslagstavla, saysomething, bokningar, doordebug, str, log, getDorrko
         <a href="/admin?approve=$i.id" method="post">Godkänn</a> | 
         <a href="/boka?id=$i.bokningshash">Ändra</a> |
         <a href="/admin?sendrules=$i.id">Skicka regler</a> |
-        <a href="/admin?fakturera=$i.id">Fakturera</a> | 
+        <a href="/admin?fakturera=$i.id">Skicka faktura</a> | 
         <a href="/admin?doorcode=$i.id">Skicka dörrkod</a> | 
         <a href="/admin?remove=$i.id">Radera</a>
         <br>
diff --git a/public_html/html/bilder.html b/public_html/html/bilder.html
@@ -2,6 +2,7 @@ $def with (bilder, rights, namn, totbilder, offset, bildpersida, search, db, str
 <div id="bilder">
 $ displayname = db.query("SELECT displayname FROM bildadmin WHERE name='"+namn+"';")[0]
 <p>inloggad som <b><a href="/tuning">$displayname.displayname</a></b> ❃ | <a href="/logout">Logga ut</a> 🗝</p>
+<a href="/"><--- tibaks till början</a>
 <h1>Byyns eji fotoalbum</h1>
 <h4><a href="/upload">lägg opp ny bild</a> 💾 <a href="/senaste">senast häändi</a> 🔥 <a href="/om">Om</a> 🤔</h4>
 $if totbilder == 0:
diff --git a/public_html/html/fakturor.html b/public_html/html/fakturor.html
@@ -1,4 +1,4 @@
-$def with (db,getkund,getmaksuja,round)
+$def with (alla_fakturor,getkund,getmaksuja,round)
 
 <br>
 <h1>Bygdis.fi</h1>
@@ -8,12 +8,21 @@ $def with (db,getkund,getmaksuja,round)
 <br>
 <br>
     <br>
-$for i in db.select('mlinvoice_invoice'):
-    $getkund(i.company_id)
-    $i.invoice_date
-    $i.invoice_no
-    $i.due_date
-    $for p in getmaksuja(i.id):
-        $round(p.price,2)
-    <br>
+$for i in alla_fakturor:
+    $if i.deleted == 0:
+        <br>
+        $if i.company_id:
+            $getkund(i.company_id)
+            <br>
+            $i.invoice_date 
+            <br>
+        nummer: $i.invoice_no
+        <br>
+        sista betalningsdag: $i.due_date
+        <br>
+        $for p in getmaksuja(i.id):
+            $if p.deleted != 1:
+                $p.description $round(p.price,2)
+        <a href='/fakturor?faktura=$i.id'>öppna pdf</a>
+        <br>
  
diff --git a/public_html/html/register.html b/public_html/html/register.html
@@ -1,21 +1,21 @@
 $def with (loginform, formfail, totusers)
 <div id="container">
-    <div id="default">
-    <br>
-    $if totusers > 1:
-        <h2>Registrera dej på bygdis.fi</h2>
-        <h2>Rekisteryöidy bygdispistefiille</h2>
-        <h2>Register please!</h2>
-        <p>Do it!</p>
-    $else:
-        <h2>Registrera superadmin</h2>
-    <div id="addevent">
-    <form method="POST">
-    $:loginform.render()
-    <h2>$formfail</h2>
-    </form>
-    </div>
-    <a href="javascript:history.back()"><--- tibaks</a>
-    </div>
-    </div>
+<div id="default">
+<img src="/static/bilder/thumb/0a91bae6e0958e1bffe2ae3e9f0d89a6.jpeg" style="border-radius:50%"><br>
+<br>
+$if totusers > 1:
+    <h2>Registrera dej på bygdis.fi</h2>
+    <h2>Rekisteryöidy bygdispistefiille</h2>
+    <h2>Register please!</h2>
+$else:
+    <h2>Registrera superadmin</h2>
+<div id="addevent">
+<form method="POST">
+$:loginform.render()
+<h2>$formfail</h2>
+</form>
+</div>
+<a href="javascript:history.back()"><--- tibaks</a>
+</div>
+</div>
 </div>
diff --git a/server.py b/server.py
@@ -14,6 +14,8 @@ import hashlib
 import random
 import bcrypt
 import sys
+from bs4 import BeautifulSoup
+import requests
 basedir = os.path.dirname(os.path.realpath(__file__))
 sys.path.append(basedir)
 import settings
@@ -52,11 +54,15 @@ urls = (
     "/nope", "nope"
 )
 
+#from settings.py
 postadmin = settings.postadmin
 postadmin_signature = settings.postadmin_signature
 mlinvoice_db = settings.mlinvoice_db
 mlinvoice_db_user = settings.mlinvoice_db_user
 mlinvoice_db_pass = settings.mlinvoice_db_pass
+mlinvoice_login=settings.mlinvoice_login
+mlinvoice_passwd=settings.mlinvoice_passwd
+
 render = web.template.render(basedir + "/public_html/html/", base="base")
 render2 = web.template.render(basedir + "/public_html/html/", base="base2")
 render3 = web.template.render(basedir + "/public_html/html/", base="base3")
@@ -343,7 +349,8 @@ class admin():
                 db.update('bokningar', where='id="'+i.sendrules+'"', approved="")
                 raise web.seeother('/admin')
             if i.fakturera != None:
-                raise web.seeother('/admin?kakka')
+                db2.insert('mlinvoice_invoice', company_id=2, invoice_date=time.time())
+                raise web.seeother('/admin')
             if i.doorcode != None:
                 raise web.seeother('/admin?kakka')
             if i.remove != None:
@@ -437,13 +444,53 @@ class tack():
 class fakturor():
     def GET(self):
         if session.login == 5:
-            #log = getDorrlog(0)
-            i = web.input(radera=None,uppdatera=None,fakturera=None)
-            #if i.radera != None:
-            #    db.delete('fakturor', where='id="'+i.radera+'"')
-            #    raise web.seeother('/fakturor')
-            #print(dorrkoder)
-            return render.fakturor(db2,getkund,getmaksuja,round)
+            viewfile = ''
+            i = web.input(radera=None,uppdatera=None,fakturera=None,add=None,faktura=None,yes=None)
+            #
+            # MLINVOICE Hack COpy Faktura och Lägg till kund och vara i fakturan
+            #
+            #curl -b -X https://bygdis.fi/static/mlinvoice/login.php -i -b ~/cookies.txt -c ~/cookies.txt -d 'backlink=0&csrf=pcCFor0ic9KfoQ4jWQ3s&login=???&passwd=????&logon=Kirjaudu'
+            #
+            #get session and login to mlinvoice with csrf lol
+            url='https://bygdis.fi/static/mlinvoice/login.php'
+            r = ['curl','-b','X', url , '-i', '-b', basedir+'/sessions/cookies.txt', '-c',basedir+'/sessions/cookies.txt', '-d', 'backlink=0&csrf=&login='+mlinvoice_login+'&passwd='+mlinvoice_passwd+'&logon=Kirjaudu']
+            view_csrf = subprocess.check_output(r)
+            view = view_csrf.decode()
+            soup = BeautifulSoup(view, "html.parser")
+            csrf = soup.find(id="csrf")['value']
+            login_mlinvoice = ['curl','-b','X', url , '-i', '-b', basedir+'/sessions/cookies.txt', '-c',basedir+'/sessions/cookies.txt', '-d', 'backlink=0&csrf='+csrf+'&login='+mlinvoice_login+'&passwd='+mlinvoice_passwd+'&logon=Kirjaudu']
+            subprocess.check_output(login_mlinvoice)
+            #yes
+            #
+            if i.add == 'faktura':
+                url='https://bygdis.fi/static/mlinvoice/copy_invoice.php'
+                copy_mlinvoice = ['curl','-b','X', url , '-i', '-b', basedir+'/sessions/cookies.txt', '-c',basedir+'/sessions/cookies.txt', '-d', 'id=40']
+                viewfile = subprocess.check_output(copy_mlinvoice)
+                print(viewfile)
+                #
+                # HACK TO GET LATEST INVOICE NR
+                #
+                senaste = db2.query("SELECT * FROM mlinvoice_invoice ORDER BY id DESC LIMIT 1;")[0]
+                print(senaste)
+                url='https://bygdis.fi/static/mlinvoice/json.php'
+                get_nr_mlinvoice = ['curl','-b','X', url , '-b', basedir+'/sessions/cookies.txt', '-c',basedir+'/sessions/cookies.txt', '-d', 'func=get_invoice_defaults']
+                next_invoice=subprocess.check_output(get_nr_mlinvoice)
+                #print(next_invoice['invoice_no']
+                invoice = json.loads(next_invoice.decode())
+                #print(invoice)
+                #print(invoice['invoice_no'])
+                db2.update('mlinvoice_invoice',where='id="'+str(senaste.id)+'"', invoice_no=invoice['invoice_no'], ref_number=invoice['ref_no'])
+                raise web.seeother('/fakturor?yes=lyckades')
+            if i.faktura != None:
+                url='https://bygdis.fi/static/mlinvoice/invoice.php'
+                open_mlinvoice = ['curl','-b','X', url , '-i', '-b', basedir+'/sessions/cookies.txt', '-c',basedir+'/sessions/cookies.txt', '-d', 'id='+i.faktura+'&template=15']
+                pdf_file = subprocess.check_output(open_mlinvoice)
+                pdfhash = hashlib.md5(str(random.getrandbits(256)).encode('utf-8')).hexdigest()
+                with open(basedir+'/public_html/static/pdf/'+pdfhash+'.pdf','wb') as binary_file:
+                    binary_file.write(pdf_file)
+                raise web.seeother('/static/pdf/'+pdfhash+'.pdf')
+            alla_fakturor = db2.query("SELECT * FROM mlinvoice_invoice ORDER BY id DESC;")
+            return render.fakturor(alla_fakturor,getkund,getmaksuja,round)
         else:
             web.seeother('/')