PurchaseOrderController.php 14 KB

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