cqp 4 недель назад
Родитель
Сommit
b3e9f6275c
1 измененных файлов с 206 добавлено и 1 удалено
  1. 206 1
      app/Jobs/ProcessDataJob.php

+ 206 - 1
app/Jobs/ProcessDataJob.php

@@ -117,7 +117,7 @@ class ProcessDataJob implements ShouldQueue
     }
 
     //U8 存货新增到用友
-    public function inventoryAddToU8($data, $service){
+    public function inventoryAddToU81($data, $service){
         $inventory = Inventory::where('del_time', 0)
             ->where('order_number', $data['order_number'])
             ->where('login_type', $data['login_type'])
@@ -324,6 +324,211 @@ class ProcessDataJob implements ShouldQueue
         return [true, ''];
     }
 
+    /**
+     * 将存货信息同步到用友 U8 系统
+     * * @param array $data 包含 order_number 和 login_type 等关键参数
+     * @param object $service 用友数据库连接实例 (DB Service)
+     * @return array [bool, string]
+     */
+    public function inventoryAddToU8($data, $service)
+    {
+        // 1. 获取本地存货记录
+        $inventory = Inventory::where('del_time', 0)
+            ->where('order_number', $data['order_number'])
+            ->where('login_type', $data['login_type'])
+            ->first();
+
+        if (empty($inventory)) {
+            return [false, '本地存货记录不存在或已被删除'];
+        }
+        $inventory = $inventory->toArray();
+
+        // 2. 提取核心属性变量并转换为布尔/整数
+        $bSale     = (int)($inventory['bSale'] ?? 0);     // 销售(内销)
+        $bExpSale  = (int)($inventory['bExpSale'] ?? 0);  // 外销
+        $bPurchase = (int)($inventory['bPurchase'] ?? 0); // 采购
+        $bSelf     = (int)($inventory['bSelf'] ?? 0);     // 自制
+        $bComsume  = (int)($inventory['bComsume'] ?? 0);  // 生产耗用
+        $bProxy    = (int)($inventory['bProxy'] ?? 0);    // 委外 (若数据库无此字段,建议默认0)
+
+        // --- 用友业务规则动态计算开始 ---
+
+        // 2.1 计划默认属性 (iPlanDefault)
+        // 规则:自制=1, 委外=2, 采购=3, 计划品=4。优先级:自制 > 委外 > 采购
+        $iPlanDefault = 0;
+        if ($bSelf) {
+            $iPlanDefault = 1;
+        } elseif ($bProxy) {
+            $iPlanDefault = 2;
+        } elseif ($bPurchase) {
+            $iPlanDefault = 3;
+        }
+
+        // 2.2 允许BOM母件 (bBomMain)
+        // 规则:自制、委外、计划品等默认为1;纯采购件默认为0
+        $bBomMain = ($bSelf || $bProxy) ? 1 : 0;
+
+        // 2.3 允许BOM子件 (bBomSub)
+        // 规则:只要涉及到生产消耗或采购,通常都允许作为子件
+        $bBomSub = ($bSelf || $bProxy || $bPurchase || $bComsume) ? 1 : 0;
+
+        // 2.4 允许生产订单 (bProductBill)
+        // 规则:只有“自制”属性为“是”时,才允许下达生产订单
+        $bProductBill = $bSelf ? 1 : 0;
+
+        // 2.5 计划方法 (cPlanMethod) 和 成本相关 (bInTotalCost)
+        // 规则:如果非内外销且非生产耗用,但勾选了BOM子件,属于辅助性物料,不计算MRP
+        $cPlanMethod = 'R'; // R: MRP/MPS计算
+        $bInTotalCost = 1;  // 默认参与成本累计
+        if (!$bSale && !$bExpSale && !$bComsume && $bBomSub) {
+            $cPlanMethod = 'N'; // N: 不计算
+            $bInTotalCost = 0;
+        }
+
+        // --- 用友业务规则动态计算结束 ---
+
+        // 3. 编码生成或使用传入编码
+        $category_code = $inventory['category_code'];
+        if (!empty($data['code'])) {
+            $no = $data['code'];
+            $flag_title = "重填";
+        } else {
+            // 调用编码生成器 (假设方法名为 generate)
+            list($status, $msg) = $this->generate('inventory', $category_code, $service);
+            if (!$status) {
+                return [false, "生成存货编码失败: " . $msg];
+            }
+            $no = $msg;
+            $flag_title = "重试";
+        }
+
+        // 4. 构建 Inventory 主表数据
+        $inventoryData = [
+            'cInvCode'          => $no,
+            'cInvName'          => $inventory['title'],
+            'cInvCCode'         => $category_code,
+            'cInvStd'           => $inventory['size'] ?? null,   // 规格型号
+            'bSale'             => $bSale,
+            'bExpSale'          => $bExpSale,
+            'bPurchase'         => $bPurchase,
+            'bSelf'             => $bSelf,
+            'bComsume'          => $bComsume,
+            'bProxyForeign'     => $bProxy,                      // 委外
+            'iGroupType'        => $inventory['unit_group_type'],
+            'cGroupCode'        => $inventory['unit_group_code'],
+            'cComUnitCode'      => $inventory['unit_code'],      // 主计量单位
+            'cShopUnit'         => $inventory['unit_code'],      // 零售单位
+            'iImpTaxRate'       => $inventory['iImpTaxRate'],    // 进项税率
+            'iTaxRate'          => $inventory['iTaxRate'],       // 销项税率
+            'dSDate'            => date("Y-m-d 00:00:00.000"),  // 启用日期
+            'cEnterprise'       => $inventory['vendor_code_title'] ?? null,
+            'iSupplyType'       => '0',                          // 领料方式:领用
+            'fConvertRate'      => '1.0',                        // 转换因子
+            'bInTotalCost'      => $bInTotalCost,                // 动态计算
+            'cPlanMethod'       => $cPlanMethod,                 // 动态计算
+            'cSRPolicy'         => 'PE',                         // 供需政策:PE
+            'bBomMain'          => $bBomMain,                    // 动态计算
+            'bBomSub'           => $bBomSub,                     // 动态计算
+            'bProductBill'      => $bProductBill,                // 动态计算
+            'iPlanDefault'      => $iPlanDefault,                // 动态计算
+            'iAllocatePrintDgt' => '4',
+            'bService'          => '0',
+            'bAccessary'        => '0',
+            'iInvAdvance'       => '0.0',
+            'bInvQuality'       => '0',
+            'bInvBatch'         => '0',
+            'bInvEntrust'       => '0',
+            'bInvOverStock'     => '0',
+            'cCreatePerson'     => $user['username'] ?? 'admin',
+            'dModifyDate'       => date("Y-m-d H:i:s.000"),
+            'bPlanInv'          => '0',
+            'bATOModel'         => '0',
+            'bPTOModel'         => '0',
+            'bMPS'              => ($iPlanDefault == 1 || $iPlanDefault == 2) ? '1' : '0', // 只有自制委外才可能作为MPS件
+            'bROP'              => '0',
+            'bRePlan'           => '0',
+            'bCutMantissa'      => '0',
+            'bCheckBSATP'       => '0',
+            'iCheckATP'         => '0',
+            // 以下为大量自由项/控制位字段,默认设为 0
+            'bFree1' => '0', 'bFree2' => '0', 'bFree3' => '0', 'bFree4' => '0', 'bFree5' => '0',
+            'bFree6' => '0', 'bFree7' => '0', 'bFree8' => '0', 'bFree9' => '0', 'bFree10' => '0',
+            'bConfigFree1' => '0', 'bConfigFree2' => '0', 'bConfigFree3' => '0', 'bConfigFree4' => '0',
+            'bConfigFree5' => '0', 'bConfigFree6' => '0', 'bConfigFree7' => '0', 'bConfigFree8' => '0',
+            'bConfigFree9' => '0', 'bConfigFree10' => '0',
+        ];
+
+        // 5. 构建 Inventory_sub 表数据
+        $inventorySubData = [
+            'cInvSubCode'        => $no,
+            'iSurenessType'      => '1',
+            'bIsAttachFile'      => '0',
+            'bInByProCheck'      => '1',
+            'iRequireTrackStyle' => '0',
+            'iExpiratDateCalcu'  => '0',
+            'iBOMExpandUnitType' => '1',                          // BOM展开单位:主计量
+            'iDrawType'          => $inventory['iDrawType'] ?? 0, // 领用方式
+            'fInvCIQExch'        => $inventory['customs_change_rate'] ?? 1,
+            'bInvKeyPart'        => '1',
+            'iAcceptEarlyDays'   => '999',
+            'dInvCreateDatetime' => date("Y-m-d H:i:s.000"),
+            'bPUQuota'           => '0',
+            'bInvROHS'           => '0',
+            'bPrjMat'            => '0',
+            'bInvAsset'          => '0',
+            'bSrvProduct'        => '0',
+            'iAcceptDelayDays'   => '0',
+            'bSCkeyProjections'  => '0',
+            'iSupplyPeriodType'  => '1',                          // 供应期间类型:天
+            'iAvailabilityDate'  => '1',                          // 第一需求日
+            'bImport'            => '0',
+            'bCheckSubitemCost'  => '1',
+            'fRoundFactor'       => '0.0',
+            'bConsiderFreeStock' => '1',
+            'bSuitRetail'        => '0',
+            'bFeatureMatch'      => '0',
+            'bProduceByFeatureAllocate' => '0',
+            'bMaintenance'       => '0',
+            'iMaintenanceCycleUnit'     => '0',
+            'bCoupon'            => '0',
+            'bStoreCard'         => '0',
+            'bProcessProduct'    => '0',
+            'bProcessMaterial'   => '0',
+            // 自由项价格控制全部设为 0
+            'bPurPriceFree1' => '0', 'bOMPriceFree1' => '0', 'bSalePriceFree1' => '0',
+            'bControlFreeRange1' => '0', 'bBatchProperty1' => '0',
+            'bBondedInv'         => '0',
+            'bBatchCreate'       => '0',
+        ];
+
+        // 6. 执行数据库同步(使用事务)
+        try {
+            $service->beginTransaction();
+
+            // 检查存货编码是否冲突
+            $exists = $service->table('Inventory')
+                ->where('cInvCode', $no)
+                ->lockForUpdate()
+                ->exists();
+
+            if ($exists) {
+                $service->rollBack();
+                return [false, '存货编码 [' . $no . '] 在用友系统中已存在,请' . $flag_title];
+            }
+
+            // 插入主表和从表
+            $service->table('Inventory')->insert($inventoryData);
+            $service->table('Inventory_sub')->insert($inventorySubData);
+
+            $service->commit();
+        } catch (\Throwable $exception) {
+            $service->rollBack();
+            return [false, "同步用友数据库失败: " . $exception->getMessage()];
+        }
+
+        return [true, '同步成功'];
+    }
+
     //U8 供应商新增
     public function vendorAddToU8($data, $user,$service){
         $vendor = Vendor::where('del_time', 0)