<?php
function UpdateData() {
        global $objAxpDB;
        $objAxpDB->Allow_Update(false);
        $strSQL=array();
        switch($_REQUEST[mode]) {
        case "update":

                     list($tgl,$bln,$thn) = explode('/', $_REQUEST['fdtrans_date']);
                      $fdTransDate="{$thn}-{$bln}-{$tgl}";
                      list($tgl,$bln,$thn) = explode('/', $_REQUEST['fdfinished_date']);
                      $fdTransEnd="{$thn}-{$bln}-{$tgl}";

                     $ssql="UPDATE  txproduction_order_request_hdr SET
                                            fdtrans_date='{$fdTransDate}',
                                            fdfinished_date='{$fdTransEnd}',
                                            ftmodified_user='{$_SESSION['username']}',
                                            fdmodified_date=NOW(),
                                            ftnotes='".$_REQUEST['ftnotes']."' 
                                            WHERE ftpor_no='{$_REQUEST['ftpor_no']}'";
                      
                         $strSQL['Insert POS_HDR'][]=$ssql;
                         
                    for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                                                         
                    $ssql="UPDATE txproduction_order_request_det SET
                                                        ffqty='".CCur($_REQUEST['ffqtyD0'][$j])."'
                                                        WHERE ftpor_no='{$_REQUEST['ftpor_no']}' and   concat(ftstock_group_code,ftstock_subgroup_code,ftdecal_code)='".$_REQUEST['fttype_barang_codeD0'][$j]."' and
                                                        ftcolor_code='".$_REQUEST['ftcolor_codeD0'][$j]."' and
                                                        ftsize_code='".$_REQUEST['ftsize_codeD0'][$j]."'";
               
                    $strSQL['Insert POS_HDR'][]=$ssql;
            }



         break;
        }
       # pr($strSQL);die("m");
        return $strSQL;
   }
   
 function FinalData() {
    $exist=0;
    global $objAxpDB;
    $objAxpDB->Allow_Save(false);
    $strSQL=array();
    switch($_REQUEST[mode]) {
    case "final":
           $DateNow=$_REQUEST['fdtrans_date'];
         
          
          
           for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                      if((double)CCur($_REQUEST['ffqtyD0'][$j])>0) {
                           $exist=1;
                       }
//                    $strSQL['Update POR'][]="UPDATE txproduction_order_request_det SET
//                                                ffqty='".CCur($_REQUEST['ffqtyD0'][$j])."'
//                                                WHERE ftpor_no='{$_REQUEST['ftpor_no']}' and   concat(ftstock_group_code,ftstock_subgroup_code,ftdecal_code)='".$_REQUEST['fttype_barang_codeD0'][$j]."' and
//                                                ftcolor_code='".$_REQUEST['ftcolor_codeD0'][$j]."' and
//                                                ftsize_code='".$_REQUEST['ftsize_codeD0'][$j]."'";
                    $ssql="UPDATE txproduction_order_request_det SET
                                                        ffqty='".CCur($_REQUEST['ffqtyD0'][$j])."'
                                                        WHERE ftpor_no='{$_REQUEST['ftpor_no']}' and   concat(ftstock_group_code,ftstock_subgroup_code,ftdecal_code)='".$_REQUEST['fttype_barang_codeD0'][$j]."' and
                                                        ftcolor_code='".$_REQUEST['ftcolor_codeD0'][$j]."' and
                                                        ftsize_code='".$_REQUEST['ftsize_codeD0'][$j]."'";
                     mysql_query( $ssql);
//                    $strSQL['Insert POS_HDR'][]=$ssql;
             }

          list($tgl,$bln,$thn) = explode('/', $_REQUEST['fdtrans_date']);
          $fdTransDate="{$thn}-{$bln}-{$tgl}";
          list($tgl,$bln,$thn) = explode('/', $_REQUEST['fdfinished_date']);
          $fdTransEnd="{$thn}-{$bln}-{$tgl}";

//           $strSQL['Final POR'][]="UPDATE  txproduction_order_request_hdr SET
//                                    fdtrans_date='{$fdTransDate}',
//                                    fdfinished_date='{$fdTransEnd}',
//                                    ftnotes='{$_REQUEST['ftnotes']}'
//                                    ,fnstatus='1',ftmodified_user='{$_SESSION['username']}',
//                                    fdmodified_date=NOW()
//                                    WHERE ftpor_no='{$_REQUEST['ftpor_no']}'";
              $ssql="UPDATE  txproduction_order_request_hdr SET
                                            fdtrans_date='{$fdTransDate}',
                                            fdfinished_date='{$fdTransEnd}',
                                            ftnotes='{$_REQUEST['ftnotes']}'
                                            ,fnstatus='1',ftmodified_user='{$_SESSION['username']}',
                                            fdmodified_date=NOW()
                                            WHERE ftpor_no='{$_REQUEST['ftpor_no']}'";
              mysql_query( $ssql);
//               $strSQL['Insert POS_HDR'][]=$ssql;
              if($exist==1) {
                     $ftpor_no=getAutoNumber($_SESSION['strbranch'],'TN103','txproduction_order_request_hdr','ftpor_no',$DateNow);
                    $ssql="UPDATE txproduction_order_request_hdr
                               set ftpor_no='{$ftpor_no}'
                          WHERE fttpor_no='{$_REQUEST['fttpor_no']}'";
                     mysql_query( $ssql);
//                      $strSQL['Insert POS_HDR'][]=$ssql;
                     $ssql="UPDATE txproduction_order_request_det
                               set ftpor_no='{$ftpor_no}'
                          WHERE ftpor_no='{$_REQUEST['ftpor_no']}'";
                       mysql_query( $ssql);
//                        $strSQL['Insert POS_HDR'][]=$ssql;
              }
              else {
                    $ftpor_no=$_REQUEST['ftpor_no'];
              }
              
              
                 // POS
             
                $ssql="
                    SELECT distinct c.ftstock_group_code,c.ftstock_subgroup_code,c.ftdecal_code
                        ,case when c.ftsize_code='0' then '0' else '1' end as ftsize_group
                    FROM 
                        txproduction_order_request_det c
                    WHERE 
                        c.ftpor_no='".$ftpor_no."' and c.ffqty>1
                    ORDER BY  c.ftstock_group_code,c.ftstock_subgroup_code,c.ftdecal_code";

              
               $DateNowSQL=$fdTransDate;
               $rs=mysql_query($ssql);

               $ftpos_no=getAutoNumber($_SESSION['strbranch'],'TN104','txproduction_order_sheet_hdr','ftpos_no',$DateNow);
//               $ftpor_no=$_REQUEST['ftpor_no'];
//               $DateNow=date("d/m/Y");
//               $DateNowSQL=date("Y-m-d");
              
             
               while($r=mysql_fetch_assoc($rs)) {
                   if($r['ftsize_group']=='0') {
                       $note="FREE SIZE";
                       $sql_insert=" and c.ftsize_code='0' ";
                   }

                   else {
                       $note="";
                       $sql_insert=" and c.ftsize_code<>'0' ";
                   }
                   $strSQL['Insert POS_HDR'][]=" INSERT 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,'{$note}' 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 POS_DET'][]="INSERT INTO txproduction_order_sheet_det
                                                    (
                                                    fnid,ftpos_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,fnqty_por,ftcreated_user,fdcreated_date,
                                                    ftmodified_user,fdmodified_date
                                                    )
                                                    SELECT '' as fnid,'{$ftpos_no}' AS ftpos_no,'' as fnindex,
                                                        c.ftstock_group_code,c.ftstock_subgroup_code,c.ftdecal_code,c.ftcolor_code,c.ftsize_code,
                                                        c.ftstock_group_name,c.ftstock_subgroup_name,c.ftdecal_name,c.ftcolor_name,c.ftsize_name,
                                                        c.ffqty,c.ffqty
                                                        ,'{$_SESSION['username']}',now(),'{$_SESSION['username']}',now()
                                                        FROM 
                                                        txproduction_order_request_det c
                                                    WHERE 
                                                        c.ftpor_no='".$ftpor_no."' and
                                                        c.ftstock_group_code='".$r['ftstock_group_code']."' and
                                                        c.ftstock_subgroup_code='".$r['ftstock_subgroup_code']."' and 
                                                        c.ftdecal_code='".$r['ftdecal_code']."'
                                                        $sql_insert 
                                                   ";

                                     $strSQL['Insert Index POS_DET'][]="update
                                                txproduction_order_sheet_det a,
                                                (select c.fnid,(select ifnull(count(d.fnid),0)+1
                                                                from txproduction_order_sheet_det d
                                                                where d.fnid<c.fnid and d.ftpos_no='{$ftpos_no}') as m
                                                        from  txproduction_order_sheet_det c
                                                        where c.ftpos_no='{$ftpos_no}'
                                                        ) b
                                                    set a.fnindex=b.m
                                                    where a.ftpos_no='{$ftpos_no}' and a.fnid=b.fnid";

                   $ftpos_no=getAutoNumberTemp('TN104','txproduction_order_sheet_hdr','ftpos_no',$DateNow,$ftpos_no);
               }


    break;

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


?>
