MFinanceType.Class.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 Mall\Framework\Core\ErrorCode;
  11. use Mall\Framework\Core\StatusCode;
  12. use Mall\Framework\Core\ResultWrapper;
  13. use JinDouYun\Dao\Finance\DFinanceType;
  14. use JinDouYun\Dao\System\DAdminSetting;
  15. class MFinanceType {
  16. private $objDFinanceType;
  17. private $objDAdminSetting;
  18. private $enterpriseId;
  19. private $userCenterId;
  20. public function __construct($enterpriseId, $userCenterId)
  21. {
  22. $this->userCenterId = $userCenterId;
  23. $this->enterpriseId = $enterpriseId;
  24. $this->objDFinanceType = new DFinanceType('finance');
  25. $this->objDAdminSetting = new DAdminSetting('default');
  26. }
  27. /**
  28. * 添加财务类型
  29. *
  30. * @param array $params 财务类型数据
  31. *
  32. * @return ResultWrapper
  33. */
  34. public function addFinanceType($params)
  35. {
  36. $setDefault = self::createDefaultValue();
  37. if ($setDefault->isSuccess() == false) {
  38. return ResultWrapper::fail($setDefault->getData(), $setDefault->getErrorCode());
  39. }
  40. if($params['isDefault'] == StatusCode::$standard) {
  41. $result = self::updateDefaultStatus();
  42. if($result->isSuccess() == false) {
  43. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  44. }
  45. }
  46. $FinanceTypeId = $this->objDFinanceType->insert($params);
  47. if($FinanceTypeId === false){
  48. return ResultWrapper::fail($this->objDFinanceType->error(), ErrorCode::$dberror);
  49. }else{
  50. return ResultWrapper::success($FinanceTypeId);
  51. }
  52. }
  53. public function updateDefaultStatus() {
  54. $result = $this->objDFinanceType->update(['isDefault'=>StatusCode::$delete]);
  55. if($result === false){
  56. return ResultWrapper::fail($this->objDFinanceType->error(), ErrorCode::$dberror);
  57. }else{
  58. return ResultWrapper::success($result);
  59. }
  60. }
  61. /**
  62. * 获取指定财务类型信息
  63. */
  64. public function getFinanceTypeInfo($FinanceTypeIds)
  65. {
  66. $dbResult = $this->objDFinanceType->get($FinanceTypeIds);
  67. if($dbResult === false){
  68. return ResultWrapper::fail($this->objDFinanceType->error(), ErrorCode::$dberror);
  69. }else{
  70. return ResultWrapper::success($dbResult);
  71. }
  72. }
  73. /**
  74. * 编辑财务类型
  75. *
  76. * @param int|array $params 修改财务类型的数据
  77. *
  78. * @return ResultWrapper
  79. */
  80. public function editFinanceType($params)
  81. {
  82. if( empty($params['id']) ){
  83. return ResultWrapper::fail('没有指定要修改的FinanceTypeid', ErrorCode::$paramError);
  84. }
  85. if($params['isDefault'] == StatusCode::$standard) {
  86. $result = self::updateDefaultStatus();
  87. if($result->isSuccess() == false) {
  88. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  89. }
  90. }
  91. $updateFinanceTypeId = $params['id'];
  92. unset($params['id']);
  93. $dbResult = $this->objDFinanceType->update($params, $updateFinanceTypeId);
  94. if($dbResult === false){
  95. return ResultWrapper::fail($this->objDFinanceType->error(), ErrorCode::$dberror);
  96. }else{
  97. return ResultWrapper::success($dbResult);
  98. }
  99. }
  100. /**
  101. * 删除财务类型
  102. *
  103. * @param array $delFinanceIds 要删除的财务类型id
  104. *
  105. * @return ResultWrapper
  106. */
  107. public function delFinanceType($delFinanceTypeIds)
  108. {
  109. if (empty($delFinanceTypeIds)) {
  110. return ResultWrapper::fail('参数错误', ErrorCode::$paramError);
  111. }
  112. $dbResult = $this->objDFinanceType->update( ['deleteStatus'=>StatusCode::$delete], $delFinanceTypeIds );
  113. if($dbResult === false){
  114. return ResultWrapper::fail($this->objDFinanceType->error(), ErrorCode::$dberror);
  115. }else{
  116. return ResultWrapper::success($dbResult);
  117. }
  118. }
  119. /**
  120. * 财务类型启用和禁用
  121. * @param array $params
  122. * @return ResultWrapper
  123. */
  124. public function updateFinanceTypeStatus($params)
  125. {
  126. $dbResult = $this->objDFinanceType->update( ['enableStatus'=>$params['enableStatus']], $params['id'] );
  127. if($dbResult === false){
  128. return ResultWrapper::fail($this->objDFinanceType->error(), ErrorCode::$dberror);
  129. }else{
  130. return ResultWrapper::success($dbResult);
  131. }
  132. }
  133. /**
  134. * 财务类型是否默认
  135. * @param array $params
  136. * @return ResultWrapper
  137. */
  138. public function updateFinanceTypeDefaultStatus($params)
  139. {
  140. $result = self::updateDefaultStatus();
  141. if($result->isSuccess() == false) {
  142. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  143. }
  144. $dbResult = $this->objDFinanceType->update( ['isDefault'=>$params['isDefault']], $params['id'] );
  145. if($dbResult === false){
  146. return ResultWrapper::fail($this->objDFinanceType->error(), ErrorCode::$dberror);
  147. }else{
  148. return ResultWrapper::success($dbResult);
  149. }
  150. }
  151. /**
  152. * 获取所有财务类型数据
  153. *
  154. * @param array $selectParams 过滤条件
  155. *
  156. * @return ResultWrapper
  157. */
  158. public function getAllFinanceType($selectParams)
  159. {
  160. $setDefault = self::createDefaultValue();
  161. if ($setDefault->isSuccess() == false) {
  162. return ResultWrapper::fail($setDefault->getData(), $setDefault->getErrorCode());
  163. }
  164. $limit = $selectParams['limit'];
  165. unset($selectParams['limit']);
  166. $offset = $selectParams['offset'];
  167. unset($selectParams['offset']);
  168. $selectParams['deleteStatus'] = StatusCode::$standard;
  169. $selectParams['enterpriseId'] = [0,$this->enterpriseId];
  170. $dbResult = $this->objDFinanceType->select($selectParams, '*', 'createTime desc', $limit, $offset);
  171. if($dbResult === false){
  172. return ResultWrapper::fail($this->objDFinanceType->error(), ErrorCode::$dberror);
  173. }
  174. $total = $this->objDFinanceType->count($selectParams);
  175. $return = [
  176. 'data' => $dbResult,
  177. 'total' => ($total)?intval($total):0,
  178. ];
  179. return ResultWrapper::success($return);
  180. }
  181. /**
  182. * 获取所有财务类型数据(不分页)
  183. * @param $selectParams
  184. * @return ResultWrapper
  185. */
  186. public function getAllFinanceTypeNoPage($selectParams)
  187. {
  188. $setDefault = self::createDefaultValue();
  189. if ($setDefault->isSuccess() == false) {
  190. return ResultWrapper::fail($setDefault->getData(), $setDefault->getErrorCode());
  191. }
  192. $selectParams['deleteStatus'] = StatusCode::$standard;
  193. $selectParams['enterpriseId'] = [0,$this->enterpriseId];
  194. $dbResult = $this->objDFinanceType->select($selectParams, '*', 'createTime desc');
  195. if($dbResult === false){
  196. return ResultWrapper::fail($this->objDFinanceType->error(), ErrorCode::$dberror);
  197. }
  198. return ResultWrapper::success($dbResult);
  199. }
  200. /**
  201. * 打入默认数据
  202. */
  203. public function createDefaultValue() {
  204. //查出当前企业有没有系统设定的财务类型,没有则打入数据
  205. $selectParams['isSystem'] = StatusCode::$standard;
  206. $dbResult = $this->objDFinanceType->select($selectParams);
  207. if($dbResult === false){
  208. return ResultWrapper::fail($this->objDFinanceType->error(), ErrorCode::$dberror);
  209. }
  210. if (empty($dbResult)) {
  211. $defaultFinanceType = $this->objDAdminSetting->select(['type' => StatusCode::$adminSettingType['financeType']]);
  212. if(empty($defaultFinanceType)) {
  213. return ResultWrapper::fail('系统未配置默认财务类型,请联系管理员', ErrorCode::$dberror);
  214. }
  215. foreach ($defaultFinanceType as $value) {
  216. $content = json_decode($value['content'], true);
  217. $v=[];
  218. $v['isSystem'] = StatusCode::$standard;
  219. $v['enterpriseId'] = 0;
  220. $v['createTime'] = time();
  221. $v['link'] =$content['link'];
  222. $v['name'] =$content['name'];
  223. $v['isDefault'] =$content['isDefault'];
  224. $v['createTime'] = time();
  225. $v['updateTime'] = time();
  226. $result = $this->objDFinanceType->insert($v);
  227. if($result === false) {
  228. return ResultWrapper::fail($this->objDFinanceType->error(), ErrorCode::$dberror);
  229. }
  230. unset($result);
  231. }
  232. }
  233. return ResultWrapper::success();
  234. }
  235. }