WorkController.php 11 KB

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