OrderItemController.php 7.7 KB

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