MSupplier.Class.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. <?php
  2. /**
  3. * 供应商数据模型
  4. * Created by PhpStorm.
  5. * User: XiaoMing
  6. * Date: 2019/11/11
  7. * Time: 14:33
  8. */
  9. namespace JinDouYun\Model\Purchase;
  10. use JinDouYun\Controller\Common\Logger;
  11. use JinDouYun\Model\GoodsManage\MGoodsBasic;
  12. use JinDouYun\Model\Order\MSupplierOrderDetails;
  13. use Mall\Framework\Core\StatusCode;
  14. use Mall\Framework\Core\ErrorCode;
  15. use Mall\Framework\Core\ResultWrapper;
  16. use JinDouYun\Cache\SupplierCache;
  17. use JinDouYun\Cache\OverviewCache;
  18. use JinDouYun\Model\SysAreaChina\MSysAreaChina;
  19. use JinDouYun\Model\UserCenter\MUserCenterRegister;
  20. use JinDouYun\Model\Enterprise\MEnterprise;
  21. use JinDouYun\Dao\Purchase\DSupplier;
  22. class MSupplier
  23. {
  24. private $objDSupplier;
  25. private $objOverviewCache;
  26. private $onlineUserId;
  27. private $onlineEnterpriseId;
  28. private $objSupplierCache;
  29. /**
  30. * MSupplier constructor.
  31. * @param $onlineUserId
  32. * @param $onlineEnterpriseId
  33. * @throws \Exception
  34. */
  35. public function __construct($onlineUserId, $onlineEnterpriseId)
  36. {
  37. $this->onlineUserId = $onlineUserId;
  38. $this->onlineEnterpriseId = $onlineEnterpriseId;
  39. $this->objDSupplier = new DSupplier('stock');
  40. $this->objOverviewCache = new OverviewCache();
  41. $this->objSupplierCache = new SupplierCache();
  42. $this->objDSupplier->setTable($this->objDSupplier->get_Table() . '_' . $this->onlineEnterpriseId);
  43. }
  44. /**
  45. * 添加供应商
  46. * @param $params
  47. * @return ResultWrapper
  48. */
  49. public function addSupplier($params)
  50. {
  51. $objMEnterprise = new MEnterprise();
  52. $modelResult = $objMEnterprise->createAppid($this->onlineUserId);
  53. $params['token'] = $modelResult['token'];
  54. $this->objDSupplier->beginTransaction();
  55. $dbResult = $this->objDSupplier->insert($params);
  56. if ($dbResult === false) {
  57. $this->objDSupplier->rollBack();
  58. return ResultWrapper::fail($this->objDSupplier->error(), ErrorCode::$dberror);
  59. }
  60. //缓存token
  61. $this->objSupplierCache->setSupplierToken($this->onlineEnterpriseId, $dbResult, $params['token']);
  62. $code = createCode(StatusCode::$code['supplier']['prefix'], $dbResult, StatusCode::$code['supplier']['length']);
  63. $dbResult = $this->objDSupplier->update(['code' => $code], ['id' => $dbResult]);
  64. if ($dbResult === false) {
  65. $this->objDSupplier->rollBack();
  66. return ResultWrapper::fail($this->objDSupplier->error(), ErrorCode::$dberror);
  67. }
  68. $this->objDSupplier->commit();
  69. return ResultWrapper::success($dbResult);
  70. }
  71. /**
  72. * 添加供应商为用户
  73. * @param $params
  74. * @return ResultWrapper
  75. */
  76. public function addSupplierUserCenter($params)
  77. {
  78. // 获取供应商信息
  79. $dbResult = $this->objDSupplier->get(['id' => $params['id']]);
  80. if($dbResult === false){
  81. return ResultWrapper::fail($this->objDSupplier->error(), ErrorCode::$dberror);
  82. }
  83. $supplier = $dbResult;
  84. unset($dbResult);
  85. if(empty($supplier)){
  86. return ResultWrapper::fail('供应商不存在', ErrorCode::$paramError);
  87. }
  88. // 添加user_center数据
  89. $objMUserCenterRegister = new MUserCenterRegister();
  90. $modelResult = $objMUserCenterRegister->addUser($supplier['mobile'], StatusCode::$roleType['supplier'], $this->onlineEnterpriseId);
  91. if(!$modelResult->isSuccess()){
  92. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  93. }
  94. $return = $modelResult->getData();
  95. unset($modelResult);
  96. // 判断是否已经存在
  97. $dbResult = $this->objDSupplier->get(['userCenterId' => $return['userCenterId']]);
  98. if($dbResult === false){
  99. return ResultWrapper::fail($this->objDSupplier->error(), ErrorCode::$dberror);
  100. }
  101. if(!empty($dbResult)){
  102. return ResultWrapper::fail('该手机号已经是供应商', ErrorCode::$paramError);
  103. }
  104. // 生成供应商token
  105. if(empty($supplier['token'])){
  106. $objMEnterprise = new MEnterprise();
  107. $modelResult = $objMEnterprise->createAppid($this->onlineUserId);
  108. $token = $modelResult['token'];
  109. $this->objSupplierCache->setSupplierToken($this->onlineEnterpriseId, $supplier['id'], $token);
  110. $update['token'] = $token;
  111. }
  112. // 绑定供应商和user_centerid
  113. $update = [
  114. 'userCenterId' => $return['userCenterId'],
  115. 'updateTime' => time(),
  116. ];
  117. $dbResult = $this->objDSupplier->update($update, ['id' => $supplier['id']]);
  118. if($dbResult === false){
  119. return ResultWrapper::fail($this->objDSupplier->error(), ErrorCode::$dberror);
  120. }
  121. return ResultWrapper::success($return['title']);
  122. }
  123. /**
  124. * 获取供应商信息
  125. * @param $id
  126. * @return ResultWrapper
  127. */
  128. public function getSupplierInfoById($id)
  129. {
  130. $dbResult = $this->objDSupplier->get($id);
  131. if ($dbResult === false) {
  132. return ResultWrapper::fail($this->objDSupplier->error(), ErrorCode::$dberror);
  133. }
  134. $formatResult = self::format($dbResult);
  135. if (!empty($formatResult)) {
  136. $dbResult = $formatResult[0];
  137. }
  138. return ResultWrapper::success($dbResult);
  139. }
  140. /**
  141. * 获取供应商信息
  142. * @param $userCenterId
  143. * @return ResultWrapper
  144. */
  145. public function getSupplierByUserCenterId($userCenterId)
  146. {
  147. $dbResult = $this->objDSupplier->get(['userCenterId' => $userCenterId]);
  148. if ($dbResult === false) {
  149. return ResultWrapper::fail($this->objDSupplier->error(), ErrorCode::$dberror);
  150. }
  151. return ResultWrapper::success($dbResult);
  152. }
  153. /**
  154. * 编辑供应商信息
  155. * @param $params
  156. * @return ResultWrapper
  157. */
  158. public function editSupplier($params)
  159. {
  160. if (empty($params['id'])) {
  161. return ResultWrapper::fail('没有id', ErrorCode::$paramError);
  162. }
  163. $id = $params['id'];
  164. unset($params['id']);
  165. $dbResult = $this->objDSupplier->update($params, $id);
  166. if ($dbResult === false) {
  167. return ResultWrapper::fail($this->objDSupplier->error(), ErrorCode::$dberror);
  168. }
  169. return ResultWrapper::success($dbResult);
  170. }
  171. /**
  172. * 供应商显示/隐藏
  173. * @param $params
  174. * @return ResultWrapper
  175. */
  176. public function updateEnableStatus($params)
  177. {
  178. $dbResult = $this->objDSupplier->update(['enableStatus' => $params['enableStatus']], $params['id']);
  179. if ($dbResult === false) {
  180. return ResultWrapper::fail($this->objDSupplier->error(), ErrorCode::$dberror);
  181. }
  182. return ResultWrapper::success($dbResult);
  183. }
  184. /**
  185. * 删除供应商
  186. * @param $id
  187. * @return ResultWrapper
  188. */
  189. public function delSupplier($id)
  190. {
  191. $dbResult = $this->objDSupplier->update(['deleteStatus' => StatusCode::$delete], $id);
  192. if ($dbResult === false) {
  193. return ResultWrapper::fail($this->objDSupplier->error(), ErrorCode::$dberror);
  194. }
  195. return ResultWrapper::success($dbResult);
  196. }
  197. /**
  198. * 供应商列表
  199. * @param $selectParams
  200. * @return ResultWrapper
  201. */
  202. public function getAllSupplier($selectParams)
  203. {
  204. $limit = $selectParams['limit'];
  205. unset($selectParams['limit']);
  206. $offset = $selectParams['offset'];
  207. unset($selectParams['offset']);
  208. $where = 'deleteStatus=' . StatusCode::$standard;
  209. if (isset($selectParams['enableStatus'])) {
  210. $where .= ' AND enableStatus = ' . $selectParams['enableStatus'];
  211. }
  212. if (isset($selectParams['keyword'])) {
  213. $where .= ' AND (realName LIKE "%'. $selectParams['keyword'] .'%" OR title LIKE "%' . $selectParams['keyword'] . '%" OR code LIKE "%' . $selectParams['keyword'] . '%")';
  214. }
  215. $dbResult = $this->objDSupplier->select($where, '*', 'createTime desc', $limit, $offset);
  216. if ($dbResult === false) {
  217. return ResultWrapper::fail($this->objDSupplier->error(), ErrorCode::$dberror);
  218. }
  219. $total = $this->objDSupplier->count($where);
  220. $return = [
  221. 'data' => self::format($dbResult),
  222. 'total' => ($total) ? intval($total) : 0,
  223. ];
  224. return ResultWrapper::success($return);
  225. }
  226. /**
  227. * 处理省市区
  228. * @param $data
  229. * @param bool $flag
  230. * @return mixed
  231. */
  232. private function format($data)
  233. {
  234. if (isset($data['id'])) {
  235. $dimension = $data;
  236. unset($data);
  237. $data[] = $dimension;
  238. }
  239. $objMSysAreaChina = new MSysAreaChina();
  240. foreach ($data as $key => $val) {
  241. $data[$key]['code'] = createCode(StatusCode::$code['supplier']['prefix'], $val['id'], StatusCode::$code['supplier']['length']);
  242. $areaName = $objMSysAreaChina->getNameByCode([
  243. $val['provinceCode'],
  244. $val['cityCode'],
  245. $val['districtCode']
  246. ]);
  247. $data[$key]['area']['provinceName'] = isset($areaName[$val['provinceCode']]) ? $areaName[$val['provinceCode']] : '';
  248. $data[$key]['area']['cityName'] = isset($areaName[$val['cityCode']]) ? $areaName[$val['cityCode']] : '';
  249. $data[$key]['area']['districtName'] = isset($areaName[$val['districtCode']]) ? $areaName[$val['districtCode']] : '';
  250. $data[$key]['area']['address'] = $val['address'];
  251. }
  252. return $data;
  253. }
  254. /**
  255. * 通过采购单id获取采购单下的商品
  256. * @param $ids
  257. * @return ResultWrapper
  258. */
  259. public function getSupplierNameByIds($ids)
  260. {
  261. if (empty($ids)){
  262. return ResultWrapper::success([]);
  263. }
  264. $dbResult = $this->objDSupplier->select(['id' => $ids], 'id,title');
  265. if ($dbResult === false) {
  266. Logger::logs(E_USER_ERROR,'sql error',__CLASS__,__LINE__,$this->objDSupplier->error());
  267. return ResultWrapper::fail($this->objDSupplier->error(), ErrorCode::$dberror);
  268. }
  269. return ResultWrapper::success($dbResult);
  270. }
  271. /**
  272. * 查询所有正常供应商数量
  273. */
  274. public function getAllSupplierNum()
  275. {
  276. $sql = "select count(id) as count from " . $this->objDSupplier->get_Table() . " where enableStatus = " . StatusCode::$standard . " and deleteStatus = " . StatusCode::$standard;
  277. $dbResult = [];
  278. $dbResult = $this->objDSupplier->query($sql);
  279. if ($dbResult === false) {
  280. return ResultWrapper::fail($this->objDSupplier->error(), ErrorCode::$dberror);
  281. }
  282. $returnData = array_shift($dbResult);
  283. return ResultWrapper::success($returnData['count']);
  284. }
  285. /**
  286. * 获取供应商余额
  287. *
  288. * @param array $selectParams 过滤条件
  289. *
  290. * @param $isExport
  291. * @return ResultWrapper
  292. */
  293. public function getSupplierMoney($selectParams, $isExport)
  294. {
  295. $limit = $selectParams['limit'];
  296. unset($selectParams['limit']);
  297. $offset = $selectParams['offset'];
  298. unset($selectParams['offset']);
  299. $selectParams['deleteStatus'] = StatusCode::$standard;
  300. if($isExport){
  301. $dbResult = $this->objDSupplier->exportSelect($selectParams, '*', 'createTime desc');
  302. }else{
  303. $dbResult = $this->objDSupplier->select($selectParams, 'id AS supplierId,title,money', 'createTime desc', $limit, $offset);
  304. }
  305. if ($dbResult === false) {
  306. return ResultWrapper::fail($this->objDSupplier->error(), ErrorCode::$dberror);
  307. }
  308. if($isExport){
  309. self::export($dbResult);
  310. }else{
  311. foreach ($dbResult as $key => $value) {
  312. $dbResult[$key]['openingBalance'] = $value['money'];
  313. $dbResult[$key]['interimBalance'] = 0;
  314. $dbResult[$key]['endingBalance'] = $value['money'];
  315. unset($dbResult[$key]['money']);
  316. }
  317. }
  318. $total = $this->objDSupplier->count($selectParams);
  319. $return = [
  320. 'data' => $dbResult,
  321. 'total' => ($total) ? intval($total) : 0,
  322. ];
  323. return ResultWrapper::success($return);
  324. }
  325. /**
  326. * 导出方法
  327. *
  328. * @param $exportData
  329. */
  330. private static function export($exportData)
  331. {
  332. //导出到本地
  333. header("Content-type:application/vnd.ms-excel");
  334. header("Content-Disposition:filename=供应商余额表.csv");
  335. header('Cache-Control: max-age=0');
  336. $fp = fopen('php://output', 'a');
  337. $head = ['供应商名称', '期初余额', '采购金额','付款金额', '期末余额']; //定义标题
  338. foreach ($head as $i => $v) {
  339. $head[$i] = mb_convert_encoding($v, 'GBK', 'utf-8'); //将中文标题转换编码,否则乱码
  340. }
  341. fputcsv($fp, $head);
  342. $limit = 10000;
  343. $num = 0;//计数器
  344. foreach ($exportData as $v) {
  345. //循环数据
  346. $num++;
  347. if ($num == $limit) {
  348. ob_flush();//释放内存
  349. flush();
  350. }
  351. $rows['title'] = $v['title'];
  352. $rows['openingBalance'] = $v['money'];
  353. $rows['purchase'] = 0;//采购金额
  354. $rows['payment'] = 0;//付款金额
  355. $rows['endingBalance'] = $v['money'];
  356. foreach ($rows as $kk => $vv) {
  357. $rs[$kk] = mb_convert_encoding($vv, 'GBK', 'utf-8'); //转译编码
  358. }
  359. fputcsv($fp, $rs);
  360. $rows = [];
  361. }
  362. exit;
  363. }
  364. /**
  365. * 获取总的应付金额
  366. */
  367. public function getTotalShouldPay()
  368. {
  369. $result = $this->objOverviewCache->getAggregateStatistics($this->onlineEnterpriseId, 'totalShouldPay');
  370. return $result ? $result : 0;
  371. }
  372. /**
  373. * 今日供应商排行
  374. * @param int $ranking
  375. * @param null $shopId
  376. * @return array
  377. */
  378. public function todaySupplierRanking($ranking = 1, $shopId = null)
  379. {
  380. if ($ranking == 1) {
  381. $result = $this->objOverviewCache->getRanking($this->onlineEnterpriseId, 'supplierRanking', $shopId);
  382. } else {
  383. $result = $this->objOverviewCache->getSalesMoneyRanking($this->onlineEnterpriseId, 'supplierRanking', $shopId);
  384. }
  385. if (empty($result)) {
  386. return [];
  387. }
  388. //拼接supplierId
  389. $supplierIds = [];
  390. $i = 1;
  391. foreach ($result as $supplierId => $v) {
  392. $supplierIds[] = $supplierId;
  393. $i++;
  394. if ($i >= 8) {
  395. break;
  396. }
  397. }
  398. $supplierResult = self::getSupplierNameByIds($supplierIds);
  399. $supplierNameData = [];
  400. if ($supplierResult->isSuccess()) {
  401. $supplierResult = $supplierResult->getData();
  402. foreach ($supplierResult as $supplierData) {
  403. $supplierNameData[$supplierData['id']] = $supplierData['title'];
  404. }
  405. }
  406. //拼接返回数据
  407. $return = [];
  408. $i = 1;
  409. foreach ($result as $supplierId => $v) {
  410. $rank = [];
  411. $rank['id'] = $i;
  412. $rank['supplierName'] = isset($supplierNameData[$supplierId]) ? $supplierNameData[$supplierId] : '';
  413. $rank['numberOrMoney'] = $v;
  414. $return[] = $rank;
  415. $i++;
  416. if ($i >= 8) {
  417. break;
  418. }
  419. }
  420. return $return;
  421. }
  422. /**
  423. * Doc: (des="根据供应商id获取数据")
  424. * User: XMing
  425. * Date: 2020/12/16
  426. * Time: 6:12 下午
  427. * @param int $id
  428. * @return ResultWrapper
  429. */
  430. public function getSupplierById(int $id): ResultWrapper
  431. {
  432. $dbResult = $this->objDSupplier->get($id);
  433. if ($dbResult === false) {
  434. return ResultWrapper::fail($this->objDSupplier->error(), ErrorCode::$dberror);
  435. }
  436. if (empty($dbResult)){
  437. return ResultWrapper::fail('未获取到供应商数据',ErrorCode::$paramError);
  438. }
  439. return ResultWrapper::success($dbResult);
  440. }
  441. public static function supplierMap(array $params): array
  442. {
  443. $map = [];
  444. foreach ($params as $param) {
  445. $map[$param['id']] = $param;
  446. }
  447. return $map;
  448. }
  449. public function getWithdrawal($id): ResultWrapper
  450. {
  451. if (empty($id)){
  452. return ResultWrapper::fail('供应商id为空',ErrorCode::$paramError);
  453. }
  454. $supplier = $this->objDSupplier->get($id);
  455. if ($supplier === false) {
  456. return ResultWrapper::fail($this->objDSupplier->error(), ErrorCode::$dberror);
  457. }
  458. $ret = [
  459. 'id' => $id,
  460. 'title' => getArrayItem($supplier,'title',''),
  461. 'money' => getArrayItem($supplier,'money',0),
  462. 'waitAuditWithdraw' => getArrayItem($supplier,'waitAuditWithdraw',0),
  463. 'auditWithdraw' => getArrayItem($supplier,'auditWithdraw',0),
  464. 'withdraw' => getArrayItem($supplier,'withdraw',0),
  465. ];
  466. return ResultWrapper::success($ret);
  467. }
  468. /**
  469. * Doc: (des="")
  470. * User: XMing
  471. * Date: 2020/12/26
  472. * Time: 4:41 下午
  473. * @param int|null $supplierId
  474. * @return ResultWrapper
  475. * @throws \Exception
  476. */
  477. public function statistics($supplierId)
  478. {
  479. $supplier = $this->objDSupplier->get($supplierId);
  480. if ($supplier === false){
  481. return ResultWrapper::fail($this->objDSupplier->error(),ErrorCode::$dberror);
  482. }
  483. if (empty($supplier)){
  484. return ResultWrapper::fail('没有获取到指定的供应商',ErrorCode::$paramError);
  485. }
  486. $objMSupplierOrderDetails = new MSupplierOrderDetails($this->onlineEnterpriseId,$this->onlineUserId);
  487. $orderTotalResult = $objMSupplierOrderDetails->getTotalBySupplierId($supplierId);
  488. if (!$orderTotalResult->isSuccess()){
  489. return ResultWrapper::fail($orderTotalResult->getData(),$orderTotalResult->getErrorCode());
  490. }
  491. $objMGoodsBasic = new MGoodsBasic($this->onlineUserId,$this->onlineEnterpriseId);
  492. $materielNumResult = $objMGoodsBasic->getTotalBySupplierId($supplierId);
  493. if (!$materielNumResult->isSuccess()){
  494. return ResultWrapper::fail($materielNumResult->getData(),$materielNumResult->getErrorCode());
  495. }
  496. $ret = [
  497. 'money' => getArrayItem($supplier,'money','0.00'),
  498. 'subOrderNum' => $orderTotalResult->getData(),
  499. 'materielNum' => $materielNumResult->getData(),
  500. ];
  501. return ResultWrapper::success($ret);
  502. }
  503. }