PurchaseOrderController.php 15 KB

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