PurchaseOrderController.php 16 KB

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