Blame view

database/migrations/2017_10_11_000000_create_invoice_totals_table.php 4.41 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
127
128
129
130
131
132
133
134
135
136
137
138
139
  <?php
  
  use Illuminate\Database\Migrations\Migration;
  use Illuminate\Database\Schema\Blueprint;
  use App\Models\Model;
  use App\Models\Company\Company;
  use App\Models\Income\Invoice;
  use App\Models\Income\InvoiceItem;
  use App\Models\Income\InvoiceTotal;
  use App\Models\Setting\Tax;
  
  class CreateInvoiceTotalsTable extends Migration
  {
      /**
       * Run the migrations.
       *
       * @return void
       */
      public function up()
      {
          Schema::create('invoice_totals', function (Blueprint $table) {
              $table->increments('id');
              $table->integer('company_id');
              $table->integer('invoice_id');
              $table->string('code')->nullable();
              $table->string('name');
              $table->double('amount', 15, 4);
              $table->integer('sort_order');
              $table->timestamps();
              $table->softDeletes();
  
              $table->index('company_id');
          });
  
          Model::unguard();
  
          $companies = Company::all();
  
          foreach ($companies as $company) {
              $invoices = Invoice::where('company_id', $company->id)->get();
  
              foreach ($invoices as $invoice) {
                  $invoice_items = InvoiceItem::where('company_id', $company->id)->where('invoice_id', $invoice->id)->get();
  
                  $taxes = [];
                  $tax_total = 0;
                  $sub_total = 0;
  
                  foreach ($invoice_items as $invoice_item) {
                      unset($tax_object);
  
                      $invoice_item->total = $invoice_item->price * $invoice_item->quantity;
  
                      if (!empty($invoice_item->tax_id)) {
                          $tax_object = Tax::where('company_id', $company->id)->where('id', $invoice_item->tax_id)->first();
  
                          $invoice_item->tax = (($invoice_item->price * $invoice_item->quantity) / 100) * $tax_object->rate;
                      }
  
                      $invoice_item->update();
  
                      if (isset($tax_object)) {
                          if (array_key_exists($invoice_item->tax_id, $taxes)) {
                              $taxes[$invoice_item->tax_id]['amount'] += $invoice_item->tax;
                          } else {
                              $taxes[$invoice_item->tax_id] = [
                                  'name' => $tax_object->name,
                                  'amount' => $invoice_item->tax
                              ];
                          }
                      }
  
                      $tax_total += $invoice_item->tax;
                      $sub_total += $invoice_item->price * $invoice_item->quantity;
                  }
  
                  $invoice->amount = $sub_total + $tax_total;
  
                  $invoice->update();
  
                  // Added invoice total sub total
                  $invoice_sub_total = [
                      'company_id' => $company->id,
                      'invoice_id' => $invoice->id,
                      'code' => 'sub_total',
                      'name' => 'invoices.sub_total',
                      'amount' => $sub_total,
                      'sort_order' => 1,
                  ];
  
                  InvoiceTotal::create($invoice_sub_total);
  
                  $sort_order = 2;
  
                  // Added invoice total taxes
                  if ($taxes) {
                      foreach ($taxes as $tax) {
                          $invoice_tax_total = [
                              'company_id' => $company->id,
                              'invoice_id' => $invoice->id,
                              'code' => 'tax',
                              'name' => $tax['name'],
                              'amount' => $tax['amount'],
                              'sort_order' => $sort_order,
                          ];
  
                          InvoiceTotal::create($invoice_tax_total);
  
                          $sort_order++;
                      }
                  }
  
                  // Added invoice total total
                  $invoice_total = [
                      'company_id' => $company->id,
                      'invoice_id' => $invoice->id,
                      'code' => 'total',
                      'name' => 'invoices.total',
                      'amount' => $sub_total + $tax_total,
                      'sort_order' => $sort_order,
                  ];
  
                  InvoiceTotal::create($invoice_total);
              }
          }
  
          Model::reguard();
      }
  
      /**
       * Reverse the migrations.
       *
       * @return void
       */
      public function down()
      {
          Schema::drop('invoice_totals');
      }
  }