app/Utils/TransactionUtil.php
/**
     * Checks if credit limit of a customer is exceeded
     *
     * @param  array $input
     * @param  int $exclude_transaction_id (For update sell)
     *
     * @return mixed
     * if exceeded returns credit_limit else false
     */
Checks if credit limit of a customer is exceeded
@param  array $input
@param  int $exclude_transaction_id (For update sell)

@return mixed
if exceeded returns credit_limit else false
Checks if credit limit of a customer is exceeded @param array $input @param int $exclude_transaction_id ( For update sell ) @return mixed if exceeded returns credit_limit else false
public function isCustomerCreditLimitExeeded(
        $input,
        $exclude_transaction_id = null
    ) {
        $final_total = $this->num_uf($input['final_total']);
        $curr_total_payment = 0;
        if (!empty($input['payment'])) {
            foreach ($input['payment'] as $payment) {
                $curr_total_payment += $this->num_uf($payment['amount']);
            }
        }
        if ($final_total <= $curr_total_payment) {
            return false;
        }
        $credit_limit = Contact::find($input['contact_id'])->credit_limit;
        if ($credit_limit == null) {
            return false;
        }
        $query = Contact::where('contacts.id', $input['contact_id'])
                ->join('transactions AS t', 'contacts.id', '=', 't.contact_id');
        if (!empty($exclude_transaction_id)) {
            $query->where('t.id', '!=', $exclude_transaction_id);
        }
        $credit_details =  $query->select(
            DB::raw("SUM(IF(t.type = 'sell', final_total, 0)) as total_invoice"),
            DB::raw("SUM(IF(t.type = 'sell', (SELECT SUM(IF(is_return = 1,-1*amount,amount)) FROM transaction_payments WHERE transaction_payments.transaction_id=t.id), 0)) as invoice_paid")
        )->first();
        $total_invoice = !empty($credit_details->total_invoice) ? $credit_details->total_invoice : 0;
        $invoice_paid = !empty($credit_details->invoice_paid) ? $credit_details->invoice_paid : 0;
        $curr_due = $final_total - $curr_total_payment;
        $total_due = $total_invoice - $invoice_paid + $curr_due;
        if ($total_due <= $credit_limit) {
            return false;
        }
        return $credit_limit;
    }
public function isCustomerCreditLimitExeeded ( $ input , $ exclude_transaction_id = null ) { $ final_total = $ this -> num_uf ( $ input [ 'final_total' ] ) ; $ curr_total_payment = 0 ; if ( ! empty ( $ input [ 'payment' ] ) ) { foreach ( $ input [ 'payment' ] as $ payment ) { $ curr_total_payment += $ this -> num_uf ( $ payment [ 'amount' ] ) ; } } if ( $ final_total <= $ curr_total_payment ) { return false ; } $ credit_limit = Contact :: find ( $ input [ 'contact_id' ] ) -> credit_limit ; if ( $ credit_limit == null ) { return false ; } $ query = Contact :: where ( 'contacts.id' , $ input [ 'contact_id' ] ) -> join ( 'transactions AS t' , 'contacts.id' , '=' , 't.contact_id' ) ; if ( ! empty ( $ exclude_transaction_id ) ) { $ query -> where ( 't.id' , '!=' , $ exclude_transaction_id ) ; } $ credit_details = $ query -> select ( DB :: raw ( " SUM(IF(t.type = 'sell', final_total, 0)) as total_invoice " ) , DB :: raw ( " SUM(IF(t.type = 'sell', (SELECT SUM(IF(is_return = 1,-1*amount,amount)) FROM transaction_payments WHERE transaction_payments.transaction_id=t.id), 0)) as invoice_paid " ) ) -> first ( ) ; $ total_invoice = ! empty ( $ credit_details -> total_invoice ) ? $ credit_details -> total_invoice : 0 ; $ invoice_paid = ! empty ( $ credit_details -> invoice_paid ) ? $ credit_details -> invoice_paid : 0 ; $ curr_due = $ final_total - $ curr_total_payment ; $ total_due = $ total_invoice - $ invoice_paid + $ curr_due ; if ( $ total_due <= $ credit_limit ) { return false ; } return $ credit_limit ; }
Checks if credit limit of a customer is exceeded
 @param  array $input
 @param  int $exclude_transaction_id (For update sell)
Checks if credit limit of a customer is exceeded @param array $input @param int $exclude_transaction_id ( For update sell )
//If not credit sell ignore credit limit check //Exclude transaction id if update transaction