Lettura della fattura elettronica XML con PHP

Visualizzare e formattare in HTML una fattura elettronica con del codice PHP concettualmente è semplice, ma scrivere un apposito programma che fa un parsing del codice XML, e quindi disegnare un layout HTML in cui inserire i dati ottenuti, è uno spreco di tempo, se lo si fa solo per “guardare” la fattura senza doverla ulteriormente processare in modo automatizzato.

Per questo ringraziamo AssoSoftware che fornisce liberamente il suo foglio di stile XSL (in realtà ne esiste anche uno ufficiale rilasciato da Sogei, ma… vabè non devo aggiungere niente su Sogei e quindi se volete provatelo, e mi saprete dire nei commenti quale vi piace di più), il quale applicato all’XML originale seguendo questa guida restituisce una gradevole formattazione tabellare completa di annotazioni.

D’accordo lo ammetto questo articolo dai risultati di ricerca sembra essere fuorviante, perché io stesso cercando una routine che leggesse l’XML per restituire delle variabili in PHP, ho trovato il mio articolo pensando “vuoi vedere che l’ho già scritta io questa routine e me ne sono dimenticato?” ed invece no… quindi non essendoci altre risorse realmente utilizzabili per il PHP, credo proprio che ora mi ci metterò io a scrivere la routine…

Ho rimediato e l’ho appena scritta.

Questo è uno snippet del mio reale codice, per chi bazzica di PHP è comprensibile. E’ incompleto siccome mi serviva solo a recepire i dati che vanno inseriti nel registro acquisti, ma la logica dietro è abbastanza chiara per poterlo estendere a vostro piacimento.

$xml = simplexml_load_string($source);

$supplier = $xml->FatturaElettronicaHeader->CedentePrestatore->DatiAnagrafici->Anagrafica->Denominazione;
if (!$supplier) {
	$supplier = $xml->FatturaElettronicaHeader->CedentePrestatore->DatiAnagrafici->Anagrafica->Nome . " " . $xml->FatturaElettronicaHeader->CedentePrestatore->DatiAnagrafici->Anagrafica->Cognome;
}
$supplierpiva = $xml->FatturaElettronicaHeader->CedentePrestatore->DatiAnagrafici->IdFiscaleIVA->IdCodice;
$suppliervia = $xml->FatturaElettronicaHeader->CedentePrestatore->Sede->Indirizzo;
$suppliercivico = $xml->FatturaElettronicaHeader->CedentePrestatore->Sede->NumeroCivico;
$suppliercap = $xml->FatturaElettronicaHeader->CedentePrestatore->Sede->CAP;
$suppliercomune = $xml->FatturaElettronicaHeader->CedentePrestatore->Sede->Comune;
$supplierprovincia = $xml->FatturaElettronicaHeader->CedentePrestatore->Sede->Provincia;

$invoicedate = $xml->FatturaElettronicaBody->DatiGenerali->DatiGeneraliDocumento->Data;
$invoiceno = $xml->FatturaElettronicaBody->DatiGenerali->DatiGeneraliDocumento->Numero;
$invoicewhat = $xml->FatturaElettronicaBody->DatiGenerali->DatiGeneraliDocumento->Causale[0];
if (!$invoicewhat) $invoicewhat=$xml->FatturaElettronicaBody->DatiBeniServizi->DettaglioLinee[0]->Descrizione;


foreach ($xml->FatturaElettronicaBody->DatiBeniServizi->DatiRiepilogo as $riepilogo) {
	switch ($riepilogo->AliquotaIVA) {
		case "22.00":
			$imponibileord = $riepilogo->ImponibileImporto;
			$impostaord = $riepilogo->Imposta;
			break;
		case "10.00":
			$imponibilerid = $riepilogo->ImponibileImporto;
			$impostarid = $riepilogo->Imposta;
			break;
		case "4.00":
			$imponibilemin = $riepilogo->ImponibileImporto;
			$impostamin = $riepilogo->Imposta;
			break;
		case "0.00":
			$esente = $riepilogo->ImponibileImporto;
			break;
	}
}

Hide disable exclude specific mail account in thunderbird global inbox

I use Thunderbird’s unified inbox as it is a very convenient way to manage multiple accounts. Yet there was a management account where the inbox was a huge pile of messages that needed to stay there but that I didn’t want to show in there unless I specifically clicked on that account.

No other guide on the first page of google results gave info on it, but here’s how you do it.

Right-click on the Inbox master node under which all the accounts’ inboxes are listed, then click on Properties, then Choose the selected folders to search, and un-check the Inbox of the account you want to hide. Then click on Update.

There you go.

Cannot downgrade Xiaomi Router 3 firmware to development version

I bought a Mi Router 3 for the express purpose of installing Padavan firmware on it, but lo and behold, I could not for the life of me install the BIN file for the 2.11.20 development version of the firmware (see here and here) as, when choosing the BIN file from the browse file dialog of the router web interface, the popup just went numb, no action, no buttons, no upload activity. Tried many times it with Firefox Chrome and even Internet Explorer AND Edge, NOTHING!

Then, I found in a forum (can’t remember for the life of me where) that there is an alternative method do to it:

  1. Put the BIN file in an empty USB pendrive
  2. Turn off the router (unplug the power)
  3. Place the USB stick in the router
  4. Press the reset button on the back of the router, and while keeping it pressed plug in the power, and STILL keep the reset switch pressed
  5. Stay there like this for a while contemplating on the meaning of existence
  6. In my case I noticed that after a while (10-15 secconds maybe?) the front led was flashing in a different way than when it just booted up, so I figured I could finally leave the reset switch be
  7. I connected to 192.168.31.1 after a while when the front led became static blue and, go figure, the firmware had downgraded

lftp and source: Is a directory error

I was running lftp with the -f switch to launch the usual mirror command, working fine with a few ftp servers at first, but when I added another sync with a pure-ftpd server something stopped working as intended: the message

source: Is a directory

appeared and nothing more happened, only after I pressed Enter the script went on with the execution.

What helped me was this discussion.

so I changed my script, and instead of having

open host
user username password
mirror blahblah

I used

open -u username,password host
mirror blahblah

and everything was magically fine!

Why? NO IDEA. Who cares. Works now.

Windows 10 bluetooth not working on Asus UX51VZ and Centrino Advanced-N 6235

Long story short, my bluetooth mouse, a Logitech Mx Master 2S, disconnected out of the blue, first times I just had to turn off and on bluetooth on windows, once the bluetooth device was automatically disabled because “it caused problems” and I had to restart, finally it stopped working altogether, always telling me “try connecting your device again”. I tested it with every other bluetooth device I had handy, to no avail.

Wasted maybe half an hour, on my almost nonexisting free time schedule (AAARGH)

So finally I had the nice thought of checking, from device manager, if there were updated drivers for the chipset responsible for wireless (WiFI and Bluetooth), a Centrino Advanced-N 6235. I updated it since it automatically downloaded new drivers, restarted as I was asked, and even if wifi kept being shitty, the bluetooth mouse finally connected, as did my android phone.

Change user of transmission-daemon under Debian and Raspbian

UPDATE: Cristian commented adding a nice solution (which I didn’t personally test though, so it’s on you):

  1.  run chmod 775 on the download folder, with -R option (recursive on subfolders):
    sudo chmod -R ug+rw folderName
  2.  add your own user (the one you need to be able to access the downloaded files) to debian-transmission group, or any other group that the transmission daemon belongs to:
    sudo usermod -a -G groupName userName

And that would be all.

Though, after some tinkering that occurred as of june 2019, I found that my original, following, solution has a -probably- nicer “feature” for someone: the config folder of transmission is saved in your own home folder, instead of /var/lib/transmission-daemon/blahblah, in my case this saved some waste of time.

This is the original article with the original solution.

I have a raspi3 running transmission daemon downloading to an external USB drive shared via Samba. I don’t want to keep using debian-transmission user with the daemon since just switching to my user account having the access rights to the external USB is much simpler.

I had it already nice and running before, but updating the daemon with apt-get messed everything (and lost all the running torrents as well) so I had to rediscover the procedure once again, and for posterity (and myself for future occasions) I’m writing it down here.

By the way all the missing torrents appeared again after I solved the issue.

So, here are the things you need to do:

  1. run sudo service transmission-daemon stop just in case
  2. edit /etc/init.d/transmission-daemon to have USER=username
  3. edit /lib/systemd/system/transmission-daemon.service to the same effect
  4. run sudo chown -R user:user /etc/transmission-daemon/
  5. run sudo chown -R user:user /var/lib/transmission-daemon/

Done.

 

According to a comment of this article, this might not work for you, so instead you might want to follow this guide instead:

https://askubuntu.com/questions/261252/how-do-i-change-the-user-transmission-runs-under/544185#544185

 

Il Mio Pos di Setefi, Move & Pay Business che non se ne può più

Nel 2014 mi sono arreso al regalo alle banche all’obbligo di legge di attivare un POS per le attività commerciali, e l’allora offerta di Banca Intesa / Intesa Sanpaolo era la più conveniente, a 0.7% di commissioni bancomat, e 1.4% per carta di credito, con canone mensile di 2€, contro alternative “free” come SumUp e compagnia bella, che ricaricano una strepitosa percentuale del 2.8% su tutte le transazioni (veramente folle, ma non che le prime non siano comunque un latrocinio tutto considerato).

Da allora l’applicazione Android che si collega via Bluetooth al dispositivo iCMP Ingenico è stata aggiornata diverse volte ed è diventata esteticamente più gradevole, ma anche io ho aggiornato più volte i miei telefoni, e soprattutto di recente le mancanze dell’applicazione sono diventate debilitanti.

L’accoppiamento via bluetooth spesso diventa impossibile: se non funziona subito dopo la procedura di inserimento credenziali e pairing da Ingenico, allora è inutile premere RETRY/RIPROVA, disinstallare, cancellare i dati dell’applicazione, riavviare telefono e dispositivo… non andrà mai più.

Unica soluzione: Full Wipe/Hard reset/Rpristino impostazioni di fabbrica come lo si vuol chiamare.

Ma anche qui, attenzione! Appena configurato l’account google sul telefono così resettato, non si può mettere mano a titanium backup per ripristinare le applicazioni mancanti, perché così facendo Move&Pay riprenderà a dare l’errore di connessione fallita in fase di configurazione, e sarà tutto da ripetere. Bisogna per prima cosa scaricare e installare dal Play Store l’applicazione Il Mio POS (MAI ripristinarla da Titanium) e forse si sarà fortunati. Ma non basta, perché l’ultima volta che ho fatto questa tiritera (ORE per ripristinare tutto a puntino come voglio) comunque dopo alcuni giorni ha smesso misteriosamente di accoppiarsi via bluetooth, e tentando di riconfigurare l’applicazione è tornato l’errore.

A suo tempo, ingenuamente, pensavo fosse una defaillance del dispositivo Ingenico, e chiamai il servizio clienti per farmelo sostituire. Dopo tutte le domande di rito, mi venne detto “il suo telefono non è tra quelli supportati”, ma mi volete prendere in giro? Il bluetooth del mio telefono è diverso da quello dei modelli supportati e dal protocollo bluetooth standard universalmente riconosciuto? O è il vostro software che è una ciofeca?

Stesso destino evidentemente lo condividono le altre applicazioni simili: curiosando sul Play Store non ce ne è una tra quelle che si associano a POS bluetooth che abbia valutazione media superiore a 4 stelle. Addirittura il destino peggiore tocca alla versione per iPhone, che ha una media di votazioni sull’AppStore se ricordo bene di 2 stelle o meno. Ed ho potuto sperimentare io stesso: accoppiato e configurata l’applicazione, è andata a buon fine solo la prima transazione di prova verso me stesso, di 1€, perché alle successive, IMPOSSIBILE ripristinare la connessione. Stesso errore riferito dagli altri utenti.
….forse il mio muletto su iPhone 5 è uno dei modelli non supportati?

fix Android gallery timestamp from video image filename

If you copied the files from another phone, you will notice the new gallery will have all files to the same date of the copy operation.

Install BusyBox, and from terminal or better yet JuiceSSH onto local device, go to /sdcard/DCIM/Camera and do

for i in VID*.mp4; do busybox touch -t ${i:4:8}${i:13:4}.${i:17:2} $i; done;

for i in IMG*.jpg; do busybox touch -t ${i:4:8}${i:13:4}.${i:17:2} $i; done;

Supposing the filename is in the format

IMG_YYYYMMDD_HHMMSS.jpg VID_YYYYMMDD_HHMMSS.mp4

 

Raspberry Pi 3 vs Cubieboard 2 Samba NAS performance

I’ve been using a Cubieboard overclocked to 1.3GHz as my home little NAS server since a few years ago, and just recently I bought a Raspberry Pi 3 thinking it would have been surely faster.

I was especially looking to get better Samba performance.

I have additional ntfs-3g and encryption layers other than Samba, but both boards had their version of Debian installed, with the same packages and same configurations, so the starting configuration was basically the same.

Both were -alternatively- attached to the same Toshiba Canvio 3TB USB3 disk (working in USB2 mode on both boards), and on two adiacent ports on my TP-Link ADSL router.

I overclocked my Raspi3 to 1.5GHz, with sdram_freq set to 500.

For my test, both were alternatively running off the same AC powered USB adapter, under the same wattmeter, each with his own USB power cord.

Well, it was an overall disappointing experience.

Raspberry Pi 3 @ 1.5GHz

  • Download speed via Samba: almost 7MB/s
  • CPU temperature: about 42°C
  • System load: ~2
  • Power draw: 2.6W, with short spikes to 2.7W

Cubieboard 2 @ 1.3GHz

  • Download speed via Samba: almost 7MB/s in average, oscillating between 6.3 and 7.5MB/s
  • CPU temperature: about 42°C
  • System load: ~2
  • Power draw: average 3.4W, ranging from 3.3 to 3.5W

So, the only area where raspi3 is superior to cubie2 is the power draw, otherwise I would have expected way better performance.

Interesting to note, the transfer speed via samba was the same on the raspi3 at the default frequency of 1.2GHz (no benefit from overclocking whatsoever, if not maybe increased oscillations in speed with the same average value), while cubie2 seriously improved after I overclocked back then.

This might indicate a saturation of the USB bus, where the ethernet adapter resides in the raspberry pi 3, which might be the case since the system load of 2 means the 4 cores of the raspberry pi 3 are loaded half as much as the 2 cores on the cubieboard 2, so they could be waiting for the bus to free itself.

Investigating further, I ran

vmstat 3

on both the boards during the transfer… and averages were as follows:

  • Cubieboard 2: user 7%, system 52%, idle 39%, waiting 2%
  • Raspberry Pi 3: user 2%, system 28%, idle 55%, waiting 15%

I think it’s quite clear at this point that while the cubieboard 2 is crunching during that time (the idle should be due to one of the two cores not being optimized), the raspberry pi 3 is waiting on the bus to free itself before sending in other data (the higher idle also must be due to the 4 cores not being all used for the task, mostly 2 of which just don’t do anything at all).

I spent about half to buy the raspberry pi 3 than what I paid for the cubieboard 2 back then, but after so much time it’s not surprising; what is surprising is that the everyday performance appears to be the same 🙁

At this point I will probably keep the cubieboard 2, because it’s already configured as I want it (it’s got more amenities to it than just the NAS server based on Samba) and the different power draw would save me only about 2€ per year on the raspberry pi 3, so no point in upgrading.

UPDATE:

It’s been a couple months since i wrote this article, and for the last month, maybe more, I’ve been actually using the raspi3 as server (and have sold the cubie2 which wasn’t useful anymore).

I noticed that, on the long run, the NAS transfer speed didn’t keep up on the cubieboard after the uptime went, well, up 🙂 It got down to about 2MB/s, with spikes of 5MB/s, maybe less, but never quite reached the 7MB/s average which I got for the tests. No idea what caused this performance drop, but it was a fact: as days went by, the transfer speed was not on par, but far less.

The raspi3 on the other side looks more reliable, its uptime is 40 days as I write, and it still is right below the 7MB/s mark.

Firefox not firing onReadyStateChange with two AJAX requests and a confirm dialogue

There are many discussions like this on the internet, but I am confident I am now examining a very specific problem of Firefox, with a very specific AJAX routine.

Symptoms: there are other AJAX requests in the background, and there is a request initiated by the user, which requires confirmation (your average “are you sure you want to delete?” dialogue). The AJAX request to delete the entry never fired. Every AJAX request was in the default, asynchronous mode.

No matter what, I couldn’t for the life of me make that request work, everything else did. Only the “delete” functions didn’t (in Firefox, as Chome and Internet Explorer worked no problem).

I used console.log messages as crumbs along the way, to follow the flow, and it stopped right at onReadyStateChange, as anything after it wouldn’t show zip in the console. Which meant the onReadyStateChange wasn’t being triggered.

I even thought it was something off with the function name (maybe the delete inside it borked something, silly me thought).

I also tried switching to synchronous mode the incriminated requests… and welp, they worked! For a short while I thought about leaving them be synchronous, but I didn’t want those pesky warnings in the console about synchronous requests ruining the user esperience.

Only by chance an epifany happened, and the obvious came forth (I KNEW there had to be a reasonable explanation which I had yet to grasp).

Well, my configuration went like this (it’s an internal web application we are talking about): an AJAX request triggered each second to a PHP to check the user credentials and inactivity time, and an AJAX request by user intervention was launched to delete an entry… after a confirm dialogue requested, well, user confirmation for the delete (duh).

Everything worked flawlessly before, the problem started appearing after I added the background AJAX request each second (which I didn’t want to get rid of anyway).

As you may or may not know, the window.confirm dialogue is a synchronous structure, which means the page execution is frozen until the user presses either OK or CANCEL in the dialogue (same thing happens with the window.alert function).

So, imagine the background AJAX request being queued behind the confirm dialogue, and obviously taking precedence over anything else that had to come after, and the new AJAX request being formed right after the press on the OK button of the confirm dialogue, to delete the entry that the confirmation was for.

Well, these two requests were launched at the very same time, in a manner that Firefox (not the other browsers though) probably overlapped the onReadyStateChange and never triggered the one for the secondary AJAX request.

In fact getting rid of the confirmation dialogue solved the problem.

Either that, or I will have to come up with an asynchronous method of requesting user confirmation.