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

View File

@@ -87,7 +87,7 @@ $pagination_entity_name = "Rechnungen";
<div class="card"> <div class="card">
<div class="card-body"> <div class="card-body">
<div class="row"> <div class="row">
<div class="col-6 border-left"> <div class="col-6">
<h4>Rechungsemails versenden</h4> <h4>Rechungsemails versenden</h4>
<form method="post" action="<?=self::getUrl("PreorderBillingInvoice", "createJob")?>"> <form method="post" action="<?=self::getUrl("PreorderBillingInvoice", "createJob")?>">
<input type="hidden" name="task" value="send-preorder-invoice-email" /> <input type="hidden" name="task" value="send-preorder-invoice-email" />
@@ -124,6 +124,27 @@ $pagination_entity_name = "Rechnungen";
<?php endif; ?> <?php endif; ?>
</form> </form>
</div> </div>
<div class="col-6 border-left">
<h4>BMD-Export</h4>
<h5>Personenkonten</h5>
<div class="row col">
<a class="btn btn-purple" href="<?=self::getUrl("PreorderBillingInvoice", "bmdExport", ["type" => "addresses"])?>"><i class="fas fa-fw fa-users"></i> Personenexport erstellen</a>
</div>
<div class="row col">
<small>Letzter Export: <?=($bmd_export["person"]) ? date("d.m.Y H:i:s", $bmd_export["person"]) : ""?> </small>
</div>
<h5>Rechnungsdaten</h5>
<div class="row col">
<a class="btn btn-purple" href="<?=self::getUrl("PreorderBillingInvoice", "bmdExport", ["type" => "invoice"])?>"><i class="fas fa-fw fa-file-invoice"></i> Rechnungsexport erstellen</a>
</div>
<div class="row col">
<small>Letzter Export: <?=($bmd_export["invoice"]) ? date("d.m.Y H:i:s", $bmd_export["invoice"]) : ""?></small>
</div>
</div>
</div> </div>
@@ -152,7 +173,7 @@ $pagination_entity_name = "Rechnungen";
<th>Netto</th> <th>Netto</th>
<th>Ust.</th> <th>Ust.</th>
<th>Brutto</th> <th>Brutto</th>
<th>Zustellung</th> <th>Versendet</th>
<th>Erstellt</th> <th>Erstellt</th>
</tr> </tr>
<?php foreach($invoices as $invoice): ?> <?php foreach($invoices as $invoice): ?>

View File

@@ -141,7 +141,7 @@ class PreorderBillingController extends mfBaseController {
$now_day = date("d"); $now_day = date("d");
$today = new DateTime("$now_year-$now_month-$now_day"); $today = new DateTime("$now_year-$now_month-$now_day");
//$today = new DateTime("2025-04-13"); //$today = new DateTime("2025-03-13");
$today->setTime(2,0,0); $today->setTime(2,0,0);
$today->setTimezone(new DateTimeZone("Europe/Vienna")); $today->setTimezone(new DateTimeZone("Europe/Vienna"));
@@ -380,7 +380,7 @@ class PreorderBillingController extends mfBaseController {
// Endkunde Setup Gebühr // Endkunde Setup Gebühr
if($status_244_change_date < $earliest_bill_date) { if($status_244_change_date < $earliest_bill_date) {
$this->log->debug(__METHOD__.": Not billing enduser_setup for preorder ".$preorder->id." because status change date ".$status_244_change_date->format("Y-m-d")." is before earliest_bill_date ".$earliest_bill_date->format("Y-m-d")); $this->log->debug(__METHOD__.": Not billing enduser_setup for preorder ".$preorder->id." because status 244 change date ".$status_244_change_date->format("Y-m-d")." is before earliest_bill_date ".$earliest_bill_date->format("Y-m-d"));
return true; return true;
} }
@@ -448,7 +448,7 @@ class PreorderBillingController extends mfBaseController {
$billing_data["end_date"] = $status_change_date->format("Y-m-d"); $billing_data["end_date"] = $status_change_date->format("Y-m-d");
} }
if(!$status_change_date) { if(!$change_to_active) {
$status_change_date = $status_244_change_date; $status_change_date = $status_244_change_date;
} }
@@ -481,10 +481,6 @@ class PreorderBillingController extends mfBaseController {
die("Unknown billing type $type"); die("Unknown billing type $type");
} }
if(!$billing_data["unit"]) {
var_dump($billing_data);exit;
}
$billing = PreorderBilling::create($billing_data); $billing = PreorderBilling::create($billing_data);
if(!$billing->save()) { if(!$billing->save()) {
@@ -568,7 +564,7 @@ class PreorderBillingController extends mfBaseController {
} }
if($status_change_date->format("Ymd") > $latest_bill_date->format("Ymd")) { if($status_change_date->format("Ymd") > $latest_bill_date->format("Ymd")) {
$this->log->debug(__METHOD__.": Skipping setup for preorder ".$preorder->id." because billing date (status change date) ".$status_change_date->format("Y-m-d")." is after latest_bill_date ".$latest_bill_date->format("Y-m-d")); $this->log->debug(__METHOD__.": Skipping usage for preorder ".$preorder->id." because billing date (status change date) ".$status_change_date->format("Y-m-d")." is after latest_bill_date ".$latest_bill_date->format("Y-m-d"));
return true; return true;
} }
@@ -722,6 +718,7 @@ class PreorderBillingController extends mfBaseController {
"preorder_id" => $preorder->id, "preorder_id" => $preorder->id,
"oaid" => $preorder->oaid, "oaid" => $preorder->oaid,
"adb_wohneinheit_id" => $preorder->adb_wohneinheit_id, "adb_wohneinheit_id" => $preorder->adb_wohneinheit_id,
"extref" => ($preorder->extref) ?: null,
"order_date" => $order_date->format("Y-m-d"), "order_date" => $order_date->format("Y-m-d"),
"start_date" => $start_date->format("Y-m-d"), "start_date" => $start_date->format("Y-m-d"),
"end_date" => $end_date->format("Y-m-d"), "end_date" => $end_date->format("Y-m-d"),

View File

@@ -145,8 +145,8 @@ class PreorderBillingCustomer extends mfBaseModel
$where = self::getSqlFilter($filter); $where = self::getSqlFilter($filter);
$sql = "SELECT COUNT(*) as cnt FROM PreorderBillingCustomer $sql = "SELECT COUNT(*) as cnt FROM PreorderBillingCustomer
WHERE $where"; WHERE $where";
//var_dump($filter);exit;
//mfLoghandler::singleton()->debug($sql); mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql); $res = $db->query($sql);
if($db->num_rows($res)) { if($db->num_rows($res)) {
@@ -276,6 +276,20 @@ class PreorderBillingCustomer extends mfBaseModel
} }
} }
if(array_key_exists("edit", $filter)) {
$edit = $filter["edit"];
if(is_numeric($edit)) {
$where .= " AND edit=$edit";
}
}
if(array_key_exists("edit>", $filter)) {
$edit = $filter["edit>"];
if(is_numeric($edit)) {
$where .= " AND edit > $edit";
}
}
if(array_key_exists("add-where", $filter)) { if(array_key_exists("add-where", $filter)) {
$where .= " ".$filter['add-where']; $where .= " ".$filter['add-where'];

View File

@@ -575,6 +575,17 @@ RML Infrastruktur GmbH";
} }
} }
if(array_key_exists("bmd_export_date", $filter)) {
$bmd_export_date = $filter['bmd_export_date'];
if($bmd_export_date === null || $bmd_export_date === false) {
$where .= " AND (PreorderBillingInvoice.bmd_export_date IS NULL OR PreorderBillingInvoice.bmd_export_date=0)";
} elseif($bmd_export_date === true) {
$where .= " AND (PreorderBillingInvoice.bmd_export_date IS NOT NULL AND PreorderBillingInvoice.bmd_export_date > 0)";
} elseif(is_numeric($bmd_export_date)) {
$where .= " AND PreorderBillingInvoice.bmd_export_date=$bmd_export_date";
}
}
if(array_key_exists("add-where", $filter)) { if(array_key_exists("add-where", $filter)) {
$where .= " ".$filter['add-where']; $where .= " ".$filter['add-where'];
} }

View File

@@ -58,11 +58,55 @@ class PreorderBillingInvoiceController extends mfBaseController {
$email_jobs = InvoiceJobModel::search(["task" => "send-preorder-invoice-email", "to_date>=" => $now->format("Y-m-d")]); $email_jobs = InvoiceJobModel::search(["task" => "send-preorder-invoice-email", "to_date>=" => $now->format("Y-m-d")]);
$this->layout()->set("email_jobs", $email_jobs); $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) { private function getPreparedFilter($filter) {
$new_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(array_key_exists("start_date_from", $filter)) {
if($filter["start_date_from"]) { if($filter["start_date_from"]) {
try { try {
@@ -679,6 +723,207 @@ class PreorderBillingInvoiceController extends mfBaseController {
return ["sent" => $sent, "defer" => $defer]; 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() { protected function apiAction() {
$do = $this->request->do; $do = $this->request->do;
$data = []; $data = [];