ClearClass.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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\cg\classes\CgClass;
  7. use bizGhs\custom\classes\CustomClass;
  8. use bizGhs\ghs\classes\GhsDebtRecordClass;
  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', 'actPrice', 'realPrice', 'debt', 'ghsId'], 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. $totalPrice = bcadd($totalPrice, $purchase->actPrice, 2);
  87. }
  88. $ghs = GhsClass::getById($currentGhsId, true);
  89. if (empty($ghs)) {
  90. util::fail('没有找到供货商');
  91. }
  92. GhsClass::valid($ghs, $data['shopId']);
  93. $ghsName = $ghs->name ?? '';
  94. $ghsAvatar = $ghs->avatar ?? '';
  95. $ghsMobile = $ghs->mobile ?? '';
  96. $customId = $ghs->customId ?? 0;
  97. $custom = CustomClass::getById($customId, true);
  98. $customName = $custom->name ?? '';
  99. $customAvatar = $custom->avatar ?? '';
  100. $customMobile = $custom->mobile ?? '';
  101. $data['ghsName'] = $ghsName;
  102. $data['ghsAvatar'] = $ghsAvatar;
  103. $data['ghsMobile'] = $ghsMobile;
  104. $data['customName'] = $customName;
  105. $data['customAvatar'] = $customAvatar;
  106. $data['customMobile'] = $customMobile;
  107. $data['prePrice'] = $totalPrice;
  108. $realPrice = $totalPrice;
  109. if (isset($data['modifyPrice']) && !empty($data['modifyPrice'])) {
  110. if (is_numeric($data['modifyPrice']) == false) {
  111. util::fail('实收金额错误');
  112. }
  113. $modifyPrice = $data['modifyPrice'] ?? 0.00;
  114. if ($modifyPrice > $totalPrice) {
  115. util::fail('实收金额不能大于订单金额');
  116. }
  117. if ($modifyPrice < $totalPrice) {
  118. $data['discountAmount'] = bcsub($totalPrice, $modifyPrice, 2);
  119. $data['discountType'] = dict::getDict('discountType', 'discount');
  120. }
  121. $realPrice = $modifyPrice;
  122. }
  123. $data['actPrice'] = $realPrice;
  124. $data['realPrice'] = $realPrice;
  125. $data['orderSn'] = $orderSn;
  126. $data['purchaseIds'] = implode(',', $arr);
  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. $payWay = $data['payWay'] ?? dict::getDict('payWay', 'wxPay');
  132. self::complete($order, $payWay);
  133. return $order;
  134. }
  135. //支付成功后的流程 ssh 2021.4.28
  136. public static function complete($order, $payWay)
  137. {
  138. if (empty($order)) {
  139. util::fail('没有找到订单');
  140. }
  141. if (isset($order->status) == false || $order->status != self::STATUS_AWAIT_PAY) {
  142. util::fail('订单不是待付款状态');
  143. }
  144. $order->status = self::STATUS_HAS_PAY;
  145. $order->save();
  146. $clearId = $order->id;
  147. $purchaseIds = $order->purchaseIds;
  148. $arr = explode(',', $purchaseIds);
  149. if (empty($arr)) {
  150. $msg = "没有找到采购订单";
  151. util::fail($msg);
  152. }
  153. $list = PurchaseOrderClass::getAllByCondition(['id' => ['in', $arr]], null, '*', null, true);
  154. if (empty($list)) {
  155. $msg = "没有找到采购订单!";
  156. util::fail($msg);
  157. }
  158. //供货商欠款变更
  159. $ghsId = $order->ghsId;
  160. $ghs = GhsClass::getLockById($ghsId);
  161. if (empty($ghs)) {
  162. util::fail('没有找到供货商信息');
  163. }
  164. $ghsAddDebtKey = 'ghs_change_debt_' . $ghsId;
  165. $lock = util::lock($ghsAddDebtKey);
  166. if (!$lock) {
  167. util::fail("系统繁忙,请重试");
  168. }
  169. $currentAmount = bcsub($ghs->debtAmount, $order->prePrice, 2);
  170. $ghs->debtAmount = $currentAmount;
  171. $ghs->debtNum -= count($arr);
  172. $ghs->debt = $currentAmount > 0 ? 2 : 1;
  173. $ghs->save();
  174. //减少欠款变动记录
  175. $capitalType = dict::getDict('capitalType', 'ghsCgOrderClear', 'id');
  176. $debtData = [
  177. 'ghsId' => $ghsId,
  178. 'relateId' => $order->id,
  179. 'ptStyle' => 2,
  180. 'capitalType' => $capitalType,
  181. 'amount' => $order->prePrice,
  182. 'staffId' => $order->customShopAdminId,
  183. 'staffName' => $order->customShopAdminName,
  184. 'balance' => $ghs->debtAmount,
  185. 'io' => 0,
  186. 'event' => '采购单结账 ' . $order->orderSn,
  187. 'sjId' => $order->sjId,
  188. 'shopId' => $order->shopId,
  189. ];
  190. GhsDebtRecordClass::add($debtData);
  191. util::unlock($ghsAddDebtKey);
  192. $shopId = $ghs->shopId;
  193. $shop = ShopClass::getLockById($shopId);
  194. if (empty($shop)) {
  195. util::fail('没有找到供货商门店');
  196. }
  197. $mainId = $shop->mainId ?? 0;
  198. $main = MainClass::getLockById($mainId);
  199. if (empty($main)) {
  200. util::fail('没有main信息19');
  201. }
  202. //门店应收客户款减少
  203. $currentGathering = bcsub($main->mayGathering, $order->actPrice, 2);
  204. $main->mayGathering = $currentGathering;
  205. $main->save();
  206. //采购单状态变更
  207. foreach ($list as $purchase) {
  208. $purchase->debt = PurchaseOrderClass::DEBT_NO;
  209. $purchase->payWay = $payWay;
  210. $purchase->clearId = $clearId;
  211. $purchase->save();
  212. }
  213. //客户欠款变化
  214. $customId = $ghs->customId;
  215. $custom = CustomClass::getLockById($customId);
  216. if (empty($custom)) {
  217. util::fail('没有找到客户信息');
  218. }
  219. $remain = bcsub($custom->debtAmount, $order->actPrice, 2);
  220. $custom->debtAmount = $remain;
  221. $custom->debtNum -= count($arr);
  222. $custom->isDebt = $remain > 0 ? 1 : 0;
  223. $custom->save();
  224. if ($custom->debtNum == 0) {
  225. $main->mayGatheringNum -= 1;
  226. $main->save();
  227. }
  228. //应付供货商款减少
  229. $customShopId = $custom->shopId;
  230. $customShop = ShopClass::getLockById($customShopId);
  231. if (empty($customShop)) {
  232. util::fail('没有找到客户门店');
  233. }
  234. $customMainId = $customShop->mainId ?? 0;
  235. $customMain = MainClass::getLockById($customMainId);
  236. if (empty($customMain)) {
  237. util::fail('没有main信息20');
  238. }
  239. $currentMayPay = bcsub($customMain->mayPay, $order->actPrice, 2);
  240. $customMain->mayPay = $currentMayPay;
  241. if ($ghs->debtNum == 0) {
  242. $customMain->mayPayNum -= 1;
  243. }
  244. $customMain->save();
  245. //扣除现金
  246. if ($payWay == dict::getDict('payWay', 'cash')) {
  247. $balance = bcsub($customMain->money, $order->actPrice, 2);
  248. if ($balance < 0) {
  249. util::fail('现金不足');
  250. }
  251. $customMain->money = $balance;
  252. $customMain->save();
  253. $staffId = $order->customShopAdminId ?? 0;
  254. $staffName = $order->customShopAdminName ?? '';
  255. $event = $staffName . '买花结账支出' . $order->actPrice . '元,单号:' . $order->orderSn;
  256. $capitalType = dict::getDict('capitalType', 'ghsClearExpend', 'id');
  257. $data = [];
  258. $data['balance'] = $balance;
  259. $data['mainId'] = $customMainId;
  260. $data['sjId'] = 0;
  261. $data['amount'] = $order->actPrice ?? 0;
  262. $data['staffId'] = $staffId;
  263. $data['staffName'] = $staffName;
  264. $data['ptStyle'] = dict::getDict('ptStyle', 'ghs');
  265. $data['capitalType'] = $capitalType;
  266. $data['event'] = $event;
  267. $data['remark'] = '';
  268. ShopMoneyClass::addData($data);
  269. }
  270. }
  271. //供货商的采购信息 ssh 20210625
  272. public static function getGhsCgInfo($info)
  273. {
  274. $purchaseIds = $info['purchaseIds'] ?? [];
  275. $cgArr = explode(',', $purchaseIds);
  276. $cgList = CgClass::getAllByCondition(['id' => ['in', $cgArr]], null, 'id,orderSn,bigNum,smallNum,realPrice,addTime', null);
  277. $info['orderList'] = $cgList;
  278. $info['orderNum'] = count($cgArr);
  279. $clearStyle = $info['clearStyle'] ?? dict::getDict('clearStyle', 'gys2KmGys');
  280. $operator = $info['customShopAdminName'] ?? '';
  281. if ($clearStyle == dict::getDict('clearStyle', 'kmGys2gys')) {
  282. $operator = '商家';
  283. }
  284. $info['operator'] = $operator;
  285. return $info;
  286. }
  287. //零售的采购信息 ssh 20210625
  288. public static function getCgInfo($info)
  289. {
  290. $id = $info['id'] ?? 0;
  291. $deadline = $info['deadline'] ?? '';
  292. $deadTime = strtotime($deadline);
  293. $current = time();
  294. if ($current >= $deadTime) {
  295. $info['status'] = PurchaseClearClass::STATUS_EXPIRE;
  296. self::updateById($id, ['status' => PurchaseClearClass::STATUS_EXPIRE]);
  297. }
  298. $purchaseIds = $info['purchaseIds'] ?? [];
  299. $cgArr = explode(',', $purchaseIds);
  300. $cgList = PurchaseClass::getAllByCondition(['id' => ['in', $cgArr]], null, '*', null);
  301. $info['orderList'] = $cgList;
  302. $info['orderNum'] = count($cgArr);
  303. $clearStyle = $info['clearStyle'] ?? dict::getDict('clearStyle', 'hd2Gys');
  304. $operator = $info['customShopAdminName'] ?? '';
  305. if ($clearStyle == dict::getDict('clearStyle', 'gys2Hd')) {
  306. $operator = '商家';
  307. }
  308. $info['operator'] = $operator;
  309. return $info;
  310. }
  311. //完整信息 ssh 20210625
  312. public static function getSaleInfo($info)
  313. {
  314. $saleIds = $info['saleIds'] ?? [];
  315. $saleArr = explode(',', $saleIds);
  316. $saleList = OrderClass::getAllByCondition(['id' => ['in', $saleArr]], null, '*', null);
  317. $info['orderList'] = $saleList;
  318. $info['orderNum'] = count($saleArr);
  319. $type = $info['clearStyle'] ?? dict::getDict('clearStyle', 'gys2KmGys');
  320. $operator = $info['ghsShopAdminName'] ?? '';
  321. if ($type == dict::getDict('clearStyle', 'hd2Gys')) {
  322. $operator = '客户';
  323. }
  324. $info['operator'] = $operator;
  325. return $info;
  326. }
  327. }