Skip to content

Commit

Permalink
Fixed actions
Browse files Browse the repository at this point in the history
  • Loading branch information
richardrandak committed May 7, 2020
1 parent 763a04c commit 4acf00d
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 28 deletions.
34 changes: 34 additions & 0 deletions FortnoxAPILibrary.Tests/GeneratedTests/InvoiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,39 @@ public void Invoice_DueDate()
new ArticleConnector().Delete(tmpArticle.ArticleNumber);
#endregion Delete arranged resources
}

[TestMethod]
public void Invoice_Print()
{
#region Arrange
var tmpCustomer = new CustomerConnector().Create(new Customer() { Name = "TmpCustomer", CountryCode = "SE", City = "Testopolis" });
var tmpArticle = new ArticleConnector().Create(new Article() { Description = "TmpArticle", Type = ArticleType.STOCK, PurchasePrice = 100 });
#endregion Arrange

IInvoiceConnector connector = new InvoiceConnector();

var newInvoice = new Invoice()
{
CustomerNumber = tmpCustomer.CustomerNumber,
InvoiceDate = new DateTime(2019, 1, 20), //"2019-01-20",
DueDate = new DateTime(2019, 2, 20), //"2019-02-20",
InvoiceType = InvoiceType.CASHINVOICE,
PaymentWay = PaymentWay.CASH,
Comments = "TestInvoice",
InvoiceRows = new List<InvoiceRow>()
{
new InvoiceRow(){ ArticleNumber = tmpArticle.ArticleNumber, DeliveredQuantity = 10, Price = 100},
new InvoiceRow(){ ArticleNumber = tmpArticle.ArticleNumber, DeliveredQuantity = 20, Price = 100},
new InvoiceRow(){ ArticleNumber = tmpArticle.ArticleNumber, DeliveredQuantity = 15, Price = 100}
}
};

var createdInvoice = connector.Create(newInvoice);
MyAssert.HasNoError(connector);

var fileData = connector.Print(createdInvoice.DocumentNumber);
MyAssert.HasNoError(connector);
Assert.IsTrue(fileData.Length > 10000);
}
}
}
49 changes: 43 additions & 6 deletions FortnoxAPILibrary/EntityConnector.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
Expand Down Expand Up @@ -181,8 +182,36 @@ protected TEntity DoAction(string documentNumber, Action action)
return DoActionAsync(documentNumber, action).Result;
}

protected byte[] DoDownloadAction(string documentNumber, Action action, string localPath = null)
{
return DoDownloadActionAsync(documentNumber, action, localPath).Result;
}

protected async Task<byte[]> DoDownloadActionAsync(string documentNumber, Action action, string localPath = null)
{
if (!IsDownloadAction(action))
throw new Exception("Invalid action type");

RequestInfo = new RequestInfo()
{
BaseUrl = BaseUrl,
Resource = Resource,
Indices = new[] { documentNumber, action.GetStringValue() },
Method = RequestMethod.Get,
ResponseType = RequestResponseType.PDF
};

var data = await DoSimpleRequest();
if (localPath != null)
File.WriteAllBytes(localPath, data);
return data;
}

protected async Task<TEntity> DoActionAsync(string documentNumber, Action action)
{
if (IsDownloadAction(action))
throw new Exception("Invalid action type");

RequestInfo = new RequestInfo()
{
BaseUrl = BaseUrl,
Expand All @@ -192,12 +221,6 @@ protected async Task<TEntity> DoActionAsync(string documentNumber, Action action

switch (action)
{
case Action.Print:
case Action.Preview:
case Action.EPrint:
RequestInfo.Method = RequestMethod.Get;
RequestInfo.ResponseType = RequestResponseType.PDF;
break;
case Action.ExternalPrint:
RequestInfo.Method = RequestMethod.Put;
RequestInfo.ResponseType = RequestResponseType.JSON;
Expand All @@ -214,6 +237,20 @@ protected async Task<TEntity> DoActionAsync(string documentNumber, Action action
var result = await DoEntityRequest<EntityWrapper<TEntity>>();
return result?.Entity;
}

private static bool IsDownloadAction(Action action)
{
switch (action)
{
case Action.Print:
case Action.PrintReminder:
case Action.Preview:
case Action.EPrint:
return true;
default:
return false;
}
}
}

public enum Action
Expand Down
14 changes: 7 additions & 7 deletions FortnoxAPILibrary/Generated/Connectors/InvoiceConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,21 +211,21 @@ public Invoice EInvoice(int? id)
/// <summary>
/// This action returns a PDF document with the current template that is used by the specific document. Note that this action also sets the property Sent as true.
/// <param name="id"></param>
/// <returns></returns>
/// <returns></returns>
/// </summary>
public Invoice Print(int? id)
public byte[] Print(int? id)
{
return DoAction(id.ToString(), Action.Print);
return DoDownloadAction(id.ToString(), Action.Print);
}

/// <summary>
/// This action returns a PDF document with the current reminder template that is used by the specific document. Note that this action also sets the property Sent as true.
/// <param name="id"></param>
/// <returns></returns>
/// </summary>
public Invoice PrintReminder(int? id)
public byte[] PrintReminder(int? id)
{
return DoAction(id.ToString(), Action.PrintReminder);
return DoDownloadAction(id.ToString(), Action.PrintReminder);
}

/// <summary>
Expand All @@ -243,9 +243,9 @@ public Invoice ExternalPrint(int? id)
/// <param name="id"></param>
/// <returns></returns>
/// </summary>
public Invoice Preview(int? id)
public byte[] Preview(int? id)
{
return DoAction(id.ToString(), Action.Preview);
return DoDownloadAction(id.ToString(), Action.Preview);
}

public async Task<EntityCollection<InvoiceSubset>> FindAsync()
Expand Down
8 changes: 4 additions & 4 deletions FortnoxAPILibrary/Generated/Connectors/OfferConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ public Offer Email(int? id)
/// <param name="id"></param>
/// <returns></returns>
/// </summary>
public Offer Print(int? id)
public byte[] Print(int? id)
{
return DoAction(id.ToString(), Action.Print);
return DoDownloadAction(id.ToString(), Action.Print);
}

/// <summary>
Expand All @@ -165,9 +165,9 @@ public Offer ExternalPrint(int? id)
/// <param name="id"></param>
/// <returns></returns>
/// </summary>
public Offer Preview(int? id)
public byte[] Preview(int? id)
{
return DoAction(id.ToString(), Action.Preview);
return DoDownloadAction(id.ToString(), Action.Preview);
}

public async Task<EntityCollection<OfferSubset>> FindAsync()
Expand Down
8 changes: 4 additions & 4 deletions FortnoxAPILibrary/Generated/Connectors/OrderConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ public Order Email(int? id)
/// <param name="id"></param>
/// <returns></returns>
/// </summary>
public Order Print(int? id)
public byte[] Print(int? id)
{
return DoAction(id.ToString(), Action.Print);
return DoDownloadAction(id.ToString(), Action.Print);
}

/// <summary>
Expand All @@ -183,9 +183,9 @@ public Order ExternalPrint(int? id)
/// <param name="id"></param>
/// <returns></returns>
/// </summary>
public Order Preview(int? id)
public byte[] Preview(int? id)
{
return DoAction(id.ToString(), Action.Preview);
return DoDownloadAction(id.ToString(), Action.Preview);
}

public async Task<EntityCollection<OrderSubset>> FindAsync()
Expand Down
6 changes: 3 additions & 3 deletions FortnoxAPILibrary/Generated/Interfaces/IInvoiceConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public interface IInvoiceConnector : IConnector
Invoice CreditInvoice(int? id);
Invoice Email(int? id);
Invoice EInvoice(int? id);
Invoice Print(int? id);
Invoice PrintReminder(int? id);
byte[] Print(int? id);
byte[] PrintReminder(int? id);
Invoice ExternalPrint(int? id);
Invoice Preview(int? id);
byte[] Preview(int? id);

Task<Invoice> UpdateAsync(Invoice invoice);
Task<Invoice> CreateAsync(Invoice invoice);
Expand Down
4 changes: 2 additions & 2 deletions FortnoxAPILibrary/Generated/Interfaces/IOfferConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public interface IOfferConnector : IConnector
Offer CreateOrder(int? id);
Offer Cancel(int? id);
Offer Email(int? id);
Offer Print(int? id);
byte[] Print(int? id);
Offer ExternalPrint(int? id);
Offer Preview(int? id);
byte[] Preview(int? id);

Task<Offer> UpdateAsync(Offer offer);
Task<Offer> CreateAsync(Offer offer);
Expand Down
4 changes: 2 additions & 2 deletions FortnoxAPILibrary/Generated/Interfaces/IOrderConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public interface IOrderConnector : IConnector
Order CreateInvoice(int? id);
Order Cancel(int? id);
Order Email(int? id);
Order Print(int? id);
byte[] Print(int? id);
Order ExternalPrint(int? id);
Order Preview(int? id);
byte[] Preview(int? id);

Task<Order> UpdateAsync(Order order);
Task<Order> CreateAsync(Order order);
Expand Down

0 comments on commit 4acf00d

Please sign in to comment.