/** * @return \Magento\Framework\Controller\ResultInterface * @throws GenericException */ public function execute() { $result = $this->resultFactory->create(ResultFactory::TYPE_JSON); $invoices = []; try { $json = file_get_contents('php://input'); $json = json_decode($json); if (!property_exists($json, 'customers')) { throw new GenericException('Invalid JSON input'); } foreach ($json->customers as $customer) { if (!property_exists($customer, 'email')) { continue; } $mageOrders = $this->getOrderCollectionFactory()->create()->addFieldToSelect('*')->addFieldToFilter('customer_email', $customer->email)->setOrder('created_at', 'desc'); $invoice = new \Expressly\Entity\Invoice(); $invoice->setEmail($customer->email); foreach ($mageOrders as $mageOrder) { $total = $mageOrder->getData('base_grand_total'); $tax = $mageOrder->getData('base_tax_amount'); $order = new \Expressly\Entity\Order(); $order->setId($mageOrder->getData('increment_id'))->setDate(new \DateTime($mageOrder->getData('created_at')))->setCurrency($mageOrder->getData('base_currency_code'))->setTotal((double) $total - (double) $tax, (double) $tax)->setItemCount((int) $mageOrder->getData('total_qty_ordered'))->setCoupon($mageOrder->getData('coupon_code')); $invoice->addOrder($order); } $invoices[] = $invoice; } $presenter = new BatchInvoicePresenter($invoices); $result->setData($presenter->toArray()); } catch (\Exception $e) { // log error $result->setData(['error' => $e->getMessage()]); } return $result; }
public function testConstruction() { $invoice = $this->getMockBuilder('Expressly\\Entity\\Invoice')->getMock(); $invoice->method('toArray')->willReturn(array('email' => '*****@*****.**', 'orderCount' => 2, 'preTaxTotal' => 100.0, 'postTaxTotal' => 110.0, 'tax' => 10.0, 'orders' => array(array('id' => 'ORDER-531', 'date' => '2015-08-08T13:00:00+0000', 'itemCount' => 1, 'coupon' => '25OFF', 'currency' => 'GBP', 'preTaxTotal' => 35.0, 'postTaxTotal' => 37.0, 'tax' => 2.0), array('id' => 'ORDER-875', 'date' => '2015-09-09T15:00:00+0000', 'itemCount' => 1, 'currency' => 'GBP', 'preTaxTotal' => 65.0, 'postTaxTotal' => 73.0, 'tax' => 8.0)))); $invoices = array($invoice, array('not an invoice')); $presenter = new BatchInvoicePresenter($invoices); return $presenter->toArray(); }
public function invoiceAction() { $this->getResponse()->setHeader('Content-type', 'application/json'); $route = $this->resolver->process(preg_replace('/.*(expressly\\/.*)/i', '/${1}', $_SERVER['REQUEST_URI'])); if ($route instanceof Route) { $json = file_get_contents('php://input'); $json = json_decode($json); $invoices = array(); try { if (!property_exists($json, 'customers')) { throw new GenericException('Invalid JSON input'); } $orderModel = Mage::getModel('sales/order'); foreach ($json->customers as $customer) { if (!property_exists($customer, 'email')) { continue; } $from = \DateTime::createFromFormat('Y-m-d', $customer->from, new \DateTimeZone('UTC')); $to = \DateTime::createFromFormat('Y-m-d', $customer->to, new \DateTimeZone('UTC')); $mageOrders = $orderModel->getCollection()->addFieldToFilter('customer_email', $customer->email)->setOrder('created_at', 'desc'); $invoice = new Invoice(); $invoice->setEmail($customer->email); foreach ($mageOrders as $mageOrder) { $orderDate = new \DateTime($mageOrder->getData('created_at'), new \DateTimeZone('UTC')); $orderDate = \DateTime::createFromFormat('Y-m-d', $orderDate->format('Y-m-d'), new \DateTimeZone('UTC')); if ($orderDate >= $from && $orderDate < $to) { $total = $mageOrder->getData('base_grand_total'); $tax = $mageOrder->getData('base_tax_amount'); $order = new Order(); $order->setId($mageOrder->getData('increment_id'))->setDate(new \DateTime($mageOrder->getData('created_at')))->setCurrency($mageOrder->getData('base_currency_code'))->setTotal((double) $total - (double) $tax, (double) $tax)->setItemCount((int) $mageOrder->getData('total_qty_ordered'))->setCoupon($mageOrder->getData('coupon_code')); $invoice->addOrder($order); } } $invoices[] = $invoice; } } catch (\Exception $e) { $this->logger->error(ExceptionFormatter::format($e)); } $presenter = new BatchInvoicePresenter($invoices); $this->getResponse()->setBody(json_encode($presenter->toArray())); } else { $this->getResponse()->setHttpResponseCode(401); } }
private function batchInvoice() { $json = file_get_contents('php://input'); $json = json_decode($json); $invoices = array(); try { if (!property_exists($json, 'customers')) { throw new GenericException('Invalid JSON request'); } foreach ($json->customers as $customer) { if (!property_exists($customer, 'email')) { continue; } if (email_exists($customer->email)) { $invoice = new Invoice(); $invoice->setEmail($customer->email); $orderPosts = get_posts(array('meta_key' => '_billing_email', 'meta_value' => $customer->email, 'post_type' => 'shop_order', 'numberposts' => -1)); foreach ($orderPosts as $post) { $wpOrder = new WC_Order($post->ID); if ($wpOrder->order_date > $customer->from && $wpOrder->order_date < $customer->to) { $total = 0.0; $tax = 0.0; $count = 0; $order = new Order(); foreach ($wpOrder->get_items('line_item') as $lineItem) { $tax += (double) $lineItem['line_tax']; $total += (double) $lineItem['line_total'] - (double) $lineItem['line_tax']; $count++; if ($lineItem->tax_class) { $order->setCurrency($lineItem['tax_class']); } } $order->setId($wpOrder->id)->setDate(new \DateTime($wpOrder->order_date))->setItemCount($count)->setTotal($total, $tax); $coupons = $wpOrder->get_used_coupons(); if (!empty($coupons)) { $order->setCoupon($coupons[0]); } $invoice->addOrder($order); } } $invoices[] = $invoice; } } } catch (GenericException $e) { $this->app['logger']->error($e); } $presenter = new BatchInvoicePresenter($invoices); wp_send_json($presenter->toArray()); }