WorkController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. public function actionNeedWork()
  20. {
  21. $get = Yii::$app->request->get();
  22. $id = $get['id'] ?? 0;
  23. $order = OrderClass::getById($id, true);
  24. if (empty($order)) {
  25. util::fail('没有订单');
  26. }
  27. $mainId = $this->mainId;
  28. if ($order->mainId != $mainId) {
  29. util::fail('不是你的订单');
  30. }
  31. if ($order->hasWork == 1) {
  32. util::fail('已经通知制作了');
  33. }
  34. $main = $this->main;
  35. WorkClass::needWork($order, $main);
  36. $has = WorkClass::getByCondition(['orderId' => $id], true);
  37. if (!empty($has)) {
  38. $order->hasWork = 1;
  39. $order->save();
  40. util::complete('通知成功');
  41. }
  42. util::fail('通知失败');
  43. }
  44. //根据订单id查询制作单信息 ssh 20220625
  45. public function actionGetInfoByOrderId()
  46. {
  47. $orderId = Yii::$app->request->get('orderId', 0);
  48. $order = OrderClass::getById($orderId, true);
  49. OrderClass::valid($order, $this->mainId);
  50. $info = WorkClass::getByCondition(['orderId' => $orderId], true);
  51. if (empty($info)) {
  52. util::fail('没有找到');
  53. }
  54. util::success(['info' => $info, 'order' => $order]);
  55. }
  56. //打印制作单 ssh 20220601
  57. public function actionPrint()
  58. {
  59. $get = Yii::$app->request->get();
  60. $id = $get['id'] ?? 0;
  61. $work = WorkClass::getById($id, true);
  62. WorkClass::valid($work, $this->mainId);
  63. WorkClass::print($work, $this->shop);
  64. util::complete();
  65. }
  66. //制作单列表 ssh 20220319
  67. public function actionList()
  68. {
  69. $get = Yii::$app->request->get();
  70. $where = [];
  71. $where['mainId'] = $this->mainId;
  72. $str = $get['sh'] ?? '';
  73. if ($str == 'yes') {
  74. $where['sh'] = 1;
  75. } else {
  76. $where['sh'] = 0;
  77. if (isset($get['fromType']) && is_numeric($get['fromType'])) {
  78. $where['fromType'] = $get['fromType'];
  79. } else {
  80. $shopId = $this->shopId;
  81. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  82. $mtAlone = $ext->mtAlone ?? 0;
  83. $isCashier = $get['isCashier'] ?? 0;
  84. if ($mtAlone == 1 && $isCashier == 1) {
  85. $where['fromType'] = ['in', [0, 1, 2, 3]];
  86. }
  87. }
  88. }
  89. if (isset($get['sendNum']) && !empty($get['sendNum']) && is_numeric($get['sendNum'])) {
  90. $where['sendNum'] = $get['sendNum'];
  91. }
  92. if (isset($get['thirdSn']) && !empty($get['thirdSn']) && is_numeric($get['thirdSn'])) {
  93. $where['thirdSn'] = $get['thirdSn'];
  94. }
  95. if (isset($get['status']) && $get['status'] >= 0) {
  96. $status = $get['status'];
  97. $where['status'] = $status;
  98. }
  99. $list = WorkClass::getWorkList($where);
  100. util::success($list);
  101. }
  102. //锁定用材 ssh 20220320
  103. public function actionLock()
  104. {
  105. $get = Yii::$app->request->get();
  106. $id = $get['id'] ?? 0;
  107. $work = WorkClass::getById($id, true);
  108. WorkClass::valid($work, $this->mainId);
  109. WorkItemClass::lock($work);
  110. util::complete();
  111. }
  112. //新建制作单 ssh 20220319
  113. public function actionCreate()
  114. {
  115. util::fail('请联系管理员');
  116. $post = Yii::$app->request->post();
  117. $post['mainId'] = $this->mainId ?? 0;
  118. $post['sjId'] = $this->sjId ?? 0;
  119. $post['shopId'] = $this->shopId ?? 0;
  120. $orderSn = orderSn::getWorkSn();
  121. $mainId = $this->mainId;
  122. $post['workSn'] = $orderSn;
  123. if (!empty($post['staffId'])) {
  124. $staffId = $post['staffId'];
  125. $staffName = $post['staffName'] ?? '';
  126. } else {
  127. $staffId = $this->shopAdminId;
  128. $staffName = $this->shopAdmin->name;
  129. }
  130. $post['staffId'] = $staffId;
  131. $post['staffName'] = $staffName;
  132. $post['name'] = !empty($post['name']) ? $post['name'] : '花束';
  133. $post['cover'] = !empty($post['cover']) ? $post['cover'] : 'default-img.png';
  134. $post['reachDate'] = date("Y-m-d");
  135. $connection = Yii::$app->db;
  136. $transaction = $connection->beginTransaction();
  137. try {
  138. //去掉花束所用的花材,没有意义,不考虑花束用了多少花材
  139. $post['product'] = [];
  140. $post['num'] = isset($post['num']) && $post['num'] > 0 ? $post['num'] : 1;
  141. $default = CategoryClass::getByCondition(['mainId' => $mainId, 'flower' => 1, 'default' => 1], true);
  142. if (empty($default)) {
  143. $catData = [
  144. 'mainId' => $mainId,
  145. 'sjId' => $this->sjId,
  146. 'shopId' => $this->shopId,
  147. 'default' => 1,
  148. 'flower' => 1,
  149. 'categoryName' => '未分类',
  150. 'inTurn' => 0,
  151. 'status' => 0,
  152. ];
  153. $default = CategoryClass::add($catData, true);
  154. }
  155. $catId = $default->id ?? 0;
  156. if (empty($catId)) {
  157. util::fail('没有找到分类');
  158. }
  159. $post['catId'] = $catId;
  160. $post['catName'] = $cat->categoryName ?? '';
  161. $main = $this->main;
  162. $work = WorkClass::createFinish($post, $main);
  163. $transaction->commit();
  164. $complete = $post['complete'] ?? 0;
  165. $print = $post['print'] ?? 1;
  166. if ($complete == 1 && $print == 1) {
  167. WorkClass::print($work, $this->shop, false);
  168. }
  169. util::success($work);
  170. } catch (\Exception $e) {
  171. $transaction->rollBack();
  172. Yii::error("失败原因:" . $e->getMessage());
  173. util::fail('操作失败');
  174. }
  175. }
  176. //商品新建制作 ssh 20220320
  177. public function actionGoodsCreate()
  178. {
  179. $post = Yii::$app->request->post();
  180. $post['mainId'] = $this->mainId ?? 0;
  181. $post['sjId'] = $this->sjId ?? 0;
  182. $post['shopId'] = $this->shopId ?? 0;
  183. $connection = Yii::$app->db;
  184. $transaction = $connection->beginTransaction();
  185. try {
  186. $goodsJson = $post['goods'] ?? '';
  187. $goods = json_decode($goodsJson, true);
  188. if (empty($goods)) {
  189. util::fail('没有找到商品');
  190. }
  191. $post['goods'] = $goods;
  192. $main = $this->main;
  193. WorkClass::goodsCreateFinish($post, $main);
  194. $transaction->commit();
  195. util::complete();
  196. } catch (\Exception $e) {
  197. $transaction->rollBack();
  198. Yii::error("失败原因:" . $e->getMessage());
  199. util::fail('操作失败');
  200. }
  201. }
  202. //包括制作单和用材信息 ssh 20220319
  203. public function actionFullInfo()
  204. {
  205. $get = Yii::$app->request->get();
  206. $id = $get['id'] ?? 0;
  207. $work = WorkClass::getById($id, true);
  208. WorkClass::valid($work, $this->mainId);
  209. $workItem = WorkItemClass::getWorkAllItem($work);
  210. $goods = [];
  211. $goodsId = $work->goodsId ?? 0;
  212. if (!empty($goodsId)) {
  213. $params = [];
  214. $shop = $this->shop;
  215. //这边传的是空的,有影响,后面要解决,请搜索关键词 work_if_need_custom
  216. $custom = [];
  217. $goods = GoodsClass::getGoodsInfo($goodsId, $shop, $custom, $params);
  218. }
  219. $work = $work->attributes;
  220. $shortCover = $work['cover'] ?? '';
  221. $work['shortCover'] = $shortCover;
  222. $work['cover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  223. $work['bigCover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_1000,w_1000";
  224. $work['smallCover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  225. $today = date("Y-m-d");
  226. $tomorrow = date("Y-m-d", strtotime('+1 day'));
  227. $work['today'] = $today;
  228. $work['tomorrow'] = $tomorrow;
  229. $addTime = $work['addTime'] ?? 0;
  230. $currentTime = strtotime($addTime) + 60;
  231. $diffTime = bcsub($currentTime, time());
  232. $beginCount = $diffTime > 0 ? $diffTime : 0;
  233. $work['beginCount'] = $beginCount;
  234. util::success(['info' => $work, 'item' => $workItem, 'goods' => $goods]);
  235. }
  236. //取消 ssh 20220320
  237. public function actionCancel()
  238. {
  239. $get = Yii::$app->request->get();
  240. $id = $get['id'] ?? 0;
  241. $work = WorkClass::getById($id, true);
  242. WorkClass::valid($work, $this->mainId);
  243. WorkClass::cancel($work);
  244. util::complete();
  245. }
  246. //完成制作单 ssh 20220319
  247. public function actionFinish()
  248. {
  249. $post = Yii::$app->request->post();
  250. $workId = $post['id'] ?? 0;
  251. $connection = Yii::$app->db;
  252. $transaction = $connection->beginTransaction();
  253. try {
  254. $work = WorkClass::getById($workId, true);
  255. WorkClass::valid($work, $this->mainId);
  256. if ($work->status == 1) {
  257. util::fail('制作单已完成');
  258. }
  259. if ($work->status == 2) {
  260. util::fail('制作单已取消');
  261. }
  262. //花材未锁定,需要锁定并扣库存
  263. if ($work->stockLock == 0) {
  264. $itemList = $post['itemList'] ?? '';
  265. if (!empty($itemList)) {
  266. $product = json_decode($itemList, true);
  267. if (!empty($product)) {
  268. WorkItemClass::modifyItem($work, $product);
  269. WorkItemClass::lock($work);
  270. }
  271. }
  272. }
  273. $staffId = $post['staffId'] ?? 0;
  274. if (empty($staffId)) {
  275. util::fail('请选择制作人');
  276. }
  277. $staffName = $post['staffName'] ?? '';
  278. $staffData = ['staffId' => $staffId, 'staffName' => $staffName];
  279. WorkClass::finish($work, $staffData);
  280. $transaction->commit();
  281. //美团的暂时不打小票
  282. if ($work->fromType != 4) {
  283. WorkClass::print($work, $this->shop, false);
  284. }
  285. $msg = '已完成';
  286. $orderId = $work->orderId ?? 0;
  287. if (!empty($orderId) && $work->fromType == dict::getDict('fromType', 'mt')) {
  288. $allWork = WorkClass::getAllByCondition(['orderId' => $orderId], null, '*', null, true);
  289. //订单下如果还有制作单未完成,则不通知美图已经完成拣货
  290. $allComplete = true;
  291. if (!empty($allWork)) {
  292. foreach ($allWork as $current) {
  293. if ($current->status != 1) {
  294. $allComplete = false;
  295. }
  296. }
  297. }
  298. if ($allComplete) {
  299. $order = OrderClass::getById($orderId, true);
  300. $thirdOrderId = $order->thirdOrderId ?? '';
  301. $fromType = $order->fromType ?? 0;
  302. if (!empty($thirdOrderId) && $fromType == dict::getDict('fromType', 'mt')) {
  303. // $shopId = $order->shopId ?? 0;
  304. // $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  305. // $config = dict::getDict('mtConfig');
  306. // $app = new Application($config);
  307. // $accessToken = mtUtil::getAccessToken($ext);
  308. // $params = ['order_id' => $thirdOrderId, 'access_token' => $accessToken];
  309. // $mtRes = $app->order->logisticsPush($params);
  310. // $mtRes = json_decode($mtRes, true);
  311. // if (isset($mtRes['data']) && $mtRes['data'] == 'ok') {
  312. // $msg = '已完成,已通知骑手';
  313. // }
  314. }
  315. }
  316. }
  317. util::complete($msg);
  318. } catch (\Exception $e) {
  319. $transaction->rollBack();
  320. Yii::error("失败原因:" . $e->getMessage());
  321. util::fail('操作失败');
  322. }
  323. }
  324. }