public function testBuildingEntity() { $orderWithCoupon = new Order(); $orderWithCoupon->setId('ORDER-531')->setDate(new \DateTime('2015-08-08 13:00:00 +00:00'))->setItemCount(1)->setCoupon('25OFF')->setCurrency('GBP')->setTotal(35.0, 2.0); $orderWithoutCoupon = new Order(); $orderWithoutCoupon->setId('ORDER-875')->setDate(new \DateTime('2015-09-09 15:00:00 +00:00'))->setItemCount(1)->setCurrency('GBP')->setTotal(65.0, 8.0); $entity = new Invoice(); $this->assertInstanceOf('Expressly\\Entity\\Invoice', $entity->setEmail('*****@*****.**')); $this->assertInstanceOf('Expressly\\Entity\\Invoice', $entity->addOrder($orderWithCoupon)); $this->assertInstanceOf('Expressly\\Entity\\Invoice', $entity->addOrder($orderWithoutCoupon)); $this->assertJson(json_encode($entity->toArray())); $this->assertJsonStringEqualsJsonString(json_encode($entity->toArray()), json_encode(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))))); }
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()); }