WorkController.php 12 KB

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