-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZYAPIV2SalesOrders.al
856 lines (758 loc) · 32.7 KB
/
ZYAPIV2SalesOrders.al
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
page 50028 "ZY APIV2 - Sales Orders"
{
APIVersion = 'v2.0';
EntityCaption = 'Sales Order';
EntitySetCaption = 'Sales Orders';
ChangeTrackingAllowed = true;
DelayedInsert = true;
EntityName = 'zysalesOrder';
EntitySetName = 'zysalesOrders';
ODataKeyFields = Id;
APIPublisher = 'zy';
APIGroup = 'zygroup';
PageType = API;
SourceTable = "Sales Order Entity Buffer";
Extensible = false;
layout
{
area(content)
{
repeater(Group)
{
field(id; Rec.Id)
{
Caption = 'Id';
Editable = false;
trigger OnValidate()
begin
RegisterFieldSet(Rec.FieldNo(Id));
end;
}
field(number; Rec."No.")
{
Caption = 'No.';
Editable = false;
trigger OnValidate()
begin
RegisterFieldSet(Rec.FieldNo("No."));
end;
}
field(externalDocumentNumber; Rec."External Document No.")
{
Caption = 'External Document No.';
trigger OnValidate()
begin
RegisterFieldSet(Rec.FieldNo("External Document No."))
end;
}
field(orderDate; Rec."Document Date")
{
Caption = 'Order Date';
trigger OnValidate()
begin
DocumentDateVar := Rec."Document Date";
DocumentDateSet := true;
RegisterFieldSet(Rec.FieldNo("Document Date"));
end;
}
field(postingDate; Rec."Posting Date")
{
Caption = 'Posting Date';
trigger OnValidate()
begin
PostingDateVar := Rec."Posting Date";
PostingDateSet := true;
RegisterFieldSet(Rec.FieldNo("Posting Date"));
end;
}
field(customerId; Rec."Customer Id")
{
Caption = 'Customer Id';
trigger OnValidate()
begin
if not SellToCustomer.GetBySystemId(Rec."Customer Id") then
Error(CouldNotFindSellToCustomerErr);
Rec."Sell-to Customer No." := SellToCustomer."No.";
RegisterFieldSet(Rec.FieldNo("Customer Id"));
RegisterFieldSet(Rec.FieldNo("Sell-to Customer No."));
end;
}
field(customerNumber; Rec."Sell-to Customer No.")
{
Caption = 'Customer No.';
trigger OnValidate()
begin
if SellToCustomer."No." <> '' then begin
if SellToCustomer."No." <> Rec."Sell-to Customer No." then
Error(SellToCustomerValuesDontMatchErr);
exit;
end;
if not SellToCustomer.Get(Rec."Sell-to Customer No.") then
Error(CouldNotFindSellToCustomerErr);
Rec."Customer Id" := SellToCustomer.SystemId;
RegisterFieldSet(Rec.FieldNo("Customer Id"));
RegisterFieldSet(Rec.FieldNo("Sell-to Customer No."));
end;
}
field(customerName; Rec."Sell-to Customer Name")
{
Caption = 'Customer Name';
Editable = false;
}
field(billToName; Rec."Bill-to Name")
{
Caption = 'Bill-to Name';
Editable = false;
}
field(billToCustomerId; Rec."Bill-to Customer Id")
{
Caption = 'Bill-to Customer Id';
trigger OnValidate()
begin
if not BillToCustomer.GetBySystemId(Rec."Bill-to Customer Id") then
Error(CouldNotFindBillToCustomerErr);
Rec."Bill-to Customer No." := BillToCustomer."No.";
RegisterFieldSet(Rec.FieldNo("Bill-to Customer Id"));
RegisterFieldSet(Rec.FieldNo("Bill-to Customer No."));
end;
}
field(billToCustomerNumber; Rec."Bill-to Customer No.")
{
Caption = 'Bill-to Customer No.';
trigger OnValidate()
begin
if BillToCustomer."No." <> '' then begin
if BillToCustomer."No." <> Rec."Bill-to Customer No." then
Error(BillToCustomerValuesDontMatchErr);
exit;
end;
if not BillToCustomer.Get(Rec."Bill-to Customer No.") then
Error(CouldNotFindBillToCustomerErr);
Rec."Bill-to Customer Id" := BillToCustomer.SystemId;
RegisterFieldSet(Rec.FieldNo("Bill-to Customer Id"));
RegisterFieldSet(Rec.FieldNo("Bill-to Customer No."));
end;
}
field(shipToName; Rec."Ship-to Name")
{
Caption = 'Ship-to Name';
trigger OnValidate()
begin
if xRec."Ship-to Name" <> Rec."Ship-to Name" then begin
Rec."Ship-to Code" := '';
RegisterFieldSet(Rec.FieldNo("Ship-to Code"));
RegisterFieldSet(Rec.FieldNo("Ship-to Name"));
end;
end;
}
field(shipToContact; Rec."Ship-to Contact")
{
Caption = 'Ship-to Contact';
trigger OnValidate()
begin
if xRec."Ship-to Contact" <> Rec."Ship-to Contact" then begin
Rec."Ship-to Code" := '';
RegisterFieldSet(Rec.FieldNo("Ship-to Code"));
RegisterFieldSet(Rec.FieldNo("Ship-to Contact"));
end;
end;
}
field(sellToAddressLine1; Rec."Sell-to Address")
{
Caption = 'Sell-to Address Line 1';
trigger OnValidate()
begin
RegisterFieldSet(Rec.FieldNo("Sell-to Address"));
end;
}
field(sellToAddressLine2; Rec."Sell-to Address 2")
{
Caption = 'Sell-to Address Line 2';
trigger OnValidate()
begin
RegisterFieldSet(Rec.FieldNo("Sell-to Address 2"));
end;
}
field(sellToCity; Rec."Sell-to City")
{
Caption = 'Sell-to City';
trigger OnValidate()
begin
RegisterFieldSet(Rec.FieldNo("Sell-to City"));
end;
}
field(sellToCountry; Rec."Sell-to Country/Region Code")
{
Caption = 'Sell-to Country/Region Code';
trigger OnValidate()
begin
RegisterFieldSet(Rec.FieldNo("Sell-to Country/Region Code"));
end;
}
field(sellToState; Rec."Sell-to County")
{
Caption = 'Sell-to State';
trigger OnValidate()
begin
RegisterFieldSet(Rec.FieldNo("Sell-to County"));
end;
}
field(sellToPostCode; Rec."Sell-to Post Code")
{
Caption = 'Sell-to Post Code';
trigger OnValidate()
begin
RegisterFieldSet(Rec.FieldNo("Sell-to Post Code"));
end;
}
field(billToAddressLine1; Rec."Bill-to Address")
{
Caption = 'Bill-to Address Line 1';
Editable = false;
}
field(billToAddressLine2; Rec."Bill-to Address 2")
{
Caption = 'Bill-to Address Line 2';
Editable = false;
}
field(billToCity; Rec."Bill-to City")
{
Caption = 'Bill-to City';
Editable = false;
}
field(billToCountry; Rec."Bill-to Country/Region Code")
{
Caption = 'Bill-to Country/Region Code';
Editable = false;
}
field(billToState; Rec."Bill-to County")
{
Caption = 'BillTo State';
Editable = false;
}
field(billToPostCode; Rec."Bill-to Post Code")
{
Caption = 'Bill-to Post Code';
Editable = false;
}
field(shipToAddressLine1; Rec."Ship-to Address")
{
Caption = 'Ship-to Address Line 1';
trigger OnValidate()
begin
Rec."Ship-to Code" := '';
RegisterFieldSet(Rec.FieldNo("Ship-to Code"));
RegisterFieldSet(Rec.FieldNo("Ship-to Address"));
end;
}
field(shipToAddressLine2; Rec."Ship-to Address 2")
{
Caption = 'Ship-to Address Line 2';
trigger OnValidate()
begin
Rec."Ship-to Code" := '';
RegisterFieldSet(Rec.FieldNo("Ship-to Code"));
RegisterFieldSet(Rec.FieldNo("Ship-to Address 2"));
end;
}
field(shipToCity; Rec."Ship-to City")
{
Caption = 'Ship-to City';
trigger OnValidate()
begin
Rec."Ship-to Code" := '';
RegisterFieldSet(Rec.FieldNo("Ship-to Code"));
RegisterFieldSet(Rec.FieldNo("Ship-to City"));
end;
}
field(shipToCountry; Rec."Ship-to Country/Region Code")
{
Caption = 'Ship-to Country/Region Code';
trigger OnValidate()
begin
Rec."Ship-to Code" := '';
RegisterFieldSet(Rec.FieldNo("Ship-to Code"));
RegisterFieldSet(Rec.FieldNo("Ship-to Country/Region Code"));
end;
}
field(shipToState; Rec."Ship-to County")
{
Caption = 'Ship-to State';
trigger OnValidate()
begin
Rec."Ship-to Code" := '';
RegisterFieldSet(Rec.FieldNo("Ship-to Code"));
RegisterFieldSet(Rec.FieldNo("Ship-to County"));
end;
}
field(shipToPostCode; Rec."Ship-to Post Code")
{
Caption = 'Ship-to Post Code';
trigger OnValidate()
begin
Rec."Ship-to Code" := '';
RegisterFieldSet(Rec.FieldNo("Ship-to Code"));
RegisterFieldSet(Rec.FieldNo("Ship-to Post Code"));
end;
}
field(shortcutDimension1Code; Rec."Shortcut Dimension 1 Code")
{
Caption = 'Shortcut Dimension 1 Code';
trigger OnValidate()
begin
RegisterFieldSet(Rec.FieldNo("Shortcut Dimension 1 Code"));
end;
}
field(shortcutDimension2Code; Rec."Shortcut Dimension 2 Code")
{
Caption = 'Shortcut Dimension 2 Code';
trigger OnValidate()
begin
RegisterFieldSet(Rec.FieldNo("Shortcut Dimension 2 Code"));
end;
}
field(currencyId; Rec."Currency Id")
{
Caption = 'Currency Id';
trigger OnValidate()
begin
if Rec."Currency Id" = BlankGUID then
Rec."Currency Code" := ''
else begin
if not Currency.GetBySystemId(Rec."Currency Id") then
Error(CurrencyIdDoesNotMatchACurrencyErr);
Rec."Currency Code" := Currency.Code;
end;
RegisterFieldSet(Rec.FieldNo("Currency Id"));
RegisterFieldSet(Rec.FieldNo("Currency Code"));
end;
}
field(currencyCode; CurrencyCodeTxt)
{
Caption = 'Currency Code';
trigger OnValidate()
begin
Rec."Currency Code" :=
GraphMgtGeneralTools.TranslateCurrencyCodeToNAVCurrencyCode(
LCYCurrencyCode, COPYSTR(CurrencyCodeTxt, 1, MAXSTRLEN(LCYCurrencyCode)));
if Currency.Code <> '' then begin
if Currency.Code <> Rec."Currency Code" then
Error(CurrencyValuesDontMatchErr);
exit;
end;
if Rec."Currency Code" = '' then
Rec."Currency Id" := BlankGUID
else begin
if not Currency.Get(Rec."Currency Code") then
Error(CurrencyCodeDoesNotMatchACurrencyErr);
Rec."Currency Id" := Currency.SystemId;
end;
RegisterFieldSet(Rec.FieldNo("Currency Id"));
RegisterFieldSet(Rec.FieldNo("Currency Code"));
end;
}
field(pricesIncludeTax; Rec."Prices Including VAT")
{
Caption = 'Prices Include Tax';
trigger OnValidate()
var
SalesLine: Record "Sales Line";
begin
if Rec."Prices Including VAT" then begin
SalesLine.SetRange("Document No.", Rec."No.");
SalesLine.SetRange("Document Type", SalesLine."Document Type"::Order);
if SalesLine.FindFirst() then
if SalesLine."VAT Calculation Type" = SalesLine."VAT Calculation Type"::"Sales Tax" then
Error(CannotEnablePricesIncludeTaxErr);
end;
RegisterFieldSet(Rec.FieldNo("Prices Including VAT"));
end;
}
field(paymentTermsId; Rec."Payment Terms Id")
{
Caption = 'Payment Terms Id';
trigger OnValidate()
begin
if Rec."Payment Terms Id" = BlankGUID then
Rec."Payment Terms Code" := ''
else begin
if not PaymentTerms.GetBySystemId(Rec."Payment Terms Id") then
Error(PaymentTermsIdDoesNotMatchAPaymentTermsErr);
Rec."Payment Terms Code" := PaymentTerms.Code;
end;
RegisterFieldSet(Rec.FieldNo("Payment Terms Id"));
RegisterFieldSet(Rec.FieldNo("Payment Terms Code"));
end;
}
field(shipmentMethodId; Rec."Shipment Method Id")
{
Caption = 'Shipment Method Id';
trigger OnValidate()
begin
if Rec."Shipment Method Id" = BlankGUID then
Rec."Shipment Method Code" := ''
else begin
if not ShipmentMethod.GetBySystemId(Rec."Shipment Method Id") then
Error(ShipmentMethodIdDoesNotMatchAShipmentMethodErr);
Rec."Shipment Method Code" := ShipmentMethod.Code;
end;
RegisterFieldSet(Rec.FieldNo("Shipment Method Id"));
RegisterFieldSet(Rec.FieldNo("Shipment Method Code"));
end;
}
field(salesperson; Rec."Salesperson Code")
{
Caption = 'Salesperson';
trigger OnValidate()
begin
RegisterFieldSet(Rec.FieldNo("Salesperson Code"));
end;
}
field(partialShipping; PartialOrderShipping)
{
Caption = 'Partial Shipping';
trigger OnValidate()
begin
ProcessPartialShipping();
end;
}
field(requestedDeliveryDate; Rec."Requested Delivery Date")
{
Caption = 'Requested Delivery Date';
trigger OnValidate()
begin
RegisterFieldSet(Rec.FieldNo("Requested Delivery Date"));
end;
}
part(dimensionSetLines; "APIV2 - Dimension Set Lines")
{
Caption = 'Dimension Set Lines';
EntityName = 'dimensionSetLine';
EntitySetName = 'dimensionSetLines';
SubPageLink = "Parent Id" = Field(Id), "Parent Type" = const("Sales Order");
}
part(salesOrderLines; "APIV2 - Sales Order Lines")
{
Caption = 'Lines';
EntityName = 'salesOrderLine';
EntitySetName = 'salesOrderLines';
SubPageLink = "Document Id" = Field(Id);
}
field(discountAmount; Rec."Invoice Discount Amount")
{
Caption = 'Discount Amount';
trigger OnValidate()
begin
RegisterFieldSet(Rec.FieldNo("Invoice Discount Amount"));
InvoiceDiscountAmount := Rec."Invoice Discount Amount";
DiscountAmountSet := true;
end;
}
field(discountAppliedBeforeTax; Rec."Discount Applied Before Tax")
{
Caption = 'Discount Applied Before Tax';
Editable = false;
}
field(totalAmountExcludingTax; Rec.Amount)
{
Caption = 'Total Amount Excluding Tax';
Editable = false;
}
field(totalTaxAmount; Rec."Total Tax Amount")
{
Caption = 'Total Tax Amount';
Editable = false;
trigger OnValidate()
begin
RegisterFieldSet(Rec.FieldNo("Total Tax Amount"));
end;
}
field(totalAmountIncludingTax; Rec."Amount Including VAT")
{
Caption = 'Total Amount Including Tax';
Editable = false;
trigger OnValidate()
begin
RegisterFieldSet(Rec.FieldNo("Amount Including VAT"));
end;
}
field(fullyShipped; Rec."Completely Shipped")
{
Caption = 'Fully Shipped';
trigger OnValidate()
begin
RegisterFieldSet(Rec.FieldNo("Completely Shipped"));
end;
}
field(status; Rec.Status)
{
Caption = 'Status';
Editable = false;
}
field(lastModifiedDateTime; Rec.SystemModifiedAt)
{
Caption = 'Last Modified Date';
Editable = false;
}
field(phoneNumber; Rec."Sell-to Phone No.")
{
Caption = 'Phone No.';
trigger OnValidate()
begin
RegisterFieldSet(Rec.FieldNo("Sell-to Phone No."));
end;
}
field(email; Rec."Sell-to E-Mail")
{
Caption = 'Email';
trigger OnValidate()
begin
RegisterFieldSet(Rec.FieldNo("Sell-to E-Mail"));
end;
}
part(attachments; "APIV2 - Attachments")
{
Caption = 'Attachments';
EntityName = 'attachment';
EntitySetName = 'attachments';
SubPageLink = "Document Id" = Field(Id), "Document Type" = const("Sales Order");
}
part(documentAttachments; "APIV2 - Document Attachments")
{
Caption = 'Document Attachments';
EntityName = 'documentAttachment';
EntitySetName = 'documentAttachments';
SubPageLink = "Document Id" = Field(Id), "Document Type" = const("Sales Order");
}
}
}
}
actions
{
}
trigger OnAfterGetRecord()
begin
SetCalculatedFields();
if HasWritePermission then
GraphMgtSalesOrderBuffer.RedistributeInvoiceDiscounts(Rec);
end;
trigger OnDeleteRecord(): Boolean
begin
GraphMgtSalesOrderBuffer.PropagateOnDelete(Rec);
exit(false);
end;
trigger OnInsertRecord(BelowxRec: Boolean): Boolean
begin
CheckSellToCustomerSpecified();
GraphMgtSalesOrderBuffer.PropagateOnInsert(Rec, TempFieldBuffer);
SetDates();
UpdateDiscount();
SetCalculatedFields();
exit(false);
end;
trigger OnModifyRecord(): Boolean
begin
if xRec.Id <> Rec.Id then
Error(CannotChangeIDErr);
GraphMgtSalesOrderBuffer.PropagateOnModify(Rec, TempFieldBuffer);
UpdateDiscount();
SetCalculatedFields();
exit(false);
end;
trigger OnNewRecord(BelowxRec: Boolean)
begin
ClearCalculatedFields();
end;
trigger OnOpenPage()
begin
CheckPermissions();
end;
var
TempFieldBuffer: Record "Field Buffer" temporary;
SellToCustomer: Record "Customer";
BillToCustomer: Record "Customer";
Currency: Record "Currency";
PaymentTerms: Record "Payment Terms";
ShipmentMethod: Record "Shipment Method";
GraphMgtSalesOrderBuffer: Codeunit "Graph Mgt - Sales Order Buffer";
GraphMgtGeneralTools: Codeunit "Graph Mgt - General Tools";
APIV2SendSalesDocument: Codeunit "APIV2 - Send Sales Document";
LCYCurrencyCode: Code[10];
CurrencyCodeTxt: Text;
CannotChangeIDErr: Label 'The "id" cannot be changed.', Comment = 'id is a field name and should not be translated.';
SellToCustomerNotProvidedErr: Label 'A "customerNumber" or a "customerId" must be provided.', Comment = 'customerNumber and customerId are field names and should not be translated.';
SellToCustomerValuesDontMatchErr: Label 'The sell-to customer values do not match to a specific Customer.';
BillToCustomerValuesDontMatchErr: Label 'The bill-to customer values do not match to a specific Customer.';
CouldNotFindSellToCustomerErr: Label 'The sell-to customer cannot be found.';
CouldNotFindBillToCustomerErr: Label 'The bill-to customer cannot be found.';
PartialOrderShipping: Boolean;
SalesOrderPermissionsErr: Label 'You do not have permissions to read Sales Orders.';
CurrencyValuesDontMatchErr: Label 'The currency values do not match to a specific Currency.';
CurrencyIdDoesNotMatchACurrencyErr: Label 'The "currencyId" does not match to a Currency.', Comment = 'currencyId is a field name and should not be translated.';
CurrencyCodeDoesNotMatchACurrencyErr: Label 'The "currencyCode" does not match to a Currency.', Comment = 'currencyCode is a field name and should not be translated.';
PaymentTermsIdDoesNotMatchAPaymentTermsErr: Label 'The "paymentTermsId" does not match to a Payment Terms.', Comment = 'paymentTermsId is a field name and should not be translated.';
ShipmentMethodIdDoesNotMatchAShipmentMethodErr: Label 'The "shipmentMethodId" does not match to a Shipment Method.', Comment = 'shipmentMethodId is a field name and should not be translated.';
CannotFindOrderErr: Label 'The order cannot be found.';
CannotEnablePricesIncludeTaxErr: Label 'The "pricesIncludeTax" cannot be set to true if VAT Calculation Type is Sales Tax.', Comment = 'pricesIncludeTax is a field name and should not be translated.';
DiscountAmountSet: Boolean;
InvoiceDiscountAmount: Decimal;
BlankGUID: Guid;
DocumentDateSet: Boolean;
DocumentDateVar: Date;
PostingDateSet: Boolean;
PostingDateVar: Date;
HasWritePermission: Boolean;
local procedure SetCalculatedFields()
begin
CurrencyCodeTxt := GraphMgtGeneralTools.TranslateNAVCurrencyCodeToCurrencyCode(LCYCurrencyCode, Rec."Currency Code");
PartialOrderShipping := (Rec."Shipping Advice" = Rec."Shipping Advice"::Partial);
end;
local procedure ClearCalculatedFields()
begin
Clear(DiscountAmountSet);
Clear(InvoiceDiscountAmount);
PartialOrderShipping := false;
TempFieldBuffer.DeleteAll();
end;
local procedure RegisterFieldSet(FieldNo: Integer)
var
LastOrderNo: Integer;
begin
LastOrderNo := 1;
if TempFieldBuffer.FindLast() then
LastOrderNo := TempFieldBuffer.Order + 1;
Clear(TempFieldBuffer);
TempFieldBuffer.Order := LastOrderNo;
TempFieldBuffer."Table ID" := Database::"Sales Invoice Entity Aggregate";
TempFieldBuffer."Field ID" := FieldNo;
TempFieldBuffer.Insert();
end;
local procedure CheckSellToCustomerSpecified()
begin
if (Rec."Sell-to Customer No." = '') and
(Rec."Customer Id" = BlankGUID)
then
Error(SellToCustomerNotProvidedErr);
end;
local procedure ProcessPartialShipping()
begin
if PartialOrderShipping then
Rec."Shipping Advice" := Rec."Shipping Advice"::Partial
else
Rec."Shipping Advice" := Rec."Shipping Advice"::Complete;
RegisterFieldSet(Rec.FieldNo("Shipping Advice"));
end;
local procedure CheckPermissions()
var
SalesHeader: Record "Sales Header";
begin
SalesHeader.SetRange("Document Type", SalesHeader."Document Type"::Order);
if not SalesHeader.ReadPermission() then
Error(SalesOrderPermissionsErr);
HasWritePermission := SalesHeader.WritePermission();
end;
local procedure UpdateDiscount()
var
SalesHeader: Record "Sales Header";
SalesCalcDiscountByType: Codeunit "Sales - Calc Discount By Type";
begin
if not DiscountAmountSet then begin
GraphMgtSalesOrderBuffer.RedistributeInvoiceDiscounts(Rec);
exit;
end;
SalesHeader.Get(SalesHeader."Document Type"::Order, Rec."No.");
SalesCalcDiscountByType.ApplyInvDiscBasedOnAmt(InvoiceDiscountAmount, SalesHeader);
end;
local procedure SetDates()
begin
if not (DocumentDateSet or PostingDateSet) then
exit;
TempFieldBuffer.Reset();
TempFieldBuffer.DeleteAll();
if DocumentDateSet then begin
Rec."Document Date" := DocumentDateVar;
RegisterFieldSet(Rec.FieldNo("Document Date"));
end;
if PostingDateSet then begin
Rec."Posting Date" := PostingDateVar;
RegisterFieldSet(Rec.FieldNo("Posting Date"));
end;
GraphMgtSalesOrderBuffer.PropagateOnModify(Rec, TempFieldBuffer);
Rec.Find();
end;
local procedure GetOrder(var SalesHeader: Record "Sales Header")
begin
if not SalesHeader.GetBySystemId(Rec.Id) then
Error(CannotFindOrderErr);
end;
local procedure PostWithShipAndInvoice(var SalesHeader: Record "Sales Header"; var SalesInvoiceHeader: Record "Sales Invoice Header")
var
LinesInstructionMgt: Codeunit "Lines Instruction Mgt.";
OrderNo: Code[20];
OrderNoSeries: Code[20];
begin
APIV2SendSalesDocument.CheckDocumentIfNoItemsExists(SalesHeader);
LinesInstructionMgt.SalesCheckAllLinesHaveQuantityAssigned(SalesHeader);
OrderNo := SalesHeader."No.";
OrderNoSeries := SalesHeader."No. Series";
SalesHeader.Ship := true;
SalesHeader.Invoice := true;
SalesHeader.SendToPosting(Codeunit::"Sales-Post");
SalesInvoiceHeader.SetCurrentKey("Order No.");
SalesInvoiceHeader.SetRange("Pre-Assigned No. Series", '');
SalesInvoiceHeader.SetRange("Order No. Series", OrderNoSeries);
SalesInvoiceHeader.SetRange("Order No.", OrderNo);
SalesInvoiceHeader.FindFirst();
end;
local procedure PostWithShip(var SalesHeader: Record "Sales Header")
var
LinesInstructionMgt: Codeunit "Lines Instruction Mgt.";
OrderNo: Code[20];
OrderNoSeries: Code[20];
begin
APIV2SendSalesDocument.CheckDocumentIfNoItemsExists(SalesHeader);
LinesInstructionMgt.SalesCheckAllLinesHaveQuantityAssigned(SalesHeader);
OrderNo := SalesHeader."No.";
OrderNoSeries := SalesHeader."No. Series";
SalesHeader.Ship := true;
SalesHeader.Invoice := false;
SalesHeader.SendToPosting(Codeunit::"Sales-Post");
/*SalesInvoiceHeader.SetCurrentKey("Order No.");
SalesInvoiceHeader.SetRange("Pre-Assigned No. Series", '');
SalesInvoiceHeader.SetRange("Order No. Series", OrderNoSeries);
SalesInvoiceHeader.SetRange("Order No.", OrderNo);
SalesInvoiceHeader.FindFirst();*/
end;
local procedure SetActionResponse(var ActionContext: WebServiceActionContext; PageId: Integer; DocumentId: Guid)
var
begin
ActionContext.SetObjectType(ObjectType::Page);
ActionContext.SetObjectId(PageId);
ActionContext.AddEntityKey(Rec.FieldNo(Id), DocumentId);
ActionContext.SetResultCode(WebServiceActionResultCode::Deleted);
end;
[ServiceEnabled]
[Scope('Cloud')]
procedure ShipAndInvoice(var ActionContext: WebServiceActionContext)
var
SalesHeader: Record "Sales Header";
SalesInvoiceHeader: Record "Sales Invoice Header";
SalesInvoiceAggregator: Codeunit "Sales Invoice Aggregator";
begin
GetOrder(SalesHeader);
PostWithShipAndInvoice(SalesHeader, SalesInvoiceHeader);
SetActionResponse(ActionContext, Page::"APIV2 - Sales Invoices", SalesInvoiceAggregator.GetSalesInvoiceHeaderId(SalesInvoiceHeader));
end;
[ServiceEnabled]
[Scope('Cloud')]
procedure Ship(var ActionContext: WebServiceActionContext)
var
SalesHeader: Record "Sales Header";
SalesInvoiceHeader: Record "Sales Invoice Header";
SalesInvoiceAggregator: Codeunit "Sales Invoice Aggregator";
begin
GetOrder(SalesHeader);
PostWithShip(SalesHeader);
//SetActionResponse(ActionContext, Page::"APIV2 - Sales Invoices", SalesInvoiceAggregator.GetSalesInvoiceHeaderId(SalesInvoiceHeader));
end;
}