-
Notifications
You must be signed in to change notification settings - Fork 526
Expand file tree
/
Copy pathOrderController.cs
More file actions
1006 lines (826 loc) · 39.2 KB
/
OrderController.cs
File metadata and controls
1006 lines (826 loc) · 39.2 KB
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Grand.Business.Core.Commands.Checkout.Orders;
using Grand.Business.Core.Interfaces.Catalog.Products;
using Grand.Business.Core.Interfaces.Checkout.Orders;
using Grand.Business.Core.Interfaces.Checkout.Shipping;
using Grand.Business.Core.Interfaces.Common.Addresses;
using Grand.Business.Core.Interfaces.Common.Directory;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Business.Core.Interfaces.Common.Pdf;
using Grand.Business.Core.Interfaces.ExportImport;
using Grand.Domain.Permissions;
using Grand.Domain.Catalog;
using Grand.Domain.Common;
using Grand.Domain.Orders;
using Grand.Infrastructure;
using Grand.Web.Admin.Extensions;
using Grand.Web.Admin.Interfaces;
using Grand.Web.Admin.Models.Orders;
using Grand.Web.Common.DataSource;
using Grand.Web.Common.Security.Authorization;
using MediatR;
using Microsoft.AspNetCore.Mvc;
namespace Grand.Web.Admin.Controllers;
[PermissionAuthorize(PermissionSystemName.Orders)]
public class OrderController(
IOrderViewModelService orderViewModelService,
IOrderService orderService,
IOrderStatusService orderStatusService,
ITranslationService translationService,
IContextAccessor contextAccessor,
IPdfService pdfService,
IGroupService groupService,
IExportManager<Order> exportManager,
IMediator mediator)
: BaseAdminController
{
#region Utilities
protected virtual async Task<bool> CheckSalesManager(Order order)
{
return await groupService.IsSalesManager(contextAccessor.WorkContext.CurrentCustomer)
&& contextAccessor.WorkContext.CurrentCustomer.SeId != order.SeId;
}
#endregion
#region Fields
#endregion
#region Ctor
#endregion
#region Order list
public IActionResult Index()
{
return RedirectToAction("List");
}
public async Task<IActionResult> List(int? orderStatusId = null,
int? paymentStatusId = null, int? shippingStatusId = null, DateTime? startDate = null, string code = null)
{
var model = await orderViewModelService.PrepareOrderListModel(orderStatusId, paymentStatusId, shippingStatusId,
startDate, contextAccessor.WorkContext.CurrentCustomer.StaffStoreId, code);
return View(model);
}
public async Task<IActionResult> ProductSearchAutoComplete(string term,
[FromServices] IProductService productService)
{
const int searchTermMinimumLength = 3;
if (string.IsNullOrWhiteSpace(term) || term.Length < searchTermMinimumLength)
return Content("");
var storeId = string.Empty;
if (await groupService.IsStaff(contextAccessor.WorkContext.CurrentCustomer))
storeId = contextAccessor.WorkContext.CurrentCustomer.StaffStoreId;
//products
const int productNumber = 15;
var products = (await productService.SearchProducts(
storeId: storeId,
keywords: term,
pageSize: productNumber,
showHidden: true)).products;
var result = (from p in products
select new {
label = p.Name,
productid = p.Id
})
.ToList();
return Json(result);
}
[PermissionAuthorizeAction(PermissionActionName.List)]
[HttpPost]
public async Task<IActionResult> OrderList(DataSourceRequest command, OrderListModel model)
{
if (await groupService.IsStaff(contextAccessor.WorkContext.CurrentCustomer))
model.StoreId = contextAccessor.WorkContext.CurrentCustomer.StaffStoreId;
var (orderModels, totalCount) =
await orderViewModelService.PrepareOrderModel(model, command.Page, command.PageSize);
var gridModel = new DataSourceResult {
Data = orderModels.ToList(),
Total = totalCount
};
return Json(gridModel);
}
[PermissionAuthorizeAction(PermissionActionName.Preview)]
[HttpPost]
public async Task<IActionResult> GoToOrderId(OrderListModel model)
{
Order order = null;
int.TryParse(model.GoDirectlyToNumber, out var orderNumber);
if (orderNumber > 0) order = await orderService.GetOrderByNumber(orderNumber);
var orders = await orderService.GetOrdersByCode(model.GoDirectlyToNumber);
switch (orders.Count)
{
case > 1:
return RedirectToAction("List", new { Code = model.GoDirectlyToNumber });
case 1:
order = orders.FirstOrDefault();
break;
}
if (order == null || await CheckSalesManager(order))
return RedirectToAction("List");
if (await groupService.IsStaff(contextAccessor.WorkContext.CurrentCustomer) &&
order.StoreId != contextAccessor.WorkContext.CurrentCustomer.StaffStoreId) return RedirectToAction("List");
return RedirectToAction("Edit", "Order", new { id = order.Id });
}
#endregion
#region Export
[PermissionAuthorizeAction(PermissionActionName.Export)]
[HttpPost]
public async Task<IActionResult> ExportExcelAll(OrderListModel model)
{
if (await groupService.IsStaff(contextAccessor.WorkContext.CurrentCustomer))
model.StoreId = contextAccessor.WorkContext.CurrentCustomer.StaffStoreId;
//load orders
var orders = await orderViewModelService.PrepareOrders(model);
try
{
var bytes = await exportManager.Export(orders);
return File(bytes, "text/xls", "orders.xlsx");
}
catch (Exception exc)
{
Error(exc);
return RedirectToAction("List");
}
}
[PermissionAuthorizeAction(PermissionActionName.Export)]
[HttpPost]
public async Task<IActionResult> ExportExcelSelected(string selectedIds)
{
var orders = new List<Order>();
if (selectedIds != null)
{
var ids = selectedIds
.Split([','], StringSplitOptions.RemoveEmptyEntries)
.Select(x => x)
.ToArray();
orders.AddRange(await orderService.GetOrdersByIds(ids));
}
if (await groupService.IsStaff(contextAccessor.WorkContext.CurrentCustomer))
orders = orders.Where(x => x.StoreId == contextAccessor.WorkContext.CurrentCustomer.StaffStoreId).ToList();
var bytes = await exportManager.Export(orders);
return File(bytes, "text/xls", "orders.xlsx");
}
#endregion
#region Order details
#region Payments and other order workflow
[PermissionAuthorizeAction(PermissionActionName.Cancel)]
[HttpGet]
public async Task<IActionResult> CancelOrder(string id)
{
var order = await orderService.GetOrderById(id);
if (order == null || await CheckSalesManager(order))
//No order found with the specified id
return RedirectToAction("List");
if (await groupService.IsStaff(contextAccessor.WorkContext.CurrentCustomer) &&
order.StoreId != contextAccessor.WorkContext.CurrentCustomer.StaffStoreId) return RedirectToAction("List");
try
{
await mediator.Send(new CancelOrderCommand { Order = order, NotifyCustomer = true });
Success("Successfully canceled order");
return RedirectToAction("Edit", "Order", new { id });
}
catch (Exception exc)
{
//error
Error(exc);
return RedirectToAction("Edit", "Order", new { id });
}
}
[PermissionAuthorizeAction(PermissionActionName.Edit)]
[HttpPost]
public async Task<IActionResult> SaveOrderTags(OrderModel orderModel)
{
var order = await orderService.GetOrderById(orderModel.Id);
if (order == null || await CheckSalesManager(order))
//No order found with the specified id
return RedirectToAction("List");
if (await groupService.IsStaff(contextAccessor.WorkContext.CurrentCustomer) &&
order.StoreId != contextAccessor.WorkContext.CurrentCustomer.StaffStoreId) return RedirectToAction("List");
try
{
await orderViewModelService.SaveOrderTags(order, orderModel.OrderTags);
var model = new OrderModel();
await orderViewModelService.PrepareOrderDetailsModel(model, order);
return RedirectToAction("Edit", "Order", new { id = order.Id });
}
catch (Exception exception)
{
//error
Error(exception, false);
return RedirectToAction("Edit", "Order", new { id = order.Id });
}
}
[PermissionAuthorizeAction(PermissionActionName.Edit)]
[HttpPost]
public async Task<IActionResult> ChangeOrderStatus(string id, OrderModel model)
{
var order = await orderService.GetOrderById(id);
if (order == null || await CheckSalesManager(order))
//No order found with the specified id
return RedirectToAction("List");
if (await groupService.IsStaff(contextAccessor.WorkContext.CurrentCustomer) &&
order.StoreId != contextAccessor.WorkContext.CurrentCustomer.StaffStoreId) return RedirectToAction("List");
try
{
var status = await orderStatusService.GetByStatusId(model.OrderStatusId);
ArgumentNullException.ThrowIfNull(status);
order.OrderStatusId = model.OrderStatusId;
await orderService.UpdateOrder(order);
//add a note
await orderService.InsertOrderNote(new OrderNote {
Note = $"Order status has been edited. New status: {status.Name}",
DisplayToCustomer = false,
OrderId = order.Id
});
model = new OrderModel();
await orderViewModelService.PrepareOrderDetailsModel(model, order);
return RedirectToAction("Edit", "Order", new { id });
}
catch (Exception exc)
{
//error
Error(exc, false);
return RedirectToAction("Edit", "Order", new { id });
}
}
#endregion
#region Edit, delete
[PermissionAuthorizeAction(PermissionActionName.Preview)]
public async Task<IActionResult> Edit(string id)
{
var order = await orderService.GetOrderById(id);
if (order == null || order.Deleted || await CheckSalesManager(order))
//No order found with the specified id
return RedirectToAction("List");
if (await groupService.IsStaff(contextAccessor.WorkContext.CurrentCustomer) &&
order.StoreId != contextAccessor.WorkContext.CurrentCustomer.StaffStoreId) return RedirectToAction("List");
var model = new OrderModel();
await orderViewModelService.PrepareOrderDetailsModel(model, order);
return View(model);
}
[PermissionAuthorizeAction(PermissionActionName.Delete)]
[HttpPost]
public async Task<IActionResult> Delete(OrderDeleteModel model)
{
var order = await orderService.GetOrderById(model.Id);
if (order == null || await CheckSalesManager(order))
//No order found with the specified id
return RedirectToAction("List");
if (ModelState.IsValid)
{
await mediator.Send(new DeleteOrderCommand { Order = order });
return RedirectToAction("List");
}
Error(ModelState);
return RedirectToAction("Edit", "Order", new { model.Id });
}
[PermissionAuthorizeAction(PermissionActionName.Delete)]
[HttpPost]
public async Task<IActionResult> DeleteSelected(
ICollection<string> selectedIds,
[FromServices] IShipmentService shipmentService)
{
if (await groupService.IsStaff(contextAccessor.WorkContext.CurrentCustomer))
return RedirectToAction("List", "Order");
if (selectedIds != null)
{
var orders = new List<Order>();
orders.AddRange(await orderService.GetOrdersByIds(selectedIds.ToArray()));
for (var i = 0; i < orders.Count; i++)
{
var order = orders[i];
var shipments = await shipmentService.GetShipmentsByOrder(order.Id);
if (shipments.Any())
Error("Some orders is in associated with shipments. Please delete it first.");
if (!shipments.Any()) await mediator.Send(new DeleteOrderCommand { Order = order });
}
}
return Json(new { Result = true });
}
public async Task<IActionResult> PdfInvoice(string orderId)
{
var order = await orderService.GetOrderById(orderId);
if ((await groupService.IsStaff(contextAccessor.WorkContext.CurrentCustomer) &&
order.StoreId != contextAccessor.WorkContext.CurrentCustomer.StaffStoreId) ||
await CheckSalesManager(order)) return RedirectToAction("List");
var orders = new List<Order> {
order
};
byte[] bytes;
using (var stream = new MemoryStream())
{
await pdfService.PrintOrdersToPdf(stream, orders, contextAccessor.WorkContext.WorkingLanguage.Id);
bytes = stream.ToArray();
}
return File(bytes, "application/pdf", $"order_{order.Id}.pdf");
}
[PermissionAuthorizeAction(PermissionActionName.Export)]
[HttpPost]
public async Task<IActionResult> PdfInvoiceAll(OrderListModel model)
{
//load orders
var orders = await orderViewModelService.PrepareOrders(model);
if (await groupService.IsStaff(contextAccessor.WorkContext.CurrentCustomer))
orders = orders.Where(x => x.StoreId == contextAccessor.WorkContext.CurrentCustomer.StaffStoreId).ToList();
byte[] bytes;
using (var stream = new MemoryStream())
{
await pdfService.PrintOrdersToPdf(stream, orders, contextAccessor.WorkContext.WorkingLanguage.Id, model.VendorId);
bytes = stream.ToArray();
}
return File(bytes, "application/pdf", "orders.pdf");
}
[PermissionAuthorizeAction(PermissionActionName.Export)]
[HttpPost]
public async Task<IActionResult> PdfInvoiceSelected(string selectedIds)
{
var orders = new List<Order>();
if (selectedIds != null)
{
var ids = selectedIds
.Split([','], StringSplitOptions.RemoveEmptyEntries)
.Select(x => x)
.ToArray();
orders.AddRange(await orderService.GetOrdersByIds(ids));
}
if (await groupService.IsStaff(contextAccessor.WorkContext.CurrentCustomer))
orders = orders.Where(x => x.StoreId == contextAccessor.WorkContext.CurrentCustomer.StaffStoreId).ToList();
//ensure that we at least one order selected
if (orders.Count == 0)
{
Error(translationService.GetResource("Admin.Orders.PdfInvoice.NoOrders"));
return RedirectToAction("List");
}
byte[] bytes;
using (var stream = new MemoryStream())
{
await pdfService.PrintOrdersToPdf(stream, orders, contextAccessor.WorkContext.WorkingLanguage.Id);
bytes = stream.ToArray();
}
return File(bytes, "application/pdf", "orders.pdf");
}
[PermissionAuthorizeAction(PermissionActionName.Edit)]
[HttpPost]
public async Task<IActionResult> EditOrderTotals(string id, OrderModel model)
{
var order = await orderService.GetOrderById(id);
if (order == null || await CheckSalesManager(order))
//No order found with the specified id
return RedirectToAction("List");
if (await groupService.IsStaff(contextAccessor.WorkContext.CurrentCustomer) &&
order.StoreId != contextAccessor.WorkContext.CurrentCustomer.StaffStoreId)
return RedirectToAction("Edit", "Order", new { id });
order.OrderSubtotalInclTax = model.OrderSubtotalInclTaxValue;
order.OrderSubtotalExclTax = model.OrderSubtotalExclTaxValue;
order.OrderSubTotalDiscountInclTax = model.OrderSubTotalDiscountInclTaxValue;
order.OrderSubTotalDiscountExclTax = model.OrderSubTotalDiscountExclTaxValue;
order.OrderShippingInclTax = model.OrderShippingInclTaxValue;
order.OrderShippingExclTax = model.OrderShippingExclTaxValue;
order.PaymentMethodAdditionalFeeInclTax = model.PaymentMethodAdditionalFeeInclTaxValue;
order.PaymentMethodAdditionalFeeExclTax = model.PaymentMethodAdditionalFeeExclTaxValue;
order.OrderTax = model.TaxValue;
order.OrderDiscount = model.OrderTotalDiscountValue;
order.OrderTotal = model.OrderTotalValue;
order.CurrencyRate = model.CurrencyRate;
await orderService.UpdateOrder(order);
//add a note
await orderService.InsertOrderNote(new OrderNote {
Note = "Order totals have been edited",
DisplayToCustomer = false,
OrderId = order.Id
});
await orderViewModelService.PrepareOrderDetailsModel(model, order);
return RedirectToAction("Edit", "Order", new { id });
}
[PermissionAuthorizeAction(PermissionActionName.Edit)]
[HttpPost]
public async Task<IActionResult> EditShippingMethod(string id, OrderModel model)
{
var order = await orderService.GetOrderById(id);
if (order == null || await CheckSalesManager(order))
//No order found with the specified id
return RedirectToAction("List");
if (await groupService.IsStaff(contextAccessor.WorkContext.CurrentCustomer) &&
order.StoreId != contextAccessor.WorkContext.CurrentCustomer.StaffStoreId)
return RedirectToAction("Edit", "Order", new { id });
order.ShippingMethod = model.ShippingMethod;
await orderService.UpdateOrder(order);
//add a note
await orderService.InsertOrderNote(new OrderNote {
Note = "Shipping method has been edited",
DisplayToCustomer = false,
OrderId = order.Id
});
await orderViewModelService.PrepareOrderDetailsModel(model, order);
//selected tab
await SaveSelectedTabIndex(persistForTheNextRequest: true);
return RedirectToAction("Edit", "Order", new { id });
}
[HttpPost]
public async Task<IActionResult> EditUserFields(string id, OrderModel model)
{
var order = await orderService.GetOrderById(id);
if (order == null || await CheckSalesManager(order))
//No order found with the specified id
return RedirectToAction("List");
if (await groupService.IsStaff(contextAccessor.WorkContext.CurrentCustomer) &&
order.StoreId != contextAccessor.WorkContext.CurrentCustomer.StaffStoreId)
return RedirectToAction("Edit", "Order", new { id });
order.UserFields = model.UserFields;
await orderService.UpdateOrder(order);
await orderViewModelService.PrepareOrderDetailsModel(model, order);
//selected tab
await SaveSelectedTabIndex(persistForTheNextRequest: true);
return RedirectToAction("Edit", "Order", new { id });
}
[PermissionAuthorizeAction(PermissionActionName.Edit)]
[HttpPost]
public async Task<IActionResult> SaveOrderItem(string id, OrderItemsModel model)
{
var order = await orderService.GetOrderById(id);
if (order == null || await CheckSalesManager(order))
//No order found with the specified id
return RedirectToAction("List");
if (await groupService.IsStaff(contextAccessor.WorkContext.CurrentCustomer) &&
order.StoreId != contextAccessor.WorkContext.CurrentCustomer.StaffStoreId)
return RedirectToAction("Edit", "Order", new { id });
if (order.OrderStatusId == (int)OrderStatusSystem.Cancelled)
{
Error("You can't edit position when order is canceled");
return RedirectToAction("Edit", "Order", new { id });
}
var orderItem = order.OrderItems.FirstOrDefault(x => x.Id == model.OrderItemId) ?? throw new ArgumentException("No order item found with the specified id");
var itemModel = model.Items.FirstOrDefault(x => x.Id == model.OrderItemId) ?? throw new ArgumentException("No order item model found with the specified id");
if (itemModel.Quantity == 0 || (orderItem.OpenQty != orderItem.Quantity && orderItem.IsShipEnabled))
{
Error("You can't change quantity");
return RedirectToAction("Edit", "Order", new { id });
}
if (orderItem.Quantity == itemModel.Quantity && orderItem.UnitPriceExclTax == itemModel.UnitPriceExclTaxValue)
{
Error("Nothing has been changed");
return RedirectToAction("Edit", "Order", new { id });
}
orderItem.Quantity = itemModel.Quantity;
orderItem.OpenQty = itemModel.Quantity;
if (orderItem.UnitPriceExclTax != itemModel.UnitPriceExclTaxValue)
{
orderItem.UnitPriceExclTax = itemModel.UnitPriceExclTaxValue;
orderItem.UnitPriceInclTax =
Math.Round(orderItem.UnitPriceExclTax * orderItem.TaxRate / 100 + orderItem.UnitPriceExclTax, 2);
orderItem.PriceInclTax = Math.Round(orderItem.UnitPriceInclTax * orderItem.Quantity, 2);
orderItem.PriceExclTax = Math.Round(orderItem.UnitPriceExclTax * orderItem.Quantity, 2);
orderItem.DiscountAmountInclTax = 0;
orderItem.DiscountAmountExclTax = 0;
}
else
{
orderItem.PriceInclTax = Math.Round(orderItem.UnitPriceInclTax * orderItem.Quantity, 2);
orderItem.PriceExclTax = Math.Round(orderItem.UnitPriceExclTax * orderItem.Quantity, 2);
orderItem.DiscountAmountInclTax = 0;
orderItem.DiscountAmountExclTax = 0;
}
await mediator.Send(new UpdateOrderItemCommand { Order = order, OrderItem = orderItem });
//selected tab
await SaveSelectedTabIndex(persistForTheNextRequest: true);
return RedirectToAction("Edit", "Order", new { id });
}
[PermissionAuthorizeAction(PermissionActionName.Edit)]
[HttpPost]
public async Task<IActionResult> DeleteOrderItem(string id, string orderItemId)
{
var order = await orderService.GetOrderById(id);
if (order == null || await CheckSalesManager(order))
//No order found with the specified id
return RedirectToAction("List");
if (await groupService.IsStaff(contextAccessor.WorkContext.CurrentCustomer) &&
order.StoreId != contextAccessor.WorkContext.CurrentCustomer.StaffStoreId)
return RedirectToAction("Edit", "Order", new { id });
var orderItem = order.OrderItems.FirstOrDefault(x => x.Id == orderItemId);
if (orderItem == null)
throw new ArgumentException("No order item found with the specified id");
var result = await mediator.Send(new DeleteOrderItemCommand { Order = order, OrderItem = orderItem });
if (result.error)
Error(result.message);
//selected tab
await SaveSelectedTabIndex(persistForTheNextRequest: true);
return RedirectToAction("Edit", "Order", new { id });
}
[PermissionAuthorizeAction(PermissionActionName.Edit)]
[HttpPost]
public async Task<IActionResult> CancelOrderItem(string id, string orderItemId)
{
var order = await orderService.GetOrderById(id);
if (order == null || await CheckSalesManager(order))
//No order found with the specified id
return RedirectToAction("List");
if (await groupService.IsStaff(contextAccessor.WorkContext.CurrentCustomer) &&
order.StoreId != contextAccessor.WorkContext.CurrentCustomer.StaffStoreId)
return RedirectToAction("Edit", "Order", new { id });
var orderItem = order.OrderItems.FirstOrDefault(x => x.Id == orderItemId);
if (orderItem == null)
throw new ArgumentException("No order item found with the specified id");
var result = await mediator.Send(new CancelOrderItemCommand { Order = order, OrderItem = orderItem });
if (result.error)
Error(result.message);
else
Success("The order item was successfully canceled");
//selected tab
await SaveSelectedTabIndex(persistForTheNextRequest: true);
return RedirectToAction("Edit", "Order", new { id });
}
[PermissionAuthorizeAction(PermissionActionName.Edit)]
[HttpPost]
public async Task<IActionResult> ResetDownloadCount(string id, string orderItemId)
{
var order = await orderService.GetOrderById(id);
if (order == null || await CheckSalesManager(order))
//No order found with the specified id
return RedirectToAction("List");
if (await groupService.IsStaff(contextAccessor.WorkContext.CurrentCustomer) &&
order.StoreId != contextAccessor.WorkContext.CurrentCustomer.StaffStoreId)
return RedirectToAction("Edit", "Order", new { id });
var orderItem = order.OrderItems.FirstOrDefault(x => x.Id == orderItemId);
if (orderItem == null)
throw new ArgumentException("No order item found with the specified id");
orderItem.DownloadCount = 0;
await orderService.UpdateOrder(order);
var model = new OrderModel();
await orderViewModelService.PrepareOrderDetailsModel(model, order);
//selected tab
await SaveSelectedTabIndex(persistForTheNextRequest: true);
return RedirectToAction("Edit", "Order", new { id });
}
[PermissionAuthorizeAction(PermissionActionName.Edit)]
[HttpPost]
public async Task<IActionResult> ActivateDownloadItem(string id, string orderItemId)
{
var order = await orderService.GetOrderById(id);
if (order == null || await CheckSalesManager(order))
//No order found with the specified id
return RedirectToAction("List");
if (await groupService.IsStaff(contextAccessor.WorkContext.CurrentCustomer) &&
order.StoreId != contextAccessor.WorkContext.CurrentCustomer.StaffStoreId)
return RedirectToAction("Edit", "Order", new { id });
var orderItem = order.OrderItems.FirstOrDefault(x => x.Id == orderItemId);
if (orderItem == null)
throw new ArgumentException("No order item found with the specified id");
orderItem.IsDownloadActivated = !orderItem.IsDownloadActivated;
await orderService.UpdateOrder(order);
var model = new OrderModel();
await orderViewModelService.PrepareOrderDetailsModel(model, order);
//selected tab
await SaveSelectedTabIndex(persistForTheNextRequest: true);
return RedirectToAction("Edit", "Order", new { id });
}
[PermissionAuthorizeAction(PermissionActionName.Edit)]
public async Task<IActionResult> UploadLicenseFilePopup(string id, string orderItemId,
[FromServices] IProductService productService)
{
var order = await orderService.GetOrderById(id);
if (order == null || await CheckSalesManager(order))
//No order found with the specified id
return RedirectToAction("List");
if (await groupService.IsStaff(contextAccessor.WorkContext.CurrentCustomer) &&
order.StoreId != contextAccessor.WorkContext.CurrentCustomer.StaffStoreId)
return RedirectToAction("Edit", "Order", new { id });
var orderItem = order.OrderItems.FirstOrDefault(x => x.Id == orderItemId);
if (orderItem == null)
throw new ArgumentException("No order item found with the specified id");
var product = await productService.GetProductByIdIncludeArch(orderItem.ProductId);
if (!product.IsDownload)
throw new ArgumentException("Product is not downloadable");
var model = new OrderModel.UploadLicenseModel {
LicenseDownloadId = !string.IsNullOrEmpty(orderItem.LicenseDownloadId) ? orderItem.LicenseDownloadId : "",
OrderId = order.Id,
OrderItemId = orderItem.Id
};
return View(model);
}
[PermissionAuthorizeAction(PermissionActionName.Edit)]
[HttpPost]
public async Task<IActionResult> UploadLicenseFilePopup(OrderModel.UploadLicenseModel model)
{
var order = await orderService.GetOrderById(model.OrderId);
if (order == null || await CheckSalesManager(order))
//No order found with the specified id
return RedirectToAction("List");
if (await groupService.IsStaff(contextAccessor.WorkContext.CurrentCustomer) &&
order.StoreId != contextAccessor.WorkContext.CurrentCustomer.StaffStoreId)
return RedirectToAction("Edit", "Order", new { id = order.Id });
var orderItem = order.OrderItems.FirstOrDefault(x => x.Id == model.OrderItemId);
if (orderItem == null)
throw new ArgumentException("No order item found with the specified id");
//attach license
orderItem.LicenseDownloadId = !string.IsNullOrEmpty(model.LicenseDownloadId) ? model.LicenseDownloadId : null;
await orderService.UpdateOrder(order);
//success
ViewBag.RefreshPage = true;
return View(model);
}
[PermissionAuthorizeAction(PermissionActionName.Edit)]
[HttpPost]
public async Task<IActionResult> DeleteLicenseFilePopup(OrderModel.UploadLicenseModel model)
{
var order = await orderService.GetOrderById(model.OrderId);
if (order == null || await CheckSalesManager(order))
//No order found with the specified id
return RedirectToAction("List");
if (await groupService.IsStaff(contextAccessor.WorkContext.CurrentCustomer) &&
order.StoreId != contextAccessor.WorkContext.CurrentCustomer.StaffStoreId)
return RedirectToAction("Edit", "Order", new { id = model.OrderId });
var orderItem = order.OrderItems.FirstOrDefault(x => x.Id == model.OrderItemId);
if (orderItem == null)
throw new ArgumentException("No order item found with the specified id");
//attach license
orderItem.LicenseDownloadId = null;
await orderService.UpdateOrder(order);
//success
ViewBag.RefreshPage = true;
return RedirectToAction("Edit", "Order", new { id = model.OrderId });
}
[PermissionAuthorizeAction(PermissionActionName.Edit)]
public async Task<IActionResult> AddProductToOrder(string orderId)
{
var order = await orderService.GetOrderById(orderId);
if (order == null || await CheckSalesManager(order))
//No order found with the specified id
return RedirectToAction("List");
var model = await orderViewModelService.PrepareAddOrderProductModel(order);
return View(model);
}
[PermissionAuthorizeAction(PermissionActionName.Edit)]
[HttpPost]
public async Task<IActionResult> AddProductToOrder(DataSourceRequest command, OrderModel.AddOrderProductModel model,
[FromServices] IProductService productService)
{
var categoryIds = new List<string>();
if (!string.IsNullOrEmpty(model.SearchCategoryId))
categoryIds.Add(model.SearchCategoryId);
var storeId = string.Empty;
if (await groupService.IsStaff(contextAccessor.WorkContext.CurrentCustomer)) storeId = contextAccessor.WorkContext.CurrentCustomer.StaffStoreId;
var gridModel = new DataSourceResult();
var products = (await productService.SearchProducts(categoryIds: categoryIds,
storeId: storeId,
brandId: model.SearchBrandId,
collectionId: model.SearchCollectionId,
productType: model.SearchProductTypeId > 0 ? (ProductType?)model.SearchProductTypeId : null,
keywords: model.SearchProductName,
pageIndex: command.Page - 1,
pageSize: command.PageSize,
showHidden: true)).products;
gridModel.Data = products.Select(x =>
{
var productModel = new OrderModel.AddOrderProductModel.ProductModel {
Id = x.Id,
Name = x.Name,
Sku = x.Sku
};
return productModel;
});
gridModel.Total = products.TotalCount;
return Json(gridModel);
}
[PermissionAuthorizeAction(PermissionActionName.Edit)]
public async Task<IActionResult> AddProductToOrderDetails(string orderId, string productId)
{
var order = await orderService.GetOrderById(orderId);
if (order == null || await CheckSalesManager(order))
return RedirectToAction("List");
if (await groupService.IsStaff(contextAccessor.WorkContext.CurrentCustomer) &&
order.StoreId != contextAccessor.WorkContext.CurrentCustomer.StaffStoreId) return RedirectToAction("List");
var model = await orderViewModelService.PrepareAddProductToOrderModel(order, productId);
return View(model);
}
[PermissionAuthorizeAction(PermissionActionName.Edit)]
[HttpPost]
public async Task<IActionResult> AddProductToOrderDetails(AddProductToOrderModel model)
{
var order = await orderService.GetOrderById(model.OrderId);
if (order == null || await CheckSalesManager(order))
return RedirectToAction("List");
if (await groupService.IsStaff(contextAccessor.WorkContext.CurrentCustomer) &&
order.StoreId != contextAccessor.WorkContext.CurrentCustomer.StaffStoreId) return RedirectToAction("List");
var warnings = await orderViewModelService.AddProductToOrderDetails(model);
if (!warnings.Any())
//redirect to order details page
return RedirectToAction("Edit", "Order", new { id = model.OrderId });
//errors
var result = await orderViewModelService.PrepareAddProductToOrderModel(order, model.ProductId);
result.Warnings.AddRange(warnings);
return View(result);
}
#endregion
#endregion
#region Addresses
[PermissionAuthorizeAction(PermissionActionName.Preview)]
public async Task<IActionResult> AddressEdit(string addressId, string orderId, bool billingAddress)
{
var order = await orderService.GetOrderById(orderId);
if (order == null || await CheckSalesManager(order))
//No order found with the specified id
return RedirectToAction("List");
if (await groupService.IsStaff(contextAccessor.WorkContext.CurrentCustomer) &&
order.StoreId != contextAccessor.WorkContext.CurrentCustomer.StaffStoreId) return RedirectToAction("List");
var address = new Address();
switch (billingAddress)
{
case true when order.BillingAddress != null:
{
if (order.BillingAddress.Id == addressId)
address = order.BillingAddress;
break;
}
case false when order.ShippingAddress != null:
{
if (order.ShippingAddress.Id == addressId)
address = order.ShippingAddress;
break;
}
}
if (address == null)
throw new ArgumentException("No address found with the specified id", nameof(addressId));
var model = await orderViewModelService.PrepareOrderAddressModel(order, address);
model.BillingAddress = billingAddress;
return View(model);
}
[PermissionAuthorizeAction(PermissionActionName.Edit)]
[HttpPost]
public async Task<IActionResult> AddressEdit(OrderAddressModel model,
[FromServices] IAddressAttributeService addressAttributeService,
[FromServices] IAddressAttributeParser addressAttributeParser)
{
var order = await orderService.GetOrderById(model.OrderId);
if (order == null || await CheckSalesManager(order))
//No order found with the specified id
return RedirectToAction("List");
if (await groupService.IsStaff(contextAccessor.WorkContext.CurrentCustomer) &&
order.StoreId != contextAccessor.WorkContext.CurrentCustomer.StaffStoreId) return RedirectToAction("List");
var address = new Address();
switch (model.BillingAddress)
{
case true when order.BillingAddress != null:
{
if (order.BillingAddress.Id == model.Address.Id)
address = order.BillingAddress;
break;
}
case false when order.ShippingAddress != null:
{
if (order.ShippingAddress.Id == model.Address.Id)
address = order.ShippingAddress;
break;
}
}
if (ModelState.IsValid)
{
var customAttributes =
await model.Address.ParseCustomAddressAttributes(addressAttributeParser, addressAttributeService);
await orderViewModelService.UpdateOrderAddress(order, address, model, customAttributes);
return RedirectToAction("AddressEdit",
new { addressId = model.Address.Id, orderId = model.OrderId, model.BillingAddress });
}
//If we got this far, something failed, redisplay form
model = await orderViewModelService.PrepareOrderAddressModel(order, address);
return View(model);
}
#endregion
#region Order notes
[PermissionAuthorizeAction(PermissionActionName.List)]
[HttpPost]
public async Task<IActionResult> OrderNotesSelect(string orderId, DataSourceRequest command)
{
var order = await orderService.GetOrderById(orderId);
if (order == null || await CheckSalesManager(order))
throw new ArgumentException("No order found with the specified id");
if (await groupService.IsStaff(contextAccessor.WorkContext.CurrentCustomer) &&
order.StoreId != contextAccessor.WorkContext.CurrentCustomer.StaffStoreId) return Content("");
//order notes
var orderNoteModels = await orderViewModelService.PrepareOrderNotes(order);
var gridModel = new DataSourceResult {
Data = orderNoteModels,
Total = orderNoteModels.Count
};
return Json(gridModel);
}
[PermissionAuthorizeAction(PermissionActionName.Edit)]
public async Task<IActionResult> OrderNoteAdd(string orderId, string downloadId, bool displayToCustomer,
string message)
{
var order = await orderService.GetOrderById(orderId);
if (order == null || await CheckSalesManager(order))
return Json(new { Result = false });
if (await groupService.IsStaff(contextAccessor.WorkContext.CurrentCustomer) &&
order.StoreId != contextAccessor.WorkContext.CurrentCustomer.StaffStoreId) return Json(new { Result = false });
await orderViewModelService.InsertOrderNote(order, downloadId, displayToCustomer, message);
return Json(new { Result = true });
}
[PermissionAuthorizeAction(PermissionActionName.Delete)]
[HttpPost]
public async Task<IActionResult> OrderNoteDelete(string id, string orderId)
{
var order = await orderService.GetOrderById(orderId);
if (order == null || await CheckSalesManager(order))
throw new ArgumentException("No order found with the specified id");
if (await groupService.IsStaff(contextAccessor.WorkContext.CurrentCustomer) &&
order.StoreId != contextAccessor.WorkContext.CurrentCustomer.StaffStoreId) return Json(new { Result = false });
await orderViewModelService.DeleteOrderNote(order, id);