|
@@ -0,0 +1,277 @@
|
|
|
|
|
+<?php
|
|
|
|
|
+
|
|
|
|
|
+namespace bizGhs\clear\classes;
|
|
|
|
|
+
|
|
|
|
|
+use bizGhs\base\classes\BaseClass;
|
|
|
|
|
+use bizGhs\order\classes\OrderClass;
|
|
|
|
|
+use bizHd\purchase\classes\PurchaseClass;
|
|
|
|
|
+use bizHd\purchase\classes\PurchaseClearClass;
|
|
|
|
|
+use common\components\dict;
|
|
|
|
|
+
|
|
|
|
|
+class OrderCgClearClass extends BaseClass
|
|
|
|
|
+{
|
|
|
|
|
+
|
|
|
|
|
+ public static $baseFile = '\bizGhs\clear\models\OrderCgClear';
|
|
|
|
|
+
|
|
|
|
|
+ const STATUS_AWAIT_PAY = 1;
|
|
|
|
|
+ const STATUS_HAS_PAY = 2;
|
|
|
|
|
+ const STATUS_EXPIRE = 3;
|
|
|
|
|
+
|
|
|
|
|
+ public static function buildPairsFromOrders($orderList)
|
|
|
|
|
+ {
|
|
|
|
|
+ if (empty($orderList)) {
|
|
|
|
|
+ return [];
|
|
|
|
|
+ }
|
|
|
|
|
+ $purchaseIds = [];
|
|
|
|
|
+ foreach ($orderList as $order) {
|
|
|
|
|
+ $purchaseId = is_array($order) ? ($order['purchaseId'] ?? 0) : ($order->purchaseId ?? 0);
|
|
|
|
|
+ if (!empty($purchaseId)) {
|
|
|
|
|
+ $purchaseIds[] = $purchaseId;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ $cgMap = [];
|
|
|
|
|
+ if (!empty($purchaseIds)) {
|
|
|
|
|
+ $cgList = PurchaseClass::getByIds(array_unique($purchaseIds));
|
|
|
|
|
+ if (!empty($cgList)) {
|
|
|
|
|
+ $cgMap = array_column($cgList, null, 'id');
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ $pairs = [];
|
|
|
|
|
+ foreach ($orderList as $order) {
|
|
|
|
|
+ $orderId = is_array($order) ? ($order['id'] ?? 0) : ($order->id ?? 0);
|
|
|
|
|
+ $orderSn = is_array($order) ? ($order['orderSn'] ?? '') : ($order->orderSn ?? '');
|
|
|
|
|
+ $amount = is_array($order) ? ($order['remainDebtPrice'] ?? 0) : ($order->remainDebtPrice ?? 0);
|
|
|
|
|
+ $cgId = intval(is_array($order) ? ($order['purchaseId'] ?? 0) : ($order->purchaseId ?? 0));
|
|
|
|
|
+ $cgSn = '';
|
|
|
|
|
+ if ($cgId > 0 && isset($cgMap[$cgId])) {
|
|
|
|
|
+ $cgSn = $cgMap[$cgId]['orderSn'] ?? '';
|
|
|
|
|
+ }
|
|
|
|
|
+ $pairs[] = [
|
|
|
|
|
+ 'orderId' => intval($orderId),
|
|
|
|
|
+ 'orderSn' => $orderSn,
|
|
|
|
|
+ 'cgId' => $cgId,
|
|
|
|
|
+ 'cgSn' => $cgSn,
|
|
|
|
|
+ 'amount' => $amount,
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+ return $pairs;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static function buildPairsFromPurchases($purchaseList)
|
|
|
|
|
+ {
|
|
|
|
|
+ if (empty($purchaseList)) {
|
|
|
|
|
+ return [];
|
|
|
|
|
+ }
|
|
|
|
|
+ $saleIds = [];
|
|
|
|
|
+ foreach ($purchaseList as $purchase) {
|
|
|
|
|
+ $saleId = is_array($purchase) ? ($purchase['saleId'] ?? 0) : ($purchase->saleId ?? 0);
|
|
|
|
|
+ if (!empty($saleId)) {
|
|
|
|
|
+ $saleIds[] = $saleId;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ $orderMap = [];
|
|
|
|
|
+ if (!empty($saleIds)) {
|
|
|
|
|
+ $orderList = OrderClass::getByIds(array_unique($saleIds));
|
|
|
|
|
+ if (!empty($orderList)) {
|
|
|
|
|
+ $orderMap = array_column($orderList, null, 'id');
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ $pairs = [];
|
|
|
|
|
+ foreach ($purchaseList as $purchase) {
|
|
|
|
|
+ $cgId = is_array($purchase) ? ($purchase['id'] ?? 0) : ($purchase->id ?? 0);
|
|
|
|
|
+ $cgSn = is_array($purchase) ? ($purchase['orderSn'] ?? '') : ($purchase->orderSn ?? '');
|
|
|
|
|
+ $amount = is_array($purchase) ? ($purchase['remainDebtPrice'] ?? 0) : ($purchase->remainDebtPrice ?? 0);
|
|
|
|
|
+ $orderId = intval(is_array($purchase) ? ($purchase['saleId'] ?? 0) : ($purchase->saleId ?? 0));
|
|
|
|
|
+ $orderSn = '';
|
|
|
|
|
+ if ($orderId > 0 && isset($orderMap[$orderId])) {
|
|
|
|
|
+ $orderSn = $orderMap[$orderId]['orderSn'] ?? '';
|
|
|
|
|
+ }
|
|
|
|
|
+ $pairs[] = [
|
|
|
|
|
+ 'orderId' => $orderId,
|
|
|
|
|
+ 'orderSn' => $orderSn,
|
|
|
|
|
+ 'cgId' => intval($cgId),
|
|
|
|
|
+ 'cgSn' => $cgSn,
|
|
|
|
|
+ 'amount' => $amount,
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+ return $pairs;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static function bindRelations($clear, $pairs, $status = self::STATUS_AWAIT_PAY)
|
|
|
|
|
+ {
|
|
|
|
|
+ $clearId = is_object($clear) ? ($clear->id ?? 0) : ($clear['id'] ?? 0);
|
|
|
|
|
+ $clearSn = is_object($clear) ? ($clear->orderSn ?? '') : ($clear['orderSn'] ?? '');
|
|
|
|
|
+ if (empty($clearId) || empty($pairs)) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ foreach ($pairs as $pair) {
|
|
|
|
|
+ $orderId = intval($pair['orderId'] ?? 0);
|
|
|
|
|
+ $cgId = intval($pair['cgId'] ?? 0);
|
|
|
|
|
+ if ($orderId <= 0 && $cgId <= 0) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ $exists = self::getByCondition([
|
|
|
|
|
+ 'clearId' => $clearId,
|
|
|
|
|
+ 'orderId' => $orderId,
|
|
|
|
|
+ 'cgId' => $cgId,
|
|
|
|
|
+ ], true);
|
|
|
|
|
+ if (!empty($exists)) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ self::add([
|
|
|
|
|
+ 'clearId' => $clearId,
|
|
|
|
|
+ 'clearSn' => $clearSn,
|
|
|
|
|
+ 'orderId' => $orderId,
|
|
|
|
|
+ 'orderSn' => $pair['orderSn'] ?? '',
|
|
|
|
|
+ 'cgId' => $cgId,
|
|
|
|
|
+ 'cgSn' => $pair['cgSn'] ?? '',
|
|
|
|
|
+ 'amount' => $pair['amount'] ?? 0,
|
|
|
|
|
+ 'status' => $status,
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static function markDoneByClearId($clearId)
|
|
|
|
|
+ {
|
|
|
|
|
+ self::markStatusByClearId($clearId, self::STATUS_HAS_PAY);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static function markExpireByClearId($clearId)
|
|
|
|
|
+ {
|
|
|
|
|
+ self::markStatusByClearId($clearId, self::STATUS_EXPIRE);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static function markStatusByClearId($clearId, $status)
|
|
|
|
|
+ {
|
|
|
|
|
+ $clearId = intval($clearId);
|
|
|
|
|
+ $status = intval($status);
|
|
|
|
|
+ if ($clearId <= 0) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ $list = self::getAllByCondition(['clearId' => $clearId], null, '*', null, true);
|
|
|
|
|
+ if (empty($list)) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ foreach ($list as $item) {
|
|
|
|
|
+ $item->status = $status;
|
|
|
|
|
+ $item->save();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static function getOrderIdsByClearId($clearId, $fallbackSaleIds = '')
|
|
|
|
|
+ {
|
|
|
|
|
+ $clearId = intval($clearId);
|
|
|
|
|
+ if ($clearId <= 0) {
|
|
|
|
|
+ return self::parseIds($fallbackSaleIds);
|
|
|
|
|
+ }
|
|
|
|
|
+ $list = self::getAllByCondition(['clearId' => $clearId, 'orderId>' => 0], null, 'orderId', null);
|
|
|
|
|
+ $ids = array_values(array_unique(array_filter(array_map('intval', array_column($list, 'orderId')))));
|
|
|
|
|
+ if (!empty($ids)) {
|
|
|
|
|
+ return $ids;
|
|
|
|
|
+ }
|
|
|
|
|
+ return self::parseIds($fallbackSaleIds);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static function getCgIdsByClearId($clearId, $fallbackPurchaseIds = '')
|
|
|
|
|
+ {
|
|
|
|
|
+ $clearId = intval($clearId);
|
|
|
|
|
+ if ($clearId <= 0) {
|
|
|
|
|
+ return self::parseIds($fallbackPurchaseIds);
|
|
|
|
|
+ }
|
|
|
|
|
+ $list = self::getAllByCondition(['clearId' => $clearId, 'cgId>' => 0], null, 'cgId', null);
|
|
|
|
|
+ $ids = array_values(array_unique(array_filter(array_map('intval', array_column($list, 'cgId')))));
|
|
|
|
|
+ if (!empty($ids)) {
|
|
|
|
|
+ return $ids;
|
|
|
|
|
+ }
|
|
|
|
|
+ return self::parseIds($fallbackPurchaseIds);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static function getPendingClearIdByOrderId($orderId, $customId)
|
|
|
|
|
+ {
|
|
|
|
|
+ $orderId = intval($orderId);
|
|
|
|
|
+ $customId = intval($customId);
|
|
|
|
|
+ if ($orderId <= 0) {
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ $list = self::getAllByCondition([
|
|
|
|
|
+ 'orderId' => $orderId,
|
|
|
|
|
+ 'status' => self::STATUS_AWAIT_PAY,
|
|
|
|
|
+ ], null, 'clearId', null);
|
|
|
|
|
+ if (empty($list)) {
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ $clearIds = array_unique(array_map('intval', array_column($list, 'clearId')));
|
|
|
|
|
+ $now = date("Y-m-d H:i:s");
|
|
|
|
|
+ foreach ($clearIds as $clearId) {
|
|
|
|
|
+ $clear = ClearClass::getById($clearId, true);
|
|
|
|
|
+ if (empty($clear)) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (($clear->status ?? 0) != PurchaseClearClass::STATUS_AWAIT_PAY) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ if ($customId > 0 && ($clear->customId ?? 0) != $customId) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ $deadline = $clear->deadline ?? '';
|
|
|
|
|
+ if (!empty($deadline) && $deadline < $now) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ return intval($clearId);
|
|
|
|
|
+ }
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static function backfillFromClear($clear)
|
|
|
|
|
+ {
|
|
|
|
|
+ $clearId = is_object($clear) ? ($clear->id ?? 0) : ($clear['id'] ?? 0);
|
|
|
|
|
+ if (empty($clearId)) {
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ $exists = self::getByCondition(['clearId' => $clearId], true);
|
|
|
|
|
+ if (!empty($exists)) {
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ $clearStyle = is_object($clear) ? ($clear->clearStyle ?? 0) : ($clear['clearStyle'] ?? 0);
|
|
|
|
|
+ $hd2Gys = dict::getDict('clearStyle', 'hd2Gys');
|
|
|
|
|
+ $gys2Hd = dict::getDict('clearStyle', 'gys2Hd');
|
|
|
|
|
+ if (!in_array(intval($clearStyle), [intval($hd2Gys), intval($gys2Hd)])) {
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ $saleIds = is_object($clear) ? ($clear->saleIds ?? '') : ($clear['saleIds'] ?? '');
|
|
|
|
|
+ $purchaseIds = is_object($clear) ? ($clear->purchaseIds ?? '') : ($clear['purchaseIds'] ?? '');
|
|
|
|
|
+ $pairs = [];
|
|
|
|
|
+ $saleArr = self::parseIds($saleIds);
|
|
|
|
|
+ if (!empty($saleArr)) {
|
|
|
|
|
+ $orderList = OrderClass::getByIds($saleArr);
|
|
|
|
|
+ if (!empty($orderList)) {
|
|
|
|
|
+ $pairs = self::buildPairsFromOrders($orderList);
|
|
|
|
|
+ }
|
|
|
|
|
+ } elseif (!empty(self::parseIds($purchaseIds))) {
|
|
|
|
|
+ $cgArr = self::parseIds($purchaseIds);
|
|
|
|
|
+ $purchaseList = PurchaseClass::getAllByCondition(['id' => ['in', $cgArr]], null, '*', null, true);
|
|
|
|
|
+ if (!empty($purchaseList)) {
|
|
|
|
|
+ $pairs = self::buildPairsFromPurchases($purchaseList);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (empty($pairs)) {
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ $status = intval(is_object($clear) ? ($clear->status ?? 0) : ($clear['status'] ?? 0));
|
|
|
|
|
+ if (!in_array($status, [self::STATUS_AWAIT_PAY, self::STATUS_HAS_PAY, self::STATUS_EXPIRE], true)) {
|
|
|
|
|
+ $status = self::STATUS_AWAIT_PAY;
|
|
|
|
|
+ }
|
|
|
|
|
+ self::bindRelations($clear, $pairs, $status);
|
|
|
|
|
+ return count($pairs);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ protected static function parseIds($idStr)
|
|
|
|
|
+ {
|
|
|
|
|
+ if ($idStr === '' || $idStr === null) {
|
|
|
|
|
+ return [];
|
|
|
|
|
+ }
|
|
|
|
|
+ $arr = explode(',', (string)$idStr);
|
|
|
|
|
+ return array_values(array_unique(array_filter(array_map('intval', $arr))));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|