needlogin = true; $me = new User(); $me->loadMe(); $this->me = $me; $this->layout()->set("me", $me); if(!$me->can(["preorderbilling", "preorderbillingReadonly"])) { $this->redirect("Dashboard"); } } protected function indexAction() : void { $this->layout()->setTemplate("PreorderBillingInvoice/Index"); if ($this->request->resetFilter) { unset($_SESSION[MFAPPNAME . '-PreorderBillingInvoice-filter']); } $filter = []; if (is_array($this->request->filter)) { $filter = $this->request->filter; $_SESSION[MFAPPNAME . '-PreorderBillingInvoice-filter'] = $filter; } else { if (array_key_exists(MFAPPNAME . '-PreorderBillingInvoice-filter', $_SESSION) && count($_SESSION[MFAPPNAME . '-PreorderBillingInvoice-filter'])) { $filter = $_SESSION[MFAPPNAME . '-PreorderBillingInvoice-filter']; } } $this->layout->set("filter", $filter); $filter = $this->getPreparedFilter($filter); // pagination defaults $pagination = []; $pagination['start'] = 0; $pagination['count'] = 25; $pagination['maxItems'] = 0; if (is_numeric($this->request->s)) { $pagination['start'] = intval($this->request->s); } $filter["netowner_id"] = $this->me->address_id; $pagination['maxItems'] = PreorderBillingInvoice::count($filter); $invoices = PreorderBillingInvoice::search($filter, $pagination); $this->layout()->set("invoices", $invoices); $this->layout()->set("pagination", $pagination); } private function getPreparedFilter($filter) { $new_filter = []; if(array_key_exists("start_date_from", $filter)) { if($filter["start_date_from"]) { try { $from = DateTime::createFromFormat("d.m.Y", $filter["start_date_from"]); } catch (Exception $e) {} $new_filter["start_date>="] = $from->format("Y-m-d"); } unset($filter["start_date_from"]); } if(array_key_exists("start_date_to", $filter)) { if($filter["start_date_to"]) { try { $to = DateTime::createFromFormat("d.m.Y", $filter["start_date_to"]); } catch (Exception $e) {} $new_filter["start_date<="] = $to->format("Y-m-d"); } unset($filter["start_date_to"]); } if (is_array($filter) && count($filter)) { foreach ($filter as $name => $value) { $new_filter[$name] = $value; } } return $new_filter; } protected function downloadInvoice() { $id = $this->request->id; if (!is_numeric($id) || !$id) { $this->layout()->setFlash("Rechnung nicht gefunden", "error"); $this->redirect("Invoice"); } $invoice = new PreorderBillingInvoice($id); if (!$invoice->id) { $this->layout()->setFlash("Rechnung nicht gefunden", "error"); $this->redirect("Invoice"); } $pdf = $invoice->pdf; $pdf = false; //var_dump($pdf, !$pdf);exit; //var_dump($pdf->name);exit; if(!$pdf || !$pdf->name) { $ifile = PreorderBillingInvoiceFile::createFromInvoice($invoice); if(!$ifile) { $this->layout()->setFlash("Fehler beim PDF erstellen"); $this->redirect("PreorderBillingInvoice"); } $pdf = $ifile->file; } $pdf_path = $pdf->getFullPath(); $filename = $pdf->filename; if(!file_exists($pdf_path)) { $this->layout()->setFlash("PDF-Datei nicht gefunden"); $this->redirect("Invoice"); } header('Content-Type: application/octet-stream'); header('Content-disposition: attachment; filename="'.$filename.'"'); header('Content-Transfer-Encoding: binary'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Content-Type: ' . mime_content_type($pdf_path)); header("Content-Length: " . filesize($pdf_path)); readfile($pdf_path); exit; } protected function createAction() { $netowner_id = $this->me->address_id; $today = new DateTime(); $billing_row_count = 0; $positions_count = 0; $bill_id_count = 0; //$products = PreorderProduct::getAll(true); $base_rows = PreorderBilling::getInvoiceBaseData(['invoice_id' => null, "netowner_id" => $netowner_id]); //var_dump($base_rows);exit; $invoice_count = 0; $invoice_detail_data = []; foreach($base_rows as $base) { $preorderbillingcustomer_id = $base["preorderbillingcustomer_id"]; $owner_id = $base["owner_id"]; $billingaddress_id = $base["billingaddress_id"]; $billing_delivery = $base["billing_delivery"]; $bill_positions = []; $credit_positions = []; $invoice_voicenumbers = []; $bill_filter = [ "netowner_id" => $netowner_id, "preorderbillingcustomer_id" => $preorderbillingcustomer_id, "owner_id" => $owner_id, "billingaddress_id" => $billingaddress_id, "billing_delivery" => $billing_delivery, "invoice_id" => null, ]; $billing_rows = PreorderBilling::search($bill_filter); if(!count($billing_rows)) { var_dump($bill_filter); die("Keine Billing rows"); continue; } $positions_to_sort = []; $sorted_positions = []; $invoice_positions = []; $invoice_vatrate = 20; $price_total_sum = 0; foreach($billing_rows as $bill) { $billing_row_count++; $netoperator_id = false; if ($bill->owner_id) { $netoperator_id = $bill->owner_id; } $price = 0; $price_gross = 0; if ($bill->price > 0.000) { $price = $bill->price; } if ($bill->price_setup > 0.000) { $price = $bill->price_setup; } if ($bill->vatrate > 0 && $price) { $price_gross = $price + ($price / 100 * $bill->vatrate); } $start_date = new DateTime($bill->start_date); $end_date = new DateTime($bill->end_date); $year_month = $start_date->format("Ym"); if ($netoperator_id) { if (!array_key_exists($netoperator_id, $invoice_detail_data)) $invoice_detail_data[$netoperator_id] = []; $detail_data = [ "owner_id" => $bill->owner_id, "company" => ($bill->company) ?: $bill->firstname . " " . $bill->lastname, "oaid" => $bill->oaid, "order_date" => $bill->order_date, "start_date" => $start_date->format("Y-m-d"), "end_date" => $end_date->format("Y-m-d"), "product_name" => $bill->product_name, "amount" => $bill->amount, "billing_periodic" => $bill->billing_period, "price" => $price, "price_gross" => $price_gross, "vatrate" => $bill->vatrate, ]; $invoice_detail_data[$netoperator_id][] = $detail_data; } $position_data = []; $position_data["product_id"] = $bill->product_id; $position_data["article_number"] = $bill->article_number; $position_data["product_name"] = $bill->product_name; $position_data["product_info"] = $bill->product_info; $position_data["total"] = round($price, 2); $position_data["total_gross"] = round($price_gross, 2); $position_data["vatrate"] = $bill->vatrate; $position_data["billing_id"] = $bill->id; if (!array_key_exists($bill->product_id, $invoice_positions)) { $invoice_positions[$bill->product_id] = []; } if (!array_key_exists($year_month, $invoice_positions[$bill->product_id])) { $invoice_positions[$bill->product_id][$year_month] = []; } $invoice_positions[$bill->product_id][$year_month][] = $position_data; } //var_dump($invoice_positions);exit; foreach($invoice_positions as $product_id => $year_months) { foreach($year_months as $year_month => $positions) { $new_pos_data = [ "count" => 0, "amount" => 0, "unit" => "Stk", "price" => 0, "price_total" => 0, "price_gross" => 0, "vatrate" => 20, "preorder_billings" => [], ]; foreach($positions as $position) { //var_dump($position);exit; $new_pos_data["count"]++; $new_pos_data["amount"]++; $new_pos_data["price"] = $position["total"]; $new_pos_data["price_total"] += $position["total"]; $new_pos_data["price_gross"] += $position["total_gross"]; $new_pos_data["vatrate"] = $position_data["vatrate"]; $new_pos_data["product_id"] = $position["product_id"]; $new_pos_data["article_number"] = $position["article_number"]; $new_pos_data["article_name"] = $position["product_name"]; $new_pos_data["preorder_billings"][] = $position["billing_id"]; $positions_count++; } if($product_id == 3) { $new_pos_data["amount"] = 1; $new_pos_data["unit"] = "Pau"; $new_pos_data["price"] = $new_pos_data["price_total"]; } $new_position = PreorderBillingInvoiceposition::create($new_pos_data); $new_position->preorder_billings = $new_pos_data["preorder_billings"]; $new_position->count = $new_pos_data["count"]; $sort_key = round(3 / $product_id)."-".$year_month; $positions_to_sort[$sort_key] = $new_position; } //var_dump($year_months, $new_pos_data); //var_dump($year_month, $product_id, $positions);exit; } ksort($positions_to_sort); $positions_to_sort = array_reverse($positions_to_sort); foreach($positions_to_sort as $key => $sort_pos) { $sorted_positions[] = $sort_pos; } //exit; //var_dump($sorted_positions);exit; //exit; $invoice_data = []; $invoice_data["netowner_id"] = $netowner_id; $invoice_data["preorderbillingcustomer_id"] = $bill->preorderbillingcustomer_id; $invoice_data["owner_id"] = $bill->owner_id; $invoice_data["billingaddress_id"] = $bill->billingaddress_id; $invoice_data["fibu_account_number"] = $bill->fibu_account_number; $invoice_data["fibu_cost_account"] = TT_PREORDER_BILLING[$netowner_id]["fibu-cost-code"]; $invoice_data["fibu_revenue_account"] = TT_PREORDER_BILLING[$netowner_id]["fibu-revenue-code"]; $invoice_data["fibu_tax_code"] = 1; $invoice_data["tax_text"] = null; $invoice_data["uid"] = $bill->uid; $invoice_data["company"] = $bill->company; $invoice_data["firstname"] = $bill->firstname; $invoice_data["lastname"] = $bill->lastname; $invoice_data["street"] = $bill->street; $invoice_data["zip"] = $bill->zip; $invoice_data["city"] = $bill->city; $invoice_data["country"] = $bill->country; $invoice_data["email"] = $bill->email; $invoice_data["billing_type"] = "invoice"; $invoice_data["billing_delivery"] = "email"; $invoice_data["total"] = 0; $invoice_data["total_gross"] = 0; $invoice = PreorderBillingInvoice::create($invoice_data); $invoice->startTransaction(); try { if (!$invoice->save()) { var_dump($invoice); $invoice->rollbackTransaction(); die("Error saving Invoice"); } $total_net = 0; $total_gross = 0; foreach($sorted_positions as $position) { //var_dump($position); $this->log->debug(__METHOD__.": count: ".$position->count); $billing_ids = $position->preorder_billings; $bill_id_count += count($billing_ids); unset($position->preorder_billings); unset($position->count); $total_net += $position->price_total; $total_gross += $position->price_gross; if (!$position->save()) { $invoice->rollbackTransaction(); die("Error saving Invoiceposition"); } $position->invoice_id = $invoice->id; if (!$position->save()) { $invoice->rollbackTransaction(); die("Error saving Invoiceposition"); } // add Invoice::id to Bill foreach($billing_ids as $bill_id) { $pbill = new PreorderBilling($bill_id); if (!$pbill->id) { $invoice->rollbackTransaction(); die("Bill for Invoiceposition not found"); } $pbill->invoice_id = $invoice->id; if (!$pbill->save()) { $invoice->rollbackTransaction(); die("error saving invoice_id to bill"); } } } // generate Invoice number $new_num = PreorderBillingInvoice::getNextInvoiceNumber($netowner_id); $invoice->total = $total_net; $invoice->total_gross = $total_gross; $invoice->invoice_number = $new_num; $invoice->invoice_date = $today->format("Y-m-d"); // voicenumbers //var_dump($invoice_voicenumbers);exit; if (!$invoice->save()) { $invoice->rollbackTransaction(); die("Error saving Invoice number and date"); } } catch(Exception $e) { if($invoice) { $invoice->rollbackTransaction(); } die("Error saving Invoice!\n"); } // commit transaction $invoice->commitTransaction(); $invoice_count++; } $this->log->debug(__METHOD__.": $billing_row_count billing rows | $positions_count positions count | $bill_id_count bill_id count"); $this->layout()->setFlash("$invoice_count Rechnungen erstellt", "success"); $this->redirect("PreorderBillingInvoice"); } }