PurchaseOrderController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\admin\classes\AdminRoleClass;
  4. use biz\ghs\classes\GhsClass;
  5. use bizGhs\order\classes\PurchaseOrderClass;
  6. use bizGhs\order\classes\PurchaseOrderItemClass;
  7. use bizGhs\product\classes\ProductClass;
  8. use common\components\noticeUtil;
  9. use common\components\printUtil;
  10. use common\components\util;
  11. use Yii;
  12. class PurchaseOrderController extends BaseController
  13. {
  14. //打印全部 ssh 20220602
  15. public function actionPrintAll()
  16. {
  17. $get = Yii::$app->request->get();
  18. $orderSn = $get['orderSn'] ?? 0;
  19. $type = $get['type'] ?? 0;
  20. $itemList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  21. if (empty($itemList)) {
  22. util::fail('没有商品');
  23. }
  24. $order = PurchaseOrderClass::getByCondition(['orderSn' => $orderSn], true);
  25. $mainId = $order->mainId ?? 0;
  26. $ext = $this->shopExt;
  27. $printLabelSn = $ext->printLabelSn ?? '';
  28. if (empty($printLabelSn)) {
  29. util::fail('请设置标签打印机');
  30. }
  31. $p = new printUtil($printLabelSn);
  32. foreach ($itemList as $key => $item) {
  33. $name = $item->name ?? '';
  34. $num = !empty($item->itemNum) ? floatval($item->itemNum) : 0;
  35. if (empty($num)) {
  36. util::fail('没有花材数量');
  37. }
  38. $ptItemId = $item->itemId ?? '';
  39. $productId = $item->productId ?? 0;
  40. $p->times = $num;
  41. $smallUnit = $item->smallUnit ?? '';
  42. $bigUnit = $item->bigUnit ?? '';
  43. $ratio = $item->ratio ?? 0;
  44. $unit = $ratio . $smallUnit . '/' . $bigUnit;
  45. $content = '<TEXT x="30" y="30" font="12" w="1" h="1" r="0">' . $name . '</TEXT>';
  46. if ($key % 2 == 1) {
  47. $content .= '<TEXT x="415" y="20" font="12" w="2" h="2" r="0">.</TEXT>';
  48. }
  49. if ($type == 0) {
  50. $content .= '<BC128 x="30" y="100" h="70" s="1" r="0" n="4" w="11">' . $ptItemId . '</BC128>';
  51. if (in_array($mainId, [52, 1459])) {
  52. $content .= '<TEXT x="30" y="250" font="12" w="2" h="2" r="0">惠雅鲜花</TEXT>';
  53. } else {
  54. $content .= '<TEXT x="30" y="250" font="12" w="2" h="2" r="0">' . $unit . '</TEXT>';
  55. }
  56. } else {
  57. $content .= '<QR x="35" y="100" e="L" w="5">' . Yii::$app->params['mallHost'] . "/item/show-info?id=" . $productId . '</QR>';
  58. $content .= '<TEXT x="40" y="270" font="12" w="1" h="1" r="0">扫码看价格</TEXT>';
  59. }
  60. $p->printLabelMsg($content);
  61. }
  62. util::complete();
  63. }
  64. //打印全部 ssh 20220
  65. public function actionPrintItem()
  66. {
  67. $get = Yii::$app->request->get();
  68. $id = $get['id'] ?? 0;
  69. $type = $get['type'] ?? 0;
  70. $item = PurchaseOrderItemClass::getById($id, true);
  71. if (empty($item)) {
  72. util::fail('没有找到花材');
  73. }
  74. $name = $item->name ?? '';
  75. $num = !empty($item->itemNum) ? floatval($item->itemNum) : 0;
  76. if (empty($num)) {
  77. util::fail('没有花材数量');
  78. }
  79. $orderSn = $item->orderSn ?? '';
  80. $order = PurchaseOrderClass::getByCondition(['orderSn' => $orderSn], true);
  81. $mainId = $order->mainId ?? 0;
  82. $ext = $this->shopExt;
  83. $printLabelSn = $ext->printLabelSn ?? '';
  84. if (empty($printLabelSn)) {
  85. util::fail('请设置标签打印机');
  86. }
  87. $p = new printUtil($printLabelSn);
  88. $productId = $item->productId ?? 0;
  89. $product = ProductClass::getById($productId, true);
  90. $smallUnit = $product->smallUnit ?? '';
  91. $bigUnit = $product->bigUnit ?? '';
  92. $ratio = $product->ratio ?? 20;
  93. $ratioType = $product->ratioType ?? 0;
  94. if ($ratioType == 1) {
  95. $unit = '若干' . $smallUnit . '/' . $bigUnit;
  96. } else {
  97. $unit = $ratio . $smallUnit . '/' . $bigUnit;
  98. }
  99. $ptItemId = $item->itemId ?? '';
  100. $p->times = $num;
  101. $content = '<TEXT x="30" y="30" font="12" w="1" h="1" r="0">' . $name . '</TEXT>';
  102. if ($type == 0) {
  103. $content .= '<BC128 x="30" y="100" h="70" s="1" r="0" n="4" w="11">' . $ptItemId . '</BC128>';
  104. if (in_array($mainId, [52, 1459])) {
  105. $content .= '<TEXT x="30" y="250" font="12" w="2" h="2" r="0">惠雅鲜花</TEXT>';
  106. } else {
  107. $content .= '<TEXT x="30" y="250" font="12" w="2" h="2" r="0">' . $unit . '</TEXT>';
  108. }
  109. } else {
  110. $content .= '<QR x="35" y="100" e="L" w="5">' . Yii::$app->params['mallHost'] . "/item/show-info?id=" . $productId . '</QR>';
  111. $content .= '<TEXT x="40" y="270" font="12" w="1" h="1" r="0">扫码看价格</TEXT>';
  112. }
  113. $p->printLabelMsg($content);
  114. util::complete();
  115. }
  116. //取消入库单 ssh 20221211
  117. public function actionCancel()
  118. {
  119. $get = Yii::$app->request->get();
  120. $id = $get['id'] ?? 0;
  121. $cg = PurchaseOrderClass::getLockById($id);
  122. if (isset($cg->mainId) == false || $cg->mainId != $this->mainId) {
  123. util::fail('请确认是否您的采购单');
  124. }
  125. if ($cg->status == PurchaseOrderClass::PURCHASE_ORDER_STATUS_CANCEL) {
  126. util::fail('已取消过了');
  127. }
  128. if ($cg->status == PurchaseOrderClass::PURCHASE_ORDER_STATUS_COMPLETE) {
  129. util::fail('已入库,无法取消');
  130. }
  131. $cg->status = PurchaseOrderClass::PURCHASE_ORDER_STATUS_CANCEL;
  132. $cg->save();
  133. util::complete();
  134. }
  135. //确认入库 sssh 20221211
  136. public function actionConfirmPutIn()
  137. {
  138. ini_set('memory_limit', '2045M');
  139. set_time_limit(0);
  140. $shopAdmin = $this->shopAdmin;
  141. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  142. util::fail('超管才能修改');
  143. }
  144. $connection = Yii::$app->db;
  145. $transaction = $connection->beginTransaction();
  146. try {
  147. $get = Yii::$app->request->get();
  148. $id = $get['id'] ?? 0;
  149. $cg = PurchaseOrderClass::getLockById($id);
  150. if (isset($cg->mainId) == false || $cg->mainId != $this->mainId) {
  151. util::fail('请确认是否您的采购单');
  152. }
  153. $staff = $this->shopAdmin;
  154. $data = [];
  155. $data['staffId'] = $staff->id ?? 0;
  156. $data['staffName'] = $staff->name ?? '';
  157. $data['adminId'] = $this->adminId ?? 0;
  158. $return = PurchaseOrderClass::putIn($cg, $data);
  159. $transaction->commit();
  160. util::success($return);
  161. } catch (\Exception $e) {
  162. $transaction->rollBack();
  163. noticeUtil::push('确认入库失败,原因:' . $e->getMessage(), '15280215347');
  164. Yii::error("确认入库失败,原因:" . $e->getMessage());
  165. util::fail('确认失败');
  166. }
  167. }
  168. //新增采购订单
  169. public function actionCreateOrder()
  170. {
  171. ini_set('memory_limit', '2045M');
  172. set_time_limit(0);
  173. $post = Yii::$app->request->post();
  174. $post['sjId'] = $this->sjId;
  175. $post['shopId'] = $this->shopId;
  176. $post['adminId'] = $this->adminId;
  177. $post['mainId'] = $this->mainId;
  178. $staff = $this->shopAdmin;
  179. $post['staffId'] = $staff->id ?? 0;
  180. $post['staffName'] = $staff->name ?? '';
  181. $shopAdmin = $this->shopAdmin;
  182. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  183. util::fail('超管才能修改');
  184. }
  185. $shop = $this->shop;
  186. $carSale = $shop->carSale ?? 0;
  187. if ($carSale == 1) {
  188. util::fail('车销不能采购');
  189. }
  190. $connection = Yii::$app->db;
  191. $transaction = $connection->beginTransaction();
  192. try {
  193. // [{productId:0,itemPrice:1,bigNum:1,smallNum:0}]
  194. $ghsItemInfo = $post['itemInfo'] ?? '';
  195. if (empty($ghsItemInfo)) {
  196. util::fail('请选择花材');
  197. }
  198. $ghsItemInfo = json_decode($ghsItemInfo, true);
  199. if (!is_array($ghsItemInfo)) {
  200. util::fail('花材格式不正确');
  201. }
  202. //合并
  203. $ghsItemInfo = PurchaseOrderClass::mergeItemInfo($ghsItemInfo);
  204. //判断花材格式,是否属于当前门店
  205. ProductClass::valid($ghsItemInfo, $this->mainId);
  206. //获取供货商信息
  207. $ghsId = $post['ghsId'];
  208. if (empty($ghsId)) {
  209. util::fail("请选择供货商");
  210. }
  211. $ghsInfo = GhsClass::getGhsInfo($ghsId);
  212. GhsClass::valid($ghsInfo, $this->shopId);
  213. $post['ghsInfo'] = json_encode($ghsInfo);
  214. $post['ghsName'] = $ghsInfo['name'] ?? '';
  215. $post['ghsAvatar'] = $ghsInfo['shortAvatar'] ?? '';
  216. //判断花材里的信息
  217. foreach ($ghsItemInfo as $v) {
  218. $bigNum = $v['bigNum'] ?? 0;
  219. $productId = $v['productId'] ?? 0;
  220. $smallNum = $v['smallNum'] ?? 0;
  221. if (!isset($v['itemPrice'])) {
  222. util::fail('采购价格不能为空');
  223. }
  224. if (!$productId) {
  225. util::fail('花材不存在');
  226. }
  227. if ($bigNum <= 0 && $smallNum <= 0) {
  228. util::fail('花材数量不能为0');
  229. }
  230. }
  231. $post['itemInfo'] = $ghsItemInfo;
  232. //0稍后入库 1直接入库
  233. $inType = $post['inType'] ?? PurchaseOrderClass::IN_TYPE_NOW;
  234. $debtStatus = $inType == 1 ? PurchaseOrderClass::DEBT_YES : PurchaseOrderClass::DEBT_UNKNOWN;
  235. $post['debt'] = $debtStatus;
  236. $order = PurchaseOrderClass::addOrder($post);
  237. $transaction->commit();
  238. util::success($order);
  239. } catch (\Exception $e) {
  240. $transaction->rollBack();
  241. noticeUtil::push('批发店采购入库失败,原因:' . $e->getMessage(), '15280215347');
  242. Yii::error("操作失败原因:" . $e->getMessage());
  243. util::fail('操作失败');
  244. }
  245. }
  246. //订单列表
  247. public function actionList()
  248. {
  249. if (getenv('YII_ENV') == 'production') {
  250. //阿东的创始人才能查看采购单
  251. if (in_array($this->mainId, [742])) {
  252. $staff = $this->shopAdmin;
  253. if ($staff->founder != 2 && in_array($staff->id, [126647]) == false) {
  254. util::fail('没有权限');
  255. }
  256. }
  257. }
  258. $get = Yii::$app->request->get();
  259. $orderStatus = $get['status'] ?? '';
  260. if ($orderStatus) {
  261. $where['status'] = $orderStatus;
  262. }
  263. $ghsId = $get['ghsId'] ?? 0;
  264. if (!empty($ghsId)) {
  265. $where['ghsId'] = $ghsId;
  266. }
  267. $where['sjId'] = $this->sjId;
  268. $where['shopId'] = $this->shopId;
  269. $data = PurchaseOrderClass::getOrderList($where);
  270. //状态统计,很慢,暂时隐藏
  271. //$statData = PurchaseOrderClass::statNum($this->shopId);
  272. $statData = [
  273. 'allNum' => 0,
  274. 'unPayNum' => 0,
  275. 'unSendNum' => 0,
  276. 'sendingNum' => 0,
  277. 'finishNum' => 0,
  278. ];
  279. $data = array_merge($data, $statData);
  280. util::success($data);
  281. }
  282. //订单详情
  283. public function actionDetail()
  284. {
  285. if (getenv('YII_ENV') == 'production') {
  286. //阿东的创始人才能查看采购单
  287. if (in_array($this->mainId, [742])) {
  288. $staff = $this->shopAdmin;
  289. if ($staff->founder != 2 && in_array($staff->id, [126647]) == false) {
  290. util::fail('没有权限');
  291. }
  292. }
  293. }
  294. $orderSn = Yii::$app->request->get('orderSn', '');
  295. $orderData = PurchaseOrderClass::getOrderDetail($orderSn, $this->shopId);
  296. util::success($orderData);
  297. }
  298. //修改备注
  299. public function actionUpdateRemark()
  300. {
  301. $remark = Yii::$app->request->post('remark', '');
  302. $id = Yii::$app->request->post('id', '');
  303. $cg = PurchaseOrderClass::getById($id, true);
  304. if (empty($cg)) {
  305. util::fail('没有找到订单');
  306. }
  307. if ($cg->shopId != $this->shopId) {
  308. util::fail('没有权限修改');
  309. }
  310. $cg->remark = $remark;
  311. $cg->save();
  312. util::complete('修改成功');
  313. }
  314. }