Well I have reviewed the plugin and wrote this one which objective is to sync baskets/payments with Joomla That Invoice Manager.
This would be a perfect fit for our customers and a nice addition to our use of CBsubs.
However I struggle to deal with payments when they are reversed or paid partially.
Hope it could help someone when people need a component for invoicing (this one is pretty complementary to a lot of components including hikashop)
Code:
<?php
use CB\Database\Table\UserTable;
if ( ! ( defined( '_VALID_CB' ) || defined( '_JEXEC' ) || defined( '_VALID_MOS' ) ) ) { die( 'Direct Access to this location is not allowed.' ); }
global $_PLUGINS;
$_PLUGINS->registerFunction( 'onCPayAfterPaymentStatusUpdateEvent', 'onCPayAfterPaymentStatusUpdateEvent', 'getahainvoicemanagerTab' );
class getahainvoicemanagerTab extends cbTabHandler {
public function onCPayAfterPaymentStatusUpdateEvent( &$user, &$paymentBasket, /** @noinspection PhpUnusedParameterInspection */ &$subscriptions, $unifiedStatus, /** @noinspection PhpUnusedParameterInspection */ $previousUnifiedStatus, /** @noinspection PhpUnusedParameterInspection */ $eventType ) {
global $_CB_framework;
// if ( ( ! ( ( $unifiedStatus == 'Completed' ) || ( $unifiedStatus == 'Processed' ) ) ) || ( ! is_object( $user ) ) ) {
// return;
// }
$params = cbpaidApp::settingsParams();
$invFile = $_CB_framework->getCfg( 'absolute_path' ) . '/components/com_invoices/helpers/helpers.php';
if ( ! file_exists( $invFile ) ) {
return;
}
$paramsiv = JComponentHelper::getParams( 'com_invoices' );
$component = "com_comprofiler" ;
include_once($invFile);
$db = JFactory::getDBO();
$is_new = false;
$must_update = false ;
$invoice_id = 0 ;
//we check if this particular order had already been tracked
$query = " SELECT id FROM #__invoices_invoices WHERE external_type = '".$component."' AND external_id = '". $paymentBasket->id."'";
$db->setQuery($query);
$exists = $db->loadResult();
if(!$exists){
$is_new = true ;
}
else{
$must_update = true ;
$invoice_id = $exists;
}
if($is_new || $must_update){
$query = ' SELECT i.real_invoice_num FROM #__invoices_invoices as i WHERE i.type = 1 '.' ORDER BY i.id DESC ';
$db->setQuery( $query );
$real_invoice_num = $db->loadResult() + 1;
$invoice_num = InvoicesHelper::getInvoiceNum($real_invoice_num) ;
$taxes = array();
$invoice_date = date('Y-m-d H:i:s');
$app = JFactory::getApplication();
if($app->isSite()) $language = "fr-FR";
else $language = $paramsiv->get('default_language', 'en-GB');
$link_contact = 2;
$contact_id = 0;
$query = " SELECT id FROM #__invoices_contacts WHERE email = '". $paymentBasket->payer_email ."'";
$db->setQuery($query);
$contact_id = (int)$db->loadResult();
if($contact_id) $link_contact = 1;
if(!$is_new){ // we are updating. These fields should NOT be updated.
$real_invoice_num = null;
$invoice_num = null;
$invoice_date = null;
$language = null;
$link_contact = 1;
$contact_id = null;
}
$invoice_data = array(
"id" => $invoice_id ,
"to_name" => $paymentBasket->first_name . " " .$paymentBasket->last_name,
"to_company" => $paymentBasket->payer_business_name,
"to_address" => $paymentBasket->address_street,
"to_email" => $paymentBasket->payer_email,
"to_zipcode" => $paymentBasket->address_zip,
"to_city" => $paymentBasket->address_city,
"to_state" => $paymentBasket->address_state,
"to_country" => $paymentBasket->address_country,
"to_phone" => $paymentBasket->contact_phone,
"to_vatid" => $paymentBasket->vat_number,
"vincular_cliente_checkbox" => $link_contact,
"user_id" => $contact_id,
"from_name" => $paramsiv->get('from_name'),
"from_address" => $paramsiv->get('from_address'),
"from_num" => $paramsiv->get('from_num'),
"from_url" => $paramsiv->get('from_url'),
"from_email" => $paramsiv->get('from_email'),
"from_phone" => $paramsiv->get('from_phone'),
"invoice_num" => $invoice_num,
"real_invoice_num" => $real_invoice_num,
"invoice_date" => $invoice_date,
"template_id" => $paramsiv->get('invoice_template'),
"currency_id" => $paramsiv->get('currency_id'),
"language" => $language,
// "publish" => $paramsiv->get('activation'),
"publish" => 1,
// "is_recurrent" => $paymentBasket->recurring,
"start_publish" => null,
"end_publish" => null,
//to do invoice date plus 15j
"invoice_duedate" => date('Y-m-d H:i:s', strtotime(' + 15 days')),
"joomla_user_id" => $paymentBasket->user_id,
"taxes" => $taxes,
"status" => null,
"external_type" => $component,
"external_id" => $paymentBasket->id,
"external_ref" => $paymentBasket->invoice,
//"discount" => $order_discount_price,
"sendemail" => $paramsiv->get('sendemail', 0)
) ;
$items = array();
$value = ($paymentBasket->mc_gross-$paymentBasket->tax);
$diff = ($paymentBasket->mc_gross-$value);
if($paymentBasket->tax > 0) $thetax = round(( $diff / $value ) * 100, $paramsiv->get('roundtax', 2)) ;
else $thetax = 0 ;
$products = $paymentBasket->loadPaymentItems();
foreach($products as $product){
$name = $product->alias;
$sku = $product->plan_id;
$items[] = array(
"id" => $paymentBasket->id,
"sku" => $sku,
"name" => strip_tags($name),
"desc" => strip_tags($paymentBasket->item_name),
"value" => $value,
"amount" => $paymentBasket->quantity,
"tax" => $thetax ,
"tax_id" => $paramsiv->get('tax_id', 0),
"external_type" => $component,
"product_id" => $products->artnum,
);
}
//Shipping
if($paymentBasket->mc_shipping > 0){
$shipping_name = "";
$shipping_name = $paymentBasket->shipping;
$shipping_price = $paymentBasket->mc_shipping;
$shipping_item_exists = 0;
if($must_update){
$query = " SELECT id FROM #__invoices_items WHERE invoice_id = '".$invoice_id."' AND external_type = '".$component."_shipping' ";
$db->setQuery($query);
$shipping_item_exists = $db->loadResult();
}
$shipping_item_id = $shipping_item_exists ;
$items[] = array(
"id" => $shipping_item_id,
"sku" => "",
"name" => JText::_("Shipping"),
"desc" => $shipping_name,
"value" => $shipping_price,
"amount" => 1,
"tax" => ( $theorder->tax / $shipping_price ) * 100,
"tax_id" => $paramsiv->get('tax_id', 0),
"external_type" => $component."_shipping",
"product_id" => 0,
);
}
//Payment
if($paymentBasket->mc_gross > 0){
$payment_name = "";
$payment_name = $paymentBasket->payment_type;
$payment_item_exists = 0 ;
if($must_update){
$query = " SELECT id FROM #__invoices_items WHERE invoice_id = '".$invoice_id."' AND external_type = '".$component."_payment'";
$db->setQuery($query);
$payment_item_exists = $db->loadResult();
}
$payment_item_id = $payment_item_exists ;
$items[] = array(
"id" => $payment_item_id,
"sku" => $sku,
"name" => strip_tags($name),
"desc" => strip_tags($paymentBasket->item_name),
"value" => $value,
"amount" => $paymentBasket->quantity,
"tax" => $thetax ,
"tax_id" => $paramsiv->get('tax_id', 0),
"external_type" => $component."_payment",
"product_id" => $products->artnum
);
}
$payments = array();
if($paymentBasket->payment_status == "Completed") $payment_status = 1 ;
else $payment_status = 0 ;
$payment_exists = 0 ;
//if(!$paymentBasket->payment_gross) $paymentBasket = $paymentBasket->old;
if($must_update){
$query = " SELECT id FROM #__invoices_payments WHERE invoice_id = '".$invoice_id."' AND payment_type = 'payment_".$paymentBasket->payment_method . "' ";
$db->setQuery($query);
$payment_exists = $db->loadResult();
}
$payment_id = $payment_exists;
$payments[0] = array(
"id" => $payment_id,
"payment_details" => $paymentBasket->txn_id,
"payment_datetime" => $paymentBasket->time_completed_date,
"payment_amount" => $paymentBasket->mc_gross,
"payment_type" => "payment_".$paymentBasket->payment_method,
"payment_description" => $paymentBasket->payment_method . " - ".$paymentBasket->payment_type . " <br /> id : " . $paymentBasket->txn_id,
"payment_id" => $paymentBasket->id,
"payment_status" => $payment_status
);
if($must_update){
$query = " SELECT id FROM #__cbsubs_payments WHERE payment_basket_id = '".$paymentBasket->id."' ORDER BY time_paid DESC LIMIT 1";
$db->setQuery($query);
$last_payment = $db->loadResult();
$payments[0] = array(
"id" => 0,
"payment_details" => $paymentBasket->txn_id,
"payment_datetime" => $last_payment->time_paid,
"payment_amount" => $last_payment->mc_gross,
"payment_type" => "payment_".$paymentBasket->payment_method,
"payment_description" => $paymentBasket->payment_method . " - ".$paymentBasket->payment_type . " <br /> id : " . $paymentBasket->txn_id,
"payment_id" => $last_payment->id,
"payment_status" => $payment_status
);
}
InvoicesHelper::create_invoice($invoice_data, $items, $payments);
}
}
}