OrderItemController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\order\classes\OrderClass;
  4. use bizGhs\order\classes\OrderItemClass;
  5. use bizGhs\order\services\OrderItemService;
  6. use bizGhs\order\services\OrderService;
  7. use bizGhs\product\classes\ProductClass;
  8. use common\components\imgUtil;
  9. use common\components\util;
  10. use Yii;
  11. class OrderItemController extends BaseController
  12. {
  13. public $guestAccess = [];
  14. //删除花材 ssh 20240123
  15. public function actionDelItem()
  16. {
  17. $post = Yii::$app->request->post();
  18. $id = $post['id'] ?? 0;
  19. $smallId = $post['smallId'] ?? 0;
  20. $order = OrderClass::getById($id, true);
  21. if (empty($order)) {
  22. util::fail('没有订单');
  23. }
  24. if ($order->status != 2) {
  25. util::fail('待发货才能添加');
  26. }
  27. if ($order->book == 0) {
  28. util::fail('不是预订单');
  29. }
  30. if (floatval($order->actPrice) != 0) {
  31. util::fail('订单金额不是0');
  32. }
  33. OrderItemClass::bookDelItem($order, $smallId);
  34. util::complete('删除成功');
  35. }
  36. //添加花材
  37. public function actionAddItem()
  38. {
  39. $post = Yii::$app->request->post();
  40. $id = $post['id'] ?? 0;
  41. $data = $post['data'] ?? 0;
  42. $arr = json_decode($data, true);
  43. $order = OrderClass::getById($id, true);
  44. if (empty($order)) {
  45. util::fail('没有订单');
  46. }
  47. if ($order->status != 2) {
  48. util::fail('待发货才能添加');
  49. }
  50. if ($order->book == 0) {
  51. util::fail('不是预订单');
  52. }
  53. if (floatval($order->actPrice) != 0) {
  54. util::fail('订单金额不是0');
  55. }
  56. if (empty($arr)) {
  57. util::fail('请选花材');
  58. }
  59. $new = [];
  60. foreach ($arr as $val) {
  61. $id = $val['id'] ?? 0;
  62. $num = $val['bigCount'] ?? 0;
  63. $name = $val['name'] ?? '';
  64. if ($num <= 0) {
  65. util::fail($name . ' 数量要大于0');
  66. }
  67. $new[$id] = ['num' => $num, 'productId' => $id];
  68. }
  69. OrderItemClass::bookAddItem($order, $new);
  70. util::complete('添加成功');
  71. }
  72. //赠送花材 ssh 20230411
  73. public function actionGiveItem()
  74. {
  75. $post = Yii::$app->request->post();
  76. $id = $post['id'] ?? 0;
  77. $num = $post['num'] ?? 0;
  78. if (is_numeric($num) == false || $num <= 0) {
  79. util::fail('请填写赠送数量');
  80. }
  81. $orderItem = OrderItemClass::getById($id, true);
  82. if (empty($orderItem)) {
  83. util::fail('没有找到花材');
  84. }
  85. if ($orderItem->mainId != $this->mainId) {
  86. util::fail('无法操作');
  87. }
  88. util::fail('开发中');
  89. $connection = Yii::$app->db;//事务处理
  90. $transaction = $connection->beginTransaction();
  91. try {
  92. $staff = $this->shopAdmin;
  93. $staffName = $staff->name ?? '';
  94. $staffId = $staff->id ?? 0;
  95. $data = ['shopId' => $this->shopId, 'sjId' => $this->sjId, 'staffName' => $staffName, 'staffId' => $staffId];
  96. $respond = OrderItemService::giveProduct($data, $orderItem, $num);
  97. $transaction->commit();
  98. util::success(['info' => $respond], '赠送成功');
  99. } catch (\Exception $e) {
  100. $transaction->rollBack();
  101. Yii::info("赠送出错了:" . $e->getMessage());
  102. util::fail('出错了');
  103. }
  104. }
  105. //重选花材 ssh 2021.3.2
  106. public function actionReset()
  107. {
  108. $post = Yii::$app->request->post();
  109. $id = $post['id'] ?? 0;
  110. $product = $post['product'] ?? '[]';
  111. $product = json_decode($product, true);
  112. //检查花材是否都是同家门店的
  113. ProductClass::valid($product, $this->mainId);
  114. $info = OrderService::getOrderInfo($id);
  115. OrderClass::valid($info, $this->shopId);
  116. //添加修改花材
  117. $orderSn = $info['orderSn'] ?? '';
  118. OrderItemClass::replaceItem($orderSn, $product);
  119. util::complete();
  120. }
  121. //修改花材时获取订单花材和花材基本信息的组合 ssh 20210809
  122. public function actionGetOrderProduct()
  123. {
  124. $id = Yii::$app->request->get('id', 0);
  125. $order = OrderClass::getById($id, true);
  126. OrderClass::valid($order, $this->shopId);
  127. $orderSn = $order->orderSn ?? '';
  128. $itemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', 'productId');
  129. $ids = array_column($itemList, 'productId');
  130. if (empty($ids)) {
  131. util::fail('没有找到花材');
  132. }
  133. $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id');
  134. if (empty($productList)) {
  135. util::fail('没有找到花材信息');
  136. }
  137. foreach ($productList as $key => $product) {
  138. $productId = $product['id'] ?? 0;
  139. $currentItem = $itemList[$productId] ?? [];
  140. $bigCount = $currentItem['bigNum'] ?? 0;
  141. $smallCount = $currentItem['smallNum'] ?? 0;
  142. $userPrice = $currentItem['userPrice'] ?? 0;
  143. $productList[$key]['bigCount'] = $bigCount;
  144. $productList[$key]['smallCount'] = $smallCount;
  145. $productList[$key]['userPrice'] = $userPrice;
  146. $cover = imgUtil::groupImg($product['cover']);
  147. $productList[$key]['cover'] = $cover;
  148. $bigPrice = $product['unitPrice'] ?? 0;
  149. $smallPrice = $product['smallUnitPrice'] ?? 0;
  150. $productList[$key]['bigPrice'] = $bigPrice;
  151. $productList[$key]['smallPrice'] = $smallPrice;
  152. $productList[$key]['cover'] = $cover;
  153. }
  154. util::success(['productList' => array_values($productList)]);
  155. }
  156. //根据花材id取订单列表 ssh 20211011
  157. public function actionGetOrderInfoList()
  158. {
  159. $get = Yii::$app->request->get();
  160. $productId = $get['productId'] ?? 0;
  161. $product = ProductClass::getById($productId, true);
  162. ProductClass::check($product, $this->mainId);
  163. $where = ['productId' => $productId];
  164. $respond = OrderItemClass::getOrderInfoList($where);
  165. util::success($respond);
  166. }
  167. }