Browse Source

Poprawki do JPK oraz wstępne przygotowanie do implementacji JPK_VAT (3)

Mariusz Muszyński 8 years ago
parent
commit
f919474305
2 changed files with 614 additions and 60 deletions
  1. 506 0
      SE/schema/jpk/Schemat_JPK_VAT(3)_v1-1.xsd
  2. 108 60
      SE/se-lib/Route/UrlAction/JPK.php

+ 506 - 0
SE/schema/jpk/Schemat_JPK_VAT(3)_v1-1.xsd

@@ -0,0 +1,506 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:etd="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2016/01/25/eD/DefinicjeTypy/" xmlns:tns="http://jpk.mf.gov.pl/wzor/2017/11/13/1113/" targetNamespace="http://jpk.mf.gov.pl/wzor/2017/11/13/1113/" elementFormDefault="qualified" attributeFormDefault="unqualified" version="1.0" xml:lang="pl">
+	<xsd:import namespace="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2016/01/25/eD/DefinicjeTypy/" schemaLocation="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2016/01/25/eD/DefinicjeTypy/StrukturyDanych_v4-0E.xsd"/>
+	<xsd:simpleType name="TKodFormularza">
+		<xsd:annotation>
+			<xsd:documentation>Symbol wzoru formularza</xsd:documentation>
+		</xsd:annotation>
+		<xsd:restriction base="xsd:string">
+			<xsd:enumeration value="JPK_VAT"/>
+		</xsd:restriction>
+	</xsd:simpleType>
+	<xsd:simpleType name="TDataCzas">
+		<xsd:restriction base="etd:TDataCzas">
+			<xsd:minInclusive value="2016-07-01T00:00:00Z"/>
+			<xsd:maxInclusive value="2030-12-31T23:59:59Z"/>
+		</xsd:restriction>
+	</xsd:simpleType>
+	<xsd:simpleType name="TData">
+		<xsd:restriction base="etd:TData">
+			<xsd:minInclusive value="2016-07-01"/>
+			<xsd:maxInclusive value="2030-01-01"/>
+		</xsd:restriction>
+	</xsd:simpleType>
+	<xsd:simpleType name="TDataT">
+		<xsd:annotation>
+			<xsd:documentation>Data transakcji lub zdarzenia</xsd:documentation>
+		</xsd:annotation>
+		<xsd:restriction base="etd:TData">
+			<xsd:minInclusive value="2006-01-01"/>
+			<xsd:maxInclusive value="2030-01-01"/>
+		</xsd:restriction>
+	</xsd:simpleType>
+	<xsd:complexType name="TNaglowek">
+		<xsd:annotation>
+			<xsd:documentation>Nagłówek JPK_VAT</xsd:documentation>
+		</xsd:annotation>
+		<xsd:sequence>
+			<xsd:element name="KodFormularza">
+				<xsd:complexType>
+					<xsd:simpleContent>
+						<xsd:extension base="tns:TKodFormularza">
+						<xsd:attribute name="kodSystemowy" type="xsd:string" use="required" fixed="JPK_VAT (3)"/>
+							<xsd:attribute name="wersjaSchemy" type="xsd:string" use="required" fixed="1-1"/>
+						</xsd:extension>
+					</xsd:simpleContent>
+				</xsd:complexType>
+			</xsd:element>
+			<xsd:element name="WariantFormularza">
+				<xsd:simpleType>
+					<xsd:restriction base="xsd:byte">
+						<xsd:enumeration value="3"/>
+					</xsd:restriction>
+				</xsd:simpleType>
+			</xsd:element>
+			<xsd:element name="CelZlozenia" default="0">
+				<xsd:simpleType>
+					<xsd:restriction base="etd:TNaturalny">
+						<xsd:maxInclusive value="999"/>
+					</xsd:restriction>
+				</xsd:simpleType>
+			</xsd:element>
+			<xsd:element name="DataWytworzeniaJPK" type="tns:TDataCzas">
+				<xsd:annotation>
+					<xsd:documentation>Data i czas wytworzenia JPK_VAT</xsd:documentation>
+				</xsd:annotation>
+			</xsd:element>
+			<xsd:element name="DataOd" type="tns:TData">
+				<xsd:annotation>
+					<xsd:documentation>Data początkowa okresu, którego dotyczy JPK_VAT</xsd:documentation>
+				</xsd:annotation>
+			</xsd:element>
+			<xsd:element name="DataDo" type="tns:TData">
+				<xsd:annotation>
+					<xsd:documentation>Data końcowa okresu, którego dotyczy JPK_VAT</xsd:documentation>
+				</xsd:annotation>
+			</xsd:element>
+			<xsd:element name="NazwaSystemu">
+				<xsd:annotation>
+					<xsd:documentation>Nazwa systemu, z którego pochodzą dane</xsd:documentation>
+				</xsd:annotation>
+				<xsd:simpleType>
+					<xsd:restriction base="xsd:token">
+						<xsd:minLength value="1"/>
+						<xsd:maxLength value="240"/>
+					</xsd:restriction>
+				</xsd:simpleType>
+			</xsd:element>
+		</xsd:sequence>
+	</xsd:complexType>
+	<xsd:simpleType name="TKwotowy">
+		<xsd:annotation>
+			<xsd:documentation>Wartość numeryczna 18 znaków max, w tym 2 znaki po przecinku</xsd:documentation>
+		</xsd:annotation>
+		<xsd:restriction base="xsd:decimal">
+			<xsd:totalDigits value="18"/>
+			<xsd:fractionDigits value="2"/>
+		</xsd:restriction>
+	</xsd:simpleType>
+	<xsd:simpleType name="TNaturalnyJPK">
+		<xsd:annotation>
+			<xsd:documentation>Liczby naturalne większe od zera</xsd:documentation>
+		</xsd:annotation>
+		<xsd:restriction base="etd:TNaturalny">
+			<xsd:minExclusive value="0"/>
+		</xsd:restriction>
+	</xsd:simpleType>
+	<xsd:simpleType name="TZnakowyJPK">
+		<xsd:annotation>
+			<xsd:documentation>Typ znakowy ograniczony do 256 znaków</xsd:documentation>
+		</xsd:annotation>
+		<xsd:restriction base="xsd:token">
+			<xsd:minLength value="1"/>
+			<xsd:maxLength value="256"/>
+		</xsd:restriction>
+	</xsd:simpleType>
+	<xsd:simpleType name="TAdresEmail">
+		<xsd:annotation>
+			<xsd:documentation>Adres e-mail</xsd:documentation>
+		</xsd:annotation>
+		<xsd:restriction base="xsd:token">
+			<xsd:minLength value="5"/>
+			<xsd:maxLength value="50"/>
+			<xsd:pattern value="([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})"/>
+		</xsd:restriction>
+	</xsd:simpleType>
+	<xsd:element name="JPK">
+		<xsd:annotation>
+			<xsd:documentation>Jednolity plik kontrolny dla ewidencji zakupu i sprzedaży VAT</xsd:documentation>
+		</xsd:annotation>
+		<xsd:complexType>
+			<xsd:sequence>
+				<xsd:element name="Naglowek" type="tns:TNaglowek">
+					<xsd:annotation>
+						<xsd:documentation>Nagłówek JPK_VAT</xsd:documentation>
+					</xsd:annotation>
+				</xsd:element>
+				<xsd:element name="Podmiot1">
+					<xsd:complexType>
+						<xsd:sequence>
+							<xsd:element name="NIP" type="etd:TNrNIP">
+								<xsd:annotation>
+									<xsd:documentation>Identyfikator podatkowy NIP</xsd:documentation>
+								</xsd:annotation>
+							</xsd:element>
+							<xsd:element name="PelnaNazwa">
+								<xsd:annotation>
+									<xsd:documentation>Pełna nazwa</xsd:documentation>
+								</xsd:annotation>
+								<xsd:simpleType>
+									<xsd:restriction base="xsd:token">
+										<xsd:maxLength value="240"/>
+										<xsd:minLength value="1"/>
+									</xsd:restriction>
+								</xsd:simpleType>
+							</xsd:element>
+							<xsd:element name="Email" type="tns:TAdresEmail" minOccurs="0"/>
+						</xsd:sequence>
+					</xsd:complexType>
+				</xsd:element>
+				<xsd:sequence minOccurs="0">
+					<xsd:element name="SprzedazWiersz" maxOccurs="unbounded">
+						<xsd:annotation>
+							<xsd:documentation>Ewidencja sprzedaży oraz nabyć towarów i usług dla których podmiot obowiązany jest naliczyć podatek należny - tj. wewnątrzwspólnotowe nabycia towarów, import towarów podlegających rozliczeniu zgodnie z art. 33 a ustawy, import usług z wyłączeniem usług nabywanych od podatników podatku od wartości dodanej, do których stosuje się art. 28 b ustawy, import usług nabywanych od podatników podatku od wartości dodanej, do których stosuje się art. 28 b ustawy, dostawa towarów, dla których podatnikiem jest nabywca zgodnie z art. 17 ust. 1 pkt 5 ustawy (wypełnia nabywca), dostawa towarów, dla których podatnikiem jest nabywca zgodnie z art. 17 ust. 1 pkt 7 lub 8 ustawy (wypełnia nabywca)</xsd:documentation>
+						</xsd:annotation>
+						<xsd:complexType>
+							<xsd:sequence>
+								<xsd:element name="LpSprzedazy" type="tns:TNaturalnyJPK">
+									<xsd:annotation>
+										<xsd:documentation>Lp. wiersza ewidencji sprzedaży VAT</xsd:documentation>
+									</xsd:annotation>
+								</xsd:element>
+								<xsd:element name="NrKontrahenta" type="etd:TNrIdentyfikacjiPodatkowej">
+									<xsd:annotation>
+										<xsd:documentation>Numer, za pomocą którego kontrahent jest zidentyfikowany na potrzeby podatku lub podatku od wartości dodanej</xsd:documentation>
+									</xsd:annotation>
+								</xsd:element>
+								<xsd:element name="NazwaKontrahenta" type="tns:TZnakowyJPK">
+									<xsd:annotation>
+										<xsd:documentation>Imię i nazwisko lub nazwa kontrahenta</xsd:documentation>
+									</xsd:annotation>
+								</xsd:element>
+								<xsd:element name="AdresKontrahenta" type="tns:TZnakowyJPK">
+									<xsd:annotation>
+										<xsd:documentation>Adres kontrahenta</xsd:documentation>
+									</xsd:annotation>
+								</xsd:element>
+								<xsd:element name="DowodSprzedazy" type="tns:TZnakowyJPK">
+									<xsd:annotation>
+										<xsd:documentation>Numer dowodu sprzedaży</xsd:documentation>
+									</xsd:annotation>
+								</xsd:element>
+								<xsd:element name="DataWystawienia" type="tns:TDataT">
+									<xsd:annotation>
+										<xsd:documentation>Data wystawienia dowodu sprzedaży</xsd:documentation>
+									</xsd:annotation>
+								</xsd:element>
+								<xsd:element name="DataSprzedazy" type="tns:TDataT" minOccurs="0">
+									<xsd:annotation>
+										<xsd:documentation>Data sprzedaży, o ile jest określona i różni się od daty wystawienia dowodu sprzedaży. W przeciwnym przypadku - pole puste</xsd:documentation>
+									</xsd:annotation>
+								</xsd:element>
+								<xsd:element name="K_10" type="tns:TKwotowy" minOccurs="0">
+									<xsd:annotation>
+										<xsd:documentation>Kwota netto - Dostawa towarów oraz świadczenie usług na terytorium kraju, zwolnione od podatku</xsd:documentation>
+									</xsd:annotation>
+								</xsd:element>
+								<xsd:element name="K_11" type="tns:TKwotowy" minOccurs="0">
+									<xsd:annotation>
+										<xsd:documentation>Kwota netto - Dostawa towarów oraz świadczenie usług poza terytorium kraju</xsd:documentation>
+									</xsd:annotation>
+								</xsd:element>
+								<xsd:element name="K_12" type="tns:TKwotowy" minOccurs="0">
+									<xsd:annotation>
+										<xsd:documentation>Kwota netto - w tym świadczenie usług, o których mowa w art. 100 ust. 1 pkt 4 ustawy</xsd:documentation>
+									</xsd:annotation>
+								</xsd:element>
+								<xsd:element name="K_13" type="tns:TKwotowy" minOccurs="0">
+									<xsd:annotation>
+										<xsd:documentation>Kwota netto - Dostawa towarów oraz świadczenie usług na terytorium kraju, opodatkowane stawką 0%</xsd:documentation>
+									</xsd:annotation>
+								</xsd:element>
+								<xsd:element name="K_14" type="tns:TKwotowy" minOccurs="0">
+									<xsd:annotation>
+										<xsd:documentation>Kwota netto - w tym dostawa towarów, o której mowa w art. 129 ustawy</xsd:documentation>
+									</xsd:annotation>
+								</xsd:element>
+								<xsd:sequence minOccurs="0">
+									<xsd:element name="K_15" type="tns:TKwotowy">
+										<xsd:annotation>
+											<xsd:documentation>Kwota netto - Dostawa towarów oraz świadczenie usług na terytorium kraju, opodatkowane stawką 5%</xsd:documentation>
+										</xsd:annotation>
+									</xsd:element>
+									<xsd:element name="K_16" type="tns:TKwotowy">
+										<xsd:annotation>
+											<xsd:documentation>Kwota podatku należnego - Dostawa towarów oraz świadczenie usług na terytorium kraju, opodatkowane stawką 5%</xsd:documentation>
+										</xsd:annotation>
+									</xsd:element>
+								</xsd:sequence>
+								<xsd:sequence minOccurs="0">
+									<xsd:element name="K_17" type="tns:TKwotowy">
+										<xsd:annotation>
+											<xsd:documentation>Kwota netto - Dostawa towarów oraz świadczenie usług na terytorium kraju, opodatkowane stawką 7% albo 8%</xsd:documentation>
+										</xsd:annotation>
+									</xsd:element>
+									<xsd:element name="K_18" type="tns:TKwotowy">
+										<xsd:annotation>
+											<xsd:documentation>Kwota podatku należnego - Dostawa towarów oraz świadczenie usług na terytorium kraju, opodatkowane stawką 7% albo 8%</xsd:documentation>
+										</xsd:annotation>
+									</xsd:element>
+								</xsd:sequence>
+								<xsd:sequence minOccurs="0">
+									<xsd:element name="K_19" type="tns:TKwotowy">
+										<xsd:annotation>
+											<xsd:documentation>Kwota netto - Dostawa towarów oraz świadczenie usług na terytorium kraju, opodatkowane stawką 22% albo 23%</xsd:documentation>
+										</xsd:annotation>
+									</xsd:element>
+									<xsd:element name="K_20" type="tns:TKwotowy">
+										<xsd:annotation>
+											<xsd:documentation>Kwota podatku należnego - Dostawa towarów oraz świadczenie usług na terytorium kraju, opodatkowane stawką 22% albo 23%</xsd:documentation>
+										</xsd:annotation>
+									</xsd:element>
+								</xsd:sequence>
+								<xsd:element name="K_21" type="tns:TKwotowy" minOccurs="0">
+									<xsd:annotation>
+										<xsd:documentation>Kwota netto - Wewnątrzwspólnotowa dostawa towarów</xsd:documentation>
+									</xsd:annotation>
+								</xsd:element>
+								<xsd:element name="K_22" type="tns:TKwotowy" minOccurs="0">
+									<xsd:annotation>
+										<xsd:documentation>Kwota netto - Eksport towarów</xsd:documentation>
+									</xsd:annotation>
+								</xsd:element>
+								<xsd:sequence minOccurs="0">
+									<xsd:element name="K_23" type="tns:TKwotowy">
+										<xsd:annotation>
+											<xsd:documentation>Kwota netto - Wewnątrzwspólnotowe nabycie towarów</xsd:documentation>
+										</xsd:annotation>
+									</xsd:element>
+									<xsd:element name="K_24" type="tns:TKwotowy">
+										<xsd:annotation>
+											<xsd:documentation>Kwota podatku należnego - Wewnątrzwspólnotowe nabycie towarów</xsd:documentation>
+										</xsd:annotation>
+									</xsd:element>
+								</xsd:sequence>
+								<xsd:sequence minOccurs="0">
+									<xsd:element name="K_25" type="tns:TKwotowy">
+										<xsd:annotation>
+											<xsd:documentation>Kwota netto - Import towarów podlegający rozliczeniu zgodnie z art. 33a ustawy</xsd:documentation>
+										</xsd:annotation>
+									</xsd:element>
+									<xsd:element name="K_26" type="tns:TKwotowy">
+										<xsd:annotation>
+											<xsd:documentation>Kwota podatku należnego - Import towarów podlegający rozliczeniu zgodnie z art. 33a ustawy</xsd:documentation>
+										</xsd:annotation>
+									</xsd:element>
+								</xsd:sequence>
+								<xsd:sequence minOccurs="0">
+									<xsd:element name="K_27" type="tns:TKwotowy">
+										<xsd:annotation>
+											<xsd:documentation>Kwota netto - Import usług z wyłączeniem usług nabywanych od podatników podatku od wartości dodanej, do których stosuje się art. 28b ustawy</xsd:documentation>
+										</xsd:annotation>
+									</xsd:element>
+									<xsd:element name="K_28" type="tns:TKwotowy">
+										<xsd:annotation>
+											<xsd:documentation>Kwota podatku należnego - Import usług z wyłączeniem usług nabywanych od podatników podatku od wartości dodanej, do których stosuje się art. 28b ustawy</xsd:documentation>
+										</xsd:annotation>
+									</xsd:element>
+								</xsd:sequence>
+								<xsd:sequence minOccurs="0">
+									<xsd:element name="K_29" type="tns:TKwotowy">
+										<xsd:annotation>
+											<xsd:documentation>Kwota netto - Import usług nabywanych od podatników podatku od wartości dodanej, do których stosuje się art. 28b ustawy</xsd:documentation>
+										</xsd:annotation>
+									</xsd:element>
+									<xsd:element name="K_30" type="tns:TKwotowy">
+										<xsd:annotation>
+											<xsd:documentation>Kwota podatku należnego - Import usług nabywanych od podatników podatku od wartości dodanej, do których stosuje się art. 28b ustawy</xsd:documentation>
+										</xsd:annotation>
+									</xsd:element>
+								</xsd:sequence>
+								<xsd:element name="K_31" type="tns:TKwotowy" minOccurs="0">
+									<xsd:annotation>
+										<xsd:documentation>Kwota netto - Dostawa towarów oraz świadczenie usług, dla których podatnikiem jest nabywca zgodnie z art. 17 ust. 1 pkt 7 lub 8 ustawy (wypełnia dostawca)</xsd:documentation>
+									</xsd:annotation>
+								</xsd:element>
+								<xsd:sequence minOccurs="0">
+									<xsd:element name="K_32" type="tns:TKwotowy">
+										<xsd:annotation>
+											<xsd:documentation>Kwota netto - Dostawa towarów, dla których podatnikiem jest nabywca zgodnie z art. 17 ust. 1 pkt 5 ustawy (wypełnia nabywca)</xsd:documentation>
+										</xsd:annotation>
+									</xsd:element>
+									<xsd:element name="K_33" type="tns:TKwotowy">
+										<xsd:annotation>
+											<xsd:documentation>Kwota podatku należnego - Dostawa towarów, dla których podatnikiem jest nabywca zgodnie z art. 17 ust. 1 pkt 5 ustawy (wypełnia nabywca)</xsd:documentation>
+										</xsd:annotation>
+									</xsd:element>
+								</xsd:sequence>
+								<xsd:sequence minOccurs="0">
+									<xsd:element name="K_34" type="tns:TKwotowy">
+										<xsd:annotation>
+											<xsd:documentation>Kwota netto - Dostawa towarów oraz świadczenie usług, dla których podatnikiem jest nabywca zgodnie z art. 17 ust. 1 pkt 7 lub 8 ustawy (wypełnia nabywca)</xsd:documentation>
+										</xsd:annotation>
+									</xsd:element>
+									<xsd:element name="K_35" type="tns:TKwotowy">
+										<xsd:annotation>
+											<xsd:documentation>Kwota podatku należnego - Dostawa towarów oraz świadczenie usług, dla których podatnikiem jest nabywca zgodnie z art. 17 ust. 1 pkt 7 lub 8 ustawy (wypełnia nabywca)</xsd:documentation>
+										</xsd:annotation>
+									</xsd:element>
+								</xsd:sequence>
+								<xsd:element name="K_36" type="tns:TKwotowy" minOccurs="0">
+									<xsd:annotation>
+										<xsd:documentation>Kwota podatku należnego od towarów i usług objętych spisem z natury, o którym mowa w art. 14 ust. 5 ustawy</xsd:documentation>
+									</xsd:annotation>
+								</xsd:element>
+								<xsd:element name="K_37" type="tns:TKwotowy" minOccurs="0">
+									<xsd:annotation>
+										<xsd:documentation>Zwrot odliczonej lub zwróconej kwoty wydatkowanej na zakup kas rejestrujących, o którym mowa w art. 111 ust. 6 ustawy</xsd:documentation>
+									</xsd:annotation>
+								</xsd:element>
+								<xsd:element name="K_38" type="tns:TKwotowy" minOccurs="0">
+									<xsd:annotation>
+										<xsd:documentation>Kwota podatku należnego od wewnątrzwspólnotowego nabycia środków transportu, wykazanego w elemencie K_24, podlegająca wpłacie w terminie, o którym mowa w art. 103 ust. 3, w związku z ust. 4 ustawy</xsd:documentation>
+									</xsd:annotation>
+								</xsd:element>
+								<xsd:element name="K_39" type="tns:TKwotowy" minOccurs="0">
+									<xsd:annotation>
+										<xsd:documentation>Kwota podatku od wewnątrzwspólnotowego nabycia paliw silnikowych, podlegająca wpłacie w terminach,
+o których mowa w art. 103 ust. 5a i 5b ustawy</xsd:documentation>
+									</xsd:annotation>
+								</xsd:element>
+							</xsd:sequence>
+						</xsd:complexType>
+					</xsd:element>
+					<xsd:element name="SprzedazCtrl">
+						<xsd:annotation>
+							<xsd:documentation>Sumy kontrolne dla ewidencji sprzedaży VAT</xsd:documentation>
+						</xsd:annotation>
+						<xsd:complexType>
+							<xsd:sequence>
+								<xsd:element name="LiczbaWierszySprzedazy" type="tns:TNaturalnyJPK">
+									<xsd:annotation>
+										<xsd:documentation>Liczba wierszy ewidencji sprzedaży, w okresie którego dotyczy JPK</xsd:documentation>
+									</xsd:annotation>
+								</xsd:element>
+								<xsd:element name="PodatekNalezny" type="tns:TKwotowy">
+									<xsd:annotation>
+										<xsd:documentation>Podatek należny wg ewidencji sprzedaży w okresie, którego dotyczy JPK - suma kwot z elementów K_16, K_18, K_20, K_24, K_26, K_28, K_30, K_33, K_35, K_36 i K_37 pomniejszona o kwotę z elementów K_38 i K_39</xsd:documentation>
+									</xsd:annotation>
+								</xsd:element>
+							</xsd:sequence>
+						</xsd:complexType>
+					</xsd:element>
+				</xsd:sequence>
+				<xsd:sequence minOccurs="0">
+					<xsd:element name="ZakupWiersz" maxOccurs="unbounded">
+						<xsd:annotation>
+							<xsd:documentation>Ewidencja zakupu VAT</xsd:documentation>
+						</xsd:annotation>
+						<xsd:complexType>
+							<xsd:sequence>
+								<xsd:element name="LpZakupu" type="tns:TNaturalnyJPK">
+									<xsd:annotation>
+										<xsd:documentation>Lp. wiersza ewidencji zakupu VAT</xsd:documentation>
+									</xsd:annotation>
+								</xsd:element>
+								<xsd:element name="NrDostawcy" type="etd:TNrIdentyfikacjiPodatkowej">
+									<xsd:annotation>
+										<xsd:documentation>Numer, za pomocą którego dostawca (kontrahent) jest zidentyfikowany na potrzeby podatku lub podatku od wartości dodanej</xsd:documentation>
+									</xsd:annotation>
+								</xsd:element>
+								<xsd:element name="NazwaDostawcy" type="tns:TZnakowyJPK">
+									<xsd:annotation>
+										<xsd:documentation>Imię i nazwisko lub nazwa dostawcy (kontrahenta)</xsd:documentation>
+									</xsd:annotation>
+								</xsd:element>
+								<xsd:element name="AdresDostawcy" type="tns:TZnakowyJPK">
+									<xsd:annotation>
+										<xsd:documentation>Adres dostawcy (kontrahenta)</xsd:documentation>
+									</xsd:annotation>
+								</xsd:element>
+								<xsd:element name="DowodZakupu" type="tns:TZnakowyJPK">
+									<xsd:annotation>
+										<xsd:documentation>Numer dowodu zakupu</xsd:documentation>
+									</xsd:annotation>
+								</xsd:element>
+								<xsd:element name="DataZakupu" type="tns:TDataT">
+									<xsd:annotation>
+										<xsd:documentation>Data wystawienia dowodu zakupu</xsd:documentation>
+									</xsd:annotation>
+								</xsd:element>
+								<xsd:element name="DataWplywu" type="tns:TDataT" minOccurs="0">
+									<xsd:annotation>
+										<xsd:documentation>Data wpływu dowodu zakupu</xsd:documentation>
+									</xsd:annotation>
+								</xsd:element>
+								<xsd:sequence minOccurs="0">
+									<xsd:element name="K_43" type="tns:TKwotowy">
+										<xsd:annotation>
+											<xsd:documentation>Kwota netto - Nabycie towarów i usług zaliczanych u podatnika do środków trwałych</xsd:documentation>
+										</xsd:annotation>
+									</xsd:element>
+									<xsd:element name="K_44" type="tns:TKwotowy">
+										<xsd:annotation>
+											<xsd:documentation>Kwota podatku naliczonego - Nabycie towarów i usług zaliczanych u podatnika do środków trwałych</xsd:documentation>
+										</xsd:annotation>
+									</xsd:element>
+								</xsd:sequence>
+								<xsd:sequence minOccurs="0">
+									<xsd:element name="K_45" type="tns:TKwotowy">
+										<xsd:annotation>
+											<xsd:documentation>Kwota netto - Nabycie towarów i usług pozostałych</xsd:documentation>
+										</xsd:annotation>
+									</xsd:element>
+									<xsd:element name="K_46" type="tns:TKwotowy">
+										<xsd:annotation>
+											<xsd:documentation>Kwota podatku naliczonego - Nabycie towarów i usług pozostałych</xsd:documentation>
+										</xsd:annotation>
+									</xsd:element>
+								</xsd:sequence>
+								<xsd:element name="K_47" type="tns:TKwotowy" minOccurs="0">
+									<xsd:annotation>
+										<xsd:documentation>Korekta podatku naliczonego od nabycia środków trwałych</xsd:documentation>
+									</xsd:annotation>
+								</xsd:element>
+								<xsd:element name="K_48" type="tns:TKwotowy" minOccurs="0">
+									<xsd:annotation>
+										<xsd:documentation>Korekta podatku naliczonego od pozostałych nabyć</xsd:documentation>
+									</xsd:annotation>
+								</xsd:element>
+								<xsd:element name="K_49" type="tns:TKwotowy" minOccurs="0">
+									<xsd:annotation>
+										<xsd:documentation>Korekta podatku naliczonego, o której mowa w art. 89b ust. 1 ustawy</xsd:documentation>
+									</xsd:annotation>
+								</xsd:element>
+								<xsd:element name="K_50" type="tns:TKwotowy" minOccurs="0">
+									<xsd:annotation>
+										<xsd:documentation>Korekta podatku naliczonego, o której mowa w art. 89b ust. 4 ustawy</xsd:documentation>
+									</xsd:annotation>
+								</xsd:element>
+							</xsd:sequence>
+						</xsd:complexType>
+					</xsd:element>
+					<xsd:element name="ZakupCtrl">
+						<xsd:annotation>
+							<xsd:documentation>Sumy kontrolne dla ewidencji zakupu VAT</xsd:documentation>
+						</xsd:annotation>
+						<xsd:complexType>
+							<xsd:sequence>
+								<xsd:element name="LiczbaWierszyZakupow" type="tns:TNaturalnyJPK">
+									<xsd:annotation>
+										<xsd:documentation>Liczba wierszy ewidencji zakupu, w okresie którego dotyczy JPK</xsd:documentation>
+									</xsd:annotation>
+								</xsd:element>
+								<xsd:element name="PodatekNaliczony" type="tns:TKwotowy">
+									<xsd:annotation>
+										<xsd:documentation>Razem kwota podatku naliczonego do odliczenia - suma kwot z elementów K_44, K_46, K_47, K_48, K_49 i K_50</xsd:documentation>
+									</xsd:annotation>
+								</xsd:element>
+							</xsd:sequence>
+						</xsd:complexType>
+					</xsd:element>
+				</xsd:sequence>
+			</xsd:sequence>
+		</xsd:complexType>
+	</xsd:element>
+</xsd:schema>

+ 108 - 60
SE/se-lib/Route/UrlAction/JPK.php

@@ -8,6 +8,11 @@ class Route_UrlAction_JPK extends RouteBase {
 	private $REFERER, $LAST_REFERER;
 	private $JPK, $BO;
 	private $NIPerrors = array();
+	private $JPK_VAT_Wariant = null;
+	const tns = [
+		2 => "http://jpk.mf.gov.pl/wzor/2016/10/26/10261/",
+		3 => "http://jpk.mf.gov.pl/wzor/2017/11/13/1113/",
+	];
 
 	public function handleAuth() {
 		if (!User::logged()) {
@@ -23,6 +28,12 @@ class Route_UrlAction_JPK extends RouteBase {
 			else throw new Exception("Dostęp zabroniony");
 			if ($result = DB::getPDO()->fetchall("select * from BILLING_OWNER where ID='{$this->JPK['ID_BILLING_OWNER']}'")) $this->BO = $result[0];
 			else throw new Exception("Błąd spójności danych");
+			if ($this->JPK['A_STATUS'] != 'WAITING') {
+				if (!$this->JPK['MONTH']) throw new Exception("Błąd daty w rekordzie JPK_VAT");
+				$year = date("Y", strtotime($this->JPK['MONTH']));
+				if ($year < 2018) $this->JPK_VAT_Wariant = 2;
+				else $this->JPK_VAT_Wariant = 3;
+			}
 		} catch (Exception $e) {
 			SE_Layout::gora();
 			SE_Layout::menu();
@@ -180,34 +191,46 @@ format: 'YYYY-MM'
 		"PodatekNaliczony" => "Podatek naliczony"
 	);
 
-	private function showJPK_VAT($xml) {
+	private static function getNamespaces($xml) {
 		$ns = $xml->getNamespaces(true);
+		if (!isset($ns['tns'])) {
+			if (isset($ns[''])) {
+				$ns['tns'] = $ns[''];
+				unset($ns['']);
+			} else throw new Exception('Błędna struktura namespace');
+		}
+		foreach ($ns as $k => $v) $xml->registerXPathNamespace($k, $v);
+		return $ns;
+	}
+
+	private function showJPK_VAT($xml) {
+		$ns = self::getNamespaces($xml);
 		$summaryJPK_VAT = $this->getSummaryJPK_VAT($xml);
 
-		$result['KodFormularza'] = $xml->Naglowek->KodFormularza;
-		$result['WariantFormularza'] = $xml->Naglowek->WariantFormularza;
-		$result['CelZlozenia'] = $xml->Naglowek->CelZlozenia;
-		$result['DataWytworzeniaJPK'] = date("Y-m-d H:i:s", strtotime($xml->Naglowek->DataWytworzeniaJPK));
-		$result['DataOd'] = $xml->Naglowek->DataOd;
-		$result['DataDo'] = $xml->Naglowek->DataDo;
-		$result['DomyslnyKodWaluty'] = $xml->Naglowek->DomyslnyKodWaluty;
-		$result['KodUrzedu'] = $xml->Naglowek->KodUrzedu;
-		$result['NIP'] = $xml->Podmiot1->IdentyfikatorPodmiotu->children($ns['etd'])->NIP;
-		$result['PelnaNazwa'] = $xml->Podmiot1->IdentyfikatorPodmiotu->children($ns['etd'])->PelnaNazwa;
-		$result['REGON'] = $xml->Podmiot1->IdentyfikatorPodmiotu->children($ns['etd'])->REGON;
-		$result['KodKraju'] = $xml->Podmiot1->AdresPodmiotu->KodKraju;
-		$result['Wojewodztwo'] = $xml->Podmiot1->AdresPodmiotu->Wojewodztwo;
-		$result['Powiat'] = $xml->Podmiot1->AdresPodmiotu->Powiat;
-		$result['Gmina'] = $xml->Podmiot1->AdresPodmiotu->Gmina;
-		$result['Ulica'] = $xml->Podmiot1->AdresPodmiotu->Ulica;
-		$result['NrDomu'] = $xml->Podmiot1->AdresPodmiotu->NrDomu;
-		$result['NrLokalu'] = $xml->Podmiot1->AdresPodmiotu->NrLokalu;
-		$result['Miejscowosc'] = $xml->Podmiot1->AdresPodmiotu->Miejscowosc;
-		$result['KodPocztowy'] = $xml->Podmiot1->AdresPodmiotu->KodPocztowy;
-		$result['Poczta'] = $xml->Podmiot1->AdresPodmiotu->Poczta;
-		$result['LiczbaWierszySprzedazy'] = $summaryJPK_VAT['LiczbaWierszySprzedazy'] ? $summaryJPK_VAT['LiczbaWierszySprzedazy'] : "";
+		$result['KodFormularza'] = $xml->xpath('/tns:JPK/tns:Naglowek/tns:KodFormularza')[0];
+		$result['WariantFormularza'] = $xml->xpath('/tns:JPK/tns:Naglowek/tns:WariantFormularza')[0];
+		$result['CelZlozenia'] = $xml->xpath('/tns:JPK/tns:Naglowek/tns:CelZlozenia')[0];
+		$result['DataWytworzeniaJPK'] = $xml->xpath('/tns:JPK/tns:Naglowek/tns:DataWytworzeniaJPK')[0];
+		$result['DataOd'] = $xml->xpath('/tns:JPK/tns:Naglowek/tns:DataOd')[0];
+		$result['DataDo'] = $xml->xpath('/tns:JPK/tns:Naglowek/tns:DataDo')[0];
+		$result['DomyslnyKodWaluty'] = $xml->xpath('/tns:JPK/tns:Naglowek/tns:DomyslnyKodWaluty')[0];
+		$result['KodUrzedu'] = $xml->xpath('/tns:JPK/tns:Naglowek/tns:KodUrzedu')[0];
+		$result['NIP'] = $xml->xpath('/tns:JPK/tns:Podmiot1/tns:IdentyfikatorPodmiotu/etd:NIP')[0];
+		$result['PelnaNazwa'] = $xml->xpath('/tns:JPK/tns:Podmiot1/tns:IdentyfikatorPodmiotu/etd:PelnaNazwa')[0];
+		$result['REGON'] = $xml->xpath('/tns:JPK/tns:Podmiot1/tns:IdentyfikatorPodmiotu/etd:REGON')[0];
+		$result['KodKraju'] = $xml->xpath('/tns:JPK/tns:Podmiot1/tns:AdresPodmiotu/tns:KodKraju')[0];
+		$result['Wojewodztwo'] = $xml->xpath('/tns:JPK/tns:Podmiot1/tns:AdresPodmiotu/tns:Wojewodztwo')[0];
+		$result['Powiat'] = $xml->xpath('/tns:JPK/tns:Podmiot1/tns:AdresPodmiotu/tns:Powiat')[0];
+		$result['Gmina'] = $xml->xpath('/tns:JPK/tns:Podmiot1/tns:AdresPodmiotu/tns:Gmina')[0];
+		$result['Ulica'] = $xml->xpath('/tns:JPK/tns:Podmiot1/tns:AdresPodmiotu/tns:Ulica')[0];
+		$result['NrDomu'] = $xml->xpath('/tns:JPK/tns:Podmiot1/tns:AdresPodmiotu/tns:NrDomu')[0];
+		$result['NrLokalu'] = $xml->xpath('/tns:JPK/tns:Podmiot1/tns:AdresPodmiotu/tns:NrLokalu')[0];
+		$result['Miejscowosc'] = $xml->xpath('/tns:JPK/tns:Podmiot1/tns:AdresPodmiotu/tns:Miejscowosc')[0];
+		$result['KodPocztowy'] = $xml->xpath('/tns:JPK/tns:Podmiot1/tns:AdresPodmiotu/tns:KodPocztowy')[0];
+		$result['Poczta'] = $xml->xpath('/tns:JPK/tns:Podmiot1/tns:AdresPodmiotu/tns:Poczta')[0];
+		$result['LiczbaWierszySprzedazy'] = $summaryJPK_VAT['LiczbaWierszySprzedazy'] ?: "";
 		$result['PodatekNalezny'] = $summaryJPK_VAT['LiczbaWierszySprzedazy'] ? number_format($summaryJPK_VAT['PodatekNalezny'], 2, ',', '.'): "";
-		$result['LiczbaWierszyZakupow'] = $summaryJPK_VAT['LiczbaWierszyZakupow'] ? $summaryJPK_VAT['LiczbaWierszyZakupow'] : "";
+		$result['LiczbaWierszyZakupow'] = $summaryJPK_VAT['LiczbaWierszyZakupow'] ?: "";
 		$result['PodatekNaliczony'] = $summaryJPK_VAT['LiczbaWierszyZakupow'] ? number_format($summaryJPK_VAT['PodatekNaliczony'], 2, ',', '.'): "";
 
 		return $result;
@@ -425,6 +448,10 @@ format: 'YYYY-MM'
 	}
 
 	private function getSummaryJPK_VAT($xml) {
+		$ns = $xml->getNamespaces(true);
+		$tns = self::tns[$this->JPK_VAT_Wariant];
+		if (array_search($tns, $ns) === false) throw new Exception("Błąd struktury pliku XML - błędny namespace");
+
 		$PodatekNaleznyFields = array("K_16" => "1", "K_18" => "1", "K_20" => "1", "K_24" => "1", "K_26" => "1", "K_28" => "1",
 			"K_30" => "1", "K_33" => "1", "K_35" => "1", "K_36" => "1", "K_37" => "1", "K_38" => "-1", "K_39" => "-1");
 		$PodatekNaliczonyFields = array("K_44" => 1, "K_46" => 1, "K_47" => 1, "K_48" => 1, "K_49" => 1, "K_50" => 1);
@@ -435,19 +462,19 @@ format: 'YYYY-MM'
 			"LiczbaWierszyZakupow" => 0,
 			"PodatekNaliczony" => 0);
 
-		foreach ($xml->SprzedazWiersz as $SprzedazWiersz) {
+		foreach ($xml->children($tns)->SprzedazWiersz as $SprzedazWiersz) {
 			foreach ($PodatekNaleznyFields as $PodatekNaleznyField => $sign) {
-				if (isset($SprzedazWiersz->$PodatekNaleznyField)) {
-					$result["PodatekNalezny"] += round((float) $SprzedazWiersz->$PodatekNaleznyField * $sign, 2);
+				if (isset($SprzedazWiersz->children($tns)->$PodatekNaleznyField)) {
+					$result["PodatekNalezny"] += round((float) $SprzedazWiersz->children($tns)->$PodatekNaleznyField * $sign, 2);
 				}
 			}
 			$result["LiczbaWierszySprzedazy"]++;
 		}
 
-		foreach ($xml->ZakupWiersz as $ZakupWiersz) {
+		foreach ($xml->children($tns)->ZakupWiersz as $ZakupWiersz) {
 			foreach ($PodatekNaliczonyFields as $PodatekNaliczonyField => $sign) {
-				if (isset($ZakupWiersz->$PodatekNaliczonyField)) {
-					$result["PodatekNaliczony"] += round((float) $ZakupWiersz->$PodatekNaliczonyField * $sign, 2);
+				if (isset($ZakupWiersz->children($tns)->$PodatekNaliczonyField)) {
+					$result["PodatekNaliczony"] += round((float) $ZakupWiersz->children($tns)->$PodatekNaliczonyField * $sign, 2);
 				}
 			}
 			$result["LiczbaWierszyZakupow"]++;
@@ -459,30 +486,30 @@ format: 'YYYY-MM'
 	}
 
 	private function validateJPK_VAT(&$xml) {
-		$ns = $xml->getNamespaces(true);
-		if ($xml->Naglowek->KodFormularza != "JPK_VAT") throw new Exception("Błędny typ deklaracji JPK");
-		if ($xml->Naglowek->WariantFormularza != "2") throw new Exception("Błędna wersja deklaracji JPK");
-		if ($xml->Naglowek->CelZlozenia != $this->JPK['PURPOSE']) throw new Exception("Niezgodny cel złożenia deklaracji JPK");
-		if ($xml->Naglowek->DataOd != ($this->JPK['MONTH'] . "-01")) throw new Exception("Błędna data 'od' deklaracji JPK");
-		if ($xml->Naglowek->DataDo != date("Y-m-d", strtotime($xml->Naglowek->DataOd . "+ 1 month - 1 day"))) throw new Exception("Błędna data 'do' deklaracji JPK");
-		if ($xml->Naglowek->DomyslnyKodWaluty != "PLN") throw new Exception("Błędna waluta deklaracji JPK");
-		if (trim(str_replace('-', '', $xml->Podmiot1->IdentyfikatorPodmiotu->children($ns['etd'])->NIP)) != trim(str_replace('-', '', $this->BO['nip']))) throw new Exception("Niezgodny NIP podmiotu w deklaracji JPK");
+		$ns = self::getNamespaces($xml);
+
+		if ($xml->xpath('/tns:JPK/tns:Naglowek/tns:KodFormularza')[0] != "JPK_VAT") throw new Exception("Błędny typ deklaracji JPK");
+		if ($xml->xpath('/tns:JPK/tns:Naglowek/tns:WariantFormularza')[0] != $this->JPK_VAT_Wariant) throw new Exception("Błędna wersja deklaracji JPK");
+		if ($xml->xpath('/tns:JPK/tns:Naglowek/tns:CelZlozenia')[0] != $this->JPK['PURPOSE']) throw new Exception("Niezgodny cel złożenia deklaracji JPK");
+		if ($xml->xpath('/tns:JPK/tns:Naglowek/tns:DataOd')[0] != ($this->JPK['MONTH'] . "-01")) throw new Exception("Błędna data 'od' deklaracji JPK");
+		if ($xml->xpath('/tns:JPK/tns:Naglowek/tns:DataDo')[0] != date("Y-m-d", strtotime($xml->xpath('/tns:JPK/tns:Naglowek/tns:DataOd')[0] . " + 1 month - 1 day"))) throw new Exception("Błędna data 'do' deklaracji JPK");
+		if ($xml->xpath('/tns:JPK/tns:Naglowek/tns:DomyslnyKodWaluty')[0] != "PLN") throw new Exception("Błędna waluta deklaracji JPK");
+		if (trim(str_replace('-', '', $xml->xpath('/tns:JPK/tns:Podmiot1/tns:IdentyfikatorPodmiotu/etd:NIP')[0])) != trim(str_replace('-', '', $this->BO['nip']))) throw new Exception("Niezgodny NIP podmiotu w deklaracji JPK");
 		$summaryJPK_VAT = $this->getSummaryJPK_VAT($xml);
 		if ($summaryJPK_VAT["LiczbaWierszySprzedazy"]) {
-			if ($xml->SprzedazCtrl->LiczbaWierszySprzedazy != $summaryJPK_VAT["LiczbaWierszySprzedazy"]) throw new Exception("Brak spójności liczby wierszy sprzedaży w deklaracji JPK");
-			if (((float) $xml->SprzedazCtrl->PodatekNalezny) != $summaryJPK_VAT["PodatekNalezny"])  throw new Exception("Brak spójności wartości podatku należnego w deklaracji JPK");
+			if ($xml->xpath('/tns:JPK/tns:SprzedazCtrl/tns:LiczbaWierszySprzedazy')[0] != $summaryJPK_VAT["LiczbaWierszySprzedazy"]) throw new Exception("Brak spójności liczby wierszy sprzedaży w deklaracji JPK");
+			if (((float) $xml->xpath('/tns:JPK/tns:SprzedazCtrl/tns:PodatekNalezny')[0]) != $summaryJPK_VAT["PodatekNalezny"]) throw new Exception("Brak spójności wartości podatku należnego w deklaracji JPK");
 		}
 		if ($summaryJPK_VAT["LiczbaWierszyZakupow"]) {
-			if ($xml->ZakupCtrl->LiczbaWierszyZakupow != $summaryJPK_VAT["LiczbaWierszyZakupow"]) throw new Exception("Brak spójności liczby wierszy zakupów w deklaracji JPK");
-			if (((float) $xml->ZakupCtrl->PodatekNaliczony) != $summaryJPK_VAT["PodatekNaliczony"])  throw new Exception("Brak spójności wartości podatku naliczonego w deklaracji JPK");
+			if ($xml->xpath('/tns:JPK/tns:ZakupCtrl/tns:LiczbaWierszyZakupow')[0] != $summaryJPK_VAT["LiczbaWierszyZakupow"]) throw new Exception("Brak spójności liczby wierszy zakupów w deklaracji JPK");
+			if (((float) $xml->xpath('/tns:JPK/tns:ZakupCtrl/tns:PodatekNaliczony')[0]) != $summaryJPK_VAT["PodatekNaliczony"]) throw new Exception("Brak spójności wartości podatku naliczonego w deklaracji JPK");
 		}
 
-		function HandleXmlError($errno, $errstr, $errfile, $errline) {
+		set_error_handler(function ($errno, $errstr, $errfile, $errline) {
 			if ($errno==E_WARNING && (substr_count($errstr,"DOMDocument::loadXML()")>0)) throw new DOMException($errstr);
 			else return false;
-		}
+		});
 
-		set_error_handler('HandleXmlError');
 		try {
 			$dom = new DOMDocument();
 			$dom->preserveWhiteSpace = false;
@@ -491,10 +518,16 @@ format: 'YYYY-MM'
 		} catch (Exception $e) {
 			throw new Exception("Błąd parsowania pliku JPK");
 		}
+
 		restore_error_handler();
 
+		$xsd = [
+			2 => 'Schemat_JPK_VAT(2)_v1-0.xsd',
+			3 => 'Schemat_JPK_VAT(3)_v1-1.xsd',
+		];
+
 		libxml_use_internal_errors(true);
-		if (!$dom->schemaValidate(APP_PATH_SCHEMA . "/jpk/Schemat_JPK_VAT(2)_v1-0.xsd")) {
+		if (!$dom->schemaValidate(APP_PATH_SCHEMA . "/jpk/" . $xsd[$this->JPK_VAT_Wariant])) {
 			$errors = '';
 			foreach (libxml_get_errors() as $libxml_error) $errors .= "<br/>{$libxml_error->message}";
 			throw new Exception("Plik JPK niezgodny ze schematem XSD{$errors}<pre>" . htmlentities($dom->saveXML()) . "</pre>");
@@ -508,6 +541,7 @@ format: 'YYYY-MM'
 			if ($_FILES['IN_INSERT']['type'] != "text/xml") throw new Exception("Błędny plik JPK");
 			$xml = simplexml_load_file($_FILES['IN_INSERT']['tmp_name']);
 			$this->validateJPK_VAT($xml);
+
 			if ($this->JPK['IN_L1']) $info = "Zaimportowano dane z Insert i L1";
 			else $info = "Zaimportowano dane z Insert";
 
@@ -764,11 +798,11 @@ EOT;
 
 			ksort($data);
 
-			function addCdata(&$node, $value) {
+			$addCdata = function(&$node, $value) {
 				$dom_node = dom_import_simplexml($node);
 				$dom_owner = $dom_node->ownerDocument;
 				$dom_node->appendChild($dom_owner->createCDATASection($value));
-			}
+			};
 
 			$i = 0;
 			foreach ($data as $pos) {
@@ -779,7 +813,7 @@ EOT;
 				ksort($pos[1]);
 				foreach ($pos as $subpos) foreach ($subpos as $key => $value) {
 					$x->addChild($key, $value);
-					if (!strlen($x->$key)) addCdata($x->$key, $value);
+					if (!strlen($x->$key)) $addCdata($x->$key, $value);
 				}
 			}
 			if ($this->NIPerrors) throw new Exception("Błędne numery NIP:<br/>" . implode("<br/>", $this->NIPerrors));
@@ -843,6 +877,19 @@ EOT;
 		}
 	}
 
+	private static function removeTnsNamespace(&$xml) {
+		$ns = $xml->getNamespaces(true);
+		if (isset($ns[''])) return;
+		if ((!isset($ns['tns']) || isset($ns['xsi']))) throw new Exception("Błędna struktura pliku XML - błędne namespace");
+
+		$dom = dom_import_simplexml($xml);
+		$ns_uri = $dom->lookupNamespaceURI('tns');
+		$dom->removeAttributeNS($ns_uri, 'tns');
+		$xml->addAttribute('xmlns', $ns['tns']);
+		$xml->addAttribute('xmlns:xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
+		$xml = simplexml_load_string($xml->asXML());
+	}
+
 	private function generate_merged_jpk() {
 
 		$xmlSchema = <<<EOT
@@ -850,28 +897,29 @@ EOT;
 <JPK xmlns="http://jpk.mf.gov.pl/wzor/2016/10/26/10261/" xmlns:etd="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2016/01/25/eD/DefinicjeTypy/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 </JPK>
 EOT;
-
-		function xml_append(SimpleXMLElement $to, SimpleXMLElement $from) {
+		$xml_append = function(SimpleXMLElement $to, SimpleXMLElement $from) {
 			$toDom = dom_import_simplexml($to);
 			$fromDom = dom_import_simplexml($from);
 			$toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));
-		}
+		};
 
 		try {
 			if (!($this->JPK['IN_INSERT'] && $this->JPK['IN_L1'])) throw new Exception("Błąd danych");
 
 			$xmlInsert = simplexml_load_string($this->JPK['IN_INSERT']);
+			self::removeTnsNamespace($xmlInsert);
 			$xmlL1 = simplexml_load_string($this->JPK['IN_L1']);
 			$xml = simplexml_load_string($xmlSchema);
+			$tns = self::tns[$this->JPK_VAT_Wariant];
 
 			switch (V::get('SRC_PODMIOT','',$_POST)) {
 				case "Insert":
-					xml_append($xml, $xmlInsert->Naglowek);
-					xml_append($xml, $xmlInsert->Podmiot1);
+					$xml_append($xml, $xmlInsert->children($tns)->Naglowek);
+					$xml_append($xml, $xmlInsert->children($tns)->Podmiot1);
 					break;
 				case "L1":
-					xml_append($xml, $xmlL1->Naglowek);
-					xml_append($xml, $xmlL1->Podmiot1);
+					$xml_append($xml, $xmlL1->children($tns)->Naglowek);
+					$xml_append($xml, $xmlL1->children($tns)->Podmiot1);
 					break;
 				default:
 					throw new Exception("Błąd formularza");
@@ -883,12 +931,12 @@ EOT;
 			foreach ($xmlInsert->SprzedazWiersz as $sprzedazWiersz) {
 				$this->validateNIP($sprzedazWiersz->NrKontrahenta, $sprzedazWiersz->NazwaKontrahenta, "Sprzedaż Insert");
 				$sprzedazWiersz->LpSprzedazy = ++$i;
-				xml_append($xml, $sprzedazWiersz);
+				$xml_append($xml, $sprzedazWiersz);
 			}
 			foreach ($xmlL1->SprzedazWiersz as $sprzedazWiersz) {
 				$this->validateNIP($sprzedazWiersz->NrKontrahenta, $sprzedazWiersz->NazwaKontrahenta, "Sprzedaż L1");
 				$sprzedazWiersz->LpSprzedazy = ++$i;
-				xml_append($xml, $sprzedazWiersz);
+				$xml_append($xml, $sprzedazWiersz);
 			}
 			$xml->addChild("SprzedazCtrl");
 
@@ -896,12 +944,12 @@ EOT;
 			foreach ($xmlInsert->ZakupWiersz as $zakupWiersz) {
 				$this->validateNIP($zakupWiersz->NrDostawcy, $zakupWiersz->NazwaDostawcy, "Zakup Insert");
 				$zakupWiersz->LpZakupu = ++$i;
-				xml_append($xml, $zakupWiersz);
+				$xml_append($xml, $zakupWiersz);
 			}
 			foreach ($xmlL1->ZakupWiersz as $zakupWiersz) {
 				$this->validateNIP($zakupWiersz->NrDostawcy, $zakupWiersz->NazwaDostawcy, "Zakup L1");
 				$zakupWiersz->LpZakupu = ++$i;
-				xml_append($xml, $zakupWiersz);
+				$xml_append($xml, $zakupWiersz);
 			}
 			$xml->addChild("ZakupCtrl");