CgRefundService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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\ghs\classes\GhsDebtRecordClass;
  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. $order->save();
  41. $data = [];
  42. $data['status'] = 1;
  43. $orderSn = orderSn::getGhsCgRefundSn();
  44. $data['refundSn'] = $orderSn;
  45. $data['relateOrderSn'] = $order->orderSn ?? '';
  46. $sjId = $post['sjId'] ?? 0;
  47. $refundType = $post['refundType'] ?? 0;
  48. $shopId = $post['shopId'] ?? 0;
  49. $mainId = $post['mainId'] ?? 0;
  50. $data['sjId'] = $sjId;
  51. $data['shopId'] = $shopId;
  52. $data['mainId'] = $mainId;
  53. $data['shopAdminId'] = $post['shopAdminId'] ?? 0;
  54. $data['shopAdminName'] = $post['shopAdminName'] ?? '';
  55. $productData = $post['product'] ?? '';
  56. if ($refundType == CgRefundClass::REFUND_TYPE_MONEY_GOOD) {
  57. //前端传过来的结构 [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}]
  58. if (empty($productData)) {
  59. util::fail('没有退款花材');
  60. }
  61. $shop = $post['shop'] ?? [];
  62. $originalPrice = 0;
  63. $cgItemList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $order->orderSn], null, '*', 'productId', true);
  64. $productIds = [];
  65. foreach ($cgItemList as $cgItem) {
  66. $productIds[] = $cgItem->productId ?? 0;
  67. }
  68. $productInfoList = ProductClass::getAllByCondition(['id' => ['in', $productIds]], null, '*', 'id', true);
  69. foreach ($productData as $currentProduct) {
  70. $num = $currentProduct['num'] ?? 0;
  71. $productId = $currentProduct['productId'] ?? 0;
  72. if (empty($productId)) {
  73. util::fail('发现无效花材');
  74. }
  75. $currentCgItem = $cgItemList[$productId] ?? null;
  76. $productInfo = $productInfoList[$productId] ?? null;
  77. if (empty($productInfo)) {
  78. util::fail('花材信息没有找到呢。');
  79. }
  80. $hasRefundNum = $currentCgItem->refundNum ?? 0;
  81. $totalNum = $currentCgItem->itemNum ?? 0;
  82. $unitPrice = $currentCgItem->bigPrice ?? 0;
  83. $unitName = $currentCgItem->bigUnit ?? '';
  84. $unitCost = $currentCgItem->cost ?? 0;
  85. //已退数量增加
  86. $currentRefundNum = bcadd($currentCgItem->refundNum, $num);
  87. $currentCgItem->refundNum = $currentRefundNum;
  88. $currentCgItem->save();
  89. $totalPrice = bcmul($unitPrice, $num, 2);
  90. $couldRefundNum = bcsub($totalNum, $hasRefundNum);
  91. if ($couldRefundNum < $num) {
  92. util::fail('超过可退数量');
  93. }
  94. $originalPrice = bcadd($originalPrice, $totalPrice, 2);
  95. $name = $currentCgItem->name ?? '';
  96. $cover = $currentCgItem->cover ?? '';
  97. $ptItemId = $currentCgItem->itemId ?? 0;
  98. $refundItemData = [
  99. 'refundSn' => $orderSn,
  100. 'name' => $name,
  101. 'cover' => $cover,
  102. 'itemId' => $productId,
  103. 'ptItemId' => $ptItemId,
  104. 'num' => $num,
  105. 'unitName' => $unitName,
  106. 'unitType' => 0,
  107. 'unitPrice' => $unitPrice,
  108. 'price' => $totalPrice,
  109. 'unitCost' => $unitCost
  110. ];
  111. CgRefundItemClass::addData($refundItemData, $order);
  112. $sendRefundData = $currentProduct['sendRefund'] ?? [];
  113. if ($shop->bookSn == $bookSn && !empty($bookSn)) {
  114. $assignSeat = $currentCgItem->assignSeat;
  115. if ($assignSeat == 0) {
  116. $params['currentChangeType'] = 'refund';
  117. $params['customName'] = '';
  118. $params['customId'] = '';
  119. $params['customNamePy'] = '';
  120. $currentItemInfo = ProductClass::getById($productId, true);
  121. $bookItemRes = BookItemClass::getByCondition(['bookSn' => $bookSn, 'itemId' => $productId], true);
  122. $bookItemRes = BookItemClass::addNeedCgNum($bookItemRes, $num, 0, $order, $shop, $currentItemInfo, $params);
  123. } else {
  124. if (!empty($sendRefundData)) {
  125. $sendIds = array_column($sendRefundData, 'id');
  126. $sendRes = CgOrderItemSendClass::getAllByCondition(['id' => ['in', $sendIds]], null, '*', 'id', true);
  127. $totalSendRefundNum = 0;
  128. foreach ($sendRefundData as $sendKey => $send) {
  129. $sendRefundNum = $send['refundNum'] ?? 0;
  130. $totalSendRefundNum = bcadd($totalSendRefundNum, $sendRefundNum);
  131. $sendId = $send['id'] ?? 0;
  132. $currentSend = $sendRes[$sendId] ?? null;
  133. if (empty($currentSend)) {
  134. util::fail('没有找到采购单花材的配送信息呢');
  135. }
  136. $totalOnNum = $currentSend->num ?? 0;
  137. $hasOutNum = $currentSend->outNum ?? 0;
  138. $remainNum = bcsub($totalOnNum, $hasOutNum);
  139. $customName = $currentSend->customName ?? '';
  140. if ($sendRefundNum > $remainNum) {
  141. util::fail($customName . '货位不够退了');
  142. }
  143. $currentSend->outNum = bcadd($currentSend->outNum, $sendRefundNum);
  144. $currentSend->save();
  145. $bookGhsGiveId = $currentSend->bookGhsGiveId ?? 0;
  146. $give = BookItemGhsClass::getById($bookGhsGiveId, true);
  147. if (empty($give)) {
  148. util::fail('售后时,货位配送相关信息缺失哦');
  149. }
  150. $give->outNum = bcadd($give->outNum, $sendRefundNum);
  151. $give->save();
  152. $bookItemCustomId = $currentSend->bookItemCustomId ?? 0;
  153. $bookItemCustomRes = BookItemCustomClass::getById($bookItemCustomId, true);
  154. if (empty($bookItemCustomRes)) {
  155. util::fail('售后时,bookItemCustom信息缺失');
  156. }
  157. $onNum = $bookItemCustomRes->onNum ?? 0;
  158. if ($sendRefundNum > $onNum) {
  159. util::fail('售后时,bookItemCustom上架数量不够扣');
  160. }
  161. $staffId = $post['shopAdminId'] ?? 0;
  162. $staffName = $post['shopAdminName'] ?? '';
  163. $params = ['staffId' => $staffId, 'staffName' => $staffName, 'currentChangeType' => 'ghsCgRefund'];
  164. $bookItemCustomRes = BookItemCustomClass::delOnNum($bookItemCustomRes, $sendRefundNum, $num, $order, $shop, $productInfo, $params);
  165. $customId = $bookItemCustomRes->customId ?? 0;
  166. $bookCustomRes = BookCustomClass::getByCondition(['bookSn' => $bookSn, 'customId' => $customId], true);
  167. if (empty($bookCustomRes)) {
  168. util::fail('售后时,bookCustom信息缺失');
  169. }
  170. $onNum2 = $bookCustomRes->onNum ?? 0;
  171. if ($sendRefundNum > $onNum2) {
  172. util::fail('售后时,bookCustom上架数量不够扣');
  173. }
  174. $bookCustomRes = BookCustomClass::delOnNum($bookCustomRes, $sendRefundNum, $num, $order, $shop, $productInfo, $params);
  175. $itemId = $bookItemCustomRes->itemId ?? 0;
  176. $bookItemRes = BookItemClass::getByCondition(['bookSn' => $bookSn, 'itemId' => $itemId], true);
  177. if (empty($bookItemRes)) {
  178. util::fail('售后时,bookItem信息缺失');
  179. }
  180. $onNum3 = $bookItemRes->onNum ?? 0;
  181. if ($sendRefundNum > $onNum3) {
  182. util::fail('售后时,bookItem上架数量不够扣');
  183. }
  184. $bookItemRes = BookItemClass::delOnNum($bookItemRes, $sendRefundNum, $num, $order, $shop, $productInfo, $params);
  185. }
  186. if (floatval($num) != floatval($totalSendRefundNum)) {
  187. util::fail("{$name} 退货数量必须和货位退货数和一致");
  188. }
  189. } else {
  190. $sendList = CgOrderItemSendClass::getAllByCondition(['parentId' => $currentCgItem->id], null, '*', null, true);
  191. if (!empty($sendList)) {
  192. foreach ($sendList as $sendInfo) {
  193. if ($sendInfo->status != 2) {
  194. util::fail('请填写货位要退的数量');
  195. }
  196. }
  197. }
  198. }
  199. }
  200. }
  201. }
  202. if ($refundPrice < $originalPrice) {
  203. util::fail('退款金额需要等于或大于花材总和');
  204. }
  205. }
  206. $data['customId'] = $order->customId ?? 0;
  207. $data['remark'] = $post['remark'] ?? '';
  208. $data['refundPrice'] = $refundPrice;
  209. $data['refundType'] = $post['refundType'] ?? 0;
  210. $data['passTime'] = date("Y-m-d H:i:s");
  211. $data['orderTime'] = $order->entryTime;
  212. $data['ghsId'] = $order->ghsId ?? 0;
  213. $data['ghsName'] = $order->ghsName ?? '';
  214. $refund = CgRefundClass::add($data, true);
  215. //锁定当前增加资产的运作,有多处,请搜索关键词 ghsAddDebtAction
  216. $ghsId = $order->ghsId ?? 0;
  217. $ghs = GhsClass::getLockById($ghsId);
  218. if (empty($ghs)) {
  219. util::fail('没有找到供货商');
  220. }
  221. $ghs->debtAmount = bcsub($ghs->debtAmount, $refundPrice, 2);
  222. $ghs->save();
  223. //减少欠款变动记录
  224. $capitalType = dict::getDict('capitalType', 'ghsCgOrderRefund', 'id');
  225. $debtData = [
  226. 'ghsId' => $ghsId,
  227. 'relateId' => $refund->id,
  228. 'ptStyle' => 2,
  229. 'capitalType' => $capitalType,
  230. 'amount' => $refundPrice,
  231. 'balance' => $ghs->debtAmount,
  232. 'io' => 0,
  233. 'staffId' => $refund->shopAdminId,
  234. 'staffName' => $refund->shopAdminName,
  235. 'event' => '采购单 ' . $order->orderSn . ' 申请售后 ' . $orderSn,
  236. 'sjId' => $sjId,
  237. 'shopId' => $shopId,
  238. ];
  239. GhsDebtRecordClass::add($debtData);
  240. return $refund;
  241. }
  242. }