OrderItemController.php 8.3 KB

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