PurchaseOrderController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\ghs\classes\GhsClass;
  4. use biz\sj\classes\SjClass;
  5. use bizGhs\order\classes\PurchaseOrderClass;
  6. use bizGhs\order\classes\PurchaseOrderItemClass;
  7. use bizGhs\product\classes\ProductClass;
  8. use bizGhs\shop\classes\ShopClass;
  9. use bizHd\purchase\classes\PurchaseClass;
  10. use common\components\dict;
  11. use common\components\noticeUtil;
  12. use common\components\printUtil;
  13. use common\components\stringUtil;
  14. use common\components\util;
  15. use biz\shop\classes\ShopExtClass;
  16. use Yii;
  17. class PurchaseOrderController extends BaseController
  18. {
  19. //打印多联 ssh 2024509
  20. public function actionPrintPaper()
  21. {
  22. $get = Yii::$app->request->get();
  23. $id = $get['id'] ?? 0;
  24. $cg = PurchaseOrderClass::getById($id, true);
  25. if (empty($cg)) {
  26. util::fail('没有找到采购信息');
  27. }
  28. if ($cg->mainId != $this->mainId) {
  29. util::fail('不是你的采购单哦');
  30. }
  31. if ($cg->status != 4) {
  32. util::fail('已入库的订单才能打印');
  33. }
  34. $orderSn = $cg->orderSn ?? '';
  35. $itemList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  36. $table = [];
  37. $totalItemNum = 0;
  38. $kind = 0;
  39. if (!empty($itemList)) {
  40. foreach ($itemList as $key => $val) {
  41. $kind++;
  42. $currentName = $val['name'] ?? '';
  43. $preNum = $val['itemNum'] ?? 0;
  44. $preNum = floatval($preNum);
  45. $refundNum = $val['refundNum'] ?? 0;
  46. $num = bcsub($preNum, $refundNum);
  47. $totalItemNum = bcadd($totalItemNum, $num);
  48. $unitPrice = $val['bigPrice'] ?? 0;
  49. $unitPrice = floatval($unitPrice);
  50. $orderNum = bcadd($key, 1);
  51. $currentPrice = $val['totalPrice'] ?? 0;
  52. $currentPrice = floatval($currentPrice);
  53. $table[] = [
  54. 'orderNum' => $orderNum,
  55. 'name' => $currentName,
  56. 'num' => $preNum,
  57. 'unitPrice' => '¥' . $unitPrice,
  58. 'refundNum' => $refundNum,
  59. 'remark' => '',
  60. 'price' => '¥' . $currentPrice,
  61. ];
  62. }
  63. }
  64. $shortOrderSn = substr($orderSn, -4);
  65. $ghsName = $cg->ghsName ?? '';
  66. $entryTime = $cg->entryTime ?? '';
  67. $entryTime = substr($entryTime, 0, 16);
  68. $addTime = $cg->addTime ?? '';
  69. $addTime = substr($addTime, 0, 16);
  70. $shopAdminName = $cg->shopAdminName ?? '';
  71. $shopId = $cg->shopId ?? 0;
  72. $shop = ShopClass::getById($shopId, true);
  73. $telephone = $shop->telephone ?? '';
  74. $shopName = $shop->shopName ?? '';
  75. $sjId = $shop->sjId ?? 0;
  76. $sj = SjClass::getById($sjId, true);
  77. $sjName = $sj->name ?? '';
  78. $sjShopName = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  79. $prePrice = $cg->prePrice ?? 0;
  80. $prePrice = floatval($prePrice);
  81. $tkPrice = $cg->tkPrice ?? 0;
  82. $tkPrice = floatval($tkPrice);
  83. $realPrice = $cg->realPrice ?? 0;
  84. $realPrice = floatval($realPrice);
  85. $printData = [
  86. 'table' => $table,
  87. 'addTime' => $addTime,
  88. 'entryTime' => $entryTime,
  89. 'ghsName' => $ghsName,
  90. 'clearCode' => $shortOrderSn,
  91. 'staffName' => $shopAdminName,
  92. 'telephone' => $telephone,
  93. 'kind' => $kind,
  94. 'orderSn' => $orderSn,
  95. 'sjShopName' => $sjShopName . ' 采购单',
  96. 'prePrice' => '¥' . $prePrice,
  97. 'tkPrice' => '¥' . $tkPrice,
  98. 'realPrice' => '¥' . $realPrice,
  99. 'totalItemNum' => $totalItemNum,
  100. 'numKind' => "共" . $kind . "种,数量:" . $totalItemNum,
  101. ];
  102. $template = '{"panels":[{"index":0,"name":1,"height":147.6,"width":210,"paperHeader":49.5,"paperFooter":387.63636363636357,"printElements":[{"options":{"left":17.5,"top":60,"height":36,"width":550,"field":"table","coordinateSync":false,"widthHeightSync":false,"textAlign":"center","columns":[[{"width":275,"title":"名称","field":"name","checked":true,"columnId":"name","fixed":false,"rowspan":1,"colspan":1},{"width":275,"title":"价格","field":"price","checked":true,"columnId":"price","fixed":false,"rowspan":1,"colspan":1}]]},"printElementType":{"title":"空白表格","type":"table","editable":true,"columnDisplayEditable":true,"columnDisplayIndexEditable":true,"columnTitleEditable":true,"columnResizable":true,"columnAlignEditable":true,"isEnableEditField":true,"isEnableContextMenu":true,"isEnableInsertRow":true,"isEnableDeleteRow":true,"isEnableInsertColumn":true,"isEnableDeleteColumn":true,"isEnableMergeCell":true}}],"paperNumberLeft":565.5,"paperNumberTop":396,"paperNumberContinue":true,"orient":1,"watermarkOptions":{"content":"vue-plugin-hiprint","rotate":25,"timestamp":true,"format":"YYYY-MM-DD HH:mm","fillStyle":"rgba(184, 184, 184, 0.3)","fontSize":"14px","width":200,"height":200},"panelLayoutOptions":{"layoutType":"row","layoutRowGap":0,"layoutColumnGap":0}}]}';
  103. util::success(['template' => $template, 'printData' => $printData]);
  104. }
  105. //修改货位号 ssh 20240410
  106. public function actionModifySeatSn()
  107. {
  108. $get = Yii::$app->request->get();
  109. $id = $get['id'] ?? 0;
  110. $seatSn = $get['seatSn'] ?? '';
  111. $cg = PurchaseOrderClass::getById($id, true);
  112. if (empty($cg)) {
  113. util::fail('没有找到采购单');
  114. }
  115. if ($cg->mainId != $this->mainId) {
  116. util::fail('没有权限');
  117. }
  118. $cg->seatSn = $seatSn;
  119. $cg->save();
  120. }
  121. //获取零售端在进行中的订单 ssh 20231201
  122. public function actionGetLsRunningOrderNum()
  123. {
  124. $lsFhNum = PurchaseClass::getCount(['mainId' => $this->mainId, 'status' => 2]);
  125. $lsShNum = PurchaseClass::getCount(['mainId' => $this->mainId, 'status' => 3]);
  126. util::success(['lsFhNum' => $lsFhNum, 'lsShNum' => $lsShNum]);
  127. }
  128. //打印小票 ssh 20230608
  129. public function actionPrintBill()
  130. {
  131. $get = Yii::$app->request->get();
  132. $id = $get['id'] ?? 0;
  133. $order = $get['order'] ?? 1;
  134. $cg = PurchaseOrderClass::getById($id, true);
  135. if (empty($cg)) {
  136. util::fail('没有找到采购单');
  137. }
  138. if ($cg->mainId != $this->mainId) {
  139. util::fail('没有权限');
  140. }
  141. if (getenv('YII_ENV') == 'production') {
  142. //小向的采购单要打印二张
  143. if ($this->mainId == 23390) {
  144. PurchaseOrderClass::printTicket($cg, false, $order);
  145. }
  146. }
  147. PurchaseOrderClass::printTicket($cg, false, $order);
  148. util::complete();
  149. }
  150. //打印全部 ssh 20220602
  151. public function actionPrintAll()
  152. {
  153. $get = Yii::$app->request->get();
  154. $orderSn = $get['orderSn'] ?? 0;
  155. $type = $get['type'] ?? 0;
  156. $itemList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  157. if (empty($itemList)) {
  158. util::fail('没有商品');
  159. }
  160. $order = PurchaseOrderClass::getByCondition(['orderSn' => $orderSn], true);
  161. $mainId = $order->mainId ?? 0;
  162. $ext = $this->shopExt;
  163. $printLabelSn = $ext->printLabelSn ?? '';
  164. if (empty($printLabelSn)) {
  165. util::fail('请设置标签打印机');
  166. }
  167. $p = new printUtil($printLabelSn);
  168. foreach ($itemList as $key => $item) {
  169. $name = $item->name ?? '';
  170. $num = !empty($item->itemNum) ? floatval($item->itemNum) : 0;
  171. if (empty($num)) {
  172. util::fail('没有花材数量');
  173. }
  174. $ptItemId = $item->itemId ?? '';
  175. $productId = $item->productId ?? 0;
  176. $p->times = $num;
  177. $smallUnit = $item->smallUnit ?? '';
  178. $bigUnit = $item->bigUnit ?? '';
  179. $ratio = $item->ratio ?? 0;
  180. $unit = $ratio . $smallUnit . '/' . $bigUnit;
  181. if (isset($item->ratioType) && $item->ratioType == 1) {
  182. $unit = '若干' . $smallUnit . '/' . $bigUnit;
  183. }
  184. if (stringUtil::getWordNum($name) > 7) {
  185. $content = '<TEXT x="30" y="30" font="12" w="1" h="1" r="0">' . $name . '</TEXT>';
  186. } else {
  187. $content = '<TEXT x="30" y="30" font="12" w="2" h="2" r="0">' . $name . '</TEXT>';
  188. }
  189. if ($key % 2 == 1) {
  190. $content .= '<TEXT x="415" y="20" font="12" w="2" h="2" r="0">.</TEXT>';
  191. }
  192. if ($type == 0) {
  193. $content .= '<BC128 x="30" y="100" h="70" s="1" r="0" n="4" w="11">' . $ptItemId . '</BC128>';
  194. if (in_array($mainId, [52, 1459])) {
  195. $content .= '<TEXT x="30" y="250" font="12" w="2" h="2" r="0">惠雅鲜花</TEXT>';
  196. } else {
  197. $content .= '<TEXT x="30" y="250" font="12" w="2" h="2" r="0">' . $unit . '</TEXT>';
  198. }
  199. } else {
  200. $content .= '<QR x="35" y="100" e="L" w="5">' . Yii::$app->params['mallHost'] . "/item/show-info?id=" . $productId . '</QR>';
  201. $content .= '<TEXT x="40" y="270" font="12" w="1" h="1" r="0">扫码看价格</TEXT>';
  202. }
  203. $p->printLabelMsg($content);
  204. }
  205. util::complete();
  206. }
  207. //打印全部 ssh 20220
  208. public function actionPrintItem()
  209. {
  210. $get = Yii::$app->request->get();
  211. $id = $get['id'] ?? 0;
  212. $type = $get['type'] ?? 0;
  213. $item = PurchaseOrderItemClass::getById($id, true);
  214. if (empty($item)) {
  215. util::fail('没有找到花材');
  216. }
  217. $name = $item->name ?? '';
  218. $num = !empty($item->itemNum) ? floatval($item->itemNum) : 0;
  219. if (empty($num)) {
  220. util::fail('没有花材数量');
  221. }
  222. $orderSn = $item->orderSn ?? '';
  223. $order = PurchaseOrderClass::getByCondition(['orderSn' => $orderSn], true);
  224. $mainId = $order->mainId ?? 0;
  225. $ext = $this->shopExt;
  226. $printLabelSn = $ext->printLabelSn ?? '';
  227. if (empty($printLabelSn)) {
  228. util::fail('请设置标签打印机');
  229. }
  230. $p = new printUtil($printLabelSn);
  231. $productId = $item->productId ?? 0;
  232. $product = ProductClass::getById($productId, true);
  233. $smallUnit = $product->smallUnit ?? '';
  234. $bigUnit = $product->bigUnit ?? '';
  235. $ratio = $product->ratio ?? 20;
  236. $ratioType = $product->ratioType ?? 0;
  237. if ($ratioType == 1) {
  238. $unit = '若干' . $smallUnit . '/' . $bigUnit;
  239. } else {
  240. $unit = $ratio . $smallUnit . '/' . $bigUnit;
  241. }
  242. $ptItemId = $item->itemId ?? '';
  243. $p->times = $num;
  244. if (stringUtil::getWordNum($name) > 7) {
  245. $content = '<TEXT x="30" y="30" font="12" w="1" h="1" r="0">' . $name . '</TEXT>';
  246. } else {
  247. $content = '<TEXT x="30" y="30" font="12" w="2" h="2" r="0">' . $name . '</TEXT>';
  248. }
  249. if ($type == 0) {
  250. $content .= '<BC128 x="30" y="100" h="70" s="1" r="0" n="4" w="11">' . $ptItemId . '</BC128>';
  251. if (in_array($mainId, [52, 1459])) {
  252. $content .= '<TEXT x="30" y="250" font="12" w="2" h="2" r="0">惠雅鲜花</TEXT>';
  253. } else {
  254. $content .= '<TEXT x="30" y="250" font="12" w="2" h="2" r="0">' . $unit . '</TEXT>';
  255. }
  256. } else {
  257. $content .= '<QR x="35" y="100" e="L" w="5">' . Yii::$app->params['mallHost'] . "/item/show-info?id=" . $productId . '</QR>';
  258. $content .= '<TEXT x="40" y="270" font="12" w="1" h="1" r="0">扫码看价格</TEXT>';
  259. }
  260. $p->printLabelMsg($content);
  261. util::complete();
  262. }
  263. //取消入库单 ssh 20221211
  264. public function actionCancel()
  265. {
  266. $get = Yii::$app->request->get();
  267. $id = $get['id'] ?? 0;
  268. $cg = PurchaseOrderClass::getLockById($id);
  269. if (isset($cg->mainId) == false || $cg->mainId != $this->mainId) {
  270. util::fail('请确认是否您的采购单');
  271. }
  272. if ($cg->status == PurchaseOrderClass::PURCHASE_ORDER_STATUS_CANCEL) {
  273. util::fail('已取消过了');
  274. }
  275. if ($cg->status == PurchaseOrderClass::PURCHASE_ORDER_STATUS_COMPLETE) {
  276. util::fail('已入库,无法取消');
  277. }
  278. PurchaseOrderClass::cancel($cg);
  279. util::complete();
  280. }
  281. //确认入库 sssh 20221211
  282. public function actionConfirmPutIn()
  283. {
  284. ini_set('memory_limit', '2045M');
  285. set_time_limit(0);
  286. $shopAdmin = $this->shopAdmin;
  287. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  288. util::fail('超管才能修改');
  289. }
  290. $connection = Yii::$app->db;
  291. $transaction = $connection->beginTransaction();
  292. try {
  293. $get = Yii::$app->request->get();
  294. $id = $get['id'] ?? 0;
  295. $cg = PurchaseOrderClass::getLockById($id);
  296. if (isset($cg->mainId) == false || $cg->mainId != $this->mainId) {
  297. util::fail('请确认是否您的采购单');
  298. }
  299. $staff = $this->shopAdmin;
  300. $data = [];
  301. $data['staffId'] = $staff->id ?? 0;
  302. $data['staffName'] = $staff->name ?? '';
  303. $data['adminId'] = $this->adminId ?? 0;
  304. $return = PurchaseOrderClass::putIn($cg, $data);
  305. $transaction->commit();
  306. util::success($return);
  307. } catch (\Exception $e) {
  308. $transaction->rollBack();
  309. noticeUtil::push('确认入库失败,原因:' . $e->getMessage(), '15280215347');
  310. Yii::error("确认入库失败,原因:" . $e->getMessage());
  311. util::fail('确认失败');
  312. }
  313. }
  314. //新增采购订单
  315. public function actionCreateOrder()
  316. {
  317. ini_set('memory_limit', '2045M');
  318. set_time_limit(0);
  319. $post = Yii::$app->request->post();
  320. $post['sjId'] = $this->sjId;
  321. $post['shopId'] = $this->shopId;
  322. $post['adminId'] = $this->adminId;
  323. $post['mainId'] = $this->mainId;
  324. $staff = $this->shopAdmin;
  325. //以下二个都需要保留
  326. $post['staffId'] = $staff->id ?? 0;
  327. $post['staffName'] = $staff->name ?? '';
  328. $post['shopAdminId'] = $staff->id ?? 0;
  329. $post['shopAdminName'] = $staff->name ?? '';
  330. $shopAdmin = $this->shopAdmin;
  331. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  332. util::fail('超管才能修改');
  333. }
  334. if (getenv('YII_ENV') == 'production') {
  335. //小向花卉采购控制
  336. if ($this->mainId == 23390) {
  337. if (!in_array($this->adminId, [24586, 24115, 24759])) {
  338. util::fail('你不能采购哦');
  339. }
  340. }
  341. } else {
  342. if ($this->mainId == 644) {
  343. // if (!in_array($this->adminId, [919])) {
  344. // util::fail('你不能采购哈');
  345. // }
  346. }
  347. }
  348. $shop = $this->shop;
  349. $carSale = $shop->carSale ?? 0;
  350. if ($carSale == 1) {
  351. util::fail('车销不能采购');
  352. }
  353. $connection = Yii::$app->db;
  354. $transaction = $connection->beginTransaction();
  355. try {
  356. // [{productId:0,itemPrice:1,bigNum:1,smallNum:0}]
  357. $ghsItemInfo = $post['itemInfo'] ?? '';
  358. if (empty($ghsItemInfo)) {
  359. util::fail('请选择花材');
  360. }
  361. $ghsItemInfo = json_decode($ghsItemInfo, true);
  362. if (!is_array($ghsItemInfo)) {
  363. util::fail('花材格式不正确');
  364. }
  365. //合并
  366. $ghsItemInfo = PurchaseOrderClass::mergeItemInfo($ghsItemInfo);
  367. //判断花材格式,是否属于当前门店
  368. ProductClass::valid($ghsItemInfo, $this->mainId);
  369. $cgStaffId = $post['cgStaffId'] ?? 0;
  370. if (empty($cgStaffId)) {
  371. $mainId = $this->mainId;
  372. if (getenv('YII_ENV') == 'production') {
  373. //花大苪、鹏诚康乃馨 采购入库必须选择采购人
  374. if (in_array($mainId, [8164, 19606])) {
  375. util::fail('请选择采购人');
  376. }
  377. } else {
  378. if (in_array($mainId, [])) {
  379. util::fail('请选择采购人');
  380. }
  381. }
  382. }
  383. //获取供货商信息
  384. $ghsId = $post['ghsId'];
  385. if (empty($ghsId)) {
  386. util::fail("请选择供货商");
  387. }
  388. $ghsInfo = GhsClass::getGhsInfo($ghsId);
  389. GhsClass::valid($ghsInfo, $this->shopId);
  390. $post['ghsInfo'] = json_encode($ghsInfo);
  391. $post['ghsName'] = $ghsInfo['name'] ?? '';
  392. $post['ghsAvatar'] = $ghsInfo['shortAvatar'] ?? '';
  393. //判断花材里的信息
  394. foreach ($ghsItemInfo as $v) {
  395. $bigNum = $v['bigNum'] ?? 0;
  396. $productId = $v['productId'] ?? 0;
  397. $smallNum = $v['smallNum'] ?? 0;
  398. if (!isset($v['itemPrice'])) {
  399. util::fail('采购价格不能为空');
  400. }
  401. if (!$productId) {
  402. util::fail('花材不存在');
  403. }
  404. if ($bigNum <= 0 && $smallNum <= 0) {
  405. util::fail('花材数量不能为0');
  406. }
  407. }
  408. $post['itemInfo'] = $ghsItemInfo;
  409. //0稍后入库 1直接入库
  410. $inType = $post['inType'] ?? PurchaseOrderClass::IN_TYPE_NOW;
  411. $debtStatus = $inType == 1 ? PurchaseOrderClass::DEBT_YES : PurchaseOrderClass::DEBT_UNKNOWN;
  412. $post['debt'] = $debtStatus;
  413. $order = PurchaseOrderClass::addOrder($post);
  414. //如果是附近供货商,则采购单标记为外采单
  415. if (isset($ghsInfo['location']) && $ghsInfo['location'] == 1) {
  416. $order->location = 1;
  417. $order->save();
  418. }
  419. if (isset($order->needPrint) && $order->needPrint == 1) {
  420. PurchaseOrderClass::printTicket($order);
  421. }
  422. $transaction->commit();
  423. util::success($order);
  424. } catch (\Exception $e) {
  425. $transaction->rollBack();
  426. noticeUtil::push('批发店采购入库失败,原因:' . $e->getMessage(), '15280215347');
  427. Yii::error("操作失败原因:" . $e->getMessage());
  428. util::fail('操作失败');
  429. }
  430. }
  431. //订单列表
  432. public function actionList()
  433. {
  434. $where = [];
  435. $get = Yii::$app->request->get();
  436. $where['shopId'] = $this->shopId;
  437. $orderStatus = $get['status'] ?? '';
  438. if ($orderStatus) {
  439. $where['status'] = $orderStatus;
  440. }
  441. $ghsId = $get['ghsId'] ?? 0;
  442. if (!empty($ghsId)) {
  443. $where['ghsId'] = $ghsId;
  444. }
  445. $location = $get['location'] ?? '';
  446. if (is_numeric($location)) {
  447. $where['location'] = $location;
  448. }
  449. $data = PurchaseOrderClass::getOrderList($where);
  450. //状态统计,很慢,暂时隐藏
  451. //$statData = PurchaseOrderClass::statNum($this->shopId);
  452. $statData = [
  453. 'allNum' => 0,
  454. 'unPayNum' => 0,
  455. 'unSendNum' => 0,
  456. 'sendingNum' => 0,
  457. 'finishNum' => 0,
  458. ];
  459. $data = array_merge($data, $statData);
  460. util::success($data);
  461. }
  462. //订单详情
  463. public function actionDetail()
  464. {
  465. $orderSn = Yii::$app->request->get('orderSn', '');
  466. $orderData = PurchaseOrderClass::getOrderDetail($orderSn, $this->shopId);
  467. util::success($orderData);
  468. }
  469. //修改备注
  470. public function actionUpdateRemark()
  471. {
  472. $remark = Yii::$app->request->post('remark', '');
  473. $id = Yii::$app->request->post('id', '');
  474. $cg = PurchaseOrderClass::getById($id, true);
  475. if (empty($cg)) {
  476. util::fail('没有找到订单');
  477. }
  478. if ($cg->shopId != $this->shopId) {
  479. util::fail('没有权限修改');
  480. }
  481. $cg->remark = $remark;
  482. $cg->save();
  483. util::complete('修改成功');
  484. }
  485. }