<?php
/**
 * Created by PhpStorm.
 * User: boyer
 * Date: 2019/5/4
 * Time: 9:37
 */
// echo strtolower('CONSUMETAX');
// die();
require_once ('../config.php');
/*require_once("PHPExcel.php");
require_once("PHPExcel/Reader/Excel2007.php");
require_once("PHPExcel/Reader/Excel5.php");*/

require_once 'PHPExcel.php';
require_once "PHPExcel/IOFactory.php";

$file = "G:/baiducloud/百度云同步盘/工作电脑2016/F/工作文档/进口系统/2018年/沙田/处理/2022年/10月/27/需要修改的产品代码.xls";

$tile = array(
    '客户代码' => 'customer_code',
    'SKU' => 'product_sku',
    '新的产品代码' => 'new_product_barcode',
);
$data = Common_Upload::readXlsFromTitle($file, $tile, true);

if(!empty($data)){
    foreach ($data as $k=>$mv){
        $res = updateproductbarcode($mv);
        if($res['state']==1){
            echo $mv['product_sku']."修改成功\r\n";
        }else{
            echo $mv['product_sku']."修改失败".$res['message']."\r\n";
        }
    }
}


echo "<pre>";
print_r($data);

function updateproductbarcode($row){
    $sku = trim($row['product_sku']);
    $customer_code = trim($row['customer_code']);
    $newproduct_code = trim($row['new_product_barcode']);
    $return = array(
        'state'=>0,
        'message'=>'',
    );
    try{
        $db = Common_Common::getAdapter();
        $db->beginTransaction();
        if($sku==''){
            throw new Exception("sku不能为空");
        }
        if($customer_code==''){
            throw new Exception("客户代码不能为空");
        }
        if($newproduct_code==''){
            throw new Exception("修改的产品代码不能为空");
        }
        $newProductCondition = array(
            'customer_code'=>$customer_code,
            'product_barcode'=>$newproduct_code,
        );
        $barcodeRows = Service_Product::getByCondition($newProductCondition,"*");
        if(!empty($barcodeRows)){
            throw new Exception("修改的产品条码已经存在商品-->".$newproduct_code);
        }
        $skuCondition = array(
            'customer_code'=>$customer_code,
            'product_sku'=>$sku,
        );
        $skuRows = Service_Product::getByCondition($skuCondition,"*");
        //var_dump($skuRows);
        if(empty($skuRows)){
            throw new Exception("sku:{$sku}不存在");
        }
        if(count($skuRows)>1){
            throw new Exception("sku:{$sku}超过1个");
        }
        $product = $skuRows[0];
        $update = array(
            'product_barcode'=>$newproduct_code,
            'product_update_time'=>date("Y-m-d H:i:s",time()),
        );
        //
        $log=array(
            'product_id' => $product['product_id'],
            'pl_type' => 0,
            'pl_ip' => '127.0.0.1',
            'user_id' => 1,
            'pl_statu_pre' => $product['product_status'],
            'pl_statu_now' => $product['product_status'],
            'pl_note' => "修改条码错误：{$product['product_barcode']}->{$newproduct_code}",
            'pl_add_time' => date("Y-m-d H:i:s"),
        );
        Service_Product::update($update, $product['product_id']);
        Service_ProductLog::add($log);

        //修改库存和批次库存的数据
        $ibCondition = array(
            'product_id'=>$product['product_id'],
        );
        $invtbatchs = Service_InventoryBatch::getByCondition($ibCondition,"*");
        //var_dump($invtbatchs);
        if(!empty($invtbatchs)){
            foreach ($invtbatchs as $k=>$invt){
                $ibUpdate = array(
                    'product_barcode'=>$newproduct_code,
                    'ib_note'=>$invt['ib_note']."修改批次信息产品条码{$invt['product_barcode']}-->{$newproduct_code}",
                );
                Service_InventoryBatch::update($ibUpdate,$invt['ib_id']);
            }
        }
        //修改库存数据
        $inventoryCondition = array(
            'product_id'=>$product['product_id'],
        );
        $inventorys = Service_InventoryBatch::getByCondition($inventoryCondition,"*");
        //var_dump($inventorys);
        if(!empty($inventorys)){
            foreach ($inventorys as $k=>$invent){
                $inventUpdate = array(
                    'product_barcode'=>$newproduct_code,
                );
                Service_ProductInventory::update($inventUpdate,$invent['pi_id']);
            }
        }

        //$db->rollBack();
        $db->commit();
        $return['state'] = 1;
    }catch (Exception $e){
        $return['message'] = $e->getMessage();
        $db->rollBack();
    }
    return $return;
}



exit;

// if($product['product_barcode']!= $sku){
//     exit('不是条码1');
// }
/*$update= array(
    'product_barcode2'=>$newHsCode,
    // 'product_barcode'=>$newHsCode,
    // 'product_barcode'=>$product['product_barcode2']
);

// $productHsElement = Service_HsElementMap::getDetailByProductId($product['product_id']);
// echo $productHsElement;
// die();
$log=array(
    'product_id' => $product['product_id'],
    'pl_type' => 0,
    'pl_ip' => '127.0.0.1',
    'user_id' => 60,
    'pl_statu_pre' => $product['product_status'],
    'pl_statu_now' => $product['product_status'],
     // 'pl_note' => "修改条码错误：{$product['product_barcode']}->{$newHsCode}",
     'pl_note'=>'添加条码2:'.$newHsCode.'',
    'pl_add_time' => date("Y-m-d H:i:s"),
);
Service_Product::update($update, $product['product_id']);
Service_ProductLog::add($log);
echo "添加成功";
die();
echo $product['product_sku'];
$db = Common_Common::getAdapter();
try {
    $db->beginTransaction();
    Service_Product::update($update, $product['product_id']);
    Service_ProductLog::add($log);
    $productHsElement = Service_HsElementMap::getByCondition(array('product_id' => $product['product_id']));
    foreach ($productHsElement as $he) {
        $old = Service_HsElement::getByField($he['he_id']);
        $new = Service_HsElement::getByCondition(array('hs_code' => $newHsCode, 'he_name' => $old['he_name']));
        if (empty($new)) {
          throw new Exception($old['he_name']);
        }
        var_dump($new[0]['he_id']);
        Service_HsElementMap::update(array('he_id' => $new[0]['he_id']), $he['hem_id']);
    }
    $db->commit();
}catch (Exception $e){
    echo $e;
    $db->rollBack();
}
die('dddddd');*/
// echo "<pre/>";
// var_dump($updateReceiving);
// die();
// foreach($update as $key=>$sql){
//     $updateUom = array(
//         'country_code_of_origin'=>$sql['country_code_of_origin']
//     );
//     Service_product::update($updateUom,$sql['product_id']);
//     Service_ProductLog::add($productLog[$key]);
// }