OrderItemController.php 8.9 KB

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