Files
thetool/application/Order/Order.php
2022-05-18 15:45:32 +02:00

269 lines
7.4 KiB
PHP

<?php
class Order extends mfBaseModel {
private $owner;
private $billingaddress;
private $products;
private $terminations;
private $journals;
private $files;
private $creator;
private $editor;
private $shippingdate;
public function getNewPos() {
if(!$this->id) {
return 0;
}
$products = $this->getProperty("products");
if(!is_array($products) || !count($products)) {
return 1;
}
$p = end($products);
return ++$p->pos;
}
public function getTerminations() {
if(!$this->id) {
return false;
}
$products = $this->getProperty("products");
if(!is_array($products) || !count($products)) {
return false;
}
$terminations = [];
foreach($products as $product) {
if($product->termination_id) {
$terminations[] = $product->termination;
}
}
//var_dump($this->terminations);exit;
return $terminations;
}
public function getShippingdate() {
if(!$this->id) {
return false;
}
$products = $this->getProperty("products");
if(!is_array($products) || !count($products) ) {
return false;
}
foreach($this->getProperty("products") as $product) {
if($product->cpeprovisioning) {
return $product->cpeprovisioning->shipping_date;
}
}
return false;
}
public function deletePositions() {
if(!is_array($this->getProperty("products")) || !count($this->getProperty("products"))) {
return true;
}
foreach($this->getProperty("products") as $position) {
$position->delete();
}
}
public function deleteFiles() {
if(!is_array($this->getProperty("files")) || !count($this->getProperty("files"))) {
return true;
}
//var_dump($this->files);exit;
foreach($this->getProperty("files") as $file) {
$file->file->delete();
$file->delete();
}
}
public function sendIntBillinfoEmail() {
// TODO template rendern auslagern nach Emailtempate klasse
$tpl = new Layout();
$tpl->setTemplate("Emailtemplates/order/int_billing_info");
$tpl->set("order", $this);
$body = $tpl->render();
$values = $tpl->getReturnedValue();
//var_dump($values); echo "<pre class='text-monospace'>".$body."</pre>"; exit;
$subject = $values['subject'];
$from = $values['from_email'];
$from_name = $values['from_email_name'];
$to = TT_INTERNAL_BILLING_EMAIL_TO;
if(!$subject || !$from || !$from_name || !$to) {
$this->log->warn("Internl Billing Email not sent. (subject: '$subject', from: '$from', from_email: '$from_name', to: '$to')");
return false;
} else {
$email = new Emailnotification();
$email->setSubject($subject);
$email->setBody($body);
$email->setFrom($from, $from_name);
$email->setTo($to);
$email->setHeader("X-xinon-oid", $this->id);
$email->send();
return true;
}
}
public function sendExtBillinfoEmail(Address $to_address) {
// TODO template rendern auslagern nach Emailtempate klasse
$tpl = new Layout();
$tpl->setTemplate("Emailtemplates/order/billing_external_energie");
$tpl->set("order", $this);
$body = $tpl->render();
$values = $tpl->getReturnedValue();
/*
$this->finish_date = null;
$this->save();
*/
//var_dump($values); echo "<pre class='text-monospace'>".$body."</pre>"; exit;
$subject = $values['subject'];
$from = $values['from_email'];
$from_name = $values['from_email_name'];
$to = $to_address->email;
if(!$to) {
$this->log->error("Productowner ".$to_address->id." does not have emailaddress");
return false;
}
if(!$subject || !$from || !$from_name || !$to) {
$this->log->warn("Internl Billing Email not sent. (subject: '$subject', from: '$from', from_email: '$from_name', to: '$to')");
return false;
} else {
$email = new Emailnotification();
$email->setSubject($subject);
$email->setBody($body);
$email->setFrom($from, $from_name);
$email->setTo($to);
$email->setHeader("X-Xinon-Oid", $this->id);
$email->send();
return true;
}
}
public function getProperty($name) {
if($this->$name == null) {
if(!$this->id) {
return null;
}
if($name == "owner") {
$this->owner = mfValuecache::singleton()->get("mfObjectmodel-Address-".$this->owner_id);
if($this->owner === null) {
$this->owner = new Address($this->owner_id);
if($this->owner->id) {
mfValuecache::singleton()->set("mfObjectmodel-Address-".$this->owner_id, $this->owner);
}
}
return $this->owner;
}
if($name == "billingaddress") {
$this->billingaddress = mfValuecache::singleton()->get("mfObjectmodel-Address-".$this->billingaddress_id);
if($this->billingaddress === null) {
$this->billingaddress = new Address($this->billingaddress_id);
if($this->billingaddress->id) {
mfValuecache::singleton()->set("mfObjectmodel-Address-".$this->billingaddress_id, $this->billingaddress);
}
}
return $this->billingaddress;
}
if($name == "products") {
$this->products = OrderProductModel::search(["order_id" => $this->id]);
return $this->products;
}
if($name == "terminations") {
$this->terminations = $this->getTerminations();
return $this->terminations;
}
if($name == "journals") {
$this->journals = OrderJournalModel::search(["order_id" => $this->id]);
return $this->journals;
}
if($name == "files") {
$this->files = OrderFileModel::search(['order_id' => $this->id]);
return $this->files;
}
if($name == "shippingdate") {
$this->shippingdate = $this->getShippingdate();
return $this->shippingdate;
}
if($name == "creator") {
$this->creator = mfValuecache::singleton()->get("Worker-id-".$this->create_by);
if($this->creator === null) {
$this->creator = new User($this->create_by);
if($this->creator->id) {
mfValuecache::singleton()->set("Worker-id-".$this->create_by, $this->creator);
}
}
return $this->creator;
}
if($name == "editor") {
$this->editor = mfValuecache::singleton()->get("Worker-id-".$this->edit_by);
if($this->editor === null) {
$this->editor = new User($this->edit_by);
if($this->editor->id) {
mfValuecache::singleton()->set("Worker-id-".$this->edit_by, $this->editor);
}
}
return $this->editor;
}
$classname = ucfirst($name);
$idfield = $name."_id";
$this->$name = mfValuecache::singleton()->get("mfObjectmodel-$name-".$this->$idfield);
if(!$this->$name) {
$this->$name = new $classname($this->$idfield);
}
if($this->$name->id) {
mfValuecache::singleton()->set("mfObjectmodel-$name-".$this->$name->id, $this->$name);
return $this->$name;
} else {
return null;
}
/*$classname = ucfirst($name);
$idfield = $name."_id";
$this->$name = new $classname($this->$idfield);
if($this->$name->id) {
return $this->$name;
} else {
return null;
}*/
}
return $this->$name;
}
}