OrderItemController.php 6.9 KB

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