Skip to content

Commit

Permalink
Add reference code to support windows services
Browse files Browse the repository at this point in the history
  • Loading branch information
mabaega committed Sep 3, 2024
1 parent ad0837d commit 9a560aa
Show file tree
Hide file tree
Showing 22 changed files with 88 additions and 95 deletions.
1 change: 0 additions & 1 deletion Zatca.EGS/Controllers/DisclaimerController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;

namespace Zatca.EGS.Controllers
{
Expand Down
2 changes: 1 addition & 1 deletion Zatca.EGS/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using Zatca.EGS.Models;

Expand Down
15 changes: 7 additions & 8 deletions Zatca.EGS/Controllers/RelayController.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using Microsoft.AspNetCore.Mvc;
using Zatca.EGS.Models;
using Zatca.eInvoice.Models;
using Zatca.EGS.Helpers;
using System.Text;
using Zatca.eInvoice;
using Newtonsoft.Json;
using System.Net.Http.Headers;
using System.Text;
using System.Xml.Serialization;
using Newtonsoft.Json.Linq;
using Zatca.EGS.Helpers;
using Zatca.EGS.Models;
using Zatca.eInvoice;
using Zatca.eInvoice.Models;

namespace Zatca.EGS.Controllers
{
Expand Down Expand Up @@ -417,9 +416,9 @@ public async Task<IActionResult> Reporting([FromForm] ApprovedInvoice model)
}

bool updateBusinessData = await ZatcaReference.UpdateReferenceFolder(_certInfo.ApiEndpoint, _certInfo.ApiSecret, model.ICV.ToString(), model.InvoiceHash);

Console.WriteLine(updateBusinessData);

if (updateBusinessData)
{
string sanitizedReference = new(model.Reference.Select(c => Path.GetInvalidFileNameChars().Contains(c) ? '_' : c).ToArray());
Expand Down
18 changes: 9 additions & 9 deletions Zatca.EGS/Controllers/WizardController.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO.Compression;
using System.Net.Http.Headers;
using System.Net.Mime;
using System.Text;
using Zatca.eInvoice.Models;
using Zatca.EGS.Helpers;
using Zatca.EGS.Models;
using Zatca.eInvoice.Helpers;
using Zatca.eInvoice;
using Zatca.EGS.Helpers;
using Newtonsoft.Json.Linq;
using System.IO.Compression;
using System.Net.Mime;
using Zatca.eInvoice.Helpers;
using Zatca.eInvoice.Models;

namespace Zatca.EGS.Controllers
{
Expand Down Expand Up @@ -164,13 +164,13 @@ public async Task<IActionResult> GenerateCustomFieldAsync([FromBody] AccessToken
string fullPath = $"{ApiUrl}{apiPath}/{key}";

string nameValue = dataObject.Value<string>("Name");
if (nameValue == null)
if (nameValue == null)
{
nameValue = dataObject.Value<string>("Description");
}

var responseMessage = await SendDataToApi(fullPath, jsonPayload, apiKey);

result.AppendLine(nameValue + " " + responseMessage);

await Task.Delay(100); // Use Task.Delay for async delay
Expand Down Expand Up @@ -446,7 +446,7 @@ public async Task<string> PostComplianceCheck(string ComplianceCheckUrl, string

return null;
}
catch
catch
{
//Console.WriteLine(ex.Message);
return null;
Expand Down
7 changes: 4 additions & 3 deletions Zatca.EGS/Helpers/ComplianceTest.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Text;
using Zatca.EGS.Models;
using Zatca.eInvoice;
using Zatca.eInvoice.Helpers;
using Zatca.eInvoice.Models;
using Zatca.EGS.Models;

namespace Zatca.EGS.Helpers
{
Expand Down Expand Up @@ -167,7 +167,8 @@ public string GetRequestApi(string InvRefNumber, string billRefNum, InvoiceType
LatestDeliveryDate = DateTime.UtcNow.ToString("yyyy-MM-dd"),
},

PaymentMeans = new PaymentMeans {
PaymentMeans = new PaymentMeans
{
PaymentMeansCode = "10",
InstructionNote = invType == InvoiceType.TaxInvoiceCreditNote || invType == InvoiceType.TaxInvoiceDebitNote ? "Instruction Note for Debit or Credit" : null
},
Expand Down Expand Up @@ -329,7 +330,7 @@ public string GetRequestApi(string InvRefNumber, string billRefNum, InvoiceType
}

}
catch
catch
{
//Console.WriteLine($"Error creating RequestApi: {ex.Message}");
throw;
Expand Down
8 changes: 4 additions & 4 deletions Zatca.EGS/Helpers/DocumentFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Text;
using System.Xml.Linq;
using System.Xml;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Globalization;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace Zatca.EGS.Helpers
{
Expand Down
15 changes: 7 additions & 8 deletions Zatca.EGS/Helpers/RellayToInvoiceMapper.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using Microsoft.VisualBasic;
using Zatca.EGS.Models;
using Zatca.eInvoice.Helpers;
using Zatca.eInvoice.Models;
using Zatca.EGS.Models;
using Newtonsoft.Json.Linq;
using static Zatca.EGS.Helpers.VATInfoHelper;

namespace Zatca.EGS.Helpers
Expand Down Expand Up @@ -38,8 +37,8 @@ public Invoice GenerateInvoiceObject()
invoiceType = 381;
}

string invoiceSubType = JsonParser.FindStringByGuid(_relayData.InvoiceJson, ManagerCustomField.InvoiceSubTypeGuid, "RefInvoice") ?? "0200000";
string invoiceSubType = JsonParser.FindStringByGuid(_relayData.InvoiceJson, ManagerCustomField.InvoiceSubTypeGuid, "RefInvoice") ?? "0200000";

Invoice invoice = new()
{
ProfileID = "reporting:1.0",
Expand Down Expand Up @@ -196,8 +195,8 @@ private AccountingCustomerParty CreateAccountingCustomerParty()
private PaymentMeans CreatePaymentMeans()
{
var paymentMeansCode = 30;
string paymentMeans = JsonParser.FindStringByGuid(_relayData.InvoiceJson, ManagerCustomField.PaymentMeansCodeGuid, "RefInvoice");
string instructionNote = JsonParser.FindStringByGuid(_relayData.InvoiceJson, ManagerCustomField.InstructionNoteGuid, "RefInvoice");
string paymentMeans = JsonParser.FindStringByGuid(_relayData.InvoiceJson, ManagerCustomField.PaymentMeansCodeGuid, "RefInvoice");
string instructionNote = JsonParser.FindStringByGuid(_relayData.InvoiceJson, ManagerCustomField.InstructionNoteGuid, "RefInvoice");

if (paymentMeans != null)
{
Expand Down Expand Up @@ -263,7 +262,7 @@ private List<InvoiceLine> CreateInvoiceLines()
invoiceLine.Item.ClassifiedTaxCategory = new ClassifiedTaxCategory
{
Percent = rate,
ID = new ID("UN/ECE 5305", "6",vatInfo.CategoryID),
ID = new ID("UN/ECE 5305", "6", vatInfo.CategoryID),
TaxScheme = new TaxScheme
{
ID = new ID("UN/ECE 5153", "6", "VAT")
Expand Down Expand Up @@ -361,7 +360,7 @@ private List<TaxTotal> CalculateTaxTotals()
};

taxTotals.Add(taxTotalWithSubtotals);


return taxTotals;
}
Expand Down
8 changes: 4 additions & 4 deletions Zatca.EGS/Helpers/ZatcaReference.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Text;
using Zatca.EGS.Models;
using Newtonsoft.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Net.Http.Headers;
using System.Text;
using Zatca.EGS.Models;

namespace Zatca.EGS.Helpers
{
Expand All @@ -11,7 +11,7 @@ public class ZatcaReference
public static async Task<(string ICV, string PIH)> GetReferenceFolder(string apiPath, string apiSecret)
{
string fullPath = $"{apiPath}/folder-form/{ManagerCustomField.FolderReferenceGuid}";

//Console.WriteLine($"Request URL: {fullPath}");

using (HttpClient client = new HttpClient())
Expand Down
2 changes: 1 addition & 1 deletion Zatca.EGS/Models/ApprovedInvoice.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.ComponentModel.DataAnnotations;
using Zatca.eInvoice.Helpers;
using Zatca.EGS.Helpers;
using Zatca.eInvoice.Helpers;

namespace Zatca.EGS.Models
{
Expand Down
2 changes: 1 addition & 1 deletion Zatca.EGS/Models/CertificateInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private string GetUrl(string endpoint)

return $"https://gw-fatoora.zatca.gov.sa/e-invoicing/{environment}/{endpoint}";
}

public bool IsRegistered() => !string.IsNullOrEmpty(CCSIDComplianceRequestId) && !string.IsNullOrEmpty(CCSIDBinaryToken) && !string.IsNullOrEmpty(CCSIDSecret) && !string.IsNullOrEmpty(PCSIDBinaryToken) && !string.IsNullOrEmpty(PCSIDSecret);
}

Expand Down
6 changes: 3 additions & 3 deletions Zatca.EGS/Models/ErrorViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ public class ErrorViewModel
{
public string RequestId { get; set; }
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
public string ErrorMessage { get; set; }
public string ReferrerLink { get; set; }
public string ErrorMessage { get; set; }
public string ReferrerLink { get; set; }
public bool ShowSetupLink { get; set; }
}

Expand All @@ -21,5 +21,5 @@ public class CertificateViewModel
public string ReferrerLink { get; set; }
public bool ShowSetupLink { get; set; }
}

}
10 changes: 5 additions & 5 deletions Zatca.EGS/Models/RelayData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ public RelayData(Dictionary<string, string> formData)
Key = formData.GetValueOrDefault("Key");
Callback = formData.GetValueOrDefault("Callback");
Data = formData.GetValueOrDefault("Data");

string DataString = JsonParser.UpdateJsonGuidValue(Data, ManagerCustomField.ZatcaUUIDGuid);

var (baseCurrency, businessDetails, dynamicParts) = JsonParser.ParseJson(DataString);

var AccessToken = JsonParser.FindStringByGuid(businessDetails, ManagerCustomField.TokenInfoGuid);

var uri = new Uri(Referrer);
var baseUrl = $"{uri.Scheme}://{uri.Host}";

Expand All @@ -53,7 +53,7 @@ public RelayData(Dictionary<string, string> formData)
{
CertificateInfo = ObjectCompressor.DeserializeFromBase64String<CertificateInfo>(certString);
}

if (CertificateInfo != null)
{
CertificateInfo.ApiSecret = AccessToken;
Expand Down Expand Up @@ -98,7 +98,7 @@ public RelayData(Dictionary<string, string> formData)
};

Base64QrCode = JsonParser.FindStringByGuid(InvoiceJson, ManagerCustomField.QrCodeGuid, "RefInvoice");

if (!string.IsNullOrEmpty(Base64QrCode))
{
ApprovalStatus = JsonParser.FindStringByGuid(InvoiceJson, ManagerCustomField.ApprovedInvoiceGuid, "RefInvoice");
Expand Down
10 changes: 9 additions & 1 deletion Zatca.EGS/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@

var builder = WebApplication.CreateBuilder(args);

// Kondisi kompilasi untuk Windows
#if WINDOWS
builder.Host.UseWindowsService();
builder.Services.AddWindowsService();
#endif

// Kondisi kompilasi untuk Linux
#if LINUX
builder.Host.UseSystemd();
#endif

builder.Services.Configure<JsonOptions>(options =>
options.SerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault | JsonIgnoreCondition.WhenWritingNull);
Expand All @@ -23,7 +31,7 @@

builder.Services.AddControllersWithViews();

builder.Services.AddDistributedMemoryCache();
builder.Services.AddDistributedMemoryCache();

builder.Services.AddSession(options =>
{
Expand Down
31 changes: 13 additions & 18 deletions Zatca.EGS/Zatca.EGS.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>disable</Nullable>
Expand All @@ -8,45 +9,39 @@
<ApplicationIcon>wwwroot\favicon.ico</ApplicationIcon>
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>

<PropertyGroup>
<AssemblyVersion>24.09.02.0010</AssemblyVersion>
<FileVersion>24.09.02.0010</FileVersion>
<AssemblyVersion>24.09.02.0012</AssemblyVersion>
<FileVersion>24.09.02.0012</FileVersion>
</PropertyGroup>

<ItemGroup>
<None Include="cfData.json" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting.Systemd" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="8.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Zatca.eInvoice\Zatca.eInvoice.csproj" />
<PackageReference Include="Microsoft.AspNetCore.Hosting.WindowsServices" Version="8.0.6">
<TreatAsUsed>true</TreatAsUsed>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="8.0.0">
<TreatAsUsed>true</TreatAsUsed>
</PackageReference>
</ItemGroup>

<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<Content Update="Views\Relay\Info.cshtml">
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<!--<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="powershell.exe -ExecutionPolicy Bypass -File &quot;$(ProjectDir)UpdateVersion.ps1&quot;" />
</Target>-->

</Project>
8 changes: 4 additions & 4 deletions Zatca.eInvoice.Test/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Net.Http.Headers;
using Newtonsoft.Json;
using System.Net.Http.Headers;
using System.Text;
using Newtonsoft.Json;
using Zatca.eInvoice;
using Zatca.eInvoice.Helpers;
using Zatca.eInvoice.Models;
Expand Down Expand Up @@ -113,10 +113,10 @@ public async Task<SignedInvoiceResult> CreateSignedInvoice(string CSIDBinaryToke
UUID = "8e6000cf-1a98-4174-b3e7-b5d5954bc10d",
IssueDate = "2022-08-17",
IssueTime = "17:41:08",
InvoiceTypeCode = new InvoiceTypeCode( InvoiceType.TaxInvoice, "0200000"),
InvoiceTypeCode = new InvoiceTypeCode(InvoiceType.TaxInvoice, "0200000"),
DocumentCurrencyCode = "SAR",
TaxCurrencyCode = "SAR",
Note = new Note() { LanguageID="ar",Value = "ABC"},
Note = new Note() { LanguageID = "ar", Value = "ABC" },

AdditionalDocumentReference =
[
Expand Down
8 changes: 1 addition & 7 deletions Zatca.eInvoice.Test/QrCodeDecoder.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Zatca.eInvoice.Test
namespace Zatca.eInvoice.Test
{
using System;
using System.Collections.Generic;
Expand Down
Loading

0 comments on commit 9a560aa

Please sign in to comment.