Beispiel #1
0
 public function action_supplier_payouts($campaign_id)
 {
     if (Auth::has_access('reports.supplier')) {
         $campaign = Model_Data_Supplier_Campaign::find($campaign_id);
         $campaign_title = $campaign->title;
         $all_lists = array();
         foreach ($campaign->data_supplier_campaign_lists as $list) {
             $list_data[$list->database_server_id][] = $list->list_id;
             $all_lists[] = $list->list_id;
         }
         $payments = GAB\Debtsolv::get_all_paid_in_report($all_lists)->as_array();
         $payments_due = array();
         $client_array = array();
         foreach ($payments as $single_payment) {
             if (!isset($client_array[$single_payment['ClientID']])) {
                 $client_array[$single_payment['ClientID']]['DI'] = $single_payment['DI'];
                 $client_array[$single_payment['ClientID']]['TotalPayments'] = 0;
                 $client_array[$single_payment['ClientID']]['TotalIncome'] = 0;
             }
             $client_array[$single_payment['ClientID']]['TotalIncome'] = $client_array[$single_payment['ClientID']]['TotalIncome'] + $single_payment['Payment'];
             $client_array[$single_payment['ClientID']]['TotalPayments']++;
             $client_array[$single_payment['ClientID']]['PaymentLog'][] = $single_payment;
         }
         $firstPayments = array();
         $secondPayments = array();
         $date_list = array();
         // Check for DI Hits
         foreach ($client_array as $client) {
             $DI = $client['DI'];
             if ($client['TotalIncome'] >= $DI) {
                 $firstPaymentMonth = "";
                 $secondPaymentMonth = "";
                 $runningTotal = 0;
                 $calculationFirstHit = FALSE;
                 $calculationSecondHit = FALSE;
                 foreach ($client['PaymentLog'] as $payment) {
                     list($d, $m, $y) = explode('-', $payment['payDay']);
                     $m = (int) $d >= 21 ? (int) $m + 1 : $m;
                     $y = (int) $m >= 13 ? (int) $y + 1 : $y;
                     $m = (int) $m >= 13 ? 0 + ((int) $m - 12) : $m;
                     $thisDate = mktime(0, 0, 0, $m, $d, $y);
                     $runningTotal = $runningTotal + $payment['Payment'];
                     if ($runningTotal >= $DI && !$calculationFirstHit) {
                         $firstPaymentMonth = date("F Y", $thisDate);
                         $calculationFirstHit = TRUE;
                     } else {
                         if ($runningTotal >= $DI * 2 && !$calculationSecondHit) {
                             $secondPaymentMonth = date("F Y", $thisDate);
                             $calculationSecondHit = TRUE;
                         }
                     }
                 }
                 if ($calculationFirstHit) {
                     $payments_due[$firstPaymentMonth]['payments'] = isset($payments_due[$firstPaymentMonth]['payments']) ? $payments_due[$firstPaymentMonth]['payments'] + $DI * 0.75 : $DI * 0.75;
                     $payments_due[$firstPaymentMonth]['totalpayments'] = isset($payments_due[$firstPaymentMonth]['totalpayments']) ? $payments_due[$firstPaymentMonth]['totalpayments'] + 1 : 1;
                     $firstPayments[$firstPaymentMonth][] = array('ClientID' => $payment['ClientID'], 'Reference' => $payment['Reference'], 'ListName' => $payment['Lead Source'], 'Name' => $payment['Name'], 'DI' => $payment['DI']);
                     $date_list[] = $firstPaymentMonth;
                 }
                 if ($calculationSecondHit) {
                     $payments_due[$secondPaymentMonth]['totalpayments'] = isset($payments_due[$secondPaymentMonth]['totalpayments']) ? $payments_due[$secondPaymentMonth]['totalpayments'] + 1 : 1;
                     $payments_due[$secondPaymentMonth]['payments'] = isset($payments_due[$secondPaymentMonth]['payments']) ? $payments_due[$secondPaymentMonth]['payments'] + $DI * 0.5 : $DI * 0.5;
                     $secondPayments[$secondPaymentMonth][] = array('ClientID' => $payment['ClientID'], 'Reference' => $payment['Reference'], 'ListName' => $payment['Lead Source'], 'Name' => $payment['Name'], 'DI' => $payment['DI']);
                     $date_list[] = $secondPaymentMonth;
                 }
             }
         }
         $paymentReturn = array();
         $totalPayments = 0;
         foreach ($payments_due as $key => $PD) {
             $paymentReturn[] = array($key, $PD['totalpayments'], "£" . number_format($PD['payments'] / 100, 2), "NO");
             $totalPayments = $totalPayments + $PD['payments'];
         }
         $paymentDetailsMonthly = array('first' => $firstPayments, 'second' => $secondPayments);
         $date_ls = array();
         foreach (array_unique($date_list) as $date_l) {
             $date_ls[] = $date_l;
         }
         $this->response(array("aaData" => $paymentReturn, "allClientsMonths" => $date_ls, "allClientsMonthly" => $paymentDetailsMonthly, "totalPayments" => "£" . number_format($totalPayments / 100, 2), "aoColumns" => array(array("bSortable" => false), array("sTitle" => "Client Payments Received", "sType" => "string", "bSortable" => false), array("sTitle" => "Payouts Due", "sType" => "string", "bSortable" => false), array("sTitle" => "Supplier Paid", "sType" => "string", "bSortable" => false))));
     } else {
         $this->response(array("error" => "You are not authorised to view this content!"));
     }
 }