WorkController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. $print = $post['print'] ?? 1;
  117. if ($complete == 1 && $print == 1) {
  118. WorkClass::print($work, $this->shop, false);
  119. }
  120. util::success($work);
  121. } catch (\Exception $e) {
  122. $transaction->rollBack();
  123. Yii::error("失败原因:" . $e->getMessage());
  124. util::fail('操作失败');
  125. }
  126. }
  127. //商品新建制作 ssh 20220320
  128. public function actionGoodsCreate()
  129. {
  130. $post = Yii::$app->request->post();
  131. $post['mainId'] = $this->mainId ?? 0;
  132. $post['sjId'] = $this->sjId ?? 0;
  133. $post['shopId'] = $this->shopId ?? 0;
  134. $connection = Yii::$app->db;
  135. $transaction = $connection->beginTransaction();
  136. try {
  137. $goodsJson = $post['goods'] ?? '';
  138. $goods = json_decode($goodsJson, true);
  139. if (empty($goods)) {
  140. util::fail('没有找到商品');
  141. }
  142. $post['goods'] = $goods;
  143. $main = $this->main;
  144. WorkClass::goodsCreateFinish($post, $main);
  145. $transaction->commit();
  146. util::complete();
  147. } catch (\Exception $e) {
  148. $transaction->rollBack();
  149. Yii::error("失败原因:" . $e->getMessage());
  150. util::fail('操作失败');
  151. }
  152. }
  153. //包括制作单和用材信息 ssh 20220319
  154. public function actionFullInfo()
  155. {
  156. $get = Yii::$app->request->get();
  157. $id = $get['id'] ?? 0;
  158. $work = WorkClass::getById($id, true);
  159. WorkClass::valid($work, $this->mainId);
  160. $workItem = WorkItemClass::getWorkAllItem($work);
  161. $goods = [];
  162. $goodsId = $work->goodsId ?? 0;
  163. if (!empty($goodsId)) {
  164. $goods = GoodsClass::getGoodsInfo($goodsId);
  165. }
  166. $work = $work->attributes;
  167. $shortCover = $work['cover'] ?? '';
  168. $work['shortCover'] = $shortCover;
  169. $work['cover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  170. $work['bigCover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_1000,w_1000";
  171. $work['smallCover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  172. $today = date("Y-m-d");
  173. $tomorrow = date("Y-m-d", strtotime('+1 day'));
  174. $work['today'] = $today;
  175. $work['tomorrow'] = $tomorrow;
  176. $addTime = $work['addTime'] ?? 0;
  177. $currentTime = strtotime($addTime) + 60;
  178. $diffTime = bcsub($currentTime, time());
  179. $beginCount = $diffTime > 0 ? $diffTime : 0;
  180. $work['beginCount'] = $beginCount;
  181. util::success(['info' => $work, 'item' => $workItem, 'goods' => $goods]);
  182. }
  183. //取消 ssh 20220320
  184. public function actionCancel()
  185. {
  186. $get = Yii::$app->request->get();
  187. $id = $get['id'] ?? 0;
  188. $work = WorkClass::getById($id, true);
  189. WorkClass::valid($work, $this->mainId);
  190. WorkClass::cancel($work);
  191. util::complete();
  192. }
  193. //完成制作单 ssh 20220319
  194. public function actionFinish()
  195. {
  196. $post = Yii::$app->request->post();
  197. $workId = $post['id'] ?? 0;
  198. $connection = Yii::$app->db;//事务处理
  199. $transaction = $connection->beginTransaction();
  200. try {
  201. $work = WorkClass::getById($workId, true);
  202. WorkClass::valid($work, $this->mainId);
  203. if ($work->status == 1) {
  204. util::fail('制作单已完成');
  205. }
  206. if ($work->status == 2) {
  207. util::fail('制作单已取消');
  208. }
  209. //花材未锁定,需要锁定并扣库存
  210. if ($work->stockLock == 0) {
  211. $itemList = $post['itemList'] ?? '';
  212. if (!empty($itemList)) {
  213. $product = json_decode($itemList, true);
  214. if (!empty($product)) {
  215. WorkItemClass::modifyItem($work, $product);
  216. WorkItemClass::lock($work);
  217. }
  218. }
  219. }
  220. WorkClass::finish($work);
  221. $transaction->commit();
  222. //情人节美团的暂时不打小票
  223. if ($work->fromType != 4) {
  224. WorkClass::print($work, $this->shop, false);
  225. }
  226. $msg = '已完成';
  227. $orderId = $work->orderId ?? 0;
  228. if (!empty($orderId) && $work->fromType == dict::getDict('fromType', 'mt')) {
  229. $allWork = WorkClass::getAllByCondition(['orderId' => $orderId], null, '*', null, true);
  230. //订单下如果还有制作单未完成,则不通知美图已经完成拣货
  231. $allComplete = true;
  232. if (!empty($allWork)) {
  233. foreach ($allWork as $current) {
  234. if ($current->status != 1) {
  235. $allComplete = false;
  236. }
  237. }
  238. }
  239. if ($allComplete) {
  240. $order = OrderClass::getById($orderId, true);
  241. $thirdOrderId = $order->thirdOrderId ?? '';
  242. $fromType = $order->fromType ?? 0;
  243. if (!empty($thirdOrderId) && $fromType == dict::getDict('fromType', 'mt')) {
  244. // $shopId = $order->shopId ?? 0;
  245. // $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  246. // $config = dict::getDict('mtConfig');
  247. // $app = new Application($config);
  248. // $accessToken = mtUtil::getAccessToken($ext);
  249. // $params = ['order_id' => $thirdOrderId, 'access_token' => $accessToken];
  250. // $mtRes = $app->order->logisticsPush($params);
  251. // $mtRes = json_decode($mtRes, true);
  252. // if (isset($mtRes['data']) && $mtRes['data'] == 'ok') {
  253. // $msg = '已完成,已通知骑手';
  254. // }
  255. }
  256. }
  257. }
  258. util::complete($msg);
  259. } catch (\Exception $e) {
  260. $transaction->rollBack();
  261. Yii::error("失败原因:" . $e->getMessage());
  262. util::fail('操作失败');
  263. }
  264. }
  265. }