WorkController.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. $list = WorkClass::getWorkList($where);
  49. $list['today'] = date("Y-m-d");
  50. util::success($list);
  51. }
  52. //锁定用材 ssh 20220320
  53. public function actionLock()
  54. {
  55. $get = Yii::$app->request->get();
  56. $id = $get['id'] ?? 0;
  57. $work = WorkClass::getById($id, true);
  58. WorkClass::valid($work, $this->mainId);
  59. WorkItemClass::lock($work);
  60. util::complete();
  61. }
  62. //新建制作单 ssh 20220319
  63. public function actionCreate()
  64. {
  65. $post = Yii::$app->request->post();
  66. $post['mainId'] = $this->mainId ?? 0;
  67. $post['sjId'] = $this->sjId ?? 0;
  68. $post['shopId'] = $this->shopId ?? 0;
  69. $orderSn = orderSn::getWorkSn();
  70. $post['workSn'] = $orderSn;
  71. $post['staffId'] = $this->shopAdminId;
  72. $post['staffName'] = $this->shopAdmin->name;
  73. $post['name'] = isset($post['name']) && !empty($post['name']) ? $post['name'] : '花束';
  74. $post['cover'] = isset($post['cover']) && !empty($post['cover']) ? $post['cover'] : 'default-img.png';
  75. $connection = Yii::$app->db;
  76. $transaction = $connection->beginTransaction();
  77. try {
  78. $product = $post['product'] ?? '';
  79. $post['product'] = [];
  80. if (!empty($product)) {
  81. $arr = json_decode($product, true);
  82. if (!empty($arr)) {
  83. $post['product'] = $arr;
  84. }
  85. }
  86. $post['num'] = isset($post['num']) && $post['num'] > 0 ? $post['num'] : 1;
  87. $catId = $post['catId'] ?? 0;
  88. $cat = CategoryClass::getById($catId, true);
  89. if ($cat->mainId != $this->mainId) {
  90. util::fail('请选择你自己的分类');
  91. }
  92. $post['catName'] = $cat->categoryName ?? '';
  93. $main = $this->main;
  94. $work = WorkClass::createFinish($post, $main);
  95. $transaction->commit();
  96. $complete = $post['complete'] ?? 0;
  97. if ($complete == 1) {
  98. WorkClass::print($work, $this->shop, false);
  99. }
  100. util::success($work);
  101. } catch (\Exception $e) {
  102. $transaction->rollBack();
  103. Yii::error("失败原因:" . $e->getMessage());
  104. util::fail('操作失败');
  105. }
  106. }
  107. //商品新建制作 ssh 20220320
  108. public function actionGoodsCreate()
  109. {
  110. $post = Yii::$app->request->post();
  111. $post['mainId'] = $this->mainId ?? 0;
  112. $post['sjId'] = $this->sjId ?? 0;
  113. $post['shopId'] = $this->shopId ?? 0;
  114. $connection = Yii::$app->db;
  115. $transaction = $connection->beginTransaction();
  116. try {
  117. $goodsJson = $post['goods'] ?? '';
  118. $goods = json_decode($goodsJson, true);
  119. if (empty($goods)) {
  120. util::fail('没有找到商品');
  121. }
  122. $post['goods'] = $goods;
  123. $main = $this->main;
  124. WorkClass::goodsCreateFinish($post, $main);
  125. $transaction->commit();
  126. util::complete();
  127. } catch (\Exception $e) {
  128. $transaction->rollBack();
  129. Yii::error("失败原因:" . $e->getMessage());
  130. util::fail('操作失败');
  131. }
  132. }
  133. //包括制作单和用材信息 ssh 20220319
  134. public function actionFullInfo()
  135. {
  136. $get = Yii::$app->request->get();
  137. $id = $get['id'] ?? 0;
  138. $work = WorkClass::getById($id, true);
  139. WorkClass::valid($work, $this->mainId);
  140. $workItem = WorkItemClass::getWorkAllItem($work);
  141. $goods = [];
  142. $goodsId = $work->goodsId ?? 0;
  143. if (!empty($goodsId)) {
  144. $goods = GoodsClass::getGoodsInfo($goodsId);
  145. }
  146. $work = $work->attributes;
  147. $shortCover = $work['cover'] ?? '';
  148. $work['shortCover'] = $shortCover;
  149. $work['cover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  150. $work['bigCover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  151. $work['smallCover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  152. $today = date("Y-m-d");
  153. util::success(['info' => $work, 'item' => $workItem, 'goods' => $goods, 'today' => $today]);
  154. }
  155. //取消 ssh 20220320
  156. public function actionCancel()
  157. {
  158. $get = Yii::$app->request->get();
  159. $id = $get['id'] ?? 0;
  160. $work = WorkClass::getById($id, true);
  161. WorkClass::valid($work, $this->mainId);
  162. WorkClass::cancel($work);
  163. util::complete();
  164. }
  165. //完成制作单 ssh 20220319
  166. public function actionFinish()
  167. {
  168. $post = Yii::$app->request->post();
  169. $workId = $post['id'] ?? 0;
  170. $connection = Yii::$app->db;//事务处理
  171. $transaction = $connection->beginTransaction();
  172. try {
  173. $work = WorkClass::getById($workId, true);
  174. WorkClass::valid($work, $this->mainId);
  175. if ($work->status == 1) {
  176. util::fail('制作单已完成');
  177. }
  178. if ($work->status == 2) {
  179. util::fail('制作单已取消');
  180. }
  181. //花材未锁定,需要锁定并扣库存
  182. if ($work->stockLock == 0) {
  183. $itemList = $post['itemList'] ?? '';
  184. if (!empty($itemList)) {
  185. $product = json_decode($itemList, true);
  186. if (!empty($product)) {
  187. WorkItemClass::modifyItem($work, $product);
  188. WorkItemClass::lock($work);
  189. }
  190. }
  191. }
  192. WorkClass::finish($work);
  193. $transaction->commit();
  194. //美团的订单完成时不需要打标签
  195. if ($work->fromType != dict::getDict('fromType', 'mt')) {
  196. WorkClass::print($work, $this->shop, false);
  197. }
  198. util::complete();
  199. } catch (\Exception $e) {
  200. $transaction->rollBack();
  201. Yii::error("失败原因:" . $e->getMessage());
  202. util::fail('操作失败');
  203. }
  204. }
  205. }