<?php

namespace Cash\Lib;

use Dever;

class Set
{
    public function info($audit_type, $audit)
    {
        $config = Dever::db('cash/order')->config;

        return $config['config_audit_type'][$audit_type] . $config['config_audit'][$audit];
    }

    public function statDate($type, $day) 
    {
        if ($type == 1) {
            $string = 'Y年m月';
        } else {
            $string = 'Y年W周';
        }

        return date($string, $day);
    }

    public function statYes($info, $type)
    {
        $table = 'cash/' . $type;

        $info = is_array($info) ? $info : Dever::db($table)->one($info);
        $other = Dever::db($type . '/info')->find($info[$type . '_id']);

        return $this->statDate($info['type'], $info['day']) . '与'.$other['name'].'对账单款项是否正确<br />对账金额¥' . $info['cash'] . '元';
    }

    public function statTime($start, $end) 
    {
        return date('Y-m-d', $start) . ' ~ ' . date('Y-m-d', $end);
    }

    public function orderUpdate($id, $name, $data)
    {
        Dever::config('base')->hook = true;
        $update = array();
        $audit = Dever::param('audit', $data);
        $info = Dever::db('cash/order')->one($id);
        if ($audit > 1 && $info['refund_id'] && $info['refund_id'] > 0) {
            Dever::load('shop/lib/refund')->set('buy')->action($info['refund_id'], $audit, false, false);

            if ($audit == 2) {
                Dever::db('cash/order')->update(array('where_id' => $id, 'status' => 2));
            }
        }
    }

    # 审核对账
    public function audit_api()
    {
        $id = Dever::input('id');
        $type = Dever::input('type', 'shop');
        $table = 'cash/' . $type;

        $info = Dever::db($table)->one($id);

        if ($info) {
            Dever::db($table)->update(array('where_id' => $id, 'status' => 2));
        }

        return 'reload';
    }

    # 审核对账
    public function audit_other_api()
    {
        $id = Dever::input('id');
        $type = Dever::input('type', 'shop');
        $table = 'cash/' . $type;

        $info = Dever::db($table)->one($id);

        if ($info) {
            Dever::db($table)->update(array('where_id' => $id, $type . '_status' => 2));
        }

        return 'reload';
    }

    # 查看对账单详情
    public function view_api()
    {
        $id = Dever::input('id');
        if (!$id) {
            return false;
        }

        $show = Dever::input('show', 1);

        $type = Dever::input('type', 'shop');
        if ($type == 'shop') {
            $name = '门店';
        } elseif ($type == 'store') {
            $name = '仓库';
        } elseif ($type == 'factory') {
            $name = '工厂';
        }

        $table = 'cash/' . $type;

        $config = Dever::db($table)->config;

        $info = Dever::db($table)->one($id);

        $status = $config['config_status'][$info['status']];
        $other_status = $config['config_status'][$info[$type . '_status']];

        $other = Dever::db($type . '/info')->find($info[$type . '_id']);

        $html = '<div class="layui-col-md12"><div class="layui-card"><div class="layui-card-header">对账单详情</div><div class="layui-card-body">';

        $html .= '<table class="layui-table"><tbody>';

        $html .= '<tr>
          <td width="100">对账'.$name.'</td>
          <td>'.$this->table(false, array(array($other['name']))).'</td>

        </tr>';

        $html .= '<tr>
          <td width="80">对账时间</td>
          <td>'.$this->table(false, array(array($this->statDate($info['type'], $info['day'])))).'</td>

        </tr>';

        $html .= '<tr>
          <td width="80">对账周期</td>
          <td>'.$this->table(false, array(array($this->statTime($info['start'], $info['end'])))).'</td>

        </tr>';

        if ($type == 'store') {
            $html .= '<tr>
              <td width="80">对账商品数量</td>
              <td>'.$this->table(false, array(array($info['num']))).'</td>

            </tr>';
        } else {
            $html .= '<tr>
              <td width="80">对账金额</td>
              <td>'.$this->table(false, array(array('¥' . round($info['cash'], 2) . '元'))).'</td>

            </tr>';
        }

        if ($show == 1) {
            $html .= '<tr>
              <td width="80">'.$name.'对账状态</td>
              <td>'.$this->table(false, array(array($other_status))).'</td>

            </tr>';

            $html .= '<tr>
              <td width="100">平台对账状态</td>
              <td>'.$this->table(false, array(array($status))).'</td>

            </tr>';
        } else {
            $html .= '<tr>
              <td width="80">对账状态</td>
              <td>'.$this->table(false, array(array($other_status))).'</td>

            </tr>';
        }

        $button = array();

        if ($show == 1) {
            if ($info['status'] == 1) {

                $url = Dever::url('lib/set.audit&id='.$id.'&type=' . $type, 'cash');
                $button[] = '<button class="layui-btn layui-btn-primary" onclick="load(\''.$url.'\', \''.$this->statYes($info, $type).'\', \'请确认\')">立即确认</button>';
            }
            $config['phone'] = '联系人:' . $other['truename'] . ',联系电话:' . $other['mobile'];
            $button[] = '<button class="layui-btn layui-btn-primary" onclick="showAlert(\''.$config['phone'].'\')">联系'.$name.'</button>';

            $start = date('Y-m-d H:i:s', $info['start']);
            $end = date('Y-m-d H:i:s', $info['end']);
            $out = Dever::url('database.list_excel?project=cash&table=order&search_option_status=2&search_option_state=1&search_option_start_cdate=' . $start . '&search_option_end_cdate=' . $end, 'manage');
            
            $button[] = '<a class="layui-btn layui-btn-primary" href="'.$out.'">导出对账单</a>';
        } elseif ($show == 2) {
            if ($info[$type . '_status'] == 1 && $show != 1) {

                $url = Dever::url('lib/set.audit_other&id='.$id.'&type=' . $type, 'cash');
                $button[] = '<button class="layui-btn layui-btn-primary" onclick="load(\''.$url.'\', \''.$this->statYes($info, $type).'\', \'请确认\')">立即确认</button>';
            }
            $config = Dever::load('factory/admin/auth.config');
            $config['phone'] = '联系电话:' . $config['dz_phone'];
            $button[] = '<button class="layui-btn layui-btn-primary" onclick="showAlert(\''.$config['phone'].'\')">联系对账专员</button>';
            
            $print = Dever::url('admin/stat.print?id=' . $info['id'] . '&type=' . $type, $type);

            $button[] = '<a class="layui-btn layui-btn-primary" href="'.$print.'" target="_blank">打印对账单</a>';
        }
        
        $html .= '<tr>
              <td>功能按钮</td>
              <td>'.$this->table(false, array($button)).'</td>

            </tr>';

        $html .= '</tbody></table></div></div>';

        $id = $info[$type . '_id'];
        if ($type == 'shop') {
            $where['type'] = 1;
            $where['type_id'] = $id;
        } elseif ($type == 'factory') {
            $where['source_type'] = 3;
            $where['source_id'] = $id;
        } else {
            $where['type'] = 2;
            $where['type_id'] = $id;
            //$where['source_type_or'] = 2;
            //$where['source_id_or'] = $id;
        }
        $where['status'] = 2;
        $where['start'] = $info['start'];
        $where['end'] = $info['end'];
        $data = Dever::db('cash/order')->getAll($where);
        if ($data) {
            
            $body = array();
            $config = Dever::db('cash/order')->config;
            foreach ($data as $k => $v) {
                $cdate = date('Y-m-d H:i', $v['cdate']);
                $fdate = $v['fdate'] ? date('Y-m-d H:i', $v['fdate']) : '';
                $operdate = $v['operdate'] ? date('Y-m-d H:i', $v['operdate']) : '';
                if ($type == 'store') {
                    $cash = $v['num'];
                } elseif ($type == 'factory') {
                    $cash = '¥' . round($v['p_cash'], 2);
                } else {
                    $cash = '¥' . round($v['cash'], 2);
                }

                if ($show == 1) {
                    $url = Dever::url('project/database/list?project=shop&table=buy_order_goods&order_id='.$v['source_order_id'].'&page_type=1', 'manage');
                    $v['source_order_num'] = '<a href="'.$url.'" style="color:blue">'.$v['source_order_num'].'</a>';

                    $head = array('结算单号', '订货单号', '结算类型', '下单日期', '完成日期', '结算日期', '对账金额', '结算状态');
                    if ($type == 'store') {
                        $head[6] = '对账数量';
                    }

                    $body[] = array
                    (
                        $v['order_num'],
                        $v['source_order_num'],
                        $config['config_jstype'][$v['jstype']],
                        $cdate,
                        $fdate,
                        $operdate,
                        $cash,
                        '已入账'
                    );
                } else {
                    $head = array('订货单号', '下单日期', '完成日期', '对账金额', '状态');
                    if ($type == 'store') {
                        $head[3] = '对账数量';
                    }

                    $body[] = array
                    (
                        $v['source_order_num'],
                        $cdate,
                        $fdate,
                        $cash,
                        '已入账'
                    );
                }
            }


            $page = Dever::page("current");

            $html .= '<div class="layui-card"><div class="layui-card-header">对账清单</div><div class="layui-card-body" style="max-heights: 500px;overflow: auto;">' . $this->table($head, $body)  . $page . '</div></div>';
        }

        $html .= '</div>';

        return '<div class="layui-card-body">' . $html . '</div>';
    }

    private function table($head, $data)
    {
        $html = '';
        if ($head) {
            $html = '<table class="layui-table">';

            $html .= '<thead><tr>';
            foreach ($head as $k => $v) {
                $html .= '<th>'.$v.'</th>';
            }
            $html .= '</tr></thead>';

            $html .= '<tbody>';
            foreach ($data as $k => $v) {
                $html .= '<tr>';
                foreach ($v as $k1 => $v1) {
                    $html .= '<td>'.$v1.'</td>';
                }
                $html .= '</tr>';
            }

            $html .= '</tbody>';

            $html .= '</table>';
        } else {
            foreach ($data as $k => $v) {
                $html .= '';
                foreach ($v as $k1 => $v1) {
                    $html .= $v1 . '&nbsp;&nbsp;&nbsp;&nbsp;';
                }
                $html .= '';
            }
        }
        return $html;
    }

    # 打印订单单
    public function printer($user)
    {
        $id = Dever::input('id');
        if (!$id) {
            return false;
        }

        $type = Dever::input('type', 'shop');
        if ($type == 'shop') {
            $name = '门店';
        } elseif ($type == 'store') {
            $name = '仓库';
        } elseif ($type == 'factory') {
            $name = '工厂';
        }

        $table = 'cash/' . $type;

        $config = Dever::db($table)->config;

        $info = Dever::db($table)->one($id);

        $status = $config['config_status'][$info['status']];
        $other_status = $config['config_status'][$info[$type . '_status']];

        $other = Dever::db($type . '/info')->find($info[$type . '_id']);

        $member = Dever::db($type  . '/member')->find($user['id']);

        $factory_config = Dever::db('main/factory_config')->find();
        $main_config = Dever::db('main/config')->find();

        $pdf = Dever::load('pdf/lib/base')->init();

        $pdf->hr('-', $other['name']);

        $pdf->br()->font(20)->center('对账周期:' . $this->statTime($info['start'], $info['end']));
        $pdf->font(10);
        $pdf->br();

        $pdf->br()->left('对账日期:' . $this->statDate($info['type'], $info['day']), 80)->left('制单人:' . $member['name'], 60)->left('制单时间:' . date('Y-m-d H:i'), 40);

        $pdf->hr();

        if ($type == 'store') {
            $where['type'] = 2;
            $where['type_id'] = $info[$type . '_id'];
        } else {
            $where['source_type'] = 3;
            $where['source_id'] = $info[$type . '_id'];
        }
        
        $where['status'] = 2;
        $where['start'] = $info['start'];
        $where['end'] = $info['end'];
        $data = Dever::db('cash/order')->getAll($where);

        $body = array();
        $body_total = array();
        $body_total['cash'] = 0;
        $body_total['num'] = 0;

        if ($data) {
            foreach ($data as $k => $v) {
                $cdate = date('Y-m-d H:i', $v['cdate']);
                $fdate = $v['fdate'] ? date('Y-m-d H:i', $v['fdate']) : '';
                $operdate = $v['operdate'] ? date('Y-m-d H:i', $v['operdate']) : '';
                if ($type == 'store') {
                    $cash = $v['num'];
                } elseif ($type == 'factory') {
                    $cash = '¥' . $v['p_cash'];
                } else {
                    $cash = '¥' . $v['cash'];
                }

                $cash = round($cash, 2);

                $body[] = array
                (
                    $v['source_order_num'],
                    $cdate,
                    $fdate,
                    $cash,
                    '已入账'
                );

                $body_total['cash'] += $cash;
                $body_total['num'] += 1;
            }
        }

        $head = array(array('订单号', 60), array('下单日期', 43), array('完成日期', 43), array('对账金额', 30), array('状态', 20));

        if ($type == 'store') {
            $head[3][0] = '对账数量';
        }

        if ($body) {
            $pdf->br();
            foreach ($head as $k => $v) {
                $pdf->left($v[0], $v[1]);
            }

            foreach ($body as $k => $v) {
                $pdf->br();
                foreach ($head as $k1 => $v1) {
                    $pdf->left($v[$k1], $v1[1]);
                }
            }
            $pdf->br();
            if ($type == 'store') {
                $pdf->right('共'.$body_total['num'].'个订单,合计对账数量' . $body_total['cash'] . '个');
            } else {
                $pdf->right('共'.$body_total['num'].'个订单,合计对账金额¥' . $body_total['cash'] . '元');
            }
            
            $pdf->hr();
        }

        $pdf->br(1);
        //$pdf->SetY(-100);
        
        $pdf->right('如遇任何问题请致电客服');
        $pdf->br();

        $pdf->font(20);
        $pdf->left($main_config['name'], 160);

        $pdf->font(10);
        $pdf->right('电话:' . $factory_config['phone'], 30);

        $pdf->br();

        $pdf->left($main_config['site'], 160);

        $pdf->font(10);
        $pdf->right($main_config['worktime'], 30);

        $pdf->out('对账单');
    }
}