Jump to content

Csv and arrays


Recommended Posts

i have files with csv format

buy is what the user bought

sell is what the user sold

 

this small script must tell me what i have in stock(storage)

#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <WindowsConstants.au3>
#include <file.au3>
#include <array.au3>

;basic GUI
Global $GUI = GUICreate("", 500, 650)
Global $hListView = GUICtrlCreateListView(" product |quantity|sold|Stock", 25, 25, 450, 600)
$buylist = _FileListToArray(@ScriptDir & "\Database\Buy")
$selllist = _FileListToArray(@ScriptDir & "\Database\Sell")
;_ArrayDisplay($selllist)
For $i = 1 To $buylist[0]
    $product = StringSplit(FileRead(@ScriptDir & "\Database\Buy\" & $buylist[$i]),";")
    $quantity = StringSplit(FileRead(@ScriptDir & "\Database\Buy\" & $buylist[$i]),";")
     $sold = 0
    for $x = 1 to $selllist[0]
        if StringSplit(FileRead(@ScriptDir & "\Database\Sell\" & $selllist[$x]),";")[1] = $product[1] Then
            $sold = StringSplit(FileRead(@ScriptDir & "\Database\Sell\" & $selllist[$x]),";")[4]
            $stock = $quantity[4] - $sold
            ;MsgBox(0,0,$stock )
        Else
            $sold = 0
        EndIf
    Next
    $stock = $quantity[4] - $sold

    GUICtrlCreateListViewItem($product[1] & "|" & $quantity[4] & "|" & $sold & "|" & $stock , $hListView)
Next

;Context Menu
Global $hCMenu = GUICtrlCreateContextMenu($hListView) ;add a context menu to the listview. I don't think you can add a seperate one to each item unless you write your own function.

GUISetState()

While 1
    Switch GUIGetMsg()
        Case -3
            Exit
    EndSwitch
WEnd

 

it is only working for the last item

coudl any one help please ?

i attached the files

 

 

 

Database.zip

Link to comment
Share on other sites

This is my try:

#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <WindowsConstants.au3>
#include <file.au3>
#include <array.au3>

;basic GUI
Global $GUI = GUICreate("", 500, 650)
Global $hListView = GUICtrlCreateListView(" product |quantity|sold|Stock", 25, 25, 450, 600)
$buylist = _FileListToArray(@ScriptDir & "\Database\Buy")
ConsoleWrite(@error&@CRLF)
$selllist = _FileListToArray(@ScriptDir & "\Database\Sell")
ConsoleWrite(@error&@CRLF)
For $i = 1 To $buylist[0]
    $product = StringSplit(FileRead(@ScriptDir & "\Database\Buy\" & $buylist[$i]),";")
    $quantity = StringSplit(FileRead(@ScriptDir & "\Database\Buy\" & $buylist[$i]),";")
    $sold = 0
    $stock = $quantity[4]
    for $x = 1 to $selllist[0]
        if StringSplit(FileRead(@ScriptDir & "\Database\Sell\" & $selllist[$x]),";")[1] = $product[1] Then
            $sold = StringSplit(FileRead(@ScriptDir & "\Database\Sell\" & $selllist[$x]),";")[4]
            $stock -= $sold
            ;MsgBox(0,0,$stock )
        EndIf
    Next
    GUICtrlCreateListViewItem($product[1] & "|" & $quantity[4] & "|" & $sold & "|" & $stock , $hListView)
Next

;Context Menu
Global $hCMenu = GUICtrlCreateContextMenu($hListView) ;add a context menu to the listview. I don't think you can add a seperate one to each item unless you write your own function.

GUISetState()

While 1
    Switch GUIGetMsg()
        Case -3
            Exit
    EndSwitch
WEnd

but it don't works:

>"C:\Program Files\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.exe" /run /prod /ErrorStdOut /in "C:\Users\Bert\AutoIt3.My\Downloads\Stock.au3" /UserParams    
+>18:41:37 Starting AutoIt3Wrapper v.14.801.2025.0 SciTE v.3.4.4.0   Keyboard:00000407  OS:WIN_81/  CPU:X64 OS:X64    Environment(Language:0407)
+>         SciTEDir => C:\Program Files\AutoIt3\SciTE   UserDir => C:\Users\Bert\AppData\Local\AutoIt v3\SciTE\AutoIt3Wrapper   SCITE_USERHOME => C:\Users\Bert\AppData\Local\AutoIt v3\SciTE 
>Running AU3Check (3.3.12.0)  from:C:\Program Files\AutoIt3  input:C:\Users\Bert\AutoIt3.My\Downloads\Stock.au3
+>18:41:37 AU3Check ended.rc:0
>Running:(3.3.12.0):C:\Program Files\AutoIt3\autoit3.exe "C:\Users\Bert\AutoIt3.My\Downloads\Stock.au3"    
--> Press Ctrl+Alt+Break to Restart or Ctrl+Break to Stop
1
1
"C:\Users\Bert\AutoIt3.My\Downloads\Stock.au3" (14) : ==> Subscript used on non-accessible variable.:
For $i = 1 To $buylist[0]
For $i = 1 To $buylist^ ERROR
->18:41:37 AutoIt3.exe ended.rc:1
+>18:41:37 AutoIt3Wrapper Finished.
>Exit code: 1    Time: 1.319

The database.zip seems to be corrupted.

Link to comment
Share on other sites

You loop through the sell folder
If the product was found in a file then a value was assigned to $sold
BUT the For loop continued testing next files... the product was not found and $sold was reset to 0
It's the reason why you always got  $sold =0, except for the last file  :)

Link to comment
Share on other sites

You loop through the sell folder
If the product was found in a file then a value was assigned to $sold
BUT the For loop continued testing next files... the product was not found and $sold was reset to 0
It's the reason why you always got  $sold =0, except for the last file  :)

Thank you !

 

here is my final code

#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <WindowsConstants.au3>
#include <file.au3>
#include <array.au3>
#include <Misc.au3>


;basic GUI
Global $GUI = GUICreate("قائمة الجرد", 500, 600)
Global $hListView = GUICtrlCreateListView("#|المادة     |الكمية|مباع|في المستودع", 25, 25, 450, 500)
$btn_refresh = GUICtrlCreateButton("تحديث",100,540,300,50)
GUICtrlSetImage(-1, "shell32.dll", 21)
GUISetState()
list()
func list()
$buylist = _FileListToArray(@ScriptDir & "\Database\Buy")
$selllist = _FileListToArray(@ScriptDir & "\Database\Sell")

For $i = 1 To $buylist[0]
    $product = StringSplit(FileRead(@ScriptDir & "\Database\Buy\" & $buylist[$i]),";")
    $quantity = StringSplit(FileRead(@ScriptDir & "\Database\Buy\" & $buylist[$i]),";")
     $sold = 0
    for $x = 1 to $selllist[0]
        if StringSplit(FileRead(@ScriptDir & "\Database\Sell\" & $selllist[$x]),";")[1] = $product[1] Then
            $sold = StringSplit(FileRead(@ScriptDir & "\Database\Sell\" & $selllist[$x]),";")[4] + $sold
            $stock = $quantity[4] - $sold
            ;MsgBox(0,0,$stock )
        EndIf
    Next
    $stock = $quantity[4] - $sold

    GUICtrlCreateListViewItem($i & "|" & $product[1] & "|" & $quantity[4] & "|" & $sold & "|" & $stock , $hListView)
Next
EndFunc

;Context Menu
Global $hCMenu = GUICtrlCreateContextMenu($hListView) ;add a context menu to the listview. I don't think you can add a seperate one to each item unless you write your own function.

GUISetState()
$z = 0
While 1
    Switch GUIGetMsg()
        Case -3
            Exit
        case $btn_refresh
            _GUICtrlListView_DeleteAllItems($hListView)
            list()
    EndSwitch
WEnd

 

the problem i'am facing is when there is a duplicate or more in the buy list

 

Untitled.png

 

i would like in that case to only only display one item and the quantity will be the duplicates sum

any ideas ?

Edited by Alexxander

Link to comment
Share on other sites

Using many files to build your database is not a very convenient way
Anyway please try this

Func list()
$buylist = _FileListToArray(@ScriptDir & "\Database\Buy")
Local $buy_array[$buylist[0]][2]
For $i = 1 To $buylist[0]
     $tmp = StringSplit(FileRead(@ScriptDir & "\Database\Buy\" & $buylist[$i]),";")
     $buy_array[$i-1][0] = $tmp[1]
     $buy_array[$i-1][1] = $tmp[4]
Next
_ArraySort($buy_array)
 ;_ArrayDisplay($buy_array, "sorted")
For $i = UBound($buy_array)-1 to 1 step -1
   If $buy_array[$i][0] = $buy_array[$i-1][0] Then
       $buy_array[$i-1][1] += $buy_array[$i][1]
       _ArrayDelete($buy_array, $i)
   EndIf
Next
; _ArrayDisplay($buy_array, "duplicates managed")

$selllist = _FileListToArray(@ScriptDir & "\Database\Sell")
For $i = 0 To UBound($buy_array)-1
     $sold = 0
    for $x = 1 to $selllist[0]
        $sold_array = StringSplit(FileRead(@ScriptDir & "\Database\Sell\" & $selllist[$x]),";")
        $soldprod = $sold_array[1]
        if $soldprod = $buy_array[$i][0] Then $sold += $sold_array[4] 
    Next
    $stock = $buy_array[$i][1] - $sold
    GUICtrlCreateListViewItem($i & "|" & $buy_array[$i][0] & "|" & $buy_array[$i][1] & "|" & $sold & "|" & $stock , $hListView)
Next
EndFunc

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...