《ERP系统》客户信用及风控代码

1.风控核心代码:

<?php

namespace core\models;

class SalesCustomersFacade extends \common\models\Base{

    /**
     * 根据申请记录修改信用额度
     * @param $salesCustomersCreditModel
     * @return array
     */
    public static function updateCreditByApply($salesCustomersCreditModel){

        if(\core\models\SalesCustomersCredit::STATUS_FINISH == $salesCustomersCreditModel->sales_customers_credit_review_status){
            return ['status'=>false, 'code'=>"05010240", 'message'=>'信用额度已经更新,不能重复更新'];
        }
        $transaction = \Yii::$app->db->beginTransaction();
        try {
            //添加信用额度变更记录
            $sales_customers_credit_result = \core\models\SalesCustomersCreditRecord::insertByApply($salesCustomersCreditModel);
            if (false == $sales_customers_credit_result){
                throw new \yii\db\Exception('新增信用额度变更记录失败!', '05010243');
            }

            //更新信用额度快照
            $sales_customers_result = \core\models\SalesCustomers::updateSalesCustomersByApply($salesCustomersCreditModel);
            if (false == $sales_customers_result){
                throw new \yii\db\Exception('更新客户信用额度失败!', '05010244');
            }

            //更新信用额度申请记录状态为完成
            $sales_customers_credit_result = \core\models\SalesCustomersCredit::updateFinishStatusByApply($salesCustomersCreditModel);
            if (false == $sales_customers_credit_result){
                throw new \yii\db\Exception('更新客户信用额度申请记录状态失败!', '05010245');
            }

            $transaction->commit();
            return ['status'=>true, 'code'=>'0', 'message'=>'更新信用额度成功'];
        }catch (\yii\db\Exception $e){
            $transaction->rollBack();
            return ['status'=>false, 'code'=>$e->getCode(), 'message'=>$e->getMessage()];
        }
    }

    public static function updateCreditByOrder($salesOrderModel){
        //判断不是先货后款
        if (\core\models\SalesOrder::TYPE_WMS_BEFORE != $salesOrderModel->sales_order_settle_mode){
            return ['status'=>false, 'code'=>"05010250", 'message'=>'订单付款状态不是先货后款'];
        }

        //判断审批状态不是已完成
        $status = \core\models\CommonApproveStatus::getApprovalStatus($salesOrderModel->sales_order_code, \core\models\CommonDingtalkApproval::SALES_ORDER);
        if(!in_array($status, array(\core\models\DingtalkApproval::COMPLETE_APPROVAL))){
            return ['status'=>false, 'code'=>"05010251", 'message'=>'订单状态审批不是已完成'];
        }

        $transaction = \Yii::$app->db->beginTransaction();
        try {
            //添加信用额度变更记录
            $sales_customers_credit_result = \core\models\SalesCustomersCreditRecord::insertByOrder($salesOrderModel);
            if (false == $sales_customers_credit_result){
                throw new \yii\db\Exception('新增信用额度变更记录失败!', '05010252');
        }

            //更新信用额度快照
            $sales_customers_result = \core\models\SalesCustomers::updateSalesCustomersByOrder($salesOrderModel);
            if (false == $sales_customers_result){
                throw new \yii\db\Exception('更新客户信用额度失败!', '05010244');
            }

            //更新信用额度申请记录状态为完成
            $sales_order_result = \core\models\SalesOrder::updateFinishStatusByOrder($salesOrderModel);
            if (false == $sales_order_result){
                throw new \yii\db\Exception('更新订单状态失败!', '05010245');
            }

            $transaction->commit();
            return ['status'=>true, 'code'=>'0', 'message'=>'更新信用额度成功'];
        }catch (\yii\db\Exception $e){
            $transaction->rollBack();
            return ['status'=>false, 'code'=>$e->getCode(), 'message'=>$e->getMessage()];
        }
    }

    public static function updateCreditByRepay($financeInvoiceModel){
        $salesOrderModel = $financeInvoiceModel->salesOrder;
        //判断不是先货后款
        if (\core\models\SalesOrder::TYPE_WMS_BEFORE != $salesOrderModel->sales_order_settle_mode){
            return ['status'=>false, 'code'=>"05010250", 'message'=>'订单付款状态不是先货后款'];
        }

        //获取发票信息
        $financeInvoiceModel = \core\models\FinanceSalesInvoice::find()->where(['is_del'=>0, 'sales_order_code'=>$salesOrderModel->sales_order_code])->one();
        if (empty($financeInvoiceModel)){
            return ['status'=>false, 'code'=>"05010252", 'message'=>'发票信息不存在'];
        }

        if ($financeInvoiceModel->finance_sales_invoice_settle_status != 1){
            return ['status'=>false, 'code'=>"05010253", 'message'=>'订单未回款'];
        }

        $transaction = \Yii::$app->db->beginTransaction();
        try {
            //添加信用额度变更记录
            $sales_customers_credit_result = \core\models\SalesCustomersCreditRecord::insertByRepay($financeInvoiceModel);
            if (false == $sales_customers_credit_result){
                throw new \yii\db\Exception('新增信用额度变更记录失败!', '05010252');
            }

            //更新信用额度快照
            $sales_customers_result = \core\models\SalesCustomers::updateSalesCustomersByRepay($financeInvoiceModel);
            if (false == $sales_customers_result){
                throw new \yii\db\Exception('更新客户信用额度失败!', '05010244');
            }

            //更新信用额度申请记录状态为完成
            /*
            $sales_order_result = \core\models\FinanceSalesInvoice::updateFinishStatusByRepay($financeInvoiceModel);
            if (false == $sales_order_result){
                throw new \yii\db\Exception('更新订单状态失败!', '05010245');
            }
            */
            $transaction->commit();
            return ['status'=>true, 'code'=>'0', 'message'=>'更新信用额度成功'];
        }catch (\yii\db\Exception $e){
            $transaction->rollBack();
            return ['status'=>false, 'code'=>$e->getCode(), 'message'=>$e->getMessage()];
        }
    }

    /**
     * 根据信用额度判断能否签订合同
     * @param $sales_contract
     * @return bool
     */
    public static function canContractByCredit($salesContractModel){
        $salesCustomersModel = $salesContractModel->salesCustomers;
        //判断是否先款后货
        if (\core\models\SalesContract::TYPE_FINANCE_BEFORE == $salesContractModel->sales_contract_settle_mode){
            return ['status'=>true, 'errcode'=>'0', 'errmsg'=>'ok'];
        }
        //判断上一单是否已经回款
        $lastestSaleOrderModels = \core\models\SalesOrder::find()->where([
            'is_del'=>0,
            'sales_customers_id'=>$salesCustomersModel->id,
            'sales_order_settle_mode'=>\core\models\SalesOrder::TYPE_WMS_BEFORE])->all();
        if (!empty($lastestSaleOrderModels)){
            foreach ($lastestSaleOrderModels as $lastestSaleOrderModel){
                $status = \core\models\CommonApproveStatus::getApprovalStatus($lastestSaleOrderModel->sales_order_code, \core\models\CommonDingtalkApproval::SALES_ORDER);
                if (empty($lastestSaleOrderModel->send_out_at)){
                    $send_out_at = 0;
                }else{
                    $send_out_at = $lastestSaleOrderModel->send_out_at;
                }
                if($status == \core\models\DingtalkApproval::COMPLETE_APPROVAL
                    && (time() - $send_out_at > $salesContractModel->sales_contract_aging)
                    && empty($lastestSaleOrderModel->repay_status == 0)){
                    return ['status'=>false, 'errcode'=>'050204', 'errmsg'=>'上一单超账期未回款'];
                }
            }
        }

        //判断信用额度是否已经过期
        $salesCustomersModel = \core\models\SalesCustomers::findOne(['id'=>$salesContractModel->sales_customers_id]);
        if ($salesCustomersModel->sales_customers_credit_end_at < time()){
            return ['status'=>false, 'errcode'=>'050201', 'errmsg'=>'客户资质已经过期'];
        }
        //判断信用额度是否已经不足
        if ($salesCustomersModel->sales_customers_credit_surplus < \common\models\Base::moneyBcmul($salesContractModel->sales_contract_receivables)){
            return ['status'=>false, 'errcode'=>'050202', 'errmsg'=>'客户信用余额不足'];
        }
        return ['status'=>true, 'errcode'=>'0', 'errmsg'=>'ok'];
    }

    /**
     * 根据信用额度判断能否下订单
     * @param $sales_contract
     * @return bool
     */
    public static function canOrderByCredit($salesOrderModel){
        $salesCustomersModel = $salesOrderModel->salesCustomers;
        $salesContractModel = $salesOrderModel->salesContract;
        //判断是否先款后货
        if (\core\models\SalesOrder::TYPE_FINANCE_BEFORE == $salesOrderModel->sales_order_settle_mode){
            return ['status'=>true, 'errcode'=>'0', 'errmsg'=>'ok'];
        }
        //判断上一单是否已经回款
        $lastestSaleOrderModels = \core\models\SalesOrder::find()->where([
            'is_del'=>0,
            'sales_customers_id'=>$salesCustomersModel->id,
            'sales_order_settle_mode'=>\core\models\SalesOrder::TYPE_WMS_BEFORE])->andWhere(['!=', 'id', $salesOrderModel->id])->all();
        if (!empty($lastestSaleOrderModels)){
            foreach ($lastestSaleOrderModels as $lastestSaleOrderModel){
                $status = \core\models\CommonApproveStatus::getApprovalStatus($lastestSaleOrderModel->sales_order_code, \core\models\CommonDingtalkApproval::SALES_ORDER);
                if (empty($lastestSaleOrderModel->send_out_at)){
                    $send_out_at = 0;
                }else{
                    $send_out_at = $lastestSaleOrderModel->send_out_at;
                }
                if($status == \core\models\DingtalkApproval::COMPLETE_APPROVAL
                && (time() - $send_out_at > $salesContractModel->sales_contract_aging)
                && empty($lastestSaleOrderModel->repay_status == 0)){
                    return ['status'=>false, 'errcode'=>'050204', 'errmsg'=>'上一单超账期未回款'];
                }
            }
        }

        //判断信用额度是否已经过期
        $salesCustomersModel = \core\models\SalesCustomers::findOne(['id'=>$salesOrderModel->sales_customers_id]);
        if ($salesCustomersModel->sales_customers_credit_end_at < time()){
            return ['status'=>false, 'errcode'=>'050206', 'errmsg'=>'客户资质已经过期'];
        }
        //判断信用额度是否已经不足
        if ($salesCustomersModel->sales_customers_credit_surplus < $salesOrderModel->sales_order_total_amount){
            return ['status'=>false, 'errcode'=>'050207', 'errmsg'=>'客户信用余额不足'];
        }
        //判断订单额度是否超出合同额度
        if ($salesContractModel->sales_contract_amount < \common\models\Base::moneyBcdiv($salesOrderModel->sales_order_total_amount)){
            return ['status'=>false, 'errcode'=>'050208', 'errmsg'=>'订单总金额超出合同总金额'];
        }
        return ['status'=>true, 'errcode'=>'0', 'errmsg'=>'ok'];
    }

    public static function canCreditApplyByCredit($salesCustomersModel){
        $salesCustomersCreditModels = $salesCustomersModel->salesCustomersCredit;

        if (empty($salesCustomersCreditModels)){
            $return_arr = ['flag'=>true, 'message'=>'信用申请'];
            return $return_arr;
        }else{
            foreach ($salesCustomersCreditModels as $salesCustomersCreditModel){
                $approval = \core\models\CommonDingtalkApproval::SALES_CUSTOMER_CREDIT;
                $status = \core\models\CommonApproveStatus::getApprovalStatus($salesCustomersCreditModel->sales_customers_credit_code, $approval);
                $update_credit_status = $salesCustomersCreditModel->sales_customers_credit_review_status;

                if ($update_credit_status != \core\models\DingtalkApproval::COMPLETE_APPROVAL){
                    $return_arr = ['flag'=>false, 'message'=>''];
                    return $return_arr;
                }

                if ($update_credit_status != \core\models\SalesCustomersCredit::STATUS_FINISH){
                    $return_arr = ['flag'=>false, 'message'=>''];
                    return $return_arr;
                }
            }
            $return_arr = ['flag'=>true, 'message'=>'信用变更申请'];
            return $return_arr;
        }
    }
}

  

上一篇:Swift项目开发实战-基于分层架构的多版本iPhone计算器-直播公开课


下一篇:聚合数据 iOS 项目开发实战:条码查询器