OrderItemController.php 7.7 KB

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