jh-admin/addon/store/event/OrderCreateCommonData.php

210 lines
11 KiB
PHP

<?php
/**
* SaaSMall商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.gobuysaas.com
* 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
* 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
* =========================================================
*/
namespace addon\store\event;
use addon\cashier\model\order\CashierOrderPay;
use app\model\express\Express;
use app\model\express\Local;
use app\model\order\OrderCommon;
use app\model\order\OrderCreateTool;
use app\model\store\Store;
/**
* 订单创建相关
*/
class OrderCreateCommonData
{
use OrderCreateTool;
// 行为扩展的执行入口必须是run
public function handle($params)
{
$type = $params['type'];
$calculate_data = $params['data'];
$store_id = $calculate_data['store_id'] ?? 0;
$site_id = $calculate_data['site_id'];
if ($store_id == 0) return null;
$store_model = new Store();
$store_condition = array(
['store_id', '=', $store_id]
);
$store_info = $store_model->getStoreInfo($store_condition)['data'] ?? [];
$store_is_express = $store_info['is_express'];
$store_is_o2o = $store_info['is_o2o'];
$store_is_pickup = $store_info['is_pickup'];
$store_config_model = new \addon\store\model\Config();
$store_config = $store_config_model->getStoreBusinessConfig($site_id)['data']['value'] ?? [];
$order_model = new OrderCommon();
switch ($type) {
case 'trade':
$shop_goods_list = $calculate_data['shop_goods_list'];
//必须是连锁模式 还需要判断某一个插件是否存在
if ($store_id > 0 && $store_config['store_business'] == 'store') {
foreach ($shop_goods_list['deliver_sort'] as $type) {
// 物流
if ($type == 'express') {
$store_is_express = $store_is_express ?? 0;
if ($store_is_express == 1) {
$title = $shop_goods_list['express_config']['value']['express_name'];
if ($title == '') {
$title = Express::express_type['express']['title'];
}
$express_type[] = ['title' => $title, 'name' => 'express'];
}
}
// 自提
if ($type == 'store') {
$store_is_pickup = $store_is_pickup ?? 0;
if ($store_is_pickup == 1) {
//根据坐标查询门店
$store_model = new Store();
$store_condition = array(
['site_id', '=', $site_id],
['is_pickup', '=', 1],
['status', '=', 1],
['is_frozen', '=', 0],
);
$store_condition[] = ['store_id', '=', $store_id];
$latlng = array(
'lat' => $calculate_data['latitude'],
'lng' => $calculate_data['longitude'],
);
$store_list_result = $store_model->getLocationStoreList($store_condition, '*', $latlng);
$store_list = $store_list_result['data'];
$title = $shop_goods_list['store_config']['value']['store_name'];
if ($title == '') {
$title = Express::express_type['store']['title'];
}
$express_type[] = ['title' => $title, 'name' => 'store', 'store_list' => $store_list];
}
}
// 外卖
if ($type == 'local') {
$store_is_o2o = $store_is_o2o ?? 0;
if ($store_is_o2o == 1) {
//查询本店的通讯地址
$title = $shop_goods_list['local_config']['value']['local_name'];
if ($title == '') {
$title = '外卖配送';
}
$store_model = new Store();
$store_condition = array(
['site_id', '=', $site_id],
);
$store_condition[] = ['is_o2o', '=', 1];
$store_condition[] = ['status', '=', 1];
$store_condition[] = ['is_frozen', '=', 0];
$store_condition[] = ['store_id', '=', $store_id];
$latlng = array(
'lat' => $calculate_data['latitude'],
'lng' => $calculate_data['longitude'],
);
$store_list_result = $store_model->getLocationStoreList($store_condition, '*', $latlng);
$store_list = $store_list_result['data'];
$express_type[] = ['title' => $title, 'name' => 'local', 'store_list' => $store_list];
}
}
}
return $order_model->success($express_type ?? []);
}
break;
case 'trade_calc':
if ($store_id > 0 && $store_config['store_business'] == 'store') {
$shop_goods = $params['shop_goods'];
//如果本地配送开启, 则查询出本地配送的配置
$local_model = new Local();
$local_info_result = $local_model->getLocalInfo([['site_id', '=', $site_id], ['store_id', '=', $store_id]]);
$local_info = $local_info_result['data'] ?? [];
$shop_goods['local_config']['info'] = $local_info;
$delivery_array = $calculate_data['delivery'] ?? [];
$delivery_type = $delivery_array['delivery_type'] ?? 'express';
if ($delivery_type == 'store') {
//门店自提
$delivery_money = 0;
$shop_goods['delivery']['delivery_type'] = 'store';
if ($store_is_pickup == 0) {
$error = ['error_msg' => '门店自提方式未开启!'];
}
$shop_goods['delivery']['store_id'] = $calculate_data['delivery']['store_id'];
$shop_goods['buyer_ask_delivery_time'] = $calculate_data['buyer_ask_delivery_time'];
$shop_goods = $this->storeOrderData($shop_goods, $calculate_data);
} else {
if (empty($calculate_data['member_address'])) {
$delivery_money = 0;
$shop_goods['delivery']['delivery_type'] = 'express';
$error = ['error_msg' => '未配置默认收货地址!'];
} else {
if ($delivery_type == 'express') {
if ($store_is_express == 1) {
//物流配送
$express = new Express();
$express_fee_result = $express->calculate($shop_goods, $calculate_data);
if ($express_fee_result['code'] < 0) {
$error = ['error_msg' => $express_fee_result['message']];
$delivery_fee = 0;
} else {
$delivery_fee = $express_fee_result['data']['delivery_fee'];
}
} else {
$error = ['error_msg' => '物流配送方式未开启!'];
$delivery_fee = 0;
}
$delivery_money = $delivery_fee;
$shop_goods['delivery']['delivery_type'] = 'express';
} else if ($delivery_type == 'local') {
//外卖配送
$delivery_money = 0;
$shop_goods['delivery']['delivery_type'] = 'local';
if ($store_is_o2o == 0) {
$error = ['error_msg' => '外卖配送方式未开启!'];
} else {
if (empty($calculate_data['delivery']['store_id'])) {
$error = ['error_msg' => '门店未选择!'];
}
$local_delivery_time = 0;
if (!empty($calculate_data['buyer_ask_delivery_time'])) {
$local_delivery_time = $calculate_data['buyer_ask_delivery_time'];
}
$shop_goods['buyer_ask_delivery_time'] = $local_delivery_time;
$local_model = new Local();
$local_result = $local_model->calculate($shop_goods, $calculate_data);
$shop_goods['delivery']['start_money'] = 0;
if ($local_result['code'] < 0) {
$shop_goods['delivery']['start_money'] = $local_result['data']['start_money_array'][0] ?? 0;
$error = ['error_msg' => $local_result['message'], 'priority' => 1, 'error' => $local_result['data']['code']];
} else {
$delivery_money = $local_result['data']['delivery_money'];
if (!empty($local_result['data']['error_code'])) {
$error = ['error_msg' => $local_result['data']['error'], 'priority' => 1, 'error' => $local_result['data']['error']];
}
}
$shop_goods['delivery']['error'] = $error['error'] ?? 0;
$shop_goods['delivery']['error_msg'] = $error['error_msg'] ?? '';
}
}
}
}
return $order_model->success(['shop_goods' => $shop_goods, 'delivery_money' => $delivery_money, 'error' => $error ?? []]);
}
break;
}
}
}