Skip to content

Instantly share code, notes, and snippets.

@karneaud
Last active June 14, 2017 22:36
Show Gist options
  • Save karneaud/4eb8f0ba235395bf9ac2b1afb70dec78 to your computer and use it in GitHub Desktop.
Save karneaud/4eb8f0ba235395bf9ac2b1afb70dec78 to your computer and use it in GitHub Desktop.
Export/ Import Quote data from Plane to Ninja
<?php
namespace App\Plane2Ninja\Transformers;
use App\Models\Client;
use App\Plane2Ninja\Ninja\NinjaFactory;
class ClientTransformer extends BaseTransformer
{
public function transform(Client $client)
{
$ninjaFactory = new NinjaFactory(new ClientTransformer(), new InvoiceTransformer(), new PaymentTransformer(), new InvoiceItemTransformer(), new ProductTransformer(), new QuoteTransformer());
return [
'id' => $client->client_id,
'name' => $client->client_ame,
'balance' => 0,
'paid_to_date' => 0,
'address1' => $client->client_address_1,
'address2' => $client->client_address_2,
'city' => $client->client_city,
'state' => $client->client_state,
'postal_code' => $client->client_zip,
'country_id' => '',
'work_phone' => $this->getString($client->client_phone),
'private_notes' => $this->formatNotes($client->notes()),
'last_login' => '',
'website' => $this->getString($client->client_web),
'industry_id' => 0,
'size_id' => 0,
'is_deleted' => 0,
'payment_terms' => 0,
'vat_number' => $this->getString($client->client_vat_id),
'id_number' => '',
'language_id' => 0,
'currency_id' => 0,
'custom_value1' => '',
'custom_value2' => '',
'invoice_number_counter' => 1,
'quote_number_counter' => 1,
'contacts' => [
[
'first_name' => $client->client_name,
'last_name' => $this->getString($client->client_surname),
'email' => $client->client_email ? $client->client_email : 'no_email_set@some_email.com',
'phone' => $this->getContactPhone($client),
],
],
'invoices' => array_merge($ninjaFactory->buildInvoices($client->invoices()->get()), $ninjaFactory->buildQuotes($client->quotes()->get()))
];
}
}
<?php
namespace App\Plane2Ninja\Transformers;
use App\Models\QuoteItem;
class QuoteItemTransformer extends BaseTransformer
{
/*
*
* Invoice plane has the ability to implement discounts on line items,
* this is an issue as this data cannot be translated across to Invoice Ninja
*
* The solution is to carry over the line item discounts into the invoice discount field.
* This will need to be calculated.
*
*
*/
public function transform(QuoteItem $item)
{
return
[
'product_key' => $item->item_name,
'notes' => $item->item_description,
'cost' => $item->item_price,
'qty' => $item->item_quantity ? $item->item_quantity : 1,
'invoice_item_type_id' => 1,
];
}
private function getTaxRate($item) {
$taxRate = $item->tax_rate()->get();
if(isset($taxRate->tax_rate_percent))
return $taxRate->tax_rate_percent;
else
return 0;
}
private function getTaxName($item){
$taxName = $item->tax_rate()->get();
if(isset($taxName->tax_rate_name))
return $taxName->tax_rate_name;
else
return '';
}
}
<?php
namespace App\Plane2Ninja\Transformers;
use App\Models\Quote;
/**
* Class InvoiceTransformer
* @package App\Plane2Ninja\Transformers
*/
class QuoteTransformer extends BaseTransformer
{
/**
* @param Invoice $invoice
* @return array
*/
public function transform(Quote $quote)
{
return [
'client_id' => $quote->client_id,
'invoice_number' => $quote->quote_number ? $this->getInvoiceNumber($quote->quote_number) : null,
'po_number' => '',
'terms' => '',
'public_notes' => '',
'invoice_date' => $this->getDate($quote->quote_date_created),
'due_date' => $this->getDate($quote->invoice_due_date),
'invoice_status_id' => $this->transformQuoteStatus($quote->quote_status_id),
'is_amount_discount' => $this->checkDiscountAmount($quote),
'discount' => $this->fillDiscount($quote),
'invoice_type_id' => 4,
'is_quote'=> true
];
}
private function getTaxRate($invoice, $index) {
$taxRate = $invoice->tax_rates()->get()->toArray();
if(isset($taxRate[$index-1])){
$v = $taxRate[$index-1];
return array_key_exists('tax_rate_percent',$v)? $v['tax_rate_percent'] : '';
}
else{
return 0;}
}
private function getTaxName($invoice, $index){
$taxName = $invoice->tax_rates()->get()->toArray();
if(isset($taxName[$index-1])){
$v = $taxName[$index-1];
return array_key_exists('tax_rate_name',$v)? $v['tax_rate_name'] : '';
}
else{
return '';}
}
/**
* @param Invoice $invoice
* @return bool|null
*/
private function checkDiscountAmount(Quote $quote){
if($quote->quote_discount_amount > 0)
return true;
elseif($quote->quote_discount_percent > 0)
return false;
else
return null;
}
/**
* @param Invoice $invoice
* @return int|mixed
*/
private function fillDiscount(Quote $quote){
if($quote->quote_discount_amount > 0)
return $quote->quote_discount_amount;
elseif($quote->quote_discount_percent > 0)
return $quote->quote_discount_percent;
else
return 0;
}
/**
* @param $invoiceStatus
* @return int
*/
private function transformInvoiceStatus($invoiceStatus) {
switch($invoiceStatus)
{
case 1:
return 1;
case 2:
return 2;
case 3:
return 3;
case 4:
return 6;
default:
return 1;
}
}
private function transformQuoteStatus($quoteStatus) {
switch($quoteStatus)
{
case 1:
return 1;
case 2:
return 2;
case 3:
return 3;
case 4:
return 4;
case 5:
return 5;
case 6:
return 6;
default:
return 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment