needlogin = true; $me = new User(); $me->loadMe(); $this->me = $me; $this->layout()->set("me", $me); if (!$me->is(["Admin"])) { $this->redirect("Dashboard"); } } protected function indexAction() { $this->layout()->setTemplate("Billing/Index"); if ($this->request->resetFilter) { unset($_SESSION[MFAPPNAME . '-Billing-filter']); } $filter = []; if (is_array($this->request->filter)) { $filter = $this->request->filter; $_SESSION[MFAPPNAME . '-Billing-filter'] = $filter; } else { if (array_key_exists(MFAPPNAME . '-Billing-filter', $_SESSION) && count($_SESSION[MFAPPNAME . '-Billing-filter'])) { $filter = $_SESSION[MFAPPNAME . '-Billing-filter']; } } $this->layout->set("filter", $filter); $filter = $this->getPreparedFilter($filter); // pagination defaults $pagination = []; $pagination['start'] = 0; $pagination['count'] = 50; $pagination['maxItems'] = 0; if (is_numeric($this->request->s)) { $pagination['start'] = intval($this->request->s); } //var_dump($filter);exit; $pagination['maxItems'] = BillingModel::count($filter); $billings = BillingModel::search($filter, $pagination); $this->layout()->set("billings", $billings); $this->layout()->set("pagination", $pagination); } private function getPreparedFilter($filter) { $new_filter = []; if (is_array($filter) && count($filter)) { foreach ($filter as $name => $value) { $new_filter[$name] = $value; } } return $new_filter; } protected function importContractsAction() { $r = $this->request; $today = new DateTime("now"); $today->setTime(0,0,0); //$tomorrow = new DateTime("tomorrow"); //$tomorrow->setTime(0,0,0); $i = 0; foreach(ContractModel::searchActive(["finish_date<" => $today->getTimestamp()]) as $contract) { //var_dump($contract);exit; $now_year = date("Y"); $now_month = date("m"); $now_day = date("d"); $finish_year = date("Y", $contract->finish_date); $finish_month = date("m", $contract->finish_date); $finish_day = date("d", $contract->finish_date); $cancel_date = false; if($contract->cancel_date) { $cancel_date = new DateTime("@".$contract->cancel_date); $cancel_date->setTime(0,0,0); if($cancel_date->format("Y") != $now_year || $cancel_date->format("m") != $now_month) { $cancel_date = false; } } // find last Billing row $last_billing = BillingModel::getLast(["contract_id" => $contract->id]); if(!$last_billing) { // First billing // check finish_date // create Billing with start_date=finish_date and end_date = end of billing_period (month or year) $start_date = new DateTime("@".$contract->finish_date); $start_date->setTime(2,0,0); $price_setup = $contract->price_setup; } else { // Concurrent Billing // start_date next date after previous end_date $start_date = new DateTime($last_billing->end_date); $start_date->modify("+1 month"); // set Setup price to 0, because it was billed already $price_setup = 0; } // if contract has cancel date this month // use cancel date as end_date if($cancel_date) { $end_date = clone($cancel_date); } else { // else calculate last of month $end_date = clone($start_date); $end_date->modify("+".$contract->billing_period." months"); $end_date->modify("first day of this month"); $end_date->modify("-1 day"); } $sday = $start_date->format("d"); $eday = $end_date->format("d"); if($sday > 1 || $cancel_date) { // aliquoter preis $days = ($eday - $sday) + 1; $pc = $days / $eday * 100; $price = round($contract->price / 100 * $pc, 4); } else { $price = $contract->price; } $owner = $contract->owner; $billingaddress = $contract->billingaddress; $data = []; $data["contract_id"] = $contract->id; $data["start_date"] = $start_date->format("Y-m-d"); $data["end_date"] = $end_date->format("Y-m-d"); $data["billingaddress_id"] = ($contract->billingaddress_id) ? $contract->billingaddress_id : $contract->owner_id; $data["customer_number"] = $contract->owner->customer_number; $data["company"] = $billingaddress->company; $data["firstname"] = $billingaddress->firstname; $data["lastname"] = $billingaddress->lastname; $data["street"] = $billingaddress->street; $data["zip"] = $billingaddress->zip; $data["city"] = $billingaddress->city; $data["country"] = $billingaddress->country->name; $data["email"] = $billingaddress->email; $data["uid"] = $billingaddress->uid; $data["billing_type"] = $billingaddress->billing_type; $data["billing_delivery"] = $billingaddress->billing_delivery; $data["bank_account_bank"] = $billingaddress->bank_account_bank; $data["bank_account_owner"] = $billingaddress->bank_account_owner; $data["bank_account_iban"] = $billingaddress->bank_account_iban; $data["bank_account_bic"] = $billingaddress->bank_account_bic; $data["matchcode"] = $contract->mathcode; $data["product_id"] = $contract->product_id; $data["product_name"] = $contract->product_name; $data["product_info"] = $contract->product_info; $data["amount"] = $contract->amount; $data["price"] = $price; $data["price_setup"] = $price_setup; $data["billing_period"] = $contract->billing_period; $billing = BillingModel::create($data); if(!$billing->save()) { var_dump($billing);exit; } $i++; } $this->layout()->setFlash("$i Billing records generiert"); } }