PurchaseOrderController.php 14 KB

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