|
|
@@ -20,299 +20,6 @@ use Yii;
|
|
|
class ClearController extends Controller
|
|
|
{
|
|
|
|
|
|
- //补回 xhOrderCgClear
|
|
|
- public function actionBc()
|
|
|
- {
|
|
|
-
|
|
|
- ini_set('memory_limit', '2045M');
|
|
|
- set_time_limit(0);
|
|
|
-
|
|
|
- if (getenv('YII_ENV') == 'production') {
|
|
|
- $autoId = 817342;
|
|
|
- } else {
|
|
|
- $autoId = 1426;
|
|
|
- }
|
|
|
-
|
|
|
- $query = new \yii\db\Query();
|
|
|
- $query->from(Clear::tableName());
|
|
|
- $query->where(['>', 'id', $autoId]);
|
|
|
- foreach ($query->batch(500) as $changeList) {
|
|
|
-
|
|
|
- $ids = array_column($changeList, 'id');
|
|
|
- $relate = OrderCgClearClass::getAllByCondition(['clearId' => ['in', $ids]], null, '*', 'clearId');
|
|
|
-
|
|
|
- $connection = Yii::$app->db;
|
|
|
- $transaction = $connection->beginTransaction();
|
|
|
-
|
|
|
- try {
|
|
|
-
|
|
|
- foreach ($changeList as $clear) {
|
|
|
- $clearId = $clear['id'] ?? 0;
|
|
|
- $clearSn = $clear['orderSn'] ?? '';
|
|
|
- $status = $clear['status'] ?? 1;
|
|
|
- $clearAmount = $clear['actPrice'] ?? 0;
|
|
|
- $purchaseIds = $clear['purchaseIds'] ?? '';
|
|
|
- $saleIds = $clear['saleIds'] ?? '';
|
|
|
- $clearStyle = $clear['clearStyle'] ?? 1;
|
|
|
- if (!isset($relate[$clearId])) {
|
|
|
- if ($clearStyle == 3) {
|
|
|
- if (!empty($purchaseIds)) {
|
|
|
- $purchaseIdList = explode(',', $purchaseIds);
|
|
|
- $purchaseInfo = PurchaseOrderClass::getAllByCondition(['id' => ['in', $purchaseIdList]], null, 'id,orderSn', 'id');
|
|
|
- foreach ($purchaseInfo as $purchase) {
|
|
|
- $purchaseId = $purchase['id'] ?? 0;
|
|
|
- $purchaseOrderSn = $purchase['orderSn'] ?? 0;
|
|
|
- $addData = ['orderId' => 0, 'orderSn' => '', 'cgId' => $purchaseId, 'cgSn' => $purchaseOrderSn,
|
|
|
- 'clearId' => $clearId, 'clearSn' => $clearSn, 'amount' => $clearAmount, 'status' => $status];
|
|
|
- OrderCgClearClass::add($addData);
|
|
|
- echo "ghs cg clearId {$clearId} OK ### \n";
|
|
|
- }
|
|
|
- } else {
|
|
|
- echo "供货商采购 clearId {$clearId} 有缺数据 \n";
|
|
|
- }
|
|
|
- } else {
|
|
|
- if (!empty($saleIds) && !empty($purchaseIds)) {
|
|
|
- $saleIdList = explode(',', $saleIds);
|
|
|
- $purchaseIdList = explode(',', $purchaseIds);
|
|
|
- $saleInfo = OrderClass::getAllByCondition(['id' => ['in', $saleIdList]], null, 'id,orderSn,purchaseId');
|
|
|
- $purchaseInfo = PurchaseClass::getAllByCondition(['id' => ['in', $purchaseIdList]], null, 'id,orderSn', 'id');
|
|
|
- if (!empty($saleInfo)) {
|
|
|
- foreach ($saleInfo as $sale) {
|
|
|
- $saleId = $sale['id'] ?? 0;
|
|
|
- $purchaseId = $sale['purchaseId'] ?? 0;
|
|
|
- $saleOrderSn = $sale['orderSn'] ?? '';
|
|
|
- $purchase = $purchaseInfo[$purchaseId] ?? [];
|
|
|
- if (empty($purchase)) {
|
|
|
- echo "purchase {$purchaseId} 没有找到 \n";
|
|
|
- exit();
|
|
|
- }
|
|
|
- $purchaseOrderSn = $purchase['orderSn'] ?? '';
|
|
|
- $addData = ['orderId' => $saleId, 'orderSn' => $saleOrderSn, 'cgId' => $purchaseId, 'cgSn' => $purchaseOrderSn,
|
|
|
- 'clearId' => $clearId, 'clearSn' => $clearSn, 'amount' => $clearAmount, 'status' => $status];
|
|
|
- OrderCgClearClass::add($addData);
|
|
|
- }
|
|
|
- echo "hd cg clearId {$clearId} OK *** \n";
|
|
|
- }
|
|
|
- } else {
|
|
|
- echo "花店采购 clearId {$clearId} 有缺数据 \n";
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- echo "有记录 {$clearId} \n";
|
|
|
- }
|
|
|
- }
|
|
|
- $transaction->commit();
|
|
|
-
|
|
|
- } catch (\Exception $e) {
|
|
|
- $transaction->rollBack();
|
|
|
- $msg = $e->getMessage();
|
|
|
- echo "报错了:" . $msg . "\n";
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- public function actionBalance()
|
|
|
- {
|
|
|
- ini_set('memory_limit', '2045M');
|
|
|
- set_time_limit(0);
|
|
|
- //$sql = "delete from xhGhsBack";
|
|
|
- //Yii::$app->db->createCommand($sql)->execute();
|
|
|
- $query = new \yii\db\Query();
|
|
|
- $query->from(Ghs::tableName());
|
|
|
- $query->where(['ownPtStyle' => 1]);
|
|
|
- $query->andWhere(['>', 'balance', 0]);
|
|
|
- foreach ($query->batch(500) as $ghsList) {
|
|
|
- $connection = Yii::$app->db;
|
|
|
- $transaction = $connection->beginTransaction();
|
|
|
- try {
|
|
|
- foreach ($ghsList as $ghs) {
|
|
|
- GhsBackClass::add($ghs);
|
|
|
- }
|
|
|
- $transaction->commit();
|
|
|
- } catch (\Exception $e) {
|
|
|
- $transaction->rollBack();
|
|
|
- $msg = $e->getMessage();
|
|
|
- echo "报错了:" . $msg . "\n";
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public function actionBalanceBack()
|
|
|
- {
|
|
|
- $list = GhsBackClass::getAllByCondition(['id>' => 0], null, '*', 'id', true);
|
|
|
- if (!empty($list)) {
|
|
|
- $connection = Yii::$app->db;
|
|
|
- $transaction = $connection->beginTransaction();
|
|
|
- try {
|
|
|
- foreach ($list as $ghs) {
|
|
|
- $ownPtStyle = $ghs['ownPtStyle'] ?? 1;
|
|
|
- $balance = $ghs['balance'] ?? 0;
|
|
|
- $customId = $ghs['customId'] ?? 0;
|
|
|
- if($customId == 16440 || $customId == 46425){
|
|
|
- if ($ownPtStyle == 1) {
|
|
|
- $custom = CustomClass::getById($customId, true);
|
|
|
- if (!empty($custom)) {
|
|
|
- $shopId = $ghs['shopId'] ?? 0;
|
|
|
- $shop = \bizGhs\shop\classes\ShopClass::getById($shopId, true);
|
|
|
- if (!empty($shop)) {
|
|
|
- $staff = new \stdClass();
|
|
|
- $staff->id = 0;
|
|
|
- $staff->name = '';
|
|
|
- $params = [
|
|
|
- 'remark' => '系统升级恢复',
|
|
|
- 'rechargeType' => 0, // 0 代表正常充值
|
|
|
- ];
|
|
|
- // 模拟商家帮充并自动销挂账,payWay 设为 0 (线下微信)
|
|
|
- GhsRechargeSettleService::merchantRechargeWithAutoClear($custom, $balance, $shop, $staff, 0, $params);
|
|
|
- echo "customId: {$customId} 充值成功 {$balance} 元\n";
|
|
|
- } else {
|
|
|
- echo "customId: {$customId} 找不到对应的 shop\n";
|
|
|
- }
|
|
|
- } else {
|
|
|
- echo "customId: {$customId} 找不到对应的 custom\n";
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- $transaction->commit();
|
|
|
- } catch (\Exception $e) {
|
|
|
- $transaction->rollBack();
|
|
|
- echo "执行失败,整体回滚: " . $e->getMessage() . "\n";
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 安全回滚最新一次“系统升级恢复”产生的多余充值和销账数据
|
|
|
- * 支持传参单个 customId 测试:php yii clear/rollback-latest-upgrade 25695
|
|
|
- * 批量跑全部:php yii clear/rollback-latest-upgrade
|
|
|
- */
|
|
|
- public function actionRollbackLatestUpgrade($customId = 0)
|
|
|
- {
|
|
|
- $connection = Yii::$app->db;
|
|
|
- $customId = intval($customId);
|
|
|
-
|
|
|
- // 1. 查找所有备注为 "系统升级恢复" 的充值流水 (按 ID 倒序,最新产生的在最前面)
|
|
|
- $query = (new \yii\db\Query())
|
|
|
- ->from('xhCustomRecharge')
|
|
|
- ->where(['remark' => '系统升级恢复']);
|
|
|
-
|
|
|
- if ($customId > 0) {
|
|
|
- $query->andWhere(['customId' => $customId]);
|
|
|
- }
|
|
|
-
|
|
|
- $recharges = $query->orderBy('id DESC')->all();
|
|
|
-
|
|
|
- if (empty($recharges)) {
|
|
|
- echo "没有找到 '系统升级恢复' 相关的充值记录,无需回滚。\n";
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- // 2. 按客户分组,找到每个客户绝对最新(最后一次运行)的那一笔充值
|
|
|
- $latestRecharges = [];
|
|
|
- foreach ($recharges as $r) {
|
|
|
- $cid = $r['customId'];
|
|
|
- if (!isset($latestRecharges[$cid])) {
|
|
|
- $latestRecharges[$cid] = $r;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if ($customId > 0) {
|
|
|
- echo "正在回滚指定客户 ID: {$customId} 的最新一笔升级恢复数据...\n";
|
|
|
- } else {
|
|
|
- echo "共找到 " . count($latestRecharges) . " 个客户的最新充值记录,准备执行批量安全回滚...\n";
|
|
|
- }
|
|
|
-
|
|
|
- $transaction = $connection->beginTransaction();
|
|
|
- try {
|
|
|
- foreach ($latestRecharges as $cr) {
|
|
|
- $cId = intval($cr['customId']);
|
|
|
- $amount = $cr['amount'];
|
|
|
- $clearId = intval($cr['clearId']);
|
|
|
- $crId = intval($cr['id']);
|
|
|
- $ghsRechargeId = intval($cr['ghsRechargeId'] ?? 0);
|
|
|
-
|
|
|
- // 1) 备注说明:根据用户指令,xhGhs (供货商) 与 xhGhsCustom (客户) 的 balance 余额无需变动还原,因此这里不执行余额扣减。
|
|
|
-
|
|
|
- // 2) 如果这笔充值关联了自动销账(clearId > 0),精准回退订单待结和结账单状态
|
|
|
- if ($clearId > 0) {
|
|
|
- $cgClears = \bizGhs\clear\classes\OrderCgClearClass::getAllByCondition(['clearId' => $clearId], null, '*', null, true);
|
|
|
- foreach ($cgClears as $cgClear) {
|
|
|
- $orderId = intval($cgClear->orderId);
|
|
|
- $cgId = intval($cgClear->cgId);
|
|
|
- $clearedAmount = $cgClear->amount;
|
|
|
-
|
|
|
- // a. 恢复批发端的订单未结挂账金额和状态 (xhOrder)
|
|
|
- if ($orderId > 0) {
|
|
|
- $order = \bizGhs\order\classes\OrderClass::getLockById($orderId);
|
|
|
- if (!empty($order)) {
|
|
|
- $order->remainDebtPrice = bcadd($order->remainDebtPrice, $clearedAmount, 2);
|
|
|
- $order->debt = 1; // 重新恢复为挂账状态
|
|
|
- if ($order->clearId == $clearId) {
|
|
|
- $order->clearId = 0;
|
|
|
- $order->clearTime = '0000-00-00 00:00:00';
|
|
|
- }
|
|
|
- $order->save(false);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // b. 恢复零售花店端的采购单未结挂账金额和状态 (xhPurchase)
|
|
|
- $purchase = null;
|
|
|
- if ($cgId > 0) {
|
|
|
- $purchase = \bizHd\purchase\classes\PurchaseClass::getLockById($cgId);
|
|
|
- } elseif ($orderId > 0) {
|
|
|
- $purchase = \bizHd\purchase\classes\PurchaseClass::getByCondition(['saleId' => $orderId], true);
|
|
|
- if (!empty($purchase)) {
|
|
|
- $purchase = \bizHd\purchase\classes\PurchaseClass::getLockById($purchase->id);
|
|
|
- }
|
|
|
- }
|
|
|
- if (!empty($purchase)) {
|
|
|
- $purchase->remainDebtPrice = bcadd($purchase->remainDebtPrice, $clearedAmount, 2);
|
|
|
- $purchase->debt = 1; // 恢复挂账状态
|
|
|
- if ($purchase->clearId == $clearId) {
|
|
|
- $purchase->clearId = 0;
|
|
|
- $purchase->clearTime = '0000-00-00 00:00:00';
|
|
|
- }
|
|
|
- $purchase->save(false);
|
|
|
- }
|
|
|
-
|
|
|
- // 删除销账明细纪录
|
|
|
- $cgClear->delete();
|
|
|
- }
|
|
|
-
|
|
|
- // 将结账单(xhClear)置为已作废状态 (status = 3)
|
|
|
- $clearObj = \bizGhs\order\classes\OrderClearClass::getLockById($clearId);
|
|
|
- if (!empty($clearObj)) {
|
|
|
- $clearObj->status = 3; // 3 代表已作废/已取消
|
|
|
- $clearObj->save(false);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 3) 删除本次充值产生的余额变动明细纪录
|
|
|
- CustomBalanceChangeClass::deleteByCondition(['relateId' => $crId]);
|
|
|
- if ($ghsRechargeId > 0) {
|
|
|
- GhsBalanceChangeClass::deleteByCondition(['relateId' => $ghsRechargeId]);
|
|
|
- }
|
|
|
-
|
|
|
- // 4) 删除充值记录本身
|
|
|
- $connection->createCommand()->delete('xhCustomRecharge', ['id' => $crId])->execute();
|
|
|
- if ($ghsRechargeId > 0) {
|
|
|
- $connection->createCommand()->delete('xhGhsRecharge', ['id' => $ghsRechargeId])->execute();
|
|
|
- }
|
|
|
-
|
|
|
- echo "【成功】客户 ID: {$cId} 多余的充值及 {$amount} 元的销账订单已完美退回并删除!\n";
|
|
|
- }
|
|
|
-
|
|
|
- $transaction->commit();
|
|
|
- echo "【全部完成】数据已成功安全回滚!\n";
|
|
|
- } catch (\Exception $e) {
|
|
|
- $transaction->rollBack();
|
|
|
- echo "【错误】回滚失败,已安全整体撤销。报错原因: " . $e->getMessage() . "\n";
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
public function actionGhsGhs()
|
|
|
{
|
|
|
ini_set('memory_limit', '2045M');
|