ClearClass.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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\order\classes\OrderClass;
  9. use bizGhs\order\classes\PurchaseOrderClass;
  10. use bizGhs\shop\classes\MainClass;
  11. use bizGhs\shop\classes\ShopMoneyClass;
  12. use bizHd\purchase\classes\PurchaseClass;
  13. use bizHd\purchase\classes\PurchaseClearClass;
  14. use common\components\dict;
  15. use common\components\noticeUtil;
  16. use common\components\orderSn;
  17. use common\components\util;
  18. use Yii;
  19. use common\components\lakala\Lakala;
  20. class ClearClass extends BaseClass
  21. {
  22. public static $baseFile = '\bizGhs\clear\models\Clear';
  23. const STATUS_AWAIT_PAY = 1;
  24. const STATUS_HAS_PAY = 2;
  25. const STATUS_EXPIRE = 3;
  26. public static function refundMoney($shop, $clear)
  27. {
  28. $refundOrderSn = orderSn::getClearRefundSn();
  29. $refundPrice = $clear->actPrice ?? 0;
  30. $orderSn = $clear->orderSn ?? '';
  31. $refundFee = bcmul($refundPrice, 100);
  32. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  33. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  34. $params = [
  35. 'appid' => 'OP00002119',
  36. 'serial_no' => '018b08cfddbd',
  37. 'merchant_no' => $shop->lklSjNo,
  38. 'term_no' => $shop->lklScanTermNo,
  39. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  40. 'lklCertificatePath' => $lklCertificatePath,
  41. ];
  42. $laResource = new Lakala($params);
  43. $refundReason = '';
  44. $thirdNo = $clear->thirdNo ?? '';
  45. $backParams = [
  46. 'refundSn' => $refundOrderSn,
  47. 'thirdNo' => $thirdNo,
  48. 'refundAmount' => $refundFee,
  49. 'refundReason' => $refundReason,
  50. ];
  51. $response = $laResource->refund($backParams);
  52. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  53. $errMsg = $response['msg'] ?? '';
  54. return ['status' => 0, 'msg' => $errMsg];
  55. } else {
  56. $sjName = $shop->merchantName;
  57. $shopName = $shop->shopName;
  58. $name = $shopName == '首店' ? $sjName : $sjName . '-' . $shopName;
  59. noticeUtil::push("取消的结账单,客户的钱已原路退回 success {$orderSn} {$name}", '15280215347');
  60. return ['status' => 1, 'msg' => '退款成功'];
  61. }
  62. }
  63. public static function addOrder($data)
  64. {
  65. $orderSn = orderSn::getPurchaseClearSn();
  66. $idJson = $data['id'];
  67. $idArr = json_decode($idJson, true);
  68. $arr = array_column($idArr, 'id');
  69. if (empty($arr)) {
  70. util::fail('请选择采购单');
  71. }
  72. $purchaseList = PurchaseOrderClass::getAllByCondition(['id' => ['in', $arr]], null, ['id', 'orderSn', 'actPrice', 'realPrice', 'debt', 'ghsId'], null, true);
  73. if (empty($purchaseList)) {
  74. util::fail('请选择采购单');
  75. }
  76. $totalPrice = 0;
  77. $currentGhsId = $data['ghsId'] ?? 0;
  78. foreach ($purchaseList as $purchase) {
  79. if ($purchase->debt != PurchaseOrderClass::DEBT_YES) {
  80. util::fail('请选择有欠款的采购单');
  81. }
  82. if ($purchase->ghsId != $currentGhsId) {
  83. util::fail('请选择同个供货商的订单');
  84. }
  85. $totalPrice = bcadd($totalPrice, $purchase->actPrice, 2);
  86. }
  87. $ghs = GhsClass::getById($currentGhsId, true);
  88. if (empty($ghs)) {
  89. util::fail('没有找到供货商');
  90. }
  91. GhsClass::valid($ghs, $data['shopId']);
  92. $ghsName = $ghs->name ?? '';
  93. $ghsAvatar = $ghs->avatar ?? '';
  94. $ghsMobile = $ghs->mobile ?? '';
  95. $customId = $ghs->customId ?? 0;
  96. $custom = CustomClass::getById($customId, true);
  97. $customName = $custom->name ?? '';
  98. $customAvatar = $custom->avatar ?? '';
  99. $customMobile = $custom->mobile ?? '';
  100. $data['ghsName'] = $ghsName;
  101. $data['ghsAvatar'] = $ghsAvatar;
  102. $data['ghsMobile'] = $ghsMobile;
  103. $data['customName'] = $customName;
  104. $data['customAvatar'] = $customAvatar;
  105. $data['customMobile'] = $customMobile;
  106. $data['prePrice'] = $totalPrice;
  107. $realPrice = $totalPrice;
  108. if (isset($data['modifyPrice']) && !empty($data['modifyPrice'])) {
  109. if (is_numeric($data['modifyPrice']) == false) {
  110. util::fail('实收金额错误');
  111. }
  112. $modifyPrice = $data['modifyPrice'] ?? 0.00;
  113. if ($modifyPrice > $totalPrice) {
  114. util::fail('实收金额不能大于订单金额');
  115. }
  116. if ($modifyPrice < $totalPrice) {
  117. $data['discountAmount'] = bcsub($totalPrice, $modifyPrice, 2);
  118. $data['discountType'] = dict::getDict('discountType', 'discount');
  119. }
  120. $realPrice = $modifyPrice;
  121. }
  122. $data['actPrice'] = $realPrice;
  123. $data['realPrice'] = $realPrice;
  124. $data['orderSn'] = $orderSn;
  125. // 生成并保存 salt 字段,用于结账单的验签和防篡改分享链接
  126. $data['salt'] = $data['salt'] ?? \common\components\stringUtil::charsShuffleLowerCase(10);
  127. $deadTime = time() + 120;
  128. $data['deadline'] = date("Y-m-d H:i:s", $deadTime);
  129. $data['status'] = self::STATUS_AWAIT_PAY;
  130. $order = self::add($data, true);
  131. $pairs = OrderCgClearClass::buildPairsFromGhsCgOrders($purchaseList);
  132. OrderCgClearClass::bindRelations($order, $pairs);
  133. $payWay = $data['payWay'] ?? dict::getDict('payWay', 'wxPay');
  134. self::complete($order, $payWay);
  135. OrderCgClearClass::markDoneByClearId($order->id);
  136. return $order;
  137. }
  138. //支付成功后的流程 ssh 2021.4.28
  139. public static function complete($order, $payWay)
  140. {
  141. if (empty($order)) {
  142. util::fail('没有找到订单');
  143. }
  144. if (isset($order->status) == false || $order->status != self::STATUS_AWAIT_PAY) {
  145. util::fail('订单不是待付款状态');
  146. }
  147. $order->status = self::STATUS_HAS_PAY;
  148. $order->save();
  149. $clearId = $order->id;
  150. $arr = OrderCgClearClass::getCgIdsByClearId($clearId);
  151. if (empty($arr)) {
  152. $msg = "没有找到采购订单";
  153. util::fail($msg);
  154. }
  155. $list = PurchaseOrderClass::getAllByCondition(['id' => ['in', $arr]], null, '*', null, true);
  156. if (empty($list)) {
  157. $msg = "没有找到采购订单!";
  158. util::fail($msg);
  159. }
  160. //供货商欠款变更
  161. $ghsId = $order->ghsId;
  162. $ghs = GhsClass::getLockById($ghsId);
  163. if (empty($ghs)) {
  164. util::fail('没有找到供货商信息');
  165. }
  166. $ghsAddDebtKey = 'ghs_change_debt_' . $ghsId;
  167. $lock = util::lock($ghsAddDebtKey);
  168. if (!$lock) {
  169. util::fail("系统繁忙,请重试");
  170. }
  171. PurchaseOrderClass::applyPurchaseClearOnBuyerGhs($ghs, $order->prePrice, $order, count($arr));
  172. util::unlock($ghsAddDebtKey);
  173. $shopId = $ghs->shopId;
  174. $shop = ShopClass::getLockById($shopId);
  175. if (empty($shop)) {
  176. util::fail('没有找到供货商门店');
  177. }
  178. $mainId = $shop->mainId ?? 0;
  179. $main = MainClass::getLockById($mainId);
  180. if (empty($main)) {
  181. util::fail('没有main信息19');
  182. }
  183. //门店应收客户款减少
  184. $currentGathering = bcsub($main->mayGathering, $order->actPrice, 2);
  185. $main->mayGathering = $currentGathering;
  186. $main->save();
  187. //采购单状态变更
  188. foreach ($list as $purchase) {
  189. $purchase->debt = PurchaseOrderClass::DEBT_NO;
  190. $purchase->payWay = $payWay;
  191. $purchase->clearId = $clearId;
  192. $purchase->save();
  193. }
  194. $custom = PurchaseOrderClass::applyPurchaseClearOnSupplierCustom($ghs, $order->actPrice, count($arr), $main);
  195. //应付供货商款减少
  196. $customShopId = $custom->shopId;
  197. $customShop = ShopClass::getLockById($customShopId);
  198. if (empty($customShop)) {
  199. util::fail('没有找到客户门店');
  200. }
  201. $customMainId = $customShop->mainId ?? 0;
  202. $customMain = MainClass::getLockById($customMainId);
  203. if (empty($customMain)) {
  204. util::fail('没有main信息20');
  205. }
  206. $currentMayPay = bcsub($customMain->mayPay, $order->actPrice, 2);
  207. $customMain->mayPay = $currentMayPay;
  208. if ($ghs->debtNum == 0) {
  209. $customMain->mayPayNum -= 1;
  210. }
  211. $customMain->save();
  212. //扣除现金
  213. if ($payWay == dict::getDict('payWay', 'cash')) {
  214. $balance = bcsub($customMain->money, $order->actPrice, 2);
  215. if ($balance < 0) {
  216. util::fail('现金不足');
  217. }
  218. $customMain->money = $balance;
  219. $customMain->save();
  220. $staffId = $order->customShopAdminId ?? 0;
  221. $staffName = $order->customShopAdminName ?? '';
  222. $event = $staffName . '买花结账支出' . $order->actPrice . '元,单号:' . $order->orderSn;
  223. $capitalType = dict::getDict('capitalType', 'ghsClearExpend', 'id');
  224. $data = [];
  225. $data['balance'] = $balance;
  226. $data['mainId'] = $customMainId;
  227. $data['sjId'] = 0;
  228. $data['amount'] = $order->actPrice ?? 0;
  229. $data['staffId'] = $staffId;
  230. $data['staffName'] = $staffName;
  231. $data['ptStyle'] = dict::getDict('ptStyle', 'ghs');
  232. $data['capitalType'] = $capitalType;
  233. $data['event'] = $event;
  234. $data['remark'] = '';
  235. ShopMoneyClass::addData($data);
  236. }
  237. }
  238. //供货商的采购信息 ssh 20210625
  239. public static function getGhsCgInfo($info)
  240. {
  241. $clearId = intval($info['id'] ?? 0);
  242. $cgArr = OrderCgClearClass::getCgIdsByClearId($clearId);
  243. $cgList = empty($cgArr) ? [] : PurchaseOrderClass::getAllByCondition(['id' => ['in', $cgArr]], null, 'id,orderSn,bigNum,smallNum,realPrice,addTime', null);
  244. $info['orderList'] = $cgList;
  245. $info['orderNum'] = count($cgArr);
  246. $clearStyle = $info['clearStyle'] ?? dict::getDict('clearStyle', 'gys2KmGys');
  247. $operator = $info['customShopAdminName'] ?? '';
  248. if ($clearStyle == dict::getDict('clearStyle', 'kmGys2gys')) {
  249. $operator = '商家';
  250. }
  251. $info['operator'] = $operator;
  252. return $info;
  253. }
  254. //零售的采购信息 ssh 20210625
  255. public static function getCgInfo($info)
  256. {
  257. $id = $info['id'] ?? 0;
  258. $deadline = $info['deadline'] ?? '';
  259. $deadTime = strtotime($deadline);
  260. $current = time();
  261. // 仅待结清且已过期时才取消,避免已结清账单被误改状态
  262. if ($current >= $deadTime && ($info['status'] ?? 0) == PurchaseClearClass::STATUS_AWAIT_PAY) {
  263. $n = date("Y-m-d H:i:s");
  264. $info['status'] = PurchaseClearClass::STATUS_EXPIRE;
  265. self::updateById($id, [
  266. 'status' => PurchaseClearClass::STATUS_EXPIRE,
  267. 'remark' => "账单过期,已自动取消,参数:{$deadline} {$n}",
  268. ]);
  269. }
  270. $clearId = intval($info['id'] ?? 0);
  271. $cgArr = OrderCgClearClass::getCgIdsByClearId($clearId);
  272. $cgList = empty($cgArr) ? [] : PurchaseClass::getAllByCondition(['id' => ['in', $cgArr]], null, '*', null);
  273. $info['orderList'] = self::buildPartialClearLinksForPurchaseOrders($cgList);
  274. $info['orderNum'] = count($cgArr);
  275. $clearStyle = $info['clearStyle'] ?? dict::getDict('clearStyle', 'hd2Gys');
  276. $operator = $info['customShopAdminName'] ?? '';
  277. if ($clearStyle == dict::getDict('clearStyle', 'gys2Hd')) {
  278. $operator = '商家';
  279. }
  280. $info['operator'] = $operator;
  281. return $info;
  282. }
  283. //完整信息 ssh 20210625
  284. public static function getSaleInfo($info)
  285. {
  286. $clearId = intval($info['id'] ?? 0);
  287. $saleArr = OrderCgClearClass::getOrderIdsByClearId($clearId);
  288. $saleList = empty($saleArr) ? [] : OrderClass::getAllByCondition(['id' => ['in', $saleArr]], null, '*', null);
  289. $info['orderList'] = self::buildPartialClearLinksForSaleOrders($saleList);
  290. $info['orderNum'] = count($saleArr);
  291. $type = $info['clearStyle'] ?? dict::getDict('clearStyle', 'gys2KmGys');
  292. $operator = $info['ghsShopAdminName'] ?? '';
  293. if ($type == dict::getDict('clearStyle', 'hd2Gys')) {
  294. $operator = '客户';
  295. }
  296. $info['operator'] = $operator;
  297. return $info;
  298. }
  299. /**
  300. * 结账单详情(供货商销售单):附加 orderClearList,与订单详情 OrderService 一致。
  301. */
  302. protected static function buildPartialClearLinksForSaleOrders($saleList)
  303. {
  304. if (empty($saleList)) {
  305. return [];
  306. }
  307. $out = [];
  308. foreach ($saleList as $order) {
  309. $row = self::normalizeOrderRow($order);
  310. $orderId = intval($row['id'] ?? 0);
  311. $row['remainDebtPrice'] = bcadd((string)($row['remainDebtPrice'] ?? '0'), '0', 2);
  312. $row['orderClearList'] = $orderId > 0
  313. ? OrderCgClearClass::listPaidClearRecordsForOrder($orderId)
  314. : [];
  315. $out[] = $row;
  316. }
  317. return $out;
  318. }
  319. /**
  320. * 结账单详情(花店采购单):附加 orderClearList;待结以批发销售单为准(采购单可能滞后)。
  321. */
  322. protected static function buildPartialClearLinksForPurchaseOrders($cgList)
  323. {
  324. if (empty($cgList)) {
  325. return [];
  326. }
  327. $out = [];
  328. foreach ($cgList as $purchase) {
  329. $row = self::normalizeOrderRow($purchase);
  330. $cgId = intval($row['id'] ?? 0);
  331. $saleId = intval($row['saleId'] ?? 0);
  332. $row['remainDebtPrice'] = self::resolvePurchaseRemainDebtPrice($row);
  333. $row['orderClearList'] = OrderCgClearClass::listPaidClearRecordsForPurchase($cgId, $saleId);
  334. $out[] = $row;
  335. }
  336. return $out;
  337. }
  338. /** 采购单待结:优先对齐 xhOrder.remainDebtPrice */
  339. protected static function resolvePurchaseRemainDebtPrice(array $row)
  340. {
  341. $remain = bcadd((string)($row['remainDebtPrice'] ?? '0'), '0', 2);
  342. $saleId = intval($row['saleId'] ?? 0);
  343. if ($saleId <= 0) {
  344. return $remain;
  345. }
  346. $sale = OrderClass::getById($saleId, true);
  347. if (empty($sale)) {
  348. return $remain;
  349. }
  350. return bcadd((string)($sale->remainDebtPrice ?? '0'), '0', 2);
  351. }
  352. /** 统一订单行为数组,便于前端附加 orderClearList */
  353. protected static function normalizeOrderRow($row)
  354. {
  355. if (is_array($row)) {
  356. return $row;
  357. }
  358. if (is_object($row) && method_exists($row, 'getAttributes')) {
  359. return $row->getAttributes();
  360. }
  361. return (array)$row;
  362. }
  363. }