WorkController.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace hd\controllers;
  3. use bizHd\goods\classes\CategoryClass;
  4. use bizHd\goods\classes\GoodsClass;
  5. use bizHd\order\classes\OrderClass;
  6. use bizHd\work\classes\WorkClass;
  7. use bizHd\work\classes\WorkItemClass;
  8. use common\components\dict;
  9. use common\components\imgUtil;
  10. use common\components\orderSn;
  11. use common\components\util;
  12. use Yii;
  13. class WorkController extends BaseController
  14. {
  15. public $guestAccess = [];
  16. //根据订单id查询制作单信息 ssh 20220625
  17. public function actionGetInfoByOrderId()
  18. {
  19. $orderId = Yii::$app->request->get('orderId', 0);
  20. $order = OrderClass::getById($orderId, true);
  21. OrderClass::valid($order, $this->mainId);
  22. $info = WorkClass::getByCondition(['orderId' => $orderId], true);
  23. if (empty($info)) {
  24. util::fail('没有找到');
  25. }
  26. util::success(['info' => $info]);
  27. }
  28. //打印制作单 ssh 20220601
  29. public function actionPrint()
  30. {
  31. $get = Yii::$app->request->get();
  32. $id = $get['id'] ?? 0;
  33. $work = WorkClass::getById($id, true);
  34. WorkClass::valid($work, $this->mainId);
  35. WorkClass::print($work, $this->shop);
  36. util::complete();
  37. }
  38. //制作单列表 ssh 20220319
  39. public function actionList()
  40. {
  41. $get = Yii::$app->request->get();
  42. $where = [];
  43. $where['mainId'] = $this->mainId;
  44. if (isset($get['status']) && $get['status'] >= 0) {
  45. $status = $get['status'];
  46. $where['status'] = $status;
  47. }
  48. if (isset($get['fromType']) && is_numeric($get['fromType'])) {
  49. //$where['fromType'] = $get['fromType'];
  50. }
  51. if (isset($get['searchType'])) {
  52. if ($get['searchType'] == 0) {
  53. util::fail('暂不支持名称搜索');
  54. } elseif ($get['searchType'] == 1) {
  55. if (!empty($get['name'])) {
  56. if (!is_numeric($get['name'])) {
  57. util::fail('编号必须是数字');
  58. }
  59. $where['sn'] = $get['name'];
  60. }
  61. }
  62. }
  63. $list = WorkClass::getWorkList($where);
  64. $list['today'] = date("Y-m-d");
  65. util::success($list);
  66. }
  67. //锁定用材 ssh 20220320
  68. public function actionLock()
  69. {
  70. $get = Yii::$app->request->get();
  71. $id = $get['id'] ?? 0;
  72. $work = WorkClass::getById($id, true);
  73. WorkClass::valid($work, $this->mainId);
  74. WorkItemClass::lock($work);
  75. util::complete();
  76. }
  77. //新建制作单 ssh 20220319
  78. public function actionCreate()
  79. {
  80. $post = Yii::$app->request->post();
  81. $post['mainId'] = $this->mainId ?? 0;
  82. $post['sjId'] = $this->sjId ?? 0;
  83. $post['shopId'] = $this->shopId ?? 0;
  84. $orderSn = orderSn::getWorkSn();
  85. $post['workSn'] = $orderSn;
  86. $post['staffId'] = $this->shopAdminId;
  87. $post['staffName'] = $this->shopAdmin->name;
  88. $post['name'] = isset($post['name']) && !empty($post['name']) ? $post['name'] : '花束';
  89. $post['cover'] = isset($post['cover']) && !empty($post['cover']) ? $post['cover'] : 'default-img.png';
  90. $connection = Yii::$app->db;
  91. $transaction = $connection->beginTransaction();
  92. try {
  93. $product = $post['product'] ?? '';
  94. $post['product'] = [];
  95. if (!empty($product)) {
  96. $arr = json_decode($product, true);
  97. if (!empty($arr)) {
  98. $post['product'] = $arr;
  99. }
  100. }
  101. $post['num'] = isset($post['num']) && $post['num'] > 0 ? $post['num'] : 1;
  102. $catId = $post['catId'] ?? 0;
  103. $cat = CategoryClass::getById($catId, true);
  104. if ($cat->mainId != $this->mainId) {
  105. util::fail('请选择你自己的分类');
  106. }
  107. $post['catName'] = $cat->categoryName ?? '';
  108. $main = $this->main;
  109. $work = WorkClass::createFinish($post, $main);
  110. $transaction->commit();
  111. $complete = $post['complete'] ?? 0;
  112. if ($complete == 1) {
  113. WorkClass::print($work, $this->shop, false);
  114. }
  115. util::success($work);
  116. } catch (\Exception $e) {
  117. $transaction->rollBack();
  118. Yii::error("失败原因:" . $e->getMessage());
  119. util::fail('操作失败');
  120. }
  121. }
  122. //商品新建制作 ssh 20220320
  123. public function actionGoodsCreate()
  124. {
  125. $post = Yii::$app->request->post();
  126. $post['mainId'] = $this->mainId ?? 0;
  127. $post['sjId'] = $this->sjId ?? 0;
  128. $post['shopId'] = $this->shopId ?? 0;
  129. $connection = Yii::$app->db;
  130. $transaction = $connection->beginTransaction();
  131. try {
  132. $goodsJson = $post['goods'] ?? '';
  133. $goods = json_decode($goodsJson, true);
  134. if (empty($goods)) {
  135. util::fail('没有找到商品');
  136. }
  137. $post['goods'] = $goods;
  138. $main = $this->main;
  139. WorkClass::goodsCreateFinish($post, $main);
  140. $transaction->commit();
  141. util::complete();
  142. } catch (\Exception $e) {
  143. $transaction->rollBack();
  144. Yii::error("失败原因:" . $e->getMessage());
  145. util::fail('操作失败');
  146. }
  147. }
  148. //包括制作单和用材信息 ssh 20220319
  149. public function actionFullInfo()
  150. {
  151. $get = Yii::$app->request->get();
  152. $id = $get['id'] ?? 0;
  153. $work = WorkClass::getById($id, true);
  154. WorkClass::valid($work, $this->mainId);
  155. $workItem = WorkItemClass::getWorkAllItem($work);
  156. $goods = [];
  157. $goodsId = $work->goodsId ?? 0;
  158. if (!empty($goodsId)) {
  159. $goods = GoodsClass::getGoodsInfo($goodsId);
  160. }
  161. $work = $work->attributes;
  162. $shortCover = $work['cover'] ?? '';
  163. $work['shortCover'] = $shortCover;
  164. $work['cover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  165. $work['bigCover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  166. $work['smallCover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  167. $today = date("Y-m-d");
  168. util::success(['info' => $work, 'item' => $workItem, 'goods' => $goods, 'today' => $today]);
  169. }
  170. //取消 ssh 20220320
  171. public function actionCancel()
  172. {
  173. $get = Yii::$app->request->get();
  174. $id = $get['id'] ?? 0;
  175. $work = WorkClass::getById($id, true);
  176. WorkClass::valid($work, $this->mainId);
  177. WorkClass::cancel($work);
  178. util::complete();
  179. }
  180. //完成制作单 ssh 20220319
  181. public function actionFinish()
  182. {
  183. $post = Yii::$app->request->post();
  184. $workId = $post['id'] ?? 0;
  185. $connection = Yii::$app->db;//事务处理
  186. $transaction = $connection->beginTransaction();
  187. try {
  188. $work = WorkClass::getById($workId, true);
  189. WorkClass::valid($work, $this->mainId);
  190. if ($work->status == 1) {
  191. util::fail('制作单已完成');
  192. }
  193. if ($work->status == 2) {
  194. util::fail('制作单已取消');
  195. }
  196. //花材未锁定,需要锁定并扣库存
  197. if ($work->stockLock == 0) {
  198. $itemList = $post['itemList'] ?? '';
  199. if (!empty($itemList)) {
  200. $product = json_decode($itemList, true);
  201. if (!empty($product)) {
  202. WorkItemClass::modifyItem($work, $product);
  203. WorkItemClass::lock($work);
  204. }
  205. }
  206. }
  207. WorkClass::finish($work);
  208. $transaction->commit();
  209. //美团的订单完成时不需要打标签
  210. if ($work->fromType != dict::getDict('fromType', 'mt')) {
  211. WorkClass::print($work, $this->shop, false);
  212. }
  213. util::complete();
  214. } catch (\Exception $e) {
  215. $transaction->rollBack();
  216. Yii::error("失败原因:" . $e->getMessage());
  217. util::fail('操作失败');
  218. }
  219. }
  220. }