Exemplo n.º 1
0
 public static function createPayment($payment)
 {
     $client = $payment->client;
     $client->balance = $client->balance - $payment->amount;
     $client->paid_to_date = $client->paid_to_date + $payment->amount;
     $client->save();
     if ($payment->contact_id) {
         $activity = Activity::getBlank($client);
         $activity->contact_id = $payment->contact_id;
         $activity->message = Utils::encodeActivity($payment->invitation->contact, 'entered ' . $payment->getName() . ' for ', $payment->invoice);
     } else {
         $activity = Activity::getBlank($client);
         $message = $payment->payment_type_id == PAYMENT_TYPE_CREDIT ? 'applied credit for ' : 'entered ' . $payment->getName() . ' for ';
         $activity->message = Utils::encodeActivity(Auth::user(), $message, $payment->invoice);
     }
     $activity->payment_id = $payment->id;
     if ($payment->invoice_id) {
         $activity->invoice_id = $payment->invoice_id;
         $invoice = $payment->invoice;
         $invoice->balance = $invoice->balance - $payment->amount;
         $invoice->invoice_status_id = $invoice->balance > 0 ? INVOICE_STATUS_PARTIAL : INVOICE_STATUS_PAID;
         if ($invoice->partial > 0) {
             $invoice->partial = max(0, $invoice->partial - $payment->amount);
         }
         $invoice->save();
     }
     $activity->payment_id = $payment->id;
     $activity->client_id = $payment->client_id;
     $activity->activity_type_id = ACTIVITY_TYPE_CREATE_PAYMENT;
     $activity->balance = $client->balance;
     $activity->adjustment = $payment->amount * -1;
     $activity->save();
     Activity::checkSubscriptions(EVENT_CREATE_PAYMENT, $payment);
 }