PurchaseOrderController.php 14 KB

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