Example #1
0
 public function testBuildingEntity()
 {
     $entity = new Order();
     $this->assertInstanceOf('Expressly\\Entity\\Order', $entity->setId('ORDER-531'));
     $this->assertInstanceOf('Expressly\\Entity\\Order', $entity->setDate(new \DateTime('2015-08-08 13:00:00 +00:00')));
     $this->assertInstanceOf('Expressly\\Entity\\Order', $entity->setItemCount(1));
     $this->assertInstanceOf('Expressly\\Entity\\Order', $entity->setCoupon('25OFF'));
     $this->assertInstanceOf('Expressly\\Entity\\Order', $entity->setCurrency('GBP'));
     $this->assertInstanceOf('Expressly\\Entity\\Order', $entity->setTotal(35.0, 2.0));
     $this->assertJson(json_encode($entity->toArray()));
     $this->assertJsonStringEqualsJsonString(json_encode($entity->toArray()), json_encode(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)));
 }
Example #2
0
 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());
 }