<commit_before>'use strict'
var ObjectId = require("mongodb").ObjectId;
require('mongodb-toolkit');
var DLModels = require('dl-models');
var assert = require('assert');
var map = DLModels.map;
var i18n = require('dl-i18n');
var PurchaseOrderManager = require('./purchase-order-manager');
var UnitPaymentCorrectionNote = DLModels.purchasing.UnitPaymentCorrectionNote;
var UnitPaymentOrderManager = require('./unit-payment-order-manager');
var BaseManager = require('module-toolkit').BaseManager;
var generateCode = require('../../utils/code-generator');
var UnitReceiptNoteManager = require('./unit-receipt-note-manager');

module.exports = class unitPaymentQuantityCorrectionNoteManager extends BaseManager {
    constructor(db, user) {
        super(db, user);
        this.collection = this.db.use(map.purchasing.collection.UnitPaymentCorrectionNote);
        this.unitPaymentOrderManager = new UnitPaymentOrderManager(db, user);
        this.purchaseOrderManager = new PurchaseOrderManager(db, user);
        this.unitReceiptNoteManager = new UnitReceiptNoteManager(db, user);
    }

    _validate(unitPaymentQuantityCorrectionNote) {
        var errors = {};
        return new Promise((resolve, reject) => {
            var valid = unitPaymentQuantityCorrectionNote;

            var getunitPaymentQuantityCorrectionNote = this.collection.singleOrDefault({
                "$and": [{
                    _id: {
                        '$ne': new ObjectId(valid._id)
                    }
                }, {
                        "no": valid.no
                    }, {
                        _deleted: false
                    }]
            });

            var getUnitPaymentOrder = valid.unitPaymentOrder && ObjectId.isValid(valid.unitPaymentOrder._id) ? this.unitPaymentOrderManager.getSingleByIdOrDefault(valid.unitPaymentOrder._id) : Promise.resolve(null);

            var getPOInternal = [];
            valid.items = valid.items || [];
            var poId = new ObjectId();
            for (var _item of valid.items) {
                if (ObjectId.isValid(_item.purchaseOrderId)) {
                    if (!poId.equals(_item.purchaseOrderId)) {
                        poId = new ObjectId(_item.purchaseOrderId);
                        getPOInternal.push(this.purchaseOrderManager.getSingleByIdOrDefault(_item.purchaseOrderId));
                    }
                }
            }

            Promise.all([getunitPaymentQuantityCorrectionNote, getUnitPaymentOrder].concat(getPOInternal))
                .then(results => {
                    var _unitPaymentQuantityCorrectionNote = results[0];
                    var _unitPaymentOrder = results[1];
                    var _poInternals = results.slice(2, results.length);
                    var now = new Date();
                    if (_unitPaymentQuantityCorrectionNote)
                        errors["no"] = i18n.__("unitPaymentQuantityCorrectionNote.no.isExists:%s is already exists", i18n.__("unitPaymentQuantityCorrectionNote.no._:No"));

                    if (!_unitPaymentOrder)
                        errors["unitPaymentOrder"] = i18n.__("unitPaymentQuantityCorrectionNote.unitPaymentOrder.isRequired:%s is required", i18n.__("unitPaymentQuantityCorrectionNote.unitPaymentOrder._:Unit Payment Order"));
                    else if (!valid.unitPaymentOrderId)
                        errors["unitPaymentOrder"] = i18n.__("unitPaymentQuantityCorrectionNote.unitPaymentOrder.isRequired:%s is required", i18n.__("unitPaymentQuantityCorrectionNote.unitPaymentOrder._:Unit Payment Order"));
                    else if (valid.unitPaymentOrder) {
                        if (!valid.unitPaymentOrder._id)
                            errors["unitPaymentOrder"] = i18n.__("unitPaymentQuantityCorrectionNote.unitPaymentOrder.isRequired:%s is required", i18n.__("unitPaymentQuantityCorrectionNote.unitPaymentOrder._:Unit Payment Order"));
                    }
                    else if (!valid.unitPaymentOrder)
                        errors["unitPaymentOrder"] = i18n.__("unitPaymentQuantityCorrectionNote.unitPaymentOrder.isRequired:%s is required", i18n.__("unitPaymentQuantityCorrectionNote.unitPaymentOrder._:Unit Payment Order"));

                    if (!valid.date || valid.date == '')
                        errors["date"] = i18n.__("unitPaymentQuantityCorrectionNote.date.isRequired:%s is required", i18n.__("unitPaymentQuantityCorrectionNote.date._:Correction Date"));

                    if (valid.items) {
                        if (valid.items.length > 0) {
                            var itemErrors = [];
                            if (!ObjectId.isValid(valid._id)) {
                                for (var item of valid.items) {
                                    var itemError = {};
                                    if (item.pricePerUnit <= 0) {
                                        itemError["pricePerUnit"] = i18n.__("unitPaymentQuantityCorrectionNote.items.pricePerUnit.isRequired:%s is required", i18n.__("unitPaymentQuantityCorrectionNote.items.pricePerUnit._:Price Per Unit"));
                                    }
                                    if (item.priceTotal <= 0) {
                                        itemError["priceTotal"] = i18n.__("unitPaymentQuantityCorrectionNote.items.priceTotal.isRequired:%s is required", i18n.__("unitPaymentQuantityCorrectionNote.items.priceTotal._:Total Price"));
                                    }
                                    for (var _unitReceiptNote of _unitPaymentOrder.items) {
                                        for (var _unitReceiptNoteItem of _unitReceiptNote.unitReceiptNote.items) {
                                            if (_unitReceiptNoteItem.product._id.toString() === item.product._id.toString()) {
                                                if (_unitReceiptNoteItem.correction.length > 0) {
                                                    if (valid.correctionType === "Harga Satuan") {
                                                        if (item.pricePerUnit === _unitReceiptNoteItem.correction[_unitReceiptNoteItem.correction.length - 1].correctionPricePerUnit) {
                                                            itemError["pricePerUnit"] = i18n.__("unitPaymentQuantityCorrectionNote.items.pricePerUnit.noChanges:%s doesn't change", i18n.__("unitPaymentQuantityCorrectionNote.items.pricePerUnit._:Price Per Unit"));
                                                        }
                                                    }
                                                    else if (valid.correctionType === "Harga Total") {
                                                        if (item.priceTotal === _unitReceiptNoteItem.correction[_unitReceiptNoteItem.correction.length - 1].correctionPriceTotal) {
                                                            itemError["priceTotal"] = i18n.__("unitPaymentQuantityCorrectionNote.items.priceTotal.noChanges:%s doesn't change", i18n.__("unitPaymentQuantityCorrectionNote.items.priceTotal._:Total Price"));
                                                        }
                                                    }
                                                }
                                                break;
                                            }
                                        }
                                    }
                                    itemErrors.push(itemError);
                                }
                            }
                            for (var itemError of itemErrors) {
                                for (var prop in itemError) {
                                    errors.items = itemErrors;
                                    break;
                                }<commit_msg>fix validation of price corretion<commit_after>'use strict'
var ObjectId = require("mongodb").ObjectId;
require('mongodb-toolkit');
var DLModels = require('dl-models');
var assert = require('assert');
var map = DLModels.map;
var i18n = require('dl-i18n');
var PurchaseOrderManager = require('./purchase-order-manager');
var UnitPaymentCorrectionNote = DLModels.purchasing.UnitPaymentCorrectionNote;
var UnitPaymentOrderManager = require('./unit-payment-order-manager');
var BaseManager = require('module-toolkit').BaseManager;
var generateCode = require('../../utils/code-generator');
var UnitReceiptNoteManager = require('./unit-receipt-note-manager');

module.exports = class unitPaymentQuantityCorrectionNoteManager extends BaseManager {
    constructor(db, user) {
        super(db, user);
        this.collection = this.db.use(map.purchasing.collection.UnitPaymentCorrectionNote);
        this.unitPaymentOrderManager = new UnitPaymentOrderManager(db, user);
        this.purchaseOrderManager = new PurchaseOrderManager(db, user);
        this.unitReceiptNoteManager = new UnitReceiptNoteManager(db, user);
    }

    _validate(unitPaymentQuantityCorrectionNote) {
        var errors = {};
        return new Promise((resolve, reject) => {
            var valid = unitPaymentQuantityCorrectionNote;

            var getunitPaymentQuantityCorrectionNote = this.collection.singleOrDefault({
                "$and": [{
                    _id: {
                        '$ne': new ObjectId(valid._id)
                    }
                }, {
                        "no": valid.no
                    }, {
                        _deleted: false
                    }]
            });

            var getUnitPaymentOrder = valid.unitPaymentOrder && ObjectId.isValid(valid.unitPaymentOrder._id) ? this.unitPaymentOrderManager.getSingleByIdOrDefault(valid.unitPaymentOrder._id) : Promise.resolve(null);

            var getPOInternal = [];
            valid.items = valid.items || [];
            var poId = new ObjectId();
            for (var _item of valid.items) {
                if (ObjectId.isValid(_item.purchaseOrderId)) {
                    if (!poId.equals(_item.purchaseOrderId)) {
                        poId = new ObjectId(_item.purchaseOrderId);
                        getPOInternal.push(this.purchaseOrderManager.getSingleByIdOrDefault(_item.purchaseOrderId));
                    }
                }
            }

            Promise.all([getunitPaymentQuantityCorrectionNote, getUnitPaymentOrder].concat(getPOInternal))
                .then(results => {
                    var _unitPaymentQuantityCorrectionNote = results[0];
                    var _unitPaymentOrder = results[1];
                    var _poInternals = results.slice(2, results.length);
                    var now = new Date();
                    if (_unitPaymentQuantityCorrectionNote)
                        errors["no"] = i18n.__("unitPaymentQuantityCorrectionNote.no.isExists:%s is already exists", i18n.__("unitPaymentQuantityCorrectionNote.no._:No"));

                    if (!_unitPaymentOrder)
                        errors["unitPaymentOrder"] = i18n.__("unitPaymentQuantityCorrectionNote.unitPaymentOrder.isRequired:%s is required", i18n.__("unitPaymentQuantityCorrectionNote.unitPaymentOrder._:Unit Payment Order"));
                    else if (!valid.unitPaymentOrderId)
                        errors["unitPaymentOrder"] = i18n.__("unitPaymentQuantityCorrectionNote.unitPaymentOrder.isRequired:%s is required", i18n.__("unitPaymentQuantityCorrectionNote.unitPaymentOrder._:Unit Payment Order"));
                    else if (valid.unitPaymentOrder) {
                        if (!valid.unitPaymentOrder._id)
                            errors["unitPaymentOrder"] = i18n.__("unitPaymentQuantityCorrectionNote.unitPaymentOrder.isRequired:%s is required", i18n.__("unitPaymentQuantityCorrectionNote.unitPaymentOrder._:Unit Payment Order"));
                    }
                    else if (!valid.unitPaymentOrder)
                        errors["unitPaymentOrder"] = i18n.__("unitPaymentQuantityCorrectionNote.unitPaymentOrder.isRequired:%s is required", i18n.__("unitPaymentQuantityCorrectionNote.unitPaymentOrder._:Unit Payment Order"));

                    if (!valid.date || valid.date == '')
                        errors["date"] = i18n.__("unitPaymentQuantityCorrectionNote.date.isRequired:%s is required", i18n.__("unitPaymentQuantityCorrectionNote.date._:Correction Date"));

                    if (valid.items) {
                        if (valid.items.length > 0) {
                            var itemErrors = [];
                            if (!ObjectId.isValid(valid._id)) {
                                for (var item of valid.items) {
                                    var itemError = {};
                                    if (item.pricePerUnit <= 0) {
                                        itemError["pricePerUnit"] = i18n.__("unitPaymentQuantityCorrectionNote.items.pricePerUnit.isRequired:%s is required", i18n.__("unitPaymentQuantityCorrectionNote.items.pricePerUnit._:Price Per Unit"));
                                    }
                                    if (item.priceTotal <= 0) {
                                        itemError["priceTotal"] = i18n.__("unitPaymentQuantityCorrectionNote.items.priceTotal.isRequired:%s is required", i18n.__("unitPaymentQuantityCorrectionNote.items.priceTotal._:Total Price"));
                                    }
                                    for (var _unitReceiptNote of _unitPaymentOrder.items) {
                                        for (var _unitReceiptNoteItem of _unitReceiptNote.unitReceiptNote.items) {
                                            if (_unitReceiptNoteItem.product._id.toString() === item.product._id.toString()) {
                                                if (_unitReceiptNoteItem.correction.length > 0) {
                                                    if (valid.correctionType === "Harga Satuan") {
                                                        if (item.pricePerUnit === _unitReceiptNoteItem.correction[_unitReceiptNoteItem.correction.length - 1].correctionPricePerUnit) {
                                                            itemError["pricePerUnit"] = i18n.__("unitPaymentQuantityCorrectionNote.items.pricePerUnit.noChanges:%s doesn't change", i18n.__("unitPaymentQuantityCorrectionNote.items.pricePerUnit._:Price Per Unit"));
                                                        }
                                                    }
                                                    else if (valid.correctionType === "Harga Total") {
                                                        if (item.priceTotal === _unitReceiptNoteItem.correction[_unitReceiptNoteItem.correction.length - 1].correctionPriceTotal) {
                                                            itemError["priceTotal"] = i18n.__("unitPaymentQuantityCorrectionNote.items.priceTotal.noChanges:%s doesn't change", i18n.__("unitPaymentQuantityCorrectionNote.items.priceTotal._:Total Price"));
                                                        }
                                                    }
                                                }else {
                                                    if (valid.correctionType === "Harga Satuan") {
                                                        if (item.pricePerUnit === _unitReceiptNoteItem.pricePerDealUnit) {
                                                            itemError["pricePerUnit"] = i18n.__("unitPaymentQuantityCorrectionNote.items.pricePerUnit.noChanges:%s doesn't change", i18n.__("unitPaymentQuantityCorrectionNote.items.pricePerUnit._:Price Per Unit"));
                                                        }
                                                    }
                                                    else if (valid.correctionType === "Harga Total") {
                                                        if (item.priceTotal === _unitReceiptNoteItem.pricePerDealUnit * item.quantity) {
                                                            itemError["priceTotal"] = i18n.__("unitPaymentQuantityCorrectionNote.items.priceTotal.noChanges:%s doesn't change", i18n.__("unitPaymentQuantityCorrectionNote.items.priceTotal._:Total Price"));
                                                        }
                                                    }
                                                }
                                                break;
                                            }
                                        }
                                    }
                                    itemErrors.push(itemError);
                                }
                            }
                            for (var itemError of itemErrors) {
                                for (var prop in itemError) {
                                    errors.items = itemErrors;
                                    break;
                                }
{'fraction_non_alphanumeric': 0.09155717904907795, 'fraction_numerical': 0.006716549814411123, 'mean_word_length': 1.4881266490765173, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 122, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4941145', 'n_tokens_mistral': 3847, 'n_tokens_neox': 3582, 'n_words': 592}