PurchaseOrderController.php 12 KB

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