2
2
3
3
namespace App \Services ;
4
4
5
+ use App \Console \Commands \DoctorCommand ;
6
+ use App \Models \CashFlow ;
5
7
use App \Models \Customer ;
6
8
use App \Models \CustomerBillingAddress ;
7
9
use App \Models \CustomerShippingAddress ;
8
10
use App \Models \Option ;
11
+ use App \Models \Order ;
12
+ use App \Models \OrderProduct ;
9
13
use App \Models \Role ;
10
14
use App \Models \User ;
11
15
use App \Models \UserAttribute ;
12
16
use Exception ;
13
17
use Illuminate \Console \Command ;
18
+ use Illuminate \Support \Str ;
14
19
use Jackiedo \DotenvEditor \Facades \DotenvEditor ;
15
20
16
21
class DoctorService
@@ -46,19 +51,15 @@ public function restoreRoles()
46
51
$ rolesLabels = [
47
52
Role::ADMIN => [
48
53
'name ' => __ ( 'Administrator ' ),
49
- 'dashid ' => Role::DASHID_STORE ,
50
54
],
51
55
Role::STOREADMIN => [
52
56
'name ' => __ ( 'Store Administrator ' ),
53
- 'dashid ' => Role::DASHID_STORE ,
54
57
],
55
58
Role::STORECASHIER => [
56
59
'name ' => __ ( 'Store Cashier ' ),
57
- 'dashid ' => Role::DASHID_CASHIER ,
58
60
],
59
61
Role::USER => [
60
62
'name ' => __ ( 'User ' ),
61
- 'dashid ' => Role::DASHID_DEFAULT ,
62
63
],
63
64
];
64
65
@@ -72,7 +73,6 @@ public function restoreRoles()
72
73
$ role = new Role ;
73
74
$ role ->namespace = $ roleNamespace ;
74
75
$ role ->name = $ rolesLabels [ $ roleNamespace ][ 'name ' ];
75
- $ role ->dashid = $ rolesLabels [ $ roleNamespace ][ 'dashid ' ];
76
76
$ role ->locked = true ;
77
77
$ role ->save ();
78
78
}
@@ -96,6 +96,95 @@ public function fixDuplicateOptions()
96
96
});
97
97
}
98
98
99
+ public function fixOrphanOrderProducts ()
100
+ {
101
+ $ orderIds = Order::get ( 'id ' );
102
+
103
+ $ query = OrderProduct::whereNotIn ( 'order_id ' , $ orderIds );
104
+ $ total = $ query ->count ();
105
+ $ query ->delete ();
106
+
107
+ return sprintf ( __ ( '%s products were freed ' ), $ total );
108
+ }
109
+
110
+ /**
111
+ * useful to configure
112
+ * session domain and sanctum stateful domains
113
+ *
114
+ * @return void
115
+ */
116
+ public function fixDomains ()
117
+ {
118
+ /**
119
+ * Set version to close setup
120
+ */
121
+ $ domain = Str::replaceFirst ( 'http:// ' , '' , url ( '/ ' ) );
122
+ $ domain = Str::replaceFirst ( 'https:// ' , '' , $ domain );
123
+ $ domain = explode ( ': ' , $ domain )[0 ];
124
+
125
+ if ( ! env ( 'SESSION_DOMAIN ' , false ) ) {
126
+ DotenvEditor::load ();
127
+ DotenvEditor::setKey ( 'SESSION_DOMAIN ' , Str::replaceFirst ( 'http:// ' , '' , explode ( ': ' , $ domain )[0 ] ) );
128
+ DotenvEditor::save ();
129
+ }
130
+
131
+ if ( ! env ( 'SANCTUM_STATEFUL_DOMAINS ' , false ) ) {
132
+ DotenvEditor::load ();
133
+ DotenvEditor::setKey ( 'SANCTUM_STATEFUL_DOMAINS ' , collect ([ $ domain , 'localhost ' , '127.0.0.1 ' ])->unique ()->join (', ' ) );
134
+ DotenvEditor::save ();
135
+ }
136
+ }
137
+
138
+ /**
139
+ * clear current cash flow and recompute
140
+ * them using the current information.
141
+ */
142
+ public function fixCashFlowOrders ( DoctorCommand $ command )
143
+ {
144
+ /**
145
+ * @var ExpenseService $expenseService
146
+ */
147
+ $ expenseService = app ()->make ( ExpenseService::class );
148
+
149
+ CashFlow::where ( 'order_id ' , '> ' , 0 )->delete ();
150
+ CashFlow::where ( 'order_refund_id ' , '> ' , 0 )->delete ();
151
+
152
+ /**
153
+ * Step 1: Recompute from order sales
154
+ */
155
+ $ orders = Order::paymentStatus ( Order::PAYMENT_PAID )->get ();
156
+
157
+ $ command ->info ( __ ( 'Restoring cash flow from paid orders... ' ) );
158
+
159
+ $ command ->withProgressBar ( $ orders , function ( $ order ) use ( $ expenseService ) {
160
+ $ expenseService ->handleCreatedOrder ( $ order );
161
+ });
162
+
163
+ $ command ->newLine ();
164
+
165
+ /**
166
+ * Step 2: Recompute from refund
167
+ */
168
+ $ command ->info ( __ ( 'Restoring cash flow from refunded orders... ' ) );
169
+
170
+ $ orders = Order::paymentStatusIn ([
171
+ Order::PAYMENT_REFUNDED ,
172
+ Order::PAYMENT_PARTIALLY_REFUNDED
173
+ ])->get ();
174
+
175
+ $ command ->withProgressBar ( $ orders , function ( $ order ) use ( $ expenseService ) {
176
+ $ order ->refundedProducts ()->with ( 'orderProduct ' )->get ()->each ( function ( $ orderRefundedProduct ) use ( $ order , $ expenseService ) {
177
+ $ expenseService ->createExpenseFromRefund (
178
+ order: $ order ,
179
+ orderProductRefund: $ orderRefundedProduct ,
180
+ orderProduct: $ orderRefundedProduct ->orderProduct
181
+ );
182
+ });
183
+ });
184
+
185
+ $ command ->newLine ();
186
+ }
187
+
99
188
public function fixCustomers ()
100
189
{
101
190
$ this ->command
0 commit comments