| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models\alipay;
- use app\modules\admin\models\AlipayThirdForm;
- use app\models\ActivityOrdervoucher;
- use app\models\AlipayVoucherCodeTask;
- use app\models\AlipayVoucherCode;
- use app\models\Coupon;
- use app\models\CouponAutoSend;
- use app\models\User;
- use app\models\Store;
- use app\models\SaasUser;
- use app\models\UserCoupon;
- use yii\base\Model;
- use yii\data\Pagination;
- class Ordervoucher extends Model
- {
- public $activity_ordervoucher_id;
- public $mini_id;
- public $alipay_activity_id;
- public $store_id;
- public $pageSize = 5;
- public $page = 1;
- public $publish_start_time;
- public $publish_end_time;
- public $status;
- public $user_id;
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['store_id', 'mini_id', 'page', 'pageSize'], 'integer'],
- [['publish_start_time', 'publish_end_time', 'status', 'user_id'], 'safe'],
- ];
- }
- public function search() {
- $query = ActivityOrdervoucher::find();
- $query->andWhere(['mini_id' => $this->mini_id, 'is_delete' => 0]);
- $this->publish_end_time && $query->andWhere(['<=', 'publish_start_time', $this->publish_end_time]);
- $this->publish_start_time && $query->andWhere(['>=', 'publish_end_time', $this->publish_start_time]);
- intval($this->status) && $query->andWhere(['status' => $this->status]);
- $count = $query->count();
- $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->pageSize, 'page' => $this->page - 1]);
- $list = $query->limit($pagination->limit)->offset($pagination->offset)->orderBy('id DESC')->all();
- $newList = [];
- foreach ($list as $item) {
- $alipayRes = $this->queryAlipayOV($item->alipay_activity_id);
- if($alipayRes['code'] == 0){
- $status = 0;
- if($alipayRes['data']->activity_status == 'ACTIVE'){
- $status = 1;
- }
- if($alipayRes['data']->activity_status == 'FINISHED'){
- $status = 2;
- }
- if($status != intval($item->status)){
- $item->status = $status;
- $item->save();
- }
- }
- $newList[] = array_merge($item->attributes, (array)$alipayRes['data']);
- }
- return [
- 'code' => 0,
- 'msg' => 'ok',
- 'count' => $count,
- 'data' => $newList,
- ];
- }
- public function queryAlipayOV($activity_id = 0) {
- $form = new AlipayThirdForm();
- $form->mini_id = $this->mini_id;
- $res = $form->AlipayMarketingActivityOrdervoucherQuery($activity_id);
- return $res;
- }
- //创建商家券活动
- public function remove() {
- $condition = [
- 'mini_id' => $this->mini_id,
- 'alipay_activity_id' => $this->alipay_activity_id,
- ];
- $model = ActivityOrdervoucher::findOne($condition);
- if(!$model){
- return [
- 'code' => 1,
- 'msg' => '参数错误',
- ];
- }
- $model->is_delete = 1;
- if(!$model->save()){
- return [
- 'code' => 1,
- 'msg' => '操作失败',
- ];
- }
- return [
- 'code' => 0,
- 'msg' => 'ok',
- ];
- }
- //创建商家券活动
- public function create($coupon_id = 0, $params = []) {
- $coupon = Coupon::findOne($coupon_id);
- if(!$coupon){
- return [
- 'code' => 1,
- 'msg' => '优惠券id错误',
- ];
- }
- $t = \Yii::$app->db->beginTransaction();
- $activity = new ActivityOrdervoucher();
- $activity->mini_id = $this->mini_id;
- $activity->store_id = $this->store_id;
- $activity->coupon_id = $coupon_id;
- $activity->publish_start_time = $params['publish_start_time'];
- $activity->publish_end_time = $params['publish_end_time'];
- if(!$activity->save()){
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $activity->errors[0],
- ];
- }
-
- $form = new AlipayThirdForm();
- $form->mini_id = $this->mini_id;
- $biz_content = [];
- $biz_content = $this->modelToBiz($coupon, $biz_content);
- $biz_content['customer_guide']['voucher_use_guide']['mini_app_path'] = $params['mini_app_path'];
- $biz_content['publish_start_time'] = $params['publish_start_time'];
- $biz_content['publish_end_time'] = $params['publish_end_time'];
- $res = $form->AlipayMarketingActivityOrdervoucherCreate($biz_content);
- if($res['code'] !== 0){
- $t->rollBack();
- return $res;
- }
- $activity_id = $res['data']->activity_id;
- $activity->alipay_activity_id = $activity_id;
- $activity->save();
- $t->commit();
- return $res;
- }
-
- public function modelToBiz($coupon, $biz_content = []) {
- // $biz_content = [
- // 'publish_start_time' => $coupon['name'],
- // 'publish_end_time' => $coupon['name'],
- // ];
- $biz_content['activity_name'] = $coupon['name'];
- $biz_content['code_mode'] = 'MERCHANT_UPLOAD';
- $biz_content['voucher_type'] = 'FIX_VOUCHER';
- $biz_content['out_biz_no'] = date('Y-m-dH:i:s') . rand(100000, 999999);
- $biz_content['belong_merchant_info'] = [
- 'business_type' => 'ISV_FOR_MERCHANT',
- 'merchant_id_type' => 'PID',
- 'merchant_id' => '0',
- ];
- $biz_content['voucher_display_info'] = [
- 'voucher_description' => $coupon['rule'] . ' ',
- ];
- if($coupon['expire_type'] == 1){
- $biz_content['voucher_use_rule'] = [
- 'voucher_valid_period' => [
- 'type' => 'RELATIVE',
- 'valid_days_after_receive' => $coupon['expire_day'],
- 'valid_end_time' => $coupon['end_time'],
- ]
- ];
- }
- if($coupon['expire_type'] == 2){
- $biz_content['voucher_use_rule'] = [
- 'voucher_valid_period' => [
- 'type' => 'ABSOLUTE',
- 'valid_begin_time' => date('Y-m-d H:i:s', $coupon['begin_time']),
- 'valid_end_time' => date('Y-m-d H:i:s', $coupon['end_time']),
- ]
- ];
- }
- $biz_content['voucher_use_rule']['voucher_available_scope'] = [
- 'voucher_available_type' => 'CITY_CODE',
- 'order_voucher_available_city_code' => [
- 'all_city' => true,
- ],
- ];
- //满减
- if($coupon['discount_type'] == 2){
- $biz_content['voucher_type'] = 'FIX_VOUCHER';
- $biz_content['voucher_use_rule']['fix_voucher'] = [
- 'amount' => $coupon['sub_price'],
- ];
- if(intval($coupon['min_price']) > 0){
- $biz_content['voucher_use_rule']['fix_voucher']['floor_amount'] = $coupon['min_price'];
- }
- }
- //折扣
- if($coupon['discount_type'] == 1){
- $biz_content['voucher_type'] = 'DISCOUNT_VOUCHER';
- $biz_content['voucher_use_rule']['discount_voucher'] = [
- 'discount' => $coupon['discount'],
- 'ceiling_amount' => 5000.00,
- ];
- if(intval($coupon['min_price']) > 0){
- $biz_content['voucher_use_rule']['discount_voucher']['floor_amount'] = $coupon['min_price'];
- }
- }
- $biz_content['voucher_send_rule'] = [
- 'voucher_quantity' => 0,
- ];
- $biz_content['customer_guide'] = [
- 'voucher_use_guide' => [
- 'offline_code_use_guide' => true,
- 'mini_app_use_guide' => true,
- 'mini_app_id' => '',
- 'mini_app_path' => '',
- ],
- ];
- return $biz_content;
- }
-
- //创建商家券券码
- public function createCodes($count = 100) {
- if($count > 1000 || $count < 1){
- return [
- 'code' => 1,
- 'msg' => '单次最大生成1000 个,最少1个',
- ];
- }
- $t = \Yii::$app->db->beginTransaction();
- try{
- $ordercoucher = ActivityOrdervoucher::findOne($this->activity_ordervoucher_id);
- // var_dump($ordercoucher);
- if(!$ordercoucher || !$ordercoucher->alipay_activity_id){
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '商家券活动不存在',
- ];
- }
-
- $task = new AlipayVoucherCodeTask();
- $task->store_id = $this->store_id;
- $task->mini_id = $this->mini_id;
- $task->activity_ordervoucher_id = $this->activity_ordervoucher_id;
- if(!$task->save()){
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $task->errors[0],
- ];
- }
- $codes = $this->randCodes($count);
- $data = [];
- foreach ($codes as $code) {
- $data[] = [$this->store_id, $this->mini_id, $task->activity_ordervoucher_id, $code];
- }
- $count = \Yii::$app->db->createCommand()->batchInsert(AlipayVoucherCode::tableName(), ['store_id', 'mini_id', 'activity_ordervoucher_id', 'voucher_code'], $data)->execute();
- if(!$count){
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '操作失败',
- ];
- }
-
- $form = new AlipayThirdForm();
- $form->mini_id = $this->mini_id;
- $res = $form->AlipayMarketingActivityOrdervoucherCodedeposit($ordercoucher->alipay_activity_id, $codes, microtime(true));
- if($res['code'] !== 0){
- $t->rollBack();
- return $res;
- }
-
- $task->is_upload = 1;
- $task->save();
- } catch (\Exception $e){
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- }
- $t->commit();
- return $res;
- }
-
- private function randCodes($count = 100) {
- $codes = [];
- $mtime = str_replace('.', '', microtime(true));
- $irand = 1000;
- for($i = 0; $i < $count; $i++){
- $irand += rand(1, 8);
- $rand = 'ov' . $this->mini_id . $mtime . $irand;
- $codes[] = $rand;
- }
- return $codes;;
- }
-
- //更新券数量
- public function refresh() {
- $form = new AlipayThirdForm();
- $form->mini_id = $this->mini_id;
- $res = $form->AlipayMarketingActivityOrdervoucherCodecount($this->alipay_activity_id);
- $count = 0;
- if($res['code'] == 0){
- $count = $res['data']->success_count;
- }else{
- return $res;
- }
- if(!$count){
- return [
- 'code' => 1,
- 'msg' => '已经导入的券码数量是0',
- ];
- }
- $form = new AlipayThirdForm();
- $form->mini_id = $this->mini_id;
- $biz_content = [
- 'activity_id' => $this->alipay_activity_id,
- 'voucher_quantity' => $count,
- 'out_biz_no' => microtime(true),
- ];
- $res = $form->AlipayMarketingActivityOrdervoucherAppend($biz_content);
- return $res;
- }
- public function use($submit = 0, $ov = '', $total_price = 100) {
- try {
- $alipay_code = AlipayVoucherCode::findOne(['voucher_code' => $ov, 'store_id' => $this->store_id, 'mini_id' => $this->mini_id, 'status' => 0]);
- if (empty($alipay_code)) {
- \Yii::error([__METHOD__, $ov, $this->store_id, $this->mini_id]);
- throw new \Exception('商家券券码错误');
- }
- $activity_ordervoucher = ActivityOrdervoucher::findOne([$alipay_code->activity_ordervoucher_id]);
- $coupon = Coupon::findOne($activity_ordervoucher['coupon_id']);
- if (empty($coupon)) {
- \Yii::error([__METHOD__, $ov, $this->store_id, $this->mini_id]);
- throw new \Exception('系统券未找到.');
- }
- if($total_price < $coupon['min_price']){
- throw new \Exception('不满足优惠券的最低消费');
- }
- if ($coupon['discount_type'] == 1) {
- $sub_price = doubleval($total_price * $coupon['discount'] / 10);
- } else {
- $sub_price = doubleval($coupon['sub_price']);
- }
- if(!$submit){
- return [
- 'code' => 0,
- 'ov' => $ov,
- 'coupon' => $coupon,
- 'total_price' => $total_price,
- 'sub_price' => $sub_price,
- ];
- }
- $user_coupon = UserCoupon::findOne(['voucher_code_id' => $alipay_code['id']]);
- if ($user_coupon) {
- $user_coupon->is_use = 1;
- $user_coupon->save();
- }
-
- $order_voucher = ActivityOrdervoucher::findOne($alipay_code->activity_ordervoucher_id);
- if (empty($order_voucher)) {
- \Yii::error([__METHOD__, $ov, $this->store_id, $this->mini_id]);
- throw new \Exception('商家券活动未找到');
- }
- $data = [
- 'activity_id' => $order_voucher->alipay_activity_id,
- 'biz_dt' => date('Y-m-d H:i:s'),
- 'voucher_code' => $alipay_code->voucher_code,
- 'trade_channel' => 'OTHER_TRADE_CHANNEL',
- 'total_fee' => $total_price,
- ];
- $form = new AlipayThirdForm();
- $result = $form->AlipayMarketingActivityOrdervoucherUse($data);
- $file_name = \Yii::$app->runtimePath . '/logs/app_order_voucherUse.log';
- file_put_contents($file_name, "\r\n" . '[核销结果推送][' . date('Y-m-d H:i:s') . ']['.$ov.']' . json_encode($result), FILE_APPEND);
- if ($result['code'] === 0) {
- $alipay_code->status = 1;
- $alipay_code->clerk_saas_id = $this->user_id;
- $alipay_code->total_price = $total_price;
- $alipay_code->sub_price = $sub_price;
- $alipay_code->save();
- $result['ov'] = $ov;
- $result['coupon'] = $coupon;
- $result['total_price'] = $total_price;
- $result['sub_price'] = $sub_price;
- }
- return $result;
- } catch (\Exception $e) {
- \Yii::error([__METHOD__, $e, $this->store_id, $this->mini_id]);
- return [
- 'code' => 1,
- 'msg' => '操作失败。' . $e->getMessage()
- ];
- }
- }
- }
|