OrderItemController.php 9.0 KB

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