o
    hY7                     @   sd  d dl mZ d dlmZmZ d dlmZmZmZm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZmZ d dlmZ d dlmZ d dlmZ d dlm Z  d dl!Z!edd Z"edddd Z#edddd Z$edddd Z%edddd Z&edddd Z'edddd Z(eddd d! Z)eddd"d# Z*dS )$    render)JsonResponseHttpResponse)QSumCountF)
TruncMonth)reverse)Product)Customer)Supplier)Sale)Employee
Attendance)Order)login_required)timezone)	timedeltaNc              
   C   s  | j dd }t|dk rtdg iS g }z9tjjt|dt|dB t|dB dd	d d
 }|D ]}|	d|j
d|j d|j td|jgdd q3W n   Y zGtjt|dt|dB t|dB t|dB t|dB d d
 }|D ]!}|	d|j d|j d|j d|j td|jgdd qyW n   Y zCtjt|dt|dB t|dB t|dB t|dB d d
 }|D ]}|	d|j
d|j d|jpd td |jgdd qW n   Y zKtjt|d!t|d"B t|d#B d d$ }	|	D ]/}
|
jr|
jj d|
jj nd%}|	d&d'|
j d(| d)|
j td*|
jgdd qW n   Y zCtjt|dt|dB t|d+B t|d,B d d$ }|D ]"}|	d-|j d|j d|j d.|j td/|jgdd qXW n   Y z7tjt|d0t|d1B d d$ }|D ] }|	d2d3|j d4|jj
 d5|j  td6|jgdd qW n   Y td|iS )7Nq    results)name__icontains)barcode__icontains)description__icontainsT	is_active   r   zPrice: KSh z
 | Stock: product_detail)args)categorytitlesubtitleurl)first_name__icontains)last_name__icontains)phone__icontains)email__icontains)customer_id__icontainsr    zID: z
 | Phone: customer_detail)contact_person__icontains)supplier_id__icontainsr   z | Contact: zN/Aview_supplier)id__icontains)customer__first_name__icontains)customer__last_name__icontains   zWalk-in Customerr   zSale #z
Customer: z | Amount: KSh sale_detail)employee_id__icontains)job_title__icontainsr   z | Position: employee_detail)order_number__icontains)supplier__name__icontainsr   zOrder z
Supplier: z | Total: KSh order_detail)!GETgetstriplenr   r   objectsfilterr   appendnameselling_pricestock_quantityr   idr   
first_name	last_namecustomer_idphoner   supplier_idcontact_personr   customertotal_amountr   employee_id	job_titler   order_numbersupplier
total_cost)requestqueryr   productsproduct	customersrL   	suppliersrQ   salessalecustomer_name	employeesemployeeordersorder r`   $/var/www/html/pos/dashboard/views.pyglobal_search   s  


"

rb   z/auth/login/)	login_urlc                 C   s  t   }|tdd }|t| d }|jdd}|tdd }|tdd }tjj|dj	t
dt
dtd	d
dt
dtdd
dt
dtdd
dt
dtdd
dtdd}tjj|dj	t
dt
dtd	d
dt
dtdd
dt
dtdd
dt
dtdd
dd}tjj|dj	t
dt
dtd	d
dt
dtdd
dt
dtdd
dt
dtdd
dd}	tjj	t
ddd pd}
|
d }tjdjt
ddd}ztjjdd }W n   d}Y z0tjjtdd }tjj||d }tjjdddj	t
tdtd d d! pd}W n
   d}d}d}Y ztjjt
d"d#jdd$d%d d& }tjj|d' }W n   g }d}Y ztjjd(d) }tjj|d*}W n   d}g }Y |
|||||td+|d, psd|d, pyd|	d, pd|||||d-}t| d.|S )/N   )days   )day      )sale_date__daterM   cash)payment_method)r@   mpesacreditchequerE   )total_sales
cash_salesmpesa_salescredit_salescheque_salestransaction_count)sale_date__date__gte)rp   rq   rr   rs   rt   )totalrw   r   rl   z-totalF)is_receivedlow_stock_threshold)stock_quantity__lt)expiry_date__lteexpiry_date__gte)stock_quantity__gtbuying_price__gtrD   buying_price)total_valuer   sale__total_amount)total_spent)total_spent__gtz-total_spentr   )date_added__gteTr   )datezreports:inventory_reportrp   )rp   profit_losspending_orderslow_stock_countnear_expiry_countinventory_valueinventory_report_urldaily_salesweekly_salesmonthly_salessales_by_payment_methodtop_customerscustomer_growthemployee_countemployee_attendancezdashboard/dashboard.html)r   nowr   r   weekdayreplacer   r?   r@   	aggregater   r   r   valuesannotateorder_byr   countr   r	   r   r   r   r   r   )rS   todayexpiry_thresholdstart_of_weekstart_of_monthsix_months_agotwo_weeks_agor   r   r   rp   r   r   r   r   r   r   r   r   r   r   contextr`   r`   ra   dashboard_view   s   		r   c                 C   sf   ddddddddd	d
ddddddddddddddddddddddddddgd}t | d|S ) zMain onboarding guide pagez Getting Started with Sales SharkCreate Your First CustomerzLLearn how to add customers to track sales and manage customer relationships.onboarding_customerszfas fa-usersEasyz	3 minutes)r#   descriptionr%   icon
difficultytimeAdd Your First ProductzLSet up your inventory by adding products with pricing and stock information.onboarding_productsz
fas fa-boxz	5 minutesImport Products from ExcelzABulk import your entire product catalog using our Excel template.onboarding_importzfas fa-file-excelMediumz
10 minutesAdd Your First SupplierzEManage your supply chain by adding supplier information and contacts.onboarding_supplierszfas fa-truckz	4 minutesRegister Your First Employeez=Set up employee records for HR management and access control.onboarding_employeeszfas fa-user-tiez	6 minutes)
page_titleguideszdashboard/onboarding_home.htmlr   rS   r   r`   r`   ra   onboarding_home   sN   				-r   c                 C   $   dddt dt dd}t| d|S )zCustomer creation guider   rf      customer_addcustomer_list)r   current_steptotal_stepsadd_customer_urlcustomer_list_urlz#dashboard/onboarding_customers.htmlr   r   r   r`   r`   ra   r   -     r   c                 C   r   )zProduct creation guider   r   r   add_productproduct_list)r   r   r   add_product_urlproduct_list_urlz"dashboard/onboarding_products.htmlr   r   r`   r`   ra   r   :  r   r   c                 C   r   )zProduct import guider   r3   r   import_productsdownload_sample_csv)r   r   r   
import_urlsample_csv_urlz dashboard/onboarding_import.htmlr   r   r`   r`   ra   r   G  r   r   c                 C   $   dddt dt dd}t| d|S )zSupplier creation guider   r   add_suppliersupplier_list)r   r   r   add_supplier_urlsupplier_list_urlz#dashboard/onboarding_suppliers.htmlr   r   r`   r`   ra   r   T  r   r   c                 C   r   )zEmployee creation guider   r   employee_addemployee_list)r   r   r   add_employee_urlemployee_list_urlz#dashboard/onboarding_employees.htmlr   r   r`   r`   ra   r   a  r   r   c                 C   st   t dd}d|d< t|}|g d |g d |g d |g d |g d	 |g d
 |S )z8Generate and download sample CSV file for product importztext/csv)content_typez*attachment; filename="sample_products.csv"zContent-Disposition)barcoderB   rC   rD   r   expiry_datepackaging_typer"   )001234567890zPremium Coffee Beans 500gz1200.0025z800.00z
2025-12-31Packets	Beverages)001234567891zOrganic Rice 2kgz450.0050z320.00z
2025-06-30BagsGrains)001234567892zNatural Honey 250mlz650.0015z400.00z
2026-03-15Bottles
Sweeteners)001234567893zFresh Milk 1Lz120.0040z85.00z
2025-01-15CartonsDairy)001234567894zWhole Wheat Breadz80.0030z60.00z
2025-01-05LoavesBakery)r   csvwriterwriterow)rS   responser   r`   r`   ra   r   n  s   

r   )+django.shortcutsr   django.httpr   r   django.db.modelsr   r   r   r	   django.db.models.functionsr
   django.urlsr   inventory.modelsr   customers.modelsr   suppliers.modelsr   sales.modelsr   	hr.modelsr   r   orders.modelsr   django.contrib.auth.decoratorsr   django.utilsr   datetimer   r   rb   r   r   r   r   r   r   r   r   r`   r`   r`   ra   <module>   sB    
}
i
2




