CgRefundService.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. namespace bizHd\cg\services;
  3. use biz\ghs\classes\GhsClass;
  4. use biz\hb\classes\HbClass;
  5. use biz\shop\classes\ShopCapitalClass;
  6. use biz\shop\classes\ShopClass;
  7. use biz\wx\classes\WxMessageClass;
  8. use bizGhs\order\classes\OrderClass;
  9. use bizGhs\order\classes\RefundOrderClass;
  10. use bizGhs\order\services\RefundOrderService;
  11. use bizGhs\product\classes\ProductClass;
  12. use bizGhs\stock\classes\StockRecordClass;
  13. use bizHd\base\services\BaseService;
  14. use bizHd\cg\classes\CgRefundClass;
  15. use bizHd\cg\classes\CgRefundItemClass;
  16. use bizHd\purchase\classes\PurchaseClass;
  17. use bizHd\purchase\classes\PurchaseItemClass;
  18. use bizHd\shop\classes\MainClass;
  19. use bizHd\stat\classes\StatIncomeClass;
  20. use bizHd\wx\classes\WxOpenClass;
  21. use common\components\dict;
  22. use common\components\noticeUtil;
  23. use common\components\orderSn;
  24. use common\components\util;
  25. use Yii;
  26. class CgRefundService extends BaseService
  27. {
  28. public static $baseFile = '\bizHd\cg\classes\CgRefundClass';
  29. public static function cancelRefundApply($refund)
  30. {
  31. CgRefundClass::cancelRefund($refund);
  32. $saleRefundId = $refund->saleRefundId ?? 0;
  33. $saleRefund = RefundOrderClass::getById($saleRefundId, true);
  34. if (empty($saleRefund)) {
  35. util::fail('退款信息缺失');
  36. }
  37. RefundOrderClass::cancelRefund($saleRefund);
  38. }
  39. public static function createOrder($cgData, $cg)
  40. {
  41. $respond = self::addRefund($cgData, $cg);
  42. $saleId = $cg->saleId ?? 0;
  43. $order = OrderClass::getById($saleId, true);
  44. if (empty($order)) {
  45. util::fail('没有订单信息');
  46. }
  47. $remark = $cgData['remark'] ?? '';
  48. $price = $cgData['price'] ?? 0;
  49. $refundType = $cgData['refundType'] ?? 1;
  50. $imgList = $cgData['imgList'] ?? '';
  51. $sjId = $order->sjId ?? 0;
  52. $shopId = $order->shopId ?? 0;
  53. $mainId = $order->mainId ?? 0;
  54. $cgProductList = $cgData['product'] ?? [];
  55. $newProductList = [];
  56. if (!empty($cgProductList)) {
  57. $cgIds = array_column($cgProductList, 'productId');
  58. $cgProductInfo = ProductClass::getAllByCondition(['id' => ['in', $cgIds]], null, 'id,itemId');
  59. if (empty($cgProductInfo)) {
  60. util::fail('花材数据有问题');
  61. }
  62. $cgMap = [];
  63. foreach ($cgProductInfo as $cc) {
  64. $ccId = $cc['id'] ?? 0;
  65. $ccPtItemId = $cc['itemId'] ?? 0;
  66. $cgMap[$ccId] = $ccPtItemId;
  67. }
  68. $ptItemList = array_column($cgProductInfo, 'itemId');
  69. $saleProductInfo = ProductClass::getAllByCondition(['mainId' => $mainId, 'itemId' => ['in', $ptItemList]], null, 'id,itemId');
  70. if (empty($saleProductInfo)) {
  71. util::fail('花材信息有问题');
  72. }
  73. $saleMap = [];
  74. foreach ($saleProductInfo as $ss) {
  75. $ssId = $ss['id'] ?? 0;
  76. $ssPtItemId = $ss['itemId'] ?? 0;
  77. $saleMap[$ssPtItemId] = $ssId;
  78. }
  79. foreach ($cgProductList as $cg) {
  80. if (isset($cg['productId']) == false) {
  81. util::fail('花材有问题呢');
  82. }
  83. $productId = $cg['productId'];
  84. if (isset($cgMap[$productId]) == false) {
  85. util::fail('花材有问题哈');
  86. }
  87. $ptItemId = $cgMap[$productId];
  88. if (isset($saleMap[$ptItemId]) == false) {
  89. util::fail('花材有问题哦');
  90. }
  91. $newProductId = $saleMap[$ptItemId];
  92. $cg['productId'] = $newProductId;
  93. $newProductList[] = $cg;
  94. }
  95. }
  96. $orderData = [
  97. 'remark' => $remark,
  98. 'price' => $price,
  99. 'refundType' => $refundType,
  100. 'imgList' => $imgList,
  101. 'sjId' => $sjId,
  102. 'shopId' => $shopId,
  103. 'mainId' => $mainId,
  104. 'product' => $newProductList,
  105. ];
  106. $cgRefundId = $respond->id ?? 0;
  107. $saleRefund = RefundOrderService::addRefund($orderData, $order);
  108. $saleRefund->cgRefundId = $cgRefundId;
  109. $saleRefund->save();
  110. $saleRefundId = $saleRefund->id ?? 0;
  111. $respond->saleRefundId = $saleRefundId;
  112. $respond->save();
  113. return $respond;
  114. }
  115. //新增退款
  116. public static function addRefund($post, $cg)
  117. {
  118. $mainId = $cg->mainId ?? 0;
  119. $shopId = $cg->shopId ?? 0;
  120. $sjId = $cg->sjId ?? 0;
  121. $cgOrderSn = $cg->orderSn ?? '';
  122. $refundOrderSn = orderSn::getCgRefundSn();
  123. $cause = $post['cause'] ?? 0;
  124. $refundPrice = $post['price'] ?? 0;
  125. if (is_numeric($refundPrice) == false || $refundPrice < 0) {
  126. util::fail('请填写退款金额');
  127. }
  128. $shop = ShopClass::getLockById($shopId);
  129. if (empty($shop)) {
  130. util::fail('没有找到门店47');
  131. }
  132. $main = MainClass::getLockById($mainId);
  133. if (empty($main)) {
  134. util::fail('没有main信息30');
  135. }
  136. $data = [];
  137. $data['orderSn'] = $refundOrderSn;
  138. $data['shopId'] = $shopId;
  139. $data['mainId'] = $mainId;
  140. $data['sjId'] = $sjId;
  141. $data['shopAdminId'] = $cg->shopAdminId;
  142. $data['shopAdminName'] = $cg->shopAdminName;
  143. $data['cgId'] = $cg->id;
  144. $refundType = $post['refundType'] ?? 0;
  145. $data['refundType'] = $refundType;
  146. $data['refundPrice'] = $refundPrice;
  147. $data['remark'] = $post['remark'] ?? '';
  148. $data['imgList'] = $post['imgList'] ?? '';
  149. $data['status'] = CgRefundClass::STATUS_UN_COMPLETE;
  150. $refundSendCost = isset($post['refundSendCost']) && is_numeric($post['refundSendCost']) ? $post['refundSendCost'] : 0;
  151. $refundPackCost = isset($post['refundPackCost']) && is_numeric($post['refundPackCost']) ? $post['refundPackCost'] : 0;
  152. $data['refundSendCost'] = $refundSendCost;
  153. $data['refundPackCost'] = $refundPackCost;
  154. $cg->refundSendCost = bcadd($refundSendCost, $cg->refundSendCost, 2);
  155. $cg->refundPackCost = bcadd($refundPackCost, $cg->refundPackCost, 2);
  156. $cg->refundLog = 1;
  157. $cg->save();
  158. //花材列表结构
  159. // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}] productId 花材id num 退货数 unitType 大小单位0大1小 unitPrice 售价 unitName单位名称
  160. $productList = $post['product'] ?? '';
  161. $bigNum = 0;
  162. $smallNum = 0;
  163. foreach ($productList as $current) {
  164. $unitType = $current['unitType'] ?? dict::getDict('unitType', 'big');
  165. $num = $current['num'] ?? 0;
  166. if ($unitType == dict::getDict('unitType', 'big')) {
  167. $bigNum = bcadd($bigNum, $num);
  168. } else {
  169. $smallNum = bcadd($smallNum, $num);
  170. }
  171. }
  172. $data['bigNum'] = $bigNum;
  173. $data['smallNum'] = $smallNum;
  174. $data['cause'] = $cause;
  175. $cgRefund = CgRefundClass::add($data, true);
  176. if ($refundType == CgRefundClass::REFUND_TYPE_MONEY_GOOD) {
  177. $cgItemInfo = PurchaseItemClass::getAllByCondition(['orderSn' => $cgOrderSn], null, '*', 'productId', true);
  178. $originPrice = 0;
  179. foreach ($productList as $info) {
  180. $productId = $info['productId'] ?? 0;
  181. if (isset($cgItemInfo[$productId]) == false) {
  182. util::fail('没有找到花材哦!');
  183. }
  184. $currentInfo = $cgItemInfo[$productId];
  185. $ptItemId = $currentInfo->itemId;
  186. $name = $currentInfo->name;
  187. $cover = $currentInfo->cover;
  188. $ratio = $currentInfo->ratio;
  189. $cgItemId = $currentInfo->id;
  190. $unitType = $info['unitType'] ?? 0;
  191. $num = $info['num'] ?? 0;
  192. $unitPrice = $info['unitPrice'] ?? 0;
  193. $unitName = $info['unitName'] ?? '';
  194. $currentPrice = bcmul($num,$unitPrice,2);
  195. $originPrice = bcadd($originPrice,$currentPrice,2);
  196. $refundData = [];
  197. $refundData['orderSn'] = $refundOrderSn;
  198. $refundData['itemId'] = $ptItemId;
  199. $refundData['productId'] = $productId;
  200. $refundData['name'] = $name;
  201. $refundData['cover'] = $cover;
  202. $refundData['itemCost'] = $unitPrice;
  203. $refundData['ratio'] = $ratio;
  204. $refundData['mainId'] = $mainId;
  205. $refundData['shopId'] = $shopId;
  206. $refundData['sjId'] = $sjId;
  207. $refundData['xhNum'] = $num;
  208. $refundData['xhUnitName'] = $unitName;
  209. $refundData['xhUnitType'] = $unitType;
  210. $refundData['xhUnitPrice'] = $unitPrice;
  211. $refundData['xhPrice'] = 0;
  212. $refundData['cgItemId'] = $cgItemId;
  213. $refundData['cause'] = $cause;
  214. CgRefundItemClass::addData($refundData, $cg);
  215. }
  216. if($refundPrice<$originPrice){
  217. util::fail('退款金额要等于或大于花材总和');
  218. }
  219. }
  220. return $cgRefund;
  221. }
  222. }