ClearClass.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. <?php
  2. namespace bizGhs\clear\classes;
  3. use biz\ghs\classes\GhsClass;
  4. use biz\shop\classes\ShopClass;
  5. use bizGhs\base\classes\BaseClass;
  6. use bizGhs\custom\classes\CustomClass;
  7. use bizGhs\clear\classes\OrderCgClearClass;
  8. use bizGhs\ghs\classes\GhsBalanceChangeClass;
  9. use bizGhs\order\classes\OrderClass;
  10. use bizGhs\order\classes\PurchaseOrderClass;
  11. use bizGhs\shop\classes\MainClass;
  12. use bizGhs\shop\classes\ShopMoneyClass;
  13. use bizHd\purchase\classes\PurchaseClass;
  14. use bizHd\purchase\classes\PurchaseClearClass;
  15. use common\components\dict;
  16. use common\components\noticeUtil;
  17. use common\components\orderSn;
  18. use common\components\util;
  19. use Yii;
  20. use common\components\lakala\Lakala;
  21. class ClearClass extends BaseClass
  22. {
  23. public static $baseFile = '\bizGhs\clear\models\Clear';
  24. const STATUS_AWAIT_PAY = 1;
  25. const STATUS_HAS_PAY = 2;
  26. const STATUS_EXPIRE = 3;
  27. public static function refundMoney($shop, $clear)
  28. {
  29. $refundOrderSn = orderSn::getClearRefundSn();
  30. $refundPrice = $clear->actPrice ?? 0;
  31. $orderSn = $clear->orderSn ?? '';
  32. $refundFee = bcmul($refundPrice, 100);
  33. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  34. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  35. $params = [
  36. 'appid' => 'OP00002119',
  37. 'serial_no' => '018b08cfddbd',
  38. 'merchant_no' => $shop->lklSjNo,
  39. 'term_no' => $shop->lklScanTermNo,
  40. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  41. 'lklCertificatePath' => $lklCertificatePath,
  42. ];
  43. $laResource = new Lakala($params);
  44. $refundReason = '';
  45. $thirdNo = $clear->thirdNo ?? '';
  46. $backParams = [
  47. 'refundSn' => $refundOrderSn,
  48. 'thirdNo' => $thirdNo,
  49. 'refundAmount' => $refundFee,
  50. 'refundReason' => $refundReason,
  51. ];
  52. $response = $laResource->refund($backParams);
  53. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  54. $errMsg = $response['msg'] ?? '';
  55. return ['status' => 0, 'msg' => $errMsg];
  56. } else {
  57. $sjName = $shop->merchantName;
  58. $shopName = $shop->shopName;
  59. $name = $shopName == '首店' ? $sjName : $sjName . '-' . $shopName;
  60. noticeUtil::push("取消的结账单,客户的钱已原路退回 success {$orderSn} {$name}", '15280215347');
  61. return ['status' => 1, 'msg' => '退款成功'];
  62. }
  63. }
  64. public static function addOrder($data)
  65. {
  66. $orderSn = orderSn::getPurchaseClearSn();
  67. $idJson = $data['id'];
  68. $idArr = json_decode($idJson, true);
  69. $arr = array_column($idArr, 'id');
  70. if (empty($arr)) {
  71. util::fail('请选择采购单');
  72. }
  73. $purchaseList = PurchaseOrderClass::getAllByCondition(['id' => ['in', $arr]], null, ['id', 'orderSn', 'actPrice', 'realPrice', 'debt', 'ghsId', 'debtPrice', 'remainDebtPrice'], null, true);
  74. if (empty($purchaseList)) {
  75. util::fail('请选择采购单');
  76. }
  77. $totalPrice = 0;
  78. $currentGhsId = $data['ghsId'] ?? 0;
  79. foreach ($purchaseList as $purchase) {
  80. if ($purchase->debt != PurchaseOrderClass::DEBT_YES) {
  81. util::fail('请选择有欠款的采购单');
  82. }
  83. if ($purchase->ghsId != $currentGhsId) {
  84. util::fail('请选择同个供货商的订单');
  85. }
  86. // 结账口径:剩余待结;历史单未回填时回落 actPrice
  87. $remain = bcadd((string)($purchase->remainDebtPrice ?? '0'), '0', 2);
  88. if (bccomp($remain, '0', 2) <= 0) {
  89. $remain = bcadd((string)($purchase->actPrice ?? '0'), '0', 2);
  90. }
  91. $totalPrice = bcadd($totalPrice, $remain, 2);
  92. }
  93. $ghs = GhsClass::getById($currentGhsId, true);
  94. if (empty($ghs)) {
  95. util::fail('没有找到供货商');
  96. }
  97. GhsClass::valid($ghs, $data['shopId']);
  98. $ghsName = $ghs->name ?? '';
  99. $ghsAvatar = $ghs->avatar ?? '';
  100. $ghsMobile = $ghs->mobile ?? '';
  101. $customId = $ghs->customId ?? 0;
  102. $custom = CustomClass::getById($customId, true);
  103. $customName = $custom->name ?? '';
  104. $customAvatar = $custom->avatar ?? '';
  105. $customMobile = $custom->mobile ?? '';
  106. $data['ghsName'] = $ghsName;
  107. $data['ghsAvatar'] = $ghsAvatar;
  108. $data['ghsMobile'] = $ghsMobile;
  109. $data['customName'] = $customName;
  110. $data['customAvatar'] = $customAvatar;
  111. $data['customMobile'] = $customMobile;
  112. $data['prePrice'] = $totalPrice;
  113. $realPrice = $totalPrice;
  114. if (isset($data['modifyPrice']) && !empty($data['modifyPrice'])) {
  115. if (is_numeric($data['modifyPrice']) == false) {
  116. util::fail('实收金额错误');
  117. }
  118. $modifyPrice = $data['modifyPrice'] ?? 0.00;
  119. if ($modifyPrice > $totalPrice) {
  120. util::fail('实收金额不能大于订单金额');
  121. }
  122. if ($modifyPrice < $totalPrice) {
  123. $data['discountAmount'] = bcsub($totalPrice, $modifyPrice, 2);
  124. $data['discountType'] = dict::getDict('discountType', 'discount');
  125. }
  126. $realPrice = $modifyPrice;
  127. }
  128. $data['actPrice'] = $realPrice;
  129. $data['realPrice'] = $realPrice;
  130. $data['orderSn'] = $orderSn;
  131. // 生成并保存 salt 字段,用于结账单的验签和防篡改分享链接
  132. $data['salt'] = $data['salt'] ?? \common\components\stringUtil::charsShuffleLowerCase(10);
  133. $deadTime = time() + 120;
  134. $data['deadline'] = date("Y-m-d H:i:s", $deadTime);
  135. $data['status'] = self::STATUS_AWAIT_PAY;
  136. $order = self::add($data, true);
  137. $pairs = OrderCgClearClass::buildPairsFromGhsCgOrders($purchaseList);
  138. OrderCgClearClass::bindRelations($order, $pairs);
  139. $payWay = $data['payWay'] ?? dict::getDict('payWay', 'wxPay');
  140. self::complete($order, $payWay);
  141. OrderCgClearClass::markDoneByClearId($order->id);
  142. return $order;
  143. }
  144. /**
  145. * 【用途】作废该供货商下待付的 gys2KmGys 采购结账单(新建自动销欠前调用)
  146. */
  147. public static function expireAwaitPayPurchaseClears($ghsId, $shopId = 0)
  148. {
  149. $ghsId = intval($ghsId);
  150. if ($ghsId <= 0) {
  151. return;
  152. }
  153. $where = [
  154. 'ghsId' => $ghsId,
  155. 'clearStyle' => dict::getDict('clearStyle', 'gys2KmGys'),
  156. 'status' => self::STATUS_AWAIT_PAY,
  157. ];
  158. if (intval($shopId) > 0) {
  159. $where['shopId'] = intval($shopId);
  160. }
  161. $list = self::getAllByCondition($where, null, '*', null, true);
  162. if (empty($list)) {
  163. return;
  164. }
  165. foreach ($list as $item) {
  166. $item->status = self::STATUS_EXPIRE;
  167. $item->remark = ($item->remark ?? '') . ';售后返充自动销欠前作废待付结账单';
  168. $item->save(false, ['status', 'remark']);
  169. OrderCgClearClass::markExpireByClearId($item->id ?? 0);
  170. }
  171. }
  172. /**
  173. * 【用途】采购售后返充余额后 FIFO 销欠:建结账单并减 remain,不再加余额(钱已入账)。
  174. * 【为什么】对齐销售侧 afterCreditFifoClear,余额流水可挂 clearId/clearSn/clearAmount。
  175. *
  176. * @param object $ghs 买方供货商(已锁)
  177. * @param array $plan [['cgId'=>int,'clearAmount'=>string,'orderSn'=>string], ...]
  178. * @param array $meta shopId/sjId/staffId/staffName/payWay/remark
  179. * @return object|null 结账单;无计划时 null
  180. */
  181. public static function settlePurchaseFifoAfterCredit($ghs, array $plan, array $meta = [])
  182. {
  183. if (empty($ghs) || empty($plan)) {
  184. return null;
  185. }
  186. $planTotal = '0.00';
  187. $pairs = [];
  188. foreach ($plan as $row) {
  189. $cgId = intval($row['cgId'] ?? 0);
  190. $cut = bcadd((string)($row['clearAmount'] ?? '0'), '0', 2);
  191. if ($cgId <= 0 || bccomp($cut, '0', 2) <= 0) {
  192. continue;
  193. }
  194. $planTotal = bcadd($planTotal, $cut, 2);
  195. $pairs[] = [
  196. 'orderId' => 0,
  197. 'orderSn' => '',
  198. 'cgId' => $cgId,
  199. 'cgSn' => (string)($row['orderSn'] ?? ''),
  200. 'amount' => $cut,
  201. ];
  202. }
  203. if (empty($pairs) || bccomp($planTotal, '0', 2) <= 0) {
  204. return null;
  205. }
  206. $ghsId = intval($ghs->id ?? 0);
  207. $shopId = intval($meta['shopId'] ?? ($ghs->shopId ?? 0));
  208. $sjId = intval($meta['sjId'] ?? 0);
  209. self::expireAwaitPayPurchaseClears($ghsId, $shopId);
  210. $customId = intval($ghs->customId ?? 0);
  211. $custom = $customId > 0 ? CustomClass::getById($customId, true) : null;
  212. $payWay = isset($meta['payWay']) && is_numeric($meta['payWay'])
  213. ? intval($meta['payWay'])
  214. : intval(dict::getDict('payWay', 'balancePay'));
  215. $orderSn = orderSn::getPurchaseClearSn();
  216. $data = [
  217. 'ghsId' => $ghsId,
  218. 'ghsName' => $ghs->name ?? '',
  219. 'ghsAvatar' => $ghs->avatar ?? '',
  220. 'ghsMobile' => $ghs->mobile ?? '',
  221. 'customId' => $customId,
  222. 'customName' => $custom->name ?? '',
  223. 'customAvatar' => $custom->avatar ?? '',
  224. 'customMobile' => $custom->mobile ?? '',
  225. 'sjId' => $sjId,
  226. 'shopId' => $shopId,
  227. 'clearStyle' => dict::getDict('clearStyle', 'gys2KmGys'),
  228. 'customShopAdminId' => intval($meta['staffId'] ?? 0),
  229. 'customShopId' => $shopId,
  230. 'customShopAdminName' => (string)($meta['staffName'] ?? ''),
  231. 'payWay' => $payWay,
  232. 'prePrice' => $planTotal,
  233. 'actPrice' => $planTotal,
  234. 'realPrice' => $planTotal,
  235. 'orderSn' => $orderSn,
  236. 'salt' => \common\components\stringUtil::charsShuffleLowerCase(10),
  237. 'deadline' => date('Y-m-d H:i:s', time() + 120),
  238. 'status' => self::STATUS_AWAIT_PAY,
  239. 'remark' => (string)($meta['remark'] ?? '采购售后返充余额自动销欠'),
  240. ];
  241. $clear = self::add($data, true);
  242. OrderCgClearClass::bindRelations($clear, $pairs, OrderCgClearClass::STATUS_HAS_PAY);
  243. // 只减采购单待结 / 更新 debtNum,不再加余额
  244. self::applyPurchasePaymentSettleOnly($clear, $pairs, $payWay);
  245. return $clear;
  246. }
  247. /**
  248. * 【用途】结账单已建好后:按明细减采购 remain,足额写真实 clearId;不加 xhGhs 余额。
  249. */
  250. protected static function applyPurchasePaymentSettleOnly($clear, array $pairs, $payWay)
  251. {
  252. if (empty($clear)) {
  253. util::fail('没有结账单');
  254. }
  255. if (intval($clear->status ?? 0) === self::STATUS_HAS_PAY) {
  256. // bind 时可能已标 HAS_PAY,仍允许走减 remain
  257. } else {
  258. $clear->status = self::STATUS_HAS_PAY;
  259. }
  260. $clear->payWay = $payWay;
  261. $clear->payTime = date('Y-m-d H:i:s');
  262. $clear->save(false);
  263. $clearId = intval($clear->id ?? 0);
  264. $clearedCount = 0;
  265. foreach ($pairs as $pair) {
  266. $cgId = intval($pair['cgId'] ?? 0);
  267. $cut = bcadd((string)($pair['amount'] ?? '0'), '0', 2);
  268. if ($cgId <= 0 || bccomp($cut, '0', 2) <= 0) {
  269. continue;
  270. }
  271. $purchase = PurchaseOrderClass::getById($cgId, true);
  272. if (empty($purchase) || intval($purchase->debt ?? 0) !== PurchaseOrderClass::DEBT_YES) {
  273. continue;
  274. }
  275. $remain = bcadd((string)($purchase->remainDebtPrice ?? '0'), '0', 2);
  276. if (bccomp($remain, '0', 2) <= 0) {
  277. $remain = bcadd((string)($purchase->actPrice ?? '0'), '0', 2);
  278. }
  279. $newRemain = bcsub($remain, $cut, 2);
  280. if (bccomp($newRemain, '0', 2) < 0) {
  281. $newRemain = '0.00';
  282. }
  283. $purchase->remainDebtPrice = $newRemain;
  284. $saveAttrs = ['remainDebtPrice', 'debt', 'clearId'];
  285. if (bccomp($newRemain, '0', 2) <= 0) {
  286. $purchase->debt = PurchaseOrderClass::DEBT_NO;
  287. $purchase->clearId = $clearId;
  288. $clearedCount++;
  289. } elseif (intval($purchase->clearId ?? 0) === 0) {
  290. $purchase->clearId = 1;
  291. }
  292. $purchase->save(false, $saveAttrs);
  293. }
  294. if ($clearedCount > 0) {
  295. $ghsId = intval($clear->ghsId ?? 0);
  296. $ghs = GhsClass::getLockById($ghsId);
  297. if (!empty($ghs)) {
  298. $ghs->debtNum = max(0, intval($ghs->debtNum ?? 0) - $clearedCount);
  299. $ghs->debt = bccomp((string)($ghs->balance ?? '0'), '0', 2) < 0
  300. ? GhsClass::DEBT_YES
  301. : (intval($ghs->debtNum) > 0 ? GhsClass::DEBT_YES : GhsClass::DEBT_NO);
  302. $ghs->save(false, ['debtNum', 'debt']);
  303. $customId = intval($ghs->customId ?? 0);
  304. if ($customId > 0) {
  305. $custom = CustomClass::getLockById($customId);
  306. if (!empty($custom)) {
  307. $custom->debtNum = max(0, intval($custom->debtNum ?? 0) - $clearedCount);
  308. $custom->isDebt = bccomp((string)($custom->balance ?? '0'), '0', 2) < 0
  309. ? CustomClass::IS_DEBT_YES
  310. : (intval($custom->debtNum) > 0 ? CustomClass::IS_DEBT_YES : CustomClass::IS_DEBT_NO);
  311. $custom->save(false, ['debtNum', 'isDebt']);
  312. }
  313. }
  314. }
  315. }
  316. }
  317. /**
  318. * 【用途】售后返充流水回写结账审计字段(供 ghs 余额变动展示「本单销」)
  319. * 【注意】getByCondition 签名为 (condition, returnObject, order, field),
  320. * 勿把「id DESC」传到第 4 参,否则会当成 SELECT 字段导致查不到 clearId。
  321. */
  322. public static function patchGhsBalanceChangeClearAudit($ghsId, $relateId, $clear, $clearAmount = null)
  323. {
  324. $ghsId = intval($ghsId);
  325. $relateId = intval($relateId);
  326. $clearId = intval(is_object($clear) ? ($clear->id ?? 0) : 0);
  327. if ($ghsId <= 0 || $relateId <= 0 || $clearId <= 0) {
  328. return;
  329. }
  330. // 第 3 参 order、第 4 参 field;取最新一条入账流水挂结账单
  331. $row = GhsBalanceChangeClass::getByCondition([
  332. 'ghsId' => $ghsId,
  333. 'relateId' => $relateId,
  334. 'io' => 1,
  335. ], true, 'id DESC', '*');
  336. if (empty($row) || !method_exists($row, 'hasAttribute') || !$row->hasAttribute('clearId')) {
  337. return;
  338. }
  339. $row->clearId = $clearId;
  340. $row->clearSn = (string)(is_object($clear) ? ($clear->orderSn ?? '') : '');
  341. $row->clearAmount = bcadd(
  342. (string)($clearAmount !== null ? $clearAmount : (is_object($clear) ? ($clear->actPrice ?? 0) : 0)),
  343. '0',
  344. 2
  345. );
  346. $row->save(false, ['clearId', 'clearSn', 'clearAmount']);
  347. }
  348. //支付成功后的流程 ssh 2021.4.28
  349. public static function complete($order, $payWay)
  350. {
  351. if (empty($order)) {
  352. util::fail('没有找到订单');
  353. }
  354. if (isset($order->status) == false || $order->status != self::STATUS_AWAIT_PAY) {
  355. util::fail('订单不是待付款状态');
  356. }
  357. $order->status = self::STATUS_HAS_PAY;
  358. $order->save();
  359. $clearId = $order->id;
  360. $arr = OrderCgClearClass::getCgIdsByClearId($clearId);
  361. if (empty($arr)) {
  362. $msg = "没有找到采购订单";
  363. util::fail($msg);
  364. }
  365. $list = PurchaseOrderClass::getAllByCondition(['id' => ['in', $arr]], null, '*', null, true);
  366. if (empty($list)) {
  367. $msg = "没有找到采购订单!";
  368. util::fail($msg);
  369. }
  370. //供货商欠款变更
  371. $ghsId = $order->ghsId;
  372. $ghs = GhsClass::getLockById($ghsId);
  373. if (empty($ghs)) {
  374. util::fail('没有找到供货商信息');
  375. }
  376. $ghsAddDebtKey = 'ghs_change_debt_' . $ghsId;
  377. $lock = util::lock($ghsAddDebtKey);
  378. if (!$lock) {
  379. util::fail("系统繁忙,请重试");
  380. }
  381. PurchaseOrderClass::applyPurchaseClearOnBuyerGhs($ghs, $order->prePrice, $order, count($arr));
  382. util::unlock($ghsAddDebtKey);
  383. $shopId = $ghs->shopId;
  384. $shop = ShopClass::getLockById($shopId);
  385. if (empty($shop)) {
  386. util::fail('没有找到供货商门店');
  387. }
  388. $mainId = $shop->mainId ?? 0;
  389. $main = MainClass::getLockById($mainId);
  390. if (empty($main)) {
  391. util::fail('没有main信息19');
  392. }
  393. //门店应收客户款减少
  394. $currentGathering = bcsub($main->mayGathering, $order->actPrice, 2);
  395. $main->mayGathering = $currentGathering;
  396. $main->save();
  397. //采购单状态变更:结清剩余待结
  398. foreach ($list as $purchase) {
  399. $purchase->debt = PurchaseOrderClass::DEBT_NO;
  400. $purchase->remainDebtPrice = '0.00';
  401. $purchase->payWay = $payWay;
  402. $purchase->clearId = $clearId;
  403. $purchase->save(false, ['debt', 'remainDebtPrice', 'payWay', 'clearId']);
  404. }
  405. $custom = PurchaseOrderClass::applyPurchaseClearOnSupplierCustom($ghs, $order->actPrice, count($arr), $main);
  406. //应付供货商款减少
  407. $customShopId = $custom->shopId;
  408. $customShop = ShopClass::getLockById($customShopId);
  409. if (empty($customShop)) {
  410. util::fail('没有找到客户门店');
  411. }
  412. $customMainId = $customShop->mainId ?? 0;
  413. $customMain = MainClass::getLockById($customMainId);
  414. if (empty($customMain)) {
  415. util::fail('没有main信息20');
  416. }
  417. $currentMayPay = bcsub($customMain->mayPay, $order->actPrice, 2);
  418. $customMain->mayPay = $currentMayPay;
  419. if ($ghs->debtNum == 0) {
  420. $customMain->mayPayNum -= 1;
  421. }
  422. $customMain->save();
  423. //扣除现金
  424. if ($payWay == dict::getDict('payWay', 'cash')) {
  425. $balance = bcsub($customMain->money, $order->actPrice, 2);
  426. if ($balance < 0) {
  427. util::fail('现金不足');
  428. }
  429. $customMain->money = $balance;
  430. $customMain->save();
  431. $staffId = $order->customShopAdminId ?? 0;
  432. $staffName = $order->customShopAdminName ?? '';
  433. $event = $staffName . '买花结账支出' . $order->actPrice . '元,单号:' . $order->orderSn;
  434. $capitalType = dict::getDict('capitalType', 'ghsClearExpend', 'id');
  435. $data = [];
  436. $data['balance'] = $balance;
  437. $data['mainId'] = $customMainId;
  438. $data['sjId'] = 0;
  439. $data['amount'] = $order->actPrice ?? 0;
  440. $data['staffId'] = $staffId;
  441. $data['staffName'] = $staffName;
  442. $data['ptStyle'] = dict::getDict('ptStyle', 'ghs');
  443. $data['capitalType'] = $capitalType;
  444. $data['event'] = $event;
  445. $data['remark'] = '';
  446. ShopMoneyClass::addData($data);
  447. }
  448. }
  449. //供货商的采购信息 ssh 20210625
  450. public static function getGhsCgInfo($info)
  451. {
  452. $clearId = intval($info['id'] ?? 0);
  453. $cgArr = OrderCgClearClass::getCgIdsByClearId($clearId);
  454. $cgList = empty($cgArr) ? [] : PurchaseOrderClass::getAllByCondition(['id' => ['in', $cgArr]], null, 'id,orderSn,bigNum,smallNum,realPrice,addTime', null);
  455. $info['orderList'] = $cgList;
  456. $info['orderNum'] = count($cgArr);
  457. $clearStyle = $info['clearStyle'] ?? dict::getDict('clearStyle', 'gys2KmGys');
  458. $operator = $info['customShopAdminName'] ?? '';
  459. if ($clearStyle == dict::getDict('clearStyle', 'kmGys2gys')) {
  460. $operator = '商家';
  461. }
  462. $info['operator'] = $operator;
  463. return $info;
  464. }
  465. //零售的采购信息 ssh 20210625
  466. public static function getCgInfo($info)
  467. {
  468. $id = $info['id'] ?? 0;
  469. $deadline = $info['deadline'] ?? '';
  470. $deadTime = strtotime($deadline);
  471. $current = time();
  472. // 仅待结清且已过期时才取消,避免已结清账单被误改状态
  473. if ($current >= $deadTime && ($info['status'] ?? 0) == PurchaseClearClass::STATUS_AWAIT_PAY) {
  474. $n = date("Y-m-d H:i:s");
  475. $info['status'] = PurchaseClearClass::STATUS_EXPIRE;
  476. self::updateById($id, [
  477. 'status' => PurchaseClearClass::STATUS_EXPIRE,
  478. 'remark' => "账单过期,已自动取消,参数:{$deadline} {$n}",
  479. ]);
  480. }
  481. $clearId = intval($info['id'] ?? 0);
  482. $cgArr = OrderCgClearClass::getCgIdsByClearId($clearId);
  483. $cgList = empty($cgArr) ? [] : PurchaseClass::getAllByCondition(['id' => ['in', $cgArr]], null, '*', null);
  484. $info['orderList'] = self::buildPartialClearLinksForPurchaseOrders($cgList);
  485. $info['orderNum'] = count($cgArr);
  486. $clearStyle = $info['clearStyle'] ?? dict::getDict('clearStyle', 'hd2Gys');
  487. $operator = $info['customShopAdminName'] ?? '';
  488. if ($clearStyle == dict::getDict('clearStyle', 'gys2Hd')) {
  489. $operator = '商家';
  490. }
  491. $info['operator'] = $operator;
  492. return $info;
  493. }
  494. //完整信息 ssh 20210625
  495. public static function getSaleInfo($info)
  496. {
  497. $clearId = intval($info['id'] ?? 0);
  498. $saleArr = OrderCgClearClass::getOrderIdsByClearId($clearId);
  499. $saleList = empty($saleArr) ? [] : OrderClass::getAllByCondition(['id' => ['in', $saleArr]], null, '*', null);
  500. $info['orderList'] = self::buildPartialClearLinksForSaleOrders($saleList);
  501. $info['orderNum'] = count($saleArr);
  502. $type = $info['clearStyle'] ?? dict::getDict('clearStyle', 'gys2KmGys');
  503. $operator = $info['ghsShopAdminName'] ?? '';
  504. if ($type == dict::getDict('clearStyle', 'hd2Gys')) {
  505. $operator = '客户';
  506. }
  507. $info['operator'] = $operator;
  508. return $info;
  509. }
  510. /**
  511. * 结账单详情(供货商销售单):附加 orderClearList,与订单详情 OrderService 一致。
  512. */
  513. protected static function buildPartialClearLinksForSaleOrders($saleList)
  514. {
  515. if (empty($saleList)) {
  516. return [];
  517. }
  518. $out = [];
  519. foreach ($saleList as $order) {
  520. $row = self::normalizeOrderRow($order);
  521. $orderId = intval($row['id'] ?? 0);
  522. $row['remainDebtPrice'] = bcadd((string)($row['remainDebtPrice'] ?? '0'), '0', 2);
  523. $row['orderClearList'] = $orderId > 0
  524. ? OrderCgClearClass::listPaidClearRecordsForOrder($orderId)
  525. : [];
  526. $out[] = $row;
  527. }
  528. return $out;
  529. }
  530. /**
  531. * 结账单详情(花店采购单):附加 orderClearList;待结以批发销售单为准(采购单可能滞后)。
  532. */
  533. protected static function buildPartialClearLinksForPurchaseOrders($cgList)
  534. {
  535. if (empty($cgList)) {
  536. return [];
  537. }
  538. $out = [];
  539. foreach ($cgList as $purchase) {
  540. $row = self::normalizeOrderRow($purchase);
  541. $cgId = intval($row['id'] ?? 0);
  542. $saleId = intval($row['saleId'] ?? 0);
  543. $row['remainDebtPrice'] = self::resolvePurchaseRemainDebtPrice($row);
  544. $row['orderClearList'] = OrderCgClearClass::listPaidClearRecordsForPurchase($cgId, $saleId);
  545. $out[] = $row;
  546. }
  547. return $out;
  548. }
  549. /** 采购单待结:优先对齐 xhOrder.remainDebtPrice */
  550. protected static function resolvePurchaseRemainDebtPrice(array $row)
  551. {
  552. $remain = bcadd((string)($row['remainDebtPrice'] ?? '0'), '0', 2);
  553. $saleId = intval($row['saleId'] ?? 0);
  554. if ($saleId <= 0) {
  555. return $remain;
  556. }
  557. $sale = OrderClass::getById($saleId, true);
  558. if (empty($sale)) {
  559. return $remain;
  560. }
  561. return bcadd((string)($sale->remainDebtPrice ?? '0'), '0', 2);
  562. }
  563. /** 统一订单行为数组,便于前端附加 orderClearList */
  564. protected static function normalizeOrderRow($row)
  565. {
  566. if (is_array($row)) {
  567. return $row;
  568. }
  569. if (is_object($row) && method_exists($row, 'getAttributes')) {
  570. return $row->getAttributes();
  571. }
  572. return (array)$row;
  573. }
  574. }