'待确认',
self::PURCHASE_ORDER_STATUS_UN_SEND => '待配送',
self::PURCHASE_ORDER_STATUS_SENDING => '待入库',
self::PURCHASE_ORDER_STATUS_COMPLETE => '已入库',
self::PURCHASE_ORDER_STATUS_CANCEL => '已取消',
];
const DEBT_UNKNOWN = 0;
const DEBT_YES = 1;
const DEBT_NO = 2;
//寄付与到付
const YF_PAY_WAY_JF = 1;
const YF_PAY_WAY_DF = 2;
//稍后入库和直接入库
const IN_TYPE_LATER = 0;
const IN_TYPE_NOW = 1;
/**
* 采购挂账:买方 xhGhs 净 balance 减少,并记余额变动(合并后模型,不再写欠款字段)
*/
public static function applyPurchaseDebtOnBuyerGhs($ghs, $amount, $purchase, $sjId, $shopId)
{
AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
$ghsNewBalance = bcsub($ghs->balance ?? '0.00', $amount, 2);
$ghs->debt = GhsClass::DEBT_YES;
$ghs->balance = $ghsNewBalance;
$ghs->debtNum += 1;
$ghs->expendAmount = bcadd($ghs->expendAmount ?? '0.00', $amount, 2);
$ghs->expendNum += 1;
$ghs->save(false, ['debt', 'balance', 'debtNum', 'expendAmount', 'expendNum']);
$orderSn = $purchase->orderSn ?? '';
$capitalType = dict::getDict('capitalType', 'ghsPurchase', 'id');
GhsBalanceChangeClass::add([
'ghsId' => $ghs->id ?? 0,
'relateId' => $purchase->id ?? 0,
'ptStyle' => 2,
'capitalType' => $capitalType,
'amount' => $amount,
'balance' => $ghsNewBalance,
'staffId' => $purchase->shopAdminId ?? 0,
'staffName' => $purchase->shopAdminName ?? '',
'io' => 0,
'event' => '新增采购 ' . $orderSn,
'sjId' => $sjId,
'shopId' => $shopId,
], true);
return $ghs;
}
/**
* 采购挂账:供货方 custom 净 balance 同步减少(双边关系)
*/
public static function applyPurchaseDebtOnSupplierCustom($ghs, $amount, $ghsMain = null)
{
$customId = $ghs->customId ?? 0;
$custom = CustomClass::getLockById($customId);
if (empty($custom)) {
util::fail('没有找到客户信息');
}
AccountMoneyClass::ensureCustomMoneyReady($custom, true);
$custom->isDebt = CustomClass::IS_DEBT_YES;
$custom->balance = bcsub($custom->balance ?? '0.00', $amount, 2);
if ($custom->debtNum == 0 && $ghsMain !== null) {
$ghsMain->mayGatheringNum += 1;
}
$custom->debtNum += 1;
$custom->buyNum += 1;
$custom->buyAmount = bcadd($custom->buyAmount ?? '0.00', $amount, 2);
$custom->save();
return $custom;
}
/**
* 采购售后退款:买方 xhGhs 净 balance 增加 + 流水;供货方 custom 同步
*/
public static function applyPurchaseRefundOnAccounts($ghs, $amount, $refund, $order, $sjId, $shopId)
{
AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
$ghsNewBalance = bcadd($ghs->balance ?? '0.00', $amount, 2);
$ghs->balance = $ghsNewBalance;
// 待结采购单售后:净 balance 增加;若该单待结金额已退尽则 debtNum 减 1
$wasDebtOrder = intval($order->debt ?? 0) === self::DEBT_YES;
$clearOrderDebt = $wasDebtOrder && bccomp($order->actPrice ?? '0', '0', 2) <= 0;
if ($clearOrderDebt) {
$ghs->debtNum = max(0, intval($ghs->debtNum ?? 0) - 1);
$order->debt = self::DEBT_NO;
$order->save(false, ['debt']);
}
$ghs->debt = bccomp($ghsNewBalance, '0', 2) < 0 ? GhsClass::DEBT_YES : GhsClass::DEBT_NO;
$ghs->save(false, ['balance', 'debtNum', 'debt']);
$orderSn = $order->orderSn ?? '';
$refundSn = $refund->refundSn ?? '';
$capitalType = dict::getDict('capitalType', 'ghsCgOrderRefund', 'id');
GhsBalanceChangeClass::add([
'ghsId' => $ghs->id ?? 0,
'relateId' => $refund->id ?? 0,
'ptStyle' => 2,
'capitalType' => $capitalType,
'amount' => $amount,
'balance' => $ghsNewBalance,
'io' => 1,
'staffId' => $refund->shopAdminId ?? 0,
'staffName' => $refund->shopAdminName ?? '',
'event' => '采购单 ' . $orderSn . ' 申请售后 ' . $refundSn,
'sjId' => $sjId,
'shopId' => $shopId,
], true);
$customId = $ghs->customId ?? 0;
if ($customId > 0) {
$custom = CustomClass::getLockById($customId);
if (!empty($custom)) {
AccountMoneyClass::ensureCustomMoneyReady($custom, true);
$customNewBalance = bcadd($custom->balance ?? '0.00', $amount, 2);
$custom->balance = $customNewBalance;
if ($clearOrderDebt) {
$custom->debtNum = max(0, intval($custom->debtNum ?? 0) - 1);
}
$custom->isDebt = bccomp($customNewBalance, '0', 2) < 0 ? CustomClass::IS_DEBT_YES : CustomClass::IS_DEBT_NO;
$saveAttrs = ['balance', 'isDebt'];
if ($clearOrderDebt) {
$saveAttrs[] = 'debtNum';
}
$custom->save(false, $saveAttrs);
}
}
return $ghs;
}
/**
* 采购结账:买方 xhGhs 净 balance 增加 + 流水
*/
public static function applyPurchaseClearOnBuyerGhs($ghs, $amount, $clear, $orderCount)
{
AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
$ghsNewBalance = bcadd($ghs->balance ?? '0.00', $amount, 2);
$ghs->balance = $ghsNewBalance;
$ghs->debtNum -= $orderCount;
$ghs->debt = bccomp($ghsNewBalance, '0', 2) < 0 ? GhsClass::DEBT_YES : GhsClass::DEBT_NO;
$ghs->save(false, ['balance', 'debtNum', 'debt']);
$capitalType = dict::getDict('capitalType', 'ghsCgOrderClear', 'id');
GhsBalanceChangeClass::add([
'ghsId' => $ghs->id ?? 0,
'relateId' => $clear->id ?? 0,
'ptStyle' => 2,
'capitalType' => $capitalType,
'amount' => $amount,
'balance' => $ghsNewBalance,
'staffId' => $clear->customShopAdminId ?? 0,
'staffName' => $clear->customShopAdminName ?? '',
'io' => 1,
'event' => '采购单结账 ' . ($clear->orderSn ?? ''),
'sjId' => $clear->sjId ?? 0,
'shopId' => $clear->shopId ?? 0,
], true);
return $ghs;
}
/**
* 采购结账:供货方 custom 净 balance 同步增加
*/
public static function applyPurchaseClearOnSupplierCustom($ghs, $amount, $orderCount, $ghsMain = null)
{
$customId = $ghs->customId ?? 0;
$custom = CustomClass::getLockById($customId);
if (empty($custom)) {
util::fail('没有找到客户信息');
}
AccountMoneyClass::ensureCustomMoneyReady($custom, true);
$customNewBalance = bcadd($custom->balance ?? '0.00', $amount, 2);
$custom->balance = $customNewBalance;
$custom->debtNum -= $orderCount;
$custom->isDebt = bccomp($customNewBalance, '0', 2) < 0 ? CustomClass::IS_DEBT_YES : CustomClass::IS_DEBT_NO;
$custom->save(false, ['balance', 'debtNum', 'isDebt']);
if ($custom->debtNum == 0 && $ghsMain !== null) {
$ghsMain->mayGatheringNum -= 1;
$ghsMain->save();
}
return $custom;
}
public static function assign($cg, $shop, $params)
{
$bookSn = $cg->bookSn ?? 0;
$orderSn = $cg->orderSn ?? '';
$cgId = $cg->id ?? 0;
$mainId = $cg->mainId ?? 0;
$ghsId = $cg->ghsId ?? 0;
$ghsName = $cg->ghsName ?? '';
$cgItemList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
if (empty($cgItemList)) {
return [];
}
$ids = [];
foreach ($cgItemList as $cgItem) {
$ids[] = $cgItem->productId;
}
$productInfoList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id', true);
$hasChange = 0;
foreach ($cgItemList as $cgItem) {
$cgItemId = $cgItem->id ?? 0;
$cgItemNum = $cgItem->itemNum ?? 0;
$productId = $cgItem->productId ?? 0;
$ptItemId = $cgItem->itemId ?? 0;
$assignSeat = $cgItem->assignSeat;
$noAssignHasDelNeedCg = $cgItem->noAssignHasDelNeedCg;
//不需要分配货位号的花材,已经减待采数
if ($assignSeat == 0 && $noAssignHasDelNeedCg == 1) {
continue;
}
if ($cgItemNum <= 0) {
continue;
}
$currentItemInfo = $productInfoList[$productId] ?? null;
if (empty($currentItemInfo)) {
util::fail('花材信息缺失');
}
$bookItemRes = BookItemClass::getByCondition(['bookSn' => $bookSn, 'itemId' => $productId], true);
if (empty($bookItemRes)) {
//没有预订花材则不需要分配货位
continue;
}
$bookItemCustomList = BookItemCustomClass::getAllByCondition(['bookSn' => $bookSn, 'itemId' => $productId], null, '*', null, true);
if (empty($bookItemCustomList)) {
//没有预订的花材和客户则不需要分配货位
continue;
}
$customIds = [];
foreach ($bookItemCustomList as $bookItemCustomRes) {
$customIds[] = $bookItemCustomRes->customId;
}
$customList = CustomClass::getAllByCondition(['id' => ['in', $customIds]], null, '*', 'id', true);
foreach ($bookItemCustomList as $bookItemCustomRes) {
$customId = $bookItemCustomRes->customId ?? 0;
$bookCustomRes = BookCustomClass::getByCondition(['bookSn' => $bookSn, 'customId' => $customId], true);
if (empty($bookCustomRes)) {
noticeUtil::push("采购单 {$orderSn} 分配货位时,发现 bookCustom 数据有问题", '15280215347');
util::fail("bookCustom 数据有问题");
}
$confirmSendNum = $cgItem->confirmSendNum ?? 0;
$remainCouldSendNum = bcsub($cgItemNum, $confirmSendNum, 2);
if ($remainCouldSendNum <= 0) {
//剩余应分配数量不足则退出本循环
break;
}
$needCgNum = $bookItemCustomRes->needCgNum ?? 0;
if ($needCgNum <= 0) {
//此客户花材,没有要采购的数量,则找下一个
continue;
}
if ($needCgNum >= $remainCouldSendNum) {
$remainNeedSendNum = $remainCouldSendNum;
} else {
$remainNeedSendNum = $needCgNum;
}
if ($remainNeedSendNum <= 0) {
//剩余要分配数量不足则退出本循环
break;
}
//待采数量减少
$params['currentChangeType'] = 'addCg';
$params['customName'] = $bookItemCustomRes->customName ?? '';
$params['customId'] = $bookItemCustomRes->customId ?? '';
$params['customNamePy'] = $bookItemCustomRes->customNamePy ?? '';
if ($assignSeat == 1) {
$bookItemCustomRes = BookItemCustomClass::delNeedCgNum($bookItemCustomRes, $remainNeedSendNum, 0, $cg, $shop, $currentItemInfo, $params);
$bookCustomRes = BookCustomClass::delNeedCgNum($bookCustomRes, $remainNeedSendNum, 0, $cg, $shop, $currentItemInfo, $params);
}
$bookItemRes = BookItemClass::delNeedCgNum($bookItemRes, $remainNeedSendNum, 0, $cg, $shop, $currentItemInfo, $params);
$hasChange = 1;
if ($assignSeat == 1) {
//路上数量增加
$bookItemCustomRes = BookItemCustomClass::addRoadNum($bookItemCustomRes, $remainNeedSendNum, $cg, $shop, $currentItemInfo, $params);
$bookCustomRes = BookCustomClass::addRoadNum($bookCustomRes, $remainNeedSendNum, $cg, $shop, $currentItemInfo, $params);
$bookItemRes = BookItemClass::addRoadNum($bookItemRes, $remainNeedSendNum, $cg, $shop, $currentItemInfo, $params);
//确认已分配数量增加
$cgItem->confirmSendNum = bcadd($cgItem->confirmSendNum, $remainNeedSendNum);
$cgItem->save();
$sendInfo = CgOrderItemSendClass::getByCondition(['parentId' => $cgItemId, 'customId' => $customId, 'itemId' => $productId], true);
$bookItemCustomId = $bookItemCustomRes->id ?? 0;
$currentCustom = $customList[$customId] ?? null;
if (empty($currentCustom)) {
util::fail('客户信息缺失,编号545');
}
if (empty($sendInfo)) {
$sendData = [
'mainId' => $mainId,
'parentOrderId' => $cgId,
'parentId' => $cgItemId,
'bookGhsGiveId' => 0,
'name' => $currentItemInfo->name ?? '',
'py' => $currentItemInfo->py ?? '',
'itemId' => $productId,
'ptItemId' => $ptItemId,
'customId' => $customId,
'customName' => $currentCustom->name ?? '',
'customPy' => $currentCustom->py ?? '',
'seatSn' => $currentCustom->seatSn ?? 0,
'num' => 0,
'bookItemCustomId' => $bookItemCustomId,
'status' => 0,
'staffId' => $currentItemInfo->cgStaffId ?? 0,
'staffName' => $currentItemInfo->cgStaffName ?? '',
];
$sendInfo = CgOrderItemSendClass::add($sendData, true);
}
$sendInfo->num = bcadd($sendInfo->num, $remainNeedSendNum);
$sendInfo->save();
$sendInfoId = $sendInfo->id ?? 0;
$giveGhs = BookItemGhsClass::getByCondition(['parentId' => $bookItemCustomId, 'ghsId' => $ghsId, 'itemId' => $productId,], true);
if (empty($giveGhs)) {
$giveGhsData = [
'mainId' => $mainId,
'bookSn' => $bookSn,
'parentId' => $bookItemCustomId,
'cgSendId' => $sendInfoId,
'name' => $currentItemInfo->name ?? '',
'py' => $currentItemInfo->py ?? '',
'itemId' => $productId,
'ptItemId' => $ptItemId,
'ghsId' => $ghsId,
'ghsName' => $ghsName,
'cgId' => $cgId,
'cgItemId' => $cgItemId,
'customId' => $customId,
'customName' => $currentCustom->name ?? '',
'staffId' => $currentItemInfo->cgStaffId ?? 0,
'staffName' => $currentItemInfo->cgStaffName ?? '',
'status' => 0,
];
$giveGhs = BookItemGhsClass::add($giveGhsData, true);
}
$giveGhs->num = bcadd($giveGhs->num, $remainNeedSendNum);
$giveGhs->save();
$giveGhsId = $giveGhs->id ?? 0;
$sendInfo->bookGhsGiveId = $giveGhsId;
$sendInfo->save();
} else {
$cgItem->noAssignHasDelNeedCg = 1;
$cgItem->save();
break;
}
}
}
return ['hasChange' => $hasChange];
}
//取消采购入库 ssh 20231218
public static function cancel($cg, $shop, $params)
{
$cg->status = self::PURCHASE_ORDER_STATUS_CANCEL;
$cg->save();
$orderSn = $cg->orderSn ?? '';
$mainId = $cg->mainId ?? 0;
$sjId = $cg->sjId ?? 0;
$shopId = $cg->shopId ?? 0;
$ghsName = $cg->ghsName ?? '';
$itemList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
if (empty($itemList)) {
util::fail('没有找到花材');
}
foreach ($itemList as $cgKey => $cgItem) {
$productId = $cgItem['productId'] ?? 0;
$currentNum = $cgItem['itemNum'] ?? 0;
$ptItemId = $cgItem['itemId'] ?? 0;
$presell = $cgItem['presell'] ?? 0;
$product = ProductClass::getLockById($productId);
if (empty($product)) {
util::fail('没有找到花材哦');
}
if ($presell == 0) {
//减少路上库存
$onRecord = [];
$onRecord['sjId'] = $sjId;
$onRecord['shopId'] = $shopId;
$onRecord['mainId'] = $mainId;
$onRecord['orderSn'] = $orderSn;
$onRecord['productId'] = $productId;
$onRecord['itemId'] = $ptItemId;
$onRecord['relateName'] = $ghsName;
$onRecord['itemNum'] = $currentNum;
$oldOnStock = $product->onStock ?? 0;
$onRecord['oldStock'] = $oldOnStock;
$onRecord['newStock'] = bcsub($oldOnStock, $currentNum);
$onRecord['ptStyle'] = 2;
OnStockRecordClass::cancelCg($onRecord);
ProductClass::decreaseOnStockByItemNum($productId, $currentNum);
}
}
$bookSn = $cg->bookSn ?? 0;
if ($shop->pfLevel == 1 && !empty($shop->bookSn)) {
if ($shop->bookSn == $bookSn) {
//取消分配的货位
$key = 'ghs_cg_order_scan_seat_' . $cg->id;
$noLock = util::lock($key);
if ($noLock) {
CgOrderItemClass::cancelAssign($cg, $shop, $params);
util::unlock($key);
} else {
util::fail('正在分配货位,请1秒后再取消');
}
}
}
}
public static function printGroup($current, $content)
{
//58mm的机器,一行打印16个汉字,32个字母
$bigPrice = $current['bigPrice'] ?? '';
$bigPrice = floatval($bigPrice);
$bigUnit = $current['bigUnit'] ?? '扎';
$aboutPrice = $current['aboutPrice'] ?? 0;
$aboutString = $aboutPrice == 1 ? '约' : '';
$name = $current['name'] ?? '';
if (floatval($bigPrice) == 0.01) {
$name = $name . '(送)';
}
$count = $current['itemNum'] ?? '';
$count = floatval($count);
$content .= '' . $name . ' ' . $count . $bigUnit . '
';
$totalPrice = $current['totalPrice'] ?? 0;
$content .= $count . $bigUnit . 'x' . $aboutString . $bigPrice . '元' . '=' . $totalPrice . '元
';
if (isset($current['refundNum']) && $current['refundNum'] > 0) {
$content .= '已退' . $current['refundNum'] . $bigUnit . '
';
}
$content .= '--------------------------------
';
return $content;
}
public static function addPrintNum($cg, $num = 1)
{
$cg->printNum += $num;
$cg->save();
if ($cg->printNum > 1) {
$mainId = $cg->mainId ?? 0;
if (getenv('YII_ENV') == 'production') {
//小向花卉采购单第二次打通要通知老板
if ($mainId == 23390) {
WxMessageClass::ghsCgRepeatPrintInform($cg);
}
if ($mainId == 50) {
//WxMessageClass::ghsCgRepeatPrintInform($cg);
}
} else {
if ($mainId == 644) {
//WxMessageClass::ghsCgRepeatPrintInform($cg);
}
}
}
return $cg;
}
//打印小票 ssh 20230608
public static function printTicket($orderInfo, $firstPrint = true, $order = 1)
{
$ghsId = $orderInfo->ghsId ?? 0;
$debtCount = PurchaseOrderClass::getCount(['ghsId' => $ghsId, 'debt' => PurchaseOrderClass::DEBT_YES]);
$debtAmount = PurchaseOrderClass::sum(['ghsId' => $ghsId, 'debt' => PurchaseOrderClass::DEBT_YES], 'actPrice');
$orderSn = $orderInfo->orderSn ?? '';
$itemList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
$shopId = $orderInfo->shopId ?? 0;
$mainId = $orderInfo->mainId ?? 0;
$shop = ShopClass::getById($shopId, true);
$telephone = $shop->telephone ?? '';
$sjId = $shop->sjId ?? 0;
$sj = SjClass::getById($sjId, true);
$sjName = $sj->name ?? '';
$shopName = $shop->shopName ?? '';
$currentName = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
if (getenv('YII_ENV') == 'production') {
//小向的采购不允许打小票
if (in_array($mainId, [23390])) {
return true;
}
}
$content = "" . $currentName . "";
$content .= "采购单";
$content .= '--------------------------------
';
$content .= '供货商
';
$content .= '' . $orderInfo->ghsName . '
';
$shortOrderSn = substr($orderSn, -4);
$content .= '核销码:' . $shortOrderSn . '
';
$seatSn = $orderInfo->seatSn ?? '';
if (!empty($seatSn)) {
$content .= '货位号:' . $seatSn . '
';
}
$currentTime = date("Y-m-d H:i", strtotime($orderInfo->entryTime));
$content .= '' . $currentTime . '
';
if (isset($orderInfo->remark) && !empty($orderInfo->remark)) {
$content .= '
';
$content .= '备注:
';
$content .= '' . $orderInfo->remark . '
';
}
$content .= '
';
$content .= '商品 数量/单价 金额
';
$content .= '--------------------------------
';
if (isset($itemList) && !empty($itemList)) {
foreach ($itemList as $current) {
$content = self::printGroup($current, $content);
}
}
$kindNum = $orderInfo->kind ?? 0;
$bigNum = $orderInfo->itemNum ? floatval($orderInfo->itemNum) : 0;
if (!empty($itemList)) {
$kindNum = 0;
$bigNum = 0;
foreach ($itemList as $row) {
$itemNumRow = floatval($row['itemNum'] ?? 0);
$refundNumRow = floatval($row['refundNum'] ?? 0);
$netNum = max(0, $itemNumRow - $refundNumRow);
if ($netNum > 0) {
$kindNum++;
$bigNum += $netNum;
}
}
}
$content .= '
********************************
';
$content .= "共{$kindNum}种";
$content .= " 数量:";
if ($bigNum > 0) {
$content .= "{$bigNum}";
}
$content .= "";
$content .= '********************************
';
if (isset($orderInfo['itemPrice']) && $orderInfo['itemPrice'] > 0) {
$content .= '商品金额:' . floatval($orderInfo['itemPrice']) . '元
';
}
if (isset($orderInfo['sendCost']) && $orderInfo['sendCost'] > 0) {
$content .= ' 运费:' . floatval($orderInfo['sendCost']) . '元
';
}
if (isset($orderInfo['packCost']) && $orderInfo['packCost'] > 0) {
$content .= ' 打包费:' . floatval($orderInfo['packCost']) . '元
';
}
$content .= '合计金额:' . floatval($orderInfo['prePrice']) . '元
';
if (isset($orderInfo['discountAmount']) && !empty($orderInfo['discountAmount']) && $orderInfo['discountAmount'] > 0) {
$content .= '优惠扣除:-' . floatval($orderInfo['discountAmount']) . '元
';
}
if (isset($orderInfo['tkPrice']) && $orderInfo['tkPrice'] > 0) {
$content .= '退款金额:' . floatval($orderInfo['tkPrice']) . '元
';
}
$content .= '订单金额:' . floatval($orderInfo['realPrice']) . '
';
if (isset($orderInfo['debt']) && $orderInfo['debt'] == 1) {
$content .= '实付金额:0元
';
} else {
$content .= '实付金额:' . floatval($orderInfo['realPrice']) . '元
';
}
// if ($debtAmount > 0) {
// $content .= '累计待结:' . $debtCount . '笔 ' . floatval($debtAmount) . '元
';
// }
$content .= '--------------------------------
';
$content .= '订单编号:' . $orderInfo['orderSn'] . '
';
if (!empty($telephone)) {
$content .= '联系电话:' . $telephone . '
';
}
if (isset($orderInfo['shopAdminName']) && !empty($orderInfo['shopAdminName'])) {
$content .= '录入人员:' . $orderInfo['shopAdminName'] . '
';
}
$content .= "
";
$bookSn = $shop->bookSn ?? 0;
if (!empty($bookSn)) {
$content .= "扫码查货位号";
$content .= "
";
$host = Yii::$app->params['ghsHost'];
$orderSn = $orderInfo->orderSn ?? '';
$salt = $orderInfo->salt ?? '';
$content .= "" . $host . "/#/pagesPurchase/info?orderSn=" . $orderSn . "&salt=" . $salt . "";
}
$content .= "
";
$ext = ShopExtClass::getByCondition(['shopId' => $shopId]);
$printSn = $ext['wcPrintSn'] ?? '';
if ($firstPrint) {
if (isset($orderInfo->fistWcPrintNum) && $orderInfo->fistWcPrintNum > 1) {
if ($orderInfo->fistWcPrintNum == 2) {
$printSn = $ext['wcPrintSn2'] ?? '';
//noticeUtil::push("使用第2台机器打印 {$printSn}", '15280215347');
}
if ($orderInfo->fistWcPrintNum == 3) {
$printSn = $ext['wcPrintSn3'] ?? '';
//noticeUtil::push("使用第3台机器打印 {$printSn}", '15280215347');
}
}
} else {
if ($order > 1) {
if ($order == 2) {
$printSn = $ext['wcPrintSn2'] ?? '';
//noticeUtil::push("使用第2台机器打印 {$printSn}", '15280215347');
}
if ($order == 3) {
$printSn = $ext['wcPrintSn3'] ?? '';
//noticeUtil::push("使用第3台机器打印 {$printSn}", '15280215347');
}
}
}
if (!empty($printSn)) {
self::addPrintNum($orderInfo);
$p = new printUtil($printSn);
$p->printMsg($content, $shop);
}
}
public static function putIn($cg, $data)
{
if ($cg->status == self::PURCHASE_ORDER_STATUS_COMPLETE) {
util::fail('已经入过库了');
}
if ($cg->status == self::PURCHASE_ORDER_STATUS_CANCEL) {
util::fail('已取消');
}
$orderSn = $cg->orderSn ?? '';
$mainId = $cg->mainId ?? 0;
$sjId = $cg->sjId ?? 0;
$shopId = $cg->shopId ?? 0;
$staffId = $data['staffId'] ?? 0;
$staffName = $data['staffName'] ?? '';
$adminId = $data['adminId'] ?? 0;
$shop = ShopClass::getLockById($shopId);
if (empty($shop)) {
util::fail('没有找到门店26');
}
$ghsName = $cg->ghsName ?? '';
$itemList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
if (empty($itemList)) {
util::fail('没有找到花材');
}
$bookSn = $shop->bookSn ?? 0;
$pfLevel = $shop->pfLevel ?? 0;
if ($pfLevel == 1 && !empty($bookSn)) {
if ($cg->bookSn == $bookSn) {
//入库前再确认分配货位
$key = 'ghs_cg_order_scan_seat_' . $cg->id;
$noLock = util::lock($key);
if ($noLock) {
//可以获得锁,没有在分配货位,可以进行货位分配
$params = ['staffId' => $staffId, 'staffName' => $staffName];
self::assign($cg, $shop, $params);
util::unlock($key);
//分配货位后上架和入库
$shop = $data['shop'] ?? null;
CgOrderItemClass::assignToPutOn($cg, $shop, $params);
} else {
util::fail('正在分配货位,请1秒后再提交');
}
}
}
foreach ($itemList as $cgKey => $cgItem) {
$productId = $cgItem['productId'] ?? 0;
$currentNum = $cgItem['itemNum'] ?? 0;
$ptItemId = $cgItem['itemId'] ?? 0;
$unitCost = $cgItem['cost'] ?? 0;
$presell = $cgItem['presell'] ?? 0;
$currentCost = $cgItem['cost'] ?? 0;
$product = ProductClass::getLockById($productId);
if (empty($product)) {
util::fail('没有找到花材哦');
}
//有新入库且 limitBuy=0,则清空限购记录缓存
if ($product->limitBuy == 0) {
ProductClass::clearLimitBuyCache($productId);
\bizHd\product\classes\ProductClass::clearLimitBuyCache($productId);
}
//库存为0时重置特价
if (intval($product->stock) == 0) {
$product->discountPrice = 0;
$product->skDiscountPrice = 0;
$product->hjDiscountPrice = 0;
}
if ($presell == 0) {
//加库存和流水记录
$stockInfo = ProductClass::addStockByItemNum($productId, $currentNum, ['cost' => $currentCost]);
$record = [];
$record['sjId'] = $sjId;
$record['shopId'] = $shopId;
$record['mainId'] = $mainId;
$record['orderSn'] = $orderSn;
$record['productId'] = $productId;
$record['itemId'] = $ptItemId;
$record['relateName'] = $ghsName;
$record['itemNum'] = $currentNum;
$record['oldStock'] = $stockInfo['oldStock'];
$record['newStock'] = $stockInfo['newStock'];
$record['totalCost'] = $stockInfo['totalCost'] ?? 0;
$record['totalStock'] = $stockInfo['totalStock'] ?? 0;
$record['avCost'] = $stockInfo['avCost'] ?? 0;
$record['changeCost'] = $stockInfo['changeCost'] ?? 0;
$record['unitCost'] = $stockInfo['unitCost'] ?? 0;
StockRecordClass::addPurchaseOrderRecord($record);
//减少路上库存
$onRecord = [];
$onRecord['sjId'] = $sjId;
$onRecord['shopId'] = $shopId;
$onRecord['mainId'] = $mainId;
$onRecord['orderSn'] = $orderSn;
$onRecord['productId'] = $productId;
$onRecord['itemId'] = $ptItemId;
$onRecord['relateName'] = $ghsName;
$onRecord['itemNum'] = $currentNum;
$oldOnStock = $product->onStock ?? 0;
$onRecord['oldStock'] = $oldOnStock;
$onRecord['newStock'] = bcsub($oldOnStock, $currentNum);
$onRecord['ptStyle'] = 2;
OnStockRecordClass::addInStockOrderRecord($onRecord);
ProductClass::decreaseOnStockByItemNum($productId, $currentNum);
}
$product->cost = $unitCost;
$product->priceLabel = ProductClass::PRICE_LABEL_AUTO;
$product->save();
//成本变动记录
$changeData = [
'ptStyle' => 2,
'sjId' => $sjId,
'mainId' => $mainId,
'itemId' => $productId,
'ptItemId' => $ptItemId,
'cost' => $unitCost,
'staffId' => $staffId,
'staffName' => $staffName,
'name' => $product->name ?? '',
'cover' => $product->cover ?? '',
'targetId' => $order->id ?? 0,
'event' => '采购',
];
CostChangeClass::addData($changeData);
if (isset($shop->default) && $shop->default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
//直营门店的成本价也要一起改掉
$allShop = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
if (!empty($allShop)) {
$currentItemId = $product->itemId ?? 0;
foreach ($allShop as $currentShop) {
$currentId = $currentShop->mainId ?? 0;
if ($currentId == $mainId) {
continue;
}
if (isset($currentShop->join) && $currentShop->join == 1) {
continue;
}
$currentProduct = ProductClass::getByCondition(['mainId' => $currentId, 'itemId' => $currentItemId], true);
if (!empty($currentProduct)) {
$currentProduct->cost = $unitCost;
$currentProduct->save();
$currentStaff = ShopAdminClass::getByCondition(['mainId' => $currentProduct->mainId, 'adminId' => $adminId], true);
//成本变动记录
$changeData = [
'ptStyle' => 2,
'sjId' => $currentProduct->sjId ?? 0,
'mainId' => $currentProduct->mainId ?? 0,
'itemId' => $currentProduct->id ?? 0,
'ptItemId' => $currentProduct->itemId ?? 0,
'cost' => $unitCost,
'staffId' => $currentStaff->id ?? 0,
'staffName' => $currentStaff->name ?? '',
'name' => $currentProduct->name ?? '',
'cover' => $currentProduct->cover ?? '',
'targetId' => $order->id ?? 0,
'event' => $shop->shopName . '采购',
];
CostChangeClass::addData($changeData);
}
}
}
}
}
//门店支出增加
$currentPrice = $cg->actPrice ?? 0;
$mainId = $shop->mainId ?? 0;
$main = MainClass::getLockById($mainId);
if (empty($main)) {
util::fail('没有资产信息');
}
//支出增加
$currentTotalExpend = bcadd($main->totalExpend, $currentPrice, 2);
$main->totalExpend = $currentTotalExpend;
$main->save();
//采购增加
$main->cgFinish += 1;
$main->totalPurchaseOrder += 1;
$currentTotalPurchase = bcadd($main->totalPurchase, $currentPrice, 2);
$main->totalPurchase = $currentTotalPurchase;
$main->save();
//供货商资产增加
$ghsId = $cg->ghsId ?? 0;
$ghs = GhsClass::getLockById($ghsId);
if (empty($ghs)) {
util::fail('没有找到供货商');
}
self::applyPurchaseDebtOnBuyerGhs($ghs, $currentPrice, $cg, $sjId, $shopId);
//采购统计
$cgNum = $cg->itemNum ?? 0;
StatCgClass::replace($main, $shop, $currentPrice, $cgNum);
//采购按供货商统计
StatCgGhsClass::ghsReplace($cg);
//门店应收客户款增加
$ghsShopId = $ghs->shopId;
$ghsShop = ShopClass::getLockById($ghsShopId);
if (empty($ghsShop)) {
util::fail('没有找到门店27');
}
$ghsMainId = $ghsShop->mainId ?? 0;
$ghsMain = \bizGhs\shop\classes\MainClass::getLockById($ghsMainId);
if (empty($ghsMain)) {
util::fail('没有main信息22');
}
$currentMayGathering = bcadd($ghsMain->mayGathering, $currentPrice, 2);
$ghsMain->mayGathering = $currentMayGathering;
self::applyPurchaseDebtOnSupplierCustom($ghs, $currentPrice, $ghsMain);
$ghsMain->save();
//当天和当月支出统计
StatOutClass::updateOrInsert($main, $shop, $currentPrice);
//支出流水
$payWay = dict::getDict('payWay', 'unknown');
$capitalType = dict::getDict('capitalType', 'ghsPurchase', 'id');
$event = '采购';
$mainId = $shop->mainId ?? 0;
$capitalData = [
'capitalType' => $capitalType,
'io' => 0,
'totalExpend' => $currentTotalExpend,
'payWay' => $payWay,
'amount' => $currentPrice,
'sjId' => $sjId,
'shopId' => $shopId,
'event' => $event,
'mainId' => $mainId
];
ShopCapitalClass::addCapital($capitalData);
$status = self::PURCHASE_ORDER_STATUS_COMPLETE;
$cg->debt = self::DEBT_YES;
$cg->status = $status;
$update = ['status' => $status];
if (isset($data['entryTime']) && !empty($data['entryTime'])) {
$cg->entryTime = $data['entryTime'];
$update['entryTime'] = $data['entryTime'];
}
PurchaseOrderItemClass::updateByCondition(['orderSn' => $orderSn], $update);
$cg->save();
return $cg;
}
//添加采购单 lqh 2021.1.20
public static function addOrder($data)
{
$sjId = $data['sjId'] ?? 0;
$shopId = $data['shopId'] ?? 0;
$mainId = $data['mainId'] ?? 0;
$ghsName = $data['ghsName'] ?? '';
$staffId = $data['staffId'] ?? 0;
$staffName = $data['staffName'] ?? '';
$inType = $data['inType'] ?? self::IN_TYPE_NOW;
$adminId = $data['adminId'] ?? 0;
$orderSn = orderSn::getGhsPurchaseSn();
$data['orderSn'] = $orderSn;
$checkCode = substr($orderSn, -4);
$data['checkCode'] = $checkCode;
$status = $inType == self::IN_TYPE_NOW ? self::PURCHASE_ORDER_STATUS_COMPLETE : self::PURCHASE_ORDER_STATUS_SENDING;
$data['status'] = $status;
//数据结构 [{itemId:0,bigNum:0,smallNum:0,productId:12,itemPrice:1,weight:1}]
$ghsItemInfo = $data['itemInfo'];
$ghsItemInfo = self::mergeItemInfo($ghsItemInfo);
//总共多少扎
$totalBigNum = 0;
//总共多少支
$totalSmallNum = 0;
//总数量
$totalNum = 0;
$kindNum = 0;
$totalItemPrice = 0;
$totalWeight = 0;
$data['confirm'] = 1;
if (getenv('YII_ENV') == 'production') {
if (in_array($mainId, [23390])) {
$data['confirm'] = 0;
}
} else {
if (in_array($mainId, [644])) {
$data['confirm'] = 0;
}
}
$productIds = array_column($ghsItemInfo, 'productId');
$productIds = array_unique(array_filter($productIds));
$productList = ProductClass::getByIds($productIds, null, 'id');
foreach ($ghsItemInfo as $v) {
$ratioType = $v['ratioType'] ?? 1;
$ratio = $v['ratio'] ?? 1;
$currentProductId = $v['productId'] ?? 0;
$currentTotal = $v['totalPrice'] ?? 0;
$currentAboutPrice = $v['aboutPrice'] ?? 0;
$unitWeight = $productList[$currentProductId]['weight'] ?? 0;
if (isset($v['itemPrice']) == false || $v['itemPrice'] <= 0) {
util::fail('请填写价格');
}
if (isset($v['bigNum']) && $v['bigNum'] > 0) {
$thisBigNum = $v['bigNum'];
$totalBigNum = bcadd($totalBigNum, $thisBigNum, 2);
$totalNum = bcadd($totalNum, $thisBigNum, 2);
if ($currentAboutPrice == 1) {
if ($currentTotal <= 0) {
util::fail('请填写总金额');
}
$currentTotalPrice = $currentTotal;
} else {
$currentTotalPrice = bcmul($thisBigNum, $v['itemPrice'], 2);
}
$weight = bcmul($unitWeight, $thisBigNum, 2);
} else {
if (isset($v['smallNum']) == false || $v['smallNum'] <= 0) {
util::fail('请填写数量');
}
if ($ratioType == 1) {
//util::fail('小单位采购,单位比必须固定');
}
if ($ratio <= 1) {
//util::fail('小单位采购,单位比必须大于1');
}
$thisSmallNum = $v['smallNum'];
$changeToBigNum = bcdiv($thisSmallNum, $ratio, 2);
$totalNum = bcadd($totalNum, $changeToBigNum, 2);
$totalSmallNum = bcadd($totalSmallNum, $thisSmallNum, 2);
if ($currentAboutPrice == 1) {
if ($currentTotal <= 0) {
util::fail('请填写总金额');
}
$currentTotalPrice = $currentTotal;
} else {
$currentTotalPrice = bcmul($thisSmallNum, $v['itemPrice'], 2);
}
$weight = bcmul($unitWeight, $changeToBigNum, 2);
}
$kindNum++;
$totalItemPrice = bcadd($totalItemPrice, $currentTotalPrice, 2);
$totalWeight = bcadd($totalWeight, $weight, 2);
}
$data['bigNum'] = $totalBigNum;
$data['smallNum'] = $totalSmallNum;
$data['itemNum'] = $totalNum;
$data['kind'] = $kindNum;
$data['price'] = $totalItemPrice;
$data['totalWeight'] = $totalWeight;
//计算订单总价
$data['packingCharge'] = isset($data['packingCharge']) && $data['packingCharge'] > 0 ? $data['packingCharge'] : 0;
$data['shortCharge'] = isset($data['shortCharge']) && $data['shortCharge'] > 0 ? $data['shortCharge'] : 0;
$data['longCharge'] = isset($data['longCharge']) && $data['longCharge'] > 0 ? $data['longCharge'] : 0;
$data['pickCharge'] = isset($data['pickCharge']) && $data['pickCharge'] > 0 ? $data['pickCharge'] : 0;
$data['localCharge'] = isset($data['localCharge']) && $data['localCharge'] > 0 ? $data['localCharge'] : 0;
$cgModel = $data['cgModel'] ?? 0;
if ($cgModel == 1) {
//简约采购模式
$customAvgKgWeight = $data['avgKgWeight'] ?? 0;
$data['longCharge'] = bcmul($totalWeight, $customAvgKgWeight, 2);
unset($data['modifyPrice']);
} else {
//正常采购模式不要使用传过来的每公斤运费
unset($data['avgKgWeight']);
}
$data['entryTime'] = isset($data['entryTime']) && !empty($data['entryTime']) ? $data['entryTime'] . ' 12:00:00' : date('Y-m-d H:i:s');
$packingCharge = $data['packingCharge'] ?? 0;
$shortCharge = $data['shortCharge'] ?? 0;
$longCharge = $data['longCharge'] ?? 0;
$pickCharge = $data['pickCharge'] ?? 0;
$localCharge = $data['localCharge'] ?? 0;
//提货费和本地运费
$paidPrice = bcadd($localCharge, $pickCharge, 2);
$yfPayWay = $data['yfPayWay'] ?? self::YF_PAY_WAY_JF;
//打包费
$currentPrice = bcadd($totalItemPrice, $packingCharge, 2);
//寄付才算长途和短途运费
if ($yfPayWay == self::YF_PAY_WAY_JF) {
$currentPrice = bcadd($currentPrice, $shortCharge, 2);
$currentPrice = bcadd($currentPrice, $longCharge, 2);
} elseif ($yfPayWay == self::YF_PAY_WAY_DF) {
$paidPrice = bcadd($paidPrice, $shortCharge, 2);
$paidPrice = bcadd($paidPrice, $longCharge, 2);
} else {
util::fail('没有找到这个运费支付方式');
}
// $paidPrice 字段说明,此字段表示收货方已付款金额,包括 $localCharge本地运费、 $pickCharge提货费、 $shortCharge短途运费(到付时算,如果不是到付,不算在$paidPrice里)、$longCharge长途运费(到付时算,如果不是到付,不算在$paidPrice里)
//请搜索关键词 cg_item_cost ,有多处相似情况要考虑
$data['prePrice'] = $currentPrice;
if (isset($data['modifyPrice']) && !empty($data['modifyPrice']) && $data['modifyPrice'] > 0) {
//报损减免后金额
$modifyPrice = $data['modifyPrice'] ?? 0;
if ($modifyPrice > $currentPrice) {
util::fail("应付金额大于总金额,应付金额:{$modifyPrice},总金额:{$currentPrice}");
}
if ($modifyPrice < $currentPrice) {
$data['discountAmount'] = bcsub($currentPrice, $modifyPrice, 2);
$data['discountType'] = dict::getDict('discountType', 'discount');
$currentPrice = $modifyPrice;
}
}
$data['orderPrice'] = $currentPrice;
$data['actPrice'] = $currentPrice;
$data['realPrice'] = $currentPrice;
$data['paidPrice'] = $paidPrice;
//总运费
$totalFreight = bcadd($packingCharge, $shortCharge, 2);
$totalFreight = bcadd($totalFreight, $longCharge, 2);
$totalFreight = bcadd($totalFreight, $pickCharge, 2);
$totalFreight = bcadd($totalFreight, $localCharge, 2);
$data['totalFreight'] = $totalFreight;
$connection = Yii::$app->db;
$transaction = $connection->beginTransaction();
try {
//总运费/总公斤数,计算每公斤的运费
if ($totalWeight > 0) {
$avgKgWeight = bcdiv($totalFreight, $totalWeight, 3);
} else {
$avgKgWeight = 0;
}
$avgKgWeight = round($avgKgWeight, 2);
$data['avgKgWeight'] = $avgKgWeight;
$data['salt'] = stringUtil::charsShuffleLowerCase(8);
$data['cgStyle'] = dict::getDict('cgStyle', 'ghs');
$order = self::add($data, true);
$shop = ShopClass::getLockById($shopId);
if (empty($shop)) {
util::fail('没有找到门店28');
}
//组装详情表数据
$batchData = self::groupOrderItem($ghsItemInfo, $orderSn);
//加库存
foreach ($batchData as $currentKey => $v) {
$itemId = $v['itemId'];
$productId = $v['productId'];
//单位采购价
$unitCgPrice = $v['bigPrice'] ?? 0;
$presell = $productList[$productId]['presell'] ?? 0;
//普通入库增加库存,预售入库不增加库存 ssh 20220109
if ($presell == 0) {
//增加路上库存
$onRecord = [];
$onRecord['sjId'] = $sjId;
$onRecord['shopId'] = $shopId;
$onRecord['mainId'] = $mainId;
$onRecord['orderSn'] = $orderSn;
$onRecord['productId'] = $productId;
$onRecord['itemId'] = $itemId;
$onRecord['relateName'] = $ghsName;
$onRecord['itemNum'] = $v['itemNum'];
$oldOnStock = $v['onStock'] ?? 0;
$onRecord['oldStock'] = $oldOnStock;
$newOnStock = bcadd($oldOnStock, $v['itemNum']);
$onRecord['newStock'] = $newOnStock;
$onRecord['ptStyle'] = 2;
OnStockRecordClass::addPurchaseOrderRecord($onRecord);
ProductClass::addOnStockByItemNum($productId, $v['itemNum']);
if ($inType == self::IN_TYPE_NOW) {
//增加库存
$stockInfo = ProductClass::addStockByItemNum($productId, $v['itemNum'], ['cost' => $unitCgPrice]);
$record = [];
$record['sjId'] = $sjId;
$record['shopId'] = $shopId;
$record['mainId'] = $mainId;
$record['orderSn'] = $orderSn;
$record['productId'] = $productId;
$record['itemId'] = $itemId;
$record['relateName'] = $ghsName;
$record['itemNum'] = $v['itemNum'];
$record['oldStock'] = $stockInfo['oldStock'];
$record['newStock'] = $stockInfo['newStock'];
$record['unitCost'] = $stockInfo['unitCost'];
$record['changeCost'] = $stockInfo['changeCost'];
$record['totalStock'] = $stockInfo['totalStock'];
$record['totalCost'] = $stockInfo['totalCost'];
$record['avCost'] = $stockInfo['avCost'];
StockRecordClass::addPurchaseOrderRecord($record);
//减少路上库存
$onRecord = [];
$onRecord['sjId'] = $sjId;
$onRecord['shopId'] = $shopId;
$onRecord['mainId'] = $mainId;
$onRecord['orderSn'] = $orderSn;
$onRecord['productId'] = $productId;
$onRecord['itemId'] = $itemId;
$onRecord['relateName'] = $ghsName;
$onRecord['itemNum'] = $v['itemNum'];
$onRecord['oldStock'] = $newOnStock;
$onRecord['newStock'] = bcsub($newOnStock, $v['itemNum']);
$onRecord['ptStyle'] = 2;
OnStockRecordClass::addInStockOrderRecord($onRecord);
ProductClass::decreaseOnStockByItemNum($productId, $v['itemNum']);
}
}
$productWeight = $productList[$productId]['weight'] ?? 0;
$unitFreight = bcmul($avgKgWeight, $productWeight, 3);
$unitCost = bcadd($unitCgPrice, $unitFreight, 3);
$unitCost = round($unitCost, 2);
$batchData[$currentKey]['bigFreight'] = $unitFreight;
$batchData[$currentKey]['cost'] = $unitCost;
$currentCover = $productList[$productId]['cover'] ?? '';
$currentName = $productList[$productId]['name'] ?? '';
$batchData[$currentKey]['cover'] = $currentCover;
$batchData[$currentKey]['name'] = $currentName;
$currentBigUnit = $productList[$productId]['bigUnit'] ?? '';
$currentSmallUnit = $productList[$productId]['smallUnit'] ?? '';
$currentRatio = $productList[$productId]['ratio'] ?? 0;
$currentRatioType = $productList[$productId]['ratioType'] ?? 0;
$currentClassId = $productList[$productId]['classId'] ?? 0;
$batchData[$currentKey]['bigUnit'] = $currentBigUnit;
$batchData[$currentKey]['smallUnit'] = $currentSmallUnit;
$batchData[$currentKey]['ratio'] = $currentRatio;
$batchData[$currentKey]['ratioType'] = $currentRatioType;
$batchData[$currentKey]['presell'] = $presell;
$batchData[$currentKey]['classId'] = $currentClassId;
$batchData[$currentKey]['entryTime'] = $order->entryTime ?? date("Y-m-d H:i:s");
$batchData[$currentKey]['addTime'] = $order->addTime ?? date("Y-m-d H:i:s");
$batchData[$currentKey]['status'] = $order->status ?? 1;
$batchData[$currentKey]['cgStaffId'] = $order->cgStaffId ?? 0;
$batchData[$currentKey]['cgStaffName'] = $order->cgStaffName ?? '';
$batchData[$currentKey]['staffId'] = $order->shopAdminId ?? 0;
$batchData[$currentKey]['staffName'] = $order->shopAdminName ?? '';
if ($inType == self::IN_TYPE_NOW) {
$product = ProductClass::getLockById($productId);
$product->cost = $unitCost;
$product->priceLabel = ProductClass::PRICE_LABEL_AUTO;
$product->save();
//成本变动记录
$changeData = [
'ptStyle' => 2,
'sjId' => $sjId,
'mainId' => $mainId,
'itemId' => $productId,
'ptItemId' => $itemId,
'cost' => $unitCost,
'staffId' => $staffId,
'staffName' => $staffName,
'name' => $product->name ?? '',
'cover' => $product->cover ?? '',
'targetId' => $order->id ?? 0,
'event' => '采购',
];
CostChangeClass::addData($changeData);
if (isset($shop->default) && $shop->default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
//直营门店的成本价也要一起改掉
$allShop = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
if (!empty($allShop)) {
$currentItemId = $product->itemId ?? 0;
foreach ($allShop as $currentShop) {
$currentId = $currentShop->mainId ?? 0;
if ($currentId == $mainId) {
continue;
}
if (isset($currentShop->join) && $currentShop->join == 1) {
continue;
}
$currentProduct = ProductClass::getByCondition(['mainId' => $currentId, 'itemId' => $currentItemId], true);
if (!empty($currentProduct)) {
$currentProduct->cost = $unitCost;
$currentProduct->save();
$currentStaff = ShopAdminClass::getByCondition(['mainId' => $currentProduct->mainId, 'adminId' => $adminId], true);
//成本变动记录
$changeData = [
'ptStyle' => 2,
'sjId' => $currentProduct->sjId ?? 0,
'mainId' => $currentProduct->mainId ?? 0,
'itemId' => $currentProduct->id ?? 0,
'ptItemId' => $currentProduct->itemId ?? 0,
'cost' => $unitCost,
'staffId' => $currentStaff->id ?? 0,
'staffName' => $currentStaff->name ?? '',
'name' => $currentProduct->name ?? '',
'cover' => $currentProduct->cover ?? '',
'targetId' => $order->id ?? 0,
'event' => $shop->shopName . '采购',
];
CostChangeClass::addData($changeData);
}
}
}
}
}
}
//写入详情表
PurchaseOrderItemClass::batchAddOrderItem($batchData);
if ($inType == self::IN_TYPE_NOW) {
//门店支出增加
$mainId = $shop->mainId ?? 0;
$main = MainClass::getLockById($mainId);
if (empty($main)) {
util::fail('没有资产信息');
}
//支出增加
$currentTotalExpend = bcadd($main->totalExpend, $currentPrice, 2);
$main->totalExpend = $currentTotalExpend;
$main->save();
//采购增加
$main->cgFinish += 1;
$main->totalPurchaseOrder += 1;
$currentTotalPurchase = bcadd($main->totalPurchase, $currentPrice, 2);
$main->totalPurchase = $currentTotalPurchase;
$main->save();
//供货商资产增加
$ghsId = $data['ghsId'] ?? 0;
$ghs = GhsClass::getLockById($ghsId);
if (empty($ghs)) {
util::fail('没有找到供货商');
}
self::applyPurchaseDebtOnBuyerGhs($ghs, $currentPrice, $order, $sjId, $shopId);
//采购统计
$cgNum = $order->itemNum ?? 0;
StatCgClass::replace($main, $shop, $currentPrice, $cgNum);
//采购按供货商统计
StatCgGhsClass::ghsReplace($order);
//门店应收客户款增加
$ghsShopId = $ghs->shopId;
$ghsShop = ShopClass::getLockById($ghsShopId);
if (empty($ghsShop)) {
util::fail('没有找到门店29');
}
$ghsMainId = $ghsShop->mainId ?? 0;
$ghsMain = \bizGhs\shop\classes\MainClass::getLockById($ghsMainId);
if (empty($ghsMain)) {
util::fail('没有main信息23');
}
$currentMayGathering = bcadd($ghsMain->mayGathering, $currentPrice, 2);
$ghsMain->mayGathering = $currentMayGathering;
self::applyPurchaseDebtOnSupplierCustom($ghs, $currentPrice, $ghsMain);
$ghsMain->save();
$order->debt = self::DEBT_YES;
$order->save(false, ['debt']);
//当天和当月支出统计
StatOutClass::updateOrInsert($main, $shop, $currentPrice);
//支出流水
$payWay = dict::getDict('payWay', 'unknown');
$capitalType = dict::getDict('capitalType', 'ghsPurchase', 'id');
$sjId = $order->sjId ?? 0;
$shopId = $order->shopId ?? 0;
$event = '采购';
$mainId = $shop->mainId ?? 0;
$capitalData = [
'capitalType' => $capitalType,
'io' => 0,
'totalExpend' => $currentTotalExpend,
'payWay' => $payWay,
'amount' => $currentPrice,
'sjId' => $sjId,
'shopId' => $shopId,
'event' => $event,
'mainId' => $mainId
];
ShopCapitalClass::addCapital($capitalData);
}
$transaction->commit();
return $order;
} catch (\Exception $exception) {
$transaction->rollBack();
util::fail('保存失败:' . $exception->getMessage());
}
return false;
}
//采购单列表 lqh 2021.1.19
public static function getOrderList($where)
{
$data = self::getList('*', $where, 'addTime DESC');
$list = $data['list'] ?? [];
$data['list'] = self::groupSupplierList($list);
$data['list'] = self::groupAdminList($data['list']);
$data['list'] = self::groupStatusName($data['list']);
//订单列表页需要显示花材
if (!empty($list)) {
foreach ($list as $key => $order) {
$orderSn = $order['orderSn'] ?? '';
$itemList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, 'id,name,itemNum,refundNum,aboutPrice,bigPrice');
if (!empty($itemList)) {
$data['list'][$key]['itemList'] = $itemList;
}
}
}
return $data;
}
//获取订单详情信息 lqh 2021.1.20
public static function getOrderDetail($orderSn, $shopId)
{
$where = ['orderSn' => $orderSn];
$orderData = self::getByCondition($where);
if (empty($orderData)) {
util::fail('订单不存在');
}
if (isset($orderData['shopId']) == false || $orderData['shopId'] != $shopId) {
util::fail('无法访问,编号669');
}
$orderInfo = PurchaseOrderItemClass::getOrderItemDetail($orderSn);
if ($orderInfo) {
foreach ($orderInfo as $key => $val) {
$id = $val['id'] ?? 0;
$shortCover = $val['cover'] ?? '';
$orderInfo[$key]['ShortCover'] = $shortCover;
$orderInfo[$key]['itemNum'] = $val['itemNum'] ? floatval($val['itemNum']) : 0;
$orderInfo[$key]['bigPrice'] = $val['bigPrice'] ? floatval($val['bigPrice']) : 0;
$orderInfo[$key]['totalPrice'] = $val['totalPrice'] ? floatval($val['totalPrice']) : 0;
$orderInfo[$key]['cover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
$orderInfo[$key]['bigCover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
$sendList = CgOrderItemSendClass::getAllByCondition(['parentId' => $id], null, '*');
if (!empty($sendList)) {
foreach ($sendList as $kk => $vv) {
//前端使用
$sendList[$kk]['currentRefundNum'] = '';
}
}
$orderInfo[$key]['sendList'] = $sendList;
$num = $val['itemNum'] ?? 0;
$confirmSendNum = $val['confirmSendNum'] ?? 0;
$orderInfo[$key]['noSeatNum'] = 0;
if ($num > $confirmSendNum) {
$orderInfo[$key]['noSeatNum'] = floatval(bcsub($num, $confirmSendNum, 2));
}
}
}
$orderData = self::groupAdmin($orderData);
//供货商信息
$ghsId = $orderData['ghsId'] ?? 0;
$ghsInfo = GhsClass::getGhsInfo($ghsId);
$orderData['supplierName'] = $ghsInfo['name'] ?? '';
$orderData['supplierMobile'] = $ghsInfo['mobile'] ?? '';
$orderData['supplierAddress'] = $ghsInfo['address'] ?? '';
$orderData['debtAmount'] = $ghsInfo['debtAmount'] ?? 0;
$orderData['itemInfo'] = $orderInfo;
$orderData['statusName'] = self::getStatusName($orderData['status']);
//1寄付 2到付
$yfPayWay = $orderData['yfPayWay'] ?? 1;
$actPrice = $orderData['actPrice'] ?? 0;
$shortCharge = $orderData['shortCharge'] ?? 0;
$longCharge = $orderData['longCharge'] ?? 0;
$pickCharge = $orderData['pickCharge'] ?? 0;
$localCharge = $orderData['localCharge'] ?? 0;
$totalCgCost = $actPrice;
if ($yfPayWay == 2) {
$totalCgCost = bcadd($totalCgCost, $shortCharge, 2);
$totalCgCost = bcadd($totalCgCost, $longCharge, 2);
}
$totalCgCost = bcadd($totalCgCost, $pickCharge, 2);
$totalCgCost = bcadd($totalCgCost, $localCharge, 2);
$orderData['totalCgCost'] = $totalCgCost;
$shop = ShopClass::getById($shopId, true);
$shopBookSn = $shop->bookSn ?? 0;
$bookSn = $orderData['bookSn'] ?? 0;
//是否需要提醒更新货位
$needRemindRefreshSeat = 0;
$inBooking = 0;
if (!empty($shopBookSn) && $shopBookSn == $bookSn) {
//在预订状态,用于采购单售后时判断是否也要退货位上花材
$inBooking = 1;
if (isset($orderData['status']) && $orderData['status'] == 3) {
$needRemindRefreshSeat = 1;
}
}
$orderData['needRemindRefreshSeat'] = $needRemindRefreshSeat;
$orderData['inBooking'] = $inBooking;
return $orderData;
}
// 组装 orderItem lqh 2021.1.25
public static function groupOrderItem($ghsItemInfo, $orderSn)
{
$productData = self::getProductMapData($ghsItemInfo);
$batchData = [];
foreach ($ghsItemInfo as $v) {
$tmp = [];
$productId = $v['productId'];
$currentAboutPrice = $v['aboutPrice'] ?? 0;
$currentTotal = $v['totalPrice'] ?? 0;
$assignSeat = $v['assignSeat'] ?? 1;
$itemId = $productData[$productId]['itemId'];
$tmp['orderSn'] = $orderSn;
$tmp['assignSeat'] = $assignSeat;
$tmp['itemId'] = $itemId;
$tmp['belongCost'] = $productData[$productId]['belongCost'] ?? 0;
$tmp['productId'] = $v['productId'];
$tmp['itemStock'] = $productData[$productId]['stock'] ?? 0;
$tmp['onStock'] = $productData[$productId]['onStock'] ?? 0;
$tmp['aboutPrice'] = $currentAboutPrice;
if (isset($v['bigNum']) && $v['bigNum'] > 0) {
$tmp['itemNum'] = $v['bigNum'];
$tmp['bigPrice'] = $v['itemPrice'];
if ($currentAboutPrice == 1) {
$totalPrice = $currentTotal;
} else {
$totalPrice = bcmul($v['itemPrice'], $tmp['itemNum'], 2);
}
$tmp['totalPrice'] = $totalPrice;
} else {
$smallNum = $v['smallNum'] ?? 0;
if ($smallNum <= 0) {
util::fail('没有数量');
}
$ratioType = $v['ratioType'] ?? 1;
if ($ratioType == 1) {
//util::fail('小单位,单位比必须固定的哈');
}
$ratio = $v['ratio'] ?? 1;
if ($ratio <= 1) {
//util::fail('小单位,单位比必须大于1的哦');
}
$changeToBigNum = bcdiv($smallNum, $ratio, 2);
$tmp['itemNum'] = $changeToBigNum;
$bigPrice = bcmul($v['itemPrice'], $ratio, 2);
$tmp['bigPrice'] = $bigPrice;
if ($currentAboutPrice == 1) {
$totalPrice = $currentTotal;
} else {
$totalPrice = bcmul($v['itemPrice'], $smallNum, 2);
}
$tmp['totalPrice'] = $totalPrice;
}
$batchData[] = $tmp;
}
return $batchData;
}
// 组装状态码 lqh 2021.1.30
public static function groupStatusName($list)
{
foreach ($list as $k => $val) {
$status = $val['status'] ?? 0;
$list[$k]['statusName'] = self::getStatusName($status);
}
return $list;
}
// 获取状态码名称
public static function getStatusName($status)
{
return self::$statusMap[$status] ?? '';
}
//获取各个状态下的订单数量 allNum unPayNum unSendNum sendingNum finishNum
public static function statNum($shopId)
{
$data = [
'allNum' => 0,
'unPayNum' => 0,
'unSendNum' => 0,
'sendingNum' => 0,
'finishNum' => 0,
];
$res = self::getAllList("status", ['shopId' => $shopId]);
foreach ($res as $v) {
$data['allNum']++;
$status = $v['status'];
switch ($status) {
case self::PURCHASE_ORDER_STATUS_WAIT:
//待支付
$data['unPayNum']++;
break;
case self::PURCHASE_ORDER_STATUS_UN_SEND:
$data['unSendNum']++;
break;
case self::PURCHASE_ORDER_STATUS_SENDING:
$data['sendingNum']++;
break;
case self::PURCHASE_ORDER_STATUS_COMPLETE:
$data['finishNum']++;
break;
}
}
return $data;
}
//采购数,采购金额统计(根据时间,今日,昨天,7天,30天)
public static function statOrderInfo($shopId, $statType)
{
$start = '';
$end = '';
switch ($statType) {
case "today":
$start = date('Y-m-d');
$end = $start;
break;
case "yesterday" :
$start = date('Y-m-d', strtotime("-1 day"));
$end = $start;
break;
case "seven":
$start = date('Y-m-d', strtotime("-6 day"));
$end = date('Y-m-d');
break;
case "thirty":
$start = date('Y-m-d', strtotime("-29 day"));
$end = date('Y-m-d');
break;
default:
}
$where = [];
$where['shopId'] = $shopId;
$where['addTime'] = ['between', [$start, $end]];
$res = self::getAllByCondition($where, null, "id,actPrice");
$totalAmount = 0;
$totalNum = count($res);
foreach ($res as $v) {
$actPrice = $v['actPrice'] ?? 0;
$totalAmount = bcadd($totalAmount, $actPrice, 2);
}
return [
'totalAmount' => $totalAmount,
'totalNum' => $totalNum,
];
}
public static function exportDueList($list, $mainId)
{
$phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
require_once($phpExcelFile . 'Classes/PHPExcel.php');
$objPHPExcel = new \PHPExcel();
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("Office 2007 XLSX Document")
->setSubject("Office 2007 XLSX Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("file");
$ghsTitle = '供货商';
$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
$objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
$objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
$objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
$objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
$objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
$objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'ID');
$objPHPExcel->getActiveSheet()->setCellValue('B1', '名称');
$objPHPExcel->getActiveSheet()->setCellValue('C1', '金额');
//设置宽度
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(25);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
//加粗
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
$objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
$objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
$baseRow = 2;
foreach ($list as $key => $custom) {
$i = $baseRow + $key;
$objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['id']);
$objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['name']);
$objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['amount']);
//居左
$objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
$objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
$objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
}
$fileName = '应付统计-' . date("m-d");
$objPHPExcel->getActiveSheet()->setTitle($fileName);
$objPHPExcel->setActiveSheetIndex(0);
$dir = './priceTable/' . $mainId;
if (file_exists($dir) == false) {
mkdir($dir, 0777, true);
}
$date = $fileName;
$file = $date . '.xls';
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
if (file_exists($dir . '/' . $file)) {
unlink($dir . '/' . $file);
}
$objWriter->save($dir . '/' . $file);
$fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
util::success(['file' => $fileUrl, 'shortFile' => $file]);
}
public static function exportOrderData($respond, $mainId)
{
$orderList = $respond['list'] ?? [];
if (empty($orderList)) {
util::fail('没有数据需要导出');
}
$orderSnList = array_column($orderList, 'orderSn');
$orderItemData = PurchaseOrderItemClass::getAllByCondition(['orderSn' => ['in', $orderSnList]], null, '*');
$orderItemList = [];
if (!empty($orderItemData)) {
foreach ($orderItemData as $itemInfo) {
$orderSn = $itemInfo['orderSn'] ?? '';
$orderItemList[$orderSn][] = $itemInfo;
}
}
foreach ($orderList as $key => $orderInfo) {
$orderSn = $orderInfo['orderSn'] ?? '';
$status = $orderInfo['status'] ?? 0;
//增加项
$actPrice = $orderInfo['actPrice'] ?? 0;
if ($status != 4) {
unset($orderList[$key]);
continue;
}
//增加项
if ($actPrice <= 0) {
unset($orderList[$key]);
continue;
}
$itemList = $orderItemList[$orderSn] ?? [];
$orderList[$key]['itemList'] = $itemList;
}
$classList = ItemClassClass::getAllByCondition(['mainId' => $mainId], null, '*', 'id');
$phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
require_once($phpExcelFile . 'Classes/PHPExcel.php');
$objPHPExcel = new \PHPExcel();
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("Office 2007 XLSX Document")
->setSubject("Office 2007 XLSX Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("file");
$ghsTitle = '供货商';
$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
$objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
$objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
$objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
$objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
$objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
$objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
$objPHPExcel->getActiveSheet()->setCellValue('A1', '订单号');
$objPHPExcel->getActiveSheet()->setCellValue('B1', '创建时间');
$objPHPExcel->getActiveSheet()->setCellValue('C1', '入库时间');
$objPHPExcel->getActiveSheet()->setCellValue('D1', '订单金额');
$objPHPExcel->getActiveSheet()->setCellValue('E1', '退款金额');
$objPHPExcel->getActiveSheet()->setCellValue('F1', '实际金额');
$objPHPExcel->getActiveSheet()->setCellValue('G1', '供货商名称');
$objPHPExcel->getActiveSheet()->setCellValue('H1', '录入人员');
$objPHPExcel->getActiveSheet()->setCellValue('I1', '采购人员');
$objPHPExcel->getActiveSheet()->setCellValue('J1', '状态');
$objPHPExcel->getActiveSheet()->setCellValue('K1', '打印次数');
$objPHPExcel->getActiveSheet()->setCellValue('L1', '结清状态');
$objPHPExcel->getActiveSheet()->setCellValue('M1', '备注');
$objPHPExcel->getActiveSheet()->setCellValue('N1', '花材Id');
$objPHPExcel->getActiveSheet()->setCellValue('O1', '花材名称');
$objPHPExcel->getActiveSheet()->setCellValue('P1', '单价');
$objPHPExcel->getActiveSheet()->setCellValue('Q1', '数量');
$objPHPExcel->getActiveSheet()->setCellValue('R1', '已退数量');
$objPHPExcel->getActiveSheet()->setCellValue('S1', '金额');
$objPHPExcel->getActiveSheet()->setCellValue('T1', '分类');
//设置宽度
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('O')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('T')->setWidth(20);
//加粗
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setSize(9)->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('F1')->getFont()->setSize(9)->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('G1')->getFont()->setSize(9)->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('H1')->getFont()->setSize(9)->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('I1')->getFont()->setSize(9)->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('J1')->getFont()->setSize(9)->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('K1')->getFont()->setSize(9)->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('L1')->getFont()->setSize(9)->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('M1')->getFont()->setSize(9)->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('N1')->getFont()->setSize(9)->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('O1')->getFont()->setSize(9)->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('P1')->getFont()->setSize(9)->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('Q1')->getFont()->setSize(9)->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('R1')->getFont()->setSize(9)->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('S1')->getFont()->setSize(9)->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('T1')->getFont()->setSize(9)->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('G1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('H1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('I1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('J1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('K1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('L1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('M1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('N1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('O1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('P1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('Q1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('R1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('S1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('T1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('G1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('H1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('I1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('J1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('K1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('L1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('M1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('N1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('O1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('P1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('Q1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('R1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('S1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('T1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$baseRow = 2;
$x = 0;
foreach ($orderList as $key => $orderData) {
$currentList = $orderData['itemList'] ?? [];
if (!empty($currentList)) {
foreach ($currentList as $k => $orderInfo) {
$i = $baseRow + $x;
$x++;
$objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $orderData['orderSn']);
$objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $orderData['addTime']);
if ($orderData['status'] == 4) {
$objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $orderData['entryTime']);
} else {
$objPHPExcel->getActiveSheet()->setCellValue('C' . $i, '');
}
$objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $orderData['orderPrice']);
$objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $orderData['tkPrice']);
$objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $orderData['actPrice']);
$objPHPExcel->getActiveSheet()->setCellValue('G' . $i, $orderData['ghsName']);
$objPHPExcel->getActiveSheet()->setCellValue('H' . $i, $orderData['shopAdminName']);
$objPHPExcel->getActiveSheet()->setCellValue('I' . $i, $orderData['cgStaffName']);
$map = [1 => '待确认', 2 => '待发货', 3 => '待入库', 4 => '已入库', 5 => '已取消'];
$statusName = isset($orderData['status']) && isset($map[$orderData['status']]) ? $map[$orderData['status']] : '未知';
$objPHPExcel->getActiveSheet()->setCellValue('J' . $i, $statusName);
$objPHPExcel->getActiveSheet()->setCellValue('K' . $i, $orderData['printNum'] ?? 0);
$debtName = '';
if ($orderData['debt'] == 1) {
$debtName = '未结清';
}
if ($orderData['debt'] == 2) {
$debtName = '已结清';
}
$objPHPExcel->getActiveSheet()->setCellValue('L' . $i, $debtName);
$objPHPExcel->getActiveSheet()->setCellValue('M' . $i, $orderData['remark']);
$objPHPExcel->getActiveSheet()->setCellValue('N' . $i, $orderInfo['productId']);
$objPHPExcel->getActiveSheet()->setCellValue('O' . $i, $orderInfo['name']);
$objPHPExcel->getActiveSheet()->setCellValue('P' . $i, $orderInfo['bigPrice']);
$remainNum = bcsub($orderInfo['itemNum'], $orderInfo['refundNum']);
$price = bcmul($remainNum, $orderInfo['bigPrice'], 2);
$price = floatval($price);
$objPHPExcel->getActiveSheet()->setCellValue('Q' . $i, $remainNum);
$objPHPExcel->getActiveSheet()->setCellValue('R' . $i, $orderInfo['refundNum']);
$objPHPExcel->getActiveSheet()->setCellValue('S' . $i, $price);
$classId = $orderInfo['classId'] ?? 0;
$className = isset($classList[$classId]) && isset($classList[$classId]['name']) ? $classList[$classId]['name'] : '';
$objPHPExcel->getActiveSheet()->setCellValue('T' . $i, $className);
//居左
$objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('F' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('G' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('H' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('I' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('J' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('K' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('L' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('M' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('N' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('O' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('P' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('Q' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('R' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('S' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('T' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
}
}
}
$fileName = '采购单明细-' . time();
$objPHPExcel->getActiveSheet()->setTitle($fileName);
$objPHPExcel->setActiveSheetIndex(0);
$dir = './priceTable/' . $mainId;
if (file_exists($dir) == false) {
mkdir($dir, 0777, true);
}
$date = $fileName;
$file = $date . '.xls';
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
if (file_exists($dir . '/' . $file)) {
unlink($dir . '/' . $file);
}
$objWriter->save($dir . '/' . $file);
$fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
util::success(['file' => $fileUrl, 'shortFile' => $file]);
}
/**
* 按 productId 升序排列,用于库存行锁顺序(降低多商品并发死锁)
* @param array $list
*/
private static function sortRowsByProductIdAsc(&$list)
{
usort($list, function ($left, $right) {
$leftId = isset($left['productId']) ? intval($left['productId']) : 0;
$rightId = isset($right['productId']) ? intval($right['productId']) : 0;
if ($leftId == $rightId) {
return 0;
}
return $leftId < $rightId ? -1 : 1;
});
}
}