MReportLoss.Class.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. <?php
  2. /**
  3. * 报损管理Model
  4. * Created by PhpStorm.
  5. * User: haoren
  6. * Date: 2021/03/09
  7. * Time: 12:00
  8. */
  9. namespace JinDouYun\Model\Stock;
  10. use Exception;
  11. use JinDouYun\Model\MBaseModel;
  12. use Mall\Framework\Core\ErrorCode;
  13. use Mall\Framework\Core\StatusCode;
  14. use Mall\Framework\Core\ResultWrapper;
  15. use JinDouYun\Dao\Stock\DReportLoss;
  16. use JinDouYun\Dao\Stock\DReportLossDetails;
  17. use JinDouYun\Model\Stock\MInventory;
  18. use JinDouYun\Model\Stock\MInventoryOut;
  19. class MReportLoss extends MBaseModel
  20. {
  21. private $objDReportLossDetails;
  22. private $objDReportLoss;
  23. private $userCenterId;
  24. private $enterpriseId;
  25. public function __construct($enterpriseId, $userCenterId = false)
  26. {
  27. $this->enterpriseId = $enterpriseId;
  28. $this->userCenterId = $userCenterId;
  29. $this->objDReportLoss = new DReportLoss('stock');
  30. $this->objDReportLossDetails = new DReportLossDetails('stock');
  31. $this->objDReportLoss->setTable('qianniao_report_loss_' . $enterpriseId);
  32. $this->objDReportLossDetails->setTable('qianniao_report_loss_details_' . $enterpriseId);
  33. }
  34. /**
  35. * 库存报损添加
  36. * @param $params
  37. * @return ResultWrapper
  38. * @throws Exception
  39. */
  40. public function addReportLoss($params)
  41. {
  42. $details = $params['details'];
  43. unset($params['details']);
  44. unset($params['deleteArray']);
  45. // $params['no'] = createOrderSn(StatusCode::$source['manage'], StatusCode::$orderType['allocate'], $this->enterpriseId);
  46. // 生成编号
  47. $dbResult = $this->objDReportLoss->get('createTime >='.strtotime(date('Ymd'.'0:0:0')), 'no', 'createTime desc');
  48. if ($dbResult === false) {
  49. return ResultWrapper::fail($this->objDReportLoss->error(), ErrorCode::$dberror);
  50. }
  51. if(empty($dbResult)){
  52. $params['no'] = createSerialNumberByDate('');
  53. }else{
  54. $params['no'] = createSerialNumberByDate($dbResult['no']);
  55. }
  56. $params['deleteStatus'] = StatusCode::$standard;
  57. $params['auditStatus'] = StatusCode::$auditStatus['auditing'];
  58. $params['auditId'] = 0;
  59. $params['auditName'] = '';
  60. $params['auditTime'] = 0;
  61. $params['createTime'] = time();
  62. $params['updateTime'] = time();
  63. //增加
  64. $beginStatus = $this->objDReportLoss->beginTransaction();
  65. $dbResult = $this->objDReportLoss->insert($params);
  66. if ($dbResult === false) {
  67. $this->objDReportLoss->rollBack();
  68. return ResultWrapper::fail($this->objDReportLoss->error(), ErrorCode::$dberror);
  69. }
  70. $returnData = $dbResult;
  71. unset($dbResult);
  72. foreach ($details as &$value) {
  73. $value['linkId'] = $returnData;
  74. $value['linkNo'] = $params['no'];
  75. $value['createTime'] = time();
  76. $value['updateTime'] = time();
  77. }
  78. unset($value);
  79. $dbResult = $this->objDReportLossDetails->insert($details, true);
  80. if ($dbResult === false) {
  81. $this->objDReportLoss->rollBack();
  82. return ResultWrapper::fail($this->objDReportLossDetails->error(), ErrorCode::$dberror);
  83. }
  84. $beginStatus && $this->objDReportLoss->commit();
  85. return ResultWrapper::success($returnData);
  86. }
  87. /**
  88. * 库存报损删除
  89. * @param $where
  90. * @return ResultWrapper
  91. */
  92. public function deleteReportLoss($where)
  93. {
  94. $params = [
  95. 'deleteStatus' => StatusCode::$delete,
  96. 'updateTime' => time(),
  97. ];
  98. $dbResult = $this->objDReportLoss->update($params, $where);
  99. if ($dbResult === false) {
  100. return ResultWrapper::fail($this->objDReportLoss->error(), ErrorCode::$dberror);
  101. }
  102. $returnData = $dbResult;
  103. return ResultWrapper::success($returnData);
  104. }
  105. /**
  106. * 库存报损审核
  107. * @param $updateData
  108. * @return ResultWrapper
  109. * @throws Exception
  110. */
  111. public function auditReportLoss($updateData)
  112. {
  113. $where['id'] = $updateData['id'];
  114. unset($updateData['id']);
  115. $updateData['auditStatus'] = StatusCode::$auditStatus['auditPass'];
  116. $updateData['auditTime'] = time();
  117. //查询报损信息
  118. $dbResult = $this->objDReportLoss->get($where);
  119. if ($dbResult === false) {
  120. return ResultWrapper::fail($this->objDReportLoss->error(), ErrorCode::$dberror);
  121. }
  122. if (empty($dbResult)) {
  123. return ResultWrapper::fail('库存报损信息为空', ErrorCode::$dberror);
  124. }
  125. $reportLossData = $dbResult;
  126. unset($dbResult);
  127. if ($reportLossData['auditStatus'] == StatusCode::$auditStatus['auditPass']) {
  128. return ResultWrapper::fail('该单据已审核', ErrorCode::$paramError);
  129. }
  130. //查询详情
  131. $dbResult = $this->objDReportLossDetails->select(['linkId' => $where['id']]);
  132. if ($dbResult === false) {
  133. return ResultWrapper::fail($this->objDReportLossDetails->error(), ErrorCode::$dberror);
  134. }
  135. $reportLossData['details'] = $dbResult;
  136. unset($dbResult);
  137. $reportLossData = array_merge($reportLossData, $updateData);
  138. $updateInventoryData = [
  139. 'warehouseId' => $reportLossData['warehouseId'],
  140. 'originId' => $reportLossData['id'],
  141. 'originNo' => $reportLossData['no'],
  142. 'sourceId' => $reportLossData['id'],
  143. 'sourceNo' => $reportLossData['no'],
  144. 'operatorId' => $this->userCenterId,
  145. 'operatorName' => $updateData['auditName'],
  146. 'remark' => $reportLossData['remark'],
  147. 'type' => StatusCode::$orderType['reportLoss'],
  148. ];
  149. $updateInventoryData['details'] = $reportLossData['details'];
  150. $beginStatus = $this->objDReportLoss->beginTransaction();
  151. //减少库存
  152. $objMInventory= new MInventory($this->enterpriseId, $this->userCenterId);
  153. $modelResult = $objMInventory->updateDecInventoryNum($updateInventoryData);
  154. if(!$modelResult->isSuccess()){
  155. $this->objDReportLoss->rollBack();
  156. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  157. }
  158. //更改审核状态
  159. $dbResult = $this->objDReportLoss->update($updateData, $where);
  160. if ($dbResult === false) {
  161. $this->objDReportLoss->rollBack();
  162. return ResultWrapper::fail($this->objDReportLoss->error(), ErrorCode::$dberror);
  163. }
  164. $returnData = $dbResult;
  165. unset($dbResult);
  166. $beginStatus && $this->objDReportLoss->commit();
  167. return ResultWrapper::success($returnData);
  168. }
  169. /**
  170. * 库存报损修改
  171. * @param $updateData
  172. * @return ResultWrapper
  173. */
  174. public function updateReportLoss($updateData)
  175. {
  176. $where['id'] = $updateData['id'];
  177. unset($updateData['id']);
  178. unset($updateData['no']);
  179. $details = $updateData['details'];
  180. unset($updateData['details']);
  181. $deleteArray = $updateData['deleteArray'];
  182. unset($updateData['deleteArray']);
  183. $this->objDReportLoss->beginTransaction();
  184. if (isset($updateData['extend'])) unset($updateData['extend']);
  185. //查询要修改的单据
  186. $dbResult = $this->objDReportLoss->get($where);
  187. if($dbResult === false){
  188. return ResultWrapper::fail($this->objDReportLoss->error(), ErrorCode::$paramError);
  189. }
  190. $allocateData = $dbResult;
  191. unset($dbResult);
  192. $dbResult = $this->objDReportLoss->update($updateData, $where);
  193. if ($dbResult === false) {
  194. $this->objDReportLoss->rollBack();
  195. return ResultWrapper::fail($this->objDReportLoss->error(), ErrorCode::$dberror);
  196. }
  197. $returnData = $dbResult;
  198. unset($dbResult);
  199. $addDetailsData = [];
  200. foreach ($details as &$value) {
  201. $id = isset($value['id']) && !empty($value['id']) ? $value['id'] : 0;
  202. unset($value['id']);
  203. isset($value['batch']) && $value['batch'] = json_encode($value['batch']);
  204. if ($id) {
  205. //修改
  206. $dbResult = $this->objDReportLossDetails->update($value, ['id' => $id]);
  207. if ($dbResult === false) {
  208. $this->objDReportLoss->rollBack();
  209. return ResultWrapper::fail($this->objDReportLossDetails->error(), ErrorCode::$dberror);
  210. }
  211. unset($dbResult);
  212. //修改es
  213. $esId = parent::setEsId($this->enterpriseId, 'allocateId', $id);
  214. $esResult = $this->objDReportLossDetails->esupdateTypeFieldVaule($updateData, $esId);
  215. if (!$esResult) {
  216. $this->objDReportLoss->rollBack();
  217. return ResultWrapper::fail($esResult, ErrorCode::$paramError);
  218. }
  219. } else {
  220. //新增
  221. $addDetails = [
  222. 'linkId' => $where['id'],
  223. 'linkNo' => $updateData['no'],
  224. 'materielId' => $value['materielId'],
  225. 'materielName' => $value['materielName'],
  226. 'materielCode' => $value['materielCode'],
  227. 'skuId' => $value['skuId'],
  228. 'num' => $value['num'],
  229. 'otherNum' => $value['otherNum'],
  230. 'costUnitPrice' => $value['costUnitPrice'],
  231. 'deleteStatus' => StatusCode::$standard,
  232. 'createTime' => time(),
  233. 'updateTime' => time(),
  234. ];
  235. isset($value['batch']) && $addDetails['batch'] = $value['batch'];
  236. $addDetailsData[] = $addDetails;
  237. }
  238. }
  239. //删除
  240. if ($deleteArray) {
  241. $dbResult = $this->objDReportLossDetails->update(['deleteStatus' => StatusCode::$delete, 'updateTime' => time()], ['id' => $deleteArray]);
  242. if ($dbResult === false) {
  243. $this->objDReportLoss->rollBack();
  244. return ResultWrapper::fail($this->objDReportLossDetails->error(), ErrorCode::$dberror);
  245. }
  246. //es删除
  247. foreach ($deleteArray as $value) {
  248. $esResult = $this->objDReportLossDetails->esupdateTypeFieldVaule(['deleteStats' => StatusCode::$delete, 'updateTime' => time()], $value);
  249. if (!$esResult) {
  250. return ResultWrapper::fail($esResult, ErrorCode::$paramError);
  251. }
  252. }
  253. }
  254. //新增
  255. if ($addDetailsData) {
  256. $dbResult = $this->objDReportLossDetails->insert($addDetailsData, true);
  257. if ($dbResult === false) {
  258. return ResultWrapper::fail($this->objDReportLossDetails->error(), ErrorCode::$dberror);
  259. }
  260. $detailsIds = $dbResult;
  261. unset($dbResult);
  262. //新增es
  263. foreach ($addDetailsData as $key => $value) {
  264. $modelResult = self::updateEsData($value, $allocateData, $detailsIds[$key], true);
  265. if (!$modelResult->isSuccess()) {
  266. $this->objDReportLoss->rollBack();
  267. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  268. }
  269. }
  270. }
  271. $this->objDReportLoss->commit();
  272. return ResultWrapper::success($returnData);
  273. }
  274. /**
  275. * 库存报损列表
  276. * @param $selectParams
  277. * @return ResultWrapper
  278. * @throws Exception
  279. */
  280. public function getAllReportLoss($selectParams)
  281. {
  282. $limit = $selectParams['limit'];
  283. unset($selectParams['limit']);
  284. $offset = $selectParams['offset'];
  285. unset($selectParams['offset']);
  286. $whereSql = ' deleteStatus = '.StatusCode::$standard;
  287. if(isset($selectParams['no']) && !empty($selectParams['no'])){
  288. if (strstr($selectParams['no'],StatusCode::$noPrefix['33'])){
  289. $selectParams['no'] = substr($selectParams['no'],strlen(StatusCode::$noPrefix['33'])+1);
  290. }
  291. $str = substr_replace($selectParams['no'].' ','%"',-1);
  292. $no = substr_replace($str,'"%',0,0);
  293. $whereSql .= ' and `no` like ' . $no;
  294. }
  295. if(isset($selectParams['warehouseId']) && !empty($selectParams['warehouseId'])){
  296. $whereSql .= ' and warehouseId = ' . $selectParams['warehouseId'];
  297. }
  298. if(isset($selectParams['auditStatus']) && !empty($selectParams['auditStatus'])){
  299. $whereSql .= ' and auditStatus = ' . $selectParams['auditStatus'];
  300. }
  301. if(isset($selectParams['star']) && !empty($selectParams['star']) && isset($selectParams['end']) && !empty($selectParams['end'])){
  302. $whereSql .= ' and createTime BETWEEN ' . $selectParams['star'] . ' and ' . $selectParams['end'];
  303. }
  304. $dbResult = $this->objDReportLoss->select($whereSql,'*', 'createTime desc', $limit, $offset);
  305. if ($dbResult === false) {
  306. return ResultWrapper::fail($this->objDReportLoss->error(), ErrorCode::$dberror);
  307. }
  308. // 渲染编号
  309. foreach ($dbResult as $key =>$value){
  310. if(isset($dbResult[$key]['no']) && !empty($dbResult[$key]['no']) ){
  311. $dbResult[$key]['no'] = StatusCode::$noPrefix['33'].'-'.$value['no'];
  312. }
  313. }
  314. $countResult = $this->objDReportLoss->count($whereSql);
  315. if ($countResult === false) {
  316. return ResultWrapper::fail($this->objDReportLoss->error(), ErrorCode::$dberror);
  317. }
  318. $formatData = parent::formatOrderMan($this->enterpriseId, $dbResult);
  319. $return = [
  320. 'data' => $formatData,
  321. 'total' => ($countResult) ? intval($countResult) : 0,
  322. ];
  323. return ResultWrapper::success($return);
  324. }
  325. /**
  326. * 库存报损详情
  327. * @param $params
  328. * @return ResultWrapper
  329. * @throws Exception
  330. */
  331. public function getReportLossInfo($params)
  332. {
  333. $params['deleteStatus'] = StatusCode::$standard;
  334. $dbResult = $this->objDReportLoss->get($params);
  335. if ($dbResult === false) {
  336. return ResultWrapper::fail($this->objDReportLoss->error(), ErrorCode::$dberror);
  337. }
  338. $returnData = $dbResult;
  339. unset($dbResult);
  340. $dbResult = $this->objDReportLossDetails->select(['linkId' => $params['id'], 'deleteStatus' => StatusCode::$standard]);
  341. if ($dbResult === false) {
  342. return ResultWrapper::fail($this->objDReportLossDetails->error(), ErrorCode::$dberror);
  343. }
  344. $returnData['details'] = $dbResult;
  345. unset($dbResult);
  346. $returnData = parent::formatOrderMan($this->enterpriseId, $returnData);
  347. return ResultWrapper::success($returnData);
  348. }
  349. /**
  350. * 查询多条报损数据
  351. * @param $where
  352. * @return ResultWrapper
  353. */
  354. public function selectReportLossData($where)
  355. {
  356. $where['deleteStatus'] = StatusCode::$standard;
  357. $dbResult = $this->objDReportLoss->select($where);
  358. if($dbResult === false){
  359. return ResultWrapper::fail($this->objDReportLoss->error(), ErrorCode::$dberror);
  360. }
  361. return ResultWrapper::success($dbResult);
  362. }
  363. /**
  364. * 查询报损数据
  365. * @param $where
  366. * @return ResultWrapper
  367. */
  368. public function getReportLoss($where)
  369. {
  370. $where['deleteStatus'] = StatusCode::$standard;
  371. $dbResult = $this->objDReportLoss->get($where);
  372. if ($dbResult === false) {
  373. return ResultWrapper::fail($this->objDReportLoss->error(), ErrorCode::$dberror);
  374. }
  375. return ResultWrapper::success($dbResult);
  376. }
  377. /**
  378. * 报损导出
  379. * @param $selectParams
  380. * @return ResultWrapper
  381. * @throws Exception
  382. */
  383. public function reportLossExport($selectParams)
  384. {
  385. $whereSql = ' where r.deleteStatus = '.StatusCode::$standard;
  386. if(isset($selectParams['no']) && !empty($selectParams['no'])){
  387. $whereSql .= ' and r.`no` = ' . $selectParams['no'];
  388. }
  389. if(isset($selectParams['warehouseId']) && !empty($selectParams['warehouseId'])){
  390. $whereSql .= ' and r.warehouseId = ' . $selectParams['warehouseId'];
  391. }
  392. if(isset($selectParams['auditStatus']) && !empty($selectParams['auditStatus'])){
  393. $whereSql .= ' and r.auditStatus = ' . $selectParams['auditStatus'];
  394. }
  395. if(isset($selectParams['star']) && !empty($selectParams['star']) && isset($selectParams['end']) && !empty($selectParams['end'])){
  396. $whereSql .= ' and r.createTime BETWEEN ' . $selectParams['star'] . ' and ' . $selectParams['end'];
  397. }
  398. $sql = 'select d.*,r.warehouseId,r.warehouseName from qianniao_report_loss_details_'.$this->enterpriseId.' d left join qianniao_report_loss_'.$this->enterpriseId.' r on d.linkId = r.id';
  399. $orderSql = ' order by d.createTime desc';
  400. $selectSql = $sql.$whereSql.$orderSql;
  401. $dbResult = $this->objDReportLoss->exportQuery($selectSql);
  402. if($dbResult === false){
  403. return ResultWrapper::fail($this->objDReportLoss->error(), ErrorCode::$dberror);
  404. }
  405. self::exportReportLoss($dbResult);
  406. ResultWrapper::success('操作成功');
  407. }
  408. /**
  409. * 报损商品导出方法
  410. * @param $result
  411. * @return void
  412. * @throws Exception
  413. */
  414. public function exportReportLoss($result)
  415. {
  416. //导出到本地
  417. header("Content-type:application/vnd.ms-excel");
  418. header("Content-Disposition:filename=报损记录表.csv");
  419. header('Cache-Control: max-age=0');
  420. $fp = fopen('php://output', 'a');
  421. $head = ['报损单号','商品名称', '仓库', '库存单位', '库存属性', '账目库存', '报损数量', '其他单位', '报损金额', '报损日期']; //定义标题
  422. foreach ($head as $i => $v) {
  423. $head[$i] = mb_convert_encoding($v, 'GBK', 'utf-8'); //将中文标题转换编码,否则乱码
  424. }
  425. fputcsv($fp, $head);
  426. $limit = 10000;
  427. $num = 0; //计数器
  428. foreach ($result as $v) { //循环数据
  429. $num++;
  430. if ($num == $limit) {
  431. ob_flush(); //释放内存
  432. flush();
  433. }
  434. $rows['no'] = isset($v['linkNo']) ? $v['linkNo'] : '';
  435. $rows['materielName'] = isset($v['materielName']) ? $v['materielName'] : '';
  436. $rows['warehouseName'] = isset($v['warehouseName']) ? $v['warehouseName'] : '';
  437. $rows['unitName'] = isset($v['unitName']) ? $v['unitName'] : '';
  438. $rows['skuName'] = isset($v['skuName']) ? $v['skuName'] : '';
  439. $rows['inventoryNum'] = isset($v['inventoryNum']) ? $v['inventoryNum'] : '';
  440. $rows['num'] = isset($v['num']) ? $v['num'] : '';
  441. $rows['otherNum'] = isset($v['otherNum']) ? $v['otherNum'] : '';
  442. $rows['lossAmount'] = isset($v['lossAmount']) ? $v['lossAmount'] : '';
  443. $rows['createTime'] = isset($v['createTime']) ? $v['createTime'] : '';
  444. foreach ($rows as $kk => $vv) {
  445. $rs[$kk] = mb_convert_encoding($vv, 'GBK', 'utf-8'); //转译编码
  446. }
  447. fputcsv($fp, $rs);
  448. $rows = [];
  449. }
  450. }
  451. }