WorkController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. namespace hd\controllers;
  3. use biz\shop\classes\ShopExtClass;
  4. use bizHd\goods\classes\CategoryClass;
  5. use bizHd\goods\classes\GoodsClass;
  6. use bizHd\order\classes\OrderClass;
  7. use bizHd\work\classes\WorkClass;
  8. use bizHd\work\classes\WorkItemClass;
  9. use common\components\dict;
  10. use common\components\imgUtil;
  11. use common\components\mtUtil;
  12. use common\components\orderSn;
  13. use common\components\util;
  14. use Shishaoqi\MeituanFlashPurchase\Application;
  15. use Yii;
  16. class WorkController extends BaseController
  17. {
  18. public $guestAccess = [];
  19. //根据订单id查询制作单信息 ssh 20220625
  20. public function actionGetInfoByOrderId()
  21. {
  22. $orderId = Yii::$app->request->get('orderId', 0);
  23. $order = OrderClass::getById($orderId, true);
  24. OrderClass::valid($order, $this->mainId);
  25. $info = WorkClass::getByCondition(['orderId' => $orderId], true);
  26. if (empty($info)) {
  27. util::fail('没有找到');
  28. }
  29. util::success(['info' => $info, 'order' => $order]);
  30. }
  31. //打印制作单 ssh 20220601
  32. public function actionPrint()
  33. {
  34. $get = Yii::$app->request->get();
  35. $id = $get['id'] ?? 0;
  36. $work = WorkClass::getById($id, true);
  37. WorkClass::valid($work, $this->mainId);
  38. WorkClass::print($work, $this->shop);
  39. util::complete();
  40. }
  41. //制作单列表 ssh 20220319
  42. public function actionList()
  43. {
  44. $get = Yii::$app->request->get();
  45. $where = [];
  46. $where['mainId'] = $this->mainId;
  47. $str = $get['sh'] ?? '';
  48. if ($str == 'yes') {
  49. $where['sh'] = 1;
  50. } else {
  51. $where['sh'] = 0;
  52. if (isset($get['fromType']) && is_numeric($get['fromType'])) {
  53. $where['fromType'] = $get['fromType'];
  54. } else {
  55. $shopId = $this->shopId;
  56. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  57. $mtAlone = $ext->mtAlone ?? 0;
  58. $isCashier = $get['isCashier'] ?? 0;
  59. if ($mtAlone == 1 && $isCashier == 1) {
  60. $where['fromType'] = ['in', [1, 2, 3]];
  61. }
  62. }
  63. }
  64. if (isset($get['sendNum']) && !empty($get['sendNum']) && is_numeric($get['sendNum'])) {
  65. $where['sendNum'] = $get['sendNum'];
  66. }
  67. if (isset($get['thirdSn']) && !empty($get['thirdSn']) && is_numeric($get['thirdSn'])) {
  68. $where['thirdSn'] = $get['thirdSn'];
  69. }
  70. if (isset($get['status']) && $get['status'] >= 0) {
  71. $status = $get['status'];
  72. $where['status'] = $status;
  73. }
  74. $list = WorkClass::getWorkList($where);
  75. util::success($list);
  76. }
  77. //锁定用材 ssh 20220320
  78. public function actionLock()
  79. {
  80. $get = Yii::$app->request->get();
  81. $id = $get['id'] ?? 0;
  82. $work = WorkClass::getById($id, true);
  83. WorkClass::valid($work, $this->mainId);
  84. WorkItemClass::lock($work);
  85. util::complete();
  86. }
  87. //新建制作单 ssh 20220319
  88. public function actionCreate()
  89. {
  90. $post = Yii::$app->request->post();
  91. $post['mainId'] = $this->mainId ?? 0;
  92. $post['sjId'] = $this->sjId ?? 0;
  93. $post['shopId'] = $this->shopId ?? 0;
  94. $orderSn = orderSn::getWorkSn();
  95. $post['workSn'] = $orderSn;
  96. $post['staffId'] = $this->shopAdminId;
  97. $post['staffName'] = $this->shopAdmin->name;
  98. $post['name'] = isset($post['name']) && !empty($post['name']) ? $post['name'] : '花束';
  99. $post['cover'] = isset($post['cover']) && !empty($post['cover']) ? $post['cover'] : 'default-img.png';
  100. $connection = Yii::$app->db;
  101. $transaction = $connection->beginTransaction();
  102. try {
  103. $product = $post['product'] ?? '';
  104. $post['product'] = [];
  105. if (!empty($product)) {
  106. $arr = json_decode($product, true);
  107. if (!empty($arr)) {
  108. $post['product'] = $arr;
  109. }
  110. }
  111. $post['num'] = isset($post['num']) && $post['num'] > 0 ? $post['num'] : 1;
  112. $catId = $post['catId'] ?? 0;
  113. $cat = CategoryClass::getById($catId, true);
  114. if ($cat->mainId != $this->mainId) {
  115. util::fail('请选择你自己的分类');
  116. }
  117. $post['catName'] = $cat->categoryName ?? '';
  118. $main = $this->main;
  119. $work = WorkClass::createFinish($post, $main);
  120. $transaction->commit();
  121. $complete = $post['complete'] ?? 0;
  122. $print = $post['print'] ?? 1;
  123. if ($complete == 1 && $print == 1) {
  124. WorkClass::print($work, $this->shop, false);
  125. }
  126. util::success($work);
  127. } catch (\Exception $e) {
  128. $transaction->rollBack();
  129. Yii::error("失败原因:" . $e->getMessage());
  130. util::fail('操作失败');
  131. }
  132. }
  133. //商品新建制作 ssh 20220320
  134. public function actionGoodsCreate()
  135. {
  136. $post = Yii::$app->request->post();
  137. $post['mainId'] = $this->mainId ?? 0;
  138. $post['sjId'] = $this->sjId ?? 0;
  139. $post['shopId'] = $this->shopId ?? 0;
  140. $connection = Yii::$app->db;
  141. $transaction = $connection->beginTransaction();
  142. try {
  143. $goodsJson = $post['goods'] ?? '';
  144. $goods = json_decode($goodsJson, true);
  145. if (empty($goods)) {
  146. util::fail('没有找到商品');
  147. }
  148. $post['goods'] = $goods;
  149. $main = $this->main;
  150. WorkClass::goodsCreateFinish($post, $main);
  151. $transaction->commit();
  152. util::complete();
  153. } catch (\Exception $e) {
  154. $transaction->rollBack();
  155. Yii::error("失败原因:" . $e->getMessage());
  156. util::fail('操作失败');
  157. }
  158. }
  159. //包括制作单和用材信息 ssh 20220319
  160. public function actionFullInfo()
  161. {
  162. $get = Yii::$app->request->get();
  163. $id = $get['id'] ?? 0;
  164. $work = WorkClass::getById($id, true);
  165. WorkClass::valid($work, $this->mainId);
  166. $workItem = WorkItemClass::getWorkAllItem($work);
  167. $goods = [];
  168. $goodsId = $work->goodsId ?? 0;
  169. if (!empty($goodsId)) {
  170. $goods = GoodsClass::getGoodsInfo($goodsId);
  171. }
  172. $work = $work->attributes;
  173. $shortCover = $work['cover'] ?? '';
  174. $work['shortCover'] = $shortCover;
  175. $work['cover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  176. $work['bigCover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_1000,w_1000";
  177. $work['smallCover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  178. $today = date("Y-m-d");
  179. $tomorrow = date("Y-m-d", strtotime('+1 day'));
  180. $work['today'] = $today;
  181. $work['tomorrow'] = $tomorrow;
  182. $addTime = $work['addTime'] ?? 0;
  183. $currentTime = strtotime($addTime) + 60;
  184. $diffTime = bcsub($currentTime, time());
  185. $beginCount = $diffTime > 0 ? $diffTime : 0;
  186. $work['beginCount'] = $beginCount;
  187. util::success(['info' => $work, 'item' => $workItem, 'goods' => $goods]);
  188. }
  189. //取消 ssh 20220320
  190. public function actionCancel()
  191. {
  192. $get = Yii::$app->request->get();
  193. $id = $get['id'] ?? 0;
  194. $work = WorkClass::getById($id, true);
  195. WorkClass::valid($work, $this->mainId);
  196. WorkClass::cancel($work);
  197. util::complete();
  198. }
  199. //完成制作单 ssh 20220319
  200. public function actionFinish()
  201. {
  202. $post = Yii::$app->request->post();
  203. $workId = $post['id'] ?? 0;
  204. $connection = Yii::$app->db;//事务处理
  205. $transaction = $connection->beginTransaction();
  206. try {
  207. $work = WorkClass::getById($workId, true);
  208. WorkClass::valid($work, $this->mainId);
  209. if ($work->status == 1) {
  210. util::fail('制作单已完成');
  211. }
  212. if ($work->status == 2) {
  213. util::fail('制作单已取消');
  214. }
  215. //花材未锁定,需要锁定并扣库存
  216. if ($work->stockLock == 0) {
  217. $itemList = $post['itemList'] ?? '';
  218. if (!empty($itemList)) {
  219. $product = json_decode($itemList, true);
  220. if (!empty($product)) {
  221. WorkItemClass::modifyItem($work, $product);
  222. WorkItemClass::lock($work);
  223. }
  224. }
  225. }
  226. WorkClass::finish($work);
  227. $transaction->commit();
  228. //美团的暂时不打小票
  229. if ($work->fromType != 4) {
  230. WorkClass::print($work, $this->shop, false);
  231. }
  232. $msg = '已完成';
  233. $orderId = $work->orderId ?? 0;
  234. if (!empty($orderId) && $work->fromType == dict::getDict('fromType', 'mt')) {
  235. $allWork = WorkClass::getAllByCondition(['orderId' => $orderId], null, '*', null, true);
  236. //订单下如果还有制作单未完成,则不通知美图已经完成拣货
  237. $allComplete = true;
  238. if (!empty($allWork)) {
  239. foreach ($allWork as $current) {
  240. if ($current->status != 1) {
  241. $allComplete = false;
  242. }
  243. }
  244. }
  245. if ($allComplete) {
  246. $order = OrderClass::getById($orderId, true);
  247. $thirdOrderId = $order->thirdOrderId ?? '';
  248. $fromType = $order->fromType ?? 0;
  249. if (!empty($thirdOrderId) && $fromType == dict::getDict('fromType', 'mt')) {
  250. // $shopId = $order->shopId ?? 0;
  251. // $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  252. // $config = dict::getDict('mtConfig');
  253. // $app = new Application($config);
  254. // $accessToken = mtUtil::getAccessToken($ext);
  255. // $params = ['order_id' => $thirdOrderId, 'access_token' => $accessToken];
  256. // $mtRes = $app->order->logisticsPush($params);
  257. // $mtRes = json_decode($mtRes, true);
  258. // if (isset($mtRes['data']) && $mtRes['data'] == 'ok') {
  259. // $msg = '已完成,已通知骑手';
  260. // }
  261. }
  262. }
  263. }
  264. util::complete($msg);
  265. } catch (\Exception $e) {
  266. $transaction->rollBack();
  267. Yii::error("失败原因:" . $e->getMessage());
  268. util::fail('操作失败');
  269. }
  270. }
  271. }