public override void SetDefaultValues(UnicontaBaseEntity dataEntity, int selectedIndex)
        {
            var newRow = (Uniconta.DataModel.InvJournalLine)dataEntity;
            var header = this.masterRecord as Uniconta.DataModel.InvJournal;

            newRow.SetMaster(header);
            newRow._Dim1         = header._Dim1;
            newRow._Dim2         = header._Dim2;
            newRow._Dim3         = header._Dim3;
            newRow._Dim4         = header._Dim4;
            newRow._Dim5         = header._Dim5;
            newRow._MovementType = header._FixedMovement;
            var lst = (IList)this.ItemsSource;

            if (lst != null && lst.Count > 0)
            {
                var castItem = lst as IEnumerable <InvJournalLineGridClient>;
                InvJournalLineGridClient last = castItem.Last();
                newRow._MovementType = last._MovementType;
                newRow._Date         = last.Date;
            }
            else
            {
                newRow._Date = BasePage.GetSystemDefaultDate().Date;
            }
        }
        private void DataControl_CurrentItemChanged(object sender, DevExpress.Xpf.Grid.CurrentItemChangedEventArgs e)
        {
            InvJournalLineGridClient oldselectedItem = e.OldItem as InvJournalLineGridClient;

            if (oldselectedItem != null)
            {
                oldselectedItem.PropertyChanged -= Journal_changed;
            }

            InvJournalLineGridClient selectedItem = e.NewItem as InvJournalLineGridClient;

            if (selectedItem != null)
            {
                selectedItem.PropertyChanged += Journal_changed;
                if (selectedItem.Variant1Source == null)
                {
                    setVariant(selectedItem, false);
                }
                if (selectedItem.Variant2Source == null)
                {
                    setVariant(selectedItem, true);
                }
                if (selectedItem.accountSource == null)
                {
                    SetAccountSource(selectedItem);
                }
            }
        }
        async void UnfoldBOM(InvJournalLineGridClient selectedItem)
        {
            var items = this.items;
            var item  = (InvItem)items.Get(selectedItem._Item);

            if (item == null || item._ItemType != (byte)ItemType.ProductionBOM)
            {
                return;
            }

            busyIndicator.IsBusy      = true;
            busyIndicator.BusyContent = Uniconta.ClientTools.Localization.lookup("LoadingMsg");
            var list = await api.Query <InvBOM>(selectedItem);

            if (list != null && list.Length > 0)
            {
                Array.Sort(list, new InvBOMSort());
                var lst = new List <UnicontaBaseEntity>(list.Length);
                foreach (var bom in list)
                {
                    var invJournalLine = new InvJournalLineGridClient();
                    invJournalLine._Item         = bom._ItemPart;
                    invJournalLine._Date         = selectedItem._Date;
                    invJournalLine._Dim1         = selectedItem._Dim1;
                    invJournalLine._Dim2         = selectedItem._Dim2;
                    invJournalLine._Dim3         = selectedItem._Dim3;
                    invJournalLine._Dim4         = selectedItem._Dim4;
                    invJournalLine._Dim5         = selectedItem._Dim5;
                    invJournalLine._MovementType = InvMovementType.IncludedInBOM;
                    invJournalLine._Variant1     = bom._Variant1;
                    invJournalLine._Variant2     = bom._Variant2;
                    invJournalLine._Variant3     = bom._Variant3;
                    invJournalLine._Variant4     = bom._Variant4;
                    invJournalLine._Variant5     = bom._Variant5;
                    item = (InvItem)items.Get(bom._ItemPart);
                    if (item != null)
                    {
                        invJournalLine.SetItemValues(item);
                        invJournalLine._Warehouse = bom._Warehouse ?? item._Warehouse ?? selectedItem._Warehouse;
                        invJournalLine._Location  = bom._Location ?? item._Location ?? selectedItem._Location;
                        invJournalLine._CostPrice = item._CostPrice;
                        invJournalLine._Qty       = -Math.Round(bom.GetBOMQty(selectedItem._Qty), item._Decimals);
                    }
                    else
                    {
                        invJournalLine._Qty = -Math.Round(bom.GetBOMQty(selectedItem._Qty), 2);
                    }
                    lst.Add(invJournalLine);
                }
                if (selectedItem._MovementType != InvMovementType.ReportAsFinished)
                {
                    selectedItem._MovementType = InvMovementType.ReportAsFinished;
                    selectedItem.NotifyPropertyChanged(nameof(selectedItem.MovementType));
                }
                dgInvJournalLine.PasteRows(lst);
                this.DataChanged = true;
            }
            busyIndicator.IsBusy = false;
        }
        private void SerieBatch_GotFocus(object sender, RoutedEventArgs e)
        {
            InvJournalLineGridClient selectedItem = dgInvJournalLine.SelectedItem as InvJournalLineGridClient;

            if (selectedItem?._Item != null)
            {
                var selected = items.Get <InvItem>(selectedItem._Item);
                setSerieBatch(selected, selectedItem);
            }
        }
        private void SetAccountSource(InvJournalLineGridClient rec)
        {
            SQLCache cache = (rec._MovementType == InvMovementType.Creditor) ? creditors : debtors;

            if (cache != null)
            {
                rec.accountSource = null;
                rec.NotifyPropertyChanged("AccountSource");
                rec.accountSource = cache;
                rec.NotifyPropertyChanged("AccountSource");
            }
        }
        private void DCAccount_GotFocus(object sender, RoutedEventArgs e)
        {
            InvJournalLineGridClient selectedItem = dgInvJournalLine.SelectedItem as InvJournalLineGridClient;

            if (selectedItem != null)
            {
                SetAccountSource(selectedItem);
                if (prevAccount != null)
                {
                    prevAccount.isValidate = false;
                }
                var editor = (CorasauGridLookupEditorClient)sender;
                prevAccount       = editor;
                editor.isValidate = true;
            }
        }
 async void setLocationTo(InvWarehouse master, InvJournalLineGridClient rec)
 {
     if (api.CompanyEntity.Location)
     {
         if (master != null)
         {
             rec.locationToSource = master.Locations ?? await master.LoadLocations(api);
         }
         else
         {
             rec.locationToSource = null;
             rec.LocationTo       = null;
         }
         rec.NotifyPropertyChanged("LocationToSource");
     }
 }
        private void Location_GotFocus(object sender, RoutedEventArgs e)
        {
            InvJournalLineGridClient selectedItem = dgInvJournalLine.SelectedItem as InvJournalLineGridClient;

            if (selectedItem?._Warehouse != null && warehouse != null)
            {
                var selected = warehouse.Get <InvWarehouseClient>(selectedItem._Warehouse);
                setLocation(selected, selectedItem);
                if (prevLocation != null)
                {
                    prevLocation.isValidate = false;
                }
                var editor = (CorasauGridLookupEditorClient)sender;
                prevLocation      = editor;
                editor.isValidate = true;
            }
        }
        async void setSerieBatch(InvItem master, InvJournalLineGridClient rec)
        {
            if (master != null && master._UseSerialBatch)
            {
                var serie = new InvSerieBatchOpen()
                {
                    _Item = rec._Item
                };
                var lst = await api.Query <InvSerieBatchClient>(serie);

                rec.serieBatchSource = lst?.Select(x => x.Number).ToList();
            }
            else
            {
                rec.serieBatchSource = null;
                rec.SerieBatch       = null;
            }
            rec.NotifyPropertyChanged("SerieBatchSource");
        }
        async void FindOnEANVariant(InvJournalLineGridClient rec)
        {
            var ap      = new Uniconta.API.Inventory.ReportAPI(api);
            var variant = await ap.GetInvVariantDetail(rec._EAN);

            if (variant != null)
            {
                rec.Item     = variant._Item;
                rec.Variant1 = variant._Variant1;
                rec.Variant2 = variant._Variant2;
                rec.Variant3 = variant._Variant3;
                rec.Variant4 = variant._Variant4;
                rec.Variant5 = variant._Variant5;
                rec._EAN     = variant._EAN;
                if (variant._CostPrice != 0d)
                {
                    rec.CostPrice = variant._CostPrice;
                }
            }
        }
        void FindOnEAN(InvJournalLineGridClient rec)
        {
            var EAN = rec._EAN;

            if (string.IsNullOrWhiteSpace(EAN))
            {
                return;
            }
            var found = (from item in (InvItem[])items.GetNotNullArray where string.Compare(item._EAN, EAN, StringComparison.CurrentCultureIgnoreCase) == 0 select item).FirstOrDefault();

            if (found != null)
            {
                rec._EAN = found._EAN;
                rec.Item = found._Item;
            }
            else
            {
                FindOnEANVariant(rec);
            }
        }
        async void GetItemFromSerailNumber(InvJournalLineGridClient rec)
        {
            var reportApi = new Uniconta.API.Inventory.ReportAPI(api);

            busyIndicator.IsBusy = true;
            var rowId = await reportApi.GetItemFromSerialNumber(rec.SerieBatch);

            busyIndicator.IsBusy = false;
            if (rowId == 0)
            {
                return;
            }
            var item = (InvItem)items.Get((int)rowId);

            if (item != null)
            {
                rec.Item = item._Item;
                rec.NotifyPropertyChanged("Item");
            }
        }
        async void setVariant(InvJournalLineGridClient rec, bool SetVariant2)
        {
            if (items == null || variants1 == null || variants2 == null)
            {
                return;
            }

            //Check for Variant2 Exist
            if (string.IsNullOrEmpty(api?.CompanyEntity?._Variant2))
            {
                SetVariant2 = false;
            }

            var item = (InvItem)items.Get(rec._Item);

            if (item != null && item._StandardVariant != null)
            {
                rec.standardVariant = item._StandardVariant;
                var master = (InvStandardVariant)standardVariants?.Get(item._StandardVariant);
                if (master == null)
                {
                    return;
                }
                if (master._AllowAllCombinations)
                {
                    rec.Variant1Source = this.variants1?.GetKeyStrRecords.Cast <InvVariant1>();
                    rec.Variant2Source = this.variants2?.GetKeyStrRecords.Cast <InvVariant2>();
                }
                else
                {
                    var Combinations = master.Combinations ?? await master.LoadCombinations(api);

                    if (Combinations == null)
                    {
                        return;
                    }

                    List <InvVariant1> invs1 = null;
                    List <InvVariant2> invs2 = null;
                    string             vr1   = null;
                    if (SetVariant2)
                    {
                        vr1   = rec._Variant1;
                        invs2 = new List <InvVariant2>();
                    }
                    else
                    {
                        invs1 = new List <InvVariant1>();
                    }

                    string LastVariant     = null;
                    var    var2Value       = rec._Variant2;
                    bool   hasVariantValue = (var2Value == null);
                    foreach (var cmb in Combinations)
                    {
                        if (SetVariant2)
                        {
                            if (cmb._Variant1 == vr1 && cmb._Variant2 != null)
                            {
                                var v2 = (InvVariant2)variants2.Get(cmb._Variant2);
                                if (v2 != null)
                                {
                                    invs2.Add(v2);
                                    if (var2Value == v2._Variant)
                                    {
                                        hasVariantValue = true;
                                    }
                                }
                            }
                        }
                        else if (LastVariant != cmb._Variant1)
                        {
                            LastVariant = cmb._Variant1;
                            var v1 = (InvVariant1)variants1.Get(cmb._Variant1);
                            if (v1 != null)
                            {
                                invs1.Add(v1);
                            }
                        }
                    }
                    if (SetVariant2)
                    {
                        rec.variant2Source = invs2;
                        if (!hasVariantValue)
                        {
                            rec.Variant2 = null;
                        }
                    }
                    else
                    {
                        rec.variant1Source = invs1;
                    }
                }
            }
            else
            {
                rec.variant1Source = null;
                rec.variant2Source = null;
            }
            if (SetVariant2)
            {
                rec.NotifyPropertyChanged("Variant2Source");
            }
            else
            {
                rec.NotifyPropertyChanged("Variant1Source");
            }
        }