Completed
Push — master ( 63d470...84e35a )
by Adam
18:27
created

Bundle/InvoiceBundle/Manager/InvoiceManager.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/*
3
 * WellCommerce Open-Source E-Commerce Platform
4
 *
5
 * This file is part of the WellCommerce package.
6
 *
7
 * (c) Adam Piotrowski <[email protected]>
8
 *
9
 * For the full copyright and license information,
10
 * please view the LICENSE file that was distributed with this source code.
11
 */
12
13
namespace WellCommerce\Bundle\InvoiceBundle\Manager;
14
15
use Carbon\Carbon;
16
use WellCommerce\Bundle\CoreBundle\Manager\AbstractManager;
17
use WellCommerce\Bundle\InvoiceBundle\Entity\Invoice;
18
use WellCommerce\Bundle\InvoiceBundle\Entity\InvoiceItem;
19
use WellCommerce\Bundle\OrderBundle\Entity\Order;
20
use WellCommerce\Bundle\OrderBundle\Entity\OrderProduct;
21
22
/**
23
 * Class InvoiceManager
24
 *
25
 * @author  Adam Piotrowski <[email protected]>
26
 */
27
class InvoiceManager extends AbstractManager
28
{
29
    public function prepareInvoiceForOrder(Order $order): Invoice
30
    {
31
        /** @var Invoice $invoice */
32
        $invoice = $this->initResource();
33
        $invoice->setCurrency($order->getCurrency());
34
        $invoice->setAmountDue(0.00);
35
        $invoice->setAmountPaid($order->getSummary()->getGrossAmount());
36
        $invoice->setPaid(true);
37
        $invoice->setBillingAddress(clone $order->getBillingAddress());
38
        $invoice->setOrder($order);
39
        $invoice->setShop($order->getShop());
40
        $invoice->setShippingMethodName($order->getShippingMethod()->translate()->getName());
41
        $invoice->setPaymentMethodName($order->getPaymentMethod()->translate()->getName());
42
        $invoice->setSignature($this->getSecurityHelper()->getAuthenticatedAdmin()->getFullName());
43
        $invoice->setProcessor($order->getShop()->getInvoiceProcessor());
44
        $invoice->setDueDate(Carbon::now()->addDays((int)$order->getShop()->getInvoiceMaturity()));
45
        
46
        $this->prepareInvoiceItems($invoice, $order);
47
        
48
        return $invoice;
49
    }
50
    
51
    private function prepareInvoiceItems(Invoice $invoice, Order $order)
52
    {
53
        $order->getProducts()->map(function (OrderProduct $orderProduct) use ($invoice) {
54
            $invoiceItem = $this->createInvoiceItem($orderProduct);
55
            $invoiceItem->setInvoice($invoice);
56
            $invoice->getItems()->add($invoiceItem);
57
            $this->getEntityManager()->persist($invoiceItem);
58
        });
59
    }
60
    
61
    private function createInvoiceItem(OrderProduct $orderProduct): InvoiceItem
62
    {
63
        $invoiceItem = new InvoiceItem();
64
        $invoiceItem->setCurrency($orderProduct->getSellPrice()->getCurrency());
65
        $invoiceItem->setQuantity($orderProduct->getQuantity());
66
        $invoiceItem->setTaxRate($orderProduct->getSellPrice()->getTaxRate());
67
        $invoiceItem->setTaxAmount($orderProduct->getSellPrice()->getTaxAmount());
68
        $invoiceItem->setGrossAmount($orderProduct->getSellPrice()->getGrossAmount());
69
        $invoiceItem->setNetAmount($orderProduct->getSellPrice()->getNetAmount());
70
        $invoiceItem->setDescription($orderProduct->getProduct()->translate()->getName());
0 ignored issues
show
It seems like getName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
71
        
72
        return $invoiceItem;
73
    }
74
}
75