File size: 3,454 Bytes
b1d7470
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9bdef4
 
b1d7470
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import csv
from datetime import datetime
import re
import gradio as gr
import requests

# URL of the CSV file
CSV_URL = "https://huggingface.co/spaces/mhammad/Khanfar/raw/main/docs/your_data.csv"

def read_csv_from_url(url, start_date, end_date):
    mechanics_work = {}
    cash_amount = 0
    response = requests.get(url)
    lines = response.text.split("\n")
    reader = csv.DictReader(lines)
    for row in reader:
        if row['ุชู‚ุฑูŠุฑ ู†ู‡ุงุฆูŠ'] and 'ุดูŠูƒู„' in row['ุชู‚ุฑูŠุฑ ู†ู‡ุงุฆูŠ']:
            try:
                entry_date = datetime.strptime(row['ุชุงุฑูŠุฎ ุงู„ุฏุฎูˆู„'], '%d.%m.%Y')
            except ValueError:
                print(f"Error parsing date for row: {row}")
                continue

            if start_date <= entry_date <= end_date:
                mechanic = row['ุงุณู… ุงู„ู…ูŠูƒุงู†ูŠูƒูŠ'].strip()  # Remove trailing spaces
                if mechanic in mechanics_work:
                    mechanics_work[mechanic]['job_count'] += 1
                    amount_str = row['ุชู‚ุฑูŠุฑ ู†ู‡ุงุฆูŠ']
                    amount = re.findall(r'(\d+(\.\d+)?) ุดูŠูƒู„', amount_str)
                    if amount:
                        mechanics_work[mechanic]['total_money'] += float(amount[0][0])
                else:
                    mechanics_work[mechanic] = {'job_count': 1, 'total_money': 0}
                    amount_str = row['ุชู‚ุฑูŠุฑ ู†ู‡ุงุฆูŠ']
                    amount = re.findall(r'(\d+(\.\d+)?) ุดูŠูƒู„', amount_str)
                    if amount:
                        mechanics_work[mechanic]['total_money'] = float(amount[0][0])

                # Check if ุฑู‚ู… ุงู„ู…ุฑูƒุจุฉ and ู†ูˆุน ุงู„ู…ุฑูƒุจู‡ are both "ูƒุงุด"
                if row['ุฑู‚ู… ุงู„ู…ุฑูƒุจุฉ'].strip() == 'ูƒุงุด' and row['ู†ูˆุน ุงู„ู…ุฑูƒุจู‡'].strip() == 'ูƒุงุด':
                    cash_amount += float(re.findall(r'(\d+(\.\d+)?) ุดูŠูƒู„', row['ุชู‚ุฑูŠุฑ ู†ู‡ุงุฆูŠ'])[0][0])
    total_amount = sum(info['total_money'] for info in mechanics_work.values())
    return mechanics_work, total_amount

def start_processing(start_date_str, end_date_str):
    start_date = datetime.strptime(start_date_str, '%d.%m.%Y')
    end_date = datetime.strptime(end_date_str, '%d.%m.%Y')

    work_by_date_range, total_amount = read_csv_from_url(CSV_URL, start_date, end_date)
    output_text = create_output_text(work_by_date_range, total_amount, start_date, end_date)
    
    return output_text

def create_output_text(data, total_amount, start_date, end_date):
    output = f'    ARAFAR JOB Calc. from date ({start_date.strftime("%d.%m.%Y")}) to date ({end_date.strftime("%d.%m.%Y")})\n'
    output += '#' * 70 + '\n'
    output += '-' * 50 + '\n'
    output += f'Total Amount: {total_amount} ุดูŠูƒู„ (Total Jobs: {sum(info["job_count"] for info in data.values())})\n'
    output += '-' * 50 + '\n'
    for mechanic, info in data.items():
        output += f'{mechanic}: {info["total_money"]} ุดูŠูƒู„ (Total Jobs: {info["job_count"]})\n'
        output += '-' * 50 + '\n'
    return output

output_text = gr.Textbox(label="Output")

iface = gr.Interface(
    fn=start_processing,
    inputs=[
        gr.Textbox(label="ู…ู† ุชุงุฑูŠุฎ (dd.mm.yyyy)"),
        gr.Textbox(label="ุงู„ู‰ ุชุงุฑูŠุฎ (dd.mm.yyyy)")
    ],
    outputs=output_text,
    title="ุญุงุณุจุฉ ุงู„ุนู…ู„ ูˆุงู„ู…ูˆุธููŠู†",
    description="ุงุญุณุจ ุงู„ุนู…ู„ ูˆุงู„ู…ูˆุธููŠู† ููŠ ูุชุฑุฉ ู…ุนูŠู†ุฉ"
)

iface.launch()