OrderItemController.php 11 KB

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