MPurchaseOut.Class.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. <?php
  2. /**
  3. * 采购退货单数据模型
  4. * Created by PhpStorm.
  5. * User: XiaoMing
  6. * Date: 2019/11/11
  7. * Time: 17:49
  8. */
  9. namespace JinDouYun\Model\Purchase;
  10. use Exception;
  11. use JinDouYun\Dao\Merchant\DMerchantApply;
  12. use JinDouYun\Model\GoodsManage\MSku;
  13. use JinDouYun\Model\Stock\MInventory;
  14. use Mall\Framework\Core\ErrorCode;
  15. use Mall\Framework\Core\ResultWrapper;
  16. use Mall\Framework\Core\StatusCode;
  17. use JinDouYun\Model\MBaseModel;
  18. use JinDouYun\Model\Purchase\MPurchase;
  19. use JinDouYun\Model\Stock\MWarehouse;
  20. use JinDouYun\Dao\Purchase\DPurchaseOut;
  21. use JinDouYun\Dao\Purchase\DPurchaseOutGoods;
  22. use JinDouYun\Model\Stock\MInventoryOut;
  23. class MPurchaseOut extends MBaseModel
  24. {
  25. private $objDPurchaseOut;
  26. private $onlineUserId;
  27. private $onlineEnterpriseId;
  28. private $objDPurchaseOutGoods;
  29. private $objDMerchantApply;
  30. /**
  31. * MOrder constructor.
  32. * @param $onlineUserId
  33. * @param $onlineEnterpriseId
  34. * @throws Exception
  35. */
  36. public function __construct($onlineUserId, $onlineEnterpriseId)
  37. {
  38. $this->onlineUserId = $onlineUserId;
  39. $this->onlineEnterpriseId = $onlineEnterpriseId;
  40. parent::__construct($this->onlineEnterpriseId, $this->onlineUserId);
  41. $this->objDPurchaseOut = new DPurchaseOut('stock');
  42. $this->objDPurchaseOutGoods = new DPurchaseOutGoods('stock');
  43. $this->objDPurchaseOutGoods->setTable($this->objDPurchaseOutGoods->get_Table() . '_' . $this->onlineEnterpriseId);
  44. $this->objDPurchaseOut->setTable($this->objDPurchaseOut->get_Table() . '_' . $this->onlineEnterpriseId);
  45. $this->objDMerchantApply = new DMerchantApply();
  46. }
  47. /**
  48. * 增加采购退货订单
  49. * @param $params
  50. * @return ResultWrapper
  51. * @throws Exception
  52. */
  53. public function addPurchaseOut($params)
  54. {
  55. $goodData = $params['goodsData'];
  56. unset($params['goodsData']);
  57. $deleteArray = $params['deleteArray'];
  58. unset($params['deleteArray']);
  59. $purchaseAmount = 0;
  60. foreach ($goodData as $key => $val) {
  61. $purchaseAmount = bcadd($purchaseAmount, $val['subtotalPrice'], 4);
  62. }
  63. $params['purchaseAmount'] = $purchaseAmount;//采购总额
  64. //$params['couponAmount'] = sprintf("%.4f", array_sum(array_column($goodData, 'couponAmount')));//优惠总额
  65. //$params['otherAmount'] = sprintf("%.4f", array_sum(array_column($goodData, 'otherAmount')));//其他总额
  66. //添加采购退货退货订单
  67. // 生成编号
  68. $dbResult = $this->objDPurchaseOut->get('createTime >='.strtotime(date('Ymd'.'0:0:0')), 'no', 'createTime desc');
  69. if ($dbResult === false) {
  70. return ResultWrapper::fail($this->objDPurchaseOut->error(), ErrorCode::$dberror);
  71. }
  72. if(empty($dbResult)){
  73. $params['no'] = createSerialNumberByDate('');
  74. }else{
  75. $params['no'] = createSerialNumberByDate($dbResult['no']);
  76. }
  77. //$no = createOrderSn(StatusCode::$source['manage'], StatusCode::$orderType['purchaseReturn'], $this->onlineUserId);
  78. //$params['no'] = $no;
  79. $params['createTime'] = time();
  80. $params['updateTime'] = time();
  81. $objMInventory = new MInventory($this->onlineEnterpriseId, $this->onlineUserId);
  82. $skuIds = [];
  83. foreach($goodData as $value){
  84. $skuIds[] = $value['skuId'];
  85. }
  86. //查询仓库库存剩余数量
  87. $modelResult = $objMInventory->getWarehouseInventoryData($params['warehouseId'], $skuIds);
  88. if(!$modelResult->isSuccess()){
  89. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  90. }
  91. $inventoryData = $modelResult->getData();
  92. unset($modelResult);
  93. if(empty($inventoryData)){
  94. return ResultWrapper::fail('库存查询失败', ErrorCode::$paramError);
  95. }
  96. //查询批次批次剩余数量
  97. $modelResult = $objMInventory->getBatchDataByOriginId($params['warehouseId'], $params['originId'], $skuIds);
  98. if(!$modelResult->isSuccess()){
  99. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  100. }
  101. $inventoryBatch = $modelResult->getData();
  102. unset($modelResult);
  103. if(empty($inventoryBatch)){
  104. return ResultWrapper::fail('批次库存查询失败', ErrorCode::$dberror);
  105. }
  106. foreach($goodData as $value){
  107. //判断库存剩余数量
  108. if(isset($inventoryData[$value['skuId']])){
  109. if($value['buyerNum'] > $inventoryData[$value['skuId']]['num']){
  110. return ResultWrapper::fail($value['goodsName'].'库存不足', ErrorCode::$dberror);
  111. }
  112. }else{
  113. return ResultWrapper::fail($value['goodsName'].'库存为空', ErrorCode::$dberror);
  114. }
  115. //判断批次剩余数量
  116. if(isset($inventoryBatch[$value['skuId']])){
  117. if($value['buyerNum'] > $inventoryBatch[$value['skuId']]['num']){
  118. return ResultWrapper::fail($value['goodsName'].' 批次库存不足', ErrorCode::$dberror);
  119. }
  120. }else{
  121. return ResultWrapper::fail($value['goodsName'].' 批次库存为空', ErrorCode::$dberror);
  122. }
  123. }
  124. $beginStatus = $this->objDPurchaseOut->beginTransaction();
  125. $purchaseOutId = $this->objDPurchaseOut->insert($params);
  126. if ($purchaseOutId === false) {
  127. $this->objDPurchaseOut->rollBack();
  128. return ResultWrapper::fail($this->objDPurchaseOut->error(), ErrorCode::$dberror);
  129. }
  130. foreach ($goodData as $key => &$val) {
  131. $val['purchaseOutId'] = $purchaseOutId;
  132. }
  133. unset($val);
  134. $objPurchaseGoods = new MPurchaseOutGoods($this->onlineUserId, $this->onlineEnterpriseId);
  135. $dbResult = $objPurchaseGoods->addPurchaseGoods($goodData);
  136. if (!$dbResult->isSuccess()) {
  137. $this->objDPurchaseOut->rollBack();
  138. return ResultWrapper::fail($dbResult->getData(), ErrorCode::$dberror);
  139. }
  140. $beginStatus && $this->objDPurchaseOut->commit();
  141. return ResultWrapper::success($purchaseOutId);
  142. }
  143. /**
  144. * 采购退货订单详情
  145. * @param $id
  146. * @return ResultWrapper
  147. * @throws Exception
  148. */
  149. public function getPurchaseOutInfoById($id)
  150. {
  151. $dbResult = $this->objDPurchaseOut->get($id);
  152. if ($dbResult === false) {
  153. return ResultWrapper::fail($this->objDPurchaseOut->error(), ErrorCode::$dberror);
  154. }
  155. if(empty($dbResult)){
  156. return ResultWrapper::fail('单据不存在', ErrorCode::$paramError);
  157. }
  158. $returnData = $dbResult;
  159. $returnData['type'] = StatusCode::$orderType['purchaseReturn'];
  160. unset($dbResult);
  161. $dbResult = $this->objDPurchaseOutGoods->select(['purchaseOutId' => $id, 'deleteStatus' => StatusCode::$standard]);
  162. if ($dbResult === false) {
  163. return ResultWrapper::fail($this->objDPurchaseOut->error(), ErrorCode::$dberror);
  164. }
  165. $skuIds = [];
  166. foreach($dbResult as $value){
  167. $skuIds[] = $value['skuId'];
  168. }
  169. //查询出采购数量
  170. $objMPurchaseGoods = new MPurchaseGoods($this->onlineUserId, $this->onlineEnterpriseId);
  171. $purchaseDetailsWhere = [
  172. 'purchaseId' => $returnData['originId'],
  173. 'skuId' => $skuIds
  174. ];
  175. $modelResult = $objMPurchaseGoods->getPurchaseGoodsData($purchaseDetailsWhere);
  176. if(!$modelResult->isSuccess()){
  177. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  178. }
  179. $purchaseGoodsData = $modelResult->getData();
  180. unset($modelResult);
  181. //查询库存剩余数量
  182. $objMPurchase = new MPurchase($this->onlineUserId, $this->onlineEnterpriseId);
  183. $purchaseWhere = [
  184. 'id' => $returnData['originId'],
  185. 'warehouseId' => $returnData['warehouseId'],
  186. ];
  187. $modelResult = $objMPurchase->getPurchaseAndBatchInfoById($purchaseWhere);
  188. if(!$modelResult->isSuccess()){
  189. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  190. }
  191. $batchResult = $modelResult->getData();
  192. unset($modelResult);
  193. $batchData = [];
  194. foreach($batchResult as $value){
  195. $batchData[$value['skuId']] = $value;
  196. }
  197. foreach($dbResult as &$value){
  198. $value['purchaseNum'] = isset($purchaseGoodsData[$value['skuId']]) ? $purchaseGoodsData[$value['skuId']]['buyerNum'] : 0;
  199. $value['inNum'] = isset($purchaseGoodsData[$value['skuId']]) ? $purchaseGoodsData[$value['skuId']]['inNum'] : 0;
  200. $value['inOfNum'] = isset($purchaseGoodsData[$value['skuId']]) ? $purchaseGoodsData[$value['skuId']]['inOfNum'] : 0;
  201. $value['returnOnNum'] = isset($purchaseGoodsData[$value['skuId']]) ? $purchaseGoodsData[$value['skuId']]['returnOnNum'] : 0;
  202. $value['returnNum'] = isset($purchaseGoodsData[$value['skuId']]) ? $purchaseGoodsData[$value['skuId']]['returnNum'] : 0;
  203. $value['inventoryNum'] = isset($batchData[$value['skuId']]) ? $batchData[$value['skuId']]['num'] : 0;
  204. }
  205. unset($value);
  206. $returnData['details'] = $dbResult;
  207. unset($dbResult);
  208. $formatResult = self::format($returnData);
  209. return ResultWrapper::success(empty($formatResult) ? [] : $formatResult[0]);
  210. }
  211. /**
  212. * 编辑采购退货订单
  213. * @param $params
  214. * @return ResultWrapper
  215. * @throws Exception
  216. */
  217. public function editPurchase($params)
  218. {
  219. if (empty($params['id'])) {
  220. return ResultWrapper::fail('id参数为空', ErrorCode::$paramError);
  221. }
  222. if( isset($params['no']) ){
  223. unset($params['no']);
  224. }
  225. $id = $params['id'];
  226. unset($params['id']);
  227. unset($params['source']);
  228. $deleteArray = $params['deleteArray'];
  229. unset($params['deleteArray']);
  230. $beginStatus = $this->objDPurchaseOut->beginTransaction();
  231. $goodData = $params['goodsData'];
  232. unset($params['goodsData']);
  233. $dbResult = $this->objDPurchaseOut->update($params, $id);
  234. if ($dbResult === false) {
  235. $this->objDPurchaseOut->rollBack();
  236. return ResultWrapper::fail($this->objDPurchaseOut->error(), ErrorCode::$dberror);
  237. }
  238. $objPurchaseOutGoods = new MPurchaseOutGoods($this->onlineUserId, $this->onlineEnterpriseId);
  239. $modelResult = $objPurchaseOutGoods->editPurchaseOutGoods($goodData, $id, $deleteArray);
  240. if (!$modelResult->isSuccess()) {
  241. $this->objDPurchaseOut->rollBack();
  242. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  243. }
  244. $beginStatus && $this->objDPurchaseOut->commit();
  245. return ResultWrapper::success($dbResult);
  246. }
  247. /**
  248. * 更新审核状态
  249. * @param $params
  250. * @return ResultWrapper
  251. * @throws Exception
  252. */
  253. public function updateAuditStatus($params)
  254. {
  255. $id = $params['id'];
  256. unset($params['id']);
  257. $beginStatus = $this->objDPurchaseOut->beginTransaction();
  258. $dbResult = $this->objDPurchaseOut->update($params, $id);
  259. if ($dbResult === false) {
  260. $this->objDPurchaseOut->rollBack();
  261. return ResultWrapper::fail($this->objDPurchaseOut->error(), ErrorCode::$dberror);
  262. }
  263. $return = $dbResult;
  264. unset($dbResult);
  265. $dbResult = $this->objDPurchaseOut->get(['id' => $id]);
  266. if ($dbResult === false) {
  267. return ResultWrapper::fail($this->objDPurchaseOut->error(), ErrorCode::$dberror);
  268. }
  269. $purchaseOutData = $dbResult;
  270. unset($dbResult);
  271. $dbResult = $this->objDPurchaseOutGoods->select(['purchaseOutId' => $id]);
  272. if ($dbResult === false) {
  273. return ResultWrapper::fail($this->objDPurchaseOut->error(), ErrorCode::$dberror);
  274. }
  275. $purchaseOutGoodsData = $dbResult;
  276. //兼容商户
  277. if($purchaseOutData['purchaseStatus'] == StatusCode::$standard){//商户
  278. //查询商户信息
  279. $sql = 'select contactMobile as mobile from qianniao_merchant_apply where id = '.$purchaseOutData['merchantId'] .' and enterpriseId='.$this->onlineEnterpriseId;
  280. unset($dbResult);
  281. $dbResult = $this->objDMerchantApply->query($sql);
  282. if ($dbResult === false) {
  283. return ResultWrapper::fail($this->objDPurchaseOut->error(), ErrorCode::$dberror);
  284. }
  285. if(empty($dbResult)){
  286. return ResultWrapper::fail('供应商信息为空', ErrorCode::$paramError);
  287. }
  288. }else{
  289. //查询供应商信息
  290. $sql = 'select code,mobile from qianniao_supplier_'.$this->onlineEnterpriseId.' where id = '.$purchaseOutData['supplierId'];
  291. unset($dbResult);
  292. $dbResult = $this->objDPurchaseOut->query($sql);
  293. if ($dbResult === false) {
  294. return ResultWrapper::fail($this->objDPurchaseOut->error(), ErrorCode::$dberror);
  295. }
  296. if(empty($dbResult)){
  297. return ResultWrapper::fail('供应商信息为空', ErrorCode::$paramError);
  298. }
  299. }
  300. $supplierData = array_shift($dbResult);
  301. unset($dbResult);
  302. //拼数据加出库
  303. $outData = [
  304. 'sourceId' => $purchaseOutData['id'],
  305. 'sourceNo' => $purchaseOutData['no'],
  306. 'originNo' => $purchaseOutData['originNo'],
  307. 'originId' => $purchaseOutData['originId'],
  308. 'materielNum' => 0,
  309. 'amount' => $purchaseOutData['purchaseAmount'],
  310. 'customerId' => $purchaseOutData['supplierId'],//供应商id
  311. 'customerName' => $purchaseOutData['supplierName'],//供应商名称
  312. 'customerCode' => $supplierData['code'] ? $supplierData['code'] : '',//供应商编码
  313. 'customerMobile' => $supplierData['mobile'],//供应商联系方式
  314. 'operatorId' => $params['auditId'],
  315. 'operatorName' => $params['auditName'],
  316. 'remark' => $purchaseOutData['remark'],
  317. 'source' => StatusCode::$orderType['purchaseReturn'],
  318. 'type' => StatusCode::$orderType['purchaseReturnOut'],
  319. 'deleteStatus' => StatusCode::$standard,
  320. 'auditStatus' => StatusCode::$auditStatus['auditing'],
  321. 'createTime' => time(),
  322. 'updateTime' => time(),
  323. 'orderGoodsData' => [],
  324. 'shopId' => $purchaseOutData['shopId'],
  325. 'shopName' => $purchaseOutData['shopName'],
  326. ];
  327. $detailsData = [];
  328. foreach ($purchaseOutGoodsData as $value) {
  329. $detailsData[$value['skuId']]['num'] = $value['buyerNum'];
  330. $outData['materielNum']++;
  331. $details = [
  332. 'materielId' => $value['goodsId'],
  333. 'materielName' => $value['goodsName'],
  334. 'materielCode' => $value['goodsCode'],
  335. 'outWarehouse' => json_encode([['warehouseId' => $purchaseOutData['warehouseId'], 'num' => $value['buyerNum']]]),
  336. 'skuId' => $value['skuId'],
  337. 'unitName' => $value['unitName'],
  338. 'skuName' => $value['skuName'],
  339. 'num' => 0,
  340. 'outNum' => $value['buyerNum'],
  341. 'otherNum' => $value['otherNum'],
  342. 'total' => $value['buyerNum'],
  343. 'unitPrice' => $value['buyerUnitPrice'],
  344. 'totalPrice' => $value['subtotalPrice'],
  345. ];
  346. $outData['orderGoodsData'][] = $details;
  347. }
  348. //增加出库单
  349. $objMInventoryOut = new MInventoryOut($this->onlineEnterpriseId, $this->onlineUserId);
  350. $modelResult = $objMInventoryOut->addInventoryOut($outData);
  351. if (!$modelResult->isSuccess()) {
  352. $this->objDPurchaseOut->rollBack();
  353. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  354. }
  355. unset($modelResult);
  356. //修改采购订单退货状态
  357. $objMPurchase = new MPurchase($this->onlineUserId, $this->onlineEnterpriseId);
  358. $modelResult = $objMPurchase->updatePurchaseDetailsReturnStatus($purchaseOutData['originId'], $detailsData);
  359. if(!$modelResult->isSuccess()){
  360. $this->objDPurchaseOut->rollBack();
  361. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  362. }
  363. $beginStatus && $this->objDPurchaseOut->commit();
  364. return ResultWrapper::success($return);
  365. }
  366. /**
  367. * 删除采购退货单
  368. * @param $id
  369. * @return ResultWrapper
  370. */
  371. public function delPurchaseOut($id)
  372. {
  373. $dbResult = $this->objDPurchaseOut->update(['deleteStatus' => StatusCode::$delete], $id);
  374. if ($dbResult === false) {
  375. return ResultWrapper::fail($this->objDPurchaseOut->error(), ErrorCode::$dberror);
  376. } else {
  377. return ResultWrapper::success($dbResult);
  378. }
  379. }
  380. /**
  381. * 采购退货单列表
  382. * @param $selectParams
  383. * @param $export
  384. * @return ResultWrapper
  385. * @throws Exception
  386. */
  387. public function getAllPurchaseOut($selectParams, $export)
  388. {
  389. $selectParams = parent::getStaffQueryParams($selectParams, 'buyerId');
  390. $selectParams = parent::getShopIdQueryParams($selectParams);
  391. $limit = $selectParams['limit'];
  392. unset($selectParams['limit']);
  393. $offset = $selectParams['offset'];
  394. unset($selectParams['offset']);
  395. $selectParams['deleteStatus'] = StatusCode::$standard;
  396. if($export){
  397. $modelResult = self::export($selectParams);
  398. if(!$modelResult->isSuccess()){
  399. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  400. }
  401. exit;
  402. }
  403. $dbResult = $this->objDPurchaseOut->select($selectParams, '*', 'createTime desc', $limit, $offset);
  404. if ($dbResult === false) {
  405. return ResultWrapper::fail($this->objDPurchaseOut->error(), ErrorCode::$dberror);
  406. }
  407. $total = $this->objDPurchaseOut->count($selectParams);
  408. $return = [
  409. 'data' => self::format($dbResult),
  410. 'total' => ($total) ? intval($total) : 0,
  411. ];
  412. return ResultWrapper::success($return);
  413. }
  414. /**
  415. * 格式化数据
  416. * @param $data
  417. * @return array
  418. * @throws Exception
  419. */
  420. public function format($data)
  421. {
  422. if (isset($data['id'])) {
  423. $dimension = $data;
  424. unset($data);
  425. $data[] = $dimension;
  426. }
  427. $allPurchaseOutIds = [];
  428. foreach ($data as $key => $val) {
  429. $allPurchaseOutIds[] = $val['id'];
  430. $data[$key]['no'] = StatusCode::$noPrefix[4].'-'.$val['no'];
  431. }
  432. $objMPurchaseOutGoods = new MPurchaseOutGoods($this->onlineUserId, $this->onlineEnterpriseId);
  433. $dbResult = $objMPurchaseOutGoods->getGoodsByPurchaseIds($allPurchaseOutIds);
  434. if (!$dbResult->isSuccess()) {
  435. return $data;
  436. }
  437. $goodsData = $dbResult->getData();
  438. foreach ($goodsData as $key => $value) {
  439. $allGoodsData[$value['purchaseId']][] = $value;
  440. }
  441. foreach ($data as $key => $value) {
  442. $data[$key]['goodsData'] = isset($allGoodsData[$value['id']]) ? $allGoodsData[$value['id']] : [];
  443. }
  444. return $data;
  445. }
  446. /**
  447. * 根据商铺id查询物料退货记录
  448. * @param array $shopIds //商铺id
  449. * @return ResultWrapper
  450. */
  451. public function getReturnDataByShopIds(array $shopIds)
  452. {
  453. if(empty($shopIds)) return ResultWrapper::success([]);
  454. $sql = 'SELECT g.goodsId,SUM(g.buyerNum) AS num FROM '.$this->objDPurchaseOut->get_Table().' p LEFT JOIN '.$this->objDPurchaseOutGoods->get_Table().' g ON p.id = g.purchaseOutId WHERE p.auditStatus = '.StatusCode::$auditStatus['auditPass'].' AND p.deleteStatus = '.StatusCode::$standard.' AND g.deleteStatus = '.StatusCode::$standard.' AND p.shopId in('.implode(',', $shopIds).') GROUP BY g.goodsId';
  455. $dbResult = $this->objDPurchaseOut->query($sql);
  456. if($dbResult === false){
  457. return ResultWrapper::fail($this->objDPurchaseOut->error(), ErrorCode::$dberror);
  458. }
  459. $formatData = [];
  460. foreach($dbResult as $value){
  461. $formatData[$value['goodsId']] = $value;
  462. }
  463. unset($dbResult);
  464. return ResultWrapper::success($formatData);
  465. }
  466. /**
  467. * 导出方法
  468. * @param $condition
  469. * @return ResultWrapper
  470. */
  471. public function export($condition)
  472. {
  473. $result = $this->objDPurchaseOut->exportSelect($condition,'id,no,originNo,supplierName,purchaseAmount,operatorName,createTime,auditStatus,outStatus');
  474. if($result === false){
  475. return ResultWrapper::fail($this->objDPurchaseOut->error(), ErrorCode::$dberror);
  476. }
  477. $formatIds = [];
  478. $formatData = [];
  479. foreach($result as $value){
  480. $formatData[$value['id']] = $value;
  481. $formatIds[] = $value['id'];
  482. }
  483. $dbResult = $this->objDPurchaseOutGoods->exportSelect(['purchaseOutId' => $formatIds, 'deleteStatus'=> StatusCode::$standard]);
  484. if($dbResult === false){
  485. return ResultWrapper::fail($this->objDPurchaseOutGoods->error(), ErrorCode::$paramError);
  486. }
  487. //导出到本地
  488. header ( "Content-type:application/vnd.ms-excel" );
  489. header ( "Content-Disposition:filename=采购退货记录表.csv" );
  490. header ('Cache-Control: max-age=0');
  491. $fp = fopen('php://output', 'a');
  492. $head = ['编号','采购退货单号','供应商','退货金额','采购关联单号','制单人员','制单日期','审核状态','出库状态','商品名称','商品编码','单位','采购数量','采购单价','采购总价'];//定义标题
  493. foreach ($head as $i => $v) {
  494. $head[$i] = mb_convert_encoding($v, 'GBK', 'utf-8');//将中文标题转换编码,否则乱码
  495. }
  496. fputcsv($fp, $head);
  497. $limit = 10000;
  498. $num = 0; //计数器
  499. foreach ( $dbResult as $value ) { //循环数据
  500. $num++;
  501. if($num == $limit){
  502. ob_flush(); //释放内存
  503. flush();
  504. }
  505. $value = parent::formatOrderMan($this->onlineEnterpriseId, $value);
  506. $v = $formatData[$value['purchaseOutId']];
  507. $rows['id'] = isset($v['id']) ? $v['id'] : null;
  508. $rows['no'] = isset($v['no']) ? $v['no'] : null;
  509. $rows['supplierName'] = isset($v['supplierName']) ? $v['supplierName'] : null;
  510. $rows['purchaseAmount'] = isset($v['purchaseAmount']) ? $v['purchaseAmount'] : null;
  511. $rows['originNo'] = isset($v['originNo']) ? $v['originNo'] : null;
  512. $rows['operatorName'] = isset($v['operatorName']) ? $v['operatorName'] : null;
  513. $rows['createTime'] = isset($v['operatorName']) ? date('Y-m-d H:i:s',$v['createTime']) : null;
  514. $rows['auditStatus'] = isset($v['auditStatus']) ? $v['auditStatus'] == StatusCode::$auditStatus['auditPass'] ? '已审核' : '未审核' : null;
  515. $rows['outStatus'] = isset($v['outStatus']) ? $v['outStatus'] == StatusCode::$standard ? '已出库' : '未出库' : null;
  516. $rows['goodsName'] = isset($value['goodsName']) ? $value['goodsName'] : null;
  517. $rows['goodsCode'] = isset($value['goodsCode']) ? $value['goodsCode'] : null;
  518. $rows['skuName'] = isset($value['skuName']) ? $value['skuName'] : null;
  519. $rows['buyerNum'] = isset($value['buyerNum']) ? $value['buyerNum'] : null;
  520. $rows['buyerUnitPrice'] = isset($value['buyerUnitPrice']) ? $value['buyerUnitPrice'] : null;
  521. $rows['subtotalPrice'] = isset($value['subtotalPrice']) ? $value['subtotalPrice'] : null;
  522. foreach ( $rows as $kk => $vv){
  523. $rs[$kk] = mb_convert_encoding($vv, 'GBK', 'utf-8'); //转译编码
  524. }
  525. fputcsv($fp, $rs);
  526. $rows = [];
  527. }
  528. return ResultWrapper::success('成功');
  529. }
  530. /**
  531. * 修改采购退货
  532. * @param $updateData
  533. * @param $where
  534. * @return ResultWrapper
  535. */
  536. public function updatePurchaseOutData($updateData, $where)
  537. {
  538. $dbResult = $this->objDPurchaseOut->update($updateData, $where);
  539. if($dbResult === false){
  540. return ResultWrapper::fail($this->objDPurchaseOut->error(), ErrorCode::$dberror);
  541. }
  542. return ResultWrapper::success($dbResult);
  543. }
  544. }