Exemplo n.º 1
0
 private function creditMemo($data)
 {
     $orderModel = VmModel::getModel('orders');
     $orderDetails = $orderModel->getOrder($data->virtuemart_order_id);
     $calc = $this->getOrderCalc($orderDetails);
     if (!$calc) {
         return false;
     }
     if (!is_array($calc['avatax_virtuemart_country_id'])) {
         $calc['avatax_virtuemart_country_id'] = json_decode($calc['avatax_virtuemart_country_id'], true);
     }
     if ($calc['activated'] == 0) {
         avadebug('Avatax creditMemo rule not activated', $calc);
         return false;
     }
     if ($calc['accrual'] and $data->order_status != 'R') {
         avadebug('Avatax creditMemo, type is accrual and not a Refund', $calc);
         return false;
     }
     if (!class_exists('TaxServiceSoap')) {
         require VMAVALARA_CLASS_PATH . DS . 'TaxServiceSoap.class.php';
     }
     if (!class_exists('DocumentType')) {
         require VMAVALARA_CLASS_PATH . DS . 'DocumentType.class.php';
     }
     if (!class_exists('DetailLevel')) {
         require VMAVALARA_CLASS_PATH . DS . 'DetailLevel.class.php';
     }
     if (!class_exists('Line')) {
         require VMAVALARA_CLASS_PATH . DS . 'Line.class.php';
     }
     if (!class_exists('ServiceMode')) {
         require VMAVALARA_CLASS_PATH . DS . 'ServiceMode.class.php';
     }
     if (!class_exists('Line')) {
         require VMAVALARA_CLASS_PATH . DS . 'Line.class.php';
     }
     if (!class_exists('GetTaxResult')) {
         require VMAVALARA_CLASS_PATH . DS . 'GetTaxResult.class.php';
     }
     $this->addresses = $this->fillValidateAvalaraAddress($calc, $orderDetails['details']['BT']);
     if (!$this->addresses) {
         vmdebug('Avatax: on order status update: no valid addresses');
         return false;
     }
     if (is_object($orderDetails['details']['BT'])) {
         self::$vmadd = get_object_vars($orderDetails['details']['BT']);
     } else {
         self::$vmadd = $orderDetails['details']['BT'];
     }
     $toInvoice = VmConfig::get('inv_os', array('C'));
     if (!is_array($toInvoice)) {
         $toInvoice = (array) $toInvoice;
     }
     //Lets find first if the committ was already done, the committ was already done, if one of history orderstatuses
     //have one status for create invoice.
     //vmdebug('my orderDetails ',$orderDetails);
     self::$vmadd['taxOverride'] = null;
     foreach ($orderDetails['history'] as $item) {
         if (in_array($item->order_status_code, $toInvoice)) {
             //the date of the order status used to create the invoice
             self::$vmadd['taxOverride'] = $this->createTaxOverride(substr($item->created_on, 0, 10), $data->order_status, $item->comments);
             //self::$vmadd['paymentDate'] = substr($item->created_on,0,10);
             //Date when order is created
             //self::$vmadd['taxOverride'] = $orderDetails['details']['BT']->created_on;
             break;
         }
     }
     //Accrual Accounting means the committ is done directly after pressing the confirm button in the cart
     //Therefore the date of the committ/invoice is the first order date and we dont need to check the order history
     if (empty(self::$vmadd['taxOverride']) and $calc['accrual']) {
         self::$vmadd['taxOverride'] = $this->createTaxOverride($orderDetails['details']['BT']->created_on, $data->order_status);
     }
     //create the products
     $products = array();
     foreach ($orderDetails['items'] as $k => $item) {
         $product = array();
         $item = (array) $item;
         //vmdebug('my item',$item);
         $product['product_sku'] = $item['order_item_sku'];
         $product['product_name'] = $item['order_item_name'];
         $product['amount'] = $item['product_quantity'];
         //$product['price'] = $item['product_final_price'];
         $product['price'] = $item['product_item_price'];
         $product['discount'] = $item['product_subtotal_discount'];
         $model = VmModel::getModel('product');
         $rProduct = $model->getProduct($item['virtuemart_product_id']);
         $product['categories'] = $rProduct->categories;
         $products[] = $product;
     }
     if (!empty($orderDetails['details']['BT']->virtuemart_shipmentmethod_id)) {
         $shipment = array();
         $shipment['product_sku'] = 'VMShipmentId_' . $orderDetails['details']['BT']->virtuemart_shipmentmethod_id;
         $shipmentModel = VmModel::getModel('Shipmentmethod');
         $shipmentModel->setId($orderDetails['details']['BT']->virtuemart_shipmentmethod_id);
         $shipmentMethod = $shipmentModel->getShipment();
         $shipment['product_name'] = $shipmentMethod->shipment_name;
         $shipment['amount'] = 1;
         $shipment['price'] = $orderDetails['details']['BT']->order_shipment;
         //decimal // TotalAmmount
         $shipment['discount'] = 0.0;
         $products[] = $shipment;
     }
     $products['discountAmount'] = $orderDetails['details']['BT']->order_discountAmount - $orderDetails['details']['BT']->coupon_discount;
     if ($data->order_status == 'R') {
         $sign = -1;
     } else {
         $sign = 1;
     }
     $request = $this->createStandardRequest($calc, $products, $sign);
     $request->setCompanyCode($calc['company_code']);
     // Your Company Code From the Dashboard
     $request->setDocDate(date('Y-m-d'));
     //date
     $request->setCustomerCode($orderDetails['details']['BT']->customer_number);
     //string Required
     if ($orderDetails['details']['BT']->order_number) {
         $request->setPurchaseOrderNo($orderDetails['details']['BT']->order_number);
         //string Optional
     }
     $totalTax = 0.0;
     $invoiceNumber = 'onr_' . $orderDetails['details']['BT']->order_number;
     vRequest::setVar('create_invoice', 1);
     $orderModel->createInvoiceNumber($orderDetails['details']['BT'], $invoiceNumber);
     if (is_array($invoiceNumber)) {
         $invoiceNumber = $invoiceNumber[0];
     }
     if ($calc['committ'] and $invoiceNumber) {
         if ($data->order_status == 'R') {
             $request->setDocType(DocumentType::$ReturnInvoice);
         } else {
             $request->setDocType(DocumentType::$SalesInvoice);
         }
         // Only supported types are SalesInvoice or SalesOrder
         $request->setCommit(true);
         $request->setDocCode($invoiceNumber);
         self::$_taxResult = FALSE;
     }
     vmSetStartTime('avagetTax');
     self::$_taxResult = $this->executeRequest($request);
     vmTime('Avalara executeRequest ', 'avagetTax');
     if (self::$_taxResult) {
         if (isset(self::$_taxResult['totalTax'])) {
             $totalTax = self::$_taxResult['totalTax'];
         }
     }
     return $totalTax;
 }
Exemplo n.º 2
0
 private function getShopperData()
 {
     if (!isset(self::$vmadd)) {
         //We need for the tax calculation the shipment Address
         //We have this usually in our cart.
         if (!class_exists('VirtueMartCart')) {
             require JPATH_VM_SITE . DS . 'helpers' . DS . 'cart.php';
         }
         $cart = VirtueMartCart::getCart();
         //Test first for ST
         if ($cart->STsameAsBT) {
             if (!empty($cart->BT)) {
                 $vmadd = $cart->BT;
             }
         } else {
             if (!empty($cart->ST)) {
                 $vmadd = $cart->ST;
             } else {
                 if (!empty($cart->BT)) {
                     $vmadd = $cart->BT;
                 }
             }
         }
         $jUser = JFactory::getUser();
         if ($jUser->id) {
             $userModel = VmModel::getModel('user');
             $userModel->setId($jUser->id);
             $vmadd['customer_id'] = $userModel->getCustomerNumberById();
         } else {
             $firstName = empty($vmadd['first_name']) ? '' : $vmadd['first_name'];
             $lastName = empty($vmadd['last_name']) ? '' : $vmadd['last_name'];
             $email = empty($vmadd['email']) ? '' : $vmadd['email'];
             $complete = $firstName . $lastName . $email;
             if (!empty($complete)) {
                 $vmadd['customer_id'] = 'nonreg_' . $vmadd['first_name'] . '_' . $vmadd['last_name'] . '_' . $vmadd['email'];
             } else {
                 $vmadd['customer_id'] = '';
             }
         }
         //vmdebug('getShopperData',$vmadd);
         //Maybe the user is logged in, but has no cart yet.
         if (empty($vmadd)) {
             $jUser = JFactory::getUser();
             $userModel = VmModel::getModel('user');
             $userModel->setId($jUser->id);
             $BT_userinfo_id = $userModel->getBTuserinfo_id();
             //Todo check if we actually need this fallback
             //vmdebug('getShopperData cart data was empty',$vmadd);
         }
         //vmdebug('Tax $vmadd',$vmadd);
         if (empty($vmadd) or !is_array($vmadd) or is_array($vmadd) and count($vmadd) < 2) {
             vmInfo('VMCALCULATION_AVALARA_INSUF_INFO');
             $vmadd = FALSE;
         }
         self::$vmadd = $vmadd;
     }
     return self::$vmadd;
 }