| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929 |
- <?php
- namespace app\modules\admin\models;
- use app\models\Attr;
- use app\models\AttrGroup;
- use app\models\common\CommonGoods;
- use app\models\Goods;
- use app\models\GoodsPic;
- use app\models\Option;
- use app\models\Order;
- use app\models\OrderDetail;
- use app\models\OrderRefund;
- use app\models\SaasUser;
- use app\models\Share;
- use app\models\ShareLevel;
- use app\models\StoreMini;
- use app\models\User;
- use app\models\VideoShopGoodsExt;
- use app\models\VideoShopOrderExt;
- use app\models\VideoShopSharer;
- use app\models\VideoShopUser;
- use app\modules\client\models\v1\ShareMoneyForm;
- use app\utils\OrderNo;
- use EasyWeChat\Kernel\BaseClient;
- use yii\base\Model;
- use yii\helpers\Json;
- use app\models\District;
- class VideoShopOrderForm extends Model
- {
- public $params;
- public $store_id;
- public $mini_id;
- public $miniProgram;
- public $ids;
- public $start_time;
- public $end_time;
- public $sharer_id;
- public $order_no;
- public $status;
- public function rules()
- {
- return [
- [['ids', 'start_time', 'end_time', 'order_no'], 'string'],
- [['sharer_id', 'status', 'mini_id'], 'integer'],
- [['params'], 'safe']
- ];
- }
- // 获取商品id列表
- public function syncOrderList($next_key = '', $page_size = 10) {
- try {
- if (!$this->miniProgram) {
- throw new \Exception('请检查小店状态');
- }
- $mini_id = $this->mini_id;
- //获取两天内
- //如果没有两天就不执行操作
- //..
- // if (cache()->get('sync_wechat_sharer_order' . $mini_id)) {
- // return false;
- // }
- $end_time = $this->end_time;//60 * 60 * 24 * 2; //两天
- // cache()->set('sync_wechat_sharer_order' . $mini_id, true, $diff_time);
- $start_time = $this->start_time;//time() - $diff_time;
- $data = [
- 'next_key' => $next_key,
- 'page_size' => $page_size,
- 'create_time_range' => [
- 'start_time' => $start_time,
- 'end_time' => $end_time
- ]
- ];
- $client = new BaseClient($this->miniProgram);
- $res = $client->httpPostJson('channels/ec/order/list/get', $data);
- if (!$res['errcode'] && !empty($res)) {
- //根据商品id同步商品
- // ....
- if ($res['order_id_list']) {
- $result = $this->syncOrderInfo($res['order_id_list']);
- //下一页
- if (count($res['order_id_list']) >= $page_size) {
- $this->syncOrderList($res['next_key']);
- }
- }
- } else {
- throw new \Exception($res['errmsg'], $res['errcode']);
- }
- return [
- 'code' => 0,
- 'msg' => '同步成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => $e->getCode() ?: 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function _orderInfo($store_id, $mini_id, $order_id, $cache_order_info = null, $refresh = 0) {
- $cache_order_info = $cache_order_info ? json_decode($cache_order_info, true) : null;
- if(!$refresh && $cache_order_info){
- if(($cache_order_info['update_time'] < time() - 86400 * 30) || $cache_order_info['status'] >= 100 || ($cache_order_info['aftersale_detail']['aftersale_order_list'] && !$cache_order_info['aftersale_detail']['on_aftersale_order_cnt'])){
- return $cache_order_info;
- }
- }
- $orderInfo = null;
- $data = [
- 'order_id' => $order_id
- ];
- $miniProgram = \app\utils\Wechat\WechatMini::getWechatConfig($store_id, $mini_id, 1);
- $client = new BaseClient($miniProgram);
- $res = $client->httpPostJson('channels/ec/order/get', $data);
- if (!$res['errcode'] && !empty($res)) {
- $orderInfo = $res['order'];
- VideoShopOrderExt::updateAll(['order_info' => json_encode($orderInfo, JSON_UNESCAPED_UNICODE)], ['out_order_id' => $order_id]);
- }
- return $orderInfo;
- }
- //根据订单id同步订单
- public function syncOrderInfo($order_id_list = []) {
- if (!$order_id_list) {
- throw new \Exception('缺少order_id');
- }
- $store_id = $this->store_id;
- $mini_id = $this->mini_id;
- foreach ($order_id_list as $item) {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $data = [
- 'order_id' => $item
- ];
- $client = new BaseClient($this->miniProgram);
- $res = $client->httpPostJson('channels/ec/order/get', $data);
- if (!$res['errcode'] && !empty($res)) {
- $orderInfo = $res['order'];
- $video_shop_order = VideoShopOrderExt::findOne(['store_id' => $store_id, 'out_order_id' => $item]);
- //增加用户
- //根据地址中的手机号判断是否存在用户
- $address_info = [];
- if ($orderInfo['order_detail']['delivery_info']['address_info']) {
- $address_info = $orderInfo['order_detail']['delivery_info']['address_info'];
- }
- //关联系统用户
- $tel_number = $address_info['tel_number'];
- $nick_name = substr_replace($tel_number, '******', 3, 6);
- $unionid = $orderInfo['unionid'];
- if(!$unionid){
- debug_log(['缺少unionid', $store_id, $item, $unionid], __CLASS__);
- // throw new \Exception('缺少unionid');
- }
- $open_id = $orderInfo['openid'];
-
- $shop_user = VideoShopUser::findOne(['unionid' => $unionid]);
- $saas_id = 0;
- if ($shop_user) {
- // $saas_id = $shop_user->saas_id;
- }
- $user_id = 0;
- if($unionid){
- $user = User::findOne(['store_id' => $store_id, 'wechat_union_id' => $unionid]);
- debug_log(['$user', $store_id, $item, $unionid, $user['id']], __CLASS__);
- if($user){
- $user_id = $user->id;
- $saasUser = SaasUser::findOne(['mobile' => $user->binding]);
- $saas_id = (int)$saasUser['id'];
- }
- }
- if(!$saas_id){
- debug_log(['非商城用户,unionid', $store_id, $item, $unionid], __CLASS__);
- }
- $saas_user = SaasUser::findOne(['id' => $saas_id, 'is_delete' => 0]);
- if (!$saas_user) {
- // $saas_user = SaasUser::findOne(['mobile' => $tel_number, 'is_delete' => 0]);
- // if (!$saas_user) {
- // $saas_user = new SaasUser();
- // $saas_user->access_token = \Yii::$app->security->generateRandomString();
- // $saas_user->avatar = '';
- // $saas_user->mobile = $tel_number ?: '';
- // $saas_user->name = $nick_name;
- // $saas_user->store_id = $store_id;
- // if (!$saas_user->save()) {
- // throw new \Exception(json_encode($saas_user->errors, JSON_UNESCAPED_UNICODE));
- // }
- // }
- }
- if (!$shop_user) {
- $shop_user = new VideoShopUser();
- $shop_user->unionid = $unionid;
- $shop_user->open_id = $open_id;
- $shop_user->store_id = $store_id;
- $shop_user->mini_id = $mini_id;
- $shop_user->saas_id = (int)$saas_id;
- if (!$shop_user->save()) {
- throw new \Exception(json_encode($shop_user->errors, JSON_UNESCAPED_UNICODE));
- }
- }
- // $user = User::findOne(['binding' => $saas_user->mobile, 'is_delete' => 0, 'store_id' => $store_id]);
- if (!$user) {
- // $user = new User();
- // $user->access_token = \Yii::$app->security->generateRandomString();
- // $user->binding = $saas_user->mobile;
- // $user->type = User::USER_TYPE_NORMAL;
- // $user->nickname = substr_replace($saas_user->mobile, '******', 3, 6);
- // $user->avatar_url = '';
- // $user->username = \Yii::$app->security->generateRandomString();
- // $user->password = \Yii::$app->security->generatePasswordHash(\Yii::$app->security->generateRandomString(), 5);
- // $user->auth_key = \Yii::$app->security->generateRandomString();
- // $user->is_delete = User::USER_NOT_DELETE;
- // $user->store_id = $store_id;
- // $user->platform = User::USER_FROM_WECHAT; // 微信
- // $user->wechat_open_id = $open_id;
- // if (!$user->save()) {
- // throw new \Exception(json_encode($user->errors, JSON_UNESCAPED_UNICODE));
- // }
- }
- $order = null;
- if ($video_shop_order && $video_shop_order->order_id) {
- $order = Order::findOne($video_shop_order->order_id);
- debug_log([$video_shop_order->order_id, $order->id, $order->is_sale], __CLASS__);
- if (intval($order->is_sale) === 1) {
- continue;
- }
- }
- if (!$order) {
- $order = new Order();
- $order->order_no = OrderNo::getOrderNo(OrderNo::ORDER_MALL);
- $order->profit = self::getOrderProfit($store_id, $orderInfo['order_detail']['product_infos']);
- }
- self::setOrderAddr($order, $orderInfo['order_detail']['delivery_info']['address_info']);
- $order->created_at = $orderInfo['create_time'];
- $order->is_pay = Order::IS_PAY_TRUE;
- $order->user_id = $user_id;
- $order->store_id = $store_id;
- $order->first_price = 0;
- $order->second_price = 0;
- $order->third_price = 0;
- //支付信息
- if ($orderInfo['order_detail']['pay_info']) {
- //支付时间
- if ($orderInfo['order_detail']['pay_info']['pay_time']) {
- $order->pay_time = $orderInfo['order_detail']['pay_info']['pay_time'];
- }
- //支付方式 微信支付
- if ($orderInfo['order_detail']['pay_info']['payment_method']) {
- $order->pay_type = Order::PAY_TYPE_WECHAT;
- }
- //微信支付id
- if ($orderInfo['order_detail']['pay_info']['transaction_id']) {
- $order->transaction_id = $orderInfo['order_detail']['pay_info']['transaction_id'];
- }
- }
- //订单金额信息
- if ($orderInfo['order_detail']['price_info']) {
- //订单金额
- if ($orderInfo['order_detail']['price_info']['order_price']) {
- $order->pay_price = $order->total_price = ($orderInfo['order_detail']['price_info']['order_price'] * 0.01);
- }
- //随机立减
- if ($orderInfo['order_detail']['price_info']['discounted_price']) {
- $order->rand_discount = ($orderInfo['order_detail']['price_info']['discounted_price'] * 0.01);
- }
- //运费
- if ($orderInfo['order_detail']['price_info']['freight']) {
- $order->express_price = ($orderInfo['order_detail']['price_info']['freight'] * 0.01);
- }
- //改价后的运费
- if ($orderInfo['order_detail']['price_info']['change_freight']) {
- $order->express_price = ($orderInfo['order_detail']['price_info']['change_freight'] * 0.01);
- }
- }
- // 发货信息
- if ($orderInfo['order_detail']['delivery_info']['delivery_product_info']) {
- $delivery_product_info = $orderInfo['order_detail']['delivery_info']['delivery_product_info'];
- //快递单号
- if ($delivery_product_info['waybill_id']) {
- $order->express_no = $delivery_product_info['waybill_id'];
- }
- //快递公司名称
- if ($delivery_product_info['delivery_name']) {
- $order->express = $delivery_product_info['delivery_name'];
- }
- //发货时间
- if ($delivery_product_info['delivery_time']) {
- $order->send_time = $delivery_product_info['delivery_time'];
- }
- }
- switch ($orderInfo['status']) {
- case 10: //待付款
- $order->trade_status = Order::ORDER_FLOW_DEFAULT;
- $order->is_pay = Order::IS_PAY_FALSE;
- break;
- case 21: //发货 / 部分发货
- case 20:
- $order->trade_status = Order::ORDER_FLOW_NO_SEND;
- break;
- case 30: //待收货
- $order->trade_status = Order::ORDER_FLOW_SEND;
- break;
- case 100: //已收货
- $order->trade_status = Order::ORDER_FLOW_CONFIRM;
- $order->confirm_time = $orderInfo['update_time'];
- break;
- case 200: //已售后完成,订单取消
- if(empty($order->express_no)){
- $order->trade_status = Order::ORDER_FLOW_CANCEL;
- }
- case 250: //超时未支付
- $order->trade_status = Order::ORDER_FLOW_CANCEL;
- $order->is_pay = Order::IS_PAY_FALSE;
- break;
- }
- if ($address_info) {
- $order->name = $address_info['user_name'];
- $order->mobile = $tel_number;
- $order->address = $address_info['province_name'] .
- $address_info['city_name'] .
- $address_info['county_name'] .
- $address_info['detail_info'];
- $order->address_data = json_encode([
- 'province' => $address_info['province_name'],
- 'city' => $address_info['city_name'],
- 'district' => $address_info['county_name'],
- 'detail' => $address_info['detail_info'],
- 'latitude' => '',
- 'longitude' => ''
- ], JSON_UNESCAPED_UNICODE);
- }
- $order->discount = 10;
- $order->integral = json_encode([
- 'forehead' => 0,
- 'forehead_integral' => 0
- ]);
- $order->version = cyy_version();
- $order->updated_at = $orderInfo['update_time'];
- $sharer_id = 0;
- if ($orderInfo['order_detail']['sharer_info']) {
- $sharer_info = $orderInfo['order_detail']['sharer_info'];
- $sharer = VideoShopSharer::findOne(['openid' => $sharer_info['sharer_openid'], 'action_id' => $mini_id, 'is_delete' => 0]);
- $sharer_id = $sharer->id ?: 0;
- //
- if ($sharer->user_id) {
- if ($video_shop_order) {
- $video_shop_order->old_parent_id = $sharer->user_id;
- }
- if($user){
- if ($user->parent_id <= 0 && $user->old_parent_id <= 0) {
- $user->parent_id = $user->old_parent_id = $order->old_parent_id;
- }
- if (!$user->save()) {
- throw new \Exception(json_encode($user->errors, JSON_UNESCAPED_UNICODE));
- }
- }
- }
- $order->rebate = 0;
- }
- if (!$order->save()) {
- throw new \Exception(json_encode($order->errors, JSON_UNESCAPED_UNICODE));
- }
- $promoter_id = [];
- if ($orderInfo['order_detail']['source_infos']) {
- $source_infos = $orderInfo['order_detail']['source_infos'];
- foreach((array)$source_infos as $source_info){
- $promoter_id[] = $source_info['account_id'];
- break;
- }
- }
- if (!$video_shop_order) {
- $video_shop_order = new VideoShopOrderExt();
- $video_shop_order->out_order_id = $item;
- $video_shop_order->store_id = $store_id;
- $video_shop_order->mini_id = $mini_id;
- }
- $video_shop_order->order_id = $order->id;
- $video_shop_order->sharer_id = $sharer_id;
- $video_shop_order->promoter_id = implode(',', $promoter_id);
- $orderInfo['order_detail']['commission_infos'] && $video_shop_order->commission_status = $orderInfo['order_detail']['commission_infos'][0]['status'];
- $video_shop_order->order_info = json_encode($orderInfo, JSON_UNESCAPED_UNICODE);
- if (!$video_shop_order->save()) {
- throw new \Exception(json_encode($video_shop_order->errors, JSON_UNESCAPED_UNICODE));
- }
- //保存售后 订单详情
- $order_d = [];
- if ($orderInfo['order_detail']['product_infos']) {
- $product_infos = $orderInfo['order_detail']['product_infos'];
- $order_detail = OrderDetail::findOne(['order_id' => $order->id]);
- if(!$order_detail){
- foreach ($product_infos as $product_info) {
- $shop_goods = VideoShopGoodsExt::findOne(['product_id' => (string)$product_info['product_id'],
- 'is_delete' => 0, 'mini_id' => $mini_id]);
- if ($shop_goods) {
- $goods = Goods::findOne($shop_goods->goods_id);
- if ($goods) {
- // 获取规格
- $attr = $goods->attr;
- $attr = json_decode($attr, true);
- $buy_attr = [];
- foreach ($attr as $attr_item) {
- if ($attr_item['sku_id'] === $product_info['sku_id']) {
- foreach ($attr_item['attr_list'] as &$attr_item_) {
- $attr_model = Attr::findOne($attr_item_['attr_id']);
- if ($attr_model) {
- $attr_group_model = AttrGroup::findOne($attr_model->attr_group_id);
- if ($attr_group_model) {
- $attr_item_ = array_merge($attr_item_, [
- 'attr_group_id' => $attr_group_model->id,
- 'attr_group_name' => $attr_group_model->attr_group_name
- ]);
- }
- }
- }
- $buy_attr = $attr_item['attr_list'];
- }
- }
- $order_detail = new OrderDetail();
- $order_detail->order_id = $order->id;
- $order_detail->goods_id = $shop_goods->goods_id;
- $order_detail->goods_name = $goods->name;
- $order_detail->num = $product_info['sku_cnt'] ?: 1;
- $order_detail->total_price = ($product_info['real_price'] * 0.01);
- $order_detail->created_at = $order->created_at;
- $order_detail->attr = json_encode($buy_attr, JSON_UNESCAPED_UNICODE);
- $order_detail->pic = $product_info['thumb_img'] ?: $goods->cover_pic;
- $order_detail->goods_info = json_encode($goods->toArray(), JSON_UNESCAPED_UNICODE);
- if (intval($orderInfo['status']) === 200) {
- $order_detail->is_refund = 1;
- }
- if (!$order_detail->save()) {
- throw new \Exception(json_encode($order_detail->errors, JSON_UNESCAPED_UNICODE));
- }
- array_push($order_d, $order_detail->id);
- }
- }
- }
- }
- }
- if (empty($order_detail)) {
- throw new \Exception('商品未找到');
- }
- $order_detail = $this->getOrderDetail($order);
- if ($video_shop_order->first_price <= 0) {
- $video_shop_order->first_price = $this->getShareCommissionMoney($order, $order_detail, $video_shop_order->old_parent_id, 1) ?: 0.00;
- }
- if (!$order->save()) {
- throw new \Exception(json_encode($order->errors, JSON_UNESCAPED_UNICODE));
- }
- if (intval($orderInfo['status']) === 200 && !empty($order->express_no)) {
- $order_refund = OrderRefund::findOne(['order_id' => $order->id]);
- if (!$order_refund) {
- $order_refund = new OrderRefund();
- $order_refund->store_id = $store_id;
- $order_refund->order_id = $order->id;
- $order_refund->user_id = $user_id;
- $order_refund->order_refund_no = $this->getOrderRefundNo();
- $order_refund->refund_price = $order->pay_price;
- $order_refund->type = 1;
- $order_refund->status = 1;
- $order_refund->is_agree = 1;
- $order_refund->is_user_send = 1;
- }
- $order_refund->order_detail_id = json_encode($order_d);
- $order_refund->pic_list = json_encode([]);
- if (!$order_refund->save()) {
- throw new \Exception(json_encode($order_refund->errors, JSON_UNESCAPED_UNICODE));
- }
- }
- }
- $t->commit();
- } catch (\Exception $e) {
- $t->rollBack();
- \Yii::error($e);
- debug_log([$store_id, $item, $order_id_list, $e->getMessage()], __CLASS__);
- // return [
- // 'code' => $e->getCode() ?: 1,
- // 'msg' => $e->getMessage() . $e->getLine() . $e->getFile()
- // ];
- }
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功'
- ];
- }
- public static function setOrderAddr(Order $order, $address_info = []){
- $province = District::find()->where(['like', 'name', $address_info['province_name']])->one();
- $city = District::find()->where(['like', 'name', $address_info['city_name']])->one();
- $county = District::find()->where(['like', 'name', $address_info['county_name']])->one();
- $order->province_id = (int)$province['id'];
- $order->city_id = (int)$city['id'];
- $order->district_id = (int)$county['id'];
- }
- public static function getOrderProfit($store_id, $product_infos = []){
- $orderProfit = 0;
- foreach($product_infos as $product_info){
- $payPrice = $product_info['real_price'] / 100;
- $goodsExt = VideoShopGoodsExt::findOne(['store_id' => $store_id, 'product_id' => (string)$product_info['product_id']]);
- $goods = Goods::findOne($goodsExt['goods_id']);
- // 计算单个商品可分红金额
- $profit = $goods['rate_type'] == 0 ? floatval($payPrice * $goods['rate'] / 100) : floatval($goods['rate']) * $product_info['sku_cnt'];
- $orderProfit += $profit;
- }
- return $orderProfit > 0 ? $orderProfit : 0;
- }
- private function getOrderRefundNo()
- {
- $order_refund_no = null;
- while (true) {
- $order_refund_no = date('YmdHis') . mt_rand(100000, 999999);
- $exist_order_refund_no = OrderRefund::find()->where(['order_refund_no' => $order_refund_no])->exists();
- if (!$exist_order_refund_no) {
- break;
- }
- }
- return $order_refund_no;
- }
- public function getShareCommissionMoney($order, $orderDetail, $user_id, $type = 1) {
- $setting = Option::get('share_money_setting', $order->store_id);
- $setting = $setting ? Json::decode($setting['value']) : [];
- if (!$setting) {
- \Yii::warning('未开启分销设置1');
- return false;
- }
- // if (!is_array($setting['commission_type']) || empty($setting['commission_type'])) {
- // \Yii::warning('未开启分销设置2');
- // return false;
- // }
- $share_send_type = 0;
- // 获取到当前到订单佣金发放类型 1: 佣金 2:积分, 3:余额
- if (!empty($setting['giveType'])) {
- $share_send_type = $setting['giveType'];
- }
- $integral = Option::get('integral', $order->store_id, 'store')['value'];
- if (!$integral && $share_send_type == 2) {
- \Yii::warning('发放积分佣金类型比例未设置');
- return false;
- }
- $order->share_send_type = $share_send_type;
- $share = Share::findOne(['user_id' => $user_id, 'status' => Share::SHARE_AUDIT_PASS, 'is_delete' => Share::SHARE_NOT_DELETE]);
- $share_commission_money = 0;
- if ($share) {
- $share_level_id = $share->level;
- $share_level = ShareLevel::findOne(['level' => $share_level_id, 'store_id' => $order->store_id, 'is_delete' => ShareLevel::SHARE_NOT_DELETE,
- 'status' => ShareLevel::STATUS_ON]);
- foreach ($orderDetail as $item) {
- // TODO: 价格
- $item_price = doubleval($item['price']);
- // $item_price = doubleval($orderShare->pay_price);
- $rate_first = 0;
- if ($item['individual_share'] == 1) {
- if ($type === 1) {
- $rate_first = $item['share_commission_new_first']['share_commission_level_' . $share_level_id];
- } elseif ($type === 2) {
- $rate_first = $item['share_commission_new_second']['share_commission_level_' . $share_level_id];
- } elseif ($type === 3) {
- $rate_first = $item['share_commission_new_third']['share_commission_level_' . $share_level_id];
- }
- $shareType = $item['share_type'];
- } else {
- $setting_shara = Option::get('share_money_setting', $order->store_id, 'share');
- $setting_shara = $setting_shara ? Json::decode($setting_shara['value']) : [];
- if ($type === 1) {
- $rate_first = $share_level->first_profit ?? ($setting_shara['level_one']['value'] ?? 0);
- } elseif ($type === 2) {
- $rate_first = $share_level->second_profit ?? ($setting_shara['level_two']['value'] ?? 0);
- } elseif ($type === 3) {
- $rate_first = $share_level->third_profit ?? ($setting_shara['level_three']['value'] ?? 0);
- }
- if ($share_level) {
- $shareType = intval($share_level->profit_type);
- } else {
- $shareType = 0;
- }
- }
- if ($shareType == 1) { // 金钱
- if ($share_send_type == 2) {
- // 兑换比例
- $share_commission_money += round($rate_first * $integral) * $item['num'];
- } else {
- $share_commission_money += $rate_first * $item['num'];
- }
- } else { // 比例
- if ($share_send_type == 2) {
- $share_commission_money += round($item_price * ($rate_first / 100) * $integral);
- } else {
- $share_commission_money += $item_price * $rate_first / 100;
- }
- }
- }
- return sprintf("%.2f", $share_commission_money < 0.01 ? 0 : $share_commission_money);
- }
- return 0;
- }
- private function getOrderDetail($order)
- {
- $list = OrderDetail::find()->where(['is_delete' => 0, 'order_id' => $order->id])->all();
- $newList = [];
- foreach ($list as $value) {
- $goods = $value->goods;
- $buyAttrList = Json::decode($value['attr']);
- if ($goods['attr_setting_type'] === 1) {
- $attrIdArr2 = [];
- foreach ($buyAttrList as $attrListItem2) {
- $attrIdArr2[] = $attrListItem2['attr_id'];
- }
- $goodsData = [
- 'attr' => $goods['attr'],
- 'price' => $goods['price'],
- 'is_level' => $goods['is_level'],
- ];
- $res = CommonGoods::currentGoodsAttr($goodsData, $attrIdArr2, [], 1, $order->user_id, $order->store_id);
- $newItem = [
- 'individual_share' => $goods['individual_share'],
- 'share_commission_new_first' => $res['share_commission_new_first'],
- 'share_commission_new_second' => $res['share_commission_new_second'],
- 'share_commission_new_third' => $res['share_commission_new_third'],
- 'share_type' => $goods['share_type'],
- 'num' => $value['num'],
- 'price' => $value['total_price'],
- 'mch_id' => $goods['mch_id']
- ];
- } else {
- $newItem = [
- 'individual_share' => $goods['individual_share'],
- 'share_commission_new_first' => $goods['share_commission_new_first'] ? json_decode($goods['share_commission_new_first'], true) : [],
- 'share_commission_new_second' => $goods['share_commission_new_second'] ? json_decode($goods['share_commission_new_second'], true) : [],
- 'share_commission_new_third' => $goods['share_commission_new_third'] ? json_decode($goods['share_commission_new_third'], true) : [],
- 'share_type' => $goods['share_type'],
- 'num' => $value['num'],
- 'price' => $value['total_price'],
- 'mch_id' => $goods['mch_id']
- ];
- }
- array_push($newList, $newItem);
- }
- return $newList;
- }
- public function getSharerOrderList($params = []) {
- try {
- $sharer_id = $this->sharer_id;
- $store_id = $this->store_id;
- $mini_id = $this->mini_id;
- $order_no = $this->order_no;
- $status = $this->status;
- $start_time = $this->start_time;
- $end_time = $this->end_time;
- $query = Order::find()->alias('o')->where(['o.store_id' => $store_id, 'o.is_delete' => 0, 's.is_cancle' => 0])
- ->leftJoin(['oe' => VideoShopOrderExt::tableName()], 'o.id = oe.order_id')->andWhere(['IS NOT', 'oe.id', NULL])
- ->leftJoin(['s' => StoreMini::tableName()], 'oe.mini_id = s.id');
- if ($params['order_id']) {
- $query->andWhere(['o.id' => $params['order_id']]);
- }
- if ($params['promoter_id']) {
- $query->andWhere(['oe.promoter_id' => $params['promoter_id']]);
- }
- if (isset($params['commission_status']) && $params['commission_status'] > -1) {
- $query->andWhere(['oe.commission_status' => $params['commission_status']]);
- }
- if ($sharer_id) {
- $query->andWhere(['oe.sharer_id' => $sharer_id]);
- }
- if ($mini_id) {
- $query->andWhere(['oe.mini_id' => $mini_id]);
- }
- if ($order_no) {
- $query->andWhere(['OR', ['LIKE', 'o.order_no', $order_no], ['LIKE', 'oe.out_order_id', $order_no]]);
- }
- if ($start_time) {
- $query->andWhere(['>=', 'o.created_at', strtotime($start_time)]);
- }
- if ($end_time) {
- $query->andWhere(['<=', 'o.created_at', strtotime($end_time)]);
- }
- if ($status !== '' && in_array($status, [-1, 0, 1, 2, 3, 4])) {//待支付 待发货 已取消 待收货 已收货 有售后
- if ($status === 4) {
- $query->leftJoin(['or' => OrderRefund::tableName()], 'or.order_id = o.id')->andWhere(['IS NOT', 'or.id', NULL]);
- } else {
- $query->andWhere(['o.trade_status' => $status]);
- }
- }
- $query->select('o.*, oe.mini_id, oe.sharer_id, oe.promoter_id, oe.order_info, oe.commission_status, oe.out_order_id')->orderBy('o.created_at desc');
- $pagination = pagination_make($query);
- $store_mini_ = StoreMini::find()->where(['store_id' => $store_id, 'fuwu_type' => 1, 'is_cancle' => 0])
- ->select('id, mini_nickname')->asArray()->all();
- $sharer_list = VideoShopSharer::find()->where(['store_id' => $store_id, 'is_delete' => 0, 'is_use' => 1])
- ->select('id, nickname')->asArray()->all();
- foreach ($pagination['list'] as &$item) {
- $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
- $item['trade_status'] = (string)$item['trade_status'];
- $order_refund = OrderRefund::findOne(['order_id' => $item['id']]);
- if ($order_refund) {
- $item['trade_status'] = '4';
- }
- $item['user'] = User::findOne($item['user_id']);
- $item['saas_user'] = SaasUser::findOne(['mobile' => $item['user']['binding']]);
- $sharer = VideoShopSharer::findOne($item['sharer_id']);
- $item['sharer_name'] = '-';
- $item['share_level'] = '-';
- $item['first_profit'] = '0.00';
- if ($sharer) {
- $item['sharer_name'] = $sharer->nickname ?: '分享员';
- $share_level = ShareLevel::findOne(['store_id' => $store_id, 'level' => $sharer->share_level, 'is_delete' => 0]);
- $item['share_level'] = $share_level['name'] ?: '默认等级';
- $item['first_profit'] = $share_level['first_profit'] ?: '0.00';
- }
- $store_mini = StoreMini::findOne($item['mini_id']);
- $item['store_mini_name'] = '-';
- if ($store_mini) {
- $item['store_mini_name'] = $store_mini->mini_nickname;
- }
- $item['goods_list'] = OrderDetail::find()->where(['order_id' => $item['id']])->select('goods_name name, pic, total_price, num, attr')->asArray()->all();
- foreach ($item['goods_list'] as &$goods_item) {
- $goods_item['attr'] = json_decode($goods_item['attr'], true);
- }
- $item['order_info'] = $this->_orderInfo($item['store_id'], $item['mini_id'], $item['out_order_id'], $item['order_info']);
- $item['promoterList'] = publicRanking\PublicRankingForm::promoterList($store_id, ['promoter_id' => explode(',', $item['promoter_id'])]);
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'q' => $query->createCommand()->getRawSql(),
- 'data' => $pagination['list'],
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount'],
- 'store_mini' => $store_mini_,
- 'sharer_list' => $sharer_list
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => $e->getCode() ?: 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function getSharerOrderNum() {
- try {
- $store_id = $this->store_id;
- $query = Order::find()->alias('o')->where(['o.store_id' => $store_id, 'o.is_delete' => 0, 's.is_cancle' => 0])
- ->leftJoin(['oe' => VideoShopOrderExt::tableName()], 'o.id = oe.order_id')->andWhere(['IS NOT', 'oe.id', NULL])
- ->leftJoin(['s' => StoreMini::tableName()], 'oe.mini_id = s.id')
- ->leftJoin(['vss' => VideoShopSharer::tableName()], 'vss.id = oe.sharer_id');
- $query->andWhere(['>' ,'oe.sharer_id', 0]);
- $query->andWhere(['o.is_pay' => 1]);
- $query->select('oe.sharer_id, count(o.id) order_num, sum(o.total_price) order_sum, vss.nickname, vss.invite_image, vss.username, vss.openid, vss.user_id');
- $query->groupBy('oe.sharer_id');
- $query->orderBy('order_sum DESC');
- $list = pagination_make($query);
- foreach ($list['list'] as &$item) {
- $item['avatar'] = '';
- if($item['user_id']){
- $user = User::find()->alias('u')->leftJoin(['su' => SaasUser::tableName()], 'su.mobile = u.binding')->select('su.avatar')->where(['u.id' => $item['user_id']])->asArray()->one();
- $item['avatar'] = $user['avatar'];
- }
-
- $first = (new ShareListForm(['store_id' => $store_id]))->getTeam($item['user_id'], 1);
- $item['first'] = $first;
- $item['first_count'] = count($first['data']);
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $list,
- ];
-
- } catch (\Exception $e) {
- return [
- 'code' => $e->getCode() ?: 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /*订单发货*/
- public function orderSend($order_id, $express_no, &$msg = []) {
- try {
- if (!$order_id) {
- return false;
- }
- $videoShopOrder = VideoShopOrderExt::findOne(['order_id' => $order_id, 'is_delete' => 0]);
- if (!$videoShopOrder) {
- return false;
- }
- $order_detail = OrderDetail::find()->where(['order_id' => $order_id, 'is_delete' => 0])->asArray()->all();
- $product_infos = [];
- if ($order_detail) {
- foreach ($order_detail as $goods_item) {
- $arr = [];
- $attr_id = json_decode($goods_item['attr'], true);
- $attr_id = array_column($attr_id, 'attr_id');
- sort($attr_id);
- $videoShopGoods = VideoShopGoodsExt::findOne(['goods_id' => $goods_item['goods_id']]);
- if ($videoShopGoods) {
- $arr['product_cnt'] = (int)$goods_item['num'];
- $arr['product_id'] = $videoShopGoods->product_id;
- $goods = Goods::findOne($goods_item['goods_id']);
- if ($goods) {
- $attr_ = json_decode($goods->attr, true);
- foreach ($attr_ as $attr_item) {
- $attr_id_ = array_column($attr_item['attr_list'], 'attr_id');
- sort($attr_id_);
- if (!array_diff($attr_id_, $attr_id)) {
- $arr['sku_id'] = $attr_item['sku_id'];
- }
- }
- }
- }
- if (!$arr['product_cnt'] && !$arr['product_id'] && !$arr['sku_id']) {
- continue;
- }
- $product_infos[] = $arr;
- }
- }
- if (!$express_no) {
- throw new \Exception('请填写快递单号');
- }
- $data = [
- 'order_id' => $videoShopOrder->out_order_id,
- 'delivery_list' => [
- [
- 'delivery_id' => 'OTHER',
- 'deliver_type' => 1,
- 'waybill_id' => $express_no,
- 'product_infos' => $product_infos
- ]
- ]
- ];
- $client = new BaseClient($this->miniProgram);
- $res = $client->httpPostJson('channels/ec/order/delivery/send', $data);
- if (!$res['errcode'] && !empty($res)) {
- $msg = [
- 'code' => 0,
- 'msg' => $res['errmsg']
- ];
- } else {
- throw new \Exception($res['errmsg'], $res['errcode']);
- }
- return true;
- } catch (\Exception $e) {
- $msg = [
- 'code' => $e->getCode() ?: 1,
- 'msg' => $e->getMessage()
- ];
- return true;
- }
- }
- }
|