Merge pull request '修改商品溯源导出' (#552) from dev into main

Reviewed-on: #552
This commit is contained in:
mkm 2025-03-17 18:48:10 +08:00
commit 0d30d497cc

View File

@ -10,7 +10,7 @@ use app\admin\validate\product_source_link\ProductSourceLinkValidate;
use app\common\model\product_source_link\ProductSourceLink; use app\common\model\product_source_link\ProductSourceLink;
use app\common\model\product_source_link_info\ProductSourceLinkInfo; use app\common\model\product_source_link_info\ProductSourceLinkInfo;
use app\common\model\store_product\StoreProduct; use app\common\model\store_product\StoreProduct;
use app\common\model\warehouse_order\WarehouseOrder; use app\common\model\warehouse_product\WarehouseProduct;
use app\common\service\xlsx\ProductSourceLinkXsl; use app\common\service\xlsx\ProductSourceLinkXsl;
@ -105,17 +105,14 @@ class ProductSourceLinkController extends BaseAdminController
if (!empty($params['warehouse_id'])) { if (!empty($params['warehouse_id'])) {
$query->where('warehouse_id', $params['warehouse_id']); $query->where('warehouse_id', $params['warehouse_id']);
} }
$list = $query->with(['product', 'warehouse', 'purchase']) $list = $query->with(['product', 'purchase'])
->field(['id', 'purchase_uid', 'product_id', 'warehouse_id',]) ->field(['id', 'purchase_uid', 'product_id'])
->order(['id' => 'desc']) ->order(['id' => 'desc'])
->group('purchase_uid,product_id')
->select() ->select()
->each(function ($item) {
$item->total_nums = ProductSourceLinkInfo::where('product_id', $item->product_id)->where('oid', $item->id)->where('types', 1)->sum('nums');
$item->warehouse_outbound_nums = ProductSourceLinkInfo::where('product_id', $item->product_id)->where('oid', $item->id)->where('types', 2)->sum('nums');
})
->toArray(); ->toArray();
$linkInfos = ProductSourceLinkInfo::whereIn('oid', array_column($list, 'id'))->select()->toArray(); $linkInfos = ProductSourceLinkInfo::whereIn('oid', array_column($list, 'id'))->select()->toArray();
$orderSnList = WarehouseOrder::field('id,code')->whereIn('id', array_unique(array_column($linkInfos, 'link_id')))->select()->toArray(); $orderSnList = WarehouseProduct::field('id,code')->whereIn('id', array_unique(array_column($linkInfos, 'link_id')))->select()->toArray();
$orderSnList = reset_index($orderSnList, 'id'); $orderSnList = reset_index($orderSnList, 'id');
foreach ($list as &$item) { foreach ($list as &$item) {
$item['in_order_no'] = []; $item['in_order_no'] = [];
@ -129,17 +126,17 @@ class ProductSourceLinkController extends BaseAdminController
if (!empty($orderSnItem)) { if (!empty($orderSnItem)) {
$item['in_order_no'][] = $orderSnItem['code']; $item['in_order_no'][] = $orderSnItem['code'];
} }
$item['in_num'] = bcmul($item['in_num'], $linkInfo['nums'], 2); $item['in_num'] = bcadd($item['in_num'], $linkInfo['nums'], 2);
} else { } else {
if (!empty($orderSnItem)) { if (!empty($orderSnItem)) {
$item['out_order_no'][] = $orderSnItem['code']; $item['out_order_no'][] = $orderSnItem['code'];
} }
$item['out_num'] = bcmul($item['out_num'], $linkInfo['nums'], 2); $item['out_num'] = bcadd($item['out_num'], $linkInfo['nums'], 2);
} }
} }
} }
$item['in_order_no'] = !empty($item['in_order_no']) ? implode(',', $item['in_order_no']) : ''; $item['in_order_no'] = !empty($item['in_order_no']) ? implode('', $item['in_order_no']) : '';
$item['out_order_no'] =!empty($item['out_order_no']) ? implode(',', $item['out_order_no']) : ''; $item['out_order_no'] =!empty($item['out_order_no']) ? implode('', $item['out_order_no']) : '';
} }
$file_path = (new ProductSourceLinkXsl())->export($list); $file_path = (new ProductSourceLinkXsl())->export($list);
return $this->success('导出成功', ['url' => $file_path]); return $this->success('导出成功', ['url' => $file_path]);