sf-apple-sdk
Version:
Apple SF SDK for SF WMS
377 lines (376 loc) • 18.5 kB
JavaScript
// src/services/inbound.service.ts
import { LocationEnum } from "../types/location.js";
import { Scene } from "../types/index.js";
import { SERVICE_CODE } from "../constants/service.js";
import { TransactionCode, TransactionCodeSfInOrderTypeMap, } from "../constants/transaction-code.js";
import { post } from "../utils/post.js";
/**
* 入库单接口 Service 3.2 ✅
* 通过此接口下发入库单。
*/
export class PurchaseOrderService {
constructor(options) {
this.options = options;
}
/**
* 通用下发到SF入库单
*/
async sendPurchaseOrders(data) {
if (!data.purchaseOrders?.length) {
throw new Error("PurchaseOrders 都是必填的");
}
const res = await post({
url: this.options.baseURL,
checkword: this.options.checkword,
sysSource: this.options.sysSource,
serviceCode: SERVICE_CODE.PURCHASE_ORDER_SERVICE,
specialStr: this.options.specialStr,
body: {
CompanyCode: this.options.sysSource,
Checkword: this.options.checkword,
AccessCode: this.options.accessCode,
ServiceCode: SERVICE_CODE.PURCHASE_ORDER_SERVICE,
PurchaseOrders: data.purchaseOrders,
},
});
return res;
}
/**
* 统一场景入库
* 退货:x -> 质检仓
* 品质变更:质检仓 -> 残次品仓
* 检测为良品:质检仓 -> 良品仓
*/
async sceneIn(data) {
try {
let transactionCode = "";
/**
* 默认: 采购入库
*/
let sfOrderType = "10";
const location = {
from: {
location_code: "",
location_name: "",
location_type: "",
},
to: {
location_code: "",
location_name: "",
},
};
const sender_address = {
province: data.sender?.province,
city: data.sender?.city,
area: data.sender?.area,
address: data.sender?.address,
};
if (data.scene === Scene.purchase_in) {
/** 采购入库 */
transactionCode = TransactionCode.purchase_in;
sfOrderType = TransactionCodeSfInOrderTypeMap.purchase_in;
location.from = {
location_code: "Factory",
location_name: "Factory",
location_type: LocationEnum.factory,
};
location.to = {
location_code: `${this.options.hqid}-${this.options.goodWarehouseCode}`,
location_name: `${this.options.goodWarehouseName}`,
};
}
else if (data.scene === Scene.sales_2c_return_in) {
/** 销售退货入库 - 2C消费者退大仓 */
transactionCode = TransactionCode.sale_in;
sfOrderType = TransactionCodeSfInOrderTypeMap.sale_in;
location.from = {
/** 企业个人客户 */
location_code: "DTC",
/** 企业个人客户名称 */
location_name: "DTC",
location_type: LocationEnum.dtc,
};
location.to = {
location_code: `${this.options.hqid}-${this.options.qualityCheckWarehouseCode}`,
location_name: `${this.options.qualityCheckWarehouseName}`,
};
}
else if (data.scene === Scene.sales_2b_return_in) {
/** 销售退货入库 - 企业客户退大仓 */
transactionCode = TransactionCode.sale_in;
sfOrderType = TransactionCodeSfInOrderTypeMap.sale_in;
location.from = {
/** 企业客户 */
location_code: "EDTC",
/** 企业客户名称 */
location_name: "EDTC",
location_type: LocationEnum.edtc,
};
location.to = {
location_code: `${this.options.hqid}-${this.options.qualityCheckWarehouseCode}`,
location_name: `${this.options.qualityCheckWarehouseName}`,
};
}
else if (data.scene === Scene.sales_2ro_return_in) {
/** 销售退货入库 - 经销商办公室退大仓 */
transactionCode = TransactionCode.sale_in;
sfOrderType = TransactionCodeSfInOrderTypeMap.sale_in;
location.from = {
/** 经销商办公室 */
location_code: `${this.options.officeCode}`,
location_name: `${this.options.officeName}`,
location_type: LocationEnum.reseller_office,
};
location.to = {
location_code: `${this.options.hqid}-${this.options.qualityCheckWarehouseCode}`,
location_name: `${this.options.qualityCheckWarehouseName}`,
};
}
// else if (data.scene === Scene.repurchase_in) {
// /** 串货回购入库 串货回购 不涉及 SF */
// transactionCode = TransactionCode.repurchase_in;
// sfOrderType = TransactionCodeSfInOrderTypeMap.repurchase_in!;
// location.from = {
// location_code: "Expense",
// location_name: "Expense",
// location_type: LocationEnum.expense,
// };
// location.to = {
// location_code: `${this.options.hqid}-${this.options.repurchaseWarehouseCode}`,
// location_name: `${this.options.repurchaseWarehouseName}`,
// };
// }
else if (data.scene === Scene.borrow_in) {
/** 借机还货入库 */
transactionCode = TransactionCode.borrow_in;
sfOrderType = TransactionCodeSfInOrderTypeMap.borrow_in;
location.from = {
location_code: "Expense",
location_name: "Expense",
location_type: LocationEnum.expense,
};
location.to = {
location_code: `${this.options.hqid}-${this.options.borrowWarehouseCode}`,
location_name: `${this.options.borrowWarehouseName}`,
};
}
// else if (data.scene === Scene.seed_in) {
// /** Seed还货入库 不涉及 SF */
// transactionCode = TransactionCode.seed_in;
// sfOrderType = TransactionCodeSfInOrderTypeMap.seed_in!;
// location.from = {
// location_code: "Expense",
// location_name: "Expense",
// location_type: LocationEnum.expense,
// };
// location.to = {
// location_code: `${this.options.hqid}-${this.options.seedWarehouseCode}`,
// location_name: `${this.options.seedWarehouseName}`,
// };
// }
else if (data.scene === Scene.quality_change_in) {
/** 品质变更入库 良品仓 -> 残次品仓 */
if (!data.transfer?.origin_bill_code) {
throw new Error("品质变更出库 transfer.origin_bill_code 是必填的");
}
transactionCode = TransactionCode.quality_change_in;
sfOrderType = TransactionCodeSfInOrderTypeMap.quality_change_in;
location.from = {
location_code: `${this.options.hqid}-${this.options.goodWarehouseCode}`,
location_name: `${this.options.goodWarehouseName}`,
location_type: LocationEnum.sf_apple_warehouse,
};
location.to = {
location_code: `${this.options.hqid}-${this.options.defectiveWarehouseCode}`,
location_name: `${this.options.defectiveWarehouseName}`,
};
}
else if (data.scene === Scene.defective_in) {
/** 残次品入库 残次品 -> 残次品 */
transactionCode = TransactionCode.quality_change_in;
sfOrderType = TransactionCodeSfInOrderTypeMap.quality_change_in;
location.from = {
location_code: `${this.options.hqid}-${this.options.defectiveWarehouseCode}`,
location_name: `${this.options.defectiveWarehouseName}`,
location_type: LocationEnum.sf_apple_warehouse,
};
location.to = {
location_code: `${this.options.hqid}-${this.options.defectiveWarehouseCode}`,
location_name: `${this.options.defectiveWarehouseName}`,
};
}
else if (data.scene === Scene.difference_in) {
/** 差异入库 from to 是同一个仓库 */
if (!data.transfer) {
throw new Error("差异入库 transfer 是必填的");
}
transactionCode = TransactionCode.difference_in;
sfOrderType = TransactionCodeSfInOrderTypeMap.difference_in;
location.from = {
location_code: data.transfer.from_location_code,
location_name: data.transfer.from_location_name,
location_type: LocationEnum.sf_apple_warehouse,
};
location.to = {
location_code: data.transfer.to_location_code,
location_name: data.transfer.to_location_name,
};
}
else if (data.scene === Scene.transfer_in) {
/** 仓间调拨入库 */
if (!data.transfer) {
throw new Error("调拨场景 transfer 是必填的");
}
transactionCode = TransactionCode.transfer_in;
sfOrderType = TransactionCodeSfInOrderTypeMap.transfer_in;
location.from = {
location_code: data.transfer.from_location_code,
location_name: data.transfer.from_location_name,
location_type: LocationEnum.sf_apple_warehouse,
};
location.to = {
location_code: data.transfer.to_location_code,
location_name: data.transfer.to_location_name,
};
}
else if (data.scene === Scene.cycle_in) {
/** 盘点入库 from to 是同一个仓库 */
if (!data.cycle) {
throw new Error("盘点入库 cycle 是必填的");
}
transactionCode = TransactionCode.cycle_in;
sfOrderType = TransactionCodeSfInOrderTypeMap.cycle_in;
location.from = {
location_code: `${this.options.hqid}-${data.cycle?.from_location_code}`,
location_name: `${data.cycle?.from_location_name}`,
location_type: LocationEnum.sf_apple_warehouse,
};
location.to = {
location_code: `${this.options.hqid}-${data.cycle?.to_location_code}`,
location_name: `${data.cycle?.to_location_name}`,
};
}
else if (data.scene === Scene.transfer_good_sku_ro_to_warehouse_in) {
/** 搬仓调拨入库 经销商办公室 -> SF 仓库 */
if (!data.transfer) {
throw new Error("搬仓调拨入库 transfer 是必填的");
}
transactionCode = TransactionCode.transfer_in;
sfOrderType = TransactionCodeSfInOrderTypeMap.transfer_in;
location.from = {
location_code: `NON-DS-WH`,
location_name: `NON-DS-WH`,
location_type: LocationEnum.sf_apple_warehouse,
};
location.to = {
location_code: `${this.options.hqid}-${this.options.goodWarehouseCode}`,
location_name: `${this.options.goodWarehouseName}`,
};
}
else if (data.scene === Scene.value_added_service_transfer_in) {
/** 增值服务调拨出库 良品仓 -> 仓内部署仓 */
if (!data.transfer?.origin_bill_code) {
throw new Error("增值服务调拨入库 transfer.origin_bill_code 是必填的");
}
transactionCode = TransactionCode.value_added_service_transfer_in;
sfOrderType = TransactionCodeSfInOrderTypeMap.value_added_service_transfer_in;
location.from = {
location_code: `${this.options.hqid}-${this.options.goodWarehouseCode}`,
location_name: `${this.options.goodWarehouseName}`,
location_type: LocationEnum.sf_apple_warehouse,
};
location.to = {
location_code: `${this.options.hqid}-${this.options.deployWarehouseCode}`,
location_name: `${this.options.deployWarehouseName}`,
};
}
else if (data.scene === Scene.value_added_service_out) {
/** 增值服务发货 仓内部署仓 -> 客户 */
transactionCode = TransactionCode.value_added_service_out;
sfOrderType = TransactionCodeSfInOrderTypeMap.value_added_service_out;
location.from = {
location_code: `${this.options.hqid}-${this.options.deployWarehouseCode}`,
location_name: `${this.options.deployWarehouseName}`,
location_type: LocationEnum.sf_apple_warehouse,
};
location.to = {
location_code: `${this.options.hqid}-${this.options.deployWarehouseCode}`,
location_name: `${this.options.deployWarehouseName}`,
};
}
const items = data.skus.map((sku) => {
return {
SkuNo: sku.sku_code,
Qty: sku.qty,
/** ToLocationCode 逻辑仓/物理仓编码 */
LotAttr4: location.to.location_code,
UserDef2: sku.mpn,
SerialInfoList: sku.sn_list?.map((sn) => ({
SerialNum: sn,
})),
InventoryStatus: sku.stock_status,
};
});
if (data.scene === Scene.sales_2c_return_in ||
data.scene === Scene.sales_2b_return_in ||
data.scene === Scene.sales_2ro_return_in) {
if (!data?.return_dn_code) {
throw new Error("退货入库单 - 运单号是必填的");
}
if (!data?.origin_out_code) {
throw new Error("退货入库单 - 原出库单号是必填的");
}
}
else if (data.scene === Scene.purchase_in) {
if (!data?.purchase_dn_code) {
throw new Error("采购入库单 - 运单号是必填的");
}
}
if (data.scene === Scene.transfer_in) {
if (!data?.transfer?.origin_bill_code) {
throw new Error("调拨出入库 - 原参考 erp 单号是必填的");
}
}
const result = await this.sendPurchaseOrders({
purchaseOrders: [
{
WarehouseCode: this.options.warehouseCode, // SF 物理仓库编码
TransferWarehouseCode: (data.scene === Scene.transfer_in || data.scene === Scene.value_added_service_transfer_in || data.scene === Scene.quality_change_in)
? this.options.warehouseCode
: data.scene === Scene.transfer_good_sku_ro_to_warehouse_in
? `NON-DS-WH`
: undefined, // 调出仓库编码
ErpOrder: data.bill_code, // 单据号
ErpOrderType: transactionCode, // 苹果要求的 TransactionCode
SFOrderType: sfOrderType, // SF 入库单类型
OriginalOrderNo: data.scene === Scene.purchase_in
? data.purchase_dn_code
: data.origin_out_code, // 2C 平台售后单号/原出库单号/DN 号
UserDef3: location.to.location_code, // 逻辑仓
UserDef4: location.from.location_code, // FromLocationCode
UserDef5: data?.transfer?.origin_bill_code, // 调拨出入库的原参考 erp 单号,用于 Apple 供数
UserDef6: data.scene === Scene.sales_2c_return_in ||
data.scene === Scene.sales_2b_return_in ||
data.scene === Scene.sales_2ro_return_in
? data.return_dn_code
: undefined, // 用来传退货入库单的运单号
SenderInfo: {
Sender: location.from.location_name, // FromLocationName
SenderName: location.from.location_type, // FromLocationType
SenderProvince: sender_address.province,
SenderCity: sender_address.city,
SenderArea: sender_address.area,
SenderAddress: `${sender_address.province}|${sender_address.city}|${sender_address.area}|${sender_address.address}`,
},
UserDef10: location.to.location_name, // ToLocationName
Items: items,
},
],
});
return result;
}
catch (error) {
throw error;
}
}
}