OrderItemController.php 10 KB

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