/*jshint esversion: 6 */

var should = require("should");
var helper = require("../../../helper");

var purchaseRequestDataUtil = require("../../../data-util/purchasing/purchase-request-data-util");
var validatePR = require("bateeq-models").validator.purchasing.purchaseRequest;
var PurchaseRequestManager = require("../../../../src/managers/purchasing/purchase-request-manager");
var purchaseRequestManager = null;
var purchaseOrderDataUtil = require("../../../data-util/purchasing/purchase-order-data-util");
var validatePO = require("bateeq-models").validator.purchasing.purchaseOrder;
var PurchaseOrderManager = require("../../../../src/managers/purchasing/purchase-order-manager");
var purchaseOrderManager = null;
var purchaseOrderId;
var purchaseRequest;

before('#00. connect db', function (done) {
    helper.getDb()
        .then(db => {
            purchaseRequestManager = new PurchaseRequestManager(db, {
                username: 'dev'
            });
            purchaseOrderManager = new PurchaseOrderManager(db, {
                username: 'dev'
            });

            purchaseRequestDataUtil.getNewTestData()
                .then(result => {
                    purchaseRequest = result;
                    validatePR(purchaseRequest);
                    done();
                })
                .catch(e => {
                    done(e);
                });
        })
        .catch(e => {
            done(e);
        });
});

it('#01. should failed when create new purchase-order with unposted purchase-request', function (done) {
    purchaseOrderDataUtil.getNewData(purchaseRequest)
        .then((purchaseOrder) => {
            return purchaseOrderManager.create(purchaseOrder);
        })
        .then(po => {
            done(purchaseRequest, "purchase-request cannot be used to create purchase-order due unposted status");
        })
        .catch(e => {
            e.errors.should.have.property('purchaseRequestId');
            done();
        });
});

it('#02. should success when create posted purchase-request', function (done) {
    purchaseRequestManager.post([purchaseRequest])
        .then(pr => {
            purchaseRequest = pr[0];
            done();
        })
        .catch(e => {
            done(e);
        });
});

it('#02. should success when create new purchase-order with posted purchase-request', function (done) {
    purchaseOrderDataUtil.getNewData(purchaseRequest)
        .then((purchaseOrder) => {
            return purchaseOrderManager.create(purchaseOrder);
        })
        .then((id) => {
           purchaseOrderId = id;
           done();
        })
        .catch(e => {
            done(e);
        });
});
@@ -1074,32 +1074,163 @@
    %7D);%0A%0A        
+    done();%0A        %7D)%0A        .catch(e =%3E %7B%0A            done(e);%0A        %7D);%0A%7D);%0A%0Ait(%22create purchase request%22, function (done) %7B%0A
     purchaseRequ
@@ -1258,24 +1258,16 @@
 tData()%0A
-        
         
@@ -1280,32 +1280,24 @@
 result =%3E %7B%0A
-        
             
@@ -1334,24 +1334,16 @@
         
- 
-       
 validate
@@ -1379,119 +1379,13 @@
     
-        done();%0A                %7D)%0A                .catch(e =%3E %7B%0A                    done(e);%0A                %7D
+done(
 );%0A 
@@ -2568,32 +2568,33 @@
 =%3E %7B%0A           
+ 
 purchaseOrderId 
@@ -2599,16 +2599,17 @@
 d = id;%0A
+ 
         

update test