public static function export_feed($entry, $form, $settings)
 {
     $name_fields = array();
     foreach ($form["fields"] as $field) {
         if (RGFormsModel::get_input_type($field) == "name") {
             $name_fields[] = $field;
         }
     }
     //Creating client
     self::log_debug("Checking to see if client exists or a new client needs to be created.");
     $client = self::get_client($form, $entry, $settings, $name_fields);
     //if client could not be created, ignore invoice and estimate
     if (!$client) {
         self::log_debug("Unable to create client, not creating invoice/estimate.");
         return;
     }
     if ($settings["meta"]["alsocreate"] == "invoice") {
         $invoice_estimate = new FreshBooks_Invoice();
     } else {
         if ($settings["meta"]["alsocreate"] == "estimate") {
             $invoice_estimate = new FreshBooks_Estimate();
         } else {
             return;
         }
     }
     //don't create invoice or estimate
     if (!empty($settings["meta"]["ponumber"])) {
         $invoice_estimate->poNumber = esc_html(self::get_entry_value($settings["meta"]["ponumber"], $entry, $name_fields));
     }
     $invoice_estimate->discount = $settings["meta"]["discount"];
     $invoice_estimate->notes = esc_html($settings["meta"]["notes2"]);
     $invoice_estimate->terms = esc_html($settings["meta"]["terms"]);
     $total = 0;
     $lines = array();
     if ($settings["meta"]["is_fixed_cost"] == "2") {
         //creating line items based on pricing fields
         $products = GFCommon::get_product_fields($form, $entry, true, false);
         foreach ($products["products"] as $product) {
             $product_name = $product["name"];
             $price = GFCommon::to_number($product["price"]);
             if (!empty($product["options"])) {
                 $product_name .= " (";
                 $options = array();
                 foreach ($product["options"] as $option) {
                     $price += GFCommon::to_number($option["price"]);
                     $options[] = $option["option_name"];
                 }
                 $product_name .= implode(", ", $options) . ")";
             }
             $subtotal = floatval($product["quantity"]) * $price;
             $total += $subtotal;
             $lines[] = array("name" => esc_html($product["name"]), "description" => esc_html($product_name), "unitCost" => $price, "quantity" => $product["quantity"], "amount" => $subtotal);
         }
         //adding shipping if form has shipping
         if (!empty($products["shipping"]["name"])) {
             $total += floatval($products["shipping"]["price"]);
             $lines[] = array("name" => esc_html($products["shipping"]["name"]), "description" => esc_html($products["shipping"]["name"]), "unitCost" => $products["shipping"]["price"], "quantity" => 1, "amount" => $products["shipping"]["price"]);
         }
     } else {
         //creating line items based on fixed cost or mapped fields
         foreach ($settings["meta"]["items"] as $item) {
             $cost = $settings["meta"]["is_fixed_cost"] ? $item["cost"] : self::get_entry_value($item["cost"], $entry, $name_fields);
             $cost = self::get_number($cost);
             $quantity = $settings["meta"]["is_fixed_cost"] ? $item["quantity"] : self::get_entry_value($item["quantity"], $entry, $name_fields);
             $amount = $quantity * $cost;
             $total += $amount;
             $lines[] = array("name" => $item["item_id"], "description" => esc_html($item["description"]), "unitCost" => $cost, "quantity" => $quantity, "amount" => $amount);
         }
     }
     $invoice_estimate->amount = $total;
     $invoice_estimate->clientId = $client->clientId;
     $invoice_estimate->firstName = $client->firstName;
     $invoice_estimate->lastName = $client->lastName;
     $invoice_estimate->lines = $lines;
     $invoice_estimate->organization = $client->organization;
     $invoice_estimate->pStreet1 = $client->pStreet1;
     $invoice_estimate->pStreet2 = $client->pStreet2;
     $invoice_estimate->pCity = $client->pCity;
     $invoice_estimate->pState = $client->pState;
     $invoice_estimate->pCode = $client->pCode;
     $invoice_estimate->pCountry = $client->pCountry;
     self::log_debug("Creating invoice/estimate.");
     $invoice_estimate->create();
     $lastError = $invoice_estimate->lastError;
     if (empty($lastError)) {
         self::log_debug("Invoice/estimate created.");
     } else {
         self::log_error("The following error occurred when trying to create an invoice/estimate: {$lastError}");
     }
 }
示例#2
0
 /**
  * process XML string response from LIST server method
  */
 protected function _internalListing($responseStatus, &$XMLObject, &$rows, &$resultInfo)
 {
     $rows = array();
     $resultInfo = array();
     $estimates = $XMLObject->estimates;
     $resultInfo['page'] = (string) $estimates['page'];
     $resultInfo['perPage'] = (string) $estimates['per_page'];
     $resultInfo['pages'] = (string) $estimates['pages'];
     $resultInfo['total'] = (string) $estimates['total'];
     foreach ($estimates->children() as $key => $currXML) {
         $thisEstimates = new FreshBooks_Estimate();
         $thisEstimates->_internalLoadXML($currXML);
         $rows[] = $thisEstimates;
     }
 }
 public function handle_note_and_send_by_email($sendByEmail, $type, $id, $entry)
 {
     if ($type == 'invoice') {
         $invoice_estimate = new FreshBooks_Invoice();
         $invoice_estimate->invoiceId = $id;
     } elseif ($type == 'estimate') {
         $invoice_estimate = new FreshBooks_Estimate();
         $invoice_estimate->estimateId = $id;
     } else {
         // abort, no need to sendByEmail or add note.
         return;
     }
     // see if invoice/estimate should automatically be emailed.
     if ($sendByEmail) {
         $this->log_debug(__METHOD__ . '(): Sending invoice/estimate automatically by email.');
         $sentByEmail = $invoice_estimate->sendByEmail();
         if ($sentByEmail) {
             $this->log_debug(__METHOD__ . '(): The invoice/estimate was successfully scheduled to be automatically sent by FreshBooks.');
         } else {
             $this->log_error(__METHOD__ . '(): Unable to schedule invoice/estimate to be automatically sent.');
         }
     }
     // add note to entry.
     $invoice_estimate->get($id);
     $amount_formatted = GFCommon::to_money($invoice_estimate->amount, $entry['currency']);
     $note = sprintf(__('%s #%s has been successfully created. Amount: %s. Status: %s.', 'gravityformsfreshbooks'), ucfirst($type), $invoice_estimate->number, $amount_formatted, ucfirst($invoice_estimate->status));
     GFFormsModel::add_note($entry['id'], 0, $this->_short_title, $note, 'success');
 }