Blame view

app/Http/Controllers/Api/Incomes/InvoicePayments.php 3.87 KB
b7c7a5f6   Alexey Boroda   first commit
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  <?php
  
  namespace App\Http\Controllers\Api\Incomes;
  
  use App\Http\Requests\Income\InvoicePayment as Request;
  use App\Models\Income\Invoice;
  use App\Models\Income\InvoiceHistory;
  use App\Models\Income\InvoicePayment;
  use App\Models\Setting\Currency;
  use App\Traits\DateTime;
  use App\Transformers\Income\InvoicePayments as Transformer;
  use Date;
  use Dingo\Api\Routing\Helpers;
  use Illuminate\Foundation\Bus\DispatchesJobs;
  use Illuminate\Routing\Controller as BaseController;
  use Illuminate\Foundation\Validation\ValidatesRequests;
  use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
  
  class InvoicePayments extends BaseController
  {
      use DateTime, Helpers, AuthorizesRequests, DispatchesJobs, ValidatesRequests;
  
      /**
       * Display a listing of the resource.
       *
       * @param  $invoice_id
       * @return \Dingo\Api\Http\Response
       */
      public function index($invoice_id)
      {
          $invoice_payments = InvoicePayment::where('invoice_id', $invoice_id)->get();
  
          return $this->response->collection($invoice_payments, new Transformer());
      }
  
      /**
       * Display the specified resource.
       *
       * @param  $invoice_id
       * @param  $id
       * @return \Dingo\Api\Http\Response
       */
      public function show($invoice_id, $id)
      {
          $invoice_payment = InvoicePayment::find($id);
  
          return $this->response->item($invoice_payment, new Transformer());
      }
  
      /**
       * Store a newly created resource in storage.
       *
       * @param  $invoice_id
       * @param  $request
       * @return \Dingo\Api\Http\Response
       */
      public function store($invoice_id, Request $request)
      {
          // Get currency object
          $currency = Currency::where('code', $request['currency_code'])->first();
  
          $request['currency_code'] = $currency->code;
          $request['currency_rate'] = $currency->rate;
  
          $request['invoice_id'] = $invoice_id;
  
          $invoice = Invoice::find($invoice_id);
  
          if ($request['currency_code'] == $invoice->currency_code) {
              if ($request['amount'] > $invoice->amount) {
                  return $this->response->noContent();
              } elseif ($request['amount'] == $invoice->amount) {
                  $invoice->invoice_status_code = 'paid';
              } else {
                  $invoice->invoice_status_code = 'partial';
              }
          } else {
              $request_invoice = new Invoice();
  
              $request_invoice->amount = (float) $request['amount'];
              $request_invoice->currency_code = $currency->code;
              $request_invoice->currency_rate = $currency->rate;
  
              $amount = $request_invoice->getConvertedAmount();
  
              if ($amount > $invoice->amount) {
                  return $this->response->noContent();
              } elseif ($amount == $invoice->amount) {
                  $invoice->invoice_status_code = 'paid';
              } else {
                  $invoice->invoice_status_code = 'partial';
              }
          }
  
          $invoice->save();
  
          $invoice_payment = InvoicePayment::create($request->input());
  
          $request['status_code'] = $invoice->invoice_status_code;
          $request['notify'] = 0;
  
          $desc_date = Date::parse($request['paid_at'])->format($this->getCompanyDateFormat());
          $desc_amount = money((float) $request['amount'], $request['currency_code'], true)->format();
          $request['description'] = $desc_date . ' ' . $desc_amount;
  
          InvoiceHistory::create($request->input());
  
          return $this->response->created(url('api/invoices/' . $invoice_id . '/payments' . $invoice_payment->id));
      }
  
      /**
       * Remove the specified resource from storage.
       *
       * @param  $invoice_id
       * @param  $id
       * @return \Dingo\Api\Http\Response
       */
      public function destroy($invoice_id, $id)
      {
          $invoice_payment = InvoicePayment::find($id);
  
          $invoice_payment->delete();
  
          return $this->response->noContent();
      }
  }