Created
April 6, 2017 08:54
-
-
Save ziizii/bb1ca08e03143ea005212569a4d9b0d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
pubspec.yaml: | |
name: myscripts | |
version: 1.0.0 | |
dependencies: | |
xml: | |
intl: | |
*/ | |
import 'dart:convert'; | |
import 'dart:io'; | |
import 'package:intl/intl.dart'; | |
import 'package:xml/xml.dart'; | |
final desktop = "${Platform.environment["HOME"]}/Desktop"; | |
const jmeno = "xxx"; | |
const prijmeni = "yyy"; | |
const dic = "1234567890"; | |
const telefon = "777123123"; | |
const email = "asd@gmail.com"; | |
final datovaSchranka = "a1aaaa"; | |
const ulice = "Ulice"; | |
const cisloPopisne = "1111"; | |
const cisloOrientacni = "11"; | |
const psc = "11111"; | |
const mesto = "PRAHA"; | |
const stat = "ČESKÁ REPUBLIKA"; | |
const pracovisteFU = "1234"; | |
const ufo = "457"; | |
const cinnost = "620200"; | |
const fakturoidSlug = "xxx"; | |
const fakturoidApiKey = "qwertyuiopasdfghjklzxcvbnm"; | |
const fakturoidUrl = "https://app.fakturoid.cz/api/v2/accounts/$fakturoidSlug/invoices.json"; | |
const fakturoidEmail = email; | |
main() async { | |
var req = await new HttpClient().getUrl(Uri.parse(fakturoidUrl)); | |
req.headers.set("Authorization", "Basic ${BASE64.encode("$fakturoidEmail:$fakturoidApiKey".codeUnits)}"); | |
req.headers.set("User-Agent", "Dart ($fakturoidEmail)"); | |
var res = await req.close(); | |
var json = await res.transform(UTF8.decoder).join(); | |
Iterable allInvoices = JSON.decode(json); | |
var invoices = allInvoices.where((d)=> DateTime.parse(d["taxable_fulfillment_due"]).month == lastMonthStart.month); | |
var suffix = "-${lastMonthStart.year}-${lastMonthStart.month}.xml"; | |
var khString = kontrolniHlaseni(invoices); | |
var dphString = dph(invoices); | |
new File("$desktop/dph-kontrolni-hlaseni$suffix").writeAsStringSync(khString); | |
new File("$desktop/dph$suffix").writeAsStringSync(dphString); | |
exit(0); | |
} | |
dph(Iterable invoices) { | |
var b = new XmlBuilder(); | |
b.processing('xml', 'version="1.0"'); | |
b.element("Pisemnost", attributes: {"nazevSW":"EPO MF ČR", "verzeSW":"39.15.1"}, nest: (){ | |
b.element("DPHDP3", attributes: {"verzePis":"01.02"}, nest: (){ | |
b.element("VetaD", attributes: { | |
"c_okec": cinnost, | |
"d_poddp": formatDate(now), | |
"dapdph_forma": "B", | |
"dokument": "DP3", | |
"k_uladis": "DPH", | |
"mesic": "${lastMonthStart.month}", | |
"rok": "${lastMonthStart.year}", | |
"trans": "A", | |
"typ_platce": "P", | |
"zdobd_do": formatDate(lastMonthEnd), | |
"zdobd_od": formatDate(lastMonthStart) | |
}); | |
b.element("VetaP", attributes: { | |
"c_orient": cisloOrientacni, | |
"c_pop": cisloPopisne, | |
"c_pracufo": pracovisteFU, | |
"c_telef": telefon, | |
"c_ufo": ufo, | |
"dic": dic, | |
"email": email, | |
"jmeno": jmeno, | |
"naz_obce": mesto, | |
"prijmeni": prijmeni, | |
"psc": psc, | |
"stat": stat, | |
"typ_ds": "F", | |
"ulice": ulice | |
}); | |
var totalDph = 0.0; | |
var total = 0.0; | |
for (var i in invoices) { | |
var bezDph = double.parse(i["subtotal"]); | |
var sDph = double.parse(i["total"]); | |
var dph = sDph - bezDph; | |
total += bezDph; | |
totalDph += dph; | |
} | |
b.element("Veta1", attributes: { | |
"dan23": formatPrice(totalDph), | |
"obrat23": formatPrice(total) | |
}); | |
b.element("Veta4", attributes: { | |
"odp_sum_kr": "0", | |
"odp_sum_nar": "0", | |
"odp_tuz23_nar": "0", | |
"pln23": "0" | |
}); | |
b.element("Veta5", attributes: {"odp_uprav_kf":"0"}); | |
b.element("Veta6", attributes: { | |
"dan_zocelk": formatPrice(totalDph), | |
"dano": "0", | |
"dano_da": formatPrice(totalDph), | |
"dano_no": "0", | |
"odp_zocelk": "0" | |
}); | |
}); | |
}); | |
return formatXml(b); | |
} | |
String kontrolniHlaseni(Iterable invoices) { | |
var b = new XmlBuilder(); | |
b.processing('xml', 'version="1.0"'); | |
b.element("Pisemnost", attributes: {"nazevSW":"EPO MF ČR", "verzeSW":"39.15.1"}, nest: (){ | |
b.element("DPHKH1", attributes: {"verzePis":"02.01"}, nest: (){ | |
b.element("VetaD", attributes: { | |
"d_poddp": formatDate(now), | |
"dokument": "KH1", | |
"k_uladis": "DPH", | |
"khdph_forma": "B", | |
"mesic": "${lastMonthStart.month}", | |
"rok": "${lastMonthStart.year}", | |
"zdobd_do": formatDate(lastMonthEnd), | |
"zdobd_od": formatDate(lastMonthStart) | |
}); | |
b.element("VetaP", attributes: { | |
"c_orient": cisloOrientacni, | |
"c_pop": cisloPopisne, | |
"c_pracufo": pracovisteFU, | |
"c_telef": telefon, | |
"c_ufo": ufo, | |
"dic": dic, | |
"email": email, | |
"id_dats": datovaSchranka, | |
"jmeno": jmeno, | |
"naz_obce": mesto, | |
"prijmeni": prijmeni, | |
"psc": psc, | |
"sest_jmeno": jmeno, | |
"sest_prijmeni": prijmeni, | |
"sest_telef": telefon, | |
"stat": stat, | |
"typ_ds": "F", | |
"ulice": ulice | |
}); | |
var radek = 0; | |
var total = 0.0; | |
for (var i in invoices) { | |
radek++; | |
var bezDph = double.parse(i["subtotal"]); | |
var sDph = double.parse(i["total"]); | |
var dph = sDph - bezDph; | |
var duzp = DateTime.parse(i["taxable_fulfillment_due"]); | |
var klientDic = i["client_vat_no"].substring(2); | |
total += bezDph; | |
b.element("VetaA4", attributes: { | |
"c_evid_dd": "${i["number"]}", | |
"c_radku": "$radek", | |
"dan1": formatPrice(dph), | |
"dic_odb": klientDic, | |
"dppd": formatDate(duzp), | |
"kod_rezim_pl": "0", | |
"zakl_dane1": formatPrice(bezDph), | |
"zdph_44": "N" | |
}); | |
} | |
b.element("VetaC", attributes: { | |
"obrat23": formatPrice(total) | |
}); | |
}); | |
}); | |
return formatXml(b); | |
} | |
DateTime get now => new DateTime.now(); | |
DateTime get lastMonthStart => new DateTime(now.year, now.month-1, 1); | |
DateTime get lastMonthEnd => new DateTime(now.year, now.month, 0); | |
String formatDate(DateTime time) => new DateFormat("dd.MM.yyy").format(time); | |
String formatXml(XmlBuilder b) => b.build().toXmlString(pretty: true); | |
String formatPrice(num price) => price.round().toString(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tohle pokrývá základní programátorské potřeby (ostatně nikdo jiný to ani nespustí) - pár faktur měsíčně nad 10k, vše s 21% DPH, žádné náklady. Pokud toho má někdo víc a složitější, určitě bych mu doporučil váš vyšší tarif.