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