MSupplierBalance.Class.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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\Model\MBaseModel;
  14. use JinDouYun\Dao\Finance\DSupplierBalance;
  15. use JinDouYun\Dao\Finance\DSupplierBalanceIndex;
  16. use JinDouYun\Dao\Purchase\DSupplier;
  17. use JinDouYun\Model\Purchase\MSupplier;
  18. use JinDouYun\Cache\OverviewCache;
  19. class MSupplierBalance extends MBaseModel
  20. {
  21. private $objDSupplierBalance;
  22. private $objDSupplierBalanceIndex;
  23. private $objDSupplier;
  24. private $objMSupplier;
  25. private $objOverviewCache;
  26. private $enterpriseId;
  27. private $userCenterId;
  28. private $cutTable = 200000;
  29. public function __construct($enterpriseId, $userCenterId)
  30. {
  31. $this->userCenterId = $userCenterId;
  32. $this->enterpriseId = $enterpriseId;
  33. parent::__construct($enterpriseId, $userCenterId);
  34. $this->objDSupplierBalance = new DSupplierBalance('finance');
  35. $this->objDSupplierBalanceIndex = new DSupplierBalanceIndex('finance');
  36. $this->objDSupplier = new DSupplier('stock');
  37. $this->objMSupplier = new MSupplier($userCenterId, $enterpriseId);
  38. $this->objOverviewCache = new OverviewCache();
  39. $this->objDSupplierBalanceIndex->setTable('qianniao_supplier_balance_index_' . $enterpriseId);
  40. $this->objDSupplier->setTable('qianniao_supplier_' . $enterpriseId);
  41. }
  42. /**
  43. * 添加供应商余额
  44. * @param $supplierId
  45. * @param $changedMoney
  46. * @return ResultWrapper
  47. * @throws \Exception
  48. */
  49. public function addSupplierBalance($supplierId, $changedMoney)
  50. {
  51. $tableName = $this->objDSupplierBalance->getTableName('qianniao_supplier_balance_' . $this->enterpriseId, $supplierId, $this->cutTable);
  52. $this->objDSupplierBalance->setTable($tableName);
  53. //获取供应商目前的余额
  54. $money = $this->objDSupplier->get_field('money', $supplierId);
  55. //新增一条供应商余额信息
  56. $balanceData = [
  57. 'supplierId' => $supplierId,
  58. 'openingBalance' => $money,
  59. 'interimBalance' => $changedMoney,
  60. 'endingBalance' => bcadd($money, $changedMoney, 4),
  61. 'createTime' => time(),
  62. 'updateTime' => time()
  63. ];
  64. $detailId = $this->objDSupplierBalance->insert($balanceData);
  65. if ($detailId === false) {
  66. return ResultWrapper::fail($this->objDSupplierBalance->error(), ErrorCode::$dberror);
  67. }
  68. //新增一条供应商余额索引数据
  69. $indexData = [
  70. 'supplierId' => $supplierId,
  71. 'detailId' => $detailId,
  72. 'createTime' => time(),
  73. 'updateTime' => time()
  74. ];
  75. $indexId = $this->objDSupplierBalanceIndex->insert($indexData);
  76. if ($indexId == false) {
  77. return ResultWrapper::fail($this->objDSupplierBalanceIndex->error(), ErrorCode::$dberror);
  78. }
  79. //更新供应商的最新余额
  80. $result = self::updateSupplierBalance($supplierId, $changedMoney);
  81. if ($result->isSuccess() === false) {
  82. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  83. }
  84. return ResultWrapper::success($result->getData());
  85. }
  86. /**
  87. * 获取所有供应商余额数据
  88. *
  89. * @param array $selectParams 过滤条件
  90. *
  91. * @return ResultWrapper
  92. */
  93. public function getAllSupplierBalance($selectParams)
  94. {
  95. $limit = $selectParams['limit'];
  96. unset($selectParams['limit']);
  97. $offset = $selectParams['offset'];
  98. unset($selectParams['offset']);
  99. $supplierId = $selectParams['supplierId'];
  100. unset($selectParams['supplierId']);
  101. $start = $selectParams['start'];
  102. unset($selectParams['start']);
  103. $end = $selectParams['end'];
  104. unset($selectParams['end']);
  105. $isExport = isset($selectParams['isExport']) ? $selectParams['isExport'] : false;
  106. //默认进来不筛选,查出供应商当前的余额
  107. if (!$start && !$end) {
  108. $where = ['limit' => $limit, 'offset' => $offset];
  109. if ($supplierId) {
  110. $where['id'] = $supplierId;
  111. }
  112. $result = $this->objMSupplier->getSupplierMoney($where, $isExport);
  113. if ($result->isSuccess() == false) {
  114. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  115. }
  116. return ResultWrapper::success($result->getData());
  117. }
  118. if($isExport){
  119. ResultWrapper::fail('有筛选条件不支持导出', ErrorCode::$paramError);
  120. }
  121. //期初余额
  122. $startResult = self::getShouldPayMoneyByTime($start, $supplierId);
  123. //期末余额
  124. $endResult = self::getShouldPayMoneyByTime($end, $supplierId);
  125. $supplierName = $this->objDSupplier->get_field('title', $supplierId);
  126. $return = [
  127. 'data' => [
  128. [
  129. 'supplierId' => $supplierId,
  130. 'title' => $supplierName,
  131. 'openingBalance' => $startResult,
  132. 'interimBalance' => $endResult - $startResult,
  133. 'endingBalance' => $endResult
  134. ]
  135. ],
  136. 'total' => 1,
  137. ];
  138. return ResultWrapper::success($return);
  139. }
  140. /**
  141. * 获取某一日期的余额
  142. * @param $time
  143. * @param $supplierId
  144. * @return int
  145. * @throws \Exception
  146. */
  147. public function getShouldPayMoneyByTime($time, $supplierId)
  148. {
  149. $tableName = $this->objDSupplierBalanceIndex->get_Table();
  150. $sql = "select * from $tableName where createTime <= $time AND supplierId = $supplierId order by id desc limit 1";
  151. $result = $this->objDSupplierBalanceIndex->query($sql);
  152. if (empty($result)) {
  153. //没有发生过应付
  154. return 0;
  155. }
  156. $data = array_shift($result);
  157. $tableName = $this->objDSupplierBalance->getTableName('qianniao_supplier_balance_' . $this->enterpriseId, $supplierId, $this->cutTable);
  158. $this->objDSupplierBalance->setTable($tableName);
  159. return $this->objDSupplierBalance->get_field('endingBalance', $data['detailId']);
  160. }
  161. /**
  162. * 修改供应商余额
  163. * @param $supplierId
  164. * @param $changedMoney
  165. * @return ResultWrapper
  166. */
  167. public function updateSupplierBalance($supplierId, $changedMoney)
  168. {
  169. $dbResult = $this->objDSupplier->set_inc('money', $supplierId, $changedMoney);
  170. if ($dbResult === false) {
  171. return ResultWrapper::fail($this->objDSupplier->error(), ErrorCode::$dberror);
  172. }
  173. //修改应付款总金额
  174. $this->objOverviewCache->saveAggregateStatistics($this->enterpriseId, 'totalShouldPay', $changedMoney);
  175. return ResultWrapper::success($dbResult);
  176. }
  177. /**
  178. * 修改供应商总收款金额
  179. * @param $supplierId
  180. * @param $money
  181. * @return ResultWrapper
  182. */
  183. public function updateSupplierTotalReceiveMoney($supplierId, $money)
  184. {
  185. $dbResult = $this->objDSupplier->set_inc('totalReceiveMoney', $supplierId, $money);
  186. if ($dbResult === false) {
  187. return ResultWrapper::fail($this->objDSupplier->error(), ErrorCode::$dberror);
  188. }
  189. return ResultWrapper::success($dbResult);
  190. }
  191. /**
  192. * 获取供应商余额
  193. * @param $supplierId
  194. * @param string $field
  195. * @return int|mixed
  196. */
  197. public function getSupplierBalance($supplierId, $field = 'money')
  198. {
  199. $money = $this->objDSupplier->get_field($field, $supplierId);
  200. return $money ? $money : 0;
  201. }
  202. public function getSupplier($id)
  203. {
  204. $dbResult = $this->objDSupplier->get($id);
  205. if ($dbResult === false) {
  206. return ResultWrapper::fail($this->objDSupplier->error(), ErrorCode::$dberror);
  207. }
  208. if (empty($dbResult)){
  209. return ResultWrapper::fail('未获取到供应商',ErrorCode::$paramError);
  210. }
  211. return ResultWrapper::success($dbResult);
  212. }
  213. /**
  214. * Doc: (des="")
  215. * User: XMing
  216. * Date: 2020/12/18
  217. * Time: 5:56 下午
  218. * @param $update
  219. * @param $id
  220. * @return ResultWrapper
  221. */
  222. public function updateSupplier($update,$id): ResultWrapper
  223. {
  224. $dbResult = $this->objDSupplier->update($update,$id);
  225. if ($dbResult === false) {
  226. return ResultWrapper::fail($this->objDSupplier->error(), ErrorCode::$dberror);
  227. }
  228. return ResultWrapper::success($dbResult);
  229. }
  230. }