cqp vor 3 Tagen
Ursprung
Commit
c2ec6b45ac
1 geänderte Dateien mit 75 neuen und 11 gelöschten Zeilen
  1. 75 11
      app/Service/U8ThirdPartyService.php

+ 75 - 11
app/Service/U8ThirdPartyService.php

@@ -764,6 +764,7 @@ class U8ThirdPartyService extends Service
                    "iunitprice"    => (float)$iUnitPrice,
                    "inatunitprice" => (float)$iUnitPrice,
                    "dvDate"        => (string)$value['failureDate'], // 示例中只需失效日期
+//                   "dMDate"          => (string)$value['productDate'], // todo
                    "iSOsID"        => (int)$item['iSOsID'], // 强制转整型,去掉引号
                    "cBatch"        => (string)$value['lot']
                ];
@@ -835,8 +836,8 @@ class U8ThirdPartyService extends Service
                "iDLsID"       => $item['iDLsID'],             // 核心:发货单子表ID
                "dMadeDate"       => $item['dMDate'], // 生产
                "dVDate"       => $item['dvDate'], // 失效
-               "iUnitCost"    => (float)($item['iUnitPrice'] ?? 0), // 无税单价
-               "iPrice"       => round((float)($item['iUnitPrice'] ?? 0) * $pendingQty, 2), // 无税金额
+//               "iUnitCost"    => (float)($item['iUnitPrice'] ?? 0), // 无税单价
+//               "iPrice"       => round((float)($item['iUnitPrice'] ?? 0) * $pendingQty, 2), // 无税金额
            ];
        }
 
@@ -1795,35 +1796,61 @@ class U8ThirdPartyService extends Service
         list($status, $msg) = $this->getToken();
         if(! $status) return [false, $msg];
 
-        // 3. 组织采购入库单 (参照来料检验单模式)
+        // --- 核心金额计算逻辑 ---
+        $qty = (float)$num;
+        $taxRate = (float)($order['iTaxRate'] ?? 13.0); // 税率
+        $taxUnitPrice = (float)($order['iOriTaxCost'] ?? 0); // 原币含税单价
+
+        // 1. 价税合计 (iSum)
+        $iSum = round($qty * $taxUnitPrice, 2);
+        // 2. 原币无税金额 (imoney) = 价税合计 / (1 + 税率/100)
+        $iMoney = round($iSum / (1 + ($taxRate / 100)), 2);
+        // 3. 税额 (itax)
+        $iTax = round($iSum - $iMoney, 2);
+        // 4. 原币无税单价 (iunitprice)
+        $iUnitPrice = round($taxUnitPrice / (1 + ($taxRate / 100)), 6);
+
+        // 3. 组织采购入库单
         $tmp = [
             "Inum" => "PurchaseIn",
             "Data" => [
                 "iHead" => [
-                    "IsVerify"      => true,          // 示例中为 false
+                    "IsVerify"      => true,
+                    "bCalPrice"     => true,          // 开启自动计算
+                    "PriceCalKey"   => "iOriTaxCost", // 以含税单价为准
                     "cWhCode"       => (string)($order['CWHCODE'] ?? "01"),
                     "cDepCode"      => (string)($order['CDEPCODE']),
                     "cVenCode"      => (string)$order['CVENCODE'],
                     "cRdCode"       => "0101",
                     "iExchRate"     => (float)($order['IEXCHRATE'] ?? 1.0),
-                    "iTaxRate"      => (float)($order['ITAXRATE'] ?? 13.0),
+                    "iTaxRate"      => (float)$taxRate,
                     "cExch_Name"    => "人民币",
-                    "cSource"       => "来料检验单",      // 匹配示例中的来源
+                    "cSource"       => "来料检验单",
                     "cBusType"      => "普通采购",
                     "cMemo"         => "接口生成",
                     "dDate"         => date("Y-m-d"),
-                    "cChkCode"      => (string)$order['CCHECKCODE'] // 检验单号
+                    "cChkCode"      => (string)$order['CCHECKCODE']
                 ],
                 "iBody" => [
                     [
                         "iRowNo"        => 1,
                         "cInvCode"      => (string)$order['CINVCODE'],
-                        "iQuantity"     => (float)$num,           // 本次入库实收数量
+                        "iQuantity"     => (float)$qty,
                         "iNum"          => 0,
-                        "iNQuantity"    => (float)($order['IQUANTITY'] ?? $num), // 检验单关联数量
+                        "iNQuantity"    => (float)($order['IQUANTITY'] ?? $qty),
                         "iNNum"         => 0,
-//                        "iCheckIdBaks"  => (int)$order['ICHECKIDBAKS'],
-                        "iArrsId"      => $order['SOURCEAUTOID'], // 子表id
+                        "iArrsId"       => (int)$order['SOURCEAUTOID'], // 强制转 int,关联到货单子表
+                        // --- 新增价格字段 ---
+                        "iOriTaxCost"   => (float)$taxUnitPrice, // 原币含税单价
+                        "iOriCost"      => (float)$iUnitPrice,    // 原币无税单价
+                        "iOriMoney"     => (float)$iMoney,       // 原币无税金额
+                        "iOriTaxPrice"  => (float)$iTax,         // 原币税额
+                        "iOriSum"       => (float)$iSum,         // 原币价税合计
+                        "iTaxRate"      => (float)$taxRate,      // 税率
+                        // 本币字段 (inat...) 建议也加上,防止 U8 换算误差
+                        "fNatMoney"     => (float)$iMoney,       // 本币无税金额
+                        "fNatTax"       => (float)$iTax,         // 本币税额
+                        "fNatSum"       => (float)$iSum,         // 本币价税合计
                         "cBatch"        => (string)($order['CBATCH'] ?? ''),
                         "dMadeDate"     => (string)($order['DPRODATE'] ?? ''),
                         "dVDate"        => (string)($order['DVDATE'] ?? ''),
@@ -1832,6 +1859,43 @@ class U8ThirdPartyService extends Service
             ]
         ];
 
+        // 3. 组织采购入库单 (参照来料检验单模式)
+//        $tmp = [
+//            "Inum" => "PurchaseIn",
+//            "Data" => [
+//                "iHead" => [
+//                    "IsVerify"      => true,          // 示例中为 false
+//                    "cWhCode"       => (string)($order['CWHCODE'] ?? "01"),
+//                    "cDepCode"      => (string)($order['CDEPCODE']),
+//                    "cVenCode"      => (string)$order['CVENCODE'],
+//                    "cRdCode"       => "0101",
+//                    "iExchRate"     => (float)($order['IEXCHRATE'] ?? 1.0),
+//                    "iTaxRate"      => (float)($order['ITAXRATE'] ?? 13.0),
+//                    "cExch_Name"    => "人民币",
+//                    "cSource"       => "来料检验单",      // 匹配示例中的来源
+//                    "cBusType"      => "普通采购",
+//                    "cMemo"         => "接口生成",
+//                    "dDate"         => date("Y-m-d"),
+//                    "cChkCode"      => (string)$order['CCHECKCODE'] // 检验单号
+//                ],
+//                "iBody" => [
+//                    [
+//                        "iRowNo"        => 1,
+//                        "cInvCode"      => (string)$order['CINVCODE'],
+//                        "iQuantity"     => (float)$num,           // 本次入库实收数量
+//                        "iNum"          => 0,
+//                        "iNQuantity"    => (float)($order['IQUANTITY'] ?? $num), // 检验单关联数量
+//                        "iNNum"         => 0,
+////                        "iCheckIdBaks"  => (int)$order['ICHECKIDBAKS'],
+//                        "iArrsId"      => $order['SOURCEAUTOID'], // 子表id
+//                        "cBatch"        => (string)($order['CBATCH'] ?? ''),
+//                        "dMadeDate"     => (string)($order['DPRODATE'] ?? ''),
+//                        "dVDate"        => (string)($order['DVDATE'] ?? ''),
+//                    ]
+//                ]
+//            ]
+//        ];
+
         $final_data = [$tmp];
 
         // 4. 调用 API