| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- <?php
- namespace addons\Happiness\common\service;
- use addons\CloudStock\common\logic\order\RestrictPurchases;
- use addons\Community\common\models\CommunityGoods;
- use addons\Community\common\service\SettingService;
- use common\enums\ShippingTypeEnum;
- use common\globals\GlobalInvoke;
- use addons\Happiness\common\logic\order\OrderInitDataLogic;
- use addons\Happiness\common\logic\order\OrderMarketingLogic;
- use common\logic\order\OrderLimitLogic;
- use addons\Happiness\common\logic\order\OrderValidateLogic;
- use common\models\common\Region;
- use common\models\mall\MallAddons;
- use common\models\user\UserAddress;
- use common\models\user\UserPartitionRelationship;
- use common\enums\StatusEnum;
- use common\enums\AddonsEnum;
- use common\enums\AppEnum;
- use common\enums\ClerkEnum;
- use addons\Happiness\common\models\Order;
- use addons\Happiness\common\models\HappinessOrder;
- use addons\Happiness\common\models\HappinessStore;
- use yii\helpers\Json;
- use Exception;
- use Money\Exchange;
- use Swoole\Http\Status;
- use Yii;
- class OrderService extends BaseService
- {
- //生成核销码
- public function getClerkCode($mall_id)
- {
- while (true) {
- $clerk_code = ClerkEnum::getCode(ClerkEnum::HAPPINESS_CLERK_CODE_PRE);
- $res = Order::findOne(['clerk_code' => $clerk_code]);
- if (empty($res)) break;
- }
- if (empty($clerk_code)) throw new \Exception('核销码生成失败');
- return $clerk_code;
- }
- public function preview($form)
- {
- try {
- // 获取收货地址
- $address_info = UserAddress::getUserAddress($this->user_id, $form->data['address_id']);
- $form->address_id = $address_info['id']??0;
- $store_info = HappinessStore::getOne([['id'=>$form->data['store_id'],'mall_id'=>$this->mall_id]], true,
- [], 'id desc', "*");
- if (empty($store_info)) throw new Exception('门店不存在');
- //省市区
- $region=Region::find()->where(['id' => [$store_info['province_id'], $store_info['city_id'],
- $store_info['district_id']]])->select('id,name')->indexBy('id')->asArray()->all();
- $store_info['address']=$region[$store_info['province_id']]['name'].$region[$store_info['city_id']]['name']
- .$region[$store_info['district_id']]['name']
- .$store_info['address'];
- $store_info['intra_city_distribution'] = json_decode($store_info['intra_city_distribution'], true);
- $form->store_info = $store_info;
- $form->store_id = $store_info['id']??0;
- if (empty($address_info)) $address_info = null;
- $orderInitDataLogic = new OrderInitDataLogic();
- $form = $orderInitDataLogic->execute($form, $form->type);
- if ($form === false) throw new Exception($orderInitDataLogic->getError());
- $is_bind_popups = UserPartitionRelationship::getIsNeedPopups($this->mall_id, $this->user_id);
- if($is_bind_popups == 1) throw new Exception('请进入商品详情页,绑定邀请码!');
- // 购买限制验证
- $orderLimitLogic = new OrderLimitLogic();
- $form = $orderLimitLogic->run($form);
- if ($form === false) throw new Exception($orderLimitLogic->getError());
- // 强制免运费
- $form = $this->isForceFreeShipping($form);
- // 营销减免活动
- $orderMarketingLogic = new OrderMarketingLogic();
- $form = $orderMarketingLogic->run($form);
- if ($form === false) throw new Exception($orderMarketingLogic->getError());
- $group_order_goods = $form['group_order_goods'];
- $group_order_goods_list = [];
- $gIds = [];
- foreach ($group_order_goods as $val){
- $group_order_goods_list[]=$val;
- foreach ($val['goods'] as $gId => $item) {
- $gIds[] = $gId;
- }
- }
- $marketing = $form['marketing']??[];
- $pay_type_list = \Yii::$app->services->pay->getEnablePayment($this->mall_id, $this->platform, 1, ['pay_money' => $form['total_pay_money'], 'user_id' => $this->user_id]);
- if($form['total_pay_money']<=0){
- $tmp = [];
- foreach ($pay_type_list as $item){
- if($item['trade_type']=='default'){
- $tmp[]=$item;
- break;
- }
- }
- $pay_type_list = $tmp;
- }
- $shipping_type_list = ShippingTypeEnum::getMap();
- $option_list = [];
- $group_order_goods = $group_order_goods_list;
- $extends_fields = [];
- $info = compact('group_order_goods', 'marketing', 'address_info', 'pay_type_list', 'shipping_type_list', 'option_list', 'extends_fields', 'store_info');
- if($form['total_pay_money'] < 0) $form['total_pay_money'] = 0;
- $info['total_pay_money'] = '¥' . $form['total_pay_money'];
- $info['total_pay_money_not_coupon'] = $form['total_pay_money_not_coupon'];
- $info['is_address'] = $form['is_address'] ?? 0;
- $info['is_virtual'] = $form['is_virtual'] ?? 0;
- $info['is_sign_agreement'] = $form['is_sign_agreement'] ?? 0;
- $info['shipping_type'] = $form->data['shipping_type'] ?? null;
- // 农业补贴 - 是否需要显示推荐人输入框
- if (MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_AGRICULTURAL_SUBSIDIES)) {
- $aConfig = Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_AGRICULTURAL_SUBSIDIES, 'basic');
- if($aConfig && isset($aConfig['goods'])){
- $tGoods = json_decode($aConfig['goods'], true);
- $tGids = array_column($tGoods, 'goods_id');
- $info['show_invite_form'] = StatusEnum::DISABLED;
- if ($aConfig['enable_bind_master'] == StatusEnum::ENABLED && !empty(array_intersect($tGids, $gIds))) {
- $info['show_invite_form'] = StatusEnum::ENABLED;
- }
- }
- // 判断是否包含当前商品
- // $info['show_invite_form'] = Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_AGRICULTURAL_SUBSIDIES, 'basic.enable_bind_master');
- }
- $form->data['type'] = $form->type;
- //物流方式为快递配送,则调用公共调用
- if ($form->shipping_type == 1) {
- $invoke_res = GlobalInvoke::exec(GlobalInvoke::ORDER_PREVIEW_APPEND_FILEDS, $this->mall_id, ['mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'data' => $info, 'params' => $form->data]);
- $info = !empty($invoke_res) ? $invoke_res : $info;
- }
- return $info;
- } catch (\Exception $e) {
- throw new Exception($e->getMessage());
- }
- }
- public function created($form)
- {
- try {
- // 获取收货地址
- $address_info = UserAddress::getUserAddress($this->user_id, $form->data['address_id']);
- //获取门店
- $store_info = HappinessStore::getOne([['id'=>$form->data['store_id'],'mall_id'=>$this->mall_id]], true,
- [], 'id desc', "*");
- if (empty($store_info)) throw new Exception('门店不存在');
- $store_info['intra_city_distribution'] = json_decode($store_info['intra_city_distribution'], true);
- $form->store_info = $store_info;
- $form->store_id = $store_info['id']??0;
- if (empty($address_info)) $address_info = null;
- $orderInitDataLogic = new OrderInitDataLogic();
- $form = $orderInitDataLogic->execute($form, $form->type);
- if ($form === false) throw new Exception($orderInitDataLogic->getError());
- $is_bind_popups = UserPartitionRelationship::getIsNeedPopups($this->mall_id, $this->user_id);
- if($is_bind_popups == 1) throw new Exception('请进入商品详情页,绑定邀请码!');
- // 购买限制验证
- $orderLimitLogic = new OrderLimitLogic();
- $form = $orderLimitLogic->run($form);
- if ($form === false) throw new Exception($orderLimitLogic->getError());
- // 强制免运费
- $form = $this->isForceFreeShipping($form);
- // 数据校验
- $flag = OrderValidateLogic::run($form);
- if (is_string($flag)) throw new Exception($flag);
- // 营销减免活动
- $orderMarketingLogic = new OrderMarketingLogic();
- $form = $orderMarketingLogic->run($form, true);
- if ($form === false) throw new Exception($orderMarketingLogic->getError());
- $goods_ids = array_column($form['order_goods'],'goods_id');
- if(MallAddons::isInstall($this->mall_id,'CloudStock')){
- (new RestrictPurchases())->orderCreate(['user_id' => $this->user_id,'mall_id' => $this->mall_id,'type' => 'order','goods_ids'=>$goods_ids]);
- }
- $need_auth = GlobalInvoke::exec(GlobalInvoke::REAL_AUTH, $this->mall_id, ['user_id' => $this->user_id,'mall_id' => $this->mall_id,'type' => 'order','goods_ids'=>$goods_ids]);
- if($need_auth === true){
- //需要先进行实名认证
- throw new Exception('请先进行实名认证');
- }
- foreach ($form['group_order_goods'] as $key => $value) {
- if ($value['mch_order_info']['num'] == 0) throw new Exception('无可购买商品');
- }
- $delivery_amount = 0;
- if ($form->data['shipping_type'] == 3) {
- if ($form->store_info['intra_city_distribution']['is_alone'] == 1) {
- $delivery_amount = $form->store_info['intra_city_distribution']['initial_delivery_amount'];
- } else if (['intra_city_distribution']['is_alone'] == 0){
- $delivery_amount = $form->store_info['setting']['delivery_amount'];
- }
-
- }
- if ($delivery_amount > $form->total_pay_money) {
- throw new Exception('订单未达起送价,还差'.bcsub($delivery_amount, $form->total_pay_money, 2).'元');
- // throw new Exception('起送金额是'.$delivery_amount.'元');
- }
- $res = Order::createOrder($form);
- if ($res === false) throw new Exception(Order::getStaticError());
- return $res;
- } catch (\Exception $e) {
- throw new Exception($e->getMessage());
- }
- }
- protected function isForceFreeShipping($form)
- {
- // 社区团购 是否强制免运费
- if (!$form->is_force_free_shipping && MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_COMMUNITY) &&
- (new SettingService())->isForceFreeShipping($this->mall_id)) { // 需要处理
- // 判断是否为社区团购订单
- // 这种判断仅限于定制创建的时候
- if ($form->action == AppEnum::ACTION_CREATE && in_array($form->shipping_type,
- [ShippingTypeEnum::PICKUP, ShippingTypeEnum::MERCHANT_DISTRIBUTION]) && !empty($form->extra_info)) {
- $ext = json_decode($form->extra_info, true);
- if (empty($ext['head_id'])) {
- return $form;
- }
- }
- // 如果有安装社区团购 并且
- $community_res = (new CommunityGoods())->orderPreviewAppendFields(['mall_id' => $this->mall_id, 'user_id' => $this->user_id,
- 'data' => [], 'params' => array_merge($form->data, ['type' => $form->type])]);
- $shipping_type = null;
- if (!empty($community_res['option_list']) && is_array($community_res['option_list'])) {
- $option_list = array_values(array_filter($community_res['option_list'], function ($val) {
- return $val['enable'] == StatusEnum::ENABLED;
- }));
- $shipping_type = $option_list[0]['key'] ?? null;
- }
- $shipping_type = $form->data['shipping_type'] ?? $shipping_type; // 如果前端没有传,那么就取默认第一个
- $form->is_force_free_shipping = in_array($shipping_type, [ShippingTypeEnum::PICKUP]);
- }
- return $form;
- }
- //订单详情公共调用
- public function globalOrderDetail($data) {
- try {
- //获取门店订单
- $store_order = HappinessOrder::getOne([
- ['mall_id' => $data['order']['mall_id']],
- ['order_id' => $data['order']['id']],
- ]);
- if (empty($store_order)) {
- $data['order']['is_happiness_order'] = 0;
- return $data['order'];
- }
- $data['order']['show_botton'] = false;
- //获取门店信息
- $store_info = HappinessStore::getOne([
- ['mall_id' => $data['order']['mall_id']],
- ['id' => $store_order->store_id],
- ], true);
- if (!empty($store_info)) $data['order']['store'] = $store_info;
- $data['order']['is_happiness_order'] = 1;
- $data['order']['store_name'] = $store_info['store_name'];
- $data['order']['shop_mobile'] = $store_info['shop_mobile'];
- $data['order']['latitude'] = $store_info['latitude'];
- $data['order']['longitude'] = $store_info['longitude'];
- //省市区
- $region=Region::find()->where(['id' => [$store_info['province_id'], $store_info['city_id'],
- $store_info['district_id']]])->select('id,name')->indexBy('id')->asArray()->all();
- $store_info['address']=$region[$store_info['province_id']]['name'].$region[$store_info['city_id']]['name']
- .$region[$store_info['district_id']]['name']
- .$store_info['address'];
- $data['order']['store_address'] = $store_info['address'];
- if ($data['order']['order_status'] == 3 && $data['order']['shipping_type'] == 3) {
- $data['order']['is_show_return_btn'] = 0;
- foreach ($data['order']['detail'] as $key => $value) {
- $data['order']['detail'][$key]['is_show_return_btn'] = 0;
- }
- }
- return $data['order'];
- } catch (\Exception $e) {
- throw new Exception($e->getMessage());
- }
- }
- public function globalBackendLists($data)
- {
- try {
- $order_ids = array_column($data['order'], 'id');
- //获取幸福小店订单
- $happiness_order = HappinessOrder::lists([
- 'where' => [
- ['order_id' => $order_ids],
- ],
- 'index' => 'order_id'
- ]);
- if (!empty($happiness_order)) {
- foreach ($data['order'] as &$item) {
- if (!empty($happiness_order[$item['id']])) {
- $item['show_botton'] = false;
- } else {
- $item['show_botton'] = true;
- }
- }
- }
- return $data['order'];
- } catch (\Exception $e) {
- throw new Exception($e->getMessage());
- }
- }
- }
|