Exemplo n.º 1
0
        public Product()
        {
            // setup the document if the methode `SetDocument`
            // was not called outside
            storage = new Store( );

            int fcount = (int)XFile.FileType.FILE_COUNT;

            for (int i = 0; i < fcount; i++)
            {
                if (xfile.Exists)
                {
                    int index = (int)XFile.FileType.PRODUCTS;
                    xfile.SetDocument(XFile.Paths[index]);
                }
            }
        }
Exemplo n.º 2
0
        public void ComingStorage(Product prod, int quantity, ListType type)
        {
            // TODO: update the quantity
            // done.

            // TODO: substract quantity when it's out!
            if (type == ListType.OUT)
            {
                quantity *= (-1);
            }

            string[] str            = new string[] { "in", "out", "rest" };
            string   currentstorage = str[(int)type];

            #region Update product quantity
            if (type != ListType.REST)
            {
                Quantity += quantity;
            }
            else
            {
                Quantity = quantity;
            }

            //update the product
            prod.UpdateXML(prod);
            #endregion

            // TODO: update the `io-file`
            // done!

            #region check file existence
            if (!(XSource == null))
            {
                int index = (int)XFile.FileType.IO;
                xfile.SetDocument(XFile.Paths[index]);
            }
            if (!xfile.Exists)
            {
                MessageBox.Show("No Document was set");
                xfile.OpenDocument(XFile.FileType.IO);
            }
            #endregion

            /* WHAT: the div with the attribute of this date
             * you may want to create it if it does not exit!
             */
            IEnumerable <XElement> current = xfile.XML_File.Descendants(currentstorage);

            // TODO: (determin the in-div from a range of divs)
            // current;

            bool storagexists = false;
            bool elem_exists  = false;
            int  prev_q       = 0;
            foreach (XElement ni in /* while(isHxH = 1) puts("<3"); */ current)
            {
                // TODO: check for today's coming storage (in-or-out)
                // done.
                XAttribute x_date     = ni.Attribute("date");
                XElement   x_id       = ni.Element("product").Element("id");
                XElement   x_quantity = ni.Element("product").Element("quantity");

                if (!storagexists && x_date.Value == DateTime.Today.ToShortDateString( ))
                {
                    storagexists = true;
                }

                if (storagexists)  /* just update it */
                {
                    if (x_id.Value == prod.Id.ToString( ))
                    {
                        elem_exists = true;
                        #region Update the element if exists

                        #region setup quantity
                        prev_q = int.Parse(x_quantity.Value);
                        if (type != ListType.REST)
                        {
                            prev_q += quantity;
                        }
                        //else prev_q = quantity;
                        #endregion

                        // the current rest is less than the previous one
                        if (type == ListType.REST && quantity - prev_q < 0)
                        {
                            XElement outt           = null;
                            int      prev_out_quant = 0;

                            #region Get the previous OUT quantity (setup `outt`)
                            // get the previous out quantity
                            foreach (XElement xout in xfile.XML_File.Descendants("out"))
                            {
                                if (xout.Element("product").Element("id").Value == prod.Id.ToString( ) &&
                                    xout.Attribute("date").Value == DateTime.Today.ToShortDateString( ))
                                {
                                    outt           = xout; // take the last out of the current product
                                    prev_out_quant = int.Parse(xout.Element("product").Element("quantity").Value);
                                }
                            }
                            #endregion

                            if (outt != null)  /* it exists */
                            {
                                int vall = (quantity - prev_q) + prev_out_quant;
                                outt.Element("product").Element("quantity").Value = vall.ToString( );
                            }
                            else   /* create one */
                            {
                                #region Generate OUT
                                XElement Xout = new XElement("out",
                                                             new XAttribute("date", DateTime.Today.ToShortDateString( )),
                                                             new XElement("product", new XElement("id", prod.Id.ToString( )),
                                                                          new XElement("quantity", (quantity - prev_q))) //</product>
                                                             );

                                xfile.XML_File.Root.Add(Xout);
                                #endregion
                            }
                        }
                        if (type != ListType.REST) /* update the rest */
                        {
                            XElement Xrest = null;
                            IEnumerable <XElement> rest = xfile.XML_File.Descendants("rest");

                            #region Update latest rest
                            foreach (XElement xrest in rest)
                            {
                                if (xrest.Element("product").Element("id").Value == prod.Id.ToString( ))
                                {
                                    Xrest = xrest; // take the last rest of the current product
                                }
                            }
                            if (Xrest != null)
                            {
                                XElement x_rest_quantity = Xrest.Element("product").Element("quantity");
                                int      prev            = int.Parse(x_rest_quantity.Value);
                                x_rest_quantity.Value = (prev + quantity).ToString( );
                                xfile.XML_File.Save(xfile.Xmlpath);
                            }
                            #endregion
                        }

                        string val = (type == ListType.REST ? quantity : prev_q).ToString( );
                        ni.Element("product").Element("quantity").Value = val;
                        // JUST
                        break;
                        // THE WALL
                        #endregion
                    }
                }
            }

            #region Create new element
            if (!(storagexists) || !(elem_exists)) /* you have to create it */
            {
                XElement rest = null;              // XElement

                #region Get the previous quantity of the rest
                foreach (XElement xrest in xfile.XML_File.Descendants("rest"))
                {
                    if (xrest.Element("product").Element("id").Value == prod.Id.ToString( ))
                    {
                        rest   = xrest; // take the last rest of the current product
                        prev_q = int.Parse(xrest.Element("product").Element("quantity").Value);
                    }
                }
                #endregion

                /* THIS CODE IS A MESS!! CLEAN THIS SHIT BEFORE IT GETS COMPLICATED! */

                // if rest and we have less products than the previous time. then
                // we want to check if there's an out (if so update it)
                // else we we'll create an out
                if (type == ListType.REST && quantity - prev_q < 0)
                {
                    XElement outt           = null;
                    int      prev_out_quant = 0;

                    #region Get the previous OUT quantity (setup `outt`)
                    // get the previous out quantity
                    foreach (XElement xout in xfile.XML_File.Descendants("out"))
                    {
                        if (xout.Element("product").Element("id").Value == prod.Id.ToString( ) &&
                            xout.Attribute("date").Value == DateTime.Today.ToShortDateString( ))
                        {
                            outt           = xout; // take the last out of the current product
                            prev_out_quant = int.Parse(xout.Element("product").Element("quantity").Value);
                        }
                    }
                    #endregion

                    if (outt != null)  /* it exists */
                    {
                        int val = (quantity - prev_q) + prev_out_quant;
                        outt.Element("product").Element("quantity").Value = val.ToString( );
                    }
                    else   /* create one */
                    {
                        #region Generate OUT
                        XElement Xout = new XElement("out",
                                                     new XAttribute("date", DateTime.Today.ToShortDateString( )),
                                                     new XElement("product", new XElement("id", prod.Id.ToString( )),
                                                                  new XElement("quantity", (quantity - prev_q))) //</product>
                                                     );

                        xfile.XML_File.Root.Add(Xout);
                        #endregion
                    }
                }
                if (type != ListType.REST)  /* IN OR OUT */
                {
                    if (rest != null)
                    {
                        #region Update the rest
                        XElement rest_quantity = rest.Element("product").Element("quantity");
                        int      prev          = int.Parse(rest_quantity.Value);

                        rest_quantity.Value = (prev + quantity).ToString( );
                        xfile.XML_File.Save(xfile.Xmlpath);
                        #endregion
                    }
                }

                XElement XCurrent;

                #region Generate current
                XCurrent = new XElement(currentstorage,
                                        new XAttribute("date", DateTime.Today.ToShortDateString( )),
                                        new XElement("product", new XElement("id", prod.Id.ToString( )),
                                                     new XElement("quantity", quantity)) //</product>
                                        );
                #endregion

                xfile.XML_File.Root.Add(XCurrent);
            }
            #endregion

            // save changes to xfile
            xfile.XML_File.Save(xfile.Xmlpath);
        }