OrderItemController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\book\classes\BookItemClass;
  4. use bizGhs\order\classes\OrderClass;
  5. use bizGhs\order\classes\OrderItemClass;
  6. use bizGhs\order\services\OrderItemService;
  7. use bizGhs\order\services\OrderService;
  8. use bizGhs\product\classes\ProductClass;
  9. use common\components\imgUtil;
  10. use common\components\util;
  11. use Yii;
  12. class OrderItemController extends BaseController
  13. {
  14. public $guestAccess = [];
  15. //过滤名称 ssh 20240429
  16. public function actionFilterName()
  17. {
  18. $get = Yii::$app->request->get();
  19. $id = $get['id'] ?? 0;
  20. $info = OrderItemClass::getById($id, true);
  21. $name = $info->name ?? '';
  22. $num = $info->xhNum ?? 0;
  23. $name = strtolower($name);
  24. $name = preg_replace('/\d/', '', $name);
  25. $arr = ['cm', '箱', '个', 'a', 'b', 'c', 'd', 'e', 'o', '梦金鹏', '拍市', '七彩', '市场', '(', ')', '(', ')', '-', '级', '红', '黄', '绿', '橙', '蓝', '基地', '支', '枝', '*', '张良', '配色', '混色', '浅粉', '粉', '玫红', '青铜', '深紫', '浅紫', '紫', '色', '重瓣', ' ', ' '];
  26. foreach ($arr as $it) {
  27. $name = str_replace($it, '', $name);
  28. }
  29. util::success(['name' => $name, 'num' => $num]);
  30. }
  31. //修改预订单花材的数量和价格
  32. public function actionModifyBookItem()
  33. {
  34. $post = Yii::$app->request->post();
  35. $id = $post['id'] ?? 0;
  36. $data = $post['data'] ?? [];
  37. $packCost = $post['packCost'] ?? 0;
  38. $sendCost = $post['sendCost'] ?? 0;
  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. $connection = Yii::$app->db;//事务处理
  56. $transaction = $connection->beginTransaction();
  57. try {
  58. $addData = [];
  59. $delData = [];
  60. foreach ($data as $item) {
  61. $id = $item['id'] ?? 0;
  62. $xhNum = $item['xhNum'] ?? 0;
  63. $xhUnitPrice = $item['xhUnitPrice'] ?? 0;
  64. $info = OrderItemClass::getById($id, true);
  65. if (empty($info)) {
  66. continue;
  67. }
  68. if ($info->orderSn != $order->orderSn) {
  69. continue;
  70. }
  71. $productId = $info->productId ?? 0;
  72. $preNum = $info->xhNum ?? 0;
  73. //新增预订花材
  74. if ($xhNum > $preNum) {
  75. $diffNum = bcsub($xhNum, $preNum);
  76. $addData[] = [
  77. 'productId' => $productId,
  78. 'num' => $diffNum,
  79. ];
  80. }
  81. //删除预订花材
  82. if ($preNum > $xhNum) {
  83. $diffNum = bcsub($preNum, $xhNum);
  84. $delData[] = [
  85. 'productId' => $productId,
  86. 'num' => $diffNum,
  87. ];
  88. }
  89. $info->xhNum = $xhNum;
  90. $info->xhPreNum = $xhNum;
  91. $info->xhUnitPrice = $xhUnitPrice;
  92. $info->xhPreUnitPrice = $xhUnitPrice;
  93. $info->save();
  94. }
  95. $order->packCost = $packCost;
  96. $order->sendCost = $sendCost;
  97. $order->save();
  98. if (!empty($addData)) {
  99. BookItemClass::addBatchItem($addData, $order);
  100. }
  101. if (!empty($delData)) {
  102. BookItemClass::delBathItem($delData, $order);
  103. }
  104. $transaction->commit();
  105. util::complete('保存成功');
  106. } catch (\Exception $e) {
  107. $transaction->rollBack();
  108. Yii::info("保存出错了:" . $e->getMessage());
  109. util::fail('保存出错了');
  110. }
  111. }
  112. //更换花材 ssh 20240430
  113. public function actionChangeItem()
  114. {
  115. $post = Yii::$app->request->post();
  116. $delId = $post['delId'] ?? 0;
  117. $orderSn = $post['orderSn'] ?? '';
  118. $num = $post['num'] ?? 0;
  119. $price = $post['price'] ?? 0;
  120. $productId = $post['productId'] ?? 0;
  121. $order = OrderClass::getByCondition(['orderSn' => $orderSn], true);
  122. if (empty($order)) {
  123. util::fail('订单没有找到哦');
  124. }
  125. if ($order->mainId != $this->mainId) {
  126. util::fail('不是你的订单');
  127. }
  128. $delInfo = OrderItemClass::getById($delId, true);
  129. if (empty($delInfo)) {
  130. util::fail('没有找到要删除的花材');
  131. }
  132. if ($delInfo->orderSn != $orderSn) {
  133. util::fail('这个花材你不能删除');
  134. }
  135. $connection = Yii::$app->db;//事务处理
  136. $transaction = $connection->beginTransaction();
  137. try {
  138. OrderItemClass::delBookItem($order, $delId);
  139. $new = [];
  140. $new[$productId] = ['num' => $num, 'productId' => $productId, 'price' => $price];
  141. OrderItemClass::addBookItemFn($order, $new);
  142. $transaction->commit();
  143. util::complete('更换成功');
  144. } catch (\Exception $e) {
  145. $transaction->rollBack();
  146. Yii::info("出错了:" . $e->getMessage());
  147. util::fail('出错了');
  148. }
  149. }
  150. //删除花材 ssh 20240123
  151. public function actionDelItem()
  152. {
  153. $post = Yii::$app->request->post();
  154. $id = $post['id'] ?? 0;
  155. $smallId = $post['smallId'] ?? 0;
  156. $order = OrderClass::getById($id, true);
  157. if (empty($order)) {
  158. util::fail('没有订单');
  159. }
  160. if ($order->status != 2) {
  161. util::fail('待发货才能添加');
  162. }
  163. if ($order->book == 0) {
  164. util::fail('不是预订单');
  165. }
  166. if ($order->mainId != $this->mainId) {
  167. util::fail('不是你的订单');
  168. }
  169. if (floatval($order->actPrice) != 0) {
  170. util::fail('订单金额不是0');
  171. }
  172. $connection = Yii::$app->db;//事务处理
  173. $transaction = $connection->beginTransaction();
  174. try {
  175. OrderItemClass::delBookItem($order, $smallId);
  176. $transaction->commit();
  177. util::complete('删除成功');
  178. } catch (\Exception $e) {
  179. $transaction->rollBack();
  180. Yii::info("删除出错了:" . $e->getMessage());
  181. util::fail('删除出错了');
  182. }
  183. }
  184. //添加花材
  185. public function actionAddItem()
  186. {
  187. $post = Yii::$app->request->post();
  188. $id = $post['id'] ?? 0;
  189. $data = $post['data'] ?? 0;
  190. $arr = json_decode($data, true);
  191. $order = OrderClass::getById($id, true);
  192. if (empty($order)) {
  193. util::fail('没有订单');
  194. }
  195. if ($order->status != 2) {
  196. util::fail('待发货才能添加');
  197. }
  198. if ($order->book == 0) {
  199. util::fail('不是预订单');
  200. }
  201. if (floatval($order->actPrice) != 0) {
  202. util::fail('订单金额不是0');
  203. }
  204. if (empty($arr)) {
  205. util::fail('请选花材');
  206. }
  207. $new = [];
  208. foreach ($arr as $val) {
  209. $id = $val['id'] ?? 0;
  210. $num = $val['bigCount'] ?? 0;
  211. $name = $val['name'] ?? '';
  212. $price = $val['price'] ?? 0;
  213. if ($num <= 0) {
  214. util::fail($name . ' 数量要大于0');
  215. }
  216. $new[$id] = ['num' => $num, 'productId' => $id, 'price' => $price];
  217. }
  218. $connection = Yii::$app->db;//事务处理
  219. $transaction = $connection->beginTransaction();
  220. try {
  221. OrderItemClass::addBookItemFn($order, $new);
  222. $transaction->commit();
  223. util::complete('添加成功');
  224. } catch (\Exception $e) {
  225. $transaction->rollBack();
  226. Yii::info("添加出错了:" . $e->getMessage());
  227. util::fail('添加出错了');
  228. }
  229. }
  230. //赠送花材 ssh 20230411
  231. public function actionGiveItem()
  232. {
  233. $post = Yii::$app->request->post();
  234. $id = $post['id'] ?? 0;
  235. $num = $post['num'] ?? 0;
  236. if (is_numeric($num) == false || $num <= 0) {
  237. util::fail('请填写赠送数量');
  238. }
  239. $orderItem = OrderItemClass::getById($id, true);
  240. if (empty($orderItem)) {
  241. util::fail('没有找到花材');
  242. }
  243. if ($orderItem->mainId != $this->mainId) {
  244. util::fail('无法操作');
  245. }
  246. if (!in_array($this->mainId, [1496])) {
  247. util::fail('开发中');
  248. }
  249. $connection = Yii::$app->db;//事务处理
  250. $transaction = $connection->beginTransaction();
  251. try {
  252. $staff = $this->shopAdmin;
  253. $staffName = $staff->name ?? '';
  254. $staffId = $staff->id ?? 0;
  255. $data = ['shopId' => $this->shopId, 'sjId' => $this->sjId, 'staffName' => $staffName, 'staffId' => $staffId];
  256. $respond = OrderItemService::giveProduct($data, $orderItem, $num);
  257. $transaction->commit();
  258. util::success(['info' => $respond], '赠送成功');
  259. } catch (\Exception $e) {
  260. $transaction->rollBack();
  261. Yii::info("赠送出错了:" . $e->getMessage());
  262. util::fail('出错了');
  263. }
  264. }
  265. //重选花材 ssh 2021.3.2
  266. public function actionReset()
  267. {
  268. $post = Yii::$app->request->post();
  269. $id = $post['id'] ?? 0;
  270. $product = $post['product'] ?? '[]';
  271. $product = json_decode($product, true);
  272. //检查花材是否都是同家门店的
  273. ProductClass::valid($product, $this->mainId);
  274. $info = OrderService::getOrderInfo($id);
  275. OrderClass::valid($info, $this->shopId);
  276. //添加修改花材
  277. $orderSn = $info['orderSn'] ?? '';
  278. OrderItemClass::replaceItem($orderSn, $product);
  279. util::complete();
  280. }
  281. //修改花材时获取订单花材和花材基本信息的组合 ssh 20210809
  282. public function actionGetOrderProduct()
  283. {
  284. $id = Yii::$app->request->get('id', 0);
  285. $order = OrderClass::getById($id, true);
  286. OrderClass::valid($order, $this->shopId);
  287. $orderSn = $order->orderSn ?? '';
  288. $itemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', 'productId');
  289. $ids = array_column($itemList, 'productId');
  290. if (empty($ids)) {
  291. util::fail('没有找到花材');
  292. }
  293. $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id');
  294. if (empty($productList)) {
  295. util::fail('没有找到花材信息');
  296. }
  297. foreach ($productList as $key => $product) {
  298. $productId = $product['id'] ?? 0;
  299. $currentItem = $itemList[$productId] ?? [];
  300. $bigCount = $currentItem['bigNum'] ?? 0;
  301. $smallCount = $currentItem['smallNum'] ?? 0;
  302. $userPrice = $currentItem['userPrice'] ?? 0;
  303. $productList[$key]['bigCount'] = $bigCount;
  304. $productList[$key]['smallCount'] = $smallCount;
  305. $productList[$key]['userPrice'] = $userPrice;
  306. $cover = imgUtil::groupImg($product['cover']);
  307. $productList[$key]['cover'] = $cover;
  308. $bigPrice = $product['unitPrice'] ?? 0;
  309. $smallPrice = $product['smallUnitPrice'] ?? 0;
  310. $productList[$key]['bigPrice'] = $bigPrice;
  311. $productList[$key]['smallPrice'] = $smallPrice;
  312. $productList[$key]['cover'] = $cover;
  313. }
  314. util::success(['productList' => array_values($productList)]);
  315. }
  316. //根据花材id取订单列表 ssh 20211011
  317. public function actionGetOrderInfoList()
  318. {
  319. $get = Yii::$app->request->get();
  320. $productId = $get['productId'] ?? 0;
  321. $product = ProductClass::getById($productId, true);
  322. ProductClass::check($product, $this->mainId);
  323. $where = ['productId' => $productId];
  324. $respond = OrderItemClass::getOrderInfoList($where);
  325. util::success($respond);
  326. }
  327. //修改花材的备注 ssh 20240514
  328. public function actionUpdateItemRemark()
  329. {
  330. $get = Yii::$app->request->get();
  331. $id = $get['id'] ?? 0;
  332. $remark = $get['remark'] ?? '';
  333. $info = OrderItemClass::getById($id, true);
  334. if (empty($info)) {
  335. util::fail('没有找到');
  336. }
  337. if ($info->mainId != $this->mainId) {
  338. util::fail('不是你的花材');
  339. }
  340. $info->remark = $remark;
  341. $info->save();
  342. util::complete();
  343. }
  344. }