<?php

ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL);
error_reporting(E_WARNING | E_ERROR);
require_once 'lib.inc.php';
$GLOBAL_SESSION = returnsession();
validateMenuPriv("开票记录");
//数据表模型文件,对应Model目录下面的kaipiaorecord_newai.ini文件
//如果是需要复制此模块,则需要修改$parse_filename参数的值,然后对应到Model目录 新文件名_newai.ini文件
if ($_GET['action'] == "add_default_data") {
    global $db;
    $db->StartTrans();
    $CaiWu = new CaiWu($db);
    $CaiWu->insertKaiPiao($_POST['customerid'], $_POST['dingdanbillid'], $_POST['kaipiaoneirong'], $_POST['piaojutype'], $_POST['fapiaono'], $_POST['piaojujine'], $_SESSION['LOGIN_USER_ID']);
    //是否事务出现错误
    if ($db->HasFailedTrans()) {
        print "<script language=javascript>alert('错误:" . str_replace("'", "\\'", $db->ErrorMsg()) . "');window.history.back(-1);</script>";
    } else {
        page_css("开票记录");
        $return = FormPageAction("action", "init_default");
        print_infor("新增开票记录成功", 'trip', "location='?{$return}'", "?{$return}", 0);
    }
    $db->CompleteTrans();
    exit;
} else {
    if ($_GET['action'] == "delete_array") {
        $selectid = $_GET['selectid'];
        $selectid = explode(",", $selectid);
        //开启事务
        global $db;
            $opertype = '货款收取';
        } else {
            //付押金
            $opertype = '收押金';
        }
        //插入新回款记录
        if ($shoukuan != 0 || $oddment != 0) {
            $CaiWu->insertShoukuanReocord($customerid, $billid, $shoukuan, $accountid, $_SESSION['LOGIN_USER_ID'], $opertype, $oddment);
        }
        //发货
        if ($billinfo['fahuostate'] == 0 && $chukubillid > 0) {
            $Store->insertFaHuo($chukubillid, $customerid, $billid, $shouhuoren, $mobile, $address);
        }
        //开票
        if ($billinfo['kaipiaostate'] == 0 && $shoukuan + $oddment != 0) {
            $CaiWu->insertKaiPiao($customerid, $billid, $fapiaoneirong, $fapiaotype, $fapiaono, $shoukuan + $oddment, $_SESSION['LOGIN_USER_ID']);
        }
        $db->CompleteTrans();
        page_css("店面销售单");
        //是否事务出现错误
        if ($db->HasFailedTrans()) {
            throw new Exception($db->ErrorMsg());
        } else {
            $return = FormPageAction("action", "init_default");
            print_infor("店面销售单执行完成", 'trip', "location='?{$return}'", "?{$return}", 0);
        }
    } catch (Exception $e) {
        print "<script language=javascript>alert('错误:" . $e->getMessage() . "');window.history.back(-1);</script>";
    }
    exit;
}