MWarehouse.Class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 Mall\Framework\Core\ErrorCode;
  11. use Mall\Framework\Core\StatusCode;
  12. use Mall\Framework\Core\ResultWrapper;
  13. use JinDouYun\Cache\GoodsBasicRelevant;
  14. use JinDouYun\Model\SysAreaChina\MSysAreaChina;
  15. use Jindouyun\Model\Shop\MShop;
  16. use JinDouYun\Dao\Stock\DWarehouse;
  17. use JinDouYun\Dao\Stock\DWarehouseBeginning;
  18. class MWarehouse
  19. {
  20. private $objDWarehouse;
  21. private $objDWarehouseBeginning;
  22. private $userCenterId;
  23. private $enterpriseId;
  24. public function __construct($enterpriseId, $userCenterId = false)
  25. {
  26. $this->enterpriseId = $enterpriseId;
  27. $this->userCenterId = $userCenterId;
  28. $this->objDWarehouse = new DWarehouse('stock');
  29. $this->objDWarehouseBeginning = new DWarehouseBeginning('stock');
  30. $this->objDWarehouse->setTable('qianniao_warehouse_'.$enterpriseId);
  31. $this->objDWarehouseBeginning->setTable('qianniao_warehouse_beginning_'.$enterpriseId);
  32. }
  33. /**
  34. * 仓库添加
  35. * @param $params
  36. * @return ResultWrapper
  37. * @throws Exception
  38. */
  39. public function addWarehouse($params)
  40. {
  41. $params['createTime'] = time();
  42. $params['updateTime'] = time();
  43. $dbResult = $this->objDWarehouse->insert($params);
  44. if($dbResult === false){
  45. return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror);
  46. }
  47. $warehouseId = $dbResult;
  48. unset($dbResult);
  49. $update = [
  50. 'warehouseCode' => createCode(StatusCode::$code['warehouse']['prefix'], $warehouseId, StatusCode::$code['warehouse']['length']),
  51. 'updateTime' => time(),
  52. ];
  53. $result = $this->objDWarehouse->update($update, ['id' => $warehouseId]);
  54. if($result === false){
  55. return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror);
  56. }
  57. return ResultWrapper::success($warehouseId);
  58. }
  59. /**
  60. * 仓库期初记录添加
  61. * @param $params
  62. * @return ResultWrapper
  63. * @throws Exception
  64. */
  65. public function addWarehouseBeginning($params)
  66. {
  67. $warehouseId = 0;
  68. $addWarehouseBeginningData = [];
  69. foreach($params as $value){
  70. $warehouseId = $value['warehouseId'];
  71. $addWarehouseBeginningData[] = [
  72. 'no' => createOrderSn(StatusCode::$source['manage'], StatusCode::$orderType['warehouseBeginning'],$value['operatorId'] ),
  73. 'warehouseId' => $value['warehouseId'],
  74. 'operatorId' => $value['operatorId'],
  75. 'operatorName' => $value['operatorName'],
  76. 'remark' => $value['remark'],
  77. 'materielId' => $value['materielId'],
  78. 'materielName' => $value['materielName'],
  79. 'materielCode' => $value['materielCode'],
  80. 'skuId' => $value['skuId'],
  81. 'num' => $value['num'],
  82. 'costPrice' => $value['costPrice'],
  83. 'updateTime' => time(),
  84. 'createTime' => time(),
  85. ];
  86. }
  87. if(empty($warehouseId)) return ResultWrapper::fail('参数错误', ErrorCode::$paramError);
  88. $dbResult = $this->objDWarehouse->get($warehouseId, 'id,beginningStatus');
  89. if($dbResult === false){
  90. return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror);
  91. }
  92. if($dbResult['beginningStatus'] == StatusCode::$delete){
  93. return ResultWrapper::fail('期初功能已失效', ErrorCode::$paramError);
  94. }
  95. $this->objDWarehouseBeginning->beginTransaction();
  96. //添加期初记录
  97. $dbResult = $this->objDWarehouseBeginning->insert($addWarehouseBeginningData, true);
  98. if($dbResult === false){
  99. $this->objDWarehouseBeginning->rollBack();
  100. return ResultWrapper::fail($this->objDWarehouseBeginning->error(), ErrorCode::$dberror);
  101. }
  102. $addIds = $dbResult;
  103. unset($dbResult);
  104. //修改期初状态
  105. $dbResult = $this->objDWarehouse->update(['beginningStatus'=>StatusCode::$delete],['id'=>$warehouseId]);
  106. if($dbResult === false){
  107. return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror);
  108. }
  109. //添加库存
  110. $objMInventory = new MInventory($this->enterpriseId, $this->userCenterId);
  111. $modelResult = $objMInventory->addInventoryByWarehouseBeginning($addWarehouseBeginningData, $addIds);
  112. if(!$modelResult->isSuccess()){
  113. $this->objDWarehouseBeginning->rollBack();
  114. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  115. }
  116. $this->objDWarehouseBeginning->commit();
  117. return ResultWrapper::success($dbResult);
  118. }
  119. /**
  120. * 仓库删除
  121. * @param $where
  122. * @return ResultWrapper
  123. */
  124. public function deleteWarehouse($where)
  125. {
  126. $params = [
  127. 'deleteStatus' => StatusCode::$delete,
  128. 'updateTime' => time(),
  129. ];
  130. $dbResult = $this->objDWarehouse->update($params, $where);
  131. if($dbResult === false){
  132. return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror);
  133. }else{
  134. return ResultWrapper::success($dbResult);
  135. }
  136. }
  137. /**
  138. * 仓库启用/禁用
  139. * @param $where
  140. * @return ResultWrapper
  141. */
  142. public function enableWarehouse($where)
  143. {
  144. $params['updateTime'] = time();
  145. $params['enableStatus'] = $where['enableStatus'];
  146. unset($where['enableStatus']);
  147. $dbResult = $this->objDWarehouse->update($params, ['id' => $where['id']]);
  148. if($dbResult === false){
  149. return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror);
  150. }else{
  151. return ResultWrapper::success($dbResult);
  152. }
  153. }
  154. /**
  155. * 仓库修改
  156. * @param $params
  157. * @param $where
  158. * @return ResultWrapper
  159. * @throws Exception
  160. */
  161. public function updateWarehouse($params, $where)
  162. {
  163. $dbResult = $this->objDWarehouse->update($params,$where);
  164. if($dbResult === false){
  165. return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror);
  166. }else{
  167. return ResultWrapper::success($dbResult);
  168. }
  169. }
  170. /**
  171. * 仓库列表
  172. * @param $selectParams
  173. * @return ResultWrapper
  174. */
  175. public function getAllWarehouse($selectParams)
  176. {
  177. $limit = $selectParams['limit'];
  178. unset($selectParams['limit']);
  179. $offset = $selectParams['offset'];
  180. unset($selectParams['offset']);
  181. if(isset($selectParams['shopId'])){
  182. // 如果多门店/多商户只查看自己店铺对应仓库
  183. $objMShop = new MShop($this->enterpriseId, 1);
  184. $result = $objMShop->getShopById($selectParams['shopId']);
  185. if(!$result->isSuccess()){
  186. return ResultWrapper::fail('查询店铺对应仓库数据为空', ErrorCode::$contentNotExists);
  187. }
  188. $shopData = $result->getData();
  189. $warehouseId = $shopData['warehouseId'];
  190. if(!is_numeric($warehouseId)){
  191. $warehouseId = explode(",", $warehouseId);
  192. }
  193. $dbResult = $this->objDWarehouse->select(['id'=>$warehouseId,'deleteStatus' => StatusCode::$standard], '*', 'createTime desc', $limit, $offset);
  194. if ($dbResult === false) {
  195. return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror);
  196. }
  197. $total = count($dbResult);
  198. }else{
  199. // 企业后台查看所有仓库
  200. $sql = 'deleteStatus = '.StatusCode::$standard;
  201. isset($selectParams['id']) && $sql .= ' and id != '.$selectParams['id'];
  202. isset($selectParams['enableStatus']) && $sql .= ' and enableStatus = '.$selectParams['enableStatus'];
  203. $dbResult = $this->objDWarehouse->select($sql, '*', 'createTime desc', $limit, $offset);
  204. if ($dbResult === false) {
  205. return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror);
  206. }
  207. $sql = 'select count(id) count from '.$this->objDWarehouse->get_Table().' where '.$sql;
  208. $countData = $this->objDWarehouse->query($sql);
  209. $total = array_shift($countData)['count'];
  210. }
  211. $return = [
  212. 'data' => self::format($dbResult),
  213. 'total' => ($total) ? intval($total) : 0,
  214. ];
  215. if($return === false){
  216. return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror);
  217. }else{
  218. return ResultWrapper::success($return);
  219. }
  220. }
  221. public function format($data) {
  222. $objMSysAreaChina = new MSysAreaChina();
  223. foreach ($data as &$value) {
  224. $value['warehouseCode'] = createCode(StatusCode::$code['warehouse']['prefix'], $value['id'], StatusCode::$code['warehouse']['length']);
  225. $areaName = $objMSysAreaChina->getNameByCode([
  226. $value['provinceCode'],
  227. $value['cityCode'],
  228. $value['districtCode']
  229. ]);
  230. $value['area']['provinceName'] = isset($areaName[$value['provinceCode']]) ? $areaName[$value['provinceCode']] : '';
  231. $value['area']['cityName'] = isset($areaName[$value['cityCode']]) ? $areaName[$value['cityCode']] : '';
  232. $value['area']['districtName'] = isset($areaName[$value['districtCode']]) ? $areaName[$value['districtCode']] : '';
  233. $value['area']['contactAddress'] = $value['contactAddress'];
  234. }
  235. return $data;
  236. }
  237. /**
  238. * 仓库详情
  239. * @param $params
  240. * @return ResultWrapper
  241. */
  242. public function getWarehouseInfo($params)
  243. {
  244. $dbResult = $this->objDWarehouse->get($params);
  245. if($dbResult === false){
  246. return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror);
  247. }else{
  248. if(empty($dbResult)){
  249. return ResultWrapper::fail('没有仓库数据', ErrorCode::$contentNotExists);
  250. }
  251. $dbResult['warehouseCode'] = createCode(StatusCode::$code['warehouse']['prefix'], $dbResult['id'], StatusCode::$code['warehouse']['length']);
  252. return ResultWrapper::success($dbResult);
  253. }
  254. }
  255. /**
  256. * 根据ids数组获取name
  257. * @param $ids
  258. * @return array|ResultWrapper
  259. */
  260. public function getNameByIds($ids)
  261. {
  262. $where = ['id' => $ids];
  263. $dbResult = $this->objDWarehouse->select($where, 'id,warehouseName');
  264. if ($dbResult === false) {
  265. return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror);
  266. }
  267. $returnData = [];
  268. foreach($dbResult as $key => $value){
  269. $returnData[$value['id']]['id'] = $value['id'];
  270. $returnData[$value['id']]['warehouseName'] = $value['warehouseName'];
  271. }
  272. if(empty($dbResult)){
  273. return ResultWrapper::fail('仓库数据为空', ErrorCode::$dberror);
  274. }
  275. return ResultWrapper::success($returnData);
  276. }
  277. /**
  278. * 获取仓库数据
  279. * @param array $where
  280. * @param string $field
  281. * @return ResultWrapper
  282. */
  283. public function selectWarehouse($where = [], $field = '*')
  284. {
  285. $dbResult = $this->objDWarehouse->select($where, $field);
  286. if($dbResult === false){
  287. return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror);
  288. }
  289. return ResultWrapper::success($dbResult);
  290. }
  291. /**
  292. * 获取仓库数据
  293. * @param array $where
  294. * @param string $field
  295. * @return ResultWrapper
  296. */
  297. public function getWarehouse($where = [], $field = '*')
  298. {
  299. $dbResult = $this->objDWarehouse->get($where, $field);
  300. if($dbResult === false){
  301. return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror);
  302. }
  303. return ResultWrapper::success($dbResult);
  304. }
  305. /**
  306. * 查询仓库名称
  307. * @param $id
  308. */
  309. public function getWarehouseName($id)
  310. {
  311. $objGoodsBasicRelevantCache = new GoodsBasicRelevant($this->enterpriseId);
  312. $result = $objGoodsBasicRelevantCache->getNameByWarehouseId($id);
  313. $warehouseName = '';
  314. if(!$result){
  315. $dbResult = $this->objDWarehouse->get(['id' => $id]);
  316. if($dbResult){
  317. $warehouseName = isset($dbResult['warehouseName']) ? $dbResult['warehouseName'] : '';
  318. $objGoodsBasicRelevantCache->cacheWarehouseIdRelationName($id, $dbResult);
  319. }
  320. }else{
  321. $warehouseName = $result;
  322. }
  323. return $warehouseName;
  324. }
  325. }