2025-02-05 11:15:40 +08:00
< ? php
namespace app\admin\logic ;
use app\common\logic\BaseLogic ;
use app\common\model\finance\AccountsReceivable ;
2025-02-07 17:54:51 +08:00
use app\common\model\finance\AccountsReceivableInfo ;
2025-02-15 17:10:50 +08:00
use app\common\model\system_store\SystemStore ;
2025-02-05 11:15:40 +08:00
use support\exception\BusinessException ;
use think\facade\Db ;
/**
* Class AccountsReceivableLogic
* @ package app\admin\logic
*/
class AccountsReceivableLogic extends BaseLogic
{
/**
* @ notes 添加
* @ param array $order
*/
public static function add ( array $order )
{
$model = new AccountsReceivable ();
$model -> order_id = $order [ 'id' ];
$model -> store_id = $order [ 'store_id' ];
$model -> user_id = $order [ 'uid' ];
2025-02-06 17:30:39 +08:00
$model -> nickname = $order [ 'other_data' ] -> nickname ;
$model -> phone = $order [ 'other_data' ] -> phone ;
2025-02-05 11:15:40 +08:00
$model -> deadline = time () + 86400 * 15 ;
$model -> total_debt = $order [ 'total_price' ];
$model -> surplus_debt = $order [ 'total_price' ];
$model -> save ();
}
2025-02-07 17:54:51 +08:00
public static function edit ( $params )
{
Db :: startTrans ();
try {
$accountsReceivable = AccountsReceivable :: where ([ 'id' => $params [ 'accounts_receivable_id' ]]) -> find ();
if ( $accountsReceivable -> surplus_debt <= 0 ) {
throw new BusinessException ( '该账单已支付' );
}
$surplusDebt = bcsub ( $accountsReceivable -> surplus_debt , $params [ 'pay_debt' ], 2 );
$model = new AccountsReceivableInfo ();
$model -> accounts_receivable_id = $params [ 'accounts_receivable_id' ];
$model -> pay_type = $params [ 'pay_type' ];
$model -> total_debt = $params [ 'surplus_debt' ];
$model -> pay_debt = $params [ 'pay_debt' ];
$model -> recipient = $params [ 'recipient' ];
$model -> surplus_debt = $surplusDebt ;
$model -> file = $params [ 'file' ];
$model -> mark = $params [ 'mark' ];
if ( ! $model -> save ()) {
throw new BusinessException ( '添加失败' );
}
$accountsReceivable -> pay_debt = bcadd ( $accountsReceivable -> pay_debt , $params [ 'pay_debt' ], 2 );
$accountsReceivable -> surplus_debt = $surplusDebt ;
if ( ! $accountsReceivable -> save ()) {
throw new BusinessException ( '更新账单出错' );
}
Db :: commit ();
return true ;
} catch ( \Exception $e ) {
Db :: rollback ();
throw new BusinessException ( $e -> getMessage ());
}
}
public static function record ( $params )
{
}
2025-02-15 17:10:50 +08:00
public static function statistics ()
{
$query = AccountsReceivable :: field ( 'store_id,sum(total_debt) as total_debt,sum(pay_debt) as pay_debt,sum(surplus_debt) as surplus_debt' ) -> group ( 'store_id' );
$count = $query -> count ();
$list = $query -> select () -> toArray ();
foreach ( $list as & $item ) {
$item [ 'store_name' ] = SystemStore :: where ( 'id' , $item [ 'store_id' ]) -> value ( 'name' );
}
return [
'list' => $list ,
'count' => $count
];
}
2025-02-05 11:15:40 +08:00
}