/var/www/hkosl.com/littleark/webadmin/models/OrderItemPhase.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php

class OrderItemPhase extends BaseModel
{
    protected 
$table "order_item_phase";

    public function 
orderItem()
    {
        return 
$this->belongsTo('OrderItem');
    }

    public function 
invoiceItems()
    {
        return 
$this->morphMany('InvoiceItem''invoiceable');
    }

    public function 
proInstallment()
    {
        return 
$this->belongsTo('ProInstallment''pro_installment_id''id');
    }

    
/**
     * for subscription and installment (Invoice has 1 invoiceItem and orderItemPhase)
     */
    
public function createInvoice(Invoice $invoice null)
    {
        if (
$invoice === null) {
            
// vdump(__FUNCTION__);
            
$invoice = new Invoice([
                
// 'code'          => "{$this->orderItem->order->code}-{$this->id}",
                
'order_id'      => $this->orderItem->order_id,
                
'customer_id'   => $this->orderItem->order->customer_id,
                
'currency_code' => $this->orderItem->order->currency_code,
                
'currency_rate' => $this->orderItem->order->currency_rate,
                
'docdate'       => timestamp(),
                
'status'        => 'NEW',
            ]);
            
$invoice->save();
        }

        
$this->createInvoiceItem($invoice);

        
$invoice->generateCode()->calcAmount()->save();
        return 
$invoice;
    }

    public function 
createInvoiceItem(Invoice $invoice)
    {
        
$invoiceItem = new InvoiceItem([
            
'supplier_price'         => $this->orderItem->proProductSku->supplier->deductCommission($this->amount),
            
'supplier_currency_code' => 'CNY',
            
'quantity'               => 1,
            
'price'                  => $this->amount,
            
'discounted_price'       => $this->amount,
            
'invoiceable_type'       => 'OrderItemPhase',
            
'invoiceable_id'         => $this->id,
            
'supplier_id'            => $this->orderItem->proProductSku->supplier_id,
        ]);
        
$invoice->invoiceItems()->save($invoiceItem);
    }

    public function 
confirm()
    {
        
$this->status "PAID";
        
$this->save();
    }

    
// create customer point when the order is completed (SALES)
    
public function createCustomerPoint()
    {
        
// check: an OrderItemPhase has only one InvoiceItem?
        
$this->invoiceItems->first()->createCustomerPoint()->save();
    }
}