ElectronCoupon.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. namespace addons\ElectronCoupon\common\models;
  3. use addons\ElectronCoupon\common\enums\ElectronCouponEnums;
  4. use common\enums\StatusEnum;
  5. use common\models\mall\MallAddons;
  6. use Yii;
  7. use yii\db\Exception;
  8. /**
  9. * This is the model class for table "{{%addons_electron_coupon}}".
  10. *
  11. * @property int $id 自增id
  12. * @property int|null $mall_id 商城id
  13. * @property string $name 电子券名称
  14. * @property int $amount 发行数量;单张券为张
  15. * @property int $receive_times 最多领取次数
  16. * @property int $unlimit_receive_times 领取次数不限制
  17. * @property int $pieces_type 1单张2多张;
  18. * @property int $pieces_amount 多张数量;单位张
  19. * @property int $receive_codition 领取条件;1全体会员;2指定会员等级;
  20. * @property int|null $receive_member_level 指定会员等级;默认为null
  21. * @property int|null $receive_begin_time 领取开始时间
  22. * @property int|null $receive_end_time 领取结束时间
  23. * @property int|null $use_begin_time 使用开始时间
  24. * @property int|null $use_end_time 使用结束时间
  25. * @property float $price 激活金额
  26. * @property float $reward_money 核销奖励金额
  27. * @property int $use_way 适用范围;1扫描核销
  28. * @property int $is_can_receive 是否可以被领取
  29. * @property int $is_show_in_center 是否出现在领取中心
  30. * @property string|null $rules 电子券规则
  31. * @property string|null $pic 缩略图
  32. * @property int|null $no_receive_amount 未领取数量
  33. * @property int $receive_amount 领取数量;单张券为张;
  34. * @property int $used_amount 兑换数量;单位张或多张
  35. * @property int $created_at 创建时间
  36. * @property int $updated_at 更新时间
  37. * @property int $sort 排序;由大到小
  38. * @property int $is_give 是否赠送电子券
  39. * @property int $is_proper 是否专有券
  40. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  41. * @property float $give_score 赠送积分
  42. * @property float $give_price 赠送佣金
  43. * @property float $give_store_price 核销门店奖励
  44. * @property int $share_expiry 分享有效期,单位小时
  45. * @property int $give_quantity 赠送数量
  46. */
  47. class ElectronCoupon extends \common\models\base\BaseActiveRecord
  48. {
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public static function tableName()
  53. {
  54. return '{{%addons_electron_coupon}}';
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function rules()
  60. {
  61. return [
  62. [['id', 'mall_id', 'amount', 'receive_times', 'unlimit_receive_times', 'pieces_type', 'pieces_amount', 'receive_codition',
  63. 'receive_member_level', 'receive_begin_time', 'receive_end_time', 'use_begin_time', 'use_end_time', 'use_way',
  64. 'is_can_receive', 'is_show_in_center', 'receive_amount', 'used_amount', 'created_at', 'updated_at', 'sort',
  65. 'is_give', 'is_proper', 'status', 'share_expiry', 'give_quantity'], 'integer'],
  66. [['name', 'amount'], 'required'],
  67. [['price', 'give_score', 'give_price', 'reward_money', 'give_store_price'], 'number'],
  68. [['rules'], 'string'],
  69. [['name', 'pic'], 'string', 'max' => 255],
  70. ];
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function attributeLabels()
  76. {
  77. return [
  78. 'id' => '自增id',
  79. 'mall_id' => '商城id',
  80. 'name' => '电子券名称',
  81. 'amount' => '发行数量;单张券为张',
  82. 'receive_times' => '最多领取次数',
  83. 'unlimit_receive_times' => '领取次数不限制',
  84. 'pieces_type' => '1单张2多张;',
  85. 'pieces_amount' => '多张数量;单位张',
  86. 'receive_codition' => '领取条件;1全体会员;2指定会员等级;',
  87. 'receive_member_level' => '指定会员等级;默认为null',
  88. 'receive_begin_time' => '领取开始时间',
  89. 'receive_end_time' => '领取结束时间',
  90. 'use_begin_time' => '使用开始时间',
  91. 'use_end_time' => '使用结束时间',
  92. 'price' => '激活金额',
  93. 'reward_money' => '奖励金额',
  94. 'use_way' => '适用范围;1扫描核销',
  95. 'is_can_receive' => '是否可以被领取',
  96. 'is_show_in_center' => '是否出现在领取中心',
  97. 'rules' => '电子券规则',
  98. 'pic' => '缩略图',
  99. 'no_receive_amount' => '未领取数量',
  100. 'receive_amount' => '领取数量;单张券为张;',
  101. 'used_amount' => '兑换数量;单位张或多张',
  102. 'created_at' => '创建时间',
  103. 'updated_at' => '更新时间',
  104. 'sort' => '排序;由大到小',
  105. 'is_give' => '是否赠送电子券',
  106. 'is_proper' => '是否专有券',
  107. 'status' => '状态[-1:删除;0:禁用;1启用]',
  108. 'give_score' => '赠送积分',
  109. 'give_price' => '赠送佣金',
  110. 'give_store_price' => '核销门店奖励',
  111. 'share_expiry' => '分享有效期,单位小时',
  112. 'give_quantity' => '赠送数量',
  113. ];
  114. }
  115. /**
  116. * @Author: ltm
  117. * @DateTime: 2022/6/27 0012 17:30
  118. * @Copyright: copyright (c) 2021 广东七件事集团
  119. * @param $data
  120. * @return string
  121. */
  122. public static function getGoodSetting($params)
  123. {
  124. // 后台页面组件展示
  125. if ($params['type'] == 'show_component') {
  126. return [
  127. 'component' => 'com-electron-coupon',
  128. 'path' => Yii::$app->basePath . '/../addons/ElectronCoupon/backend/views/goods',
  129. ];
  130. } else {
  131. try {
  132. if (empty($params['setting'][ElectronCouponEnums::ADDONS_NAME])) return true;
  133. $setting = $params['setting'][ElectronCouponEnums::ADDONS_NAME];
  134. // 数据保存
  135. //var_dump($setting['electron_selection']);die;
  136. //统一设置无效
  137. ElectronCouponGoods::setCanceGoods($params);
  138. if ($setting['electron_selection']) {
  139. $data['is_open'] = $setting['is_open'];
  140. $data['send_type'] = $setting['send_type'];
  141. foreach ($setting['electron_selection'] as $k => $v) {
  142. $data['num'] = $v['num'];
  143. $data['status'] = StatusEnum::ENABLED;
  144. $data['e_coupon_id'] = $v['coupon_id'];
  145. $res = ElectronCouponGoods::setGoodsData($params['mall_id'], $params['goods_id'], $data);
  146. if ($res === false) throw new Exception(ElectronCouponGoods::getStaticError());
  147. }
  148. }
  149. return true;
  150. } catch (Exception $e) {
  151. self::setStaticError($e->getMessage());
  152. return false;
  153. }
  154. }
  155. }
  156. /**
  157. * @Author: ltm
  158. * @DateTime: 2022/6/27 0012 12:45
  159. * @Copyright: copyright (c) 2021 广东七件事集团
  160. * @param $data
  161. * @return string
  162. */
  163. public static function isInstall(array $params)
  164. {
  165. //插件判断 是否显示卡密信息
  166. $addons = MallAddons::isInstall($params['mall_id'], ElectronCouponEnums::ADDONS_NAME);
  167. $data = $params['order'];
  168. if ($addons) {
  169. $data[ElectronCouponEnums::ADDONS_NAME]['is_show_addons'] = 1;
  170. } else {
  171. $data[ElectronCouponEnums::ADDONS_NAME]['is_show_addons'] = 0;
  172. }
  173. return $data;
  174. }
  175. /**
  176. * 数据设置
  177. * @param array $params
  178. * int receive_amount 未激活数量,传入数量进行加减计算
  179. * int used_amount 已使用数量,传入数量进行加减计算
  180. * @return ElectronCoupon|bool|null
  181. */
  182. public static function setData(array $params)
  183. {
  184. try {
  185. $params['receive_amount'] = $params['receive_amount'] ?? 0;
  186. $params['used_amount'] = $params['used_amount'] ?? 0;
  187. $params['status'] = $params['status'] ?? 1;
  188. $params['give_quantity'] = $params['give_quantity'] ?? 0;
  189. if (!isset($params['mall_id']) || empty($params['mall_id'])) throw new Exception('缺少商城ID');
  190. $mall_id = $params['mall_id'];
  191. if (isset($params['id']) && !empty($params['id'])) {
  192. $model = self::findOne(['id' => $params['id'], 'mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  193. if (empty($model)) throw new Exception('电子券不存在或已删除');
  194. $bcmath = ['receive_amount', 'used_amount'];
  195. foreach ($bcmath as $field) {
  196. if (isset($params[$field])) {
  197. $model[$field] = $model[$field] + $params[$field];
  198. unset($params[$field]);
  199. }
  200. }
  201. } else {
  202. $model = new self();
  203. $model->loadDefaultValues();
  204. $model->mall_id = $mall_id;
  205. }
  206. if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  207. return $model;
  208. } catch (Exception $e) {
  209. self::setStaticError($e->getMessage());
  210. return false;
  211. }
  212. }
  213. /**
  214. * 修改是否显示在首页
  215. *
  216. * @param $mall_id
  217. * @param $id
  218. * @param $is_show_in_center
  219. * @return ElectronCoupon|false
  220. */
  221. public static function changeIsShowInCenter($mall_id, $id, $is_show_in_center)
  222. {
  223. try {
  224. $model = self::findOne(['id' => $id, 'mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  225. if (empty($model)) throw new Exception('电子券不存在或已删除');
  226. $model->is_show_in_center = $is_show_in_center;
  227. if (!$model->save()) throw new Exception($model->getErrorMessage());
  228. return $model;
  229. } catch (Exception $e) {
  230. self::setStaticError($e->getMessage());
  231. return false;
  232. }
  233. }
  234. /**
  235. * 修改状态
  236. *
  237. * @param $mall_id
  238. * @param $id
  239. * @param $status
  240. * @return ElectronCoupon|false
  241. */
  242. public static function changeStatus($mall_id, $id, $status)
  243. {
  244. try {
  245. $model = self::findOne(['id' => $id, 'mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  246. if (empty($model)) throw new Exception('电子券不存在或已删除');
  247. $model->status = $status;
  248. if (!$model->save()) throw new Exception($model->getErrorMessage());
  249. return $model;
  250. } catch (Exception $e) {
  251. self::setStaticError($e->getMessage());
  252. return false;
  253. }
  254. }
  255. }