WorkController.php 8.5 KB

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