|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Conversations; |
|
4
|
|
|
|
|
5
|
|
|
use App\Enum\OrderStatus as EnumOrderStatus; |
|
6
|
|
|
use App\Events\OrderCreated; |
|
7
|
|
|
use App\Models\Branch; |
|
8
|
|
|
use App\Models\Customer; |
|
9
|
|
|
use App\Models\Denomination; |
|
10
|
|
|
use App\Models\Item; |
|
11
|
|
|
use App\Models\Order; |
|
12
|
|
|
use App\Models\OrderStatus as ModelsOrderStatus; |
|
13
|
|
|
use App\Models\User; |
|
14
|
|
|
use BotMan\BotMan\Messages\Incoming\Answer; |
|
15
|
|
|
use BotMan\BotMan\Messages\Outgoing\Actions\Button; |
|
16
|
|
|
use BotMan\BotMan\Messages\Outgoing\Question; |
|
17
|
|
|
use BotMan\Drivers\Telegram\Extensions\Keyboard; |
|
18
|
|
|
use BotMan\Drivers\Telegram\Extensions\KeyboardButton; |
|
19
|
|
|
use Illuminate\Support\Collection; |
|
20
|
|
|
use Illuminate\Support\Facades\Event; |
|
21
|
|
|
use Illuminate\Support\Str; |
|
22
|
|
|
|
|
23
|
|
|
class ExchangeConversation extends Conversation |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Start the conversation. |
|
27
|
|
|
* |
|
28
|
|
|
* @return $this |
|
29
|
|
|
*/ |
|
30
|
|
|
public function run() |
|
31
|
|
|
{ |
|
32
|
|
|
if (User::where('telegram_chat_id', $this->getUser()->getId())->count()) { |
|
33
|
|
|
return $this->say('conversations.exchange.alert-user'); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
if (Order::isMaximumOrderPerDayExceeded()) { |
|
37
|
|
|
return $this->sayRenderable('conversations.exchange.alert-exceeded-maximum-order-per-day'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return $this |
|
41
|
|
|
->sayRenderable('conversations.exchange.index') |
|
42
|
|
|
->verifyCustomer(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Verify if the current customer has been registered or not. |
|
47
|
|
|
* |
|
48
|
|
|
* @return $this |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function verifyCustomer() |
|
51
|
|
|
{ |
|
52
|
|
|
if (!$customer = Customer::retrieveByBotManUser($this->getUser())) { |
|
53
|
|
|
$username = $this->getUser()->getUsername(); |
|
54
|
|
|
$email = $this->getUserStorage('email'); |
|
55
|
|
|
|
|
56
|
|
|
if (!$customer = Customer::retrieveByUsernameAndEmail(compact('username', 'email'))) { |
|
57
|
|
|
return $this |
|
|
|
|
|
|
58
|
|
|
->setPreviousConversation($this) |
|
59
|
|
|
->sayRenderable('conversations.exchange.alert-registration-first') |
|
60
|
|
|
->startConversation(new RegisterCustomerConversation); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $this->displayCustomerData($customer); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Reply with customer data. |
|
69
|
|
|
* |
|
70
|
|
|
* @param \App\Models\Customer $customer |
|
71
|
|
|
* @param string|null $validationErrorMessage |
|
72
|
|
|
* @return $this |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function displayCustomerData(Customer $customer, string $validationErrorMessage = null) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->displayValidationErrorMessage($validationErrorMessage); |
|
77
|
|
|
|
|
78
|
|
|
if (!$this->getPreviousConversation() instanceof static) { |
|
79
|
|
|
$this->say('<em>Data anda sebelumnya sudah pernah terekam di database kami.</em>'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$this->destroyUserStorage(forceDestroy: true); |
|
83
|
|
|
|
|
84
|
|
|
$question = Question::create(view('conversations.exchange.confirm-customer_data', compact('customer'))->render()) |
|
|
|
|
|
|
85
|
|
|
->callbackId('exchange_confirm_customer_data') |
|
86
|
|
|
->addButtons([ |
|
87
|
|
|
Button::create(view('conversations.register-customer.reply-customer_data-yes')->render())->value('yes'), |
|
|
|
|
|
|
88
|
|
|
Button::create(view('conversations.register-customer.reply-customer_data-no')->render())->value('no'), |
|
89
|
|
|
]); |
|
90
|
|
|
|
|
91
|
|
|
return $this->ask($question, next: function (Answer $answer) use ($customer) { |
|
92
|
|
|
if (!$answer->isInteractiveMessageReply()) { |
|
93
|
|
|
return; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if (!in_array($value = $answer->getValue(), ['yes', 'no'])) { |
|
97
|
|
|
return $this->displayCustomerData($customer, $this->fallbackMessage($answer->getText())); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if ($value === 'no') { |
|
101
|
|
|
$this->destroyUserStorage(); |
|
102
|
|
|
|
|
103
|
|
|
return $this |
|
|
|
|
|
|
104
|
|
|
->setPreviousConversation($this) |
|
105
|
|
|
->startConversation(new UpdateCustomerConversation); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $this->recordOrder($customer); |
|
109
|
|
|
}); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Record customer order data. |
|
114
|
|
|
* |
|
115
|
|
|
* @param \App\Models\Customer $customer |
|
116
|
|
|
* @param string|null $validationErrorMessage |
|
117
|
|
|
* @return $this |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function recordOrder(Customer $customer, string $validationErrorMessage = null) |
|
120
|
|
|
{ |
|
121
|
|
|
$this->displayValidationErrorMessage($validationErrorMessage); |
|
122
|
|
|
|
|
123
|
|
|
$denominations = Denomination::all('id', 'name', 'value'); |
|
124
|
|
|
$keyboard = Keyboard::create(Keyboard::TYPE_INLINE)->resizeKeyboard(); |
|
125
|
|
|
|
|
126
|
|
|
foreach ($denominations as $denomination) { |
|
127
|
|
|
$keyboard->addRow( |
|
128
|
|
|
KeyboardButton::create( |
|
129
|
|
|
view('conversations.exchange.reply-denomination', compact('denomination'))->render() |
|
130
|
|
|
)->callbackData($denomination->getKey()) |
|
131
|
|
|
); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$response = $this->reply( |
|
135
|
|
|
$question = view('conversations.exchange.alert-denomination')->render(), |
|
|
|
|
|
|
136
|
|
|
$additionalParameters = $keyboard->toArray() |
|
137
|
|
|
); |
|
138
|
|
|
|
|
139
|
|
|
return $this->getBot()->storeConversation($this, next: function (Answer $answer) use ($response, $customer, $denominations) { |
|
|
|
|
|
|
140
|
|
|
if (!$answer->isInteractiveMessageReply()) { |
|
141
|
|
|
return; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
$this->deleteTelegramMessageFromResponse($response); |
|
145
|
|
|
|
|
146
|
|
|
/** @var \App\Models\Denomination|null $denomination */ |
|
147
|
|
|
if (!$denominations->contains($answer->getValue()) || !$denomination = Denomination::find($answer->getValue())) { |
|
148
|
|
|
return $this->recordOrder($customer, $this->fallbackMessage($answer->getText())); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** @var \App\Models\Order $order */ |
|
152
|
|
|
$order = Order::findOrCreateFromCode($this->getUserStorage('order_code'), $customer, function (Order $order) { |
|
153
|
|
|
$this->setUserStorage(['order_code' => $order->code]); |
|
154
|
|
|
}); |
|
155
|
|
|
|
|
156
|
|
|
if ($order->isMaximumTotalOrderExceeded()) { |
|
157
|
|
|
return $this->confirmOrder($order, 'Maaf, total pesanan anda sudah mencapai batas maksimum'); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* If there is an order's item that has same denomination with the selected one, |
|
162
|
|
|
* then it will be deleted first before customer continue to record item. |
|
163
|
|
|
*/ |
|
164
|
|
|
if ($item = $order->items()->whereHasDenomination($denomination)->first('id')) { |
|
165
|
|
|
$item->delete(); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* If customer want to add another item, we assume that |
|
170
|
|
|
* the customer already choose the branch. |
|
171
|
|
|
*/ |
|
172
|
|
|
if ($order->branch && $order->has('items')->exists()) { |
|
173
|
|
|
return $this->recordItem($order, $denomination); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
return $this->recordBranch($order, $denomination); |
|
177
|
|
|
}, question: $question, additionalParameters: $additionalParameters); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Record customer branch data. |
|
182
|
|
|
* |
|
183
|
|
|
* @param \App\Models\Order $order |
|
184
|
|
|
* @param \App\Models\Denomination $denomination |
|
185
|
|
|
* @param string|null $validationErrorMessage |
|
186
|
|
|
* @return $this |
|
187
|
|
|
*/ |
|
188
|
|
|
protected function recordBranch(Order $order, Denomination $denomination, string $validationErrorMessage = null) |
|
189
|
|
|
{ |
|
190
|
|
|
$this->displayValidationErrorMessage($validationErrorMessage); |
|
191
|
|
|
|
|
192
|
|
|
$branches = Branch::all('id', 'name'); |
|
193
|
|
|
$keyboard = Keyboard::create(Keyboard::TYPE_INLINE)->resizeKeyboard(); |
|
194
|
|
|
|
|
195
|
|
|
foreach ($branches as $branch) { |
|
196
|
|
|
$keyboard->addRow( |
|
197
|
|
|
KeyboardButton::create( |
|
198
|
|
|
view('conversations.exchange.reply-branch', compact('branch'))->render() |
|
199
|
|
|
)->callbackData($branch->getKey()) |
|
200
|
|
|
); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
$keyboard->addRow( |
|
204
|
|
|
KeyboardButton::create( |
|
205
|
|
|
view('conversations.exchange.reply-branch-empty')->render() |
|
206
|
|
|
)->callbackData('branch_empty') |
|
207
|
|
|
); |
|
208
|
|
|
|
|
209
|
|
|
$response = $this->reply( |
|
210
|
|
|
$question = view('conversations.exchange.alert-branch')->render(), |
|
|
|
|
|
|
211
|
|
|
$additionalParameters = $keyboard->toArray() |
|
212
|
|
|
); |
|
213
|
|
|
|
|
214
|
|
|
return $this->getBot()->storeConversation($this, next: function (Answer $answer) use ($response, $order, $denomination, $branches) { |
|
|
|
|
|
|
215
|
|
|
if (!$answer->isInteractiveMessageReply()) { |
|
216
|
|
|
return; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
$this->deleteTelegramMessageFromResponse($response); |
|
220
|
|
|
|
|
221
|
|
|
/** @var \App\Models\Branch|null $branch */ |
|
222
|
|
|
if ((!$branches->contains($answer->getValue()) && $answer->getValue() !== 'branch_empty') || !$branch = Branch::find($answer->getValue())) { |
|
223
|
|
|
return $this->recordBranch($order, $denomination, $this->fallbackMessage($answer->getText())); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
if ($answer->getValue() !== 'branch_empty') { |
|
227
|
|
|
$order->setBranchRelationValue($branch)->save(); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
return $this->recordItem($order, $denomination); |
|
231
|
|
|
}, question: $question, additionalParameters: $additionalParameters); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Record customer item data. |
|
236
|
|
|
* |
|
237
|
|
|
* @param \App\Models\Order $order |
|
238
|
|
|
* @param \App\Models\Denomination $denomination |
|
239
|
|
|
* @param string|null $validationErrorMessage |
|
240
|
|
|
* @return $this |
|
241
|
|
|
*/ |
|
242
|
|
|
protected function recordItem(Order $order, Denomination $denomination, string $validationErrorMessage = null) |
|
243
|
|
|
{ |
|
244
|
|
|
$this->displayValidationErrorMessage($validationErrorMessage); |
|
245
|
|
|
|
|
246
|
|
|
$keyboard = Keyboard::create(Keyboard::TYPE_INLINE)->resizeKeyboard(); |
|
247
|
|
|
$unit = Str::lower($denomination->type->label); |
|
248
|
|
|
|
|
249
|
|
|
collect($denomination->range_order_bundle)->chunk(3)->map(function (Collection $quantities) use ($keyboard, $unit) { |
|
|
|
|
|
|
250
|
|
|
$keyboard->addRow(...$quantities->map(fn ($quantity) => KeyboardButton::create( |
|
251
|
|
|
view('conversations.exchange.reply-bundle_quantity-quantity', compact('quantity'))->render() |
|
252
|
|
|
)->callbackData($quantity))->toArray()); |
|
253
|
|
|
}); |
|
254
|
|
|
|
|
255
|
|
|
$keyboard->addRow( |
|
256
|
|
|
KeyboardButton::create( |
|
257
|
|
|
view('conversations.exchange.reply-bundle_quantity-custom')->render() |
|
258
|
|
|
)->callbackData('bundle_quantity_custom') |
|
259
|
|
|
); |
|
260
|
|
|
|
|
261
|
|
|
$response = $this->reply( |
|
262
|
|
|
$question = view('conversations.exchange.ask-bundle_quantity', compact('denomination'))->render(), |
|
|
|
|
|
|
263
|
|
|
$additionalParameters = $keyboard->toArray() |
|
264
|
|
|
); |
|
265
|
|
|
|
|
266
|
|
|
return $this->getBot()->storeConversation($this, next: function (Answer $answer) use ($response, $order, $denomination) { |
|
|
|
|
|
|
267
|
|
|
$this->deleteTelegramMessageFromResponse($response); |
|
268
|
|
|
|
|
269
|
|
|
if ($answer->getValue() === 'bundle_quantity_custom') { |
|
270
|
|
|
$this->deleteTelegramMessageFromResponse($response); |
|
271
|
|
|
|
|
272
|
|
|
$response2 = $this->reply( |
|
273
|
|
|
$question2 = view('conversations.exchange.ask-bundle_quantity-custom', compact('denomination'))->render(), |
|
|
|
|
|
|
274
|
|
|
$additionalParameters2 = Keyboard::create(Keyboard::TYPE_KEYBOARD) |
|
275
|
|
|
->resizeKeyboard() |
|
276
|
|
|
->oneTimeKeyboard() |
|
277
|
|
|
->addRow(KeyboardButton::create( |
|
278
|
|
|
$backToDenominationOption = trim(view('components.conversations.back', ['text' => 'opsi nominal'])->render()) |
|
|
|
|
|
|
279
|
|
|
)->callbackData('back_to_denomination_option'))->toArray() |
|
280
|
|
|
); |
|
281
|
|
|
|
|
282
|
|
|
return $this->getBot()->storeConversation($this, function (Answer $answer) use ($response2, $order, $denomination, $backToDenominationOption) { |
|
|
|
|
|
|
283
|
|
|
$this->deleteTelegramMessageFromResponse($response2); |
|
284
|
|
|
|
|
285
|
|
|
if ($answer->getText() === $backToDenominationOption) { |
|
286
|
|
|
return $this->recordItem($order, $denomination); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
return $this->createItem($order, $denomination, $answer->getText()); |
|
|
|
|
|
|
290
|
|
|
}, $question2, $additionalParameters2); |
|
|
|
|
|
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
return $this->createItem($order, $denomination, $answer->getValue()); |
|
|
|
|
|
|
294
|
|
|
}, question: $question, additionalParameters: $additionalParameters); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* Create an item data based on the given order, denomination, and value. |
|
299
|
|
|
* |
|
300
|
|
|
* @param \App\Models\Order $order |
|
301
|
|
|
* @param \App\Models\Denomination $denomination |
|
302
|
|
|
* @param int $value |
|
303
|
|
|
* @return $this |
|
304
|
|
|
*/ |
|
305
|
|
|
protected function createItem(Order $order, Denomination $denomination, int $value) |
|
306
|
|
|
{ |
|
307
|
|
|
if (!$denomination->isBetweenOrderBundle($value)) { |
|
308
|
|
|
return $this->recordItem($order, $denomination, trans('validation.between.numeric', [ |
|
|
|
|
|
|
309
|
|
|
'attribute' => trans('Quantity Per Bundle'), |
|
310
|
|
|
'min' => $denomination->minimum_order_bundle, |
|
311
|
|
|
'max' => $denomination->maximum_order_bundle, |
|
312
|
|
|
])); |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
$item = new Item([ |
|
316
|
|
|
'quantity_per_bundle' => $denomination->quantity_per_bundle, |
|
317
|
|
|
'bundle_quantity' => $value, |
|
318
|
|
|
]); |
|
319
|
|
|
|
|
320
|
|
|
$item->setDenominationRelationValue($denomination); |
|
321
|
|
|
|
|
322
|
|
|
$order->items()->save($item); |
|
323
|
|
|
|
|
324
|
|
|
return $this->confirmOrder($order); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
/** |
|
328
|
|
|
* Confirm customer order data. |
|
329
|
|
|
* |
|
330
|
|
|
* @param \App\Models\Order $order |
|
331
|
|
|
* @param string|null $validationErrorMessage |
|
332
|
|
|
* @return $this |
|
333
|
|
|
*/ |
|
334
|
|
|
protected function confirmOrder(Order $order, string $validationErrorMessage = null) |
|
335
|
|
|
{ |
|
336
|
|
|
$this->displayValidationErrorMessage($validationErrorMessage); |
|
337
|
|
|
|
|
338
|
|
|
$keyboard = Keyboard::create(Keyboard::TYPE_INLINE)->resizeKeyboard() |
|
339
|
|
|
->addRow(KeyboardButton::create(view('conversations.exchange.reply-order-yes')->render()) |
|
340
|
|
|
->callbackData('update_order_status') |
|
341
|
|
|
)->addRow(KeyboardButton::create(view('conversations.exchange.reply-order-no-update-item')->render()) |
|
342
|
|
|
->callbackData('update_item') |
|
343
|
|
|
)->addRow(KeyboardButton::create(view('conversations.exchange.reply-order-no-recreate-item')->render()) |
|
344
|
|
|
->callbackData('recreate_item') |
|
345
|
|
|
); |
|
346
|
|
|
|
|
347
|
|
|
$order->load('customer:id,fullname', 'items.denomination'); |
|
348
|
|
|
|
|
349
|
|
|
$responseConfirmOrder = $this->reply(view('conversations.exchange.confirm-order', compact('order'))->render()); |
|
|
|
|
|
|
350
|
|
|
|
|
351
|
|
|
$response = $this->reply( |
|
352
|
|
|
$question = view('conversations.exchange.ask-order')->render(), |
|
353
|
|
|
$additionalParameters = $keyboard->toArray() |
|
354
|
|
|
); |
|
355
|
|
|
|
|
356
|
|
|
return $this->getBot()->storeConversation($this, next: function (Answer $answer) use ($response, $responseConfirmOrder, $order) { |
|
|
|
|
|
|
357
|
|
|
$this->deleteTelegramMessageFromResponse($response); |
|
358
|
|
|
|
|
359
|
|
|
switch ($answer->getValue()) { |
|
360
|
|
|
case 'update_order_status': |
|
361
|
|
|
$order->statuses()->save(ModelsOrderStatus::make([ |
|
|
|
|
|
|
362
|
|
|
'status' => EnumOrderStatus::on_progress(), |
|
363
|
|
|
])->setIssuerableRelationValue($order->getCustomerRelationValue())); |
|
364
|
|
|
|
|
365
|
|
|
Event::dispatch(new OrderCreated($order)); |
|
366
|
|
|
|
|
367
|
|
|
return $this->sayRenderable('conversations.exchange.alert-update_order_status', compact('order')); |
|
368
|
|
|
|
|
369
|
|
|
case 'update_item': |
|
370
|
|
|
case 'recreate_item': |
|
371
|
|
|
$this->deleteTelegramMessageFromResponse($responseConfirmOrder); |
|
372
|
|
|
|
|
373
|
|
|
if ($answer->getValue() === 'recreate_item') { |
|
374
|
|
|
$order->items->map->delete(); |
|
375
|
|
|
} |
|
376
|
|
|
|
|
377
|
|
|
return $this->recordOrder($order->getCustomerRelationValue()); |
|
378
|
|
|
|
|
379
|
|
|
default: |
|
380
|
|
|
return $this->confirmOrder($order, $this->fallbackMessage($answer->getText())); |
|
381
|
|
|
} |
|
382
|
|
|
}, question: $question, additionalParameters: $additionalParameters); |
|
383
|
|
|
} |
|
384
|
|
|
} |
|
385
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.