MInventoryIn.Class.php 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  1. <?php
  2. /**
  3. * 采购入库管理Model
  4. * Created by PhpStorm.
  5. * User: 小威
  6. * Date: 2019/11/11
  7. * Time: 18:30
  8. */
  9. namespace JinDouYun\Model\Stock;
  10. use Exception;
  11. use JinDouYun\Dao\Purchase\DPurchase;
  12. use Mall\Framework\Factory;
  13. use Mall\Framework\Core\ErrorCode;
  14. use Mall\Framework\Core\StatusCode;
  15. use Mall\Framework\Core\ResultWrapper;
  16. use JinDouYun\Cache\Stock;
  17. use Jindouyun\Cache\EnterpriseCache;
  18. use JinDouYun\Model\Purchase\MPurchase;
  19. use JinDouYun\Model\Common\MCommon;
  20. use JinDouYun\Model\MBaseModel;
  21. use JinDouYun\Model\Order\MOrderReturn;
  22. use JinDouYun\Model\System\MPrintNum;
  23. use JinDouYun\Model\Order\MOrder;
  24. use JinDouYun\Model\Finance\MRefund;
  25. use JinDouYun\Model\Finance\MAccount;
  26. use JinDouYun\Dao\Order\DOrder;
  27. use JinDouYun\Dao\Stock\DInventoryIn;
  28. use JinDouYun\Dao\Stock\DInventoryInDetails;
  29. class MInventoryIn extends MBaseModel
  30. {
  31. private $objDInventoryIn;
  32. private $objDInventoryInDetails;
  33. private $objMInventory;
  34. private $onlineUserId;
  35. private $enterpriseId;
  36. public function __construct($enterpriseId, $userCenterId)
  37. {
  38. $this->enterpriseId = $enterpriseId;
  39. $this->onlineUserId = $userCenterId;
  40. parent::__construct($enterpriseId, $userCenterId);
  41. $this->objDInventoryIn = new DInventoryIn('stock');
  42. $this->objDInventoryInDetails = new DInventoryInDetails('stock');
  43. $this->objMInventory = new MInventory($enterpriseId, $this->onlineUserId);
  44. $this->objDInventoryIn->setTable($this->objDInventoryIn->get_Table().'_'.$enterpriseId);
  45. $this->objDInventoryInDetails->setTable($this->objDInventoryInDetails->get_Table().'_'.$enterpriseId);
  46. $this->objDInventoryIn->setSearchIndex('inventory_in_search')->setType('inventory_in');
  47. }
  48. /**
  49. * 入库添加
  50. * @param $params
  51. * @return ResultWrapper
  52. * @throws Exception
  53. */
  54. public function addInventoryIn($params)
  55. {
  56. //接收参数
  57. // 生成编号
  58. $dbResult = $this->objDInventoryIn->get('createTime >='.strtotime(date('Ymd'.'0:0:0')), 'no', 'createTime desc');
  59. if ($dbResult === false) {
  60. return ResultWrapper::fail($this->objDInventoryIn->error(), ErrorCode::$dberror);
  61. }
  62. if(empty($dbResult)){
  63. $InventoryInAddData['no'] = createSerialNumberByDate('');
  64. }else{
  65. $InventoryInAddData['no'] = createSerialNumberByDate($dbResult['no']);
  66. }
  67. //$InventoryInAddData['no'] = createOrderSn(StatusCode::$source['manage'], $params['type'],$params['operatorId'] );
  68. $InventoryInAddData['sourceId'] = $params['sourceId'];
  69. $InventoryInAddData['sourceNo'] = $params['sourceNo'];
  70. $InventoryInAddData['originNo'] = $params['originNo'];
  71. $InventoryInAddData['merchantId'] = isset($params['merchantId']) ? $params['merchantId'] : 0;
  72. $InventoryInAddData['originId'] = $params['originId'];
  73. $InventoryInAddData['shopId'] = isset($params['shopId']) ? $params['shopId'] : '';
  74. $InventoryInAddData['materielNum'] = $params['materielNum'];
  75. $InventoryInAddData['amount'] = $params['amount'];
  76. $InventoryInAddData['warehouseId'] = $params['warehouseId'];
  77. $InventoryInAddData['warehouseName'] = $params['warehouseName'];
  78. $InventoryInAddData['operatorId'] = $params['operatorId'];
  79. $InventoryInAddData['operatorName'] = $params['operatorName'];
  80. $InventoryInAddData['auditId'] = 0;
  81. $InventoryInAddData['auditName'] = '';
  82. $InventoryInAddData['source'] = $params['source'];
  83. $InventoryInAddData['type'] = $params['type'];
  84. $InventoryInAddData['deleteStatus'] = StatusCode::$standard;
  85. $InventoryInAddData['auditStatus'] = StatusCode::$auditStatus['auditing'];
  86. $InventoryInAddData['createTime'] = time();
  87. $InventoryInAddData['updateTime'] = time();
  88. $InventoryInAddData['auditTime'] = 0;
  89. $InventoryInAddData['extend'] = null;
  90. //拿出详情数组
  91. $paramsDetailsAddData = $params['orderGoodsData'];
  92. //开启事务
  93. $beginStatus = $this->objDInventoryIn->beginTransaction();
  94. //插入基础表
  95. $dbResult = $this->objDInventoryIn->insert($InventoryInAddData);
  96. if($dbResult === false){
  97. $this->objDInventoryIn->rollBack();
  98. return ResultWrapper::fail($this->objDInventoryIn->error(), ErrorCode::$dberror);
  99. }
  100. $InventoryInId = $dbResult;
  101. unset($dbResult);
  102. //定义固定参数
  103. $InventoryInDetailsAddData = [];
  104. foreach($paramsDetailsAddData as $v){
  105. $detailsAddData = [];
  106. $detailsAddData['linkId'] = $InventoryInId;
  107. $detailsAddData['linkNo'] = $InventoryInAddData['no'];
  108. $detailsAddData['materielId'] = $v['materielId'];
  109. $detailsAddData['materielName'] = $v['materielName'];
  110. $detailsAddData['materielCode'] = $v['materielCode'];
  111. $detailsAddData['skuId'] = $v['skuId'];
  112. $detailsAddData['num'] = $v['num'];
  113. $detailsAddData['otherNum'] = $v['otherNum'];
  114. $detailsAddData['unitPrice'] = $v['unitPrice'];
  115. $detailsAddData['unitName'] = isset($v['unitName']) ? $v['unitName'] : '';
  116. $detailsAddData['skuName'] = isset($v['skuName']) ? $v['skuName'] : '';
  117. $detailsAddData['totalPrice'] = bcmul($detailsAddData['unitPrice'], $detailsAddData['num'], 4);
  118. $detailsAddData['createTime'] = time();
  119. $detailsAddData['updateTime'] = time();
  120. $InventoryInDetailsAddData[] = $detailsAddData;
  121. }
  122. //插入详情表
  123. $dbResult = $this->objDInventoryInDetails->insert($InventoryInDetailsAddData, true);
  124. if($dbResult === false){
  125. $this->objDInventoryIn->rollBack();
  126. return ResultWrapper::fail($this->objDInventoryInDetails->error(), ErrorCode::$dberror);
  127. }
  128. //增加es搜索
  129. /*
  130. $modelResult = self::updateEsData($InventoryInAddData, $InventoryInId, true);
  131. if(!$modelResult->isSuccess()){
  132. $this->objDInventoryIn->rollBack();
  133. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  134. }*/
  135. //删除redis
  136. $objCacheStock = new Stock($this->enterpriseId);
  137. $cacheResult = $objCacheStock->deleteCacheStatistics('InventoryIn');
  138. if(!$cacheResult && $cacheResult !== 0){
  139. $this->objDInventoryIn->rollBack();
  140. return ResultWrapper::fail('删除缓存失败', ErrorCode::$paramError);
  141. }
  142. $beginStatus && $this->objDInventoryIn->commit();
  143. return ResultWrapper::success($InventoryInId);
  144. }
  145. /**
  146. * 入库单明细删除
  147. *
  148. */
  149. public function deleteDetailByInventoryInId($params)
  150. {
  151. $updateData = [
  152. 'deleteStatus' => StatusCode::$delete,
  153. 'updateTime' => time(),
  154. ];
  155. $dbResult = $this->objDInventoryInDetails->update($updateData, ['linkId'=>$params['inventoryInId'],'id'=>$params['inventoryInDetailIds']]);
  156. if($dbResult === false){
  157. return ResultWrapper::fail($this->objDInventoryInDetails->error(), ErrorCode::$dberror);
  158. }
  159. return ResultWrapper::success($dbResult);
  160. }
  161. /**
  162. * 入库单驳回
  163. * @param $params
  164. */
  165. public function rejectInventory($params)
  166. {
  167. // 获取入库单主单据信息
  168. $InventoryInData = $this->objDInventoryIn->get(['id' => $params['inventoryInId']] );
  169. if($InventoryInData === false){
  170. return ResultWrapper::fail($this->objDInventoryIn->error(), ErrorCode::$dberror);
  171. }
  172. // 删除
  173. $updateData = [
  174. 'deleteStatus' => StatusCode::$delete,
  175. 'updateTime' => time(),
  176. ];
  177. // 删除入库单主单据信息
  178. $InventoryIn = $this->objDInventoryIn->update($updateData,['id' => $params['inventoryInId']] );
  179. if($InventoryIn === false){
  180. return ResultWrapper::fail($this->objDInventoryIn->error(), ErrorCode::$dberror);
  181. }
  182. // 删除入库详情数据
  183. $dbResult = $this->objDInventoryInDetails->update($updateData, ['linkId'=>$params['inventoryInId'],'id'=>$params['inventoryInDetailIds']]);
  184. if($dbResult === false){
  185. return ResultWrapper::fail($this->objDInventoryInDetails->error(), ErrorCode::$dberror);
  186. }
  187. unset($updateData);
  188. // 修改采购表(审核状态改为待审核)
  189. $purchaseUpdateData = [
  190. 'auditStatus' => StatusCode::$auditStatus['auditing'],
  191. 'updateTime' => time(),
  192. ];
  193. // 采购表
  194. $objDPurchase = new DPurchase();
  195. $objDPurchase->setTable($objDPurchase->get_Table().'_'.$this->enterpriseId);
  196. $purchaseResult = $objDPurchase->update($purchaseUpdateData, ['id' => $InventoryInData['originId']]);
  197. if($purchaseResult === false){
  198. return ResultWrapper::fail($objDPurchase->error(), ErrorCode::$dberror);
  199. }
  200. if($dbResult && $purchaseResult == true){
  201. return ResultWrapper::success($purchaseResult);
  202. }
  203. }
  204. /**
  205. * 入库审核
  206. * @param $params
  207. * @return ResultWrapper
  208. * @throws Exception
  209. */
  210. public function updateInventoryInStatus($params)
  211. {
  212. $where['id'] = $params['id'];
  213. unset($params['id']);
  214. $updateDetails = isset($params['details']) ? $params['details'] : [];
  215. unset($params['details']);
  216. if(empty($params['inNumTotal']) || $params['inNumTotal'] == 0){
  217. return ResultWrapper::fail('入库数量为空', ErrorCode::$paramError);
  218. }
  219. unset($params['inNumTotal']);
  220. $params['updateTime'] = time();
  221. $params['auditTime'] = time();
  222. $params['auditStatus'] = StatusCode::$auditStatus['auditPass'];
  223. // 获取入库单主单据信息
  224. $InventoryInData = $this->objDInventoryIn->get($where);
  225. if($InventoryInData === false){
  226. return ResultWrapper::fail($this->objDInventoryIn->error(), ErrorCode::$dberror);
  227. }
  228. if(empty($InventoryInData)){
  229. return ResultWrapper::fail('要审核的单据不存在', ErrorCode::$contentNotExists);
  230. }
  231. if($InventoryInData['auditStatus'] == StatusCode::$auditStatus['auditPass']){
  232. return ResultWrapper::fail('该单据已审核', ErrorCode::$paramError);
  233. }
  234. // 判断商城设置 是否开启保质期
  235. $objEnterpriseCache = new EnterpriseCache();
  236. $cacheResult = $objEnterpriseCache->getEnterpriseData($this->enterpriseId, 'shelfLifeSetUp');
  237. if($cacheResult == StatusCode::$standard){
  238. if(empty($updateDetails)){
  239. return ResultWrapper::fail('details参数错误', ErrorCode::$paramError);
  240. }
  241. foreach($updateDetails as $value){
  242. if(empty($value['productionData'])){
  243. return ResultWrapper::fail('productionData参数错误', ErrorCode::$paramError);
  244. }
  245. if(empty($value['inCost']) || $value['inCost'] <= 0){
  246. return ResultWrapper::fail('入库成本参数错误', ErrorCode::$paramError);
  247. }
  248. if(empty($value['inCostTotal']) || $value['inCostTotal'] <= 0){
  249. return ResultWrapper::fail('入库成本参数错误', ErrorCode::$paramError);
  250. }
  251. }
  252. }
  253. $beginStatus = $this->objDInventoryIn->beginTransaction();
  254. // 修改入库状态为已审核
  255. if(!empty($updateDetails)){
  256. // 用实际入库成本重新计算采购入库单总成本
  257. $params['amount'] = 0;
  258. // 用实际入库数量重新计算采购入库单sku数量
  259. $params['materielNum'] = 0;
  260. //修改单据详情 生产日期
  261. foreach($updateDetails as $value){
  262. if( isset($value['id']) ){
  263. $update = [
  264. 'productionData' => getArrayItem($value,'productionData',0),
  265. 'inNum' => $value['inNum'],
  266. 'otherNum' => $value['otherNum'],
  267. 'areaId' => $value['areaId'],
  268. 'areaName' => $value['areaName'],
  269. 'areaCode' => $value['areaCode'],
  270. 'storageLocationId' => $value['storageLocationId'],
  271. 'storageLocationName' => $value['storageLocationName'],
  272. 'storageLocationCode' => $value['storageLocationCode'],
  273. 'inCost' => $value['inCost'],
  274. 'inCostTotal' => $value['inCostTotal']
  275. ];
  276. $dbResult = $this->objDInventoryInDetails->update($update, ['id'=>$value['id']]);
  277. if($dbResult === false){
  278. $this->objDInventoryIn->rollBack();
  279. return ResultWrapper::fail($this->objDInventoryInDetails->error(), ErrorCode::$dberror);
  280. }
  281. }else{
  282. $dbResult = $this->objDInventoryInDetails->insert($value);
  283. if($dbResult === false){
  284. $this->objDInventoryIn->rollBack();
  285. return ResultWrapper::fail($this->objDInventoryInDetails->error(), ErrorCode::$dberror);
  286. }
  287. }
  288. $params['amount'] += $value['inCostTotal'];
  289. $params['materielNum'] += 1;
  290. }
  291. }
  292. $dbResult = $this->objDInventoryIn->update($params, $where);
  293. if($dbResult === false){
  294. $this->objDInventoryIn->rollBack();
  295. return ResultWrapper::fail($this->objDInventoryIn->error(), ErrorCode::$dberror);
  296. }
  297. $returnData = $dbResult;
  298. unset($dbResult);
  299. //合并修改数据
  300. $InventoryInData = array_merge($InventoryInData, $params);
  301. //修改es
  302. /*
  303. $esId = self::esId($InventoryInData['id']);
  304. $esResult = $this->objDInventoryIn->esupdateTypeFieldVaule($params, $esId);
  305. if(!$esResult){
  306. $this->objDInventoryIn->rollBack();
  307. return ResultWrapper::fail($esResult, ErrorCode::$paramError);
  308. }
  309. unset($esResult);*/
  310. // 获取入库单详情信息
  311. $condition = [
  312. 'linkId' => $where['id'],
  313. 'linkNo' => $InventoryInData['no'],
  314. 'deleteStatus' => StatusCode::$standard,
  315. ];
  316. $InventoryInDetailsData = $this->objDInventoryInDetails->select($condition);
  317. if($InventoryInDetailsData === false){
  318. $this->objDInventoryIn->rollBack();
  319. return ResultWrapper::fail($this->objDInventoryInDetails->error(), ErrorCode::$dberror);
  320. }
  321. if(empty($InventoryInDetailsData)){
  322. return ResultWrapper::fail('单据详情数据为空', ErrorCode::$contentNotExists);
  323. }
  324. $InventoryInData['Details'] = $InventoryInDetailsData;
  325. $inNumData = [];
  326. $skuIds = [];
  327. $skuCostParse = [];
  328. foreach($InventoryInData['Details'] as &$value){
  329. $skuIds[] = $value['skuId'];
  330. if($value['inNum'] <= 0 || $value['inCost'] <= 0){
  331. unset($value);
  332. }else{
  333. $value['num'] = $value['inNum'];
  334. $value['unitPrice'] = $value['inCost'];
  335. $value['totalPrice'] = $value['inCostTotal'];
  336. $inNumData[$value['skuId']]['inNum'] = bcadd($inNumData[$value['skuId']]['inNum'],$value['inNum'],4);
  337. $skuCostParse[$value['skuId']] = $value['unitPrice'];
  338. unset($value['inNum'], $value['inCost'], $value['inCostTotal']);
  339. }
  340. }
  341. unset($value);
  342. //判断如果是调拨入库 查询出库仓库id传过去
  343. if($InventoryInData['type'] == StatusCode::$orderType['allocateIn']){
  344. //查询调拨原始单据信息
  345. $objMAllocate = new MAllocate($this->enterpriseId,$this->onlineUserId);
  346. $modelResult = $objMAllocate->getAllocate(['id'=>$InventoryInData['originId']]);
  347. if(!$modelResult->isSuccess()){
  348. $this->objDInventoryIn->rollBack();
  349. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  350. }
  351. if(empty($modelResult)){
  352. $this->objDInventoryIn->rollBack();
  353. return ResultWrapper::fail('调拨单数据为空', ErrorCode::$paramError);
  354. }
  355. $allocateData = $modelResult->getData();
  356. unset($modelResult);
  357. $InventoryInData['outWarehouseId'] = $allocateData['warehouseId'];
  358. }
  359. //查询仓库
  360. $objMWarehouse = new MWarehouse($this->enterpriseId);
  361. $modelResult = $objMWarehouse->getWarehouse(['id' => $InventoryInData['warehouseId'], 'deleteStatus' => StatusCode::$standard]);
  362. if(!$modelResult->isSuccess()){
  363. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  364. }
  365. $warehouseData = $modelResult->getData();
  366. unset($modelResult);
  367. if(empty($warehouseData)){
  368. return ResultWrapper::fail('仓库查询失败', ErrorCode::$paramError);
  369. }
  370. //获取计算成本方式
  371. $costType = $warehouseData['costType'];
  372. // 操作库存
  373. $result = $this->objMInventory->updateInventory($InventoryInData, $costType);
  374. if(!$result->isSuccess()){
  375. $this->objDInventoryIn->rollBack();
  376. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  377. }
  378. //采购入库
  379. if($InventoryInData['type'] == StatusCode::$orderType['purchaseIn'] || $InventoryInData['type'] == StatusCode::$orderType['merchantPurchaseIn']){
  380. //回写负库存商品出库成本
  381. //查询每个skuId采购入库 审核成功的记录
  382. //判断该sku是否是第一次采购
  383. //如果是第一次采购
  384. //查询该skuId出库记录 回写出库成本
  385. //修改流水记录的出库成本
  386. /*//回写负库存商品出库成本
  387. //查询第一次采购的skuId
  388. $sql = 'select d.skuId,count(d.skuId) as num from qianniao_inventory_in_'.$this->enterpriseId.' i left join qianniao_inventory_in_details_'.$this->enterpriseId.' d on i.id = d.linkId where d.skuId in('.implode(',', $skuIds).') and i.auditStatus = 2 and i.warehouseId = '.$InventoryInData['warehouseId'].' group by d.skuId having count(d.skuId) < 2';
  389. $dbResult = $this->objDInventoryIn->query($sql);
  390. if($dbResult === false){
  391. $this->objDInventoryIn->rollBack();
  392. return ResultWrapper::fail($this->objDInventoryIn->error(), ErrorCode::$dberror);
  393. }
  394. $updateCostSkuData = $dbResult;
  395. unset($dbResult);
  396. if(!empty($updateCostSkuData)){
  397. $updateCostSkuIds = array_column($updateCostSkuData, 'skuId');
  398. //修改库存流水成本
  399. $modelResult = $this->objMInventory->updateSaleOrderOutCostPrise($InventoryInData['warehouseId'], $updateCostSkuIds, $skuCostParse);
  400. if(!$modelResult->isSuccess()){
  401. $this->objDInventoryIn->rollBack();
  402. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  403. }
  404. $updateCostArray = $modelResult->getData();
  405. unset($modelResult);
  406. if(!empty($updateCostArray)){
  407. //回写订单出库成本
  408. $objMOrder = new MOrder($this->onlineUserId, $this->enterpriseId);
  409. $modelResult = $objMOrder->updateCostPriceBySku($updateCostArray['orderId'], $updateCostArray['updateOrder']);
  410. if(!$modelResult->isSuccess()){
  411. $this->objDInventoryIn->rollBack();
  412. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  413. }
  414. }
  415. }*/
  416. //单据回写 修改采购订单入库状态,入库数量
  417. $objMPurchase = new MPurchase($this->onlineUserId ,$this->enterpriseId);
  418. $updatePurchase = [
  419. 'inStatus' => StatusCode::$standard,
  420. 'details' => $inNumData
  421. ];
  422. $modelResult = $objMPurchase->updatePurchaseData($updatePurchase, ['id' => $InventoryInData['originId']]);
  423. if(!$modelResult->isSuccess()){
  424. $this->objDInventoryIn->rollBack();
  425. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  426. }
  427. unset($modelResult);
  428. // 采购入库 推送生成应付异步任务
  429. if($InventoryInData['type'] == StatusCode::$orderType['purchaseIn']){
  430. $i = 1;
  431. do {
  432. $postData = [
  433. 'topicName' => 'MyJob',
  434. 'topicClass' => 'Jobs\Model\MTopic\Finance\MPayable',
  435. 'topicMethon' => 'createPayable',
  436. 'topicMethonParams' => [
  437. 'userCenterId' => $this->onlineUserId,
  438. 'enterpriseId' => $this->enterpriseId,
  439. 'id' => $InventoryInData['originId'],
  440. 'inventoryInId' => $where['id'],
  441. 'type' => StatusCode::$orderType['purchaseIn'],
  442. ],
  443. ];
  444. $url = QIANNIAO_QUEUE . '/CAddJob/add';
  445. $result = request($url, $postData);
  446. $i++;
  447. } while ($result['httpcode'] != 200 && $i <= 3);
  448. }
  449. }
  450. if($InventoryInData['type'] == StatusCode::$orderType['saleReturnIn']){
  451. //查出源订单的userCenterId
  452. $stockDbName = Factory::config()->get('db.stock.dbname');
  453. $defaultDbName = Factory::config()->get('db.default.dbname');
  454. if (!$stockDbName || !$defaultDbName) {
  455. $this->objDInventoryIn->rollBack();
  456. return ResultWrapper::fail('dbname获取错误', ErrorCode::$dberror);
  457. }
  458. $sql = 'select a.originId,b.id as orderId,b.userCenterId from `'. $stockDbName .'`.'. $this->objDInventoryIn->get_Table() .' as a join `'. $defaultDbName .'`.qianniao_order_index_'. $this->enterpriseId .' as b on a.originId = b.id and a.originId = '. $InventoryInData['originId'] .' limit 1';
  459. $userResult = $this->objDInventoryIn->query($sql);
  460. if ($userResult === false) {
  461. $this->objDInventoryIn->rollBack();
  462. return ResultWrapper::fail($this->objDInventoryIn->error(), ErrorCode::$dberror);
  463. }
  464. $user = array_shift($userResult);
  465. // 查询订单数据
  466. $objMOrder = new MOrder($this->onlineUserId, $this->enterpriseId, $user['userCenterId']);
  467. $orderTableName = $objMOrder->getOrderTable($this->enterpriseId, $user['userCenterId']);
  468. $objDOrder = new DOrder('default');
  469. $objDOrder->setTable($orderTableName);
  470. $order = $objDOrder->get($InventoryInData['originId']);
  471. if($order === false){
  472. $this->objDInventoryIn->rollBack();
  473. return ResultWrapper::fail($objDOrder->error(), ErrorCode::$dberror);
  474. }
  475. if(empty($order)){
  476. return ResultWrapper::fail('订单数据为空', ErrorCode::$contentNotExists);
  477. }
  478. // 已经支付的订单生成退款单
  479. if($order['payStatus'] == StatusCode::$standard){
  480. // 生成财务退款单
  481. $financeRefundData = [
  482. 'originId' => getArrayItem($InventoryInData,'originId',''),
  483. 'originNo' => getArrayItem($InventoryInData,'originNo',''),
  484. 'sourceId' => getArrayItem($InventoryInData,'id',0),
  485. 'sourceNo' => getArrayItem($InventoryInData,'no',''),
  486. 'refundTime' => time(),
  487. 'createTime' => time(),
  488. 'unitId' => getArrayItem($order,'customerId',0),
  489. 'unitName' => getArrayItem($order,'customerName',0),
  490. 'currentAccountName' => getArrayItem($order,'customerName',''),
  491. 'financeType' => '订单退货退款',
  492. 'financeTypeId' => 1,
  493. 'payType' =>getArrayItem($order,'payType',''),
  494. 'shopId' => getArrayItem($order,'shopId',0),
  495. 'shopName' => getArrayItem($order,'shopName',0),
  496. 'money' => $InventoryInData['amount'],
  497. 'operatorId' => $this->onlineUserId,
  498. 'type' => StatusCode::$standard,
  499. 'auditStatus' => StatusCode::$auditStatus['auditing'],
  500. 'remark' => '订单编号:'. $order['no'].'退货,退款金额:'.$InventoryInData['amount']
  501. ];
  502. // 根据订单支付方式规划退款账户
  503. $objMAccount = new MAccount($this->enterpriseId,$this->onlineUserId);
  504. $payType = explode(',', $order['payType']);
  505. // 包含余额组合支付方式统一返回余额,其他原路返回
  506. if( in_array(StatusCode::$payType['balance'],$payType) ){
  507. $financeAccountData = $objMAccount->getDefaultAccount(StatusCode::$payType['balance']);
  508. }else{
  509. $financeAccountData = $objMAccount->getDefaultAccount($payType);
  510. }
  511. if(!$financeAccountData->isSuccess()){
  512. return ResultWrapper::fail($financeAccountData->getData(), $financeAccountData->getErrorCode());
  513. }
  514. $financeAccountData = $financeAccountData->getData();
  515. $financeRefundData['accountList'] = [
  516. [
  517. 'accountId' => $financeAccountData['id'],
  518. 'accountNumber' => $financeAccountData['accountNumber'],
  519. 'accountName' => $financeAccountData['name'],
  520. 'money' => $InventoryInData['amount'],
  521. ]
  522. ];
  523. $objMRefund = new MRefund($this->enterpriseId,$this->onlineUserId);
  524. $result = $objMRefund->addRefund($financeRefundData);
  525. if(!$result->isSuccess()){
  526. $this->objDInventoryIn->rollBack();
  527. return ResultWrapper::fail($objMRefund->getData(),$objMRefund->getErrorCode());
  528. }
  529. }
  530. // 生成应收单数据
  531. $i = 1;
  532. do {
  533. $postData = [
  534. 'topicName' => 'MyJob',
  535. 'topicClass' => 'Jobs\Model\MTopic\Finance\MReceivable',
  536. 'topicMethon' => 'createReceivable',
  537. 'topicMethonParams' => [
  538. 'userCenterId' => $user['userCenterId'],
  539. 'enterpriseId' => $this->enterpriseId,
  540. 'id' => $InventoryInData['id'],
  541. 'type' => StatusCode::$orderType['saleReturnIn'],
  542. ],
  543. ];
  544. $url = QIANNIAO_QUEUE . '/CAddJob/add';
  545. $result = request($url, $postData);
  546. $i++;
  547. } while ($result['httpcode'] != 200 && $i <= 3);
  548. }
  549. //销售退货单入库状态回写
  550. if($InventoryInData['type'] == StatusCode::$orderType['saleReturnIn']){
  551. $objMOrderReturn = new MOrderReturn($this->enterpriseId, $this->onlineUserId);
  552. $modelResult = $objMOrderReturn->updateOrderReturnData(['inStatus'=>StatusCode::$standard, 'updateTime'=>time()], ['no'=>$InventoryInData['sourceNo']]);
  553. if(!$modelResult->isSuccess()){
  554. $this->objDInventoryIn->rollBack();
  555. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  556. }
  557. }
  558. // 修改仓库初始化状态(有采购之后,仓库不让初始化)
  559. if($warehouseData['beginningStatus'] == StatusCode::$standard){
  560. $modelResult = $objMWarehouse->updateWarehouse(['beginningStatus' => StatusCode::$delete, 'updateTime' => time()], ['id' => $InventoryInData['warehouseId']]);
  561. if(!$modelResult->isSuccess()){
  562. $this->objDInventoryIn->rollBack();
  563. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  564. }
  565. unset($modelResult);
  566. }
  567. // 删除redis采购入库总数统计项
  568. $objCacheStock = new Stock($this->enterpriseId);
  569. $cacheResult = $objCacheStock->deleteCacheStatistics('InventoryIn');
  570. if(!$cacheResult && $cacheResult !== 0){
  571. $this->objDInventoryIn->rollBack();
  572. return ResultWrapper::fail('删除缓存失败', ErrorCode::$paramError);
  573. }
  574. $beginStatus && $this->objDInventoryIn->commit();
  575. return ResultWrapper::success($returnData);
  576. }
  577. /**
  578. * 入库列表
  579. * @param $selectParams
  580. * @param $export //导出标识字段
  581. * @return ResultWrapper
  582. */
  583. public function getAllInventoryIn($selectParams,$export = 0)
  584. {
  585. $limit = $selectParams['limit'];
  586. unset($selectParams['limit']);
  587. $offset = $selectParams['offset'];
  588. unset($selectParams['offset']);
  589. $selectParams['deleteStatus'] = StatusCode::$standard;
  590. $type = $selectParams['type'];
  591. $selectParams = self::formatSqlWhere($selectParams);
  592. if($export){
  593. $whereSql = '';
  594. if(isset($type) && !empty($type)){
  595. $whereSql = ' i.type= '.$type.' and ';
  596. }
  597. $linkedTab =' qianniao_inventory_in_'.$this->enterpriseId.' as i left join qianniao_inventory_in_details_'.$this->enterpriseId.' as d ON i.id=d.linkId ';
  598. $where = $whereSql.' i.deleteStatus='.StatusCode::$standard.' ORDER BY i.createTime desc';
  599. $sql ='select * from '.$linkedTab.' where '.$where;
  600. $exportDbResult = $this->objDInventoryIn->query($sql);
  601. if ($exportDbResult === false) {
  602. return ResultWrapper::fail($this->objDInventoryIn->error(), ErrorCode::$dberror);
  603. }
  604. $inventoryInData = parent::formatOrderMan($this->enterpriseId, $exportDbResult);
  605. self::exportInventoryIn($inventoryInData);
  606. exit;
  607. }
  608. $dbResult = $this->objDInventoryIn->select($selectParams, '*', 'createTime desc', $limit, $offset);
  609. if ($dbResult === false) {
  610. return ResultWrapper::fail($this->objDInventoryIn->error(), ErrorCode::$dberror);
  611. }
  612. $total = $this->objDInventoryIn->count($selectParams);
  613. $return = [
  614. 'data' => parent::formatOrderMan($this->enterpriseId, $dbResult),
  615. 'total' => ($total) ? intval($total) : 0,
  616. ];
  617. if($return === false){
  618. return ResultWrapper::fail($this->objDInventoryIn->error(), ErrorCode::$dberror);
  619. }else{
  620. return ResultWrapper::success($return);
  621. }
  622. }
  623. /**
  624. * 入库导出方法
  625. * @param $inventoryInData
  626. */
  627. public function exportInventoryIn($inventoryInData)
  628. {
  629. //导出到本地
  630. header("Content-type:application/vnd.ms-excel");
  631. header("Content-Disposition:filename=入库管理.csv");
  632. header('Cache-Control: max-age=0');
  633. $fp = fopen('php://output', 'a');
  634. $head = ['ID', '入库单号','仓库名称', '入库类型', '入库商品','审核人员','制单人员', '来源单号', '审核状态', '制单日期','商品名称','商品编码','规格','采购数量','采购成本','采购总成本','入库数量','其他单位','入库成本','入库总成本','生产日期','库区库位','商品条码','货架编码']; //定义标题
  635. foreach ($head as $i => $v) {
  636. $head[$i] = mb_convert_encoding($v, 'GBK', 'utf-8'); //将中文标题转换编码,否则乱码
  637. }
  638. fputcsv($fp, $head);
  639. $limit = 10000;
  640. $num = 0;//计数器
  641. foreach ($inventoryInData as $value) {//循环数据
  642. $num++;
  643. if ($num == $limit) {
  644. ob_flush();//释放内存
  645. flush();
  646. }
  647. $rows['id'] = isset($value['linkId']) ? $value['linkId'] : null; // ID
  648. $rows['no'] = isset($value['no']) ? $value['no'] : null; // 入库单号
  649. $rows['warehouseName'] = isset($value['warehouseName']) ? $value['warehouseName'] : null; // 仓库名称
  650. $rows['typeName'] = isset($value['typeName']) ? $value['typeName'] : null; // 入库类型
  651. $rows['materielNum'] = isset($value['materielNum']) ? $value['materielNum'] : null; // 入库商品
  652. $rows['auditName'] = isset($value['auditName']) ? $value['auditName'] : null; // 审核人员
  653. $rows['operatorName'] = isset($value['operatorName']) ? $value['operatorName'] : null; // 制单人员
  654. $rows['sourceNo'] = isset($value['sourceNo']) ? $value['sourceNo'] : null; // 来源单号
  655. $rows['auditStatus'] = isset($value['auditStatus'])&& $value['auditStatus']== 1 ?'未审核':'已审核';// 审核状态
  656. $rows['createTime'] = isset($value['createTime']) ? date("Y-m-d H:i:s",$value['createTime']) : null; // 制单日期date('Y-m-d H:i:s')
  657. $rows['materielName'] = isset($value['materielName']) ? $value['materielName'] : null; // 商品名称
  658. $rows['materielCode'] = isset($value['materielCode']) ? $value['materielCode'] : null; // 商品编码
  659. $rows['specs'] = !empty($value['unitName']) && !empty($value['skuName']) ? $value['unitName'].';'.$value['skuName'] : $value['unitName']; // 规格
  660. $rows['num'] = isset($value['num']) ? $value['num'] : null; // 采购数量
  661. $rows['unitPrice'] = isset($value['unitPrice']) ? $value['unitPrice'] : null; // 采购成本
  662. $rows['totalPrice'] = isset($value['totalPrice']) ? $value['totalPrice'] : null; // 采购总成本
  663. $rows['inNum'] = isset($value['inNum']) ? $value['inNum'] : null; // 入库数量
  664. $rows['otherNum'] = isset($value['otherNum']) ? $value['otherNum'] : null; // 其他单位
  665. $rows['inUnitPrice'] = isset($value['unitPrice']) ? $value['unitPrice'] : null; // 入库成本
  666. $rows['inTotalPrice'] = isset($value['totalPrice']) ? $value['totalPrice'] : null; // 入库总成本
  667. $rows['productionData'] = isset($value['productionData']) ? date("Y-m-d",$value['productionData']) : null; // 生产日期
  668. $rows['storageLocationName'] = isset($value['storageLocationName']) ? $value['storageLocationName'] : null; // 库区库位
  669. $rows['barCode'] = isset($value['barCode']) ? $value['barCode'] : null; // 商品条码
  670. $rows['storage'] = isset($value['storage']) ? $value['storage'] : null; // 货架编码
  671. foreach ($rows as $kk => $vv) {
  672. $rs[$kk] = mb_convert_encoding($vv, 'GBK', 'utf-8'); //转译编码
  673. }
  674. fputcsv($fp, $rs);
  675. $rows = [];
  676. }
  677. }
  678. /**
  679. * 入库列表搜索
  680. * @param $params
  681. * @return ResultWrapper
  682. * @throws Exception
  683. */
  684. public function searchAllInventoryIn($params)
  685. {
  686. $defaultDSL = ['from' => $params['offset'],'size' => $params['limit'],'sort' => ['createTime' => ['order' => 'desc']]];
  687. $selectParams = [];
  688. $selectParams[] = ['term' => ['deleteStatus' => StatusCode::$standard]];
  689. $selectParams[] = ['term' => ['enterpriseId' => $this->enterpriseId]];
  690. !empty($params['type']) && $selectParams[] = ['term' => ['type' => $params['type']]];
  691. !empty($params['shopId']) && $selectParams[] = ['term' => ['shopId' => $params['shopId']]];
  692. !empty($params['warehouseId']) && $selectParams[] = ['term' => ['warehouseId' => $params['warehouseId']]];
  693. !empty($params['auditStatus']) && $selectParams[] = ['terms' => ['auditStatus' => is_array($params['auditStatus']) ? $params['auditStatus'] : [$params['auditStatus']]]];
  694. (!empty($params['start']) && !empty($params['end'])) && $selectParams[] = ['range' => ['createTime' => ['gte' => $params['start'],'lte' => $params['end']]]];
  695. !empty($params['search']) && $selectParams[] = ['multi_match' => ['fields' => ['no', 'operatorName'],'query' => $params['search'],'fuzziness' => 'AUTO']];
  696. $dsl = [];
  697. !empty($selectParams) && $dsl['query']['bool']['must'][] = $selectParams;
  698. $dsl =
  699. array_merge($defaultDSL, $dsl);
  700. $dbResult = $this->objDInventoryIn->getSearchQueryDsl($dsl);
  701. if(isset($dbResult['error'])){
  702. if($dbResult['error']['reason'] == 'no such index'){
  703. return ResultWrapper::success(['data'=>[],'total'=>[]]);
  704. }
  705. return ResultWrapper::fail($dbResult, ErrorCode::$dberror);
  706. }
  707. $returnData = parent::formatEsSelectData($dbResult);
  708. $objMCommon = new MCommon();
  709. foreach($returnData['data'] as &$value){
  710. $value['typeName'] = $objMCommon->formatOrderSource($value['type']);
  711. }
  712. unset($value);
  713. return ResultWrapper::success($returnData);
  714. }
  715. /**
  716. * 入库统计
  717. * @param $type
  718. * @param int $shopId
  719. * @return ResultWrapper
  720. * @throws Exception
  721. */
  722. public function statisticsAllInventoryIn($type, $shopId = 0)
  723. {
  724. if($shopId){
  725. $sql = "select `type`,count(id) as count from ".$this->objDInventoryIn->get_Table()." where deleteStatus = ".StatusCode::$standard." and shopId = ".$shopId." GROUP BY `type`";
  726. $dbResult = $this->objDInventoryIn->query($sql);
  727. if($dbResult === false){
  728. return ResultWrapper::fail($this->objDInventoryIn->error(), ErrorCode::$dberror);
  729. }
  730. $typeData = [];
  731. foreach($dbResult as $value){
  732. $typeData[$value['type']] = $value['count'];
  733. }
  734. $cacheData = [];
  735. foreach($type as $value){
  736. $cacheData[] = [
  737. 'type' => $value,
  738. 'count' => isset($typeData[$value]) ? $typeData[$value] : 0,
  739. ];
  740. }
  741. return ResultWrapper::success($cacheData);
  742. }
  743. $cacheKey = 'InventoryIn';
  744. $objCacheStock = new Stock($this->enterpriseId);
  745. $cacheData = [];
  746. //判断redis里有没有这个key
  747. $cacheResult = $objCacheStock->getCacheStatisticsCount($cacheKey);
  748. $issetCacheKey = $cacheResult;
  749. unset($cacheResult);
  750. foreach($type as $value){
  751. $cacheResult = $objCacheStock->getCacheStatistics($value,$cacheKey);
  752. $cacheData[] = [
  753. 'type' => $value,
  754. 'count' => $cacheResult ? $cacheResult : 0,
  755. ];
  756. unset($cacheResult);
  757. }
  758. //如果有就拿出来 当做值返回
  759. if($issetCacheKey) return ResultWrapper::success($cacheData);
  760. //如果没有 从数据库里查一遍 删除redis 存到redis 值返回
  761. $sql = "select `type`,count(id) as count from ".$this->objDInventoryIn->get_Table()." where deleteStatus = ".StatusCode::$standard." GROUP BY `type`";
  762. $dbResult = $this->objDInventoryIn->query($sql);//销售出库
  763. if($dbResult === false){
  764. return ResultWrapper::fail($this->objDInventoryIn->error(), ErrorCode::$dberror);
  765. }
  766. $typeData = [];
  767. foreach($dbResult as $value){
  768. $typeData[$value['type']] = $value['count'];
  769. }
  770. $cacheData = [];
  771. $cacheResult = $objCacheStock->deleteCacheStatistics($cacheKey);
  772. if(!$cacheResult && $cacheResult !== 0){
  773. $this->objDInventoryIn->rollBack();
  774. return ResultWrapper::fail('删除缓存失败', ErrorCode::$paramError);
  775. }
  776. unset($cacheResult);
  777. foreach($type as $value){
  778. $cacheData[] = [
  779. 'type' => $value,
  780. 'count' => isset($typeData[$value]) ? $typeData[$value] : 0,
  781. ];
  782. if(isset($typeData[$value])){
  783. $objCacheStock->createCacheStatistics($value,$typeData[$value], $cacheKey);
  784. }
  785. }
  786. return ResultWrapper::success($cacheData);
  787. }
  788. /**
  789. * 根据no单号查询入库信息
  790. * @param $nos
  791. * @return ResultWrapper
  792. */
  793. public function getAllInventoryInByNo($nos)
  794. {
  795. if(empty($nos)) return ResultWrapper::success($nos);
  796. $dbResult = $this->objDInventoryIn->select(['sourceNo'=>$nos], 'sourceNo,auditStatus');
  797. if($dbResult === false){
  798. return ResultWrapper::fail($this->objDInventoryIn->error(), ErrorCode::$dberror);
  799. }
  800. $formatData = [];
  801. foreach($dbResult as $value){
  802. $formatData[$value['sourceNo']] = $value;
  803. }
  804. return ResultWrapper::success($formatData);
  805. }
  806. /**
  807. * 入库详情
  808. * @param $params
  809. * @return ResultWrapper
  810. * @throws Exception
  811. */
  812. public function getInventoryInInfo($params)
  813. {
  814. $params['deleteStatus'] = StatusCode::$standard;
  815. $dbResult = $this->objDInventoryIn->get($params);
  816. if($dbResult === false){
  817. return ResultWrapper::fail($this->objDInventoryIn->error(), ErrorCode::$dberror);
  818. }
  819. if(empty($dbResult)){
  820. return ResultWrapper::fail('未查询到入库单据', ErrorCode::$paramError);
  821. }
  822. $condition = [
  823. 'linkId' => $dbResult['id'],
  824. 'deleteStatus' => StatusCode::$standard,
  825. ];
  826. $dbResult['details'] = $this->objDInventoryInDetails->select($condition);
  827. if($dbResult['details'] === false){
  828. return ResultWrapper::fail($this->objDInventoryInDetails->error(), ErrorCode::$dberror);
  829. }
  830. $dbResult = parent::formatOrderMan($this->enterpriseId,$dbResult);
  831. $objMPrintNum = new MPrintNum($this->enterpriseId);
  832. $dbResult['printNum'] = $objMPrintNum->getObjectPrintNum($dbResult['no'], StatusCode::$orderType['purchaseIn']);
  833. if($dbResult['type'] == StatusCode::$orderType['allocateIn']){
  834. // 映射仓库名称
  835. $objMAllocate = new MAllocate($this->enterpriseId,$this->onlineUserId);
  836. $data = $objMAllocate->getAllocateInfoByWarehouseName([$dbResult]);
  837. if(!$data->isSuccess()){
  838. return ResultWrapper::fail($objMAllocate->error(), ErrorCode::$dberror);
  839. }
  840. $dbResult = $data->getData();
  841. foreach ($dbResult as $key=>$value){
  842. $dbResult[$key]['outWarehouseId'] = $dbResult[$key]['warehouseId'];
  843. $dbResult[$key]['outWarehouseName'] = $dbResult[$key]['warehouseName'];
  844. unset($dbResult[$key]['warehouseId']);
  845. unset($dbResult[$key]['warehouseName']);
  846. }
  847. $dbResult = array_pop($dbResult);
  848. }
  849. return ResultWrapper::success($dbResult);
  850. }
  851. /**
  852. * 查询入库数据
  853. * @param $where
  854. * @return ResultWrapper
  855. */
  856. public function getInventoryInData($where)
  857. {
  858. $dbResult = $this->objDInventoryIn->select($where);
  859. if($dbResult === false){
  860. return ResultWrapper::fail($this->objDInventoryIn->error(), ErrorCode::$dberror);
  861. }
  862. return ResultWrapper::success($dbResult);
  863. }
  864. /**
  865. * 添加es
  866. * @param $data
  867. * @param $id
  868. * @param bool $type & false:修改 true:新增
  869. * @return ResultWrapper
  870. */
  871. public function updateEsData($data, $id, $type = false)
  872. {
  873. $addEsData = [
  874. 'id' => $id,
  875. 'enterpriseId' => $this->enterpriseId,
  876. 'no' => isset($data['no']) ? StatusCode::$noPrefix[$data['type']].$data['no'] : false,
  877. 'sourceId' => isset($data['sourceId']) ? $data['sourceId'] : false,
  878. 'sourceNo' => isset($data['sourceNo']) ? StatusCode::$noPrefix[$data['source']].$data['sourceNo'] : false,
  879. 'originId' => isset($data['originId']) ? $data['originId'] : false,
  880. 'originNo' => isset($data['originNo']) ? StatusCode::$noPrefix[$data['source']].$data['originNo'] : false,
  881. 'materielNum' => isset($data['materielNum']) ? $data['materielNum'] : false,
  882. 'amount' => isset($data['amount']) ? $data['amount'] : false,
  883. 'warehouseId' => isset($data['warehouseId']) ? $data['warehouseId'] : false,
  884. 'warehouseName' => isset($data['warehouseName']) ? $data['warehouseName'] : false,
  885. 'operatorId' => isset($data['operatorId']) ? $data['operatorId'] : false,
  886. 'operatorName' => isset($data['operatorName']) ? $data['operatorName'] : false,
  887. 'auditId' => isset($data['auditId']) ? $data['auditId'] : false,
  888. 'auditName' => isset($data['auditName']) ? $data['auditName'] : false,
  889. 'deleteStatus' => isset($data['deleteStatus']) ? $data['deleteStatus'] : false,
  890. 'source' => isset($data['source']) ? $data['source'] : false,
  891. 'type' => isset($data['type']) ? $data['type'] : false,
  892. 'auditStatus' => isset($data['auditStatus']) ? $data['auditStatus'] : false,
  893. 'auditTime' => isset($data['auditTime']) ? $data['auditTime'] : false,
  894. 'createTime' => isset($data['createTime']) ? $data['createTime'] : false,
  895. 'updateTime' => isset($data['updateTime']) ? $data['updateTime'] : false,
  896. ];
  897. foreach($addEsData as $key => $value){
  898. if($value === false){
  899. if($type){
  900. return ResultWrapper::fail($key.'ES字段未设置', ErrorCode::$paramError);
  901. }else{
  902. unset($addEsData[$key]);
  903. }
  904. }
  905. }
  906. $addEsData['remark'] = isset($data['remark']) ? $data['remark'] : '';
  907. //创建es id
  908. $esId = self::esId($id);
  909. $result = $this->objDInventoryIn->addUpSearchIndexDocument($addEsData, $esId);
  910. // var_dump($result);
  911. if (isset($result['_shards']) && isset($result['_shards']['successful']) && $result['_shards']['successful'] == 1) {
  912. return ResultWrapper::success(isset($result['_id']) ? $result['_id'] : false);
  913. }
  914. return ResultWrapper::fail($result['error']['reason'], ErrorCode::$paramError);
  915. }
  916. /**
  917. * 拼接es id
  918. * @param $id
  919. * @return string
  920. */
  921. public function esId($id)
  922. {
  923. return 'EnterpriseId_' . $this->enterpriseId . '_InventoryInId_' . $id;
  924. }
  925. /**
  926. * 格式化入库es
  927. */
  928. public function formatInventoryInEsData()
  929. {
  930. //查询列表数据
  931. $dbResult = $this->objDInventoryIn->select([],'*');
  932. if($dbResult === false){
  933. return ResultWrapper::fail($this->objDInventoryIn->error(), ErrorCode::$dberror);
  934. }
  935. if(empty($dbResult)){
  936. return ResultWrapper::fail('格式化数据为空', ErrorCode::$dberror);
  937. }
  938. //循环插入es
  939. foreach($dbResult as $value){
  940. //增加es搜索
  941. $modelResult = self::updateEsData($value, $value['id'], true);
  942. if(!$modelResult->isSuccess()){
  943. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  944. }
  945. }
  946. return ResultWrapper::success('格式化成功');
  947. }
  948. /**
  949. * 成本均摊
  950. */
  951. public function countPurchaseCost($params)
  952. {
  953. $total = 0;
  954. $num = 0;
  955. foreach($params['skuData'] as $value){
  956. $total = bcadd($total, $value['money']);
  957. $num = bcadd($num, $value['num']);
  958. }
  959. $skuData = [];
  960. if($params['type'] == StatusCode::$standard){
  961. $moneyOne = ceil( bcdiv($params['money'], $total,4) * 10000 / 100) / 100; // 保留两位小数向上取整
  962. foreach($params['skuData'] as $value){
  963. if($moneyOne > 0){
  964. $skuData[$value['skuId']]['cost'] = bcdiv(bcadd($value['money'], bcmul($moneyOne,$value['money'])), $value['num'], 2);
  965. $skuData[$value['skuId']]['money'] = bcadd($value['money'], bcmul($moneyOne,$value['money']));
  966. }else{
  967. $skuData[$value['skuId']]['cost'] = $value['cost'];
  968. $skuData[$value['skuId']]['money'] = $value['money'];
  969. }
  970. }
  971. }else{
  972. $numOne = ceil( bcdiv($params['money'], $num,4) * 10000 / 100) / 100; // 保留两位小数向上取整
  973. foreach($params['skuData'] as $value){
  974. if($numOne > 0){
  975. $skuData[$value['skuId']]['cost'] = bcadd($value['cost'], $numOne);
  976. $skuData[$value['skuId']]['money'] = bcadd($value['money'], bcmul($numOne,$value['num']));
  977. }else{
  978. $skuData[$value['skuId']]['cost'] = $value['cost'];
  979. $skuData[$value['skuId']]['money'] = $value['money'];
  980. }
  981. }
  982. }
  983. return ResultWrapper::success($skuData);
  984. }
  985. }