WorkController.php 11 KB

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