OrderItemController.php 8.3 KB

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