WorkController.php 10 KB

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