Эх сурвалжийг харах

星科源 大版本修改

cqp 1 өдөр өмнө
parent
commit
f486bad0b7

+ 208 - 2
app/Service/U8ServerService.php

@@ -832,7 +832,99 @@ class U8ServerService extends Service
     }
 
     //获取u8请购单
-    public function purchaseRequisitionU8List($data, $user){
+    /**
+     * 获取 U8 请购单列表(带存货明细 detail 字段)
+     */
+    public function purchaseRequisitionU8List($data, $user)
+    {
+        $order_date = $data['order_date'] ?? [];
+        $order_date = array_filter($order_date);
+        $code = $data['code'] ?? "";
+
+        // 1. 构建主表查询 Model
+        $model = $this->databaseService->table('PU_AppVouch as a')
+            ->when(!empty($code), function ($query) use ($code) {
+                return $query->where('a.cCode', 'LIKE', '%' . $code . '%');
+            })
+            ->when(!empty($order_date), function ($query) use ($order_date) {
+                $start = date('Y-m-d 00:00:00.000', $order_date[0]);
+                $end = date('Y-m-d 23:59:59.000', $order_date[1]);
+                return $query->whereBetween('a.dDate', [$start, $end]);
+            })
+            ->where('a.iverifystateex', 2) // 已审核状态
+            ->whereExists(function ($query) {
+                $query->select(DB::raw(1))
+                    ->from('PU_AppVouchs as b')
+                    ->whereRaw('b.ID = a.ID')
+                    ->whereRaw('ISNULL(b.fQuantity, 0) > ISNULL(b.iReceivedQTY, 0)');
+            })
+            ->select(
+                'a.ID as id', // 必须查出主表 ID 用来后续关联子表
+                DB::raw("ISNULL(a.cBusType, '') as business_type"),
+                DB::raw("ISNULL(a.cCode, '') as order_number"),
+                DB::raw("ISNULL(CONVERT(varchar(10), a.dDate, 120), '') as order_date"),
+                DB::raw("ISNULL(a.cMaker, '') as crt_name")
+            )
+            ->orderBy('a.ID', 'desc');
+
+        // 2. 获取主表分页列表数据
+        $list = $this->limit($model, '', $data);
+
+        // 如果列表为空,直接返回
+        $items = $list['data'] ?? [];
+        if (empty($items)) {
+            return [true, $list];
+        }
+
+        // 3. 【核心结合】批量提取当前页所有主表的 ID
+        $mainIds = array_column($items, 'id');
+
+        // 4. 一次性批量查出这些主表对应的所有存货明细
+        $details = $this->databaseService->table('PU_AppVouchs as b')
+            ->leftJoin('Inventory as i', 'i.cInvCode', 'b.cInvCode')
+            ->leftJoin('ComputationUnit as u', 'u.cComUnitCode', 'i.cComUnitCode')
+            ->whereIn('b.ID', $mainIds) // 批量范围查询
+            ->whereRaw('ISNULL(b.fQuantity, 0) > ISNULL(b.iReceivedQTY, 0)')
+            ->select(
+                'b.AutoID as detail_id',
+                'b.ID as main_id', // 核心:用这个字段在内存里与主表归类映射
+                'b.ivouchrowno as row_no',
+                'b.cInvCode as code',
+                'b.dRequirDate as plan_date',
+                'b.fTaxPrice as price',
+                'b.iPerTaxRate as rate',
+                'i.cComUnitCode as unit_code',
+                'u.cComUnitName as unit',
+                DB::raw("ISNULL(i.cInvName, '') as name"),
+                DB::raw("ISNULL(i.cInvStd, '') as size"),
+                DB::raw("ISNULL(b.fQuantity, 0) as req_qty"),
+                DB::raw("ISNULL(b.iReceivedQTY, 0) as order_qty"),
+                DB::raw("(ISNULL(b.fQuantity, 0) - ISNULL(b.iReceivedQTY, 0)) as available_qty")
+            )
+            ->orderBy('b.ivouchrowno', 'asc')
+            ->get();
+
+        // 转化为普通纯数组
+        $detailList = json_decode(json_encode($details), true);
+
+        // 5. 按 main_id 将明细数据分门别类组装成 Map [main_id => [明细数组]]
+        $detailMap = [];
+        foreach ($detailList as $detail) {
+            $detailMap[$detail['main_id']][] = $detail;
+        }
+
+        // 6. 将明细 Map 塞回主表列表项的 detail 字段中
+        foreach ($items as &$item) {
+            $item->detail = $detailMap[$item->id] ?? [];
+        }
+
+        // 重写回原列表结构中
+        $list['data'] = $items;
+
+        return [true, $list];
+    }
+
+    public function purchaseRequisitionU8List1($data, $user){
         $order_date = $data['order_date'] ?? [];
         $order_date = array_filter($order_date);
         $code = $data['code'] ?? "";
@@ -938,7 +1030,121 @@ class U8ServerService extends Service
     }
 
     //获取u8采购订单
-    public function purchaseOrderU8List($data, $user){
+    /**
+     * 获取 U8 采购订单列表(带存货明细 detail 字段)
+     */
+    public function purchaseOrderU8List($data, $user)
+    {
+        // 0 蓝单  1 红单
+        if (!isset($data['bredvouch'])) return [false, '参照类型(蓝单|红单不能为空)'];
+        $type = $data['bredvouch'];
+        $order_date = $data['order_date'] ?? [];
+        $order_date = array_filter($order_date);
+        $code = $data['code'] ?? "";
+
+        // 1. 构建主表查询 Model
+        $model = $this->databaseService->table('PO_Pomain as a')
+            ->leftJoin('Vendor as v', 'v.cVenCode', '=', 'a.cVenCode')
+            ->when(!empty($code), function ($query) use ($code) {
+                return $query->where('a.cPOID', 'LIKE', '%' . $code . '%');
+            })
+            ->when(!empty($order_date), function ($query) use ($order_date) {
+                $start = date('Y-m-d 00:00:00.000', $order_date[0]);
+                $end = date('Y-m-d 23:59:59.000', $order_date[1]);
+                return $query->whereBetween('a.dPODate', [$start, $end]);
+            })
+            ->where('a.iverifystateex', 2) // 已审核
+            ->whereExists(function ($query) use ($type) {
+                if ($type == 0) {
+                    $query->select(DB::raw(1))
+                        ->from('PO_Podetails as b')
+                        ->whereRaw('b.POID = a.POID')
+                        ->whereRaw('ISNULL(b.iQuantity, 0) > ISNULL(b.iReceivedQTY, 0)');
+                } elseif ($type == 1) {
+                    $query->select(DB::raw(1))
+                        ->from('PO_Podetails as b')
+                        ->whereRaw('b.POID = a.POID')
+                        ->whereRaw('ISNULL(b.iReceivedQTY, 0) > 0');
+                }
+            })
+            ->select(
+                'a.POID as id', // 对应明细表的 b.POID
+                DB::raw("ISNULL(a.cBusType, '') as business_type"),
+                DB::raw("ISNULL(a.cVenCode, '') as supply_code"),
+                DB::raw("ISNULL(v.cVenName, '') as supply_name"),
+                DB::raw("ISNULL(a.cexch_name, '') as cexch_name"),
+                DB::raw("ISNULL(a.nflat, '') as nflat"),
+                DB::raw("ISNULL(a.iDiscountTaxType, '') as iDiscountTaxType"),
+                DB::raw("ISNULL(a.cPOID, '') as order_number"),
+                DB::raw("ISNULL(CONVERT(varchar(10), a.dPODate, 120), '') as order_date"),
+                DB::raw("ISNULL(a.cMaker, '') as crt_name")
+            )
+            ->orderBy('a.ID', 'desc');
+
+        // 2. 获取主表分页列表数据
+        $list = $this->limit($model, '', $data);
+
+        $items = $list['data'] ?? [];
+        if (empty($items)) {
+            return [true, $list];
+        }
+
+        // 3. 批量提取当前页所有主表 POID
+        $mainIds = array_column($items, 'id');
+
+        // 4. 一次性批量查出这些主表对应的所有子表存货明细(继承蓝单/红单过滤逻辑)
+        $details = $this->databaseService->table('PO_Podetails as b')
+            ->leftJoin('Inventory as i', 'i.cInvCode', 'b.cInvCode')
+            ->leftJoin('ComputationUnit as u', 'u.cComUnitCode', 'i.cComUnitCode')
+            ->whereIn('b.POID', $mainIds) // 批量范围锁定
+            ->when(isset($type), function ($query) use ($type) {
+                if ($type == 0) {
+                    return $query->whereRaw('ISNULL(b.iQuantity, 0) > ISNULL(b.iReceivedQTY, 0)');
+                } elseif ($type == 1) {
+                    return $query->whereRaw('ISNULL(b.iReceivedQTY, 0) > 0');
+                }
+            })
+            ->select(
+                'b.POID as main_id',                // 用于在内存中与主表 id 映射
+                'b.ID as detail_id',                // 子表行单据唯一标识
+                'b.ivouchrowno as row_no',
+                'b.cInvCode as code',
+                'b.iTaxPrice as price',
+                'b.iPerTaxRate as rate',
+                'i.cComUnitCode as unit_code',
+                'i.bInvBatch as pici',               // 批次管理
+                'i.bInvQuality as baozhiqi',         // 保质期管理
+                'u.cComUnitName as unit',
+                DB::raw("ISNULL(i.cInvName, '') as name"),
+                DB::raw("ISNULL(i.cInvStd, '') as size"),
+                DB::raw("ISNULL(b.iQuantity, 0) as i_qty"),
+                DB::raw("ISNULL(b.iReceivedQTY, 0) as in_qty"),
+                DB::raw("(ISNULL(b.iQuantity, 0) - ISNULL(b.iReceivedQTY, 0)) as available_qty")
+            )
+            ->orderBy('b.ivouchrowno', 'asc')
+            ->get();
+
+        // 转化为普通纯数组
+        $detailList = json_decode(json_encode($details), true);
+
+        // 5. 按 main_id 将明细数据归类到 Map 容器中
+        $detailMap = [];
+        foreach ($detailList as $detail) {
+            $detailMap[$detail['main_id']][] = $detail;
+        }
+
+        // 6. 将明细 Map 塞回主表对应的列表项中
+        foreach ($items as &$item) {
+            $item->detail = $detailMap[$item->id] ?? [];
+        }
+
+        // 重写回原分页列表结构
+        $list['data'] = $items;
+
+        return [true, $list];
+    }
+
+    public function purchaseOrderU8List1($data, $user){
         // 0 蓝单  1 红单
         if(! isset($data['bredvouch'])) return [false, '参照类型(蓝单|红单不能为空)'];
         $type = $data['bredvouch'];