MAccount.Class.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. <?php
  2. /**
  3. * 账户管理模块
  4. * Created by PhpStorm.
  5. * User: wxj
  6. * Date: 2019/10/30
  7. * Time: 14:02
  8. */
  9. namespace JinDouYun\Model\Finance;
  10. use JinDouYun\Dao\Finance\DAccountDetail;
  11. use JinDouYun\Dao\Order\DOrder;
  12. use JinDouYun\Dao\Stock\DInventoryOut;
  13. use JinDouYun\Model\MBaseModel;
  14. use Mall\Framework\Core\ErrorCode;
  15. use Mall\Framework\Core\StatusCode;
  16. use Mall\Framework\Core\ResultWrapper;
  17. use JinDouYun\Dao\Finance\DAccount;
  18. class MAccount extends MBaseModel {
  19. private $objDAccount;
  20. private $enterpriseId;
  21. private $userCenterId;
  22. private $objDAccountDetail;
  23. private $cutTable = 100;
  24. public function __construct($enterpriseId, $userCenterId)
  25. {
  26. $this->userCenterId = $userCenterId;
  27. $this->enterpriseId = $enterpriseId;
  28. parent::__construct($enterpriseId, $userCenterId);
  29. $this->objDAccount = new DAccount('finance');
  30. $this->objDAccountDetail = new DAccountDetail('finance');
  31. }
  32. /**
  33. * 添加账户
  34. *
  35. * @param array $params 账户数据
  36. *
  37. * @return ResultWrapper
  38. */
  39. public function addAccount($params)
  40. {
  41. if($params['isDefault'] == StatusCode::$standard) {
  42. $result = self::updateDefaultStatus();
  43. if($result->isSuccess() == false) {
  44. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  45. }
  46. }
  47. $AccountId = $this->objDAccount->insert($params);
  48. if($AccountId === false){
  49. return ResultWrapper::fail($this->objDAccount->error(), ErrorCode::$dberror);
  50. }else{
  51. return ResultWrapper::success($AccountId);
  52. }
  53. }
  54. /**
  55. * 获取指定账户信息
  56. */
  57. public function getAccountInfo($AccountIds)
  58. {
  59. $where = ['id'=>$AccountIds, 'enterpriseId'=>$this->enterpriseId];
  60. $dbResult = $this->objDAccount->get($where);
  61. if($dbResult === false){
  62. return ResultWrapper::fail($this->objDAccount->error(), ErrorCode::$dberror);
  63. }else{
  64. return ResultWrapper::success($dbResult);
  65. }
  66. }
  67. /**
  68. * 编辑账户
  69. *
  70. * @param int|array $params 修改账户的数据
  71. *
  72. * @return ResultWrapper
  73. */
  74. public function editAccount($params)
  75. {
  76. if( empty($params['id']) ){
  77. return ResultWrapper::fail('没有指定要修改的Accountid', ErrorCode::$paramError);
  78. }
  79. if(isset($params['isDefault']) && $params['isDefault'] == StatusCode::$standard) {
  80. $result = self::updateDefaultStatus();
  81. if($result->isSuccess() == false) {
  82. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  83. }
  84. }
  85. $updateAccountId = $params['id'];
  86. unset($params['id']);
  87. $dbResult = $this->objDAccount->update($params, $updateAccountId);
  88. if($dbResult === false){
  89. return ResultWrapper::fail($this->objDAccount->error(), ErrorCode::$dberror);
  90. }else{
  91. return ResultWrapper::success($dbResult);
  92. }
  93. }
  94. /**
  95. * 给账户加减金额
  96. * @param $id
  97. * @param $changedMoney
  98. * @return ResultWrapper
  99. */
  100. public function updateMoney($id, $changedMoney) {
  101. $dbResult = $this->objDAccount->set_inc('money', $id, $changedMoney);
  102. if($dbResult === false){
  103. return ResultWrapper::fail($this->objDAccount->error(), ErrorCode::$dberror);
  104. }else{
  105. return ResultWrapper::success($dbResult);
  106. }
  107. }
  108. /**
  109. * 删除账户
  110. *
  111. * @param array $delFinanceIds 要删除的账户id
  112. *
  113. * @return ResultWrapper
  114. */
  115. public function delAccount($delAccountIds)
  116. {
  117. if (empty($delAccountIds)) {
  118. return ResultWrapper::fail('参数错误', ErrorCode::$paramError);
  119. }
  120. $dbResult = $this->objDAccount->update( ['deleteStatus'=>StatusCode::$delete], $delAccountIds );
  121. if($dbResult === false){
  122. return ResultWrapper::fail($this->objDAccount->error(), ErrorCode::$dberror);
  123. }else{
  124. return ResultWrapper::success($dbResult);
  125. }
  126. }
  127. /**
  128. * 账户启用和禁用
  129. * @param array $params
  130. * @return ResultWrapper
  131. */
  132. public function updateAccountStatus($params)
  133. {
  134. $dbResult = $this->objDAccount->update( ['enableStatus'=>$params['enableStatus']], $params['id'] );
  135. if($dbResult === false){
  136. return ResultWrapper::fail($this->objDAccount->error(), ErrorCode::$dberror);
  137. }else{
  138. return ResultWrapper::success($dbResult);
  139. }
  140. }
  141. /**
  142. * 账户是否默认
  143. * @param array $params
  144. * @return ResultWrapper
  145. */
  146. public function updateAccountDefaultStatus($params)
  147. {
  148. $result = self::updateDefaultStatus();
  149. if($result->isSuccess() == false) {
  150. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  151. }
  152. $dbResult = $this->objDAccount->update( ['isDefault'=>$params['isDefault']], $params['id'] );
  153. if($dbResult === false){
  154. return ResultWrapper::fail($this->objDAccount->error(), ErrorCode::$dberror);
  155. }else{
  156. return ResultWrapper::success($dbResult);
  157. }
  158. }
  159. public function updateDefaultStatus() {
  160. $result = $this->objDAccount->update(['isDefault'=>StatusCode::$delete]);
  161. if($result === false){
  162. return ResultWrapper::fail($this->objDAccount->error(), ErrorCode::$dberror);
  163. }else{
  164. return ResultWrapper::success($result);
  165. }
  166. }
  167. /**
  168. * 获取所有账户数据
  169. *
  170. * @param array $selectParams 过滤条件
  171. *
  172. * @return ResultWrapper
  173. */
  174. public function getAllAccount($selectParams,$includeMaster = true)
  175. {
  176. $limit = $selectParams['limit'];
  177. unset($selectParams['limit']);
  178. $offset = $selectParams['offset'];
  179. unset($selectParams['offset']);
  180. $whereSql = ' where enterpriseId = '.$this->enterpriseId .' and deleteStatus = '.StatusCode::$standard;
  181. if (isset($selectParams['shopId']) && !empty($selectParams['shopId'])) {
  182. $inMasterSql = $includeMaster ? ",0" : "";
  183. $whereSql .= ' and shopId in ( ' . $selectParams['shopId'] .$inMasterSql.')';
  184. }
  185. $totalSql = 'SELECT COUNT(1) as count FROM ' .$this->objDAccount->get_Table() . $whereSql;
  186. $dbTotalResult = $this->objDAccount->query($totalSql);
  187. if ($dbTotalResult === false) {
  188. return ResultWrapper::fail($this->objDAccount->error(), ErrorCode::$dberror);
  189. }
  190. if(empty($dbTotalResult)){
  191. return ResultWrapper::success([]);
  192. }
  193. $sql = 'select * from '. $this->objDAccount->get_Table(). $whereSql.' ORDER BY createTime DESC LIMIT ' . $offset . ' , ' . $limit;
  194. $dbResult = $this->objDAccount->query($sql);
  195. if($dbResult === false){
  196. return ResultWrapper::fail($this->objDAccount->error(), ErrorCode::$dberror);
  197. }
  198. // isMem 是为了区分后台会员充值返回不需要余额支付
  199. if (isset($selectParams['isMem']) && !empty($selectParams['isMem'])) {
  200. foreach ($dbResult as $accountKey => $accountValue){
  201. if ($accountValue['name'] == '余额支付'){
  202. unset($dbResult[$accountKey]);
  203. }
  204. }
  205. $dbTotalResult[0]['count'] = $dbTotalResult[0]['count']-1;
  206. }
  207. //$total = $this->objDAccount->count($selectParams);
  208. //传时间代表需要重现更新数据
  209. unset($whereSql);
  210. foreach ($dbResult as $key => $value){
  211. $whereSql = '';
  212. if ( (isset($selectParams['start']) && !empty($selectParams['start']))&&(isset($selectParams['end']) && !empty($selectParams['end'])) ) {
  213. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  214. $whereSql .= $where . ' createTime BETWEEN ' . $selectParams['start'] . ' AND '. $selectParams['end'] .' and accountId = '.$value['id'];
  215. }else{
  216. $whereSql .= ' where accountId = '.$value['id'];
  217. }
  218. $tableName = $this->objDAccountDetail->getTableName('qianniao_account_detail_' . $this->enterpriseId, $value['id'], $this->cutTable);
  219. $this->objDAccountDetail->setTable($tableName);
  220. // 查询流水表拿到时间段的收入,支出
  221. $detailSql = 'select beginBalance,SUM(income) as income,SUM(expend) as expend from '.$this->objDAccountDetail->get_Table().$whereSql;
  222. $detailDbResult = $this->objDAccountDetail->query($detailSql);
  223. if($detailDbResult === false){
  224. return ResultWrapper::fail($this->objDAccountDetail->error(), ErrorCode::$dberror);
  225. }
  226. unset($whereSql);
  227. //如果是新增的账户,没有detail
  228. if ( empty($detailDbResult[0]['beginBalance']) && empty($detailDbResult[0]['income']) || empty($detailDbResult[0]['expend']) ){
  229. $dbResult[$key]['beginMoney'] = isset($value['beginMoney']) ? $value['beginMoney'] : 0.00;
  230. $dbResult[$key]['income'] = isset($detailDbResult[0]['income']) ? $detailDbResult[0]['income'] : 0.00;
  231. $dbResult[$key]['expend'] = isset($detailDbResult[0]['expend']) ? $detailDbResult[0]['expend'] : 0.00;
  232. }else{
  233. //源数据加本期收入,本期支出
  234. $dbResult[$key]['beginMoney'] = isset($detailDbResult[0]['endBalance']) ? $detailDbResult[0]['endBalance'] : 0.00;
  235. $dbResult[$key]['income'] = isset($detailDbResult[0]['income']) ? $detailDbResult[0]['income'] : 0.00;
  236. $dbResult[$key]['expend'] = isset($detailDbResult[0]['expend']) ? $detailDbResult[0]['expend'] : 0.00;
  237. }
  238. }
  239. $return = [
  240. 'data' => self::format(array_values($dbResult)),
  241. 'total' => $dbTotalResult[0]['count'],
  242. ];
  243. return ResultWrapper::success($return);
  244. }
  245. public function format($data) {
  246. foreach ($data as $k=>$v) {
  247. $data[$k]['accountCode'] = createCode(StatusCode::$code['account']['prefix'], $v['id'], StatusCode::$code['account']['length']);
  248. }
  249. return $data;
  250. }
  251. /**
  252. * 获取特定账户
  253. * @param int $type 0普通账户 1微信支付 2支付宝支付 3银行卡支付
  254. * @return ResultWrapper
  255. */
  256. public function getDefaultAccount($type=1) {
  257. $selectParams['enterpriseId'] = $this->enterpriseId;
  258. $selectParams['deleteStatus'] = StatusCode::$standard;
  259. $selectParams['type'] = $type;
  260. // 如果是货到付款 默认退到余额账户
  261. if($selectParams['type'][0] == StatusCode::$payType['cashPay']){
  262. $selectParams['type'] = StatusCode::$payType['balance'];
  263. }
  264. $dbResult = $this->objDAccount->get($selectParams);
  265. if($dbResult === false){
  266. return ResultWrapper::fail($this->objDAccount->error(), ErrorCode::$dberror);
  267. }else{
  268. return ResultWrapper::success($dbResult);
  269. }
  270. }
  271. /**
  272. * 财务今日报表
  273. */
  274. public function getTodayStatistics($selectParams)
  275. {
  276. $starTime = strtotime(date('Y-m-d 00:00:00'));
  277. $endTime = strtotime(date('Y-m-d 23:59:59'));
  278. if (isset($selectParams['start']) && !empty($selectParams['start'])){
  279. $starTime = $selectParams['start'];
  280. }
  281. if (isset($selectParams['end']) && !empty($selectParams['end'])){
  282. $endTime = $selectParams['end'];
  283. }
  284. $tableName = '_'.$this->enterpriseId.'_'.date('Y',$starTime).'_'.ceil(date('m',$starTime) / 3);
  285. // 查询之前切一下表,避免表不存在导致的sql报错问题
  286. $this->objDAccount->setTable('qianniao_receive_receipt'.$tableName);
  287. $this->objDAccount->setTable('qianniao_received'.$tableName);
  288. $this->objDAccount->setTable('qianniao_pay_receipt'.$tableName);
  289. $this->objDAccount->setTable('qianniao_paid'.$tableName);
  290. $this->objDAccount->setTable('qianniao_received_account'.$tableName);
  291. //应收总额
  292. $sql = 'select sum(receiveMoney) as money from qianniao_receive_receipt'.$tableName.' where deleteStatus = 5 and auditStatus = 2 and createTime between '.$starTime.' and '.$endTime;
  293. $dbResult = $this->objDAccount->query($sql);
  294. if($dbResult === false){
  295. return ResultWrapper::fail($this->objDAccount->error(), ErrorCode::$dberror);
  296. }
  297. $return['getMoney'] = $dbResult[0]['money'] ? $dbResult[0]['money'] : 0;
  298. //实收总额
  299. $sql = 'select sum(totalFinalMoney) as money from qianniao_received'.$tableName.' where auditStatus = 2 and createTime between '.$starTime.' and '.$endTime;
  300. $dbResult = $this->objDAccount->query($sql);
  301. if($dbResult === false){
  302. return ResultWrapper::fail($this->objDAccount->error(), ErrorCode::$dberror);
  303. }
  304. $return['shouldGetMoney'] = $dbResult[0]['money'] ? $dbResult[0]['money'] : 0;
  305. //欠款总额
  306. $return['oweMoney'] = $return['getMoney'] - $return['shouldGetMoney'];
  307. //毛利总额
  308. $objDOrder = new DOrder();
  309. $sql = 'select sum(g.buyNum * g.outCostPrice) as money from qianniao_order_'.$this->enterpriseId.'_1 as o left join qianniao_order_goods_'.$this->enterpriseId.'_1 as g on o.id = g.orderId where o.outStatus = 5 and o.deleteStatus = 5 and g.returnStatus = 0 and o.createTime between '.$starTime.' and '.$endTime;
  310. $dbResult = $objDOrder->query($sql);
  311. if($dbResult === false){
  312. return ResultWrapper::fail($objDOrder->error(), ErrorCode::$dberror);
  313. }
  314. $costMoney = $dbResult[0]['money'] ? $dbResult[0]['money'] : 0;
  315. $return['profitMoney'] = $return['getMoney'] - $costMoney;
  316. //应付总额
  317. $sql = 'select sum(payMoney) as money from qianniao_pay_receipt'.$tableName.' where auditStatus = 2 and deleteStatus = 5 and createTime between '.$starTime.' and '.$endTime;
  318. $dbResult = $this->objDAccount->query($sql);
  319. if($dbResult === false){
  320. return ResultWrapper::fail($this->objDAccount->error(), ErrorCode::$dberror);
  321. }
  322. $return['payMoney'] = $dbResult[0]['money'] ? $dbResult[0]['money'] : 0;
  323. //实付总额
  324. $sql = 'select sum(totalFinalMoney) as money from qianniao_paid'.$tableName.' where auditStatus = 2 and createTime between '.$starTime.' and '.$endTime;
  325. $dbResult = $this->objDAccount->query($sql);
  326. if($dbResult === false){
  327. return ResultWrapper::fail($this->objDAccount->error(), ErrorCode::$dberror);
  328. }
  329. $return['shouldPayMoney'] = $dbResult[0]['money'] ? $dbResult[0]['money'] : 0;
  330. foreach($return as &$value){
  331. $value = sprintf('%.2f',$value);
  332. }
  333. // 各个财务账户今天收款金额
  334. $sql = "select a.accountId,sum(r.totalFinalMoney) as totalMoney,a.accountName from qianniao_received".$tableName." r left JOIN qianniao_received_account".$tableName." a on r.id = a.receivedId where r.auditStatus = ".StatusCode::$auditStatus['auditPass']." and r.createTime between ".$starTime." and ".$endTime." GROUP BY a.accountId";
  335. $dbResult = $this->objDAccount->query($sql);
  336. if($dbResult === false){
  337. return ResultWrapper::fail($this->objDAccount->error(), ErrorCode::$dberror);
  338. }
  339. $return['todayreceivedMoney'] = !empty($dbResult) ? $dbResult : [];
  340. /*
  341. //微信应收总额 账户类型 0普通账户 1支付宝账户 2微信账户 3银行账户
  342. $sql = 'select sum(receiveMoney) as money from qianniao_receive_receipt'.$tableName.' r left join qianniao_received_account'.$tableName.' a on a.receivedId = r.id left join qianniao_account aa on a.accountId = aa.id where r.deleteStatus = 5 and r.auditStatus = 2 and aa.type = 2 and r.createTime between '.$starTime.' and '.$endTime;
  343. $dbResult = $this->objDAccount->query($sql);
  344. if($dbResult === false){
  345. return ResultWrapper::fail($this->objDAccount->error(), ErrorCode::$dberror);
  346. }
  347. $return['wxGetMoney'] = $dbResult[0]['money'] ? $dbResult[0]['money'] : 0;
  348. //现金应收总额
  349. $sql = 'select sum(receiveMoney) as money from qianniao_receive_receipt'.$tableName.' r left join qianniao_received_account'.$tableName.' a on a.receivedId = r.id left join qianniao_account aa on a.accountId = aa.id where r.deleteStatus = 5 and r.auditStatus = 2 and aa.type = 0 and r.createTime between '.$starTime.' and '.$endTime;
  350. $dbResult = $this->objDAccount->query($sql);
  351. if($dbResult === false){
  352. return ResultWrapper::fail($this->objDAccount->error(), ErrorCode::$dberror);
  353. }
  354. $return['cashGetMoney'] = $dbResult[0]['money'] ? $dbResult[0]['money'] : 0;
  355. //支付宝应收总额
  356. $sql = 'select sum(receiveMoney) as money from qianniao_receive_receipt'.$tableName.' r left join qianniao_received_account'.$tableName.' a on a.receivedId = r.id left join qianniao_account aa on a.accountId = aa.id where r.deleteStatus = 5 and r.auditStatus = 2 and aa.type = 1 and r.createTime between '.$starTime.' and '.$endTime;
  357. $dbResult = $this->objDAccount->query($sql);
  358. if($dbResult === false){
  359. return ResultWrapper::fail($this->objDAccount->error(), ErrorCode::$dberror);
  360. }
  361. $return['aliGetMoney'] = $dbResult[0]['money'] ? $dbResult[0]['money'] : 0; */
  362. return ResultWrapper::success($return);
  363. }
  364. /**
  365. * 计算表名
  366. * @param $params
  367. * @param string $table
  368. * @return ResultWrapper
  369. * @throws Exception
  370. */
  371. public function calTableName($params,$table = 'qianniao_receive_receipt')
  372. {
  373. if(empty($params['start'])) return ResultWrapper::fail('开始时间参数错误', ErrorCode::$paramError);
  374. if(empty($params['end'])) return ResultWrapper::fail('结束时间参数错误', ErrorCode::$paramError);
  375. $table && $table = $table.'_'.$this->enterpriseId.'_';
  376. $startTableTerm = self::setDetailsTableTerm($params['start']);
  377. $endTableTerm = self::setDetailsTableTerm($params['end']);
  378. $startYear = substr(date('Y', $params['start']), -2);
  379. $endYear = substr(date('Y', $params['end']), -2);
  380. if($startTableTerm == $endTableTerm){//同一个表
  381. $dbResult = $this->objDAccount->existsTable($table.$startTableTerm);
  382. if($dbResult){
  383. return ResultWrapper::success([$table.$startTableTerm]);
  384. }
  385. return ResultWrapper::success([]);
  386. }
  387. //不同表
  388. $tableNameArray = [];
  389. for($ii = $startYear; $ii <= $endYear; $ii++){
  390. for($i = 1; $i <= 4; $i++){
  391. $term = $ii.$i;
  392. if($startTableTerm <= $term && $term <= $endTableTerm){
  393. $tableName = $table.$term;
  394. $dbResult = $this->objDAccount->existsTable($tableName);
  395. if($dbResult){//存在
  396. $tableNameArray[] = $tableName;
  397. }else{//不存在
  398. continue;
  399. }
  400. }
  401. }
  402. }
  403. return ResultWrapper::success($tableNameArray);
  404. }
  405. /**
  406. * Doc: (des="")
  407. * User: XMing
  408. * Date: 2021/1/25
  409. * Time: 11:23 上午
  410. * @param int $createTime
  411. * @return string
  412. */
  413. public function setDetailsTableTerm($createTime = 0)
  414. {
  415. if(!$createTime) $createTime = time();
  416. return substr(date('Y', $createTime), -2) . ceil(date('m', $createTime) / 3);
  417. }
  418. }