private function makeInvoiceFromOrder($order_id) { $comment = ''; $data = array(); $this->load->model('sale/order'); $this->load->model('catalog/product'); $order = $this->model_sale_order->getOrder($order_id); if ($order) { opencart_invfox__trace($order, 0); $api = new InvfoxAPI($this->CONF['API_KEY'], $this->CONF['API_DOMAIN'], true); $api->setDebugHook("opencart_invfox__trace"); opencart_invfox__trace("============ INVFOX::begin ==========="); $r = $api->assurePartner(array('name' => $order['payment_firstname'] . " " . $order['payment_lastname'] . ($order['payment_company'] ? ", " : "") . $order['payment_company'], 'street' => $order['payment_address_1'] . "\n" . $order['payment_address_2'], 'postal' => $order['payment_postcode'], 'city' => $order['payment_city'], 'country' => $order['payment_country'], 'vatid' => $order['payment_tax_id'], 'phone' => $order['telephone'], 'website' => "", 'email' => $order['email'], 'notes' => '', 'vatbound' => false, 'custaddr' => '', 'payment_period' => $this->CONF['customer_general_payment_period'], 'street2' => '')); opencart_invfox__trace("============ INVFOX::assured partner ============"); if ($r->isOk()) { $comment .= "- contact added\n"; opencart_invfox__trace("============ INVFOX::before create invoice ============"); $clientIdA = $r->getData(); $clientId = $clientIdA[0]['id']; $date1 = $api->_toSIDate(date('Y-m-d')); //// TODO LONGTERM ... figure out what we do with different Dates on api side (maybe date optionally accepts dbdate format) $invid = str_pad($order_id, 5, "0", STR_PAD_LEFT); //todo: ask, check $data['products'] = array(); $products = $this->model_sale_order->getOrderProducts($order_id); $body2 = array(); $subtotal = 0; $producttax = 0; foreach ($products as $product) { $productMore = $this->model_catalog_product->getProduct($product['product_id']); $product_options_text = ''; if ($this->CONF['display_product_option_label']) { $product_options = $this->model_sale_order->getOrderOptions($order_id, $product['order_product_id']); if ($product_options) { foreach ($product_options as $product_option) { $product_options_text .= $product_option['name'] . ':' . $product_option['value'] . "\n"; } } } $subtotal = $subtotal + round($product['price'], 2); opencart_invfox__trace($product, 0); $body2[] = array('code' => $productMore['sku'], 'title' => $product['name'] . " " . $product['model'] . "\n" . $product_options_text, 'qty' => $product['quantity'], 'mu' => '', 'price' => round($product['price'], 2), 'vat' => round($product['tax'] / $product['price'] * 100, $this->CONF['round_calculated_taxrate_to']), 'discount' => 0); $producttax = round($product['tax'] / $product['price'] * 100, $this->CONF['round_calculated_taxrate_to']); } $shipping = $this->findShipping($this->model_sale_order->getOrderTotals($order_id)); $coupon = $this->findCoupon($this->model_sale_order->getOrderTotals($order_id)); if ($this->CONF['document_to_make'] != 'inventory' && $shipping['value'] > 0) { if ($shipping) { opencart_invfox__trace("============ INVFOX:: adding shipping ============"); $discount = 0; if ($coupon) { $discount = $coupon['value']; } /* if ($this->CONF['partial_sum_label']) { $body2[] = array( 'title' => "= ".$this->CONF['partial_sum_label'], 'qty' => 1, 'mu' => '', 'price' => 0, 'vat' => 0, 'discount' => 0 ); } */ $body2[] = array('title' => $shipping['title'], 'qty' => 1, 'mu' => '', 'price' => $shipping['value'], 'vat' => $this->CONF['tax_rate_on_shipping'], 'discount' => 0); } if ($coupon) { //$couponvat = round($product['tax'] / $coupon['value'] * 100, $this->CONF['round_calculated_taxrate_to']); $body2[] = array('title' => $coupon['title'], 'qty' => 1, 'mu' => '', 'price' => $coupon['value'], 'vat' => $producttax, 'discount' => 0); } } } /* */ opencart_invfox__trace("============ INVFOX::before create invoice call ============"); // TODO -- it can make it's own INVOICENUMS OR INVFOX CAN MAKE THEM $invoice_no = $order['invoice_prefix'] . $order['order_id']; $invid = $this->CONF['use_shop_document_numbers'] ? $order['invoice_prefix'] . $order['order_id'] : ''; opencart_invfox__trace($invoice_no); if ($this->CONF['document_to_make'] == 'invoice') { $r2 = $api->createInvoice(array('title' => $invid, 'date_sent' => $date1, 'date_to_pay' => $date1, 'date_served' => $date1, 'id_partner' => $clientId, 'taxnum' => '-', 'doctype' => 0, 'id_document_ext' => $order['order_id'], 'pub_notes' => $invoice_no), $body2); if ($r2->isOk()) { $invA = $r2->getData(); $this->model_sale_order->insertInvoicefoxId($order_id, $invA[0]['id']); $comment .= "- invoice # {$invA[0]['title']} was created at {$this->CONF['APP_NAME']}."; } } elseif ($this->CONF['document_to_make'] == 'proforma') { $r2 = $api->createProFormaInvoice(array('title' => $invoice_no, 'date_sent' => $date1, 'days_valid' => $this->CONF['proforma_days_valid'], 'id_partner' => $clientId, 'taxnum' => '-', 'pub_notes' => $invoice_no), $body2); if ($r2->isOk()) { $invA = $r2->getData(); $comment .= "- pro forma invoice # {$invA[0]['title']} was created at {$this->CONF['APP_NAME']}."; } } elseif ($this->CONF['document_to_make'] == 'inventory') { $invoice_no = $invoice_no == "-" ? "" : $invoice_no; $r2 = $api->createInventorySale(array('docnum' => $invoice_no, 'date_created' => $date1, 'id_contact_to' => $clientId, 'id_contact_from' => $this->CONF['from_warehouse_id'], 'taxnum' => '-', 'doctype' => 1, 'pub_notes' => $invoice_no), $body2); if ($r2->isOk()) { $invA = $r2->getData(); $comment .= "Inventory sales document No. {$invA[0]['docnum']} was created at {$this->CONF['APP_NAME']}."; } } opencart_invfox__trace($r2); opencart_invfox__trace("============ INVFOX::after create invoice ============"); } return $comment; }
<?php include "style/header.php"; require_once "lib/strpcapi.php"; require_once "lib/invfoxapi.php"; if (isset($_POST['call'])) { $api = new InvfoxAPI('vr64g6891lgatnf83dmxticsc10ashr2f4s9ye7w'); $r = $api->assurePartner(array('name' => 'asdasd', 'street' => 'asdasds', 'postal' => '12312', 'city' => 'asdasd', 'vatid' => '', 'phone' => '', 'website' => '', 'email' => '', 'notes' => '', 'vatbound' => 0, 'custaddr' => '', 'payment_period' => 14, 'street2' => 'asdsad')); echo "<br/><div class='type'>response</div><pre class='resp'>" . print_r($r->getData(), true) . "</pre>"; if ($r->isOk()) { $clientIdA = $r->getData(); $clientId = $clientIdA[0]['id']; $r = $api->createInvoice(array('title' => 'INV00001', 'date_sent' => '10/22/2010', 'date_to_pay' => '11/04/2010', 'id_partner' => $clientId, 'vat_level' => 10), array(array('title' => 'custom programming', 'qty' => 20, 'mu' => 'hour', 'price' => 80), array('title' => 'support on project', 'qty' => 6, 'mu' => 'hour', 'price' => 60))); if ($r->isOk()) { $invIdA = $r->getData(); $invId = $invIdA[0]['id']; $api->downloadPDF($invId); echo "<p><a href='invoices/{$invId}.pdf'>Download PDF</a></p>"; } } } ?> <form method="post"><input type="submit" name="call" value="Call" /></form> <?php include "style/footer.php";