php-wms-client/src/GwallWms.php
2020-03-25 14:16:20 +08:00

414 lines
14 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace PdWms;
use GuzzleHttp\Client;
class GwallWms extends BaseWms implements WmsInterface
{
protected $config = [];
public function __construct($config = null)
{
$wmsConf = $config ?? (require(dirname(dirname(__FILE__)) . '/config/wms.php'))['gwall'];
$this->config = $wmsConf;
}
/**
* 销售发货单创建
* @param array $params
* @return mixed
*/
public function saleOrderCreate($params)
{
$service = 'gwisSubCreateSaleOrder';
$detailList = [];
$skuTotalWeight = 0;
foreach ($params['order_goods'] as $item) {
if ($item['sku_type'] == 0) {
array_push($detailList, [
'sku' => $item['wms_code'], //SKU编码
'qty' => $item['sku_quantity'], //数量
]);
$skuTotalWeight += ($item['weight'] * $item['sku_quantity']) / 1000;
} elseif ($item['sku_type'] == 1) {
foreach ($item['sku_nexus_snapshot'] as $gv) {
array_push($detailList, [
'sku' => $gv['wms_code'], //SKU编码
'qty' => $gv['sku_quantity'] * $item['sku_quantity'], //数量
]);
$skuTotalWeight += ($gv['weight'] * $gv['sku_quantity'] * $item['sku_quantity']) / 1000;
}
}
}
$postData = [
'headList' => [
[
'orderCode' => $params['order_no'], //配货单号
'bity' => 'EBUS', //订单类型 EBUS线上订单 OFFLINE线下订单 SPECIAL特殊订单
'warehouseCode' => $this->config['warehouse_code'], //仓库编码
'isUrgency' => 0, //是否紧急(0普通,1紧急)
'downDate' => $params['created_at'], //下单时间(yyyy/MM/dd HH:mm:ss)
'payTime' => $params['pay_at'], //支付时间(yyyy/MM/dd HH:mm:ss)
'auditTime' => date('Y-m-d H:i:s'), //审核时间(即生成通知单的时间)(yyyy/MM/dd HH:mm:ss)
'logisticsCompanyCode' => $this->chooseLogisticsCompany($params['order_address']['receive_province'], $skuTotalWeight), //物流公司编码
'postage' => $params['express_fee'], //邮费
'isDeliveryPay' => false, //是否货到付款(true/false)
'shopName' => $order['order_sender_info']['shop_name'] ?? self::DEFAULT_SHOP_NAME, //店铺名称
'shopCode' => $order['order_sender_info']['shop_name'] ?? self::DEFAULT_SHOP_NAME, //店铺编码
'consignee' => $this->filterEmoji($params['order_address']['receive_name']), //收货人姓名
'postCode' => $params['order_address']['receive_zip_code'] ?? self::DEFAULT_ZIP_CODE, //邮码
'provinceName' => $params['order_address']['receive_province'], //省名称
'cityName' => $params['order_address']['receive_city'], //市名称
'areaName' => $params['order_address']['receive_district'], //区名称
'address' => $params['order_address']['receive_address'], //收货地址
'mobile' => trim($params['order_address']['receive_mobile']), //移动电话
'orderPrice' => $params['total_fee'], //订单金额
'amountReceivable' => $params['pay_fee'], //应收金额
'isPostagePay' => false, //邮费是否到付 true/false
'actualPayment' => $params['pay_fee'], //实际支付
'goodsOwner' => $this->config['shipper_code'], //货主
'remark' => $params['remark'], //备注
'detailList' => $detailList,
],
]
];
$result = $this->request($service, $postData);
return $result;
}
/**
* 新增采购订单
* @param array $params
* @return mixed
*/
public function purchaseOrderCreate($params)
{
$service = 'gwisSubCreatePurchaseOrder';
$detailList = [];
foreach ($params['sku_info'] as $skuV) {
array_push($detailList, [
'sku' => $skuV['wms_code'],
'price' => $this->formatNumber($skuV['price']),
'qty' => intval($skuV['apply_stock']) + intval($skuV['apply_bad_stock']),
]);
}
$postData = [
'head' => [
'orderCode' => $params['number'],
'supplierCode' => $params['orders_purchase']['supplier_id'],
'warehouseCode' => $this->config['warehouse_code'],
'planDate' => $params['orders_purchase']['estimated_time'],
'goodsOwner' => $this->config['shipper_code'],
'remark' => $params['remarks'],
'detailList' => $detailList,
],
];
$result = $this->request($service, $postData);
return $result;
}
/**
* 商品库存查询(单个)
* @param string $wmsCode
* @param string $inventoryType
* @return mixed
*/
public function goodsStockInfoSingle($wmsCode, $inventoryType = 'ZP')
{
$service = 'gwisSubGetGoodsStockInfo';
$postData = [
'warehouseCode' => $this->config['warehouse_code'],
'sku' => $wmsCode,
'goodsOwner' => $this->config['shipper_code'],
];
$result = $this->request($service, $postData);
if (!$result['success']) {
return $result;
}
$data = $result['response'][0];
$formatData = $this->formatGoodsStockInfo($data);
return array_merge($result, ['response' => $formatData]);
}
/**
* 商品库存查询(批量)
* @param array $params
* @param string $inventoryType
* @return array
*/
public function goodsStockInfo($params, $inventoryType = 'ZP')
{
$data = [];
foreach ($params as $item) {
$result = $this->goodsStockInfoSingle($item['wms_code']);
if (!$result['success']) {
continue;
}
array_push($data, $result['response']);
}
return ['success' => true, 'response' => $data];
}
/**
* 商品同步(商品创建或修改)
* @param array $params
* @return mixed
*/
public function goodsSync($params)
{
$service = 'gwisSubGoodsCreateOrUpadte';
$postData = [
'items' => [
[
'sku' => $params['wms_code'],
'goodsName' => $params['name'],
'goodsType' => 'P', //商品属性(默认P) P正常商品 G赠品 S饰品 M辅料 C:耗材 B包装 X虚拟物品 L残次品 I:发票
'price' => $params['unit_price'],
'brcode' => $params['upc_code'] ?? $params['wms_code'],
'state' => 1, //状态(默认有效) 1:有效 0:注销
'goodsOwner' => $this->config['shipper_code'],
]
],
];
$result = $this->request($service, $postData);
if ($result['success']) {
$result = array_merge($result, [
'response' => [
'message' => $result['response'],
'itemId' => $params['wms_code'],
],
]);
}
return $result;
}
/**
* 单据取消
* @param array $params
* @return mixed
*/
public function orderCancel($params)
{
$service = 'gwisSubCancelOrder';
$postData = [
'orderCode' => $params['order_code'],
'orderType' => $params['order_type'], //取消订单类型 JYCK:销售订单 XTRK:销售退货 CGRK:采购订单 CGTH:采购退货 TRAN:调拨计划
'goodsOwner' => $this->config['shipper_code'],
'remark' => $params['cancel_reason'],
];
$result = $this->request($service, $postData);
return $result;
}
/**
* 退货入库单创建(新增销售退货计划)
* @param array $params
* @return mixed
*/
public function saleReturnOrderCreate($params)
{
$service = 'gwisSubCreateSaleReturnPlan';
$detailList = [];
foreach ($params['refund_goods'] as $item) {
array_push($detailList, [
'saleOrderCode' => $params['order_info']['order_no'],
'sku' => $item['wms_code'],
'qty' => $item['sku_quantity'],
]);
}
$postData = [
'head' => [
'orderCode' => $params['refund_no'],
'warehouseCode' => $this->config['warehouse_code'],
'createDate' => $params['created_at'],
'logisticsCompanyCode' => strtoupper($params['express_code']),
'expressNo' => $params['express_no'],
'phno' => $params['order_address_info']['receive_mobile'],
'stna' => $params['order_address_info']['receive_name'],
'goodsOwner' => $this->config['shipper_code'],
'remark' => $params['desc'],
'detailList' => $detailList,
],
];
$result = $this->request($service, $postData);
return $result;
}
/**
* 新增调拨入库计划
* @param $params
* @return mixed
*/
public function stockTransferInCreate($params)
{
$service = 'gwisSubCreateTranPlan';
$detailList = [];
foreach($params['sku_info'] as $skuV){
array_push($detailList, [
'sku' => $skuV['wms_code'],
'qty' => intval($skuV['apply_stock']),
]);
}
$postData = [
'head' => [
'orderCode' => $params['number'],
'sourceType' => '02', //来源类型不能为空,默认为(01) 01普通调拨(出、入仓库必需传) 02:调拨入库 03:调拨出库 04:唯品调拨
'targetWhcode' => $this->config['warehouse_code'],
'createDate' => date('Y-m-d H:i:s'),
'goodsOwner' => $this->config['shipper_code'],
'detailList' => $detailList,
]
];
$result = $this->request($service, $postData);
return $result;
}
/**
* 新增入库单
* @param array $params
* @param string $type
* @return mixed
*/
public function stockInCreate($params, $type)
{
$result = [];
switch ($type) {
//采购入库
case 0:
$result = $this->purchaseOrderCreate($params);
break;
//调拨入库
case 10:
$result = $this->stockTransferInCreate($params);
break;
//销退入库
case 40:
$result = $this->saleReturnOrderCreate($params);
break;
}
return $result;
}
/**
* 新增出库单
* @param array $params
* @return mixed
*/
public function stockOutCreate($params)
{
$service = 'gwisSubCreateTranPlan';
$detailList = [];
foreach ($params['order_goods'] as $item) {
array_push($detailList, [
'sku' => $item['wms_code'],
'qty' => $item['number'],
]);
}
$postData = [
'head' => [
'orderCode' => $params['out_warehouse_number'],
'sourceType' => '03', //来源类型不能为空,默认为(01) 01普通调拨(出、入仓库必需传) 02:调拨入库 03:调拨出库 04:唯品调拨
'fromWhcode' => $this->config['warehouse_code'],
'createDate' => date('Y-m-d H:i:s'),
'logisticsCompanyCode' => 'SF',
'goodsOwner' => $this->config['shipper_code'],
'detailList' => $detailList,
]
];
$result = $this->request($service, $postData);
return $result;
}
final protected function post($query, $body = '', $headers = ['content-type' => 'application/json; charset = utf-8'])
{
$url = $this->config['base_uri'];
$url .= http_build_query($query);
$data = [
'body' => $body,
'headers' => $headers,
];
$client = new Client();
$response = $client->request('POST', $url, $data);
$content = $response->getBody()->getContents();
return $content;
}
public function request($service, $content)
{
$encrypt = $this->config['encrypt'];
$base64Content = $this->base64Content($content, $encrypt);
$query = [
'service' => $service,
'appkey' => $this->config['app_key'],
'secret' => $encrypt ? $this->createSign($base64Content) : '1',
'format' => $this->config['format'],
'encrypt' => $encrypt,
'content' => $base64Content,
];
$result = $this->post($query);
$result = \GuzzleHttp\json_decode($result, true);
$isSuccess = (empty($result) || !$result['isSuccess']) ? false : true;
$response = [
'success' => $isSuccess,
'response' => $isSuccess ? $result['body'] : $result,
];
return $response;
}
public function base64Content($content, $encrypt = 0)
{
$content = \GuzzleHttp\json_encode($content);
if (!$encrypt) {
return $content;
}
$content = str_replace('=', '\\u003d', base64_encode($content));
return $content;
}
public function createSign($content)
{
return strtoupper(md5($content . $this->config['session_key']));
}
public function formatGoodsStockInfo($params)
{
$params = array_map(function ($item) {
return array_merge($item, [
'itemCode' => $item['sku'],
'itemId' => $item['sku'],
'quantity' => $item['uqty'],
'lockQuantity' => $item['qty'] - $item['uqty'],
]);
}, $params);
return $params;
}
public function callback($params)
{
}
}