| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519 |
- <?php
- namespace ghs\controllers;
- use biz\stat\classes\StatOrderCountClass;
- use bizGhs\express\services\DadaExpressServices;
- use bizGhs\order\classes\StockWastageOrderClass;
- use bizHd\purchase\classes\PurchaseClass;
- use bizHd\wx\classes\WxOpenClass;
- use bizGhs\custom\classes\CustomClass;
- use bizGhs\order\classes\OrderClass;
- use bizGhs\order\services\OrderService;
- use bizHd\saas\services\RegionService;
- use bizGhs\product\classes\ProductClass;
- use common\components\dada\dada;
- use common\components\dict;
- use common\components\dateUtil;
- use common\components\freight;
- use common\components\httpUtil;
- use Yii;
- use common\components\util;
- use bizGhs\order\classes\RefundOrderClass;
- class OrderController extends BaseController
- {
- public $guestAccess = ['create-order', 'order-relate', 'fast-pay'];
- //订单列表 ssh 2021.1.20
- public function actionList()
- {
- $get = Yii::$app->request->get();
- $status = $get['status'] ?? 0;
- if (!empty($status)) {
- $where['status'] = $status;
- }
- $where['sjId'] = $this->sjId;
- if (isset($get['shopId'])) {
- $shopId = $get['shopId'] ?? 0;
- if (!empty($shopId)) {
- $where['shopId'] = $this->shopId;
- }
- } else {
- $where['shopId'] = $this->shopId;
- }
- $searchTime = $get['searchTime'] ?? '';
- if (!empty($searchTime)) {
- $startTime = $get['startTime'] ?? '';
- $endTime = $get['endTime'] ?? '';
- $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
- $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
- }
- $name = $get['name'] ?? '';
- if (!empty($name)) {
- //订单编号
- if (strpos($name, 'XSD') === 0) {
- $where['orderSn'] = $name;
- } elseif (preg_match("/^[a-zA-Z\s]+$/", $name)) {
- $where['customNamePy'] = ['like', $name];
- } else {
- $where['customName'] = ['like', $name];
- }
- }
- $respond = OrderClass::getOrderList($where);
- $respond['shop'] = $this->shop;
- $respond['shopExt'] = $this->shopExt;
- util::success($respond);
- }
- //商城下单操作 ssh 2021.1.14
- public function actionCreateOrder()
- {
- $post = Yii::$app->request->post();
- $shopId = $this->shopId;
- $customId = $post['customId'] ?? 0;
- $post['customId'] = $customId;
- //开单必须选客户
- if (empty($customId)) {
- util::fail('请选择客户');
- }
- $custom = CustomClass::getCustom($customId);
- if (empty($custom)) {
- util::fail('请选客户哦');
- }
- CustomClass::valid($custom, $this->shopId);
- $post['ghsId'] = $custom['ghsId'] ?? 0;
- $post['customMobile'] = $custom['mobile'] ?? '';
- $customName = $custom['name'] ?? '';
- $post['customName'] = $customName;
- $post['customNamePy'] = $custom['py'] ?? '';
- $post['customAvatar'] = $custom['shortSmallAvatar'] ?? '';
- $post['shopAdminId'] = $this->shopAdminId;
- $post['province'] = $custom['province'] ?? '';
- $post['city'] = $custom['city'] ?? '';
- $post['dist'] = $custom['dist'] ?? '';
- $post['address'] = $custom['address'] ?? '';
- $post['floor'] = $custom['floor'] ?? '';
- $post['fullAddress'] = $custom['fullAddress'] ?? '';
- $post['showAddress'] = $custom['showAddress'] ?? '';
- $shopAdmin = $this->shopAdmin->attributes;
- $post['shopAdminName'] = $shopAdmin['name'] ?? '';
- $post['sjId'] = $this->sjId;
- $post['shopId'] = $shopId;
- $post['fromType'] = 1;
- $post['expressId'] = $post['expressId'] ?? 0;
- $post['sendCost'] = $post['sendCost'] ?? '0.00';
- //$post['payWay'] = dict::getDict('payWay', 'wxPay');
- //PC端开单
- if (isset($post['clearType'])) {
- $clearType = $post['clearType'];
- if ($clearType == OrderClass::CLEAR_DEBT) {
- $post['debt'] = OrderClass::DEBT_YES;
- $post['payWay'] = dict::getDict('payWay', 'debtPay');
- } else {
- $post['debt'] = OrderClass::DEBT_NO;
- }
- }
- //员工不能开已收款订单
- $debt = $post['debt'] ?? OrderClass::DEBT_YES;
- if ($debt == OrderClass::DEBT_NO) {
- $shopAdmin = $this->shopAdmin;
- if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
- util::fail('只能开欠款订单');
- }
- }
- //PC端开单
- if (isset($post['getType'])) {
- util::fail('PC端开单未开放');
- $getType = $post['getType'];
- //自取
- if ($getType == 1) {
- $post['sendType'] = OrderClass::SEND_TYPE_NO;
- } else {
- //选择配送时,在发货时让商家自己再确认一下用什么方式
- $post['sendType'] = OrderClass::SEND_TYPE_UNKNOWN;
- }
- }
- $productJson = $post['product'] ?? '';
- if (empty($productJson)) {
- util::fail('请选择花材');
- }
- $productList = json_decode($productJson, true);
- if (empty($productList)) {
- util::fail('请选择花材');
- }
- $connection = Yii::$app->db;//事务处理
- $transaction = $connection->beginTransaction();
- try {
- //判断花材有效性
- ProductClass::valid($productList, $this->shopId);
- $post['product'] = $productList;
- $return = OrderService::createOrder($post, $custom);
- $transaction->commit();
- util::success($return);
- } catch (\Exception $e) {
- $transaction->rollBack();
- Yii::error("下单失败原因:" . $e->getMessage());
- util::fail('下单失败');
- }
- }
- //延期支付 ssh 2021.1.19
- public function actionDebt()
- {
- $get = Yii::$app->request->get();
- $id = $get['id'] ?? 0;
- $info = OrderService::getOrderInfo($id);
- OrderClass::valid($info, $this->shopId);
- OrderClass::debt($info, $this->shop);
- util::complete();
- }
- //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
- public function actionWxPay()
- {
- ini_set('date.timezone', 'Asia/Shanghai');
- $post = Yii::$app->request->post();
- $couponId = $post['couponId'] ?? 0;
- $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
- $order = OrderClass::getByOrderSn($orderSn);
- if (empty($order)) {
- util::fail('订单号无效');
- }
- $id = $order['id'];
- //支付前验证订单有效性
- if (isset($order['adminId']) == false || $order['adminId'] != $this->adminId) {
- util::fail('没有权限操作');
- }
- $name = $order['orderName'] ?? '购买商品';
- $totalFee = $order['actPrice'];
- //强制使用小程序的miniOpenId
- $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
- if (empty($openId)) {
- util::fail('没有找到openId');
- }
- $capitalType = dict::getDict('capitalType', 'xhGhsOrder', 'id');
- //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
- $wxPayType = 0;
- if (httpUtil::isMiniProgram()) {
- $wxPayType = 1;
- }
- $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
- //订单30分钟后过期
- $now = time();
- $expireTime = $now + 1800;
- $wx = Yii::getAlias("@vendor/weixin");
- require_once($wx . '/lib/WxPay.Api.php');
- require_once($wx . '/example/WxPay.JsApiPay.php');
- $input = new \WxPayUnifiedOrder();
- $input->SetBody($name);
- $input->SetOut_trade_no($orderSn);
- $input->SetTotal_fee($totalFee * 100);
- $input->SetTime_start(date("YmdHis", $now));
- $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
- $input->SetTime_expire(date("YmdHis", $expireTime));
- $input->SetNotify_url(Yii::$app->params['ghsHost'] . '/notice/wx-callback/');
- $input->SetTrade_type("JSAPI");
- //花卉宝代为申请的微信支付
- //$merchantExtend = $this->sjExtend->attributes;
- $merchantExtend = WxOpenClass::getGhsWxInfo();
- if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
- $input->SetSub_openid($openId);
- } else {
- $input->SetOpenid($openId);
- }
- //强制使用小程序的miniAppId
- $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
- $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
- $tools = new \JsApiPay();
- $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
- $newParams = json_decode($jsApiParameters, true);
- //微信不能修改价格
- $current = date("Y-m-d H:i:s");
- $updateData = ['deadline' => $current, 'modPrice' => 0];
- OrderClass::updateById($id, $updateData);
- util::success($newParams);
- }
- //获取订单详情 ssh 2021.1.21
- public function actionDetail()
- {
- $get = Yii::$app->request->get();
- $id = $get['id'] ?? 0;
- $info = OrderService::getOrderInfo($id, true, true, true, false);
- OrderClass::valid($info, $this->shopId);
- //是否有云打印判断
- $hasCloudPrint = 0;
- $shopExt = $this->shopExt;
- if (isset($shopExt->printSn) && !empty($shopExt->printSn) && isset($shopExt->printKey) && !empty($shopExt->printKey)) {
- $hasCloudPrint = 1;
- }
- $info['hasCloudPrint'] = $hasCloudPrint;
- util::success($info);
- }
- //自取免发货流程处理 ssh 2021.1.22
- public function actionWithoutSend()
- {
- $get = Yii::$app->request->get();
- $id = $get['id'] ?? 0;
- $info = OrderService::getById($id, true);
- OrderClass::valid($info, $this->shopId);
- $purchaseId = $info->purchaseId;
- $purchase = PurchaseClass::getById($purchaseId, true);
- OrderService::withoutSend($info, $purchase);
- util::complete();
- }
- //本店送 ssh 2021.1.24
- public function actionSelfSend()
- {
- $get = Yii::$app->request->get();
- $id = isset($get['id']) ? $get['id'] : 0;
- $info = OrderClass::getById($id, true);
- OrderClass::valid($info->attributes, $this->shopId);
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- OrderClass::selfSend($info);
- $transaction->commit();
- } catch (\Exception $exception) {
- $transaction->rollBack();
- Yii::info("本店送操作报错:" . $exception->getMessage());
- util::fail('操作失败');
- }
- util::complete();
- }
- //运费计算 ssh 2021.1.24
- public function actionFreight()
- {
- //2021.3.28 接入达达
- $get = Yii::$app->request->get();
- $post = Yii::$app->request->post();
- if (empty($get)) {
- $get = $post;
- }
- $orderId = $get['id'] ?? 0; //订单ID
- $customId = $get['customId'] ?? 0;
- if (empty($orderId)) {
- //自定义运费
- //freight::getCost();
- if (empty($customId)) {
- util::fail("请选择客户");
- }
- $customInfo = CustomClass::getById($customId);
- if (empty($customInfo)) {
- util::fail('没有找到客户');
- }
- $shop = $this->shop;
- //花材信息
- $productJson = $get['product'] ?? '';
- // $productJson = '[{"productId":31993,"bigNum":1,"smallNum":0}]';
- if (empty($productJson)) {
- util::fail('请选择花材');
- }
- $productList = json_decode($productJson, true);
- if (empty($productList)) {
- util::fail('请选择花材');
- }
- $productList = ProductClass::mergeItemInfo($productList);
- $weight = 0;
- $price = 0;
- $productData = ProductClass::getProductMapData($productList);
- $bigNum = 0;
- foreach ($productList as $key => $val) {
- $productId = $val['productId'];
- $w = ProductClass::getWeight($val['productId'], $val['bigNum'], $val['smallNum']);
- $bigNum += $val['bigNum'];
- $weight = bcadd($w, $weight, 2);
- $ratio = $productData[$productId]['ratio'] ?? 0;
- //大小数量合并多少扎
- $itemNum = ProductClass::mergeItemNum($val['bigNum'], $val['smallNum'], $ratio);
- //售卖价格
- $itemPrice = $productData[$productId]['price'] ?? 0;
- //合计价格
- $price = bcadd($price, bcmul($itemNum, $itemPrice, 2), 2);
- }
- //没有经纬度的客户运费直接返回空 shish 20210612
- if (empty($customInfo['lat']) || empty($customInfo['long'])) {
- util::success(['sedCost' => '']);
- }
- $cost = freight::getCost($shop->lat, $shop->long, $customInfo['lat'], $customInfo['long'], $weight);
- util::success(['sedCost' => $cost]);
- }
- $province = $get['province'] ?? '';
- $city = $get['city'] ?? '';
- $address = $get['address'] ?? '';
- $floor = $get['floor'] ?? '';
- $customName = $get['customName'] ?? '';
- $customMobile = $get['customMobile'] ?? '';
- $fullAddress = $province . $city . $address . $floor;
- $sendTime = $get['sendTime'] ?? '';
- $lat = $get['lat'] ?? '';
- $lng = $get['long'] ?? '';
- if (empty($lat) || empty($lng) || empty($fullAddress)) {
- util::fail('请填写客户地址');
- }
- ini_set('date.timezone', 'Asia/Shanghai');
- if (!empty($sendTime)) {
- //配送时间不为空
- // 预约发单时间(预约时间unix时间戳(10位),精确到分;整分钟为间隔,并且需要至少提前5分钟预约
- $sendTimeInt = strtotime($sendTime);
- $now = time();
- $diff = $sendTimeInt - $now;
- //因服务器写入时间,改为至少提前6分钟
- if ($diff <= 360) {
- util::fail('配送时间至少提前6分钟');
- }
- } else {
- //默认立即发送
- $sendTime = 0;
- }
- //更新订单表
- $info = OrderService::getOrderInfo($orderId);
- OrderClass::valid($info, $this->shopId);
- if ($sendTime) {
- $sendTime = date('Y-m-d H:i', strtotime($sendTime));
- } else {
- $sendTime = '0000-00-00 00:00:00';
- }
- $expressAddInfo = [
- 'customName' => $customName,
- 'customMobile' => $customMobile,
- 'province' => $province,
- 'city' => $city,
- 'address' => $address,
- 'floor' => $floor,
- 'lat' => $lat,
- 'long' => $lng,
- 'sendTime' => $sendTime,
- ];
- OrderClass::updateCustomExpressInfo($orderId, $expressAddInfo);
- //计算运费
- $info['province'] = $province;
- $info['city'] = $city;
- $info['address'] = $address;
- $info['floor'] = $floor;
- $info['fullAddress'] = $fullAddress;
- $info['lat'] = $lat;
- $info['long'] = $lng;
- $info['sendTime'] = $expressAddInfo['sendTime'];
- $res = DadaExpressServices::queryDeliverFee($info);
- util::success(['sedCost' => $res['fee']]);
- }
- //发快递和第三方配送 ssh 2021.1.24
- public function actionThirdSend()
- {
- $get = Yii::$app->request->get();
- $id = isset($get['id']) ? $get['id'] : 0;
- $info = OrderService::getById($id, true);
- OrderClass::valid($info->attributes, $this->shopId);
- if (empty($info->lat) || empty($info->long) || empty($info->fullAddress)) {
- util::fail('请填写客户地址');
- }
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- $respond = OrderClass::thirdSend($info, $get);
- $transaction->commit();
- util::success($respond);
- } catch (\Exception $exception) {
- $transaction->rollBack();
- Yii::info("发快递操作报错:" . $exception->getMessage());
- util::fail('操作失败' . $exception->getMessage());
- }
- }
- //确认送达 ssh 2021.1.24
- public function actionReach()
- {
- $get = Yii::$app->request->get();
- $id = isset($get['id']) ? $get['id'] : 0;
- $info = OrderClass::getById($id, true);
- OrderClass::valid($info->attributes, $this->shopId);
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- OrderService::reach($info);
- $transaction->commit();
- } catch (\Exception $exception) {
- $transaction->rollBack();
- Yii::info("送达操作报错:" . $exception->getMessage());
- util::fail('操作失败');
- }
- util::complete();
- }
- //下单要用到的相关信息 ssh 2019.12.6
- public function actionOrderRelate()
- {
- $regionTree = RegionService::tree();
- $shop = $this->shop->attributes;
- $freight = ['first' => 5, 'add' => 2];
- $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
- $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'txMapKey' => $mapKey, 'merchant' => $shop];
- util::success($out);
- }
- //客户欠款的订单 ssh 2021.2.4
- public function actionDebtList()
- {
- $id = Yii::$app->request->get('id', 0);
- $info = CustomClass::getCustom($id);
- CustomClass::valid($info, $this->shopId);
- $where = ['customId' => $id, 'debt' => 1];
- $respond = OrderService::getDebtOrderList($where);
- $respond['customInfo'] = $info;
- util::success($respond);
- }
- //更新打印次数
- public function actionAddPrintNum()
- {
- $id = Yii::$app->request->get('id', 0);
- $order = OrderClass::getById($id, true);
- OrderClass::valid($order, $this->shopId);
- $order->printNum += 1;
- $order->save();
- util::complete();
- }
- //打印订单 shish 20210714
- public function actionCloudPrintOrder()
- {
- $id = Yii::$app->request->get('id', 0);
- $order = OrderClass::getById($id, true);
- OrderClass::valid($order, $this->shopId);
- //打印订单
- OrderClass::onlinePrint($order);
- $order->printNum += 1;
- $order->save();
- util::complete();
- }
- }
|