예제 #1
0
    //////////////////////////////// Implementation ///////////////////////////////

    protected static GMDS CalculateHelper(double JD, double sunlongrad, double betarad, double R)
    {
        //What will be the return value
        GMDS details = new GMDS();

        //Calculate the position of Jupiter decreased by the light travel time from Jupiter to the specified position
        double DELTA = 5;
        double PreviousLightTravelTime = 0;
        double LightTravelTime         = ELL.DistanceToLightTime(DELTA);
        double x        = 0;
        double y        = 0;
        double z        = 0;
        double l        = 0;
        double lrad     = 0;
        double b        = 0;
        double brad     = 0;
        double r        = 0;
        double JD1      = JD - LightTravelTime;
        bool   bIterate = true;

        while (bIterate)
        {
            //Calculate the position of Jupiter
            l    = CAAJupiter.EclipticLongitude(JD1);
            lrad = CT.D2R(l);
            b    = CAAJupiter.EclipticLatitude(JD1);
            brad = CT.D2R(b);
            r    = CAAJupiter.RadiusVector(JD1);

            x               = r * Math.Cos(brad) * Math.Cos(lrad) + R * Math.Cos(sunlongrad);
            y               = r * Math.Cos(brad) * Math.Sin(lrad) + R * Math.Sin(sunlongrad);
            z               = r * Math.Sin(brad) + R * Math.Sin(betarad);
            DELTA           = Math.Sqrt(x * x + y * y + z * z);
            LightTravelTime = ELL.DistanceToLightTime(DELTA);

            //Prepare for the next loop around
            bIterate = (Math.Abs(LightTravelTime - PreviousLightTravelTime) > 2E-6); //2E-6 corresponds to 0.17 of a second
            if (bIterate)
            {
                JD1 = JD - LightTravelTime;
                PreviousLightTravelTime = LightTravelTime;
            }
        }

        //Calculate Jupiter's Longitude and Latitude
        double lambda0 = Math.Atan2(y, x);
        double beta0   = Math.Atan(z / Math.Sqrt(x * x + y * y));

        double t = JD - 2443000.5 - LightTravelTime;

        //Calculate the mean longitudes
        double l1    = 106.07719 + 203.488955790 * t;
        double l1rad = CT.D2R(l1);
        double l2    = 175.73161 + 101.374724735 * t;
        double l2rad = CT.D2R(l2);
        double l3    = 120.55883 + 50.317609207 * t;
        double l3rad = CT.D2R(l3);
        double l4    = 84.44459 + 21.571071177 * t;
        double l4rad = CT.D2R(l4);

        //Calculate the perijoves
        double pi1 = CT.D2R(CT.M360(97.0881 + 0.16138586 * t));
        double pi2 = CT.D2R(CT.M360(154.8663 + 0.04726307 * t));
        double pi3 = CT.D2R(CT.M360(188.1840 + 0.00712734 * t));
        double pi4 = CT.D2R(CT.M360(335.2868 + 0.00184000 * t));

        //Calculate the nodes on the equatorial plane of jupiter
        double w1    = 312.3346 - 0.13279386 * t;
        double w1rad = CT.D2R(w1);
        double w2    = 100.4411 - 0.03263064 * t;
        double w2rad = CT.D2R(w2);
        double w3    = 119.1942 - 0.00717703 * t;
        double w3rad = CT.D2R(w3);
        double w4    = 322.6186 - 0.00175934 * t;
        double w4rad = CT.D2R(w4);

        //Calculate the Principal inequality in the longitude of Jupiter
        double GAMMA = 0.33033 * Math.Sin(CT.D2R(163.679 + 0.0010512 * t)) + 0.03439 * Math.Sin(CT.D2R(34.486 - 0.0161731 * t));

        //Calculate the "phase of free libration"
        double philambda = CT.D2R(199.6766 + 0.17379190 * t);

        //Calculate the longitude of the node of the equator of Jupiter on the ecliptic
        double psi = CT.D2R(316.5182 - 0.00000208 * t);

        //Calculate the mean anomalies of Jupiter and Saturn
        double G     = CT.D2R(30.23756 + 0.0830925701 * t + GAMMA);
        double Gdash = CT.D2R(31.97853 + 0.0334597339 * t);

        //Calculate the longitude of the perihelion of Jupiter
        double PI = CT.D2R(13.469942);

        //Calculate the periodic terms in the longitudes of the satellites
        double Sigma1 = 0.47259 * Math.Sin(2 * (l1rad - l2rad)) + -0.03478 * Math.Sin(pi3 - pi4) + 0.01081 * Math.Sin(l2rad - 2 * l3rad + pi3) + 0.00738 * Math.Sin(philambda) + 0.00713 * Math.Sin(l2rad - 2 * l3rad + pi2) + -0.00674 * Math.Sin(pi1 + pi3 - 2 * PI - 2 * G) + 0.00666 * Math.Sin(l2rad - 2 * l3rad + pi4) + 0.00445 * Math.Sin(l1rad - pi3) + -0.00354 * Math.Sin(l1rad - l2rad) + -0.00317 * Math.Sin(2 * psi - 2 * PI) + 0.00265 * Math.Sin(l1rad - pi4) + -0.00186 * Math.Sin(G) + 0.00162 * Math.Sin(pi2 - pi3) + 0.00158 * Math.Sin(4 * (l1rad - l2rad)) + -0.00155 * Math.Sin(l1rad - l3rad) + -0.00138 * Math.Sin(psi + w3rad - 2 * PI - 2 * G) + -0.00115 * Math.Sin(2 * (l1rad - 2 * l2rad + w2rad)) + 0.00089 * Math.Sin(pi2 - pi4) + 0.00085 * Math.Sin(l1rad + pi3 - 2 * PI - 2 * G) + 0.00083 * Math.Sin(w2rad - w3rad) + 0.00053 * Math.Sin(psi - w2rad);

        double Sigma2 = 1.06476 * Math.Sin(2 * (l2rad - l3rad)) + 0.04256 * Math.Sin(l1rad - 2 * l2rad + pi3) + 0.03581 * Math.Sin(l2rad - pi3) + 0.02395 * Math.Sin(l1rad - 2 * l2rad + pi4) + 0.01984 * Math.Sin(l2rad - pi4) + -0.01778 * Math.Sin(philambda) + 0.01654 * Math.Sin(l2rad - pi2) + 0.01334 * Math.Sin(l2rad - 2 * l3rad + pi2) + 0.01294 * Math.Sin(pi3 - pi4) + -0.01142 * Math.Sin(l2rad - l3rad) + -0.01057 * Math.Sin(G) + -0.00775 * Math.Sin(2 * (psi - PI)) + 0.00524 * Math.Sin(2 * (l1rad - l2rad)) + -0.00460 * Math.Sin(l1rad - l3rad) + 0.00316 * Math.Sin(psi - 2 * G + w3rad - 2 * PI) + -0.00203 * Math.Sin(pi1 + pi3 - 2 * PI - 2 * G) + 0.00146 * Math.Sin(psi - w3rad) + -0.00145 * Math.Sin(2 * G) + 0.00125 * Math.Sin(psi - w4rad) + -0.00115 * Math.Sin(l1rad - 2 * l3rad + pi3) + -0.00094 * Math.Sin(2 * (l2rad - w2rad)) + 0.00086 * Math.Sin(2 * (l1rad - 2 * l2rad + w2rad)) + -0.00086 * Math.Sin(5 * Gdash - 2 * G + CT.D2R(52.225)) + -0.00078 * Math.Sin(l2rad - l4rad) + -0.00064 * Math.Sin(3 * l3rad - 7 * l4rad + 4 * pi4) + 0.00064 * Math.Sin(pi1 - pi4) + -0.00063 * Math.Sin(l1rad - 2 * l3rad + pi4) + 0.00058 * Math.Sin(w3rad - w4rad) + 0.00056 * Math.Sin(2 * (psi - PI - G)) + 0.00056 * Math.Sin(2 * (l2rad - l4rad)) + 0.00055 * Math.Sin(2 * (l1rad - l3rad)) + 0.00052 * Math.Sin(3 * l3rad - 7 * l4rad + pi3 + 3 * pi4) + -0.00043 * Math.Sin(l1rad - pi3) + 0.00041 * Math.Sin(5 * (l2rad - l3rad)) + 0.00041 * Math.Sin(pi4 - PI) + 0.00032 * Math.Sin(w2rad - w3rad) + 0.00032 * Math.Sin(2 * (l3rad - G - PI));

        double Sigma3 = 0.16490 * Math.Sin(l3rad - pi3) + 0.09081 * Math.Sin(l3rad - pi4) + -0.06907 * Math.Sin(l2rad - l3rad) + 0.03784 * Math.Sin(pi3 - pi4) + 0.01846 * Math.Sin(2 * (l3rad - l4rad)) + -0.01340 * Math.Sin(G) + -0.01014 * Math.Sin(2 * (psi - PI)) + 0.00704 * Math.Sin(l2rad - 2 * l3rad + pi3) + -0.00620 * Math.Sin(l2rad - 2 * l3rad + pi2) + -0.00541 * Math.Sin(l3rad - l4rad) + 0.00381 * Math.Sin(l2rad - 2 * l3rad + pi4) + 0.00235 * Math.Sin(psi - w3rad) + 0.00198 * Math.Sin(psi - w4rad) + 0.00176 * Math.Sin(philambda) + 0.00130 * Math.Sin(3 * (l3rad - l4rad)) + 0.00125 * Math.Sin(l1rad - l3rad) + -0.00119 * Math.Sin(5 * Gdash - 2 * G + CT.D2R(52.225)) + 0.00109 * Math.Sin(l1rad - l2rad) + -0.00100 * Math.Sin(3 * l3rad - 7 * l4rad + 4 * pi4) + 0.00091 * Math.Sin(w3rad - w4rad) + 0.00080 * Math.Sin(3 * l3rad - 7 * l4rad + pi3 + 3 * pi4) + -0.00075 * Math.Sin(2 * l2rad - 3 * l3rad + pi3) + 0.00072 * Math.Sin(pi1 + pi3 - 2 * PI - 2 * G) + 0.00069 * Math.Sin(pi4 - PI) + -0.00058 * Math.Sin(2 * l3rad - 3 * l4rad + pi4) + -0.00057 * Math.Sin(l3rad - 2 * l4rad + pi4) + 0.00056 * Math.Sin(l3rad + pi3 - 2 * PI - 2 * G) + -0.00052 * Math.Sin(l2rad - 2 * l3rad + pi1) + -0.00050 * Math.Sin(pi2 - pi3) + 0.00048 * Math.Sin(l3rad - 2 * l4rad + pi3) + -0.00045 * Math.Sin(2 * l2rad - 3 * l3rad + pi4) + -0.00041 * Math.Sin(pi2 - pi4) + -0.00038 * Math.Sin(2 * G) + -0.00037 * Math.Sin(pi3 - pi4 + w3rad - w4rad) + -0.00032 * Math.Sin(3 * l3rad - 7 * l4rad + 2 * pi3 + 2 * pi4) + 0.00030 * Math.Sin(4 * (l3rad - l4rad)) + 0.00029 * Math.Sin(l3rad + pi4 - 2 * PI - 2 * G) + -0.00028 * Math.Sin(w3rad + psi - 2 * PI - 2 * G) + 0.00026 * Math.Sin(l3rad - PI - G) + 0.00024 * Math.Sin(l2rad - 3 * l3rad + 2 * l4rad) + 0.00021 * Math.Sin(l3rad - PI - G) + -0.00021 * Math.Sin(l3rad - pi2) + 0.00017 * Math.Sin(2 * (l3rad - pi3));

        double Sigma4 = 0.84287 * Math.Sin(l4rad - pi4) + 0.03431 * Math.Sin(pi4 - pi3) + -0.03305 * Math.Sin(2 * (psi - PI)) + -0.03211 * Math.Sin(G) + -0.01862 * Math.Sin(l4rad - pi3) + 0.01186 * Math.Sin(psi - w4rad) + 0.00623 * Math.Sin(l4rad + pi4 - 2 * G - 2 * PI) + 0.00387 * Math.Sin(2 * (l4rad - pi4)) + -0.00284 * Math.Sin(5 * Gdash - 2 * G + CT.D2R(52.225)) + -0.00234 * Math.Sin(2 * (psi - pi4)) + -0.00223 * Math.Sin(l3rad - l4rad) + -0.00208 * Math.Sin(l4rad - PI) + 0.00178 * Math.Sin(psi + w4rad - 2 * pi4) + 0.00134 * Math.Sin(pi4 - PI) + 0.00125 * Math.Sin(2 * (l4rad - G - PI)) + -0.00117 * Math.Sin(2 * G) + -0.00112 * Math.Sin(2 * (l3rad - l4rad)) + 0.00107 * Math.Sin(3 * l3rad - 7 * l4rad + 4 * pi4) + 0.00102 * Math.Sin(l4rad - G - PI) + 0.00096 * Math.Sin(2 * l4rad - psi - w4rad) + 0.00087 * Math.Sin(2 * (psi - w4rad)) + -0.00085 * Math.Sin(3 * l3rad - 7 * l4rad + pi3 + 3 * pi4) + 0.00085 * Math.Sin(l3rad - 2 * l4rad + pi4) + -0.00081 * Math.Sin(2 * (l4rad - psi)) + 0.00071 * Math.Sin(l4rad + pi4 - 2 * PI - 3 * G) + 0.00061 * Math.Sin(l1rad - l4rad) + -0.00056 * Math.Sin(psi - w3rad) + -0.00054 * Math.Sin(l3rad - 2 * l4rad + pi3) + 0.00051 * Math.Sin(l2rad - l4rad) + 0.00042 * Math.Sin(2 * (psi - G - PI)) + 0.00039 * Math.Sin(2 * (pi4 - w4rad)) + 0.00036 * Math.Sin(psi + PI - pi4 - w4rad) + 0.00035 * Math.Sin(2 * Gdash - G + CT.D2R(188.37)) + -0.00035 * Math.Sin(l4rad - pi4 + 2 * PI - 2 * psi) + -0.00032 * Math.Sin(l4rad + pi4 - 2 * PI - G) + 0.00030 * Math.Sin(2 * Gdash - 2 * G + CT.D2R(149.15)) + 0.00029 * Math.Sin(3 * l3rad - 7 * l4rad + 2 * pi3 + 2 * pi4) + 0.00028 * Math.Sin(l4rad - pi4 + 2 * psi - 2 * PI) + -0.00028 * Math.Sin(2 * (l4rad - w4rad)) + -0.00027 * Math.Sin(pi3 - pi4 + w3rad - w4rad) + -0.00026 * Math.Sin(5 * Gdash - 3 * G + CT.D2R(188.37)) + 0.00025 * Math.Sin(w4rad - w3rad) + -0.00025 * Math.Sin(l2rad - 3 * l3rad + 2 * l4rad) + -0.00023 * Math.Sin(3 * (l3rad - l4rad)) + 0.00021 * Math.Sin(2 * l4rad - 2 * PI - 3 * G) + -0.00021 * Math.Sin(2 * l3rad - 3 * l4rad + pi4) + 0.00019 * Math.Sin(l4rad - pi4 - G) + -0.00019 * Math.Sin(2 * l4rad - pi3 - pi4) + -0.00018 * Math.Sin(l4rad - pi4 + G) + -0.00016 * Math.Sin(l4rad + pi3 - 2 * PI - 2 * G);

        details.Satellite1.MeanLongitude = CT.M360(l1);
        details.Satellite1.TrueLongitude = CT.M360(l1 + Sigma1);
        double L1 = CT.D2R(details.Satellite1.TrueLongitude);

        details.Satellite2.MeanLongitude = CT.M360(l2);
        details.Satellite2.TrueLongitude = CT.M360(l2 + Sigma2);
        double L2 = CT.D2R(details.Satellite2.TrueLongitude);

        details.Satellite3.MeanLongitude = CT.M360(l3);
        details.Satellite3.TrueLongitude = CT.M360(l3 + Sigma3);
        double L3 = CT.D2R(details.Satellite3.TrueLongitude);

        details.Satellite4.MeanLongitude = CT.M360(l4);
        details.Satellite4.TrueLongitude = CT.M360(l4 + Sigma4);
        double L4 = CT.D2R(details.Satellite4.TrueLongitude);

        //Calculate the periodic terms in the latitudes of the satellites
        double B1 = Math.Atan(0.0006393 * Math.Sin(L1 - w1rad) + 0.0001825 * Math.Sin(L1 - w2rad) + 0.0000329 * Math.Sin(L1 - w3rad) + -0.0000311 * Math.Sin(L1 - psi) + 0.0000093 * Math.Sin(L1 - w4rad) + 0.0000075 * Math.Sin(3 * L1 - 4 * l2rad - 1.9927 * Sigma1 + w2rad) + 0.0000046 * Math.Sin(L1 + psi - 2 * PI - 2 * G));

        details.Satellite1.EquatorialLatitude = CT.R2D(B1);

        double B2 = Math.Atan(0.0081004 * Math.Sin(L2 - w2rad) + 0.0004512 * Math.Sin(L2 - w3rad) + -0.0003284 * Math.Sin(L2 - psi) + 0.0001160 * Math.Sin(L2 - w4rad) + 0.0000272 * Math.Sin(l1rad - 2 * l3rad + 1.0146 * Sigma2 + w2rad) + -0.0000144 * Math.Sin(L2 - w1rad) + 0.0000143 * Math.Sin(L2 + psi - 2 * PI - 2 * G) + 0.0000035 * Math.Sin(L2 - psi + G) + -0.0000028 * Math.Sin(l1rad - 2 * l3rad + 1.0146 * Sigma2 + w3rad));

        details.Satellite2.EquatorialLatitude = CT.R2D(B2);

        double B3 = Math.Atan(0.0032402 * Math.Sin(L3 - w3rad) + -0.0016911 * Math.Sin(L3 - psi) + 0.0006847 * Math.Sin(L3 - w4rad) + -0.0002797 * Math.Sin(L3 - w2rad) + 0.0000321 * Math.Sin(L3 + psi - 2 * PI - 2 * G) + 0.0000051 * Math.Sin(L3 - psi + G) + -0.0000045 * Math.Sin(L3 - psi - G) + -0.0000045 * Math.Sin(L3 + psi - 2 * PI) + 0.0000037 * Math.Sin(L3 + psi - 2 * PI - 3 * G) + 0.0000030 * Math.Sin(2 * l2rad - 3 * L3 + 4.03 * Sigma3 + w2rad) + -0.0000021 * Math.Sin(2 * l2rad - 3 * L3 + 4.03 * Sigma3 + w3rad));

        details.Satellite3.EquatorialLatitude = CT.R2D(B3);

        double B4 = Math.Atan(-0.0076579 * Math.Sin(L4 - psi) + 0.0044134 * Math.Sin(L4 - w4rad) + -0.0005112 * Math.Sin(L4 - w3rad) + 0.0000773 * Math.Sin(L4 + psi - 2 * PI - 2 * G) + 0.0000104 * Math.Sin(L4 - psi + G) + -0.0000102 * Math.Sin(L4 - psi - G) + 0.0000088 * Math.Sin(L4 + psi - 2 * PI - 3 * G) + -0.0000038 * Math.Sin(L4 + psi - 2 * PI - G));

        details.Satellite4.EquatorialLatitude = CT.R2D(B4);

        //Calculate the periodic terms for the radius vector
        details.Satellite1.r = 5.90569 * (1 + (-0.0041339 * Math.Cos(2 * (l1rad - l2rad)) + -0.0000387 * Math.Cos(l1rad - pi3) + -0.0000214 * Math.Cos(l1rad - pi4) + 0.0000170 * Math.Cos(l1rad - l2rad) + -0.0000131 * Math.Cos(4 * (l1rad - l2rad)) + 0.0000106 * Math.Cos(l1rad - l3rad) + -0.0000066 * Math.Cos(l1rad + pi3 - 2 * PI - 2 * G)));

        details.Satellite2.r = 9.39657 * (1 + (0.0093848 * Math.Cos(l1rad - l2rad) + -0.0003116 * Math.Cos(l2rad - pi3) + -0.0001744 * Math.Cos(l2rad - pi4) + -0.0001442 * Math.Cos(l2rad - pi2) + 0.0000553 * Math.Cos(l2rad - l3rad) + 0.0000523 * Math.Cos(l1rad - l3rad) + -0.0000290 * Math.Cos(2 * (l1rad - l2rad)) + 0.0000164 * Math.Cos(2 * (l2rad - w2rad)) + 0.0000107 * Math.Cos(l1rad - 2 * l3rad + pi3) + -0.0000102 * Math.Cos(l2rad - pi1) + -0.0000091 * Math.Cos(2 * (l1rad - l3rad))));

        details.Satellite3.r = 14.98832 * (1 + (-0.0014388 * Math.Cos(l3rad - pi3) + -0.0007919 * Math.Cos(l3rad - pi4) + 0.0006342 * Math.Cos(l2rad - l3rad) + -0.0001761 * Math.Cos(2 * (l3rad - l4rad)) + 0.0000294 * Math.Cos(l3rad - l4rad) + -0.0000156 * Math.Cos(3 * (l3rad - l4rad)) + 0.0000156 * Math.Cos(l1rad - l3rad) + -0.0000153 * Math.Cos(l1rad - l2rad) + 0.0000070 * Math.Cos(2 * l2rad - 3 * l3rad + pi3) + -0.0000051 * Math.Cos(l3rad + pi3 - 2 * PI - 2 * G)));

        details.Satellite4.r = 26.36273 * (1 + (-0.0073546 * Math.Cos(l4rad - pi4) + 0.0001621 * Math.Cos(l4rad - pi3) + 0.0000974 * Math.Cos(l3rad - l4rad) + -0.0000543 * Math.Cos(l4rad + pi4 - 2 * PI - 2 * G) + -0.0000271 * Math.Cos(2 * (l4rad - pi4)) + 0.0000182 * Math.Cos(l4rad - PI) + 0.0000177 * Math.Cos(2 * (l3rad - l4rad)) + -0.0000167 * Math.Cos(2 * l4rad - psi - w4rad) + 0.0000167 * Math.Cos(psi - w4rad) + -0.0000155 * Math.Cos(2 * (l4rad - PI - G)) + 0.0000142 * Math.Cos(2 * (l4rad - psi)) + 0.0000105 * Math.Cos(l1rad - l4rad) + 0.0000092 * Math.Cos(l2rad - l4rad) + -0.0000089 * Math.Cos(l4rad - PI - G) + -0.0000062 * Math.Cos(l4rad + pi4 - 2 * PI - 3 * G) + 0.0000048 * Math.Cos(2 * (l4rad - w4rad))));



        //Calculate T0
        double T0 = (JD - 2433282.423) / 36525;

        //Calculate the precession in longitude from Epoch B1950 to the date
        double P = CT.D2R(1.3966626 * T0 + 0.0003088 * T0 * T0);

        //Add it to L1 - L4 and psi
        L1 += P;
        details.Satellite1.TropicalLongitude = CT.M360(CT.R2D(L1));
        L2 += P;
        details.Satellite2.TropicalLongitude = CT.M360(CT.R2D(L2));
        L3 += P;
        details.Satellite3.TropicalLongitude = CT.M360(CT.R2D(L3));
        L4 += P;
        details.Satellite4.TropicalLongitude = CT.M360(CT.R2D(L4));
        psi += P;

        //Calculate the inclination of Jupiter's axis of rotation on the orbital plane
        double T    = (JD - 2415020.5) / 36525;
        double I    = 3.120262 + 0.0006 * T;
        double Irad = CT.D2R(I);

        double X1 = details.Satellite1.r * Math.Cos(L1 - psi) * Math.Cos(B1);
        double X2 = details.Satellite2.r * Math.Cos(L2 - psi) * Math.Cos(B2);
        double X3 = details.Satellite3.r * Math.Cos(L3 - psi) * Math.Cos(B3);
        double X4 = details.Satellite4.r * Math.Cos(L4 - psi) * Math.Cos(B4);
        double X5 = 0;

        double Y1 = details.Satellite1.r * Math.Sin(L1 - psi) * Math.Cos(B1);
        double Y2 = details.Satellite2.r * Math.Sin(L2 - psi) * Math.Cos(B2);
        double Y3 = details.Satellite3.r * Math.Sin(L3 - psi) * Math.Cos(B3);
        double Y4 = details.Satellite4.r * Math.Sin(L4 - psi) * Math.Cos(B4);
        double Y5 = 0;

        double Z1 = details.Satellite1.r * Math.Sin(B1);
        double Z2 = details.Satellite2.r * Math.Sin(B2);
        double Z3 = details.Satellite3.r * Math.Sin(B3);
        double Z4 = details.Satellite4.r * Math.Sin(B4);
        double Z5 = 1;

        //Now do the rotations, first for the ficticious 5th satellite, so that we can calculate D
        double omega = CT.D2R(EPO.JupiterLongitudeAscendingNode(JD));
        double i     = CT.D2R(EPO.JupiterInclination(JD));
        double A6    = 0;
        double B6    = 0;
        double C6    = 0;
        C3D    north = new C3D();

        double[] abc = Rotations(X5, Y5, Z5, Irad, psi, i, omega, lambda0, beta0, north);

        A6 = abc[0];
        B6 = abc[1];
        C6 = abc[2];

        double D = Math.Atan2(A6, C6);

        //Now calculate the values for satellite 1

        abc = Rotations(X1, Y1, Z1, Irad, psi, i, omega, lambda0, beta0, details.Satellite1.EclipticRectangularCoordinates);

        A6 = abc[0];
        B6 = abc[1];
        C6 = abc[2];

        details.Satellite1.TrueRectangularCoordinates.X = A6 * Math.Cos(D) - C6 * Math.Sin(D);
        details.Satellite1.TrueRectangularCoordinates.Y = A6 * Math.Sin(D) + C6 * Math.Cos(D);
        details.Satellite1.TrueRectangularCoordinates.Z = B6;

        //Now calculate the values for satellite 2
        abc = Rotations(X2, Y2, Z2, Irad, psi, i, omega, lambda0, beta0, details.Satellite2.EclipticRectangularCoordinates);

        A6 = abc[0];
        B6 = abc[1];
        C6 = abc[2];


        details.Satellite2.TrueRectangularCoordinates.X = A6 * Math.Cos(D) - C6 * Math.Sin(D);
        details.Satellite2.TrueRectangularCoordinates.Y = A6 * Math.Sin(D) + C6 * Math.Cos(D);
        details.Satellite2.TrueRectangularCoordinates.Z = B6;

        //Now calculate the values for satellite 3
        abc = Rotations(X3, Y3, Z3, Irad, psi, i, omega, lambda0, beta0, details.Satellite3.EclipticRectangularCoordinates);
        A6  = abc[0];
        B6  = abc[1];
        C6  = abc[2];

        details.Satellite3.TrueRectangularCoordinates.X = A6 * Math.Cos(D) - C6 * Math.Sin(D);
        details.Satellite3.TrueRectangularCoordinates.Y = A6 * Math.Sin(D) + C6 * Math.Cos(D);
        details.Satellite3.TrueRectangularCoordinates.Z = B6;

        //And finally for satellite 4
        abc = Rotations(X4, Y4, Z4, Irad, psi, i, omega, lambda0, beta0, details.Satellite4.EclipticRectangularCoordinates);
        A6  = abc[0];
        B6  = abc[1];
        C6  = abc[2];

        details.Satellite4.TrueRectangularCoordinates.X = A6 * Math.Cos(D) - C6 * Math.Sin(D);
        details.Satellite4.TrueRectangularCoordinates.Y = A6 * Math.Sin(D) + C6 * Math.Cos(D);
        details.Satellite4.TrueRectangularCoordinates.Z = B6;

        //apply the differential light-time correction
        details.Satellite1.ApparentRectangularCoordinates.X = details.Satellite1.TrueRectangularCoordinates.X + Math.Abs(details.Satellite1.TrueRectangularCoordinates.Z) / 17295 * Math.Sqrt(1 - (details.Satellite1.TrueRectangularCoordinates.X / details.Satellite1.r) * (details.Satellite1.TrueRectangularCoordinates.X / details.Satellite1.r));
        details.Satellite1.ApparentRectangularCoordinates.Y = details.Satellite1.TrueRectangularCoordinates.Y;
        details.Satellite1.ApparentRectangularCoordinates.Z = details.Satellite1.TrueRectangularCoordinates.Z;

        details.Satellite2.ApparentRectangularCoordinates.X = details.Satellite2.TrueRectangularCoordinates.X + Math.Abs(details.Satellite2.TrueRectangularCoordinates.Z) / 21819 * Math.Sqrt(1 - (details.Satellite2.TrueRectangularCoordinates.X / details.Satellite2.r) * (details.Satellite2.TrueRectangularCoordinates.X / details.Satellite2.r));
        details.Satellite2.ApparentRectangularCoordinates.Y = details.Satellite2.TrueRectangularCoordinates.Y;
        details.Satellite2.ApparentRectangularCoordinates.Z = details.Satellite2.TrueRectangularCoordinates.Z;

        details.Satellite3.ApparentRectangularCoordinates.X = details.Satellite3.TrueRectangularCoordinates.X + Math.Abs(details.Satellite3.TrueRectangularCoordinates.Z) / 27558 * Math.Sqrt(1 - (details.Satellite3.TrueRectangularCoordinates.X / details.Satellite3.r) * (details.Satellite3.TrueRectangularCoordinates.X / details.Satellite3.r));
        details.Satellite3.ApparentRectangularCoordinates.Y = details.Satellite3.TrueRectangularCoordinates.Y;
        details.Satellite3.ApparentRectangularCoordinates.Z = details.Satellite3.TrueRectangularCoordinates.Z;

        details.Satellite4.ApparentRectangularCoordinates.X = details.Satellite4.TrueRectangularCoordinates.X + Math.Abs(details.Satellite4.TrueRectangularCoordinates.Z) / 36548 * Math.Sqrt(1 - (details.Satellite4.TrueRectangularCoordinates.X / details.Satellite4.r) * (details.Satellite4.TrueRectangularCoordinates.X / details.Satellite4.r));
        details.Satellite4.ApparentRectangularCoordinates.Y = details.Satellite4.TrueRectangularCoordinates.Y;
        details.Satellite4.ApparentRectangularCoordinates.Z = details.Satellite4.TrueRectangularCoordinates.Z;

        //apply the perspective effect correction
        double W = DELTA / (DELTA + details.Satellite1.TrueRectangularCoordinates.Z / 2095);

        details.Satellite1.ApparentRectangularCoordinates.X *= W;
        details.Satellite1.ApparentRectangularCoordinates.Y *= W;

        W = DELTA / (DELTA + details.Satellite2.TrueRectangularCoordinates.Z / 2095);
        details.Satellite2.ApparentRectangularCoordinates.X *= W;
        details.Satellite2.ApparentRectangularCoordinates.Y *= W;

        W = DELTA / (DELTA + details.Satellite3.TrueRectangularCoordinates.Z / 2095);
        details.Satellite3.ApparentRectangularCoordinates.X *= W;
        details.Satellite3.ApparentRectangularCoordinates.Y *= W;

        W = DELTA / (DELTA + details.Satellite4.TrueRectangularCoordinates.Z / 2095);
        details.Satellite4.ApparentRectangularCoordinates.X *= W;
        details.Satellite4.ApparentRectangularCoordinates.Y *= W;

        return(details);
    }
        public IEnumerable <GarmentStockReportViewModel> GetStockQuery(string ctg, string unitcode, DateTime?datefrom, DateTime?dateto, int offset)
        {
            DateTime DateFrom = datefrom == null ? new DateTime(1970, 1, 1) : (DateTime)datefrom;
            DateTime DateTo   = dateto == null ? DateTime.Now : (DateTime)dateto;

            var PPAwal = (from a in dbContext.GarmentUnitReceiptNotes
                          join b in dbContext.GarmentUnitReceiptNoteItems on a.Id equals b.URNId
                          join d in dbContext.GarmentDeliveryOrderDetails on b.POId equals d.POId
                          join f in dbContext.GarmentInternalPurchaseOrders on b.POId equals f.Id
                          join e in dbContext.GarmentReceiptCorrectionItems on b.Id equals e.URNItemId into RC
                          from ty in RC.DefaultIfEmpty()
                          join c in dbContext.GarmentUnitExpenditureNoteItems on b.UENItemId equals c.Id into UE
                          from ww in UE.DefaultIfEmpty()
                          join r in dbContext.GarmentUnitExpenditureNotes on ww.UENId equals r.Id into UEN
                          from dd in UEN.DefaultIfEmpty()
                          join epoItem in dbContext.GarmentExternalPurchaseOrderItems on b.POId equals epoItem.POId into EP
                          from epoItem in EP.DefaultIfEmpty()
                          join epo in dbContext.GarmentExternalPurchaseOrders on epoItem.GarmentEPOId equals epo.Id into EPO
                          from epo in EPO.DefaultIfEmpty()
                          where d.CodeRequirment == (String.IsNullOrWhiteSpace(ctg) ? d.CodeRequirment : ctg) &&
                          a.IsDeleted == false && b.IsDeleted == false
                          //String.IsNullOrEmpty(a.UENNo) ? false : a.UENNo.Contains(unitcode)
                          //|| a.UnitCode == unitcode
                          //a.UENNo.Contains(unitcode) || a.UnitCode == unitcode
                          //a.UnitCode == unitcode || a.UENNo.Contains(unitcode)

                          //&& a.ReceiptDate.AddHours(offset).Date >= DateFrom.Date
                          && a.CreatedUtc.AddHours(offset).Date < DateFrom.Date
                          select new
            {
                ReceiptDate = a.ReceiptDate,
                CodeRequirment = d.CodeRequirment,
                ProductCode = b.ProductCode,
                ProductName = b.ProductName,
                ProductRemark = b.ProductRemark,
                RO = b.RONo,
                Uom = b.UomUnit,
                Buyer = f.BuyerCode,
                PlanPo = b.POSerialNumber,
                NoArticle = f.Article,
                QtyReceipt = b.ReceiptQuantity,
                QtyCorrection = ty.POSerialNumber == null ? 0 : ty.Quantity,
                QtyExpend = ww.POSerialNumber == null ? 0 : ww.Quantity,
                PriceReceipt = b.PricePerDealUnit,
                PriceCorrection = ty.POSerialNumber == null ? 0 : ty.PricePerDealUnit,
                PriceExpend = ww.POSerialNumber == null ? 0 : ww.PricePerDealUnit,
                POId = b.POId,
                URNType = a.URNType,
                UnitCode = a.UnitCode,
                UENNo = a.UENNo,
                UnitSenderCode = dd.UnitSenderCode == null ? "-" : dd.UnitSenderCode,
                UnitRequestName = dd.UnitRequestName == null ? "-" : dd.UnitRequestName,
                ExpenditureTo = dd.ExpenditureTo == null ? "-" : dd.ExpenditureTo,
                PaymentMethod = epo.PaymentMethod == null ? "-": epo.PaymentMethod,
                a.IsDeleted
            });
            var CobaPP = from a in PPAwal
                         //where a.ReceiptDate.AddHours(offset).Date < DateFrom.Date && a.CodeRequirment == (String.IsNullOrWhiteSpace(ctg) ? a.CodeRequirment : ctg) && a.IsDeleted == false
                         where a.UENNo.Contains((String.IsNullOrWhiteSpace(unitcode) ? a.UnitCode : unitcode)) || a.UnitCode == (String.IsNullOrWhiteSpace(unitcode) ? a.UnitCode : unitcode)
                         select a;
            var PPAkhir = from a in dbContext.GarmentUnitReceiptNotes
                          join b in dbContext.GarmentUnitReceiptNoteItems on a.Id equals b.URNId
                          join d in dbContext.GarmentDeliveryOrderDetails on b.POId equals d.POId
                          join f in dbContext.GarmentInternalPurchaseOrders on b.POId equals f.Id
                          //join f in SaldoAwal on b.POId equals f.POID
                          join e in dbContext.GarmentReceiptCorrectionItems on b.Id equals e.URNItemId into RC
                          from ty in RC.DefaultIfEmpty()
                          join c in dbContext.GarmentUnitExpenditureNoteItems on b.UENItemId equals c.Id into UE
                          from ww in UE.DefaultIfEmpty()
                          join r in dbContext.GarmentUnitExpenditureNotes on ww.UENId equals r.Id into UEN
                          from dd in UEN.DefaultIfEmpty()
                          join epoItem in dbContext.GarmentExternalPurchaseOrderItems on b.POId equals epoItem.POId into EP
                          from epoItem in EP.DefaultIfEmpty()
                          join epo in dbContext.GarmentExternalPurchaseOrders on epoItem.GarmentEPOId equals epo.Id into EPO
                          from epo in EPO.DefaultIfEmpty()

                          where d.CodeRequirment == (String.IsNullOrWhiteSpace(ctg) ? d.CodeRequirment : ctg) &&
                          a.IsDeleted == false && b.IsDeleted == false
                          //String.IsNullOrEmpty(a.UENNo) ? false : a.UENNo.Contains(unitcode)
                          //|| a.UnitCode == unitcode
                          //a.UnitCode == unitcode || a.UENNo.Contains(unitcode)
                          // a.UENNo.Contains(unitcode) || a.UnitCode == unitcode     /*String.IsNullOrEmpty(a.UENNo) ? true :*/
                          && a.CreatedUtc.AddHours(offset).Date >= DateFrom.Date &&
                          a.CreatedUtc.AddHours(offset).Date <= DateTo.Date

                          select new
            {
                ReceiptDate     = a.ReceiptDate,
                CodeRequirment  = d.CodeRequirment,
                ProductCode     = b.ProductCode,
                ProductName     = b.ProductName,
                ProductRemark   = b.ProductRemark,
                RO              = b.RONo,
                Uom             = b.UomUnit,
                Buyer           = f.BuyerCode,
                PlanPo          = b.POSerialNumber,
                NoArticle       = f.Article,
                QtyReceipt      = b.ReceiptQuantity,
                QtyCorrection   = ty.POSerialNumber == null ? 0 : ty.Quantity,
                QtyExpend       = ww.POSerialNumber == null ? 0 : ww.Quantity,
                PriceReceipt    = b.PricePerDealUnit,
                PriceCorrection = ty.POSerialNumber == null ? 0 : ty.PricePerDealUnit,
                PriceExpend     = ww.POSerialNumber == null ? 0 : ww.PricePerDealUnit,
                POId            = b.POId,
                URNType         = a.URNType,
                UnitCode        = a.UnitCode,
                UENNo           = a.UENNo,
                UnitSenderCode  = dd.UnitSenderCode == null ? "-" : dd.UnitSenderCode,
                UnitRequestName = dd.UnitRequestName == null ? "-" : dd.UnitRequestName,
                ExpenditureTo   = dd.ExpenditureTo == null ? "-" : dd.ExpenditureTo,
                PaymentMethod   = epo.PaymentMethod == null ? "-" : epo.PaymentMethod,
                a.IsDeleted
            };
            var CobaPPAkhir = from a in PPAkhir
                              where a.UENNo.Contains((String.IsNullOrWhiteSpace(unitcode) ? a.UnitCode : unitcode)) || a.UnitCode == (String.IsNullOrWhiteSpace(unitcode) ? a.UnitCode : unitcode)
                              //where a.ReceiptDate.AddHours(offset).Date >= DateFrom.Date
                              //      && a.ReceiptDate.AddHours(offset).Date <= DateTo.Date
                              //      && a.CodeRequirment == (String.IsNullOrWhiteSpace(ctg) ? a.CodeRequirment : ctg)
                              //      && a.IsDeleted == false
                              select a;
            var SaldoAwal = from query in CobaPP
                            group query by new { query.ProductCode, query.ProductName, query.RO, query.PlanPo, query.POId, query.UnitCode, query.UnitSenderCode, query.UnitRequestName } into data
                select new GarmentStockReportViewModel
            {
                ProductCode          = data.Key.ProductCode,
                RO                   = data.Key.RO,
                PlanPo               = data.FirstOrDefault().PlanPo,
                NoArticle            = data.FirstOrDefault().NoArticle,
                ProductName          = data.FirstOrDefault().ProductName,
                ProductRemark        = data.FirstOrDefault().ProductRemark,
                Buyer                = data.FirstOrDefault().Buyer,
                BeginningBalanceQty  = data.Sum(x => x.QtyReceipt) + Convert.ToDecimal(data.Sum(x => x.QtyCorrection)) - Convert.ToDecimal(data.Sum(x => x.QtyExpend)),
                BeginningBalanceUom  = data.FirstOrDefault().Uom,
                ReceiptCorrectionQty = 0,
                ReceiptQty           = 0,
                ReceiptUom           = data.FirstOrDefault().Uom,
                ExpendQty            = 0,
                ExpandUom            = data.FirstOrDefault().Uom,
                EndingBalanceQty     = 0,
                EndingUom            = data.FirstOrDefault().Uom,
                POId                 = data.FirstOrDefault().POId,
                PaymentMethod        = data.FirstOrDefault().PaymentMethod
            };
            var SaldoAkhir = from query in CobaPPAkhir
                             group query by new { query.ProductCode, query.ProductName, query.RO, query.PlanPo, query.POId, query.UnitCode, query.UnitSenderCode, query.UnitRequestName } into data
                select new GarmentStockReportViewModel
            {
                ProductCode          = data.Key.ProductCode,
                RO                   = data.Key.RO,
                PlanPo               = data.FirstOrDefault().PlanPo,
                NoArticle            = data.FirstOrDefault().NoArticle,
                ProductName          = data.FirstOrDefault().ProductName,
                ProductRemark        = data.FirstOrDefault().ProductRemark,
                Buyer                = data.FirstOrDefault().Buyer,
                BeginningBalanceQty  = 0,
                BeginningBalanceUom  = data.FirstOrDefault().Uom,
                ReceiptCorrectionQty = 0,
                ReceiptQty           = data.Sum(x => x.QtyReceipt),
                ReceiptUom           = data.FirstOrDefault().Uom,
                ExpendQty            = data.Sum(x => x.QtyExpend),
                ExpandUom            = data.FirstOrDefault().Uom,
                EndingBalanceQty     = Convert.ToDecimal(Convert.ToDouble(data.Sum(x => x.QtyReceipt)) - data.Sum(x => x.QtyExpend)),
                EndingUom            = data.FirstOrDefault().Uom,
                POId                 = data.FirstOrDefault().POId,
                PaymentMethod        = data.FirstOrDefault().PaymentMethod
            };
            List <GarmentStockReportViewModel> Data1 = SaldoAwal.Concat(SaldoAkhir).ToList();
            var Data = (from query in Data1
                        group query by new { query.POId, query.ProductCode, query.RO } into groupdata
                        select new GarmentStockReportViewModel
            {
                ProductCode = groupdata.FirstOrDefault().ProductCode == null ? "-" : groupdata.FirstOrDefault().ProductCode,
                RO = groupdata.FirstOrDefault().RO == null ? "-" : groupdata.FirstOrDefault().RO,
                PlanPo = groupdata.FirstOrDefault().PlanPo == null ? "-" : groupdata.FirstOrDefault().PlanPo,
                NoArticle = groupdata.FirstOrDefault().NoArticle == null ? "-" : groupdata.FirstOrDefault().NoArticle,
                ProductName = groupdata.FirstOrDefault().ProductName == null ? "-" : groupdata.FirstOrDefault().ProductName,
                ProductRemark = groupdata.FirstOrDefault().ProductRemark,
                Buyer = groupdata.FirstOrDefault().Buyer == null ? "-" : groupdata.FirstOrDefault().Buyer,
                BeginningBalanceQty = groupdata.Sum(x => x.BeginningBalanceQty),
                BeginningBalanceUom = groupdata.FirstOrDefault().BeginningBalanceUom,
                ReceiptCorrectionQty = 0,
                ReceiptQty = groupdata.Sum(x => x.ReceiptQty),
                ReceiptUom = groupdata.FirstOrDefault().ReceiptUom,
                ExpendQty = groupdata.Sum(x => x.ExpendQty),
                ExpandUom = groupdata.FirstOrDefault().ExpandUom,
                EndingBalanceQty = Convert.ToDecimal((groupdata.Sum(x => x.BeginningBalanceQty) + groupdata.Sum(x => x.ReceiptQty) + 0) - (Convert.ToDecimal(groupdata.Sum(x => x.ExpendQty)))),
                EndingUom = groupdata.FirstOrDefault().EndingUom,
                PaymentMethod = groupdata.FirstOrDefault().PaymentMethod
            });

            return(Data.AsQueryable());
        }
예제 #3
0
        public List <GarmentStockReportViewModel> GetStockQuery(string ctg, string unitcode, DateTime?datefrom, DateTime?dateto, int offset)
        {
            DateTime DateFrom = datefrom == null ? new DateTime(1970, 1, 1) : (DateTime)datefrom;
            DateTime DateTo   = dateto == null ? DateTime.Now : (DateTime)dateto;

            var lastdate = dbContext.BalanceStocks.OrderByDescending(x => x.CreateDate).Select(x => x.CreateDate).FirstOrDefault();

            var BalaceStock = (from a in dbContext.BalanceStocks
                               where a.CreateDate == lastdate
                               group a by new { a.ArticleNo, a.EPOID, a.EPOItemId } into data
                               select new
            {
                BalanceStockId = data.FirstOrDefault().BalanceStockId,
                ArticleNo = data.FirstOrDefault().ArticleNo,
                EPOID = data.FirstOrDefault().EPOID,
                EPOItemId = data.FirstOrDefault().EPOItemId,
                CloseStock = (double)data.Sum(x => x.CloseStock),
                ClosePrice = (decimal)data.Sum(x => x.ClosePrice)
            }).ToList();


            //var saldoawalbalanceepoid = SaldoAwal.Select(x => x.EPOID).ToList();

            var SAkhir = (from a in dbContext.GarmentUnitReceiptNotes
                          join b in dbContext.GarmentUnitReceiptNoteItems on a.Id equals b.URNId
                          join h in dbContext.GarmentDeliveryOrderDetails on b.DODetailId equals h.Id
                          join f in dbContext.GarmentInternalPurchaseOrders on b.POId equals f.Id
                          join e in dbContext.GarmentReceiptCorrectionItems on b.Id equals e.URNItemId into RC
                          from ty in RC.DefaultIfEmpty()
                          join c in dbContext.GarmentUnitExpenditureNoteItems on b.Id equals c.URNItemId into UE
                          from ww in UE.DefaultIfEmpty()
                          join r in dbContext.GarmentUnitExpenditureNotes on ww.UENId equals r.Id into UEN
                          from dd in UEN.DefaultIfEmpty()
                          join epoItem in dbContext.GarmentExternalPurchaseOrderItems on b.EPOItemId equals epoItem.Id into EP
                          from epoItem in EP.DefaultIfEmpty()
                          join epo in dbContext.GarmentExternalPurchaseOrders on epoItem.GarmentEPOId equals epo.Id into EPO
                          from epo in EPO.DefaultIfEmpty()
                          where h.CodeRequirment == (String.IsNullOrWhiteSpace(ctg) ? h.CodeRequirment : ctg) &&
                          a.IsDeleted == false && b.IsDeleted == false &&
                          a.CreatedUtc.AddHours(offset).Date >= DateFrom.Date &&
                          a.CreatedUtc.AddHours(offset).Date <= DateTo.Date &&
                          a.UnitCode == (string.IsNullOrWhiteSpace(unitcode) ? a.UnitCode : unitcode)
                          select new
            {
                URNId = a.Id,
                URNItemsId = b.Id,
                DODEtailId = h.Id,
                IPOId = f.Id,
                RCorrId = ty == null ? 0 : ty.Id,
                UENItemsId = ww == null ? 0 : ww.Id,
                UENId = dd == null ? 0 : dd.Id,
                EPOItemId = epoItem == null ? 0 : epoItem.Id,
                EPOId = epo == null ? 0 : epo.Id,
                UnitCode = a.UnitCode,
                UnitSenderCode = dd == null ? "-" : dd.UnitSenderCode
            }).Distinct().ToList();
            //SaldoAkhir = SaldoAkhir.Where(x => x.UENNo.Contains((String.IsNullOrWhiteSpace(unitcode) ? x.UnitCode : unitcode)) || x.UnitCode == ((String.IsNullOrWhiteSpace(unitcode) ? x.UnitCode : unitcode))).Select(x => x).ToList();
            var SAkhirs                      = SAkhir.Select(x => x).ToList();
            var unitreceiptnoteIds           = SAkhirs.Select(x => x.URNId).ToList();
            var unitreceiptnotes             = dbContext.GarmentUnitReceiptNotes.Where(x => unitreceiptnoteIds.Contains(x.Id)).Select(s => new { s.ReceiptDate, s.URNType, s.UnitCode, s.Id }).ToList();
            var unitreceiptnoteItemIds       = SAkhirs.Select(x => x.URNItemsId).ToList();
            var unitreceiptnoteItems         = dbContext.GarmentUnitReceiptNoteItems.Where(x => unitreceiptnoteItemIds.Contains(x.Id)).Select(s => new { s.ProductCode, s.ProductName, s.ProductRemark, s.RONo, s.SmallUomUnit, s.POSerialNumber, s.ReceiptQuantity, s.PricePerDealUnit, s.Id }).ToList();
            var deliveryorderdetailid        = SAkhirs.Select(x => x.DODEtailId).ToList();
            var deliveryorderdetails         = dbContext.GarmentDeliveryOrderDetails.Where(x => deliveryorderdetailid.Contains(x.Id)).Select(s => new { s.CodeRequirment, s.Id }).ToList();
            var purchaseorderIds             = SAkhirs.Select(x => x.IPOId).ToList();
            var purchaseorders               = dbContext.GarmentInternalPurchaseOrders.Where(x => purchaseorderIds.Contains(x.Id)).Select(s => new { s.BuyerCode, s.Id, s.Article }).ToList();
            var receiptCorrectionitemIds     = SAkhirs.Select(x => x.RCorrId).ToList();
            var receiptCorrectionitems       = dbContext.GarmentReceiptCorrectionItems.Where(x => receiptCorrectionitemIds.Contains(x.Id)).Select(s => new { s.CorrectionQuantity, s.PricePerDealUnit, s.Id }).ToList();
            var unitexpenditureitemIds       = SAkhirs.Select(x => x.UENItemsId).ToList();
            var unitexpenditureitems         = dbContext.GarmentUnitExpenditureNoteItems.Where(x => unitexpenditureitemIds.Contains(x.Id)).Select(s => new { s.PricePerDealUnit, s.Quantity, s.Id }).ToList();
            var unitexpenditureIds           = SAkhirs.Select(x => x.UENId).ToList();
            var unitexpenditures             = dbContext.GarmentUnitExpenditureNotes.Where(x => unitexpenditureIds.Contains(x.Id)).Select(s => new { s.UnitRequestCode, s.UnitSenderCode, s.ExpenditureTo, s.Id }).ToList();
            var externalpurchaseorderitemIds = SAkhirs.Select(x => x.EPOItemId).ToList();
            var externalpurchaseorderitems   = dbContext.GarmentExternalPurchaseOrderItems.Where(x => externalpurchaseorderitemIds.Contains(x.Id)).Select(s => new { s.Id }).ToList();
            var externalpurchaseorderIds     = SAkhirs.Select(x => x.EPOId).ToList();
            var externalpurchaseorders       = dbContext.GarmentExternalPurchaseOrders.Where(x => externalpurchaseorderIds.Contains(x.Id)).Select(s => new { s.PaymentMethod, s.Id }).ToList();
            //var balancestocks = SaldoAwal.Where(x => externalpurchaseorderitemIds.Contains((long)x.EPOItemId)).Select(s => new { s.BeginningBalanceQty, s.BeginningBaancePrice, s.EPOItemId}).ToList();
            List <GarmentStockReportViewModel> SaldoAkhir = new List <GarmentStockReportViewModel>();

            foreach (var item in SAkhir)
            {
                var unitreceiptnote           = unitreceiptnotes.Where(x => x.Id.Equals(item.URNId)).FirstOrDefault();
                var unitreceiptnoteItem       = unitreceiptnoteItems.FirstOrDefault(x => x.Id.Equals(item.URNItemsId));
                var deliveryorderdetail       = deliveryorderdetails.FirstOrDefault(x => x.Id.Equals(item.DODEtailId));
                var purchaseorder             = purchaseorders.FirstOrDefault(x => x.Id.Equals(item.IPOId));
                var receiptCorrectionitem     = receiptCorrectionitems.FirstOrDefault(x => x.Id.Equals(item.RCorrId));
                var unitexpenditureitem       = unitexpenditureitems.FirstOrDefault(x => x.Id.Equals(item.UENItemsId));
                var unitexpenditure           = unitexpenditures.FirstOrDefault(x => x.Id.Equals(item.UENId));
                var externalpurchaseorderitem = externalpurchaseorderitems.FirstOrDefault(x => x.Id.Equals(item.EPOItemId));
                var externalpurchaseorder     = externalpurchaseorders.FirstOrDefault(x => x.Id.Equals(item.EPOId));
                //var balancestock = balancestocks.FirstOrDefault(x => x.EPOItemId.Equals(item.EPOItemId));

                SaldoAkhir.Add(new GarmentStockReportViewModel
                {
                    BeginningBalanceQty = 0,
                    BeginningBalanceUom = unitreceiptnoteItem.SmallUomUnit,
                    Buyer                = purchaseorder.BuyerCode,
                    EndingBalanceQty     = 0,
                    EndingUom            = unitreceiptnoteItem.SmallUomUnit,
                    ExpandUom            = unitreceiptnoteItem.SmallUomUnit,
                    ExpendQty            = unitexpenditureitem == null ? 0 : unitexpenditureitem.Quantity,
                    NoArticle            = purchaseorder.Article,
                    PaymentMethod        = externalpurchaseorder == null ? "-" : externalpurchaseorder.PaymentMethod,
                    PlanPo               = unitreceiptnoteItem == null ? "-" : unitreceiptnoteItem.POSerialNumber,
                    POId                 = purchaseorder.Id,
                    ProductCode          = unitreceiptnoteItem.ProductCode,
                    ProductName          = unitreceiptnoteItem.ProductName,
                    ProductRemark        = unitreceiptnoteItem.ProductRemark,
                    ReceiptCorrectionQty = receiptCorrectionitem == null ? 0 : (decimal)receiptCorrectionitem.CorrectionQuantity,
                    ReceiptQty           = unitreceiptnoteItem.ReceiptQuantity,
                    ReceiptUom           = unitreceiptnoteItem.SmallUomUnit,
                    RO              = unitreceiptnoteItem.RONo,
                    UNitCode        = unitreceiptnote.UnitCode,
                    UnitSenderCode  = unitexpenditure == null ? "-" : unitexpenditure.UnitSenderCode,
                    UnitRequestCode = unitexpenditure == null ? "-" : unitexpenditure.UnitRequestCode
                });
            }

            SaldoAkhir = (from query in SaldoAkhir
                          group query by new { query.ProductCode, query.ProductName, query.RO, query.PlanPo, query.POId, query.UNitCode, query.UnitSenderCode } into data
                          //group query by new { query.POId } into data
                          select new GarmentStockReportViewModel
            {
                BeginningBalanceQty = data.Sum(x => x.BeginningBalanceQty),
                BeginningBalanceUom = data.FirstOrDefault().BeginningBalanceUom,
                Buyer = data.FirstOrDefault().Buyer,
                EndingBalanceQty = data.FirstOrDefault().EndingBalanceQty,
                EndingUom = data.FirstOrDefault().EndingUom,
                ExpandUom = data.FirstOrDefault().EndingUom,
                ExpendQty = data.Sum(x => x.ExpendQty),
                NoArticle = data.FirstOrDefault().NoArticle,
                PaymentMethod = data.FirstOrDefault().PaymentMethod,
                PlanPo = data.FirstOrDefault().PlanPo,
                POId = data.FirstOrDefault().POId,
                ProductCode = data.FirstOrDefault().ProductCode,
                ProductName = data.FirstOrDefault().ProductName,
                ProductRemark = data.FirstOrDefault().ProductRemark,
                ReceiptCorrectionQty = data.Sum(x => x.ReceiptCorrectionQty),
                //ReceiptQty = data.Sum(x => x.ReceiptQty),
                ReceiptQty = data.FirstOrDefault().ReceiptQty,
                ReceiptUom = data.FirstOrDefault().ReceiptUom,
                RO = data.FirstOrDefault().RO,
                UNitCode = data.FirstOrDefault().UNitCode,
                UnitSenderCode = data.FirstOrDefault().UnitSenderCode,
                UnitRequestCode = data.FirstOrDefault().UnitRequestCode
            }).ToList();
            var SaldoAkhirId = SaldoAkhir.Select(x => x.POId).ToList();
            var SA           = (from a in dbContext.GarmentUnitReceiptNotes
                                join b in dbContext.GarmentUnitReceiptNoteItems on a.Id equals b.URNId
                                join i in dbContext.GarmentInternalPurchaseOrders on b.POId equals i.Id
                                join c in dbContext.GarmentDeliveryOrderDetails on b.DODetailId equals c.Id
                                join e in dbContext.GarmentUnitExpenditureNoteItems on b.Id equals e.URNItemId into UENItem
                                from ee in UENItem.DefaultIfEmpty()
                                join h in dbContext.GarmentReceiptCorrectionItems on b.Id equals h.URNItemId into RC
                                from hh in RC.DefaultIfEmpty()
                                join f in dbContext.GarmentUnitExpenditureNotes on ee.UENId equals f.Id into UEN
                                from ff in UEN.DefaultIfEmpty()
                                join g in dbContext.GarmentExternalPurchaseOrders on b.EPOItemId equals g.Id into Exter
                                from gg in Exter.DefaultIfEmpty()
                                join epoItem in dbContext.GarmentExternalPurchaseOrderItems on b.EPOItemId equals epoItem.Id into EP
                                from epoItem in EP.DefaultIfEmpty()
                                join epo in dbContext.GarmentExternalPurchaseOrders on epoItem.GarmentEPOId equals epo.Id into EPO
                                from epo in EPO.DefaultIfEmpty()
                                where c.CodeRequirment == (string.IsNullOrWhiteSpace(ctg) ? c.CodeRequirment : ctg) &&
                                a.IsDeleted == false && b.IsDeleted == false &&
                                a.CreatedUtc > lastdate && a.CreatedUtc < DateFrom &&
                                a.UnitCode == (string.IsNullOrWhiteSpace(unitcode) ? a.UnitCode : unitcode) &&
                                SaldoAkhirId.Contains(i.Id)
                                select new
            {
                //BSId = bb == null ? "" : bb.BalanceStockId,
                URNItemsId = b == null ? 0 : b.Id,
                UENItemId = ee == null ? 0 : ee.Id,
                RCorItemId = hh != null ? hh.Id : 0,
                URNId = a == null ? 0 : a.Id,
                DODEtailId = c == null ? 0 : c.Id,
                IPOId = i == null ? 0 : i.Id,
                RCorrId = hh == null ? 0 : hh.Id,
                //UENItemsId = ee == null ? 0 : ee.Id,
                UENId = ff == null ? 0 : ff.Id,
                UnitCode = a == null ? "-" : a.UnitCode,
                UnitSenderCode = ff == null ? "-" : ff.UnitSenderCode,
                EPOItemId = epoItem == null ? 0 : epoItem.Id,
                EPOId = epo == null ? 0 : epo.Id,
                //SAQty = (bb != null ? bb.CloseStock : 0) + (hh != null ? hh.CorrectionQuantity : 0) + (double)b.ReceiptQuantity - (ee != null ? ee.Quantity : 0),
                //SAPrice = (bb != null ? bb.ClosePrice : 0) + (hh != null ? (decimal)hh.PricePerDealUnit * (decimal)hh.CorrectionQuantity : 0) +  b.PricePerDealUnit * b.ReceiptQuantity - (ee != null ? (decimal)ee.PricePerDealUnit * (decimal)ee.Quantity : 0)
            }).ToList();

            var SaldoAwals = SA.Select(x => x).ToList().Distinct();
            var saldoawalunitreceiptnoteIds       = SaldoAwals.Select(x => x.URNId).ToList();
            var saldoawalunitreceiptnotes         = dbContext.GarmentUnitReceiptNotes.Where(x => saldoawalunitreceiptnoteIds.Contains(x.Id)).Select(s => new { s.ReceiptDate, s.URNType, s.UnitCode, s.Id }).ToList();
            var saldoawalunitreceiptnoteItemIds   = SaldoAwals.Select(x => x.URNItemsId).ToList();
            var saldoawalunitreceiptnoteItems     = dbContext.GarmentUnitReceiptNoteItems.Where(x => saldoawalunitreceiptnoteItemIds.Contains(x.Id)).Select(s => new { s.ProductCode, s.ProductName, s.ProductRemark, s.RONo, s.SmallUomUnit, s.POSerialNumber, s.ReceiptQuantity, s.PricePerDealUnit, s.Id }).ToList();
            var saldoawaldeliveryorderdetailid    = SaldoAwals.Select(x => x.DODEtailId).ToList();
            var saldoawaldeliveryorderdetails     = dbContext.GarmentDeliveryOrderDetails.Where(x => saldoawaldeliveryorderdetailid.Contains(x.Id)).Select(s => new { s.CodeRequirment, s.Id }).ToList();
            var saldoawalpurchaseorderIds         = SaldoAwals.Select(x => x.IPOId).ToList();
            var saldoawalpurchaseorders           = dbContext.GarmentInternalPurchaseOrders.Where(x => saldoawalpurchaseorderIds.Contains(x.Id)).Select(s => new { s.BuyerCode, s.Id, s.Article }).ToList();
            var saldoawalreceiptCorrectionitemIds = SaldoAwals.Select(x => x.RCorrId).ToList();
            var saldoawalreceiptCorrectionitems   = dbContext.GarmentReceiptCorrectionItems.Where(x => saldoawalreceiptCorrectionitemIds.Contains(x.Id)).Select(s => new { s.CorrectionQuantity, s.PricePerDealUnit, s.Id }).ToList();
            var saldoawalunitexpenditureitemIds   = SaldoAwals.Select(x => x.UENItemId).ToList();
            var saldoawalunitexpenditureitems     = dbContext.GarmentUnitExpenditureNoteItems.Where(x => saldoawalunitexpenditureitemIds.Contains(x.Id)).Select(s => new { s.PricePerDealUnit, s.Quantity, s.Id }).ToList();
            var saldoawalunitexpenditureIds       = SaldoAwals.Select(x => x.UENId).ToList();
            var saldoawalunitexpenditures         = dbContext.GarmentUnitExpenditureNotes.Where(x => saldoawalunitexpenditureIds.Contains(x.Id)).Select(s => new { s.UnitRequestCode, s.UnitSenderCode, s.ExpenditureTo, s.Id }).ToList();
            //var saldoawalbalancestockepoitemids = SaldoAwals.Select(x => x.BSId).ToList();

            var saldoawalexternalpurchaseorderitemIds = SaldoAwals.Select(x => x.EPOItemId).ToList();
            var saldoawalexternalpurchaseorderitems   = dbContext.GarmentExternalPurchaseOrderItems.Where(x => saldoawalexternalpurchaseorderitemIds.Contains(x.Id)).Select(s => new { s.Id }).ToList();
            var saldoawalexternalpurchaseorderIds     = SaldoAwals.Select(x => x.EPOId).ToList();
            var saldoawalexternalpurchaseorders       = dbContext.GarmentExternalPurchaseOrders.Where(x => saldoawalexternalpurchaseorderIds.Contains(x.Id)).Select(s => new { s.PaymentMethod, s.Id }).ToList();
            var saldoawalbalancestocks = BalaceStock.Where(x => saldoawalexternalpurchaseorderitemIds.Contains((long)x.EPOItemId)).Select(s => new { s.ArticleNo, s.ClosePrice, s.CloseStock, s.EPOID, s.EPOItemId, s.BalanceStockId }).ToList();
            List <GarmentStockReportViewModel> SaldoAwal = new List <GarmentStockReportViewModel>();

            foreach (var i in SaldoAwals)
            {
                var saldoawalunitreceiptnote           = saldoawalunitreceiptnotes.AsParallel().FirstOrDefault(x => x.Id == i.URNId);
                var saldoawalunitreceiptnoteItem       = saldoawalunitreceiptnoteItems.AsParallel().FirstOrDefault(x => x.Id.Equals(i.URNItemsId));
                var saldoawaldeliveryorderdetail       = saldoawaldeliveryorderdetails.AsParallel().FirstOrDefault(x => x.Id.Equals(i.DODEtailId));
                var saldoawalpurchaseorder             = saldoawalpurchaseorders.AsParallel().FirstOrDefault(x => x.Id.Equals(i.IPOId));
                var saldoawalreceiptCorrectionitem     = saldoawalreceiptCorrectionitems.AsParallel().FirstOrDefault(x => x.Id.Equals(i.RCorrId));
                var saldoawalunitexpenditureitem       = saldoawalunitexpenditureitems.AsParallel().FirstOrDefault(x => x.Id.Equals(i.URNItemsId));
                var saldoawalunitexpenditure           = saldoawalunitexpenditures.AsParallel().FirstOrDefault(x => x.Id.Equals(i.URNId));
                var saldoawalbalancestock              = saldoawalbalancestocks.AsParallel().FirstOrDefault(x => x.EPOItemId.Equals(i.EPOItemId));
                var saldoawalexternalpurchaseorderitem = saldoawalexternalpurchaseorderitems.AsParallel().FirstOrDefault(x => x.Id.Equals(i.EPOItemId));
                var saldoawalexternalpurchaseorder     = saldoawalexternalpurchaseorders.AsParallel().FirstOrDefault(x => x.Id.Equals(i.EPOId));

                SaldoAwal.Add(new GarmentStockReportViewModel
                {
                    BeginningBalanceQty = saldoawalbalancestock == null ? (saldoawalreceiptCorrectionitem != null ? (decimal)saldoawalreceiptCorrectionitem.CorrectionQuantity : 0) + saldoawalunitreceiptnoteItem.ReceiptQuantity - (saldoawalunitexpenditureitem != null ? (decimal)saldoawalunitexpenditureitem.Quantity : 0) : (decimal)saldoawalbalancestock.CloseStock + (saldoawalreceiptCorrectionitem != null ? (decimal)saldoawalreceiptCorrectionitem.CorrectionQuantity : 0) + saldoawalunitreceiptnoteItem.ReceiptQuantity - (saldoawalunitexpenditureitem != null ? (decimal)saldoawalunitexpenditureitem.Quantity : 0),
                    //BeginningBalanceQty = saldoawalbalancestock != null ? (decimal)saldoawalbalancestock.CloseStock : 0) + (saldoawalreceiptCorrectionitem != null ? (decimal)saldoawalreceiptCorrectionitem.CorrectionQuantity : 0) + saldoawalunitreceiptnoteItem.ReceiptQuantity - (saldoawalunitexpenditureitem != null ? (decimal)saldoawalunitexpenditureitem.Quantity : 0,
                    BeginningBalanceUom = saldoawalunitreceiptnoteItem.SmallUomUnit,
                    Buyer                = saldoawalpurchaseorder.BuyerCode,
                    EndingBalanceQty     = 0,
                    EndingUom            = saldoawalunitreceiptnoteItem.SmallUomUnit,
                    ExpandUom            = saldoawalunitreceiptnoteItem.SmallUomUnit,
                    ExpendQty            = saldoawalunitexpenditureitem == null ? 0 : saldoawalunitexpenditureitem.Quantity,
                    NoArticle            = saldoawalpurchaseorder.Article,
                    PaymentMethod        = saldoawalexternalpurchaseorder == null ? "-" : saldoawalexternalpurchaseorder.PaymentMethod,
                    PlanPo               = saldoawalunitreceiptnoteItem == null ? "-" : saldoawalunitreceiptnoteItem.POSerialNumber,
                    POId                 = saldoawalpurchaseorder == null ? 0 : saldoawalpurchaseorder.Id,
                    ProductCode          = saldoawalunitreceiptnoteItem.ProductCode,
                    ProductName          = saldoawalunitreceiptnoteItem.ProductName,
                    ProductRemark        = saldoawalunitreceiptnoteItem.ProductRemark,
                    ReceiptCorrectionQty = saldoawalreceiptCorrectionitem == null ? 0 : (decimal)saldoawalreceiptCorrectionitem.CorrectionQuantity,
                    ReceiptQty           = saldoawalunitreceiptnoteItem.ReceiptQuantity,
                    ReceiptUom           = saldoawalunitreceiptnoteItem.SmallUomUnit,
                    RO              = saldoawalunitreceiptnoteItem.RONo,
                    UNitCode        = saldoawalunitreceiptnote.UnitCode,
                    UnitSenderCode  = saldoawalunitexpenditure == null ? "-" : saldoawalunitexpenditure.UnitSenderCode,
                    UnitRequestCode = saldoawalunitexpenditure == null ? "-" : saldoawalunitexpenditure.UnitRequestCode
                });
            }

            SaldoAwal = (from query in SaldoAwal
                         group query by new { query.ProductCode, query.ProductName, query.RO, query.PlanPo, query.POId, query.UNitCode, query.UnitSenderCode } into data
                         select new GarmentStockReportViewModel
            {
                BeginningBalanceQty = data.Sum(x => x.BeginningBalanceQty),
                BeginningBalanceUom = data.FirstOrDefault().BeginningBalanceUom,
                Buyer = data.FirstOrDefault().Buyer,
                EndingBalanceQty = data.FirstOrDefault().EndingBalanceQty,
                EndingUom = data.FirstOrDefault().EndingUom,
                ExpandUom = data.FirstOrDefault().EndingUom,
                ExpendQty = 0,
                NoArticle = data.FirstOrDefault().NoArticle,
                PaymentMethod = data.FirstOrDefault().PaymentMethod,
                PlanPo = data.FirstOrDefault().PlanPo,
                POId = data.FirstOrDefault().POId,
                ProductCode = data.FirstOrDefault().ProductCode,
                ProductName = data.FirstOrDefault().ProductName,
                ProductRemark = data.FirstOrDefault().ProductRemark,
                ReceiptCorrectionQty = 0,
                ReceiptQty = 0,
                ReceiptUom = data.FirstOrDefault().ReceiptUom,
                RO = data.FirstOrDefault().RO,
                UNitCode = data.FirstOrDefault().UNitCode,
                UnitSenderCode = data.FirstOrDefault().UnitSenderCode,
                UnitRequestCode = data.FirstOrDefault().UnitRequestCode
            }).ToList();


            var stock = (from a in SaldoAkhir
                         join b in SaldoAwal on a.POId equals b.POId into stockdata
                         from bb in stockdata.DefaultIfEmpty()
                         select new GarmentStockReportViewModel
            {
                BeginningBalanceQty = (bb == null ? 0 : bb.BeginningBalanceQty),
                BeginningBalanceUom = a.BeginningBalanceUom,
                Buyer = a.Buyer,
                EndingBalanceQty = (bb == null ? 0 : bb.BeginningBalanceQty) + a.ReceiptQty + a.ReceiptCorrectionQty - (decimal)a.ExpendQty,
                EndingUom = a.EndingUom,
                ExpendQty = a.ExpendQty,
                NoArticle = a.NoArticle,
                PaymentMethod = a.PaymentMethod,
                PlanPo = a.PlanPo,
                POId = a.POId,
                ProductCode = a.ProductCode,
                ProductName = a.ProductName,
                ProductRemark = a.ProductRemark,
                ReceiptCorrectionQty = a.ReceiptCorrectionQty,
                ReceiptQty = a.ReceiptQty,
                ReceiptUom = a.ReceiptUom,
                RO = a.RO,
                UNitCode = a.UNitCode,
                UnitSenderCode = a.UnitSenderCode,
                UnitRequestCode = a.UnitRequestCode
            }).ToList();

            //var stock = SaldoAwal.Concat(SaldoAkhir).ToList();
            //stock = (from query in stock
            //         group query by new { query.POId, query.ProductCode, query.RO } into data
            //         select new GarmentStockReportViewModel
            //         {
            //             BeginningBalanceQty = data.Sum(x => x.BeginningBalanceQty),
            //             BeginningBalanceUom = data.FirstOrDefault().BeginningBalanceUom,
            //             Buyer = data.FirstOrDefault().Buyer,
            //             EndingBalanceQty = data.Sum(x => x.BeginningBalanceQty) + data.Sum(x => x.ReceiptQty) + data.Sum(x => x.ReceiptCorrectionQty) - data.Sum(x => (decimal)x.ExpendQty),
            //             EndingUom = data.FirstOrDefault().EndingUom,
            //             ExpandUom = data.FirstOrDefault().EndingUom,
            //             ExpendQty = data.Sum(x => x.ExpendQty),
            //             NoArticle = data.FirstOrDefault().NoArticle,
            //             PaymentMethod = data.FirstOrDefault().PaymentMethod,
            //             PlanPo = data.FirstOrDefault().PlanPo,
            //             POId = data.FirstOrDefault().POId,
            //             ProductCode = data.FirstOrDefault().ProductCode,
            //             ProductName = data.FirstOrDefault().ProductName,
            //             ProductRemark = data.FirstOrDefault().ProductRemark,
            //             ReceiptCorrectionQty = data.Sum(x => x.ReceiptCorrectionQty),
            //             ReceiptQty = data.Sum(x => x.ReceiptQty),
            //             ReceiptUom = data.FirstOrDefault().ReceiptUom,
            //             RO = data.FirstOrDefault().RO,
            //             UNitCode = data.FirstOrDefault().UNitCode,
            //             UnitSenderCode = data.FirstOrDefault().UnitSenderCode,
            //             UnitRequestCode = data.FirstOrDefault().UnitRequestCode
            //         }).ToList();
            return(stock);
            //return SaldoAwal;
        }