OrderItemController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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->xhUnitPrice = $xhUnitPrice;
  103. $info->xhPreUnitPrice = $xhUnitPrice;
  104. $info->save();
  105. }
  106. $order->bookSavePrice = 1;
  107. $order->save();
  108. util::complete('保存成功');
  109. }
  110. //删除花材 ssh 20240123
  111. public function actionDelItem()
  112. {
  113. $post = Yii::$app->request->post();
  114. $id = $post['id'] ?? 0;
  115. $smallId = $post['smallId'] ?? 0;
  116. $order = OrderClass::getById($id, true);
  117. if (empty($order)) {
  118. util::fail('没有订单');
  119. }
  120. if ($order->status != 2) {
  121. util::fail('待发货才能添加');
  122. }
  123. if ($order->book == 0) {
  124. util::fail('不是预订单');
  125. }
  126. if ($order->mainId != $this->mainId) {
  127. util::fail('不是你的订单');
  128. }
  129. if (floatval($order->actPrice) != 0) {
  130. util::fail('订单金额不是0');
  131. }
  132. $connection = Yii::$app->db;//事务处理
  133. $transaction = $connection->beginTransaction();
  134. try {
  135. OrderItemClass::bookDelItem($order, $smallId);
  136. $transaction->commit();
  137. util::complete('删除成功');
  138. } catch (\Exception $e) {
  139. $transaction->rollBack();
  140. Yii::info("删除出错了:" . $e->getMessage());
  141. util::fail('删除出错了');
  142. }
  143. }
  144. //添加花材
  145. public function actionAddItem()
  146. {
  147. $post = Yii::$app->request->post();
  148. $id = $post['id'] ?? 0;
  149. $data = $post['data'] ?? 0;
  150. $arr = json_decode($data, true);
  151. $order = OrderClass::getById($id, true);
  152. if (empty($order)) {
  153. util::fail('没有订单');
  154. }
  155. if ($order->status != 2) {
  156. util::fail('待发货才能添加');
  157. }
  158. if ($order->book == 0) {
  159. util::fail('不是预订单');
  160. }
  161. if (floatval($order->actPrice) != 0) {
  162. util::fail('订单金额不是0');
  163. }
  164. if (empty($arr)) {
  165. util::fail('请选花材');
  166. }
  167. $new = [];
  168. foreach ($arr as $val) {
  169. $id = $val['id'] ?? 0;
  170. $num = $val['bigCount'] ?? 0;
  171. $name = $val['name'] ?? '';
  172. if ($num <= 0) {
  173. util::fail($name . ' 数量要大于0');
  174. }
  175. $new[$id] = ['num' => $num, 'productId' => $id, 'price' => 0];
  176. }
  177. $connection = Yii::$app->db;//事务处理
  178. $transaction = $connection->beginTransaction();
  179. try {
  180. OrderItemClass::bookAddItem($order, $new);
  181. $transaction->commit();
  182. util::complete('添加成功');
  183. } catch (\Exception $e) {
  184. $transaction->rollBack();
  185. Yii::info("添加出错了:" . $e->getMessage());
  186. util::fail('添加出错了');
  187. }
  188. }
  189. //赠送花材 ssh 20230411
  190. public function actionGiveItem()
  191. {
  192. $post = Yii::$app->request->post();
  193. $id = $post['id'] ?? 0;
  194. $num = $post['num'] ?? 0;
  195. if (is_numeric($num) == false || $num <= 0) {
  196. util::fail('请填写赠送数量');
  197. }
  198. $orderItem = OrderItemClass::getById($id, true);
  199. if (empty($orderItem)) {
  200. util::fail('没有找到花材');
  201. }
  202. if ($orderItem->mainId != $this->mainId) {
  203. util::fail('无法操作');
  204. }
  205. if (!in_array($this->mainId, [1496])) {
  206. util::fail('开发中');
  207. }
  208. $connection = Yii::$app->db;//事务处理
  209. $transaction = $connection->beginTransaction();
  210. try {
  211. $staff = $this->shopAdmin;
  212. $staffName = $staff->name ?? '';
  213. $staffId = $staff->id ?? 0;
  214. $data = ['shopId' => $this->shopId, 'sjId' => $this->sjId, 'staffName' => $staffName, 'staffId' => $staffId];
  215. $respond = OrderItemService::giveProduct($data, $orderItem, $num);
  216. $transaction->commit();
  217. util::success(['info' => $respond], '赠送成功');
  218. } catch (\Exception $e) {
  219. $transaction->rollBack();
  220. Yii::info("赠送出错了:" . $e->getMessage());
  221. util::fail('出错了');
  222. }
  223. }
  224. //重选花材 ssh 2021.3.2
  225. public function actionReset()
  226. {
  227. $post = Yii::$app->request->post();
  228. $id = $post['id'] ?? 0;
  229. $product = $post['product'] ?? '[]';
  230. $product = json_decode($product, true);
  231. //检查花材是否都是同家门店的
  232. ProductClass::valid($product, $this->mainId);
  233. $info = OrderService::getOrderInfo($id);
  234. OrderClass::valid($info, $this->shopId);
  235. //添加修改花材
  236. $orderSn = $info['orderSn'] ?? '';
  237. OrderItemClass::replaceItem($orderSn, $product);
  238. util::complete();
  239. }
  240. //修改花材时获取订单花材和花材基本信息的组合 ssh 20210809
  241. public function actionGetOrderProduct()
  242. {
  243. $id = Yii::$app->request->get('id', 0);
  244. $order = OrderClass::getById($id, true);
  245. OrderClass::valid($order, $this->shopId);
  246. $orderSn = $order->orderSn ?? '';
  247. $itemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', 'productId');
  248. $ids = array_column($itemList, 'productId');
  249. if (empty($ids)) {
  250. util::fail('没有找到花材');
  251. }
  252. $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id');
  253. if (empty($productList)) {
  254. util::fail('没有找到花材信息');
  255. }
  256. foreach ($productList as $key => $product) {
  257. $productId = $product['id'] ?? 0;
  258. $currentItem = $itemList[$productId] ?? [];
  259. $bigCount = $currentItem['bigNum'] ?? 0;
  260. $smallCount = $currentItem['smallNum'] ?? 0;
  261. $userPrice = $currentItem['userPrice'] ?? 0;
  262. $productList[$key]['bigCount'] = $bigCount;
  263. $productList[$key]['smallCount'] = $smallCount;
  264. $productList[$key]['userPrice'] = $userPrice;
  265. $cover = imgUtil::groupImg($product['cover']);
  266. $productList[$key]['cover'] = $cover;
  267. $bigPrice = $product['unitPrice'] ?? 0;
  268. $smallPrice = $product['smallUnitPrice'] ?? 0;
  269. $productList[$key]['bigPrice'] = $bigPrice;
  270. $productList[$key]['smallPrice'] = $smallPrice;
  271. $productList[$key]['cover'] = $cover;
  272. }
  273. util::success(['productList' => array_values($productList)]);
  274. }
  275. //根据花材id取订单列表 ssh 20211011
  276. public function actionGetOrderInfoList()
  277. {
  278. $get = Yii::$app->request->get();
  279. $productId = $get['productId'] ?? 0;
  280. $product = ProductClass::getById($productId, true);
  281. ProductClass::check($product, $this->mainId);
  282. $where = ['productId' => $productId];
  283. $respond = OrderItemClass::getOrderInfoList($where);
  284. util::success($respond);
  285. }
  286. }