Add payment after monthly schedule expired

时间:2016-10-19 13:35:49

标签: php mysql laravel phpmyadmin laravel-5.2

I have two models, Payment and GroupStarts.

Payment has these fields:

student_id, amount_to_pay, student_paid, date_paid, due_date, group_name

On GroupStarts are:

group_name, group_start, group_end, group_status.

This is how it should work:

If payment is made for one month and it expires after month 1, it should add amount_to_pay to that for month 1 and if in month 2 student didn't paid nothing than we add + amount to pay, similar to: if student have to pay $10 and paid so owe is 0 but for next month he didn't paid so we add it to payment $10 if on month 3 he didn't paid we add $10 more to that amount.

How can I do that ?

This is code I tried:

private function getMonthlyUnpayments()
{

  $payments     = Payment::all();
  $unpayment    = 0;
  $totalPayment = 0;

        foreach ($payments as $key => $value) {
            $unpayment    += $value->amount_to_pay;
            $totalPayment += $value->student_paid;          
        }    

        $studentsAllU     = Student::where('is_canceled', '=', 0)->get();
        $groupAllU        = Gstart::where('group_status', '=', 0)->get();
        $studentGroupAllU = [];
        foreach ($studentsAllU as $key => $value) {

            foreach ($groupAllU as $keys => $valu) {
              foreach ($value->payment as $paymentID) {
                 if ($value->group_id == $valu->group_name) {
                         $studentGroupAllU[] = ['name' => $value->full_name,  'student_id' => $value->id, 'gr_id' => $value->group_id, 'paid' => $paymentID->student_paid, 'to_pay' =>  $paymentID->amount_to_pay, 'due_date' => $paymentID->due_date, 'group_ends' => $valu->group_end];
                      }  
              }                       
                }            
        }
function nb_mois($from, $to)
{
     $fromYear = date("Y", strtotime($from));
        $fromMonth = date("m", strtotime($from));
        $toYear = date("Y", strtotime($to));
        $toMonth = date("m", strtotime($to));
        if ($fromYear == $toYear) {
            return ($toMonth-$fromMonth)+1;
        } else {
            return (12-$fromMonth)+1+$toMonth;
        }
}

function createDateRangeArray($strDateFrom,$strDateTo)
{
    $aryRange=array();

    $iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2),     substr($strDateFrom,8,2),substr($strDateFrom,0,4));
    $iDateTo=mktime(1,0,0,substr($strDateTo,5,2),     substr($strDateTo,8,2),substr($strDateTo,0,4));

    if ($iDateTo>=$iDateFrom)
    {
        array_push($aryRange,date('Y-m-d',$iDateFrom));
        while ($iDateFrom<$iDateTo)
        {
            $iDateFrom+=86400; 
            array_push($aryRange,date('Y-m-d',$iDateFrom));
        }
    }
    return $aryRange;
}
        $dtArr = [];
        $incrementer = 0;
            $inc = 0;

        foreach ($studentGroupAllU as $key => $value) {

                if ($value['group_ends'] >= $value['due_date']  || $value['paid'] < $value['to_pay']) {
                  if ($value['paid'] < $value['to_pay']) {
                    $incrementer += ($value['to_pay'] - $value['paid']);
                  }
                  else {
                     $incrementer += ($value['to_pay']);
                  }

                      $dtArr[] = ['days_left' => count(createDateRangeArray($value['due_date'], $value['group_ends'])), 'group_id_d' => $value['gr_id'], 'paid' => $value['paid'], 'to_pay' => $value['to_pay'], 'student_id' => $value['student_id'], 'owe' => ($inc +=($value['to_pay'] - $value['paid'])), 'dates' =>  createDateRangeArray($value['due_date'], $value['group_ends']),'dates_fron_last_payment' =>  createDateRangeArray($value['due_date'], Carbon::now()->format('Y-m-d')), 'days_fron_last_payments' => count(createDateRangeArray($value['due_date'], Carbon::now()->format('Y-m-d')))];

                }
                    $inc = 0;
        }

function uniqueAssocArray($array, $uniqueKey) {
      $arr = array();

      foreach($array as $key => $item)
      {
         $arr[$item[$uniqueKey]][$key] = $item;

      }

      ksort($arr, SORT_NUMERIC);
      return $arr;
}
$dd = uniqueAssocArray($dtArr, 'group_id_d');
$tpStudentGr = 0;
        // dd($dtArr);
      foreach ($dtArr as $key => $value) {
           foreach ($value['dates'] as $k => $val) {

            if ($val == Carbon::createFromFormat('Y-m-d', $val)->startOfMonth()->format('Y-m-d') && $val <= Carbon::now()->startOfMonth()->format('Y-m-d')) {

         if ($value['paid'] <  $value['to_pay']){

                      $tpStudentGr += ($value['to_pay'] - $value['paid'] + $value['to_pay']);                  

                }
            if ($value['paid'] ==  $value['to_pay']){
                          $tpStudentGr += $value['to_pay'];
                }    
            }    
          }
          if ($value['days_left'] > 0) {

          }        
      }     
           $tpStudentGr = $unpayment - $totalPayment;
           return $tpStudentGr;    
}

0 个答案:

没有答案
相关问题