public static void CreateOrder(XSupport XSupport, DataRow OrderRow, DataTable OrderItems, decimal vatPercentage, DataTable contactsForSync, DataTable productsForSync)
{
int warehouse = Convert.ToInt32(Main.GetSettingValueByName(XSupport, "Warehouse"));
bool invoice = Convert.ToBoolean(OrderRow["Invoice"]);
var trdr = invoice
? contactsForSync.AsEnumerable()
.Where(c => Convert.ToBoolean(c.Field<long>("is_company"))
&& c.Field<UInt32>("id") == (UInt32)OrderRow["company_id"])
.FirstOrDefault()
: contactsForSync.AsEnumerable()
.Where(c => !Convert.ToBoolean(c.Field<long>("is_company"))
&& c.Field<UInt32>("id") == (UInt32)OrderRow["payments_contact_id"])
.FirstOrDefault();
int Series = 0;
if (invoice)
{
Series = Convert.ToInt32(Main.GetSettingValueByName(XSupport, "OrderSeries"));
}
else
{
Series = Convert.ToInt32(Main.GetSettingValueByName(XSupport, "OrderSeriesRetail"));
}
int? paymentsMethod = OrderRow["payments_method"] != DBNull.Value
? Convert.ToInt32(OrderRow["payments_method"])
: (int?)null;
int? shipmentsMethod = OrderRow["shipments_method"] != DBNull.Value
? Convert.ToInt32(OrderRow["shipments_method"])
: (int?)null;
Main.SalDocModule.InsertData();
using (XTable tab = Main.SalDocModule.GetTable("FINDOC"))
{
tab.Current.Insert();
tab.Current["SERIES"] = Series;
tab.Current["TRNDATE"] = Convert.ToDateTime(OrderRow["created"]);
tab.Current["TRDR"] = Convert.ToInt32(trdr["TRDR"]);
tab.Current["PAYMENT"] = paymentsMethod ?? (object)DBNull.Value;
tab.Current["SHIPMENT"] = shipmentsMethod ?? (object)DBNull.Value;
tab.Current["INT01"] = Convert.ToInt32(OrderRow["id"]);
}
using (XTable MtrDoc = Main.SalDocModule.GetTable("MTRDOC"))
{
MtrDoc.Current["SHIPPINGADDR"] = trdr["address"].ToString();
MtrDoc.Current["SHPZIP"] = trdr["postcode"].ToString();
MtrDoc.Current["SHPCITY"] = trdr["city"].ToString();
MtrDoc.Current["WHOUSE"] = warehouse; //Εδώ δημιουργείται το πρόβλημα
}
using (XTable tabLines = Main.SalDocModule.GetTable("ITELINES"))
{
foreach (DataRow row in OrderItems.Rows)
{
int mtrl = productsForSync.AsEnumerable()
.Where(c => c.Field<UInt32>("id") == (UInt32)row["product_id"])
.Select(c => Convert.ToInt32(c.Field<UInt64>("MTRL")))
.FirstOrDefault();
tabLines.Current.Insert();
tabLines.Current["MTRL"] = mtrl;
tabLines.Current["QTY1"] = Convert.ToDouble(row["quantity"]);
decimal price = invoice
? Convert.ToDecimal(row["price"]) / ((vatPercentage / 100) + 1)
: Convert.ToDecimal(row["price"]);
tabLines.Current["PRICE"] = Convert.ToDouble(price);
}
}
decimal shipmentsPrice = (decimal)OrderRow["shipments_price"];
decimal paymentsPrice = (decimal)OrderRow["payments_price"];
if (shipmentsPrice > 0 || paymentsPrice > 0)
{
using (XTable expDoc = Main.SalDocModule.GetTable("EXPANAL"))
{
if (shipmentsPrice > 0)
{
expDoc.Current.Insert();
expDoc.Current["EXPN"] = Convert.ToInt32(Main.GetSettingValueByName(XSupport, "EXP_Transport"));
expDoc.Current["EXPVAL"] = Convert.ToDouble(shipmentsPrice) / 1.24;
}
if (paymentsPrice > 0)
{
expDoc.Current.Insert();
expDoc.Current["EXPN"] = Convert.ToInt32(Main.GetSettingValueByName(XSupport, "EXP_PayOnDelivery"));
expDoc.Current["EXPVAL"] = Convert.ToDouble(paymentsPrice) / 1.24;
}
}
}
Main.SalDocModule.PostData();
}