Files
thetool/application/Order/Order.php
2024-07-08 17:51:54 +02:00

480 lines
14 KiB
PHP

<?php
class Order extends mfBaseModel {
private $owner;
private $billingaddress;
private $techcontact;
private $products;
private $customer_type;
private $contracts;
private $terminations;
private $journals;
private $install_products;
private $files;
private $creator;
private $editor;
private $shippingdate;
protected function afterSave() {
if($this->billingaddress_id) {
$this->getProperty("billingaddress")->generateFibuAccountNumber();
}
}
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 sendSpinEmail() {
$owner = $this->getProperty("owner");
$spin_pdf_template = "Emailtemplates/attachments/new_order_spin.pdf";
$new_order_template = "Emailtemplates/customer/new_order";
$pdf_vars = [
'order' => $this,
'owner' => $owner
];
if(TT_CUSTOM_ORDER_PDF_PER_NETOWNER) {
$terms = $this->getTerminations();
if(is_array($terms) && count($terms) && $terms[0]->id) {
$netowner = $terms[0]->building->network->owner;
} else {
$netowner = new Address(TT_CUSTOM_ORDER_PDF_DEFAULT_OWNER_ID);
}
$pdf_vars['netowner'] = $netowner;
}
// render service pin PDF
$pdf = new PdfForm($spin_pdf_template, $pdf_vars);
$pdfpath = $pdf->render();
$tvalue = $pdf->getReturnedValues();
$pdfname = $tvalue['filename'];
// send email to customer
// TODO template rendern auslagern nach Emailtempate klasse
$tpl = new Layout();
$tpl->setTemplate($new_order_template);
foreach($pdf_vars as $name => $val) {
$tpl->set($name, $val);
}
$body = $tpl->render();
$values = $tpl->getReturnedValue();
$subject = $values['subject'];
$from = $values['from_email'];
$from_name = $values['from_email_name'];
$to = $owner->email;
if(!$subject || !$from || !$from_name || !$to) {
$this->log->warn("Service PIN Email not sent. (subject: '$subject', from: '$from', from_email: '$from_email', to: '$to')");
} else {
$email = new Emailnotification();
$email->setSubject($subject);
$email->setBody($body);
$email->setFrom($from, $from_name);
$email->setTo($to);
$email->setHeader("X-".MFAPPNAME."-Oid", $this->id);
$email->addAttachment($pdfpath, null, $pdfname, "application/pdf");
$email->send();
$this->log->info(__CLASS__."::sendSpinEmail(): Sending new SPIN for ".$owner->getCompanyOrName()." (".$owner->id.")");
}
return true;
}
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-".MFAPPNAME."-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-".MFAPPNAME."-Oid", $this->id);
$email->send();
return true;
}
}
public function sendExtBillNbeInfoEmail() {
// TODO template rendern auslagern nach Emailtempate klasse
$tpl = new Layout();
$tpl->setTemplate("Emailtemplates/order/exernal_product_nbe_billing");
$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("Billing NBE 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-".MFAPPNAME."-oid", $this->id);
$email->send();
return true;
}
}
public function sendFileuploadEmail(OrderFile $file) {
$filetype = $file->name;
$this->log->debug("in sendFileuploadEmail(); filetype: $filetype");
if(!defined("TT_ORDER_FILE_TYPES") || ! is_array(TT_ORDER_FILE_TYPES )) {
return false;
}
if(!array_key_exists($filetype, TT_ORDER_FILE_TYPES)) {
return false;
}
$tpl = new Layout();
$tpl->setTemplate("Emailtemplates/order/fileupload/".$filetype);
if(!$tpl->templatePathExists()) {
$this->log->debug(__METHOD__.": tpl does not exist, not sending email.");
return true;
}
$tpl->set("order", $this);
$tpl->set("file", $file);
$body = $tpl->render();
$values = $tpl->getReturnedValue();
$subject = $values['subject'];
$from = $values['from_email'];
$from_name = $values['from_email_name'];
$to = $values['to'];
if(!$subject || !$from || !$from_name || !$to) {
$this->log->warn("Order Fileupload Email not sent. (subject: '$subject', from: '$from_name', from_email: '$from', to: '$to')");
} else {
$filepath = MFUPLOAD_FILE_SAVE_PATH;
if($file->file->subfolder) {
$filepath .= "/".$file->file->subfolder;
}
$filepath .= "/".$file->file->store_filename;
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mimetype = $finfo->file($filepath);
if(!$mimetype) {
$this->log->warning("File (orderfile->id: ".$file->id.") not a valid mimetype: $mimetype");
}
$email = new Emailnotification();
$email->setSubject($subject);
$email->setBody($body);
$email->setFrom($from, $from_name);
$email->setTo($to);
$email->setHeader("X-".MFAPPNAME."-Oid", $this->id);
$email->setHeader("X-".MFAPPNAME."-OFid", $file->id);
$email->addAttachment($filepath, null, $file->file->filename, $mimetype);
$email->send();
$this->log->info(__CLASS__."::save(): Sending Order Fileupload Email to '$to'");
return true;
}
return false;
}
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 == "techcontact") {
$this->techcontact = mfValuecache::singleton()->get("mfObjectmodel-Address-".$this->techcontact_id);
if($this->techcontact === null) {
$this->techcontact = new Address($this->techcontact_id);
if($this->techcontact->id) {
mfValuecache::singleton()->set("mfObjectmodel-Address-".$this->techcontact_id, $this->techcontact);
}
}
return $this->techcontact;
}
if($name == "products") {
$this->products = OrderProductModel::search(["order_id" => $this->id]);
//var_dump($this->products);exit;
return $this->products;
}
if($name == "install_products") {
$this->install_products = [];
foreach($this->getProperty("products") as $orderproduct) {
$product = $orderproduct->product;
//$atrribs = $product->attributes;
//var_dump($attribs);exit;
if(is_array($product->attributes) && array_key_exists("vot", $product->attributes) && $product->attributes["vot"] == 1) {
$this->install_products[] = $product;
}
}
//var_dump($this->install_products);exit;
return $this->install_products;
}
if($name == "customer_type") {
$this->customer_type = "residential";
foreach($this->getProperty("products") as $order_product) {
if($order_product->product->producttech->customer_type == "business") {
$this->customer_type = "business";
break;
}
}
return $this->customer_type;
}
if($name == "contracts") {
foreach($this->getProperty("products") as $product) {
//var_dump($product);
if($product->contract && $product->contract->id) {
$this->contracts[] = $product->contract;
}
}
return $this->contracts;
}
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;
}
}