<?php
namespace BooklyCoupons\Backend\Modules\Coupons;
use BooklyCoupons\Lib;
use Bookly\Lib as BooklyLib;
/**
* Class Ajax
* @package BooklyCoupons\Backend\Modules\Coupons
*/
class Ajax extends BooklyLib\Base\Ajax
{
/**
* Get coupons list
*/
public static function getCoupons()
{
global $wpdb;
$columns = self::parameter( 'columns' );
$order = self::parameter( 'order', array() );
$filter = self::parameter( 'filter' );
$total = Lib\Entities\Coupon::query( 'c' )->count();
$query = Lib\Entities\Coupon::query( 'c' )
->select( 'DISTINCT c.id' )
->leftJoin( 'CouponService', 'cs', 'cs.coupon_id = c.id' )
->leftJoin( 'CouponStaff', 'cst', 'cst.coupon_id = c.id' )
->leftJoin( 'CouponCustomer', 'cc', 'cc.coupon_id = c.id' );
// Filters.
if ( $filter['code'] != '' ) {
$query->whereLike( 'c.code', '%' . BooklyLib\Query::escape( $filter['code'] ) . '%' );
}
if ( $filter['service'] != '' ) {
$query->where( 'cs.service_id', $filter['service'] );
}
if ( $filter['staff'] != '' ) {
$query->where( 'cst.staff_id', $filter['staff'] );
}
if ( $filter['customer'] != '' ) {
$query->where( 'cc.customer_id', $filter['customer'] );
}
if ( $filter['active'] ) {
$today = BooklyLib\Slots\DatePoint::now()->format( 'Y-m-d' );
$query->whereRaw(
'c.used < c.usage_limit AND (c.date_limit_start IS NULL OR %s >= c.date_limit_start) AND (c.date_limit_end IS NULL OR %s <= c.date_limit_end)',
array( $today, $today )
);
}
$ids = array();
foreach ( $query->fetchArray() as $row ) {
$ids[] = $row['id'];
}
$query = Lib\Entities\Coupon::query( 'c' )
->select( 'SQL_CALC_FOUND_ROWS c.*,
GROUP_CONCAT(DISTINCT cs.service_id) AS service_ids,
GROUP_CONCAT(DISTINCT cst.staff_id) AS staff_ids,
GROUP_CONCAT(DISTINCT cc.customer_id) AS customer_ids,
COUNT(DISTINCT cs.service_id) AS services,
COUNT(DISTINCT cst.staff_id) AS staff,
COUNT(DISTINCT cc.customer_id) AS customers'
)
->leftJoin( 'CouponService', 'cs', 'cs.coupon_id = c.id' )
->leftJoin( 'CouponStaff', 'cst', 'cst.coupon_id = c.id' )
->leftJoin( 'CouponCustomer', 'cc', 'cc.coupon_id = c.id' )
->whereIn( 'c.id', $ids )
->groupBy( 'c.id' );
foreach ( $order as $sort_by ) {
$query
->sortBy( str_replace( '.', '_', $columns[ $sort_by['column'] ]['data'] ) )
->order( $sort_by['dir'] == 'desc' ? BooklyLib\Query::ORDER_DESCENDING : BooklyLib\Query::ORDER_ASCENDING );
}
$coupons = $query
->limit( self::parameter( 'length' ) )
->offset( self::parameter( 'start' ) )
->fetchArray();
$filtered = (int) $wpdb->get_var( 'SELECT FOUND_ROWS()' );
foreach ( $coupons as &$coupon ) {
$coupon['service_ids'] = is_null( $coupon['service_ids'] ) ? array() : explode( ',', $coupon['service_ids'] );
$coupon['staff_ids'] = is_null( $coupon['staff_ids'] ) ? array() : explode( ',', $coupon['staff_ids'] );
$coupon['customer_ids'] = is_null( $coupon['customer_ids'] ) ? array() : explode( ',', $coupon['customer_ids'] );
$coupon['date_limit_start_formatted'] = is_null( $coupon['date_limit_start'] ) ? '' : BooklyLib\Utils\DateTime::formatDate( $coupon['date_limit_start'] );
$coupon['date_limit_end_formatted'] = is_null( $coupon['date_limit_end'] ) ? '' : BooklyLib\Utils\DateTime::formatDate( $coupon['date_limit_end'] );
}
BooklyLib\Utils\Tables::updateSettings( 'coupons', $columns, $order, $filter );
wp_send_json( array(
'draw' => (int) self::parameter( 'draw' ),
'recordsTotal' => $total,
'recordsFiltered' => $filtered,
'data' => $coupons,
) );
}
/**
* Create/update coupon
*/
public static function saveCoupon()
{
$params = array_map( function ( $value ) { return $value != '' ? $value : null; }, self::postParameters() );
if ( $params['code'] === null ) {
$params['code'] = '';
}
$duplicates_query = Lib\Entities\Coupon::query()->where( 'code', $params['code'] );
if ( isset ( $params['id'] ) ) {
$duplicates_query->whereNot( 'id', $params['id'] );
}
if ( $params['discount'] < 0 || $params['discount'] > 100 ) {
wp_send_json_error( array( 'message' => __( 'Discount should be between 0 and 100.', 'bookly' ) ) );
} elseif ( $params['deduction'] < 0 ) {
wp_send_json_error( array( 'message' => __( 'Deduction should be a positive number.', 'bookly' ) ) );
} elseif ( $params['min_appointments'] < 1 ) {
wp_send_json_error( array( 'message' => __( 'Min appointments should be greater than zero.', 'bookly' ) ) );
} elseif ( $params['max_appointments'] !== null && $params['max_appointments'] < 1 ) {
wp_send_json_error( array( 'message' => __( 'Max appointments should be greater than zero.', 'bookly' ) ) );
} elseif ( $duplicates_query->count() > 0 ) {
wp_send_json_error( array( 'message' => __( 'The code already exists', 'bookly' ) ) );
} else {
if ( isset ( $params['create_series'] ) ) {
if ( $params['mask'] == '' ) {
wp_send_json_error( array( 'message' => __( 'Please enter a non empty mask.', 'bookly' ) ) );
}
try {
$codes = Lib\CodeGenerator::generateUniqueCodeSeries( $params['mask'], $params['amount'] );
} catch ( \Exception $e ) {
wp_send_json_error( array( 'message' => sprintf(
__( 'It is not possible to generate %d codes for this mask. Only %d codes available.', 'bookly' ),
$params['amount'],
$e->getMessage()
) ) );
}
} else {
$codes = array( $params['code'] );
}
foreach ( $codes as $code ) {
$params['code'] = $code;
$form = new Forms\Coupon();
$form->bind( $params );
$coupon = $form->save();
// Services.
$service_ids = (array) self::parameter( 'service_ids' );
if ( empty ( $service_ids ) ) {
Lib\Entities\CouponService::query()
->delete()
->where( 'coupon_id', $coupon->getId() )
->execute();
} else {
Lib\Entities\CouponService::query()
->delete()
->where( 'coupon_id', $coupon->getId() )
->whereNotIn( 'service_id', $service_ids )
->execute();
$existing_services = Lib\Entities\CouponService::query()
->select( 'service_id' )
->where( 'coupon_id', $coupon->getId() )
->indexBy( 'service_id' )
->fetchArray();
foreach ( $service_ids as $service_id ) {
if ( ! isset ( $existing_services[ $service_id ] ) ) {
$coupon_service = new Lib\Entities\CouponService();
$coupon_service
->setCouponId( $coupon->getId() )
->setServi
没有合适的资源?快使用搜索试试~ 我知道了~
【WordPress插件】2022年最新版完整功能demo+插件v3.3.zip
共34个文件
php:33个
js:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 187 浏览量
2022-04-08
07:44:42
上传
评论
收藏 44KB ZIP 举报
温馨提示
"【WordPress插件】2022年最新版完整功能demo+插件v3.3 Bookly Coupons (Add-on) 邮费优惠券(附加)" ---------- 泰森云每天更新发布最新WordPress主题、HTML主题、WordPress插件、shopify主题、opencart主题、PHP项目源码、安卓项目源码、ios项目源码,更有超10000个资源可供选择,如有需要请站内联系。
资源推荐
资源详情
资源评论
收起资源包目录
【WordPress插件】2022年最新版完整功能demo+插件v3.3.zip (34个子文件)
bookly-addon-coupons
main.php 865B
frontend
modules
booking
templates
coupon_block.php 935B
Ajax.php 3KB
proxy_providers
Local.php 937B
autoload.php 626B
lib
base
Installer.php 7KB
Boot.php 4KB
CodeGenerator.php 3KB
Plugin.php 1KB
proxy_providers
Shared.php 2KB
Local.php 816B
Updater.php 4KB
notifications
assets
test
proxy_providers
Shared.php 457B
order
proxy_providers
Shared.php 1007B
Installer.php 6KB
entities
CouponStaff.php 2KB
Coupon.php 9KB
CouponCustomer.php 2KB
CouponService.php 2KB
backend
modules
coupons
forms
Coupon.php 719B
Page.php 4KB
templates
index.php 5KB
coupon.php 13KB
export.php 3KB
Ajax.php 14KB
resources
js
coupons.js 25KB
appearance
templates
coupon_block.php 1KB
show_coupons.php 311B
proxy_providers
Shared.php 815B
Local.php 532B
settings
templates
settings_tab.php 868B
proxy_providers
Shared.php 1KB
notifications
proxy_providers
Shared.php 499B
customers
proxy_providers
Shared.php 569B
共 34 条
- 1
资源评论
Lee达森
- 粉丝: 966
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 本科毕业设计基于C# wpf人脸识别的考勤系统的设计与实现源码.zip
- 基于Ruoyi+uniapp实现学生考勤系统 学生考勤源码+项目说明.zip
- feae6bc968ca68a099455d8b8a8dea35
- 基于Pytorch训练CIRAR10上分类算法.zip
- Pytorch-pytorch深度学习教程之Tensorboard.zip
- 基于C++和Python开发yolov8-face作为人脸检测器dlib作为人脸识别器的人脸考勤系统源码+项目说明.zip
- Pytorch-pytorch深度学习教程之变分自动编码器.zip
- Pytorch-pytorch深度学习教程之神经风格迁移.zip
- Pytorch-pytorch深度学习教程之深度残差网络.zip
- Pytorch-pytorch深度学习教程之循环神经网络.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功