WorkController.php 7.7 KB

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