Skip to content

armgit / Vvveb

Public Imported from GitHub
0

Powerful and easy to use cms to build websites, blogs or ecommerce stores.

Order code refactoring

givanzbbfb0fc51ea0
diff --git a/admin/component/product.php b/admin/component/product.php
new file mode 100644
index 0000000..221d71a
--- /dev/null
+++ b/admin/component/product.php
@@ -0,0 +1,87 @@
+<?php
+
+/**
+ * Vvveb
+ *
+ * Copyright (C) 2022 Ziadin Givan
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Vvveb\Component;
+
+use Vvveb\Sql\ProductSQL;
+use Vvveb\System\Cart\Currency;
+use Vvveb\System\Cart\Tax;
+use Vvveb\System\Component\ComponentBase;
+use Vvveb\System\Event;
+use Vvveb\System\Images;
+use function Vvveb\url;
+
+class Product extends ComponentBase {
+ public static $defaultOptions = [
+ 'product_id' => 'url',
+ 'slug' => 'url',
+ 'status' => 1,
+ 'language_id' => null,
+ 'site_id' => null,
+ 'user_id' => null,
+ 'user_group_id' => null,
+ 'reviews' => true,
+ 'rating' => true,
+ 'promotion' => true,
+ 'image_size' => '',
+ ];
+
+ function results() {
+ $product = new ProductSQL();
+ $results = $product->get($this->options);
+
+ $results['images'] = [];
+
+ if (isset($results['product_image'])) {
+ $results['images'] = Images::images($results['product_image'], 'product', $this->options['image_size']);
+ }
+
+ if (isset($results['image'])) {
+ //$results['images'][] = ['image' => Images::image('product', $results['image'])];
+ $results['image']= Images::image($results['image'], 'product', $this->options['image_size']);
+ }
+
+ $results['add_cart_url'] = url('cart/cart/add', ['product_id' => $results['product_id']]);
+ $results['buy_url'] = url('checkout/checkout/index', ['product_id' => $results['product_id']]);
+ $results['add_wishlist_url'] = url('user/wishlist/add', ['product_id' => $results['product_id']]);
+ $results['add_compare_url'] = url('cart/compare/add', ['product_id' => $results['product_id']]);
+ $results['manufacturer_url'] = url('product/manufacturer/index', ['slug' => $results['manufacturer_slug']]);
+ $results['vendor_url'] = url('product/vendor/index', ['slug' => $results['vendor_slug']]);
+
+ $tax = Tax::getInstance();
+ $currency = Currency::getInstance();
+ $results['price_tax'] = $tax->addTaxes($results['price'], $results['tax_type_id']);
+ $results['price_tax_formatted'] = $currency->format($results['price_tax']);
+ $results['price_formatted'] = $currency->format($results['price']);
+
+ if ($results['promotion']) {
+ $results['promotion_tax'] = $tax->addTaxes($results['promotion'], $results['tax_type_id']);
+ $results['promotion_tax_formatted'] = $currency->format($results['promotion_tax']);
+ $results['promotion_formatted'] = $currency->format($results['promotion']);
+ $results['promotion_discount'] = 100 - ceil($results['promotion'] * 100 / $results['price']);
+ }
+
+ list($results) = Event :: trigger(__CLASS__,__FUNCTION__, $results);
+
+ return $results;
+ }
+}
diff --git a/admin/controller/order/order.php b/admin/controller/order/order.php
index ab01866..67b0809 100755
--- a/admin/controller/order/order.php
+++ b/admin/controller/order/order.php
@@ -31,6 +31,7 @@ use Vvveb\Sql\Order_LogSQL;
use Vvveb\Sql\OrderSQL;
use Vvveb\System\Cart\Cart;
use Vvveb\System\Cart\Currency;
+use Vvveb\System\Cart\Order as SystemOrder;
use Vvveb\System\Core\View;
use Vvveb\System\Images;
use Vvveb\System\Payment;
@@ -107,6 +108,7 @@ class Order extends Base {
$url = ['module' => 'order/order', 'action' => 'print', 'order_id' => $order['order_id']];
$view->printUrl = \Vvveb\url($url);
$view->printShippingUrl = \Vvveb\url(['action' => 'printShipping'] + $url);
+ $view->printInvoiceUrl = \Vvveb\url(['action' => 'printInvoice'] + $url);
} else {
return $this->notFound(true, __('Order not found!'));
}
@@ -126,6 +128,10 @@ class Order extends Base {
return $this->index();
}
+ function printInvoice() {
+ return $this->index();
+ }
+
function cart() {
$order_id = $this->request->get['order_id'] ?? false;
$product_id = $this->request->post['product_id'] ?? false;
@@ -196,11 +202,14 @@ class Order extends Base {
$view->errors = [$orders->error];
}
} else {
- $result = $orders->add(['order' => $order + $this->global]);
+ $order['user_id'] = 0;
+ $systemOrder = SystemOrder::getInstance();
+ $order_id = $systemOrder->add($order + $this->global);
+ //$result = $orders->add(['order' => $order + $this->global]);
- if (isset($result['order'])) {
+ if (isset($order_id)) {
$view->success = __('Order saved!');
- $url = ['module' => 'order/order', 'order_id' => $result['order']];
+ $url = ['module' => 'order/order', 'order_id' => $order_id];
$this->redirect($url);
} else {
$view->errors = [$orders->error];
diff --git a/admin/template/common.tpl b/admin/template/common.tpl
index 8582e67..da473b7 100755
--- a/admin/template/common.tpl
+++ b/admin/template/common.tpl
@@ -7,6 +7,7 @@ import(notifications.tpl)
import(components/posts.tpl)
import(components/products.tpl)
+import(components/product.tpl)
import(components/orders.tpl)
import(components/users.tpl)
import(components/languages.tpl)
@@ -23,6 +24,7 @@ import(menu.tpl)
[data-v-check-permission-*]|if_exists = $this->actionPermissions['@@__data-v-check-permission-(*)__@@']
+[data-v-check-action-permission]|if_exists = $this->actionPermissions['@@__data-v-check-action-permission__@@']
[data-v-check-permission]|if_exists = $this->modulePermissions['@@__data-v-check-permission__@@']
html|addNewAttribute = <?php if (isset($_COOKIE['theme'])) {
@@ -33,4 +35,4 @@ html|addNewAttribute = <?php if (isset($_COOKIE['theme'])) {
#container|addClass = <?php if (isset($_COOKIE['sidebar-size']) && ($_COOKIE['sidebar-size'] == 'small-nav')) {
echo 'small-nav';
-} ?>
\ No newline at end of file
+} ?>
diff --git a/admin/template/components/product.tpl b/admin/template/components/product.tpl
new file mode 100644
index 0000000..fdfba8e
--- /dev/null
+++ b/admin/template/components/product.tpl
@@ -0,0 +1,66 @@
+@product = [data-v-component-product]
+@images = [data-v-component-product] [data-v-product-images]
+
+
+@product|data-v-id = $product['product_id']
+@product|data-v-type = 'product'
+
+@product|before = <?php
+if (isset($_product_idx)) $_product_idx++; else $_product_idx = 0;
+$previous_component = isset($component)?$component:null;
+
+$previous_component = isset($current_component)?$current_component:null;
+$product = $current_component = $this->_component['product'][$_product_idx] ?? [];
+
+$_pagination_count = $product['count'] ?? 0;
+$_pagination_limit = isset($product['limit']) ? $product['limit'] : 5;
+?>
+
+//editor attributes
+@product|data-v-id = $product['product_id']
+@product|data-v-type = 'product'
+
+//catch all data attributes
+@product [data-v-product-*]|innerText = $product['@@__data-v-product-(*)__@@']
+@product input[data-v-product-*]|value = $product['@@__data-v-product-(*)__@@']
+@product a[data-v-product-*]|href = $product['@@__data-v-product-(*)__@@']
+
+//manual echo to avoid html escape
+@product [data-v-product-content] = <?php echo $product['content'];?>
+
+@product button[data-v-product-*]|formaction = $product['@@__data-v-product-(*)__@@']
+@product a[data-v-product-*]|href = $product['@@__data-v-product-(*)__@@']
+
+
+@product img[data-v-product-main-image]|src = <?php echo $product['image'];?>
+@product [data-v-product-main-image-background-image]|style = <?php echo 'background-image: url(\'' . $product['image'] . '\');';?>
+@product a[data-v-product-main-image]|href = <?php echo reset($product['images'])['image'];?>
+
+@images [data-v-product-image]|deleteAllButFirstChild
+
+@images [data-v-product-image]|before = <?php
+$_images = $product['images'] ?? [];
+$_default = (isset($vvveb_is_page_edit) && $vvveb_is_page_edit ) ? [0 => ['product_image_id' => 1, 'image' => '']] : false;
+$_images = empty($_images) ? $_default : $_images;
+
+if($_images) {
+ $i = 1;
+ foreach ($_images as $index => $_image) { ?>
+
+ @images [data-bs-slide-to]|data-bs-slide-to = <?php echo $i;?>
+ @images img[data-v-product-image-src]|src = $_image['image']
+ @images [data-v-product-image-background-image]|style = <?php echo 'background-image: url(\'' . $_image['image'] . '\');';?>
+ @images a[data-v-product-image-src]|href = $_image['image']
+ @images img[data-v-product-image-src]|data-v-id = $_image['product_image_id']
+ @images img[data-v-product-image-src]|data-v-type = 'product_image'
+
+ @images [data-v-product-image]|after = <?php
+ $i++;
+ }
+}
+
+$component = $previous_component;
+?>
+
+
+
\ No newline at end of file
diff --git a/admin/template/order/order.tpl b/admin/template/order/order.tpl
index 0f11a5e..0856764 100755
--- a/admin/template/order/order.tpl
+++ b/admin/template/order/order.tpl
@@ -64,6 +64,7 @@ if(is_array($logs)) foreach ($logs as $index => $log) {
[data-v-order-print-url]|href = $this->printUrl
[data-v-order-print-shipping-url]|href = $this->printShippingUrl
+[data-v-order-print-invoice-url]|href = $this->printInvoiceUrl
@payment = [data-v-payments] [data-v-payment]
diff --git a/admin/template/order/order/printinvoice.tpl b/admin/template/order/order/printinvoice.tpl
new file mode 100644
index 0000000..12af6d8
--- /dev/null
+++ b/admin/template/order/order/printinvoice.tpl
@@ -0,0 +1 @@
+import(order/order.tpl)
diff --git a/app/controller/checkout/checkout.php b/app/controller/checkout/checkout.php
index 2e9f62c..f5a5bee 100755
--- a/app/controller/checkout/checkout.php
+++ b/app/controller/checkout/checkout.php
@@ -36,6 +36,7 @@ use Vvveb\System\CacheManager;
use Vvveb\System\Cart\Cart;
use Vvveb\System\Cart\Order;
use Vvveb\System\Core\View;
+use Vvveb\System\Event;
use Vvveb\System\Payment;
use Vvveb\System\Shipping;
use Vvveb\System\Sites;
@@ -252,17 +253,21 @@ class Checkout extends Base {
$this->view->errors = [];
- $order = $order->add($checkoutInfo);
+ list($checkoutInfo) = Event::trigger(__CLASS__, 'add', $checkoutInfo);
- if ($order && is_array($order)) {
- $order_id = $order['order'];
+ $order_id = $order->add($checkoutInfo);
+
+ if ($order_id) {
$this->request->request['order_id'] = $order_id;
+ $checkoutInfo['order_id'] = $order_id;
$this->view->messages[] = __('Order placed!');
- $this->session->set('order', $order);
+ $this->session->set('order', $checkoutInfo);
$cart->empty();
$site = siteSettings();
+ list($checkoutInfo, $order_id, $site) = Event::trigger(__CLASS__, 'add:after', $checkoutInfo, $order_id, $site);
+
try {
$error = __('Error sending order confirmation mail!');
diff --git a/app/controller/checkout/confirm.php b/app/controller/checkout/confirm.php
index 1bbd673..77bba1c 100755
--- a/app/controller/checkout/confirm.php
+++ b/app/controller/checkout/confirm.php
@@ -27,7 +27,7 @@ use Vvveb\Controller\Base;
class Confirm extends Base {
function index() {
$order = $this->session->get('order');
- $this->view->order_id = $order['order'] ?? false;
+ $this->view->order = $order;
if (! $order) {
$this->notFound();
diff --git a/system/cart/cart.php b/system/cart/cart.php
index aa7a37e..6fe9e64 100755
--- a/system/cart/cart.php
+++ b/system/cart/cart.php
@@ -22,6 +22,7 @@
namespace Vvveb\System\Cart;
+use function \Vvveb\model;
use function \Vvveb\url;
use Vvveb\Sql\Product_Option_ValueSQL;
use Vvveb\Sql\ProductSQL;
@@ -37,6 +38,10 @@ class Cart {
private $tax;
+ protected $productModel = 'product';
+
+ protected $sessionKey = 'cart';
+
protected $options;
protected $products = [];
@@ -118,7 +123,7 @@ class Cart {
'product_id' => $productIds,
] + $this->options;
- $productSql = new ProductSQL();
+ $productSql = model($this->productModel); //new ProductSQL();
$results = $productSql->getAll(
$options
);
@@ -200,6 +205,13 @@ class Cart {
}
$this->addTotal('sub_total', 'Sub-total', $this->total);
+
+ //set cart cookie to disable cache if products in cart
+ if ($this->total_items) {
+ setcookie('cart', '1', 0, '/');
+ } else {
+ setcookie('cart', '', time() - 3600, '/');
+ }
//write is done by addTotal
//$this->write();
@@ -399,7 +411,7 @@ class Cart {
}
protected function read() {
- $data = $this->session->get('cart');
+ $data = $this->session->get($this->sessionKey);
if (is_array($data)) {
foreach ($data as $property => $value) {
@@ -414,10 +426,12 @@ class Cart {
$data[$property] = $this->$property;
}
- $this->session->set('cart', $data);
+ $this->session->set($this->sessionKey, $data);
}
public function empty() {
- $this->session->set('cart', []);
+ $this->session->set($this->sessionKey, []);
+ //enable cache back by clearing the cart cookie
+ setcookie('cart', '', time() - 3600, '/');
}
}
diff --git a/system/cart/order.php b/system/cart/order.php
index db79cef..adac2fc 100755
--- a/system/cart/order.php
+++ b/system/cart/order.php
@@ -48,7 +48,7 @@ class Order {
public function add($data) {
//set defaults
- $defaults = ['invoice_prefix', 'order_status_id', 'remote_ip', 'forwarded_for_ip'];
+ $defaults = ['invoice_format', 'order_status_id', 'remote_ip', 'forwarded_for_ip'];
$site = siteSettings();
$site['remote_ip'] = $_SERVER['REMOTE_ADDR'] ?? false;
@@ -63,7 +63,19 @@ class Order {
}
}
- //check if email is already registerd
- return $this->model->add(['order' => $data]);
+ $data['invoice_no'] = \Vvveb\invoiceFormat($data['invoice_format'], $data);
+
+ //todo: check if email is already registerd for new accounts
+ $result = $this->model->add(['order' => $data]);
+ $order_id = $result['order'] ?? false;
+
+ if ($order_id) {
+ //update invoice to set {order_id} variable
+ $data['order_id'] = $order_id;
+ $data['invoice_no'] = \Vvveb\invoiceFormat($data['invoice_format'], $data);
+ $result = $this->model->edit(['order' => ['invoice_no' => $data['invoice_no']], 'order_id' => $order_id]);
+ }
+
+ return $order_id;
}
}