PurchaseOrderController.php 13 KB

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