MAllocate.Class.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. <?php
  2. /**
  3. * 仓库调拨管理Model
  4. * Created by PhpStorm.
  5. * User: 小威
  6. * Date: 2019/11/09
  7. * Time: 15:00
  8. */
  9. namespace JinDouYun\Model\Stock;
  10. use Dining\Dao\ShopManage\DTableAdministration;
  11. use Exception;
  12. use JinDouYun\Cache\Stock;
  13. use JinDouYun\Dao\Stock\DInventoryOut;
  14. use JinDouYun\Model\MBaseModel;
  15. use Mall\Framework\Core\ErrorCode;
  16. use Mall\Framework\Core\StatusCode;
  17. use Mall\Framework\Core\ResultWrapper;
  18. use JinDouYun\Dao\Stock\DAllocate;
  19. use JinDouYun\Dao\Stock\DAllocateDetails;
  20. use JinDouYun\Model\Stock\MInventory;
  21. use JinDouYun\Model\Stock\MInventoryOut;
  22. use JinDouYun\Cache\GoodsBasicRelevant;
  23. class MAllocate extends MBaseModel
  24. {
  25. private $objDAllocateDetails;
  26. private $objDAllocate;
  27. private $userCenterId;
  28. private $enterpriseId;
  29. public function __construct($enterpriseId, $userCenterId = false)
  30. {
  31. $this->enterpriseId = $enterpriseId;
  32. $this->userCenterId = $userCenterId;
  33. $this->objDAllocateDetails = new DAllocateDetails('stock');
  34. $this->objDAllocate = new DAllocate('stock');
  35. $this->objDAllocateDetails->setTable($this->objDAllocateDetails->get_Table() . '_' . $enterpriseId);
  36. $this->objDAllocate->setTable($this->objDAllocate->get_Table() . '_' . $enterpriseId);
  37. //$this->objDAllocateDetails->setSearchIndex('allocate_search')->setType('allocate');
  38. }
  39. /**
  40. * 仓库调拨记录添加
  41. * @param $params
  42. * @return ResultWrapper
  43. * @throws Exception
  44. */
  45. public function addAllocate($params)
  46. {
  47. $details = $params['details'];
  48. unset($params['details']);
  49. unset($params['deleteArray']);
  50. $this->objDAllocate->beginTransaction();
  51. // 生成编号
  52. $dbResult = $this->objDAllocate->get('createTime >='.strtotime(date('Ymd'.'0:0:0')), 'no', 'createTime desc');
  53. if ($dbResult === false) {
  54. return ResultWrapper::fail($this->objDAllocate->error(), ErrorCode::$dberror);
  55. }
  56. if(empty($dbResult)){
  57. $params['no'] = createSerialNumberByDate('');
  58. }else{
  59. $params['no'] = createSerialNumberByDate($dbResult['no']);
  60. }
  61. $params['deleteStatus'] = StatusCode::$standard;
  62. $params['auditStatus'] = StatusCode::$auditStatus['auditing'];
  63. $params['auditId'] = 0;
  64. $params['auditName'] = '';
  65. $params['auditTime'] = 0;
  66. $params['createTime'] = time();
  67. $params['updateTime'] = time();
  68. //增加调拨记录
  69. $dbResult = $this->objDAllocate->insert($params);
  70. if ($dbResult === false) {
  71. $this->objDAllocate->rollBack();
  72. return ResultWrapper::fail($this->objDAllocate->error(), ErrorCode::$dberror);
  73. }
  74. $returnData = $dbResult;
  75. unset($dbResult);
  76. $batchParams = [];
  77. foreach ($details as &$value) {
  78. $value['linkId'] = $returnData;
  79. $value['linkNo'] = $params['no'];
  80. //查询批次
  81. $batchParams[] = [
  82. 'warehouseId' => $params['warehouseId'],
  83. 'materielId' => $value['materielId'],
  84. 'num' => $value['num'],
  85. 'otherNum' => $value['otherNum'],
  86. ];
  87. }
  88. unset($value);
  89. //获取批次数据
  90. $objMInventory = new MInventory($this->enterpriseId, $this->userCenterId);
  91. $modelResult = $objMInventory->getBatchByInventoryOut($batchParams, true);
  92. if (!$modelResult->isSuccess()) {
  93. $this->objDAllocate->rollBack();
  94. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  95. }
  96. $batchData = $modelResult->getData();
  97. unset($modelResult);
  98. foreach ($details as &$value) {
  99. $value['createTime'] = time();
  100. $value['updateTime'] = time();
  101. $mdKey = md5($params['warehouseId'] . $value['materielId']);
  102. $value['batch'] = json_encode($batchData[$mdKey]);
  103. }
  104. unset($value);
  105. $dbResult = $this->objDAllocateDetails->insert($details, true);
  106. if ($dbResult === false) {
  107. $this->objDAllocate->rollBack();
  108. return ResultWrapper::fail($this->objDAllocateDetails->error(), ErrorCode::$dberror);
  109. }
  110. $detailsId = $dbResult;
  111. unset($dbResult);
  112. //增加es搜索
  113. /*foreach ($details as $key => $value) {
  114. $modelResult = self::updateEsData($value, $params, $detailsId[$key], true);
  115. if (!$modelResult->isSuccess()) {
  116. $this->objDAllocate->rollBack();
  117. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  118. }
  119. }*/
  120. $this->objDAllocate->commit();
  121. return ResultWrapper::success($returnData);
  122. }
  123. /**
  124. * 仓库调拨记录删除
  125. * @param $where
  126. * @return ResultWrapper
  127. */
  128. public function deleteAllocate($where)
  129. {
  130. $params = [
  131. 'deleteStatus' => StatusCode::$delete,
  132. 'updateTime' => time(),
  133. ];
  134. $dbResult = $this->objDAllocate->update($params, $where);
  135. if ($dbResult === false) {
  136. return ResultWrapper::fail($this->objDAllocate->error(), ErrorCode::$dberror);
  137. }
  138. $returnData = $dbResult;
  139. unset($dbResult);
  140. $dbResult = $this->objDAllocateDetails->select(['linkId' => $where['id']], 'id');
  141. if ($dbResult === false) {
  142. return ResultWrapper::fail($this->objDAllocate->error(), ErrorCode::$dberror);
  143. }
  144. $detailsIds = $dbResult;
  145. unset($dbResult);
  146. /*foreach ($detailsIds as $value) {
  147. //修改es
  148. $esId = parent::setEsId($this->enterpriseId, 'allocateId', $value);
  149. $esResult = $this->objDAllocateDetails->esupdateTypeFieldVaule(['deleteStatus' => StatusCode::$delete, 'updateTime' => time()], $esId);
  150. if (!$esResult) {
  151. return ResultWrapper::fail($esResult, ErrorCode::$paramError);
  152. }
  153. }*/
  154. return ResultWrapper::success($returnData);
  155. }
  156. /**
  157. * 仓库调拨审核
  158. * @param $updateData
  159. * @return ResultWrapper
  160. * @throws Exception
  161. */
  162. public function auditAllocate($updateData)
  163. {
  164. $where['id'] = $updateData['id'];
  165. unset($updateData['id']);
  166. $updateData['auditStatus'] = StatusCode::$auditStatus['auditPass'];
  167. $updateData['auditTime'] = time();
  168. $beginStatus = $this->objDAllocate->beginTransaction();
  169. //查询调拨信息
  170. $dbResult = $this->objDAllocate->get($where);
  171. if ($dbResult === false) {
  172. $this->objDAllocate->rollBack();
  173. return ResultWrapper::fail($this->objDAllocate->error(), ErrorCode::$dberror);
  174. }
  175. if (empty($dbResult)) {
  176. $this->objDAllocate->rollBack();
  177. return ResultWrapper::fail('调拨信息为空', ErrorCode::$dberror);
  178. }
  179. $AllocateData = $dbResult;
  180. unset($dbResult);
  181. if ($AllocateData['auditStatus'] == StatusCode::$auditStatus['auditPass']) {
  182. $this->objDAllocate->rollBack();
  183. return ResultWrapper::fail('该单据已审核', ErrorCode::$paramError);
  184. }
  185. // 查询调拨单详情数据
  186. $condition = [
  187. 'linkId' => $where['id'],
  188. 'deleteStatus' => StatusCode::$standard
  189. ];
  190. $dbResult = $this->objDAllocateDetails->select($condition);
  191. if ($dbResult === false) {
  192. $this->objDAllocate->rollBack();
  193. return ResultWrapper::fail($this->objDAllocateDetails->error(), ErrorCode::$dberror);
  194. }
  195. $AllocateData['details'] = $dbResult;
  196. unset($dbResult);
  197. $AllocateData = array_merge($AllocateData, $updateData);
  198. // 组装调拨出库单数据
  199. $outInventoryData = [
  200. 'originId' => $AllocateData['id'],
  201. 'originNo' => $AllocateData['no'],
  202. 'sourceId' => $AllocateData['id'],
  203. 'sourceNo' => $AllocateData['no'],
  204. 'materielNum' => 0,
  205. 'operatorId' => $this->userCenterId,
  206. 'operatorName' => $updateData['auditName'],
  207. 'remark' => $AllocateData['remark'],
  208. 'source' => StatusCode::$orderType['allocate'],
  209. 'type' => StatusCode::$orderType['allocateOut'],
  210. ];
  211. $outInventoryData['amount'] = 0;
  212. $outInventoryData['orderGoodsData'] = [];
  213. foreach ($AllocateData['details'] as $value) {
  214. $outInventoryData['materielNum']++;
  215. $outInventoryData['amount'] = bcadd($outInventoryData['amount'], bcmul($value['costUnitPrice'], $value['num']));
  216. $details = [
  217. 'materielId' => $value['materielId'],
  218. 'materielName' => $value['materielName'],
  219. 'materielCode' => $value['materielCode'],
  220. 'outWarehouse' => json_encode([['warehouseId' => $AllocateData['warehouseId'],'num' => $value['num']]]),
  221. 'skuId' => $value['skuId'],
  222. 'unitName' => isset($value['unitName']) ? $value['unitName'] : '',
  223. 'skuName' => isset($value['skuName']) ? $value['skuName'] : '',
  224. 'num' => 0,
  225. 'outNum' => $value['num'],
  226. 'otherNum' => $value['otherNum'],
  227. 'total' => $value['num'],
  228. 'unitPrice' => $value['costUnitPrice'],
  229. 'totalPrice' => bcmul($value['costUnitPrice'], $value['num']),
  230. 'isEq' => isset($value['isEq']) ? $value['isEq'] : 4
  231. ];
  232. $outInventoryData['orderGoodsData'][] = $details;
  233. //修改es
  234. /*$esId = parent::setEsId($this->enterpriseId, 'allocateId', $value['id']);
  235. $esResult = $this->objDAllocateDetails->esupdateTypeFieldVaule($updateData, $esId);
  236. if (!$esResult) {
  237. $this->objDAllocate->rollBack();
  238. return ResultWrapper::fail($esResult, ErrorCode::$paramError);
  239. }*/
  240. }
  241. // 新增调拨出库单
  242. $objMInventoryOut = new MInventoryOut($this->enterpriseId, $this->userCenterId);
  243. $modelResult = $objMInventoryOut->addInventoryOut($outInventoryData);
  244. if (!$modelResult->isSuccess()) {
  245. $this->objDAllocate->rollBack();
  246. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  247. }
  248. //更改审核状态
  249. $dbResult = $this->objDAllocate->update($updateData, $where);
  250. if ($dbResult === false) {
  251. $this->objDAllocate->rollBack();
  252. return ResultWrapper::fail($this->objDAllocate->error(), ErrorCode::$dberror);
  253. }
  254. $returnData = $dbResult;
  255. unset($dbResult);
  256. $beginStatus && $this->objDAllocate->commit();
  257. return ResultWrapper::success($returnData);
  258. }
  259. /**
  260. * 仓库调配记录修改
  261. * @param $updateData
  262. * @return ResultWrapper
  263. */
  264. public function updateAllocate($updateData)
  265. {
  266. $where['id'] = $updateData['id'];
  267. unset($updateData['id']);
  268. $details = $updateData['details'];
  269. unset($updateData['details']);
  270. $deleteArray = $updateData['deleteArray'];
  271. unset($updateData['deleteArray']);
  272. unset($updateData['no']);
  273. $this->objDAllocate->beginTransaction();
  274. if (isset($updateData['extend'])) unset($updateData['extend']);
  275. //查询要修改的单据
  276. $dbResult = $this->objDAllocate->get($where);
  277. if($dbResult === false){
  278. return ResultWrapper::fail($this->objDAllocate->error(), ErrorCode::$paramError);
  279. }
  280. $allocateData = $dbResult;
  281. unset($dbResult);
  282. $dbResult = $this->objDAllocate->update($updateData, $where);
  283. if ($dbResult === false) {
  284. $this->objDAllocate->rollBack();
  285. return ResultWrapper::fail($this->objDAllocate->error(), ErrorCode::$dberror);
  286. }
  287. $returnData = $dbResult;
  288. unset($dbResult);
  289. $addDetailsData = [];
  290. foreach ($details as &$value) {
  291. $id = isset($value['id']) && !empty($value['id']) ? $value['id'] : 0;
  292. unset($value['id']);
  293. isset($value['batch']) && $value['batch'] = json_encode($value['batch']);
  294. if ($id) {
  295. //修改
  296. $dbResult = $this->objDAllocateDetails->update($value, ['id' => $id]);
  297. if ($dbResult === false) {
  298. $this->objDAllocate->rollBack();
  299. return ResultWrapper::fail($this->objDAllocateDetails->error(), ErrorCode::$dberror);
  300. }
  301. unset($dbResult);
  302. //修改es
  303. /*$esId = parent::setEsId($this->enterpriseId, 'allocateId', $id);
  304. $esResult = $this->objDAllocateDetails->esupdateTypeFieldVaule($updateData, $esId);
  305. if (!$esResult) {
  306. $this->objDAllocate->rollBack();
  307. return ResultWrapper::fail($esResult, ErrorCode::$paramError);
  308. }*/
  309. } else {
  310. //新增
  311. $addDetails = [
  312. 'linkId' => $where['id'],
  313. 'linkNo' => $updateData['no'],
  314. 'materielId' => $value['materielId'],
  315. 'materielName' => $value['materielName'],
  316. 'materielCode' => $value['materielCode'],
  317. 'skuId' => $value['skuId'],
  318. 'num' => $value['num'],
  319. 'otherNum' => $value['otherNum'],
  320. 'inAreaId' => isset($value['inAreaId']) ? $value['inAreaId'] : 0,
  321. 'inAreaName' => isset($value['inAreaName']) ? $value['inAreaName'] : '',
  322. 'inAreaCode' => isset($value['inAreaCode']) ? $value['inAreaCode'] : '',
  323. 'inStorageLocationId' => isset($value['inStorageLocationId']) ? $value['inStorageLocationId'] : 0,
  324. 'inStorageLocationName' => isset($value['inStorageLocationName']) ? $value['inStorageLocationName'] : '',
  325. 'inStorageLocationCode' => isset($value['inStorageLocationCode']) ? $value['inStorageLocationCode'] : '',
  326. 'outAreaId' => isset($value['outAreaId']) ? $value['outAreaId'] : 0,
  327. 'outAreaName' => isset($value['outAreaName']) ? $value['outAreaName'] : '',
  328. 'outAreaCode' => isset($value['outAreaCode']) ? $value['outAreaCode'] : '',
  329. 'outStorageLocationId' => isset($value['outStorageLocationId']) ? $value['outStorageLocationId'] : 0,
  330. 'outStorageLocationName' => isset($value['outStorageLocationName']) ? $value['outStorageLocationName'] : '',
  331. 'outStorageLocationCode' => isset($value['outStorageLocationCode']) ? $value['outStorageLocationCode'] : '',
  332. 'costUnitPrice' => $value['costUnitPrice'],
  333. 'deleteStatus' => StatusCode::$standard,
  334. 'createTime' => time(),
  335. 'updateTime' => time(),
  336. 'isEq' => $value['isEq'],
  337. ];
  338. isset($value['batch']) && $addDetails['batch'] = $value['batch'];
  339. $addDetailsData[] = $addDetails;
  340. }
  341. }
  342. //删除
  343. if ($deleteArray) {
  344. $dbResult = $this->objDAllocateDetails->update(['deleteStatus' => StatusCode::$delete, 'updateTime' => time()], ['id' => $deleteArray]);
  345. if ($dbResult === false) {
  346. $this->objDAllocate->rollBack();
  347. return ResultWrapper::fail($this->objDAllocateDetails->error(), ErrorCode::$dberror);
  348. }
  349. //es删除
  350. /*foreach ($deleteArray as $value) {
  351. $esResult = $this->objDAllocateDetails->esupdateTypeFieldVaule(['deleteStats' => StatusCode::$delete, 'updateTime' => time()], $value);
  352. if (!$esResult) {
  353. return ResultWrapper::fail($esResult, ErrorCode::$paramError);
  354. }
  355. }*/
  356. }
  357. //新增
  358. if ($addDetailsData) {
  359. $dbResult = $this->objDAllocateDetails->insert($addDetailsData, true);
  360. if ($dbResult === false) {
  361. return ResultWrapper::fail($this->objDAllocateDetails->error(), ErrorCode::$dberror);
  362. }
  363. $detailsIds = $dbResult;
  364. unset($dbResult);
  365. //新增es
  366. /*foreach ($addDetailsData as $key => $value) {
  367. $modelResult = self::updateEsData($value, $allocateData, $detailsIds[$key], true);
  368. if (!$modelResult->isSuccess()) {
  369. $this->objDAllocate->rollBack();
  370. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  371. }
  372. }*/
  373. }
  374. $this->objDAllocate->commit();
  375. return ResultWrapper::success($returnData);
  376. }
  377. /**
  378. * 仓库调配记录列表
  379. * @param $selectParams
  380. * @param $export
  381. * @return ResultWrapper
  382. * @throws Exception
  383. */
  384. public function getAllAllocate($selectParams,$export = 0)
  385. {
  386. /*
  387. * 'warehouseId' => isset($params['warehouseId']) ? $params['warehouseId'] : '',
  388. 'inWarehouseId' => isset($params['inWarehouseId']) ? $params['inWarehouseId'] : '',
  389. 'auditStatus' => isset($params['auditStatus']) ? $params['auditStatus'] : '',
  390. 'start' => isset($params['start']) ? $params['start'] : '',
  391. 'end' => isset($params['end']) ? $params['end'] : '',
  392. 'search' => isset($params['search']) ? $params['search'] : '',
  393. */
  394. $limit = $selectParams['limit'];
  395. unset($selectParams['limit']);
  396. $offset = $selectParams['offset'];
  397. unset($selectParams['offset']);
  398. $sql = "select a.*,d.materielId,d.materielCode,d.materielName,d.num,d.skuId,d.costUnitPrice,d.batch,d.skuId,d.otherNum from " . $this->objDAllocate->get_Table() . " a left join " . $this->objDAllocateDetails->get_Table() . " d on a.id = d.linkId";
  399. $whereSql = ' where a.deleteStatus = ' . StatusCode::$standard.' and d.deleteStatus = '.StatusCode::$standard;
  400. if(isset($selectParams['search']) && !empty($selectParams['search'])){
  401. if (strstr($selectParams['search'],StatusCode::$noPrefix['32'])){
  402. $selectParams['search'] = substr($selectParams['search'],strlen(StatusCode::$noPrefix['32'])+1);
  403. }
  404. $whereSql .= ' and (a.no like "%'.$selectParams['search'].'%" or d.materielName like "%'.$selectParams['search'].'%")';
  405. }
  406. if(isset($selectParams['warehouseId']) && !empty($selectParams['warehouseId'])){
  407. $whereSql .= ' and a.warehouseId = '.$selectParams['warehouseId'];
  408. }
  409. if(isset($selectParams['inWarehouseId']) && !empty($selectParams['inWarehouseId'])){
  410. $whereSql .= ' and a.inWarehouseId = '.$selectParams['inWarehouseId'];
  411. }
  412. if(isset($selectParams['start']) && isset($selectParams['end']) && !empty($selectParams['start']) && !empty($selectParams['end'])){
  413. $whereSql .= ' and (a.createTime between '.$selectParams['start'].' and '.$selectParams['end'].')';
  414. }
  415. if(isset($selectParams['auditStatus']) && !empty($selectParams['auditStatus'])){
  416. $whereSql .= ' and a.auditStatus = '.$selectParams['auditStatus'];
  417. }
  418. $orderSql = ' ORDER BY a.createTime desc';
  419. $limitSql = ' limit '. $offset . "," . $limit;
  420. if($export){
  421. $limitSql = '';
  422. }
  423. $dbResult = $this->objDAllocate->query($sql.$whereSql.$orderSql.$limitSql);
  424. if ($dbResult === false) {
  425. return ResultWrapper::fail($this->objDAllocate->error(), ErrorCode::$dberror);
  426. }
  427. $sql = "select count(d.id) as num from " . $this->objDAllocate->get_Table() . " a left join " . $this->objDAllocateDetails->get_Table() . " d on a.id = d.linkId";
  428. $dbDAllocateResult = $this->objDAllocate->query($sql.$whereSql);
  429. if ($dbDAllocateResult === false) {
  430. return ResultWrapper::fail($this->objDAllocate->error(), ErrorCode::$dberror);
  431. }
  432. $DAllocate = array_shift($dbDAllocateResult);
  433. $total = $DAllocate['num'];
  434. foreach ($dbResult as &$value) {
  435. $value['batch'] = json_decode($value['batch'], true);
  436. }
  437. $formatData = parent::formatOrderMan($this->enterpriseId, $dbResult);
  438. foreach ($formatData as $key => $val){
  439. if( isset($val['no']) && !empty($val['no']) ){
  440. $formatData[$key]['no'] = StatusCode::$noPrefix['32'].'-'.$val['no'];
  441. }
  442. }
  443. //仓库库存导出
  444. if($export){
  445. self::exportAllocate($formatData);
  446. exit;
  447. }
  448. $return = [
  449. 'data' => $formatData,
  450. 'total' => ($total) ? intval($total) : 0,
  451. ];
  452. return ResultWrapper::success($return);
  453. }
  454. /**
  455. * 调拨搜索
  456. * @param $params
  457. * @return ResultWrapper
  458. * @throws Exception
  459. */
  460. public function searchAllocate($params)
  461. {
  462. $defaultDSL = ['from' => $params['offset'], 'size' => $params['limit'], 'sort' => ['createTime' => ['order' => 'desc']]];
  463. $selectParams = [];
  464. $selectParams[] = ['term' => ['enterpriseId' => $this->enterpriseId]];
  465. !empty($params['warehouseId']) && $selectParams[] = ['term' => ['warehouseId' => $params['warehouseId']]];
  466. !empty($params['inWarehouseId']) && $selectParams[] = ['term' => ['inWarehouseId' => $params['inWarehouseId']]];
  467. !empty($params['auditStatus']) && $selectParams[] = ['term' => ['auditStatus' => $params['auditStatus']]];
  468. (!empty($params['start']) && !empty($params['end'])) && $selectParams[] = ['range' => ['createTime' => ['gte' => $params['start'], 'lte' => $params['end']]]];
  469. !empty($params['search']) && $selectParams[] = ['multi_match' => ['fields' => ['no', 'materielName'], 'query' => $params['search'], 'fuzziness' => 'AUTO']];
  470. $dsl = [];
  471. !empty($selectParams) && $dsl['query']['bool']['must'][] = $selectParams;
  472. $dsl = array_merge($defaultDSL, $dsl);
  473. $dbResult = $this->objDAllocateDetails->getSearchQueryDsl($dsl);
  474. if (isset($dbResult['error'])) {
  475. return ResultWrapper::fail($dbResult, ErrorCode::$dberror);
  476. }
  477. $returnData = parent::formatEsSelectData($dbResult, $this->enterpriseId);
  478. return ResultWrapper::success($returnData);
  479. }
  480. /**
  481. * 仓库调拨详情
  482. * @param $params
  483. * @return ResultWrapper
  484. * @throws Exception
  485. */
  486. public function getAllocateInfo($params)
  487. {
  488. $params['deleteStatus'] = StatusCode::$standard;
  489. $dbResult = $this->objDAllocate->get($params);
  490. if ($dbResult === false) {
  491. return ResultWrapper::fail($this->objDAllocate->error(), ErrorCode::$dberror);
  492. }
  493. $returnData = $dbResult;
  494. unset($dbResult);
  495. $dbResult = $this->objDAllocateDetails->select(['linkId' => $params['id'], 'deleteStatus' => StatusCode::$standard]);
  496. if ($dbResult === false) {
  497. return ResultWrapper::fail($this->objDAllocateDetails->error(), ErrorCode::$dberror);
  498. }
  499. $returnData['details'] = $dbResult;
  500. unset($dbResult);
  501. $skuIds = [];
  502. foreach ($returnData['details'] as &$value) {
  503. $value['batch'] = json_decode($value['batch'], true);
  504. $skuIds[] = $value['skuId'];
  505. }
  506. unset($value);
  507. $objMInventory = new MInventory($this->enterpriseId, $this->userCenterId);
  508. $modelResult = $objMInventory->getWarehouseInventoryData($returnData['warehouseId'], $skuIds);
  509. if (!$modelResult->isSuccess()) {
  510. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  511. }
  512. $inventoryData = $modelResult->getData();
  513. unset($modelResult);
  514. foreach ($returnData['details'] as &$value) {
  515. $value['inventoryNum'] = isset($inventoryData[$value['skuId']]) ? $inventoryData[$value['skuId']]['num'] : 0;
  516. }
  517. $returnData = parent::formatOrderMan($this->enterpriseId, $returnData);
  518. return ResultWrapper::success($returnData);
  519. }
  520. /**
  521. * 查询调拨单数据
  522. * @param $where
  523. * @return ResultWrapper
  524. */
  525. public function getAllocateData($where)
  526. {
  527. $where['deleteStatus'] = StatusCode::$standard;
  528. $dbResult = $this->objDAllocate->select($where);
  529. if($dbResult === false){
  530. return ResultWrapper::fail($this->objDAllocate->error(), ErrorCode::$dberror);
  531. }
  532. return ResultWrapper::success($dbResult);
  533. }
  534. /**
  535. * 查询调拨主单据数据
  536. * @param $where
  537. * @return ResultWrapper
  538. */
  539. public function getAllocate($where)
  540. {
  541. $dbResult = $this->objDAllocate->get($where);
  542. if ($dbResult === false) {
  543. return ResultWrapper::fail($this->objDAllocate->error(), ErrorCode::$dberror);
  544. }
  545. return ResultWrapper::success($dbResult);
  546. }
  547. /**
  548. * 添加es
  549. * @param $data
  550. * @param $params
  551. * @param $id
  552. * @param bool $type
  553. * @return ResultWrapper
  554. */
  555. public function updateEsData($data, $params, $id, $type = false)
  556. {
  557. $addEsData = [
  558. 'id' => $id,
  559. 'enterpriseId' => $this->enterpriseId,
  560. 'no' => isset($params['no']) ? $params['no'] : false,
  561. 'warehouseId' => isset($params['warehouseId']) ? $params['warehouseId'] : false,
  562. 'inWarehouseId' => isset($params['inWarehouseId']) ? $params['inWarehouseId'] : false,
  563. 'operatorId' => isset($params['operatorId']) ? $params['operatorId'] : false,
  564. 'operatorName' => isset($params['operatorName']) ? $params['operatorName'] : false,
  565. 'auditId' => isset($params['auditId']) ? $params['auditId'] : null,
  566. 'auditName' => isset($params['auditName']) ? $params['auditName'] : null,
  567. 'remark' => isset($params['remark']) ? $params['remark'] : false,
  568. 'deleteStatus' => isset($params['deleteStatus']) ? $params['deleteStatus'] : false,
  569. 'auditStatus' => isset($params['auditStatus']) ? $params['auditStatus'] : null,
  570. 'auditTime' => isset($params['auditTime']) ? $params['auditTime'] : null,
  571. 'createTime' => isset($params['createTime']) ? $params['createTime'] : false,
  572. 'updateTime' => isset($params['updateTime']) ? $params['updateTime'] : false,
  573. 'linkId' => isset($data['linkId']) ? $data['linkId'] : false,
  574. 'materielId' => isset($data['materielId']) ? $data['materielId'] : false,
  575. 'materielName' => isset($data['materielName']) ? $data['materielName'] : false,
  576. 'materielCode' => isset($data['materielCode']) ? $data['materielCode'] : false,
  577. 'skuId' => isset($data['skuId']) ? $data['skuId'] : false,
  578. 'unitName' => isset($data['unitName']) ? $data['unitName'] : false,
  579. 'skuName' => isset($data['skuName']) ? $data['skuName'] : false,
  580. 'num' => isset($data['num']) ? $data['num'] : false,
  581. 'batch' => isset($data['batch']) ? json_decode($data['batch'], true) : null,
  582. 'costUnitPrice' => isset($data['costUnitPrice']) ? $data['costUnitPrice'] : false,
  583. ];
  584. foreach ($addEsData as $key => $value) {
  585. if ($value === false) {
  586. if ($type) {
  587. return ResultWrapper::fail($key . '字段未设置ES', ErrorCode::$paramError);
  588. } else {
  589. unset($addEsData[$key]);
  590. }
  591. }
  592. }
  593. //创建es id
  594. $esId = self::esId($id);
  595. $result = $this->objDAllocateDetails->addUpSearchIndexDocument($addEsData, $esId);
  596. if (isset($result['_shards']) && isset($result['_shards']['successful']) && $result['_shards']['successful'] == 1) {
  597. return ResultWrapper::success(isset($result['_id']) ? $result['_id'] : false);
  598. }
  599. return ResultWrapper::fail($result['error']['reason'], ErrorCode::$paramError);
  600. }
  601. /**
  602. * 拼接es id
  603. * @param $id
  604. * @return string
  605. */
  606. public function esId($id)
  607. {
  608. return 'EnterpriseId_' . $this->enterpriseId . '_allocateId_' . $id;
  609. }
  610. /**
  611. * 调拨单导出方法
  612. * @param $result
  613. * @return void
  614. * @throws Exception
  615. */
  616. public function exportAllocate($result)
  617. {
  618. //导出到本地
  619. header("Content-type:application/vnd.ms-excel");
  620. header("Content-Disposition:filename=调拨单记录表.csv");
  621. header('Cache-Control: max-age=0');
  622. $fp = fopen('php://output', 'a');
  623. $head = ['单号', '商品编码', '商品名称', '规格', '调出仓库','调入仓库','数量','其他单位','单价','金额','操作人']; //定义标题
  624. foreach ($head as $i => $v) {
  625. $head[$i] = mb_convert_encoding($v, 'GBK', 'utf-8'); //将中文标题转换编码,否则乱码
  626. }
  627. fputcsv($fp, $head);
  628. $limit = 10000;
  629. $num = 0; //计数器
  630. foreach ($result as $v) { //循环数据
  631. $num++;
  632. if ($num == $limit) {
  633. ob_flush(); //释放内存
  634. flush();
  635. }
  636. $rows['no'] = isset($v['materielCode']) ? $v['materielCode'] : '';//单号
  637. $rows['materielCode'] = isset($v['materielCode']) ? $v['materielCode'] : '';//商品编码
  638. $rows['materielName'] = isset($v['materielName']) ? $v['materielName'] : '';//商品名称
  639. $rows['unitName'] = isset($v['unitName']) ? $v['unitName'] : '';//规格
  640. $rows['warehouseName'] = isset($v['warehouseName']) ? $v['warehouseName'] : '';//调出仓库
  641. $rows['inWarehouseName'] = isset($v['inWarehouseName']) ? $v['inWarehouseName'] : '';//调入仓库
  642. $rows['num'] = isset($v['num']) ? $v['num'] : '';//数量
  643. $rows['otherNum'] = isset($v['otherNum']) ? $v['otherNum'] : '';//其他单位
  644. $rows['costUnitPrice'] = isset($v['costUnitPrice']) ? $v['costUnitPrice'] : '';//单价
  645. $rows['costUnitTotal'] = bcmul($rows['num'],$rows['costUnitPrice'],2);//金额
  646. $rows['operatorName'] = isset($v['operatorName']) ? $v['operatorName'] : '';//操作人
  647. foreach ($rows as $kk => $vv) {
  648. $rs[$kk] = mb_convert_encoding($vv, 'GBK', 'utf-8'); //转译编码
  649. }
  650. fputcsv($fp, $rs);
  651. $rows = [];
  652. }
  653. }
  654. /**
  655. * 查询调拨仓库
  656. */
  657. public function getAllocateInfoByWarehouseName($params)
  658. {
  659. $originIds = [];
  660. foreach ($params as $k => $v){
  661. if(!empty($v['originId'])){
  662. $originIds[]= $v['originId'];
  663. }
  664. }
  665. $where =['id' => $originIds];
  666. $originIdBindWarehouseName = [];
  667. $originIdBindWarehouseId =[];
  668. $originIdBindinWarehouseId =[];
  669. $originIdBindinWarehouseName =[];
  670. if(!empty($originIds)){
  671. $allocateData = $this->objDAllocate->select($where,'id,warehouseId,warehouseName,inWarehouseId,inWarehouseName');
  672. if ($allocateData === false) {
  673. return ResultWrapper::fail($this->objDAllocate->error(), ErrorCode::$dberror);
  674. }
  675. if(!empty($allocateData)){
  676. foreach ($allocateData as $k => $v){
  677. $originIdBindWarehouseId[$v['id']] = $v['warehouseId'];
  678. $originIdBindWarehouseName[$v['id']] = $v['warehouseName'];
  679. $originIdBindinWarehouseId[$v['id']] = $v['inWarehouseId'];
  680. $originIdBindinWarehouseName[$v['id']] = $v['inWarehouseName'];
  681. }
  682. }
  683. // 循环主数据,进行对应的字段映射操作
  684. foreach ($params as $key => $value){
  685. $params[$key]['warehouseId'] = isset($originIdBindWarehouseId[$value['originId']]) ? $originIdBindWarehouseId[$value['originId']] :'';
  686. $params[$key]['warehouseName'] = isset($originIdBindWarehouseName[$value['originId']]) ? $originIdBindWarehouseName[$value['originId']] :'';
  687. $params[$key]['inWarehouseId'] = isset($originIdBindinWarehouseId[$value['originId']]) ? $originIdBindinWarehouseId[$value['originId']] :'';
  688. $params[$key]['inWarehouseName'] = isset($originIdBindinWarehouseName[$value['originId']]) ? $originIdBindinWarehouseName[$value['originId']] :'';
  689. }
  690. }
  691. return ResultWrapper::success($params);
  692. }
  693. }