<?php



$_REQUEST['fdtrans_date']=CDate($_REQUEST['fdtrans_date']);

function SaveData() {
    global $objAxpDB;
    $objAxpDB->Allow_Save(false);
    $strSQL=array();
    switch($_REQUEST[mode]) {
    case "simpan":
             $strSQL['transaksiSQL'][]="INSERT INTO txproduction_monitoring_por_hdr
                                        (
                                            ftbranch_code,ftmpor_no,fdtrans_date,fnstatus,
                                            ftcreated_user,fdcreated_date,ftmodified_user,fdmodified_date
                                         )
                                         VALUES
                                         (
                                             '{$_SESSION["strbranch"]}','{$_REQUEST['ftmpor_no']}','{$_REQUEST['fdtrans_date']}','0',
                                             '{$_SESSION['username']}',now(),'{$_SESSION['username']}',now()
                                        )";

            for($j=0; $j<count($_REQUEST['ftstock_group_codeD']); $j++) {
                $m=$j+1;
                $strSQL['transaksiSQL'][]="
                                        INSERT INTO txproduction_monitoring_por_det
                                        (
                                            ftbranch_code,ftmpor_no,fnindex,
                                            ftstock_group_code,ftstock_subgroup_code,ftdecal_code,ftcolor_code,
                                            ftstock_group_name,ftstock_subgroup_name,ftdecal_name,ftcolor_name,
                                            fnqty_available,
                                            fnqty_painting,fnqty_neto,fnqty_total_por,fnqty_rekap,
                                            fnqty_container,fnqty_jakarta,fnqty_total_order,
                                            fnqty_result_temp,
                                            fnminim_stock,
                                            fnqty_result,
                                            ftcreated_user,fdcreated_date,ftmodified_user,fdmodified_date
                                        )
                                        VALUES
                                        (
                                            '{$_SESSION["strbranch"]}','{$_REQUEST['ftmpor_no']}','{$m}',
                                            '{$_REQUEST['ftstock_group_codeD'][$j]}','{$_REQUEST['ftstock_subgroup_codeD'][$j]}','{$_REQUEST['ftdecal_codeD'][$j]}','{$_REQUEST['ftcolor_codeD'][$j]}',
                                             '{$_REQUEST['ftstock_group_nameD'][$j]}','{$_REQUEST['ftstock_subgroup_nameD'][$j]}','{$_REQUEST['ftdecal_nameD'][$j]}','{$_REQUEST['ftcolor_nameD'][$j]}',
                                            '".CCur($_REQUEST['fnqty_availableD'][$j])."',
                                            '".CCur($_REQUEST['fnqty_paintingD'][$j])."','".CCur($_REQUEST['fnqty_netoD'][$j])."','".CCur($_REQUEST['fnqty_total_porD'][$j])."','".CCur($_REQUEST['fnqty_rekapD'][$j])."',
                                            '".CCur($_REQUEST['fnqty_containerD'][$j])."','".CCur($_REQUEST['fnqty_jakartaD'][$j])."','".CCur($_REQUEST['fnqty_total_orderD'][$j])."',
                                            '".CCur($_REQUEST['fnqty_result_tempD'][$j])."',
                                             '".CCur($_REQUEST['fnminim_stockD'][$j])."',
                                              '".CCur($_REQUEST['fnqty_resultD'][$j])."',
                                            '{$_SESSION['username']}',now(),'{$_SESSION['username']}',now()
                                        )";

            }
             $strSQL['transaksiSQL'][]="update txproduction_outstanding_weekly_hdr
                                        set fnstatus=4,ftmpor_no='{$_REQUEST['ftmpor_no']}' where fnstatus=1";

            break;

    }
//    pr($strSQL);
//    die("");
    return $strSQL;
}

function UpdateData() {
    global $objAxpDB;
    $objAxpDB->Allow_Update(false);
    $strSQL=array();
    switch($_REQUEST[mode]) {
    case "update":
           $strSQL['transaksiSQL'][]="UPDATE  txproduction_monitoring_por_hdr
                                        SET
                                        fdtrans_date='{$_REQUEST['fdtrans_date']}',fnstatus='0',
                                        ftmodified_user='{$_SESSION['username']}',fdmodified_date=NOW()
                                        WHERE ftmpor_no='{$_REQUEST['ftmpor_no']}'";

//          $strSQL['transaksiSQL'][]="DELETE FROM   txproduction_monitoring_por_det WHERE ftmpor_no='{$_REQUEST['ftmpor_no']}'";

        for($j=0; $j<count($_REQUEST['ftstock_group_codeD']); $j++) {
            $m=$j+1;
            $ssql="UPDATE txproduction_monitoring_por_det
                    SET fnindex='{$_REQUEST['fnindexD'][$j]}', 
                        fnqty_available='".CCur($_REQUEST['fnqty_availableD'][$j])."',
                        fnqty_painting='".CCur($_REQUEST['fnqty_paintingD'][$j])."',
                        fnqty_neto='".CCur($_REQUEST['fnqty_netoD'][$j])."',
                        fnqty_total_por='".CCur($_REQUEST['fnqty_total_porD'][$j])."',
                        fnqty_rekap='".CCur($_REQUEST['fnqty_rekapD'][$j])."',
                        fnqty_container='".CCur($_REQUEST['fnqty_containerD'][$j])."',
                        fnqty_jakarta='".CCur($_REQUEST['fnqty_jakartaD'][$j])."',
                        fnqty_total_order='".CCur($_REQUEST['fnqty_total_orderD'][$j])."',
                        fnqty_result_temp='".CCur($_REQUEST['fnqty_result_tempD'][$j])."',
                        fnminim_stock='".CCur($_REQUEST['fnminim_stockD'][$j])."',
                        fnqty_result='".CCur($_REQUEST['fnqty_resultD'][$j])."',
                        ftmodified_user='{$_SESSION['username']}',
                        fdmodified_date= now() 
                    WHERE ftmpor_no='{$_REQUEST['ftmpor_no']}' AND
                        ftstock_group_code='{$_REQUEST['ftstock_group_codeD'][$j]}' AND
                        ftstock_subgroup_code='{$_REQUEST['ftstock_subgroup_codeD'][$j]}' AND
                        ftdecal_code='{$_REQUEST['ftdecal_codeD'][$j]}' AND
                        ftcolor_code='{$_REQUEST['ftcolor_codeD'][$j]}'   ";
               $strSQL['transaksiSQL'][]=$ssql;
                
//            $strSQL['transaksiSQL'][]="
//                                     INSERT INTO txproduction_monitoring_por_det
//                                        (
//                                            ftbranch_code,ftmpor_no,fnindex,
//                                            ftstock_group_code,ftstock_subgroup_code,ftdecal_code,ftcolor_code,
//                                            ftstock_group_name,ftstock_subgroup_name,ftdecal_name,ftcolor_name,
//                                            fnqty_available,
//                                            fnqty_painting,fnqty_neto,fnqty_total_por,fnqty_rekap,
//                                            fnqty_container,fnqty_jakarta,fnqty_total_order,
//                                            fnqty_result_temp,
//                                            fnminim_stock,
//                                            fnqty_result,
//                                            ftcreated_user,fdcreated_date,ftmodified_user,fdmodified_date
//                                        )
//                                        VALUES
//                                        (
//                                            '{$_SESSION["strbranch"]}','{$_REQUEST['ftmpor_no']}','{$m}',
//                                            '{$_REQUEST['ftstock_group_codeD'][$j]}','{$_REQUEST['ftstock_subgroup_codeD'][$j]}','{$_REQUEST['ftdecal_codeD'][$j]}','{$_REQUEST['ftcolor_codeD'][$j]}',
//                                             '{$_REQUEST['ftstock_group_nameD'][$j]}','{$_REQUEST['ftstock_subgroup_nameD'][$j]}','{$_REQUEST['ftdecal_nameD'][$j]}','{$_REQUEST['ftcolor_nameD'][$j]}',
//                                            '".CCur($_REQUEST['fnqty_availableD'][$j])."',
//                                            '".CCur($_REQUEST['fnqty_paintingD'][$j])."','".CCur($_REQUEST['fnqty_netoD'][$j])."','".CCur($_REQUEST['fnqty_total_porD'][$j])."','".CCur($_REQUEST['fnqty_rekapD'][$j])."',
//                                            '".CCur($_REQUEST['fnqty_containerD'][$j])."','".CCur($_REQUEST['fnqty_jakartaD'][$j])."','".CCur($_REQUEST['fnqty_total_orderD'][$j])."',
//                                            '".CCur($_REQUEST['fnqty_result_tempD'][$j])."',
//                                             '".CCur($_REQUEST['fnminim_stockD'][$j])."',
//                                              '".CCur($_REQUEST['fnqty_resultD'][$j])."',
//                                            '{$_SESSION['username']}',now(),'{$_SESSION['username']}',now()
//                                        )";

        }
           $strSQL['transaksiSQL'][]="update txproduction_outstanding_weekly_hdr
                                        set fnstatus=4
                                            where ftmpor_no='{$_REQUEST['ftmpor_no']}' and ( fnstatus=1 or fnstatus=4)";
        break;
    }
  return $strSQL;
}

function FinalData() {

    global $objAxpDB;
    #$objAxpDB->Allow_Final(false);
    $strSQL=array();
    switch($_REQUEST[mode]) {
    case "final":

     $strSQL['transaksiSQL'][]="UPDATE  txproduction_monitoring_por_hdr
                                        SET
                                        fdtrans_date='{$_REQUEST['fdtrans_date']}',fnstatus='1',
                                        ftmodified_user='{$_SESSION['username']}',fdmodified_date=NOW()
                                        WHERE ftmpor_no='{$_REQUEST['ftmpor_no']}'";

   // $strSQL['transaksiSQL'][]="DELETE FROM   txproduction_monitoring_por_det WHERE ftmpor_no='{$_REQUEST['ftmpor_no']}'";

    
    for($j=0; $j<count($_REQUEST['ftstock_group_codeD']); $j++) {
        $m=$j+1;
//        $strSQL['transaksiSQL'][]="
//                                 INSERT INTO txproduction_monitoring_por_det
//                                        (
//                                            ftbranch_code,ftmpor_no,fnindex,
//                                            ftstock_group_code,ftstock_subgroup_code,ftdecal_code,ftcolor_code,
//                                            ftstock_group_name,ftstock_subgroup_name,ftdecal_name,ftcolor_name,
//                                            fnqty_available,
//                                            fnqty_painting,fnqty_neto,fnqty_total_por,fnqty_rekap,
//                                            fnqty_container,fnqty_jakarta,fnqty_total_order,
//                                            fnqty_result_temp,
//                                            fnminim_stock,
//                                            fnqty_result,
//                                            ftcreated_user,fdcreated_date,ftmodified_user,fdmodified_date
//                                        )
//                                        VALUES
//                                        (
//                                            '{$_SESSION["strbranch"]}','{$_REQUEST['ftmpor_no']}','{$m}',
//                                            '{$_REQUEST['ftstock_group_codeD'][$j]}','{$_REQUEST['ftstock_subgroup_codeD'][$j]}','{$_REQUEST['ftdecal_codeD'][$j]}','{$_REQUEST['ftcolor_codeD'][$j]}',
//                                             '{$_REQUEST['ftstock_group_nameD'][$j]}','{$_REQUEST['ftstock_subgroup_nameD'][$j]}','{$_REQUEST['ftdecal_nameD'][$j]}','{$_REQUEST['ftcolor_nameD'][$j]}',
//                                            '".CCur($_REQUEST['fnqty_availableD'][$j])."',
//                                            '".CCur($_REQUEST['fnqty_paintingD'][$j])."','".CCur($_REQUEST['fnqty_netoD'][$j])."','".CCur($_REQUEST['fnqty_total_porD'][$j])."','".CCur($_REQUEST['fnqty_rekapD'][$j])."',
//                                            '".CCur($_REQUEST['fnqty_containerD'][$j])."','".CCur($_REQUEST['fnqty_jakartaD'][$j])."','".CCur($_REQUEST['fnqty_total_orderD'][$j])."',
//                                            '".CCur($_REQUEST['fnqty_result_tempD'][$j])."',
//                                             '".CCur($_REQUEST['fnminim_stockD'][$j])."',
//                                              '".CCur($_REQUEST['fnqty_resultD'][$j])."',
//                                            '{$_SESSION['username']}',now(),'{$_SESSION['username']}',now()
//                                        )";
             $ssql="UPDATE txproduction_monitoring_por_det
                    SET fnindex='{$_REQUEST['fnindexD'][$j]}',
                        fnqty_available='".CCur($_REQUEST['fnqty_availableD'][$j])."',
                        fnqty_painting='".CCur($_REQUEST['fnqty_paintingD'][$j])."',
                        fnqty_neto='".CCur($_REQUEST['fnqty_netoD'][$j])."',
                        fnqty_total_por='".CCur($_REQUEST['fnqty_total_porD'][$j])."',
                        fnqty_rekap='".CCur($_REQUEST['fnqty_rekapD'][$j])."',
                        fnqty_container='".CCur($_REQUEST['fnqty_containerD'][$j])."',
                        fnqty_jakarta='".CCur($_REQUEST['fnqty_jakartaD'][$j])."',
                        fnqty_total_order='".CCur($_REQUEST['fnqty_total_orderD'][$j])."',
                        fnqty_result_temp='".CCur($_REQUEST['fnqty_result_tempD'][$j])."',
                        fnminim_stock='".CCur($_REQUEST['fnminim_stockD'][$j])."',
                        fnqty_result='".CCur($_REQUEST['fnqty_resultD'][$j])."',
                        ftmodified_user='{$_SESSION['username']}',
                        fdmodified_date= now()
                    WHERE ftmpor_no='{$_REQUEST['ftmpor_no']}' AND
                        ftstock_group_code='{$_REQUEST['ftstock_group_codeD'][$j]}' AND
                        ftstock_subgroup_code='{$_REQUEST['ftstock_subgroup_codeD'][$j]}' AND
                        ftdecal_code='{$_REQUEST['ftdecal_codeD'][$j]}' AND
                        ftcolor_code='{$_REQUEST['ftcolor_codeD'][$j]}'   ";
               $strSQL['transaksiSQL'][]=$ssql;
    }


               //POR
               $DateNow=CDateRev($_REQUEST['fdtrans_date']);#date("d/m/Y");
               $DateNowSQL=$_REQUEST['fdtrans_date'];#date("Y-m-d");
               $ftpor_no=getAutoNumber($_SESSION['strbranch'],'TN105','txproduction_order_request_hdr','fttpor_no',$DateNow);

               //group by ftstock_group_code
               //-------------------------
               $sql1="select distinct a.ftstock_group_code
                          from txproduction_monitoring_por_det a
                          where a.fnqty_result<0 and 
                                a.ftmpor_no='{$_REQUEST['ftmpor_no']}'";

               $rs=mysql_query($sql1) or die($sql1);
               while($r=mysql_fetch_assoc($rs)) {
                        $strSQL['Insert POR'][]="INSERT INTO txproduction_order_request_hdr (
                                            fnid,ftpor_no,fttpor_no,ftmpor_no,fdtrans_date,ftstock_group_code,fdfinished_date,ftnotes
                                            ,fnstatus,ftcreated_user,fdcreated_date ,ftmodified_user,
                                            fdmodified_date)
                                            SELECT
                                            '' AS fnid,'{$ftpor_no}' AS ftpor_no,'{$ftpor_no}' AS fttpor_no,'{$_REQUEST['ftmpor_no']}' as ftmpor_no,'{$DateNowSQL}' AS fdtrans_date, '{$r['ftstock_group_code']}' AS ftstock_group_code,
                                            '{$DateNowSQL}' AS fdfinished_date,'' AS ftnotes,0 AS fnstatus,
                                            '{$_SESSION['username']}' AS ftcreated_user,NOW() AS fdcreated_date,'{$_SESSION['username']}' AS ftmodified_user,
                                            NOW() AS fdmodified_date";

                        $strSQL['Insert POR'][]= "INSERT INTO txproduction_order_request_det
                                                (fnid,ftpor_no,fnindex,
                                                ftstock_group_code,ftstock_subgroup_code,ftdecal_code,ftcolor_code,ftsize_code,
                                                ftstock_group_name,ftstock_subgroup_name,ftdecal_name,ftcolor_name,ftsize_name,
                                                ffqty_mpor,ftcreated_user,
                                                fdcreated_date,ftmodified_user,fdmodified_date
                                                )
                                                select DISTINCT '' AS fnid,'{$ftpor_no}' AS ftpor_no,'' AS fnindex,
                                                    c.ftstock_group_code,
                                                    c.ftstock_subgroup_code,c.ftdecal_code,c.ftcolor_code,b.ftsize_code as ftsize_code,
                                                    c.ftstock_group_name,
                                                    c.ftstock_subgroup_name,c.ftdecal_name,c.ftcolor_name,b.ftsize_name as ftsize_code,
                                                    abs(c.fnqty_result) as fnqty_result ,
                                                     '{$_SESSION['username']}',now(),'{$_SESSION['username']}',now()
                                                from
                                                    txproduction_outstanding_weekly_hdr a,
                                                    txproduction_outstanding_weekly_det b ,
                                                    txproduction_monitoring_por_det c
                                                where a.ftpow_no=b.ftpow_no and
                                                    c.ftmpor_no=a.ftmpor_no and
                                                    a.ftmpor_no='{$_REQUEST['ftmpor_no']}'   and
                                                    c.ftstock_group_code='".$r['ftstock_group_code']."' and 
                                                    concat(c.ftstock_group_code,c.ftstock_subgroup_code,c.ftdecal_code,c.ftcolor_code)=left(b.ftitem_code,12) and
                                                    c.fnqty_result<0
                                                 ";


                                      $strSQL['Insert POR'][]=  "update
                                                        txproduction_order_request_det a,
                                                        (select c.fnid,(select ifnull(count(d.fnid),0)+1
                                                                        from txproduction_order_request_det d
                                                                        where d.fnid<c.fnid and d.ftpor_no='{$ftpor_no}') as m
                                                                from  txproduction_order_request_det c
                                                                where c.ftpor_no='{$ftpor_no}'
                                                                ) b

                                                        set a.fnindex=b.m
                                                        where a.ftpor_no='{$ftpor_no}' and a.fnid=b.fnid";

                               $ftpor_no=getAutoNumberTemp('TN105','txproduction_order_request_hdr','fttpor_no',$DateNow,$ftpor_no);

               }



                    $strSQL['Insert POR'][]="update txproduction_outstanding_weekly_hdr
                                                set fnstatus=5
                                                where (fnstatus=1 or fnstatus=4) and ftmpor_no='{$_REQUEST['ftmpor_no']}'";
//               // POS
//
//                $ssql=" SELECT c.ftcustomer_code,abs(fnqty_result) as fnqty_result
//                        FROM txproduction_monitoring_por_det a
//                            INNER JOIN tlstock_hdr b
//                            ON a.ftitem_code=b.ftitem_code
//                            INNER JOIN txproduction_outstanding_weekly_hdr C
//                            ON A.ftpow_no=C.ftpow_no
//                         WHERE a.fnqty_result<0 AND a.ftmpor_no='{$_REQUEST['ftmpor_no']}'
//                        GROUP BY c.ftcustomer_code";
//
//
//               $rs=mysql_query($ssql);
//               $ftpos_no=getAutoNumber('TN104','txproduction_order_sheet_hdr','ftpos_no',$DateNow);
//
//               while($r=mysql_fetch_assoc($rs)) {
//                   $DateNow=date("d/m/Y");
//                   $DateNowSQL=date("Y-m-d");
//
//
//                   $strSQL['Insert POS_HDR'][]=" REPLACE INTO txproduction_order_sheet_hdr
//                                                    (fnid,ftpos_no,fdtrans_date,ftpor_no,
//                                                    ftcustomer_code,fftotal_qty,ftnotes,
//                                                    fnstatus,ftcreated_user,fdcreated_date,ftmodified_user,
//                                                    fdmodified_date)
//                                                    SELECT '' AS fnid,'{$ftpos_no}' AS ftpos_no,'{$DateNowSQL}' AS fdtrans_date,'{$ftpor_no}' AS ftpor_no,
//                                                    '{$r['ftcustomer_code']}' AS ftcustomer_code,'{$r['fnqty_result']}' AS fftotal_qty,'' AS ftnotes,
//                                                    '1' AS fnstatus,'{$_SESSION['username']}' AS ftcreated_user,NOW() AS fdcreated_date,'{$_SESSION['username']}' AS ftmodified_user,
//                                                    NOW()  AS fdmodified_date";
//
//                   $strSQL['Insert POS_DET'][]="REPLACE INTO txproduction_order_sheet_det
//                                                    (
//                                                    fnid,ftpos_no,fnindex,ftmodel,
//                                                    ftcolor,ffqty,ftcreated_user,fdcreated_date,
//                                                    ftmodified_user,fdmodified_date
//                                                    )
//                                                    SELECT
//                                                    '' AS fnid,'{$ftpos_no}' AS ftpos_no,'' AS fnindex, B.ftstock_group_code AS ftmodel,
//                                                    B.ftcolor_code AS ftcolor,ABS(sum(a.fnqty_result)) AS ffqty,'{$_SESSION['username']}' AS ftcreated_user,NOW() AS fdcreated_date,
//                                                    '{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date
//                                                    FROM txproduction_monitoring_por_det a
//                                                        INNER JOIN tlstock_hdr b
//                                                        ON a.ftitem_code=b.ftitem_code
//                                                     INNER JOIN txproduction_outstanding_weekly_hdr C
//                                                     ON A.ftpow_no=C.ftpow_no
//                                                     WHERE a.fnqty_result<0 AND a.ftmpor_no='{$_REQUEST['ftmpor_no']}'
//                                                            AND c.ftcustomer_code='{$r['ftcustomer_code']}'
//                                                    GROUP BY  B.ftstock_group_code,B.ftcolor_code";
//
//                   $ftpos_no=getAutoNumberTemp('TN104','txproduction_order_sheet_hdr','ftpos_no',$DateNow,$ftpos_no);
//               }

     break;
    }
//   pr($strSQL);die("mm");
  return $strSQL;
}
function CancelData() {
    global $objAxpDB;
//    $objAxpDB->Allow_Cancel(false);
    $strSQL=array();
    switch($_REQUEST[mode]) {
      case "batal":
            $strSQL["out minggu"][]="update txproduction_outstanding_weekly_hdr
                                    set fnstatus=1,ftmpor_no=''
                                    where ftmpor_no='{$_REQUEST['ftmpor_no']}' ";
    break;

    }

    return $strSQL;
 }




?>
