CgRefundService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. namespace bizGhs\cg\services;
  3. use bizGhs\base\services\BaseService;
  4. use bizGhs\book\classes\BookCustomClass;
  5. use bizGhs\book\classes\BookItemClass;
  6. use bizGhs\book\classes\BookItemCustomClass;
  7. use bizGhs\book\classes\BookItemGhsClass;
  8. use bizGhs\cg\classes\CgOrderItemSendClass;
  9. use bizGhs\cg\classes\CgRefundClass;
  10. use bizGhs\cg\classes\CgRefundItemClass;
  11. use bizGhs\ghs\classes\GhsClass;
  12. use bizGhs\order\classes\PurchaseOrderClass;
  13. use bizGhs\order\classes\PurchaseOrderItemClass;
  14. use bizGhs\product\classes\ProductClass;
  15. use common\components\dict;
  16. use common\components\orderSn;
  17. use common\components\util;
  18. use Yii;
  19. class CgRefundService extends BaseService
  20. {
  21. public static $baseFile = '\bizGhs\cg\classes\CgRefundClass';
  22. //退款
  23. public static function addRefund($order, $post)
  24. {
  25. $refundPrice = $post['price'] ?? 0;
  26. if ($refundPrice <= 0) {
  27. util::fail('请填写退款金额');
  28. }
  29. $order->refundPrice = $refundPrice;
  30. $currentTkPrice = bcadd($order->tkPrice, $refundPrice, 2);
  31. $order->tkPrice = $currentTkPrice;
  32. $actPrice = $order->actPrice ?? 0;
  33. $realPrice = $order->realPrice ?? 0;
  34. $bookSn = $order->bookSn ?? 0;
  35. $currentActPrice = bcsub($actPrice, $refundPrice, 2);
  36. $currentRealPrice = bcsub($realPrice, $refundPrice, 2);
  37. $order->actPrice = $currentActPrice;
  38. $order->realPrice = $currentRealPrice;
  39. $order->refund = 2;
  40. // 待结采购单售后退尽:actPrice 已为 0 须同步改为已结清,否则会留下 debt=待结 的幽灵单
  41. if (intval($order->debt ?? 0) === PurchaseOrderClass::DEBT_YES
  42. && bccomp($currentActPrice, '0', 2) <= 0) {
  43. $order->debt = PurchaseOrderClass::DEBT_NO;
  44. }
  45. $order->save();
  46. $data = [];
  47. $data['status'] = 1;
  48. $orderSn = orderSn::getGhsCgRefundSn();
  49. $data['refundSn'] = $orderSn;
  50. $data['relateOrderSn'] = $order->orderSn ?? '';
  51. $sjId = $post['sjId'] ?? 0;
  52. $refundType = $post['refundType'] ?? 0;
  53. $shopId = $post['shopId'] ?? 0;
  54. $mainId = $post['mainId'] ?? 0;
  55. $data['sjId'] = $sjId;
  56. $data['shopId'] = $shopId;
  57. $data['mainId'] = $mainId;
  58. $data['shopAdminId'] = $post['shopAdminId'] ?? 0;
  59. $data['shopAdminName'] = $post['shopAdminName'] ?? '';
  60. $productData = $post['product'] ?? '';
  61. if ($refundType == CgRefundClass::REFUND_TYPE_MONEY_GOOD) {
  62. //前端传过来的结构 [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}]
  63. if (empty($productData)) {
  64. util::fail('没有退款花材');
  65. }
  66. $shop = $post['shop'] ?? [];
  67. $originalPrice = 0;
  68. $cgItemList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $order->orderSn], null, '*', 'productId', true);
  69. $productIds = [];
  70. foreach ($cgItemList as $cgItem) {
  71. $productIds[] = $cgItem->productId ?? 0;
  72. }
  73. $productInfoList = ProductClass::getAllByCondition(['id' => ['in', $productIds]], null, '*', 'id', true);
  74. foreach ($productData as $currentProduct) {
  75. $num = $currentProduct['num'] ?? 0;
  76. $productId = $currentProduct['productId'] ?? 0;
  77. if (empty($productId)) {
  78. util::fail('发现无效花材');
  79. }
  80. $currentCgItem = $cgItemList[$productId] ?? null;
  81. $productInfo = $productInfoList[$productId] ?? null;
  82. if (empty($productInfo)) {
  83. util::fail('花材信息没有找到呢。');
  84. }
  85. $hasRefundNum = $currentCgItem->refundNum ?? 0;
  86. $totalNum = $currentCgItem->itemNum ?? 0;
  87. $unitPrice = $currentCgItem->bigPrice ?? 0;
  88. $unitName = $currentCgItem->bigUnit ?? '';
  89. $unitCost = $currentCgItem->cost ?? 0;
  90. //已退数量增加
  91. $currentRefundNum = bcadd($currentCgItem->refundNum, $num);
  92. $currentCgItem->refundNum = $currentRefundNum;
  93. $currentCgItem->save();
  94. $totalPrice = bcmul($unitPrice, $num, 2);
  95. $couldRefundNum = bcsub($totalNum, $hasRefundNum);
  96. if ($couldRefundNum < $num) {
  97. util::fail('超过可退数量');
  98. }
  99. $originalPrice = bcadd($originalPrice, $totalPrice, 2);
  100. $name = $currentCgItem->name ?? '';
  101. $cover = $currentCgItem->cover ?? '';
  102. $ptItemId = $currentCgItem->itemId ?? 0;
  103. $refundItemData = [
  104. 'refundSn' => $orderSn,
  105. 'name' => $name,
  106. 'cover' => $cover,
  107. 'itemId' => $productId,
  108. 'ptItemId' => $ptItemId,
  109. 'num' => $num,
  110. 'unitName' => $unitName,
  111. 'unitType' => 0,
  112. 'unitPrice' => $unitPrice,
  113. 'price' => $totalPrice,
  114. 'unitCost' => $unitCost
  115. ];
  116. CgRefundItemClass::addData($refundItemData, $order);
  117. $sendRefundData = $currentProduct['sendRefund'] ?? [];
  118. if ($shop->bookSn == $bookSn && !empty($bookSn)) {
  119. $assignSeat = $currentCgItem->assignSeat;
  120. if ($assignSeat == 0) {
  121. $params['currentChangeType'] = 'refund';
  122. $params['customName'] = '';
  123. $params['customId'] = '';
  124. $params['customNamePy'] = '';
  125. $currentItemInfo = ProductClass::getById($productId, true);
  126. $bookItemRes = BookItemClass::getByCondition(['bookSn' => $bookSn, 'itemId' => $productId], true);
  127. $bookItemRes = BookItemClass::addNeedCgNum($bookItemRes, $num, 0, $order, $shop, $currentItemInfo, $params);
  128. } else {
  129. if (!empty($sendRefundData)) {
  130. $sendIds = array_column($sendRefundData, 'id');
  131. $sendRes = CgOrderItemSendClass::getAllByCondition(['id' => ['in', $sendIds]], null, '*', 'id', true);
  132. $totalSendRefundNum = 0;
  133. foreach ($sendRefundData as $sendKey => $send) {
  134. $sendRefundNum = $send['refundNum'] ?? 0;
  135. $totalSendRefundNum = bcadd($totalSendRefundNum, $sendRefundNum);
  136. $sendId = $send['id'] ?? 0;
  137. $currentSend = $sendRes[$sendId] ?? null;
  138. if (empty($currentSend)) {
  139. util::fail('没有找到采购单花材的配送信息呢');
  140. }
  141. $totalOnNum = $currentSend->num ?? 0;
  142. $hasOutNum = $currentSend->outNum ?? 0;
  143. $remainNum = bcsub($totalOnNum, $hasOutNum);
  144. $customName = $currentSend->customName ?? '';
  145. if ($sendRefundNum > $remainNum) {
  146. util::fail($customName . '货位不够退了');
  147. }
  148. $currentSend->outNum = bcadd($currentSend->outNum, $sendRefundNum);
  149. $currentSend->save();
  150. $bookGhsGiveId = $currentSend->bookGhsGiveId ?? 0;
  151. $give = BookItemGhsClass::getById($bookGhsGiveId, true);
  152. if (empty($give)) {
  153. util::fail('售后时,货位配送相关信息缺失哦');
  154. }
  155. $give->outNum = bcadd($give->outNum, $sendRefundNum);
  156. $give->save();
  157. $bookItemCustomId = $currentSend->bookItemCustomId ?? 0;
  158. $bookItemCustomRes = BookItemCustomClass::getById($bookItemCustomId, true);
  159. if (empty($bookItemCustomRes)) {
  160. util::fail('售后时,bookItemCustom信息缺失');
  161. }
  162. $onNum = $bookItemCustomRes->onNum ?? 0;
  163. if ($sendRefundNum > $onNum) {
  164. util::fail('售后时,bookItemCustom上架数量不够扣');
  165. }
  166. $staffId = $post['shopAdminId'] ?? 0;
  167. $staffName = $post['shopAdminName'] ?? '';
  168. $params = ['staffId' => $staffId, 'staffName' => $staffName, 'currentChangeType' => 'ghsCgRefund'];
  169. $bookItemCustomRes = BookItemCustomClass::delOnNum($bookItemCustomRes, $sendRefundNum, $num, $order, $shop, $productInfo, $params);
  170. $customId = $bookItemCustomRes->customId ?? 0;
  171. $bookCustomRes = BookCustomClass::getByCondition(['bookSn' => $bookSn, 'customId' => $customId], true);
  172. if (empty($bookCustomRes)) {
  173. util::fail('售后时,bookCustom信息缺失');
  174. }
  175. $onNum2 = $bookCustomRes->onNum ?? 0;
  176. if ($sendRefundNum > $onNum2) {
  177. util::fail('售后时,bookCustom上架数量不够扣');
  178. }
  179. $bookCustomRes = BookCustomClass::delOnNum($bookCustomRes, $sendRefundNum, $num, $order, $shop, $productInfo, $params);
  180. $itemId = $bookItemCustomRes->itemId ?? 0;
  181. $bookItemRes = BookItemClass::getByCondition(['bookSn' => $bookSn, 'itemId' => $itemId], true);
  182. if (empty($bookItemRes)) {
  183. util::fail('售后时,bookItem信息缺失');
  184. }
  185. $onNum3 = $bookItemRes->onNum ?? 0;
  186. if ($sendRefundNum > $onNum3) {
  187. util::fail('售后时,bookItem上架数量不够扣');
  188. }
  189. $bookItemRes = BookItemClass::delOnNum($bookItemRes, $sendRefundNum, $num, $order, $shop, $productInfo, $params);
  190. }
  191. if (floatval($num) != floatval($totalSendRefundNum)) {
  192. util::fail("{$name} 退货数量必须和货位退货数和一致");
  193. }
  194. } else {
  195. $sendList = CgOrderItemSendClass::getAllByCondition(['parentId' => $currentCgItem->id], null, '*', null, true);
  196. if (!empty($sendList)) {
  197. foreach ($sendList as $sendInfo) {
  198. if ($sendInfo->status != 2) {
  199. util::fail('请填写货位要退的数量');
  200. }
  201. }
  202. }
  203. }
  204. }
  205. }
  206. }
  207. if ($refundPrice < $originalPrice) {
  208. util::fail('退款金额需要等于或大于花材总和');
  209. }
  210. }
  211. $data['customId'] = $order->customId ?? 0;
  212. $data['remark'] = $post['remark'] ?? '';
  213. $data['refundPrice'] = $refundPrice;
  214. $data['refundType'] = $post['refundType'] ?? 0;
  215. $data['passTime'] = date("Y-m-d H:i:s");
  216. $data['orderTime'] = $order->entryTime;
  217. $data['ghsId'] = $order->ghsId ?? 0;
  218. $data['ghsName'] = $order->ghsName ?? '';
  219. $refund = CgRefundClass::add($data, true);
  220. //锁定当前增加资产的运作,有多处,请搜索关键词 ghsAddDebtAction
  221. $ghsId = $order->ghsId ?? 0;
  222. $ghs = GhsClass::getLockById($ghsId);
  223. if (empty($ghs)) {
  224. util::fail('没有找到供货商');
  225. }
  226. PurchaseOrderClass::applyPurchaseRefundOnAccounts($ghs, $refundPrice, $refund, $order, $sjId, $shopId);
  227. return $refund;
  228. }
  229. }