using RestSharp; using System; using System.Collections.Generic; using System.Linq; using Tanshu.Accounts.Entities; namespace Tanshu.Accounts.Repository { public static class VoucherBI { public static Voucher Insert(Voucher voucher, bool updateTable) { var request = new RestRequest(Method.PUT); request.Resource = "Voucher.json"; request.AddJsonBody(GetVoucherJson(voucher)); request.AddQueryParameter("u", true.ToString()); return JsonStore.Execute(request); } public static Guid? Update(Voucher voucher, bool updateTable) { var request = new RestRequest(Method.POST); request.Resource = "Voucher/{id}.json"; request.AddParameter("id", voucher.VoucherID, ParameterType.UrlSegment); request.AddJsonBody(GetVoucherJson(voucher)); request.AddQueryParameter("u", true.ToString()); voucher = JsonStore.Execute(request); return voucher.Kots.Last().KotID; } public static Voucher Get(Guid id) { var request = new RestRequest(); request.Resource = "Voucher/{id}.json"; request.AddParameter("id", id, ParameterType.UrlSegment); return JsonStore.Execute(request); } public static Voucher Get(string billID) { var request = new RestRequest(); request.Resource = "Voucher.json"; request.AddQueryParameter("b", billID); return JsonStore.Execute(request); } public static void VoidBill(Guid voucherID, string reason, bool updateTable) { var request = new RestRequest(Method.POST); request.Resource = "Void/{id}.json"; request.AddParameter("id", voucherID, ParameterType.UrlSegment); request.AddJsonBody(new { Reason = reason }); request.AddQueryParameter("u", true.ToString()); JsonStore.Execute(request); } public static Guid MergeKot(Guid kotID, Guid newVoucherID) { var request = new RestRequest(Method.POST); request.Resource = "MergeKot.json"; request.AddJsonBody(new { KotID = kotID, NewVoucherID = newVoucherID }); return JsonStore.Execute(request); } public static Guid MergeTables(Guid voucherID, Guid newVoucherID) { var request = new RestRequest(Method.POST); request.Resource = "MergeTable.json"; request.AddJsonBody(new { VoucherID = voucherID, NewVoucherID = newVoucherID }); return JsonStore.Execute(request); } public static Guid MoveKot(Guid kotID, Guid tableID) { var request = new RestRequest(Method.POST); request.Resource = "MoveKot.json"; request.AddJsonBody(new { KotID = kotID, FoodTableID = tableID }); return JsonStore.Execute(request); } public static Guid MoveTable(Guid voucherID, Guid tableID) { var request = new RestRequest(Method.POST); request.Resource = "MoveTable.json"; request.AddJsonBody(new { VoucherID = voucherID, FoodTableID = tableID }); JsonStore.Execute(request); return voucherID; } public static void SplitBill(Guid oldVoucherID, Voucher first, Voucher second) { var request = new RestRequest(Method.POST); request.Resource = "Split/{id}.json"; request.AddParameter("id", oldVoucherID, ParameterType.UrlSegment); request.AddJsonBody(new { One = GetVoucherJson(first), Two = GetVoucherJson(second) }); JsonStore.Execute(request); } public static Voucher DiscountPrintedBill(Guid oldVoucherID, Voucher newVoucher) { var request = new RestRequest(Method.POST); request.Resource = "ReprintVoucher/{id}.json"; request.AddParameter("id", oldVoucherID, ParameterType.UrlSegment); request.AddJsonBody(GetVoucherJson(newVoucher)); return JsonStore.Execute(request); } public static void SettleVoucher(User user, Guid voucherID, IDictionary values, bool updateTable) { var request = new RestRequest(Method.POST); request.Resource = "Settle/{id}.json"; request.AddParameter("id", voucherID, ParameterType.UrlSegment); request.AddJsonBody(values.Select(x => new { Settled = x.Key, Amount = x.Value })); request.AddQueryParameter("u", true.ToString()); JsonStore.Execute(request); } private static dynamic GetVoucherJson(Voucher voucher) { return new { Pax = voucher.Pax, Table = new { FoodTableID = voucher.Table.FoodTableID }, Customer = new { CustomerID = voucher.Customer.CustomerID }, Printed = voucher.Printed, VoucherType = voucher.VoucherType, User = new { UserID = voucher.User.UserID }, Kots = voucher.Kots.Select(k => new { KotID = k.KotID, Inventories = k.Inventories.Select(i => new { InventoryID = i.InventoryID, ProductID = i.Product.ProductID, Quantity = i.Quantity, Price = i.Price, Discount = i.Discount, IsHappyHour = i.IsHappyHour, ServiceCharge = i.ServiceCharge, IsScTaxable = i.IsScTaxable, ServiceTaxID = i.ServiceTax.TaxID, ServiceTaxRate = i.ServiceTaxRate, VatID = i.Vat.TaxID, VatRate = i.VatRate, Modifiers = i.InventoryModifier.Select(m => new { ModifierID = m.Modifier.ModifierID, Price = m.Price }) }) }), Settlements = voucher.Settlements.Select(s => new { Settled = s.Settled, Amount = s.Amount }) }; } } }