| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- <?php
- namespace addons\Store\common\models;
- use addons\Coupon\common\models\AddonsCouponGoodStatistics;
- use addons\Coupon\common\models\AddonsCouponStatistics;
- use common\enums\AddonsEnum;
- use common\enums\StatusEnum;
- use common\enums\UserWalletEnum;
- use common\models\base\BaseActiveRecord;
- use common\models\mall\MallAddons;
- use common\models\order\Order;
- use common\models\order\OrderDetail;
- use services\common\UserWalletService;
- use Yii;
- /**
- * This is the model class for table "qimall_addons_store_order_marketing".
- *
- * @property int $id 主键
- * @property int $mall_id mall_id
- * @property int $store_id store_id
- * @property int $order_detail_id 订单详情id
- * @property float $give_score 赠送积分
- * @property int $give_coupon_id 赠送优惠券id
- * @property int $give_coupon_from_type 1:平台优化券;2:门店优惠券
- * @property float $give_balance 赠送余额
- * @property string|null $give_currency 赠送货币(json字符串保存)
- * @property float $deduct_score 减扣积分
- * @property string|null $deduct_currency 减扣货币(json字符串保存)
- * @property float $pay_money 商品实际付款金额,单位:元
- * @property float $full_reduce_money 满减金额,单位:元
- * @property float $coupon_discount_money 优惠券优惠金额,单位:元
- * @property float $member_price_deduct 会员价优惠
- * @property string $user_coupon_id 用户优惠券id map
- * @property float $agent_deduct
- * @property string $agent_extra 经销商内购商品抵扣额外信息
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- */
- class StoreOrderMarketing extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_store_order_marketing}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id','store_id', 'order_detail_id','status', 'created_at', 'updated_at'], 'integer'],
- [['give_score', 'give_balance', 'deduct_score', 'pay_money', 'full_reduce_money', 'coupon_discount_money', 'member_price_deduct', 'agent_deduct'], 'number'],
- [['give_currency', 'deduct_currency'], 'string'],
- [['user_coupon_id','user_card_id'], 'string', 'max' => 1000],
- [['agent_extra'], 'string', 'max' => 255],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => '主键',
- 'mall_id' => 'mall_id',
- 'store_id' => 'store_id',
- 'order_detail_id' => '订单详情id',
- 'give_score' => '赠送积分',
- 'give_balance' => '赠送余额',
- 'give_currency' => '赠送货币(json字符串保存)',
- 'deduct_score' => '减扣积分',
- 'deduct_currency' => '减扣货币(json字符串保存)',
- 'pay_money' => '商品实际付款金额,单位:元',
- 'full_reduce_money' => '满减金额,单位:元',
- 'coupon_discount_money' => '优惠券优惠金额,单位:元',
- 'member_price_deduct' => '会员价优惠',
- 'user_coupon_id' => '用户优惠券id map',
- 'user_card_id' => '用户会员卡id map',
- 'agent_deduct' => 'Agent Deduct',
- 'agent_extra' => '经销商内购商品抵扣额外信息',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => '创建时间',
- 'updated_at' => '修改时间',
- ];
- }
- public static function setData($deduct_data, $order_detail_id, $store_id, $mall_id)
- {
- $deduct_data['user_coupon_id'] = !empty($deduct_data['user_coupon_id'])? json_encode($deduct_data['user_coupon_id']):json_encode([]);
- $order_marketing = new StoreOrderMarketing();
- $order_marketing->loadDefaultValues();
- $order_marketing->load($deduct_data,'');
- $order_marketing->order_detail_id = $order_detail_id;
- $order_marketing->store_id = $store_id;
- $order_marketing->mall_id = $mall_id;
- $order_marketing->save();
- return $order_marketing;
- }
- public static function giveMarketing($mallId, $user_id, $source_table_id)
- {
- try {
- // 查询订单详情
- $detail_ids = StoreOrderDetail::find()->select('id')->where(['order_id' => $source_table_id])->asArray()->column();
- if (empty($detail_ids)) throw new \Exception('订单详情id查询为空');
- // 获取订单的营销数据
- $order_marketing_list = self::find()->andWhere(['in', 'order_detail_id', $detail_ids])->all();
- if (empty($order_marketing_list)) return true;
- $give_type = [UserWalletService::WALLET_TYPE_SCORE];
- $give_money = [];
- // 循环计算赠送数量
- foreach ($order_marketing_list as $marketing) {
- if (!isset($give_money[UserWalletService::WALLET_TYPE_SCORE])) $give_money[UserWalletService::WALLET_TYPE_SCORE] = 0;
- $give_money[UserWalletService::WALLET_TYPE_SCORE] += $marketing->give_score;
- }
- // 插入资金
- foreach ($give_type as $type) {
- if ($give_money[$type] <= 0) continue;
- $insert_wallet[] = [
- 'mall_id' => $mallId,
- 'user_id' => $user_id,
- 'is_frozen' => false,
- 'wallet_type' => $type,
- 'money' => $give_money[$type],
- 'desc' => 'o2o订单支付赠送',
- 'source_table' => StoreOrder::tableName(),
- 'source_table_id' => $source_table_id,
- 'from_type' => UserWalletEnum::CONSUME,
- 'capital_type' => UserWalletEnum::CONSUME_ORDER_GIVE,
- ];
- $res = UserWalletService::batchHandleByQueue($insert_wallet);
- if (empty($res)) throw new \Exception(UserWalletService::getStaticError());
- }
- return true;
- } catch (\Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- public static function deductBack($store_order,$order_detail_id_all, $order_detail_ids, $mall_id, $user_id, $desc, $from_type = UserWalletEnum::CONSUME, $capital_type = UserWalletEnum::CONSUME_ORDER_CLOSE)
- {
- try {
- $order_id = $store_order['id'];
- $order_marketing_list = StoreOrderMarketing::lists([
- 'where' => [['order_detail_id' => $order_detail_ids], ['status' => StatusEnum::ENABLED]],
- 'select' => 'deduct_score,user_coupon_id,order_detail_id'
- ]);
- $refund_user_coupon_id_arr = [];
- $user_coupon_arr = $order_detail_arr = $user_coupon_order_detail_id_arr = [];
- $order_detail_list = StoreOrderDetail::lists(['where'=>[['order_id'=>$order_id]]]);
- foreach($order_detail_list as $order_detail) {
- $order_detail_arr[$order_detail['id']] = ['goods_id' => $order_detail['goods_id'], 'num' => $order_detail['num']];
- }
- $deduct_score = 0;
- if ($from_type == UserWalletEnum::REFUND) {
- $capital_type = 'consume_store_order_refund';
- //退款
- $order_detail_id_diff = array_diff($order_detail_id_all, $order_detail_ids);
- if (empty($order_detail_id_diff)) $order_detail_id_diff = $order_detail_id_all;
- $order_marketing_list_diff = StoreOrderMarketing::lists([
- 'where' => [['order_detail_id' => $order_detail_id_diff], ['status' => StatusEnum::ENABLED]],
- 'select' => 'user_coupon_id'
- ]);
- $user_coupon_id_arr_diff = [];
- foreach ($order_marketing_list_diff as $value) {
- $user_coupon_id = json_decode($value['user_coupon_id'], true);
- if (!empty($user_coupon_id)) $user_coupon_id_arr_diff = array_merge($user_coupon_id_arr_diff, array_keys($user_coupon_id));
- }
- foreach ($order_marketing_list as $item) {
- $deduct_score+=$item['deduct_score'];
- $user_coupon_id_arr = json_decode($item['user_coupon_id'], true);
- if (!empty($user_coupon_id_arr)) {
- foreach ($user_coupon_id_arr as $user_coupon_id => $money_) {
- if (in_array($user_coupon_id, $user_coupon_id_arr_diff)){
- $refund_user_coupon_id_arr[] = $user_coupon_id;
- if(isset($user_coupon_arr[$user_coupon_id])){
- $user_coupon_arr[$user_coupon_id] += $money_;
- }else{
- $user_coupon_arr[$user_coupon_id] = $money_;
- }
- }
- $user_coupon_order_detail_id_arr[$user_coupon_id][] = $item['order_detail_id'];
- }
- }
- }
- } else {
- //订单关闭
- $deduct_score = 0;
- foreach ($order_marketing_list as $item) {
- $deduct_score+=$item['deduct_score'];
- $user_coupon_id = json_decode($item['user_coupon_id'], true);
- if (!empty($user_coupon_id)) $refund_user_coupon_id_arr = array_merge($refund_user_coupon_id_arr, array_keys($user_coupon_id));
- foreach ($user_coupon_id as $key=>$value){
- if(isset($user_coupon_arr[$key])){
- $user_coupon_arr[$key] += $value;
- }else{
- $user_coupon_arr[$key] = $value;
- }
- $user_coupon_order_detail_id_arr[$key][] = $item['order_detail_id'];
- }
- }
- $capital_type = 'consume_store_order_close';
- }
- if($deduct_score>0){
- $insert_wallet[] = [
- 'mall_id' => $mall_id,
- 'user_id' => $user_id,
- 'is_frozen' => false,
- 'wallet_type' => 'score',
- 'money' => $deduct_score,
- 'desc' => $desc,
- 'source_table' => StoreOrder::tableName(),
- 'source_table_id' => $order_id,
- 'from_type' => UserWalletEnum::CONSUME,
- 'capital_type' =>$capital_type,
- ];
- $res = UserWalletService::batchHandleByQueue($insert_wallet);
- if (empty($res)) throw new \Exception(UserWalletService::getStaticError());
- }
- if (!empty($refund_user_coupon_id_arr)) {
- StoreCouponUserRelate::updateAll(['is_use' => 0], ['id' => $refund_user_coupon_id_arr]);
- StoreCouponUseLog::updateAll(['status' => StatusEnum::DELETE], ['order_id' => $order_id, 'user_coupon_id' => $refund_user_coupon_id_arr]);
- $coupon_user_relation_list = StoreCouponUserRelate::lists(['where'=>[['id'=>$refund_user_coupon_id_arr]]]);
- foreach ($coupon_user_relation_list as $coupon_user_relation){
- //优惠券-商品统计
- $set_goods_data = [
- 'mall_id'=>$mall_id,
- 'coupon_id'=>$coupon_user_relation['coupon_id'],
- 'payed_number'=>0,
- 'buyed_user_number'=>-1,
- ];
- if(!empty($user_coupon_order_detail_id_arr[$coupon_user_relation['id']])){
- $order_detail_ids = $user_coupon_order_detail_id_arr[$coupon_user_relation['id']];
- foreach ($order_detail_ids as $order_detail_id){
- if(!empty($order_detail_arr[$order_detail_id])){
- $set_goods_data['good_id'] = !empty($order_detail_arr[$order_detail_id]['goods_id'])?$order_detail_arr[$order_detail_id]['goods_id']:0;
- $set_goods_data['payed_number'] = !empty($order_detail_arr[$order_detail_id]['num'])?-$order_detail_arr[$order_detail_id]['num']:0;
- if($coupon_user_relation['from_type']==1){
- if(MallAddons::isInstall($coupon_user_relation['mall_id'],AddonsEnum::ADDONS_NAME_COUPON)){
- AddonsCouponGoodStatistics::setData($set_goods_data);
- }
- }else{
- StoreCouponGoodStatistics::setData($set_goods_data);
- }
- }
- }
- }
- //优惠券-统计
- $set_data = [
- 'mall_id'=>$mall_id,
- 'coupon_id'=>$coupon_user_relation['coupon_id'],
- 'used_number'=>-1,
- 'use_user_number'=>-1,
- 'use_order_number'=>-1,
- 'total_payed_amount'=>-($store_order['original_goods_price']+$store_order['shipping_money']),
- 'total_use_coupon_payed_amount'=>-($store_order['goods_price']+$store_order['shipping_money']),
- 'total_discount_amount'=>!empty($user_coupon_arr[$coupon_user_relation['id']])?-$user_coupon_arr[$coupon_user_relation['id']]:0,
- ];
- if($coupon_user_relation['from_type']==1){
- if(MallAddons::isInstall($coupon_user_relation['mall_id'],AddonsEnum::ADDONS_NAME_COUPON)){
- AddonsCouponStatistics::setData($set_data);
- }
- }else{
- $set_data['store_id'] = $coupon_user_relation['store_id'];
- StoreCouponStatistics::setData($set_data);
- }
- }
- }
- return true;
- } catch (\Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- }
|