OrderItemController.php 12 KB

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