PurchaseOrderController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\ghs\classes\GhsClass;
  4. use bizGhs\order\classes\PurchaseOrderClass;
  5. use bizGhs\order\classes\PurchaseOrderItemClass;
  6. use bizGhs\product\classes\ProductClass;
  7. use bizHd\purchase\classes\PurchaseClass;
  8. use common\components\dict;
  9. use common\components\noticeUtil;
  10. use common\components\printUtil;
  11. use common\components\stringUtil;
  12. use common\components\util;
  13. use biz\shop\classes\ShopExtClass;
  14. use Yii;
  15. class PurchaseOrderController extends BaseController
  16. {
  17. //修改货位号 ssh 20240410
  18. public function actionModifySeatSn()
  19. {
  20. $get = Yii::$app->request->get();
  21. $id = $get['id'] ?? 0;
  22. $seatSn = $get['seatSn'] ?? '';
  23. $cg = PurchaseOrderClass::getById($id, true);
  24. if (empty($cg)) {
  25. util::fail('没有找到采购单');
  26. }
  27. if ($cg->mainId != $this->mainId) {
  28. util::fail('没有权限');
  29. }
  30. $cg->seatSn = $seatSn;
  31. $cg->save();
  32. }
  33. //获取零售端在进行中的订单 ssh 20231201
  34. public function actionGetLsRunningOrderNum()
  35. {
  36. $lsFhNum = PurchaseClass::getCount(['mainId' => $this->mainId, 'status' => 2]);
  37. $lsShNum = PurchaseClass::getCount(['mainId' => $this->mainId, 'status' => 3]);
  38. util::success(['lsFhNum' => $lsFhNum, 'lsShNum' => $lsShNum]);
  39. }
  40. //打印小票 ssh 20230608
  41. public function actionPrintBill()
  42. {
  43. $get = Yii::$app->request->get();
  44. $id = $get['id'] ?? 0;
  45. $order = $get['order'] ?? 1;
  46. $cg = PurchaseOrderClass::getById($id, true);
  47. if (empty($cg)) {
  48. util::fail('没有找到采购单');
  49. }
  50. if ($cg->mainId != $this->mainId) {
  51. util::fail('没有权限');
  52. }
  53. if (getenv('YII_ENV') == 'production') {
  54. //小向的采购单要打印二张
  55. if ($this->mainId == 23390) {
  56. PurchaseOrderClass::printTicket($cg, false, $order);
  57. }
  58. }
  59. PurchaseOrderClass::printTicket($cg, false, $order);
  60. util::complete();
  61. }
  62. //打印全部 ssh 20220602
  63. public function actionPrintAll()
  64. {
  65. $get = Yii::$app->request->get();
  66. $orderSn = $get['orderSn'] ?? 0;
  67. $type = $get['type'] ?? 0;
  68. $itemList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  69. if (empty($itemList)) {
  70. util::fail('没有商品');
  71. }
  72. $order = PurchaseOrderClass::getByCondition(['orderSn' => $orderSn], true);
  73. $mainId = $order->mainId ?? 0;
  74. $ext = $this->shopExt;
  75. $printLabelSn = $ext->printLabelSn ?? '';
  76. if (empty($printLabelSn)) {
  77. util::fail('请设置标签打印机');
  78. }
  79. $p = new printUtil($printLabelSn);
  80. foreach ($itemList as $key => $item) {
  81. $name = $item->name ?? '';
  82. $num = !empty($item->itemNum) ? floatval($item->itemNum) : 0;
  83. if (empty($num)) {
  84. util::fail('没有花材数量');
  85. }
  86. $ptItemId = $item->itemId ?? '';
  87. $productId = $item->productId ?? 0;
  88. $p->times = $num;
  89. $smallUnit = $item->smallUnit ?? '';
  90. $bigUnit = $item->bigUnit ?? '';
  91. $ratio = $item->ratio ?? 0;
  92. $unit = $ratio . $smallUnit . '/' . $bigUnit;
  93. if (isset($item->ratioType) && $item->ratioType == 1) {
  94. $unit = '若干' . $smallUnit . '/' . $bigUnit;
  95. }
  96. if (stringUtil::getWordNum($name) > 7) {
  97. $content = '<TEXT x="30" y="30" font="12" w="1" h="1" r="0">' . $name . '</TEXT>';
  98. } else {
  99. $content = '<TEXT x="30" y="30" font="12" w="2" h="2" r="0">' . $name . '</TEXT>';
  100. }
  101. if ($key % 2 == 1) {
  102. $content .= '<TEXT x="415" y="20" font="12" w="2" h="2" r="0">.</TEXT>';
  103. }
  104. if ($type == 0) {
  105. $content .= '<BC128 x="30" y="100" h="70" s="1" r="0" n="4" w="11">' . $ptItemId . '</BC128>';
  106. if (in_array($mainId, [52, 1459])) {
  107. $content .= '<TEXT x="30" y="250" font="12" w="2" h="2" r="0">惠雅鲜花</TEXT>';
  108. } else {
  109. $content .= '<TEXT x="30" y="250" font="12" w="2" h="2" r="0">' . $unit . '</TEXT>';
  110. }
  111. } else {
  112. $content .= '<QR x="35" y="100" e="L" w="5">' . Yii::$app->params['mallHost'] . "/item/show-info?id=" . $productId . '</QR>';
  113. $content .= '<TEXT x="40" y="270" font="12" w="1" h="1" r="0">扫码看价格</TEXT>';
  114. }
  115. $p->printLabelMsg($content);
  116. }
  117. util::complete();
  118. }
  119. //打印全部 ssh 20220
  120. public function actionPrintItem()
  121. {
  122. $get = Yii::$app->request->get();
  123. $id = $get['id'] ?? 0;
  124. $type = $get['type'] ?? 0;
  125. $item = PurchaseOrderItemClass::getById($id, true);
  126. if (empty($item)) {
  127. util::fail('没有找到花材');
  128. }
  129. $name = $item->name ?? '';
  130. $num = !empty($item->itemNum) ? floatval($item->itemNum) : 0;
  131. if (empty($num)) {
  132. util::fail('没有花材数量');
  133. }
  134. $orderSn = $item->orderSn ?? '';
  135. $order = PurchaseOrderClass::getByCondition(['orderSn' => $orderSn], true);
  136. $mainId = $order->mainId ?? 0;
  137. $ext = $this->shopExt;
  138. $printLabelSn = $ext->printLabelSn ?? '';
  139. if (empty($printLabelSn)) {
  140. util::fail('请设置标签打印机');
  141. }
  142. $p = new printUtil($printLabelSn);
  143. $productId = $item->productId ?? 0;
  144. $product = ProductClass::getById($productId, true);
  145. $smallUnit = $product->smallUnit ?? '';
  146. $bigUnit = $product->bigUnit ?? '';
  147. $ratio = $product->ratio ?? 20;
  148. $ratioType = $product->ratioType ?? 0;
  149. if ($ratioType == 1) {
  150. $unit = '若干' . $smallUnit . '/' . $bigUnit;
  151. } else {
  152. $unit = $ratio . $smallUnit . '/' . $bigUnit;
  153. }
  154. $ptItemId = $item->itemId ?? '';
  155. $p->times = $num;
  156. if (stringUtil::getWordNum($name) > 7) {
  157. $content = '<TEXT x="30" y="30" font="12" w="1" h="1" r="0">' . $name . '</TEXT>';
  158. } else {
  159. $content = '<TEXT x="30" y="30" font="12" w="2" h="2" r="0">' . $name . '</TEXT>';
  160. }
  161. if ($type == 0) {
  162. $content .= '<BC128 x="30" y="100" h="70" s="1" r="0" n="4" w="11">' . $ptItemId . '</BC128>';
  163. if (in_array($mainId, [52, 1459])) {
  164. $content .= '<TEXT x="30" y="250" font="12" w="2" h="2" r="0">惠雅鲜花</TEXT>';
  165. } else {
  166. $content .= '<TEXT x="30" y="250" font="12" w="2" h="2" r="0">' . $unit . '</TEXT>';
  167. }
  168. } else {
  169. $content .= '<QR x="35" y="100" e="L" w="5">' . Yii::$app->params['mallHost'] . "/item/show-info?id=" . $productId . '</QR>';
  170. $content .= '<TEXT x="40" y="270" font="12" w="1" h="1" r="0">扫码看价格</TEXT>';
  171. }
  172. $p->printLabelMsg($content);
  173. util::complete();
  174. }
  175. //取消入库单 ssh 20221211
  176. public function actionCancel()
  177. {
  178. $get = Yii::$app->request->get();
  179. $id = $get['id'] ?? 0;
  180. $cg = PurchaseOrderClass::getLockById($id);
  181. if (isset($cg->mainId) == false || $cg->mainId != $this->mainId) {
  182. util::fail('请确认是否您的采购单');
  183. }
  184. if ($cg->status == PurchaseOrderClass::PURCHASE_ORDER_STATUS_CANCEL) {
  185. util::fail('已取消过了');
  186. }
  187. if ($cg->status == PurchaseOrderClass::PURCHASE_ORDER_STATUS_COMPLETE) {
  188. util::fail('已入库,无法取消');
  189. }
  190. PurchaseOrderClass::cancel($cg);
  191. util::complete();
  192. }
  193. //确认入库 sssh 20221211
  194. public function actionConfirmPutIn()
  195. {
  196. ini_set('memory_limit', '2045M');
  197. set_time_limit(0);
  198. $shopAdmin = $this->shopAdmin;
  199. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  200. util::fail('超管才能修改');
  201. }
  202. $connection = Yii::$app->db;
  203. $transaction = $connection->beginTransaction();
  204. try {
  205. $get = Yii::$app->request->get();
  206. $id = $get['id'] ?? 0;
  207. $cg = PurchaseOrderClass::getLockById($id);
  208. if (isset($cg->mainId) == false || $cg->mainId != $this->mainId) {
  209. util::fail('请确认是否您的采购单');
  210. }
  211. $staff = $this->shopAdmin;
  212. $data = [];
  213. $data['staffId'] = $staff->id ?? 0;
  214. $data['staffName'] = $staff->name ?? '';
  215. $data['adminId'] = $this->adminId ?? 0;
  216. $return = PurchaseOrderClass::putIn($cg, $data);
  217. $transaction->commit();
  218. util::success($return);
  219. } catch (\Exception $e) {
  220. $transaction->rollBack();
  221. noticeUtil::push('确认入库失败,原因:' . $e->getMessage(), '15280215347');
  222. Yii::error("确认入库失败,原因:" . $e->getMessage());
  223. util::fail('确认失败');
  224. }
  225. }
  226. //新增采购订单
  227. public function actionCreateOrder()
  228. {
  229. ini_set('memory_limit', '2045M');
  230. set_time_limit(0);
  231. $post = Yii::$app->request->post();
  232. $post['sjId'] = $this->sjId;
  233. $post['shopId'] = $this->shopId;
  234. $post['adminId'] = $this->adminId;
  235. $post['mainId'] = $this->mainId;
  236. $staff = $this->shopAdmin;
  237. //以下二个都需要保留
  238. $post['staffId'] = $staff->id ?? 0;
  239. $post['staffName'] = $staff->name ?? '';
  240. $post['shopAdminId'] = $staff->id ?? 0;
  241. $post['shopAdminName'] = $staff->name ?? '';
  242. $shopAdmin = $this->shopAdmin;
  243. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  244. util::fail('超管才能修改');
  245. }
  246. if (getenv('YII_ENV') == 'production') {
  247. if ($this->mainId == 23390) {
  248. if (!in_array($this->adminId, [24586])) {
  249. util::fail('你不能采购哦');
  250. }
  251. }
  252. } else {
  253. if ($this->mainId == 644) {
  254. echo $this->adminId;die;
  255. if (!in_array($this->adminId, [919])) {
  256. util::fail('你不能采购哈');
  257. }
  258. }
  259. }
  260. $shop = $this->shop;
  261. $carSale = $shop->carSale ?? 0;
  262. if ($carSale == 1) {
  263. util::fail('车销不能采购');
  264. }
  265. $connection = Yii::$app->db;
  266. $transaction = $connection->beginTransaction();
  267. try {
  268. // [{productId:0,itemPrice:1,bigNum:1,smallNum:0}]
  269. $ghsItemInfo = $post['itemInfo'] ?? '';
  270. if (empty($ghsItemInfo)) {
  271. util::fail('请选择花材');
  272. }
  273. $ghsItemInfo = json_decode($ghsItemInfo, true);
  274. if (!is_array($ghsItemInfo)) {
  275. util::fail('花材格式不正确');
  276. }
  277. //合并
  278. $ghsItemInfo = PurchaseOrderClass::mergeItemInfo($ghsItemInfo);
  279. //判断花材格式,是否属于当前门店
  280. ProductClass::valid($ghsItemInfo, $this->mainId);
  281. $cgStaffId = $post['cgStaffId'] ?? 0;
  282. if (empty($cgStaffId)) {
  283. $mainId = $this->mainId;
  284. if (getenv('YII_ENV') == 'production') {
  285. //花大苪、鹏诚康乃馨 采购入库必须选择采购人
  286. if (in_array($mainId, [8164, 19606])) {
  287. util::fail('请选择采购人');
  288. }
  289. } else {
  290. if (in_array($mainId, [])) {
  291. util::fail('请选择采购人');
  292. }
  293. }
  294. }
  295. //获取供货商信息
  296. $ghsId = $post['ghsId'];
  297. if (empty($ghsId)) {
  298. util::fail("请选择供货商");
  299. }
  300. $ghsInfo = GhsClass::getGhsInfo($ghsId);
  301. GhsClass::valid($ghsInfo, $this->shopId);
  302. $post['ghsInfo'] = json_encode($ghsInfo);
  303. $post['ghsName'] = $ghsInfo['name'] ?? '';
  304. $post['ghsAvatar'] = $ghsInfo['shortAvatar'] ?? '';
  305. //判断花材里的信息
  306. foreach ($ghsItemInfo as $v) {
  307. $bigNum = $v['bigNum'] ?? 0;
  308. $productId = $v['productId'] ?? 0;
  309. $smallNum = $v['smallNum'] ?? 0;
  310. if (!isset($v['itemPrice'])) {
  311. util::fail('采购价格不能为空');
  312. }
  313. if (!$productId) {
  314. util::fail('花材不存在');
  315. }
  316. if ($bigNum <= 0 && $smallNum <= 0) {
  317. util::fail('花材数量不能为0');
  318. }
  319. }
  320. $post['itemInfo'] = $ghsItemInfo;
  321. //0稍后入库 1直接入库
  322. $inType = $post['inType'] ?? PurchaseOrderClass::IN_TYPE_NOW;
  323. $debtStatus = $inType == 1 ? PurchaseOrderClass::DEBT_YES : PurchaseOrderClass::DEBT_UNKNOWN;
  324. $post['debt'] = $debtStatus;
  325. $order = PurchaseOrderClass::addOrder($post);
  326. //如果是附近供货商,则采购单标记为外采单
  327. if (isset($ghsInfo['location']) && $ghsInfo['location'] == 1) {
  328. $order->location = 1;
  329. $order->save();
  330. }
  331. if (isset($order->needPrint) && $order->needPrint == 1) {
  332. PurchaseOrderClass::printTicket($order);
  333. }
  334. $transaction->commit();
  335. util::success($order);
  336. } catch (\Exception $e) {
  337. $transaction->rollBack();
  338. noticeUtil::push('批发店采购入库失败,原因:' . $e->getMessage(), '15280215347');
  339. Yii::error("操作失败原因:" . $e->getMessage());
  340. util::fail('操作失败');
  341. }
  342. }
  343. //订单列表
  344. public function actionList()
  345. {
  346. $where = [];
  347. $get = Yii::$app->request->get();
  348. $where['shopId'] = $this->shopId;
  349. $orderStatus = $get['status'] ?? '';
  350. if ($orderStatus) {
  351. $where['status'] = $orderStatus;
  352. }
  353. $ghsId = $get['ghsId'] ?? 0;
  354. if (!empty($ghsId)) {
  355. $where['ghsId'] = $ghsId;
  356. }
  357. $location = $get['location'] ?? '';
  358. if (is_numeric($location)) {
  359. $where['location'] = $location;
  360. }
  361. $data = PurchaseOrderClass::getOrderList($where);
  362. //状态统计,很慢,暂时隐藏
  363. //$statData = PurchaseOrderClass::statNum($this->shopId);
  364. $statData = [
  365. 'allNum' => 0,
  366. 'unPayNum' => 0,
  367. 'unSendNum' => 0,
  368. 'sendingNum' => 0,
  369. 'finishNum' => 0,
  370. ];
  371. $data = array_merge($data, $statData);
  372. util::success($data);
  373. }
  374. //订单详情
  375. public function actionDetail()
  376. {
  377. $orderSn = Yii::$app->request->get('orderSn', '');
  378. $orderData = PurchaseOrderClass::getOrderDetail($orderSn, $this->shopId);
  379. util::success($orderData);
  380. }
  381. //修改备注
  382. public function actionUpdateRemark()
  383. {
  384. $remark = Yii::$app->request->post('remark', '');
  385. $id = Yii::$app->request->post('id', '');
  386. $cg = PurchaseOrderClass::getById($id, true);
  387. if (empty($cg)) {
  388. util::fail('没有找到订单');
  389. }
  390. if ($cg->shopId != $this->shopId) {
  391. util::fail('没有权限修改');
  392. }
  393. $cg->remark = $remark;
  394. $cg->save();
  395. util::complete('修改成功');
  396. }
  397. }