-
-
Notifications
You must be signed in to change notification settings - Fork 151
/
TransactionEntryTest.php
296 lines (234 loc) · 10.5 KB
/
TransactionEntryTest.php
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
namespace Tests\Feature;
use App\Cart\CartCollection;
use App\Cart\CashDraft;
use App\Cart\CreditDraft;
use App\Cart\Item;
use App\Product;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\BrowserKitTestCase;
class TransactionEntryTest extends BrowserKitTestCase
{
use DatabaseMigrations;
/** @test */
public function user_can_visit_transaction_drafts_page()
{
$this->loginAsUser();
// Add new draft to collection
$cart = new CartCollection();
$draft = $cart->add(new CashDraft());
$this->visit(route('cart.index'));
$this->assertViewHas('draft', $draft);
$this->see($draft->type);
}
/** @test */
public function user_can_create_transaction_draft_by_transaction_create_button()
{
$this->loginAsUser();
$this->visit(route('home'));
$this->press(trans('transaction.create'));
$cart = new CartCollection();
$draft = $cart->content()->last();
$this->seePageIs(route('cart.show', $draft->draftKey));
$this->press(trans('transaction.create_credit'));
$cart = new CartCollection();
$draft = $cart->content()->last();
$this->seePageIs(route('cart.show', $draft->draftKey));
}
/** @test */
public function user_can_search_product_on_transaction_draft_page()
{
$product = factory(Product::class)->create(['name' => 'Testing Produk 1']);
$this->loginAsUser();
$cart = new CartCollection();
$draft = new CreditDraft();
$cart->add($draft);
// Visit cart index page
$this->visit(route('cart.index'));
// Visit search for products
$this->submitForm(trans('cart.product_search'), [
'query' => 'testing',
]);
$this->seePageIs(route('cart.show', [$draft->draftKey, 'query' => 'testing']));
// See product list appears
$this->see($product->name);
$this->see(format_rp($product->credit_price));
$this->seeElement('form', ['action' => route('cart.add-draft-item', [$draft->draftKey, $product->id])]);
$this->seeElement('input', ['id' => 'qty-'.$product->id, 'name' => 'qty']);
$this->seeElement('input', ['id' => 'add-product-'.$product->id]);
$this->dontSee($product->cash_price);
}
/** @test */
public function user_can_add_item_to_cash_draft()
{
$product1 = factory(Product::class)->create(['name' => 'Testing Produk 1', 'cash_price' => 400, 'credit_price' => 500]);
$product2 = factory(Product::class)->create(['name' => 'Testing Produk 2', 'cash_price' => 1000, 'credit_price' => 1100]);
$this->loginAsUser();
$cart = new CartCollection();
$draft = new CashDraft();
$cart->add($draft);
// Visit cart index with searched item
$this->visit(route('cart.show', [$draft->draftKey, 'query' => 'testing']));
$this->type(2, 'qty');
$this->press('add-product-'.$product1->id);
// $this->see(trans('cart.item_added', [
// 'product_name' => $product1->name.' ('.$product1->unit->name.')',
// 'qty' => 2,
// ]));
$this->type(3, 'qty');
$this->press('add-product-'.$product2->id);
// $this->see(trans('cart.item_added', [
// 'product_name' => $product2->name.' ('.$product2->unit->name.')',
// 'qty' => 3,
// ]));
$this->seePageIs(route('cart.show', [$draft->draftKey, 'query' => 'testing']));
$this->assertTrue($cart->draftHasItem($draft, $product1));
$this->assertTrue($cart->draftHasItem($draft, $product2));
$this->assertEquals(3800, $draft->getTotal());
$this->seeElement('input', ['id' => 'qty-'. 0]);
$this->seeElement('input', ['id' => 'item_discount-'. 0]);
$this->seeElement('button', ['id' => 'remove-item-'. 0]);
$this->see(format_rp(3800));
}
/** @test */
public function user_can_add_item_to_credit_draft()
{
$product1 = factory(Product::class)->create(['name' => 'Testing Produk 1', 'cash_price' => 400, 'credit_price' => 500]);
$product2 = factory(Product::class)->create(['name' => 'Testing Produk 2', 'cash_price' => 1000, 'credit_price' => 1100]);
$this->loginAsUser();
$cart = new CartCollection();
$draft = new CreditDraft();
$cart->add($draft);
// Visit cart index with searched item
$this->visit(route('cart.show', [$draft->draftKey, 'query' => 'testing']));
$this->type(2, 'qty');
$this->press('add-product-'.$product1->id);
$this->type(3, 'qty');
$this->press('add-product-'.$product2->id);
$this->seePageIs(route('cart.show', [$draft->draftKey, 'query' => 'testing']));
$this->assertTrue($cart->draftHasItem($draft, $product1));
$this->assertTrue($cart->draftHasItem($draft, $product2));
$this->assertEquals(4300, $draft->getTotal());
}
/** @test */
public function user_can_update_item_qty()
{
$cart = new CartCollection();
$draft = $cart->add(new CashDraft());
$product1 = factory(Product::class)->create(['cash_price' => 1000]);
$product2 = factory(Product::class)->create(['cash_price' => 2000]);
$item1 = new Item($product1, 1);
$item2 = new Item($product2, 3);
// Add items to draft
$cart->addItemToDraft($draft->draftKey, $item1);
$cart->addItemToDraft($draft->draftKey, $item2);
$this->loginAsUser();
$this->visit(route('cart.show', $draft->draftKey));
$this->submitForm('update-item-0', [
'item_key' => 0,
'qty' => 2,
'item_discount' => 100,
]);
$this->submitForm('update-item-1', [
'item_key' => 1,
'qty' => 2,
'item_discount' => 100,
]);
$this->assertEquals(400, $draft->getDiscountTotal());
$this->assertEquals(6000, $draft->getSubtotal());
$this->assertEquals(5600, $draft->getTotal());
$this->see(format_rp($draft->getSubtotal()));
$this->see(format_rp($draft->getTotal()));
}
/** @test */
public function user_can_update_draft_transaction_detail_and_get_confirm_page()
{
$cart = new CartCollection();
$draft = $cart->add(new CashDraft());
$product1 = factory(Product::class)->create(['cash_price' => 1000]);
$product2 = factory(Product::class)->create(['cash_price' => 2000]);
$item1 = new Item($product1, 1);
$item2 = new Item($product2, 3);
// Add items to draft
$cart->addItemToDraft($draft->draftKey, $item1);
$cart->addItemToDraft($draft->draftKey, $item2);
$this->loginAsUser();
$this->visit(route('cart.show', $draft->draftKey));
$this->type('Nafies', 'customer[name]');
$this->type('-', 'customer[phone]');
$this->type('catatan', 'notes');
$this->type(10000, 'payment');
$this->press(trans('transaction.proccess'));
$this->seePageIs(route('cart.show', [$draft->draftKey, 'action' => 'confirm']));
$this->see(trans('transaction.confirm'));
$this->see($draft->customer['name']);
$this->see($draft->customer['phone']);
$this->see($draft->notes);
$this->see(format_rp(10000));
$this->see(format_rp(3000));
$this->seeElement('input', ['id' => 'save-transaction-draft']);
}
/** @test */
public function it_validates_proper_payment_amount()
{
$cart = new CartCollection();
$draft = $cart->add(new CashDraft());
$product1 = factory(Product::class)->create(['cash_price' => 100000]);
$product2 = factory(Product::class)->create(['cash_price' => 50000]);
$item1 = new Item($product1, 1);
$item2 = new Item($product2, 3);
// Add items to draft
$cart->addItemToDraft($draft->draftKey, $item1);
$cart->addItemToDraft($draft->draftKey, $item2);
$this->loginAsUser();
$this->visit(route('cart.show', $draft->draftKey));
$this->type('Nafies', 'customer[name]');
$this->type('-', 'customer[phone]');
$this->type('catatan', 'notes');
$this->type(100000, 'payment');
$this->press(trans('transaction.proccess'));
$this->dontSee(trans('transaction.confirm'));
$this->type(350001, 'payment');
$this->press(trans('transaction.proccess'));
$this->dontSee(trans('transaction.confirm'));
$this->type(350000, 'payment');
$this->press(trans('transaction.proccess'));
$this->see(trans('transaction.confirm'));
}
/** @test */
public function user_can_save_transaction_if_draft_is_completed()
{
$cart = new CartCollection();
$draft = $cart->add(new CashDraft());
$product1 = factory(Product::class)->create(['cash_price' => 1000]);
$product2 = factory(Product::class)->create(['cash_price' => 2000]);
$item1 = new Item($product1, 1);
$item2 = new Item($product2, 3);
// Add items to draft
$cart->addItemToDraft($draft->draftKey, $item1);
$cart->addItemToDraft($draft->draftKey, $item2);
$draftAttributes = [
'customer' => [
'name' => 'Nafies',
'phone' => '081234567890',
],
'payment' => 10000,
'notes' => 'Catatan',
];
$cart->updateDraftAttributes($draft->draftKey, $draftAttributes);
$user = $this->loginAsUser();
$this->visit(route('cart.show', [$draft->draftKey, 'action' => 'confirm']));
$this->press(trans('transaction.save'));
$this->seePageIs(route('transactions.show', date('ym').'0001'));
// $this->see(trans('transaction.created', ['invoice_no' => date('ym').'0001']));
$this->seeInDatabase('transactions', [
'invoice_no' => date('ym').'0001',
'items' => '[{"id":'.$product1->id.',"name":"'.$product1->name.'","unit":"'.$product1->unit->name.'","price":1000,"qty":1,"item_discount":0,"item_discount_subtotal":0,"subtotal":1000},{"id":'.$product2->id.',"name":"'.$product2->name.'","unit":"'.$product2->unit->name.'","price":2000,"qty":3,"item_discount":0,"item_discount_subtotal":0,"subtotal":6000}]',
'customer' => '{"name":"Nafies","phone":"081234567890"}',
'payment' => 10000,
'total' => 7000,
'notes' => 'Catatan',
'user_id' => $user->id,
]);
}
}