206 lines
7.1 KiB
PHP
206 lines
7.1 KiB
PHP
<?php
|
||
|
||
namespace PdWms;
|
||
|
||
class WmsStrategy
|
||
{
|
||
public $wmsClient;
|
||
|
||
public function __construct($warehouseId)
|
||
{
|
||
$config = require(dirname(dirname(__FILE__)) . '/config/wms.php');
|
||
switch ($warehouseId)
|
||
{
|
||
case $config['fineex']['warehouse_id']:
|
||
$this->wmsClient = new FineexWms($config['fineex']);
|
||
break;
|
||
default:
|
||
throw new \Exception('仓库不存在');
|
||
}
|
||
}
|
||
|
||
public function saleOrderCreate($params) {
|
||
$orderLogisticsType = '';
|
||
if ($params['delivery_type'] == 1) {
|
||
$orderLogisticsType = 'toC';
|
||
} else if ($params['delivery_type'] == 2) {
|
||
$orderLogisticsType = 'toB';
|
||
}
|
||
|
||
if (!$orderLogisticsType || !$params['express_code']) {
|
||
//计算商品重量
|
||
$skuTotalWeight = $this->getSkuTotalWeight($params['order_goods']);
|
||
$logistics = $this->chooseLogisticsCompany($params['order_address']['receive_province'], $skuTotalWeight);
|
||
$params['order_sender_info']['logistics_code'] = $logistics['code'];
|
||
$params['order_sender_info']['logistics_name'] = $logistics['name'];
|
||
$orderLogisticsType = $logistics['type'];
|
||
} else {
|
||
$params['order_sender_info']['logistics_code'] = $params['express_code'];
|
||
$params['order_sender_info']['logistics_name'] = self::getExpressNameByExpressCode($params['express_code']);
|
||
}
|
||
|
||
if ($orderLogisticsType == 'toC') {
|
||
// 2C订单发货
|
||
$res = $this->deliveryOrderCreate($params);
|
||
$type = 1;
|
||
} else {
|
||
// 2B订单发货
|
||
$params['out_warehouse_number'] = $params['order_no'];
|
||
$params['address_name'] = $params['order_address']['receive_name'];
|
||
$params['address_telephone'] = $params['order_address']['receive_mobile'];
|
||
$params['address_province'] = $params['order_address']['receive_province'];
|
||
$params['address_city'] = $params['order_address']['receive_city'];
|
||
$params['address_area'] = $params['order_address']['receive_district'];
|
||
$params['address_detail'] = $params['order_address']['receive_address'];
|
||
$params['order_type'] = 'PTCK';
|
||
foreach ($params['order_goods'] as &$item) {
|
||
$item['product_name'] = $item['goods_name'];
|
||
$item['number'] = $item['sku_quantity'];
|
||
}
|
||
$res = $this->stockOutCreate($params);
|
||
$type = 0;
|
||
}
|
||
|
||
$res['response']['type'] = $type;
|
||
return $res;
|
||
}
|
||
|
||
public function deliveryOrderCreate($params) {
|
||
return $this->wmsClient->deliveryOrderCreate($params);
|
||
}
|
||
|
||
public function inventoryQuerySingle($wmsCode, $inventoryType = 'ZP') {
|
||
return $this->wmsClient->inventoryQuerySingle($wmsCode, $inventoryType);
|
||
}
|
||
|
||
public function inventoryQuery($params, $inventoryType = 'ZP') {
|
||
return $this->wmsClient->inventoryQuery($params, $inventoryType);
|
||
}
|
||
|
||
public function goodsSkuSync($params) {
|
||
return $this->wmsClient->goodsSkuSync($params);
|
||
}
|
||
|
||
public function orderCancel($params) {
|
||
return $this->wmsClient->orderCancel($params);
|
||
}
|
||
|
||
public function returnOrderCreate($params) {
|
||
return $this->wmsClient->returnOrderCreate($params);
|
||
}
|
||
|
||
public function stockInCreate($params) {
|
||
return $this->wmsClient->stockInCreate($params);
|
||
}
|
||
|
||
public function stockOutCreate($params) {
|
||
return $this->wmsClient->stockOutCreate($params);
|
||
}
|
||
|
||
/**
|
||
* 获取sku总重量
|
||
* @param $orderGoods
|
||
* @return float|int
|
||
*/
|
||
public function getSkuTotalWeight($orderGoods)
|
||
{
|
||
$skuTotalWeight = 0;
|
||
foreach ($orderGoods as $item) {
|
||
$item['product_name'] = $item['goods_name'];
|
||
$item['number'] = $item['sku_quantity'];
|
||
if ($item['sku_type'] == 0) {
|
||
$skuTotalWeight += ($item['weight'] * $item['sku_quantity']) / 1000;
|
||
} elseif ($item['sku_type'] == 1) {
|
||
foreach ($item['sku_nexus_snapshot'] as $gv) {
|
||
$skuTotalWeight += ($gv['weight'] * $gv['sku_quantity'] * $item['sku_quantity']) / 1000;
|
||
}
|
||
}
|
||
}
|
||
return $skuTotalWeight;
|
||
}
|
||
|
||
/**
|
||
* 选择物流公司
|
||
* @param string $province
|
||
* @param string $weight
|
||
* @return array
|
||
*/
|
||
public function chooseLogisticsCompany($province, $weight)
|
||
{
|
||
$expressChooseConf = require(dirname(dirname(__FILE__)) . '/config/express_choose.php');
|
||
$type = self::getOrderType($province, $weight, $expressChooseConf);
|
||
return [
|
||
'type' => $type,
|
||
'code' => $expressChooseConf['logistics'][$type][0]['code'],
|
||
'name' => $expressChooseConf['logistics'][$type][0]['name'],
|
||
];
|
||
}
|
||
|
||
/**
|
||
* 根据商品重量区分bc单,返回可选择的物流快递
|
||
* @param string $province
|
||
* @param string $weight
|
||
* @return array
|
||
*/
|
||
static public function getLogistics($province, $weight)
|
||
{
|
||
$expressChooseConf = require(dirname(dirname(__FILE__)) . '/config/express_choose.php');
|
||
$type = self::getOrderType($province, $weight, $expressChooseConf);
|
||
return $expressChooseConf['logistics'][$type];
|
||
}
|
||
|
||
/**
|
||
* 判断订单BC类型
|
||
* @param $province
|
||
* @param $weight
|
||
* @return string
|
||
*/
|
||
static public function getOrderType($province, $weight, $expressChooseConf)
|
||
{
|
||
if (in_array($province, $expressChooseConf['address_list'])) {
|
||
if ($weight >= $expressChooseConf['jzh_express_weight']) {
|
||
return 'toB';
|
||
}
|
||
return 'toC';
|
||
} else {
|
||
if ($weight >= $expressChooseConf['other_express_weight']) {
|
||
return 'toB';
|
||
}
|
||
return 'toC';
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 根据物流快递判断订单bc类型
|
||
* @param $expressCode
|
||
* @return string
|
||
*/
|
||
static public function getOrderTypeByExpressCode($expressCode)
|
||
{
|
||
$expressChooseConf = require(dirname(dirname(__FILE__)) . '/config/express_choose.php');
|
||
$logisticsToC = collect($expressChooseConf['logistics']['toC'])->pluck('code')->toArray();
|
||
if (in_array($expressCode, $logisticsToC)) {
|
||
return 'toC';
|
||
}
|
||
$logisticsToB = collect($expressChooseConf['logistics']['toB'])->pluck('code')->toArray();
|
||
if (in_array($expressCode, $logisticsToB)) {
|
||
return 'toB';
|
||
}
|
||
return '';
|
||
}
|
||
|
||
/**
|
||
* 根据物流码获取物流名称
|
||
*/
|
||
static public function getExpressNameByExpressCode($expressCode)
|
||
{
|
||
$expressChooseConf = require(dirname(dirname(__FILE__)) . '/config/express_choose.php');
|
||
$logistics = collect([$expressChooseConf['logistics']['toC'], $expressChooseConf['logistics']['toB']])
|
||
->collapse()
|
||
->keyBy('code')
|
||
->all();
|
||
$expressName = $logistics[$expressCode] ? $logistics[$expressCode]['name'] : '';
|
||
return $expressName;
|
||
}
|
||
}
|