WorkController.php 11 KB

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