初始上传

This commit is contained in:
2026-04-04 17:27:12 +08:00
parent 4d80d28eb4
commit b7e11774ee
11191 changed files with 1588469 additions and 0 deletions

363
addon/presale/model/Poster.php Executable file
View File

@@ -0,0 +1,363 @@
<?php
/**
* Niushop商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.niushop.com
* =========================================================
*/
namespace addon\presale\model;
use addon\postertemplate\model\PosterTemplate as PosterTemplateModel;
use app\model\BaseModel;
use app\model\system\Site;
use app\model\upload\Upload;
use app\model\web\Config;
use extend\Poster as PosterExtend;
/**
* 海报生成类
*/
class Poster extends BaseModel
{
/**
* 商品海报
*/
public function goods($app_type, $page, $qrcode_param, $promotion_type, $site_id)
{
try {
$goods_info = $this->getGoodsInfo($qrcode_param[ 'id' ]);
if (empty($goods_info)) return $this->error('未获取到商品信息');
//判断海报是否存在或停用
$template_info = $this->getTemplateInfo($goods_info[ 'template_id' ]);
$qrcode_info = $this->getGoodsQrcode($app_type, $page, $qrcode_param, $promotion_type, $site_id);
if ($qrcode_info[ 'code' ] < 0) return $qrcode_info;
$site_model = new Site();
$condition = array (
[ "site_id", "=", $site_id ]
);
$site_info = $site_model->getSiteInfo($condition);
if (!empty($qrcode_param[ 'source_member' ])) {
$member_info = $this->getMemberInfo($qrcode_param[ 'source_member' ]);
}
$upload_config_model = new Config();
$upload_config_result = $upload_config_model->getDefaultImg($site_id);
if (empty($goods_info[ 'template_id' ]) || empty($template_info) || $template_info[ 'template_status' ] == 0) {
$poster_width = 720;
$poster_height = 1150;
$poster = new PosterExtend($poster_width, $poster_height);
$option = [
[
'action' => 'setBackground', // 设背景色
'data' => [255, 255, 255]
],
[
'action' => 'imageCopy', // 写入商品图
'data' => [
img($goods_info['sku_image'], 'mid'),
50,
165,
620,
620,
'square',
true,
1
]
],
[
'action' => 'imageText', // 写入商品名称
'data' => [
$goods_info['sku_name'],
22,
[35, 35, 35],
50,
915,
360,
2,
true,
1
]
],
[
'action' => 'imageCopy', // 写入商品二维码
'data' => [
$qrcode_info['data']['path'],
435,
825,
240,
240,
'square',
0,
1
]
],
[
'action' => 'imageText', // 写入提示
'data' => [
'长按识别二维码',
19,
[102, 102, 102],
465,
1110,
490,
1,
1,
1
]
],
[
'action' => 'imageText', // 写入商品价格
'data' => [
'定金¥' . $goods_info[ 'presale_deposit' ] . '可抵¥' . $goods_info[ 'presale_price' ],
20,
[255, 0, 0],
50,
862,
490,
2,
true,
1
]
],
];
} else {
$condition = [
[ 'template_id', '=', $goods_info[ 'template_id' ] ],
[ 'site_id', '=', $site_id ]
];
$poster_template_model = new PosterTemplateModel();
$poster_data = $poster_template_model->getPosterTemplateInfo($condition);
$poster_data[ 'data' ][ 'template_json' ] = json_decode($poster_data[ 'data' ][ 'template_json' ], true);
$poster_width = 720;
$poster_height = 1280;
$poster = new PosterExtend($poster_width, $poster_height);
$fontRate = 0.725; // 20px 等于 14.5磅,换算比率 1px = 0.725磅
$option = [
[
'action' => 'imageText', // 写入店铺名称
'data' => [
$site_info[ 'data' ][ 'site_name' ],
$poster_data[ 'data' ][ 'template_json' ][ 'store_name_font_size' ] * $fontRate * 2,
hex2rgb($poster_data[ 'data' ][ 'template_json' ][ 'store_name_color' ]),
$poster_data[ 'data' ][ 'template_json' ][ 'store_name_left' ] * 2,
( $poster_data[ 'data' ][ 'template_json' ][ 'store_name_top' ] + $poster_data[ 'data' ][ 'template_json' ][ 'store_name_font_size' ] ) * 2,
$poster_data[ 'data' ][ 'template_json' ][ 'store_name_width' ] * 2,
$poster_data[ 'data' ][ 'template_json' ][ 'store_name_height' ] * 2,
true,
$poster_data[ 'data' ][ 'template_json' ][ 'store_name_is_show' ]
]
],
[
'action' => 'imageCopy', // 店铺logo
'data' => [
!empty($site_info[ 'data' ][ 'logo_square' ]) ? $site_info[ 'data' ][ 'logo_square' ] : getUrl() . '/app/shop/view/public/img/shop_logo.png',
$poster_data[ 'data' ][ 'template_json' ][ 'store_logo_left' ] * 2,
$poster_data[ 'data' ][ 'template_json' ][ 'store_logo_top' ] * 2,
$poster_data[ 'data' ][ 'template_json' ][ 'store_logo_width' ] * 2,
$poster_data[ 'data' ][ 'template_json' ][ 'store_logo_height' ] * 2,
'square',
true,
$poster_data[ 'data' ][ 'template_json' ][ 'store_logo_is_show' ]
]
],
[
'action' => 'imageCopy', // 写入商品图
'data' => [
$goods_info[ 'sku_image' ],
$poster_data[ 'data' ][ 'template_json' ][ 'goods_img_left' ] * 2,
$poster_data[ 'data' ][ 'template_json' ][ 'goods_img_top' ] * 2,
$poster_data[ 'data' ][ 'template_json' ][ 'goods_img_width' ] * 2,
$poster_data[ 'data' ][ 'template_json' ][ 'goods_img_height' ] * 2,
!empty($poster_data[ 'data' ][ 'template_json' ][ 'goods_img_shape' ]) ? $poster_data[ 'data' ][ 'template_json' ][ 'goods_img_shape' ] : 'square',
0,
$poster_data[ 'data' ][ 'template_json' ][ 'goods_img_is_show' ]
]
],
[
'action' => 'imageText', // 写入商品名称
'data' => [
$goods_info[ 'sku_name' ],
$poster_data[ 'data' ][ 'template_json' ][ 'goods_name_font_size' ] * $fontRate * 2,
hex2rgb($poster_data[ 'data' ][ 'template_json' ][ 'goods_name_color' ]),
$poster_data[ 'data' ][ 'template_json' ][ 'goods_name_left' ] * 2,
( $poster_data[ 'data' ][ 'template_json' ][ 'goods_name_top' ] + $poster_data[ 'data' ][ 'template_json' ][ 'goods_name_font_size' ] ) * 2,
$poster_data[ 'data' ][ 'template_json' ][ 'goods_name_width' ] * 2,
1,//文本行数 $poster_data['data']['template_json']['goods_name_height']*2,
true,
$poster_data[ 'data' ][ 'template_json' ][ 'goods_name_is_show' ]
]
],
[
'action' => 'imageCopy', // 写入商品二维码
'data' => [
$qrcode_info[ 'data' ][ 'path' ],
$poster_data[ 'data' ][ 'qrcode_left' ] * 2,
$poster_data[ 'data' ][ 'qrcode_top' ] * 2,
$poster_data[ 'data' ][ 'qrcode_width' ] * 2,
$poster_data[ 'data' ][ 'qrcode_height' ] * 2,
'square',
0,
1
]
],
[
'action' => 'imageText', // 写入商品价格
'data' => [
'定金¥' . $goods_info[ 'presale_deposit' ] . '可抵¥' . $goods_info[ 'presale_price' ],
$poster_data[ 'data' ][ 'template_json' ][ 'goods_price_font_size' ] * $fontRate * 2,
hex2rgb($poster_data[ 'data' ][ 'template_json' ][ 'goods_price_color' ]),
$poster_data[ 'data' ][ 'template_json' ][ 'goods_price_left' ] * 2,
( $poster_data[ 'data' ][ 'template_json' ][ 'goods_price_top' ] + $poster_data[ 'data' ][ 'template_json' ][ 'goods_price_font_size' ] ) * 2,
$poster_data[ 'data' ][ 'template_json' ][ 'goods_price_width' ] * 2,
$poster_data[ 'data' ][ 'template_json' ][ 'goods_price_height' ] * 2,
true,
$poster_data[ 'data' ][ 'template_json' ][ 'goods_price_is_show' ]
]
],
];
if (!empty($member_info)) {
$member_option = [
[
'action' => 'imageCopy', // 写入用户头像
'data' => [
!empty($member_info[ 'headimg' ]) ? $member_info[ 'headimg' ] : $upload_config_result[ 'data' ][ 'value' ][ 'head' ],
$poster_data[ 'data' ][ 'template_json' ][ 'headimg_left' ] * 2,
$poster_data[ 'data' ][ 'template_json' ][ 'headimg_top' ] * 2,
$poster_data[ 'data' ][ 'template_json' ][ 'headimg_width' ] * 2,
$poster_data[ 'data' ][ 'template_json' ][ 'headimg_height' ] * 2,
!empty($poster_data[ 'data' ][ 'template_json' ][ 'headimg_shape' ]) ? $poster_data[ 'data' ][ 'template_json' ][ 'headimg_shape' ] : 'square',
0,
$poster_data[ 'data' ][ 'template_json' ][ 'headimg_is_show' ]
]
],
[
'action' => 'imageText', // 写入分享人昵称
'data' => [
$member_info[ 'nickname' ],
$poster_data[ 'data' ][ 'template_json' ][ 'nickname_font_size' ] * $fontRate * 2,
hex2rgb($poster_data[ 'data' ][ 'template_json' ][ 'nickname_color' ]),
$poster_data[ 'data' ][ 'template_json' ][ 'nickname_left' ] * 2,
( $poster_data[ 'data' ][ 'template_json' ][ 'nickname_top' ] + $poster_data[ 'data' ][ 'template_json' ][ 'nickname_font_size' ] ) * 2,
$poster_data[ 'data' ][ 'template_json' ][ 'nickname_width' ] * 2,
$poster_data[ 'data' ][ 'template_json' ][ 'nickname_height' ] * 2,
0,
$poster_data[ 'data' ][ 'template_json' ][ 'nickname_is_show' ]
]
],
];
$option = array_merge($option, $member_option);
}
if (!empty($poster_data[ 'data' ][ 'background' ])) {
list($width, $height, $type, $attr) = getimagesize(img($poster_data[ 'data' ][ 'background' ]));
$back_ground = [
[
'action' => 'imageCopy', // 写入背景图
'data' => [
img($poster_data[ 'data' ][ 'background' ]),
0,
0,
$poster_width,
$poster_height,
'square',
0,
1
]
],
];
} else {
$back_ground = [
[
'action' => 'setBackground', // 设背景色
'data' => [ 255, 255, 255 ]
],
];
}
$ground = [
[
'action' => 'setBackground',
'data' => [ 255, 255, 255 ]
]
];
$option = array_merge($ground, $back_ground, $option);
}
$option_res = $poster->create($option);
if (is_array($option_res)) return $option_res;
$res = $option_res->jpeg('upload/poster/goods', 'goods_' . $promotion_type . '_' . $goods_info[ 'presale_id' ] . '_' . $qrcode_param[ 'source_member' ] . '_' . time() . '_' . $app_type);
if ($res[ 'code' ] == 0) {
$upload = new Upload($site_id);
$cloud_res = $upload->fileCloud($res[ 'data' ][ 'path' ]);
if ($cloud_res[ 'code' ] >= 0) {
return $this->success([ "path" => $cloud_res[ 'data' ] ]);
} else {
return $this->error();
}
}
return $res;
} catch (\Exception $e) {
return $this->error($e->getMessage() . $e->getFile() . $e->getLine());
}
}
/**
* 获取用户信息
* @param unknown $member_id
*/
private function getMemberInfo($member_id)
{
$info = model('member')->getInfo([ 'member_id' => $member_id ], 'nickname,headimg');
return $info;
}
/**
* 获取商品信息
* @param unknown $sku_id
*/
private function getGoodsInfo($id)
{
$join = [
[ 'goods_sku sku', 'pbg.sku_id = sku.sku_id', 'inner' ],
];
$field = 'pbg.presale_id,pbg.presale_deposit,pbg.presale_price,sku.sku_name,sku.sku_image,sku.template_id';
$info = model('promotion_presale_goods')->getInfo([ 'pbg.presale_id' => $id ], $field, 'pbg', $join);
return $info;
}
/**
* 获取商品二维码
* @param unknown $app_type 请求类型
* @param unknown $page uniapp页面路径
* @param unknown $qrcode_param 二维码携带参数
* @param string $promotion_type 活动类型 null为无活动
*/
private function getGoodsQrcode($app_type, $page, $qrcode_param, $promotion_type, $site_id)
{
$res = event('Qrcode', [
'site_id' => $site_id,
'app_type' => $app_type,
'type' => 'create',
'data' => $qrcode_param,
'page' => $page,
'qrcode_path' => 'upload/qrcode/goods',
'qrcode_name' => 'goods_' . $promotion_type . '_' . $qrcode_param[ 'id' ] . '_' . $qrcode_param[ 'source_member' ] . '_' . $site_id,
], true);
return $res;
}
/**
* 获取海报信息
* @param unknown $template_id
*/
private function getTemplateInfo($template_id)
{
$info = model('poster_template')->getInfo([ 'template_id' => $template_id ], 'template_id,template_status');
return $info;
}
}

896
addon/presale/model/Presale.php Executable file
View File

@@ -0,0 +1,896 @@
<?php
/**
* Niushop商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.niushop.com
* =========================================================
*/
namespace addon\presale\model;
use app\model\BaseModel;
use app\model\goods\Goods;
use app\model\system\Config as ConfigModel;
use app\model\system\Cron;
use think\facade\Cache;
use think\facade\Db;
/**
* 预售活动
*/
class Presale extends BaseModel
{
private $status = [
0 => '未开始',
1 => '进行中',
2 => '已结束',
3 => '已关闭'
];
/**
* 获取预售活动状态
* @return array
*/
public function getPresaleStatus()
{
return $this->success($this->status);
}
/**
* 添加预售
* @param $presale_data
* @param $goods
* @param $sku_list
* @return array
*/
public function addPresale($presale_data, $goods, $sku_list)
{
if (empty($goods[ 'sku_ids' ])) {
return $this->error('', '该活动至少需要一个商品参与');
}
$presale_data[ 'create_time' ] = time();
//查询该商品是否存在预售
$presale_info = model('promotion_presale_goods')->getInfo(
[
[ 'ppg.site_id', '=', $presale_data[ 'site_id' ] ],
[ 'pp.status', 'in', '0,1' ],
[ 'ppg.goods_id', 'in', $goods[ 'goods_ids' ] ],
[ '', 'exp', Db::raw('not ( (`start_time` > ' . $presale_data[ 'end_time' ] . ' and `start_time` > ' . $presale_data[ 'start_time' ] . ' ) or (`end_time` < ' . $presale_data[ 'start_time' ] . ' and `end_time` < ' . $presale_data[ 'end_time' ] . '))') ]
], 'ppg.id', 'ppg', [ [ 'promotion_presale pp', 'pp.presale_id = ppg.presale_id', 'left' ] ]
);
if (!empty($presale_info)) {
return $this->error('', "当前商品在当前时间段内已经存在预售活动");
}
if (time() > $presale_data[ 'end_time' ]) {
return $this->error('', '当前时间不能大于结束时间');
}
if ($presale_data[ 'start_time' ] <= time()) {
$presale_data[ 'status' ] = 1;
$presale_data[ 'status_name' ] = $this->status[ 1 ];
} else {
$presale_data[ 'status' ] = 0;
$presale_data[ 'status_name' ] = $this->status[ 0 ];
}
model("promotion_presale")->startTrans();
try {
foreach ($goods[ 'goods_ids' ] as $goods_id) {
//添加预售活动
$presale_data[ 'goods_id' ] = $goods_id;
$presale_id = model("promotion_presale")->add($presale_data);
$presale_stock = 0;
$sku_list_data = [];
foreach ($sku_list as $k => $sku) {
if ($sku[ 'goods_id' ] == $goods_id) {
$presale_stock += $sku[ 'presale_stock' ];//总库存
$sku_list_data[] = [
'site_id' => $presale_data[ 'site_id' ],
'presale_id' => $presale_id,
'goods_id' => $goods_id,
'sku_id' => $sku[ 'sku_id' ],
'presale_stock' => $sku[ 'presale_stock' ],
'presale_deposit' => $sku[ 'presale_deposit' ],
'presale_price' => $sku[ 'presale_price' ],
];
}
}
array_multisort(array_column($sku_list_data, 'presale_deposit'), SORT_ASC, $sku_list_data);
model('promotion_presale_goods')->addList($sku_list_data);
model('promotion_presale')->update(
[
'presale_stock' => $presale_stock,
'presale_deposit' => $sku_list_data[ 0 ][ 'presale_deposit' ],
'presale_price' => $sku_list_data[ 0 ][ 'presale_price' ],
'sku_id' => $sku_list_data[ 0 ][ 'sku_id' ]
],
[ [ 'presale_id', '=', $presale_id ] ]
);
$cron = new Cron();
if ($presale_data[ 'status' ] == 1) {
$goods = new Goods();
$goods->modifyPromotionAddon($goods_id, [ 'presale' => $presale_id ]);
$cron->addCron(1, 0, "预售活动关闭", "ClosePresale", $presale_data[ 'end_time' ], $presale_id);
} else {
$cron->addCron(1, 0, "预售活动开启", "OpenPresale", $presale_data[ 'start_time' ], $presale_id);
$cron->addCron(1, 0, "预售活动关闭", "ClosePresale", $presale_data[ 'end_time' ], $presale_id);
}
}
model('promotion_presale')->commit();
return $this->success();
} catch (\Exception $e) {
model('promotion_presale')->rollback();
return $this->error('', $e->getMessage());
}
}
/**
* 编辑预售
* @param $presale_data
* @param $goods
* @param $sku_list
* @return array
*/
public function editPresale($presale_data, $goods, $sku_list)
{
if (empty($goods[ 'sku_ids' ])) {
return $this->error('', '该活动至少需要一个商品参与');
}
//查询该商品是否存在预售
$presale_info = model('promotion_presale_goods')->getInfo(
[
[ 'ppg.site_id', '=', $presale_data[ 'site_id' ] ],
[ 'pp.status', 'in', '0,1' ],
[ 'ppg.presale_id', '<>', $presale_data[ 'presale_id' ] ],
[ 'ppg.sku_id', 'in', $goods[ 'sku_ids' ] ],
[ '', 'exp', Db::raw('not ( (`start_time` > ' . $presale_data[ 'end_time' ] . ' and `start_time` > ' . $presale_data[ 'start_time' ] . ' ) or (`end_time` < ' . $presale_data[ 'start_time' ] . ' and `end_time` < ' . $presale_data[ 'end_time' ] . '))') ]
], 'ppg.id', 'ppg', [ [ 'promotion_presale pp', 'pp.presale_id = ppg.presale_id', 'left' ] ]
);
if (!empty($presale_info)) {
return $this->error('', "当前商品在当前时间段内已经存在预售活动");
}
$presale_count = model("promotion_presale")->getCount([ [ 'presale_id', '=', $presale_data[ 'presale_id' ] ], [ 'site_id', '=', $presale_data[ 'site_id' ] ] ]);
if ($presale_count == 0) {
return $this->error('', '该预售活动不存在');
}
$cron = new Cron();
if (time() > $presale_data[ 'end_time' ]) {
return $this->error('', '当前时间不能大于结束时间');
}
if ($presale_data[ 'start_time' ] <= time()) {
$presale_data[ 'status' ] = 1;
$presale_data[ 'status_name' ] = $this->status[ 1 ];
} else {
$presale_data[ 'status' ] = 0;
$presale_data[ 'status_name' ] = $this->status[ 0 ];
}
$presale_data[ 'modify_time' ] = time();
model('promotion_presale')->startTrans();
try {
$presale_stock = 0;
$sku_list_data = [];
foreach ($sku_list as $k => $sku) {
$count = model('promotion_presale_goods')->getCount([ [ 'sku_id', '=', $sku[ 'sku_id' ] ], [ 'presale_id', '=', $presale_data[ 'presale_id' ] ] ]);
$is_delete = $sku[ 'is_delete' ];
unset($sku[ 'is_delete' ]);
if ($is_delete == 2) {//是否参与 1参与 2不参与
if ($count) {
model('promotion_presale_goods')->delete([ [ 'sku_id', '=', $sku[ 'sku_id' ] ], [ 'presale_id', '=', $presale_data[ 'presale_id' ] ] ]);
}
} else {
$presale_stock += $sku[ 'presale_stock' ];//总库存
$sku_data = [
'site_id' => $presale_data[ 'site_id' ],
'presale_id' => $presale_data[ 'presale_id' ],
'goods_id' => $goods[ 'goods_id' ],
'sku_id' => $sku[ 'sku_id' ],
'presale_stock' => $sku[ 'presale_stock' ],
'presale_deposit' => $sku[ 'presale_deposit' ],
'presale_price' => $sku[ 'presale_price' ],
];
if ($count > 0) {
model('promotion_presale_goods')->update($sku_data, [ [ 'sku_id', '=', $sku[ 'sku_id' ] ], [ 'presale_id', '=', $presale_data[ 'presale_id' ] ] ]);
} else {
model('promotion_presale_goods')->add($sku_data);
}
$sku_list_data[] = $sku_data;
}
}
array_multisort(array_column($sku_list_data, 'presale_deposit'), SORT_ASC, $sku_list_data);
model("promotion_presale")->update(
array_merge($presale_data, [
'presale_stock' => $presale_stock,
'presale_deposit' => $sku_list_data[ 0 ][ 'presale_deposit' ],
'presale_price' => $sku_list_data[ 0 ][ 'presale_price' ],
'sku_id' => $sku_list_data[ 0 ][ 'sku_id' ]
]),
[ [ 'presale_id', '=', $presale_data[ 'presale_id' ] ] ]
);
if ($presale_data[ 'start_time' ] <= time()) {
$goods_model = new Goods();
$goods_model->modifyPromotionAddon($goods[ 'goods_id' ], [ 'presale' => $presale_data[ 'presale_id' ] ]);
//活动商品启动
$this->cronOpenPresale($presale_data[ 'presale_id' ]);
$cron->deleteCron([ [ 'event', '=', 'OpenPresale' ], [ 'relate_id', '=', $presale_data[ 'presale_id' ] ] ]);
$cron->deleteCron([ [ 'event', '=', 'ClosePresale' ], [ 'relate_id', '=', $presale_data[ 'presale_id' ] ] ]);
$cron->addCron(1, 0, "预售活动关闭", "ClosePresale", $presale_data[ 'end_time' ], $presale_data[ 'presale_id' ]);
} else {
$cron->deleteCron([ [ 'event', '=', 'OpenPresale' ], [ 'relate_id', '=', $presale_data[ 'presale_id' ] ] ]);
$cron->deleteCron([ [ 'event', '=', 'ClosePresale' ], [ 'relate_id', '=', $presale_data[ 'presale_id' ] ] ]);
$cron->addCron(1, 0, "预售活动开启", "OpenPresale", $presale_data[ 'start_time' ], $presale_data[ 'presale_id' ]);
$cron->addCron(1, 0, "预售活动关闭", "ClosePresale", $presale_data[ 'end_time' ], $presale_data[ 'presale_id' ]);
}
model('promotion_presale')->commit();
return $this->success();
} catch (\Exception $e) {
model('promotion_presale')->rollback();
return $this->error('', $e->getMessage());
}
}
/**
* 增加预售组人数及购买人数
* @param array $data
* @param array $condition
* @return array
*/
public function editPresaleNum($data = [], $condition = [])
{
$res = model('promotion_presale')->update($data, $condition);
return $this->success($res);
}
/**
* 删除预售活动
* @param $presale_id
* @param $site_id
* @return array|\multitype
*/
public function deletePresale($presale_id, $site_id)
{
//预售信息
$presale_info = model('promotion_presale')->getInfo([ [ 'presale_id', '=', $presale_id ], [ 'site_id', '=', $site_id ] ], 'status');
if ($presale_info) {
if ($presale_info[ 'status' ] != 1) {
$res = model('promotion_presale')->delete([ [ 'presale_id', '=', $presale_id ], [ 'site_id', '=', $site_id ] ]);
if ($res) {
//删除商品
model('promotion_presale_goods')->delete([ [ 'presale_id', '=', $presale_id ], [ 'site_id', '=', $site_id ] ]);
$cron = new Cron();
$cron->deleteCron([ [ 'event', '=', 'OpenPresale' ], [ 'relate_id', '=', $presale_id ] ]);
$cron->deleteCron([ [ 'event', '=', 'ClosePresale' ], [ 'relate_id', '=', $presale_id ] ]);
}
return $this->success($res);
} else {
return $this->error('', '预售活动进行中,请先关闭该活动');
}
} else {
return $this->error('', '预售活动不存在');
}
}
/**
* 关闭预售活动
* @param $presale_id
* @param $site_id
* @return array
*/
public function finishPresale($presale_id, $site_id)
{
//预售信息
$presale_info = model('promotion_presale')->getInfo([ [ 'presale_id', '=', $presale_id ], [ 'site_id', '=', $site_id ] ], 'status,goods_id');
if (!empty($presale_info)) {
if ($presale_info[ 'status' ] != 3) {
$res = model('promotion_presale')->update([ 'status' => 3, 'status_name' => $this->status[ 3 ] ], [ [ 'presale_id', '=', $presale_id ], [ 'site_id', '=', $site_id ] ]);
$cron = new Cron();
$cron->deleteCron([ [ 'event', '=', 'OpenPresale' ], [ 'relate_id', '=', $presale_id ] ]);
$cron->deleteCron([ [ 'event', '=', 'ClosePresale' ], [ 'relate_id', '=', $presale_id ] ]);
$goods = new Goods();
$goods->modifyPromotionAddon($presale_info[ 'goods_id' ], [ 'presale' => $presale_id ], true);
$presale_order = new PresaleOrderCommon();
$presale_order->depositPresaleOrderClose([ [ 'presale_id', '=', $presale_id ], [ 'site_id', '=', $site_id ] ]);
return $this->success($res);
} else {
$this->error('', '该预售活动已关闭');
}
} else {
$this->error('', '该预售活动不存在');
}
}
/**
* 获取预售信息
* @param array $condition
* @param string $field
* @return array
*/
public function getPresaleInfo($condition = [], $field = '*')
{
$presale_info = model("promotion_presale")->getInfo($condition, $field);
return $this->success($presale_info);
}
/**
* 获取预售详细信息
* @param $presale_id
* @param $site_id
* @return array
*/
public function getPresaleDetail($presale_id, $site_id)
{
//预售信息
$alias = 'p';
$join = [
[
'goods g',
'g.goods_id = p.goods_id',
'inner'
]
];
$presale_info = model("promotion_presale")->getInfo(
[
[ 'p.presale_id', '=', $presale_id ], [ 'p.site_id', '=', $site_id ],
[ 'g.goods_state', '=', 1 ], [ 'g.is_delete', '=', 0 ]
], 'p.*', $alias, $join
);
if (!empty($presale_info)) {
//商品sku信息
$goods_list = model('goods_sku')->getList(
[ [ 'goods_id', '=', $presale_info[ 'goods_id' ] ] ],
'goods_id,sku_id,sku_name,price,sku_images,stock,sku_image'
);
foreach ($goods_list as $k => $v) {
$v[ 'stock' ] = numberFormat($v[ 'stock' ]);
$presale_goods = model('promotion_presale_goods')->getInfo(
[ [ 'presale_id', '=', $presale_id ], [ 'sku_id', '=', $v[ 'sku_id' ] ] ],
'presale_stock,presale_deposit,presale_price'
);
if (empty($presale_goods)) {
$presale_goods = [
'presale_stock' => 0,
'presale_deposit' => 0,
'presale_price' => 0
];
}
$goods_list[ $k ] = array_merge($v, $presale_goods);
}
array_multisort(array_column($goods_list, 'presale_price'), SORT_DESC, $goods_list);
$presale_info[ 'sku_list' ] = $goods_list;
}
return $this->success($presale_info);
}
/**
* 获取预售详细信息
* @param $presale_id
* @param $site_id
* @return array
*/
public function getPresaleJoinGoodsList($presale_id, $site_id)
{
//预售信息
$alias = 'p';
$join = [
[ 'goods g', 'g.goods_id = p.goods_id', 'inner' ]
];
$presale_info = model("promotion_presale")->getInfo(
[
[ 'p.presale_id', '=', $presale_id ], [ 'p.site_id', '=', $site_id ],
[ 'g.goods_state', '=', 1 ], [ 'g.is_delete', '=', 0 ]
], 'p.*', $alias, $join
);
if (!empty($presale_info)) {
$goods_list = model('promotion_presale_goods')->getList(
[ [ 'ppg.presale_id', '=', $presale_info[ 'presale_id' ] ] ],
'ppg.presale_stock,ppg.presale_deposit,ppg.presale_price,sku.sku_id,sku.sku_name,sku.price,sku.sku_image,sku.stock',
'', 'ppg', [ [ 'goods_sku sku', 'sku.sku_id = ppg.sku_id', 'inner' ] ]
);
foreach ($goods_list as $k => $v) {
$goods_list[ $k ][ 'stock' ] = numberFormat($goods_list[ $k ][ 'stock' ]);
}
$presale_info[ 'sku_list' ] = $goods_list;
}
return $this->success($presale_info);
}
/**
* 预售商品详情
* @param array $condition
* @param string $field
* @return array
*/
public function getPresaleGoodsDetail($condition = [], $field = '')
{
$alias = 'ppg';
if (empty($field)) {
$field = 'ppg.id,ppg.presale_id,ppg.goods_id,ppg.sku_id,ppg.presale_stock,ppg.presale_stock as stock,ppg.presale_deposit,ppg.presale_price,(pp.sale_num + g.virtual_sale) as sale_num,pp.presale_name,
pp.presale_num,pp.start_time,pp.end_time,pp.pay_start_time,pp.pay_end_time,pp.deliver_type,pp.deliver_time,sku.site_id,sku.sku_name,sku.sku_spec_format,sku.price,sku.promotion_type,pp.remark,
sku.click_num,(g.sale_num + g.virtual_sale) as goods_sale_num,sku.collect_num,sku.sku_image,sku.sku_images,sku.site_id,sku.goods_content,sku.goods_state,sku.is_virtual,
sku.is_free_shipping,sku.goods_spec_format,sku.goods_attr_format,sku.introduction,sku.unit,sku.video_url,sku.evaluate,sku.goods_service_ids,sku.support_trade_type,
g.goods_image,g.goods_stock,g.goods_name,sku.qr_id,g.stock_show,g.sale_show,g.label_name';
}
$join = [
[ 'goods_sku sku', 'ppg.sku_id = sku.sku_id', 'inner' ],
[ 'goods g', 'g.goods_id = sku.goods_id', 'inner' ],
[ 'promotion_presale pp', 'ppg.presale_id = pp.presale_id', 'inner' ],
];
$presale_goods_info = model('promotion_presale_goods')->getInfo($condition, $field, $alias, $join);
if (!empty($presale_goods_info)) {
if (isset($presale_goods_info[ 'goods_sale_num' ])) {
$presale_goods_info[ 'goods_sale_num' ] = numberFormat($presale_goods_info[ 'goods_sale_num' ]);
}
if (isset($presale_goods_info[ 'goods_stock' ])) {
$presale_goods_info[ 'goods_stock' ] = numberFormat($presale_goods_info[ 'goods_stock' ]);
}
}
return $this->success($presale_goods_info);
}
/**
* 预售商品详情
* @param array $condition
* @return array
*/
public function getPresaleGoodsSkuList($condition = [])
{
$alias = 'ppg';
$field = 'ppg.id,ppg.presale_id,ppg.sku_id,ppg.presale_stock,ppg.presale_stock as stock,ppg.presale_deposit,ppg.presale_price,sku.sku_name,
sku.sku_spec_format,sku.price,sku.sku_image,sku.sku_images,sku.goods_spec_format,g.goods_image';
$join = [
[ 'promotion_presale pp', 'ppg.presale_id = pp.presale_id', 'inner' ],
[ 'goods_sku sku', 'ppg.sku_id = sku.sku_id', 'inner' ],
[ 'goods g', 'g.goods_id = sku.goods_id', 'inner' ],
];
$list = model('promotion_presale_goods')->getList($condition, $field, 'ppg.id asc', $alias, $join);
return $this->success($list);
}
/**
* 获取预售列表
* @param array $condition
* @param string $field
* @param string $order
* @param string $limit
*/
public function getPresaleList($condition = [], $field = '', $order = '', $limit = null)
{
$alias = 'pp';
if (empty($field)) {
$field = 'pp.*,g.price,g.goods_name,g.goods_image,(g.sale_num + g.virtual_sale) as goods_sale_num,g.unit,g.goods_stock,g.recommend_way';
}
$join = [
[ 'goods g', 'pp.goods_id = g.goods_id', 'inner' ]
];
$list = model('promotion_presale')->getList($condition, $field, $order, $alias, $join, '', $limit);
foreach ($list as $k => $v) {
if (isset($v[ 'goods_sale_num' ])) {
$list[ $k ][ 'goods_sale_num' ] = numberFormat($list[ $k ][ 'goods_sale_num' ]);
}
if (isset($v[ 'goods_stock' ])) {
$list[ $k ][ 'goods_stock' ] = numberFormat($list[ $k ][ 'goods_stock' ]);
}
}
return $this->success($list);
}
/**
* 获取预售分页列表
* @param array $condition
* @param number $page
* @param string $page_size
* @param string $order
* @param string $field
*/
public function getPresalePageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '')
{
$field = 'p.*,g.goods_name,g.goods_image,g.price';
$alias = 'p';
$join = [
[
'goods g',
'p.goods_id = g.goods_id',
'inner'
]
];
$list = model('promotion_presale')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
return $this->success($list);
}
/**
* 获取预售商品列表
* @param $presale_id
* @param $site_id
* @return array
*/
public function getPresaleGoodsList($presale_id, $site_id)
{
$field = 'pbg.*,sku.sku_name,sku.price,sku.sku_image,sku.stock';
$alias = 'pbg';
$join = [
[
'goods g',
'g.goods_id = pbg.goods_id',
'inner'
],
[
'goods_sku sku',
'sku.sku_id = pbg.sku_id',
'inner'
]
];
$condition = [
[ 'pbg.presale_id', '=', $presale_id ], [ 'pbg.site_id', '=', $site_id ],
[ 'g.is_delete', '=', 0 ], [ 'g.goods_state', '=', 1 ]
];
$list = model('promotion_presale_goods')->getList($condition, $field, '', $alias, $join);
foreach ($list as $k => $v) {
$list[ $k ][ 'stock' ] = numberFormat($list[ $k ][ 'stock' ]);
}
return $this->success($list);
}
/**
* 获取预售商品分页列表
* @param array $condition
* @param number $page
* @param string $page_size
* @param string $order
* @param string $field
*/
public function getPresaleGoodsPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'pp.presale_id desc', $field = '')
{
$alias = 'pp';
if (empty($field)) {
$field = 'pp.*,g.price,g.goods_name,g.goods_image,(g.sale_num + g.virtual_sale) as goods_sale_num,g.unit,g.goods_stock,g.recommend_way';
}
$join = [
[ 'goods g', 'pp.goods_id = g.goods_id', 'inner' ]
];
$res = model('promotion_presale')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
foreach ($res[ 'list' ] as $k => $v) {
if (isset($v[ 'goods_sale_num' ])) {
$res[ 'list' ][ $k ][ 'goods_sale_num' ] = numberFormat($res[ 'list' ][ $k ][ 'goods_sale_num' ]);
}
if (isset($v[ 'goods_stock' ])) {
$res[ 'list' ][ $k ][ 'goods_stock' ] = numberFormat($res[ 'list' ][ $k ][ 'goods_stock' ]);
}
}
return $this->success($res);
}
/**
* 开启预售活动
* @param $presale_id
* @return array|\multitype
*/
public function cronOpenPresale($presale_id)
{
$presale_info = model('promotion_presale')->getInfo([ [ 'presale_id', '=', $presale_id ] ], 'status,goods_id');
if (!empty($presale_info)) {
if ($presale_info[ 'status' ] == 0) {
$res = model('promotion_presale')->update([ 'status' => 1, 'status_name' => $this->status[ 1 ] ], [ [ 'presale_id', '=', $presale_id ] ]);
$goods = new Goods();
$goods->modifyPromotionAddon($presale_info[ 'goods_id' ], [ 'presale' => $presale_id ]);
return $this->success($res);
} else {
return $this->error("", "预售活动已开启或者关闭");
}
} else {
return $this->error("", "预售活动不存在");
}
}
/**
* 关闭预售活动
* @param $presale_id
* @return array|\multitype
*/
public function cronClosePresale($presale_id)
{
$presale_info = model('promotion_presale')->getInfo([ [ 'presale_id', '=', $presale_id ] ], 'status,goods_id');
if (!empty($presale_info)) {
if ($presale_info[ 'status' ] == 1) {
$res = model('promotion_presale')->update([ 'status' => 2, 'status_name' => $this->status[ 2 ] ], [ [ 'presale_id', '=', $presale_id ] ]);
$goods = new Goods();
$goods->modifyPromotionAddon($presale_info[ 'goods_id' ], [ 'presale' => $presale_id ], true);
return $this->success($res);
} else {
return $this->error("", "该活动已结束");
}
} else {
return $this->error("", "预售活动不存在");
}
}
/**
* 判断规格值是否禁用
* @param $presale_id
* @param $site_id
* @param string $goods_spec_format
* @return int|mixed
*/
public function getGoodsSpecFormat($presale_id, $site_id, $goods_spec_format = '')
{
//获取活动参与的商品sku_ids
$sku_ids = model('promotion_presale_goods')->getColumn([ [ 'presale_id', '=', $presale_id ], [ 'site_id', '=', $site_id ] ], 'sku_id');
$goods_model = new Goods();
$res = $goods_model->getGoodsSpecFormat($sku_ids, $goods_spec_format);
return $res;
}
/**
* 判断sku是否参与预售
* @param $sku_id
* @return array
*/
public function isJoinPresaleBySkuId($sku_id)
{
$condition = [
[ 'ppg.sku_id', '=', $sku_id ],
[ 'pp.status', '=', 1 ],
[ 'pp.end_time', '>=', time() ]
];
$alias = 'ppg';
$join = [
[ 'promotion_presale pp', 'pp.presale_id = ppg.presale_id', 'inner' ]
];
$info = model('promotion_presale_goods')->getInfo($condition, 'ppg.presale_id,ppg.sku_id', $alias, $join);
if (empty($info)) {
return $this->error();
} else {
return $this->success([ 'promotion_type' => 'presale', 'presale_id' => $info[ 'presale_id' ], 'sku_id' => $sku_id ]);
}
}
/**
* 判断sku是否参与预售
* @param $sku_ids
* @return array
*/
public function isJoinPresaleBySkuIds($sku_ids)
{
$condition = [
[ 'ppg.sku_id', 'in', $sku_ids ],
[ 'pp.status', '=', 1 ],
[ 'pp.end_time', '>=', time() ]
];
$alias = 'ppg';
$join = [
[ 'promotion_presale pp', 'pp.presale_id = ppg.presale_id', 'inner' ]
];
$list = model('promotion_presale_goods')->getList($condition, 'ppg.presale_id,ppg.sku_id', '', $alias, $join);
return $this->success($list);
}
/**
* 判断sku是否参与预售
* @param $sku_id
* @return array
*/
public function isJoinPresaleByGoodsId($goods_id)
{
$condition = [
[ 'ppg.goods_id', '=', $goods_id ],
[ 'pp.status', '=', 1 ],
[ 'pp.end_time', '>=', time() ]
];
$alias = 'ppg';
$join = [
[ 'promotion_presale pp', 'pp.presale_id = ppg.presale_id', 'inner' ]
];
$list = model('promotion_presale_goods')->getList($condition, 'ppg.presale_id,ppg.sku_id','', $alias, $join);
return $this->success($list);
}
/**
* 获取会员已购该商品数
* @param $goods_id
* @param $member_id
* @return float
*/
public function getGoodsPurchasedNum($presale_id, $member_id)
{
$num = model("promotion_presale_order")->getSum([
[ 'member_id', '=', $member_id ],
[ 'presale_id', '=', $presale_id ],
[ 'order_status', '<>', PresaleOrderCommon::ORDER_CLOSE ],
[ 'refund_status', '<>', PresaleOrderRefund::REFUND_COMPLETE ]
], 'num');
return $num;
}
/**
* 获取预售订单数量
* @param $condition
* @param string $field
* @return array
*/
public function getPresaleOrderCount($condition, $field = '*')
{
$count = model("promotion_presale_order")->getCount($condition, $field);
return $this->success($count);
}
/**
* 查询预售商品数量
* @param array $where
* @param string $field
* @param string $alias
* @param null $join
* @param null $group
* @return array
*/
public function getPresaleGoodsCount($where = [], $field = '*', $alias = 'a', $join = null, $group = null)
{
$count = model("promotion_presale_order")->getCount($where, $field, $alias, $join, $group);
return $this->success($count);
}
/**
* 生成预售二维码
* @param $presale_id
* @param string $name
* @param string $type 类型 create创建 get获取
* @return mixed|array
*/
public function qrcode($presale_id, $name, $site_id, $type = 'create')
{
$data = [
'site_id' => $site_id,
'app_type' => "all", // all为全部
'type' => $type, // 类型 create创建 get获取
'data' => [
'id' => $presale_id
],
'page' => '/pages_promotion/presale/detail',
'qrcode_path' => 'upload/qrcode/presale',
'qrcode_name' => "presale_qrcode_" . $presale_id
];
event('Qrcode', $data, true);
$app_type_list = config('app_type');
$path = [];
foreach ($app_type_list as $k => $v) {
switch ( $k ) {
case 'h5':
$wap_domain = getH5Domain();
$path[ $k ][ 'status' ] = 1;
$path[ $k ][ 'url' ] = $wap_domain . $data[ 'page' ] . '?id=' . $presale_id;
$path[ $k ][ 'img' ] = "upload/qrcode/presale/presale_qrcode_" . $presale_id . "_" . $k . ".png";
break;
case 'weapp' :
$config = new ConfigModel();
$res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'WEAPP_CONFIG' ] ]);
if (!empty($res[ 'data' ])) {
if (empty($res[ 'data' ][ 'value' ][ 'qrcode' ])) {
$path[ $k ][ 'status' ] = 2;
$path[ $k ][ 'message' ] = '未配置微信小程序';
} else {
$path[ $k ][ 'status' ] = 1;
$path[ $k ][ 'img' ] = $res[ 'data' ][ 'value' ][ 'qrcode' ];
}
} else {
$path[ $k ][ 'status' ] = 2;
$path[ $k ][ 'message' ] = '未配置微信小程序';
}
break;
case 'wechat' :
$config = new ConfigModel();
$res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'WECHAT_CONFIG' ] ]);
if (!empty($res[ 'data' ])) {
if (empty($res[ 'data' ][ 'value' ][ 'qrcode' ])) {
$path[ $k ][ 'status' ] = 2;
$path[ $k ][ 'message' ] = '未配置微信公众号';
} else {
$path[ $k ][ 'status' ] = 1;
$path[ $k ][ 'img' ] = $res[ 'data' ][ 'value' ][ 'qrcode' ];
}
} else {
$path[ $k ][ 'status' ] = 2;
$path[ $k ][ 'message' ] = '未配置微信公众号';
}
break;
}
}
$return = [
'path' => $path,
'name' => $name,
];
return $this->success($return);
}
/**
* 商品用到的分类
* @param $condition
* @return array
*/
public function getGoodsCategoryIds($condition)
{
$cache_name = "shop_presale_goods_category_" . md5(json_encode($condition));
$cache_time = 60;
$cache_res = Cache::get($cache_name);
if (empty($cache_res) || time() - $cache_res[ 'time' ] > $cache_time) {
$list = Db::name('promotion_presale')
->alias('pp')
->join('goods g', 'pp.goods_id = g.goods_id', 'inner')
->where($condition)
->group('g.category_id')
->column('g.category_id');
$category_ids = trim(join('0', $list), ',');
$category_id_arr = array_unique(explode(',', $category_ids));
Cache::set($cache_name, [ 'time' => time(), 'data' => $category_id_arr ]);
} else {
$category_id_arr = $cache_res[ 'data' ];
}
return $this->success($category_id_arr);
}
public function urlQrcode($page, $qrcode_param, $promotion_type, $app_type, $site_id)
{
$params = [
'site_id' => $site_id,
'data' => $qrcode_param,
'page' => $page,
'promotion_type' => $promotion_type,
'app_type' => $app_type,
'h5_path' => $page . '?id=' . $qrcode_param[ 'id' ],
'qrcode_path' => 'upload/qrcode/presale',
'qrcode_name' => 'presale_qrcode_' . $promotion_type . '_' . $qrcode_param[ 'id' ] . '_' . $site_id
];
$solitaire = event('PromotionQrcode', $params, true);
return $this->success($solitaire);
}
}

View File

@@ -0,0 +1,52 @@
<?php
/**
* Niushop商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.niushop.com
* =========================================================
*/
namespace addon\presale\model;
use app\model\BaseModel;
/**
* 商品预售
*/
class PresaleOrder extends BaseModel
{
/**
* 获取预售订单信息
* @param array $condition
* @param string $field
* @return array
*/
public function getPresaleOrderInfo($condition = [], $field = '*')
{
$info = model('promotion_presale_order')->getInfo($condition, $field);
return $this->success($info);
}
/**
* 获取预售订单分页列表
* @param array $condition
* @param int $page
* @param int $page_size
* @param string $order
* @param string $field
* @return array
*/
public function getPresaleOrderPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'id desc', $field = '*')
{
$join = [
[ 'member m', 'ppo.member_id = m.member_id', 'left' ]
];
$field = 'ppo.*,m.nickname';
$list = model('promotion_presale_order')->pageList($condition, $field, $order, $page, $page_size, 'ppo', $join);
return $this->success($list);
}
}

View File

@@ -0,0 +1,780 @@
<?php
/**
* Niushop商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.niushop.com
* =========================================================
*/
namespace addon\presale\model;
use addon\coupon\model\Coupon;
use addon\store\model\StoreGoodsSku;
use app\dict\member_account\AccountDict;
use app\model\BaseModel;
use app\model\member\MemberAccount;
use app\model\order\Config;
use app\model\order\Order;
use app\model\order\OrderCommon;
use app\model\order\OrderLog;
use app\model\system\Cron;
use app\model\system\Pay;
/**
* 商品预售
*/
class PresaleOrderCommon extends BaseModel
{
// 订单待付款
const ORDER_CREATE = 0;
//等待付尾款
const WAIT_FINAL_PAY = 1;
//订单完成(尾款已支付)
const ORDER_PAY = 2;
// 订单已关闭
const ORDER_CLOSE = -1;
/**
* 基础订单状态(不同类型的订单可以不使用这些状态,但是不能冲突)
* @var unknown
*/
public $order_status = [
self::ORDER_CREATE => [
'status' => self::ORDER_CREATE,
'name' => '待付款',
'is_allow_refund' => 0,
'icon' => 'public/uniapp/order/order-icon-send.png',
'action' => [
[
'action' => 'orderClose',
'title' => '关闭订单',
'color' => ''
],
[
'action' => 'offlinePayDeposit',
'title' => '线下支付定金',
'color' => ''
],
],
'member_action' => [
[
'action' => 'orderClose',
'title' => '关闭订单',
'color' => ''
],
[
'action' => 'orderPayDeposit',
'title' => '支付定金',
'color' => ''
],
],
'color' => ''
],
self::WAIT_FINAL_PAY => [
'status' => self::WAIT_FINAL_PAY,
'name' => '待付尾款',
'is_allow_refund' => 0,
'icon' => 'public/uniapp/order/order-icon-send.png',
'action' => [
[
'action' => 'offlinePayFinal',
'title' => '线下支付尾款',
'color' => ''
],
],
'member_action' => [
[
'action' => 'refundDeposit',
'title' => '退定金',
'color' => ''
],
[
'action' => 'orderPayFinal',
'title' => '支付尾款',
'color' => ''
],
],
'color' => ''
],
self::ORDER_PAY => [
'status' => self::ORDER_PAY,
'name' => '已完成',
'is_allow_refund' => 0,
'icon' => 'public/uniapp/order/order-icon-send.png',
'action' => [],
'member_action' => [],
'color' => ''
],
self::ORDER_CLOSE => [
'status' => self::ORDER_CLOSE,
'name' => '已关闭',
'is_allow_refund' => 0,
'icon' => 'public/uniapp/order/order-icon-close.png',
'action' => [
[
'action' => 'deleteOrder',
'title' => '删除订单',
'color' => ''
],
],
'member_action' => [
[
'action' => 'deleteOrder',
'title' => '删除订单',
'color' => ''
],
],
'color' => ''
],
];
/**
* 订单状态
* @return array
*/
public function getOrderStatus()
{
return $this->success($this->order_status);
}
/*********************************************** 订单关闭 start *****************************************************************/
/**
* 订单关闭
* @param array $condition
*/
public function depositOrderClose($condition = [])
{
$info = model('promotion_presale_order')->getInfo($condition, 'id,order_status');
if (empty($info)) {
return $this->error('', '订单不存在');
}
if ($info[ 'order_status' ] == self::ORDER_CLOSE) {
return $this->error('', '该订单已关闭');
}
if ($info[ 'order_status' ] != self::ORDER_CREATE) {
return $this->error('', '该订单已支付或已完成');
}
$res = $this->cronDepositOrderClose($info[ 'id' ]);
return $res;
}
/**
* 添加定金订单自动关闭
*/
public function addDepositOrderCronClose($order_id, $site_id)
{
//计算订单自动关闭时间
$config_model = new Config();
$order_config_result = $config_model->getOrderEventTimeConfig($site_id);
$order_config = $order_config_result[ "data" ];
$now_time = time();
if (!empty($order_config)) {
$execute_time = $now_time + $order_config[ "value" ][ "auto_close" ] * 60; //自动关闭时间
} else {
$execute_time = $now_time + 3600; //尚未配置 默认一天
}
$cron_model = new Cron();
$cron_model->addCron(1, 0, "预售订单自动关闭", "CronDepositOrderClose", $execute_time, $order_id);
}
/**
* 添加尾款支付到期自动退定金操作
*/
public function addRefundOrderCronClose($order_id, $site_id, $time)
{
//计算订单自动关闭时间
// $config_model = new Config();
// $order_config_result = $config_model->getOrderEventTimeConfig($site_id);
// $order_config = $order_config_result["data"];
// $now_time = time();
// if (!empty($order_config)) {
// $execute_time = $now_time + $order_config["value"]["auto_close"] * 60; //自动关闭时间
// } else {
// $execute_time = $now_time + 3600; //尚未配置 默认一天
// }
$cron_model = new Cron();
$cron_model->addCron(1, 0, "预售订单尾款支付到期退定金", "CronRefundOrderDepositClose", $time, $order_id);
}
/**
* 自动关闭定金订单
* @param $order_id
* @return array
*/
public function cronDepositOrderClose($order_id)
{
$order_info = model('promotion_presale_order')->getInfo([ [ 'id', '=', $order_id ] ]);
if (empty($order_info)) {
return $this->error('', '订单不存在');
}
//订单已关闭
if ($order_info[ 'order_status' ] == self::ORDER_CLOSE) {
return $this->success();
}
if ($order_info[ 'order_status' ] != self::ORDER_CREATE) {
return $this->success();
}
model('promotion_presale_order')->startTrans();
try {
//修改订单状态
$data = [
'order_status' => self::ORDER_CLOSE,
'order_status_name' => $this->order_status[ self::ORDER_CLOSE ][ "name" ],
'order_status_action' => json_encode($this->order_status[ self::ORDER_CLOSE ], JSON_UNESCAPED_UNICODE),
'close_time' => time(),
];
model('promotion_presale_order')->update($data, [ [ 'id', '=', $order_id ] ]);
//增加库存
// model('promotion_presale')->setInc([['presale_id','=',$order_info['presale_id']]],'presale_stock',$order_info['num']);
//返还sku库存 2021.06.10
model('promotion_presale_goods')->setInc([ [ 'presale_id', '=', $order_info[ 'presale_id' ] ], [ 'sku_id', '=', $order_info[ 'sku_id' ] ] ], 'presale_stock', $order_info[ 'num' ]);
//增加门店库存
if ($order_info[ 'delivery_store_id' ] > 0) {
$store_data = [
'delivery_store_id' => $order_info[ 'delivery_store_id' ],
'num' => $order_info[ 'num' ],
'sku_id' => $order_info[ 'sku_id' ]
];
$store_result = $this->incStoreGoodsStock($store_data);
if ($store_result[ 'code' ] < 0) {
return $store_result;
}
}
//返还店铺优惠券
$coupon_id = $order_info[ "coupon_id" ];
if ($coupon_id > 0) {
$coupon_model = new Coupon();
$coupon_model->refundCoupon($coupon_id, $order_info[ "member_id" ]);
}
//平台优惠券
//平台余额 退还余额
if ($order_info[ "balance_deposit_money" ] > 0) {
$member_account_model = new MemberAccount();
$member_account_model->addMemberAccount($order_info[ 'site_id' ], $order_info[ "member_id" ], AccountDict::balance, $order_info[ "balance_deposit_money" ], "presale_deposit_refund", "余额返还", "订单关闭返还");
}
model('promotion_presale_order')->commit();
return $this->success();
} catch (\Exception $e) {
model('promotion_presale_order')->rollback();
return $this->error('', $e->getMessage());
}
}
/**
* 订单尾款结束自动退款
* @param $order_id
* @return array
*/
public function cronRefundOrderDepositClose($order_id)
{
$order_info = model('promotion_presale_order')->getInfo([ [ 'id', '=', $order_id ] ]);
if (empty($order_info)) {
return $this->error('', '订单不存在');
}
//订单已关闭
if ($order_info[ 'order_status' ] == self::ORDER_CLOSE) {
return $this->success();
}
if ($order_info[ 'order_status' ] != self::WAIT_FINAL_PAY) {
return $this->success();
}
model('promotion_presale_order')->startTrans();
try {
//修改订单状态
$data = [
'order_status' => self::ORDER_CLOSE,
'order_status_name' => $this->order_status[ self::ORDER_CLOSE ][ "name" ],
'order_status_action' => json_encode($this->order_status[ self::ORDER_CLOSE ], JSON_UNESCAPED_UNICODE),
'close_time' => time(),
];
model('promotion_presale_order')->update($data, [ [ 'id', '=', $order_id ] ]);
//增加库存
// model('promotion_presale')->setInc([['presale_id','=',$order_info['presale_id']]],'presale_stock',$order_info['num']);
//返还sku库存 2021.06.10
model('promotion_presale_goods')->setInc([ [ 'presale_id', '=', $order_info[ 'presale_id' ] ], [ 'sku_id', '=', $order_info[ 'sku_id' ] ] ], 'presale_stock', $order_info[ 'num' ]);
//增加门店库存
if ($order_info[ 'delivery_store_id' ] > 0) {
$store_data = [
'delivery_store_id' => $order_info[ 'delivery_store_id' ],
'num' => $order_info[ 'num' ],
'sku_id' => $order_info[ 'sku_id' ]
];
$store_result = $this->incStoreGoodsStock($store_data);
if ($store_result[ 'code' ] < 0) {
return $store_result;
}
}
//返还店铺优惠券
$coupon_id = $order_info[ "coupon_id" ];
if ($coupon_id > 0) {
$coupon_model = new Coupon();
$coupon_model->refundCoupon($coupon_id, $order_info[ "member_id" ]);
}
//平台优惠券
//平台余额 退还余额
if ($order_info[ "balance_deposit_money" ] > 0) {
$member_account_model = new MemberAccount();
$member_account_model->addMemberAccount($order_info[ 'site_id' ], $order_info[ "member_id" ], AccountDict::balance, $order_info[ "balance_deposit_money" ], "presale_deposit_refund", "余额返还", "订单关闭返还");
}
model('promotion_presale_order')->commit();
return $this->success();
} catch (\Exception $e) {
model('promotion_presale_order')->rollback();
return $this->error('', $e->getMessage());
}
}
/**
* 订单关闭增加门店商品库存
* @param $data
* @return array
*/
public function incStoreGoodsStock($data)
{
$store_goods_sku_model = new StoreGoodsSku();
$stock_result = $store_goods_sku_model->incStock([ "store_id" => $data[ "delivery_store_id" ], "sku_id" => $data[ "sku_id" ], "stock" => $data[ "num" ] ]);
if ($stock_result[ "code" ] < 0) {
return $stock_result;
}
}
/*********************************************** 订单关闭 end ******************************************************/
/********************************************** 订单线下支付 start *************************************************/
/**
* 线下支付定金
* @param $order_id
* @param $site_id
* @return array
*/
public function offlinePayDeposit($order_id, $site_id)
{
$order_info = model("promotion_presale_order")->getInfo(
[ [ 'id', '=', $order_id ], [ 'site_id', '=', $site_id ] ],
'deposit_out_trade_no,order_status'
);
if (empty($order_info)) {
return $this->error('', '订单不存在');
}
if ($order_info[ 'order_status' ] != self::ORDER_CREATE) {
return $this->error('', '订单已经支付或已经关闭');
}
$pay_model = new Pay();
$result = $pay_model->onlinePay($order_info[ 'deposit_out_trade_no' ], "offlinepay", '', '');
if ($result[ "code" ] < 0) {
return $result;
}
}
/**
* 线下支付尾款
* @param $order_id
* @param $site_id
* @return array
*/
public function offlinePayFinal($order_id, $site_id)
{
$order_info = model("promotion_presale_order")->getInfo(
[ [ 'id', '=', $order_id ], [ 'site_id', '=', $site_id ] ],
'final_out_trade_no,order_status,pay_start_time,pay_end_time'
);
if (empty($order_info)) {
return $this->error('', '订单不存在');
}
if ($order_info[ 'order_status' ] != self::WAIT_FINAL_PAY) {
return $this->error('', '订单已经支付或已经关闭');
}
// if ($order_info['pay_start_time'] > time() || $order_info['pay_end_time'] < time()){
// return $this->error('','未在尾款支付时间');
// }
if (!( $order_info[ 'pay_start_time' ] < time() && time() < $order_info[ 'pay_end_time' ] )) {
return $this->error('', '未在尾款支付时间');
}
if (empty($order_info[ 'final_out_trade_no' ])) {
$pay = new Pay();
$out_trade_no = $pay->createOutTradeNo();
model('promotion_presale_order')->startTrans();
try {
model('promotion_presale_order')->update(
[ 'final_out_trade_no' => $out_trade_no ],
[ [ 'id', '=', $order_id ], [ 'site_id', '=', $site_id ] ]
);
//将预售订单信息添加到普通订单中
$res = $this->finalOrderOnlinePay([ 'out_trade_no' => $out_trade_no, 'pay_type' => 'offlinepay' ]);
if ($res[ 'code' ] < 0) {
model('promotion_presale_order')->rollback();
return $res;
}
model('promotion_presale_order')->commit();
return $this->success();
} catch (\Exception $e) {
model('promotion_presale_order')->rollback();
return $this->error('', $e->getMessage());
}
} else {
$pay_model = new Pay();
$result = $pay_model->onlinePay($order_info[ 'final_out_trade_no' ], "offlinepay", '', '');
if ($result[ "code" ] < 0) {
return $result;
}
}
}
/********************************************** 订单线下支付 end ***************************************************/
/*********************************************** 订单异步回调 start ************************************************/
/**
* 定金订单线上支付回调
* @param $data
* @return array
*/
public function depositOrderOnlinePay($data)
{
$out_trade_no = $data[ "out_trade_no" ];
$order_info = model("promotion_presale_order")->getInfo([ [ 'deposit_out_trade_no', '=', $out_trade_no ] ]);
if ($order_info[ 'order_status' ] != self::ORDER_CREATE) {
return $this->success();
}
$order_model = new Order();
$pay_type_list = $order_model->getPayType();
model('promotion_presale_order')->startTrans();
try {
//获取预售活动信息
$presale_info = model('promotion_presale')->getInfo([ [ 'presale_id', '=', $order_info[ 'presale_id' ] ] ], 'deliver_type,deliver_time');
if ($presale_info[ 'deliver_type' ] == 0) {
$predict_delivery_time = $presale_info[ 'deliver_time' ];
} else {
$predict_delivery_time = strtotime('+' . $presale_info[ 'deliver_time' ] . 'days');
}
//修改订单状态
$order_data = [
'order_status' => self::WAIT_FINAL_PAY,
'order_status_name' => $this->order_status[ self::WAIT_FINAL_PAY ][ "name" ],
'order_status_action' => json_encode($this->order_status[ self::WAIT_FINAL_PAY ], JSON_UNESCAPED_UNICODE),
'pay_deposit_time' => time(),
'deposit_pay_type' => $data[ 'pay_type' ],
'deposit_pay_type_name' => $pay_type_list[ $data[ 'pay_type' ] ],
'predict_delivery_time' => $predict_delivery_time
];
model("promotion_presale_order")->update($order_data, [ [ 'id', '=', $order_info[ 'id' ] ] ]);
if ($order_info[ 'final_money' ] == 0) {
// $this->finalOrderOnlinePay(['out_trade_no' => $order_info['final_out_trade_no'], 'id' => $order_info['id'], 'pay_type' => $data['pay_type'] ]);
}
//增加销量
model('promotion_presale')->setInc([ [ 'presale_id', '=', $order_info[ 'presale_id' ] ] ], 'sale_num', $order_info[ 'num' ]);
model('promotion_presale_order')->commit();
return $this->success();
} catch (\Exception $e) {
model('promotion_presale_order')->rollback();
return $this->error('', $e->getMessage());
}
}
/**
* 尾款订单线上支付回调
* @param $data
* @return array
*/
public function finalOrderOnlinePay($data)
{
if ($data[ "out_trade_no" ] == '' && isset($data[ 'id' ])) {
$condition = [ [ 'id', '=', $data[ 'id' ] ] ];
} else {
$condition = [ [ 'final_out_trade_no', '=', $data[ "out_trade_no" ] ] ];
}
$order_info = model("promotion_presale_order")->getInfo($condition);
if ($order_info[ 'order_status' ] != self::WAIT_FINAL_PAY) {
return $this->success();
}
$order_model = new Order();
$pay_type_list = $order_model->getPayType();
model('promotion_presale_order')->startTrans();
try {
//修改预售订单状态
$order_data = [
'order_status' => self::ORDER_PAY,
'order_status_name' => $this->order_status[ self::ORDER_PAY ][ "name" ],
'order_status_action' => json_encode($this->order_status[ self::ORDER_PAY ], JSON_UNESCAPED_UNICODE),
'pay_final_time' => time(),
'final_pay_type' => $data[ 'pay_type' ],
'final_pay_type_name' => $pay_type_list[ $data[ 'pay_type' ] ]
];
model("promotion_presale_order")->update($order_data, [ [ 'id', '=', $order_info[ 'id' ] ] ]);
//将预售订单信息添加到普通订单中
$res = $this->presaleOrderToOrder($order_info);
if ($res[ 'code' ] < 0) {
model('promotion_presale_order')->rollback();
return $res;
}
model('promotion_presale_order')->commit();
return $this->success();
} catch (\Exception $e) {
model('promotion_presale_order')->rollback();
return $this->error('', $e->getMessage());
}
}
/**
* 将预售订单信息添加到普通订单中
* @param $presale_order
* @return array
*/
public function presaleOrderToOrder($presale_order)
{
model('order')->startTrans();
model("order_log")->startTrans();
try {
//添加订单记录
$order_data = [
'order_no' => $presale_order[ 'order_no' ],
'site_id' => $presale_order[ 'site_id' ],
'site_name' => $presale_order[ 'site_name' ],
'order_name' => $presale_order[ 'sku_name' ],
'order_from' => $presale_order[ 'order_from' ],
'order_from_name' => $presale_order[ 'order_from_name' ],
'order_type' => $presale_order[ 'order_type' ],
'order_type_name' => $presale_order[ 'order_type_name' ],
'out_trade_no' => $presale_order[ 'deposit_out_trade_no' ],
'out_trade_no_2' => $presale_order[ 'final_out_trade_no' ],
'pay_type' => $presale_order[ 'deposit_pay_type' ],
'pay_type_name' => $presale_order[ 'deposit_pay_type_name' ],
'delivery_type' => $presale_order[ 'delivery_type' ],
'delivery_type_name' => $presale_order[ 'delivery_type_name' ],
'member_id' => $presale_order[ 'member_id' ],
'name' => $presale_order[ 'name' ],
'mobile' => $presale_order[ 'mobile' ],
'telephone' => $presale_order[ 'telephone' ],
'province_id' => $presale_order[ 'province_id' ],
'city_id' => $presale_order[ 'city_id' ],
'district_id' => $presale_order[ 'district_id' ],
'community_id' => $presale_order[ 'community_id' ],
'address' => $presale_order[ 'address' ],
'full_address' => $presale_order[ 'full_address' ],
'longitude' => $presale_order[ 'longitude' ],
'latitude' => $presale_order[ 'latitude' ],
'buyer_ip' => $presale_order[ 'buyer_ip' ],
'buyer_ask_delivery_time' => $presale_order[ 'buyer_ask_delivery_time' ],
'delivery_start_time' => $presale_order[ 'delivery_start_time' ],
'delivery_end_time' => $presale_order[ 'delivery_end_time' ],
'buyer_message' => $presale_order[ 'buyer_message' ],
'goods_money' => $presale_order[ 'goods_money' ],
'delivery_money' => $presale_order[ 'delivery_money' ],
'promotion_money' => $presale_order[ 'promotion_money' ],
'coupon_id' => $presale_order[ 'coupon_id' ],
'coupon_money' => $presale_order[ 'coupon_money' ],
'invoice_money' => $presale_order[ 'invoice_money' ],
'order_money' => $presale_order[ 'order_money' ],
'balance_money' => $presale_order[ 'balance_deposit_money' ] + $presale_order[ 'balance_final_money' ],
'pay_money' => $presale_order[ 'pay_deposit_money' ] + $presale_order[ 'pay_final_money' ],//现金支付金额
'create_time' => $presale_order[ 'create_time' ],
'pay_time' => time(),
'goods_num' => $presale_order[ 'num' ],
'delivery_store_id' => $presale_order[ 'delivery_store_id' ],
'delivery_store_name' => $presale_order[ 'delivery_store_name' ],
'delivery_store_info' => $presale_order[ 'delivery_store_info' ],
'promotion_type' => 'presale',
'promotion_type_name' => '商品预售',
'promotion_status_name' => '已完成',
'is_invoice' => $presale_order[ 'is_invoice' ],
'invoice_type' => $presale_order[ 'invoice_type' ],
'invoice_title' => $presale_order[ 'invoice_title' ],
'taxpayer_number' => $presale_order[ 'taxpayer_number' ],
'invoice_rate' => $presale_order[ 'invoice_rate' ],
'invoice_content' => $presale_order[ 'invoice_content' ],
'invoice_delivery_money' => $presale_order[ 'invoice_delivery_money' ],
'invoice_full_address' => $presale_order[ 'invoice_full_address' ],
'is_tax_invoice' => $presale_order[ 'is_tax_invoice' ],
'invoice_email' => $presale_order[ 'invoice_email' ],
'invoice_title_type' => $presale_order[ 'invoice_title_type' ],
'is_fenxiao' => $presale_order[ 'is_fenxiao' ],
'predict_delivery_time' => $presale_order[ 'predict_delivery_time' ],//预计发货时间
'store_id' => $presale_order[ 'delivery_store_id' ]
];
$order_id = model('order')->add($order_data);
$sku_info = model('goods_sku')->getInfo([
[ 'site_id', '=', $presale_order[ 'site_id' ] ],
[ 'sku_id', '=', $presale_order[ 'sku_id' ] ]
], 'supplier_id');
//添加订单商品项
$order_goods_data = [
'order_id' => $order_id,
'order_no' => $presale_order[ 'order_no' ],
'site_id' => $presale_order[ 'site_id' ],
'member_id' => $presale_order[ 'member_id' ],
'goods_id' => $presale_order[ 'goods_id' ],
'sku_id' => $presale_order[ 'sku_id' ],
'sku_name' => $presale_order[ 'sku_name' ],
'sku_image' => $presale_order[ 'sku_image' ],
'sku_no' => $presale_order[ 'sku_no' ],
'is_virtual' => $presale_order[ 'is_virtual' ],
'goods_class' => $presale_order[ 'goods_class' ],
'goods_class_name' => $presale_order[ 'goods_class_name' ],
'price' => $presale_order[ 'price' ],
'cost_price' => $presale_order[ 'cost_price' ],
'num' => $presale_order[ 'num' ],
'goods_money' => $presale_order[ 'goods_money' ],
'cost_money' => $presale_order[ 'cost_price' ] * $presale_order[ 'num' ],
'real_goods_money' => $presale_order[ 'goods_money' ] - ( $presale_order[ 'presale_money' ] - $presale_order[ 'presale_deposit_money' ] ),
'promotion_money' => $presale_order[ 'promotion_money' ],
'coupon_money' => $presale_order[ 'coupon_money' ],
'goods_name' => $presale_order[ 'goods_name' ],
'sku_spec_format' => $presale_order[ 'sku_spec_format' ],
'is_fenxiao' => $presale_order[ 'is_fenxiao' ],
'delivery_status_name' => '未发货',
'delivery_status' => 0,
'create_time' => time(),
'store_id' => $presale_order[ 'delivery_store_id' ],
'supplier_id' => $sku_info[ 'supplier_id' ] ?? 0
];
model('order_goods')->add($order_goods_data);
model('promotion_presale_order')->update([ 'relate_order_id' => $order_id ], [ [ 'id', '=', $presale_order[ 'id' ] ] ]);
//调用线上支付
$order_model = new OrderCommon();
//记录订单日志 start
//获取用户信息
$member_info = model('member')->getInfo([ 'member_id' => $presale_order[ 'member_id' ] ], 'nickname');
$buyer_name = empty($member_info[ 'nickname' ]) ? '' : '【' . $member_info[ 'nickname' ] . '】';
$log_data = [
'order_id' => $order_id,
'action' => '买家' . $buyer_name . '下单了',
'uid' => $presale_order[ 'member_id' ],
'nick_name' => $member_info[ 'nickname' ],
'action_way' => 1,
'order_status' => 0,
'order_status_name' => '待支付'
];
OrderLog::addOrderLog($log_data, $order_model);
//记录订单日志 end
$result = $order_model->orderOnlinePay([ 'out_trade_no' => $presale_order[ 'deposit_out_trade_no' ], 'pay_type' => $presale_order[ 'deposit_pay_type' ] ]);
if ($result[ 'code' ] < 0) {
model('order')->rollback();
return $result;
}
model('order')->commit();
model("order_log")->commit();
return $this->success();
} catch (\Exception $e) {
model('order')->rollback();
model("order_log")->rollback();
return $this->error('', $e->getMessage());
}
}
/*********************************************** 订单异步回调 end *****************************************************************/
/**
* 订单删除
* @param $condition
* @return array
*/
public function deleteOrder($condition)
{
$info = model('promotion_presale_order')->getInfo($condition, 'order_status');
if (empty($info)) {
return $this->error('', '订单不存在');
}
if ($info[ 'order_status' ] != self::ORDER_CLOSE) {
return $this->error('', '抱歉,只有已关闭的订单才可以删除');
}
$res = model('promotion_presale_order')->delete($condition);
if ($res) {
return $this->success($res);
} else {
return $this->error();
}
}
/**
* 获取定金/尾款支付流水号
* @param $id
* @param $member_id
* @param $site_id
* @return array
*/
public function getPresaleOrderOutTradeNo($id, $member_id, $site_id)
{
//订单信息
$order_info = model('promotion_presale_order')->getInfo(
[
[ 'id', '=', $id ],
[ 'member_id', '=', $member_id ],
[ 'site_id', '=', $site_id ]
],
'order_status,deposit_out_trade_no,final_out_trade_no'
);
if (empty($order_info)) {
return $this->error('', '订单不存在');
}
//未支付定金
if ($order_info[ 'order_status' ] == self::ORDER_CREATE) {
return $this->success($order_info[ 'deposit_out_trade_no' ]);
}
if ($order_info[ 'order_status' ] == self::WAIT_FINAL_PAY) {
return $this->success($order_info[ 'final_out_trade_no' ]);
}
return $this->error('', '请核实数据后重试');
}
/**
* 订单关闭
* @param array $condition
*/
public function depositPresaleOrderClose($condition = [])
{
$list = model('promotion_presale_order')->getList($condition, 'id,order_status');
if (empty($list)) {
return $this->error('', '订单不存在');
}
foreach ($list as $key => $val) {
if ($val[ 'order_status' ] != self::ORDER_CLOSE && $val[ 'order_status' ] != self::ORDER_CREATE) {
$this->cronDepositOrderClose($val[ 'id' ]);
}
}
}
}

View File

@@ -0,0 +1,634 @@
<?php
/**
* Niushop商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.niushop.com
* 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
* 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
* =========================================================
*/
namespace addon\presale\model;
use addon\coupon\model\Coupon;
use addon\store\model\StoreGoodsSku;
use addon\store\model\StoreMember as StoreMemberModel;
use app\model\BaseModel;
use app\model\express\Config as ExpressConfig;
use app\model\express\Express;
use app\model\express\Local;
use app\model\order\Config;
use app\model\order\OrderCreate;
use app\model\order\OrderCreateTool;
use app\model\store\Store;
use app\model\system\Pay;
use extend\exception\OrderException;
use think\facade\Cache;
/**
* 订单创建(商品预售)
*
* @author Administrator
*
*/
class PresaleOrderCreate extends BaseModel
{
use OrderCreateTool;
// private $goods_money = 0;//商品金额
// private $balance_money = 0;//余额
// private $delivery_money = 0;//配送费用
// private $coupon_money = 0;//优惠券金额
// private $adjust_money = 0;//调整金额
// private $invoice_money = 0;//发票费用
// private $promotion_money = 0;//优惠金额
// private $order_money = 0;//订金金额
// private $pay_money = 0;//支付总价
//
// private $is_virtual = 0; //是否是虚拟类订单
// private $order_name = ''; //订单详情
// private $goods_num = 0; //商品种数
// private $member_balance_money = 0;//会员账户余额(计算过程中会逐次减少)
// private $pay_type = 'ONLINE_PAY';//支付方式
// private $invoice_delivery_money = 0;
// private $error = 0; //是否有错误
// private $error_msg = ''; //错误描述
// private $recommend_member_card; // 推荐会员卡
public $final_money = 0;//尾款金额
public $presale_id = 0;
public $deduction_money = 0;//抵扣金额
public $promotion_presale_info = [];
public $presale_info = [];
public $pay_end_time = 0;
public $is_deposit_back = 0;
/************************************************** 定金支付 start *********************************************************************/
/**
* 订单创建
* @param unknown $data
*/
public function create()
{
//计算
$this->confirm();
if ($this->error > 0) {
return $this->error(['error_code' => $this->error], $this->error_msg);
}
$pay = new Pay();
$this->out_trade_no = $pay->createOutTradeNo($this->member_id);
$presale_common_order = new PresaleOrderCommon();
$order_status_data = $presale_common_order->getOrderStatus();
$order_status = $order_status_data['data'];
$this->order_no = $this->createOrderNo();
model('promotion_presale_order')->startTrans();
//循环生成多个订单
try {
$this->orderType();
$express_type_list = $this->config('delivery_type');
$delivery_type_name = $express_type_list[$this->delivery['delivery_type']] ?? '';
$sku_info = $this->goods_list[0];
$data_order = [
'site_id' => $this->site_id,
'site_name' => $this->site_name,
'presale_id' => $this->presale_id,
'order_no' => $this->order_no,
'order_from' => $this->order_from,
'order_from_name' => $this->order_from_name,
'order_type' => $this->order_type['order_type_id'],
'order_type_name' => $this->order_type['order_type_name'],
'order_status_name' => $order_status[$presale_common_order::ORDER_CREATE]['name'],
'order_status_action' => json_encode($order_status[$presale_common_order::ORDER_CREATE], JSON_UNESCAPED_UNICODE),
'pay_start_time' => $this->presale_info['pay_start_time'],
'pay_end_time' => $this->presale_info['pay_end_time'],
'goods_id' => $this->presale_info['goods_id'],
'goods_name' => $this->presale_info['goods_name'],
'sku_id' => $sku_info['sku_id'],
'sku_name' => $sku_info['sku_name'],
'sku_image' => $sku_info['sku_image'],
'sku_no' => $sku_info['sku_no'],
'is_virtual' => $this->is_virtual,
'goods_class' => $sku_info['goods_class'],
'goods_class_name' => $sku_info['goods_class_name'],
'cost_price' => $sku_info['cost_price'],
'sku_spec_format' => $sku_info['sku_spec_format'],
'member_id' => $this->member_id,
'name' => $this->delivery['member_address']['name'] ?? '',
'mobile' => $this->delivery['member_address']['mobile'] ?? '',
'telephone' => $this->delivery['member_address']['telephone'] ?? '',
'province_id' => $this->delivery['member_address']['province_id'] ?? '',
'city_id' => $this->delivery['member_address']['city_id'] ?? '',
'district_id' => $this->delivery['member_address']['district_id'] ?? '',
'community_id' => $this->delivery['member_address']['community_id'] ?? '',
'address' => $this->delivery['member_address']['address'] ?? '',
'full_address' => $this->delivery['member_address']['full_address'] ?? '',
'longitude' => $this->delivery['member_address']['longitude'] ?? '',
'latitude' => $this->delivery['member_address']['latitude'] ?? '',
'buyer_ip' => request()->ip(),
'buyer_message' => $this->param['buyer_message'],
'num' => $this->goods_num,
'price' => $sku_info['price'],
'goods_money' => $this->goods_money,
'balance_deposit_money' => $this->balance_money,
'delivery_money' => $this->delivery_money,
'promotion_money' => $this->promotion_money,
'coupon_id' => $this->coupon_id ?? 0,
'coupon_money' => $this->coupon_money,
'invoice_money' => $this->invoice_money,
'invoice_delivery_money' => $this->invoice_delivery_money,
'order_money' => $this->order_money,
'delivery_type' => $this->delivery['delivery_type'],
'delivery_type_name' => $delivery_type_name,
'delivery_store_id' => $this->delivery['store_id'] ?? 0,
'delivery_store_name' => $this->delivery['delivery_store_name'] ?? '',
'delivery_store_info' => $this->delivery['delivery_store_info'] ?? '',
'buyer_ask_delivery_time' => $this->delivery['buyer_ask_delivery_time']['remark'] ?? '',//定时达
'delivery_start_time' => $this->delivery['buyer_ask_delivery_time']['delivery_start_time'] ?? '',//配送开始时间
'delivery_end_time' => $this->delivery['buyer_ask_delivery_time']['delivery_end_time'] ?? '',//配送结束时间
'order_status' => '0',
'create_time' => time(),
//预售相关
'deposit_out_trade_no' => $this->out_trade_no,
'is_deposit_back' => $this->promotion_presale_info['is_deposit_back'],
'deposit_agreement' => $this->promotion_presale_info['deposit_agreement'],
'presale_deposit' => $this->presale_info['presale_deposit'],//定金单价
'presale_deposit_money' => $this->presale_deposit_money,//定金总额
'presale_price' => $this->presale_info['presale_price'],//抵扣单价
'presale_money' => $this->presale_money,//抵扣总额
'pay_deposit_money' => $this->pay_money,
'final_money' => $this->final_money,
'is_fenxiao' => $this->presale_info['is_fenxiao'],
];
if (isset($this->param['is_invoice']) && $this->param['is_invoice'] == 1) {
$data_order = array_merge($data_order,
[
'is_invoice' => $this->param['is_invoice'] ?? 0,
'invoice_type' => $this->invoice['invoice_type'] ?? 0,
'invoice_title' => $this->invoice['invoice_title'] ?? '',
'taxpayer_number' => $this->invoice['taxpayer_number'] ?? '',
'invoice_rate' => $this->invoice['invoice_rate'] ?? 0,
'invoice_content' => $this->invoice['invoice_content'] ?? '',
'invoice_full_address' => $this->invoice['invoice_full_address'] ?? '',
'is_tax_invoice' => $this->invoice['is_tax_invoice'] ?? '',
'invoice_email' => $this->invoice['invoice_email'] ?? '',
'invoice_title_type' => $this->invoice['invoice_title_type'] ?? 0
]
);
}
$this->order_id = model('promotion_presale_order')->add($data_order);
//预售订单创建
event('PresaleOrderCreate', ['id' => $this->order_id]);
//添加门店关注记录和减少门店商品库存 最新代码位置
$result_list = $this->addStoreMemberAndDecStock($data_order);
if ($result_list['code'] < 0) {
model('promotion_presale_order')->rollback();
return $result_list;
}
//减少库存
$stock_result = $this->decStock();
if ($stock_result['code'] != 0) {
model('promotion_presale_order')->rollback();
return $stock_result;
}
//使用余额
$this->useBalance();
//批量库存处理(卡密商品支付后在扣出库存)//todo 可以再商品中设置扣除库存步骤
// $this->batchDecOrderGoodsStock();
//添加门店关注记录和减少门店商品库存 原本代码位置
model('promotion_presale_order')->commit();
//删除订单缓存
$this->deleteOrderCache();
//增加关闭订单自动事件
$presale_order_model = new PresaleOrderCommon();
$presale_order_model->addDepositOrderCronClose($this->order_id, $this->site_id);
//如果退定金 增加尾款支付到期时间退定金操作
if ($this->is_deposit_back == 0) {
$presale_order_model->addRefundOrderCronClose($this->order_id, $this->site_id, $this->pay_end_time);
}
//生成整体支付单据
$pay->addPay($this->site_id, $this->out_trade_no, $this->pay_type, $this->order_name, $this->order_name, $this->pay_money, '', 'DepositOrderPayNotify', '', $this->order_id, $this->member_id);
return $this->success($this->out_trade_no);
} catch ( \Exception $e ) {
model('promotion_presale_order')->rollback();
return $this->error('', $e->getMessage());
}
}
/**
* 订单计算(定金)
* @param unknown $data
*/
public function depositCalculate()
{
$this->initMemberAddress(); //初始化地址
$this->initMemberAccount();//初始化会员账户
$this->presale_id = $this->param['presale_id'];
$sku_id = $this->param['sku_id'];
$num = $this->param['num'];
//查询预售活动信息
$this->promotion_presale_info = model('promotion_presale')->getInfo([['presale_id', '=', $this->presale_id]]);
if (empty($this->promotion_presale_info)) {
$this->error = 1;
$this->error_msg = '预售活动不存在!';
}
//查询预售信息
$join = [
['promotion_presale pp', 'pp.presale_id = ppg.presale_id', 'inner'],
['goods g', 'g.goods_id = ppg.goods_id', 'inner']
];
$condition = [
['ppg.presale_id', '=', $this->presale_id],
['ppg.sku_id', '=', $sku_id],
['g.goods_state', '=', 1],
['g.is_delete', '=', 0]
];
$field = 'pp.*,ppg.sku_id,ppg.presale_stock,ppg.presale_deposit,ppg.presale_price,g.goods_name,g.is_virtual';
$this->presale_info = model('promotion_presale_goods')->getInfo($condition, $field, 'ppg', $join);
$this->is_virtual = $this->presale_info['is_virtual'];
if (empty($this->presale_info)) {
$this->error = 1;
$this->error_msg = '商品不存在!';
}
//判断活动是否过期或开启
if ($this->presale_info['status'] != 1) {
$this->error = 1;
$this->error_msg = '当前商品预售活动未开启或已过期!';
}
//判断购买数是否超过限购
if ($this->presale_info['presale_num'] < $num && $this->presale_info['presale_num'] > 0) {
$this->error = 1;
$this->error_msg = '该商品限制购买不能大于' . $this->presale_info['presale_num'] . '件!';
}
//判断是否已存在订单
$presale_order_count = model('promotion_presale_order')->getCount(
[
['member_id', '=', $this->member_id],
['presale_id', '=', $this->presale_id],
['order_status', '>=', 0],
['refund_status', '=', 0]
]);
if ($presale_order_count > 0) {
$this->error = 1;
$this->error_msg = '预售期间,同一商品只可购买一次!';
}
//商品列表信息
$this->getOrderGoodsCalculate();
//订单计算
$this->shopOrderCalculate();
//定金金额
$this->presale_deposit_money = $this->presale_info['presale_deposit'] * $num;
//余额抵扣(判断是否使用余额)
if ($this->member_balance_money > 0) {
$balance_money = min($this->presale_deposit_money, $this->member_balance_money);
} else {
$balance_money = 0;
}
$this->pay_money = $this->presale_deposit_money - $balance_money;//计算出实际支付金额
$this->member_balance_money -= $balance_money;//预减少账户余额
$this->balance_money += $balance_money;//累计余额
$this->order_money = $this->final_money + $this->presale_deposit_money;
$this->presale_money = $this->presale_info['presale_price'] * $this->goods_num;
$this->is_deposit_back = $this->presale_info['is_deposit_back'];
$this->pay_end_time = $this->presale_info['pay_end_time'];
//获取发票相关
$this->getInovice();
//todo 统一检测库存(创建订单操作时扣除库存同理)
// 商品限购判断
$this->checkLimitPurchase();
$this->order_key = create_no();
$this->setOrderCache(get_object_vars($this), $this->order_key);
return true;
}
/**
* 待付款订单(定金)
* @param unknown $data
*/
public function depositOrderPayment()
{
$this->depositCalculate();
$this->getDeliveryData();
//订单初始项
event('OrderPayment', ['order_object' => $this]);
return get_object_vars($this);
}
/**
* 获取商品的计算信息
* @param unknown $data
*/
public function getOrderGoodsCalculate()
{
$this->getPresaleShopGoodsList();
return true;
}
/**
* 获取立即购买商品信息
* @param $data
* @return array
*/
public function getPresaleShopGoodsList()
{
$sku_id = $this->param['sku_id'];
$num = $this->param['num'];
$join = [
['site ns', 'ngs.site_id = ns.site_id', 'inner']
];
$field = 'sku_id, sku_name, sku_no, price, discount_price,cost_price, stock, volume, weight, sku_image, ngs.site_id, goods_state, is_virtual, is_free_shipping, shipping_template,goods_class, goods_class_name, goods_id, ns.site_name,sku_spec_format,goods_name,max_buy,min_buy,support_trade_type, is_limit, limit_type,form_id';
$sku_info = model('goods_sku')->getInfo([['sku_id', '=', $sku_id], ['ngs.site_id', '=', $this->site_id]], $field, 'ngs', $join);
if (empty($sku_info)) throw new OrderException('不存在的商品!');
$price = $sku_info['price'];
$sku_info['num'] = $num;
$goods_money = $price * $num;
$sku_info['price'] = $price;
$sku_info['goods_money'] = $goods_money;
$sku_info['real_goods_money'] = $goods_money;
$sku_info['coupon_money'] = 0; //优惠券金额
$sku_info['promotion_money'] = 0; //优惠金额
$sku_info['stock'] = numberFormat($sku_info['stock']);
$goods_list[] = $sku_info;
$this->goods_money = $goods_money;
$this->site_name = $sku_info['site_name'];
$this->goods_list_str = $sku_info['sku_id'] . ':' . $sku_info['num'];
$this->goods_list = $goods_list;
$this->order_name = $sku_info['sku_name'];
$this->goods_num = $sku_info['num'];
$this->limit_purchase = [
'goods_' . $sku_info['goods_id'] => [
'goods_id' => $sku_info['goods_id'],
'goods_name' => $sku_info['sku_name'],
'num' => $sku_info['num'],
'max_buy' => $sku_info['max_buy'],
'min_buy' => $sku_info['min_buy'],
'is_limit' => $sku_info['is_limit'],
'limit_type' => $sku_info['limit_type'],
]
];
return true;
}
/**
* 库存变化
* @return array
*/
public function decStock()
{
$condition = array(
['site_id', '=', $this->site_id],
['presale_id', '=', $this->presale_id],
['sku_id', '=', $this->goods_list[0]['sku_id']]
);
$presale_info = model('promotion_presale_goods')->getInfo($condition, 'presale_stock');
if (empty($presale_info))
return $this->error();
if ($presale_info['presale_stock'] <= 0)
return $this->error('', '库存不足!');
//编辑sku库存
$res = model('promotion_presale_goods')->setDec($condition, 'presale_stock', $this->goods_num);
//减少总库存 2021.06.10
if ($res === false)
return $this->error();
return $this->success($res);
}
/**
* 计算后的进一步计算(不存缓存,每次都是重新计算)
* @return array
*/
public function confirm()
{
$order_key = $this->param['order_key'];
$this->getOrderCache($order_key);
//初始化地址
$this->initMemberAddress();
//初始化门店信息
$this->initStore();
//配送计算
$this->is_check_buyer_ask_delivery_time = false;
$this->calculateDelivery();
//批量校验配送方式
$this->batchCheckDeliveryType();
//计算发票相关
$this->calculateInvoice();
//尾款金额
$this->final_money = $this->final_money + $this->invoice_money + $this->invoice_delivery_money;
//定金金额
$this->presale_deposit_money = $this->presale_info['presale_deposit'] * $this->goods_num;
//余额抵扣(判断是否使用余额)
//使用余额
$is_use_balance = $this->param['is_balance'] ?? 0;
if ($is_use_balance > 0) {
//余额付款
$this->member_balance_money = $this->member_account['balance_total'] ?? 0;
if ($this->member_balance_money > 0) {
$balance_money = min($this->presale_deposit_money, $this->member_balance_money);
} else {
$balance_money = 0;
}
$this->balance_money = $balance_money;
}
$this->pay_money = $this->presale_deposit_money - $this->balance_money;//计算出实际支付金额
$this->member_balance_money -= $this->balance_money;//预减少账户余额
$this->order_money = $this->final_money + $this->presale_deposit_money;
return get_object_vars($this);
}
/**
* 获取店铺订单计算
*/
public function shopOrderCalculate()
{
//实际抵扣金额
if ($this->presale_info['presale_price'] == 0) {//全款预售
$this->deduction_money = 0;
} else {
$this->deduction_money = $this->presale_info['presale_price'] * $this->goods_num - $this->presale_info['presale_deposit'] * $this->goods_num;
}
//尾款金额
$this->final_money = $this->goods_money - $this->presale_info['presale_deposit'] * $this->goods_num - $this->promotion_money - $this->deduction_money + $this->delivery_money;
//理论上是多余的操作
if ($this->final_money < 0) {
$this->final_money = 0;
}
return true;
}
/**
* 添加门店关注记录和减少门店商品库存
* @param $data
* @return array
*/
public function addStoreMemberAndDecStock($data)
{
if (!empty($data['delivery_store_id'])) {
//添加店铺关注记录
$shop_member_model = new StoreMemberModel();
$res = $shop_member_model->addStoreMember($data['delivery_store_id'], $data['member_id']);
if ($res['code'] < 0) {
return $res;
}
$stock_result = $this->skuDecStock($data, $data['delivery_store_id']);
if ($stock_result['code'] < 0) {
return $stock_result;
}
// $store_goods_sku_model = new StoreGoodsSku();
// $stock_result = $store_goods_sku_model->decStock([ 'store_id' => $data[ 'delivery_store_id' ], 'sku_id' => $data[ 'sku_id' ], 'stock' => $data[ 'num' ] ]);
// if ($stock_result[ 'code' ] < 0) {
// return $this->error('', '当前门店库存不足,请选择其他门店');
// }
}
return $this->success();
}
/************************************************** 定金支付 end *********************************************************************/
/************************************************** 尾款支付 start *********************************************************************/
/**
* 订单计算(尾款)
* @param unknown $data
*/
public function finalCalculate($data)
{
$this->initMemberAccount();//初始化会员账户
//余额付款
if ($data['is_balance'] > 0) {
$this->member_balance_money = $this->member_account['balance_total'] ?? 0;
}
//查询预售订单信息
$presale_order_model = new PresaleOrder();
$order_info = $presale_order_model->getPresaleOrderInfo([['id', '=', $data['id']], ['site_id', '=', $this->site_id]])['data'] ?? [];
$data['order_info'] = $order_info;
//判断是否可以支付尾款
if ($order_info['pay_start_time'] > time()) {
$this->error = 1;
$this->error_msg = '尾款支付时间还未开始!';
}
if ($order_info['pay_end_time'] < time()) {
$this->error = 1;
$this->error_msg = '尾款支付时间已过,已停止支付!';
}
//尾款总金额(尾款实际金额 + 发票 + 物流等)
$order_money = $order_info['final_money'];
//余额抵扣(判断是否使用余额)
if ($this->member_balance_money > 0) {
if ($order_money <= $this->member_balance_money) {
$balance_money = $order_money;
} else {
$balance_money = $this->member_balance_money;
}
} else {
$balance_money = 0;
}
$pay_money = $order_money - $balance_money;//计算出实际支付金额
$this->member_balance_money -= $balance_money;//预减少账户余额
$this->balance_money += $balance_money;//累计余额
$is_use = 1;
$this->pay_money += $pay_money;
//总结计算
$data['balance_final_money'] = $this->balance_money;
$data['pay_final_money'] = $this->pay_money;
$data['is_use_balance'] = $is_use;
$data['balance_money'] = $this->balance_money;
return $data;
}
/**
* 尾款支付
* @param $data
* @return array
*/
public function payfinalMoneyPresaleOrder($data)
{
//查询出会员相关信息
$calculate_data = $this->finalCalculate($data);
if (isset($calculate_data['code']) && $calculate_data['code'] < 0)
return $calculate_data;
if ($this->error > 0) {
return $this->error(['error_code' => $this->error], $this->error_msg);
}
$pay = new Pay();
$out_trade_no = $pay->createOutTradeNo();
$order_data = [
'balance_final_money' => $calculate_data['balance_final_money'],
'pay_final_money' => $calculate_data['pay_final_money'],
'final_out_trade_no' => $out_trade_no,
];
model('promotion_presale_order')->startTrans();
try {
model('promotion_presale_order')->update($order_data, [['site_id', '=', $data['site_id']], ['id', '=', $data['id']]]);
$this->order_id = $data['id'];
//扣除余额(统一扣除)
$this->order_from_type = 'presale_order';
$this->useBalance();
$order_name = $calculate_data['order_info']['sku_name'];
//生成整体支付单据
$pay->addPay($data['site_id'], $out_trade_no, $this->pay_type, $order_name, $order_name, $this->pay_money, '', 'FinalOrderPayNotify', '', $this->order_id, $this->member_id);
model('promotion_presale_order')->commit();
return $this->success($out_trade_no);
} catch ( \Exception $e ) {
model()->rollback();
return $this->error('', $e->getMessage());
}
}
/************************************************** 尾款支付 end *********************************************************************/
}

View File

@@ -0,0 +1,402 @@
<?php
/**
* Niushop商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.niushop.com
* 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
* 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
* =========================================================
*/
namespace addon\presale\model;
use app\dict\member_account\AccountDict;
use app\model\member\MemberAccount;
use app\model\BaseModel;
use addon\coupon\model\Coupon;
use app\model\system\Pay;
/**
* 订单退款
*
* @author Administrator
*
*/
class PresaleOrderRefund extends BaseModel
{
//已申请退款
const REFUND_APPLY = 1;
//已完成
const REFUND_COMPLETE = 2;
//已拒绝
const REFUND_REFUSE = -1;
/**
* 订单退款状态
* @var unknown
*/
public $order_refund_status = [
self::REFUND_APPLY => [
'status' => self::REFUND_APPLY,
'name' => '申请退款',
],
self::REFUND_COMPLETE => [
'status' => self::REFUND_COMPLETE,
'name' => '退款成功',
],
self::REFUND_REFUSE => [
'status' => self::REFUND_REFUSE,
'name' => '退款拒绝',
]
];
/*************************************************************************** 用户申请退款操作start**********************************/
/**
* 用户申请退款
* @param $data
* @return array
*/
public function applyRefund($data)
{
$order_info = model('promotion_presale_order')->getInfo(
[
[ 'id', '=', $data[ 'id' ] ],
[ 'site_id', '=', $data[ 'site_id' ] ]
],
'pay_end_time,order_status,refund_status,member_id,balance_final_money,pay_final_money'
);
if (empty($order_info) || $order_info[ 'member_id' ] != $data[ 'member_id' ]) {
return $this->error('', '非法请求!');
}
//不能重复申请退款
if ($order_info[ 'refund_status' ] == self::REFUND_APPLY) {
return $this->error('', '请不要重复申请退款');
}
//支付尾款了不能申请退款
if ($order_info[ 'order_status' ] != PresaleOrderCommon::WAIT_FINAL_PAY) {
return $this->error();
}
$pay_model = new Pay();
$refund_no = $pay_model->createRefundNo();
if ($order_info[ 'refund_status' ] == self::REFUND_REFUSE) {
$order_data = [
'refund_status' => self::REFUND_APPLY,
'refund_status_name' => $this->order_refund_status[ self::REFUND_APPLY ][ 'name' ],
];
} else {
$order_data = [
'deposit_refund_no' => $refund_no,
'refund_status' => self::REFUND_APPLY,
'refund_status_name' => $this->order_refund_status[ self::REFUND_APPLY ][ 'name' ],
'apply_refund_time' => time(),
];
}
$res = model('promotion_presale_order')->update($order_data, [ [ 'id', '=', $data[ 'id' ] ], [ 'site_id', '=', $data[ 'site_id' ] ] ]);
return $this->success($res);
}
/**
* 用户取消申请退款
* @param array $condition
* @return array
*/
public function cancelRefund($condition = [])
{
$order_info = model('promotion_presale_order')->getInfo(
$condition,
'pay_end_time,order_status,refund_status,'
);
if (empty($order_info)) {
return $this->error('', '非法请求!');
}
//不能重复申请退款
if ($order_info[ 'refund_status' ] != self::REFUND_APPLY) {
return $this->error('', '已退款或已取消');
}
$order_data = [
'deposit_refund_no' => '',
'refund_status' => '',
'refund_status_name' => '',
'apply_refund_time' => '',
];
$res = model('promotion_presale_order')->update($order_data, $condition);
return $this->success($res);
}
/**
* 拒绝退款
* @param $data
* @return array
*/
public function refuseRefund($data)
{
$order_info = model('promotion_presale_order')->getInfo(
[
[ 'id', '=', $data[ 'id' ] ], [ 'site_id', '=', $data[ 'site_id' ] ]
]
, 'refund_status'
);
if ($order_info[ 'refund_status' ] == self::REFUND_REFUSE) {
return $this->success();
}
$order_data = [
'refund_status' => self::REFUND_REFUSE,
'refund_status_name' => $this->order_refund_status[ self::REFUND_REFUSE ][ 'name' ],
'refuse_reason' => $data[ 'refuse_reason' ],
'refund_time' => time()
];
$res = model('promotion_presale_order')->update($order_data, [ [ 'id', '=', $data[ 'id' ] ], [ 'site_id', '=', $data[ 'site_id' ] ] ]);
return $this->success($res);
}
/**
* 同意退款
* @param $data
* @return array
*/
public function agreeRefund($data)
{
$order_info = model('promotion_presale_order')->getInfo(
[
[ 'id', '=', $data[ 'id' ] ], [ 'site_id', '=', $data[ 'site_id' ] ]
]
);
if ($order_info[ 'refund_status' ] == self::REFUND_COMPLETE) {
return $this->success();
}
if ($order_info[ 'order_status' ] == 1 && $order_info[ 'pay_end_time' ] < time()) {
$pay_model = new Pay();
$refund_no = $pay_model->createRefundNo();
}
model('promotion_presale_order')->startTrans();
try {
//增加库存
// model('promotion_presale')->setInc([['presale_id', '=', $order_info['presale_id']]], 'presale_stock', $order_info['num']);
//增加门店库存
if ($order_info[ 'delivery_store_id' ] > 0) {
$presale_order_common = new PresaleOrderCommon();
$store_data = [
'delivery_store_id' => $order_info[ 'delivery_store_id' ],
'num' => $order_info[ 'num' ],
'sku_id' => $order_info[ 'sku_id' ]
];
$store_result = $presale_order_common->incStoreGoodsStock($store_data);
if ($store_result[ 'code' ] < 0) {
model('promotion_presale_order')->rollback();
return $store_result;
}
}
//返还店铺优惠券
$coupon_id = $order_info[ "coupon_id" ];
if ($coupon_id > 0) {
$coupon_model = new Coupon();
$coupon_model->refundCoupon($coupon_id, $order_info[ "member_id" ]);
}
//平台优惠券
//平台余额 退还余额(定金余额)
$balance_money = 0;
$deposit_money = 0;//退款定金金额(余额支付+现金支付)
if ($order_info[ "balance_deposit_money" ] > 0) {
$balance_money += $order_info[ "balance_deposit_money" ];
$deposit_money += $order_info[ "balance_deposit_money" ];
}
if ($order_info[ "balance_final_money" ] > 0) {
$balance_money += $order_info[ "balance_final_money" ];
}
if ($balance_money > 0) {
$member_account_model = new MemberAccount();
$member_account_model->addMemberAccount($order_info[ 'site_id' ], $order_info[ "member_id" ], AccountDict::balance, $balance_money, "presale_deposit_refund", "余额返还", "订单关闭返还");
}
//现金原路退还
if ($order_info[ "pay_deposit_money" ] > 0) {
$pay_model = new Pay();
$refund_result = $pay_model->refund($order_info[ "deposit_refund_no" ], $order_info[ 'pay_deposit_money' ], $order_info[ "deposit_out_trade_no" ], '', $order_info[ 'pay_deposit_money' ], $order_info[ "site_id" ], 1);
if ($refund_result[ "code" ] < 0) {
model('promotion_presale_order')->rollback();
return $refund_result;
}
}
$order_common = new PresaleOrderCommon();
//修改订单退款状态
$order_data = [
'order_status' => $order_common::ORDER_CLOSE,
'order_status_name' => $order_common->order_status[ $order_common::ORDER_CLOSE ][ 'name' ],
'order_status_action' => json_encode($order_common->order_status[ $order_common::ORDER_CLOSE ], JSON_UNESCAPED_UNICODE),
'refund_status' => self::REFUND_COMPLETE,
'refund_status_name' => $this->order_refund_status[ self::REFUND_COMPLETE ][ 'name' ],
// 'refund_money' => $balance_money + $order_info["pay_deposit_money"],
'refund_money' => $deposit_money + $order_info[ "pay_deposit_money" ],
'refund_time' => time(),
];
if (isset($refund_no) && !empty($refund_no)) {
$order_data[ 'deposit_refund_no' ] = $refund_no;
}
model('promotion_presale_order')->update($order_data, [ [ 'id', '=', $data[ 'id' ] ], [ 'site_id', '=', $data[ 'site_id' ] ] ]);
//减少预约数量
model('promotion_presale')->setDec([ [ 'presale_id', '=', $order_info[ 'presale_id' ] ] ], 'sale_num', $order_info[ 'num' ]);
model('promotion_presale_order')->commit();
return $this->success();
} catch (\Exception $e) {
model('promotion_presale_order')->rollback();
return $this->error('', $e->getMessage());
}
}
/*************************************************************************** 用户申请退款操作end**********************************/
/*************************************************************************** 订单退款相关操作start**********************************/
/**
* 订单退款
* @param $order_no
* @param $is_deposit_back
* @param $refund_money_type
* @return array|mixed|void
*/
public function refundPresaleOrder($order_no, $is_deposit_back, $refund_money_type)
{
//$refund_money_type 1原路退款 2线下退款 3退款到余额
//$is_deposit_back 0退定金 1不退定金
$is_offline = 1;
//是否线下退款
if ($refund_money_type == 2) {
$is_offline = 2;
}
$order_info = model('promotion_presale_order')->getInfo(
[
[ 'order_no', '=', $order_no ]
]
);
$order_goods_info = model('order_goods')->getInfo(
[
[ 'order_no', '=', $order_no ]
],
'refund_action_time'
);
if (empty($order_info)) {
return $this->success();
}
if ($order_info[ 'refund_status' ] == self::REFUND_COMPLETE) {
return $this->success();
}
model('promotion_presale_order')->startTrans();
try {
//修改订单退款状态
$pay_model = new Pay();
$deposit_refund_no = $pay_model->createRefundNo();
$final_refund_no = $pay_model->createRefundNo();
$balance_money = 0;
if ($refund_money_type == 1) {//原路退款
//在线支付定金原路退还
if ($order_info[ "pay_deposit_money" ] > 0 && $is_deposit_back == 1 && $is_offline == 1) {
$pay_model = new Pay();
$refund_result = $pay_model->refund($deposit_refund_no, $order_info[ 'pay_deposit_money' ], $order_info[ "deposit_out_trade_no" ], '', $order_info[ 'pay_deposit_money' ], $order_info[ "site_id" ], 1);
if ($refund_result[ "code" ] < 0) {
model('promotion_presale_order')->rollback();
return $refund_result;
}
}
//在线支付尾款原路退还
if ($order_info[ "pay_final_money" ] > 0 && $is_offline == 1) {
$pay_model = new Pay();
$refund_result = $pay_model->refund($final_refund_no, $order_info[ 'pay_final_money' ], $order_info[ "final_out_trade_no" ], '', $order_info[ 'pay_final_money' ], $order_info[ "site_id" ], 1);
if ($refund_result[ "code" ] < 0) {
model('promotion_presale_order')->rollback();
return $refund_result;
}
}
//余额支付定金原路退还
if ($order_info[ "balance_deposit_money" ] > 0 && $is_deposit_back == 1) {
$balance_money += $order_info[ "balance_deposit_money" ];
}
//余额支付尾款原路退还
if ($order_info[ "balance_final_money" ] > 0) {
$balance_money += $order_info[ "balance_final_money" ];
}
if ($balance_money > 0 && $is_offline == 1) {
$member_account_model = new MemberAccount();
$member_account_model->addMemberAccount($order_info[ 'site_id' ], $order_info[ "member_id" ], AccountDict::balance, $balance_money, "presale_refund", "余额返还", "订单关闭返还");
}
} elseif ($refund_money_type == 3) {//退款到余额
$balance_money += $order_info[ 'final_money' ];
if ($is_deposit_back == 1) {
$balance_money += $order_info[ 'presale_deposit_money' ];
}
$member_account_model = new MemberAccount();
$member_account_model->addMemberAccount($order_info[ 'site_id' ], $order_info[ "member_id" ], AccountDict::balance, $balance_money, "presale_refund", "余额返还", "订单关闭返还");
}
$refund_money = 0;
$refund_money += $order_info[ 'final_money' ];
if ($is_deposit_back == 1) {
$refund_money += $order_info[ 'presale_deposit_money' ];
}
$order_common = new PresaleOrderCommon();
$order_data = [
'order_status' => $order_common::ORDER_CLOSE,
'order_status_name' => $order_common->order_status[ $order_common::ORDER_CLOSE ][ 'name' ],
'order_status_action' => json_encode($order_common->order_status[ $order_common::ORDER_CLOSE ], JSON_UNESCAPED_UNICODE),
'deposit_refund_no' => $deposit_refund_no,
'final_refund_no' => $final_refund_no,
'refund_status' => self::REFUND_COMPLETE,
'refund_status_name' => $this->order_refund_status[ self::REFUND_COMPLETE ][ 'name' ],
// 'refund_money' => $order_info["pay_deposit_money"] + $order_info["pay_final_money"] + $balance_money,
'refund_money' => $refund_money,
'refuse_time' => time(),
'apply_refund_time' => $order_goods_info[ 'refund_action_time' ]
];
model('promotion_presale_order')->update($order_data, [ [ 'order_no', '=', $order_no ] ]);
model('promotion_presale_order')->commit();
return $this->success();
} catch (\Exception $e) {
model('promotion_presale_order')->rollback();
return $this->error('', $e->getMessage());
}
}
/*************************************************************************** 订单退款相关操作end**********************************/
}

View File

@@ -0,0 +1,142 @@
<?php
/**
* Niushop商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.niushop.com
* =========================================================
*/
namespace addon\presale\model\share;
use addon\presale\model\Presale as PresaleModel;
use app\model\share\WchatShareBase as BaseModel;
use app\model\system\Config as ConfigModel;
/**
* 分享
*/
class WchatShare extends BaseModel
{
protected $config = [
[
'title' => '预售分享',
'config_key' => 'WCHAT_SHARE_CONFIG_PRESALE_PROMOTE',
'path' => [ '/pages_promotion/presale/detail' ],
'method_prefix' => 'goodsDetail',
],
[
'title' => '预售列表',
'config_key' => 'WCHAT_SHARE_CONFIG_PRESALE_LIST_PROMOTE',
'path' => [ '/pages_promotion/presale/list' ],
'method_prefix' => 'goodsList',
],
];
protected $sort = 7;
/**
* 预售列表
* @param $param
* @return array
*/
protected function goodsListShareData($param)
{
//跳转路径
$link = $this->getShareLink($param);
$config_data = $this->goodsListShareConfig($param)[ 'value' ];
$data = [
'link' => $link,
'desc' => $config_data[ 'desc' ],
'imgUrl' => $config_data[ 'imgUrl' ],
'title' => $config_data[ 'title' ]
];
return [
'permission' => [
'hideOptionMenu' => false,
'hideMenuItems' => [],
],
'data' => $data,//分享内容
];
}
/**
* 预售列表分享配置
* @param $param
* @return array
*/
public function goodsListShareConfig($param)
{
$site_id = $param[ 'site_id' ];
$config = $param[ 'config' ];
$config_model = new ConfigModel();
$data = $config_model->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', $config[ 'config_key' ] ] ])[ 'data' ];
if (empty($data[ 'value' ])) {
$data[ 'value' ] = [
'title' => "预售列表",
'desc' => "预先抢购\n优惠更多",
'imgUrl' => ''
];
}
if (empty($data[ 'value' ][ 'imgUrl' ])) {
$data[ 'value' ][ 'imgUrl' ] = img('addon/presale/icon.png');
}
return [
'value' => $data[ 'value' ],
];
}
/**
* 预售分享数据
* @param $param
* @return array
*/
protected function goodsDetailShareData($param)
{
$member_id = $param[ 'member_id' ] ?? 0;
$url = $param[ 'url' ];
$parse_res = parse_url($url);
parse_str($parse_res[ 'query' ] ?? '', $query);
if (isset($query[ 'id' ])) {
$id = $query[ 'id' ];
$presale_model = new PresaleModel();
$condition = [
[ 'pp.presale_id', '=', $id ],
[ 'pp.status', '=', 1 ],
[ 'g.goods_state', '=', 1 ],
[ 'g.is_delete', '=', 0 ]
];
$presale_info = $presale_model->getPresaleGoodsDetail($condition)[ 'data' ];
if (!empty($presale_info)) {
$config_model = new \app\model\share\WchatShare();
$config_data = $config_model->goodsDetailShareConfig($param);
$title = str_replace('{goods_name}', $presale_info[ 'sku_name' ], $config_data[ 'value' ][ 'title' ]);
$desc = str_replace('{price}', $presale_info[ 'presale_deposit' ], $config_data[ 'value' ][ 'desc' ]);
$link = $parse_res[ 'scheme' ] . '://' . $parse_res[ 'host' ] . $parse_res[ 'path' ] . '?id=' . $id;
if (!empty($member_id)) $link .= '&source_member=' . $member_id;
$image_url = $presale_info[ 'sku_image' ];
$data = [
'title' => $title,
'desc' => $desc,
'link' => $link,
'imgUrl' => $image_url,
];
return [
'permission' => [
'hideOptionMenu' => false,
'hideMenuItems' => [],
],
'data' => $data,//分享内容
];
}
}
}
}

View File

@@ -0,0 +1,89 @@
<?php
/**
* Niushop商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.niushop.com
* =========================================================
*/
namespace addon\presale\model\share;
use app\model\share\WeappShareBase;
use app\model\system\Config as ConfigModel;
/**
* 分享
*/
class WeappShare extends WeappShareBase
{
protected $config = [
[
'title' => '预售列表',
'config_key' => 'WEAPP_SHARE_CONFIG_PRESALE_LIST',
'path' => [ '/pages_promotion/presale/list' ],
'method_prefix' => 'presaleList',
],
];
protected $sort = 8;
/**
* 预售列表
* @param $param
* @return array
*/
protected function presaleListShareData($param)
{
//获取和替换配置数据
$config_data = $this->presaleListShareConfig($param);
$title = $config_data[ 'value' ][ 'title' ];
$image_url = $config_data[ 'value' ][ 'imageUrl' ] ? img($config_data[ 'value' ][ 'imageUrl' ]) : '';
$path = $this->getSharePath($param);
$data = [
'title' => $title,
'path' => $path,
'imageUrl' => $image_url,
];
return [
'permission' => [
'onShareAppMessage' => true,
'onShareTimeline' => true,
],
'data' => $data,//分享内容
];
}
/**
* 预售列表
* @param $param
* @return array
*/
protected function presaleListShareConfig($param)
{
$site_id = $param[ 'site_id' ];
$config = $param[ 'config' ];
$config_model = new ConfigModel();
$data = $config_model->getConfig([
[ 'site_id', '=', $site_id ],
[ 'app_module', '=', 'shop' ],
[ 'config_key', '=', $config[ 'config_key' ] ],
])[ 'data' ];
if (empty($data[ 'value' ])) {
$data[ 'value' ] = [
'title' => '预先抢购更优惠哦!',
'imageUrl' => '',
];
}
$variable = [];
return [
'value' => $data[ 'value' ],
'variable' => $variable,
];
}
}