Exemplo n.º 1
0
function processFile($filename)
{
    //extract the filename to get the exact filename on the folder
    $newFile = substr($filename, 3);
    $dirname = substr($filename, 0, 2);
    $fs = new Filesystem();
    if ($fs->exists('../Repo/' . $dirname . '/' . $newFile)) {
        $xml = simplexml_load_file('../Repo/' . $dirname . '/' . $newFile, null, LIBXML_NOCDATA);
        $newXML = json_decode(json_encode($xml), true);
        $data = formulateData($newXML, $dirname);
        //insert data to db
        $po = new PurchaseOrder();
        $po->setFilename($newFile);
        $po->setStore($dirname);
        $po->setCustomerCode($data['customer_code']);
        $po->setDeliveryDate($data['delivery_date']);
        $po->setNumber($data['number']);
        $po->save();
        $po_id = $po->getId();
        //insert all items
        foreach ($data['items'] as $key => $value) {
            $items = new Item();
            $items->setPurchaseOrderId($po_id);
            $items->setUpc($value['upc']);
            $items->setQty($value['qty']);
            $items->setQty($value['discount']);
            $items->save();
        }
        $formulated_data = formulateData($newXML, $dirname);
        $formulated_data['id'] = $po_id;
        //print_r($formulated_data);
        return $formulated_data;
    }
}
Exemplo n.º 2
0
function processFile($filename)
{
    //extract the filename to get the exact filename on the folder
    $newFile = substr($filename, 3);
    $dirname = substr($filename, 0, 2);
    $fs = new Filesystem();
    if ($fs->exists('../Repo/' . $dirname . '/' . $newFile)) {
        $xml = simplexml_load_file('../Repo/' . $dirname . '/' . $newFile, null, LIBXML_NOCDATA);
        $newXML = json_decode(json_encode($xml), true);
        $data = formulateData($newXML, $dirname);
        //checks if the record is already processed before
        $hasRecord = PurchaseOrderQuery::create()->filterByNumber($data['number'])->find();
        if ($hasRecord->count() === 0) {
            logMessage("No duplicate data: processFile " . date('m/d/Y h:i:s') . ".\n");
            //log something
            //insert data to db
            $po = new PurchaseOrder();
            $po->setFilename($newFile);
            $po->setStore($dirname);
            $po->setCustomerCode($data['customer_code']);
            $po->setDeliveryDate($data['delivery_date']);
            $po->setNumber($data['number']);
            $po->save();
            $po_id = $po->getId();
            //insert all items
            foreach ($data['items'] as $key => $value) {
                $items = new Item();
                $items->setPurchaseOrderId($po_id);
                $items->setUpc($value['upc']);
                $items->setQty($value['qty']);
                $items->setQty($value['discount']);
                $items->save();
            }
            $formulated_data = formulateData($newXML, $dirname);
            $formulated_data['id'] = $po_id;
            //print_r($formulated_data);
            logMessage("Return data in JSON: processFile " . date('m/d/Y h:i:s') . ".\n");
            //log something
            return $formulated_data;
            print_r($formulated_data);
        } else {
            logMessage("This file(" . $newFile . ") with store(" . $dirname . ") is already processed before: processFile " . date('m/d/Y h:i:s') . ".\n");
            //log something
        }
        print_r($data);
    } else {
        logMessage("Return no such file: processFile " . date('m/d/Y h:i:s') . ".\n");
        //log something
        return "No such file!";
    }
}