WorkController.php 12 KB

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