OrderItemController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. $staff = $this->shopAdmin;
  176. $staffId = $staff->id??0;
  177. $staffName = $staff->name??'';
  178. $params = ['staff'=>$staff,'staffId'=>$staffId,'staffName'=>$staffName,];
  179. OrderItemClass::delBookItem($order, $smallId,$params);
  180. $transaction->commit();
  181. util::complete('删除成功');
  182. } catch (\Exception $e) {
  183. $transaction->rollBack();
  184. Yii::info("删除出错了:" . $e->getMessage());
  185. util::fail('删除出错了');
  186. }
  187. }
  188. //添加花材
  189. public function actionAddItem()
  190. {
  191. $post = Yii::$app->request->post();
  192. $id = $post['id'] ?? 0;
  193. $data = $post['data'] ?? 0;
  194. $arr = json_decode($data, true);
  195. $order = OrderClass::getById($id, true);
  196. if (empty($order)) {
  197. util::fail('没有订单');
  198. }
  199. if ($order->status != 2) {
  200. util::fail('待发货才能添加');
  201. }
  202. if ($order->book == 0) {
  203. util::fail('不是预订单');
  204. }
  205. if (floatval($order->actPrice) != 0) {
  206. util::fail('订单金额不是0');
  207. }
  208. if (empty($arr)) {
  209. util::fail('请选花材');
  210. }
  211. $new = [];
  212. foreach ($arr as $val) {
  213. $id = $val['id'] ?? 0;
  214. $num = $val['bigCount'] ?? 0;
  215. $name = $val['name'] ?? '';
  216. $price = $val['price'] ?? 0;
  217. if ($num <= 0) {
  218. util::fail($name . ' 数量要大于0');
  219. }
  220. $new[$id] = ['num' => $num, 'productId' => $id, 'price' => $price];
  221. }
  222. $connection = Yii::$app->db;//事务处理
  223. $transaction = $connection->beginTransaction();
  224. try {
  225. $staff = $this->shopAdmin;
  226. $staffId = $staff->id??0;
  227. $staffName = $staff->name??'';
  228. $params = ['staff'=>$staff,'staffId'=>$staffId,'staffName'=>$staffName,];
  229. OrderItemClass::addBookItemFn($order, $new,$params);
  230. $transaction->commit();
  231. util::complete('添加成功');
  232. } catch (\Exception $e) {
  233. $transaction->rollBack();
  234. Yii::info("添加出错了:" . $e->getMessage());
  235. util::fail('添加出错了');
  236. }
  237. }
  238. //赠送花材 ssh 20230411
  239. public function actionGiveItem()
  240. {
  241. $post = Yii::$app->request->post();
  242. $id = $post['id'] ?? 0;
  243. $num = $post['num'] ?? 0;
  244. if (is_numeric($num) == false || $num <= 0) {
  245. util::fail('请填写赠送数量');
  246. }
  247. $orderItem = OrderItemClass::getById($id, true);
  248. if (empty($orderItem)) {
  249. util::fail('没有找到花材');
  250. }
  251. if ($orderItem->mainId != $this->mainId) {
  252. util::fail('无法操作');
  253. }
  254. if (!in_array($this->mainId, [1496])) {
  255. util::fail('开发中');
  256. }
  257. $connection = Yii::$app->db;//事务处理
  258. $transaction = $connection->beginTransaction();
  259. try {
  260. $staff = $this->shopAdmin;
  261. $staffName = $staff->name ?? '';
  262. $staffId = $staff->id ?? 0;
  263. $data = ['shopId' => $this->shopId, 'sjId' => $this->sjId, 'staffName' => $staffName, 'staffId' => $staffId];
  264. $respond = OrderItemService::giveProduct($data, $orderItem, $num);
  265. $transaction->commit();
  266. util::success(['info' => $respond], '赠送成功');
  267. } catch (\Exception $e) {
  268. $transaction->rollBack();
  269. Yii::info("赠送出错了:" . $e->getMessage());
  270. util::fail('出错了');
  271. }
  272. }
  273. //重选花材 ssh 2021.3.2
  274. public function actionReset()
  275. {
  276. $post = Yii::$app->request->post();
  277. $id = $post['id'] ?? 0;
  278. $product = $post['product'] ?? '[]';
  279. $product = json_decode($product, true);
  280. //检查花材是否都是同家门店的
  281. ProductClass::valid($product, $this->mainId);
  282. $info = OrderService::getOrderInfo($id);
  283. OrderClass::valid($info, $this->shopId);
  284. //添加修改花材
  285. $orderSn = $info['orderSn'] ?? '';
  286. OrderItemClass::replaceItem($orderSn, $product);
  287. util::complete();
  288. }
  289. //修改花材时获取订单花材和花材基本信息的组合 ssh 20210809
  290. public function actionGetOrderProduct()
  291. {
  292. $id = Yii::$app->request->get('id', 0);
  293. $order = OrderClass::getById($id, true);
  294. OrderClass::valid($order, $this->shopId);
  295. $orderSn = $order->orderSn ?? '';
  296. $itemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', 'productId');
  297. $ids = array_column($itemList, 'productId');
  298. if (empty($ids)) {
  299. util::fail('没有找到花材');
  300. }
  301. $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id');
  302. if (empty($productList)) {
  303. util::fail('没有找到花材信息');
  304. }
  305. foreach ($productList as $key => $product) {
  306. $productId = $product['id'] ?? 0;
  307. $currentItem = $itemList[$productId] ?? [];
  308. $bigCount = $currentItem['bigNum'] ?? 0;
  309. $smallCount = $currentItem['smallNum'] ?? 0;
  310. $userPrice = $currentItem['userPrice'] ?? 0;
  311. $productList[$key]['bigCount'] = $bigCount;
  312. $productList[$key]['smallCount'] = $smallCount;
  313. $productList[$key]['userPrice'] = $userPrice;
  314. $cover = imgUtil::groupImg($product['cover']);
  315. $productList[$key]['cover'] = $cover;
  316. $bigPrice = $product['unitPrice'] ?? 0;
  317. $smallPrice = $product['smallUnitPrice'] ?? 0;
  318. $productList[$key]['bigPrice'] = $bigPrice;
  319. $productList[$key]['smallPrice'] = $smallPrice;
  320. $productList[$key]['cover'] = $cover;
  321. }
  322. util::success(['productList' => array_values($productList)]);
  323. }
  324. //根据花材id取订单列表 ssh 20211011
  325. public function actionGetOrderInfoList()
  326. {
  327. $get = Yii::$app->request->get();
  328. $productId = $get['productId'] ?? 0;
  329. $product = ProductClass::getById($productId, true);
  330. ProductClass::check($product, $this->mainId);
  331. $where = ['productId' => $productId];
  332. $respond = OrderItemClass::getOrderInfoList($where);
  333. util::success($respond);
  334. }
  335. //修改花材的备注 ssh 20240514
  336. public function actionUpdateItemRemark()
  337. {
  338. $get = Yii::$app->request->get();
  339. $id = $get['id'] ?? 0;
  340. $remark = $get['remark'] ?? '';
  341. $info = OrderItemClass::getById($id, true);
  342. if (empty($info)) {
  343. util::fail('没有找到');
  344. }
  345. if ($info->mainId != $this->mainId) {
  346. util::fail('不是你的花材');
  347. }
  348. $info->remark = $remark;
  349. $info->save();
  350. util::complete();
  351. }
  352. }