LiveOrderController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\custom\classes\CustomClass;
  4. use bizGhs\live\classes\LiveOrderClass;
  5. use bizGhs\live\classes\LiveOrderCustomClass;
  6. use bizGhs\product\classes\ProductClass;
  7. use common\components\dateUtil;
  8. use common\components\stringUtil;
  9. use common\components\util;
  10. use Yii;
  11. class LiveOrderController extends BaseController
  12. {
  13. //获取未转化的花材 ssh 20241009
  14. public function actionUnConvert()
  15. {
  16. $mainId = $this->mainId;
  17. $list = LiveOrderClass::getAllByCondition(['mainId' => $mainId, 'switchOrder' => 0], null, '*', 'itemId', true);
  18. util::success(['list'=>$list]);
  19. }
  20. //转换成订单 ssh 20241006
  21. public function actionConvertOrder()
  22. {
  23. $mainId = $this->mainId;
  24. $cacheKey = 'live_order_' . $mainId;
  25. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  26. if (!empty($has)) {
  27. util::fail('已经在转换了');
  28. }
  29. $list = LiveOrderClass::getAllByCondition(['mainId' => $mainId, 'switchOrder' => 0], null, '*', null, true);
  30. if (empty($list)) {
  31. util::fail('没有记录需要转换');
  32. }
  33. $arr = [];
  34. foreach ($list as $live) {
  35. $addTime = $live->addTime;
  36. $date = date("Y-m-d", strtotime($addTime));
  37. $arr[$date] = 1;
  38. }
  39. if (count($arr) > 1) {
  40. util::fail('有记录不是同一天的');
  41. }
  42. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 180, 1]);
  43. util::success('提交成功,3分钟后查看订单');
  44. }
  45. //走播单列表 ssh 20241003
  46. public function actionList()
  47. {
  48. $get = Yii::$app->request->get();
  49. $staffId = $get['staffId'] ?? 0;
  50. $switchOrder = $get['switchOrder'] ?? -1;
  51. $searchContent = $get['searchContent'] ?? '';
  52. $where = [];
  53. $where['mainId'] = $this->mainId;
  54. if (!empty($staffId)) {
  55. $where['staffId'] = $staffId;
  56. }
  57. if (!empty($searchContent)) {
  58. if (stringUtil::isLetter($searchContent)) {
  59. $where['itemPy'] = ['like', $searchContent];
  60. } else {
  61. $where['itemName'] = ['like', $searchContent];
  62. }
  63. }
  64. if ($switchOrder > -1) {
  65. $where['switchOrder'] = $switchOrder;
  66. }
  67. $searchTime = $get['searchTime'] ?? '';
  68. if (!empty($searchTime)) {
  69. $startTime = $get['startTime'] ?? '';
  70. $endTime = $get['endTime'] ?? '';
  71. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  72. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  73. }
  74. $respond = LiveOrderClass::getOrderList($where);
  75. util::success($respond);
  76. }
  77. //添加订单 ssh 20241003
  78. public function actionAddOrder()
  79. {
  80. $post = Yii::$app->request->post();
  81. $itemId = $post['itemId'] ?? 0;
  82. if (empty($itemId)) {
  83. util::fail('请选花材');
  84. }
  85. $product = ProductClass::getById($itemId, true);
  86. if (empty($product)) {
  87. util::fail('没有找到花材');
  88. }
  89. if ($product->mainId != $this->mainId) {
  90. util::fail('不是你的花材');
  91. }
  92. $itemPrice = $post['itemPrice'] ?? 0;
  93. $itemRemark = $post['itemRemark'] ?? '';
  94. $selectCustom = $post['selectCustom'] ?? '';
  95. if (empty($selectCustom)) {
  96. util::fail('没有客户');
  97. }
  98. $arr = json_decode($selectCustom, true);
  99. if (empty($arr) || is_array($arr) == false) {
  100. util::fail('没有客户信息');
  101. }
  102. $mainId = $this->mainId;
  103. $hasItem = LiveOrderClass::getByCondition(['mainId' => $mainId, 'itemId' => $itemId, 'switchOrder' => 0], true);
  104. if (!empty($hasItem)) {
  105. util::fail('这个花材已添加过');
  106. }
  107. $has = [];
  108. $customList = [];
  109. foreach ($arr as $key => $info) {
  110. $customId = $info['customId'] ?? 0;
  111. if (isset($has[$customId])) {
  112. util::fail('重复的客户');
  113. }
  114. $has[$customId] = 1;
  115. $customName = $info['customName'] ?? '';
  116. $num = $info['num'] ?? 0;
  117. $price = $info['price'] ?? 0;
  118. $customList[] = ['customId' => $customId, 'customName' => $customName, 'num' => $num, 'price' => $price];
  119. }
  120. $staff = $this->shopAdmin;
  121. $staffId = $staff->id ?? 0;
  122. $staffName = $staff->name ?? '';
  123. $params = ['product' => $product, 'customList' => $customList, 'staffId' => $staffId, 'staffName' => $staffName, 'itemPrice' => $itemPrice, 'itemRemark' => $itemRemark];
  124. $connection = Yii::$app->db;
  125. $transaction = $connection->beginTransaction();
  126. try {
  127. LiveOrderClass::addOrder($params, $mainId);
  128. $transaction->commit();
  129. util::complete('创建成功');
  130. } catch (\Exception $e) {
  131. Yii::info("操作失败原因:" . $e->getMessage());
  132. $transaction->rollBack();
  133. util::fail('操作失败');
  134. }
  135. }
  136. //获取明细 ssh 20241005
  137. public function actionGetDetail()
  138. {
  139. $get = Yii::$app->request->get();
  140. $id = $get['id'] ?? 0;
  141. $live = LiveOrderClass::getById($id, true);
  142. if (empty($live)) {
  143. util::fail('没有找到记录');
  144. }
  145. if ($live->mainId != $this->mainId) {
  146. util::fail('不是你的记录');
  147. }
  148. $orderSn = $live->orderSn ?? '';
  149. $customList = LiveOrderCustomClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  150. util::success(['customList' => $customList, 'info' => $live]);
  151. }
  152. //添加客户 ssh 20241005
  153. public function actionAddCustom()
  154. {
  155. $post = Yii::$app->request->post();
  156. $id = $post['id'] ?? 0;
  157. $customId = $post['customId'] ?? 0;
  158. $custom = CustomClass::getById($customId, true);
  159. if (empty($custom)) {
  160. util::fail('没有找到客户');
  161. }
  162. if ($custom->ownMainId != $this->mainId) {
  163. util::fail('不是你的客户');
  164. }
  165. $num = $post['num'] ?? 0;
  166. $price = $post['price'] ?? 0;
  167. $live = LiveOrderClass::getById($id, true);
  168. if (empty($live)) {
  169. util::fail('没有找到记录');
  170. }
  171. if ($live->mainId != $this->mainId) {
  172. util::fail('不是你的记录');
  173. }
  174. if ($live->switchOrder == 1) {
  175. util::fail('已转订单,不能添加');
  176. }
  177. $params = ['num' => $num, 'price' => $price];
  178. LiveOrderClass::addCustom($params, $custom, $live);
  179. util::complete('添加成功');
  180. }
  181. //删除客户 ssh 20241005
  182. public function actionDelCustom()
  183. {
  184. $get = Yii::$app->request->get();
  185. $id = $get['id'] ?? 0;
  186. $customId = $get['customId'] ?? 0;
  187. $custom = CustomClass::getById($customId, true);
  188. if (empty($custom)) {
  189. util::fail('没有找到客户');
  190. }
  191. if ($custom->ownMainId != $this->mainId) {
  192. util::fail('不是你的客户');
  193. }
  194. $live = LiveOrderClass::getById($id, true);
  195. if (empty($live)) {
  196. util::fail('没有找到记录');
  197. }
  198. if ($live->mainId != $this->mainId) {
  199. util::fail('不是你的记录');
  200. }
  201. if ($live->switchOrder == 1) {
  202. util::fail('已转订单,不能删除');
  203. }
  204. LiveOrderClass::delCustom($custom, $live);
  205. util::complete('添加成功');
  206. }
  207. //更新客户 ssh 20241005
  208. public function actionUpdateCustom()
  209. {
  210. $post = Yii::$app->request->post();
  211. $id = $post['id'] ?? 0;
  212. $customList = $post['customList'] ?? 0;
  213. $customData = json_decode($customList, true);
  214. if (empty($customData)) {
  215. util::fail('没有客户信息');
  216. }
  217. $live = LiveOrderClass::getById($id, true);
  218. if (empty($live)) {
  219. util::fail('没有找到记录');
  220. }
  221. if ($live->mainId != $this->mainId) {
  222. util::fail('不是你的记录');
  223. }
  224. if ($live->switchOrder == 1) {
  225. util::fail('已转订单,不能修改');
  226. }
  227. LiveOrderClass::updateCustom($customData, $live);
  228. util::complete('修改成功');
  229. }
  230. public function actionPrintOrder()
  231. {
  232. $get = Yii::$app->request->get();
  233. $id = $get['id'] ?? 0;
  234. $live = LiveOrderClass::getById($id, true);
  235. if (empty($live)) {
  236. util::fail('没有找到');
  237. }
  238. if ($live->mainId != $this->mainId) {
  239. util::fail('不是你的');
  240. }
  241. $orderSn = $live->orderSn ?? '';
  242. $liveCustomList = LiveOrderCustomClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  243. if (empty($liveCustomList)) {
  244. util::fail('没有客户预订');
  245. }
  246. $ids = array_column($liveCustomList, 'customId');
  247. $customList = CustomClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id');
  248. $table = [];
  249. foreach ($liveCustomList as $liveCustom) {
  250. $customId = $liveCustom['customId'] ?? 0;
  251. $currentName = $liveCustom['customName'] ?? '';
  252. $num = $liveCustom['num'] ?? '';
  253. $customInfo = $customList[$customId] ?? [];
  254. if (isset($customInfo['seatSnName']) && !empty($customInfo['seatSnName'])) {
  255. $currentName = $customInfo['seatSnName'];
  256. }
  257. $seatSn = $customInfo['seatSn'] ?? '';
  258. $table[] = [
  259. 'name' => $currentName,
  260. 'num' => $num,
  261. 'seatSn' => $seatSn,
  262. 'remark' => '',
  263. ];
  264. }
  265. $addTime = $live->addTime;
  266. $itemName = $live->itemName ?? '';
  267. $time = date("Y-m-d", strtotime($addTime));
  268. $itemPrice = $live->itemPrice ? floatval($live->itemPrice) : 0;
  269. $itemRemark = $live->itemRemark ?? '';
  270. $title = $time . ' ' . $itemName . ' ' . $itemPrice . '元';
  271. if (!empty($itemRemark)) {
  272. $title .= ' ' . $itemRemark;
  273. }
  274. $a4Print = [
  275. 'table' => $table,
  276. 'title' => $title,
  277. ];
  278. $info['printData'] = $a4Print;
  279. $template = '{"panels":[{"index":0,"name":1,"height":297,"width":210,"paperHeader":49.5,"paperFooter":780,"printElements":[{"options":{"left":30,"top":53,"height":52,"width":538,"field":"table","coordinateSync":false,"widthHeightSync":false,"fontSize":16,"lineHeight":30,"tableBorder":"border","tableHeaderBorder":"border","tableHeaderCellBorder":"border","tableHeaderFontWeight":"bold","right":567.25,"bottom":110.24609375,"vCenter":298.25,"hCenter":82.24609375,"textAlign":"center","columns":[[{"width":107.90432245461145,"title":"货位","field":"seatSn","checked":true,"columnId":"seatSn","fixed":false,"rowspan":1,"colspan":1,"align":"center","halign":"center","vAlign":"middle","tableQRCodeLevel":0,"tableSummaryTitle":true,"tableSummary":""},{"width":186.15171514242957,"title":"客户名称","field":"name","checked":true,"columnId":"name","fixed":false,"rowspan":1,"colspan":1,"align":"center","tableQRCodeLevel":0,"tableSummaryTitle":true,"tableSummary":""},{"width":103.10001880877743,"title":"数量","field":"num","checked":true,"columnId":"num","fixed":false,"rowspan":1,"colspan":1,"align":"center","tableQRCodeLevel":0,"tableSummaryTitle":true,"tableSummary":""},{"width":140.84394359418155,"title":"备注","field":"remark","checked":true,"columnId":"remark","fixed":false,"rowspan":1,"colspan":1,"align":"center","tableQRCodeLevel":0,"tableSummaryTitle":true,"tableSummary":""},{"width":93.32065807548679,"title":"日期","field":"shortTime","checked":false,"columnId":"shortTime","fixed":false,"rowspan":1,"colspan":1,"align":"center","tableQRCodeLevel":0,"tableSummaryTitle":true,"tableSummary":""},{"width":74.09275968851016,"title":"库存","field":"stock","checked":false,"columnId":"stock","fixed":false,"rowspan":1,"colspan":1,"align":"center","tableQRCodeLevel":0,"tableSummaryTitle":true,"tableSummary":""},{"width":276,"title":"金额\n","field":"amount","checked":false,"columnId":"amount","fixed":false,"rowspan":1,"colspan":1,"tableQRCodeLevel":0,"tableSummaryTitle":true,"tableSummary":""},{"width":100,"title":"","field":"","checked":false,"columnId":"","fixed":false,"rowspan":1,"colspan":1},{"width":100,"title":"","field":"","checked":false,"columnId":"","fixed":false,"rowspan":1,"colspan":1}]]},"printElementType":{"title":"空白表格","type":"table","editable":true,"columnDisplayEditable":true,"columnDisplayIndexEditable":true,"columnTitleEditable":true,"columnResizable":true,"columnAlignEditable":true,"isEnableEditField":true,"isEnableContextMenu":true,"isEnableInsertRow":true,"isEnableDeleteRow":true,"isEnableInsertColumn":true,"isEnableDeleteColumn":true,"isEnableMergeCell":true}},{"options":{"left":12,"top":786,"height":49,"width":49},"printElementType":{"title":"html","type":"html"}},{"options":{"left":67.5,"top":18,"height":35,"width":455,"title":"文本","right":522.5,"bottom":53,"vCenter":295,"hCenter":35.5,"field":"title","testData":"纯彩 15280215347","coordinateSync":false,"widthHeightSync":false,"hideTitle":true,"fontSize":19.5,"fontWeight":"bold","textAlign":"center","textContentVerticalAlign":"middle","qrCodeLevel":0},"printElementType":{"title":"文本","type":"text"}}],"paperNumberLeft":565.5,"paperNumberTop":819,"paperNumberContinue":true,"watermarkOptions":{"content":"","rotate":25,"timestamp":true,"format":"YYYY-MM-DD HH:mm","fillStyle":"rgba(184, 184, 184, 0.3)","fontSize":"14px","width":200,"height":200},"panelLayoutOptions":{"layoutType":"column","layoutRowGap":0,"layoutColumnGap":0}}]}';
  280. $info['template'] = $template;
  281. $live->printOrder = 1;
  282. $live->save();
  283. util::success($info);
  284. }
  285. //修改创建时间 ssh 20241006
  286. public function actionChangeDate()
  287. {
  288. $get = Yii::$app->request->get();
  289. $id = $get['id'] ?? 0;
  290. $live = LiveOrderClass::getById($id, true);
  291. if (empty($live)) {
  292. util::fail('没有找到');
  293. }
  294. if ($live->mainId != $this->mainId) {
  295. util::fail('不是你的');
  296. }
  297. $flag = $get['flag'] ?? 0;
  298. $addTime = $live->addTime;
  299. if ($flag == 0) {
  300. $time = strtotime($addTime) - 86400;
  301. } else {
  302. $time = strtotime($addTime) + 86400;
  303. }
  304. $newDate = date("Y-m-d H:i:s", $time);
  305. $live->addTime = $newDate;
  306. $live->save();
  307. util::complete('修改成功');
  308. }
  309. }