2025-05-12 15:44:39 +00:00
< ? php
class PurchaseCreator {
private $db ;
public $kontrahenci ;
public function __construct ( $date ){
$this -> db = $GLOBALS [ 'db' ];
$date = new DateTime ( $date );
$this -> date_from = $date -> format ( " Y-m-01 " );
$this -> date_to = $date -> format ( " Y-m-t " );
$this -> kontrahenci = [];
}
public function getPurchases ( $type ){
2025-07-10 08:04:15 +00:00
$query = " select * from documents where document_date>=' " . $this -> date_from . " ' and document_date<=' " . $this -> date_to . " ' and category_id in ('invoice','compain_note','return_note') and deleted=0 " ;
2025-05-12 15:44:39 +00:00
if ( $type == " new " ) {
$query .= " and exported = 0 " ;
}
$rs = $this -> db -> query ( $query );
global $app_list_strings ;
$documents = array ();
while ( $dane = $this -> db -> fetchByAssoc ( $rs )){
$dokument = new Dokument ();
$kontrahent = new Kontrahent ();
if ( $dane [ 'corrected_document_name' ] != " " ){
$dokument -> typ_dokumentu = ( string ) 'KFZ' ;
} else {
$dokument -> typ_dokumentu = ( string ) 'FZ' ;
}
$dokument -> status_dokumentu = 1 ;
$dokument -> status_rejestracji_fiskalnej = 0 ;
$dokument -> numer_dokumentu = 0 ;
$dokument -> numer_dokumentu_dostawcy = str_replace ( " Faktura VAT nr " , " " , $dane [ 'document_name' ]);
$dokument -> rozszerzenie_numeru = ( string ) " FZ " ;
$dokument -> pelny_numer_dokumentu = str_replace ( " Faktura VAT nr " , " " , $dane [ 'document_name' ]);
if ( $dane [ 'corrected_document_name' ] != " " ){
$dokument -> numer_dokumentu_korygowanego = $dane [ 'corrected_document_name' ];
$dokument -> data_wystawienia_korekty = date ( " Ymd000000 " , strtotime ( $dane [ 'document_date' ]));
} else {
$dokument -> numer_dokumentu_korygowanego = null ;
$dokument -> data_wystawienia_korekty = null ;
}
$dokument -> numer_zamowienia = null ;
$dokument -> magazyn_docelowy = null ;
$value = new Document ();
$value -> retrieve ( $dane [ 'id' ]);
$relations = $value -> getParentList ( true );
$a = new Account ();
$a -> retrieve ( $relations [ 0 ][ 'parent_id' ]);
$a -> name = str_replace ( " \r \n " , " " , $a -> name );
$dokument -> kod_identyfikacyjny_kontrahenta = $a -> index_dbf ;
$dokument -> nazwa_skrocona = mb_substr ( $a -> name , 0 , 40 , 'UTF-8' );
$dokument -> nazwa_pelna = str_replace ( " \r \n " , " " , $a -> name );
$dokument -> miasto = $a -> billing_address_city ;
$dokument -> kod_pocztowy = $a -> billing_address_postalcode ;
$dokument -> ulica_i_numer = $a -> billing_address_street ;
$dokument -> nip = $a -> to_vatid ;
$dokument -> kategoria = null ;
$dokument -> podtytul = null ;
$kontrahent -> typ_kontrahenta = ( float ) 0 ;
$kontrahent -> kod_identyfikacyjny = $a -> index_dbf ;
$kontrahent -> nazwa_skrocona = mb_substr ( $a -> name , 0 , 40 , 'UTF-8' );
$kontrahent -> nazwa_pelna = str_replace ( " \r \n " , " " , $a -> name );
$kontrahent -> miasto = $a -> billing_address_city ;
$kontrahent -> kod_pocztowy = $a - billing_address_postalcode ;
$kontrahent -> ulica_i_adres = $a -> billing_address_street ;
$kontrahent -> nip = $a -> to_vatid ;
if ( $a -> invoice_type == 'U' ){
$kontrahent -> prefix = mb_substr ( $a -> to_vatid , 0 , 2 , 'UTF-8' );
$kontrahent -> czy_unijny = 1 ;
} else {
$kontrahent -> czy_unijny = 0 ;
}
$this -> kontrahenci [ $a -> id ] = $kontrahent ;
$dokument -> data_wystawienia = date ( " Ymd000000 " , strtotime ( $dane [ 'document_date' ]));
$dokument -> data_sprzedazy = date ( " Ymd000000 " , strtotime ( $dane [ 'document_date' ]));
$dokument -> data_otrzymania = date ( " Ymd000000 " , strtotime ( $dane [ 'document_date' ]));
$sumQuery = $this -> db -> query ( " select ROUND(sum(netto),2) AS total_netto,ROUND(sum(vat),2) AS total_vat,ROUND(sum(vat+netto),2) AS total_brutto from documents_vat WHERE document_id=' " . $dane [ 'id' ] . " ' and netto!='NaN' and vat!='NaN' and netto!=0 and deleted=0 " );
$sumRes = $this -> db -> fetchByAssoc ( $sumQuery );
$dokument -> czy_dokumenty_wystawian_wg_ceny_netto = 0 ;
$dokument -> wartosc_netto = ( float ) $sumRes [ 'total_netto' ];
$dokument -> wartosc_vat = ( float ) $sumRes [ 'total_vat' ];
$dokument -> wartosc_brutto = ( float ) $sumRes [ 'total_brutto' ];
$dokument -> koszt = ( float ) $dane [ 'value' ];
if ( $dane [ 'payment_date' ] != " " ){
$dokument -> rabat_format_platnosci = 'przelew' ;
$dokument -> rabat_termin_platnosci = date ( " Ymd000000 " , strtotime ( $dane [ 'payment_date' ]));
} else {
$dokument -> rabat_format_platnosci = 'gotówka' ;
$dokument -> rabat_termin_platnosci = date ( " Ymd000000 " , strtotime ( $dane [ 'document_date' ]));
}
if ( $dane [ 'left_paid' ] != 0 ){
$dokument -> kwota_zaplacona_przy_odbiorze = ( float ) $dane [ 'left_paid' ];;
$dokument -> wartosc_do_zaplaty = ( float ) round ( $dane [ 'value' ], 2 );
} else {
$dokument -> kwota_zaplacona_przy_odbiorze = ( float ) $dane [ 'value' ];
$dokument -> wartosc_do_zaplaty = ( float ) $dane [ 'value' ];
}
$dokument -> zaokraglenie_wartosci_do_zaplaty = 0 ;
$dokument -> zaokraglenie_wartosci_vat = 0 ;
$dokument -> automatyczne_zaokgraglanie = 0 ;
$dokument -> statusy_rozszerzone_i_specjalne = 0 ;
$u = new User ();
$u -> retrieve ( $dane [ 'assigned_user_id' ]);
$dokument -> nazwisko_i_imie_wystawcy = $u -> full_name ;
if ( $dane [ 'currency_id' ] == " PLN " ){
$dokument -> waluta = 'PLN' ;
} else {
$cur = new Currency ();
$cur -> retrieve ( $dane [ 'currency_id' ]);
$dokument -> waluta = $cur -> name ;
$dokument -> kurs_waluty = ( float ) $dane [ 'currency_value' ];
}
$dokument -> uwagi = null ;
$dokument -> import_dokumentu = 0 ;
if ( $dane [ 'document_type' ] == 'k' ){
$dokument -> rodzaj_transakcji = 0 ;
} else {
if ( $dane [ 'document_type' ] == 'u' ){
$dokument -> rodzaj_transakcji = 2 ;
} else {
$dokument -> rodzaj_transakcji = 1 ;
}
}
$dokument -> panstwo_kontrahenta = $a -> billing_address_country ;
if ( $a -> invoice_type == 'U' ){
$dokument -> prefix_panstwa_ue = mb_substr ( $a -> to_vatid , 0 , 2 , 'UTF-8' );
$dokument -> czy_kontrahent_unijny = 1 ;
}
$dokument -> setPozycjeObiekt ( $this -> getDocumentPositions ( $dane [ 'id' ], $dokument ));
$documents [] = $dokument ;
$query = " update documents set exported = 1 where id=' " . $dane [ 'id' ] . " ' " ;
$this -> db -> query ( $query );
}
return $documents ;
}
public function getDocumentPositions ( $id , & $doc ){
$query = " select * from documents_vat WHERE document_id=' " . $id . " ' and netto!='NaN' and vat!='NaN' and netto!=0 and deleted=0 " ;
$rr = $this -> db -> query ( $query );
$pozycje = [];
while ( $dane = $this -> db -> fetchByAssoc ( $rr )){
$pozycja = new Pozycja ;
$pozycja -> symbol_stawki_vat = $dane [ 'vat_id' ];
$pozycja -> wysokosc_stawki_vat_w_procentach = floatval ( $dane [ 'vat_id' ] . '.00' );
if ( $doc -> numer_dokumentu_korygowanego != '' ) {
$pozycja -> wartosc_netto = ( float ) $dane [ 'netto' ];
$pozycja -> wartosc_vat = ( float ) $dane [ 'vat' ];
$pozycja -> wartosc_brutto = ( float ) round ( $dane [ 'netto' ] + $dane [ 'vat' ], 2 );
} else {
$pozycja -> wartosc_netto = ( float ) $dane [ 'netto' ];
$pozycja -> wartosc_vat = ( float ) $dane [ 'vat' ];
$pozycja -> wartosc_brutto = ( float ) round ( $dane [ 'netto' ] + $dane [ 'vat' ], 2 );
}
$pozycja -> wartosc_nabycia = ( float ) round ( $dane [ 'netto' ] + $dane [ 'vat' ], 2 );
$pozycje [] = $pozycja ;
}
$doc -> pozycje = count ( $pozycja );
return $pozycje ;
}
}
?>