| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\controllers;
- use app\jobs\LgCashJob;
- use app\models\AccountLog;
- use app\models\Admin;
- use app\models\AgentFrontBind;
- use app\models\Cash;
- use app\models\CashExt;
- use app\models\Color;
- use app\models\Coupon;
- use app\models\District;
- use app\models\Goods;
- use app\models\Lg;
- use app\models\Md;
- use app\models\MdGoods;
- use app\models\MdProfit;
- use app\models\MdStaff;
- use app\models\Order;
- use app\models\OrderDetail;
- use app\models\Qrcode;
- use app\models\SaasUser;
- use app\models\Salesman;
- use app\models\Share;
- use app\models\Shop;
- use app\models\Store;
- use app\models\User;
- use app\models\WechatConfig;
- use app\modules\admin\models\DriverForm;
- use app\modules\admin\models\InventoryForm;
- use app\modules\admin\models\MdForm;
- use app\modules\admin\models\MdGroupActivitiesForm;
- use app\modules\admin\models\MdGroupDriverForm;
- use app\modules\admin\models\MdSettingForm;
- use app\modules\admin\models\QrcodeForm;
- use app\utils\Alipay\Alipay;
- use app\utils\Export;
- use app\utils\ExportList;
- use app\utils\LgApi;
- use app\utils\Notice\NoticeSend;
- use app\utils\OrderNo;
- use yii\base\BaseObject;
- use yii\helpers\Json;
- use app\jobs\BatchDownGoodsJob;
- use app\models\Option;
- use app\models\StoreMoneyLog;
- class MdController extends BaseController
- {
- /**
- * 门店列表
- */
- public function actionList()
- {
- $param = get_params();
- $form = new MdForm();
- $form->scenario = $form::SCENARIO_LIST;
- $form->attributes = $param;
- return $this->asJson($form->searchMd());
- }
- /**
- * 门店列表
- */
- public function actionAuditList()
- {
- $param = get_params();
- $form = new MdForm();
- $form->attributes = $param;
- return $this->asJson($form->searchAudit());
- }
- /**
- * 门店添加
- */
- public function actionAdd()
- {
- $form = new MdForm();
- $form->scenario = $form::SCENARIO_ADD;
- $form->attributes = post_params();
- $form->store_id = get_store_id();
- return $this->asJson($form->saveMd());
- }
- /**
- * 门店编辑
- */
- public function actionDetail()
- {
- $id = get_md_id();
- $md = Md::findOne($id);
- $md->password = "";
- $md->cat_id = intval($md->cat_id) ?: null;
- $res = [
- 'code' => 0,
- 'data' => $md
- ];
- return $this->asJson($res);
- }
- /**
- * 门店编辑
- */
- public function actionEdit()
- {
- $param = post_params();
- $form = new MdForm();
- $form->scenario = $form::SCENARIO_EDIT;
- $form->attributes = $param;
- $form->store_id = get_store_id();
- return $this->asJson($form->saveMd());
- }
- /**
- * 删除门店
- */
- public function actionDel()
- {
- //主键id用key标识并进行获取
- $param = get_params('key');
- $form = new MdForm();
- $form->scenario = $form::SCENARIO_DEL;
- $form->id = $param;
- return $this->asJson($form->delMd());
- }
- /**
- * 审核
- */
- public function actionAudit()
- {
- $param = post_params();
- $form = new MdForm();
- $form->scenario = $form::SCENARIO_AUDIT;
- $form->attributes = $param;
- return $this->asJson($form->auditMd());
- }
- /**
- * 审核门店
- * @return \yii\web\Response
- */
- public function actionBatchAudit()
- {
- $id = post_params('id');
- $status = post_params('shop_audit');
- if (!in_array($status, [Md::SHOP_AUDIT_NO, Md::SHOP_AUDIT_YES])) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '参数错误'
- ]);
- }
- if (empty($id) || !is_array($id)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '参数错误'
- ]);
- }
- $t = \Yii::$app->db->beginTransaction();
- Md::updateAll(['shop_audit' => $status, 'audit_time' => time()], ['in', 'id', $id]);
- if ($status == 1) {
- foreach ($id as $value) {
- $md = Md::findOne($value);
- if ($md->user_id > 0) {
- $md->manager = $md->user_id;
- if (!$md->save()) {
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => $md->errors[0]
- ]);
- }
- $saas_user = SaasUser::findOne($md->user_id);
- $md_staff = MdStaff::findOne(['saas_user_id' => $md->user_id, 'is_delete' => 0, 'store_id' => get_store_id()]);
- if (!$md_staff) {
- $md_staff = new MdStaff();
- $md_staff->saas_user_id = $md->user_id;
- $md_staff->is_manager = 1;
- $md_staff->store_id = get_store_id();
- $md_staff->md_id = $md->id;
- $md_staff->name = $saas_user->name;
- $md_staff->mobile = $saas_user->mobile;
- if (!$md_staff->save()) {
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => $md_staff->errors[0]
- ]);
- }
- //兼容老版本
- $user_name = $md->user_name ?: $md->mobile;
- $exist_admin = Admin::findOne(['type' => 'md', 'type_id' => $md->id, 'username' => $user_name, 'store_id' => get_store_id()]);
- if ($exist_admin) {
- $exist_admin->is_delete = 1;
- if (!$exist_admin->save()) {
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => $exist_admin->errors[0]
- ]);
- }
- }
- $admin = Admin::find()->where(['username' => $user_name, 'is_delete' => 0])->one();
- if ($admin) {
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => '保存失败,管理员账户已经存在'
- ]);
- }
- // 自动创建admin账号
- $admin = new Admin();
- $admin->store_id = get_store_id();
- $admin->access_token = \Yii::$app->security->generateRandomString();
- $admin->username = $md->user_name;
- $admin->password = $md->password;
- $admin->mobile = $md->mobile;
- $admin->avatar = $saas_user->avatar;
- $admin->name = $md->name;
- $admin->type = 'md';
- $admin->type_id = $md->id;
- if (!$admin->save()) {
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => $admin->errors[0]
- ]);
- }
- }
- }
- }
- // 消息队列处理批量下架门店商品
- // queue_push(new BatchDownGoodsJob(['ids' => $id, 'store_id' => get_store_id()]));
- }
- $t->commit();
- return $this->asJson([
- 'code' => 0,
- 'msg' => '审核成功'
- ]);
- }
- /**
- * 手动更新平台规格数据
- * @return \yii\web\Response
- */
- public function actionSetAttr()
- {
- $goods_id = post_params('goods_id');
- // $md_id = get_md_id();
- if (!is_array($goods_id) || empty($goods_id)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '参数错误'
- ]);
- }
- try {
- $count = 0;
- foreach ($goods_id as $id) {
- $md_goods = MdGoods::find()->where(['goods_id' => $id])->all();
- if (empty($md_goods)) {
- continue;
- }
- $goods = Goods::findOne($id);
- foreach ($md_goods as $key => $value) {
- $value->attr = $goods->attr;
- $value->goods_num = $goods->goods_num;
- $value->price = $goods->price;
- $value->status = 0;
- if ($value->save()) {
- $count++;
- }
- }
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => '成功同步' . $count . '条门店商品数据',
- ]);
- } catch (\Exception $e) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => '出现错误',
- ]);
- }
- }
- /**
- * 门店订单转单至平台
- */
- public function actionTransOrder()
- {
- $order_id = post_params('id');
- $order = Order::find()->where(['store_id' => get_store_id(), 'id' => $order_id, 'is_delete' => 0])->andWhere(
- ['<', 'trade_status', Order::ORDER_FLOW_CANCEL]
- )->one();
- if (!$order) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '转单数据有误'
- ]);
- }
- $order->is_trans = 1;
- if ($order->save()) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => '转单成功'
- ]);
- } else {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $order->errors[0]
- ]);
- }
- }
- /**
- * 更换店长
- * @return \yii\web\Response
- */
- public function actionManager()
- {
- $md_id = post_params('md_id');
- $saas_user_id = post_params('saas_user_id');
- if (empty($md_id) || empty($saas_user_id)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '参数有误'
- ]);
- }
- $md = Md::findOne($md_id);
- if (!$md) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '门店不存在'
- ]);
- }
- $staff = MdStaff::findOne(['store_id' => get_store_id(), 'saas_user_id' => $saas_user_id, 'is_delete' => 0]);
- $staff_1 = MdStaff::findOne(['store_id' => get_store_id(), 'md_id' => $md_id, 'saas_user_id' => $saas_user_id, 'is_delete' => 0]);
- if ($staff && !$staff_1) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '该员工已在其他门店下,请重新选择'
- ]);
- }
- $t = \Yii::$app->db->beginTransaction();
- $old_manager = MdStaff::findOne(['store_id' => get_store_id(), 'md_id' => $md_id, 'is_manager' => 1, 'is_delete' => 0]);
- if ($old_manager) {
- $old_manager->is_manager = 0;
- if (!$old_manager->save()) {
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => $old_manager->errors[0]
- ]);
- }
- }
- if (!$staff_1) {
- $saas_user = SaasUser::findOne($saas_user_id);
- $md_staff = new MdStaff();
- $md_staff->store_id = get_store_id();
- $md_staff->md_id = $md_id;
- $md_staff->saas_user_id = $saas_user_id;
- $md_staff->name = $saas_user->name;
- $md_staff->mobile = $saas_user->mobile;
- $md_staff->is_manager = 1;
- if (!$md_staff->save()) {
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => $md_staff->errors[0]
- ]);
- }
- } else {
- $staff_1->is_manager = 1;
- if (!$staff_1->save()) {
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => $staff_1->errors[0]
- ]);
- }
- }
- $md->manager = $saas_user_id;
- $md->user_id = $saas_user_id;
- if (!$md->save()) {
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'data' => $md->errors[0]
- ]);
- }
- $t->commit();
- return $this->asJson([
- 'code' => 0,
- 'data' => '操作成功'
- ]);
- }
- /**
- * 批量操作
- * @return \yii\web\Response
- */
- public function actionBatch()
- {
- $id = post_params('id');
- $status = post_params('status');
- if (!is_array($id)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '参数不正确'
- ]);
- }
- // 这里需要判断是否是独立运营门店 如果是独立运营门店 将独立运营门店修改为非独立的 并保留门店员工数据 如果是非独立运营门店执行删除 要删除门店员工
- $md_list = Md::find()->where(['in', 'id', $id])->all();
- foreach ($md_list as $md){
- if($md->is_single == 1){
- $md->is_single = 0;
- $md->save();
- }else{
- $md->is_delete = 1;
- if($md->save()){
- // 删除门店员工
- MdStaff::updateAll(['is_delete' => 1], ['id'=>$md->id]);
- }
- }
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => '更新成功'
- ]);
- }
- public function actionStaff()
- {
- $name = get_params('name');
- $mobile = get_params('mobile');
- $start_time = get_params('start_time');
- $end_time = get_params('end_time');
- $md_id = get_params('md_id');
- $store_id = get_store_id();
- $is_single = get_params('is_single');// 这里不能给默认值,给了的话会影响门店后台的员工列表请求
- if ($is_single == null) {
- $md = Md::findOne($md_id);
- if ($md) {
- $is_single = $md->is_single;
- }
- }
- $query = MdStaff::find()->alias('ms')->leftJoin(['md' => Md::tableName()], 'md.id=ms.md_id')->where(['ms.is_delete' => 0])->orderBy("ms.created_at desc, ms.updated_at desc");
- if ($md_id !== null) {
- if ($md_id <= 0) {
- $query->andWhere(['AND', ['ms.md_id' => [0, -1]], ['IS', 'md.id', NULL]]);
- } else {
- $query->andWhere(['ms.md_id' => $md_id, 'md.is_delete' => 0, 'md.is_single' =>$is_single]);
- }
- }else{
- // 商城后台查询员工的时候 要根据$is_single取独立运营门店 或者 非独立运营门店的店员
- $query->andWhere( ['md.is_single' =>$is_single]);
- }
- if ($store_id > 0) {
- $query->andWhere(['ms.store_id' => $store_id]);
- }
- if ($name) {
- $query->andWhere(['like', 'ms.name', $name]);
- }
- if ($mobile) {
- $query->andWhere(['like', 'ms.mobile', $mobile]);
- }
- if ($start_time) {
- $query->andWhere(['>=', 'ms.created_at', strtotime($start_time)]);
- }
- if ($end_time) {
- $query->andWhere(['<=', 'ms.created_at', strtotime($end_time)]);
- }
- $list = pagination_make($query);
- foreach ($list['list'] as &$val) {
- $saas_user = SaasUser::findOne($val['saas_user_id']);
- $user = User::findOne(['store_id' => get_store_id(),'binding' => $saas_user->mobile]);
- $val['avatar'] = $user->avatar_url;
- $val['md_name'] = Md::findOne($val['md_id'])->name;
- if ($val['md_id'] <= 0) {
- $val['md_name'] = Store::findOne($val['store_id'])->name;
- }
- $val['nickname'] = $user->nickname;
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list['list'],
- 'pageNo' => $list['pageNo'],
- 'totalCount' => $list['totalCount']
- ]
- ]);
- }
- public function actionStaffAdd()
- {
- $store_id = get_store_id();
- $md_id = get_md_id();
- $name = post_params('name');
- $mobile = post_params('mobile');
- $saas_user_id = post_params('saas_user_id');
- if (empty($name) || empty($mobile) || empty($saas_user_id)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '参数有误'
- ]);
- }
- $user = User::findOne($saas_user_id);
- if (!$user) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '用户不存在'
- ]);
- }
- $saas_user = SaasUser::findOne(['mobile' => $user->binding]);
- if (!$saas_user) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '用户不存在'
- ]);
- }
- $staff = MdStaff::findOne(['store_id' => $store_id, 'saas_user_id' => $saas_user->id, 'is_delete' => 0]);
- if ($staff) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '该员工已在其他门店下,请重新绑定'
- ]);
- }
- if ($md_id <= 0) {
- $staff = MdStaff::findOne(['saas_user_id' => $saas_user->id, 'is_delete' => 0]);
- if ($staff) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '该用户已在其他店铺下,请重新绑定'
- ]);
- }
- }
- $md_staff = new MdStaff();
- $md_staff->store_id = $store_id;
- $md_staff->md_id = $md_id;
- $md_staff->saas_user_id = $saas_user->id;
- $md_staff->name = $name;
- $md_staff->mobile = $mobile;
- if (!$md_staff->save()) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $md_staff->errors[0]
- ]);
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => '保存成功'
- ]);
- }
- /**
- * 更新状态
- * @return \yii\web\Response
- */
- public function actionStaffStatus()
- {
- $id = post_params('id');
- $type = post_params('type');
- $status = post_params('status');
- if (empty($id) || !is_array($id)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '参数有误'
- ]);
- }
- if (!in_array($status, [0, 1])) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '状态参数有误'
- ]);
- }
- if ($type == 'is_disable') {
- MdStaff::updateAll(['is_disable' => $status], ['in', 'id', $id]);
- }
- if ($type == 'is_delete') {
- MdStaff::updateAll(['is_delete' => $status], ['in', 'id', $id]);
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => '更新成功'
- ]);
- }
- public function actionSelectList()
- {
- $is_single = get_params('is_single', -1);
- $query = Md::find()->where(['is_delete' => 0, 'shop_audit' => 1, 'store_id' => get_store_id()])->select('id, name');
- if($is_single != -1){
- $query->andWhere(['is_single' => $is_single]);
- }
- $list = $query->asArray()->all();
- return $this->asJson(
- ['code' => 0, 'data' => $list]
- );
- }
- public function actionProfit()
- {
- $export = get_params('export');
- $order_no = get_params('order_no');
- $md_name = get_params('md_name');
- $is_send = get_params('is_send');
- $start_time = get_params('start_time');
- $end_time = get_params('end_time');
- $md_id = get_md_id();
- $query = MdProfit::find()->alias('mp')->leftJoin(['md' => Md::tableName()], 'md.id=mp.md_id')
- ->leftJoin(['o' => Order::tableName()], 'o.id=mp.order_id')
- ->where(['mp.store_id' => get_store_id()])
- ->select('mp.*, md.is_single, md.cover_url, o.order_no, o.user_id')->orderBy("mp.created_at desc, mp.updated_at desc");
- if ($md_id) {
- $query->andWhere(['mp.md_id' => $md_id]);
- }
- if (in_array($is_send, [0, 1])) {
- $query->andWhere(['mp.is_send' => $is_send]);
- }
- if ($order_no) {
- $query->andWhere(['like', 'o.order_no', $order_no]);
- }
- if ($md_name) {
- $query->andWhere(['like', 'md.name', $md_name]);
- }
- if ($start_time) {
- $query->andWhere(['>=', 'mp.created_at', strtotime($start_time)]);
- }
- if ($end_time) {
- $query->andWhere(['<=', 'mp.created_at', strtotime($end_time)]);
- }
- $list = pagination_make($query);
- foreach ($list['list'] as &$val) {
- $val['md_name'] = Md::findOne($val['md_id'])->name;
- if ($val['saas_user_id']) {
- $val['user_name'] = SaasUser::findOne($val['saas_user_id'])->name;
- } else {
- $val['user_name'] = SaasUser::findOne(['mobile' => User::findOne($val['user_id'])->binding])->name;
- }
- }
- if($export){
- return $this->exportProfit($list['list']);
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list['list'],
- 'pageNo' => $list['pageNo'],
- 'totalCount' => $list['totalCount']
- ]
- ]);
- }
- private function exportProfit($list)
- {
- $rows = [[
- 'ID',
- '门店名称',
- '昵称',
- '下单时间',
- '订单号',
- '销售佣金(元)',
- '核销佣金(元)',
- '利润佣金(元)',
- '总佣金(元)',
- '操作时间',
- '佣金状态',
- ]];
- foreach($list as $item){
- $r = [
- $item['id'],
- $item['md_name'],
- $item['user_name'],
- date('Y-m-d H:i:s', $item['created_at']),
- $item['order_no'],
- $item['pay_profit'],
- $item['clerk_profit'],
- $item['sale_profit'],
- $item['total_profit'],
- $item['updated_at'] ? date('Y-m-d H:i:s', $item['updated_at']) : '-',
- $item['is_send'] == 0 ? '未到账' : '已到账',
- ];
- $rows[] = $r;
- }
- $writer = \Spatie\SimpleExcel\SimpleExcelWriter::streamDownload(time() . '.xlsx')->noHeaderRow()
- ->addRows($rows)->toBrowser();
- }
- public function actionProfitData()
- {
- $id = input_params('id');
- $store_id = get_store_id();
- $query = Md::find()->where(['store_id' => $store_id, 'is_delete' => 0, 'shop_audit' => 1])->orderBy("id desc");
- $queryCount = clone $query;
- if ($id) {
- $query->andWhere(['id' => $id]);
- }
- $query->select('id, is_single, name, cover_url, total_profit, cash_profit');
- $list = pagination_make($query);
- foreach($list['list'] as &$item){
- $item['mdCashSum'] = floor_num(Cash::find()->where(['is_delete' => Cash::IS_DELETE_NO, 'md_id' => $item['id'], 'status' => 2])->andWhere(['cash_type' => 2])->sum('price'));
- $item['mdProfitWait'] = floor_num(MdProfit::find()->where(['md_id' => $item['id'], 'is_send' => 0])->sum('total_profit'));
- }
- if($list['pageNo'] == 1){
- $count = [];
- $count['mdCount'] = (int)$queryCount->count();
- $count['mdCashPriceSum'] = floor_num($queryCount->sum('cash_profit'));
- $count['mdCashSum'] = floor_num(Cash::find()->where(['is_delete' => Cash::IS_DELETE_NO, 'store_id' => $store_id, 'status' => 2])->andWhere(['cash_type' => 2])->sum('price'));
- $count['mdProfitWait'] = floor_num(MdProfit::find()->where(['store_id' => $store_id, 'is_send' => 0])->sum('total_profit'));
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $list,
- 'count' => $count,
- ]);
- }
- public function actionOtherInfo()
- {
- // 获取门店设置
- $form = new MdSettingForm();
- $form->store_id = get_store_id();
- $result = $form->getSetting();
- return $this->asJson([
- 'code' => 0,
- 'data' => $result
- ]);
- }
- public function actionSubmitOtherInfo()
- {
- // 保存门店设置
- $form = new MdSettingForm();
- $form->store_id = get_store_id();
- $form->scenario = 'save';
- $form->attributes = all_params();
- $result = $form->saveSetting();
- return $this->asJson($result);
- }
- public function actionCashList()
- {
- $export = get_params('export');
- $md_id = get_params('md_id');
- $start_time = get_params('start_time');
- $end_time = get_params('end_time');
- $status = get_params('status');
- $type = get_params('type');
- $store_id = get_store_id();
- $query = Cash::find()->alias('c')
- ->leftJoin(['md' => Md::tableName()], 'md.id=c.md_id')
- ->where(['c.is_delete' => Cash::IS_DELETE_NO, 'c.store_id' => $store_id])
- ->andWhere(['c.cash_type' => 2]);
- if ($start_time) {
- $query->andWhere(['>=', 'c.created_at', strtotime($start_time)]);
- }
- if ($end_time) {
- $query->andWhere(['<=', 'c.created_at', strtotime($end_time)]);
- }
- if ($type != -1 && isset($type)) {
- $query->andWhere(['c.type' => $type]);
- }
- if ($md_id) {
- $query->andWhere(['c.md_id' => $md_id]);
- }
- if ($status == Cash::STATUS_APPLY) { //待审核
- $query->andWhere(['c.status' => Cash::STATUS_APPLY]);
- }
- if ($status == Cash::STATUS_CONFIRM) { // 待打款
- $query->andWhere(['c.status' => Cash::STATUS_CONFIRM]);
- }
- if ($status == Cash::STATUS_GIVEN) { // 已打款
- $query->andWhere(['in', 'c.status', [Cash::STATUS_GIVEN, Cash::STATUS_RECHARGE]]);
- }
- if ($status == Cash::STATUS_REFUSE) { // 已拒绝
- $query->andWhere(['c.status' => Cash::STATUS_REFUSE]);
- }
- $query->distinct()->orderBy('c.status ASC,c.created_at DESC')->select([
- 'c.*','md.name md_name', 'md.cover_url', 'md.is_single'
- ]);
- $pagination = pagination_make($query);
- $list = $pagination['list'];
- foreach($list as &$value){
- $value['service_money'] = $value['service_charge'] * $value['price'] / 100;
- $value['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
- $value['updated_at'] = !empty($value['updated_at']) ? date('Y-m-d H:i:s', $value['updated_at']) : '';
- $value['money'] = Cash::getServiceMoney($value);
- $value['status_name'] = Cash::getCashStatusName($value);
- $saas_user = SaasUser::findOne(['mobile' => $value['binding']]);
- $value['avatar_url'] = !empty($saas_user['avatar']) ? $saas_user['avatar'] : $value['avatar_url'];
- $value['card_no'] = '';
- if (intval($value['type']) === Cash::TYPE_BANK) {
- if ($saas_user->withdraw_method) {
- $withdraw_method = json_decode($saas_user->withdraw_method, true);
- if (!empty($withdraw_method)) {
- foreach ($withdraw_method as $method_item) {
- if ($method_item['type'] === 'bank_card') {
- $value['card_no'] = $method_item['card_no'];
- }
- }
- }
- }
- }
- $value['type_text'] = Cash::getTypeName($value['type']) . "提现";
- }
- if($export){
- return $this->exportCashList($list);
- }
- $md = Md::findOne($md_id);
- $profit = [
- 'cash_profit' => '0.00',
- 'profit' => '0.00',
- 'total_profit' => '0.00',
- ];
- if ($md) {
- //可提现金额
- $profit['cash_profit'] = $md->cash_profit ?: '0.00';
- //已提现金额
- $saas_user_id = MdStaff::findOne(['store_id' => $store_id, 'md_id' => $md_id, 'is_manager' => 1])->saas_user_id;
- $profit['profit'] = Cash::find()->where(['user_id' => $saas_user_id, 'status' => [0, 1, 2],
- 'md_id' => $md_id, 'cash_type' => 2])->sum('price') ?: '0.00';
- //累计金额
- $profit['total_profit'] = $md->total_profit ?: '0.00';
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list,
- 'profit' => $profit,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount']
- ],
- ]);
- }
- private function exportCashList($list)
- {
- $rows = [[
- 'ID',
- '提现方式',
- '提现金额(元)',
- '申请时间',
- '审批时间',
- '状态',
- ]];
- foreach($list as $item){
- $r = [
- $item['id'],
- $item['type'] == 0 ? '微信转账' : $item['type'] == 1 ? '支付宝转账' : '银行卡转账',
- $item['price'],
- date('Y-m-d H:i:s', $item['created_at']),
- $item['updated_at'] ? date('Y-m-d H:i:s', $item['updated_at']) : '-',
- $item['status'] == 0 ? '未处理' : $item['status'] == 1 ? '待打款' : $item['status'] == 2 ? '已完成' : $item['status'] == 3 ? '已拒绝' : '',
- ];
- $rows[] = $r;
- }
- $writer = \Spatie\SimpleExcel\SimpleExcelWriter::streamDownload(time() . '.xlsx')->noHeaderRow()
- ->addRows($rows)->toBrowser();
- }
- /**
- * 提现申请审核
- * @return \yii\web\Response
- */
- public function actionCashApply()
- {
- $id = post_params('id');
- $status = post_params('status');
- $store_id = get_store_id();
- if (empty($id) || !is_array($id)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '数据格式错误'
- ]);
- }
- $t = \Yii::$app->db->beginTransaction();
- foreach ($id as $value) {
- $cash = Cash::findOne(['id' => $value, 'is_delete' => Cash::IS_DELETE_NO,
- 'store_id' => $store_id]);
- if (!$cash) {
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => '提现记录不存在,请刷新重试'
- ]);
- }
- if (\in_array($cash->status, [1, 3])) {
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => '操作失败,请刷新重试'
- ]);
- }
- if (!$cash->order_no) {
- $order_no = null;
- while (true) {
- $order_no = date('YmdHis') . mt_rand(100000, 999999);
- $exist_order_no = Cash::find()->where(['order_no' => $order_no])->exists();
- if (!$exist_order_no) {
- break;
- }
- }
- $cash->order_no = $order_no;
- $cash->save();
- }
- \Yii::$app->cache->set('md_cash_cache_' . $value, $cash->order_no);
- if (!in_array($status, [1, 3])) {
- \Yii::$app->cache->set('md_cash_cache_' . $value, false);
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => '提现记录ID: ' . $value . '已审核,请刷新重试'
- ]);
- }
- $cash->status = $status;
- if ($status == Cash::STATUS_REFUSE) {
- $md = Md::findOne($cash->md_id);
- $md->updateCounters(['cash_profit' => $cash->price]);
- }
- if (!$cash->save()) {
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => '网络异常,请刷新重试'
- ]);
- }
- }
- $t->commit();
- return $this->asJson([
- 'code' => 0,
- 'msg' => '审核成功'
- ]);
- }
- public function actionCashConfirm()
- {
- $id = post_params('id');
- $status = post_params('status');
- $store_id = get_store_id();
- $store = Store::findOne($store_id);
- $cash = Cash::findOne([
- 'id' => $id,
- 'is_delete' => Cash::IS_DELETE_NO,
- 'store_id' => $store_id
- ]);
- if (!$cash) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '提现记录不存在,请刷新重试'
- ]);
- }
- if ($status == Cash::STATUS_GIVEN) {
- if($store->is_platform_transfers == 1){
- $store_wechat_cash = json_decode(Option::get('store_wechat_cash', $store_id, 'store')['value'], true);
- $val = 0;
- foreach ((array)$store_wechat_cash as $value) {
- if (!empty($value)) {
- $val = 1;
- break;
- }
- }
- if(!$val){
- return $this->asJson([
- 'code' => 1,
- 'msg' => '未配置平台微信提现设置'
- ]);
- }
- }
- }
- if (!$cash->order_no) {
- $order_no = null;
- while (true) {
- $order_no = date('YmdHis') . mt_rand(100000, 999999);
- $exist_order_no = Cash::find()->where(['order_no' => $order_no])->exists();
- if (!$exist_order_no) {
- break;
- }
- }
- $cash->order_no = $order_no;
- $cash->save();
- }
- if ($cash->status != 1) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '操作错误,请刷新重试'
- ]);
- }
- $res = [];
- // $price = $cash->price;
- $price = Cash::getServiceMoney($cash);
- $servePrice = ($cash->price * ($cash->service_charge / 100));
- if ($store->store_balance < $price && $store->is_platform_transfers == 1) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '店铺剩余提现余额不足'
- ]);
- }
- $wechat_type = 1;
- $wechat_cash = Option::get('wechat_cash', $store_id, 'store')['value'];
- if($store->is_platform_transfers == 1){
- $wechat_cash = Option::get('store_wechat_cash', $store_id, 'store')['value'];
- $wechat_type = 0;
- }
- $t = \Yii::$app->db->beginTransaction();
- if ($status == Cash::STATUS_GIVEN) { //微信自动打款
- $cash->status = Cash::STATUS_GIVEN;
- $cash->pay_time = time();
- $cash->pay_type = Cash::PAY_TYPE_WX;
- $cash->wx_cash_status = -1;
- $user = User::findOne(['id' => $cash->user_id]);
- $data = [
- 'partner_trade_no' => $cash->order_no,
- 'openid' => $user->wechat_open_id,
- 'check_name' => 'NO_CHECK',
- 'amount' => $price * 100,
- 'desc' => '转账',
- 'user_name' => $cash->name
- ];
- if ((int)$cash->type === 0) {
- if (\Yii::$app->prod_is_dandianpu() && !\app\models\Store::hasIncoming($store_id)) {
- //商城是否进件
- //商城余额是否充足
- $storeMoney = \app\models\StoreCash::getMaxCash($store);
- if($storeMoney < $price){
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => '操作错误,商城余额不足! 当前余额:¥' . $storeMoney
- ]);
- }
- //扣除商城余额
- $subMoney = \app\models\Store::subMoney($store, $price);
- if(!$subMoney){
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => '网络异常,subMoney失败1',
- ]);
- }
- $cash->save();
- $t->commit();
- return $this->asJson([
- 'code' => 0,
- 'msg' => '成功'
- ]);
- } else {
- if (\Yii::$app->prod_is_duli()) {
- $WechatConfig = WechatConfig::findOne(['store_id' => get_store_id()]);
- if (empty($WechatConfig->mch_id) || empty($WechatConfig->pay_key) || empty($WechatConfig->app_id)|| empty($WechatConfig->cert_pem)|| empty($WechatConfig->key_pem)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => "后台参数配置错误,请检查参数后重试"
- ]);
- }
- }
- }
- if ($wechat_type == 0) {
- $cashExt = CashExt::findOne(['cash_id' => $cash->id, 'cash_price_type' => CashExt::CASH_PRICE_TYPE_AMOUNT]);
- $before = $store->store_balance;
- $real_price = $cashExt ? $cashExt->real_price : $price;
- $store->updateCounters(['store_balance' => -$real_price, 'price' => -$real_price, 'store_withdrawn_cash' => $real_price]);
- $after = $store->store_balance;
- $cashExtServePrice = $cashExt ? bcsub($cashExt->price, $cashExt->real_price, 2) : $servePrice;
- $cashExtPrice = $cashExt ? $cashExt->price : $cash->price;
- $cashExtRealPrice = $cashExt ? $cashExt->real_price : $price;
- StoreMoneyLog::saveLog(get_store_id(), StoreMoneyLog::LOG_TYPE_EXPEND, StoreMoneyLog::TYPE_SHARE, $cashExtRealPrice, " ID{$id}:用户提现{$cashExtPrice}元,扣除手续费{$cashExtServePrice}元,提现实际扣除{$cashExtRealPrice}元", $before, $after, $wechat_type);
- }
- $wechat = \Yii::$app->controller->wechatPay;
- // $res = $wechat->transfer->toBalance($data);
- $res = (new \app\utils\WechatMerchant\WxV3($wechat))->transferBatches(get_store_id(), $data);
- //判断是否使用新版本转账 增加转账标识
- $wechat_cash = json_decode($wechat_cash, true);
- if (intval($wechat_cash['is_open']) === 2) {
- if (!$res['code']) {
- $cash->is_platform_transfers = intval($store->is_platform_transfers);
- $cash->wx_cash_type = Cash::WX_CASH_TYPE_NEW;
- $cash->wx_cash_state = $res['data']['state'];
- $cash->wx_cash_result_info = json_encode($res['data'], JSON_UNESCAPED_UNICODE);
- $cash->save();
- }
- }
- NoticeSend::CashSuccess($cash->user_id, $user->binding, $price, '微信自动打款', ($cash->price - $price));
- } elseif ((int)$cash->type === 1) {
- $order = (object)[
- 'store_id' => $cash->store_id,
- 'order_no' => $cash->order_no,
- 'pay_price' => sprintf("%.2f", $price),
- 'name' => $cash->name
- ];
- $result = Alipay::transfer($order, $cash->mobile);
- if (isset($result['code']) && $result['code'] == 1) {
- if (strpos($result['msg'], 'aop.invalid-app-auth-token-no-api')) {
- $result['msg'] = '接口未授权,请前往支付宝开放平台查询是否开通产品或授权支付宝转账产品';
- }
- if (strpos($result['msg'], 'PAYEE_NOT_EXIST')) {
- $result['msg'] = '收款账号不存在或姓名有误,建议核实账号和姓名是否准确';
- }
- if (strpos($result['msg'], 'BALANCE_IS_NOT_ENOUGH')) {
- $result['msg'] = '商户余额不足';
- }
- return $this->asJson($result);
- } else {
- // TODO 扣除店铺提现金额
- if ($wechat_type == 0) {
- $cashExt = CashExt::findOne(['cash_id' => $cash->id, 'cash_price_type' => CashExt::CASH_PRICE_TYPE_AMOUNT]);
- $before = $store->store_balance;
- $real_price = $cashExt ? $cashExt->real_price : $price;
- $store->updateCounters(['store_balance' => -$real_price, 'price' => -$real_price, 'store_withdrawn_cash' => $real_price]);
- $after = $store->store_balance;
- $cashExtServePrice = $cashExt ? bcsub($cashExt->price, $cashExt->real_price, 2) : $servePrice;
- $cashExtPrice = $cashExt ? $cashExt->price : $cash->price;
- $cashExtRealPrice = $cashExt ? $cashExt->real_price : $price;
- StoreMoneyLog::saveLog(get_store_id(), StoreMoneyLog::LOG_TYPE_EXPEND, StoreMoneyLog::TYPE_SHARE, $cashExtRealPrice, " ID{$id}:用户提现{$cashExtPrice}元,扣除手续费{$cashExtServePrice}元,提现实际扣除{$cashExtRealPrice}元", $before, $after, $wechat_type);
- }
- }
- $cash->save();
- $t->commit();
- return $this->asJson([
- 'code' => 0,
- 'msg' => '成功'
- ]);
- }elseif ((int)$cash->type === 4) {
- $user = User::findOne($cash->user_id);
- $saas = SaasUser::findOne(['mobile' => $user->binding]);
- $lg_info = Lg::find()->where(['user_id'=>$saas->id,'status'=>1,'is_delete'=>0])->one();
- $lgApi = new LgApi($cash->store_id);
- //灵工提现
- $post_data = [
- 'store_id' => $cash->store_id,
- 'outTradeNo' => $cash->order_no,//唯一批次号
- 'accNo' => bcmul($price,100),
- 'amt' => bcmul($price,100),
- 'name' => $lg_info->name,
- 'certCard' => $lg_info->cert_card//身份证号
- ];
- $result = $lgApi->FlexiblePay($post_data);
- if (isset($result['status']) && $result['status'] == 999) {
- return $this->asJson($result);
- }
- $cash->status = 6;//灵工待打款
- $cash->save();
- $t->commit();
- //灵工提现接口调用后消息队列查询状态
- \queue_push(new LgCashJob(['id'=>$cash->id,'store_id'=>$store_id,'type'=>0,'retry' => 5]), 60);
- return $this->asJson([
- 'code' => 0,
- 'msg' => '成功'
- ]);
- }
- } elseif ($status == Cash::STATUS_HAND) { //手动打款
- // TODO 扣除店铺提现金额
- if ($wechat_type == 0) {
- $before = $store->store_balance;
- $store->updateCounters(['store_balance' => -$price, 'price' => -$price, 'store_withdrawn_cash' => $price]);
- $after = $store->store_balance;
- StoreMoneyLog::saveLog(get_store_id(), StoreMoneyLog::LOG_TYPE_EXPEND, StoreMoneyLog::TYPE_SHOP, $price, " ID{$id}:用户提现{$cash->price}元,扣除手续费{$servePrice}元,提现实际扣除{$price}元", $before, $after, $wechat_type);
- }
- $cash->status = Cash::STATUS_GIVEN;
- $cash->pay_time = time();
- $cash->pay_type = Cash::PAY_TYPE_HAND;
- $res['result_code'] = "SUCCESS";
- // NoticeSend::CashSuccess($cash->user_id, $user->binding, $price, '手动打款', ($cash->price - $price));
- }
- if (isset($res['result_code']) && $res['result_code'] == 'SUCCESS') {
- $cash->save();
- if ($cash->cash_type == 2){
- //灵工提现接口调用后消息队列查询状态
- \queue_push(new LgCashJob(['id'=>$cash->id,'store_id'=>$store_id,'type'=>2,'retry' => 5]), 60);
- }
- if (intval($cash->type) === Cash::TYPE_RECHARGE) {
- AccountLog::saveLog($cash->user_id, $price, 2, 1, 0, 0, '佣金提现打款');
- }
- $t->commit();
- return $this->asJson([
- 'code' => 0,
- 'msg' => '成功'
- ]);
- } else {
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => !empty($res['err_code_des']) ? $res['err_code_des'] : '请稍后重试',
- 'data' => $res
- ]);
- }
- }
- public function actionCashSubmit()
- {
- $price = post_params('cash');
- $type = post_params('type');
- $name = post_params('name', '');
- $account = post_params('account', '');
- $bank = post_params('bank', '');
- if (!$price || !in_array($type, [0, 1, 2])) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '参数非法',
- ]);
- }
- if (empty($name) || empty($account)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '账户和姓名不能为空',
- ]);
- }
- if ($type == 2 && empty($bank)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '银行账号开户行不能为空',
- ]);
- }
- $store_id = get_store_id();
- $md_id = get_md_id();
- $saas_user_id = MdStaff::findOne(['store_id' => $store_id, 'md_id' => $md_id, 'is_manager' => 1])->saas_user_id;
- $md = Md::findOne($md_id);
- if ($price > $md->cash_profit) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '当前大于可提现金额'
- ]);
- }
- $exit = Cash::find()->andWhere(['=', 'status', 0])->andWhere(['user_id' => $saas_user_id,
- 'store_id' => $store_id, 'cash_type' => Cash::IS_CASH_TYPE_MD])->exists();
- if ($exit) {
- // return $this->asJson([
- // 'code' => 1,
- // 'msg' => '尚有未完成的提现申请'
- // ]);
- }
- $saasUser = SaasUser::findOne($saas_user_id);
- $user = User::find()->where(['store_id' => $store_id,'binding' => $saasUser->mobile,'is_delete' => 0])->one();
- if(empty($user)){
- return $this->asJson([
- 'code' => 1,
- 'msg' => '提现用户数据不能为空'
- ]);
- }
- $t = \Yii::$app->db->beginTransaction();
- $cash = new Cash();
- $cash->order_no = OrderNo::getOrderNo(OrderNo::ORDER_CASH);
- $cash->is_delete = 0;
- $cash->status = 0;
- $cash->md_id = $md->id;
- $cash->price = $price;
- $cash->created_at = time();
- $cash->user_id = $user->id;
- $cash->store_id = $store_id;
- $cash->type = $type;
- $cash->name = $name;
- $cash->mobile = $account;
- $cash->bank_name = $bank;
- $cash->pay_time = 0;
- $cash->service_charge = 0;
- $cash->cash_type = Cash::IS_CASH_TYPE_MD;
- if ($cash->save()) {
- $md->updateCounters(['cash_profit' => -$price]);
- $t->commit();
- return $this->asJson([
- 'code' => 0,
- 'msg' => '申请成功'
- ]);
- } else {
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => '网络异常'
- ]);
- }
- }
- public function actionClerkOrderList()
- {
- $export = get_params('export');
- $md_id = get_params('md_id');
- $start_time = get_params('start_time');
- $end_time = get_params('end_time');
- $status = get_params('status');
- $order_no = get_params('order_no');
- $name = get_params('name');
- $store_id = get_store_id();
- $query = MdProfit::find()->alias('mp')->where(['mp.is_clerk' => 1, 'mp.store_id' => $store_id])
- ->leftJoin(['ms' => MdStaff::tableName()], 'ms.id=mp.clerk_id')
- ->leftJoin(['o' => Order::tableName()], 'o.id=mp.order_id');
- if ($start_time) {
- $query->andWhere(['>=', 'mp.created_at', strtotime($start_time)]);
- }
- if ($order_no) {
- $query->andWhere(['like', 'o.order_no', $order_no]);
- }
- if ($name) {
- $query->andWhere(['like', 'ms.name', $name]);
- }
- if ($end_time) {
- $query->andWhere(['<=', 'mp.created_at', strtotime($end_time)]);
- }
- if ($md_id) {
- $query->andWhere(['mp.md_id' => $md_id]);
- }
- if ($status != -1) {
- $query->andWhere(['mp.is_send' => $status]);
- }
- $query->orderBy('mp.created_at desc, mp.updated_at desc')->select('mp.*, ms.name, o.order_no, o.pay_price');
- $pagination = pagination_make($query);
- $list = $pagination['list'];
- foreach ($list as &$value) {
- $value['goods_list'] = OrderDetail::find()->where(['order_id' => $value['order_id']])->select('goods_name, num, attr, pic, total_price')->asArray()->all();
- }
- if($export){
- return $this->exportClerkOrderList($list);
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount']
- ],
- 'q' => $query->createCommand()->getRawSql(),
- ]);
- }
- private function exportClerkOrderList($list)
- {
- $rows = [[
- 'ID',
- '订单编号',
- '产品名称',
- '规格',
- '单价(元)',
- '数量',
- '订单金额(元)',
- '核销员工',
- '下单时间',
- '核销时间',
- '订单状态',
- ]];
- foreach($list as $item){
- $r = [
- $item['id'],
- $item['order_no'],
- $item['goods_list'][0]['goods_name'],
- implode(',', array_column(json_decode($item['goods_list'][0]['attr'], true), 'attr_name')),
- $item['goods_list'][0]['total_price'],
- $item['goods_list'][0]['num'],
- $item['pay_price'],
- $item['name'],
- date('Y-m-d H:i:s', $item['created_at']),
- $item['updated_at'] ? date('Y-m-d H:i:s', $item['updated_at']) : '-',
- $item['is_send'] == 0 ? '待核销':'已核销',
- ];
- $rows[] = $r;
- if(count($item['goods_list']) > 1){
- foreach($item['goods_list'] as $i => $goods){
- if($i == 0) continue;
- $r = [
- '',
- $item['goods_list'][$i]['goods_name'],
- implode(',', array_column(json_decode($item['goods_list'][$i]['attr'], true), 'attr_name')),
- $item['goods_list'][$i]['total_price'],
- $item['goods_list'][$i]['num'],
- ];
- $rows[] = $r;
- }
- }
- }
- $writer = \Spatie\SimpleExcel\SimpleExcelWriter::streamDownload(time() . '.xlsx')->noHeaderRow()
- ->addRows($rows)->toBrowser();
- }
- /**
- * 门店团购活动列表
- * @return \yii\web\Response
- * @author: hankaige
- * @Time: 2024/3/20 10:02
- */
- public function actionGroupActivities()
- {
- $form = new MdGroupActivitiesForm();
- $form->store_id = get_store_id();
- $form->attributes = get_params();
- return $this->asJson($form->list());
- }
- /**
- * 门店团购活动详情
- * @return \yii\web\Response
- * @author: hankaige
- * @Time: 2024/3/20 10:02
- */
- public function actionGetGroupActivitiesItem()
- {
- $form = new MdGroupActivitiesForm();
- $form->store_id = get_store_id();
- $form->id = get_params('id');
- return $this->asJson($form->item());
- }
- /**
- * 设置门店团购活动
- * @return \yii\web\Response
- * @author: hankaige
- * @Time: 2024/3/20 10:02
- */
- public function actionSaveGroupActivitiesItem()
- {
- $form = new MdGroupActivitiesForm();
- $form->store_id = get_store_id();
- $form->scenario = 'create';
- $form->attributes = all_params();
- return $this->asJson($form->createItem());
- }
- /**
- * 删除门店团购活动
- * @return \yii\web\Response
- * @author: hankaige
- * @Time: 2024/3/20 10:47
- */
- public function actionDelGroupActivitiesItem()
- {
- $form = new MdGroupActivitiesForm();
- $form->store_id = get_store_id();
- $form->ids = get_params('ids');
- $form->scenario = 'del';
- return $this->asJson($form->del());
- }
- public function actionGetQrcodeSetting()
- {
- //兼容联盟
- $is_saas = get_params('is_saas',0);
- if($is_saas){
- $store_id = -1;
- }else{
- $store_id = get_store_id();
- }
- $qrcode = Qrcode::findOne(['store_id' => $store_id, 'is_delete' => 0, 'type' => Qrcode::TYPE_MD]);
- // \Yii::error($qrcode->font);
- $color = Color::find()->select('id, color')->andWhere(['is_delete' => 0])->asArray()->all();
- $font_position = json_decode($qrcode->font_position, true);
- $qrcode_position = json_decode($qrcode->qrcode_position, true);
- $avatar_position = json_decode($qrcode->avatar_position, true);
- $avatar_size = json_decode($qrcode->avatar_size, true);
- $qrcode_size = json_decode($qrcode->qrcode_size, true);
- $font_size = json_decode($qrcode->font, true);
- // \Yii::error($font_size);
- $first = Color::findOne(['color' => $font_size['color']]);
- $res = [
- 'qrcode_bg' => $qrcode->qrcode_bg ?: \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/2_1.png',
- 'qrcode_pic' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/1.png',
- 'avatar' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png',
- 'qrcode' => $qrcode,
- 'color' => $color,
- 'first' => $first->id,
- 'font_c' => Json::decode($qrcode->font, true)['color'] ? Json::decode($qrcode->font, true)['color'] : ($first->color ?: "#000"),
- 'avatar_w' => $avatar_size['w'] ?: '63',
- 'avatar_x' => $avatar_position['x'] ?: '50',
- 'avatar_y' => $avatar_position['y'] ?: '160',
- 'qrcode_w' => $qrcode_size['w'] ?: '169',
- 'qrcode_c' => empty($qrcode_size['c']) ? 1 : (($qrcode_size['c'] == "true") ? 1 : 0),
- 'qrcode_x' => $qrcode_position['x'] ?: '65',
- 'qrcode_y' => $qrcode_position['y'] ?: '240',
- 'font_x' => $font_position['x'] ?: '124',
- 'font_y' => $font_position['y'] ?: '180',
- 'font_w' => $font_size['size'] ?: '20',
- ];
- return $this->asJson(['code' => 0, 'msg' => 'success', 'data' => $res]);
- }
- /**
- * 设置门店海报
- * @return \yii\web\Response
- * @author: hankaige
- * @Time: 2024/3/25 10:36
- */
- public function actionSetQrcodeSetting()
- {
- $store_id = get_store_id();
- //兼容联盟
- $is_saas = post_params('is_saas',0);
- if($is_saas){
- $store_id = -1;
- }
- $qrcode = Qrcode::findOne(['store_id' => $store_id, 'is_delete' => 0, 'type' => Qrcode::TYPE_MD]);
- if (!$qrcode) {
- $qrcode = new Qrcode();
- }
- $form = new QrcodeForm();
- $form->attributes = post_params();
- $form->store_id = $store_id;
- $form->qrcode = $qrcode;
- $form->type = Qrcode::TYPE_MD;
- return $this->asJson($form->save());
- }
- /**
- * 司机列表
- * @return \yii\web\Response
- * @author: hankaige
- * @Time: 2024/3/22 15:57
- */
- public function actionDriverList()
- {
- $form = new MdGroupDriverForm();
- $form->store_id = get_store_id();
- $form->attributes = all_params();
- return $this->asJson($form->getList());
- }
- /**
- * 新增编辑司机
- * @return \yii\web\Response
- * @author: hankaige
- * @Time: 2024/3/22 15:57
- */
- public function actionDriverSave()
- {
- $form = new MdGroupDriverForm();
- $form->scenario = 'save';
- $form->store_id = get_store_id();
- $form->attributes = all_params();
- return $this->asJson($form->createItem());
- }
- /**
- * 删除司机
- * @return \yii\web\Response
- * @author: hankaige
- * @Time: 2024/3/22 16:02
- */
- public function actionDriverDel()
- {
- $form = new MdGroupDriverForm();
- $form->scenario = 'del';
- $form->store_id = get_store_id();
- $form->id = get_params('id');
- return $this->asJson($form->delItem());
- }
- /**
- * diy装修获取活动列表
- * @return \yii\web\Response
- * @author: hankaige
- * @Time: 2024/3/25 11:47
- */
- public function actionGetActivities()
- {
- $form = new MdGroupActivitiesForm();
- $form->store_id = get_store_id();
- return $this->asJson($form->diyGetActivitiesList());
- }
- /**
- * diy获取指定活动商品列表
- * @return \yii\web\Response
- * @author: hankaige
- * @Time: 2024/3/25 11:47
- */
- public function actionGetActivityGoodsList()
- {
- $form = new MdGroupActivitiesForm();
- $form->store_id = get_store_id();
- $form->id = get_params('id');
- return $this->asJson($form->getGoodsListByActivity());
- }
- /**
- * 生成备货单操作
- * @return \yii\web\Response
- * @author: hankaige
- * @Time: 2024/3/27 11:07
- */
- public function actionCreateInventory()
- {
- $form = new InventoryForm();
- $form->store_id = get_store_id();
- $form->attributes = all_params();
- return $this->asJson($form->handleInventory());
- }
- /**
- * 备货单列表
- * @return \yii\web\Response
- * @author: hankaige
- * @Time: 2024/3/27 11:29
- */
- public function actionInventoryList()
- {
- $form = new InventoryForm();
- $form->store_id = get_store_id();
- return $this->asJson($form->getInventoryList());
- }
- /**
- * 获取备货单详情
- * @return \yii\web\Response
- * @author: hankaige
- * @Time: 2024/3/27 11:29
- */
- public function actionInventoryDetail()
- {
- $form = new InventoryForm();
- $form->store_id = get_store_id();
- $form->id = get_params('id');// 备货单ID
- return $this->asJson($form->getInventoryDetail());
- }
- /**
- * 分拣单列表
- * @return \yii\web\Response
- * @author: hankaige
- * @Time: 2024/3/27 11:29
- */
- public function actionSortingList()
- {
- $form = new InventoryForm();
- $form->store_id = get_store_id();
- $form->md_id = get_params('md_id',0);// 根据门店搜索分拣单
- $form->inventory_id = get_params('inventory_id',0);// 备货单iD
- $form->status = get_params('status',-1);
- $form->md_id = get_params('md_id',0);
- return $this->asJson($form->getSortingList());
- }
- /**
- * 获取分拣单详情
- * @return \yii\web\Response
- * @author: hankaige
- * @Time: 2024/3/27 11:29
- */
- public function actionSortingDetail()
- {
- $form = new InventoryForm();
- $form->store_id = get_store_id();
- $form->id = get_params('id');// 分拣单ID
- return $this->asJson($form->getSortingDetail());
- }
- /**
- * 获取备货单的所有配送单
- * @return \yii\web\Response
- * @author: hankaige
- * @Time: 2024/3/28 15:11
- */
- public function actionSortingDetailAll()
- {
- $form = new InventoryForm();
- $form->store_id = get_store_id();
- $form->id = get_params('id');// 备货单ID
- return $this->asJson($form->getSortingDetailAll());
- }
- /**
- * 获取可用司机列表
- * @return \yii\web\Response
- * @author: hankaige
- * @Time: 2024/3/28 15:12
- */
- public function actionGetDriverList()
- {
- $form = new MdGroupDriverForm();
- $form->store_id = get_store_id();
- $form->keywords = get_params('keyword','');
- $form->status = 1;
- return $this->asJson($form->getList());
- }
- /**
- * 配送单分配司机
- * @return \yii\web\Response
- * @author: hankaige
- * @Time: 2024/3/28 15:12
- */
- public function actionSetDriver()
- {
- $form = new InventoryForm();
- $form->store_id = get_store_id();
- $form->driver_id = post_params('driver_id',0);
- $form->sorting_id = post_params('sorting_id',[]);
- return $this->asJson($form->setDriver());
- }
- /**
- * 打印提货单信息
- * @return \yii\web\Response
- * @author: hankaige
- * @Time: 2024/3/28 15:54
- */
- public function actionPrintTicket()
- {
- $form = new InventoryForm();
- $form->store_id = get_store_id();
- $form->inventory_id = get_params('id',0);
- return $this->asJson($form->printTicket());
- }
- /**
- * 获取自提门店选择列表
- * @return \yii\web\Response
- * @author: hankaige
- * @Time: 2024/4/26 15:19
- */
- public function actionGetSelectShopList()
- {
- $query = Md::find()->where(['store_id'=>get_store_id(),'is_single'=>0,'is_delete' => 0])->select('id,name,cover_url');
- $keyword = get_params('keyword','');
- if(!empty($keyword)){
- $query->andWhere(['like','name',$keyword]);
- }
- $result = pagination_make($query);
- $result['data'] = $result['list'];
- unset($result['list']);
- return $this->asJson(['code'=>0,'data'=>$result,'msg'=>'数据获取成功']);
- }
- public function actionSetShopSingle()
- {
- $mdId = get_params('id');
- $md = Md::findOne($mdId);
- if(empty($md)){
- return $this->asJson(['code'=>1,'data'=>[],'msg'=>'门店不存在']);
- }
- $md->is_single = 1;
- if($md->save()){
- return $this->asJson(['code'=>0,'data'=>[],'msg'=>'设置成功']);
- }else{
- return $this->asJson(['code'=>1,'data'=>[],'msg'=>'设置失败']);
- }
- }
- //通过店铺获取门店列表
- public function actionGetStoreMdList() {
- $store_id = get_store_id();
- $type = get_params('type', 0);//0获取所有门店 1排除绑定过仓库的门店
- $field = "id, name";
- $filter_md_id = [];
- switch ($type) {
- case 1:
- $filter_md_id = AgentFrontBind::find()->where([
- 'type' => AgentFrontBind::TYPE_STORE,
- 'type_id' => $store_id,
- 'is_delete' => 0,
- 'status' => 1
- ])->andWhere(['>', 'md_id', 0])->select('md_id')->column();
- }
- return $this->asJson([
- 'code' => 0,
- 'data' => MdForm::getMdList($store_id, $field, $filter_md_id),
- 'msg' => '数据获取成功'
- ]);
- }
- }
|