public HttpResponseMessage detail(string repair_code)
        {
            //Service
            var b3gRepair    = new b3gRepair();
            var sapSaleOrder = new sapSaleOrder();
            var sapCustomer  = new sapCustomer();
            var sapBilling   = new sapBilling();

            var result = new RepairForm();
            var rh     = new RepairHeader();
            var rp     = new RepairAppointment();
            ICollection <RepairItems> ri = new List <RepairItems>();
            ICollection <RepairRaw>   rr = new List <RepairRaw>();

            try
            {
                afs_repair_header h = b3gRepair.GetRepairHeader(repair_code);
                if (h == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Repair data not found"));
                }
                ;

                rh.repair_code      = h.REPAIR_CODE;
                rh.repair_date      = h.REPAIR_DATE;
                rh.so_number        = h.SO_NUMBER;
                rh.soldto_code      = h.SOLDTO_CODE;
                rh.soldto_name      = h.SOLDTO_NAME;
                rh.status           = h.STATUS;
                rh.transport_amount = h.TRANSPORT_AMOUNT;
                rh.contact_tel      = h.CONTACT_TEL;
                rh.remark           = h.REMARK;
                rh.created_by       = h.CREATED_BY;
                rh.created_date     = h.CREATED_DATE;
                rh.update_by        = h.UPDATE_BY;
                rh.update_date      = h.UPDATE_DATE;

                var vbak = sapSaleOrder.GetVBAK(client, h.SO_NUMBER);
                rh.so_date = DateTime.ParseExact(vbak.ERDAT, "yyyyMMdd", null);
                //rh.soldto_code = vbak.KUNNR;

                ////Get Customer Detail
                var kna1 = sapCustomer.GetKNA1(vbak.MANDT, rh.soldto_code);
                //rh.soldto_name = kna1.NAME1 + kna1.NAME2;

                //Get Customer Address
                var adrc = sapCustomer.GetADRC(kna1.MANDT, kna1.ADRNR);
                rh.soldto_address = sapCustomer.ConcateAddress(adrc.Where(w => w.NATION == " ").FirstOrDefault());

                //Get Partner Customer
                var vbpa = sapSaleOrder.GetVBPA(vbak.MANDT, vbak.VBELN, "000000");

                //Ship-To Address
                var partner = vbpa.Where(w => w.PARVW == "WE").FirstOrDefault();
                rh.shipto_code    = partner.KUNNR;
                kna1              = sapCustomer.GetKNA1(vbak.MANDT, partner.KUNNR);
                adrc              = sapCustomer.GetADRC(kna1.MANDT, kna1.ADRNR);
                rh.shipto_address = sapCustomer.ConcateAddress(adrc.Where(w => w.NATION == " ").FirstOrDefault());

                //Get Sale Rep name
                partner         = vbpa.Where(w => w.PARVW == "ZR").FirstOrDefault();
                rh.salerep_code = partner.KUNNR;
                var salerep = sapCustomer.GetKNA1(partner.MANDT, partner.KUNNR);
                rh.salerep_name = salerep.NAME1 + salerep.NAME2;

                //Get Billing
                var vbrks = sapBilling.GetVBRKbySONumber(vbak.MANDT, vbak.VBELN);
                var vbrk  = vbrks.OrderBy(o => o.FKDAT).FirstOrDefault();
                rh.billing_date = DateTime.ParseExact(vbrk.FKDAT, "yyyyMMdd", null);

                ICollection <afs_repair_items> items = b3gRepair.GetRepairItem(repair_code);
                if (items == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Repair item data not found"));
                }
                ;

                //Items
                foreach (var item in items)
                {
                    //Get Sale Order item
                    var vbap = sapSaleOrder.GetVBAP(vbak.MANDT, item.SO_NUMBER, item.SO_ITEM);
                    if (vbap == null)
                    {
                        vbap = new VBAP();
                    }

                    //Get Images
                    List <RepairImages> ims = new List <RepairImages>();
                    var images = b3gRepair.GetRepairImage(item.REPAIR_ITEM_ID);
                    if (images == null)
                    {
                        images = new List <afs_repair_images>();
                    }
                    foreach (var img in images)
                    {
                        var im = new RepairImages();
                        im.repair_image_id = img.REPAIR_IMAGE_ID;
                        im.repair_code     = img.REPAIR_CODE;
                        im.repair_item_id  = img.REPAIR_ITEM_ID;
                        im.so_number       = img.SO_NUMBER;
                        im.so_item         = img.SO_ITEM;
                        im.filename        = img.FILENAME;
                        im.base64          = img.BASE64;
                        ims.Add(im);
                    }

                    ri.Add(new RepairItems
                    {
                        select           = true,
                        repair_item_id   = item.REPAIR_ITEM_ID,
                        repair_code      = item.REPAIR_CODE,
                        so_number        = item.SO_NUMBER,
                        so_item          = item.SO_ITEM,
                        article_number   = vbap.MATNR,
                        article_name     = vbap.ARKTX,
                        repair_item_type = item.REPAIR_ITEM_TYPE,
                        qty           = item.REPAIR_QTY,
                        repair_remark = item.REPAIR_REMARK,
                        repair_desc   = item.REPAIR_DESC,
                        repair_type   = item.REPAIR_TYPE,
                        waranty       = item.WARANTY,
                        images        = ims
                    });
                }

                //Appointment
                afs_repair_appointment p = b3gRepair.GetRepairAppointment(repair_code);
                if (p != null)
                {
                    rp.repair_code       = p.REPAIR_CODE;
                    rp.repair_appoint_id = p.REPAIR_APPOINT_ID;
                    rp.appointment_date  = p.APPOINTMENT_DATE;
                    rp.appointment_time  = p.APPOINTMENT_TIME;
                    rp.target_date       = p.TARGET_DATE;
                    rp.technician_team   = p.TECHNICIAN_TEAM;
                    rp.price_amount      = p.PRICE_AMOUNT;
                    rp.price_extra       = p.PRICE_EXTRA;
                    rp.remark_customer   = p.REMARK_CUSTOMER;
                    rp.created_by        = p.CREATED_BY;
                    rp.created_date      = p.CREATED_DATE;
                    rp.update_by         = p.UPDATE_BY;
                    rp.update_date       = p.UPDATE_DATE;
                }
                ;

                //Raw
                ICollection <afs_repair_raw> raws = b3gRepair.GetRepairRaw(repair_code);
                if (raws != null)
                {
                    foreach (var raw in raws)
                    {
                        rr.Add(new RepairRaw
                        {
                            repair_raw_id = raw.REPAIR_RAW_ID,
                            repair_code   = raw.REPAIR_CODE,
                            raw_name      = raw.RAW_NAME,
                            raw_qty       = raw.RAW_QTY,
                            status        = raw.STATUS,
                            raw_date      = raw.RAW_DATE,
                            remark        = raw.REMARK,
                            created_by    = raw.CREATED_BY,
                            created_date  = raw.CREATED_DATE,
                            update_by     = raw.UPDATE_BY,
                            update_date   = raw.UPDATE_DATE
                        });
                    }
                }

                result.header  = rh;
                result.items   = ri;
                result.appoint = rp;
                result.raws    = rr;

                return(result.header == null || result.items == null
                         ? Request.CreateErrorResponse(HttpStatusCode.NotFound, "Repair data not found")
                         : Request.CreateResponse(HttpStatusCode.OK, result));
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex));
            }
        }
        public HttpResponseMessage search_list(RepairSearchModel search)
        {
            var loadUser      = new loadUser();
            var sapSaleOrder  = new sapSaleOrder();
            var sapCustomer   = new sapCustomer();
            var sapBilling    = new sapBilling();
            var BranchService = new sapBranch();

            search.fetchdata = search.fetchdata == null ? new fetchdata() : search.fetchdata;
            var result = new List <RepairHeader>();
            var rh     = new RepairHeader();
            ICollection <RepairItems> ri = new List <RepairItems>();

            try
            {
                Expression <Func <afs_repair_header, bool> > RepairDateFromExpression;
                if (!string.IsNullOrEmpty(search.repair_date_from))
                {
                    var datefrom = DateTime.ParseExact(search.repair_date_from, "dd/MM/yyyy", null);
                    //RepairDateFromExpression = gto => gto.REPAIR_DATE.Value.ToString("dd/MM/yyyy").CompareTo(search.repair_date_from) >= 0;
                    RepairDateFromExpression = gto => gto.REPAIR_DATE >= datefrom;
                }
                else
                {
                    RepairDateFromExpression = gto => 1 == 1;
                }

                Expression <Func <afs_repair_header, bool> > RepairDateToExpression;
                if (!string.IsNullOrEmpty(search.repair_date_to))
                {
                    var dateto = DateTime.ParseExact(search.repair_date_to + " 23:59:59", "dd/MM/yyyy HH:mm:ss", null);
                    //RepairDateToExpression = gto => gto.REPAIR_DATE.Value.ToString("dd/MM/yyyy").CompareTo(search.repair_date_to) <= 0;
                    RepairDateToExpression = gto => gto.REPAIR_DATE <= dateto;
                }
                else
                {
                    RepairDateToExpression = gto => 1 == 1;
                }

                Expression <Func <afs_repair_header, bool> > SoldToNameExpression;
                if (!string.IsNullOrEmpty(search.soldto_name))
                {
                    SoldToNameExpression = gto => gto.SOLDTO_NAME.Contains(search.soldto_name);
                }
                else
                {
                    SoldToNameExpression = gto => 1 == 1;
                }

                Expression <Func <afs_repair_header, bool> > SoNumberExpression;
                if (!string.IsNullOrEmpty(search.sonumber))
                {
                    SoNumberExpression = gto => gto.SO_NUMBER == search.sonumber;
                }
                else
                {
                    SoNumberExpression = gto => 1 == 1;
                }

                Expression <Func <afs_repair_header, bool> > RepairStatusExpression;
                if (search.status != "ALL" && !string.IsNullOrEmpty(search.status))
                {
                    RepairStatusExpression = gto => gto.STATUS == search.status;
                }
                else
                {
                    RepairStatusExpression = gto => 1 == 1;
                }

                Expression <Func <afs_repair_header, bool> > RepairCodeExpression;
                if (!string.IsNullOrEmpty(search.repair_code))
                {
                    RepairCodeExpression = gto => gto.REPAIR_CODE == search.repair_code;
                }
                else
                {
                    RepairCodeExpression = gto => 1 == 1;
                }

                using (var context = new SAPContext())
                {
                    var repair_headers = context.afs_repair_header
                                         .Where(RepairCodeExpression)
                                         .Where(RepairDateFromExpression)
                                         .Where(RepairDateToExpression)
                                         .Where(SoNumberExpression)
                                         .Where(SoldToNameExpression)
                                         .Where(RepairStatusExpression)
                                         .ToList();

                    foreach (var item in repair_headers)
                    {
                        var vbak = sapSaleOrder.GetVBAK(client, item.SO_NUMBER);

                        rh             = new RepairHeader();
                        rh.repair_code = item.REPAIR_CODE;
                        rh.repair_date = item.REPAIR_DATE;
                        rh.so_number   = item.SO_NUMBER;
                        rh.soldto_code = item.SOLDTO_CODE;
                        rh.soldto_name = item.SOLDTO_NAME;
                        rh.saleorg     = vbak.VKORG;
                        rh.distchannel = vbak.VTWEG;
                        rh.division    = vbak.SPART;
                        rh.salegroup   = vbak.VKGRP;
                        rh.saleoffice  = vbak.VKBUR;

                        var branch = BranchService.GetZBRANCH(client, rh.saleoffice);
                        if (branch != null)
                        {
                            rh.saleoffice_name = branch.HOST_DESC;
                        }

                        //Get Partner Customer
                        var vbpa = sapSaleOrder.GetVBPA(vbak.MANDT, vbak.VBELN, "000000");

                        //Get Sale Rep name
                        var partner = vbpa.Where(w => w.PARVW == "ZR").FirstOrDefault();
                        rh.salerep_code = partner.KUNNR;
                        var salerep = sapCustomer.GetKNA1(partner.MANDT, partner.KUNNR);
                        rh.salerep_name = salerep.NAME1 + salerep.NAME2;

                        rh.status           = item.STATUS;
                        rh.transport_amount = item.TRANSPORT_AMOUNT;
                        rh.contact_tel      = item.CONTACT_TEL;
                        rh.remark           = item.REMARK;
                        rh.created_by       = item.CREATED_BY;

                        var ud = loadUser.GetUser(rh.created_by);
                        if (ud != null)
                        {
                            rh.created_name = ud.firstname + ' ' + ud.lastname;
                        }
                        ;

                        rh.created_date = item.CREATED_DATE;
                        rh.update_by    = item.UPDATE_BY;

                        ud = loadUser.GetUser(rh.update_by);
                        if (ud != null)
                        {
                            rh.update_name = ud.firstname + ' ' + ud.lastname;
                        }
                        ;

                        rh.update_date = item.UPDATE_DATE;
                        result.Add(rh);
                    }
                }

                return(result.Count() == 0
                         ? Request.CreateErrorResponse(HttpStatusCode.NotFound, "Repair data not found")
                         : Request.CreateResponse(HttpStatusCode.OK, result));
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.InnerException.Message));
            }
        }
        public HttpResponseMessage PostSearh(RepairSearchModel search)
        {
            var sapSaleOrder = new sapSaleOrder();
            var sapCustomer  = new sapCustomer();
            var sapBilling   = new sapBilling();

            search.fetchdata = search.fetchdata == null ? new fetchdata() : search.fetchdata;
            var result = new RepairForm();
            var rh     = new RepairHeader();
            ICollection <RepairItems> ri = new List <RepairItems>();

            try
            {
                var vbak = sapSaleOrder.GetVBAK(client, search.sonumber);
                if (vbak == null || vbak.VBTYP != "C")
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "VBAK not found"));
                }
                ;
                rh.so_number   = vbak.VBELN;
                rh.so_date     = DateTime.ParseExact(vbak.ERDAT, "yyyyMMdd", null);
                rh.soldto_code = vbak.KUNNR;

                //Get Customer Detail
                var kna1 = sapCustomer.GetKNA1(vbak.MANDT, vbak.KUNNR);
                rh.soldto_name = kna1.NAME1 + kna1.NAME2;

                //Get Customer Address
                var adrc = sapCustomer.GetADRC(kna1.MANDT, kna1.ADRNR);
                rh.soldto_address = sapCustomer.ConcateAddress(adrc.Where(w => w.NATION == " ").FirstOrDefault());

                //Get Partner Customer
                var vbpa = sapSaleOrder.GetVBPA(vbak.MANDT, vbak.VBELN, "000000");

                //Ship-To Address
                var partner = vbpa.Where(w => w.PARVW == "WE").FirstOrDefault();
                rh.shipto_code    = partner.KUNNR;
                kna1              = sapCustomer.GetKNA1(vbak.MANDT, partner.KUNNR);
                adrc              = sapCustomer.GetADRC(kna1.MANDT, kna1.ADRNR);
                rh.shipto_address = sapCustomer.ConcateAddress(adrc.Where(w => w.NATION == " ").FirstOrDefault());

                //Get Sale Rep name
                partner         = vbpa.Where(w => w.PARVW == "ZR").FirstOrDefault();
                rh.salerep_code = partner.KUNNR;
                var salerep = sapCustomer.GetKNA1(partner.MANDT, partner.KUNNR);
                rh.salerep_name = salerep.NAME1 + salerep.NAME2;

                //Get Sale Order item
                var vbap = sapSaleOrder.GetVBAP(vbak.MANDT, vbak.VBELN);
                foreach (var item in vbap)
                {
                    ri.Add(new RepairItems
                    {
                        select         = false,
                        so_number      = item.VBELN,
                        so_item        = item.POSNR,
                        article_number = item.MATNR,
                        article_name   = item.ARKTX,
                        qty            = item.KWMENG
                    });
                }

                //Get Billing
                var vbrks = sapBilling.GetVBRKbySONumber(vbak.MANDT, vbak.VBELN);
                if (vbrks == null || vbrks.Count == 0)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Billing not found"));
                }
                var vbrk = vbrks.OrderBy(o => o.FKDAT).FirstOrDefault();
                rh.billing_date = DateTime.ParseExact(vbrk.FKDAT, "yyyyMMdd", null);

                result.header = rh;
                result.items  = ri;

                return(result.header == null || result.items == null
                         ? Request.CreateErrorResponse(HttpStatusCode.NotFound, "Sale Order not found")
                         : Request.CreateResponse(HttpStatusCode.OK, result));
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex));
            }
        }