Skip to content

Commit 3d9ef9b

Browse files
authored
Merge pull request #1198 from Blair2004/v4.8.x
V4.8.x
2 parents 8bfa97a + c140ec3 commit 3d9ef9b

File tree

6 files changed

+133
-22
lines changed

6 files changed

+133
-22
lines changed

app/Console/Commands/DoctorCommand.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ class DoctorCommand extends Command
1919
{--fix-users-attributes}
2020
{--fix-orders-products}
2121
{--fix-customers}
22+
{--fix-domains}
23+
{--fix-orphan-orders-products}
24+
{--fix-cash-flow-orders}
2225
{--fix-duplicate-options}';
2326

2427
/**
@@ -62,7 +65,7 @@ public function handle()
6265
if ( $this->option( 'fix-duplicate-options' ) ) {
6366
$doctorService->fixDuplicateOptions();
6467

65-
return $this->info( 'The duplicated options where cleared.' );
68+
return $this->info( 'The duplicated options were cleared.' );
6669
}
6770

6871
if ( $this->option( 'fix-customers' ) ) {
@@ -71,6 +74,20 @@ public function handle()
7174
return $this->info( 'The customers were fixed.' );
7275
}
7376

77+
if ( $this->option( 'fix-domains' ) ) {
78+
$doctorService->fixDomains();
79+
80+
return $this->info( 'The domain is correctly configured.' );
81+
}
82+
83+
if ( $this->option( 'fix-orphan-orders-products' ) ) {
84+
return $this->info( $doctorService->fixOrphanOrderProducts() );
85+
}
86+
87+
if ( $this->option( 'fix-cash-flow-orders' ) ) {
88+
return $doctorService->fixCashFlowOrders( $this );
89+
}
90+
7491
if ( $this->option( 'fix-orders-products' ) ) {
7592
$products = OrderProduct::where( 'total_purchase_price', 0 )->get();
7693

app/Services/DoctorService.php

Lines changed: 94 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@
22

33
namespace App\Services;
44

5+
use App\Console\Commands\DoctorCommand;
6+
use App\Models\CashFlow;
57
use App\Models\Customer;
68
use App\Models\CustomerBillingAddress;
79
use App\Models\CustomerShippingAddress;
810
use App\Models\Option;
11+
use App\Models\Order;
12+
use App\Models\OrderProduct;
913
use App\Models\Role;
1014
use App\Models\User;
1115
use App\Models\UserAttribute;
1216
use Exception;
1317
use Illuminate\Console\Command;
18+
use Illuminate\Support\Str;
1419
use Jackiedo\DotenvEditor\Facades\DotenvEditor;
1520

1621
class DoctorService
@@ -46,19 +51,15 @@ public function restoreRoles()
4651
$rolesLabels = [
4752
Role::ADMIN => [
4853
'name' => __( 'Administrator' ),
49-
'dashid' => Role::DASHID_STORE,
5054
],
5155
Role::STOREADMIN => [
5256
'name' => __( 'Store Administrator' ),
53-
'dashid' => Role::DASHID_STORE,
5457
],
5558
Role::STORECASHIER => [
5659
'name' => __( 'Store Cashier' ),
57-
'dashid' => Role::DASHID_CASHIER,
5860
],
5961
Role::USER => [
6062
'name' => __( 'User' ),
61-
'dashid' => Role::DASHID_DEFAULT,
6263
],
6364
];
6465

@@ -72,7 +73,6 @@ public function restoreRoles()
7273
$role = new Role;
7374
$role->namespace = $roleNamespace;
7475
$role->name = $rolesLabels[ $roleNamespace ][ 'name' ];
75-
$role->dashid = $rolesLabels[ $roleNamespace ][ 'dashid' ];
7676
$role->locked = true;
7777
$role->save();
7878
}
@@ -96,6 +96,95 @@ public function fixDuplicateOptions()
9696
});
9797
}
9898

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+
99188
public function fixCustomers()
100189
{
101190
$this->command

app/Services/ExpenseService.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -578,21 +578,23 @@ public function createExpenseFromRefund( Order $order, OrderProductRefund $order
578578
*/
579579
public function handleCreatedOrder( Order $order )
580580
{
581-
$expenseCategory = $this->getAccountTypeByCode( CashFlow::ACCOUNT_SALES );
582-
583-
$expense = new Expense;
584-
$expense->value = $order->total;
585-
$expense->active = true;
586-
$expense->operation = CashFlow::OPERATION_CREDIT;
587-
$expense->author = $order->author;
588-
$expense->order_id = $order->id;
589-
$expense->name = sprintf( __( 'Sale : %s' ), $order->code );
590-
$expense->id = 0; // this is not assigned to an existing expense
591-
$expense->category = $expenseCategory;
592-
$expense->created_at = $order->created_at;
593-
$expense->updated_at = $order->updated_at;
594-
595-
$this->recordCashFlowHistory( $expense );
581+
if ( $order->payment_status === Order::PAYMENT_PAID ) {
582+
$expenseCategory = $this->getAccountTypeByCode( CashFlow::ACCOUNT_SALES );
583+
584+
$expense = new Expense;
585+
$expense->value = $order->total;
586+
$expense->active = true;
587+
$expense->operation = CashFlow::OPERATION_CREDIT;
588+
$expense->author = $order->author;
589+
$expense->order_id = $order->id;
590+
$expense->name = sprintf( __( 'Sale : %s' ), $order->code );
591+
$expense->id = 0; // this is not assigned to an existing expense
592+
$expense->category = $expenseCategory;
593+
$expense->created_at = $order->created_at;
594+
$expense->updated_at = $order->updated_at;
595+
596+
$this->recordCashFlowHistory( $expense );
597+
}
596598
}
597599

598600
/**

app/Services/OrdersService.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2395,6 +2395,7 @@ public function orderTemplateMapping( $option, Order $order )
23952395
'cashier_name' => $order->user->username,
23962396
'cashier_id' => $order->author,
23972397
'order_code' => $order->code,
2398+
'order_type' => $this->getTypeLabel( $order->type ),
23982399
'order_date' => ns()->date->getFormatted( $order->created_at ),
23992400
'customer_name' => $order->customer->name,
24002401
'customer_email' => $order->customer->email,

app/Settings/invoice-settings/receipts.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
__( '{cashier_id}: displays the cashier id.' ), '<br>',
5252
__( '{order_code}: displays the order code.' ), '<br>',
5353
__( '{order_date}: displays the order date.' ), '<br>',
54+
__( '{order_type}: displays the order type.' ), '<br>',
5455
__( '{customer_name}: displays the customer name.' ), '<br>',
5556
__( '{customer_email}: displays the customer email.' ), '<br>',
5657
__( '{shipping_name}: displays the shipping name.' ), '<br>',
@@ -88,6 +89,7 @@
8889
__( '{cashier_id}: displays the cashier id.' ) . '<br>',
8990
__( '{order_code}: displays the order code.' ) . '<br>',
9091
__( '{order_date}: displays the order date.' ) . '<br>',
92+
__( '{order_type}: displays the order type.' ), '<br>',
9193
__( '{customer_name}: displays the customer name.' ) . '<br>',
9294
__( '{customer_email}: displays the customer email.' ) . '<br>',
9395
__( '{shipping_name}: displays the shipping name.' ) . '<br>',

config/nexopos.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
return [
4-
'version' => '4.8.9',
4+
'version' => '4.8.10',
55
'languages' => [
66
'en' => 'English',
77
'fr' => 'Français',

0 commit comments

Comments
 (0)