| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- <?php
- namespace bizGhs\clear\classes;
- use bizGhs\base\classes\BaseClass;
- use bizGhs\order\classes\OrderClass;
- use bizGhs\order\classes\PurchaseOrderClass;
- use bizHd\purchase\classes\PurchaseClass;
- use bizHd\purchase\classes\PurchaseClearClass;
- 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 [];
- }
- $hdPurchaseIds = [];
- foreach ($orderList as $order) {
- $purchaseId = is_array($order) ? ($order['purchaseId'] ?? 0) : ($order->purchaseId ?? 0);
- if (!empty($purchaseId)) {
- $hdPurchaseIds[] = $purchaseId;
- }
- }
- $cgMap = [];
- if (!empty($hdPurchaseIds)) {
- $cgList = PurchaseClass::getByIds(array_unique($hdPurchaseIds));
- 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 = self::resolveDebtAmount($order);
- $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 [];
- }
- $ghsOrderIds = [];
- foreach ($purchaseList as $purchase) {
- $saleId = is_array($purchase) ? ($purchase['saleId'] ?? 0) : ($purchase->saleId ?? 0);
- if (!empty($saleId)) {
- $ghsOrderIds[] = $saleId;
- }
- }
- $orderMap = [];
- if (!empty($ghsOrderIds)) {
- $orderList = OrderClass::getByIds(array_unique($ghsOrderIds));
- 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 = self::resolveDebtAmount($purchase);
- $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;
- }
- /**
- * gys2KmGys:cgId 存 xhGhsCgOrder.id,orderId=0
- */
- public static function buildPairsFromGhsCgOrders($cgList)
- {
- if (empty($cgList)) {
- return [];
- }
- $pairs = [];
- foreach ($cgList as $cg) {
- $cgId = intval(is_array($cg) ? ($cg['id'] ?? 0) : ($cg->id ?? 0));
- if ($cgId <= 0) {
- continue;
- }
- $cgSn = is_array($cg) ? ($cg['orderSn'] ?? '') : ($cg->orderSn ?? '');
- $pairs[] = [
- 'orderId' => 0,
- 'orderSn' => '',
- 'cgId' => $cgId,
- 'cgSn' => $cgSn,
- 'amount' => self::resolveDebtAmount($cg),
- ];
- }
- 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);
- $newAmount = self::normalizeAmount($pair['amount'] ?? 0);
- if (!empty($exists)) {
- self::patchAmountIfEmpty($exists, $newAmount);
- continue;
- }
- self::add([
- 'clearId' => $clearId,
- 'clearSn' => $clearSn,
- 'orderId' => $orderId,
- 'orderSn' => $pair['orderSn'] ?? '',
- 'cgId' => $cgId,
- 'cgSn' => $pair['cgSn'] ?? '',
- 'amount' => $newAmount,
- '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();
- }
- }
- /**
- * 销售单已销账的结账单列表(部分销、多次销均含,对齐 hd orderSettleList)
- *
- * @return array<int, array{clearId:int, clearSn:string, amount:string}>
- */
- public static function listPaidClearRecordsForOrder($orderId)
- {
- $orderId = intval($orderId);
- if ($orderId <= 0) {
- return [];
- }
- $list = self::getAllByCondition([
- 'orderId' => $orderId,
- 'status' => self::STATUS_HAS_PAY,
- ], 'id asc', '*', null, true);
- return self::mapPaidClearRecordRows($list);
- }
- /**
- * 采购单关联的已销结账单(只认本采购单 cgId,绝不回退到 saleId)
- * 说明1:若按 saleId 查,会把该关联销售单的客户结账一并带出,采购详情「结账信息」会错混销售结账。
- * 说明2:xhOrderCgClear.cgId 在不同结账场景下可能指向不同表的主键(如供货商自身采购
- * bizGhs\order\classes\PurchaseOrderClass 与花店采购 bizHd\purchase\classes\PurchaseClass),
- * 两边自增 id 存在撞号可能。仅按 cgId 过滤仍可能混入其它业务线的结账记录,
- * 必须再按 xhClear.clearStyle 精确限定属于哪种结账方式,才能确认是「同一个」采购单。
- *
- * @param int $purchaseId 采购单/cgId
- * @param int|null $clearStyle 限定的结账方式(dict clearStyle),传 null 则不按结账方式过滤
- * @return array<int, array{clearId:int, clearSn:string, amount:string}>
- */
- public static function listPaidClearRecordsForPurchase($purchaseId, $clearStyle = null)
- {
- $purchaseId = intval($purchaseId);
- if ($purchaseId <= 0) {
- return [];
- }
- $cgList = self::getAllByCondition([
- 'cgId' => $purchaseId,
- 'status' => self::STATUS_HAS_PAY,
- ], 'id asc', '*', null, true);
- if (empty($cgList)) {
- return [];
- }
- if ($clearStyle !== null) {
- $clearIds = array_values(array_unique(array_filter(array_map(function ($item) {
- return intval(is_object($item) ? ($item->clearId ?? 0) : ($item['clearId'] ?? 0));
- }, $cgList))));
- $allowedClearIds = empty($clearIds) ? [] : ClearClass::getAllByCondition([
- 'id' => ['in', $clearIds],
- 'clearStyle' => intval($clearStyle),
- ], null, 'id', null);
- $allowedClearIds = array_flip(array_map('intval', array_column($allowedClearIds, 'id')));
- $cgList = array_values(array_filter($cgList, function ($item) use ($allowedClearIds) {
- $clearId = intval(is_object($item) ? ($item->clearId ?? 0) : ($item['clearId'] ?? 0));
- return isset($allowedClearIds[$clearId]);
- }));
- }
- return self::mapPaidClearRecordRows($cgList);
- }
- protected static function mapPaidClearRecordRows($list)
- {
- if (empty($list)) {
- return [];
- }
- $rows = [];
- foreach ($list as $item) {
- $clearId = intval(is_object($item) ? ($item->clearId ?? 0) : ($item['clearId'] ?? 0));
- if ($clearId <= 0) {
- continue;
- }
- $rows[] = [
- 'clearId' => $clearId,
- 'clearSn' => (string)(is_object($item) ? ($item->clearSn ?? '') : ($item['clearSn'] ?? '')),
- 'amount' => self::normalizeAmount(is_object($item) ? ($item->amount ?? 0) : ($item['amount'] ?? 0)),
- ];
- }
- return $rows;
- }
- public static function getOrderIdsByClearId($clearId)
- {
- $clearId = intval($clearId);
- if ($clearId <= 0) {
- return [];
- }
- $list = self::getAllByCondition(['clearId' => $clearId, 'orderId>' => 0], null, 'orderId', null);
- return array_values(array_unique(array_filter(array_map('intval', array_column($list, 'orderId')))));
- }
- public static function getCgIdsByClearId($clearId)
- {
- $clearId = intval($clearId);
- if ($clearId <= 0) {
- return [];
- }
- $list = self::getAllByCondition(['clearId' => $clearId, 'cgId>' => 0], null, 'cgId', null);
- return array_values(array_unique(array_filter(array_map('intval', array_column($list, 'cgId')))));
- }
- 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;
- }
- /**
- * 待结金额:销售单/采购单用 remainDebtPrice(与 hd 充值销账 FIFO 一致),勿用 actPrice(实付会小于待结)
- */
- protected static function resolveDebtAmount($entity)
- {
- $remain = self::readField($entity, 'remainDebtPrice');
- if ($remain !== '' && $remain !== null && bccomp(self::normalizeAmount($remain), '0', 2) > 0) {
- return self::normalizeAmount($remain);
- }
- return self::normalizeAmount(self::readField($entity, 'actPrice'));
- }
- protected static function readField($entity, $key)
- {
- return is_array($entity) ? ($entity[$key] ?? 0) : ($entity->$key ?? 0);
- }
- protected static function normalizeAmount($amount)
- {
- if ($amount === '' || $amount === null) {
- return '0.00';
- }
- return bcadd((string)$amount, '0', 2);
- }
- protected static function patchAmountIfEmpty($row, $amount)
- {
- $amount = self::normalizeAmount($amount);
- if (bccomp($amount, '0', 2) <= 0) {
- return false;
- }
- $current = self::normalizeAmount(is_object($row) ? ($row->amount ?? 0) : ($row['amount'] ?? 0));
- if (bccomp($current, '0', 2) > 0) {
- return false;
- }
- if (is_object($row)) {
- $row->amount = $amount;
- return $row->save(false);
- }
- return false;
- }
- }
|