| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace app\controller\manager; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use app\exception\RepositoryException; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use app\model\CouponMain; | 
					
						
							| 
									
										
										
										
											2022-01-14 18:21:18 +08:00
										 |  |  | use app\model\CouponType as CouponTypeModel; | 
					
						
							| 
									
										
										
										
											2021-12-16 10:34:06 +08:00
										 |  |  | use app\model\Business as BusinessModel; | 
					
						
							| 
									
										
										
										
											2021-12-22 15:45:48 +08:00
										 |  |  | use app\model\Member as MemberModel; | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  | use app\repository\BusinessRepository; | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  | use app\repository\CouponRepository; | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  | use app\validate\CouponRelease; | 
					
						
							| 
									
										
										
										
											2021-12-02 18:34:44 +08:00
										 |  |  | use app\validate\CouponUsingRule; | 
					
						
							| 
									
										
										
										
											2022-01-21 10:32:52 +08:00
										 |  |  | use Endroid\QrCode\Builder\Builder; | 
					
						
							|  |  |  | use Endroid\QrCode\Encoding\Encoding; | 
					
						
							|  |  |  | use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh; | 
					
						
							|  |  |  | use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin; | 
					
						
							|  |  |  | use Endroid\QrCode\Writer\PngWriter; | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  | use Exception; | 
					
						
							| 
									
										
										
										
											2021-12-16 10:34:06 +08:00
										 |  |  | use think\facade\Config; | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  | use think\facade\Db; | 
					
						
							|  |  |  | use think\response\Json; | 
					
						
							|  |  |  | use think\response\View; | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  | use think\facade\Config as CConfig; | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2022-03-31 14:42:56 +08:00
										 |  |  |  * 签到券相关 | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  |  **/ | 
					
						
							|  |  |  | class Coupon extends Base | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-21 10:32:52 +08:00
										 |  |  |     protected $noNeedLogin = ["downloadQrCode"]; | 
					
						
							| 
									
										
										
										
											2022-03-24 10:59:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |     protected function initialize() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         parent::initialize(); // TODO: Change the autogenerated stub
 | 
					
						
							|  |  |  |         CConfig::load('extra/distribution_proportion', 'distributionProportion'); | 
					
						
							| 
									
										
										
										
											2022-03-15 14:10:36 +08:00
										 |  |  |         CConfig::load('extra/distribution_proportion_user', 'distributionProportionUser'); | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |         $this->data['distributionProportion'] = config('distributionProportion'); | 
					
						
							| 
									
										
										
										
											2022-03-15 14:10:36 +08:00
										 |  |  |         $this->data['distributionProportionUser'] = config('distributionProportionUser'); | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * 列表 | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Json|View | 
					
						
							|  |  |  |      * @throws Exception | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function index() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($this->request->isPost()) { | 
					
						
							| 
									
										
										
										
											2021-12-23 10:23:10 +08:00
										 |  |  |             $repo = new CouponMain(); | 
					
						
							|  |  |  |             $repo = CouponRepository::getInstance($repo); | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  |             $keyword = $this->request->param('keyword/s', ''); | 
					
						
							| 
									
										
										
										
											2021-12-16 17:41:17 +08:00
										 |  |  |             $on_shelf = $this->request->param('on_shelf',CouponMain::on_shelf_off); | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  |             $start_time = $this->request->param('start_time',); | 
					
						
							|  |  |  |             $end_time = $this->request->param('end_time'); | 
					
						
							|  |  |  |             $page = $this->request->param('page/d', 1); | 
					
						
							|  |  |  |             $size = $this->request->param('size/d', 30); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $whereMap = []; | 
					
						
							| 
									
										
										
										
											2021-12-23 10:23:10 +08:00
										 |  |  |             $orders = ['a.sort' => 'desc', "a.id" => "desc"]; | 
					
						
							|  |  |  |             if (in_array($on_shelf, [CouponMain::COMMON_ON, CouponMain::COMMON_OFF])) { | 
					
						
							|  |  |  |                 $whereMap[] = ['a.on_shelf', '=', $on_shelf]; | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  |             } | 
					
						
							|  |  |  |             if (!empty($start_time)) { | 
					
						
							| 
									
										
										
										
											2021-12-23 10:23:10 +08:00
										 |  |  |                 $whereMap[] = ['a.start_time', '>= TIME' , $start_time]; | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  |             } | 
					
						
							|  |  |  |             if (!empty($end_time)) { | 
					
						
							| 
									
										
										
										
											2021-12-23 10:23:10 +08:00
										 |  |  |                 $whereMap[] = ['a.end_time', '<= TIME', $end_time]; | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  |             } | 
					
						
							|  |  |  |             if (!empty($keyword)) { | 
					
						
							| 
									
										
										
										
											2021-12-23 10:23:10 +08:00
										 |  |  |                 $whereMap[] = ['a.name|b.business_name', 'like', "%" . $keyword . "%"]; | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-12-23 10:23:10 +08:00
										 |  |  |             $list =$repo->couponMainList($whereMap,$page,$size,$orders,$this->auth); | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  |             $time = time(); | 
					
						
							|  |  |  |             $list['list']->each(function ($item) use ($time) { | 
					
						
							|  |  |  |                 if (strtotime($item->start_time) > $time) { | 
					
						
							|  |  |  |                     $item->state_text = '<span >未开始</span>'; | 
					
						
							|  |  |  |                 } else if ((strtotime($item->start_time) < $time) && (strtotime($item->end_time) > $time)) { | 
					
						
							|  |  |  |                     $item->state_text = '<span  class="f_green">进行中</span>'; | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     $item->state_text = '<span  class="f_red">已过期</span>'; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-12-20 17:53:03 +08:00
										 |  |  |                 //渠道商名称
 | 
					
						
							| 
									
										
										
										
											2021-12-22 15:45:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-20 17:53:03 +08:00
										 |  |  |                 if(isset($item->business) && $item->business && !empty($item->business->agency_code)){ | 
					
						
							| 
									
										
										
										
											2021-12-22 15:45:48 +08:00
										 |  |  |                     $item->agency_text = MemberModel::whereRaw("FIND_IN_SET('".MemberModel::ANENT_ROLE_ID."',roles)"  ) | 
					
						
							|  |  |  |                         ->where("business_code",$item->business->agency_code)->value("nickname",""); | 
					
						
							| 
									
										
										
										
											2021-12-20 17:53:03 +08:00
										 |  |  |                 }else{ | 
					
						
							|  |  |  |                     $item->agency_text = ''; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  |             }); | 
					
						
							|  |  |  |             return $this->json(0, 'success', $list); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $this->view(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-30 15:26:38 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * 上架状态 0上架 1下架 | 
					
						
							|  |  |  |      * */ | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  |     public function shelf() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $id = input("id/d", 0); | 
					
						
							|  |  |  |         $on_shelf = input("on_shelf/d", 1); | 
					
						
							|  |  |  |         $model = new CouponMain(); | 
					
						
							|  |  |  |         $repo = CouponRepository::getInstance($model); | 
					
						
							|  |  |  |         $coupon = $repo->findById($id); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (empty($coupon)) { | 
					
						
							| 
									
										
										
										
											2022-03-31 14:42:56 +08:00
										 |  |  |             return $this->json(4001, "签到券不存在"); | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-24 16:34:56 +08:00
										 |  |  |         if (!in_array($on_shelf, [CouponMain::COMMON_OFF, CouponMain::COMMON_ON])) { | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  |             return $this->json(4001, "状态错误"); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-11-24 16:34:56 +08:00
										 |  |  |         $coupon->save(["on_shelf" => $on_shelf]); | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  |         return $this->json(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-24 16:34:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |      * 持有详细 | 
					
						
							| 
									
										
										
										
											2021-11-24 16:34:56 +08:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return Json|View | 
					
						
							|  |  |  |      * @throws Exception | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function info() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ($this->request->isPost()) { | 
					
						
							|  |  |  |             $model = new CouponMain(); | 
					
						
							|  |  |  |             $repo = CouponRepository::getInstance($model); | 
					
						
							|  |  |  |             $id = input("id/d"); | 
					
						
							|  |  |  |             $keyword = $this->request->param('keyword/s', ''); | 
					
						
							|  |  |  |             $page = $this->request->param('page/d', 1); | 
					
						
							|  |  |  |             $size = $this->request->param('size/d', 30); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $list = $repo->couponMainHasList($id, $keyword, $page, $size); | 
					
						
							|  |  |  |             $time = time(); | 
					
						
							|  |  |  |             $list["list"]->each(function ($item) use ($time) { | 
					
						
							|  |  |  |                 if (strtotime($item['end_time']) < $time) { | 
					
						
							|  |  |  |                     $item->time_state = '已过期'; | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     $item->time_state = '未过期'; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |             return $this->json(0, 'success', $list); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $id = input("id/d"); | 
					
						
							|  |  |  |         $model = new CouponMain(); | 
					
						
							|  |  |  |         $repo = CouponRepository::getInstance($model); | 
					
						
							|  |  |  |         $coupon = $repo->getModel()->with(["business" => function ($query) { | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |             $query | 
					
						
							|  |  |  |                 ->field("code,business_name,business_subtitle,type") | 
					
						
							| 
									
										
										
										
											2021-11-24 16:34:56 +08:00
										 |  |  |                 ->with('category'); | 
					
						
							|  |  |  |         }, "couponType"])->where("id", $id)->find(); | 
					
						
							|  |  |  |         if (empty($coupon)) { | 
					
						
							| 
									
										
										
										
											2022-03-31 14:42:56 +08:00
										 |  |  |             return $this->error("签到券不存在"); | 
					
						
							| 
									
										
										
										
											2021-11-24 16:34:56 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |         $this->data["coupon"] = $coupon; | 
					
						
							|  |  |  |         return $this->view(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-03-31 14:42:56 +08:00
										 |  |  |      * 发布签到券 | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return Json|View | 
					
						
							|  |  |  |      * @throws Exception | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function release() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ($this->request->isPost()) { | 
					
						
							|  |  |  |             $data = input("item/a", []); | 
					
						
							| 
									
										
										
										
											2022-01-19 13:40:46 +08:00
										 |  |  |             //$usingRule = input("using_rule/a", []);
 | 
					
						
							| 
									
										
										
										
											2021-12-16 10:34:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-30 15:52:49 +08:00
										 |  |  |             $business   = BusinessRepository::getInstance()->getBusinessAccount($data["business_code"], true); | 
					
						
							|  |  |  |             $time       = time(); | 
					
						
							| 
									
										
										
										
											2021-12-16 10:34:06 +08:00
										 |  |  |             if (empty($business)) { | 
					
						
							|  |  |  |                 return $this->json(4001, '商家不存在'); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-12-20 16:13:30 +08:00
										 |  |  |             if($business->enable != BusinessModel::COMMON_OFF){ | 
					
						
							|  |  |  |                 return $this->json(4001, "商家已禁用"); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-03-30 17:22:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-31 14:42:56 +08:00
										 |  |  |             // 指定时间段内发布的签到券个数
 | 
					
						
							| 
									
										
										
										
											2022-03-30 17:22:07 +08:00
										 |  |  |             $TimeListModeReleaseCount = BusinessRepository::getInstance() | 
					
						
							|  |  |  |                 ->getTimeListModeReleaseCount($business["code"],$business["time_limit_start"],$business["time_limit_end"]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $businessModel = $business["model"]; | 
					
						
							|  |  |  |             if($business["model"] == BusinessModel::model_time_limit){ | 
					
						
							|  |  |  |                 //如果到了时间 或者发布数量到达规定 更改商家模式
 | 
					
						
							|  |  |  |                 if( strtotime($business["time_limit_end"] ) < $time | 
					
						
							|  |  |  |                     || | 
					
						
							|  |  |  |                     (($TimeListModeReleaseCount) >= $business["time_limit_release_count"]) | 
					
						
							|  |  |  |                 ){ | 
					
						
							|  |  |  |                     $businessModel = BusinessModel::model_ordinary; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |             $validate = new CouponRelease(); | 
					
						
							| 
									
										
										
										
											2021-12-16 10:34:06 +08:00
										 |  |  |             //普通商家要验证扣除金额
 | 
					
						
							| 
									
										
										
										
											2022-03-30 17:22:07 +08:00
										 |  |  |             if (!$validate->scene(($businessModel == BusinessModel::model_ordinary) ? "ordinary" : "")->check($data)) { | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |                 return $this->json(4001, $validate->getError()); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-12-02 18:34:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-19 13:40:46 +08:00
										 |  |  |             //$usingRuleValidate = new CouponUsingRule();
 | 
					
						
							|  |  |  |             //if (!$usingRuleValidate->check($usingRule)) {
 | 
					
						
							|  |  |  |             //    return $this->json(4001, $usingRuleValidate->getError());
 | 
					
						
							|  |  |  |             //}
 | 
					
						
							| 
									
										
										
										
											2021-12-02 18:34:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 10:34:06 +08:00
										 |  |  |             //持有限量
 | 
					
						
							|  |  |  |             Config::load("extra/wechat","wechat"); | 
					
						
							|  |  |  |             $hasCouponMax = config("wechat.hasCouponMax")??0; | 
					
						
							|  |  |  |             if ($hasCouponMax > 0) { | 
					
						
							|  |  |  |                 $hasCouponCount = CouponRepository::getInstance()->getBusinessOnShelfOnCount($business->code); | 
					
						
							|  |  |  |                 if ($hasCouponCount > $hasCouponMax) { | 
					
						
							| 
									
										
										
										
											2022-03-31 14:42:56 +08:00
										 |  |  |                     return $this->json(4001, "商家持有商家签到券不能超过{$hasCouponMax}"); | 
					
						
							| 
									
										
										
										
											2021-12-16 10:34:06 +08:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-12-16 10:34:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-14 18:21:18 +08:00
										 |  |  |             //检查类型
 | 
					
						
							|  |  |  |             if(CouponTypeModel::checkType($data['type']) !== true){ | 
					
						
							| 
									
										
										
										
											2022-03-31 14:42:56 +08:00
										 |  |  |                 return $this->json(4001, '签到券类型不存在'); | 
					
						
							| 
									
										
										
										
											2022-01-14 18:21:18 +08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-01-20 20:12:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-19 13:40:46 +08:00
										 |  |  |             $type = CouponRepository::getInstance()->getCouponTypeAll(); | 
					
						
							| 
									
										
										
										
											2022-01-20 20:12:22 +08:00
										 |  |  |             $type = array_column($type->toArray(), null, "id"); | 
					
						
							| 
									
										
										
										
											2022-01-19 13:40:46 +08:00
										 |  |  |             $data['type_name']              = $type[$data['type']]['name']; | 
					
						
							| 
									
										
										
										
											2021-12-16 10:34:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 13:37:55 +08:00
										 |  |  |             $data['business_type']          = $business['type']; | 
					
						
							|  |  |  |             $data['business_name']          = $business['business_name']; | 
					
						
							|  |  |  |             $data['lng']                    = $business['lng']; | 
					
						
							|  |  |  |             $data['lat']                    = $business['lat']; | 
					
						
							|  |  |  |             $data['business_circle_id']     = $business['business_circle_id']; | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-14 18:21:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-21 18:09:02 +08:00
										 |  |  |             $data['commission_agency']              = $this->data["distributionProportion"]["agency"]; | 
					
						
							|  |  |  |             $data['commission_admin']               = $this->data["distributionProportion"]["admin"]; | 
					
						
							|  |  |  |             $data['commission_consumer']            = $this->data["distributionProportion"]["consumer"]; | 
					
						
							|  |  |  |             $data['commission_dis_admin']           = $this->data["distributionProportionUser"]["admin"]; | 
					
						
							|  |  |  |             $data['commission_dis_distribution']    = $this->data["distributionProportionUser"]["distribution"]; | 
					
						
							|  |  |  |             $data['commission_dis_distributed']     = $this->data["distributionProportionUser"]["distributed"]; | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-15 14:10:36 +08:00
										 |  |  |             $totalCommission = $data['commission_agency'] + $data['commission_admin'] + $data['commission_consumer']; | 
					
						
							|  |  |  |             if ($totalCommission != 100) { | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |                 return $this->json(5002, "分配比例总和不等于100"); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-03-15 14:10:36 +08:00
										 |  |  |             $totalCommissionDis = $data['commission_dis_admin'] + $data['commission_dis_distribution'] + $data['commission_dis_distributed']; | 
					
						
							|  |  |  |             if ($totalCommissionDis != 100) { | 
					
						
							|  |  |  |                 return $this->json(5002, "(开启分销)分配比例总和不等于100"); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             //保留两位小数
 | 
					
						
							| 
									
										
										
										
											2022-01-20 14:09:20 +08:00
										 |  |  |             $data['deduction_money'] = floor($data['deduction_money'] * 100) / 100; | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-15 14:10:36 +08:00
										 |  |  |             //如果开启分销  添加操作 处理扣除金额
 | 
					
						
							|  |  |  |             if($data["is_distribution"] == CouponMain::COMMON_ON ){ | 
					
						
							| 
									
										
										
										
											2022-03-15 18:51:28 +08:00
										 |  |  |                 if($business->is_assign == BusinessModel::COMMON_ON){ | 
					
						
							|  |  |  |                     return $this->json(5002, "商家已经指派了渠道商,不能开启分销"); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-03-16 10:13:11 +08:00
										 |  |  |             }else{ | 
					
						
							|  |  |  |                 if($business->is_assign == BusinessModel::COMMON_OFF){ | 
					
						
							|  |  |  |                     return $this->json(5002, "商家没有指派渠道商,不能关闭分销"); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-03-15 18:51:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-28 20:17:16 +08:00
										 |  |  |             //if($data['commission_dis_distribution'] < $data['commission_dis_distributed']){
 | 
					
						
							| 
									
										
										
										
											2022-03-15 18:51:28 +08:00
										 |  |  |                 //检测【分销者】部分是否最少0.3元
 | 
					
						
							|  |  |  |                 $distributionMoney = ($data['commission_dis_distribution'] / 100) * $data['deduction_money']; | 
					
						
							|  |  |  |                 if ($distributionMoney != 0 && $distributionMoney < CouponMain::min_redpack_money) { | 
					
						
							|  |  |  |                     return $this->json(4002, | 
					
						
							| 
									
										
										
										
											2022-03-18 11:37:30 +08:00
										 |  |  |                         "开启分销:【领券分销者】红包不足" | 
					
						
							| 
									
										
										
										
											2022-03-15 18:51:28 +08:00
										 |  |  |                         . CouponMain::min_redpack_money | 
					
						
							|  |  |  |                         . "元,扣除金额最低" . | 
					
						
							|  |  |  |                         (CouponMain::min_redpack_money * (100 / ($data['commission_dis_distribution']))) | 
					
						
							|  |  |  |                         . "元"); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-03-28 20:17:16 +08:00
										 |  |  |             //}else{
 | 
					
						
							| 
									
										
										
										
											2022-03-18 11:37:30 +08:00
										 |  |  |                 //检测【核销分销者】部分是否最少0.3元
 | 
					
						
							| 
									
										
										
										
											2022-03-15 18:51:28 +08:00
										 |  |  |                 $distributedMoney = ($data['commission_dis_distributed'] / 100) * $data['deduction_money']; | 
					
						
							|  |  |  |                 if ($distributedMoney != 0 && $distributedMoney < CouponMain::min_redpack_money) { | 
					
						
							|  |  |  |                     return $this->json(4002, | 
					
						
							| 
									
										
										
										
											2022-03-18 11:37:30 +08:00
										 |  |  |                         "开启分销:【核销分销者】红包不足" | 
					
						
							| 
									
										
										
										
											2022-03-15 18:51:28 +08:00
										 |  |  |                         . CouponMain::min_redpack_money | 
					
						
							|  |  |  |                         . "元,扣除金额最低" . | 
					
						
							|  |  |  |                         (CouponMain::min_redpack_money * (100 / ($data['commission_dis_distributed']))) | 
					
						
							|  |  |  |                         . "元"); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-03-28 20:17:16 +08:00
										 |  |  |             //}
 | 
					
						
							| 
									
										
										
										
											2022-03-15 18:51:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-16 10:13:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-28 20:17:16 +08:00
										 |  |  |             //if($data['commission_agency'] < $data['commission_consumer']){
 | 
					
						
							| 
									
										
										
										
											2022-03-15 18:51:28 +08:00
										 |  |  |                 //检测【渠道商】部分是否最少0.3元
 | 
					
						
							|  |  |  |                 $agencyMoney = ($data['commission_agency'] / 100) * $data['deduction_money']; | 
					
						
							|  |  |  |                 if ($agencyMoney != 0 && $agencyMoney < CouponMain::min_redpack_money) { | 
					
						
							|  |  |  |                     return $this->json(4002, | 
					
						
							|  |  |  |                         "关闭分销:【渠道商】红包不足" | 
					
						
							|  |  |  |                         . CouponMain::min_redpack_money | 
					
						
							|  |  |  |                         . "元,扣除金额最低" . | 
					
						
							|  |  |  |                         (CouponMain::min_redpack_money * (100 / ($data['commission_agency']))) | 
					
						
							|  |  |  |                         . "元"); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-03-28 20:17:16 +08:00
										 |  |  |             //}else{
 | 
					
						
							| 
									
										
										
										
											2022-03-15 18:51:28 +08:00
										 |  |  |                 //检测【消费者】部分是否最少0.3元
 | 
					
						
							|  |  |  |                 $consumerMoney = ($data['commission_consumer'] / 100) * $data['deduction_money']; | 
					
						
							|  |  |  |                 if ($consumerMoney != 0 && $consumerMoney < CouponMain::min_redpack_money) { | 
					
						
							|  |  |  |                     return $this->json(4002, | 
					
						
							|  |  |  |                         "关闭分销:【消费者】红包不足" | 
					
						
							|  |  |  |                         . CouponMain::min_redpack_money | 
					
						
							|  |  |  |                         . "元,扣除金额最低" . | 
					
						
							|  |  |  |                         (CouponMain::min_redpack_money * (100 / ($data['commission_consumer']))) | 
					
						
							|  |  |  |                         . "元"); | 
					
						
							| 
									
										
										
										
											2022-03-15 14:10:36 +08:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-03-28 20:17:16 +08:00
										 |  |  |             //}
 | 
					
						
							| 
									
										
										
										
											2022-02-08 14:32:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-16 10:13:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-20 14:09:20 +08:00
										 |  |  |             $totalMoney = $data['deduction_money'] * $data['count']; | 
					
						
							| 
									
										
										
										
											2021-12-14 17:34:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-31 14:42:56 +08:00
										 |  |  |             //未领取的签到券
 | 
					
						
							| 
									
										
										
										
											2021-12-16 10:34:06 +08:00
										 |  |  |             $NotClaimedMoney = CouponRepository::getInstance()->getBusinessNotClaimedCoupon($business["code"]); | 
					
						
							| 
									
										
										
										
											2022-03-30 18:11:53 +08:00
										 |  |  | //            if(
 | 
					
						
							|  |  |  | //                $business["model"] == BusinessModel::model_ordinary
 | 
					
						
							|  |  |  | //                ||
 | 
					
						
							|  |  |  | //                (
 | 
					
						
							|  |  |  | //                    $business["model"] == BusinessModel::model_time_limit
 | 
					
						
							|  |  |  | //                    &&
 | 
					
						
							|  |  |  | //                    (
 | 
					
						
							|  |  |  | //                        strtotime($business["time_limit_end"]) < $time
 | 
					
						
							|  |  |  | //                        ||
 | 
					
						
							|  |  |  | //                        $TimeListModeReleaseCount >= $business["time_limit_release_count"]
 | 
					
						
							|  |  |  | //                    )
 | 
					
						
							|  |  |  | //                )
 | 
					
						
							|  |  |  | //            ){
 | 
					
						
							|  |  |  | //                if ($business["balance"] < ($totalMoney + $NotClaimedMoney)) {
 | 
					
						
							|  |  |  | //                    return $this->json(4001, '商家余额不足');
 | 
					
						
							|  |  |  | //                }
 | 
					
						
							|  |  |  | //            }
 | 
					
						
							|  |  |  |             if ($business["balance"] < ($totalMoney + $NotClaimedMoney)) { | 
					
						
							|  |  |  |                 return $this->json(4001, '商家余额不足'); | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $date = date("Y-m-d H:i:s"); | 
					
						
							|  |  |  |             $data['create_time'] = $date; | 
					
						
							|  |  |  |             $data['update_time'] = $date; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 18:34:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |             Db::startTrans(); | 
					
						
							|  |  |  |             try { | 
					
						
							| 
									
										
										
										
											2022-01-19 13:40:46 +08:00
										 |  |  |                 //CouponRepository::getInstance()->releaseCouponMain($data, $totalMoney, $usingRule);
 | 
					
						
							|  |  |  |                 CouponRepository::getInstance()->releaseCouponMain($data); | 
					
						
							| 
									
										
										
										
											2022-03-30 15:52:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 if($business["model"] == BusinessModel::model_time_limit){ | 
					
						
							|  |  |  |                     //如果到了时间 或者发布数量到达规定 更改商家模式
 | 
					
						
							|  |  |  |                     if( strtotime($business["time_limit_end"] ) < $time | 
					
						
							|  |  |  |                         || | 
					
						
							|  |  |  |                         (($TimeListModeReleaseCount+1) >= $business["time_limit_release_count"]) | 
					
						
							|  |  |  |                     ){ | 
					
						
							|  |  |  |                         $business->save(["model"=>BusinessModel::model_ordinary]); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |                 Db::commit(); | 
					
						
							|  |  |  |                 return $this->json(); | 
					
						
							|  |  |  |             } catch (RepositoryException $e) { | 
					
						
							|  |  |  |                 Db::rollback(); | 
					
						
							|  |  |  |                 return $this->json(5001, "发布失败" . $e->getMessage()); | 
					
						
							|  |  |  |             } catch (\think\Exception $e) { | 
					
						
							|  |  |  |                 Db::rollback(); | 
					
						
							|  |  |  |                 return $this->json(5002, "发布失败" . $e->getMessage()); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->data['business'] = BusinessRepository::getInstance()->getBusinessAll(); | 
					
						
							|  |  |  |         $this->data['type'] = CouponRepository::getInstance()->getCouponTypeAll(); | 
					
						
							|  |  |  |         return $this->view('add'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 修改 | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Json|View | 
					
						
							|  |  |  |      * @throws Exception | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function edit() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-01-19 13:40:46 +08:00
										 |  |  |         $couponMain = CouponMain::findOne(["id" => input("id/d", 0)], []); | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |         if ($this->request->isPost()) { | 
					
						
							|  |  |  |             $data = input("item/a", []); | 
					
						
							| 
									
										
										
										
											2022-01-19 13:40:46 +08:00
										 |  |  |             //$usingRule = input("using_rule/a", []);
 | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |             if (empty($couponMain)) { | 
					
						
							| 
									
										
										
										
											2022-03-31 14:42:56 +08:00
										 |  |  |                 return $this->json(4001, "签到券不存在"); | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-03-15 18:51:28 +08:00
										 |  |  |             if (!isset($couponMain->business) || empty($couponMain->business)) { | 
					
						
							| 
									
										
										
										
											2022-03-31 14:42:56 +08:00
										 |  |  |                 return $this->json(4001, "签到券所属商家不存在"); | 
					
						
							| 
									
										
										
										
											2022-03-15 18:51:28 +08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |             $validate = new CouponRelease(); | 
					
						
							|  |  |  |             if (!$validate->scene("edit")->check($data)) { | 
					
						
							|  |  |  |                 return $this->json(4001, $validate->getError()); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-12-02 18:34:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-19 13:40:46 +08:00
										 |  |  |             //$usingRuleValidate = new CouponUsingRule();
 | 
					
						
							|  |  |  |             //if (!$usingRuleValidate->check($usingRule)) {
 | 
					
						
							|  |  |  |             //    return $this->json(4001, $usingRuleValidate->getError());
 | 
					
						
							|  |  |  |             //}
 | 
					
						
							| 
									
										
										
										
											2021-12-02 18:34:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |             $business = BusinessRepository::getInstance()->getBusinessAccount($data["business_code"], true); | 
					
						
							|  |  |  |             if (empty($business)) { | 
					
						
							|  |  |  |                 return $this->json(4001, '商家不存在'); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-01-14 18:21:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             //检查类型
 | 
					
						
							|  |  |  |             if(CouponTypeModel::checkType($data['type']) !== true){ | 
					
						
							| 
									
										
										
										
											2022-03-31 14:42:56 +08:00
										 |  |  |                 return $this->json(4001, '签到券类型不存在'); | 
					
						
							| 
									
										
										
										
											2022-01-14 18:21:18 +08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-01-20 20:12:22 +08:00
										 |  |  |             $type                   = CouponRepository::getInstance()->getCouponTypeAll(); | 
					
						
							|  |  |  |             $type                   = array_column($type->toArray(), null, "id"); | 
					
						
							|  |  |  |             $data['type_name']      = $type[$data['type']]['name']; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-14 18:21:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |             //更新经纬度
 | 
					
						
							| 
									
										
										
										
											2021-12-02 18:34:44 +08:00
										 |  |  |             $data['business_type'] = $business['type']; | 
					
						
							|  |  |  |             $data['business_name'] = $business['business_name']; | 
					
						
							|  |  |  |             $data['lng'] = $business['lng']; | 
					
						
							|  |  |  |             $data['lat'] = $business['lat']; | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-15 14:10:36 +08:00
										 |  |  |             $data['commission_agency']              = input("item.commission_agency/d", 0,"abs"); | 
					
						
							|  |  |  |             $data['commission_admin']               = input("item.commission_admin/d", 0,"abs"); | 
					
						
							|  |  |  |             $data['commission_consumer']            = input("item.commission_consumer/d", 0,"abs"); | 
					
						
							|  |  |  |             $data['commission_dis_admin']           = input("item.commission_dis_admin/d", 0,"abs"); | 
					
						
							|  |  |  |             $data['commission_dis_distribution']    = input("item.commission_dis_distribution/d", 0,"abs"); | 
					
						
							|  |  |  |             $data['commission_dis_distributed']     = input("item.commission_dis_distributed/d", 0,"abs"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $totalCommission = $data['commission_agency'] + $data['commission_admin'] + $data['commission_consumer']; | 
					
						
							|  |  |  |             if ($totalCommission != 100) { | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |                 return $this->json(5002, "分配比例总和不等于100"); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-03-15 18:51:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-15 14:10:36 +08:00
										 |  |  |             $totalCommissionDis = $data['commission_dis_admin'] + $data['commission_dis_distribution'] + $data['commission_dis_distributed']; | 
					
						
							|  |  |  |             if ($totalCommissionDis != 100) { | 
					
						
							|  |  |  |                 return $this->json(5002, "(开启分销)分配比例总和不等于100"); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-03-15 18:51:28 +08:00
										 |  |  |             //如果开启分销
 | 
					
						
							| 
									
										
										
										
											2022-03-15 14:10:36 +08:00
										 |  |  |             if($data["is_distribution"] == CouponMain::COMMON_ON ){ | 
					
						
							| 
									
										
										
										
											2022-03-15 18:51:28 +08:00
										 |  |  |                 if($business['is_assign'] == BusinessModel::COMMON_ON){ | 
					
						
							|  |  |  |                     return $this->json(5002, "商家已经指派了渠道商,不能开启分销"); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-03-16 10:13:11 +08:00
										 |  |  |             }else{ | 
					
						
							|  |  |  |                 if($business['is_assign'] == BusinessModel::COMMON_OFF){ | 
					
						
							|  |  |  |                     return $this->json(5002, "商家没有指派渠道商,不能关闭分销"); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-03-28 20:17:16 +08:00
										 |  |  |             //if($data['commission_dis_distribution'] < $data['commission_dis_distributed']){
 | 
					
						
							| 
									
										
										
										
											2022-03-18 11:37:30 +08:00
										 |  |  |                 //检测【领券分销者】部分是否最少0.3元
 | 
					
						
							| 
									
										
										
										
											2022-03-16 10:49:59 +08:00
										 |  |  |                 $distributionMoney = ($data['commission_dis_distribution']/100) * $couponMain['deduction_money']; | 
					
						
							| 
									
										
										
										
											2022-03-15 14:10:36 +08:00
										 |  |  |                 if ($distributionMoney != 0 && $distributionMoney < CouponMain::min_redpack_money) { | 
					
						
							|  |  |  |                     return $this->json(4002, | 
					
						
							| 
									
										
										
										
											2022-03-18 11:37:30 +08:00
										 |  |  |                         "开启分销:【领券分销者】红包不足" | 
					
						
							| 
									
										
										
										
											2022-03-15 14:10:36 +08:00
										 |  |  |                         .CouponMain::min_redpack_money | 
					
						
							| 
									
										
										
										
											2022-03-18 11:37:30 +08:00
										 |  |  |                         ."元,【领券分销者】占比最低" . | 
					
						
							| 
									
										
										
										
											2022-03-15 18:51:28 +08:00
										 |  |  |                         (round((CouponMain::min_redpack_money / ($data['deduction_money'])) * 100, 2)) | 
					
						
							| 
									
										
										
										
											2022-03-15 14:10:36 +08:00
										 |  |  |                         . "%" | 
					
						
							|  |  |  |                     ); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-03-28 20:17:16 +08:00
										 |  |  |             //}else{
 | 
					
						
							| 
									
										
										
										
											2022-03-18 11:37:30 +08:00
										 |  |  |                 //检测【核销分销者】部分是否最少0.3元
 | 
					
						
							| 
									
										
										
										
											2022-03-16 10:49:59 +08:00
										 |  |  |                 $distributedMoney = ($data['commission_dis_distributed']/100) * $couponMain['deduction_money']; | 
					
						
							| 
									
										
										
										
											2022-03-15 14:10:36 +08:00
										 |  |  |                 if ($distributedMoney != 0 && $distributedMoney < CouponMain::min_redpack_money) { | 
					
						
							|  |  |  |                     return $this->json(4002, | 
					
						
							| 
									
										
										
										
											2022-03-18 11:37:30 +08:00
										 |  |  |                         "开启分销:【核销分销者】红包不足" | 
					
						
							| 
									
										
										
										
											2022-03-15 14:10:36 +08:00
										 |  |  |                         .CouponMain::min_redpack_money | 
					
						
							| 
									
										
										
										
											2022-03-18 11:37:30 +08:00
										 |  |  |                         ."元,【核销分销者】占比最低" . | 
					
						
							| 
									
										
										
										
											2022-03-15 18:51:28 +08:00
										 |  |  |                         (round((CouponMain::min_redpack_money / ($data['deduction_money'])) * 100, 2)) | 
					
						
							|  |  |  |                         . "%"); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-03-28 20:17:16 +08:00
										 |  |  |             //}
 | 
					
						
							| 
									
										
										
										
											2022-03-15 18:51:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-16 10:13:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-28 20:17:16 +08:00
										 |  |  |             //if($data['commission_consumer'] < $data['commission_agency']){
 | 
					
						
							| 
									
										
										
										
											2022-03-15 18:51:28 +08:00
										 |  |  |                 //检测 【消费者】部分是否最少0.3元
 | 
					
						
							| 
									
										
										
										
											2022-03-16 10:49:59 +08:00
										 |  |  |                 $consumerMoney = ($data['commission_consumer']/100) * $couponMain['deduction_money']; | 
					
						
							| 
									
										
										
										
											2022-03-15 18:51:28 +08:00
										 |  |  |                 if ($consumerMoney != 0 && $consumerMoney < CouponMain::min_redpack_money) { | 
					
						
							|  |  |  |                     return $this->json(4002, | 
					
						
							|  |  |  |                         "关闭分销:【消费者】红包不足" | 
					
						
							|  |  |  |                         .CouponMain::min_redpack_money | 
					
						
							| 
									
										
										
										
											2022-03-18 11:37:30 +08:00
										 |  |  |                         ."元,【消费者】占比最低" . | 
					
						
							| 
									
										
										
										
											2022-03-16 09:35:10 +08:00
										 |  |  |                         (round((CouponMain::min_redpack_money / ($couponMain['deduction_money'])) * 100, 2)) | 
					
						
							| 
									
										
										
										
											2022-03-15 18:51:28 +08:00
										 |  |  |                         . "%"); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-03-28 20:17:16 +08:00
										 |  |  |             //}else{
 | 
					
						
							| 
									
										
										
										
											2022-03-15 18:51:28 +08:00
										 |  |  |                 //检测 【渠道商】部分是否最少0.3元
 | 
					
						
							| 
									
										
										
										
											2022-03-16 10:49:59 +08:00
										 |  |  |                 $agencyMoney = ($data['commission_agency']/100) * $couponMain['deduction_money']; | 
					
						
							| 
									
										
										
										
											2022-03-15 18:51:28 +08:00
										 |  |  |                 if ($agencyMoney != 0 && $agencyMoney < CouponMain::min_redpack_money) { | 
					
						
							|  |  |  |                     return $this->json(4002, | 
					
						
							|  |  |  |                         "关闭分销:【渠道商】红包不足" | 
					
						
							|  |  |  |                         .CouponMain::min_redpack_money | 
					
						
							| 
									
										
										
										
											2022-03-18 11:37:30 +08:00
										 |  |  |                         ."元,【渠道商】占比最低" . | 
					
						
							| 
									
										
										
										
											2022-03-16 09:35:10 +08:00
										 |  |  |                         (round((CouponMain::min_redpack_money / ($couponMain['deduction_money'])) * 100, 2)) | 
					
						
							| 
									
										
										
										
											2022-03-15 14:10:36 +08:00
										 |  |  |                         . "%"); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-03-28 20:17:16 +08:00
										 |  |  |             //}
 | 
					
						
							| 
									
										
										
										
											2022-03-15 14:10:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-16 10:13:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |             $data['update_time'] = date("Y-m-d H:i:s"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             Db::startTrans(); | 
					
						
							|  |  |  |             try { | 
					
						
							|  |  |  |                 $couponMain->save($data); | 
					
						
							| 
									
										
										
										
											2022-01-19 13:40:46 +08:00
										 |  |  |                 //$couponMain->usingRule->save($usingRule);
 | 
					
						
							| 
									
										
										
										
											2021-12-22 13:53:57 +08:00
										 |  |  |                 CouponRepository::getInstance()->getModel()->where("coupon_id",$couponMain->id)->update(["end_time"=>$data["end_time"]." 00:00:00"]); | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |                 Db::commit(); | 
					
						
							|  |  |  |                 return $this->json(); | 
					
						
							|  |  |  |             } catch (RepositoryException $e) { | 
					
						
							|  |  |  |                 Db::rollback(); | 
					
						
							|  |  |  |                 return $this->json(5001, "发布失败" . $e->getMessage()); | 
					
						
							| 
									
										
										
										
											2021-12-02 18:34:44 +08:00
										 |  |  |             } catch (Exception $e) { | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |                 Db::rollback(); | 
					
						
							|  |  |  |                 return $this->json(5002, "发布失败" . $e->getMessage()); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-19 13:40:46 +08:00
										 |  |  |         if (empty($couponMain)) { | 
					
						
							| 
									
										
										
										
											2022-03-31 14:42:56 +08:00
										 |  |  |             return $this->error("签到券不存在"); | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |         $this->data['business'] = BusinessRepository::getInstance()->getBusinessAll(); | 
					
						
							|  |  |  |         $this->data['type'] = CouponRepository::getInstance()->getCouponTypeAll(); | 
					
						
							|  |  |  |         $this->data['item'] = $couponMain; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this->view(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-21 10:32:52 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * 下载小程序二维码 | 
					
						
							|  |  |  |      * */ | 
					
						
							|  |  |  |     public function downloadQrCode() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $id =  input("id/d"); | 
					
						
							|  |  |  |         $couponMain = CouponMain::findById($id); | 
					
						
							|  |  |  |         if(empty($couponMain)){ | 
					
						
							| 
									
										
										
										
											2022-03-31 14:42:56 +08:00
										 |  |  |             return $this->json(4001,"签到券不存在"); | 
					
						
							| 
									
										
										
										
											2022-01-21 10:32:52 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $qrData = $this->request->domain() . "/coupon_info?id={$id}&business_code={$couponMain['business_code']}" ; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $w      = 3000;//尺寸
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $logoImg    = app()->getRootPath().'public/static/images/icon-logo.jpg'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $result = Builder::create() | 
					
						
							|  |  |  |             ->writer(new PngWriter()) | 
					
						
							|  |  |  |             ->writerOptions([]) | 
					
						
							|  |  |  |             ->data($qrData) | 
					
						
							|  |  |  |             ->encoding(new Encoding('UTF-8')) | 
					
						
							|  |  |  |             ->errorCorrectionLevel(new ErrorCorrectionLevelHigh()) | 
					
						
							|  |  |  |             ->size($w) | 
					
						
							|  |  |  |             ->margin(10) | 
					
						
							|  |  |  |             ->roundBlockSizeMode(new RoundBlockSizeModeMargin()) | 
					
						
							|  |  |  |             ->logoPath($logoImg) | 
					
						
							|  |  |  |             ->logoResizeToHeight(ceil($w/6)) | 
					
						
							|  |  |  |             ->logoResizeToWidth(ceil($w/6)) | 
					
						
							|  |  |  |             ->logoPunchoutBackground(true) | 
					
						
							|  |  |  |             ->build(); | 
					
						
							|  |  |  |         header( | 
					
						
							|  |  |  |             "Content-type: image/jpg" | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |         $path = "/storage/coupon/{$id}.jpg"; | 
					
						
							|  |  |  |         $result->saveToFile( public_path() . $path); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return download(public_path() . $path,$couponMain['name']); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-01-14 18:21:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-15 14:10:36 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      *  设置分销开关 | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * */ | 
					
						
							|  |  |  |     public function setDistribution() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $ids            = input("ids/a", []); | 
					
						
							|  |  |  |         $isDistribution = input("is_distribution/d"); | 
					
						
							| 
									
										
										
										
											2022-03-15 18:51:28 +08:00
										 |  |  |         $couponMains = CouponMain::findList([["id", "in", $ids]], [],1,0,function ($q){ | 
					
						
							|  |  |  |             return $q->with("business"); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2022-03-15 14:10:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-15 18:51:28 +08:00
										 |  |  |             foreach ($couponMains["list"] as $key=> $item) { | 
					
						
							|  |  |  |                 if (!isset($item->business) || empty($item->business)) { | 
					
						
							|  |  |  |                     return $this->json(4001,"【{$item["name"]}】所属商家不存在"); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 //如果开启分销操作
 | 
					
						
							|  |  |  |                 if ($isDistribution == CouponMain::COMMON_ON) { | 
					
						
							|  |  |  |                     if($item->business->is_assign == BusinessModel::COMMON_ON){ | 
					
						
							|  |  |  |                         return $this->json(4001, | 
					
						
							|  |  |  |                             "【{$item["name"]}】所属商家【{$item->business->business_name}】已指派渠道商不能开启分销"); | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2022-03-16 10:13:11 +08:00
										 |  |  |                 }else{ | 
					
						
							|  |  |  |                     if($item->business->is_assign == BusinessModel::COMMON_OFF){ | 
					
						
							|  |  |  |                         return $this->json(4001, | 
					
						
							|  |  |  |                             "【{$item["name"]}】所属商家【{$item->business->business_name}】未指派渠道商不能关闭分销"); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if($item['commission_dis_distribution']  < $item['commission_dis_distributed'] ){ | 
					
						
							| 
									
										
										
										
											2022-03-18 11:37:30 +08:00
										 |  |  |                     //检测【领券分销者】部分是否最少0.3元
 | 
					
						
							| 
									
										
										
										
											2022-03-15 14:10:36 +08:00
										 |  |  |                     $distributionMoney = ($item['commission_dis_distribution'] / 100) * $item['deduction_money']; | 
					
						
							|  |  |  |                     if ($distributionMoney != 0 && $distributionMoney < CouponMain::min_redpack_money) { | 
					
						
							|  |  |  |                         return $this->json(4002, | 
					
						
							| 
									
										
										
										
											2022-03-15 18:51:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-18 11:37:30 +08:00
										 |  |  |                             "【{$item['name']}】开启分销后:【领券分销者】红包不足" | 
					
						
							| 
									
										
										
										
											2022-03-15 14:10:36 +08:00
										 |  |  |                             . CouponMain::min_redpack_money | 
					
						
							| 
									
										
										
										
											2022-03-18 11:37:30 +08:00
										 |  |  |                             . "元,【领券分销者】占比最低" . | 
					
						
							| 
									
										
										
										
											2022-03-15 14:10:36 +08:00
										 |  |  |                             (round((CouponMain::min_redpack_money / ($item['deduction_money'])) * 100, 2)) | 
					
						
							|  |  |  |                             . "%" | 
					
						
							|  |  |  |                         ); | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2022-03-16 10:13:11 +08:00
										 |  |  |                 }else{ | 
					
						
							| 
									
										
										
										
											2022-03-18 11:37:30 +08:00
										 |  |  |                     //检测【核销分销者】部分是否最少0.3元
 | 
					
						
							| 
									
										
										
										
											2022-03-15 14:10:36 +08:00
										 |  |  |                     $distributedMoney = ($item['commission_dis_distributed'] / 100) * $item['deduction_money']; | 
					
						
							|  |  |  |                     if ($distributedMoney != 0 && $distributedMoney < CouponMain::min_redpack_money) { | 
					
						
							|  |  |  |                         return $this->json(4002, | 
					
						
							| 
									
										
										
										
											2022-03-18 11:37:30 +08:00
										 |  |  |                             "【{$item['name']}】开启分销后:【核销分销者】红包不足" | 
					
						
							| 
									
										
										
										
											2022-03-15 14:10:36 +08:00
										 |  |  |                             . CouponMain::min_redpack_money | 
					
						
							| 
									
										
										
										
											2022-03-18 11:37:30 +08:00
										 |  |  |                             . ",【核销分销者】占比最低" . | 
					
						
							| 
									
										
										
										
											2022-03-15 14:10:36 +08:00
										 |  |  |                             (round((CouponMain::min_redpack_money / ($item['deduction_money'])) * 100, 2)) | 
					
						
							|  |  |  |                             . "%"); | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2022-03-16 10:13:11 +08:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-03-15 14:10:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-16 10:13:11 +08:00
										 |  |  |                 if($item['commission_agency'] < $item['commission_consumer']){ | 
					
						
							| 
									
										
										
										
											2022-03-15 18:51:28 +08:00
										 |  |  |                     //检测【渠道商】部分是否最少0.3元
 | 
					
						
							|  |  |  |                     $agencyMoney = ($item['commission_agency'] / 100) * $item['deduction_money']; | 
					
						
							|  |  |  |                     if ($agencyMoney != 0 && $agencyMoney < CouponMain::min_redpack_money) { | 
					
						
							|  |  |  |                         return $this->json(4002, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                             "【{$item['name']}】开启分销后:【渠道商】红包不足" | 
					
						
							|  |  |  |                             . CouponMain::min_redpack_money | 
					
						
							| 
									
										
										
										
											2022-03-18 11:37:30 +08:00
										 |  |  |                             . "元,【渠道商】占比最低" . | 
					
						
							| 
									
										
										
										
											2022-03-15 18:51:28 +08:00
										 |  |  |                             (round((CouponMain::min_redpack_money / ($item['deduction_money'])) * 100, 2)) | 
					
						
							|  |  |  |                             . "%" | 
					
						
							|  |  |  |                         ); | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2022-03-16 10:13:11 +08:00
										 |  |  |                 }else{ | 
					
						
							| 
									
										
										
										
											2022-03-15 18:51:28 +08:00
										 |  |  |                     //检测【消费者】部分是否最少0.3元
 | 
					
						
							|  |  |  |                     $consumerMoney = ($item['commission_consumer'] / 100) * $item['deduction_money']; | 
					
						
							|  |  |  |                     if ($consumerMoney != 0 && $consumerMoney < CouponMain::min_redpack_money) { | 
					
						
							|  |  |  |                         return $this->json(4002, | 
					
						
							|  |  |  |                             "【{$item['name']}】开启分销后:【消费者】红包不足" | 
					
						
							|  |  |  |                             . CouponMain::min_redpack_money | 
					
						
							| 
									
										
										
										
											2022-03-18 11:37:30 +08:00
										 |  |  |                             . ",【消费者】占比最低" . | 
					
						
							| 
									
										
										
										
											2022-03-15 18:51:28 +08:00
										 |  |  |                             (round((CouponMain::min_redpack_money / ($item['deduction_money'])) * 100, 2)) | 
					
						
							|  |  |  |                             . "%"); | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2022-03-15 14:10:36 +08:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-03-15 18:51:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-15 14:10:36 +08:00
										 |  |  |         Db::startTrans(); | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             CouponMain::whereIn("id",$ids)->update(["is_distribution"=>$isDistribution]); | 
					
						
							|  |  |  |             Db::commit(); | 
					
						
							|  |  |  |             return $this->json(); | 
					
						
							|  |  |  |         } catch (Exception $e) { | 
					
						
							|  |  |  |             Db::rollback(); | 
					
						
							|  |  |  |             return $this->json(5001, "设置失败".$e->getMessage()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-01-14 18:21:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  | } |