PurchaseOrderController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. $cg->status = PurchaseOrderClass::PURCHASE_ORDER_STATUS_CANCEL;
  169. $cg->save();
  170. util::complete();
  171. }
  172. //确认入库 sssh 20221211
  173. public function actionConfirmPutIn()
  174. {
  175. ini_set('memory_limit', '2045M');
  176. set_time_limit(0);
  177. $shopAdmin = $this->shopAdmin;
  178. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  179. util::fail('超管才能修改');
  180. }
  181. $connection = Yii::$app->db;
  182. $transaction = $connection->beginTransaction();
  183. try {
  184. $get = Yii::$app->request->get();
  185. $id = $get['id'] ?? 0;
  186. $cg = PurchaseOrderClass::getLockById($id);
  187. if (isset($cg->mainId) == false || $cg->mainId != $this->mainId) {
  188. util::fail('请确认是否您的采购单');
  189. }
  190. $staff = $this->shopAdmin;
  191. $data = [];
  192. $data['staffId'] = $staff->id ?? 0;
  193. $data['staffName'] = $staff->name ?? '';
  194. $data['adminId'] = $this->adminId ?? 0;
  195. $return = PurchaseOrderClass::putIn($cg, $data);
  196. $transaction->commit();
  197. util::success($return);
  198. } catch (\Exception $e) {
  199. $transaction->rollBack();
  200. noticeUtil::push('确认入库失败,原因:' . $e->getMessage(), '15280215347');
  201. Yii::error("确认入库失败,原因:" . $e->getMessage());
  202. util::fail('确认失败');
  203. }
  204. }
  205. //新增采购订单
  206. public function actionCreateOrder()
  207. {
  208. ini_set('memory_limit', '2045M');
  209. set_time_limit(0);
  210. $post = Yii::$app->request->post();
  211. $post['sjId'] = $this->sjId;
  212. $post['shopId'] = $this->shopId;
  213. $post['adminId'] = $this->adminId;
  214. $post['mainId'] = $this->mainId;
  215. $staff = $this->shopAdmin;
  216. $post['staffId'] = $staff->id ?? 0;
  217. $post['staffName'] = $staff->name ?? '';
  218. $shopAdmin = $this->shopAdmin;
  219. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  220. util::fail('超管才能修改');
  221. }
  222. $shop = $this->shop;
  223. $carSale = $shop->carSale ?? 0;
  224. if ($carSale == 1) {
  225. util::fail('车销不能采购');
  226. }
  227. $connection = Yii::$app->db;
  228. $transaction = $connection->beginTransaction();
  229. try {
  230. // [{productId:0,itemPrice:1,bigNum:1,smallNum:0}]
  231. $ghsItemInfo = $post['itemInfo'] ?? '';
  232. if (empty($ghsItemInfo)) {
  233. util::fail('请选择花材');
  234. }
  235. $ghsItemInfo = json_decode($ghsItemInfo, true);
  236. if (!is_array($ghsItemInfo)) {
  237. util::fail('花材格式不正确');
  238. }
  239. //合并
  240. $ghsItemInfo = PurchaseOrderClass::mergeItemInfo($ghsItemInfo);
  241. //判断花材格式,是否属于当前门店
  242. ProductClass::valid($ghsItemInfo, $this->mainId);
  243. $cgStaffId = $post['cgStaffId'] ?? 0;
  244. if (empty($cgStaffId)) {
  245. $mainId = $this->mainId;
  246. if (getenv('YII_ENV') == 'production') {
  247. //花大苪采购入库必须选择采购人
  248. if (in_array($mainId, [8164])) {
  249. util::fail('请选择采购人');
  250. }
  251. } else {
  252. if (in_array($mainId, [644])) {
  253. util::fail('请选择采购人');
  254. }
  255. }
  256. }
  257. //获取供货商信息
  258. $ghsId = $post['ghsId'];
  259. if (empty($ghsId)) {
  260. util::fail("请选择供货商");
  261. }
  262. $ghsInfo = GhsClass::getGhsInfo($ghsId);
  263. GhsClass::valid($ghsInfo, $this->shopId);
  264. $post['ghsInfo'] = json_encode($ghsInfo);
  265. $post['ghsName'] = $ghsInfo['name'] ?? '';
  266. $post['ghsAvatar'] = $ghsInfo['shortAvatar'] ?? '';
  267. //判断花材里的信息
  268. foreach ($ghsItemInfo as $v) {
  269. $bigNum = $v['bigNum'] ?? 0;
  270. $productId = $v['productId'] ?? 0;
  271. $smallNum = $v['smallNum'] ?? 0;
  272. if (!isset($v['itemPrice'])) {
  273. util::fail('采购价格不能为空');
  274. }
  275. if (!$productId) {
  276. util::fail('花材不存在');
  277. }
  278. if ($bigNum <= 0 && $smallNum <= 0) {
  279. util::fail('花材数量不能为0');
  280. }
  281. }
  282. $post['itemInfo'] = $ghsItemInfo;
  283. //0稍后入库 1直接入库
  284. $inType = $post['inType'] ?? PurchaseOrderClass::IN_TYPE_NOW;
  285. $debtStatus = $inType == 1 ? PurchaseOrderClass::DEBT_YES : PurchaseOrderClass::DEBT_UNKNOWN;
  286. $post['debt'] = $debtStatus;
  287. $order = PurchaseOrderClass::addOrder($post);
  288. //如果是附近供货商,则采购单标记为外采单
  289. if (isset($ghsInfo['location']) && $ghsInfo['location'] == 1) {
  290. $order->location = 1;
  291. $order->save();
  292. }
  293. if (isset($order->needPrint) && $order->needPrint == 1) {
  294. PurchaseOrderClass::printTicket($order);
  295. }
  296. $transaction->commit();
  297. util::success($order);
  298. } catch (\Exception $e) {
  299. $transaction->rollBack();
  300. noticeUtil::push('批发店采购入库失败,原因:' . $e->getMessage(), '15280215347');
  301. Yii::error("操作失败原因:" . $e->getMessage());
  302. util::fail('操作失败');
  303. }
  304. }
  305. //订单列表
  306. public function actionList()
  307. {
  308. if (getenv('YII_ENV') == 'production') {
  309. //阿东的创始人才能查看采购单
  310. if (in_array($this->mainId, [742])) {
  311. $staff = $this->shopAdmin;
  312. if ($staff->founder != 2 && in_array($staff->id, [126647]) == false) {
  313. util::fail('没有权限');
  314. }
  315. }
  316. }
  317. $where = [];
  318. $get = Yii::$app->request->get();
  319. $where['shopId'] = $this->shopId;
  320. $orderStatus = $get['status'] ?? '';
  321. if ($orderStatus) {
  322. $where['status'] = $orderStatus;
  323. }
  324. $ghsId = $get['ghsId'] ?? 0;
  325. if (!empty($ghsId)) {
  326. $where['ghsId'] = $ghsId;
  327. }
  328. $location = $get['location'] ?? '';
  329. if (is_numeric($location)) {
  330. $where['location'] = $location;
  331. }
  332. $data = PurchaseOrderClass::getOrderList($where);
  333. //状态统计,很慢,暂时隐藏
  334. //$statData = PurchaseOrderClass::statNum($this->shopId);
  335. $statData = [
  336. 'allNum' => 0,
  337. 'unPayNum' => 0,
  338. 'unSendNum' => 0,
  339. 'sendingNum' => 0,
  340. 'finishNum' => 0,
  341. ];
  342. $data = array_merge($data, $statData);
  343. util::success($data);
  344. }
  345. //订单详情
  346. public function actionDetail()
  347. {
  348. if (getenv('YII_ENV') == 'production') {
  349. //阿东的创始人才能查看采购单
  350. if (in_array($this->mainId, [742])) {
  351. $staff = $this->shopAdmin;
  352. if ($staff->founder != 2 && in_array($staff->id, [126647]) == false) {
  353. util::fail('没有权限');
  354. }
  355. }
  356. }
  357. $orderSn = Yii::$app->request->get('orderSn', '');
  358. $orderData = PurchaseOrderClass::getOrderDetail($orderSn, $this->shopId);
  359. util::success($orderData);
  360. }
  361. //修改备注
  362. public function actionUpdateRemark()
  363. {
  364. $remark = Yii::$app->request->post('remark', '');
  365. $id = Yii::$app->request->post('id', '');
  366. $cg = PurchaseOrderClass::getById($id, true);
  367. if (empty($cg)) {
  368. util::fail('没有找到订单');
  369. }
  370. if ($cg->shopId != $this->shopId) {
  371. util::fail('没有权限修改');
  372. }
  373. $cg->remark = $remark;
  374. $cg->save();
  375. util::complete('修改成功');
  376. }
  377. }