Example #1
0
        private async Task SendApexPOAmendments()
        {
            using (ApexDataDataContext apexData = new ApexDataDataContext(_SqlConnBuilder.ConnectionString))
            {
                try
                {
                    var dlo = new DataLoadOptions();
                    dlo.LoadWith <PO>(p => p.POLines);  //grab the lines at the same time for efficiency
                    apexData.LoadOptions = dlo;

                    List <PO> apexPOList = apexData.POs.Where(p => (p.Vendor ?? "") != "" &&
                                                              (((p.Job ?? "") != "") || ((p.WorkOrd ?? "") != "")) &&
                                                              ((p.ExpBatch == 0) || (p.ExpBatch == -1)) && (p.ExpSent ?? "F") == "T" &&
                                                              (p.POStatus == "F" || p.POStatus == "C")).ToList();

                    if (apexPOList.Count == 0)
                    {
                        ProgressInfo.Add("There are no purchase order amendments to send.");
                    }
                    else
                    {
                        _POSent = true;  //We have a valid P/O to send so present the interface status report when complete

                        using (var writer = new StreamWriter(_POAmendDirectory + @"\POAmend-" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".csv"))
                            using (var csv = new CsvWriter(writer))
                            {
                                _AmendmentList.Clear();
                                //_AmendmentList.Add(POAmendmentCSVRecord.BuildLine1());
                                _AmendmentList.Add(POAmendmentCSVRecord.BuildLine2());

                                foreach (PO porec in apexPOList)
                                {
                                    _AmendmentList.AddRange(await ProcessPOAmendment(porec, apexData));
                                }

                                csv.WriteRecords(_AmendmentList);
                            }
                    }
                }

                catch (Exception ex)
                {
                    MessageBox.Show("Error:" + ex.Message, "Unexpected error");
                }
            }
        }
Example #2
0
        private async Task SendApexPOs()
        {
            using (ApexDataDataContext apexData = new ApexDataDataContext(_SqlConnBuilder.ConnectionString))
            {
                try
                {
                    _StatusLines.Clear();

                    var dlo = new DataLoadOptions();
                    dlo.LoadWith <PO>(p => p.POLines);  //grab the lines at the same time for efficiency
                    apexData.LoadOptions = dlo;

                    List <PO> apexPOList = apexData.POs.Where(p => (p.Vendor ?? "") != "" &&
                                                              (((p.Job ?? "") != "") || ((p.WorkOrd ?? "") != "")) &&
                                                              ((p.ExpBatch == 0) || (p.ExpBatch == -1)) &&
                                                              ((p.POStatus == "F" || p.POStatus == "C"))).ToList();

                    if (apexPOList.Count == 0)
                    {
                        ProgressInfo.Add("There are no purchase orders to send.");
                    }
                    else
                    {
                        _POSent = true;  //We have a valid P/O to send so present the interface status report when complete

                        foreach (PO porec in apexPOList)
                        {
                            await ProcessPO(porec, apexData);
                        }
                    }
                }

                catch (Exception ex)
                {
                    MessageBox.Show("Error:" + ex.Message, "Unexpected error");
                }
            }
        }
Example #3
0
        private void btnResetPO_Click(object sender, RoutedEventArgs e)
        {
            //This is ugly for now but I am in a hurry to get this functionality
            var po = Microsoft.VisualBasic.Interaction.InputBox("P/O:", "Reset a P/O to show as not sent to COINS");

            using (ApexDataDataContext apexData = new ApexDataDataContext(_SqlConnBuilder.ConnectionString))
            {
                PO apexPO = apexData.POs.Where(s => s.Po1 == po.PadLeft(12)).SingleOrDefault();
                if (apexPO == null)
                {
                    MessageBox.Show("P/O not found", "Unable to Reset P/O", MessageBoxButton.OK);
                    return;
                }
                else
                {
                    apexPO.ExpBatch = -1;
                    apexPO.ExpSent  = "F";
                    apexData.SubmitChanges();

                    apexData.ExecuteCommand("DELETE FROM COINSESB_ExpL WHERE PO = '" + apexPO.Po1 + "'");
                    MessageBox.Show("P/O reset as requested", "Reset Successful", MessageBoxButton.OK);
                }
            }
        }
Example #4
0
 private void btnDiscard_Click(object sender, RoutedEventArgs e)
 {
     dc = new ApexDataDataContext();
     grdExpL.ItemsSource = dc.COINSESB_ExpLs;
     MessageBox.Show("Changes discarded");
 }
Example #5
0
        private async Task <List <POAmendmentCSVRecord> > ProcessPOAmendment(PO porec, ApexDataDataContext apexData)
        {
            ProgressInfo.Add($"Filing P/O amendment {porec.Po1.Trim()}");

            ApexSystem apexSystem = apexData.ApexSystems.FirstOrDefault();

            List <POAmendmentCSVRecord> csvList = new List <POAmendmentCSVRecord>();

            if (porec.Job != null && porec.Job != "") //Job based P/O
            {
                POAmendmentCSVRecord pocsvrec = BuildJobBasedAmendmentHeader(porec, apexSystem);

                for (int i = 0; i < porec.POLines.Count; i++)
                {
                    POLine polinerec = porec.POLines[i];

                    string jobphase = String.IsNullOrEmpty(porec.JobPhase) ? "00" : porec.JobPhase;
                    string wbs;

                    COINSESB_WB wbsRec = apexData.COINSESB_WBs.Where(s => s.Job == porec.Job).FirstOrDefault();
                    if (wbsRec == null || !(wbsRec.UsesActivity ?? false))
                    {
                        wbs = jobphase + "-";
                    }
                    else
                    {
                        wbs = jobphase + "-00-";
                    }

                    pocsvrec = BuildJobBasedAmendmentLine(polinerec, wbs, pocsvrec);
                    csvList.Add(pocsvrec);
                    pocsvrec = CreateNotNewCSVRec(); //Clear the header portion for subsequent line items
                }
            }
            else //Work Order based P/O
            {
                POAmendmentCSVRecord pocsvrec = BuildWOBasedAmendmentHeader(porec, apexSystem);

                for (int i = 0; i < porec.POLines.Count; i++)
                {
                    POLine polinerec = porec.POLines[i];

                    pocsvrec = BuildWOBasedAmemdmentPOLine(porec, polinerec, pocsvrec);
                    csvList.Add(pocsvrec);
                    pocsvrec = CreateNotNewCSVRec(); //Clear the header portion for subsequent line items
                }
            }

            _StatusLines.Add(new StatusLine
            {
                PO      = porec.Po1?.Trim(),
                Job     = porec.Job?.Trim(),
                WorkOrd = porec.WorkOrd?.Trim(),
                Vendor  = porec.Vendor?.Trim(),
                Message = "Amendment written to file"
            });

            return(csvList);
        }
Example #6
0
        private async Task ProcessPO(PO porec, ApexDataDataContext apexData)
        {
            ProgressInfo.Add($"Sending P/O {porec.Po1.Trim()}");

            string reportMessage;

            ApexSystem apexSystem = apexData.ApexSystems.FirstOrDefault();

            COINSESBService.COINSInterfaceHeader    header;
            COINSESBService.COINSInterfacePo_hdrRow poheader;

            header = new COINSESBService.COINSInterfaceHeader
            {
                id          = (apexSystem.ExportBatch ?? 1).ToString(),
                confirm     = COINSESBService.COINSInterfaceHeaderConfirm.no,
                UserID      = "pkramer",
                From        = "Apex",
                HostName    = "coinsoa",
                Environment = "live",
                Created     = DateTime.UtcNow,
                Login       = new COINSESBService.COINSInterfaceHeaderLogin()
                {
                    User     = "******",
                    Password = "******",
                    CID      = 1
                }
            };

            if (!String.IsNullOrEmpty(porec.Job)) //Job based P/O
            {
                poheader = BuildJobBasedPOHeader(porec, apexSystem);

                poheader.po_lineRow = new COINSESBService.COINSInterfacePo_hdrRowPo_lineRow[porec.POLines.Count];

                for (int i = 0; i < porec.POLines.Count; i++)
                {
                    POLine polinerec = porec.POLines[i];

                    string jobphase = String.IsNullOrEmpty(porec.JobPhase) ? "00" : porec.JobPhase;
                    string wbs;

                    COINSESB_WB wbsRec = apexData.COINSESB_WBs.Where(s => s.Job == porec.Job).FirstOrDefault();
                    if (wbsRec == null || !(wbsRec.UsesActivity ?? false))
                    {
                        wbs = jobphase + "-";
                    }
                    else
                    {
                        wbs = jobphase + "-00-";
                    }

                    COINSESB_ExpL expL      = apexData.COINSESB_ExpLs.Where(s => s.PO == polinerec.Po && s.POLine == polinerec.PoLine1).SingleOrDefault();
                    int?          pol_seq   = expL?.POL_Seq;
                    decimal?      lastPrice = expL?.LastPrice;

                    string cat      = "MA";
                    string schedule = "STD";
                    Job    job      = apexData.Jobs.Where(s => s.Job1 == porec.Job).FirstOrDefault();
                    if (job != null)
                    {
                        schedule = job.Schedule;
                        Costcode ccd = apexData.Costcodes.Where(s => s.Schedule == schedule && s.CostCode1 == polinerec.CostCode).FirstOrDefault();
                        if (ccd != null)
                        {
                            cat = ccd.GL;
                        }
                    }

                    COINSESBService.COINSInterfacePo_hdrRowPo_lineRow
                        polinerow = BuildJobBasedPOLine(polinerec, wbs, pol_seq, cat,
                                                        expL != null && ((polinerec.Price ?? 0) != (lastPrice ?? 0)), porec.ShipDate ?? DateTime.Now);
                    poheader.po_lineRow[i] = polinerow;
                }
            }
            else //Work Order based P/O
            {
                poheader = BuildWOBasedPOHeader(porec, apexSystem);

                poheader.po_lineRow = new COINSESBService.COINSInterfacePo_hdrRowPo_lineRow[porec.POLines.Count];

                for (int i = 0; i < porec.POLines.Count; i++)
                {
                    POLine polinerec = porec.POLines[i];

                    COINSESB_ExpL expL      = apexData.COINSESB_ExpLs.Where(s => s.PO == polinerec.Po && s.POLine == polinerec.PoLine1).SingleOrDefault();
                    int?          pol_seq   = expL?.POL_Seq;
                    decimal?      lastPrice = expL?.LastPrice;

                    string   cat      = "MA";
                    string   schedule = "STD";
                    Costcode ccd      = apexData.Costcodes.Where(s => s.Schedule == schedule && s.CostCode1 == polinerec.CostCode).FirstOrDefault();
                    if (ccd != null)
                    {
                        cat = ccd.GL;
                    }

                    COINSESBService.COINSInterfacePo_hdrRowPo_lineRow
                        polinerow = BuildWOBasedPOLine(porec, polinerec, pol_seq, cat,
                                                       expL != null && ((polinerec.Price ?? 0) != (lastPrice ?? 0)), porec.ShipDate ?? DateTime.Now);
                    poheader.po_lineRow[i] = polinerow;
                }
            }

            COINSESBService.COINSInterfacePo_hdrRow[] poheaderrows = new COINSESBService.COINSInterfacePo_hdrRow[1];
            poheaderrows[0] = poheader;

            var client        = new COINSESBService.COINSInterfacePortClient("COINSInterface");
            var actionrequest = new COINSESBService.doActionRequest
            {
                Header = header,
                Body   = poheaderrows
            };

#if SENDXMLDIAGNOSTIC
            XmlSerializer serializer = new XmlSerializer(typeof(COINSESBService.doActionRequest));
            using (MemoryStream stream = new MemoryStream())
            {
                serializer.Serialize(stream, actionrequest);
                stream.Seek(0, SeekOrigin.Begin);
                string     capturedXML  = stream.ReadToString();
                HttpClient postClient   = new HttpClient();
                var        postResponse = await postClient.PostAsync("http://apexcoin.apexpurchasing.com/api/coinsxml",
                                                                     new StringContent(capturedXML));
            }
#endif

            var response = await client.doActionAsync(actionrequest);

            if (response.Header.action == COINSESBService.COINSInterfaceResponseHeaderAction.RESPONSE)
            {
                if (porec.ExpSent == "T")
                {
                    reportMessage = "Sent to COINS (Amendment)";
                }
                else
                {
                    reportMessage = "Sent to COINS";
                }

                porec.ExpSent = "T";
                var lines         = response.Body.po_hdrRow[0].po_lineRow;
                int lineRowNumber = 0;
                foreach (var POLine in porec.POLines)
                {
                    var  pol_seq   = lines[lineRowNumber++].pol_seq;
                    bool newRecord = false;
                    var  expL      = apexData.COINSESB_ExpLs.Where(s => s.PO == POLine.Po && s.POLine == POLine.PoLine1).SingleOrDefault();
                    if (expL == null)
                    {
                        expL = new COINSESB_ExpL
                        {
                            PO     = POLine.Po,
                            POLine = POLine.PoLine1
                        };
                        newRecord = true;
                    }

                    expL.POL_Seq   = pol_seq;
                    expL.LastAmt   = POLine.Ext;
                    expL.LastPrice = POLine.Price;
                    if (newRecord)
                    {
                        apexData.COINSESB_ExpLs.InsertOnSubmit(expL);
                    }
                    apexData.SubmitChanges();
                }
            }
            else if (response.Header.action == COINSESBService.COINSInterfaceResponseHeaderAction.EXCEPTION)
            {
                reportMessage = response.Body.Exception.Exception;
            }
            else
            {
                reportMessage = "Unknown response from COINS";
            }

            porec.ExpBatch          = apexSystem.ExportBatch ?? 1;
            apexSystem.ExportBatch += 1;
            apexData.SubmitChanges();

            _StatusLines.Add(new StatusLine
            {
                PO      = porec.Po1?.Trim(),
                Job     = porec.Job?.Trim(),
                WorkOrd = porec.WorkOrd?.Trim(),
                Vendor  = porec.Vendor?.Trim(),
                Message = reportMessage
            });
        }