| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\alliance\controllers\purchase;
- use app\models\Admin;
- use app\models\Option;
- use app\models\Order;
- use app\models\RechargeReOrder;
- use app\models\SaaSLeaguePriceLog;
- use app\models\SaasUser;
- use app\models\Store;
- use app\models\OrderDetail;
- use app\models\StoreCloud;
- use app\models\Supplier;
- use app\modules\alliance\controllers\BaseController;
- use app\models\Cat;
- use app\modules\admin\models\PlatformForm;
- use app\modules\models\GoodsListForm;
- use app\modules\admin\models\GoodsForm;
- use app\modules\admin\models\MerchantForm;
- use app\models\Purchase;
- use app\models\PurchaseImportCart;
- use app\modules\alliance\models\purchase\FavoriteForm;
- use \app\models\PurchaseFavorite;
- use app\modules\alliance\models\purchase\BalanceForm;
- use app\modules\alliance\models\purchase\order\OrderListForm;
- use yii\data\Pagination;
- class DefaultController extends BaseController
- {
- public function actionIndex(){
- $merchantForm = new MerchantForm();
- $merchantForm->page = 1;
- $merchantForm->limit = 1;
- $merchantForm->setSaasStoreCloudId();
- if (!$merchantForm->token_stroe_cloud_id) {
- $this->asJson([
- 'code' => 1,
- 'msg' => '未查到云仓用户'
- ]);
- }
- $balance = $merchantForm->mchGetBalanceLog();
- $order = OrderListForm::getCountData(get_saas_user_id());
- $item = get_saas_purchase_store_cloud();
- $mch = $item ? $item->name : '';
- $access_token = get_params('access_token');
- $saas_user_model = SaasUser::findOne(['access_token' => $access_token]);
- $admin = Admin::findOne(['saas_user_id' => $saas_user_model->id, 'is_delete' => 0, 'type' => 'store']);
- $store = Store::findOne($admin->type_id);
- //联盟券部分
- $query = SaaSLeaguePriceLog::find()->where(['store_id' => $store->id, 'is_delete' => 0, 'role' => SaaSLeaguePriceLog::ROLE_STORE])
- ->andWhere(['AND', ['>=', 'addtime', strtotime(date('Ymd'))], ['<=', 'addtime', time()]]);
- //今日收入
- $today_price_query = clone $query;
- $today_price= $today_price_query->andWhere(['send_or_take_type' => 0])->sum('league_price') ?: 0;
- //今日赠送
- $today_send_price_query = clone $query;
- $today_send_price = $today_send_price_query->andWhere(['type' => 0])->sum('league_price') ?: 0;
- //今日支出
- $today_out_price_query = clone $query;
- $today_out_price = $today_out_price_query->andWhere(['send_or_take_type' => 1])->sum('league_price') ?: 0;
- //列表
- $price_list = $query->select('id, type, send_or_take_type, league_price, addtime')->orderBy('addtime desc')->asArray()->all();
- foreach ($price_list as &$item) {
- $item['addtime'] = date('Y-m-d H:i:s', $item['addtime']);
- $item['type'] = (int)$item['type'];
- $item['send_or_take_type'] = (int)$item['send_or_take_type'];
- }
- $league_price = [
- 'price' => $today_price,
- 'send_price' => $today_send_price,
- 'out_price' => $today_out_price
- ];
- return $this->asJson([
- 'code' => 0,
- 'data' => [
- 'user' => get_saas_user(),
- 'mch' => $mch,
- 'balance' => $balance['data']['mch'],
- 'league_price' => $store->league_price,
- 'order' => $order,
- 'today_league_price' => $league_price,
- 'price_list' => $price_list
- ]
- ]);
- }
- /**
- *
- */
- public function actionBalance()
- {
- $merchantForm = new MerchantForm();
- $merchantForm->attributes = post_params();
- $merchantForm->setSaasStoreCloudId();
- return $this->asJson($merchantForm->mchGetBalanceLog());
- }
- public function actionLeague()
- {
- $page = post_params('page');
- $access_token = post_params('access_token');
- $saas_user_model = SaasUser::findOne(['access_token' => $access_token]);
- $saas_user_id = $saas_user_model->id;
- $admin = Admin::findOne(['saas_user_id' => $saas_user_id, 'is_delete' => 0, 'type' => 'store']);
- $store = Store::findOne($admin->type_id);
- $query= RechargeReOrder::find()->where(['store_id' => $store->id,'is_pay' => 1]);
- $count = $query->count();
- $pagination = new Pagination(['totalCount' => $count, 'page' => $page - 1, 'pageSize' => 10]);
- $list = $query->limit($pagination->limit)->offset($pagination->offset)->asArray()->orderBy('id DESC')->all();
- foreach ($list as & $arr) {
- $arr['desc'] = '充值联盟券'. $arr['league_price'] . '元';
- $arr['price'] = $arr['pay_price'];
- $arr['addtime'] = date('Y-m-d H:i:s' ,$arr['created_at']);
- }
- return $this->asJson([
- 'code' => 0,
- 'sql' => $query,
- 'msg' => '查找成功',
- 'count' => $count,
- 'data' => ['list' => $list]
- ]);
- }
- /**
- * 充值提交
- */
- public function actionBalanceSubmit()
- {
- $form = new \app\modules\alliance\models\purchase\BalanceForm();
- $store_id = get_saas_purchase_store_id();
- $form->store_id = $store_id;
- $form->saas_id = get_saas_user_id();
- $form->user = get_saas_user();
- $form->attributes = get_params();
- return $this->asJson($form->payData());
- }
- public function actionLeagueSubmit()
- {
- $form = new \app\modules\alliance\models\purchase\BalanceForm();
- $store_id = get_saas_purchase_store_id();
- $form->store_id = $store_id;
- $form->saas_id = get_saas_user_id();
- $form->user = get_saas_user();
- $form->attributes = get_params();
- return $this->asJson($form->payDataLeague());
- }
- public function actionBalanceSubmit2()
- {
- $order = \app\models\PurchaseReOrder::findOne(['order_no' => 'PR20221019154533294742']);
- $form = new \app\modules\alliance\models\purchase\BalanceForm();
- $form->store_id = $order->store_id;
- $form->saas_id = $order->saas_id;
- $form->order = $order;
- $result = $form->storeSubmitRecharge();
- var_dump($result);
- }
- /**
- * 商户商品分类列表
- */
- public function actionMchCatList()
- {
- $is_show = get_params('is_show',Cat::IS_SHOW_TRUE);
- $store_id = get_saas_purchase_store_id();
- $list = Cat::getCatList($store_id, $is_show, 0, 0);
-
- return $this->asJson([
- 'code' => 0,
- 'data' => [
- 'list' => $list
- ]
- ]);
- }
- /**
- * 云仓商品分类列表
- */
- public function actionCatList()
- {
- $form = new PlatformForm();
- $form->attributes = get_params();
- $form->is_show = get_params('is_show',Cat::IS_SHOW_TRUE);
- $this->asJson($form->getGoodsCatList());
- }
- /**
- * 云仓商品分类列表
- */
- public function actionCatListChild()
- {
- $form = new PlatformForm();
- $form->attributes = get_params();
- $this->asJson($form->getCatListChildren());
- }
- /**
- * 商品列表
- */
- public function actionGoodsInfo()
- {
- $form = new PlatformForm();
- $form->attributes = get_params();
- $goods = $form->goodsInfo();
- $hasFav = PurchaseFavorite::findOne(['goods_id'=>get_params('id', 0), 'saas_id' => get_saas_user_id(), 'is_delete' => 0]);
- $goods['is_favorite'] = empty($hasFav) ? 0 : 1;
- // 获取客服id
- $goods['kefu_id'] = '';
- if (isset($goods['data']['list'][0]['supplier']['id'])) {
- $supplier = Supplier::find()->where(['cloud_supplier_id' => $goods['data']['list'][0]['supplier']['id']])->one();
- if ($supplier) {
- $goods['data']['list'][0] = \app\modules\admin\models\jushuitan\JuShuiTanForm::syncSupplierJstGoodsQty($supplier->id, $goods['data']['list'][0]);
- $goods['supplier'] = [
- 'kefu_id' => $supplier->kefu_id,
- 'name'=> $supplier->supplier_name,
- ];
- }
- }
- //修改金额 建议零售价显示问题
- $store_cloud = get_saas_purchase_store_cloud();
- $store_id = $store_cloud->store_id;
- $rate = Store::find()->where(['id' => $store_id])->select('rate')->scalar() ?: 0;
- if (isset($goods['data']['list'])) {
- foreach ($goods['data']['list'] as &$goods_item) {
- $sale_price_type = Option::get('sale_price_type', $store_id, 'store')['value'];
- $goods_item['original_price'] = intval($sale_price_type) === 0 ? sprintf('%.2f', ($goods_item['platform_negotiated_price'] + ($goods_item['platform_negotiated_price']) * ($rate / 100))) : $goods_item['original_price'];
- $attrs = json_decode($goods_item['attrs'], true);
- foreach ($attrs as &$attr_item) {
- if (!intval($sale_price_type)) {
- $attr_item['original_price'] = sprintf('%.2f', ($attr_item['platform_negotiated_price'] + ($attr_item['platform_negotiated_price']) * ($rate / 100)));
- }
- }
- $goods_item['attrs'] = json_encode($attrs, JSON_UNESCAPED_UNICODE);
- }
- }
- return $this->asJson($goods);
- }
- /**
- * 商品列表
- */
- public function actionGoodsList()
- {
- $form = new MerchantForm();
- $form->attributes = get_params();
- $form->setSaasStoreCloudId();
- if (!$form->token_stroe_cloud_id) {
- $this->asJson([
- 'code' => 1,
- 'msg' => '未查到云仓用户'
- ]);
- }
- $store_cloud = get_saas_purchase_store_cloud();
- if($store_cloud && $store_cloud->cloud_platform_list){
- $cloud_platform_list = json_decode($store_cloud->cloud_platform_list, true);
- if($cloud_platform_list){
- $form->supplier_id = implode(',', $cloud_platform_list);
- }
- }
- if (intval(get_params('type')) === 2) {
- $form->is_distribution = 0;
- }
- $form->store_id = $store_cloud->store_id;
- $this->asJson($form->mchGetGoodsList());
- }
- //导入一个商品
- public function actionMchGoodsImport(){
- $post = post_params();
- if(!is_array($post['id'])){
- $post['id'] = [$post['id']];
- }
- foreach($post['id'] as $id){
- $_post = $post;
- $_post['id'] = $id;
- $form = new GoodsForm();
- $form->attributes = $_post;
- $form->set_saas_purchase_store_id();
- $goodsInfo = $form->saveCloudGoods();
- if (isset($goodsInfo['code'])) {
- return $this->asJson($goodsInfo);
- }
- $merchantForm = new MerchantForm();
- $merchantForm->setSaasStoreCloudId();
- if (!$merchantForm->token_stroe_cloud_id) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '未查到云仓用户'
- ]);
- }
- $bind = $merchantForm->mchGoodsImport($goodsInfo['cloudBindInfo'],$goodsInfo['goods_id']);
- }
- return $this->asJson($bind);
- }
- //导入商品列表
- public function actionMchGoodsImports(){
- $ids = post_params('ids');
- $list = PurchaseImportCart::findAll(['id' => $ids, 'saas_id' => get_saas_user_id()]);
- foreach($list as $item){
- $form = new GoodsForm();
- $form->attributes = [
- 'id' => $item['goods_id'],
- // 'store_id' => $item['store_id'],
- 'cat_id' => explode(',', $item['cat_ids']),
- ];
- $form->set_saas_purchase_store_id();
- $goodsInfo = $form->saveCloudGoods();
- $merchantForm = new MerchantForm();
- $merchantForm->setSaasStoreCloudId();
- if (!$merchantForm->token_stroe_cloud_id) {
- $this->asJson([
- 'code' => 1,
- 'msg' => '未查到云仓用户'
- ]);
- }
- $bind = $merchantForm->mchGoodsImport($goodsInfo['cloudBindInfo'],$goodsInfo['goods_id']);
- if($bind['code'] == 0){
- PurchaseImportCart::deleteAll(['id' => $item['id']]);
- }
- }
- $this->asJson($bind);
- }
- //添加铺货单
- public function actionAddMchGoodsImport(){
- $res = [
- 'code' => 1,
- 'msg' => '操作失败',
- ];
- $post = post_params();
- if (is_array($post['cat_id'])) {
- $post['cat_id'] = implode(',', $post['cat_id']);
- }
- $exist = PurchaseImportCart::findOne([
- 'goods_id' => $post['id'],
- 'store_id' => get_saas_purchase_store_id(),
- 'cat_ids' => $post['cat_id'],
- 'saas_id' => get_saas_user_id(),
- ]);
- if($exist){
- $res = [
- 'code' => 0,
- 'msg' => '操作成功',
- ];
- return $this->asJson($res);
- }
- $cart = new PurchaseImportCart();
- $cart->goods_id = $post['id'];
- $cart->store_id = get_saas_purchase_store_id();
- $cart->cat_ids = $post['cat_id'];
- $cart->saas_id = get_saas_user_id();
- $save = $cart->save();
- if($save){
- $res = [
- 'code' => 0,
- 'msg' => '操作成功',
- ];
- }
- $this->asJson($res);
- }
- //修改铺货单
- public function actionUpdateMchGoodsImport(){
- $res = [
- 'code' => 1,
- 'msg' => '操作失败',
- ];
- $post = post_params();
- $cart = PurchaseImportCart::findOne([
- 'id' => $post['id'],
- ]);
- if(!$cart){
- return $this->asJson($res);
- }
- $cart->cat_ids = implode(',', $post['cat_ids']);
- $cart->saas_id = get_saas_user_id();
- $save = $cart->save();
- if($save){
- $res = [
- 'code' => 0,
- 'msg' => '操作成功',
- ];
- }
- $this->asJson($res);
- }
- //删除铺货单
- public function actionDelMchGoodsImport(){
- $res = [
- 'code' => 1,
- 'msg' => '操作失败',
- ];
- $ids = post_params('ids');
- $del = PurchaseImportCart::deleteAll(['id' => $ids, 'saas_id' => get_saas_user_id()]);
- if($del){
- $res = [
- 'code' => 0,
- 'msg' => '操作成功',
- ];
- }
- $this->asJson($res);
- }
- //获取铺货单
- public function actionGetMchGoodsImport(){
- $catList = Cat::find()->where([
- 'store_id' => get_saas_purchase_store_id(),
- ])->all();
- $list = PurchaseImportCart::findAll(['saas_id' => get_saas_user_id()]);
- foreach ($list as $i => $item) {
- $list[$i] = $item->toArray();
- $list[$i]['cat_list'] = [];
- foreach (explode(',', $item['cat_ids']) as $citem) {
- foreach ($catList as $_citem) {
- if($citem == $_citem['id']){
- $list[$i]['cat_list'][] = [
- 'id' => $_citem['id'],
- 'name' => $_citem['name'],
- ];
- }
- }
- }
- $form = new PlatformForm();
- $form->id = $item['goods_id'];
- $glist = $form->goodsList();
- if($glist['code'] == 0 && $glist['data']['count'] == 1){
- $list[$i]['goods_info'] = $glist['data']['list'][0];
- }else{
- unset($list[$i]);
- }
- }
- $newList = array();
- foreach ($list as $i => $item) {
- $supplier_id = $item['goods_info']['supplier_id'];
- $newList[$supplier_id]['supplier'] = $item['goods_info']['supplier'];
- $newList[$supplier_id]['goods_list'][] = $item;
- }
- $res = [
- 'code' => 0,
- 'msg' => '操作成功',
- 'data' => array_values($newList),
- ];
- $this->asJson($res);
- }
- /**
- * 添加商品或店铺到我的喜欢
- */
- public function actionFavoriteAdd()
- {
- $form = new FavoriteForm();
- $form->attributes = post_params();
- $form->store_id = get_saas_purchase_store_id();
- $form->saas_id = get_saas_user_id();
- return $this->asJson($form->add());
- }
- /**
- * 喜欢的商品或店铺列表
- */
- public function actionFavoriteList()
- {
- $form = new FavoriteForm();
- $form->attributes = get_params();
- $form->store_id = get_saas_purchase_store_id();
- $form->saas_id = get_saas_user_id();
- return $this->asJson($form->search());
- }
- /**
- * 取消喜欢的商品或店铺列表
- */
- public function actionFavoriteRemove()
- {
- $form = new FavoriteForm();
- $form->attributes = post_params();
- $form->store_id = get_saas_purchase_store_id();
- $form->saas_id = get_saas_user_id();
- return $this->asJson($form->remove());
- }
- /**
- * 申请供货商
- */
- public function actionApplySupplier()
- {
- $tel = post_params('tel');
- $form = new PlatformForm();
- $form->attributes = post_params();
- if ($tel) {
- $admin = Admin::findOne(['mobile' => $tel, 'is_delete' => 0, 'type' => 'supplier']);
- if ($admin) {
- // if ($admin->type !== 'supplier') {
- // return $this->asJson([
- // 'code' => 1,
- // 'msg' => '当前手机号已经绑定'
- // ]);
- // }
- $supplier = Supplier::findOne($admin->type_id);
- if ($supplier) {
- if ((int)$supplier->status === 1) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '当前已经为供货商'
- ]);
- } elseif ((int)$supplier->status === 2) {
- $form->id = $supplier->cloud_supplier_id;
- $form->status = 0;
- return $this->asJson($form->editSupplier());
- } else {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '供货商信息审核中不可编辑'
- ]);
- }
- }
- }
- }
- $form->saas_user_id = get_saas_user_id();
- return $this->asJson($form->createSupplier());
- }
- /**
- * 获取供货商信息
- */
- public function actionGetSupplierApply()
- {
- $saas_user = get_saas_user();
- if ($saas_user->id) {
- $suppler = Supplier::findOne(['saas_user_id' => $saas_user->id, 'is_delete' => 0]);
- if (!empty($suppler->cloud_supplier_id)) {
- $form = new PlatformForm();
- $form->ids = $suppler->cloud_supplier_id;
- $res = $form->listSupplier();
- $res['data'] = $res['data']['list'][0];
- $res['data']['url'] = \Yii::$app->request->hostInfo . '/admin/#/login';
- $res['data']['password'] = $suppler->password;
- } else {
- $res = [
- 'code' => 0,
- 'data' => [
- "supplier_name" => "",
- "name" => "",
- "logo" => "",
- "tel" => $saas_user->mobile,
- "type" => "",
- "certificate" => "",
- "id_card_reverse" => "",
- "id_card_front" => "",
- "sale_day" => "",
- "status" => "",
- "url" => \Yii::$app->request->hostInfo . '/admin/#/login'
- ],
- 'msg' => '获取成功'
- ];
- }
- return $this->asJson($res);
- } else {
- return $this->asJson([
- 'code' => 1,
- 'data' => '未获取登陆状态'
- ]);
- }
- }
- /**
- * 创建转单
- */
- public function actionCreateOrderTrans() {
- $order_id = post_params('order_id');
- $order = Order::findOne($order_id);
- $order_detail = OrderDetail::find()->where(['order_id' => $order_id])->select('goods_id')->asArray()->all();
- if (!empty($order) && !empty($order_detail)) {
- $form = new MerchantForm();
- $form->address = $order->address;
- $form->province_id = $order->province_id;
- $form->city_id = $order->city_id;
- $form->district_id = $order->district_id;
- $form->order_id = $order_id;
- $form->tel = $order->mobile;
- $form->name = $order->name;
- $form->store_id = $order->store_id;
- $result = $form->mchSetPurchaseOrder();
- return $this->asJson($result);
- } else {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '订单信息错误'
- ]);
- }
- }
- /**
- * 设置云仓
- */
- public function actionStoreRate() {
- try {
- $saas_user_id = get_saas_user_id();
- $store_cloud = StoreCloud::find()
- ->where(['AND', ['saas_user_id' => $saas_user_id, 'is_delete' => 0], ['>', 'store_id', 0]])->asArray()->one();
- if (!$store_cloud) {
- throw new \Exception('当前云仓账户未绑定商城信息');
- }
- $store_id = $store_cloud['store_id'];
- $store = Store::findOne(['id' => $store_id, 'is_delete' => 0]);
- if (!$store) {
- throw new \Exception('商城信息查找失败');
- }
- $data = [];
- if (\Yii::$app->request->isGet) {
- $cloud_is_update = Option::get('cloud_is_update', $store_id, 'store')['value'];
- $reduce_type = Option::get('reduce_type', $store_id, 'store')['value'];
- $sale_price_type = Option::get('sale_price_type', $store_id, 'store')['value'];
- $arr = [
- 'rate' => $store->rate,
- // 'is_auth_trans' => $store->is_auth_trans,
- 'cloud_is_update' => (int)$cloud_is_update,
- 'reduce_type' => $reduce_type,
- 'sale_price_type' => (int)$sale_price_type
- ];
- //自动优先扣除方式
- $form = new MerchantForm();
- $result = $form->setCloudMerchantConfig(null, $store_cloud['id']);
- if ($result['code'] === 0) {
- $arr['reduce_type'] = (int)$result['data']['reduce_type'];
- }
- $data = [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => $arr
- ];
- } elseif (\Yii::$app->request->isPost) {
- $rate = floatval(post_params('rate', 0));
- $reduce_type = post_params('reduce_type', 0);
- // $is_auth_trans = post_params('is_auth_trans', 0);
- $cloud_is_update = post_params('cloud_is_update', 0);
- $sale_price_type = post_params('sale_price_type', 0);
- Option::set('sale_price_type', $sale_price_type, $store_id, 'store');
- Option::set('cloud_is_update', $cloud_is_update, $store_id, 'store');
- Option::set('reduce_type', $reduce_type, $store_id, 'store');
- $store->rate = $rate;
- // $store->is_auth_trans = $is_auth_trans;
- if (!$store->save()) {
- throw new \Exception(json_encode($store->errors));
- }
- //自动优先扣除方式
- $form = new MerchantForm();
- $result = $form->setCloudMerchantConfig($reduce_type, $store_cloud['id']);
- $data = [
- 'code' => 0,
- 'msg' => "操作成功",
- ];
- }
- return $this->asJson($data);
- } catch (\Exception $e) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ]);
- }
- }
- }
|