WorkController.php 6.9 KB

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