WorkController.php 8.6 KB

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