ver. 0.290: ShopCoupon + ShopOrder frontend migration to Domain + Controllers
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -359,7 +359,7 @@ class ClientRepository
|
||||
$orders = [];
|
||||
if (is_array($rows)) {
|
||||
foreach ($rows as $row) {
|
||||
$orders[] = \front\factory\ShopOrder::order_details($row);
|
||||
$orders[] = (new \Domain\Order\OrderRepository($this->db))->orderDetailsFrontend($row);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -188,6 +188,73 @@ class CouponRepository
|
||||
return (bool)$this->db->delete('pp_shop_coupon', ['id' => $couponId]);
|
||||
}
|
||||
|
||||
public function findByName(string $name)
|
||||
{
|
||||
$name = trim($name);
|
||||
if ($name === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$coupon = $this->db->get('pp_shop_coupon', '*', ['name' => $name]);
|
||||
if (!is_array($coupon)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$coupon['id'] = (int)($coupon['id'] ?? 0);
|
||||
$coupon['status'] = (int)($coupon['status'] ?? 0);
|
||||
$coupon['used'] = (int)($coupon['used'] ?? 0);
|
||||
$coupon['one_time'] = (int)($coupon['one_time'] ?? 0);
|
||||
$coupon['type'] = (int)($coupon['type'] ?? 0);
|
||||
$coupon['include_discounted_product'] = (int)($coupon['include_discounted_product'] ?? 0);
|
||||
$coupon['used_count'] = (int)($coupon['used_count'] ?? 0);
|
||||
|
||||
return (object)$coupon;
|
||||
}
|
||||
|
||||
public function isAvailable($coupon)
|
||||
{
|
||||
if (!$coupon) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$id = is_object($coupon) ? ($coupon->id ?? 0) : ($coupon['id'] ?? 0);
|
||||
$status = is_object($coupon) ? ($coupon->status ?? 0) : ($coupon['status'] ?? 0);
|
||||
$used = is_object($coupon) ? ($coupon->used ?? 0) : ($coupon['used'] ?? 0);
|
||||
|
||||
if (!(int)$id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!(int)$status) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return !(int)$used;
|
||||
}
|
||||
|
||||
public function markAsUsed(int $couponId)
|
||||
{
|
||||
if ($couponId <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->db->update('pp_shop_coupon', [
|
||||
'used' => 1,
|
||||
'date_used' => date('Y-m-d H:i:s'),
|
||||
], ['id' => $couponId]);
|
||||
}
|
||||
|
||||
public function incrementUsedCount(int $couponId)
|
||||
{
|
||||
if ($couponId <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->db->update('pp_shop_coupon', [
|
||||
'used_count[+]' => 1,
|
||||
], ['id' => $couponId]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array<string, mixed>>
|
||||
*/
|
||||
|
||||
@@ -435,6 +435,278 @@ class OrderRepository
|
||||
return true;
|
||||
}
|
||||
|
||||
// --- Frontend methods ---
|
||||
|
||||
public function findIdByHash(string $hash)
|
||||
{
|
||||
$hash = trim($hash);
|
||||
if ($hash === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$id = $this->db->get('pp_shop_orders', 'id', ['hash' => $hash]);
|
||||
|
||||
return $id ? (int)$id : null;
|
||||
}
|
||||
|
||||
public function findHashById(int $orderId)
|
||||
{
|
||||
if ($orderId <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$hash = $this->db->get('pp_shop_orders', 'hash', ['id' => $orderId]);
|
||||
|
||||
return $hash ? (string)$hash : null;
|
||||
}
|
||||
|
||||
public function orderDetailsFrontend($orderId = null, $hash = '', $przelewy24Hash = '')
|
||||
{
|
||||
$order = null;
|
||||
|
||||
if ($orderId) {
|
||||
$order = $this->db->get('pp_shop_orders', '*', ['id' => $orderId]);
|
||||
if (is_array($order)) {
|
||||
$order['products'] = $this->db->select('pp_shop_order_products', '*', ['order_id' => $orderId]);
|
||||
if (!is_array($order['products'])) {
|
||||
$order['products'] = [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($hash) {
|
||||
$order = $this->db->get('pp_shop_orders', '*', ['hash' => $hash]);
|
||||
if (is_array($order)) {
|
||||
$order['products'] = $this->db->select('pp_shop_order_products', '*', ['order_id' => $order['id']]);
|
||||
if (!is_array($order['products'])) {
|
||||
$order['products'] = [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($przelewy24Hash) {
|
||||
$order = $this->db->get('pp_shop_orders', '*', ['przelewy24_hash' => $przelewy24Hash]);
|
||||
if (is_array($order)) {
|
||||
$order['products'] = $this->db->select('pp_shop_order_products', '*', ['order_id' => $order['id']]);
|
||||
if (!is_array($order['products'])) {
|
||||
$order['products'] = [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return is_array($order) ? $order : null;
|
||||
}
|
||||
|
||||
public function generateOrderNumber()
|
||||
{
|
||||
$date = date('Y-m');
|
||||
|
||||
$results = $this->db->query(
|
||||
'SELECT MAX( CONVERT( substring_index( substring_index( number, \'/\', -1 ), \' \', -1 ), UNSIGNED INTEGER) ) FROM pp_shop_orders WHERE date_order LIKE \'' . $date . '%\''
|
||||
)->fetchAll();
|
||||
|
||||
$nr = 0;
|
||||
if (is_array($results) && count($results)) {
|
||||
foreach ($results as $row) {
|
||||
$nr = ++$row[0];
|
||||
}
|
||||
}
|
||||
|
||||
if (!$nr) {
|
||||
$nr = 1;
|
||||
}
|
||||
|
||||
if ($nr < 10) {
|
||||
$nr = '00' . $nr;
|
||||
} elseif ($nr < 100) {
|
||||
$nr = '0' . $nr;
|
||||
}
|
||||
|
||||
return date('Y/m', strtotime($date)) . '/' . $nr;
|
||||
}
|
||||
|
||||
public function createFromBasket(
|
||||
$client_id,
|
||||
$basket,
|
||||
$transport_id,
|
||||
$payment_id,
|
||||
$email,
|
||||
$phone,
|
||||
$name,
|
||||
$surname,
|
||||
$street,
|
||||
$postal_code,
|
||||
$city,
|
||||
$firm_name,
|
||||
$firm_street,
|
||||
$firm_postal_code,
|
||||
$firm_city,
|
||||
$firm_nip,
|
||||
$inpost_info,
|
||||
$orlen_point_id,
|
||||
$orlen_point_info,
|
||||
$coupon,
|
||||
$basket_message
|
||||
) {
|
||||
global $lang_id, $settings;
|
||||
|
||||
if ($client_id) {
|
||||
$email = (new \Domain\Client\ClientRepository($this->db))->clientEmail((int)$client_id);
|
||||
}
|
||||
|
||||
if (!is_array($basket) || !$transport_id || !$payment_id || !$email || !$phone || !$name || !$surname) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$transport = \front\factory\ShopTransport::transport($transport_id);
|
||||
$payment_method = \front\factory\ShopPaymentMethod::payment_method($payment_id);
|
||||
$basket_summary = \Domain\Basket\BasketCalculator::summaryPrice($basket, $coupon);
|
||||
$order_number = $this->generateOrderNumber();
|
||||
$order_date = date('Y-m-d H:i:s');
|
||||
$hash = md5($order_number . time());
|
||||
|
||||
if ($transport['delivery_free'] == 1 && $basket_summary >= $settings['free_delivery']) {
|
||||
$transport_cost = '0.00';
|
||||
} else {
|
||||
$transport_cost = $transport['cost'];
|
||||
}
|
||||
|
||||
$this->db->insert('pp_shop_orders', [
|
||||
'number' => $order_number,
|
||||
'client_id' => $client_id ? $client_id : null,
|
||||
'date_order' => $order_date,
|
||||
'comment' => null,
|
||||
'client_name' => $name,
|
||||
'client_surname' => $surname,
|
||||
'client_email' => $email,
|
||||
'client_street' => $street,
|
||||
'client_postal_code' => $postal_code,
|
||||
'client_city' => $city,
|
||||
'client_phone' => $phone,
|
||||
'firm_name' => $firm_name ? $firm_name : null,
|
||||
'firm_street' => $firm_street ? $firm_street : null,
|
||||
'firm_postal_code' => $firm_postal_code ? $firm_postal_code : null,
|
||||
'firm_city' => $firm_city ? $firm_city : null,
|
||||
'firm_nip' => $firm_nip ? $firm_nip : null,
|
||||
'transport_id' => $transport_id,
|
||||
'transport' => $transport['name_visible'],
|
||||
'transport_cost' => $transport_cost,
|
||||
'transport_description' => $transport['description'],
|
||||
'orlen_point' => ($orlen_point_id) ? $orlen_point_id . ' | ' . $orlen_point_info : null,
|
||||
'inpost_paczkomat' => ($transport_id == 1 || $transport_id == 2) ? $inpost_info : null,
|
||||
'payment_method' => $payment_method['name'],
|
||||
'payment_method_id' => $payment_id,
|
||||
'hash' => $hash,
|
||||
'summary' => \Shared\Helpers\Helpers::normalize_decimal($basket_summary + $transport_cost),
|
||||
'coupon_id' => $coupon ? $coupon->id : null,
|
||||
'message' => $basket_message ? $basket_message : null,
|
||||
'apilo_order_status_date' => date('Y-m-d H:i:s'),
|
||||
]);
|
||||
|
||||
$order_id = $this->db->id();
|
||||
|
||||
if (!$order_id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($coupon) {
|
||||
(new \Domain\Coupon\CouponRepository($this->db))->incrementUsedCount((int)$coupon->id);
|
||||
}
|
||||
|
||||
// ustawienie statusu zamówienia
|
||||
$this->db->insert('pp_shop_order_statuses', ['order_id' => $order_id, 'status_id' => 0, 'mail' => 1]);
|
||||
|
||||
if (is_array($basket)) {
|
||||
foreach ($basket as $basket_position) {
|
||||
$attributes = '';
|
||||
$product = \shop\Product::getFromCache($basket_position['product-id'], $lang_id);
|
||||
|
||||
if (is_array($basket_position['attributes'])) {
|
||||
foreach ($basket_position['attributes'] as $row) {
|
||||
$row = explode('-', $row);
|
||||
$attributeRepo = new \Domain\Attribute\AttributeRepository($this->db);
|
||||
$attribute = $attributeRepo->frontAttributeDetails((int)$row[0], $lang_id);
|
||||
$value = $attributeRepo->frontValueDetails((int)$row[1], $lang_id);
|
||||
|
||||
if ($attributes) {
|
||||
$attributes .= '<br>';
|
||||
}
|
||||
$attributes .= '<b>' . $attribute['language']['name'] . '</b>: ';
|
||||
$attributes .= $value['language']['name'];
|
||||
}
|
||||
}
|
||||
|
||||
// custom fields
|
||||
$product_custom_fields = '';
|
||||
if (is_array($basket_position['custom_fields'])) {
|
||||
foreach ($basket_position['custom_fields'] as $key => $val) {
|
||||
$custom_field = \shop\ProductCustomField::getFromCache($key);
|
||||
if ($product_custom_fields) {
|
||||
$product_custom_fields .= '<br>';
|
||||
}
|
||||
$product_custom_fields .= '<b>' . $custom_field['name'] . '</b>: ' . $val;
|
||||
}
|
||||
}
|
||||
|
||||
$product_price_tmp = \shop\Product::calculate_basket_product_price((float)$product['price_brutto_promo'], (float)$product['price_brutto'], $coupon, $basket_position);
|
||||
|
||||
$this->db->insert('pp_shop_order_products', [
|
||||
'order_id' => $order_id,
|
||||
'product_id' => $basket_position['product-id'],
|
||||
'parent_product_id' => $basket_position['parent_id'] ? $basket_position['parent_id'] : $basket_position['product-id'],
|
||||
'name' => $product->language['name'],
|
||||
'attributes' => $attributes,
|
||||
'vat' => $product->vat,
|
||||
'price_brutto' => $product_price_tmp['price'],
|
||||
'price_brutto_promo' => $product_price_tmp['price_new'],
|
||||
'quantity' => $basket_position['quantity'],
|
||||
'message' => $basket_position['message'],
|
||||
'custom_fields' => $product_custom_fields,
|
||||
]);
|
||||
|
||||
$product_quantity = \shop\Product::get_product_quantity($basket_position['product-id']);
|
||||
if ($product_quantity != null) {
|
||||
$this->db->update('pp_shop_products', ['quantity[-]' => $basket_position['quantity']], ['id' => $basket_position['product-id']]);
|
||||
} else {
|
||||
$this->db->update('pp_shop_products', ['quantity[-]' => $basket_position['quantity']], ['id' => $basket_position['parent_id']]);
|
||||
}
|
||||
|
||||
$this->db->update('pp_shop_products', ['quantity' => 0], ['quantity[<]' => 0]);
|
||||
}
|
||||
}
|
||||
|
||||
if ($coupon && $coupon->is_one_time()) {
|
||||
$coupon->set_as_used();
|
||||
}
|
||||
|
||||
$order = $this->orderDetailsFrontend($order_id);
|
||||
|
||||
$mail_order = \Shared\Tpl\Tpl::view('shop-order/mail-summary', [
|
||||
'settings' => $settings,
|
||||
'order' => $order,
|
||||
'coupon' => $coupon,
|
||||
]);
|
||||
|
||||
$settings['ssl'] ? $base = 'https' : $base = 'http';
|
||||
|
||||
$regex = "-(<img[^>]+src\s*=\s*['\"])(((?!'|\"|https?://).)*)(['\"][^>]*>)-i";
|
||||
$mail_order = preg_replace($regex, "$1" . $base . "://" . $_SERVER['SERVER_NAME'] . "$2$4", $mail_order);
|
||||
|
||||
$regex = "-(<a[^>]+href\s*=\s*['\"])(((?!'|\"|https?://).)*)(['\"][^>]*>)-i";
|
||||
$mail_order = preg_replace($regex, "$1" . $base . "://" . $_SERVER['SERVER_NAME'] . "$2$4", $mail_order);
|
||||
|
||||
\Shared\Helpers\Helpers::send_email($email, \Shared\Helpers\Helpers::lang('potwierdzenie-zamowienia-ze-sklepu') . ' ' . $settings['firm_name'], $mail_order);
|
||||
\Shared\Helpers\Helpers::send_email($settings['contact_email'], 'Nowe zamówienie / ' . $settings['firm_name'] . ' / ' . $order['number'] . ' - ' . $order['client_surname'] . ' ' . $order['client_name'], $mail_order);
|
||||
|
||||
// zmiana statusu w realizacji jeżeli płatność przy odbiorze
|
||||
if ($payment_id == 3) {
|
||||
$order_tmp = new \shop\Order($order_id);
|
||||
$order_tmp->update_status(4, true);
|
||||
}
|
||||
|
||||
return $order_id;
|
||||
}
|
||||
|
||||
private function nullableString(string $value): ?string
|
||||
{
|
||||
$value = trim($value);
|
||||
|
||||
Reference in New Issue
Block a user