PreorderBilling BMD Expot

This commit is contained in:
Frank Schubert
2025-04-10 13:26:56 +02:00
parent 4bc1a0c97b
commit 653b28aa5c
5 changed files with 300 additions and 12 deletions
@@ -58,11 +58,55 @@ class PreorderBillingInvoiceController extends mfBaseController {
$email_jobs = InvoiceJobModel::search(["task" => "send-preorder-invoice-email", "to_date>=" => $now->format("Y-m-d")]);
$this->layout()->set("email_jobs", $email_jobs);
$netoperators = [];
foreach(PreordercampaignModel::search(["owner_id" => $this->me->address_id]) as $campaign) {
foreach($campaign->active_operators as $op) {
if(!array_key_exists($op->operator_id, $netoperators)) {
$nop = new Address($op->operator_id);
if($nop->id) {
$netoperators[$nop->id] = $nop;
}
}
}
foreach($campaign->passive_operators as $op) {
if(!array_key_exists($op->operator_id, $netoperators)) {
$nop = new Address($op->operator_id);
if($nop->id) {
$netoperators[$nop->id] = $nop;
}
}
}
}
$this->layout()->set("netoperators", $netoperators);
// get last BMD export dates
$bmd_export_date_invoice = new mfConfig("preorder.invoice.bmd.export.invoice.ts.".$this->me->address_id);
$bmd_export_date_persion = new mfConfig("preorder.invoice.bmd.export.person.ts.".$this->me->address_id);
$bmd_export = [
"invoice" => $bmd_export_date_invoice->value(),
"person" => $bmd_export_date_persion->value(),
];
$this->layout()->set("bmd_export", $bmd_export);
}
private function getPreparedFilter($filter) {
$new_filter = [];
if(array_key_exists("name", $filter)) {
if (array_key_exists("name", $filter)) {
if (array_key_exists("name", $filter) && $filter["name"]) {
$kunde = $this->db()->escape(trim($filter['name']));
if (!array_key_exists("add-where", $new_filter)) $new_filter["add-where"] = "";
$new_filter['add-where'] .= " AND (company like '%$kunde%' OR firstname like '%$kunde%' OR lastname like '%$kunde%' OR concat(firstname, ' ', lastname) like '%$kunde%' OR concat(lastname, ' ', firstname) like '%$kunde%')";
}
}
}
if(array_key_exists("start_date_from", $filter)) {
if($filter["start_date_from"]) {
try {
@@ -679,6 +723,207 @@ class PreorderBillingInvoiceController extends mfBaseController {
return ["sent" => $sent, "defer" => $defer];
}
protected function bmdExportPerson() {
$netowner_id = $this->me->address_id;
$netowner_config = TT_PREORDER_BILLING[$netowner_id];
if(!$netowner_config) {
$this->layout()->setFlash("Interner Fehler", "error");
$this->redirect("PreorderBillingInvoice");
}
$last_export = new mfConfig("preorder.invoice.bmd.export.person.ts.".$this->me->address_id);
$last_export_date = $last_export->value();
$csv = "\u{FEFF}Konto;Vorname;Nachname;Straße;Postleitzahl;Ort;Land;Telefon;Mail;UID\n";
if(!PreorderBillingCustomer::count(["edit>" => $last_export_date])) {
$this->layout()->setFlash("Keine neuen Personendaten zum Exportieren vorhanden", "info");
$this->redirect("PreorderBillingInvoice");
}
foreach(PreorderBillingCustomer::search(["edit>" => $last_export_date]) as $person) {
$konto = $person->fibu_account_number;
if($person->company) {
$vorname = "";
$nachname = $person->company;
} else {
$vorname = $person->firstname;
$nachname = $person->lastname;
}
$strasse = $person->street;
$plz = $person->zip;
$ort = $person->city;
$land = $person->country;
$telefon = $person->phone;
$mail = $person->email;
$uid = $person->uid;
if($konto) {
$csv .= "$konto;$vorname;$nachname;$strasse;$plz;$ort;$land;$telefon;$mail;$uid\n";
}
}
$subfolder = $netowner_config["subfolder"];
$today = new DateTime();
$csv_path = MFUPLOAD_FILE_SAVE_PATH."/$subfolder";
if(!file_exists($csv_path)) {
mkdir($csv_path, 0777, true);
}
$filename = "thetool-bmd-person-export-".$today->format("Y-m-d_H-i-s").".csv";
$filepath = "$csv_path/$filename";
if(file_put_contents($filepath, $csv) === false) {
$this->layout()->setFlash("Fehler beim Erstellen des BMD-Exports", "error");
$this->redirect("PreorderBillingInvoice");
}
$file = FileModel::create([
"name" => $filename,
"filename" => $filename,
"subfolder" => $subfolder,
"store_filename" => $filename,
"orig_filename" => $filename,
]);
$file->save();
// copy csv file to bmd export transfer directory
$transfer_path = $netowner_config["bmd-export-transfer-path"];
$transfer_file = "$transfer_path/$filename";
$this->log->debug(__METHOD__.": Copying $filepath to $transfer_file");
if(!copy($filepath, $transfer_file)) {
$this->layout()->setFlash("Fehler beim Kopieren des BMD-Exports", "error");
$this->redirect("PreorderBillingInvoice");
}
$last_export->value(date("U"));
$last_export->save();
$this->layout()->setFlash("Personenexport erfolgreich erstellt", "success");
$this->redirect("PreorderBillingInvoice");
}
protected function bmdExport() {
$type = $this->request->type;
$netowner_id = $this->me->address_id;
$netowner_config = TT_PREORDER_BILLING[$netowner_id];
if(!$netowner_config) {
$this->layout()->setFlash("Interner Fehler", "error");
$this->redirect("PreorderBillingInvoice");
}
if($type != "addresses" && $type != "invoice") {
$this->layout()->setFlash("Ungültiger Export-Typ", "error");
$this->redirect("PreorderBillingInvoice");
}
if($type == "addresses") {
return $this->bmdExportPerson();
}
$satzart = 0;
$buchsymbol = "ABR";
$buchcode = "1";
$prozent = 20;
$steuercode = 1;
$csv = "\u{FEFF}satzart;konto;gkonto;belegnr;belegdatum;buchsymbol;buchcode;prozent;steuercode;betrag;steuer;text;kost;kotraeger;kobetrag\n";
$invoices = [];
if(!PreorderBillingInvoice::count(["netowner_id" => $netowner_id, "bmd_export_date" => null])) {
$this->layout()->setFlash("Keine neuen Rechnungen zum exportieren vorhanden", "info");
$this->redirect("PreorderBillingInvoice");
}
foreach(PreorderBillingInvoice::search(["netowner_id" => $netowner_id, "bmd_export_date" => null]) as $invoice) {
$invoice->bmd_export_date = date("U");
$invoices[] = $invoice;
$kostentraeger = [];
foreach($invoice->positions as $position) {
if(!array_key_exists($position->article_number, $kostentraeger)) {
$kostentraeger[$position->article_number] = 0;
}
$kostentraeger[$position->article_number] += $position->price_gross;
}
$invoice_date = new DateTime($invoice->invoice_date);
$konto = $invoice->customer_number;
$gkonto = $invoice->fibu_revenue_code;
$belegnr = $invoice->invoice_number;
$belegdatum = $invoice_date->format("d.m.Y");
$betrag = number_format($invoice->total_gross, 2, ",", "");
$steuer = number_format(($invoice->total_gross - $invoice->total) * -1, 2, ",", "");
$text = ($invoice->company ?: $invoice->firstname." ".$invoice->lastname)." $belegnr";
$text = str_replace(["\r","\n", '"'], [" "," ",'""'], $text);
$kost = $invoice->fibu_cost_account;
if(count($kostentraeger) < 2) {
$csv_line = "$satzart;$konto;$gkonto;$belegnr;$belegdatum;$buchsymbol;$buchcode;$prozent;$steuercode;$betrag;$steuer;\"$text\";$kost";
} else {
$csv_line = "$satzart;$konto;$gkonto;$belegnr;$belegdatum;$buchsymbol;$buchcode;$prozent;$steuercode;$betrag;$steuer;\"$text\";;";
foreach($kostentraeger as $kotraeger => $kobetrag) {
$kobetrag_text = number_format($kobetrag, 2, ",", "");
$csv_line .= "\n1;;;;;;;;;;;;$kost;$kotraeger;$kobetrag_text";
}
}
$csv .= "$csv_line\n";
}
/*echo $csv;
exit;*/
$subfolder = $netowner_config["subfolder"];
$today = new DateTime();
$csv_path = MFUPLOAD_FILE_SAVE_PATH."/$subfolder";
if(!file_exists($csv_path)) {
mkdir($csv_path, 0777, true);
}
$filename = "thetool-bmd-invoice-export-".$today->format("Y-m-d_H-i-s").".csv";
$filepath = "$csv_path/$filename";
if(file_put_contents($filepath, $csv) === false) {
$this->layout()->setFlash("Fehler beim Erstellen des BMD-Exports", "error");
$this->redirect("PreorderBillingInvoice");
}
$file = FileModel::create([
"name" => $filename,
"filename" => $filename,
"subfolder" => $subfolder,
"store_filename" => $filename,
"orig_filename" => $filename,
]);
$file->save();
// copy csv file to bmd export transfer directory
$transfer_path = $netowner_config["bmd-export-transfer-path"];
$transfer_file = "$transfer_path/$filename";
$this->log->debug(__METHOD__.": Copying $filepath to $transfer_file");
if(!copy($filepath, $transfer_file)) {
$this->layout()->setFlash("Fehler beim Kopieren des BMD-Exports", "error");
$this->redirect("PreorderBillingInvoice");
}
$bmd_export_date = new mfConfig("preorder.invoice.bmd.export.invoice.ts.".$this->me->address_id);
$bmd_export_date->value(date("U"));
$bmd_export_date->save();
foreach($invoices as $invoice) {
$invoice->save();
}
$this->layout()->setFlash("Rechnungsexport erfolgreich erstellt", "success");
$this->redirect("PreorderBillingInvoice");
}
protected function apiAction() {
$do = $this->request->do;
$data = [];