示例#1
0
 /**
  * Create a new PPI Invoice
  * 
  * @author David Stansfield
  * @public
  * @param int $clientID, int $claimID
  */
 public function action_create_invoice()
 {
     // -- Check that one hasn't been created for this product
     // ------------------------------------------------------
     // -- Set the POST variables
     // -------------------------
     $clientID = \Input::post('Invoice_ClientID');
     $productTypeID = \Input::post('Invoice_Product_Type_ID');
     $clientProductID = \Input::post('Invoice_Client_Product_ID');
     $referenceID = \Input::post('Invoice_ReferenceID');
     $refundMethodID = \Input::post('Refund_Method');
     $correspondence = \Crm\Ppi\Ppi_correspondence::forge((int) $clientID);
     if ($refundMethodID > 0) {
         $refundMethod = \Crm\Ppi\Ppi_class::getRefundMethod($refundMethodID);
     }
     $description = \Input::post('Invoice_Description');
     $qty = \Input::post('Invoice_Qty');
     $charge = \Input::post('Invoice_Charge');
     $fee = \Input::post('Invoice_Fee');
     $vatPc = \Input::post('VAT_Percentage');
     $vat = 0;
     $total = 0;
     $feeTotal = $charge / 100 * $fee;
     $total = $feeTotal * $qty;
     $vat = $total / 100 * $vatPc;
     $total = $total + $vat;
     // -- Create the Invoice
     // ---------------------
     $details = array();
     $details = array('description' => $description . (isset($refundMethod) ? ' | Chosen refund method is ' . $refundMethod : false), 'qty' => $qty, 'charge' => $charge, 'vat_pc' => $vatPc, 'vat_amount' => $vat, 'fee' => $fee, 'total' => $total);
     $invoiceID = \Crm\Invoice\Invoice_class::createInvoice($clientID, $productTypeID, $clientProductID, $referenceID, $details);
     if ($invoiceID > 0) {
         // -- Invoice has been created in the system, now create a Refund Method entry
         // ---------------------------------------------------------------------------
         $refundData = array('claim_id' => $referenceID, 'invoice_id' => $invoiceID, 'method_id' => 0, 'refund_amount' => $charge);
         if (\Crm\Ppi\Ppi_class::createRefundMethod($refundData)) {
             // -- Print the Invoice
             // --------------------
             $extraData = array('date' => date("jS F Y"), 'invoiceDescription' => $description, 'invoiceQuantity' => $qty, 'invoiceCharge' => $charge, 'vatPc' => $vatPc, 'vatAmount' => $vat, 'totalGross' => $feeTotal, 'invoiceCostTotal' => $total, 'claimID' => $referenceID);
             $fileName = 'invoice_' . $clientID . '_' . $invoiceID . '.pdf';
             \Crm\Letter\Letter::forge($clientID, null, $extraData)->writeContent(27)->setOutputFilename($fileName)->printLetter(2)->create();
             // -- Save Correspondence
             // ----------------------
             $correspondence = \Crm\Ppi\Ppi_correspondence::forge((int) $clientID);
             $correspondence->saveNew($referenceID, 10, 28, 0, 'Invoice #' . $invoiceID . ' Created and printed <a href="/uploads/crm/letter/' . $fileName . '" target="_blank">View Letter</a>');
             // -- Save the invoice ID to the claim
             // -----------------------------------
             $claim = \Crm\Ppi\Ppi_claims::forge($referenceID);
             $claim->setInvoiceID($invoiceID);
             $message = $invoiceID;
             $status = 'done';
         } else {
             $message = 'Unable to save Refund Method';
             $status = 'error';
         }
     } else {
         $message = 'Unable to create a new invoice';
         $status = 'error';
     }
     $this->response(array('status' => $status, 'message' => $message));
 }