
' ============================================
' frmReport - التقارير
' ============================================

Option Explicit

' تقرير المخزون الحالي
Private Sub cmdReportStockNow_Click()
    GenerateStockNowReport
End Sub

' تقرير الوارد والصادر
Private Sub cmdReportInOut_Click()
    GenerateInOutReport
End Sub

' تقرير الجرد الدوري
Private Sub cmdJard_Click()
    Dim countNum As String
    countNum = InputBox("أدخل رقم الجرد:", "تقرير الجرد الدوري")
    If countNum <> "" Then
        GenerateStockCountReport countNum
    End If
End Sub

' الأصناف الأكثر مبيعاً
Private Sub cmdHtmlWinerProdect_Click()
    GenerateTopSellingProducts
End Sub

' الأصناف المنخفضة
Private Sub cmdHtmlProdectLow_Click()
    GenerateLowStockReport
End Sub

' تقرير الصادر فقط
Private Sub cmdhtmlOut_Click()
    GenerateOutReport
End Sub

' تقرير الوارد فقط
Private Sub cmdhtmlIn_Click()
    GenerateInReport
End Sub

' تقرير قيمة المخزون
Private Sub cmdHTMLStock_Click()
    GenerateStockValueReport
End Sub

' تقرير الأرباح والخسائر
Private Sub cmdProfitLost_Click()
    GenerateProfitLossReport
End Sub

' تقرير الأصناف المطلوبة لإعادة الطلب
Private Sub cmdAgen_Click()
    GenerateReorderReport
End Sub

' تقرير المنتجات الراكدة
Private Sub btnRakeed_Click()
    GenerateSlowMovingProductsReport
End Sub

' ============================================
' تقارير إضافية
' ============================================

' تقرير الصادر فقط
Private Sub GenerateOutReport()
    Dim html As String
    Dim tbl As ListObject
    Dim i As Long
    Dim totalOut As Double
    
    Set tbl = GetTableByName("tlbTransaction")
    If tbl Is Nothing Then Exit Sub
    
    html = GetHTMLHeader("تقرير حركة الصادر")
    html = html & "<div class='container'>"
    html = html & "<h1>تقرير حركة الصادر</h1>"
    html = html & "<p><strong>التاريخ:</strong> " & Format(Date, "yyyy/mm/dd") & "</p>"
    html = html & "<table class='data-table'>"
    html = html & "<thead><tr>"
    html = html & "<th>رقم الحركة</th>"
    html = html & "<th>التاريخ</th>"
    html = html & "<th>اسم الصنف</th>"
    html = html & "<th>الكمية</th>"
    html = html & "<th>سعر الوحدة</th>"
    html = html & "<th>الإجمالي</th>"
    html = html & "<th>العميل</th>"
    html = html & "<th>رقم الفاتورة</th>"
    html = html & "</tr></thead><tbody>"
    
    totalOut = 0
    For i = 1 To tbl.ListRows.count
        If UCase(tbl.ListRows(i).Range(3).Value) = "صادر" Then
            html = html & "<tr>"
            html = html & "<td>" & tbl.ListRows(i).Range(1).Value & "</td>"
            html = html & "<td>" & Format(tbl.ListRows(i).Range(2).Value, "yyyy/mm/dd") & "</td>"
            html = html & "<td>" & tbl.ListRows(i).Range(5).Value & "</td>"
            html = html & "<td>" & Format(tbl.ListRows(i).Range(6).Value, "#,##0.00") & "</td>"
            html = html & "<td>" & Format(tbl.ListRows(i).Range(7).Value, "#,##0.00") & "</td>"
            html = html & "<td>" & Format(tbl.ListRows(i).Range(8).Value, "#,##0.00") & "</td>"
            html = html & "<td>" & tbl.ListRows(i).Range(10).Value & "</td>"
            html = html & "<td>" & tbl.ListRows(i).Range(11).Value & "</td>"
            html = html & "</tr>"
            
            totalOut = totalOut + CDbl(tbl.ListRows(i).Range(8).Value)
        End If
    Next i
    
    html = html & "</tbody></table>"
    html = html & "<div class='summary'><h2>إجمالي الصادر: " & Format(totalOut, "#,##0.00") & " ريال</h2></div>"
    html = html & "</div>" & GetHTMLFooter
    
    SaveAndOpenHTML html, "OutReport"
End Sub

Private Function GetHTMLHeader(title As String) As String
    GetHTMLHeader = Module_HTMLReports.GetHTMLHeader(title)
End Function

Private Function GetHTMLFooter() As String
    GetHTMLFooter = Module_HTMLReports.GetHTMLFooter
End Function

Private Sub SaveAndOpenHTML(htmlContent As String, fileName As String)
    Module_HTMLReports.SaveAndOpenHTML htmlContent, fileName
End Sub

' تقرير الوارد فقط
Private Sub GenerateInReport()
    Dim html As String
    Dim tbl As ListObject
    Dim i As Long
    Dim totalIn As Double
    
    Set tbl = GetTableByName("tlbTransaction")
    If tbl Is Nothing Then Exit Sub
    
    html = GetHTMLHeader("تقرير حركة الوارد")
    html = html & "<div class='container'>"
    html = html & "<h1>تقرير حركة الوارد</h1>"
    html = html & "<p><strong>التاريخ:</strong> " & Format(Date, "yyyy/mm/dd") & "</p>"
    html = html & "<table class='data-table'>"
    html = html & "<thead><tr>"
    html = html & "<th>رقم الحركة</th>"
    html = html & "<th>التاريخ</th>"
    html = html & "<th>اسم الصنف</th>"
    html = html & "<th>الكمية</th>"
    html = html & "<th>سعر الوحدة</th>"
    html = html & "<th>الإجمالي</th>"
    html = html & "<th>المورد</th>"
    html = html & "<th>رقم الفاتورة</th>"
    html = html & "</tr></thead><tbody>"
    
    totalIn = 0
    For i = 1 To tbl.ListRows.count
        If UCase(tbl.ListRows(i).Range(3).Value) = "وارد" Then
            html = html & "<tr>"
            html = html & "<td>" & tbl.ListRows(i).Range(1).Value & "</td>"
            html = html & "<td>" & Format(tbl.ListRows(i).Range(2).Value, "yyyy/mm/dd") & "</td>"
            html = html & "<td>" & tbl.ListRows(i).Range(5).Value & "</td>"
            html = html & "<td>" & Format(tbl.ListRows(i).Range(6).Value, "#,##0.00") & "</td>"
            html = html & "<td>" & Format(tbl.ListRows(i).Range(7).Value, "#,##0.00") & "</td>"
            html = html & "<td>" & Format(tbl.ListRows(i).Range(8).Value, "#,##0.00") & "</td>"
            html = html & "<td>" & tbl.ListRows(i).Range(10).Value & "</td>"
            html = html & "<td>" & tbl.ListRows(i).Range(11).Value & "</td>"
            html = html & "</tr>"
            
            totalIn = totalIn + CDbl(tbl.ListRows(i).Range(8).Value)
        End If
    Next i
    
    html = html & "</tbody></table>"
    html = html & "<div class='summary'><h2>إجمالي الوارد: " & Format(totalIn, "#,##0.00") & " ريال</h2></div>"
    html = html & "</div>" & GetHTMLFooter
    
    SaveAndOpenHTML html, "InReport"
End Sub

' تقرير قيمة المخزون
Private Sub GenerateStockValueReport()
    Dim html As String
    Dim tbl As ListObject
    Dim i As Long
    Dim totalValue As Double
    
    Set tbl = GetTableByName("tlbInventory")
    If tbl Is Nothing Then Exit Sub
    
    html = GetHTMLHeader("تقرير قيمة المخزون")
    html = html & "<div class='container'>"
    html = html & "<h1>تقرير قيمة المخزون حسب المنتجات</h1>"
    html = html & "<p><strong>التاريخ:</strong> " & Format(Date, "yyyy/mm/dd") & "</p>"
    html = html & "<table class='data-table'>"
    html = html & "<thead><tr>"
    html = html & "<th>رقم الصنف</th>"
    html = html & "<th>اسم الصنف</th>"
    html = html & "<th>المخزون الحالي</th>"
    html = html & "<th>قيمة المخزون</th>"
    html = html & "<th>نسبة من الإجمالي %</th>"
    html = html & "</tr></thead><tbody>"
    
    ' حساب الإجمالي أولاً
    totalValue = 0
    For i = 1 To tbl.ListRows.count
        totalValue = totalValue + CDbl(tbl.ListRows(i).Range(8).Value)
    Next i
    
    ' عرض البيانات
    For i = 1 To tbl.ListRows.count
        Dim stockValue As Double
        Dim percentage As Double
        stockValue = CDbl(tbl.ListRows(i).Range(8).Value)
        percentage = IIf(totalValue > 0, (stockValue / totalValue) * 100, 0)
        
        html = html & "<tr>"
        html = html & "<td>" & tbl.ListRows(i).Range(1).Value & "</td>"
        html = html & "<td>" & tbl.ListRows(i).Range(2).Value & "</td>"
        html = html & "<td>" & Format(tbl.ListRows(i).Range(7).Value, "#,##0.00") & "</td>"
        html = html & "<td>" & Format(stockValue, "#,##0.00") & "</td>"
        html = html & "<td>" & Format(percentage, "#,##0.00") & "%</td>"
        html = html & "</tr>"
    Next i
    
    html = html & "</tbody></table>"
    html = html & "<div class='summary'><h2>إجمالي قيمة المخزون: " & Format(totalValue, "#,##0.00") & " ريال</h2></div>"
    html = html & "</div>" & GetHTMLFooter
    
    SaveAndOpenHTML html, "StockValueReport"
End Sub


