File: /var/www/vhosts/creativefellows.nl/test.creativefellows.nl/tekenmappen/classes/OrderController.php
<?php
/**
* Description
*/
class OrderController extends BaseController
{
function __construct($db,$settings)
{
$this->db = $db;
//$this->settings = $settings;
$this->client_settings = $settings;
//d($settings);
}
/*
* Create new order
*/
public function create($user_id,$invoice_no,$order_total,$tax_total,$delivery_costs,$betaalwijze,$opmerkingen,$cart)
{
$sth = $this->db->prepare(
"INSERT INTO bestellingen(
klant_id,
order_id,
totaal,
tax,
bezorgkosten,
type_betaling,
opmerkingen
)
VALUES(
:klant_id,
:order_id,
:totaal,
:tax,
:bezorgkosten,
:type_betaling,
:opmerkingen
)"
);
$sth->execute([
"klant_id" => $user_id,
"order_id" => $invoice_no,
"totaal" => $order_total + $delivery_costs,
"tax" => $tax_total,
"bezorgkosten" => $delivery_costs,
"type_betaling" => $betaalwijze,
"opmerkingen" => $opmerkingen,
]);
$order_id = $this->db->lastInsertId();
$this->setOrder($order_id);
$this->insertProducts($order_id,$cart);
return $order_id;
}
/*
* Insert session products in order
*/
private function insertProducts($order_id,$cart)
{
foreach($cart->all() as $uid => $p)
{
$sth = $this->db->prepare(
"INSERT INTO bestellingen_producten
(
uid,
bestelling_id,
aantal,
prijs,
options,
totaal
)
VALUES
(
:uid,
:bestelling_id,
:aantal,
:prijs,
:options,
:totaal
)"
);
$sth->execute([
"uid" => $cart->get($uid,"pid"),
"bestelling_id" => $order_id,
"aantal" => $cart->get($uid,"amount"),
"prijs" => $cart->get($uid,"price"),
"options" => $cart->get($uid,"material"),
"totaal" => $cart->getProductTotal($uid)
]);
}
}
/*
* Get the order confirmation email
*/
public function setEmail($client,$cart)
{
// add email footer
$bericht = $this->getEmailHeader();
// email body toevoegen
$bericht .= "<tr><td colspan='4'>". $this->getTemplate(1) ."</td></tr>";
// add email footer
$bericht .= $this->getEmailFooter();
$products = "";
// '<tr><td colspan="5">producten</td></tr>',
foreach($cart->all() as $product)
{
$products .= '<tr>';
$products .= '<td style="vertical-align:top;width:300px;" colspan="2"><strong>'. $cart->get($product["uid"],"article") .'</strong><br />'. $cart->get($product["uid"],"size") .'</td>';
$products .= '<td style="vertical-align:top;width:80px;">'. $cart->get($product["uid"],"amount") .'</td>';
$products .= '<td style="width:80px;vertical-align:top;text-align:right;">'. $cart->formatPrice( $cart->get($product["uid"],"price") ) .'</td>';
$products .= '<td style="width:80px;vertical-align:top;text-align:right;">'. $cart->formatPrice( $cart->getProductTotal($product["uid"]) ) .'</td>';
$products .= '</tr>';
}
$f = [
"/{user_address}/",
"/{order_id}/",
"/{date_order}/",
"/{paymentmethod}/",
"/{products}/",
"/{order_subtotal}/",
"/{order_total}/",
"/{tax_global}/",
"/{order_minus_tax}/",
"/{order_tax}/",
"/{remarkts}/",
"/{delivery}/",
"/{color}/"
];
$r = [
$client->getFullName() ."<br />".$client->getAddress(),
$this->getInvoiceId(),
$this->getDate(),
$this->getPaymentMethod(),
$products,
$this->formatPrice( $this->getTotal()),
$this->formatPrice( $this->getTotal() + $this->getDeliveryCost(),true),
$this->getTaxPercentage(),
$this->formatPrice($this->getTotal() + $this->getDeliveryCost() - $this->getTax(),true),
$this->formatPrice($this->getTax(),true),
$this->getOrderRemarks(),
$this->formatPrice( $this->getDeliveryCost() ),
$this->getColor()
];
$html = preg_replace($f,$r,$bericht);
$sql = 'UPDATE bestellingen SET html = :html WHERE bestelling_id = :bestelling_id';
$sth = $this->db->prepare($sql);
$sth->execute([
"html" => $html,
"bestelling_id" => $this->getBestellingId()
]);
$this->htmlEmail = $html;
}
public function getEmail()
{
return $this->htmlEmail;
}
public function updateTransactionId($payment_id,$besteling_id)
{
$sql = "UPDATE bestellingen SET ideal_trans_id = '$payment_id' WHERE bestelling_id = '$besteling_id'";
$sth = $this->db->prepare($sql);
$sth->execute([
"ideal_trans_id" => $payment_id,
"order_id" => $besteling_id
]);
}
public function getTransactionId()
{
return $this->ideal_trans_id;
}
/*
* Get current invoice number
*/
public function getInvoiceNumber()
{
$current_no = $this->getClientSetting("order_nummer");
// update invoice number in db
$sql = 'UPDATE instellingen set order_nummer = :order_nummer WHERE id = :id';
$sth = $this->db->prepare($sql);
$sth->execute([
"id" => 1,
"order_nummer" => $current_no + 1
]);
return $this->getClientSetting("prefix_order_nummer") . sprintf('%05d', $this->getClientSetting("order_nummer") );
}
/*
* Update payment status
*/
public function updatePaymentStatus($status=1)
{
$sql = "UPDATE bestellingen SET betaald = :payed WHERE bestelling_id = :bestelling_id";
$sth = $this->db->prepare($sql);
$sth->execute([
"payed" => $status,
"bestelling_id" => $this->getBestellingId()
]);
}
public function setOrderStatus($status)
{
$sql = "UPDATE bestellingen SET status = :status WHERE bestelling_id = :bestelling_id";
$sth = $this->db->prepare($sql);
$sth->execute([
"status" => $status,
"bestelling_id" => $this->getBestellingId()
]);
}
public function getOrderByPayid($payment_id)
{
$sql = "SELECT * FROM bestellingen WHERE ideal_trans_id = :transaction_id";
$sth = $this->db->prepare($sql);
$sth->execute([
"transaction_id" => $payment_id
]);
return $sth->fetch();
}
public function setOrderById($order_id)
{
$sth = $this->db->prepare('SELECT * FROM bestellingen WHERE order_id = :order_id');
$sth->execute([
"order_id" => $order_id
]);
$order_fields = $sth->fetch();
foreach($order_fields as $column =>$value)
{
$this->$column = $value;
}
}
public function getClientId()
{
return $this->klant_id;
}
/*
* Set order data
*/
public function setOrder($order_id)
{
$sth = $this->db->prepare("SELECT * FROM bestellingen WHERE bestelling_id = :bestelling_id");
$sth->execute([
"bestelling_id" => $order_id
]);
$order_fields = $sth->fetch();
foreach($order_fields as $column =>$value)
{
$this->$column = $value;
}
}
private function getTotal()
{
return $this->totaal;
}
private function getTax()
{
return $this->tax;// * (1 + $this->getTaxPercentage()/100 );
}
private function getDeliveryCost()
{
return $this->bezorgkosten;
}
private function getTaxPercentage()
{
//return 21;
return $this->settings["btw_percentage"];
}
private function getColor()
{
//return 21;
return $this->settings["standaard_kleur"];
}
private function getOrderRemarks(){
return $this->opmerkingen;
}
/*
* email header
*/
private function getEmailHeader()
{
$header = "<html><head></head><body>";
$header .= "<table border='0' cellspacing='0' cellpadding='5' style='width:600px; font-size:12px;font-family:Helvetica,arial, sans-serif; color:#333;' align='center'>";
$header .= "<tr><td colspan='4' style='border-bottom:solid 1px #e6e6e6;padding-bottom:20px;padding-top:20px;'><img src='". $this->settings["website"] .'/'. $this->settings["logo"] ."' alt='". $this->settings["bedrijfsnaam"] ."' width=\"300\" /></td></tr>";
return $header;
}
/*
* email footer
*/
private function getEmailFooter()
{
$footer = "<tr><td colspan='4' style='border-bottom:solid 1px #e6e6e6;'> </td></tr>";
$footer .= "<tr><td colspan='4'> </td></tr>";
$footer .= "<tr><td colspan='4' style='width:260px;vertical-align:top;line-height:16px;'>";
$footer .= "<table border='0' cellspacing='0' cellpadding='3' style='width:100%; font-size:12px;font-family:Helvetica,arial, sans-serif; color:#333;'>";
$footer .= "<tr>";
$footer .= "<td style='width:300px;vertical-align:top;font-size:12px;'>
<strong>".$this->settings["bedrijfsnaam"]."</strong><br />
".$this->settings["adres"]."<br />
".$this->settings["postcode_woonplaats"]."<br />
Telefoon ".$this->settings["telefoon"]."</td>";
$footer .= "<td style='vertical-align:top;font-size:12px;'><br />Bank ".$this->settings["bank"]."<br />IBAN ".$this->settings["bank_iban"]."<br />BIC ".$this->settings["bank_bic"]."</td>";
$footer .= "</tr>";
$footer .= "</table>";
$footer .= "</td></tr>";
$footer .= "<tr><td colspan='4'> </td></tr>";
$footer .= "<tr><td colspan='4'> </td></tr>";
$footer .= "</table>";
$footer .= "</body></html>";
return $footer;
}
/*
* Get a template
*/
private function getTemplate($template_id)
{
$sth = $this->db->prepare('SELECT * FROM site_templates WHERE template_id = :template_id');
$sth->execute([
"template_id" => $template_id
]);
$data = $sth->fetch();
return $data["template"];
}
public function getInvoiceId(){
return $this->order_id ;
}
private function getDate(){
return strftime("%d %b %Y %H:%M");
}
private function getPaymentMethod(){
return $this->type_betaling === 0 ? "iDeal" : "Overboeking";
}
public function getOrderId(){
return $this->order_id;
}
public function getBestellingId(){
return $this->bestelling_id;
}
public function getStatus(){
return $this->status;
}
public function getHTML()
{
return $this->html;
}
}
?>