MPicking.Class.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Gss
  5. * Date: 2021/5/17 0017
  6. * Time: 17:58
  7. */
  8. namespace JinDouYun\Model\Stock;
  9. use JinDouYun\Dao\Customer\DCustomer;
  10. use JinDouYun\Dao\Stock\DPicking;
  11. use JinDouYun\Dao\Stock\DPickingGoods;
  12. use JinDouYun\Dao\Stock\DPickingIndex;
  13. use JinDouYun\Model\Finance\MAccount;
  14. use JinDouYun\Model\Order\MOrder;
  15. use Mall\Framework\Core\StatusCode;
  16. use Mall\Framework\Core\ErrorCode;
  17. use Mall\Framework\Core\ResultWrapper;
  18. use JinDouYun\Model\MBaseModel;
  19. class MPicking extends MBaseModel
  20. {
  21. /**
  22. * @var int 用户id
  23. */
  24. private $userCenterId;
  25. /**
  26. * @var int 企业id
  27. */
  28. private $onlineEnterpriseId;
  29. /**
  30. * @var int 单表数据量
  31. */
  32. private $cutTable = 200000;
  33. private $objDPicking;
  34. private $objDPickingGoods;
  35. private $objDPickingIndex;
  36. private $objMAccount;
  37. private $objDCustomer;
  38. /**
  39. * MOrder constructor.
  40. * @param $userCenterId
  41. * @param $onlineEnterpriseId
  42. * @param null $loginUserCenterId
  43. * @param bool $isFront
  44. * @throws \Exception
  45. */
  46. public function __construct($userCenterId, $onlineEnterpriseId, $loginUserCenterId = null, $isFront = false, $isCashier = false)
  47. {
  48. $this->isCashier = $isCashier;
  49. $this->isFront = $isFront;
  50. $this->userCenterId = $userCenterId;
  51. $this->onlineEnterpriseId = $onlineEnterpriseId;
  52. $this->loginUserCenterId = $loginUserCenterId;
  53. parent::__construct($this->onlineEnterpriseId, $loginUserCenterId);
  54. $this->objDPicking = new DPicking('stock');
  55. self::pickingSubTable($onlineEnterpriseId, $userCenterId);
  56. $this->objMAccount = new MAccount($onlineEnterpriseId, $userCenterId);
  57. $this->objDCustomer = new DCustomer();
  58. $this->objDCustomer->setTable('qianniao_customer_' . $this->onlineEnterpriseId);
  59. $this->objDPickingGoods = new DPickingGoods();
  60. self::pickingGoodsSubTable($onlineEnterpriseId, $userCenterId);
  61. $this->objDPickingIndex = new DPickingIndex();
  62. $this->objDPickingIndex->setTable('qianniao_picking_index_'.$this->onlineEnterpriseId);
  63. }
  64. /**
  65. * 拣货单分表 分表规则:企业id_(客户/200000)
  66. * @param $enterpriseId (企业id)
  67. * @param $userId (用户id)
  68. * @throws \Exception
  69. */
  70. public function pickingSubTable($enterpriseId, $userId)
  71. {
  72. $tableName = $this->objDPicking->getTableName('qianniao_picking_' . $enterpriseId, $userId, $this->cutTable);
  73. $this->objDPicking->setTable($tableName);
  74. }
  75. /**
  76. * 拣货单分表 分表规则:企业id_(客户/200000)
  77. * @param $enterpriseId (企业id)
  78. * @param $userId (用户id)
  79. * @throws \Exception
  80. */
  81. public function pickingGoodsSubTable($enterpriseId, $userId)
  82. {
  83. $tableName = $this->objDPickingGoods->getTableName('qianniao_picking_goods_' . $enterpriseId, $userId, $this->cutTable);
  84. $this->objDPickingGoods->setTable($tableName);
  85. }
  86. /**
  87. * 获取拣货单表名
  88. */
  89. public function getPickingTable($enterpriseId, $userId){
  90. return $this->objDPicking->getTableName('qianniao_picking_' . $enterpriseId, $userId, $this->cutTable);
  91. }
  92. /* *************************************************************************************************************** */
  93. /**
  94. * 新增拣货单
  95. */
  96. public function addPicking($params)
  97. {
  98. $goodsDate = $params['goodsData'];
  99. unset($params['goodsData']);
  100. // 生成拣货单编号
  101. $dbResult = $this->objDPicking->get('createTime >='.strtotime(date('Ymd'.'0:0:0')), 'no', 'createTime desc');
  102. if ($dbResult === false) {
  103. return ResultWrapper::fail($this->objDPicking->error(), ErrorCode::$dberror);
  104. }
  105. if(empty($dbResult)){
  106. $params['no'] = createSerialNumberByDate('');
  107. }else{
  108. $params['no'] = createSerialNumberByDate($dbResult['no']);
  109. }
  110. unset($dbResult);
  111. //订单no 切割
  112. if( isset($params['originNo']) && !empty($params['originNo']) ){
  113. $params['originNo'] = explode('-',$params['originNo']);
  114. if(count($params['originNo']) == 3){
  115. $params['originNo'] = $params['originNo'][1].'-'.$params['originNo'][2];
  116. }
  117. }
  118. //开启事务
  119. $beginTransactionStatus = $this->objDPicking->beginTransaction();
  120. // 组装index数据
  121. $indexDate = [
  122. 'no' => getArrayItem($params,'no',''),
  123. 'shopId' => getArrayItem($params,'shopId',0),
  124. 'shopName' => getArrayItem($params,'shopName',''),
  125. 'merchantId' => getArrayItem($params,'merchantId',0),
  126. 'originId' => getArrayItem($params,'originId',0),
  127. 'originNo' => getArrayItem($params,'originNo',''),
  128. 'warehouseId' => getArrayItem($params,'warehouseId',0),
  129. 'warehouseName' => getArrayItem($params,'warehouseName',''),
  130. 'currentUnitId' => getArrayItem($params,'currentUnitId',0),
  131. 'currentUnitName' => getArrayItem($params,'currentUnitName',''),
  132. 'personnel' => getArrayItem($params,'personnel',0),
  133. 'personnelName' => getArrayItem($params,'personnelName',''),
  134. 'sort' => getArrayItem($params,'sort',0),
  135. 'managerId' => getArrayItem($params,'managerId',0),
  136. 'managerName' => getArrayItem($params,'managerName',''),
  137. 'pickingStatus' => getArrayItem($params,'pickingStatus',4),
  138. 'pickingType' => getArrayItem($params,'pickingType',4),
  139. 'createTime' => getArrayItem($params,'createTime',''),
  140. ];
  141. $pickIndexId = $this->objDPickingIndex->insert($indexDate);
  142. if ($pickIndexId === false) {
  143. $this->objDPicking->rollBack();
  144. return ResultWrapper::fail($this->objDPickingIndex->error(), ErrorCode::$dberror);
  145. }
  146. $params['id'] = $pickIndexId;
  147. $pickId = $this->objDPicking->insert($params);
  148. if ($pickId === false) {
  149. $this->objDPicking->rollBack();
  150. return ResultWrapper::fail($this->objDPicking->error(), ErrorCode::$dberror);
  151. }
  152. // 插入拣货单id、no
  153. foreach ($goodsDate as $key => $value){
  154. $goodsDate[$key]['pickingId'] = $pickIndexId;
  155. // 拣货单编号
  156. $goodsDate[$key]['no'] = getArrayItem($params,'no','');
  157. }
  158. $pickingGoodsId = $this->objDPickingGoods->insert($goodsDate,true);
  159. if ($pickingGoodsId === false) {
  160. $this->objDPicking->rollBack();
  161. return ResultWrapper::fail($this->objDPickingGoods->error(), ErrorCode::$dberror);
  162. }
  163. //生成完后同步更新订单的拣货状态为拣货中
  164. $objMOrder = new MOrder($this->userCenterId, $this->onlineEnterpriseId);
  165. $orderIds = explode(',',$params['originId']);
  166. //修改订单拣货状态
  167. $result = $objMOrder->updateOrderPicking($orderIds,StatusCode::$orderPickStatus['picking']);
  168. if(!$result->isSuccess()){
  169. return ResultWrapper::fail($result->getData(),$result->getErrorCode());
  170. }
  171. if($beginTransactionStatus){
  172. $this->objDPicking->commit();
  173. }
  174. return ResultWrapper::success($pickIndexId);
  175. }
  176. /**
  177. * 拣货单详情
  178. */
  179. public function getPickingInfo($pickingId)
  180. {
  181. $dbResult = $this->objDPicking->get($pickingId);
  182. if ($dbResult === false) {
  183. return ResultWrapper::fail($this->objDPicking->error(), ErrorCode::$dberror);
  184. }
  185. $goodsDate = $this->objDPickingGoods->select(['pickingId'=>$pickingId]);
  186. if ($goodsDate === false) {
  187. return ResultWrapper::fail($this->objDPicking->error(), ErrorCode::$dberror);
  188. }
  189. foreach ($goodsDate as $key =>$value){
  190. if (empty($value['specGroup'])){
  191. $goodsDate[$key]['specGroup'] = json_decode($value['specGroup'],true);
  192. }
  193. }
  194. $dbResult['goodsDate'] = $goodsDate;
  195. return ResultWrapper::success($dbResult);
  196. }
  197. /**
  198. * @param $pickingDate
  199. * @param $goodsDate
  200. * @return mixed
  201. * 编辑拣货单
  202. */
  203. public function editPicking($params)
  204. {
  205. $goodsDate = $params['goodsDate'];
  206. unset($params['goodsDate']);
  207. // 重新计算编辑后的数量
  208. $number = 0;
  209. $notPickingNum = 0;
  210. $totalMoney = 0;
  211. $notOutNum = 0;
  212. //循环获取商品数量,销售金额
  213. foreach ($goodsDate as $key =>$value ){
  214. $params['number'] = bcadd($number,$value['orderNum']);
  215. $params['notPickingNum'] = bcadd($notPickingNum,$value['orderNum']);
  216. $params['totalMoney'] = bcadd($totalMoney,$value['totalMoney']);
  217. $params['notOutNum'] = bcadd($notOutNum,$value['orderNum']);
  218. }
  219. //商品行数
  220. $params['goodsLine'] = count($goodsDate);
  221. // 拣货单id
  222. $pickingId = $params['id'];
  223. unset($params['id']);
  224. unset($params['no']);
  225. unset($params['originNo']);
  226. $params['updateTime'] = time();
  227. $beginTransactionstatus = $this->objDPicking->beginTransaction();
  228. $dbResult = $this->objDPicking->update($params, $pickingId);
  229. if ($dbResult === false) {
  230. $this->objDPicking->rollBack();
  231. return ResultWrapper::fail($this->objDPicking->error(), ErrorCode::$dberror);
  232. }
  233. unset($dbResult);
  234. //index数据
  235. $indexDate = [
  236. 'personnel' => getArrayItem($params,'personnel',0),
  237. 'personnelName' => getArrayItem($params,'personnelName',''),
  238. 'sort' => getArrayItem($params,'sort',0),
  239. 'managerId' => getArrayItem($params,'managerId',0),
  240. 'managerName' => getArrayItem($params,'managerName',''),
  241. 'pickingStatus' => getArrayItem($params,'pickingStatus',4),
  242. 'pickingType' => getArrayItem($params,'pickingType',4),
  243. 'updateTime' => getArrayItem($params,'updateTime',''),
  244. ];
  245. $dbResult = $this->objDPickingIndex->update($indexDate, $pickingId);
  246. if ($dbResult === false) {
  247. $this->objDPicking->rollBack();
  248. return ResultWrapper::fail($this->objDPickingIndex->error(), ErrorCode::$dberror);
  249. }
  250. unset($dbResult);
  251. // 拣货商品数据
  252. foreach ($goodsDate as $key => $value){
  253. $goodsId = getArrayItem($value, 'id', 0);
  254. //判断拣货状态 根据应拣数量和本次拣货数量判断
  255. if($value['pickingedNum'] <=0){
  256. return ResultWrapper::fail('本次件货数量不能为0',ErrorCode::$notAllowAccess);
  257. }
  258. // if($value['pickingedNum'] == $value['pickingNum']){ // 说明全部拣货
  259. // $value['pickingStatus'] = StatusCode::$standard;
  260. // }else{
  261. // $value['pickingStatus'] = StatusCode::$partion;
  262. // }
  263. unset($value['no']);
  264. $value['updateTime'] = time();
  265. $dbResult = $this->objDPickingGoods->update($value, $goodsId);
  266. if ($dbResult === false) {
  267. $this->objDPicking->rollBack();
  268. return ResultWrapper::fail($this->objDPickingGoods->error(), ErrorCode::$dberror);
  269. }
  270. }
  271. if($beginTransactionstatus){
  272. $this->objDPicking->commit();
  273. }
  274. return ResultWrapper::success($pickingId);
  275. }
  276. /**
  277. * 完成拣货
  278. */
  279. public function updatePicking($params)
  280. {
  281. $dbResult = $this->objDPicking->get($params['id']);
  282. if ($dbResult === false) {
  283. return ResultWrapper::fail($this->objDPicking->error(), ErrorCode::$dberror);
  284. }
  285. $beginTransactionstatus = $this->objDPicking->beginTransaction();
  286. //更新拣货状态
  287. $updateResult = $this->objDPicking->update(['pickingStatus'=>StatusCode::$standard,'updateTime'=>time()],['id'=>$params['id']]);
  288. if ($updateResult === false) {
  289. $this->objDPicking->rollBack();
  290. return ResultWrapper::fail($this->objDPicking->error(), ErrorCode::$dberror);
  291. }
  292. if($beginTransactionstatus){
  293. $this->objDPicking->commit();
  294. }
  295. return ResultWrapper::success($updateResult);
  296. }
  297. /**
  298. * 拣货单列表
  299. */
  300. public function getAllPicking($selectParams)
  301. {
  302. $limit = $selectParams['limit'];
  303. unset($selectParams['limit']);
  304. $offset = $selectParams['offset'];
  305. unset($selectParams['offset']);
  306. //组装sql where条件
  307. $whereSql = '';
  308. if (isset($selectParams['no']) && !empty($selectParams['no'])) {
  309. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  310. $whereSql .= $where . ' no like "%' . $selectParams['no'] . '%"';
  311. }
  312. if (isset($selectParams['warehouseId']) && !empty($selectParams['warehouseId'])) {
  313. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  314. $whereSql .= $where . ' warehouseId = ' . $selectParams['warehouseId'];
  315. }
  316. if (isset($selectParams['personnel']) && !empty($selectParams['personnel'])) {
  317. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  318. $whereSql .= $where . ' personnel = ' . $selectParams['personnel'];
  319. }
  320. if (isset($selectParams['originId']) && !empty($selectParams['originId'])) {
  321. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  322. $whereSql .= $where . ' originId = ' . $selectParams['originId'];
  323. }
  324. if (isset($selectParams['originNo']) && !empty($selectParams['originNo'])) {
  325. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  326. $whereSql .= $where . ' originNo like "%' . $selectParams['originNo'] . '%"';
  327. }
  328. if (isset($selectParams['currentUnitId']) && !empty($selectParams['currentUnitId'])) {
  329. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  330. $whereSql .= $where . ' currentUnitId = ' . $selectParams['currentUnitId'];
  331. }
  332. if (isset($selectParams['managerId']) && !empty($selectParams['managerId'])) {
  333. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  334. $whereSql .= $where . ' managerId = ' . $selectParams['managerId'];
  335. }
  336. //时间删选 有timeName字段优先
  337. if( isset($selectParams['timeName']) && !empty($selectParams['timeName']) ){
  338. $timeName = $selectParams['timeName'];
  339. $timestamp = gettimestamp($timeName,time());
  340. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  341. $whereSql .= $where . ' createTime BETWEEN ' . $timestamp['start'] . ' AND '. $timestamp['end'];
  342. }else{
  343. if ( (isset($selectParams['start']) && !empty($selectParams['start']))&&(isset($selectParams['end']) && !empty($selectParams['end'])) ) {
  344. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  345. $whereSql .= $where . ' createTime BETWEEN ' . $selectParams['start'] . ' AND '. $selectParams['end'];
  346. }
  347. }
  348. $sql = 'select * from '.$this->objDPickingIndex->get_Table().$whereSql . ' ORDER BY createTime DESC LIMIT ' . $offset . ' , ' . $limit;
  349. $pickIndexDbResult = $this->objDPickingIndex->query($sql);
  350. $pickDbResult = [];
  351. foreach ($pickIndexDbResult as $key => $value){
  352. $dbResult = $this->objDPicking->select(['id'=>$value['id']], '*', 'createTime desc');
  353. if ($dbResult === false) {
  354. return ResultWrapper::fail($this->objDPicking->error(), ErrorCode::$dberror);
  355. }
  356. $pickDbResult = array_merge($pickDbResult, $dbResult);
  357. }
  358. // 渲染编号
  359. foreach ($pickDbResult as $k => $v){
  360. $pickDbResult[$k]['originNo'] = StatusCode::$noPrefix[1].'-'.$v['originNo'];
  361. $pickDbResult[$k]['no'] = StatusCode::$noPrefix[31].'-'.$v['no'];
  362. }
  363. $totalSql = 'SELECT COUNT(1) as count FROM '.$this->objDPickingIndex->get_Table().$whereSql;
  364. $dbTotalResult = $this->objDPickingIndex->query($totalSql);
  365. if ($dbTotalResult === false) {
  366. return ResultWrapper::fail($this->objDPickingIndex->error(), ErrorCode::$dberror);
  367. }
  368. if(empty($dbTotalResult)){
  369. return ResultWrapper::success([]);
  370. }
  371. $return = [
  372. 'data' => $pickDbResult,
  373. 'total' => $dbTotalResult[0]['count']
  374. ];
  375. return ResultWrapper::success($return);
  376. }
  377. /**
  378. * 拣货明细(根据商品维度进行统计)
  379. */
  380. public function getAllPickingGoodsDetail($selectParams)
  381. {
  382. // $timeName = $selectParams['today'];
  383. $time = gettimestamp('preday',time());
  384. $limit = $selectParams['limit'];
  385. unset($selectParams['limit']);
  386. $offset = $selectParams['offset'];
  387. unset($selectParams['offset']);
  388. //组装sql where条件
  389. $whereSql = '';
  390. if (isset($selectParams['no']) && !empty($selectParams['no'])) {
  391. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  392. $whereSql .= $where . ' p.no like "%' . $selectParams['no'] . '%"';
  393. }
  394. if (isset($selectParams['warehouseId']) && !empty($selectParams['warehouseId'])) {
  395. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  396. $whereSql .= $where . ' p.warehouseId = ' . $selectParams['warehouseId'];
  397. }
  398. if (isset($selectParams['personnel']) && !empty($selectParams['personnel'])) {
  399. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  400. $whereSql .= $where . ' p.personnel = ' . $selectParams['personnel'];
  401. }
  402. if (isset($selectParams['goodsName']) && !empty($selectParams['goodsName'])) {
  403. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  404. $whereSql .= $where . ' g.goodsName = ' . $selectParams['goodsName'];
  405. }
  406. if (isset($selectParams['originId']) && !empty($selectParams['originId'])) {
  407. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  408. $whereSql .= $where . ' g.originId = ' . $selectParams['originId'];
  409. }
  410. if (isset($selectParams['originNo']) && !empty($selectParams['originNo'])) {
  411. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  412. $whereSql .= $where . ' p.originNo like "%' . $selectParams['originNo'] . '%"';
  413. }
  414. if (isset($selectParams['currentUnitId']) && !empty($selectParams['currentUnitId'])) {
  415. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  416. $whereSql .= $where . ' p.currentUnitId = ' . $selectParams['currentUnitId'];
  417. }
  418. if (isset($selectParams['managerId']) && !empty($selectParams['managerId'])) {
  419. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  420. $whereSql .= $where . ' p.managerId = ' . $selectParams['managerId'];
  421. }
  422. if (isset($selectParams['pickingStatus']) && !empty($selectParams['pickingStatus'])) {
  423. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  424. $whereSql .= $where . ' p.pickingStatus = ' . $selectParams['pickingStatus'];
  425. }
  426. //时间删选 有timeName字段优先
  427. if( isset($selectParams['timeName']) && !empty($selectParams['timeName']) ){
  428. $timeName = $selectParams['timeName'];
  429. $timestamp = gettimestamp($timeName,time());
  430. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  431. $whereSql .= $where . ' p.createTime BETWEEN ' . $timestamp['start'] . ' AND '. $timestamp['end'];
  432. }else{
  433. if ( (isset($selectParams['start']) && !empty($selectParams['start']))&&(isset($selectParams['end']) && !empty($selectParams['end'])) ) {
  434. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  435. $whereSql .= $where . ' p.createTime BETWEEN ' . $selectParams['start'] . ' AND '. $selectParams['end'];
  436. }
  437. }
  438. $sql = 'select p.createTime,p.pickingStatus,p.no,p.currentUnitId,p.currentUnit,p.personnel,p.personnelName,p.managerId,p.managerName,g.* from '.$this->objDPicking->get_Table().' as p left join '.$this->objDPickingGoods->get_Table().' as g on p.id = g.pickingId'.$whereSql.' LIMIT '. $offset . ' , ' . $limit;
  439. $dbResult = $this->objDPicking->query($sql);
  440. if ($dbResult === false) {
  441. return ResultWrapper::fail($this->objDPicking->error(), ErrorCode::$dberror);
  442. }
  443. //渲染编号
  444. foreach ($dbResult as $k => $v){
  445. $dbResult[$k]['no'] = StatusCode::$noPrefix[31].'-'.$v['no'];
  446. }
  447. $totalSql = 'select COUNT(1) AS count from '.$this->objDPicking->get_Table().' as p left join '.$this->objDPickingGoods->get_Table().' as g on p.id = g.pickingId'.$whereSql;
  448. $dbTotalResult = $this->objDPickingIndex->query($totalSql);
  449. if ($dbTotalResult === false) {
  450. return ResultWrapper::fail($this->objDPickingIndex->error(), ErrorCode::$dberror);
  451. }
  452. if(empty($dbTotalResult)){
  453. return ResultWrapper::success([]);
  454. }
  455. $return = [
  456. 'data' => $dbResult,
  457. 'total' => $dbTotalResult[0]['count']
  458. ];
  459. return ResultWrapper::success($return);
  460. }
  461. }