using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using DotNetDBF; using Newtonsoft.Json.Linq; namespace BodyshopPartner.Utils { public static class OEConnection { private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); private static string workingDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); private static string emsBasePath = workingDirectory + @"\EmsBase"; private static string TempEmsDir = @"C:\VPS\TEMPEMS"; private static Encoding DbfFileCharEncoding = Encoding.GetEncoding("UTF-8"); private static byte DbfFileSignature = DBFSignature.DBase3; private static byte DbfFileLanguageDriver = 0x37; public static void SendToOEConnection(dynamic PartsOrder) { GenerateAd1File(PartsOrder.job); } public static DBFField[] ReadDbfFields(string dbfFilePath) { using (var blankEmsFileStream = File.Open(dbfFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (var reader = new DBFReader(blankEmsFileStream)) { return reader.Fields; } } } public static string GenerateFileName(dynamic Job) { if (string.IsNullOrEmpty(Job.clm_no?.Value)) { return Job.ro_number?.Value; } return Job.clm_no; } public static void GenerateAd1File(dynamic Job) { try { DBFField[] Ad1Fields = ReadDbfFields(emsBasePath + @"\EMSA.AD1"); using (var fileStream = File.Create(TempEmsDir + $@"\{GenerateFileName(Job)}A.ad1")) { using (var writer = new DBFWriter()) { writer.CharEncoding = DbfFileCharEncoding; writer.Signature = DbfFileSignature; writer.LanguageDriver = DbfFileLanguageDriver; writer.Fields = Ad1Fields; writer.AddRecord( Job.ins_co_id?.Value, Job.ins_co_nm?.Value, Job.ins_addr1?.Value, Job.ins_addr2?.Value, Job.ins_city?.Value, Job.ins_st?.Value, Job.ins_zip?.Value, Job.ins_ctry?.Value, Job.ins_ph1?.Value, Job.ins_ph1x?.Value, Job.ins_ph2?.Value, Job.ins_ph2x?.Value, Job.ins_fax?.Value, Job.ins_faxx?.Value, Job.ins_ct_ln?.Value, Job.ins_ct_fn?.Value, Job.ins_title?.Value, Job.ins_ct_ph?.Value, Job.ins_ct_phx?.Value, Job.ins_ea?.Value, Job.ins_memo?.Value, Job.policy_no?.Value, Job.ded_amt?.Value, Job.ded_status?.Value, Job.asgn_no?.Value, string.IsNullOrEmpty(Job.asgn_date?.Value) ? null : DateTime.Parse(Job.asgn_date?.Value), Job.asgn_type?.Value, Job.clm_no?.Value, Job.clm_ofc_id?.Value, Job.clm_ofc_nm?.Value, Job.clm_addr1?.Value, Job.clm_addr2?.Value, Job.clm_city?.Value, Job.clm_st?.Value, Job.clm_zip?.Value, Job.clm_ctry?.Value, Job.clm_ph1?.Value, Job.clm_ph1x?.Value, Job.clm_ph2?.Value, Job.clm_ph2x?.Value, Job.clm_fax?.Value, Job.clm_faxx?.Value, Job.clm_ct_ln?.Value, Job.clm_ct_fn?.Value, Job.clm_title?.Value, Job.clm_ct_ph?.Value, Job.clm_ct_phx?.Value, Job.clm_ea?.Value, Job.payee_nms?.Value, Job.pay_type?.Value, Job.pay_date?.Value, Job.pay_chknm?.Value, Job.pay_amt?.Value, Job.pay_memo?.Value, Job.agt_co_id?.Value, Job.agt_co_nm?.Value, Job.agt_addr1?.Value, Job.agt_addr2?.Value, Job.agt_city?.Value, Job.agt_st?.Value, Job.agt_zip?.Value, Job.agt_ctry?.Value, Job.agt_ph1?.Value, Job.agt_ph1x?.Value, Job.agt_ph2?.Value, Job.agt_ph2x?.Value, Job.agt_fax?.Value, Job.agt_faxx?.Value, Job.agt_ct_ln?.Value, Job.agt_ct_fn?.Value, Job.agt_ct_ph?.Value, Job.agt_ct_phx?.Value, Job.agt_ea?.Value, Job.agt_lic_no?.Value, string.IsNullOrEmpty(Job.loss_date?.Value) ? null : DateTime.Parse(Job.loss_date?.Value), Job.loss_cat?.Value, Job.loss_type?.Value, Job.loss_desc?.Value, Job.theft_ind?.Value, Job.cat_no?.Value, Job.tlos_ind?.Value, Job.loss_memo?.Value, Job.cust_pr?.Value, Job.insd_ln?.Value, Job.insd_fn?.Value, Job.insd_title?.Value, Job.insd_co_nm?.Value, Job.insd_addr1?.Value, Job.insd_addr2?.Value, Job.insd_city?.Value, Job.insd_st?.Value, Job.insd_zip?.Value, Job.insd_ctry?.Value, Job.insd_ph1?.Value, Job.insd_ph1x?.Value, Job.insd_ph2?.Value, Job.insd_ph2x?.Value, Job.insd_fax?.Value, Job.insd_faxx?.Value, Job.insd_ea?.Value, Job.ownr_ln?.Value, Job.ownr_fn?.Value, Job.ownr_title?.Value, Job.ownr_co_nm?.Value, Job.ownr_addr1?.Value, Job.ownr_addr2?.Value, Job.ownr_city?.Value, Job.ownr_st?.Value, Job.ownr_zip?.Value, Job.ownr_ctry?.Value, Job.ownr_ph1?.Value, Job.ownr_ph1x?.Value, Job.ownr_ph2?.Value, Job.ownr_ph2x?.Value, Job.ownr_fax?.Value, Job.ownr_faxx?.Value, Job.ownr_ea.Value ); ; writer.Write(fileStream); } } } catch (Exception ex) { logger.Error(ex,"Error when creating AD1 file."); } } } }