PurchaseOrderController.php 14 KB

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