WIP ManualInvoice BMD Export

This commit is contained in:
Frank Schubert
2026-01-20 21:18:23 +01:00
parent e3f209c04b
commit 7997d19fc1
2 changed files with 250 additions and 3 deletions

View File

@@ -780,6 +780,207 @@ class InvoiceController extends mfBaseController {
}
protected function manualExportBmd() {
if(!$this->me->can("Billing")) {
$this->redirect("Dashboard");
}
//var_dump($this->request->get());
$csv_header = "\u{FEFF}satzart;konto;gkonto;belegnr;belegdatum;zziel;skontopz;skontotage;buchsymbol;buchcode;prozent;steuercode;betrag;steuer;text;";
$csv_header .= "bank-iban-nr;bank-swiftcode;bank-mandatsid;bank-mandatsdatum;bank-mandatskz;bank-letztereinzug;zvsperre;bankeinzug;";
$csv_header .= "kost;kobetrag";
$csv_out = "";
//var_dump($filter);exit;
$filter = [
"lock" => 1,
"exported" => 0,
];
if($this->request->manual_invoice_date_from) {
$date_from = Layout::dateToInt($this->request->manual_invoice_date_from);
if($date_from) {
$filter["invoice_date"] = ["from" => $date_from];
}
}
if($this->request->manual_invoice_date_to) {
$date_to = Layout::dateToInt($this->request->manual_invoice_date_to);
if($date_to) {
$filter["invoice_date"] = ["to" => $date_to];
}
}
//var_dump($filter);exit;
if(!ManualInvoiceModel::count($filter)) {
$this->layout()->setFlash("Keine Rechnungen zum Exportieren gefunden.");
$this->redirect("Invoice");
}
foreach(ManualInvoiceModel::getAll($filter) as $invoice) {
if($invoice->exported) {
die("wtf");
}
$billingaddress = new Address($invoice->billingaddress_id);
if(!$billingaddress->id) {
die("Billingaddresse für Rechnung {$invoice->invoice_number} not found");
}
$kostentraeger = [];
//var_dump($invoice->getProperty("positions"));
//$vat_total_gross = 0;
foreach($invoice->getProperty("positions") as $position) {
if(!array_key_exists($position->position_group, $kostentraeger)) {
$kostentraeger[$position->position_group] = 0;
}
//$kostentraeger[$position->position_group] += $position->price_gross;
//$vat_total_gross += $position->price_gross - $position->price_total;
$price = $position->price_total;
/*if($position->discount) {
$price -= ($price / 100) * $position->discount;
}*/
if($invoice->gesamtrabatt) {
$price -= ($price / 100) * $invoice->gesamtrabatt;
}
$kostentraeger[$position->position_group] += $price;
}
$total_gross = $invoice->total_gross;
/*if($invoice->gesamtrabatt) {
$total_gross -= round(($total_gross / 100) * $invoice->gesamtrabatt, 4);
}*/
$total = $invoice->total;
/*if($invoice->gesamtrabatt) {
$total -= round(($total / 100) * $invoice->gesamtrabatt, 4);
}*/
if($invoice->total_gross) {
$vatrate = 20;
}
if($invoice->total == $invoice->total_gross && $invoice->fibu_cost_area != "domestic") {
$vatrate = "0";
} else {
$vatrate = "20";
}
$vat = $total_gross - $total;
$vat *= -1;
//$vat_total_gross *= -1;
if($invoice->total < 0) {
$buchsymbol = "GU";
} else {
$buchsymbol = "AR";
}
$fibu_account = $invoice->fibu_account_number;
$buchungstext = "[".$invoice->customer_number."]";
if($invoice->company) {
$buchungstext .= " ".$invoice->company;
} elseif($invoice->firstname || $invoice->lastname) {
$buchungstext .= " ".$invoice->firstname." ".$invoice->lastname;
}
$buchungstext = str_replace(["\n","\r", ";"], "", $buchungstext);
$buchcode = "1";
$is_sepa = ($invoice->billing_type == "sepa");
$iban = "";
$bic = "";
$sepa_id = "";
$sepa_date = false;
$last_invoice_date = false;
$mandatskz = "";
if($is_sepa) {
$iban = $invoice->bank_account_iban;
$bic = $invoice->bank_account_bic;
$sepa_id = "R".$fibu_account;
if($billingaddress->sepa_date) {
$sepa_date = new DateTime("@".$billingaddress->sepa_date);
$sepa_date->setTimezone(new DateTimeZone("Europe/Vienna"));
if($billingaddress->last_invoice_date) {
$sepa_last_date = new DateTime("@".$billingaddress->last_invoice_date);
$data["sepa_last_date"] = $sepa_last_date->format("Y-m-d");
$last_invoice_date = new DateTime("@".$billingaddress->last_invoice_date);
$last_invoice_date->setTimezone(new DateTimeZone("Europe/Vienna"));
if($last_invoice_date->format("Y-m-d") < $sepa_date->format("Y-m-d")) {
$last_invoice_date = false;
}
}
}
$mandatskz = ($last_invoice_date ? "1" : "0");
$three_years_ago = new DateTime("now");
$three_years_ago->modify("-3 years");
if($mandatskz == "0") {
while($sepa_date->format("Y-m-d") < $three_years_ago->format("Y-m-d")) {
$sepa_date->modify("+1 year");
}
}
}
$kost = $invoice->fibu_cost_account;
$csv_out .= "0;";
$csv_out .= $fibu_account.";";
$csv_out .= $invoice->fibu_cost_account.";";
$csv_out .= $invoice->invoice_number.";";
$csv_out .= date("d.m.Y", $invoice->invoice_date).";";
$csv_out .= ($invoice->fibu_payment_due === null) ? ";" : $invoice->fibu_payment_due.";";
$csv_out .= ($invoice->fibu_payment_skonto) ? $invoice->fibu_payment_skonto.";" : ";";
$csv_out .= ($invoice->fibu_payment_skonto_rate) ? $invoice->fibu_payment_skonto_rate.";" : ";";
$csv_out .= $buchsymbol.";";
$csv_out .= $buchcode.";";
$csv_out .= $vatrate.";";
$csv_out .= $invoice->fibu_taxcode.";";
$csv_out .= number_format($total_gross, 2, ",", "").";";
$csv_out .= number_format($vat, 2, ",", "").";";
$csv_out .= $buchungstext.";";
$csv_out .= $iban.";";
$csv_out .= $bic.";";
$csv_out .= $sepa_id.";";
$csv_out .= ($sepa_date ? $sepa_date->format("d.m.Y") : "").";";
$csv_out .= $mandatskz.";";
$csv_out .= ($last_invoice_date ? $last_invoice_date->format("d.m.Y") : "").";";
$csv_out .= ($is_sepa ? 0 : 10).";";
$csv_out .= ($is_sepa ? 1 : 0);
if(count($kostentraeger) >= 2) {
foreach($kostentraeger as $kostelle => $kobetrag) {
$kobetrag_text = number_format($kobetrag, 2, ",", "");
$csv_out .= "\n1;;;;;;;;;;;;;;;;;;;;;;;$kostelle;$kobetrag_text;";
}
}
///var_dump($kostentraeger);
$csv_out .= "\n";
}
//exit;
/*$this->layout()->setFlash("Export erfolgreich abgeschlossen", "success");
$this->redirect("Invoice");*/
header("Content-type: text/csv; charset=utf-8");
header('Content-disposition: attachment; filename="tt-mrech-export-bmd-'.date('Y-m-d_H-i-s').'.csv"');
echo $csv_header."\n".$csv_out;
exit;
}
protected function exportBmdAction() {
if(!$this->me->can("Billing")) {
$this->redirect("Dashboard");