<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>EHONGMD</title>
  <?php
  // error_reporting(0);
  // error_reporting(E_ALL);
  include '_plugin.php';
  include '_connect.php';
?>
</head>
<body class="hold-transition skin-red sidebar-mini">
<div class="wrapper">

<?php
include '_header.php';
include '_menu.php';
$DMY_1 = $_GET['DMY_1'];
$DMY_2 = $_GET['DMY_2'];
$xcorp_no = $_GET['xcorp_no'];
$type_sale = $_GET['type_sale'];
if ($type_sale == '1') {
  $typesale = "AND a.type_sale = '1'";
}
if ($type_sale == '2') {
  $typesale = "AND a.type_sale IN ('2','5')";
}
$calcdate1 = substr($DMY_1,0,4); $calcdate2 = substr($DMY_1,5,2); $calcdate3 = substr($DMY_1,8,2);
$calcdate = $calcdate3."/".$calcdate2."/".$calcdate1;
$calcdate12 = substr($DMY_2,0,4); $calcdate22 = substr($DMY_2,5,2); $calcdate32 = substr($DMY_2,8,2);
$calcdate25 = $calcdate32."/".$calcdate22."/".$calcdate12;
?>
  <div class="content-wrapper">
    <section class="content">
      <div class="row">
      <!-- ค้นหา -->
      <!-- ตาราง -->
      <?php include 'act_search_2.php' ?>
      <div class="col-md-12">
        <div class="box box-success">
          <div class="box-header">
            <h3 class="box-title"><p class="text-success">รายงาน ขายสินค้า (แบบละเอียด.)</p></h3>
            <p class="text-warning">วันที่ <?php echo $calcdate; ?> ถึงวันที่ <?php echo $calcdate25; ?></p>
          </div>
          <div class="box-body">
            <div class="form-group">
              <table id="example" class="table table-striped table-bordered" style="width:100%">
                <thead style="white-space: nowrap">
                  <tr>
                    <th rowspan="" style="background-color:black; color:#fff; text-align:center;"><b>No.</b></th>
                    <th rowspan="" style="background-color:black; color:#fff; text-align:center;"><b>#สัญญา</b></th>
                    <th rowspan="" style="background-color:black; color:#fff; text-align:center;"><b>ลูกค้า</b></th>
                    <th rowspan="" style="background-color:black; color:#fff; text-align:center;"><b>ที่อยู่</b></th>
                    <th rowspan="" style="background-color:black; color:#fff; text-align:center;"><b>เลขบัตร</b></th>
                    <th rowspan="" style="background-color:black; color:#fff; text-align:center;"><b>วันที่ขาย</b></th>
                    <th rowspan="" style="background-color:black; color:#fff; text-align:center;"><b>#ใบส่งของ</b></th>
                    <th rowspan="" style="background-color:black; color:#fff; text-align:center;"><b>สาขา</b></th>
                    <th rowspan="" style="background-color:black; color:#fff; text-align:center;"><b>ยี่ห้อ</b></th>
                    <th rowspan="" style="background-color:black; color:#fff; text-align:center;"><b>รุ่น</b></th>
                    <th rowspan="" style="background-color:black; color:#fff; text-align:center;"><b>ชื่อสินค้า</b></th>
                    <th rowspan="" style="background-color:black; color:#fff; text-align:center;"><b>เลขเครื่อง</b></th>
                    <th rowspan="" style="background-color:black; color:#fff; text-align:center;"><b>สภาพ</b></th>
                    <th rowspan="" style="background-color:black; color:#fff; text-align:center;"><b>สี</b></th>
                    <th rowspan="" style="background-color:black; color:#fff; text-align:center;"><b>ประเภทขาย</b></th>
                    <th rowspan="" style="background-color:yellow; color:#000; text-align:center;"><b>รับมา</b></th>
                    <th rowspan="" style="background-color:yellow; color:#000; text-align:center;"><b>ตั้งสด</b></th>
                    <th rowspan="" style="background-color:yellow; color:#000; text-align:center;"><b>(A) ดาวน์</b></th>
                    <th rowspan="" style="background-color:yellow; color:#000; text-align:center;"><b>(B) เงินต้น</b></th>
                    <th rowspan="" style="background-color:yellow; color:#000; text-align:center;"><b>(C) ดอกเบี้ย</b></th>
                    <th rowspan="" style="background-color:yellow; color:#000; text-align:center;"><b>(A+B+C) ราคารวม</b></th>
                    <th rowspan="" style="background-color:yellow; color:#000; text-align:center;"><b>ส่วนเหลื่อม</b></th>
                    <th rowspan="" style="background-color:black; color:#fff; text-align:center;"><b>รับในนาม</b></th>
                  </tr>
                </thead>
                <tbody style="white-space: nowrap">
                  <?php
                    $sql = "SELECT *, (price - cost) AS product_profit1 FROM [EHONGDB].[dbo].[hps_ctmain] a
                    LEFT OUTER JOIN (SELECT br_id AS bbr_id, br_name FROM [EHONGDB].[dbo].[branch]) b ON (a.br_id = b.bbr_id)
                    LEFT OUTER JOIN (SELECT serial_no AS xserial_no, product_id AS xproduct_id, product_cost, color_name AS xcolor_name, crop_no AS zcorp_no FROM [EHONGDB].[dbo].[product_vserialno]) c ON (a.product_id = c.xproduct_id AND a.serial_no = c.xserial_no)
                    WHERE (br_id LIKE '%' AND (ct_date BETWEEN '$DMY_1' AND '$DMY_2') AND (ptype_no LIKE 'N') AND ptype_no <> 'F' AND status <> 'X')
                    $typesale AND (xcorp_no LIKE '$xcorp_no' OR xcorp_no IS NULL)
                    ORDER BY ct_date, ct_no";
                    $select=$conn->prepare($sql);
                    $select->execute();
                    // echo $sql;
                    $item = 1;
                    $sum_num=0;
                    $sum_cost=0;
                    $sum_price=0;
                    $sum_down=0;
                    $sum_netamnt=0;
                    $sum_interate=0;
                    $sum_final=0;
                    $sum_profit=0;
                    $profit=0;
                    $sum_profit=0;
                    while($fetch=$select->fetch()) {
                      if ($fetch['ct_date'] != '') {
                        $BILL_DATE1 = substr($fetch['ct_date'],0,4)+543; $BILL_DATE2 = substr($fetch['ct_date'],5,2); $BILL_DATE3 = substr($fetch['ct_date'],8,2);
                        $BILL_DATE = $BILL_DATE3."/".$BILL_DATE2."/".$BILL_DATE1;
                      } else {
                        $BILL_DATE = '';
                      }
                      $sum_cost=$sum_cost+$fetch["product_cost"];
                      $sum_price=$sum_price+$fetch["cost"];
                      $sum_down=$sum_down+$fetch["down_amnt"];
                      $sum_netamnt=$sum_netamnt+$fetch["net_amnt"];
                      $sum_interate=$sum_interate+$fetch["interate_cost"];
                      $sum_final=$sum_final+$fetch["price"];
                      if($fetch['type_sale']=="1"||$fetch['type_sale']=="2"||$fetch['type_sale']=="4"){
                        $profit=$fetch["cost"]-$fetch["product_cost"];
                      }else if($fetch['type_sale']=="3"){
                         $profit=$fetch["cost"]-$fetch["product_cost"];
                      };
                      $sum_profit=$sum_profit+$profit;
                      $astr="";
                      if($fetch["df_addrhome"]!="") $astr.=$fetch["df_addrhome"];
                      if($fetch["df_tuname"]!="") $astr.=" ต. ".$fetch["df_tuname"];
                      if($fetch["df_amname"]!="") $astr.=" อ. ".$fetch["df_amname"];
                      if($fetch["df_prname"]!="") $astr.=" จ. ".$fetch["df_prname"];
                      if($fetch["df_zip"]!="") $astr.=" ".$fetch["df_zip"];
                      if($fetch["ct_mobileno"]) $astr.=" โทร.".$fetch["ct_mobileno"];
                      $person_address=$astr;
                  ?>
                  <tr>
                    <td style="background-color:; color:; text-align:right;"><?php echo number_format($item); ?>.</td>
                    <td style="background-color:; color:; text-align:;"><a href="http://192.168.10.51/xehong-linux/index.php?sid=hps&fid=view&ct_no=<?=$fetch["ct_no"]."#"?>" target="_blank"><?=$fetch["ct_no"]?></a></td>
                    <td style="background-color:; color:; text-align:;"><?php echo $fetch['ps_name']; ?></td>
                    <td style="background-color:; color:; text-align:;"><?php echo $person_address; ?></td>
                    <td style="background-color:; color:; text-align:;"><?php echo $fetch['ct_cardno']; ?></td>
                    <td style="background-color:; color:; text-align:;"><?php echo $BILL_DATE; ?></td>
                    <td style="background-color:; color:; text-align:;"><?php echo $fetch['bill_no']; ?></td>
                    <td style="background-color:; color:; text-align:;"><?php echo $fetch['br_id']." : ".$fetch['br_name']; ?></td>
                    <td style="background-color:; color:; text-align:;"><?php echo $fetch['brand_name']; ?></td>
                    <td style="background-color:; color:; text-align:;"><?php echo $fetch['model_name']; ?></td>
                    <td style="background-color:; color:; text-align:;"><?php echo $fetch["product_name"]; ?></td>
                    <td style="background-color:; color:; text-align:;"><?php echo $fetch['serial_no']; ?></td>
                    <td style="background-color:; color:; text-align:center;"><?php echo $fetch['ptype_no']; ?></td>
                    <td style="background-color:; color:; text-align:;"><?php echo $fetch['xcolor_name']; ?></td>
                    <td style="background-color:; color:; text-align:center;"><?php echo $fetch['type_sale']; ?></td>
                    <td style="background-color:yellow; color:#000; text-align:right;"><?php echo number_format($fetch['product_cost'],2); ?></td>
                    <td style="background-color:yellow; color:#000; text-align:right;"><?php echo number_format($fetch['cost'],2); ?></td>
                    <td style="background-color:yellow; color:#000; text-align:right;"><?php echo number_format($fetch['down_amnt'],2); ?></td>
                    <td style="background-color:yellow; color:#000; text-align:right;"><?php echo number_format($fetch['net_amnt'],2); ?></td>
                    <td style="background-color:yellow; color:#000; text-align:right;"><?php echo number_format($fetch['interate_cost'],2); ?></td>
                    <td style="background-color:yellow; color:#000; text-align:right;"><?php echo number_format($fetch['price'],2); ?></td>
                    <td style="background-color:yellow; color:#000; text-align:right;"><?php echo number_format($profit,2); ?></td>
                    <td style="background-color:; color:; text-align:;"><?php echo $fetch['zcorp_no']; ?></td>
                  </tr>
                  <?php $item++; } ?>
                </tbody>
                <tfoot>
                  <td colspan="15" style="background-color:black; color:#fff; text-align:;"><b>รวม : </b></td>
                  <td colspan="" style="background-color:yellow; color:#000; text-align:right;"><b><?php echo number_format($sum_cost,2); ?></b></td>
                  <td colspan="" style="background-color:yellow; color:#000; text-align:right;"><b><?php echo number_format($sum_price,2); ?></b></td>
                  <td colspan="" style="background-color:yellow; color:#000; text-align:right;"><b><?php echo number_format($sum_down,2); ?></b></td>
                  <td colspan="" style="background-color:yellow; color:#000; text-align:right;"><b><?php echo number_format($sum_netamnt,2); ?></b></td>
                  <td colspan="" style="background-color:yellow; color:#000; text-align:right;"><b><?php echo number_format($sum_interate,2); ?></b></td>
                  <td colspan="" style="background-color:yellow; color:#000; text-align:right;"><b><?php echo number_format($sum_final,2); ?></b></td>
                  <td colspan="" style="background-color:yellow; color:#000; text-align:right;"><b><?php echo number_format($sum_profit,2); ?></b></td>
                  <td colspan="" style="background-color:black; color:#fff; text-align:;"><b></b></td>
                </tfoot>
            </table>
            </div>
          </div>
        </div>
      </div>

      </div>
    </section>
  </div>
</div>

<?php
// include '_footer.php';
// include '_form.php';
?>
<div class="control-sidebar-bg"></div>
<?php include '_script_2.php'; ?>
<?php include 'scroll0.php' ?>
</body>
</html>
