
    nj                       d dl mZ d dlZd dlmZmZ d dlmZmZ	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mZmZ d d
lmZ d dlmZ d dlmZ d dlZd dlZd dlZd dl Z d dl!Z!d dl"m#Z# d dl$m$Z$m%Z%m&Z& ddl'm(Z( d dl)Z)d dl*Z*d dl+m,Z, d dlm-Z- d dl.m/Z/ d dl0m1Z1m2Z2 d dl3m4Z4 d dl5m&Z& d dl6m7Z7m8Z8m9Z9 d dl:m;Z; d dl<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZC ddlDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZRmSZSmTZTmUZUmVZVmWZWmXZXmYZYmZZZm[Z[m\Z\m]Z]m^Z^m_Z_m`Z`maZambZbmcZcmdZdmeZemfZfmgZg d dlhmiZimjZjmkZkmlZl d dlmmnZnmoZompZpmqZqmrZrmsZsmtZtmuZumvZvmwZwmxZxmyZymzZzm{Z{m|Z| d dl} d dl~mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ d dlmZ d d lmZ d dlZd dlZd d!l$mZ d d"lmZ d d#l$m$Z$ d d$lmZ d d%lmZ d d&lmZmZmZmZ d d'lmZ d d(lmZ d dl)Z)d d)lmZ d dlZd dl.m/Z/ d dlmZ d d$lmZ d d*lmZ d d+lmZ d d,lmZ d dlZd d-lmZ d d.lmZ d dlZd dlm,Z, d d/l}mZ d d0lmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ d d1lmZ d d2lmZmZmZ d3 Zd4 Zd5 Zdd6Zd7 Zd8 Zd9 Zd: Zd; Zd< Zd= Zd> Zd? Zd@ Ze e eg dA            dB               Ze e edCdDg            dE               ZdF ZddGZe e edCdDg            dH               Zee e edCdDg            dI                      ZdJ Zee e edCdDg            dK                      ZdL ZdM Ze e edCdDg            ddN              Ze e edCdDg            dO               Ze e edCdDg            dP               Ze e edCdDg            dQ               Zee e edCdDg            dR                      Ze e edCdDg            dS               ZedT        Ze e edCdDg            dU               Ze e edCdDg            dV               Ze e edCdDg            dW               Ze e edCdDg            ddX              Ze e edCdDg            dY               Ze e edCdDg            dZ               Ze e edCdDg            d[               Ze e edCdDg            d\               Ze e edCdDg            d]               Ze e edCdDg            d^               Ze e edCdDg            d_               Zd` Ze e edCdDg            da               Zdb Ze e edCdDg            dc               Zedd        Zede        Zdf Z d dl3m4Z4 d dglmZ d dlZe e edCdDg            dh               Zd dil0m1Z1 dj Zdk Zdl Ze e edCdDg            dm               Ze e edCdDg            ddn              Ze e eg do            ddp              Ze e eg do            dq               Ze e edCdDg            dr               Zds Z	e e edtg            du               Z
 e edCdDg            dv        Zdw Ze e edCdDg            dx               Z e edCdDg            dy        Zee e edCdDg            dz                      Ze e edCdDg            d{               Ze e edCdDg            d|               Z e edCdDg            d}        Zee e edCdDg            d~                      Ze e edCdDg            d               Ze e edCdDg            d               Z e edCdDg            d        Zee e edCdDg            d                      Ze e edCdDg            d               Ze e edCdDg            d               Z G d de      Z G d de      Z e edtdg            d        Zee e edtg            d                      Ze e edtg            d               Ze e edtg            d               Zd dl5m&Z& d dl m!Z! e e edtdg            dd              Z"e e edg            d               Z# G d dejH                        Z%d Z&d Z'd Z(d dl*Z*d Z)d Z*e#d        Z+e#d        Z,d Z-e e edCdDg            d               Z.e e edCdDg            d               Z/e e edCdDg            d               Z0e e edCdDg            d               Z1 e eg d            d        Z2ee e edCdDg            d                      Z3e e edCdDg            d               Z4e e edCdDg            d               Z5e e edCdDg            d               Z6e e edCdDg            d               Z7e e edCdDg            d               Z8e e edCdDg            d               Z9e e edCdDg            d               Z:e e edCdDg            d               Z;e e edCdDg            d               Z<e e edCdDg            d               Z=e e edCdDg            d               Z>e e edCdDg            d               Z?e e edCdDg            d               Z@e e edCdDg            d               ZAe e edCdDg            d               ZBed        ZCed        ZDed        ZEe e edCdDg            d               ZFee e edCdDg            d                      ZGe e edCdDg            dd              ZHe e edCdDg            d               ZIe e edCdDg            d               ZJe e edCdDg            d               ZKe e edCdDg            d               ZLe e edCdDg            d               ZMe e edCdDg            d               ZNe e edCdDg            d               ZOe e edCdDg            d               ZPe e edtdg            dd              ZQd d#l$m$Z$ d dl5m&Z& d ZRd ZSddZTe e edtg            dÄ               ZUe e edCdDg            dĄ               ZVdń ZWdƄ ZXdǄ ZYd dlZZZdȄ Z[dɄ Z\dʄ Z] edg      d̄        Z^ edg      d̈́        Z_ e edCdDg            d΄        Z`ee e edCdDg            dτ                      Zae e edCdDg            dЄ               Zbe e edCdDg            dф               Zcd҄ Zddӄ Zed dl3m4Z4 dԄ ZfdՄ Zgdք Zhe e edCdDg            dׄ               Zie e edCdDg            d؄               Zje e edCdDg            dل               Zke e edCdDg            dڄ               Zle e edCdDg            dۄ               Zme e edCdDg            d܄               Zndd݄Zodބ Zpd߄ Zqd Zry)    )DecimalN)renderredirect)authenticateloginlogout)user_passes_testlogin_required)api_view)require_POST)inlineformset_factory)SumAvgDateTimeField)validate_email)ValidationError)F)csrf_exempt)datetimedatetimezone   )shop_add_status_change)chain)get_object_or_404)settings)JsonResponseHttpResponse)messages)r   )	Paginator	EmptyPagePageNotAnInteger)apps)FAQAboutUsAppUrl	ContactUsPrivacyPolicyReturnPolicyTermsAndConditions)#AboutUsForm
AppUrlFormContactUsFormFAQFormPrivacyPolicyFormRefundPolicyFormShopFormShopSlobIdFormStoreUserFormTermsAndConditionsFormUserEditFormUserFormUserModalFormBankDetailsFormCategoryFormProductSubCategoryFormProductFormTagFormDynamicFilterFormSpecialListFormDeliveryBoyFormSKUFormCourierDetailsForm
CouponFormDeliverySlotFormDiscountFormAdFormCustomProductFormProductionUnitFormCustomOrderFormMessageFormYouMayAlsoLikeFormDeliverySettingsFormDeliveryDiscountFormCustomCouponSettingsForm)ShopBankDetailsProductionUnit
ShopSlobId)ProductCategoryProductReviewProductSubCategoryProductsSalesUnitProductSelectionProductImageProductVideoTagsDynamicFilteringSpecialListSKUCustomProductCustomProductImageCustomProductVideoYouMayAlsoLike)*)AdditionalDetails
BillEditedCustomOrderTracking	DelhiveryDeliveryBoysDeliveryOTPOrderProductCustomizationOrderProductsOrdersOrderDeliveryCouponsDeliverySlotDiscountAdsPaymentCommunicationMessageDeliveryDiscountDeliverySettingsCustomCouponSettingsCustomCouponPaymentModes)	send_mail)EMAIL_HOST_USER)	timedelta)initialize_fcm_app)r   )reverse)Group)CountQr   Prefetch)render_to_string)autocomplete)IsAuthenticated)APIView)Responsestatus)
parse_date)EmailMessage)LoggingOperation)send_delivery_assign_email"send_whatsapp_message_delivery_boyemail_sendingsend_notificationsend_whatsapp_message&send_whatsapp_message_delivery_partnersmsshop_admin_status_update&shop_admin_status_update_only_one_shop'category_based_shop_admin_status_celery+sub_category_based_shop_admin_status_celerycategory_product_list.product_availability_shop_status_change_celery;master_product_edit_celery_task_to_update_shop_availability,update_sku_long_distance_status_check_celery!long_distance_status_check_celerysku_delete_long_distance_celery!long_distance_master_product_edit)group)create_delhivery_shipmentcalculate_cart_weightcheck_pincodec                    	 t        j                  d| d|  d       t        d| d|  d       t        j                  | |t
        j                  |dgd      }|S # t        $ r&}t        j                  d|  d| d	       Y d }~y d }~ww xY w)
NzSending Email to (z) with subject ()F   args	countdownzEmail Send Failed for ():  )logginginfoprintr   apply_asyncr   ry   	Exception)subjectmessage
email_listfail_silently
email_dataes         OC:\Users\Saasvaap\Desktop\Akash\Saasvaap\DEMO\Navya-Bakers\adminportal\views.py	EmailSendr   W   s    A)*5EgYaPQ":,.>wiqIJ"..WgxG_G_akmr4s9:<
 A.wis1#Q?@@As   AA 	B%BBc                     	 t        j                  | |j                  |||j                  |j                  gd       t        ddd      S # t        $ r%}t        j                  d| d|        Y d }~y d }~ww xY w)Nr   r   r   znotification send successfullyr   msgzNotification send Failed on (r   )	r   r   order_IDpkuuidr   r   r   error)	fcm_tokenordertitledescription	user_uuidr   s         r   NotificationInitr   m   s|    %%Iu~~uk[d[g[ginisis+t  AB  	Cq2RSTT 5k]#aSIJs   AA 	A9A44A9c                     	 t        j                  d       t        j                  | |||gd       y # t        $ r"}t        j
                  d|        Y d }~y d }~ww xY w)Nz#Sending Whatsapp Message to Shipperr   r   )Send Whatsapp message to Shipper Failed: )r   r   r   r   r   r   )numberorderIDnametemplate_namer   s        r   WhatsappMessager   {   sU    :;))}/U`ab A!EFs   03 	AAAc                     t         j                  t         j                  z   dj                  fdt	        |       D              S )N c              3   H   K   | ]  }t        j                          y wN)secretschoice).0_
characterss     r   	<genexpr>z+generate_random_password.<locals>.<genexpr>   s     E}!7>>*-}s   ")stringascii_lettersdigitsjoinrange)lengthr   s    @r   generate_random_passwordr      s0    %%5J77EuV}EEE    c                    i }| j                   dk(  rc| j                  j                  dd      }| j                  j                  dd      }t        | ||      }|t	        | |       t        d      S ddi}t        | d	|      S )
NPOSTusernamer   password)r   r   homepager   zinvalid username or passwordz
login.html)methodr   getr   
auth_loginr   r   )requestr   emailr   users        r   r   r      s    E~~  R0<<##J3 GehGw%J'' >?E'<//r   c                       fd}|S )Nc                      | j                   v S r   	user_type)r   allowed_roless    r   inner_functionz'check_user_role.<locals>.inner_function   s    ~~..r    )r   r   s   ` r   check_user_roler      s    / r   c                 6    | rt        || z  dz  d      }|S d}|S )Nd      r   round)orders_todayfailed_order_todayavg_failed_orderss      r   get_average_failed_ordersr      s3    !#5#D"KQO  r   c                 4   i }| d   rX|d   rSt        | d   |d   z  dz  d      }|dkD  r|dz
  }t        |d      |d<   d|d<   |S d|z
  }t        |d      |d<   d|d<   |S | d   r|d   sd}t        |d      |d<   d|d<   |S d	}t        |d      |d<   d|d<   |S )
Nweek_revenuelast_week_revenuer   r   revenue_differenceincreaseflagdecreaser   r   )r   week_before_last_week_revenuerevenue_difference_outputweek_revenue_percent_with_weeks       r   get_week_difference_outputr     s"    "N#(EFY(Z)..),IJ],^^beegh*j&)C/-Kc-Q*>CDbde>f%&:;0:%f- %$ .13Q-Q*>CDbde>f%&:;0:%f- %$ '0MNa0b-0*>CDbde>f%&:;0:%f-
 %$ ./*>CDbde>f%&:;0:%f-$$r   c                 4   i }| d   rX|d   rSt        | d   |d   z  dz  d      }|dkD  r|dz
  }t        |d      |d<   d|d<   |S d|z
  }t        |d      |d<   d|d<   |S | d   r|d   sd}t        |d      |d<   d|d<   |S d	}t        |d      |d<   d|d<   |S )
Ntoday_revenuelast_week_avg_revenuer   r   today_revenue_percent_with_weekr   r   r   r   r   )r  last_week_revenue_avgrevenue_percent_datar  s       r   get_revenue_percent_datar	     s(   _%*?@W*X*/1ORg#S% 2%(+1,-.+0'*S0.MPS.S+FKLkmnFo !BC+5 (   /24S.S+FKLkmnFo !BC+5 (   )2GH_2`.1+FKLkmnFo !BC+5 (
   /0+FKLkmnFo !BC+5 (r   c                    i }| rO|rMt        | |z  d      dz  }|dkD  r|dz
  }t        |d      |d<   d|d<   |S d|z
  }t        |d      |d<   d|d<   |S | r|sd}t        |d      |d<   d|d<   |S d}t        |d      |d<   d|d<   |S )Nr   r   today_count_percent_with_weekr   r   r   r   r   )r   last_week_revenue_countrevenue_count_datar  s       r   get_revenue_count_datar     s    /(-|>U/UXY(Z]`(`%(3.,IC,O)BGHeghBi>?)3v&  -02O,O)BGHeghBi>?)3v&   7,/)BGHeghBi>?)3v&
  -.)BGHeghBi>?)3v&r   c                    t         j                  j                  | j                  | j                  |j
                        j                  g d      j                  t        d            }t        |d   r|d   ndd      |d<   |S )	N)created_date__yearcreated_date__month
store_uuid	New OrderFailed	Cancelledorder_status__ingrand_totalmonth_revenuer  r   r   )
rj   objectsfilteryearmonthr   exclude	aggregater   r   )todayshop_idr  s      r   get_month_revenuer$    s    NN))UZZ>CkkV]VbVb * ddkdk> el e@@I	-( AJ A*  &+*7*Ho&aQR&TM/"r   c                 
   d }t         j                  j                  d      j                  | j                  j
                        }|r|d   }d| j                  d<   t        j                  j                  g d      j                  ||j
                        j                         }t        t        j                  j                  |d	|j
                  
      j                  d            }t        |      }t        j                  j                  |d|j
                  
      j                         }t        ||      }t        j                  j                  |d|j
                  
      j                         }	t        j                  j                  |g d|j
                        j                         }
t        j                  j                  |j
                        j                  dg      j                         }t        j                  j                  ||j
                        j                  g d      j                  t        d            }d}|d   rt!        |d   r|d   ndd      }|t#        d      z
  }|t#        d      z
  }t        j                  j                  |||j
                        j                  g d      j                  t        d            }t        j                  j                  |||j
                        j                  g d      j                  t        d            }t%        ||      }t        j                  j                  |j
                  ||      j                  g d      j                  t'        d            }t)        ||      }t        j                  j                  |j
                  ||      j                  g d      j                         }t+        ||      }t-        ||      }d}|	r|rt!        |	|z  dz  d      }t        j                  j                  |j
                  |      j                  g d      j                  t'        d            }d}|d   rt!        |d   d      }t        j                  j                  |j
                  |      j                  g d      j                  d       j/                  t        d      !      j1                  d"      d d# }|D ]  }|d$   s	t!        |d$   d      |d$<    i d%|d&|d'|	d(|
d)|d*|d+|d|d,|d-|d.|d/|d0|d1|d2|d3d4d5|}|S y )6Nunit_admin_userunit_admin_user__uuidr   Yesproduct_viewr  r  r  r  )created_date__dater  	Confirmed)r,  order_statusr  r   r  	Deliveredr-  ViewedOrder Packed)r,  r  r  )r  r  r  r  r  r  r      days)created_date__date__gtecreated_date__date__lter  r   r   )r  r7  r8  r  r   )r  r,  store_uuid__unit_nametotal_amount-total_amountr   r>  r   new_order_todayfulfiled_ordercurrently_unassigned_orderfailed_orderavg_failed_ordertotal_ordersr  grand_total_by_storer  r  delivery_success_percentageavg_today_revenuer   home_activeactivenew_order_data)rN   r  prefetch_relatedr  r   r   sessionrj   r   countlistvalueslenr   r!  r   r   rz   r  r   r	  r  r$  annotateorder_by)r   r"  shop_id_verifyr#  r   rK  r@  r   r   rA  rB  rE  r  today_revenue_datalast_week_startweek_before_last_weekr   r   r   r  r  r  r  r  rG  rH  avg_today_revenue_dataresultsicontexts                                 r   shop_admin_homepager\    s   N \\223DELL%ll// M 1N #*/' ~~--?d-ell$ m ??Duw 	 fnn33u[f?F|| 4 MMSVT^Macn- $^^22eZb>Ell 3 LLQEG 	 6lDVW
  ..%Va:A,, / HHM 	 &,^^%:%:eLsFMll &; &TTYTYT[ 	#
 ~~,,,EMM)] N ,,1EG 	--SZS_S_-`hhB i DDMIm, EN E. 	 )!&WfIg}_'Emnpq!r)"33 /)2C C~~,,_EJW^WcWc - eelelB fm fDDMI]+ EN E- 	
 )/(=(=VkVeIP )> )WW^W^B X_ XDDMI!-0 EN E2 	& %?|Mj$k! !' 5 5N]NS !6 !UU\U\B V] VDDMIdghudvIDw 	  8G\]"(.."7"77<<P_PU #8 #WW^W^B X_ XDDIEG 	  4LBYZ)%9&'#l*/,1NRU0UWX*Y'"NN11W\\^c1dllB m DDMIm, EN E. 	 "#_-%*+<_+Mq%Q"..''7<<TY'ZbbB c DDJFKbDcdldl]+ em e--5X.a!
 A $)!N*;Q$?.! 5>< 51BO 5#^5/1K5 "#55 &'8	5
 "<5
 2ABT5 #M5 4J75 34F5 56J5 12M5 '(>5 /0I5 LYZb5 $^5 I r   c                    d| j                   d<   t        j                  j                  g d      j	                  |      j                         }t        t        j                  j	                  |d      j                  d            }t        |      }t        j                  j	                  |d	      j                         }t        ||      }t        j                  j	                  |d
      j                         }t        j                  j	                  g d|      j                         }|t        d      z
  }	|	t        d      z
  }
t        j                  j	                  |	|      j                  g d      j                  t        d            }t        j                  j	                  |
|	      j                  g d      j                  t        d            }i }|d   rV|d   rQt        |d   |d   z  dz  d      }|dkD  r|dz
  }t        |d      |d<   d|d<   nQd|z
  }t        |d      |d<   d|d<   n7|d   r|d   sd}t        |d      |d<   d|d<   nd}t        |d      |d<   d|d<   t        j                  j	                  |	|      j                  g d      j                  t        d            }t        j                  j	                  |      j                  g d      j                  t        d            }d}|d   rt        |d   d      }i }|d   rV|d    rQt        |d   |d    z  dz  d      }|dkD  r|dz
  }t        |d      |d!<   d|d<   nQd|z
  }t        |d      |d!<   d|d<   n7|d   r|d    sd}t        |d      |d!<   d|d<   nd}t        |d      |d!<   d|d<   t        j                  j	                  |	|      j                  g d      j                         }i }|rM|rKt        ||z  dz  d      }|dkD  r|dz
  }t        |d      |d"<   d|d<   nKd|z
  }t        |d      |d"<   d|d<   n1|r|sd}t        |d      |d"<   d|d<   nd}t        |d      |d"<   d|d<   d}|r|rt        ||z  dz  d      }t        j                  j	                  |      j                  g d      j                  t        d            }d}|d   rt        |d   d      }t        j                  j                  d#g      j                         }t        j                  j	                  |j                  |j                   d
$      j                  t        d      %      }t        |d&   r|d&   ndd      |d&<   t        j                  j	                  |d'(      j                  g d      j                  d)      j#                  t        d      *      j%                  d+      d d, }|D ]  }|d-   s	t        |d-   d      |d-<    i d.|d/|d0|d1|d2|d3|d4|d|d&|d5|d"|d!|d6|d7|d8|d9d:d;|}|S )<NNor*  r+  r  r,  r-  )r,  r.  r   r  r/  r0  )r  r,  r4  r5  )r7  r8  r  r  r9  r:  r   r   r   r   r   r   r   r   r   r;  r3  r  r  r  r  r  )r  r  r.  r  r  Local Ordersr,  
order_typer<  r=  r?  r   r>  r   r@  rA  rB  rC  r   rE  rF  rG  rH  r   rI  rJ  rK  rM  rj   r  r   r  rN  rO  rP  rQ  r   rz   r!  r   r   r   r  r  rR  rS  )r   r"  r   rK  r@  r   r   rA  rB  rV  rW  r   r   r   r  r  r  rU  r  r  r  r  r  rG  rH  rX  rE  r  rY  rZ  r[  s                                  r   nbc_admin_homepagerd    s7    +/'~~--?d-ell$ m &&+eg 	fnn33uZe3gnnoy|}n-#^^22eZb2ciik5lDVW..%Va.bhhj%+^^%:%:LsNS &; &UUZUZU\ 	#)"33 /)2C C~~,,_EJ - LLSGB MT MDDMI]+ EN E- 	 )/(=(=VkVe )> )ggngnB ho hDDMI!-0 EN E2 	& %'!',IJ],^-2n-0MNa0bbfiikl.n*-31ORU1U.BGHfhiBj)*>?4>)&1147U1U.BGHfhiBj)*>?4>)&1 N+4QRe4f14.BGHfhiBj)*>?4>)&112.BGHfhiBj)*>?4>)&1 & 5 5oNS !6 !UU\U\B V] VDDMI"%m"4 EN E6 	 ---GOOB P DDMI\_`m\nIDo 	)!&}_'Eq!I!).CD[.\.3/2GH_2``dggij/l+.42QTW2W/JOPoqrJs$%FG/9$V,258W2W/JOPoqrJs$%FG/9$V, _-6KLc6d25/JOPoqrJs$%FG/9$V,23/JOPoqrJs$%FG/9$V,"(.."7"7P_PU #8 #WW^W^B X_ XDDIEG 	   3,1<BY3Y]`2`bc,d),s20MPS0S-FKLiklFm"#BC-7"6*036S0S-FKLiklFm"#BC-7"6*$;03-FKLiklFm"#BC-7"6*01-FKLiklFm"#BC-7"6*&'#l*/,1NRU0UWX*Y'"NN11U1KSSB T DDMI\_`m\nIDo 	 "#_-%*+<_+Mq%Q"~~--}-MSSU--BG++\g . iirirm, js j. 	 */Q^_nQo}_/Muv/0*2o&..''5^'\ddB e DDJFKbDcdldl]+ em e--5X.a! A $)!N*;Q$?.! N>< N1BO NUegu N/1KN "#5N '(9	N
 "<N
 2ABTN #MN 4J7N 34FN 56JN 12MN '(>N A\]vN !(N -=nN r   c                    d| j                   d<   t        j                  j                  g d      j	                  d|      j                         }t        t        j                  j	                  |dd      j                  d	            }t        |      }t        j                  j	                  |d
d      j                         }t        j                  j	                  |dd      j                         }t        ||      }t        j                  j	                  g d|d      j                         }|t        d      z
  }	|	t        d      z
  }
t        j                  j	                  d|	|      j                  g d      j                  t        d            }t        j                  j	                  d|
|	      j                  g d      j                  t        d            }i }|d   rV|d   rQt        |d   |d   z  dz  d      }|dkD  r|dz
  }t        |d      |d<   d|d<   nQd|z
  }t        |d      |d<   d|d<   n7|d   r|d   sd}t        |d      |d<   d|d<   nd}t        |d      |d<   d|d<   t        j                  j	                  d|	|      j                  g d      j                  t        d            }t        j                  j	                  d|      j                  g d      j                  t        d            }d}|d    rt        |d    d      }i }|d    rV|d!   rQt        |d    |d!   z  dz  d      }|dkD  r|dz  }t        |d      |d"<   d|d<   nQd|z  }t        |d      |d"<   d|d<   n7|d    r|d!   sd}t        |d      |d"<   d|d<   nd}t        |d      |d"<   d|d<   t        j                  j	                  d|	|d
#      j                         }i }|rM|rKt        ||z  dz  d      }|dkD  r|dz
  }t        |d      |d$<   d|d<   nKd|z
  }t        |d      |d$<   d|d<   n1|r|sd}t        |d      |d$<   d|d<   nd}t        |d      |d$<   d|d<   d}|r|rt        ||z  dz  d      }t        j                  j	                  d|      j                  g d      j                  t        d            }d}|d    rt        |d    d      }t        j                  j	                  d%      j                  d&g      j                         }t        j                  j	                  d|j                  |j                   '      j                  g d      j                  t        d      (      }t        |d)   r|d)   ndd      |d)<   t        j                  j	                  |d*      j                  g d      j                  d+      j#                  t        d      ,      j%                  d-      d d. }|D ]  }|d/   s	t        |d/   d      |d/<    i d0|d1|d2|d3|d4|d5|d6|d |d)|d7|d$|d"|d8|d9|d:|d;d<d=|}|S )>Nr^  r*  r+  r  Long Distance Orders)rb  r,  r-  )r,  r.  rb  r   r/  r  r0  )r  r,  rb  r4  r5  )rb  r7  r8  r  r  r9  r:  r   r   r   r   r   r   r   r   r   r;  r3  r  r  r  )rb  r7  r8  r.  r  rb  r  )rb  r  r  r  r  ra  pu_uuid__pu_namer=  r?  r   r>  r   r@  rA  rB  rC  r   rE  grand_total_by_purG  rH  r   rI  rJ  rK  rc  )r   r"  r   rK  r@  rA  r   r   rB  rV  rW  r   r   r   r  r  r  rU  r  r  r  r  r  rG  rH  rX  rE  r  rY  rZ  r[  s                                  r   pu_admin_home_pagerj    s   &*GOON#>>));`)ahh)  i ""'%'  &..//5Wb;Q 0 SSYSYZdSgiN.)O^^**eR]6L + NNSeg ..%V^:P / RRWRWRY  2,@RS!'!6!6HoJOBX "7 "ZZ_Z_Za  iQ//O+iQ.??>>((4JdsAF ) HHO> IP I@@I	' AJ A)  %+NN$9$9E[RgRa %: %ccjcj> dk d@@I	m, AJ A. "
 !#N#(EFY(Z)..),IJ],^^beegh*j&)C/-Kc-Q*>CDbde>f%&:;0:%f--03Q-Q*>CDbde>f%&:;0:%f- '0MNa0b-0*>CDbde>f%&:;0:%f--.*>CDbde>f%&:;0:%f-"NN11=SJYJO 2 QQXQX> RY R@@I	!-0 AJ A2 
 NN))5K`e)fnn> o @@I	X[\iXj	@k _%"=#A1E_%*?@W*X*/1ORg#S% 2%(+1,-.+0'*S0.MPS.S+FKLkmnFo !BC+5 (.14S.S+FKLkmnFo !BC+5 ( )2GH_2`.1+FKLkmnFo !BC+5 (./+FKLkmnFo !BC+5 ($nn33?UL[LQ`k 4 mmrmrmt  /(-|>U/UY\.\^_(`%(3.,IC,O)BGHeghBi>?)3v&,/2O,O)BGHeghBi>?)3v& 7,/)BGHeghBi>?)3v&,-)BGHeghBi>?)3v&"#,&+^l-Jc,QST&U#--9Odi-jrr> s @@I	X[\iXj	@k )!&'8'I1!M>>((4J(KSS% T ((-  NN))5K`e`j`j>Ckk * KKR7> LS L@@I	-( AJ A*  &+][jMk=+Iqr+,&.M/"nn##uI_#`hh> i @@FGY@Z[c[c' \d \))1*!G ^ %a&7 ;An 
J~| 
J-> 
JQacq 
J+-G
J1
J #$5
J |	
J .=>P	
J
 
J
 0CG
J /0B
J 12F
J -.I
J #$:
J =XYr
J h
J )9.
JG Nr   )Super Admin	NBC Admin
Shop AdminPU Adminc                 x   i }t        j                         j                         }| j                  j                  dk(  rt        | |      }nd| j                  j                  dk(  s| j                  j                  dk(  rt        | |      }n%| j                  j                  dk(  rt        | |      }t        | d|      S )Nrm  rl  rk  rn  z
index.html)	r   nowr   r   r   r\  rd  rj  r   )r   r[  r"  s      r   	home_pagerq    s     GLLN!E ||-%gu5 
		;	.',,2H2HM2Y$We4 
		:	-$We4'<11r   rk  rl  c                 l   t         j                  j                  d      }t        |d      }| j                  j                  d      }	 |j                  |      }|dd}t        | d|      S # t        t        f$ r |j                  d      }Y 4t        $ r |j                  |j                        }Y Yw xY w)N-created_at
   pager   rJ  )	shop_datasales_unit_activezshop_list.html)rN   r  rS  r    GETr   get_pager"   	TypeErrorr!   	num_pagesr   )r   shops	paginatorru  r[  s        r   	shop_listr~    s     LL!!-0E%$I;;??6"D8""4(
 "AG'+W55 i( &""1% 8""9#6#678   A+ + B3#B32B3c           	         g }t         j                  j                         D ]  }t        j                  j                  |      j	                         }|r|j
                  }n|j                  }|j                  dk(  r|j                  t        | |||             z|j                  dk(  r|j                  t        | |||             |j                  dk(  s|j                  t        | |||              |S )NskuVisible)
sales_unitr  r   shop_admin_statusDisabledzOut of Stock)r\   r  r  rV   firstr   
sku_statusappend)shopproduct_enter_listrZ  sales_statusr   s        r   shop_status_initializationr    s    [[!088??A?FLLN!((F\\F<<9$%%)Tqcijl\\Z'%%)Tqcijl\\^+%%)Tqcijl "  r   c                     t        | j                  j                               }g d}g d}t        |      t        |      z  }t        |      t        |      z  }d}|rd}|S |rd}|S )z
    Determines which page/step has errors in the multi-step form
    Returns: page index (0 for personal details, 1 for location details)
    )
	unit_name	unit_codeunit_locationr   
contact_nor   r&  delivery_modedelivery_radiusgst)latitude	longitudestreetcitystate_or_provincedistrictpin_coder   r   )rO  errorskeysset)	shop_form	bank_formshop_error_columnsstep1_columnsstep2_columnsstep1step2ru  s           r   shop_add_error_page_findr    s~    
 i..3356]M-M "#c-&88E"#c-&88E D K 
Kr   c                 :   g }t               }| j                  dk(  rt        | j                        }t	        d| j                         t	        d|j                                t	        d|j                         |j                         r|j                         }t        |      }t        j                  j                  |       d}d|j                   d}d}|j                  j                         D ]A  }	|g}
|	j                  s|
j!                  |	j                         t#        ||t$        |
d	
       C 	 | j&                  }d}d}d|j                   d|j(                   }t*        j                  j-                  ||||       t1        d      S |j!                  |j                         t3        |d       }|||dd|d}t5        | d|      S t               }g }|||dddd}t5        | d|      S # t.        $ r"}t	        d|        Y d }~t1        d      S d }~ww xY w)Nr   
POST Data:zForm is valid:zForm errors:z"New Shop Assignment - Navya BakerszgDear User,

            You have been assigned as a Unit Admin for a new shop.

            Shop Name: z

            Please log in to the system to manage the shop.

            If you were not expecting this assignment, please contact support.

            Best regards,  
            Navya Bakers Team
            e-commerce@navyabakers.comTr   r  zShop CreatedShop z created with code r   log_typer   r   zLogging error: r~  
modal_hiderJ  )r  r  user_modal_form
show_modalrw  ru  shop_add.htmlr   )r7   r   r1   r   r   is_validr  saver  rV   r  bulk_creater  r&  allr   r  rx   ry   r   r  r   creater   r   r  r   )r   r  r  r  r  r  r   r   admin_emailr   recipient_listlog_userr  r   r   ru  r[  s                    r   shop_addr    s.    F#oO~~W\\*	 	lGLL)	 2 2 45ni../ >>#D "<D!A%--99:LM ;G ' (	G 7K,,002"-::"))$**5w.`df	 3"<<!( %dnn%55HHXY ((//!%# +	 0  K(( MM)**+ ,It<D !&#2*%-G '?G<< J	 *"%G '?G44W  s+,K((	s   6AG/ /	H8HHc                    t         j                  j                  |      }|rt        j                  j                  |      j	                          |j                         }|j	                          	 	 t        j                  d|j                         | j                  }d}d}d|j                   d	|j                   d
}t        j                  j!                  ||||       t#        d      S t#        d      S # t        $ r"}t        j                  d|        Y d }~d }~ww xY w# t        $ r,}t        j                  d|        Y d }~t#        d      S d }~ww xY w)Nr   )r  zShop Delete)senderr#  z.Signal Status Updation on Shop Delete Failed: r  zShop Deletedr    with code  deletedr  zShop Deletion Error: r~  )rN   r  r  rV   deleter  r   sendr   r   r   r   r   r  r  r   r  r   )	r   r#  r  rv  r   r  r  r   r   s	            r   shop_deleter  n  s?    <<'*D 	"))00G0DKKMJJL		T&++=)..Y ||HH$G!)"5"5!6l9CVCVBWW_`K$$++HV]kv+w
 K  8K    T NqcRSST  	MM1!56K  		s=   3!C: AD( :	D%D D(  D%%D( (	E1EEc                 Z    	 t         j                  j                  | |||       y #  Y y xY w)Nr  )r   r  r  )r  r  r   r   s       r   logr    s0      ''XRYgr'ss   #& *c                    t        t        |      }t        j                  j	                  |      }|r!t        j                  | d       t        d      S |rl|j                          | j                  }d}d}d|j                   d|j                   d}t        ||||       t        j                  | d	       t        d      S t        j                  | d
       t        d      S )Nr   categoryz$Couldn't delete subcategories existsproductzCategory Deletedz	Cagegory r  r  zCategory deleted successfullySomething went wronglist_category)r   rR   rT   r  r  r   warningr  r   category_namecategory_coder  successr   )r   category_idr  sub_categoryr  r  r   r   s           r   category_deleter    s     !{CH%--44h4GL"HI O$$ 
 <<$!("8"8!9hF\F\E]]efHh5"AB O$$ 	"89O$$r   c                    t         j                  j                  |      }|j                  j                  }|ry|j                          	 | j                  }d}d}d|j                   d|j                   d|j                  j                   d|j                  j                   }t        ||||       t        dt        |       d	      S #  Y xY w)
Nr  r  zSub category deletedSub category r   deleted for category  with code /adminportal/add-sub-category//)rT   r  r   r  r   r  r   sub_category_namesub_category_coder  r  r  r   str)r   sub_category_idr  r  r  r  r   r   s           r   sub_category_deleter    s   %--111GL'',,K	||H H,G),*H*H)IVbVtVtUu  vL  MY  Mb  Mb  Mp  Mp  Lq  q|  }I  }R  }R  }`  }`  |a  bK(G[9 4S5E4FaHII	s   
A(C
 
Cc                     | |j                   k7  r]|j                  j                         D ]?  }d}d|j                   d|j                    }t	        ||t
        |j                  gd       A y y )NzShop status updatedz
Your shop z changed their status to Tr  )r   r&  r  r  rx   ry   r   )existing_shop_statusr  r   r   r   s        r   send_email_on_status_changer    sf    t{{*((,,.D+G"4>>"22KDKK=YG/DJJ<t / +r   c                    t        t        |      }|j                  }| j                  dk(  rt	        | j
                  |      }t        d| j
                         |j                         rc|j                         }t        ||       	 | j                  }d}d}d|j                   d|j                   d	}t        ||||       t#        d      S t%        |      }
|d|
d}t'        | d|      S t	        |      }|ddd}t'        | d|      S # t        $ r,}	t        j                   d
|	        Y d }	~	t#        d      S d }	~	ww xY w)Nr  r   instancer  r  zShop Updatedr  r   updatedz&Shop Updation Error(logging section): r~  rJ  )r  rw  ru  zshop_edit.htmlr   )r   rN   r   r   r1   r   r   r  r  r  r   r  r  r  r   r   r   r   r  r   )r   r#  r  r  r  r  r  r   r   r   ru  r[  s               r   	shop_editr    sP    Tg.D;;~~W\\D9	lGLL)>>#D ((<dCL"<<!( %dnn%5[@PPXYHh= K(( ,I6D '%-G
 '#3W==d+	 %G
 '+W55-  L FqcJKKK((Ls   ;D
 
	D?D::D?c                    t         j                  j                  ddg      j                  d      }t	        |d      }| j
                  j                  d      }	 |j                  |      }|dd	}t        | d
|      S # t        t        f$ r |j                  d      }Y 4t        $ r |j                  |j                        }Y Yw xY w)NCustomerrk  )user_type__in-created_datert  ru  r   rJ  )usersusers_activez
users.html)Usersr  r   rS  r    rx  r   ry  r"   rz  r!   r{  r   )r   r  r}  ru  r[  s        r   r  r    s     MM!!!=1 " h 
 %$I;;??6"D8""4( x8G'<11 i( &""1% 8""9#6#678s   A= = C#CCc                 B   g }t        t        |      }| j                  dk(  r^t        | j                  |      }|j                         r|j                          t        d      S |j                  |j                         nt        |      }||d|d}t        | d|      S )Nr  r   r  r  rJ  r  formpublic_users_activeuser_idedit_users.html)r   r  r   r5   r   r  r  r   r  r  r   )r   r  r  r   r  r[  s         r   	user_editr    s     FUw/D~~GLL48==?IIKG$$ MM$++&T*h[bcG',g66r   c           	         g }| j                   dk(  rt        | j                        }|j                         r|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }t        ||||||      }	t               }
|	j                  |
       d	}d
| d| d| d|
 d	}t        |	j                        }	 t        ||t        |gd       |	j                          t        d      S |j                  |j                          nt               }g }||d}t        | d|      S #  ddgig}||d}t        | d|      cY S xY w)Nr   
first_name	last_namer   r   phone_numberr   )r  r  r   r   r  r      Welcome to Navya Bakers 🎉
            Dear r   z,

            Welcome to Navya Bakers!

            We are pleased to inform you that your account has been successfully created. Please find your login details below:

            Email: 
            Password: z

            Note: For security reasons, please do not share your password with anyone.

            Best regards,  
            Navya Bakers Team
            Fr  r  zPlease enter valid email)r  	user_formzadd_user.html)r   r6   r   r  cleaned_datar  r   set_passwordr  r   rx   ry   r  r   r   r  r  )r   r  r  r  r  r   r   r  r   user_objmy_passwordr   r   	recepientr[  s                  r   user_addr  +  s    F~~W\\*	"//=J!..{;I**73E!..{;I$11.AL++H5F%##)H 34K!!+.4GQyk * 7 "m $G HNN+IAWo	{RW (( MM)**+J	 G '?G44+A"%?$@AB %!* g@@s   %/E E+c                 |    t         j                  j                  |      }|r|j                          t	        d      S )Nr  r  )r  r  r  r  r   )r   r  r   s      r   user_deleter  w  s2     ==7+DGr   c           	      4   g }t               }| j                  dk(  rt        | j                        }|j	                         r|j                         }t               }||_        |j                  }|j                  }|j                  }d}	d| d| d| d| d	}
t        |j                        }t        |	|
t        |gd	       t               }d
||d}t        | d|      S |j                  |j                          nt               }g }|||dd}t        | d|      S )Nr   r  r  r   z,

            Welcome to Navya Bakers!

            Your account has been successfully created. Below are your login details:

            Email: r  a  

            For security reasons, we strongly recommend that you change your password after your first login.

            If you did not request this account, please contact our support team immediately.

            Best regards,  
            Navya Bakers Team
            Tr  r  )r  r  r  r  
modal_show)r  r  r  r  )r1   r   r7   r   r  r  r   r   r  r  r   r  rx   ry   r   r  r  )r   r  r  r  r  r   r  r  r   r   r   r  r[  s                r   user_add_modalr    s?    F
I~~'5##%&++-H 34K +H!,,J **INNE4GQyk * 7 "m $G" HNN+I/I;d
 ,oO*&#2G
 '?G<<MM/001'/ *"	G '?G44r   c                 .    t        |        t        d      S )Nz/adminportal/login/)r   r   r   s    r   admin_logoutr
    s    
7O)**r   c                    g }t        t        |      }t               }|j                  |       |j	                          d}d|j
                   d|j                   d| d}t        |j                        }d}t        ||t        ||gd	       t        j                  | t        j                  d
       t        |      }	||	d|d}
t        | d|
      S )Nr  z+Your Password Has Been Reset - Navya BakersDear r   zc,

    Your password has been successfully reset.

    Your new login Password are:

    Password: z

    Note: Please do not share your password with anyone.

    If you did not request this change, please contact our support team immediately.

    Best regards,  
    Navya Bakers Team
    r  Fr  zGPassword Reset Successfully. You will receive a mail with the password.r  rJ  r  r  )r   r  r   r  r  r  r  r  r   rx   ry   r   add_messageSUCCESSr5   r   )r   r  r  r   r   r   r   	recipientr  r  r[  s              r   reset_passwordr    s     FUw/D +,Kk"IIK <G($..)9 : m G  DJJI.Kgw)[1IY^_ ("2"2bd &Dh[bcG',g66r   c                 l   t         j                  j                  d      }t        |d      }| j                  j                  d      }	 |j                  |      }|dd}t        | d|      S # t        t        f$ r |j                  d      }Y 4t        $ r |j                  |j                        }Y Yw xY w)Nr  rt  ru  r   rJ  )category_listcategory_activezcategory_list.html)rR   r  rS  r    rx  r   ry  r"   rz  r!   r{  r   )r   category_datar}  ru  r  r[  s         r   r  r    s     $++44_EM-,I;;??6"D;%%d+
 !)XFG'/99 i( )%%a( ;%%i&9&9:;r  c                    g }t        t        |      }t        j                  j	                  |      }t               }| j                  dk(  rTt        | j                  | j                  |      }|j                         r| j                  j                  dd      rd |j                  _        | j                  j                  dd      rd |j                  _        | j                  j                  dd      rd |j                  _        |j!                         }	 | j"                  }d	}d
}	d|j$                   d|j&                   d}
t)        |||	|
       t+        d      S |j-                  |j.                         |d|j0                  |dt3        |      d}t5        | d|      S t        |      }|d|j0                  |d}t5        | d|      S #  Y t+        d      S xY w)Nr  r  r   r  image1r   image2image3r  zCategory updated	Category r  r  r  T)category_formsubcategory_statusr  sub_category_formsubcategory_addr  category_add.htmlr  r  r  r  )r   rR   rT   r  r  r:   r   r9   r   FILESr  r   r  iconstandard_imagebanner_imager  r   r  r  r  r   r  r  r   rO  r   )r   r  r  r  sub_category_listr  r  r  r  r   r   r[  s               r   category_editr%    s    F [AH*229989L.0~~GLL'--(K==?||"-%)"||"-/3,||"--1*yy{H	"<<$, )(*@*@)AhNdNdMeemnHh= O,, MM$++&(,DYaYfYf,=RV'+,='>@G '#6@@X. $DQYQ^Q^$57G'.88O,,s   ;G Gc                 d   g }| j                   dk(  rt        | j                  | j                        }t	               }|j                         r|j                         }	 | j                  }d}d}d|j                   d|j                   d}	t        ||||	       t        t        |j                        }
t        |
      }|d	|j                  |d
}t        | d|      S |j                  |j                          ||dd}t        | d|      S t               }g }||ddd}t        | d|      S #  Y xY w)Nr   r  zCategory createdr  r  z createdr  r  Tr  r  r   )r  r  r  F)r  r  r  r  )r   r9   r   r   r:   r  r  r   r  r  r  r   rR   r   r   r  r  )r   r  r  r  r  r  r  r  r   r   category_instancer[  s               r   add_categoryr(  <  sJ    F~~$W\\7==A24!!#$))+H"<<$, )(*@*@)AhNdNdMeemnHh= !2/hmm T(2CDM(5Tbjbobo,=?G'#6@@MM-../ !.!G '#6@@$&#	G '.883s   ;D+ +D/c           	         g }t        t        |      }| j                  dk(  rRt        | j                        }t
        j                  j                  |      }t        |      }|j                         r|j                  d      }||_        |j                          	 | j                  }d}	d}
d	|j                   d
|j                   d|j                  j                   d|j                  j                    }t#        ||	|
|       t               }|d||dt%        |      d}t'        | d|      S |j)                  |j*                         |r||d||dt%        |      d}n||d||d}t'        | d|      S t        |      }t               }t
        j                  j                  |      }g }|d||dt%        |      d}t'        | d|      S #  Y xY w)Nr  r   r  r  Fcommitr  Sub category createdr  r   created for category r  T)r  r  r  r  r  r  r  )r  r  r  r  r  r  r  )r  r  r  r  r  )r   rR   r   r:   r   rT   r  r  r9   r  r  r  r   r  r  r  r  r  rO  r   r  r  )r   r  r  r  r  r$  r  r  r  r  r   r   r[  s                r   add_sub_categoryr.  i  sB    F {CH~~27<<@ /66==x=P$h7%%',111?L$,L!"<<$0 -l.L.L-M\ZfZxZxYy  zP  Q]  Qf  Qf  Qt  Qt  Pu  u@  AM  AV  AV  Ad  Ad  @e  fHh= !7 8,=UY(5kfj'+,='>@G '#6@@ MM+223 $):*._j'+TBS=T	 %):*._j '#6@@ %h724.66==x=P(9QUhu"-$X\]nXoq '.88Ms   A(G Gc                    t        t        |      }|j                  j                  }g }| j                  dk(  rt        | j                  | j                  |      }|j                         r|j                          	 | j                  }d}d}d|j                   d|j                   d|j                  j                   d	|j                  j                   }	t        ||||	       t!        d
t#        |       d      S |j%                  |j&                         nt        |      }||d||d}
t)        | d|
      S #  Y YxY w)Nr  r   r  r  zSub category updatedr  r   updated for category r  r  r  rJ  )r  r  r  r  r  zedit_sub_category.html)r   rT   r  r   r   r:   r   r   r  r  r   r  r  r  r  r  r   r  r  r  r   )r   r  r  r  r  r  r  r  r   r   r[  s              r   edit_sub_category_rowr1    so    %%7oNL'',,KF~~%gllGMMLY==?IIK"<<$0 -l.L.L-M\ZfZxZxYy  zP  Q]  Qf  Qf  Qt  Qt  Pu  u@  AM  AV  AV  Ad  Ad  @e  fHh= <S=M<NaPQQ MM$++&%|<dS["1+OG'3W==s   <A(D9 9D=c                    g }d}d}d }| j                   dk(  r| j                  j                  d      }|r4t        t        |      }t        | j                  | j                  |      }n t        | j                  | j                        }|j                         r|j                         }| j                  j                  d      }|D ]#  }t        j                  j                  ||       % | j                  j                  d	      }	|	D ]#  }
t        j                  j                  ||

       % d}d}	 | j                  }d}|sdnd}d|j                   d|j                    d}t#        ||||       t%        d||d|j&                  id      S t%        d|j(                  d      S t               }||d|||d}t+        | d|      S #  Y XxY w)NFTr   
product_idr  r  imagesr  imagevideosr  videor  zMaster Product AddedMaster Product UpdatedProduct r  z addedidr  
sku_activedetails_activer  r  r  rJ  )r  product_formr  r>  r?  r  zproduct_add.html)r   r   r   r   rU   r;   r   r  r  getlistrW   r  r  rX   r   	item_name	item_coder  r   r<  r  r   )r   r  r>  r?  r  r3  rA  r4  r6  r7  r9  r  r  r   r   r[  s                   r   master_product_addrE    s    FJNG~~\\%%l3
'Z@G&w||W]]WUL&w||W]]CL  ""'')G ]]**84F$$++G5+I   ]]**84F$$++G5+I   J"N"<<$8B0H` ():):(;;wGXGXFYY_`Hh=  ("0 '**-	!    E\=P=P QRR"} $# (G '-w77/s   5?G Gc           
         t        t        |      }| j                  dk(  rOt        | j                        }|j                         r|j                  d      }|j                  dkD  r|j                  dk(  rd|_	        ||_
        |j                         }t        j                  j                         D ]  }t        j                  j                  |||j                  |j                  d	      \  }}|j                  dk(  r|j                  j                   j#                  |       |j                   j#                  |       |j                  j$                  j                   j#                  |       |j                  j&                  j                   j#                  |       |j                  rt        j                  j)                  |j                  j*                  
      j-                  d       t.        j                  j)                  |j                  j$                  j0                        j-                  d       t2        j                  j)                  |j                  j&                  j0                        j-                  d       |j                           	 | j4                  }	d}
d}d|j6                   d|j8                   d|j                  j:                   d|j                  j<                   d	}t?        |	|
||       tA        d|j6                  |j8                  |jB                  |jD                  |jF                  |j*                  d      S tA        d|jH                  d      S tA        ddd      S #  Y yxY w)a5  
    Sku is different sub category of product.
    for eg: if we consider a biscuit. Same biscuit have 50g, 60g packet etc......
    We can add sku from submitting a modal from from master product create and edit form
    in master product listing

    :param request:
    :param product_id:
    :return:
    r  r   Fr*     r  Tr  r   r  r  defaultsr<  long_distance_availabilityr  r  z	SKU AddedSKU r  z added for product () r  )r   sku_namesku_codesku_quantitysku_unitsku_mrpsku_idr   r   r  Invalid request methodr   r   )%r   rU   r   r@   r   r  r  sku_expiry_durationr  rM  r  rN   r  r  rV   get_or_creater  additem_categoryitem_sub_categoryr  r<  updaterR   r   rT   r   rQ  rR  rC  rD  r  r   rS  rT  rU  r  )r   r3  r  r  r  sku_datarZ  sales_productr   r  r  r   r   s                r   add_skurb    s      Z8G~~ w||$==?))5))C&&+)0K15.!CKxxzH \\%%'#<#D#D#R#R c36>>S^^\ $S $ q >>Y.KK**..q1NN&&q)KK--88<<Q?KK11<<@@C11$$++s{{~~+>EEaeEf#++228Q8Q8V8V2W^^37 _ 9&..553;;;X;X;];]5^ee37 f 9 
3 (8"<<$% $S\\N,s||nL_`c`k`k`u`u_vvwx{  yD  yD  yN  yN  xO  OQ  RHh=  #LLLL # 0 0LL;;&&!    7dkk JKK77OPQQs   !A)M? ?Nc           
      *   t         j                  j                  |      j                  d      }|D cg c]G  }|j                  |j
                  |j                  |j                  |j                  |j                  dI }}t        d|d      S c c}w )z
    Used to list the skus in master product add and edit form.
    Master product add : product_add.html,
    Master product edit : product_edit_01.html
    :param request:
    :param product_id:
    :return:
    r3  
created_at)rV  rQ  rR  rS  rT  rU  r  )r   skus)r\   r  r  rS  r<  rQ  rR  rS  rT  rU  r   )r   r3  rf  r  sku_lists        r   get_skusrh  i  s     ;;4==lKD  # &&LLLL((LL;;    9h?@@s   ABc           	         | j                   dk(  r=| j                  j                  d      }t        t        |      }|j
                  j                  }|j
                  j                  j                  }|j
                  j                  j                  }|j                          t        j                  |||gd       t        j                  |||gd       	 | j                  }d}d}d|j                   d	|j                    d
|j
                  j"                   d|j
                  j$                   d	}	t'        ||||	       t)        ddi      S t)        ddd      S #  Y xY w)z
    Delete Sku function used to delete an sku from product edit and product add section.
    product edit : product_edit_01.html,
    product add : product_add.html
    :param request:
    :return:
    r   rV  rK  r   r   r  zSKU DeletedrN  r  z deleted for product rO  rP  r   r  r   zInvalid requestrY  )r   r   r   r   r\   r  r<  r]  r   r^  r  r   r   r   r   rQ  rR  rC  rD  r  r   )
r   rV  r  sku_product_idsku_category_idsku_sub_category_idr  r  r   r   s
             r   
delete_skurm    s`    ~~!!(+/ ++..++3388!kk;;@@ 	

 	*55NK^`o;q}~'33.J]_n9o{|}	||H H#G l3<<.H]^a^i^i^s^s]ttuvy  wB  wB  wL  wL  vM  MO  PK(G[9 Xy12277HIJJ	s   A)E E c                    	 t         j                  j                  |      }d|j                  |j                  |j
                  |j                  |j                  |j                  |j                  |j                  |j                  |j                  d
d}t        |      S # t         j                  $ r t        ddd      cY S w xY w)z3
    Used to get a sku detail with the sku id.
    rK  r  )
rQ  rR  rS  rT  rU  rZ  sku_bulk_qty_limitsame_day_deliverycustomization_availabler  )r   r  r   zSKU not found.rY  )r\   r  r   rQ  rR  rS  rT  rU  rZ  ro  rp  rq  r  r   DoesNotExist)r   rV  r  datas       r   get_skurt    s    Nkkooo(  LLLL # 0 0LL;;'*'>'>&)&<&<%(%:%:+.+F+F!nn
 D!! Nw;KLMMNs   BB! !!CCc                    t         j                  j                         D ]  }t        j                  j	                  || | j
                  | j
                  d      \  }}| j
                  |_        | j
                  |_        |j                          t        j                  | j                  gd       | j
                  dk(  rt         j                  j                         D ]  }t        j                  j                  | |      j                         }|r|j                  dk(  r| j                  j                  j!                  |       | j                  j!                  |       | j                  j"                  j                  j!                  |       | j                  j$                  j                  j!                  |       | j                  j'                  |       t)        j                  | j                  gd       "t         j                  j                         D ]  }| j                  j'                  |        t)        j                  | j                  gd        It         j                  j                         D ]  }| j                  j'                  |        t)        j                  | j                  gd        y)aQ  
    On sku change we need to update the status to every shop the sku is assigned
    to.
    Based on the sku and shop admin status we are checking the availability of product.
    If shop_admin status is not visible we check for long distance availability based on
    sku status.
    :param sku:
    :param sku_data:
    :return:
    rH  rI  r   r   r  r  r  N)rN   r  r  rV   r[  r  r  r   r  r   r   r<  r  r  r  r  r\  r]  r^  remover   )r`  rZ  ra  r   rv  sales_unit_status_checkr  s          r   sku_update_status_changesry    s"    \\ 5<<JJh+3+>+>(J]J]^ K 
q
 +3*=*=''224@@x{{m_`a )+!\\--/	*C*K*K*R*RW_^g +S +iininip (* /@@IM !((3377	B ++//	: ((66AAEEiP ((::EEII)T ++229=0<<8;;-[\] $ 0 0 2 ++2248 !3,88x{{mWXY+ 00 "\\--/	##**95 0$00x{{mqQS  r   c           	      ^   t        t        |      }| j                  dk(  rt        | j                  |      }|j                         r|j                         }t        |       	 | j                  }d}d}d|j                   d|j                   d|j                  j                   d	|j                  j                   d
	}t        ||||       t%        ddd      S t%        d|j&                  d      S t%        ddd      S # t        $ r"}	t!        j"                  d|	        Y d}	~	Zd}	~	ww xY w)z
    Update an sku listed in the master product listing.
    update the sku in edit or add sku page.
    product edit : product_edit_01.html,
    product add : product_add.html

    :param request:
    :param sku_id:
    :return:
    rK  r   r  r  zSKU UpdatedrN  r  z updated for product rO  rP  zSKU Updated Error (logging): Nr  zSKU updated successfully.rY  r   rW  rX  )r   r\   r   r@   r   r  r  ry  r   rQ  rR  r  rC  rD  r  r   r   r   r   r  )
r   rV  r  r  r`  r  r  r   r   r   s
             r   
update_skur{    sM    CF
+C~~w||c2==?yy{H &h/"<<$' $X%6%6$7|HDUDUCVVkltl|l|  mG  mG  lH  HI  JR  JZ  JZ  Jd  Jd  Ie  eg  hHh=  9A\ ]^^7dkk JKK77OPQQ   =aSAB	s   $A)D 	D,
D''D,c                    t        t        |      }| j                  dk(  r| j                  j	                  d      }|||_        |j                          t        |       	 | j                  }d}d}d|j                   d|j                   d| d	|j                  j                   d
|j                  j                   d}t        ||||       t        ddd      S t        ddd      S t        ddd      S #  Y .xY w)z
    Sku status update from product listing.
    When we click on the drop down on product listing, sku will list.
    On the sku list we have the option to change the sku status.

    :param request:
    :param sku_id:
    :return:
    rK  r   r  r  zSKU Status UpdatedrN  r  z updated status to  for product rO  rP  r  z SKU status updated successfully.rY  r   z"No status provided in the request.z-Invalid request method. Only POST is allowed.)r   r\   r   r   r   r  r  ry  r   rQ  rR  r  rC  rD  r  r   )r   rV  r  r   r  r  r   r   s           r   update_sku_from_product_listr~  2  s>    CF
+C~~!!,/ #CNHHJ &c*"<<$. $S\\N,s||nL_`f_ggtux  vA  vA  vK  vK  uL  LM  NQ  NY  NY  Nc  Nc  Md  df  gHh=  9Ac dee7?c dee77fghhs   !A,C7 7C;c                 t   t        t        |      }g }d}d}|j                  }|j                  }| j                  j                  d      }| j                  j                  d      }	| j                  dk(  rt        | j                  | j                  |      }
|
j                         r|
j                         }| j                  j                  d      }|D ]#  }t        j                  j                  ||	       % | j                  j                  d
      }|D ]#  }t        j                  j                  ||       % d}d}t!        j"                  |j$                  |j$                  |j&                  g       t)        j"                  |j$                  |j$                  |	|gd       	 | j*                  }d}d}d|j,                   d|j.                   d|j                  j0                   d|j                  j2                   d|j                  j4                   d|j                  j6                   d}t9        ||||       t;        d||d|j&                  id      S |j=                  |
j>                         t;        d|
j>                  d      S t        |      }
||
d||||d}tA        | d|      S #  Y vxY w)a  
        Editing the product from the product listing page.
        In the side bar we have master product section
        On opening the master product we have the option to edit
        the product.
    :param request:
    :param product_id:
    :return:
    r  FTr]  r^  r   r  r4  r5  r7  r8  r   r   r   r  r:  r;  r  r0   () and sub category rP  r<  r=  r@  rJ  )r  rA  r  r>  r?  r  r3  zproduct_edit_01.html)!r   rU   r]  r^  r   r   r   r;   r   r  r  rB  rW   r  r  rX   r   r   r   r<  r   r   rC  rD  r  r  r  r  r  r   r  r  r   )r   r3  r  r  r>  r?  existing_categoryexisting_sub_categoryto_categoryto_sub_categoryrA  r4  r6  r7  r9  r  r  r   r   r[  s                       r   master_product_editr  [  s4     Z8GFJN  --#55,,""?3Kll&&':;O ~~"7<<Q  ""'')G ]]**84F$$++G5+I   ]]**84F$$++G5+I   J"N .99@Q@V@VWlWqWqryr|r|?}~:FFOdOiOij{  kA  kA  BQ  S^  O`  lm  n"<<$2 ():):(;<HYHYGZZpqx  rG  rG  rU  rU  qV  VX  Y`  Yn  Yn  Y|  Y|  X}  }P  QX  Qj  Qj  Q|  Q|  P}  }  @G  @Y  @Y  @k  @k  l  ln  oHh=  ("0'**	!   MM,--.E\=P=P QRR0L $# ( G '17;;7s   *BJ3 3J7c                 @   t        t        |      }|j                  j                  }|j	                          	 | j
                  }d}d}d|j                  j                   d|j                  j                   d|j                  j                  j                   d|j                  j                  j                   d|j                  j                  j                   d|j                  j                  j                   d	}t        ||||       t        d
|      S #  Y xY w)zV
    deleting product image
    :param request:
    :param image_id:
    :return:
    rK  r  zMaster Product Image deletedr;  r  z  deleted its image for category r  r  rP  edit_master_productrd  )r   rW   r  r<  r  r   rC  rD  r]  r  r  r^  r  r  r  r   )r   image_idr6  r3  r  r  r   r   s           r   delete_imager    s    lx8E!!J	LLN<<0 !8!8 9emmF]F]E^^~  @E  @M  @M  @[  @[  @i  @i  j  jl  mr  mz  mz  mH  mH  mV  mV  lW  Wj  kp  kx  kx  kJ  kJ  k\  k\  j]  ]_  `e  `m  `m  `  `  `Q  `Q  _R  RT  UHh5 )jAA   CD Dc                 @   t        t        |      }|j                  j                  }|j	                          	 | j
                  }d}d}d|j                  j                   d|j                  j                   d|j                  j                  j                   d|j                  j                  j                   d|j                  j                  j                   d|j                  j                  j                   d	}t        ||||       t        d
|      S #  Y xY w)zZ
    deleting the product video
    :param request:
    :param video_id:
    :return:
    rK  r  zMaster Product Video deletedr;  r  z  deleted its video for category r  r  rP  r  rd  )r   rX   r  r<  r  r   rC  rD  r]  r  r  r^  r  r  r  r   )r   video_idr9  r3  r  r  r   r   s           r   delete_videor    s    lx8E!!J	LLN<<0 !8!8 9emmF]F]E^^~  @E  @M  @M  @[  @[  @i  @i  j  jl  mr  mz  mz  mH  mH  mV  mV  lW  Wj  kp  kx  kx  kJ  kJ  k\  k\  j]  ]_  `e  `m  `m  `  `  `Q  `Q  _R  RT  UHh5 )jAAr  c                    | s;t         j                  j                  |j                        j	                  d       nd}| D ]q  }t
        j                  j                  |d      j                         }|s5t         j                  j                  |j                        j	                  d       d} n |s:t         j                  j                  |j                        j	                  d       t        j                  j                  |dd      }|s;t        j                  j                  |j                        j	                  d       y	d}|D ]q  }t
        j                  j                  |d      j                         }|s5t        j                  j                  |j                        j	                  d       d} n |s:t        j                  j                  |j                        j	                  d       y	)
z
    long distance product availability check and update the availability
    :param long_dist_sku_sub_category:
    :param product_category:
    :param product_sub_category:
    :return:
    r  FrL  r  r  r   TrG  product__item_categoryr  sku_expiry_duration__gtN)	rT   r  r  r   r_  rV   r  r\   rR   )	long_dist_sku_sub_categoryproduct_categoryproduct_sub_categorylong_dist_sub_category_flagrZ  long_dist_sales_check_sublong_dist_sku_categorylong_dist_sku_category_flaglong_dist_sales_check_cats	            r   #long_distance_master_product_deleter    s    &""))/C/H/H)IPP', 	Q 	. ',#+A(A(I(I(P(PUV_h(P(i(o(o(q%("**117K7P7P1QXX/3 Y 5.2+ , +&&--3G3L3L-MTT+0 U 2 ![[//GW;D^` 0 b!&&,<,A,A&BII', 	J 	.  ',#'A(A(I(I(P(PUV_h(P(i(o(o(q%(''..4D4I4I.JQQ/3 R 5.2+ ( +##**0@0E0E*FMM+0 N 2
r   )r   c                    t         j                  j                  |      }|j                         }|s!t	        j
                  | d       t        d      S |j                  }|j                  }t        j                  j                  |      }|j                         r!t	        j                  | d       t        d      S |j                          t	        j                  | d|j                   d       t        j                  j                  |dd	
      }t!        |||       t#        j$                  |j&                  |j&                  dddd       	 | j(                  }d}	d}
d|j                   d|j*                   d|j,                   d|j.                   d|j0                   d|j2                   d}t5        ||	|
|       t        d      S # t6        $ r,}t9        j
                  d|        Y d}~t        d      S d}~ww xY w)z%
    deleting the master product
    r  zProduct not found.list_master_product)Productz9Your Product is used in ads! Couldn't delete the product.z	Product 'z' deleted successfully.r  rG  product__item_sub_categoryr  r  N)r  r  r  r  r   kwargsr   r  zMaster Product deletedr;  r  r  r  r  r   z&Master Product Delete Error(logging): )rU   r  r  r  r   r   r   r]  r^  ro   existsr  r  r  rC  r\   r  r   r   r   r   rD  r  r  r  r  r  r   r   )r   r3  r  product_datar  r  adsr  r  r  r   r   r   s                r   master_product_deleter    s    %%%4G==?Lw 45-..#11'99 ++

Z

0C
zz|G	
 -.. NN 
L**++BC "%!3!3#7 " "4 " (" 3>>%9%>%>!1!6!6#	
 D<<*|--.k,:P:P9Q R$$4$B$B#C D ..//B#556 7$667q	: 	 	Hh5 )**  D>qcBCC)**Ds   A/G 	H G;;H )r   c           
         | j                   j                  d      }| j                   j                  d      }| j                   j                  d      }| j                   j                  d      }t        j                  j	                  |      j                  d      }|r|j	                  ||      }n|j	                  |      }g }|j	                         D ]  }|j                  |j                  |j                  |j                  j                  |j                  j                  g d	}g }	|j                  j                         D ]J  }
|
j                  |
j                  |
j                   |
j"                  |
j$                  |
j&                  |
j(                  d
}	 t*        j                  j-                  ||
ddd      \  }}|j.                  dk(  r|
j0                  j3                  |j0                         |
j4                  j0                  j3                  |j0                         |
j4                  j                  j0                  j3                  |j0                         |
j4                  j                  j0                  j3                  |j0                         n7t7        j8                  |
j:                  |j0                  j<                  gd       |j>                  dk(  rG|
j@                  dkD  rd|
_!        |
jE                          t        j                  j	                  |
j4                  j:                        jG                  d       tH        j                  j	                  |
j4                  j                  j<                        jG                  d       tJ        j                  j	                  |
j4                  j                  j<                        jG                  d       nztL        j                  j	                  |
j:                        jG                  d       tL        j                  j	                  |
j4                  dd      }|sEt        j                  j	                  |
j4                  j:                        jG                  d       nd}|D ]{  }t*        j                  j	                  |d      jO                         }|s5t        j                  j	                  |
j4                  j:                        jG                  d       d} n |sDt        j                  j	                  |
j4                  j:                        jG                  d       tL        j                  j	                  |
j4                  j                  dd      }|sOtJ        j                  j	                  |
j4                  j                  j<                        jG                  d       nd}|D ]  }t*        j                  j	                  |d      jO                         }|s5tJ        j                  j	                  |
j4                  j                  j<                        jG                  d       d} n |sNtJ        j                  j	                  |
j4                  j                  j<                        jG                  d       tL        j                  j	                  |
j4                  j                  dd      }|sPtH        j                  j	                  |
j4                  j                  j<                        jG                  d       nd}|D ]  }t*        j                  j	                  |d      jO                         }|s5tH        j                  j	                  |
j4                  j                  j<                        jG                  d       d} n |stH        j                  j	                  |
j4                  j                  j<                        jG                  d       n;tL        j                  j	                  |
j:                        jG                  d       tL        j                  j	                  |
j4                  dd      }|sEt        j                  j	                  |
j4                  j:                        jG                  d       nd}|D ]{  }t*        j                  j	                  |d      jO                         }|s5t        j                  j	                  |
j4                  j:                        jG                  d       d} n |sDt        j                  j	                  |
j4                  j:                        jG                  d       tL        j                  j	                  |
j4                  j                  dd      }|sOtJ        j                  j	                  |
j4                  j                  j<                        jG                  d       nd}|D ]  }t*        j                  j	                  |d      jO                         }|s5tJ        j                  j	                  |
j4                  j                  j<                        jG                  d       d} n |sNtJ        j                  j	                  |
j4                  j                  j<                        jG                  d       tL        j                  j	                  |
j4                  j                  dd      }|sOtH        j                  j	                  |
j4                  j                  j<                        jG                  d       nd}|D ]  }t*        j                  j	                  |d      jO                         }|s5tH        j                  j	                  |
j4                  j                  j<                        jG                  d       d} n |sNtH        j                  j	                  |
j4                  j                  j<                        jG                  d       |j>                  |d<   |j.                  |d<   |	jQ                  |       M |	|d<   |jQ                  |        tS        |d      }|jU                  d      }tW        d d!|i      }| jX                  jZ                  d"k(  rtW        d#d!|i      } ntW        d$d!|i      } t]        | |d%d&      S #  d|d<   d|d<   |	jQ                  |       Y xY w)'a  
    this is a function to filter and list the products based on sub category
    we have this drop down menu having the list of sub categories
    if we select one sub category based on that sub category product will list on the below table.
    :param request:
    :return:
    r  subcategory_idr#  product_typer  r  )r]  r^  r]  r   rC  rD  r  r  r  r   rQ  rR  rS  rT  rU  r  r  rH  rI  r   r   rG  TrK  rL  r  Fr  r  r  r  r  r  r   r  r  r  rt  r   pagination.htmlr  rm  product_list_page.htmlproduct_page.htmltbody
paginationsafe)/rx  r   rU   r  r  rS  r   rC  rD  r]  r  r^  r  rf  r  rQ  rR  rS  rT  rU  r  rV   r[  r  r  r\  r  r   r   r<  r   r   rZ  rM  r  r_  rR   rT   r\   r  r  r    ry  r   r   r   r   )!r   r  r  r#  r  product_list	list_datarZ  r  rg  jr`  ra  r   long_dist_sku_productlong_dist_product_flagxlong_dist_sales_checkr  r  ar  r  r  br  cdr   r}  productsr  r  s!                                    r   subcategory_product_listr  f  s    ++//-0K[[__%56Nkkooi(G;;??>2L##***ENN_L#**Xf*g#***EI  "dd1;;)*)F)F-.-@-@-R-R[]_ Add



dedrdr$%JJ199TUT`T`bHo*#<#D#D#R#R&A3<	R $S $ q
 !22i?LL$$]%=%=>II((,,]-E-EFII++66::=;S;STII//::>>}?W?WX:FFQTTS`SkSkSpSpLq}~ !''94,,r17;4 ((//199<</@GGcgGh'//66AII<S<S<X<X6Y``7; a =*2299qyy?Z?Z?_?_9`gg7; h = **add*3::V[:\030B0B199NW[] 1C 1_-  5$,,33qyy||3DKK;@ L B 6;2%:8Q8Y8Y8`8`efpy8`8z  9A  9A  9C 5#8$,$4$4$;$;qyy||$;$L$S$SCG %T %I=A$:$) &; $: ( 0 0 7 7199<< 7 H O O?D !P !F 69[[5G5G78yy7R7R'0" 6H 6N2  :.66==%&YY%@%@%E%E > GGMv;@ HN HB ;@7%?<U<]<]<d<dijs|<d<}  =D  =D  =F 9#<$6$>$>$E$E-.YY-H-H-M-M %F %OOUvCG PV PIBF$?$) &@ $? 2 : : A A)*)D)D)I)I !B !KKQ6?D LR LF 251C1C34993J3J'0" 2D 2N.  6+33::%&YY%<%<%A%A ; CCI6;@ DJ DB ;@7%;<U<]<]<d<diju~<d<  =F  =F  =H 9#<$3$;$;$B$B-.YY-D-D-I-I %C %KKQ6CG LR LIBF$?$) &< $? / 7 7 > >)*)@)@)E)E !? !GGMv?D HN HF KK&&!$$&/66RW6X,/KK,>,>qyyJSWY -? -[) 1 ((//199<</@GG7< H > 27.!6A4M4U4U4\4\abmv4\4w4}4}414 ( 0 0 7 7199<< 7 H O O?C !P !E9= 6 % "7  6$,,33qyy||3DKK;@ L B 251C1C34993N3N#,b 2D 2J. 6*2299!"!<!<!A!A : CCI67< DJ D> 7<3!;A8Q8Y8Y8`8`efqz8`8{  9B  9B  9D58 2 : : A A)*)D)D)I)I !B !KKQ6?C LR LE>B ; % "<  ;.66==%&YY%@%@%E%E > GGMv;@ HN HB .1[[-?-?/0yy/F/F#,b .@ .J* 2'//66!"!8!8!=!= 7 ??Ev7< @F @> 7<3!7A8Q8Y8Y8`8`efqz8`8{  9B  9B  9D58 / 7 7 > >)*)@)@)E)E !? !GGMv?C HN HE>B ; % "8  ;+33::%&YY%<%<%A%A ; CCI6;@ DJ DB &3%9%9" 1>0O0O,-)[ h 'U&w #x )R(I!!!$H!"3nh5OPJ||- !9NH;UV !4~x6PQ%zBOO'*%/"0:,-)s7   L2tEtE#t FtEtE"t>Ctt=c           
         | j                   j                  d      }| j                   j                  d      }| j                   j                  d      }t        j                  j	                  |      j                  d      }g }|rB|j	                  |      }t        j                  j	                  |      j                  dd	      }g }|j	                         D ]  }g }|j                  |j                  |j                  |j                  j                  |j                  j                  g d
}	|j                  j!                         D ]n  }
|
j                  |
j"                  |
j$                  |
j&                  |
j(                  |
j*                  |
j,                  d}	 t.        j                  j1                  ||
ddd      \  }}|j2                  dk(  r|
j4                  j7                  |j4                         |
j8                  j4                  j7                  |j4                         |
j8                  j                  j4                  j7                  |j4                         |
j8                  j                  j4                  j7                  |j4                         n7t;        j<                  |
j>                  |j4                  j@                  gd       |jB                  dk(  rk|
jD                  dkD  rtF        j                  j	                  |
j>                        jI                  d       t        j                  j	                  |
j8                  j>                        jI                  d       tJ        j                  j	                  |
j8                  j                  j@                        jI                  d       t        j                  j	                  |
j8                  j                  j@                        jI                  d       nztF        j                  j	                  |
j>                        jI                  d       tF        j                  j	                  |
j8                  dd      }|sEt        j                  j	                  |
j8                  j>                        jI                  d       nd}|D ]{  }t.        j                  j	                  |d      jM                         }|s5t        j                  j	                  |
j8                  j>                        jI                  d       d} n |sDt        j                  j	                  |
j8                  j>                        jI                  d       tF        j                  j	                  |
j8                  j                  dd      }|sOt        j                  j	                  |
j8                  j                  j@                        jI                  d       nd}|D ]  }t.        j                  j	                  |d      jM                         }|s5t        j                  j	                  |
j8                  j                  j@                        jI                  d       d} n |sNt        j                  j	                  |
j8                  j                  j@                        jI                  d       tF        j                  j	                  |
j8                  j                  dd      }|sPtJ        j                  j	                  |
j8                  j                  j@                        jI                  d       nd}|D ]  }t.        j                  j	                  |d      jM                         }|s5tJ        j                  j	                  |
j8                  j                  j@                        jI                  d       d} n |stJ        j                  j	                  |
j8                  j                  j@                        jI                  d       n;tF        j                  j	                  |
j>                        jI                  d       tF        j                  j	                  |
j8                  dd      }|sEt        j                  j	                  |
j8                  j>                        jI                  d       nd}|D ]{  }t.        j                  j	                  |d      jM                         }|s5t        j                  j	                  |
j8                  j>                        jI                  d       d} n |sDt        j                  j	                  |
j8                  j>                        jI                  d       tF        j                  j	                  |
j8                  j                  dd      }|sOt        j                  j	                  |
j8                  j                  j@                        jI                  d       nd}|D ]  }t.        j                  j	                  |d      jM                         }|s5t        j                  j	                  |
j8                  j                  j@                        jI                  d       d} n |sNt        j                  j	                  |
j8                  j                  j@                        jI                  d       tF        j                  j	                  |
j8                  j                  dd      }|sOtJ        j                  j	                  |
j8                  j                  j@                        jI                  d       nd}|D ]  }t.        j                  j	                  |d      jM                         }|s5tJ        j                  j	                  |
j8                  j                  j@                        jI                  d       d} n |sNtJ        j                  j	                  |
j8                  j                  j@                        jI                  d       |jB                  |d<   |j2                  |d<   |jO                  |
       q ||	d<   |jO                  |	        tQ        |d      }|jS                  d       }tU        d!d"|i      }| jV                  jX                  d#k(  rtU        d$d"|i      } ntU        d%d"|i      } |rt[        t]        |      | |d&d'      S t[        g | |d&d'      S #  d|d<   d|d<   |jO                  |       Y 9xY w)(z
    get the list of subcategories from a category
    on selecting the category from drop down a list of sub category loaded on drop down subcategory
    could be more efficient with processing limit to the page

    :param request:
    :return:
    r  r#  r  r  r  r  r  r   r  r  r  r  rH  rI  r   r   rG  rK  TrL  r  Fr  r  r  r  r   r  r  r  rt  r   r  r  rm  r  r  )subcategoriesr  r  r  )/rx  r   rU   r  r  rS  rT   rP  r   rC  rD  r]  r  r^  r  rf  r  rQ  rR  rS  rT  rU  r  rV   r[  r  r  r\  r  r   r   r<  r   r   rZ  r\   r_  rR   r  r  r    ry  r   r   r   r   rO  )!r   r  r#  r  r  r  r  rZ  rg  r  r  r`  ra  r   r  r  rr  r  r  nr  r  r  lr  gowr}  r  r  r  s!                                    r   load_subcategoriesr  H	  s    ++//-0Kkkooi(G;;??>2L##***ENN_LM#***E*2299k9RYYZ`buvI  "dd1;;)*)F)F-.-@-@-R-R[]_ Add



dedrdr$%JJ199TUT`T`bHi*#<#D#D#R#R&A3<	R $S $ q
 !22i?LL$$]%=%=>II((,,]-E-EFII++66::M<T<TUII//::>>}?W?WX ;FFQTTS`SkSkSpSpLq}~ ''94,,r1**add*3::VZ:[ ((//199<</@GGcgGh'//66AII<S<S<X<X6Y``7; a =*2299qyy?Z?Z?_?_9`gg7; h = **add*3::V[:\030B0B199NW[] 1C 1_-  5$,,33qyy||3DKK;@ L B 6;2%:8Q8Y8Y8`8`efqz8`8{  9B  9B  9D 5#8$,$4$4$;$;qyy||$;$L$S$SCG %T %I=A$:$) &; $: ( 0 0 7 7199<< 7 H O O?D !P !F 69[[5G5G78yy7R7R'0" 6H 6N2  :.66==%&YY%@%@%E%E > GGMv;@ HN HB ;@7%?<U<]<]<d<diju~<d<  =F  =F  =H 9#<$6$>$>$E$E-.YY-H-H-M-M %F %OOUvCG PV PIBF$?$) &@ $? 2 : : A A)*)D)D)I)I !B !KKQ6?D LR LF 251C1C34993J3J'0" 2D 2N.  6+33::%&YY%<%<%A%A ; CCI6;@ DJ DB ;@7%;<U<]<]<d<diju~<d<  =F  =F  =H 9#<$3$;$;$B$B-.YY-D-D-I-I %C %KKQ6CG LR LIBF$?$) &< $? / 7 7 > >)*)@)@)E)E !? !GGMv?D HN HF KK&&!$$&/66RW6X,/KK,>,>qyyJSWY -? -[) 1 ((//199<</@GG7< H > 27.!6A4M4U4U4\4\abmv4\4w4}4}414 ( 0 0 7 7199<< 7 H O O?C !P !E9= 6 % "7  6$,,33qyy||3DKK;@ L B 251C1C34993N3N#,b 2D 2J. 6*2299!"!<!<!A!A : CCI67< DJ D> 7<3!;A8Q8Y8Y8`8`efqz8`8{  9B  9B  9D58 2 : : A A)*)D)D)I)I !B !KKQ6?C LR LE>B ; % "<  ;.66==%&YY%@%@%E%E > GGMv;@ HN HB .1[[-?-?/0yy/F/F#,b .@ .J* 2'//66!"!8!8!=!= 7 ??Ev7< @F @> 7<3!7A8Q8Y8Y8`8`efqz8`8{  9B  9B  9D58 / 7 7 > >)*)@)@)E)E !? !GGMv?C HN HE>B ; % "8  ;+33::%&YY%<%<%A%A ; CCI6;@ DJ DB &3%9%9"0=0O0O,-"O \ 'U&k #l )R(I!!!$H!"3nh5OPJ||- !9NH;UV !4~x6PQd=.AEakl!&( 	( b5PZ[bghh+*%/"0:,-)s7   Mu!4Eu!E#u!'Fu!3Eu!E"u!%Cu!!vc                     | j                   j                  d      }|rFt        j                  j	                  |      j                  dd      }t        t        |      d      S t        g d      S )Nr  r  r   r  Fr  )rx  r   rT   r  r  rP  r   rO  )r   r  r  s      r   load_subcategories_productr  '
  s^    ++//-0K*2299k9RYYZ`buvD/e<<BU++r   c                 l   t         j                  j                  d      }t        |d      }| j                  j                  d      }	 |j                  |      }|dd}t        | d|      S # t        t        f$ r |j                  d      }Y 4t        $ r |j                  |j                        }Y Yw xY w)Nrs  rt  ru  r   rJ  r  r  product_list.html)rU   r  rS  r    rx  r   ry  r"   rz  r!   r{  r   )r   r  r}  ru  r[  s        r   master_product_listr  0
  s     ((7H(B'I;;??6"D;%%d+
  (HEG'.88 i( )%%a( ;%%i&9&9:;r  c           	      X   | j                   j                  dd      }| j                   j                  dd      }| j                   j                  dd      }t        j                  j	                  d      j                  d      }|r_t        j                  j	                  t        |      t        |	      z  t        |
      z  t        |      z        j                  d      }|r(t        j                  |d      }|j	                  |      }|r|j	                  |      }t        |d      }| j                   j                  d      }	 |j                  |      }	t        d      }
| j                   j!                         }d|v r|d= |	j"                  j$                  D cg c]  }||
 d|j'                  d       d| d! }}|	||||dd}t)        | d|      S # t        t        f$ r |j                  d      }	Y t        $ r |j                  |j                        }	Y w xY wc c}w )z
    In the product section in side bar. if we click on the master product.
    We will get the list of master products.
    :param request:
    :return:
    searchr   product_dateproduct_statusMaster Productr  r  item_name__icontainsitem_code__icontains'item_category__category_name__icontains/item_sub_category__sub_category_name__icontains%Y-%m-%dr_  r  rt  ru  r   r  ?r  r  &page=page_numberurlrJ  )r  search_queryr   r  pagination_linksr  r  )rx  r   rU   r  r  rS  r   r   strptimer    ry  r"   rz  r!   r{  r|   copyr}  
page_range	urlencoder   )r   product_splitr  r  r  r  formatted_dater}  ru  r  base_urlquery_paramsr  r  r[  s                  r   r  r  A
  s9    ;;??8R0L;;??>26L[[__%5r:N ##**8H*IRRSbcL ''..<0<01lCD lKL

 (?
# 	 !**<D#**n*M#**.*I,+I ;;??6"D;%%d+ ,-H;;##%L  $--88
 9K 'Zq!7!7S!7!A B&V	
 9   !$(,#G '.885 i( )%%a( ;%%i&9&9:;s   G  $H' H$>#H$#H$)rk  rl  rm  c           
      H    | j                   j                  dd      }t        j                  j	                  |      j                  d      }| j                  j                  dk(  r[t        j                  j                  d      j	                  | j                  j                        j                         j                  }t        j                  j                         j                  d      }t        t        |	      }|j                  }|j                   }|rot        j                  j	                  |      j	                  t#        |
      t#        |      z  t#        |      z  t#        |      z        j                  d      }| j                   j                  d      }	|	r|j	                  |	      }| j                   j                  d      }
|
r|j	                  |
      }g }|D ]"  }|j$                  |j&                  |j(                  |j*                  j,                  |j.                  j0                  g d}g }|j2                  j                         D ]  }	 |j$                  |j4                  |j6                  |j8                  |j:                  |j<                  |j>                  d}t@        j                  jC                  ||ddd      \  }}|jD                  dk(  r|jF                  jI                  |       |jJ                  jF                  jI                  |       |jJ                  j*                  jF                  jI                  |       |jJ                  j.                  jF                  jI                  |       n7tM        jN                  |jP                  |jF                  j                  gd       |jR                  dk(  r|jT                  dkD  rptV        j                  j	                  |jP                        j                         }d|_,        |j[                          t        j                  j	                  |jJ                  jP                        j                         }d|_,        |j[                          t        j                  j	                  |jJ                  j*                  j                  	      j                         }d|_,        |j[                          t\        j                  j	                  |jJ                  j.                  j                  	      j                         }d|_,        |j[                          nztV        j                  j	                  |jP                        j_                  d       tV        j                  j	                  |jJ                  dd      }|sEt        j                  j	                  |jJ                  jP                        j_                  d       nd}|D ]{  }t@        j                  j	                  |d      j                         }|s5t        j                  j	                  |jJ                  jP                        j_                  d       d} n |sDt        j                  j	                  |jJ                  jP                        j_                  d       tV        j                  j	                  |jJ                  j.                  dd       }|sOt\        j                  j	                  |jJ                  j.                  j                  	      j_                  d       nd}|D ]  }t@        j                  j	                  |d      j                         }|s5t\        j                  j	                  |jJ                  j.                  j                  	      j_                  d       d} n |sNt\        j                  j	                  |jJ                  j.                  j                  	      j_                  d       tV        j                  j	                  |jJ                  j*                  dd!      }|sPt        j                  j	                  |jJ                  j*                  j                  	      j_                  d       nd} |D ]  }!t@        j                  j	                  |!d      j                         }"|"s5t        j                  j	                  |jJ                  j*                  j                  	      j_                  d       d}  n | st        j                  j	                  |jJ                  j*                  j                  	      j_                  d       n;tV        j                  j	                  |jP                        j_                  d       tV        j                  j	                  |jJ                  dd      }|sEt        j                  j	                  |jJ                  jP                        j_                  d       nd}|D ]{  }#t@        j                  j	                  |#d      j                         }|s5t        j                  j	                  |jJ                  jP                        j_                  d       d} n |sDt        j                  j	                  |jJ                  jP                        j_                  d       tV        j                  j	                  |jJ                  j.                  dd       }|sOt\        j                  j	                  |jJ                  j.                  j                  	      j_                  d       nd}|D ]  }$t@        j                  j	                  |$d      j                         }|s5t\        j                  j	                  |jJ                  j.                  j                  	      j_                  d       d} n |sNt\        j                  j	                  |jJ                  j.                  j                  	      j_                  d       tV        j                  j	                  |jJ                  j*                  dd!      }|sOt        j                  j	                  |jJ                  j*                  j                  	      j_                  d       nd} |D ]  }%t@        j                  j	                  |%d      j                         }"|"s5t        j                  j	                  |jJ                  j*                  j                  	      j_                  d       d}  n | sNt        j                  j	                  |jJ                  j*                  j                  	      j_                  d       |jR                  |d"<   |jD                  |d#<   |ja                  |        ||d'<   |ja                  |       % ti        |d(      }'| j                   j                  d)      }(	 |'jk                  |(      })tu        d+|d,d-.      }*| j                   jw                         }+d)|+v r|+d)= |)jx                  jz                  D ,cg c]  },|,|* d/|+j}                  d01       d2|, d3! }-},|)||-|d4||||d5	}.| j                  j                  dk(  rV| j~                  j                  d6      d7k(  r+t        d8|)|-d9      }/t        d:d;|)i      }0t        |/|0d<      S t        | d=|.      S | j~                  j                  d6      d7k(  r+t        d>|)|-d9      }/t        d:d;|)i      }0t        |/|0d<      S t        | d?|.      S # tb        $ r>}&te        jf                  d$|&        d%d"<   d%|d#<   |ja                  |       Y d&}&~&rd&}&~&ww xY w# tl        tn        f$ r |'jk                  d*      })Y tp        $ r |'jk                  |'jr                        })Y w xY wc c},w )@z
    get the product list on shop admin product listing.
    we can view the product menu on the side bar.
    :param request:
    :param shop_id:
    :param product_type:
    :return:
    r  r   r  r  rm  r&  r'  rs  r  r  r  r  r  category_pager  subcategory_pager^  r  r  r  rH  rI  r   r   rG  rK  TFrL  r  r  r  r  r   r  z'SKU Status Issue(display as disabled): r  Nr  rt  ru  r   list_shop_productr  )r#  r  r  r  r  r  r  r  rJ  )	r  r  r  r  rw  r#  	shop_nameshop_locationr  x-requested-withXMLHttpRequestr  )r  r  r  r  r  zproduct_list_shop_admin.htmlr  zproduct_list_shop.html)Crx  r   rU   r  r  rS  r   r   rN   rL  r   r  rR   r  r   r  r  r   r   rC  rD  r]  r  r^  r  rf  rQ  rR  rS  rT  rU  r  rV   r[  r  r  r\  r  r   r   r<  r   rZ  r\   rM  r  rT   r_  r  r   r   r   r    ry  r"   rz  r!   r{  r|   r  r}  r  r  headersr   r   r   )1r   r#  r  r  r  r  shop_detailsr  r  r  sub_category_pager  rZ  r  rg  r  r`  ra  r   
sku_updateproduct_updateproduct_category_updateproduct_sub_category_updater  r  r  r  r  r  vr  r  r  r  r  stqr   r}  ru  r  r  r  r  r  r[  r  r  s1                                                    r   r  r  
  s    ;;??8R0L ##***ENN_L ||-,,//0ABII"),,"3"3 J 55:UWTT 	 $++//1::=IM %T8L&&I ..M ''..L.IPP<0<01lCD lKL

 (?
# 	 KKOOO4M #***G  (:;#**=N*O" Idd1;;)*)F)F-.-@-@-R-R[]_ AA*"#$$AJJAJJhihvhv()

qyyXYXdXdf $=#D#D#R#R+3<	R $S $ q
 !22i?LL$$W-II((,,W5II++66::7CII//::>>wG:FFQTTS`SkSkSpSpLq}~ !''94,,r1%([[%7%7144%7%@%F%F%H
@D
="))1)9)9)@)@AIILL)@)Q)W)W)YDHA&++-2A2I2I2P2P!"!8!8!=!= 3Q 3??Duw 0MQ/J/4466H6P6P6W6W!"!<!<!A!A 7X 7CCH57 4QU3N388: **add*3::V[:\030B0B199NW[] 1C 1_-  5$,,33qyy||3DKK;@ L B 6;2%:8Q8Y8Y8`8`efqz8`8{  9B  9B  9D 5#8$,$4$4$;$;qyy||$;$L$S$SCG %T %I=A$:$) &; $: ( 0 0 7 7199<< 7 H O O?D !P !F 69[[5G5G78yy7R7R'0" 6H 6N2  :.66==%&YY%@%@%E%E > GGMv;@ HN HB ;@7%?<U<]<]<d<dijt}<d<~  =E  =E  =G 9#<$6$>$>$E$E-.YY-H-H-M-M %F %OOUvCG PV PIBF$?$) &@ $? 2 : : A A)*)D)D)I)I !B !KKQ6?D LR LF 251C1C34993J3J'0" 2D 2N.  6+33::%&YY%<%<%A%A ; CCI6;@ DJ DB ;@7%;<U<]<]<d<diju~<d<  =F  =F  =H 9#<$3$;$;$B$B-.YY-D-D-I-I %C %KKQ6CG LR LIBF$?$) &< $? / 7 7 > >)*)@)@)E)E !? !GGMv?D HN HF KK&&!$$&/66RW6X,/KK,>,>qyyJSWY -? -[) 1 ((//199<</@GG7< H > 27.!6A4M4U4U4\4\abmv4\4w4}4}414 ( 0 0 7 7199<< 7 H O O?C !P !E9= 6 % "7  6$,,33qyy||3DKK;@ L B 251C1C34993N3N#,b 2D 2J. 6*2299!"!<!<!A!A : CCI67< DJ D> 7<3!;A8Q8Y8Y8`8`efpy8`8z  9A  9A  9C58 2 : : A A)*)D)D)I)I !B !KKQ6?C LR LE>B ; % "<  ;.66==%&YY%@%@%E%E > GGMv;@ HN HB .1[[-?-?/0yy/F/F#,b .@ .J* 2'//66!"!8!8!=!= 7 ??Ev7< @F @> 7<3!7A8Q8Y8Y8`8`efqz8`8{  9B  9B  9D58 / 7 7 > >)*)@)@)E)E !? !GGMv?C HN HE>B ; % "8  ;+33::%&YY%<%<%A%A ; CCI6;@ DJ DB &3%9%9"0=0O0O,-)o J 'U&W \ )R(I;;??6"D;%%d+ *wXh3ijH;;##%L  $--88
 9K 'Zq!7!7S!7!A B&V	
 9   !$,$%&&G ||-??126FF$%=6>Td%egE)*;nh=WXJ%z JKKg=wGG-.2BB !4xeu6vw%&7.(9ST
e:FGG '3W==  * GsKL &0"0:,-))** i( )%%a( ;%%i&9&9:;sf   O~0E~?E#~#F~/E~>E"~!C~7 $A@	3

 A@5#A@@A@c                 R   | j                   dk(  r| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }	 t        j                  j                  ||      }	 ||_        ||_        |j                          |d	k(  rJ|j                  j                  d
kD  r0t        j                  j                  |      j                  d       t        j                  j                  |j                  j                   j"                        j                  d       t$        j                  j                  |j                  j                   j&                  j(                        j                  d       t*        j                  j                  |j                  j                   j,                  j(                        j                  d       nt        j                  j                  |      j                  d       t        j                  j                  |j                  j                   d	d
      }|sOt        j                  j                  |j                  j                   j"                        j                  d       nd}|D ]  }t        j                  j                  |d	      j/                         }|s5t        j                  j                  |j                  j                   j"                        j                  d       d} n |sNt        j                  j                  |j                  j                   j"                        j                  d       t        j                  j                  |j                  j                   j,                  d	d
      }	|	sYt*        j                  j                  |j                  j                   j,                  j(                        j                  d       nd}
|	D ]  }t        j                  j                  |d	      j/                         }|s5t*        j                  j                  |j                  j                   j,                  j(                        j                  d       d}
 n |
sXt*        j                  j                  |j                  j                   j,                  j(                        j                  d       t        j                  j                  |j                  j                   j&                  d	d
      }|sYt$        j                  j                  |j                  j                   j&                  j(                        j                  d       nd}|D ]  }t        j                  j                  |d	      j/                         }|s5t$        j                  j                  |j                  j                   j&                  j(                        j                  d       d} n |sXt$        j                  j                  |j                  j                   j&                  j(                        j                  d       |d	k(  r|j                  j0                  j3                  |j0                         |j                  j                   j0                  j3                  |j0                         |j                  j                   j&                  j0                  j3                  |j0                         |j                  j                   j,                  j0                  j3                  |j0                         np|j                  j0                  j5                  |j0                         t7        j8                  |j                  j"                  |j0                  j(                  gd       	 | j:                  }d}d|j0                  j<                   d|j0                  j>                   d}d|j                  j@                   d|j                  jB                   d| d|j                  j                   jD                   d|j                  j                   jF                   d}tH        j                  jK                  ||||       t        dd i      S y## t        j
                  $ r t        ddd      cY S w xY w#  Y 9xY w#  t        d!d"i      cY S xY w)$ac  
    it is the status which can be changed with the admin dashboard not shop admin
    we have the status details in the sales product selection table
    in that table we have a field status
    with this view we can change that status
    with this change of status shop admin status also change to the new status.

    :param request:
    :return:
    r   rV  r#  r   r  r  FOperation not allowed.r  r   r  rG  rK  TrL  r  r  r  r  r  r   r   r  z Product Status Updated for Shop rO  r   rN  r   updated its status to r}  r  r   r  r  r   Invalid request.N)&r   r   r   rV   r  rr  r   r   r  r  r  rZ  r\   r  r_  rU   r  r<  rR   r]  r   rT   r^  r  r  r\  rw  r   r   r   r  r  rQ  rR  rC  rD  r   r  )r   rV  r#  
new_statussales_unit_productr  r  rZ  r  r  r  r  r  r  r  r  r  r   r   s                      r   update_product_sales_statusr    s    ~~!!(+,,""9-\\%%h/
	W!:!B!B!F!FRY_e!F!fk	?(2%3=0##% Y&+=+A+A+U+UXZ+Z""f"-44PT4U  ''+=+A+A+I+I+L+L'MTTptTu''..4F4J4J4R4R4`4`4e4e.fmm/3 n 5"**117I7M7M7U7U7g7g7l7l1mtt/3 u 5 ""f"-44PU4V(+(:(:CUCYCYCaCanwSU ); )W%,$$++/A/E/E/M/M/P/P+QXX38 Y : .3*20I0Q0Q0X0X]^ir0X0s0y0y0{-0$,,337I7M7M7U7U7X7X3Y``;? a A592! 3 2 ((//3E3I3I3Q3Q3T3T/U\\7< ] > .1[[-?-?/A/E/E/M/M/_/_(" .@ .F* 2&..55/33;;MMRR 6 TTZTZ38 U[ U: 38/74M4U4U4\4\abmv4\4w4}4}414.66==%7%;%;%C%C%U%U%Z%Z > \\b\b;? ]c ]A:>7! 8 7*2299!3!7!7!?!?!Q!Q!V!V : XX^X^7< Y_ Y> *-););+=+A+A+I+I+W+W(" *< *F& .#++228J8N8N8V8V8d8d8i8i2jqq38 r : 38/34M4U4U4\4\abmv4\4w4}4}414+33::%7%;%;%C%C%Q%Q%V%V ; XX^X^;? Y_ YA:>7! 4 7'//66<N<R<R<Z<Z<h<h<m<m6nuu7< v > Y&"&&11556H6S6ST"&&..99==>P>[>[\"&&..<<GGKKL^LiLij"&&..@@KKOOPbPmPmn
 #&&11889K9V9VW 7BBI[I_I_IbIbdv  eB  eB  eG  eG  IH  TU  V"<<$<=O=Z=Z=d=d<eefgy  hE  hE  hO  hO  gP  PQ  R $%7%;%;%D%D$E\RdRhRhRqRqQr  sJ  KU  JV  Vc  dv  dz  dz  dB  dB  dL  dL  cM  MO  Pb  Pf  Pf  Pn  Pn  Px  Px  Oy  yz  { ((//XZa<G 0 I  9 566g   )55 	WE<T UVV	WT	?*< =>>sM   #!c' Id #E6d F
d %Hd Cd d '!d
ddd d&c           
      Z   | j                   j                  d      }| j                   j                  d      }| j                   j                  d      }t        j                  j	                  |      j                         }t        j                  j                  d      }|r|j	                  |      }|r|j	                  |      }t        j                  j	                  |      }g }g }	|D ]<  }
|
j                  |
j                  |
j                  |
j                  j                  |
j                  j                  g d	}g }|
j                   j#                         D ]  }|j                  |j$                  |j&                  |j(                  |j*                  |j,                  |j.                  d
}|j.                  dk(  r|j	                  |      j                         }||_        ||_        ||d<   ||d<   |j5                  |       |j5                  |       |dk(  r:|j6                  j9                  |       |j:                  dkD  rd|_        nd|_        n|j6                  j?                  |       d|_        n|j	                  |      j                         }|j0                  |d<   |d   dk(  r|j:                  dkD  rd|_        nd|_        |j2                  |d<   |j5                  |       |j5                  |       |jA                           ||d<   |	j5                  |       ? t        j                  jC                  |ddg       |rBtD        j                  j	                  |d      }|j	                  d      }|s1tF        j                  j	                  |      jI                  d       nd}|D ]g  }t        j                  j	                  |d      j                         }|s5tF        j                  j	                  |      jI                  d       d} n |s0tF        j                  j	                  |      jI                  d       |sNtF        j                  j	                  |      j                         }|r|j6                  j?                  |       nrd}|D ]G  }t        j                  j	                  ||      j                         }|s5|j2                  dk(  sEd} n |rNtF        j                  j	                  |      j                         }|r|j6                  j?                  |       ntF        j                  j	                  |      j                         }|r|j6                  j9                  |       ntF        j                  j#                         D ]d  }tD        j                  j	                  |d      }|j	                  d      }|sd|_        |jA                          nnd}|D ]N  }t        j                  j	                  |d      j                         }|s5d|_        |jA                          d} n |sd|_        |jA                          |s|j6                  j?                  |       d}|D ]G  }t        j                  j	                  ||      j                         }|s5|j2                  dk(  sEd} n |r|j6                  j?                  |       J|j6                  j9                  |       g |rBtD        j                  j	                  |d      }|j	                  d      }|s1tJ        j                  j	                  |      jI                  d       nd}|D ]g  }t        j                  j	                  |d      j                         } | s5tJ        j                  j	                  |      jI                  d       d} n |s0tJ        j                  j	                  |      jI                  d       |sNtJ        j                  j	                  |      j                         }|r+|j6                  j?                  |       nd}!|D ]G  }"t        j                  j	                  |"|      j                         }#|#s5|#j2                  dk(  sEd}! n |!rNtJ        j                  j	                  |      j                         }|r|j6                  j?                  |       nptJ        j                  j	                  |      j                         }|r?|j6                  j9                  |       n"|rtJ        j                  j	                  |      D ]s  }$tD        j                  j	                  |$d      }|j	                  d      }|sd|$_        |$jA                          n}d}|D ]]  }%t        j                  j	                  |%      j                         } | s4| j0                  dk(  sDd|$_        |$jA                          d} n |sd|$_        |$jA                          |s|$j6                  j?                  |       d}!|D ]G  }&t        j                  j	                  |&|      j                         }#|#s5|#j2                  dk(  sEd}! n |!r|$j6                  j?                  |       Y|$j6                  j9                  |       v ntJ        j                  j#                         D ]d  }$tD        j                  j	                  |$d      }|j	                  d      }|sd|$_        |$jA                          nnd}|D ]N  }'t        j                  j	                  |'d      j                         } | s5d|$_        |$jA                          d} n |sd|$_        |$jA                          |s|$j6                  j?                  |       d}!|D ]G  }(t        j                  j	                  |(|      j                         }#|#s5|#j2                  dk(  sEd}! n |!r|$j6                  j?                  |       J|$j6                  j9                  |       g g })g }*|D ]d  }+tD        j                  j	                  |+d      },|,j	                  d      }-|-sd|+_        |+jA                          nnd}.|-D ]N  }/t        j                  j	                  |/d      j                         } | s5d|+_        |+jA                          d}. n |.sd|+_        |+jA                          |,s|+j6                  j?                  |       d}0|,D ]G  }1t        j                  j	                  |1|      j                         }2|2s5|2j2                  dk(  sEd}0 n |0r|+j6                  j?                  |       J|+j6                  j9                  |       g 	 | jL                  }3d}4d}5d}6tN        j                  jQ                  |3|4|5|6        tS        |	d!      }7|7jU                  d"      }8| jV                  j                  d#      d$k(  r*tY        d%d&|8i      }9tY        d'd&|8i      }:t[        |9|:d(      S t]        d)|d*d+d,-      };t_        |;      S #  Y xY w).z;

    :param request:
    :param shop_id:
    :return:
    r  subcategorystatus_category_selectr  r  item_category__uuid)item_sub_category__uuidsales_unit__uuidr  r  r  r  r   r  rG  TFr  )product__item_category__uuidr  )r  rL  r  rv  )r  r  ) product__item_sub_category__uuidr  )category__uuid)r  r  )r  r  r  z3Product Status Updated for Category or Sub CategoryzBulk Category updater  rt  r   r  r  r  r  r  r  r  r  rJ  )r#  r  rw  r  )0r   r   rN   r  r  r  rU   rS  rV   r   rC  rD  r]  r  r^  r  rf  r  rQ  rR  rS  rT  rU  r  r   r  r  r  r\  rZ  rM  rw  r  bulk_updater\   rR   r_  rT   r   r   r  r    ry  r  r   r   r|   r   )<r   r#  r  r  r  r  r  product_selectionupdate_list	show_listrZ  r  rg  r  r`  checksku_category_datar  r  zr  update_statuscategory_remove_flagr  sku_category_salescatyr  sku_sub_category_datar  r  ur  sub_category_remove_flagr  sku_sub_category_salessub_catr  hr  psku_category_listsku_sub_category_listr  sku_productsr  r  r   product_remove_flagsku_productsku_product_salesr  r  r   r   r}  r  r  r  r  s<                                                               r   category_based_update_statusr6  V  s    ,,"":.Kll&&}5O!!":;J<<G,224D##,,_=L#**{*K#**?*S199@@RY@ZKIdd1;;)*)F)F-.-@-@-R-R[]_ Add



dedrdr$%JJ199TUT`T`bH||y()00Q07==?)*4' &0"0:,- ""5))*LL$$T*,,r17;47<4LL''-38A0 *00Q07==?%*\\"H%2q7L7Lr7Q37A038A0 160G0G,-""5))FFHQ T 'U&c f %%11+J]?^_ KK..K:C / E "3!9!9RT!9!U%##***<CC+0 D 2 +0'+,E,M,M,T,TYZen,T,o,u,u,w),#++222DKK37 L 926/ , /''..K.@GG/4 H 6 !+33:::LRRTM((//5#' &%>%F%F%M%MRS`d%M%e%k%k%m"%);;yH/4, ' $ / 7 7 > >K > P V V X !,,33D9 / 7 7 > >K > P V V X !,,006 #**..0C # 2 2#>G !3 !I &7%=%=VX%=%Y")16.
.3+/A0I0Q0Q0X0X]^ir0X0s0y0y0{-09=6
6:3 0 35:C2HHJ$%%d+'+$*A)B)J)J)Q)QVWdh)Q)i)o)o)q&)-??9L380! + (NN))$/NN&&t,O 1R  # 2 2-<  !3 !" &;%A%AZ\%A%]")&&--?-CJJ+0 K 2 +0'/,E,M,M,T,TYZen,T,o,u,u,w),&..55, 6 ..4f37 /5 /926/ 0 /"**111GNN/4 O 6 %.66==?=SYY[M((//5'+$*)B)J)J)Q)QVW]a *R *cchchcj ' *-??9L380 + ( 2 : : A A A W ] ] _ !,,33D9 2 : : A A A W ] ] _ !,,006 -55<<K<X(+(:(:V]FO ); )Q% .C-I-Ibd-I-e*19>G6LLN 38/74M4U4U4\4\ab4\4c4i4i4k148??9LEI B '>B ; % 8 7=B: -&&--d3/3,21J1R1R1Y1Y^_ei 2Z 2kkpkpkr / 25GG9T;@ 8 % 3 0**11$7**..t4W YZ .5599;(+(:(:V]FO ); )Q%-B-I-Ibd-I-e*19>G6LLN 38/74M4U4U4\4\abmv4\4w4}4}414AEG>#LLN:>7! 8 7=B: -&&--d3/3,21J1R1R1Y1Y^_ei 2Z 2kkpkpkr / 25GG9T;@ 8 % 3 0  **11$7**..t4W <^ {{))'i)P , 3 3B 3 O$16G.LLN &+"*,E,M,M,T,TYZen,T,o,u,u,w),9=G6LLN-1* + *5:2%%d+"&+$=$E$E$L$LQ\im$L$n$t$t$v!$(::iG.3+  , #""))$/""&&t,M  T<<G,  ''XRY4? 	( 	A
 )R(I!!!$H -.2BB !4~x6PQ%&7.(9ST
e:FGG**1CSjrsuHHs   05z& &z*c                 R   | j                   dk(  r| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }	 t        j                  j                  ||      }	 ||_        |j                          |d	k(  r|j                  j                  j                  |j                         |j                  j                  j                  j                  |j                         |j                  j                  j                  j                  j                  |j                         |j                  j                  j                  j                  j                  |j                         np|j                  j                  j                  |j                         t!        j"                  |j                  j$                  |j                  j&                  gd
       	 | j(                  }d}d|j                  j*                   d|j                  j,                   d}d|j                  j.                   d|j                  j0                   d| d|j                  j                  j2                   d|j                  j                  j4                   d}t6        j                  j9                  ||||       t        ddi      S y # t        j
                  $ r t        ddd      cY S w xY w#  Y 9xY w#  t        ddi      cY S xY w)Nr   rV  r#  r   r
  Fr  r  r  r   r   r  z+Product Shop Admin Status Updated for Shop rO  r   rN  r  r  r}  r  r   r  r  r   r  )r   r   r   rV   r  rr  r   r  r  r  r  r\  r  r]  r^  rw  r   r   r<  r   r   r  r  rQ  rR  rC  rD  r   r  )	r   rV  r#  r  r  r  r  r   r   s	            r   #update_product_product_admin_statusr8    sW   ~~!!(+,,""9-\\%%h/
	W!:!B!B!F!FRY_e!F!f	?3=0##% Y&"&&11556H6S6ST"&&..99==>P>[>[\"&&..<<GGKKL^LiLij"&&..@@KKOOPbPmPmn #&&11889K9V9VW6BBI[I_I_IbIbdv  eB  eB  eG  eG  IH  TU  V"<<$GHZHeHeHoHoGppq  sE  sP  sP  sZ  sZ  r[  [\  ] $%7%;%;%D%D$E\RdRhRhRqRqQr  sJ  KU  JV  Vc  dv  dz  dz  dB  dB  dL  dL  cM  MO  Pb  Pf  Pf  Pn  Pn  Px  Px  Oy  yz  { ((//XZa<G 0 I  9 566E   )55 	WE<T UVV	W4	?*< =>>s8   #!K' E;L CL L '!L
LLL L&rm  c           
         | j                   j                  d      }| j                   j                  d      }| j                   j                  d      }t        j                  j	                  |      j                         }t        j                  j                  d      }|r|j	                  |      }|r|j	                  |      }g }t        j                  j	                  |      }g }g }	|D ]  }
|
j                  |
j                  |
j                  |
j                  j                  |
j                  j                  g d	}g }|
j                   j#                         D ]N  }|j                  |j$                  |j&                  |j(                  |j*                  |j,                  |j.                  d
}|j.                  dk(  r|j	                  |      j                         }|j0                  dk(  r||_        |j0                  |d<   ||d<   n|j0                  |d<   |j2                  |d<   |j5                  |       |j5                  |       |j	                  |      j                         }|j0                  |d<   |j2                  |d<   |j5                  |       |j5                  |       Q ||d<   |	j5                  |        t        j                  j7                  |dg       t9        j:                  |j<                  |gd       t?        j:                  |j<                  ||gd       tA        j:                  |j<                  ||gd       tC        |	d      }|jE                  d      }	 | jF                  }d}d}d}tH        j                  jK                  ||||       | jL                  j                  d      dk(  r*tO        dd|i      }tO        dd|i      }tQ        ||d      S tS        d      }tU        |      S #  Y bxY w)Nr  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r   r   rt  r   r  z<Product Status Updated for Category or Sub Category For ShopzBulk Category update for Shopr  r  r  r  r  r  r  list_product_shop_admin)+r   r   rN   r  r  r  rU   rS  rV   r   rC  rD  r]  r  r^  r  rf  r  rQ  rR  rS  rT  rU  r  r   r  r  r  r   r   r   r   r   r    ry  r   r   r  r  r   r   r|   r   )r   r#  r  r  r  r  r  r  r  r   rZ  r  rg  r  r`  r!  r}  r  r  r  r   r   r  r  r  s                            r   'category_based_shop_admin_update_statusr;    s    ,,"":.Kll&&}5O!!":;J<<G,224D##,,_=L#**{*K#**_*MK199@@RY@ZKIdd1;;)*)F)F-.-@-@-R-R[]_ Add



dedrdr$%JJ199TUT`T`bH||y()00Q07==?<<9,.8E+ */HX&4>H01
 */HX&494K4KH01""5)) *00Q07==? &+\\"050G0G,-""5))C F 'U&U X %%11+@S?TU+77		;?Wdef/;;$))_^iAjwxy%%diio-Vcde)R(I!!!$H<<P5  ''XRY4? 	( 	A
 -.2BB !9NH;UV%&7.(9ST
e:FGG01HHs   35P P
c                    | j                   j                  dd      }| j                   j                  dd      }| j                   j                  dd      }t        ddd	d
d      }t        ddddd      }t        ddddd      }g }| j                  j                  d      dk(  rt	        t
        |      }	t        | j                         }
t        j                  j                  |	      }|
j                         r|
j                  d      }|	|_        |j                          	 | j                  }d}d}d|j                   d|j                   d|j                  j                    d|j                  j"                   }t$        j                  j'                  ||||       t        dd |i      }t)        |g d!||||||d"	      S i }|
j*                  j-                         D ]  \  }}|d#   ||<    |j/                  |
j*                         |r#t        dd |i      }t)        ||d||||||d"	      S t)        g |d||||||d"	      S t)        g d$d%id||||||d"	      S #  Y xY w)&Nimage_link_1/media/icons/icon.jpgimage_link_2)/media/standard_images/standard_image.jpgimage_link_3/media/banners/banner.jpgimage_field.htmluploadimage_reset_button1r!  
class_nameimage_reset_button
field_nameupload2image_reset_button2r"  upload3image_reset_button3r#  r  r  r  r  Fr*  r  r,  r  r  r-  r  r  zsub_category_delete_list.htmlr  T)	rs  r  r  image_field1image_field2image_field3link_1link_2link_3r   r  zSomething went wrong try again)r   r   r   r  r   rR   r:   rT   r  r  r  r  r  r   r  r  r  r  r   r  r   r  itemsr  )r   r  rQ  rR  rS  rN  rO  rP  r  r  r  r$  r  r  r  r   r   sub_categoriesr   rZ  rs  s                        r   sub_category_creationrV  \  s   \\n.EFF\\n.YZF\\n.IJF#$63;Sh39%;<L $$63<Ti3C%EFL $$63<Ti3A%CDL F-.2BB$_;G27<<@.66==x=P%%',111?L$,L!"<<$0 -l.L.L-M\ZfZxZxYy  zP  Q]  Qf  Qf  Qt  Qt  Pu  u@  AM  AV  AV  Ad  Ad  @e  f ((//XZa<G 0 I ..MP^`qOrsN2RVht1=|+1Vv!W X X E,3399;47a < MM+223 !12QTbduSv!w# ."'#((4ldp"(Ff  $#$$0,`l$&%   *,LM(,Xd&
  	Es   &A=I I"c                 x   | j                   j                  dd       }|r!t        j                  j	                  |      }nt        j                  j                         }g }|D ]J  }|j                  |j                  |j                  |j                  |j                  j                  d       L t        d|i      S )Nr  r  )r<  r   pricer  r  )rx  r   rU   r  r  r  r  r<  r   rX  r  r   )r   r  r  r  r  s        r   filter_productsrY    s    ++//-6K ##**{*K##'') L**LL]]((--	
 	  \233r   c           	      2   | j                   j                  dd      }| j                   j                  dd      }| j                   j                  dd      }t        ddd	d
d      }t        dddd|d      }t        dddd|d      }t        ||||||d      S )Nr=  r>  r?  r@  rA  rB  rC  rD  rE  r!  rF  rJ  rK  r"  rG  rH  rI  linkrL  rM  r#  rN  rO  rP  rQ  rR  rS  rx  r   r   r   r   rQ  rR  rS  rN  rO  rP  s          r   image_field_renderingr`    s     [[__^-DEF[[__^-XYF[[__^-HIF#$63;Sh39%;<L $$63<Ti3CV%UVL $$63<Ti3A6%STL %|UamsV	-. .r   c                 l   t         j                  j                  d      }t        |d      }| j                  j                  d      }	 |j                  |      }|dd}t        | d|      S # t        t        f$ r |j                  d      }Y 4t        $ r |j                  |j                        }Y Yw xY w)z
    In the product section on the side bar. We have
    the product tags. We list the products based on the tags
    on app.
    :param request:
    :return:
    rs     ru  r   rJ  )tag_datar  ztag_list.html)rY   r  rS  r    rx  r   ry  r"   rz  r!   r{  r   )r   tagsr}  ru  r[  s        r   tag_listre    s     <<  /D$"I;;??6"D7!!$'
  H=G'?G44 i( %!!!$ 7!!)"5"567r  c                 |    t         j                  j                  |      }|r|j                          t	        d      S )zq
    delete a tag listed in product section on side bar.
    :param request:
    :param tag_id:
    :return:
    r  re  )rY   r  r  r  r   )r   tag_idrd  s      r   
tag_deleterh  '  s2     <<&)DJr   c                    g }t        t        |      }| j                  dk(  rt        | j                  | j
                  |      }|j                         rH| j                  j                  dd      rd|j                  _	        |j                          t        d      S |j                  |j                         nt        |      }|dd	}t        | d
|      S )z
    If we click on the edit button on tag listing we are
    redirecting to edit form. We can change the tag details
    with the form.
    :param request:
    :param tag_id:
    :return:
    r  r   r  r  r   Nre  rJ  )tag_formr  ztag_edit.html)r   rY   r   r<   r   r   r  r   r  r!  r  r   r  r  r   )r   rg  r  rd  r  r[  s         r   tag_editrk  7  s     FTf-D~~w||W]]TB==? ||"-%)"IIKJ'' MM$++&%H=G'?G44r   c                 P   g }| j                   dk(  ryt        | j                  | j                        }|j	                         r|j                         }t        d      S |j                  |j                         ||dd}t        | d|      S t               }||dd}t        | d|      S )z
    In the tag list window we have a button add tag. on clicking
    the add tag button. we can add a new tag. fields are
    tag_name, icon, products.
    :param request:
    :return:
    r   re  rJ  )r  rj  r  ztag_add.html)
r   r<   r   r   r  r  r   r  r  r   )r   r  rj  rd  r[  s        r   tag_addrm  W  s     F~~7<<7==?DJ''MM(//* $#+G
 '>7;;9 #G '>733r   c                 l   t         j                  j                  d      }t        |d      }| j                  j                  d      }	 |j                  |      }|dd}t        | d|      S # t        t        f$ r |j                  d      }Y 4t        $ r |j                  |j                        }Y Yw xY w)zIn app just like tags we have easy filters also. One of them is dynamic
    filter. for eg: black forest. if we select the black forest we will return the
    products associated with the filter.
    admin listing of dynamic filter.
    rs  rb  ru  r   rJ  )filter_datar  zdynamic_filter_list.html)rZ   r  rS  r    rx  r   ry  r"   rz  r!   r{  r   )r   filter_listr}  ru  r[  s        r   dynamic_filter_listrq  {  s     #**33MBK+q)I;;??6"D>((.
 *hGG'5w?? i( ,((+ >(()<)<=>r  c                 |    t         j                  j                  |      }|r|j                          t	        d      S )a=  
    Dynamic filter also listed on the product section on side bar.
    for each dynamic filter, There is a delete button on the listing.
    on clicking the delete button. a confirmation dialog box will appear.
    click yes and we could delete the filter.
    :param request:
    :param filter_id:
    :return:
    r  rp  )rZ   r  r  r  r   )r   	filter_idrp  s      r   dynamic_filter_deletert    s7     #**11Y1?KM""r   c                 >   g }t        t        |      }| j                  dk(  r^t        | j                  |      }|j                         r|j                          t        d      S |j                  |j                         nt        |      }|dd}t        | d|      S )a  
    Editing a dynamic filter. for each dynamic filter listed on
    dynamic filter list, we have a edit button. clicking on
    the edit button will open a form. fields are tag name and products.

    :param request:
    :param filter_id:
    :return:
    r  r   r  rp  rJ  )filter_formr  zdynamic_filter_edit.html)r   rZ   r   r=   r   r  r  r   r  r  r   )r   rs  r  rp  r  r[  s         r   dynamic_filter_editrw    s     F#$4CK~~ D==?IIKM** MM$++& +6"x@G'5w??r   c                 :   g }| j                   dk(  rnt        | j                        }|j                         r|j	                         }t        d      S |j                  |j                         ||dd}t        | d|      S t               }||dd}t        | d|      S )aJ  
    Adding a dynamic filter.
    On dynamic filter listing page which we can open by clicking
    on the product section on side bar have a add dynamic filter button.
    clicking will open the form enter the products and tag name on the form.
    on submission create a new dynamic filter.

    :param request:
    :return:
    r   rp  rJ  )r  rv  r  zdynamic_filter_add.html)	r   r=   r   r  r  r   r  r  r   )r   r  rv  r  r[  s        r   dynamic_filter_addry    s     F~~'5! %%'FM**MM+,,- *#+G
 '#<gFF') "#G '4g>>r   c                 l   t         j                  j                  d      }t        |d      }| j                  j                  d      }	 |j                  |      }|dd}t        | d|      S # t        t        f$ r |j                  d      }Y 4t        $ r |j                  |j                        }Y Yw xY w)z
    special list showed on the app. for eg:  Rolls - Chicken Spring Roll,
    special list will list when we click on the special list on product section
    on side bar.
    :param request:
    :return:
    rs  rb  ru  r   rJ  )special_datar  zspecial_list.html)r[   r  rS  r    rx  r   ry  r"   rz  r!   r{  r   )r   special_product_listr}  ru  r[  s        r   special_listr}    s     '..77F.2I;;??6"DG(11$7
  4QG'.88 i( 5(11!4 G(11)2E2EFGr  c                 |    t         j                  j                  |      }|r|j                          t	        d      S )a#  
    Deleting a special list. for each special list listed on
    special listing, we have a delete button. clicking on
    the delete button will open a confirm dialog box. if you confirm
    it will delete the special list.
    :param request:
    :param special_list_id:
    :return:
    r  r}  )r[   r  r  r  r   )r   special_list_idr|  s      r   special_list_deleter    s8     '..555I##%N##r   c                 b   g }t        t        |      }| j                  dk(  rt        | j                  | j
                  |      }|j                         r| j                  j                  dd      rd|j                  _	        | j                  j                  dd      rd|j                  _
        | j                  j                  dd      rd|j                  _        |j                          t        d	      S |j                  |j                         nt        |      }|d
d}t!        | d|      S )a0  

    Editing a special list. for each special list listed on
    special listing, we have a edit button. clicking on
    the edit button will open a form. fields are special name and icon,
    standard image, banner image, and products.

    :param request:
    :param special_list_id:
    :return:
    r  r   r  r  r   Nr  r  r}  rJ  )special_formr  zspecial_list_edit.html)r   r[   r   r>   r   r   r  r   r  r!  r"  r#  r  r   r  r  r   )r   r  r  r|  r  r[  s         r   special_list_editr    s     F,[_M~~w||W]]EYZ ==?||"-%)"||"-/3,||"--1*IIKN++ MM$++&(<=#AG'3W==r   c                 P   g }| j                   dk(  ryt        | j                  | j                        }|j	                         r|j                         }t        d      S |j                  |j                         ||dd}t        | d|      S t               }||dd}t        | d|      S )a`  
    Adding a special list.
    On special listing page which we can open by clicking
    on the product section on side bar have a add special list button.
    clicking will open the form enter the special name and icon,
    standard image, banner image, and products.
    on submission create a new special list.
    :param request:
    :return:
    r   r}  rJ  )r  r  r  zspecial_list_add.html)
r   r>   r   r   r  r  r   r  r  r   )r   r  r  specialr[  s        r   special_list_addr  4  s     F~~w||W]];==?iikGN++ MM$++&  $#+G
 '#:GDD  #G '2G<<r   c                       e Zd ZdZd Zy)SendVerificationEmailViewz7
    View to send delivery boy verification email.
    c                 @   |j                   }t        t        |      }|j                  dk(  rddd}t	        |d|      S t        j                  t        |j                        t        j                         t        d      z   d	t        j                  d
      }t        t        dd      }| t        d|g       }d}		 t!        dd| t        j"                  |j$                  gd       |	rdddd}ndddd}t	        |d|      S # t&        $ r}
d}	Y d}
~
,d}
~
ww xY w)a  
        On orders on side bar, we have a menu delivery details. if we click on
        delivery details we have the delivery boys listed. we can add new delivery
        boys. for assigning a order, adding alone not help. for that we have to
        verify the delivery boy. we will send the verification sms to the phone number using this view.

        :param request:
        :param delivery_boy:
        :return:
        r  verifiedzPhone  alreay verified .r   )r   r   phone_verification.html   hours)delivery_boyexpHS256)	algorithmDOMAINzhttp://localhost/zverify-phoner  TzVerify your emailz%Click the link to verify your email: Fr  Nz3Verification link sent to your email. Please check.r   )r   r   r  zInvalid Phone number or email!)r   r   rf   r   r   jwtencoder  r   r   rp  rz   r   
SECRET_KEYgetattrr|   rx   DEFAULT_FROM_EMAILr   r   )selfr   r  r   delivery_boysr[  tokendomainverification_link
email_flagr   s              r   postzSendVerificationEmailView.postg  s/    ||),<H:-"<JG'#<gFF 

C0@0@,A(,,.[dkm[nJno#..'C 8-@A%hw~UG'L&MN 
		#78I7JK++$$%#  "WcdnopG"BaYZ[G g8'BB  	J	s    ,D 	DDDN)__name__
__module____qualname____doc__r  r   r   r   r  r  `  s    =Cr   r  c                       e Zd ZdZd Zy)VerifyEmailViewz
    Delivery details menu on Orders section in the side bar will list the delivery boys.
    this view is to verify with the link.
    we send a link to verify the delivery boy on clicking that link, VerifyEmailView will
    verify.
    c           	      h   	 t        j                  |t        j                  dg      }t	        t
        |d         }d|_        |j                          t        |dddd	d
      S # t         j                  $ r t        |dd	dd	d
      cY S t         j                  $ r t        |dd	dd	d
      cY S w xY w)Nr  )
algorithmsr  r  r  r  r   z#Phone number verified successfully.r   )r   r   r  zVerification link has expired.zInvalid verification link.)r  decoder   r  r   rf   r   r  r   ExpiredSignatureErrorInvalidTokenError)r  r   r  payloadr  s        r   r   zVerifyEmailView.get  s    	jj(;(;	RG-lw~?VWM $.M  '#<@?  
 (( 	'#<;?  
 $$ 	'#<7?  	s   A"A% %$B1#B10B1N)r  r  r  r  r   r   r   r   r  r    s    r   r  rn  c                 &   t         j                  j                  d      j                  | j                  j
                        }|r|d   }nd}t        j                  j                  |      j                  d      }t        |d      }| j                  j                  d      }	 |j                  |      }|d
d}t!        | d|      S # t        t        f$ r |j                  d	      }Y 4t        $ r |j                  |j                        }Y Yw xY w)z
    Delivery details menu on Orders section in the side bar will list the delivery boys.
    It is only available for shop admin.
    each shop have their own delivery boys.
    :param request:
    :return:
    r&  r'  r   r   )r  r  rb  ru  r   rJ  )delivery_boy_dataorders_activezdelivery_boy_list.html)rN   r  rL  r  r   r   rf   rS  r    rx  r   ry  r"   rz  r!   r{  r   )r   rT  r#  r  r}  ru  r[  s          r   delivery_boy_listr    s    \\223DELL%ll// M 1N  # !((//W/=FFWM-+I;;??6"D@!**40
 %2HMG'3W== i( .!**1- @!**9+>+>?@s   %C  D*#DDc                 |    t         j                  j                  |      }|r|j                          t	        d      S )a8  
    Delivery details menu on Orders section in the side bar will list the delivery boys.
    Deleting a delivery boy. for each delivery boy listed on
    delivery boy list, we have a delete button. clicking on
    the delete button will open a confirm dialog box. if you confirm
    it will delete the boy.
    r  r  )rf   r  r  r  r   )r   delivery_boy_idr  s      r   delivery_boy_deleter    s8      ''../.BL'((r   c                    g }t         j                  j                  |      j                         }| j                  dk(  rut        | j                  |      }|j                         r2|j                         }d|_	        |j                          t        d      S |j                  |j                         nt        |      }|dd}t        | d|      S )	a  
    Delivery details menu on Orders section in the side bar will list the delivery boys.
    Editing a delivery boy. for each delivery boy listed, we have a edit button. clicking on
    the edit button will open a form. fields are team member name and phone number,
    email.

    r  r   r  znot verifiedr  rJ  )delivery_boy_formr  zdelivery_boy_edit.html)rf   r  r  r  r   r?   r   r  r  r   r   r  r  r   )r   r  r  r  r  r[  s         r   delivery_boy_editr     s     F''../.BHHJL~~w||lC==?99;L"0L/00 MM$++&5$(8DG'3W==r   c                    g }| j                   dk(  rt        j                  j                  d      j	                  | j
                  j                        }|r|d   }nd}t        | j                        }|j                         r2|j                         }||_        |j                          t        d      S |j                  |j                         ||dd}t        | d	|      S t               }||dd}t        | d	|      S )
a4  
    Adding a delivery boy.
    On delivery boy listing page which we can open by clicking
    on the order section on side bar have a add delivery boy button.
    clicking will open the form enter the fields are team member name and phone number,
    email.
    on submission create a new delivery boy.
    r   r&  r'  r   r   r  rJ  )r  r  r  zdelivery_boy_add.html)r   rN   r  rL  r  r   r   r?   r   r  r  r  r   r  r  r   )r   r  rT  r#  r  r  r[  s          r   delivery_boy_addr    s     F~~667HIPP"),,"3"3 Q 5$Q'GGw||,==?99;L 'L/00MM$++& %)!)G
 '#:GDD  !!G '2G<<r   )parserc                 V   	 | j                   j                  d      }| j                   j                  dd      }t        j                  j	                  | j
                  j                        }|r|d   nd}g }| j                  j                  d      }| j                  j                  d      }| j                  dk(  rt        j                  j	                  |	      j                         }	t        j                  j	                  |
      j                         }
t        | j                  |	      }|j                         rP|j                  d      }|
|_        d|_        |j                          |
j$                  dk(  rd|
_        |
j                          d}d|
j&                  j(                   d|
j*                   d}t-        |
j&                  j.                  |
|||
j&                         t1        |||
j&                  j2                  gd       d}|
j&                  j4                  dk(  rMt7        j8                  |
j&                  j:                  |
j*                  |
j&                  j(                  |gd       t=        d||
j*                  d      S |j?                  |j@                         t=        d|j@                  d      S | j                   j                  dd      }| j                   j                  dd      }| j                   j                  dd      }| j                   j                  d d      }| jB                  j                  d!d"      }| j                   j                  dd      }|s|}| j                   j                  dd      }|s|}tD        j                  jG                  d#      jI                  tK        d$tL        j                  jO                         %            }tP        j                  jO                         }|r4t        j                  j	                  |d&d'g(      jS                  d)*      }n0t        j                  j	                  d+,      jS                  d)*      }|jG                  d-d.d/      jI                  tK        d0|%      tK        d1|%            jU                  d2      }|d3k(  r|j	                  d4*      }|rp|d5k(  r)|j	                  tW        d67      tW        d87      z        }nB|j	                  tW        d97      tW        d:7      z  tW        d;7      z  tW        d<7      z        }| jX                  j                  d=      d>k(  r|rQ|j	                  tW        |?      tW        |@      z  tW        |A      z  tW        |B      z        jU                  d2      }|r)	 t[        j\                  |dC      }|j	                  |D      }|r|j	                  |E      }|rd|rb|dFk(  r,|d-k(  r|jU                  |dGz         }nC|jU                  |      }n1|d-k(  r|jU                  dH|z   dGz         }n|jU                  dH|z         }ta        |dI      }| j                   j                  dJ      }	 |jc                  |      }|D ]@  }
g }|
jl                  jO                         D ]  } d"}!	 | jn                  jq                         r| jn                  jO                         d   nd"}!| jt                  tw        | d#d"      r-tw        | jx                  dLd"      r| jx                  jz                  ndtw        | jx                  dMd      tw        | jx                  dNd      | j|                  | j~                  |!r|!j                  nd|!r#tw        |!dOd"      r|!j                  j                  nd"dP}"|j?                  |"        t        |
dQ|       C t               }	 t        j                  j	                  dR|S      }#|D 
cg c]   }
|
j$                  d4k(  sdT|
j*                  i" }$}
||||||#dU||r|nd|$dV
}%| jX                  j                  d=      d>k(  rD| jB                  j                  d!      }&d"}|&r@	 t        j                  |&      }t        j                  |      rt        j                  |      }d}(|r	 |rCt        j                  j	                  ||d&d'gX      jS                  d)*      j                         }(n?t        j                  j	                  d+|Y      jS                  d)*      j                         }(t        t        j                               | jB                  d!<   ||#|||$d[})t        d\|)      }*t        d]d0|i      }+t=        |*|+|(dd^      S |d3k(  rt        | d_|%      S t        | d`|%      S # t^        $ r Y w xY w# td        tf        f$ r |jc                  dK      }Y cth        $ r |jc                  |jj                        }Y w xY w# tr        $ r d"}!Y 9w xY w# tr        $ r g }#Y ;w xY wc c}
w # tr        $ r}'t        dW|'        Y d"}'~'d"}'~'ww xY w# tr        $ r}'t        dZ|'        Y d"}'~'Cd"}'~'ww xY w# t        $ r g dUda}%t        | d`|%      cY S w xY w)bza
    List orders for shop admin / PU admin with product data and custom images (if present).
    platformrb  r   r'  r   rI  
sort_orderr   r   r  r  Fr*  Courier Deliveryr2  
Despatchedu   Your Order is On the Way 🚚Hi z",

                    Your order u    has been dispatched and is on its way to your location.

                    We’ll notify you once it is delivered.

                    Thank you for choosing Navya Bakers!out_for_delivery_generalTr   r   r  r   order_idOrderIDfailrW  r  r  r  check_new_orderslast_order_checkNr  customizations)querysetr`  Pick Up)r  order_type__inr  r.  rf  rg  r   drop_addressr  
order_dataadditionaldetailsr  z
new ordersr-  webWebplatform__iexactOtherIOSAndriodAndroidFlutter_web_appr  r  order_ID__icontainsorder_type__icontainsorder_status__icontains user_uuid__first_name__icontainsr  r_  r  asc__first_name-rb  ru  r   rQ  rS  rT  custom_image)product_namerQ  rS  rT  quantityrX  custom_notecustom_image_urlrT  r  r   r  r   rJ  )
courier_formr  r  r   r  ongoing_ordersr  r  rb  rK  zError parsing date: )r  created_date__gtr  )rb  r  zError counting new orders: )r  r  r  rb  rK  zorder_list_page.htmlorder_list_pagination.html)r  r  new_orders_countr   znew_orders.htmlzorder_list.htmlr  r  )Qrx  r   rN   r  r  r   r   COOKIESr   rk   r  rj   rA   r   r  r  r   delivery_typer.  r   r  r   r   r   r   r   opt_inr   r   r  r   r  r  rM  ri   select_relatedrL  r   rh   r  rb   r   rS  r   r  r   r  
ValueErrorr    ry  r"   rz  r!   r{  r  r  r  r   r  r  r  rQ  r  rX  r  r  r  setattrrf   r  parser   is_naive
make_awarer   rN  r  rp  r   r   
IndexError),r   r  r  rb  shop_qsr#  r  rI  r  order_deliveryr   r  delivery_datar   r   	temp_namer  
order_dater.  r  r  sort_order_datafield_name_dataorderproducts_qsadditionaldetails_qsbase_qs
order_listr  r}  ru  ordersrT  opcustomizationitemr  rK  r[  last_order_check_strr   r  ajax_contextr  r  s,                                               r   r  r  \  s5
   @;;;??:.[[__\26
,,%%GLL<M<M%N ''!*R__((6
__((6
>>V#*22999IOOQNNN))X)6<<>E%gll^LD}} $			 7&+#.@+""$%%7)5E&JJL;E&)%//*D*D)E F  %/ 09#<K %U__%>%>uk[`[j[jke[5??3H3H2I5Q :I--5-99@\@\^c^l^lnsn}n}  oI  oI  KT  @U  ab  d#' ($~~%   dkk*#v$MNN {{x4[[__^R8
{{'7<";;??+=uE"??../A4H!++//,;(O!++//,;(O )00??FWW%0I0Q0Q0U0U0WX

  188<<>nn++" .	: ,  g;g/ 
 nn++1 , g;g/  ++KVgg\,<=(3GH
 (?
# 	 %#***DJ 5 '..u-70KK
 (..u-y12y12 '89:
 ??126FF'..,7L9:l;< |DE
 (?+  %-%6%6z:%NN!+!2!2n!2!UJ '..l.K
?"e+&+5%/%8%8>9Y%Z
%/%8%8%I
&+5%/%8%89NQ_9_%`
%/%8%89N%O
 j!,	{{v&	=''-F EE&&**, $)BDBSBSBZBZB\B$5$5$9$9$;A$>bfM
 %'OO3:2ud3KPWXZX^X^`jlpPqwy$+BFFNB$G '
B ? "XX@M=#<#<SUJW\cdq  tB  DH  ]I(B(B(F(F  OS	 T"# -$ E7E*) . "#	 )1188
QX8YN EKrF5uOaOaepOp:u~~6Fr ! (*,% (2*,
 ??126FF#*??#6#67I#J   $#6'-||4H'I$(()9:+3+>+>?O+P(
  !=+1>>+@+@'.-=,:I+F ,A , "'{';EEG	 ) ,2>>+@+@'=-= ,A , "'{';EEG ) 36hlln2EGOO./ %"0$("0L %%;\JE)*FW]H^_J($4#	!   %'#4g>>g0'::G " . !), 	+''*F 	=''	(;(;<F	= ! )$(M)(  	 N	  s6 ! 604556$ ! =7s;<<=<  ;%
 g0'::;s2  Jj
 2j
 K)j
 /(f: B!j
 9g
 
(j
 39h,C.j
 !h) <j
  h;h;%Aj
 7?i  6j
 ;Bi% ?Aj
 j
 -j
 :	gj
 gj
 
 h*j
 -#hj
 hj
 h&"j
 %h&&j
 )h84j
 7h88j
  	i"	ij
 i""j
 %	j.j<j
 jj
 
j('j(c                    | j                   j                  d      }g }d}t        j                  j	                  |      j                         }|r(t        |      }|j                  j                  dk(  rod}nlt        j                  j	                  |      j                         }t        j                  j                  d|	      }t        |      }|j                  dk(  rd}t        d
|||d      }t        d|d      S )Ncsrfmiddlewaretokenfalser  r  r2  truer  r  )r  r   zcourier_edit_form.html)r  r  button_statusTr  r  )r   r   rk   r  r  r  rA   r   r.  rj   r  r   r   )	r   r  csrfr  r  r  r  r   r  s	            r   courier_details_editr  j  s     <<12DFM"**111AGGIN!>:,,>"M %%8%4::<&..55DV^c5d!>:/"M#$<59xbo$prL D,?@@r   c                       e Zd Zd Zd Zy)ShipperAutocompletec                     |j                    S r   )team_member_name)r  r  s     r   get_result_labelz$ShipperAutocomplete.get_result_label  s    ''()r   c                     t         j                  j                  dd      }| j                  r|j                  | j                        }|S )Nr  not assigned)r   delivery_boy_status)team_member_name__istartswith)rf   r  r  r  )r  r  s     r   get_querysetz ShipperAutocomplete.get_queryset  sE    %--44J\j4k66+22QUQWQW2XNr   N)r  r  r  r  r  r   r   r   r  r    s    *r   r  c                    	 |r| j                   j                  }| j                   j                  }| j                  j                  }| j                  j                  }d| d| d| d| }|||| j                  d}	t        j                  ddddd	d
dddd
      }	|	S y # t        $ r"}
t        j                  d|
        Y d }
~
y d }
~
ww xY w)N https://www.google.com/maps/dir/,r  )pick_up_addressr  google_place_linkr   z+919876543210RaviORD12345zNavya Restaurant, Coimbatorez12, Gandhipuram, CoimbatoreAkash
9876543210450zCash on Deliveryz6:00 PM - 7:00 PM)
r   delivery_partner_namer  pickup_locationdelivery_addresscustomer_namecustomer_phoneorder_amountpayment_modedelivery_slotr   )
r  r  r  r  r   r   r   r   r   r   )r   r  r  r  	START_LAT	START_LNGDEST_LATDEST_LNGr   rs  r   s              r   WhatsappMessageSendr2    s    **33I**44I''00H''11H"B9+QykYZ[cZddefneo p'6):u~~WD9EE 22#%D K A!EFs   BB 	C&CCc                    	 g }t         j                  j                  | d      }|D ]B  }	 |j                  j                  }|j                  j
                  }	|j                  ||	f       D |D cg c]  }|d    d|d     }
}dj                  |
      }d|j                   d}t        |j                  j                        }t        |j                  j
                        }	d	}| d
| d|	 d| d|	 d| d}d|j                   d| d| d| d	}t        j                  ||t        j                  |j                  gdgd      }|S #  Y )xY wc c}w # t         $ r"}t#        j$                  d|        Y d }~y d }~ww xY w)NDelivery Assigned)r  r.  r   r  r   |zOrder z Assinged to youz&https://www.google.com/maps/dir/?api=1z&origin=z&destination=z&waypoints=z&travelmode=drivingz The Order no: z5 assigned to you by NavyaBakeShop.
Pickup Location : z 
Delivery Location : z%

Please click link to track order : z!

Navya Bakers
                  Fr   r   z2Failed to Send Email While Assigning Delivery Boy:)rj   r  r  r  r  r  r  r   r   r  r  r   r   r   ry   r   r   r   r   )shipper_uuidr   r  r  r  location_listr  rZ  r  r  map_linkmap_link_joinr   basemap_link_to_sendr   r   r   s                     r   ShipperEmailSendr<    s   %&&LOb&cA>>22NN44	$$h	%:;	  /<<mqtfAadV$m<* 5>>**:;u''001((223	7"V8H:QykxjXYZcYddop}o~  R  S%enn%55kl{k|  }T  Ua  Tb  bI  JZ  I[ [  #..WgxG_G_bnbtbtauw|4}  JK  L
-<.  J1#NOs;   (E ?E*E 0ECE EE 	F!E>>Fc                 `   | j                   dk7  r#t        j                  d       t        ddd      S | j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d	      }| j                  j                  d
      }t        j                  j                  |      j                         }t        j                  d| d|        	 |dk(  rt        j                  d|j                          	 |j                  dk7  rt        ddd      S |j                  st        ddd      S t        j                  j                  |      j                         }|r|j                  rt        ddd      S |j                   r|j                   j"                  dk(  rd}nd}t%        ||      }	|	j                  d      st        dd|	j                  d      d      S t        j                  j'                  |d|	j                  d      |	j                  d      dd       d |_        |j+                          t        j                  d!|	j                  d              t        d"d#|	j                  d      |	j                  d      |||d$      S t        j                  d'|        t2        j                  j                  |(      j                         }|s&t        j.                  d)|        t        dd*d      S t        j                  d+|j4                          t        j                  d,|        |s&t        j.                  d-|        t        dd.d      S t        j                  d/|j                   d0|j(                          |j(                  d1v r1t        j                  d2|j                   d3       t        dd4d      S |j(                  d5k(  r1t        j                  d2|j                   d6       t        dd7d      S |j(                  d8k(  r1t        j                  d2|j                   d9       t        dd:d      S |j(                  d;k(  r1t        j                  d2|j                   d<       t        dd=d      S t        j                  d>|j                   d?|j4                          d;|_        t7        j8                         |_        ||_        |j+                          d@|j>                   dA}t        j                  dB|        t@        j                  j'                  ||dC|j                  iD       t        j                  dE       dF|_!        |j+                          t        j                  dG       t        j                  dH       dI}|j                  rp|j                  }|jD                   dJ|jF                   dJ|jH                   dJ|jJ                   dJ|jL                   dK|jN                   }t        j                  dL       dI}|jP                  rV|jP                  }|jR                   dM|jT                   dN|jV                   dJ|jJ                   }t        j                  dO       dI}|j                  r|jP                  rsdP|j                  jX                   dQ|j                  jZ                   dA|jP                  jX                   dQ|jP                  jZ                   }t        j                  dR       t        j                  dS       dI}dI}|j\                  r|j^                  r	 ta        jb                  |j\                   dT|j^                   dU      }t        j                  dV       |rI|tg        dZ[      z
  }|ji                  d\      }|ji                  d]      }t        j                  d^|        |j                   r|j                   j"                  nd_}t        j                  d`|        t        j                  da       	 tk        jl                  |jn                  |j4                  |j                  ||jp                  jr                  |jp                  jn                  ||t1        |jt                        ||||gdbc      }t        j                  dd|jv                          t        j                  dg       	 t}        jl                  |j4                  |j                  ||||jp                  jr                  |jp                  jn                  t1        |jt                        ||||j~                  |gdbc      }t        j                  dh|jv                          t        j                  dj       	 dk}dl}t        j                  dm       t        |jp                  j                  ||||jp                         t        j                  dn       t        j                  do       t        |||jp                  j~                  gd       t        j                  dp       t               }t        j                  j'                  ||ddq       t        }|jp                  j                  rt        j                  dr       t        jl                  |jp                  jn                  |j                  |jp                  jr                  dsgdbc      }t        jl                  |jp                  jn                  |ta        j8                         ji                  dt      dugdbc      }t        j                  dv|jv                          t        j                  dw|jv                          nt        j                  dx       t        j                  dz|j                          d"|||d{}!t        j                  d||!        t        |!      S # t,        $ r9}
t        j.                  d%t1        |
              t        dd&d      cY d }
~
S d }
~
ww xY w# td        $ rz 	 ta        jb                  |j\                   dT|j^                   dW      }t        j                  dX       n0# td        $ r$}
t        j.                  dY|
        d }Y d }
~
nd }
~
ww xY wY w xY w# t,        $ r8}t        j.                  de| dfty        jz                                 Y d }~d }~ww xY w# t,        $ r8}t        j.                  di| dfty        jz                                 Y d }~d }~ww xY w# t,        $ r8} t        j.                  dy|  dfty        jz                                 Y d } ~ d } ~ ww xY w# t,        $ r}
t        j.                  d}t        |
      j                   d~t1        |
       dty        jz                                 dd|||d}"t        j.                  d|"        t        |"      cY d }
~
S d }
~
ww xY w)Nr   u*   ❌ Invalid request method - expected POSTFrX  )r  r   r6  
order_uuidru  r  r  r  u#   📦 save_shipper called | shipper=z, order=	DELHIVERYu,   🚚 Delhivery assignment started for order rf  z/Delhivery only allowed for long distance orderszDelivery address missingr  zShipment already createdCODPrepaidr  zShipment creation failedr   )r  r   r   awbtracking_urlCREATED)r  
awb_numberrC  r   )r   rJ  r  u   ✅ Delhivery AWB: TzAssigned to Delhivery)r  r   rB  rC  ru  r  r  u   ❌ Delhivery error: zDelhivery integration failedu   🔍 Validating shipper: r  u   ❌ Invalid shipper UUID: zInvalid shipperu   ✅ Shipper validated: u   🔍 Validating order: u   ❌ Invalid order UUID: zInvalid orderu   ✅ Order found: z
, Status: )r1  r  u   ⚠️ Order z not confirmed yetzOrder not confirmedr/  z already deliveredzOrder already deliveredr  z has failedzOrder failedr4  z already assignedzOrder already assignedu   🚀 Assigning order z to z:https://navyasvpstaging.test4u.in/orders/delivery-confirm/r  u.   ✅ Order status updated to Delivery Assigned r  )r  r   rJ  u(   ✅ OrderDelivery record created/updatedassignedu(   ✅ Shipper status updated to 'assigned'u!   🏠 Building address informationr   z,  - u   ✅ Drop address builtr  z), u   ✅ Pickup address builtr  r  u   ✅ Google Maps link generatedu,   ⏰ Processing delivery slot and pickup timer   z%Y-%m-%d %H:%M:%Su(   ✅ Parsed delivery datetime (DB format)z%Y-%m-%d %I:%M %pu(   ✅ Parsed delivery datetime (UI format)u   ❌ Failed to parse datetime: r   r  z%d %b %Y | %I:%M %pz%I:%M %p (%d-%m-%Y)u   ✅ Delivery slot: zN/Au   💳 Payment mode: u.   📱 Attempting to queue WhatsApp message taskr   r   u"   ✅ WhatsApp task queued with ID: u    ❌ WhatsApp task queue failed: 
u#   📧 Attempting to queue email tasku   ✅ Email task queued with ID: u   ❌ Email task queue failed: u#   🔔 Sending customer notificationszOrder AssignedzFYour order has been assigned. Delivery partner will reach you shortly!u   📲 Sending FCM notificationu   ✅ FCM notification sentu   📧 Sending customer emailu   ✅ Customer email sent)otpis_verifiedu&   📱 Queuing customer WhatsApp messagedelivery_assining_genz%d %b %Ydelivery_otpu&   ✅ Customer WhatsApp queued with ID: u*   ✅ Delivery OTP WhatsApp queued with ID: u3   ℹ️ Customer opted out of WhatsApp notificationsu4   ⚠️ Customer notification failed (non-blocking): u3   🎉 save_shipper COMPLETED SUCCESSFULLY for order )r  ru  r  r  u!   📤 Returning success response: uK   ❌❌❌ save_shipper CRITICAL EXCEPTION CAUGHT ❌❌❌
Exception Type: z
Exception Message: z
Full Traceback:
r  )r  r   ru  r  r  u   📤 Returning error response: )Jr   r   r  r   r   r   rj   r  r  r  r   r   rb  r  re   rE  r,  	mode_namer   update_or_creater.  r  r   r   r  rf   r  r   rp  order_assigntimer  r   rk   r  r   house_number_or_namer  r  r  r  r  r  r  r  r  r  delivery_slot_datedelivery_slot_timer   r  r  rz   strftimer   r   r  r   r  r  r<  	traceback
format_excr   r   r   r   r   generate_delivery_otprg   r   r  r   typer  )#r   r6  r>  ru  r  r  r   existingpayment_typeresponser   shipperdeliverylinkr  r  r  r  r   r-  pickup_timedelivery_datetimepickup_datetimer,  whatsapp_taskwhatsapp_err
email_task	email_errr   r   rI  customer_whatsapp_taskrL  
notify_errresponse_dataerror_responses#                                      r   save_shipperrh    s   ~~DE:RSTT<<##N3L!!,/J<<F#D\\h'F<<##N3LNN!!z!288:ELL6|nHZLYZo, ;&LLGGWXYL ##'=='#(#T)   ))'#(#=)   %,,33%3@FFH 3 3'#(#=)   %%%*<*<*F*F%*O#(L#,L 5ULI||I.'#(#=!)g!6)   !!22)4&.ll5&9(0^(D"+	 3  &2"

28<<3F2GHI##6#<<.$,LL$@ $$0%  & 	0?@&&--->DDFMM6|nEFE>O PQQ.w/G/G.HIJ
 	.zl;<MM4ZLABEo NOO((8
5CUCUBVWX!88OOmENN+;;MNOE>S TUU,OOmENN+;;MNOE>W XYY)OOmENN+;;GHEn MNN!44OOmENN+;;LMNE>V WXX
 	,U^^,<DAYAY@Z[\0!))

STYT^T^S__`aEl^TU.. %u'7'78 	/ 	

 	?A&0#?A
 	89""A66("Q334Bqxxj66("Q001QZZLB  LL13  A;;-r!++c!//1B"QVVHM  LL35%"2"22%%../q1C1C1M1M0Na##,,-Qu/?/?/I/I.JL 
 LL9;
 	CD##(@(@-$,$5$5//0%2J2J1KL'%! GI !"3ia6H"H 1 : :;P Q-667LM2=/BC -2,>,>E((E 	 	*<.9:
 	EF	BNN((,,NN#OO..OO00 %))* !   #M& LL=m>N>N=OPQ 	:;	3??,,NN# %OO..OO00))* !MM  !J$ LL::==/JK 	:;=	$E; 
 LL89)) LL45LL67&&'	 LL23')C00!$U; 1  E%%EF)>)J)J4422/	  *&  5@@44 //
;&	    EF\F_F_E`abI,//IZ[\RS 	J5>>JZ[\ (	
 	8HIM**]   5c!fX>?#$=%  `  	--(0(9(9 334Ae6N6N5OP+)% LL#KM! -MM$B1#"FG(,%-	-^  	MM2<.9CWCWCYBZ[ 	4  	MM/	{"Y=Q=Q=S<TU 	D  	MMF,b!5!5!7 8: 	*  ,#Aw//0 1""%a& *  ) 4 4 679	
 -(
 	77GHIN++#,se  (v# -p 
p $A	p .A$p B2p A-v# 4A!v# A-v# ?v# ?v# ?v# J2v# 7Aq ;Bv# Bs 7v# Bt ,v# G>u  Av# 	q.q	qv# 	qv# 	sAr s 	s)ssssv# sv# 	t -tv# tv# 	u$-uv# uv# 	v (-vv# v  v# #	x-,A6x("x-(x-c                  @    t        t        j                  dd            S )Ni i?B )r  randomrandintr   r   r   rV  rV  _  s    v~~ff-..r   c           	      t
   | j                   dk(  r| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }	 | j                  j                  d      }| j                  j                  d      j                         }t        j
                  j                  |      j                         }||_        |j                          |dk(  r	 t        j                  |j                  |j                        }|j                         t        j                         j                         z
  }|j!                         }	t#        |	d	      d
   }
|
d
k  r1d|_        t        j                         |_        |j                          nk|
dk  r6|
d
kD  r1d|_        t        j                         |_        |j                          n0d|_        t        j                         |_        |j                          |dk(  s|dk(  rd|_        t        j                         |_        |j                          	 |j(                  }t*        j
                  j                  |      j                         }|j,                  dk(  rd|_        |j                          	 t/                |dk(  r_d}d|j0                   d}|j2                  dk7  r8t5        |j6                  j8                  ||||j6                         t;        d|||d      S |dk(  r*d}d|j6                  j<                   d|j0                   d}n|d k(  s|d!k(  red"}|j2                  dk(  r*d#}d|j6                  j<                   d$|j0                   d%}nd&}d|j6                  j<                   d$|j0                   d'}n]|dk(  r*d(}d|j6                  j<                   d)|j0                   d*}n.|dk(  r)d+}d|j6                  j<                   d|j0                   d,}|}t5        |j6                  j8                  |||j6                         t?        |||j6                  j@                  gd-       |j6                  jB                  d.k(  rMtE        jF                  |j6                  jH                  |j0                  |j6                  j<                  gd/0       t;        d|||d      S y #  t        j                         }Y xY w#  Y TxY w#  Y QxY w# tJ        $ r$}t;        d1tM        |      |||d2      cY d }~S d }~ww xY w)3Nr   ru  r  r  r>  selected_valuer  r2    r   Redr   OrangeWhiter  r  Dark Redr  rF  r  order_ready_for_pickupzYour order zh is ready for pickup. Please visit our shop to collect it at your convenience.Thank you for choosing us!r  r  r   ru  r  r  r4  delivery_assigned_generalr  z,

            Your order z has been assigned to a delivery partner.

            It will be delivered to your doorstep shortly.

            Thank you for choosing Navya Bakers!r/  	Collecteddelivered_generalzOrder Collectedz,

                Your order zx has been successfully collected.

                We hope you enjoy your purchase. Thank you for choosing Navya Bakers!zOrder Deliveredz has been successfully delivered.

                We hope you enjoyed your experience. Your feedback means a lot to us and helps us improve!

                Thank you for choosing Navya Bakers!deliveryfailedcase_generalu;   ,

            We’re sorry to inform you that your order z could not be completed.

            If you need any assistance or would like to place the order again, please feel free to contact our support team.

            Thank you for your understanding.custom_delivery_failedcasez has been cancelled successfully.

            If this was not intended or you need any help, please contact our support team.

            We hope to serve you again soon!FTr   r   r  r   r   ru  r  r  )'r   r   r   striprj   r  r  r  r.  r  r   combinerQ  rR  rp  
astimezonetotal_secondsdivmodcolor_statuscolor_status_updation_timer  rf   r  r{   r   rb  r   r   r   r   r  r   r   r  r   r   r  r   r  )r   ru  r  r  r>  r   r   combined_datetimetime_dif_with_del_slotduration_in_s_data	hours_delr6  r[  r  r   r   r   s                    r   save_order_statusr  d  s   ~~||'!!(+||''7q	q )),7J\\%%&67==?FNN))z):@@BE!'EJJL'7(0(8(89Q9QSXSkSk(l% *;)E)E)G(,,.JcJcJe)e&%;%I%I%K""#5t<Q?	q=).E&7?||~E4JJL]y1})1E&7?||~E4JJL)0E&7?||~E4JJL!V{%:%/"3;<<>0

#(#5#5L*2299\9JPPRG22j@6D3"$ '4	 +ENN+;  <d  e##y0$U__%>%>v{\a\k\kl'9dV\nz({||..7	"%eoo&@&@%A B' (14 ;&&K*?/	##y0.F&)%//*D*D)E F!NN+ ,V#YK /F&)%//*D*D)E F!NN+ ,5#8K 8#8	"%eoo&@&@%A B99>8H I.1 ;&8	"%eoo&@&@%A B' (-0 EU__66ukSXSbSbce[5??+@+@*A5I%%-%118T8TV[VdVdejetetee  AJ  8K  VW  Y9dffr stte  7(0%8B  	q!#a&$&bnoq q	qsk   #BT
 3*S D-T
 A S: ,
T 6A#T
 GT
 S74T
 :S?<T
 TT
 
	T7T2,T72T7c                    | j                   dk(  r	 | j                  j                  d      }| j                  j                  d      }t        j                  j                  |      j                         }|st        ddd      S |j                  dk(  rt        dd	d      S ||_        |j                          t        d
di      S y # t        $ r!}t        dt        |      d      cY d }~S d }~ww xY w)Nr   r>  rm  r  r  Order not foundr   	Contactedz;Order status cannot be changed after being set to Contactedr   r  )r   r   r   rj   r  r  r  r   r.  r  r   r  )r   r>  r   r   r   s        r   save_customorder_statusr    s    ~~	C )),7J\\%%&67FNN))z):@@BE#v>O$PQQ !![0#%.kln n "(EJJL9 566%  &  	C6#a& ABB	Cs*   A3C C "#C 	C1C,&C1,C1c                    | j                   dk7  rt        ddd      S | j                  j                  d      }t        j
                  j                  |      j                         }|st        ddd	      S | j                  j                  d
      }| j                  j                  d      }| j                  j                  d      }d }d |_        d |_	        |dk(  r0d}| j                  j                  d      }|rt        |      |_        n|dk(  r~d}| j                  j                  d      }| j                  j                  d      }	|rt        |      |_        |	r2	 t        j
                  j                  |	      }
|
j                  |_	        |j                          t        j
                  j!                  |      \  }}||_        ||_        ||_        |j                          t)        | j                  |      }|j+                         r|j                          t        ddd      S t        d|j,                  d      S # t        j                  $ r
 d |_	        Y w xY w)Nr   r   rX  rY  r   r   r  r  r   r  quantity_unitr  pickupr  pickup_datehome_deliveryHome Deliverydelivery_date	time_slotrK  r  r  r  zOrder updated successfullyrW  )r   r   r   r   rj   r  r  r  rQ  rR  r   rm   
start_timerr  r  rd   r[  r  r  r  rH   r  r  )r   r>  r   r  r  r  del_typer  r  slot_idslotcustom_trackcreatedr  s                 r   save_customorder_datar    s   ~~w;STUU !!*-JNN!!:!6<<>Ev6GHII ||
+HLL$$_5MLL$$_5MH  $E#E  ll&&}5'1+'>E$	/	)"((9,,"";/'1-'@E$0#++//7/;+/??(
 
JJL 077EEuEUL'$L!.L!)L 7<<%8D}}		y=YZ[[w$++FGG-  ,, 0+/(0s   1I I"!I"c           	      t   | j                   dk(  r| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }	 t        j                  j                  |      j                         }|j                  dk(  rZd|_        |j                          d}d	}d
}t        |j                  j                  ||||j                         t        d|||d      S t        d|||d      S y # t        $ r$}	t        dt        |	      |||d      cY d }	~	S d }	~	ww xY w)Nr   ru  r  r  r  r-  r1  zOrder Viewedz:Your Order Viewed by the Merchant and will be packed soon!order_viewedr  rt  r  rz  )r   r   r   rj   r  r  r  r.  r  r   r   r   r   r   r  )
r   r   ru  r  r  r   r   r   r  r   s
             r   order_status_update_to_viewedr  ;  s)   ~~||'!!(+||''7	qNN))7);AACE!![0%-"

 'Z)	 !:!:E5+W\WfWfg $y$RXjv$wxx646co pqq7  <  	q!#a&$&bnoq q	qs%   "BD
 9D
 
	D7D2,D72D7c                 l   t         j                  j                  d      }t        |d      }| j                  j                  d      }	 |j                  |      }|dd}t        | d|      S # t        t        f$ r |j                  d      }Y 4t        $ r |j                  |j                        }Y Yw xY w)a?  
    Coupons  menu on Promotions section in the side bar will list the coupons.
    It is only available for super admin and nbc admin.
    nbc admin can create, edit and delete coupons.
    this defined coupons will list on apps.
    if they are eligible they can use the coupons.
    :param request:
    :return:
    rs  rb  ru  r   rJ  )coupon_datapromotion_activezcoupon_list.html)rl   r  rS  r    rx  r   ry  r"   rz  r!   r{  r   )r   couponr}  ru  coupon_list_datar[  s         r   coupon_listr  _  s     __%%m4F&!$I;;??6"DC$--d3
 /HMG'-w77 i( 1$--a0 C$--i.A.ABCr  c                     t         j                  j                  |      }|rbt        j                  j                  |      }|r0t	        j
                  | t        j                  d       t        d      S |j                          t        d      S )aW  
     Coupons  menu on Promotions section in the side bar will list the coupons.
    Deleting a coupon. for each coupon listed on
    coupon list, we have a delete button. clicking on
    the delete button will open a confirm dialog box. if you confirm
    it will delete the coupon.
    :param request:
    :param coupon_id:
    :return:
    r  )Couponz/Your coupon used in ads! couldn't delete couponlist_coupons)	rl   r  r  ro   r   r  WARNINGr  r   )r   	coupon_idr  r  s       r   coupon_deleter  w  sy     __##y#1F kk  	 2  (*:*:!RT
 N## MMON##r   c                    g }t         j                  j                  |      j                         }| j                  dk(  rt| j
                  j                         }d|j                  dd      v r7|j                  dd      }|r|j                  d       |j                  d|       d|j                  dd      v r7|j                  dd      }|r|j                  d       |j                  d|       d|j                  dd      v r7|j                  dd      }|r|j                  d       |j                  d|       d|j                  dd      v r7|j                  dd      }|r|j                  d       |j                  d|       |j                  d	d      }		 |	d
k(  r
|d= |d= |d= n|	dk(  r|d= |d= n|	dk(  r|d= t        || j                  |      }
|
j                         r| j
                  j                  dd      rd|
j                  _        | j
                  j                  d      dk(  r#d|
j                  _        d|
j                  _        nd|
j                  _        	 |
j'                          t5        ddi      S |j9                  |
j6                         t5        d|
j6                  d      S t        |      }
||
|j:                  d|j<                  |j>                  d}tA        | d|      S #  Y =xY w# t(        $ rg}|j*                  }|
j-                  t/        |j1                               d   |j2                  d          t5        d|
j6                  d      cY d}~S d}~ww xY w)a  
    Coupons  menu on Promotions section in the side bar will list the coupons.
    Editing a coupon. for each coupon listed, we have a edit button. clicking on
    the edit button will open a form. fields are coupon_name and coupon_code,
    coupon_description, icon, coupon_on, applicable_category, applicable_subcategory,
    applicable_product, applicable_sku, minimum_total_bill_amount, coupon_type,
    discount_amount, no_of_users, validity_start_date, validity_end_date, terms_and_conditions.
    validations listed on the forms. applicable_category, applicable_subcategory,
    applicable_product, applicable_sku are dynamic fields based on coupon_on field.
    coupon maybe amount based or percentage based. the field used to indicate is
    coupon_type.

    Imp: image refresh and showing logic is handled via javascript.It is field wise and based on id.

    :param request:
    :param coupon_id:
    :return:
    r  r   r   ApplicableCategoryNApplicableSubCategoryApplicableProductApplicableSkuCouponOnCategorySubCategoryr  r  r6  
CouponTypeamountr   Fr@  r  TrJ  )r  coupon_formr  r  r  coupon_typezcoupon_edit.html)!rl   r  r  r  r   r   r  rB  rw  setlistr   rB   r   r  r  IconDiscountPercentageMaxDiscountAmountForPercentageDiscountAmountr  r   message_dict	add_errorrO  r  r   r   r  r  r   r  r  r   )r   r  r  r  	post_datar  r$  r  rg  	coupon_onr  r   
error_datar[  s                 r   coupon_editr    s6   * F__##y#1779F~~LL%%'	
 ""#7>>%--.BDIM$$R(2MB""#:DAA ) 1 12I4 P !((,57HI""#6==$,,-@$GL##B'1<@""?D99 (($?H#ox8MM*d3	
	J&6723/m+23/i'/ )W]]VD==?||,%)"||-9370?C</3,O		  D 122 MM$++&ET[[ IJJ6*YY$((G '-w77K	  $ O^^
tJOO$56q91::a=I#$MNNOs+   #K5 %K= 5K:=	M-AM("M-(M-c                 B   g }| j                   dk(  rr| j                  j                         }d|j                  dd      v r7|j                  dd      }|r|j	                  d       |j                  d|       d|j                  dd      v r7|j                  dd      }|r|j	                  d       |j                  d|       d|j                  dd      v r7|j                  dd      }|r|j	                  d       |j                  d|       d|j                  dd      v r7|j                  dd      }|r|j	                  d       |j                  d|       |j                  dd      }	 |d	k(  r
|d= |d= |d= n|d
k(  r|d= |d= n|dk(  r|d= t        || j                        }|j                         r| j                  j                  dd      rd|j                  _        | j                  j                  d      dk(  r#d|j                  _        d|j                  _        nd|j                  _        	 |j                          t-        ddi      S |j1                  |j.                         t-        d|j.                  d      S t               }g }||dd	dd}t3        | d|      S #  Y xY w# t         $ rg}	|	j"                  }
|j%                  t'        |
j)                               d   |	j*                  d          t-        d|j.                  d      cY d}	~	S d}	~	ww xY w)as  
    Coupons  menu on Promotions section in the side bar will list the coupons.
    adding a coupon.We have a add coupon button. clicking on
    the add button will open a form. fields are coupon_name and coupon_code,
    coupon_description, icon, coupon_on, applicable_category, applicable_subcategory,
    applicable_product, applicable_sku, minimum_total_bill_amount, coupon_type,
    discount_amount, no_of_users, validity_start_date, validity_end_date, terms_and_conditions.
    validations listed on the forms. applicable_category, applicable_subcategory,
    applicable_product, applicable_sku are dynamic fields based on coupon_on field.
    coupon maybe amount based or percentage based. the field used to indicate is
    coupon_type.

    Imp: image refresh and showing logic is handled via javascript.It is field wise and based on id.
    :param request:
    :return:
    r   r   r  Nr  r  r  r  r  r  r  r6  r  r  r   Fr@  r  TrJ  )r  r  r  r  r  zcoupon_add.html)r   r   r  rB  rw  r  r   rB   r   r  r  r  r  r  r  r  r   r  r  rO  r  r   r   r  r  r   )r   r  r  r  r$  r  rg  r  r  r   r  r[  s               r   
coupon_addr    s   & F~~LL%%'	 ""#7>>%--.BDIM$$R(2MB""#:DAA ) 1 12I4 P !((,57HI""#6==$,,-@$GL##B'1<@""?D99 (($?H#ox8MM*d3		J&6723/m+23/i'/ !GMM:!||,,0$$)||-9:>$$7FJ$$C6:$$3	V  "  D 122MM+,,-E[=O=O PQQ !l"$!G ',g66O	 $ V
 ^^
%%d:??+<&=a&@!**Q-P#ASAS$TUUVs+   ,#J& 5J. &J+.	L7ALLL)rk  rl  rn  c                 l   t         j                  j                  d      }t        |d      }| j                  j                  d      }	 |j                  |      }|dd}t        | d|      S # t        t        f$ r |j                  d      }Y 4t        $ r |j                  |j                        }Y Yw xY w)z
    In NBC admin dashboard, On the orders section we have order slot menu. clicking on the
    menu will open the slot list. we can add, edit and view the slots from here.
    :param request:
    :return:
    rs  rb  ru  r   rJ  )delivery_slot_datar  zdelivery_slot_list.html)rm   r  rS  r    rx  r   ry  r"   rz  r!   r{  r   )r   delivery_slotsr}  ru  r-  r[  s         r   delivery_slot_listr  q  s     "))22=AN.!,I;;??6"D@!**40
 &3XNG'4g>> i( .!**1- @!**9+>+>?@r  c                 |    t         j                  j                  |      }|r|j                          t	        d      S )aD  
    In NBC admin dashboard, On the orders section we have order slot menu.
    on the slots listed, for each slot we have a delete button. Clicking on
    the delete button will open a dialog box. clicking on the confirm will
    delete the delivery slot.

    :param request:
    :param delivery_slot_id:
    :return:
    r  r  )rm   r  r  r  r   )r   delivery_slot_idr-  s      r   delivery_slot_deleter    s9     !((//3C/DM())r   c                 x   g }t         j                  j                  |      j                         }| j                  dk(  r^t        | j                  |      }|j                         r|j                         }t        d      S |j                  |j                         nt        |      }|dd}t        | d|      S )a  
    In NBC admin dashboard, On the orders section we have order slot menu.
    on the slots listed, for each slot we have a edit button. Clicking on
    the edit button will open a edit form. fields of the form are  start_time,
    end_time, is_available, status. based on the availability status, it is
    showing on the app(select Delivery slots).

    :param request:
    :param delivery_slot_id:
    :return:
    r  r   r  r  rJ  )delivery_slot_formr  zdelivery_slot_edit.html)rm   r  r  r  r   rC   r   r  r  r   r  r  r   )r   r  r  r-  r  del_slotr[  s          r   delivery_slot_editr    s     F ((//3C/DJJLM~~}E==?yy{H011 MM$++&7%)HEG'4g>>r   c                 :   g }| j                   dk(  rnt        | j                        }|j                         r|j	                         }t        d      S |j                  |j                         ||dd}t        | d|      S t               }||dd}t        | d|      S )a  
        In NBC admin dashboard, On the orders section we have order slot menu.
    Clicking on the Add Delivery Slot button will open a add delivery slot form.
    fields of the form are  start_time, end_time, is_available, status.

    :param request:
    :return:
    r   r  rJ  )r  r  r  zdelivery_slot_add.html)	r   rC   r   r  r  r   r  r  r   )r   r  r  r  r[  s        r   delivery_slot_addr    s     F~~-==?yy{H011MM$++& &*!)G
 '#;WEE! "!G '3W==r   c                 l   t         j                  j                  d      }t        |d      }| j                  j                  d      }	 |j                  |      }|dd}t        | d|      S # t        t        f$ r |j                  d      }Y 4t        $ r |j                  |j                        }Y Yw xY w)a  
    Discount  menu on Promotions section in the side bar will list the discounts.
    It is only available for super admin and nbc admin.
    nbc admin can create, edit and delete discounts.
    this defined discount will shown on product listing with a strike.
    rs  rb  ru  r   rJ  )discount_datar  zdiscount_list.html)rn   r  rS  r    rx  r   ry  r"   rz  r!   r{  r   )r   discountr}  ru  discount_list_datar[  s         r   discount_listr    s     ((7H(A&I;;??6"DE&//5
 !3QG'/99 i( 3&//2 E&//	0C0CDEr  c                    	 t         j                  j                  |      }t        j                  j                  |      }|j                         r!t	        j                  | d       t        d      S |j                          t	        j                  | d       t        d      S # t         j                  $ r$ t	        j
                  | d       t        d      cY S w xY w)Nr  zDiscount not found.list_discounts)rn   z3This discount is used in ads and cannot be deleted.zDiscount deleted successfully.)rn   r  r   rr  r   r   r   ro   r  r  r  r  r  )r   discount_idr  r  s       r   discount_deleter    s    *##'';'7
 ++

h

/C
zz|A	
 $%% 	"BC$%%    *w 56())*s    B$ $4CCc                 8   g }t         j                  j                  |      j                         }| j                  dk(  rN| j
                  j                         }d|j                  dd      v r7|j                  dd      }|r|j                  d       |j                  d|       d|j                  dd      v r7|j                  dd      }|r|j                  d       |j                  d|       d|j                  dd      v r7|j                  dd      }|r|j                  d       |j                  d|       d|j                  dd      v r7|j                  dd      }|r|j                  d       |j                  d|       |j                  d	d      }		 |	d
k(  r
|d= |d= |d= n|	dk(  r|d= |d= n|	dk(  r|d= t        || j                  |      }
|
j                         rw| j
                  j                  dd      rd|
j                  _        | j
                  j                  dd      rd|
j                  _        |
j#                          t%        ddi      S |j'                  |
j(                         t%        d|
j(                  d      S t        |      }
d}d}|j                  r|j                  j*                  }nd}|j                   r|j                   j*                  }nd}||
|j,                  d|j.                  ||d}t1        | d|      S #  Y \xY w)a  
    Discount  menu on Promotions section in the side bar will list the discounts.
    Editing a discount. for each discount listed, we have a edit button. clicking on
    the edit button will open a form. fields are discount_name and discount_code,
    discount_description, discount_on, applicable_category, applicable_subcategory,
    applicable_product, applicable_sku,
    discount_percentage.
    validations listed on the forms. applicable_category, applicable_subcategory,
    applicable_product, applicable_sku are dynamic fields based on discount_on field.
    discount is percentage based.
     Imp: image refresh and showing logic is handled via javascript.It is field wise and based on id.
    :param request:
    :param discount_id:
    :return:
    r  r   r   r  Nr  r  r  
DiscountOnr  r  r  r  r  r  r  TFr@  r@  rB  rJ  )r  discount_formr  r  r  standard_image_urlbanner_image_urlzdiscount_edit.html)rn   r  r  r  r   r   r  rB  rw  r  r   rD   r   r  r  StandardImageBannerImager  r   r  r  r  r   r  r   )r   r  r  r  r  r  r$  r  rg  discount_onr  r  r  r[  s                 r   discount_editr    s   $ F&&+&6<<>H~~LL%%'	
 ""#7>>%--.BDIM$$R(2MB""#:DAA ) 1 12I4 P !((,57HI""#6==$,,-@$GL##B'1<@""?D99 (($?H#ox8mmL$7	j(6723/-23/	)/ Iw}}xH==?||"-.2+||"-,0)IIKD 122 MM$++&ET[[ IJJX. %3377H#//336 {{$!,,0,G '/99U	s   #L Lc                    g }| j                   dk(  r| j                  j                         }t        d       t        dt	        |j                                       t        d| j                          | j                  rt        dd| j                  v         t        dd| j                  v         d| j                  v rJt        d	| j                  d   j                          t        d
| j                  d   j                          d| j                  v rJt        d| j                  d   j                          t        d| j                  d   j                          d|j                  dd      v r7|j                  dd      }|r|j                  d       |j                  d|       d|j                  dd      v r7|j                  dd      }|r|j                  d       |j                  d|       d|j                  dd      v r7|j                  dd      }|r|j                  d       |j                  d|       d|j                  dd      v r7|j                  dd      }|r|j                  d       |j                  d|       |j                  dd      }	 |dk(  r
|d= |d= |d= n|dk(  r|d= |d= n|dk(  r|d= t        || j                        }t        d       t        d|j                                 |j                         sUt        d|j                          t        d|j                   r#t	        |j"                  j                               nd        |j                         rt        d       |j"                  }	|	j%                         D ]  \  }
}t        |
 d|         d|	v r+t        d|	d           t        dt'        |	d                 d|	v r+t        d |	d           t        d!t'        |	d                 | j                  j                  d"d      rt        d#       d|j(                  _        | j                  j                  d$d      rt        d%       d|j(                  _        |j/                         }t        d&       t        d'|j0                          t        d(|j*                          t        d)|j,                          |j*                  rDt        d*|j*                  j2                          t        d+|j*                  j4                          |j,                  rDt        d,|j,                  j2                          t        d-|j,                  j4                          t7        d.d/i      S |j9                  |j                         t7        d0|j                  d1      S t               }t        d2       g }||d3dd4}t;        | d5|      S #  Y 3xY w)6a  
    Discount  menu on Promotions section in the side bar will list the discounts.
    adding a discount.We have a add coupon discount. clicking on
    the add button will open a form. fields are discount_name and discount_code,
    discount_description, discount_on, applicable_category, applicable_subcategory,
    applicable_product, applicable_sku,
    discount_percentage.
    validations listed on the forms. applicable_category, applicable_subcategory,
    applicable_product, applicable_sku are dynamic fields based on discount_on field.
    discount is percentage based.
     Imp: image refresh and showing logic is handled via javascript.It is field wise and based on id.
    r   z=== DEBUG: Form POST Data ===zPOST data keys: zFILES data: zStandardImage in FILES: r  zBannerImage in FILES: r  zStandardImage name: zStandardImage size: zBannerImage name: zBannerImage size: r   r  Nr  r  r  r  r  r  r  z
=== DEBUG: Form Validation ===zForm is valid: zForm errors: zForm cleaned_data keys: zForm not boundz!
=== DEBUG: Form Cleaned Data ===z: zStandardImage value: zStandardImage type: zBannerImage value: zBannerImage type: r  z<DEBUG: image2 reset detected - setting StandardImage to Noner  z:DEBUG: image3 reset detected - setting BannerImage to Nonez
=== DEBUG: Instance Saved ===zInstance ID: zStandardImage saved: zBannerImage saved: zStandardImage path: zStandardImage URL: zBannerImage path: zBannerImage URL: r  TFr@  z,DEBUG: GET request - initializing empty formrJ  )r  r  r  r  zdiscount_add.html)r   r   r  r   rO  r  r   r   sizerB  rw  r  r   rD   r  r  is_boundr  rT  rW  r  r  r  r  r<  pathr  r   r  r   )r   r  r  r  r$  r  rg  r  r  r  keyvaluediscount_instancer[  s                 r   discount_addr  z  s    F~~LL%%'	 	-. inn&6!7 89: 	W]]O,-==,_-M,NOP*=GMM+I*JKL'--/,W]]?-K-P-P,QRS,W]]?-K-P-P,QRS-*7==+G+L+L*MNO*7==+G+L+L*MNO
 ""#7>>%--.BDIM$$R(2MB""#:DAA ) 1 12I4 P !((,57HI""#6==$,,-@$GL##B'1<@""?D99 (($?H#ox8mmL$7
	j(6723/-23/	)/ %Y> 	01 6 6 89:;%%'M-"6"6!789,XeXnXnT-2L2L2Q2Q2S-T  uE  -F  G  H!!#67(55L*002
URw'( 3 ,.-l?.K-LMN,T,2O-P,QRS,+L,G+HIJ*4]0K+L*MNO ||"-TU7;&&4||"-RS59&&2 !. 2 2 435M"3"6"6!789)*;*I*I)JKL'(9(E(E'FGH !..,->-L-L-Q-Q,RST+,=,K,K,O,O+PQR ,,*+<+H+H+M+M*NOP)*;*G*G*K*K)LMND 122MM-../E]=Q=Q RSS %<= &$#	G '.88E	s   $#W5 5W:c           	      2   | j                   j                  dd      }| j                   j                  dd      }| j                   j                  dd      }t        ddd	d
d      }t        dddd|d      }t        dddd|d      }t        ||||||d      S )Nr=  r>  r?  r@  rA  rB  rC  rD  rE  r  rF  rJ  rK  r  r[  rL  rM  r  r]  r^  r_  s          r   image_field_styler    s     [[__^-DEF[[__^-XYF[[__^-HIF#$63;Sh39%;<L $$63<Ti3BF%TUL $$63<Ti3@&%RSL %|UamsV	-. .r   c                 l   t         j                  j                  d      }t        |d      }| j                  j                  d      }	 |j                  |      }|dd}t        | d|      S # t        t        f$ r |j                  d      }Y 4t        $ r |j                  |j                        }Y Yw xY w)aW  
    Ads menu on Promotions section in the side bar will list the ads.
    It is only available for super admin and nbc admin.
    nbc admin can create, edit and delete ads.
    These ads are used in carousel in the app and also the bottom banner.
    We are defining the placement of ads while creating.

    :param request:
    :return:
    rs  rb  ru  r   rJ  )ad_datar  zad_list.html)ro   r  rS  r    rx  r   ry  r"   rz  r!   r{  r   )r   adr}  ru  ad_list_datar[  s         r   ad_listr  #  s     
		m	,B"a I;;??6"D? ))$/
 'HEG'>733 i( - ))!, ? )))*=*=>?r  c                 |    t         j                  j                  |      }|r|j                          t	        d      S )a<  
    Ads  menu on Promotions section in the side bar will list the ads.
    Deleting a ad. for each ads listed on
    ads list, we have a delete button. clicking on
    the delete button will open a confirm dialog box. if you confirm
    it will delete the ad.
    :param request:
    :param ad_id:
    :return:
    r  list_ad)ro   r  r  r  r   )r   ad_idr  s      r   	ad_deleter  <  s2     
		u		%B	
		Ir   c                    g }t         j                  j                  |      j                         }| j                  dk(  rt        | j                  | j                  |      }| j                  j                  dd      }| j                  j                  dd      }| j                  j                  dd      }|j                         rX|j                  d	      }|j                  dk(  r7	 t        j                  j                  |      j                         }||_        n|j                  dk(  r7	 t        j                  j                  |      j                         }	|	|_        nE|j                  dk(  r6	 t        j                  j                  |      j                         }
|
|_        | j                  j                  d
d      rd|j"                  _        | j                  j                  dd      rd|j"                  _        |j                          t)        d      S |j+                  |j,                         nt        |      }||d|j                  d}t/        | d|      S #  Y xY w#  Y xY w#  Y xY w)a  
    Ads menu on Promotions section in the side bar will list the ads.
    Editing a ad. for each ad listed, we have a edit button. clicking on
    the edit button will open a form. fields are placement_choices and AdTitle,
    AdDescription, AdPlacement, AdType, StandardImage,
    BannerImage, Discount,
    Coupon, Product.
    validations listed on the forms. Discount, Coupon, Product are dynamic fields based on AdType field.
    On submitting we can edit a ad already existing.
    Imp: image refresh and showing logic is handled via javascript.It is field wise and based on id.
    :param request:
    :param ad_id:
    :return:
    r  r   r  r  r   rn   r  Fr*  r  Nr  r  rJ  r  ad_formr  r  zad_edit.html)ro   r  r  r  r   rE   r   r   r   r  r  AdTyperl   r  rn   rU   r  r  r  r  r   r  r  r   )r   r  r  r  r  r  r  r  coupon_instancer  product_instancer[  s               r   ad_editr  N  s   " F			u		%	+	+	-B~~gllGMMB?!!(B/<<##J3,,""9b1==?%(B yyH$&-oo&<&<&<&G&M&M&OO /BI j((0(8(8(?(?8(?(L(R(R(T%"3BK i''/'7'7'>'>''>'J'P'P'R$!1BJ
 ||"-.2+||"-,0)GGII&& MM$++&r"$99	G '>733Gs$   *5I& 05I- 65I4 &I*-I14I8c                    g }| j                   dk(  rt        | j                  | j                        }| j                  j	                  dd      }| j                  j	                  dd      }| j                  j	                  dd      }|j                         rX|j                  d      }|j                  dk(  r7	 t        j                  j                  |      j                         }||_        n|j                  dk(  r7	 t        j                  j                  |      j                         }||_        nE|j                  dk(  r6	 t        j                  j                  |      j                         }	|	|_        | j                  j	                  d	d      rd
|j                   _        | j                  j	                  dd      rd
|j                   _        |j                          t'        d      S |j)                  |j*                         ||d|j                   j                  d}
nt               }g }||ddd}
t-        | d|
      S #  Y xY w#  Y xY w#  Y xY w)a  
    Ad  menu on Promotions section in the side bar will list the ads.
    adding a ad.We have a add ad button. clicking on
    the add button will open a form. fields are placement_choices and AdTitle,
    AdDescription, AdPlacement, AdType, StandardImage,
    BannerImage, Discount,
    Coupon, Product.
    validations listed on the forms. Discount, Coupon, Product are dynamic fields based on AdType field.
    Imp: image refresh and showing logic is handled via javascript.It is field wise and based on id.

    r   r  r   rn   r  Fr*  r  r  Nr  r  rJ  r  zad_add.html)r   rE   r   r   r   r  r  r  rl   r  r  r  r  rn   rU   r  r  r  r  r   r  r  r   )r   r  r  r  r  r  r  r  r  r  r[  s              r   ad_addr    s    F~~gllGMM2!!(B/<<##J3,,""9b1==?%(B yyH$&-oo&<&<&<&G&M&M&OO /BI
 j((0(8(8(?(?8(?(L(R(R(T%"3BK i''/'7'7'>'>''>'J'P'P'R$!1BJ ||"-.2+||"-,0)IIKI&&MM$++& !$,==//	G x (!	
 '='22Ws$   :5I  5I 5I IIIc                    g }| j                   dk(  r-t        | j                  | j                        }|j	                         r|j                         }| j                  j                  d      }|r(|D ]#  }t        j                  j                  ||       % | j                  j                  d      }|r(|D ]#  }t        j                  j                  ||       % 	 | j                  }d}	d}
d|j                   d	|j                   d
}t        ||	|
|       t        d      S |j!                  |j"                         n
t               }||dd}t%        | d|      S #  Y t        d      S xY w)a  
    Custom Products  menu on Products section in the side bar will list the custom products.
    adding a Custom Product.We have a Add Custom Product button. clicking on
    the add button will open a form. fields are item_name and item_code,
    item_description, veg_or_non_veg_status, min_size, max_size,
    size_unit, availability.
    Imp: Images are stored separately in CustomProductVideo and CustomProductImage.
    multiple image upload supported
    for validations please refer CustomProductForm
    r   r4  custom_productr6  r7  r  r9  r  zCustom Product AddedzCustom Product r  z added custom_product_listingrJ  )r  custom_product_formr  zcustom_product_add.html)r   rF   r   r   r  r  rB  r^   r  r  r_   r   rC  rD  r  r   r  r  r   )r   r  r  r  r4  r6  r7  r9  r  r  r   r   r[  s                r   custom_product_addr    sp    F~~/gmmL'')0557N ]]**84F#E&..55^[`5a $ ]]**84F#E&..55^[`5a $"<<$0 /0H0H/IVdVnVnUoovwHh=
 455 MM-445/1 2#G '4g>># 455s   ;E E+c                 l   t         j                  j                  d      }t        |d      }| j                  j                  d      }	 |j                  |      }|dd}t        | d|      S # t        t        f$ r |j                  d      }Y 4t        $ r |j                  |j                        }Y Yw xY w)a  
    Custom Products  menu on Products section in the side bar will list the custom products.
    It is only available for super admin and nbc admin.
    nbc admin can create, edit and delete custom products.
    custom product means, customer can define a product by directly contact
    with navya bakers.
    There is a section in the app to create a custom order. The admin can define the customization
    available products in the custom products section.
    :param request:
    :return:
    r  rb  ru  r   rJ  r  zcustom_product_list.html)r]   r  rS  r    rx  r   ry  r"   rz  r!   r{  r   )r   r  r}  ru  product_list_datar[  s         r   r  r  &  s     !((11/BL,*I;;??6"DD%..t4
  1XNG'5w?? i( 2%..q1 D%..y/B/BCDr  c                    t        t        |      }|}| j                  dk(  rW|j                          	 | j                  }d}d}d|j
                   d|j                   d}t        ||||       t        d
      S t        | dd|i      S # t        $ r,}t        j                  d|        Y d	}~t        d
      S d	}~ww xY w)a  
    Custom Products  menu on Products section in the side bar will list the custom products.
    Deleting a custom product. for each custom product listed on
    custom product list, we have a delete button. clicking on
    the delete button will open a confirm dialog box. if you confirm
    it will delete the custom product.
    :param request:
    :param product_id:
    :return:
    r  r   r  zCustom Product deletedCustom product r  z	 deleted z)Error on Custom Product Delete(logging): Nr  zdelete_confirmation.html)r   r]   r   r  r   rC  rD  r  r   r   r   r   r   )	r   r3  r  r  r  r  r   r   r   s	            r   delete_custom_productr
  D  s      *=GL~~
	||H H.G+L,B,B+C<P\PfPfOggpqK(G[9
 011'5	77KLL  	MMEaSIJ011		s   ;B	 		B>B99B>c                    g }t        t        |      }t        | j                  xs d| j                  xs d|      }| j
                  dk(  r|j                         r|j                         }| j                  j                  d      }|D ]#  }t        j                  j                  ||       % | j                  j                  d      }|D ]#  }t        j                  j                  ||       % 	 | j                  }	d	}
d
}d|j                   d|j                   d}t!        |	|
||       t#        d      S |j%                  |j&                         |||d}t)        | d|      S #  Y t#        d      S xY w)aR  
    Custom Products  menu on Products section in the side bar will list the custom products.
    Editing a Custom Product. for each custom product listed, we have a edit button. clicking on
    the edit button will open a form. fields are item_name and item_code,
    item_description, veg_or_non_veg_status, min_size, max_size,
    size_unit, availability.
        Imp: Images are stored separately in CustomProductVideo and CustomProductImage.
    multiple image upload supported
    for validations please refer CustomProductForm
    :param request:
    :param product_id:
    :return:
    r  Nr  r   r4  r   r7  r  r  zCustom Product Updatedr;  r  z	 updated r  )r  r  r  zcustom_product_edit.html)r   r]   rF   r   r   r   r  r  rB  r^   r  r  r_   r   rC  rD  r  r   r  r  r   )r   r3  r  r  r  r4  r6  r7  r9  r  r  r   r   r[  s                 r   custom_product_editr  i  sx     F&}DN+GLL,@D'--BWSWbpq~~'')0557N ]]**84F"**11W\1]   ]]**84F"**11W\1]  "<<$2 ()A)A(B,~OgOgNhhqrHh= 455 MM-445 2(G '5w??455s   5;E) )E7c                 b   t        t        |      }|j                  j                  }|j	                          	 | j
                  }d}d}d|j                  j                   d|j                  j                   d}t        j                  j                  ||||       t        d|	      S #  Y xY w)
a  
        Custom Products  menu on Products section in the side bar will list the custom products.
        custom product have its associated images and videos which is not listed. when we try to
        add or edit a custom product it will be on form.
        These images and videos are stored in separate table and managed separately.
        access via id.
        Table: CustomProductImage

        We can remove the image with this api.
    :param request:
    :param product_id:
    :param image_id:
    :return:
    rK  r  zCustom Product Image deletedr	  r  z deleted its imager  r  rd  )r   r^   r  r<  r  r   rC  rD  r   r  r  r   )	r   r3  r  r6  custom_product_idr  r  r   r   s	            r   delete_custom_imager    s      0X>E,,//	LLN<<0'(<(<(F(F'G|TYThThTrTrSs  tF  G  ''XRYgr's )6GHHs   A$B* *B.c                    t        t        |      }|j                  j                  }|j	                          	 | j
                  }d}d}d|j                  j                   d|j                  j                   d}t        ||||       t        d	|
      S # t        $ r"}	t        j                  d|	        Y d}	~	3d}	~	ww xY w)a  
    Custom Products  menu on Products section in the side bar will list the custom products.
    custom product have its associated images and videos which is not listed. when we try to
    add or edit a custom product it will be on form.
    These images and videos are stored in separate table and managed separately.
    access via id.
    Table: CustomProductVideo
    We can remove the video with this api.

    :param request:
    :param product_id:
    :param video_id:
    :return:
    rK  r  zCustom Product Video deletedr	  r  z deleted its videoz&Custom Product Video Delete(logging): Nr  rd  )r   r_   r  r<  r  r   rC  rD  r  r   r   r   r   )
r   r3  r  r9  r  r  r  r   r   r   s
             r   delete_custom_videor    s      0X>E,,// 
LLN
<<0'(<(<(F(F'G|TYThThTrTrSs  tF  GHh5
 )6GHH	  >qcBCs   AB 	C B;;C c                 l   t         j                  j                  d      }t        |d      }| j                  j                  d      }	 |j                  |      }|dd}t        | d|      S # t        t        f$ r |j                  d      }Y 4t        $ r |j                  |j                        }Y Yw xY w)at  
    Production unit means where products are manufacturing. Currently only one
    production unit available.
    production units  menu on Sales Unit section in the side bar will list the production unit.
    It is only available for super admin and nbc admin.
    nbc admin can create, edit and delete production unit. Only one production unit could be
    created now. creating more than one production unit is restricted. If no production unit created,
    a add button is available to create one else we hide that button.
    edit and delete button available for the production unit.

    :param request:
    :return:
    rs  rt  ru  r   rJ  )production_unit_datarw  zproduction_unit_list.html)rP   r  rS  r    rx  r   ry  r"   rz  r!   r{  r   )r   production_unitr}  ru  r[  s        r   r  r    s     %,,55mDO/2.I;;??6"DB#,,T2
 (7XVG'6@@ i( 0#,,Q/ B#,,Y-@-@ABr  c                 @   g }| j                   dk(  rot        | j                        }|j                         r|j	                          t        d      S |j                  |j                         ||ddd}t        | d|      S t               }g }||dd}t        | d|      S )a`  
    production units  menu on Sales Unit section in the side bar will list the production unit.
    adding a Production unit.We have a Add Production Unit button. clicking on
    the add button will open a form. fields are pu_name, pu_code, pu_location, street, city
    district, state_or_province, pin_code, latitude, longitude, gst, contact_no, email, status

    *NOt implemented yet (Only one production unit, not checking the status)
    We can change the status of pu by open, close or currently not accepting any order. it is by
    changing the status field.


    :param request:
    :return:
    r   production_unit_listr  rJ  )r  production_unit_formr  rw  zproduction_unit_add.html)r  r  rw  )	r   rG   r   r  r  r   r  r  r   )r   r  r  r[  s       r   production_unit_addr     s    " F~~1',,?((* %%'233MM.556 (<*%-	G '#=wGG13 4%G
 '5w??r   c                 |    t         j                  j                  |      }|r|j                          t	        d      S )a  
    production units  menu on Sales Unit section in the side bar will list the production unit.
    Deleting a production unit. for each production unit listed on
    production unit list, we have a delete button. clicking on
    the delete button will open a confirm dialog box. if you confirm
    it will delete the production unit.
    :param request:
    :param pu_id:
    :return:
    r  r  )rP   r  r  r  r   )r   pu_idpus      r   production_unit_deleter  +  s5     
			&	&%	&	0B	
		*++r   c                 >   t        t        |      }g }| j                  dk(  r^t        | j                  |      }|j                         r|j                          t        d      S |j                  |j                         nt        |      }|dd}t        | d|      S )a  
    production units  menu on Sales Unit section in the side bar will list the production unit.
    Editing a production unit. for each production unit listed, we have a edit button. clicking on
    the edit button will open a form. fields are pu_name, pu_code, pu_location, street, city
    district, state_or_province, pin_code, latitude, longitude, gst, contact_no, email, status
    :param request:
    :param pu_id:
    :return:
    r  r   r  r  rJ  )pu_formrw  zproduction_unit_edit.html)r   rP   r   rG   r   r  r  r   r  r  r   )r   r  r  r  r  r[  s         r   production_unit_editr  ?  s     
>e	4BF~~$W\\B?LLN233MM'..)$b1!AG'6@@r   c                 \    t         j                  j                         }t        | dd|i      S )a  
    Faq's are the possible questions raised by the user.
    Faq  menu on Settings in the side bar will list the Faq.
    It is only available for super admin and nbc admin.
    nbc admin can create, edit and delete faq.
    add button is available to create.
    edit and delete button available for each faq listed.
    This created faq will render on the faq section on app.

    :param request:
    :return:
    zfaq.htmlfaqs)r$   r  r  r   )r   r!  s     r   faqr"  Z  s'     ;;??D':~66r   c                     | j                   dk(  rWt        | j                        }|j                         r|j	                          t        d      S t        j                  | d       n
t               }t        | dd|i      S )z
    Faq  menu on Settings in the side bar will list the Faq.
    adding a Faq.We have a Add Faq button. clicking on
    the add button will open a form. fields are category, question,
    answer
    :param request:
    :return:
    r   r"  -There was an error with your form submission.zadd_faq.htmlr  )	r   r.   r   r  r  r   r   r   r   )r   r  s     r   add_faqr%  m  sc     ~~w||$==?IIKE?"NN7$STy'>FD>::r   c                 ,   t        t        |      }| j                  dk(  rYt        | j                  |      }|j                         r|j                          t        d      S t        j                  | d       nt        |      }t        | d||d      S )a  
    Faq  menu on Settings in the side bar will list the Faq.
    editing a Faq.We have a edit Faq button for each faq listed. clicking on
    the edit button will open a form. fields are category, question,
    answer
    :param request:
    :param faq_id:
    :return:
    rK  r   r  r"  r$  zedit_faq.html)r  r"  )r   r$   r   r.   r   r  r  r   r   r   r   )r   faq_idr"  r  s       r   edit_faqr(    su     CF
+C~~w||c2==?IIKE?"NN7$ST$'?T#,FGGr   c                 Z    t        t        |      }|j                          t        d      S )a  
    Faq  menu on Settings in the side bar will list the Faq.
    deleting a Faq.We have a delete Faq button for each faq listed. clicking on
    the delete button will open a form. fields are category, question,
    answer
    :param request:
    :param faq_id:
    :return:
    rK  r"  )r   r$   r  r   )r   r'  r"  s      r   
delete_faqr*    s#     CF
+CJJLE?r   c                 *   t         j                  j                         }t        j                  j                         }t        j                  j                         }t
        j                  j                         }t        j                  j                         }t        j                  j                         }| j                  dk(  rd| j                  v rNt        | j                  | j                  |      }|j                         r|j                          t'        d	      S d| j                  v rCt        | j                  |      }|j                         rQ|j                          t'        d	      S d| j                  v rCt        | j                  |      }	|	j                         r |	j                          t'        d	      S d| j                  v rBt!        | j                  |      }
|
j                         r|
j                          t'        d	      S d| j                  v rBt#        | j                  |      }|j                         r`|j                          t'        d	      S d| j                  v r7t%        | j                  |      }|j                         r|j                          t'        d	      S t        |      }t        |      }t        |      }	t!        |      }
t#        |      }t%        |      }t)        | d
||||||||	|
|||d      S )a+  
    About us describe about the Navya backers,
    Where there shops, contact details, app url,
    privacy polices, terms and conditions and what is their motto etc......

    each of this details are handled separately.
    we can edit each of this details.

    :param request:
    :return:
    r   edit_about_usr  edit_contact_usedit_app_urledit_privacy_policyedit_terms_conditionsedit_refund_policyabout_uszabout_us.html)r2  
contact_usapp_urlprivacy_policyterms_and_conditionsabout_us_formcontact_us_formapp_url_formprivacy_policy_formterms_conditions_formrefund_policyrefund_policy_form)r%   r  r  r'   r&   r(   r*   r)   r   r   r+   r   r  r  r-   r,   r/   r4   r0   r   r   )r   r2  r3  r4  r5  r6  r<  r7  r8  r9  r:  r;  r=  s                r   r2  r2    s    $$&H""((*Jnn""$G"**002N-55;;= ((..0M~~gll*'gmm19;M%%'""$, 
##+ ',,.+GLL:NO'')$$&$ 
### w||+%gllWEL$$&!!# 
## #gll2"3GLL>"Z"++-#((* 
## %4$:7<<Rf$g!$--/%**, 
## "W\\1!1',,!W!**,"'')
##  2M#Z8Ow/L+^D2<PQ)=A'? ( 4&*$2!6&0-  r   c                    t         j                  j                         }t        | j                  xs d|      }| j
                  dk(  r+|j                         r|j                          t        d      S t        | d||d      S )aD  
    In about us section we have the privacy policy view, in addition to that
    we have a separate menu on the settings, Privacy Policy. on clicking
    it will list current privacy policy, we have edit button on the list.
    by clicking edit we can edit the current privacy policy.

    :param request:
    :return:
    Nr  r   r5  zprivacy_policy.html)r  policy)
r(   r  r  r/   r   r   r  r  r   r   )r   r?  r  s      r   privacy_policy_viewr@    ss     ""((*FW\\1TFCD~~DMMO		())'03  r   c                    t         j                  j                         }t        | j                  xs d|      }| j
                  dk(  r+|j                         r|j                          t        d      S t        | d||d      S )a\  
        In about us section we have the Terms and condition view, in addition to that
    we have a separate menu on the settings, Terms and condition. on clicking
    it will list current terms and condition, we have edit button on the list.
    by clicking edit we can edit the current terms and condition.

    :param request:
    :return:
    Nr  r   r6  zterms_conditions.html)r  terms)
r*   r  r  r4   r   r   r  r  r   r   )r   rB  r  s      r   terms_conditions_viewrC    ss     &&,,.E!',,"6$GD~~DMMO		.//'25  r   c                     t        | d      S )zH
    * Not implemented yet........
    :param request:
    :return:
    delivery_settings.htmlr   r	  s    r   delivery_settingsrG  ,  s     '344r   c           
      	   	 | j                   j                  d      }t        j                  j	                  d      j                  | j                  j                        }|d   }g }| j                  j                  d      }| j                  j                  d      }| j                   j                  dd      }| j                   j                  d	d      }	| j                   j                  d
d      }
| j                   j                  dd      }|s|}| j                   j                  dd      }|s|}t        j                  j                  |d      j                  d      j                  dddd      j	                  d      j                  d      }|rp|dk(  r)|j                  t        d      t        d      z        }nB|j                  t        d      t        d      z  t        d      z  t        d      z        }| j                  j                  d      dk(  r|rQ|j                  t        |      t        |       z  t        |!      z  t        |"      z        j                  d      }|	r(t        j                   |	d#      }|j                  |$      }|
r|j                  |
      }|rd|rb|d%k(  r,|dk(  r|j                  |d&z         }nC|j                  |      }n1|dk(  r|j                  d'|z   d&z         }n|j                  d'|z         }t#        |d(      }| j                   j                  d)      }	 |j%                  |      }t/        d+      }| j                   j1                         }d)|v r|d)= |j2                  j4                  D cg c]  }|| d,|j7                  d-.       d/| d0! }}t9               }t:        j                  j                  d1|2      }||||	|
|d3||d4	}| j                  j                  d      dk(  r-t=        d5||||d6      }t=        d7d8|i      }t?        ||d9      S tA        | d:|      }|S # t&        t(        f$ r |j%                  d*      }Y t*        $ r |j%                  |j,                        }Y Dw xY wc c}w # tB        $ r g d3d;}tA        | d:|      cY S w xY w)<Nr  r&  r'  r   rI  r  r  r   r  r  Custom Orders)r  rb  r  r  r  r   r  r  custom_orderr  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r_  r  r  r  rb  ru  r   custom_ordersr  r  r  r  r  r  r  rJ  )	
order_formr  r  r   r  r  r  r  r  zcustom_order_list_page.html)r  r  r  r  r  r  r  zcustom_orders.htmlr  )"rx  r   rN   r  rL  r  r   r   r  rj   r   r  rS  r   r  r   r  r    ry  r"   rz  r!   r{  r|   r  r}  r  r  rH   rf   r   r   r   r  )r   r  r  rT  r#  r  rI  r  r  r  r.  r  r  r  r  r}  ru  r  r  r  r  r  r  r  r[  r  r  rZ  s                               r   rK  rK  7  s   w>;;??:.667HIPP"),,"3"3 Q 5 #__((6
__((6
{{x4[[__^R8
{{'7<!++//,;(O!++//,;(O NNVw?VCW+W.^,k>J]^n-Xo& 	 5 '..u-70KK
 (..u-y12y12 '89:
 ??126FF'..,7L9:l;< |DE
 (?+  !)!2!2:z!J'....Q
'..L.I
?"e+&+5%/%8%8>9Y%Z
%/%8%8%I
&+5%/%8%89NQ_9_%`
%/%8%89N%O
j!,	{{v&	=''-F ?+{{'')\!V$  &//::

  ;  +"1\%;%;%;%E$Ff[MZ  ; 	 
  %--44JW4U (* 0%, 

 ??126FF$%B4:P`8FT\&^_E **FW]H^_J%z JKK'#7AO !), 	+''*F 	=''	(;(;<F	=
@  >%
 g3W==>sV   L>R Q AR $R:BR >R  R-R 0#RR RR R=<R=c           	         | j                   j                  d      }|st        dddd      S 	 t        j                  |d      j                         }t        j                         j                         }t        j                         }t        j                  j                  d	d
      }||k(  r$|j                  |      }|j                  |      }|D cg c]H  }|j                  |j                   j#                  d       d|j$                  j#                  d       dJ }}t        d|d      S # t        $ r t        dddd      cY S w xY wc c}w )Nr   r  zDate not providedrY    r   r  zInvalid date formatTrJ  is_availabler   end_time__gt)end_time__ltez%I:%M %prG  )r<  labelr  )r   slots)rx  r   r   r   r  r   r  r   	localtimetime	localdaterm   r  r  r   r<  r  rS  end_time)r   selected_dateselected_date_objcurrent_timer"  rU  r  	slot_datas           r   get_available_delivery_slotsr^    sT   KKOOF+Mv:MNWZ[[^$--mZHMMO %%',,.L E  ''T('KE E!,7L9 
 D ''00<=SAWAWXbAc@de	
    9yABB/  ^v:OPY\]]^s   $D4 AE4EEc                 t   | j                   dk7  rt        ddid      S | j                  j                  d      }|st        ddid      S t        j
                  j                  |      j                  d	      j                         }|st        dd
id      S |j                  }t        j
                  j                  |      j                  d      j                         }t        d      }|r*|j                  t        t        |j                              }t        |      }t        dt        |      i      S )zLReturn delivery fee for a given order (based on user's last known distance).rx  r   zInvalid methodrN  r   r  zMissing order_idr  r   r    r   rs  0fee)r   r   rx  r   rj   r  r  r  r  r   UserLocationrS  r   distancer  calculate_dynamic_delivery_fee)r   r>  r   r   user_locationdistance_kmrc  s          r   get_delivery_feeri    s   ~~W&67DD,JW&89#FF NN!!z!2AA+NTTVEW&78EE??D ((//T/:CCMRXXZM#,K//;c-"8"89: )
5CC)**r   c                 *   t         j                  j                         }|st        d      S t	        | t              st        t        |             } |j                  }|j                  }|j                  }| |kD  r| |z
  }|||z  z   }n|}t        |d      S )z=Calculate delivery fee based on distance and global settings.z0.0r   )
rt   r  r  r   
isinstancer  base_feebase_km
per_km_feer   )rh  r   rl  rm  rn  extra_kmdelivery_fees          r   rf  rf    s    ''--/Hu~ k7+c+./  HG$$JW(:#89q!!r   c                    | j                   dk(  r| j                  j                  d      }t        j                  j                  |      j                         }d}|st        dddd	      S t        | j                  |
      }|j                         st        d|j                  d      S | j                  j                  d      }|rB	 t        t        |            }|t        d      z  j                  t        d            }||_        |j#                          | j                  j                  d      }| j                  j                  d      }	| j                  j                  d      }
d }d }d }|
dk(  rU| j                  j                  d      }d }d }d|_        d}|rt'        |      nd |_        d |_        t        d      |_        nO|
dk(  r%| j                  j                  d      }| j                  j                  d      }d }d|_        d}|rt'        |      nd |_        |r3	 t.        j                  j                  |      }|j0                  |_        nd |_        	 |j4                  }t6        j                  j                  |      j9                  d      j                         }t        d      }|r*|j:                  t        t        |j:                              }t=        |      }||_        n$d }d }d }d |_        d |_        t        d      |_        tB        j                  jE                  |       \  }}||_#        |	|_$        ||_%        |j#                          d!|_&        |j#                          tN        j                  jE                  |"      \  }}|sd#|_(        |j#                          d$}d%|j4                  jR                   d&| d'}t        d(|jT                  |jV                  d)      S | j                   d*k(  r| jX                  j                  d+      }t        j                  j                  |,      j                         }|rt        |
      }	 tB        j                  j                  |       }d}|r|jJ                  dk(  rd}nd}d }d }|dk(  r|j(                  }n|dk(  r|j(                  }t        d      }|jZ                  rE	 t        t        |jZ                              }|t        d      z  j                  t        d            }t        d      }|dk(  r	 |j4                  }t6        j                  j                  |      j9                  d      j                         }t        d      }|r*|j:                  t        t        |j:                              }t=        |      }nt        d      }t]        j^                         }t]        j`                         jc                         }t.        j                  j                  d#d-.      j9                  d/      }|xs |} | r| n|}!|!|k(  r|j                  |0      }"n|}"d }#|dk(  r"|j*                  r	 t.        j                  j                  |j*                  d#d-1      j                         }|r|!|k(  r|jd                  |k  rd }|s |"jg                         r|"j                         }|r|jh                  }#nt               }d }d}d }d }d }#t]        j^                         }!t        d      }t        d      }t]        j`                         jc                         }t.        j                  j                  d#d-|3      j9                  d/      }"tk        d4|||r|jL                  nd|"|||||#||d5      }$t        d#|$d6      S t        dd7d      S # t        t         f$ r t        d      |_        Y w xY w# t.        j2                  $ r d |_        Y w xY w# t>        $ r)}tA        d|        t        d      |_        Y d }~d }~ww xY w# tB        j2                  $ r d }Y w xY w# t        t         f$ r t        d      }Y Uw xY w# t>        $ r$}tA        d|        t        d      }Y d }~d }~ww xY w# t>        $ rA}tA        d2|       |"jg                         r|"j                         jh                  }#Y d }~pd }~ww xY w)8Nr   r   r  r   r  r  rY  r`  r   r  rW  	sub_totalz0.05z0.01z0.00r  r  r  r  r  rI  r  r  r  r  r  rK  ra  rs  rb  z Error calculating delivery fee: r  Bill Createdr  Tu   Your Bill is Ready 🧾r  z',
 
        Your bill for custom order a    has been successfully generated.
 
        Please review the details and proceed with the payment at your convenience.
 
        If you have any questions, feel free to contact our support team.
 
        Best regards,  
        Navya Bakers Team
        r  r  rx  r  r  rJ  rO  r  rQ  )r  rP  r   zSlot selection error:)rP  r   rR  zorder_status_update_form.html)rL  r  r.  r  custom_trackingexisting_delivery_typer  r  selected_slot_idcalculated_taxescalculated_delivery_feer  rX  )6r   r   r   rj   r  r  r  r   rH   r  r  r   r  quantizetaxes_and_chargesr  rz  r  rb  r   rQ  rR  delivery_chargesrm   r  rr  r   rd  rS  re  rf  r   r   rd   r[  r  r  r  r.  rc   	is_editedr  r   r   rx  rr  r   rX  rV  rW  rY  r  r<  r   )%r   r   rK  r  r  rr  sub_total_decimalrw  r  r  r  r  r  time_slot_idr  r   rg  rh  rp  r   r  r  	bill_editr   r   r  rt  ru  rx  r"  r\  	all_slotsrZ  r[  available_slotsrv  	form_datas%                                        r   custom_order_editr    s    ~~,,"":.--w-?EEG6>O PY\]] w||mD}}6T[[ IJJ LL$$[1	B$+C	N$;!$5$G#Q#QRYZ`Ra#b 2B/ 			 <<##J/((9((9  H$!,,**=9K ML'6M$ HJUz+/F[_M,/3M, .5V_M*o-#LL,,_=M"<<++K8LK'6M$&HLYz-/H_cM,<'//33|3DD7;M4 480A$.. , 4 4 ; ; ; F O OP] ^ d d f%cl ]%;%;%G")#m.D.D*E"FK=kJ1=. K ML/3M,/3M,-4V_M* !4 ; ; I IS` I ag (%2"%-" &4"'//==M=R	7"&INN * 7 7 B BC D$$+9 -	
& %**$--
  	 
5	 ;;??:.--8-<BBD"M:D'"5"="="A"A="A"Y
 &("?#@#@I#M)1&)8& K M%1+>>'?: - @ @  'v&&7(/M4K4K0L(M%(9GFO(K'U'UV]^dVe'f$
 '.fo#%8
>(22D$0$8$8$?$?T$?$J$S$STa$b$h$h$jM")#,K$)?)?)K&-c-2H2H.I&J.L[.Y+ +2&/' &&(E#--/446L %,,33! 4  h|$ 
 *8[M1>E !E)"+"2"2"2"M #,  $%8]=]=]F'//66#0#C#C%)' 7  eg	   1U :==L8#'D  O$:$:$<.446+/77( #$D"O%'"K M# ( 2 2 4&v&-fo# $--/446L*2299!) :  h|$	  %+"$>K : :QS"1#2*@*!.$4$4+B
	" i@AA66NOPPK 	* B29&//BV $00 <7;M4<  A8<=18..AD '33 '"&'2 #I. 7'.v$7 ! ><QC@A.5fo+>j ! F115&--/+:+@+@+B+E+E(	Fs   A^= +1_# %B` " `9 Aa .Ba6 'Bb& =_ _ #` `	`6`11`69aaa32a36	b#?bb#&	c0/6c++c0c                     t        | d      S )Nzsystem_settings.htmlrF  r	  s    r   system_settingsr  (  s     '122r   c                    | j                   j                  d      }|dk(  rnt        j                  j	                         D cg c]  }|j
                  |j                  d }}	 |rt        t        |      |dd      S t        g |dd      S |dk(  rnt        j                  j	                         D cg c]  }|j
                  |j                  d }}	 |rt        t        |      |dd      S t        g |dd      S |dk(  rnt        j                  j	                         D cg c]  }|j
                  |j                  d }}	 |rt        t        |      |dd      S t        g |dd      S y c c}w # t        $ r t        g |dd      cY S w xY wc c}w # t        $ r t        g |dd      cY S w xY wc c}w # t        $ r t        g |dd      cY S w xY w)	Nr   rn   )r<  r   )tablesrW  Fr  r  r  )rx  r   rn   r  r  r<  DiscountNamer   rO  StopIterationrl   
CouponNamerU   rC  )r   r   rZ  discount_choicecoupon_choiceproduct_choices         r   choice_listr  Y  s   [[__X&FIQIYIYI]I]I_`I_A!$$?I_`	L#tO/Df$U).0 0 $r6$BOO
 EL__EXEXEZ[EZall;EZ[	L#tM/BF$S).0 0 $r6$BOO
 EMEUEUEYEYE[\E[q{{;E[\	L#tN/CV$T).0 0 $r6$BOO / a  	L2v >UKK	L \  	L2v >UKK	L
 ]  	L2v >UKK	LsZ    E6$E; ?E; 4 FF 2F ' F>
G %G ;FFF;:F;GGc                    t        j                         }t        j                  j	                  g d      }g d}|D ]  }	 |j
                  r|j
                  rst        j                  |j                  |j
                        }|j                         t        j                         j                         z
  }|j                         }|dz  }nd}|j                  dk(  s|j                  dk(  r|dk  rM|j                  d	k7  r>|j                  |vr0d
|_        t        j                         |_        |j                          |dk  r>|j                  |vr0d|_        t        j                         |_        |j                          |dk  rC|dkD  r>|j                  |vr0d|_        t        j                         |_        |j                          |dkD  r0d|_        t        j                         |_        |j                          |j                  d	k(  r|dk  r>|j                  |vr0d|_        t        j                         |_        |j                          |dk  rC|dkD  r>|j                  |vr0d|_        t        j                         |_        |j                          |dkD  r0d|_        t        j                         |_        |j                          |j                  dk(  s|j                  dk(  r0d|_        t        j                         |_        |j                           t!        ddi      S #  |j                  dk(  s|j                  dk(  r0d|_        t        j                         |_        |j                          Y CxY w)N)r-  r1  r2  r  r  )r4  r  r  rs  r/  r  r  rn  r   r-  r1  r   r2  Yellowro  r   rp  rq  r  r  rr  StatusT)r   r"  rj   r  r  rR  r   r|  rQ  r}  rp  r~  r.  r  r  r  r   )	r   r"  r  status_excludedrZ  r  r  r  r  s	            r   my_polling_taskr    s   JJLE^^""4e"fFzOB	$$)=)=$,$4$4Q5I5I1K_K_$`!):)E)E)G(,,.JcJcJe)e&%;%I%I%K".5	 	~~,(0Jq=~~7ANNRa<a)17?||~4>~~_<).7?||~4q=Y]~~_<)17?||~4q=%,AN3;<<>A0FFH ~~/>~~_<).7?||~4q=Y]~~_<)17?||~4q=%,AN3;<<>A0FFH ~~)Q^^{-J!+/7||~,{ J 4())	~~)Q^^{-J!+/7||~,s   KL--AN c                    t         j                  j                  dd      j                  d      j	                  d      }t        |d      }| j                  j                  d      }|j                  |      }|dd}t        | d	|      S )
Nr   r   message__adsrs  rb  ru  rJ  )msg_datar  zcommunication_list.html)
rq   r  r  rL  rS  r    rx  r   ry  r   )r   r   r}  ru  msg_list_datar[  s         r   list_communicationr    s|    



.
.vy
A
R
RSa
b
k
kC#q!I;;??6"D&&t,M(hGG'4g>>r   c                     | sy| j                  dd      j                  dd      }t        j                  d|      syh d}||v ry|S )z
    Validate Indian mobile numbers:
    - Must start with 6, 7, 8, or 9
    - Must be 10 digits
    - Reject spam patterns: 0000000000, 1111111111, 1234567890
    Fr   r   z+91z
[6-9]\d{9}>   
0000000000
1111111111
1234567890
2222222222
3333333333
4444444444
5555555555
6666666666
7777777777
8888888888r$  
9999999999)replacere	fullmatch)r   cleanedspam_numberss      r   is_valid_indian_mobiler    sU      nnS"%--eR8G <<w/L ,Nr   c           
         | j                   dk(  rt        | j                        }| j                  j                  d      xs d}|j	                         rT|j                         }t        j                  j                  d      }|j                  dk(  ri }|D ],  }|j                  }|s	 t        |       |j                  ||<   . |j                         D ]  \  }}	 t        j                  j                  |      }d|j                   d	|j                    }	t#        j$                  ||	t&        j(                  |gd
dd       t*        j                  j-                  ||        | j6                  j                  d      dk(  rt9        dt;        d      d      S t=        d      S | j6                  j                  d      dk(  r(t9        d
|j>                  jA                         dd      S tC        | dd|i      S t               }tC        | dd|i      S # t        $ r Y w xY w# t.        $ r/}
t1        j2                  d| dt5        |
              Y d }
~
d }
~
ww xY w)Nr   r   zNotification from Navya Bakersr  r   Emailr  r  z,

F)r   r   
from_emailr  r   r   r  )r   r   zEmail Broadcast Error: z | zX-Requested-Withr  Tcommunication_list)r  redirect_urlr@  rN  r   zsend_message.htmlr  )"r   rI   r   r   r  r  r  r  r  message_typer   r   r   r   rT  r  r   r   r   r   ry   rq   r  r   r   r   r  r  r   r|   r   r  get_json_datar   )r   r  r   msg_form	customersvalid_emailscustomerr   r  bodyr   s              r   send_messager    sH   ~~7<<(,,""9-Q1Q==?yy{H ,,z,BI
 $$/! )H$NNE  !&u-
 +3--L' !*  '3&8&8&:NE7!#(==#4#4'#4#B!&x':':&;5AQAQ@RS%11'.'+*2*B*B/4g-2  #$	 &--44(H4U' ';l ""#56:JJ##$+,@$A%  
 011 ""#56:JJ#AZAZA\$]fijj'#6GG =D'.??] + ! !8 % !(?wc#a&&RS !s+   ,HB	H(	H%$H%(	I 1$II c                     t        | |t        j                  |      }|j                  |       |j	                          y )N)r   r  r  to)r   r   ry   attach_filer  )r   r  to_emailfilepathmails        r   send_email_attachmentr  ~  s0    dx?W?W\deDXIIKr   r   c                 
   	 | j                   j                  dd       }| j                   j                  dd       }| j                   j                  dd       }| j                   j                  dd       }| j                   j                  dd       }d|v r|j                  d       d|v r|j                  d       d|v r|j                  d       d|v r|j                  d       |dk(  rot        j                  j                  dd      }|D ]  }t        |d         |v rd	|d
<   d|d
<    t        j                  j                  |      j                  dd      }|D ]  }	t        |	d         |v rd	|	d
<   d|	d
<    t        j                  j                  |      j                  dd      }
|
D ]  }t        |d         |v rd	|d
<   d|d
<    t        j                  j                  |      j                  dd      }|D ]  }t        |d         |v rd	|d
<   d|d
<    t        t        |      t        |      t        |
      t        |      |dd      S |dk(  rot        j                  j                  dd      }|D ]  }t        |d         |v rd	|d
<   d|d
<    t        j                  j                  |      j                  dd      }|D ]  }	t        |	d         |v rd	|	d
<   d|	d
<    t        j                  j                  |      j                  dd      }
|
D ]  }t        |d         |v rd	|d
<   d|d
<    t        j                  j                  |      j                  dd      }|D ]  }t        |d         |v rd	|d
<   d|d
<    t        t        |      t        |      t        |
      t        |      |dd      S |dk(  rot        j                  j                  dd      }|D ]  }t        |d         |v rd	|d
<   d|d
<    t        j                  j                  |      j                  dd      }|D ]  }	t        |	d         |v rd	|	d
<   d|	d
<    t        j                  j                  |      j                  dd      }
|
D ]  }t        |d         |v rd	|d
<   d|d
<    t        j                  j                  |      j                  dd      }|D ]  }t        |d         |v rd	|d
<   d|d
<    t        t        |      t        |      t        |
      t        |      |dd      S y # t        $ r#}t        dt        |      dd      cY d }~S d }~ww xY w)Nr  r  r  r  triggerr   r   r  TruecheckedFalse)category_id__inr  )item_category__inr<  rC  )product__item_category__inrQ  )
categoriesrU  r  r  r  Fr  )item_sub_category__in)product__item_sub_category__in)product_id__in)r   r   )rs  r   rw  rR   r  rP  r  rT   r  rU   r\   r   rO  r   )r   category_idssub_category_idsproduct_idssku_idsr  r  rZ  r  r  r  kr  r  r   s                  r   coupon_subcategory_listr    s   zL||''
D9"<<++NDAll&&y$7,,""5$/,,""9d3#!!##B'r"=NN2j (0077PJqy>\1#)AiL#*AiL	  
 /66==l=[bbcicvxM #qy>%55#)AiL#*AiL	 #
  ''...NUUVZ\ghHqw<;.#)AiL#*AiL	  ++$$$MTTUY[efCqw<7*#)AiL#*AiL	   #J/4CVdhiqdrS	g7=BD D &(0077PJqy>\1#)AiL#*AiL	  
 /66==l=[bbcicvxM #qy>%55#)AiL#*AiL	 #
  ''..EU.V]]^bdopHqw<;.#)AiL#*AiL	  ++$$DT$U\\]acmnCqw<7*#)AiL#*AiL	   #J/4CVdhiqdrS	g7=BD D 	!(0077PJqy>\1#)AiL#*AiL	  
 /66==l=[bbcicvxM #qy>%55#)AiL#*AiL	 #
  ''..EU.V]]^bdopHqw<;.#)AiL#*AiL	  ++$$K$@GGjYCqw<7*#)AiL#*AiL	   #J/4CVdhiqdrS	g7=BD D= "F  Lus1v>UKKLs,   IU E4U E4U 	U.U)#U.)U.c                 B   | j                   dk(  r	 t        j                  j                  |      j	                         }t
        j                  j                  |      }|j                  dk(  rg|dk(  rd|_        d|_        |j                          n2|dk(  rd|_        d|_        |j                          nt        dd	d
      S |j                          nH|j                  dk(  rt        ddd
      S |j                  dk(  rt        ddd
      S t        dd	d
      S d}d| d}t        |j                  j                  ||||j                         t        |||j                  j                  gd       d}|j                  j                  dk(  rCt!        j"                  |j                  j$                  ||j                  j&                  |gd       t        dd| d
      S t        ddd
      S # t(        $ r"}t        ddt+        |      d      cY d }~S d }~ww xY w)Nr   r  r  r4  r/  Paidr  r   zStatus change not allowedr   z Status already updated to Failedr  z#Status already updated to Deliveredu"   Your Order Has Been Delivered 🎉zGreat news! Your order z has been successfully delivered.

            We hope you enjoy your purchase. Thank you for choosing Navya Bakers!

            We look forward to serving you again.Frw  Tr   r   r   z Status of your order changes to zsomething went wrong)r   r   r   zmethod not allowed)r   rj   r  r  r  rp   r.  payment_statusr  r   r   r   r   r   r   r  r   r   r  r  r   r  )	r   r   r   r   paymentr   r   r  r   s	            r    order_status_update_to_deliveredr    s   ~~*	_NN))7);AACEoo,,5,9G!!%88[()4E&-3G*LLN x')1E&-5G*LLN'1=X(YZZ

##x/#q8Z$[\\##{2#yAf$ghh#q9T$UVV 9E5gY ?2 5K
 U__66ukSXSbSbce[5??+@+@*A5I*I%%-%118T8TU\]b]l]l]w]w  yB  8C  NO  Q17WX^W_5` abb q1EFGG  	_15KVYZ[V\ ]^^	_s7   B2G3 -G3 3G3 G3 CG3 3	H<HHHc                 l   t         j                  j                  d      }t        |d      }| j                  j                  d      }	 |j                  |      }|dd}t        | d|      S # t        t        f$ r |j                  d      }Y 4t        $ r |j                  |j                        }Y Yw xY w)aq  
    Liked List menu on Product section in the side bar will list the You may also like list.
    It is only available for NBC admin.
    from Liked list we can add a product and we can map product related to that product.
    if one person orders a jam, liked products will contain bread it will be defined in
    the liked list

    :param request:
    :return:

    rs  rb  ru  r   rJ  )	like_listr  zpeople_also_like_list.html)r`   r  rS  r    rx  r   ry  r"   rz  r!   r{  r   )r   
liked_datar}  ru  rs  r[  s         r   you_may_also_like_listr  <  s      ''00?J*a(I;;??6"D7!!$'
 !X>G'7AA i( %!!!$ 7!!)"5"567r  c                 |    t         j                  j                  |      }|r|j                          t	        d      S )a8  
    Liked List menu on Product section in the side bar will list the You may also like list.
    Deleting a Liked Data. for each liked data listed on
    liked list, we have a delete button. clicking on
    the delete button will open a confirm dialog box. if you confirm
    it will delete the liked data.
    r  you-may-also-like)r`   r  r  r  r   )r   r<  you_may_also_like_datas      r   you_may_also_like_deleter  V  s9     ,33::b:A%%''((r   c                    g }t        t        |      }| j                  j                         }d|j	                  dg       v r7|j	                  dg       }|r|j                  d       |j                  d|       | j                  dk(  rTt        ||      }|j                         r|j                          t        d      S |j                  |j                         nt        |      }|dd}t        | d	|      S )
a	  
    Liked List menu on Product section in the side bar will list the You may also like list.
    Editing a Liked data. for each liked data listed, we have a edit button. clicking on
    the edit button will open a form. fields are product and liked products.

    r  r   liked_productsr   r  r  rJ  )	like_formr  zpeople_also_like_edit.html)r   r`   r   r  rB  rw  r  r   rJ   r  r  r   r  r  r   )r   r<  r  r  request_postliked_product_listr  r[  s           r   you_may_also_like_editr  g  s     F.~"E<<$$&L 
\!!"2B77*223CRH%%b)-/AB~~!,9OP==?IIK/00 MM$++&!+AB X>G'7AAr   c                    g }| j                   j                         }d|j                  dg       v r7|j                  dg       }|r|j                  d       |j	                  d|       | j
                  dk(  rdt        |      }|j                         r|j                          t        d      S |j                  |j                         ||dd}t        | d|      S t               }||dd}t        | d|      S )a  
    Adding a liked product.
    Liked List menu on Product section in the side bar will list the You may also like list.
    clicking on add Liked list will open the form. fields are product and liked products.
    on submission create a liked product data.
    r   r  r   r  rJ  )r  r  r  zpeople_also_like_add.html)r   r  rB  rw  r  r   rJ   r  r  r   r  r  r   )r   r  r  r  r  r[  s         r   you_may_also_like_addr    s     F<<$$&L 
\!!"2B77*223CRH%%b)-/AB~~!,/==?IIK/00MM$++& !#+G
 '#>HH"$ #G '6@@r   c                 J   t         j                  j                         }|st         j                  j                         }t        j                  j                         j                  d      }| j                  dk(  r d| j                  v rBt        | j                  |      }|j                         r|j                          t        d      S d| j                  v r@t        | j                        }|j                         r|j                          t        d      S d| j                  v rj| j                  j                  d      }t        j                  j                  |	      j!                          t        d      S t        |      }t               }|d
}t#        | d|      S )a  
    delivery settings.
    delivery settings menu on Settings section in the side bar will list Delivery setting.
    delivery fee configuration, current delivery fee discount, add delivery discount option are
    listed on the delivery settings.
    :param request:
    :return:
    min_order_valuer   save_settingsr  rG  add_discountdelete_discountr  rK  )settings_formr  delivery_discountsrE  )rt   r  r  r  rs   r  rS  r   r   rK   r  r  r   rL   r   r  r  r   )r   r   	discountsr  r  r  r[  s          r   delivery_settings_viewr    sc     ''--/H#++224 ((,,.778IJI~~ gll*0QM%%'""$ 344 W\\)0>M%%'""$ 344 ,!,,**=9K$$++{+;BBD/00 -h?,. '&'G '3W==r   c                 Z   t         j                  j                         }|st         j                  j                         }| j                  dk(  rBt        | j                  |      }|j                         r'|j                          t        d      S t        |      }d|i}t        | d|      S )a  
    custom coupon settings.
    Custom Coupon settings menu on Promotions section in the side bar will list Custom coupon list.
    if a customer buys a product between a price range and there is a
    coupon for that price range. A coupon code will send through the email, whatsapp or sms.
    settings view for entering the price range is here

    :param request:
    :return:
    r   r  zcoupon-settingsr  zcustom_coupon_setting.html)ru   r  r  r  r   rM   r   r  r  r   r   )r   coupon_settingsr  r[  s       r   coupon_settings_viewr    s     +2288:O.66==? ~~'Q==?IIK-..'A 	tG '7AAr   c                 z    t         j                  j                         j                  d      }t	        | dd|i      S )Nz-updated_atzpayment_modes_list.htmlmodes)rw   r  r  rS  r   )r   r  s     r   payment_mode_listr     s6      $$&//>E'4w6FGGr   c                 r   | j                   dk(  r| j                  j                  d      }| j                  j                  d      dk(  }|s!t        j                  | d       t        d      S t        j                  j                  ||       t        j                  | d       t        d      S t        d      S )	Nr   rM  r   r  zMode name is requiredr  )rM  r   zPayment Mode added successfully)
r   r   r   r   r   r   rw   r  r  r  )r   rM  r   s      r   add_payment_moder     s    ~~LL$$[1	!!(+v5NN7$;</00## 	$ 	
 	"CD+,,'((r   c                     t        t        |      }|j                   |_        t        j                         |_        |j                          t        j                  | d       t        d      S )NrK  zStatus updatedr  )
r   rw   r   r   rp  
updated_atr  r   r  r   )r   mode_idmodes      r   toggle_payment_statusr  $   sN    \g6Dkk/DKllnDOIIKW./'((r   c                     t         j                  j                  d      j                  dd      }t	        | dd|i      S )z2
    List all unapproved reviews (admin only)
    F)is_approvedr   r  zpending_reviews.htmlreviews)rS   r  r  r  r   )r   r  s     r   pending_reviewsr  /   sN     ##** + nVY'  	G r   c                 n    t        t        |      }d|_        |j                  dg       t	        d      S )z
    Approve a review
    rK  Tr  )update_fieldsr  )r   rS   r  r  r   r   	review_idreviews      r   approve_reviewr  A   s4     };FF
KK}oK.%&&r   c                 Z    t        t        |      }|j                          t        d      S )z"
    Reject (delete) a review
    rK  r  )r   rS   r  r   r  s      r   reject_reviewr  N   s%     };F
MMO%&&r   c           	         t         j                  j                  d      j                         }t	               }| j
                  dk(  rt	        | j                        }|j                         r|j                          t        j                  d|j                  j                   d|j                  j                  j                   dt        j                                  t#        j$                  | d       t'        d      S t)        | d||d	      S )
Nr  r   zAssigned Slob ID:  for Shop:  at zSlob ID assigned successfullyshop_slob_listzshop_slob.html)	slob_listr  )rQ   r  r  r  r2   r   r   r  r  r   r   r  r<  r  r  r   rp  r   r  r   r   )r   r  r  s      r   r  r  Z   s     ""11&9==?ID~~gll+==?IIKLL-dmm.>.>-?{4==K]K]KgKgJhhlmumymym{l|}~W&EF,--'+.  r   c           	         t        t        |      }| j                  dk(  rXt        | j                  |      }|j                         r=|j                          t        j                  | d       t        d      S t        |      }t        j                  d|j                   d|j                  j                   dt        j                                  t#        | d	d
|i      S )Nr  r   r  zSlob ID updated successfullyr  zEdited Slob ID: r	  r
  zedit_shop_slob.htmlr  )r   rQ   r   r2   r   r  r  r   r  r   r   r   r<  r  r  r   rp  r   )r   r   slobr  s       r   edit_shop_slobr  p   s     ZB/D~~gllT:==?IIKW&DE,--t,LL#DGG9K		8K8K7LDQYQ]Q]Q_P`ab'064.AAr   c           	      "   t        t        |      }|j                          t        j                  d|j
                   d|j                  j                   dt        j                                 t        j                  | d       t        d      S )Nr  zDeleting Slob ID: r	  r
  zSlob ID deleted successfullyr  )r   rQ   r  r   r   r<  r  r  r   rp  r   r  r   )r   r   r  s      r   delete_shop_slobr     sr     ZB/D 	KKMLL%dggYk$)):M:M9NdS[S_S_SaRbcdW<=$%%r   c                 r    t        ||      }| j                  j                  d      }|j                  |      S )Nru  )r    rx  r   ry  )r   r  per_pager}  r  s        r   paginate_querysetr     s1    (H-I++//&)Kk**r   c                     t         j                  j                  d      j                  d      j	                  ddd      j                  d      }t        | |      }t        | d	d
|i      S )Nrf  rg  r  r  r   r  pu_uuidr  zlong_distance_orders.htmlr  rj   r  r  r   r  rS  r  r   r   orders_listr  s      r   long_distance_orders_viewr     sj    ..'') ( g;g'{LR[(\]e]efu]v  w4F'668JKKr   c                     t         j                  j                  d      j                  d      j	                  ddd      j                  d      }t        | |      }t        | d	d
|i      S )NrI  rg  r  r  r   r  r  r  zcustom_orderss.htmlr  r  r  s      r   custom_orders_viewr     sj    ..''" ( g;g'{LR[(\]e]efu]v  w4F'08V2DEEr   c                     t         j                  j                  ddg      j                  d      j	                  ddd      j                  d	      }t        | |      }t        | d
d|i      S )Nr`  r  )r  r  r  r   r  r  r  zlocal_pickup_orders.htmlr  r  r  s      r   local_pickup_orders_viewr     so    ..''&	2 ( g;g'{LR[(\]e]efu]v  w4F'5&7IJJr   )   r   )F)Nr  )r   )rt  (s  decimalr   rT  django.shortcutsr   r   django.contrib.authr   r   r   r   django.contrib.auth.decoratorsr	   r
   rest_framework.decoratorsr   django.views.decorators.httpr   django.forms.modelsr   django.db.modelsr   r   r   django.core.validatorsr   django.core.exceptionsr   r   razorpayhmachashlibjsontwiliodjango.views.decorators.csrfr   r   r   r   signals.custom_signalsr   requestsrj  	itertoolsr   r   django.confr   django.httpr   r   django.contribr   django.utilsdjango.core.paginatorr    r!   r"   django.appsr#   
cms.modelsr$   r%   r&   r'   r(   r)   r*   formsr+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   shops.modelsrN   rO   rP   rQ   products.modelsrR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   accounts.modelsorders.modelsrb   rc   rd   re   rf   rg   rh   ri   rj   rk   rl   rm   rn   ro   rp   rq   rr   rs   rt   ru   rv   rw   django.core.mailrx   NavyaBackers.settingsry   r   r   rz   accounts.viewsr{   django.urlsr|   django.contrib.auth.modelsr}   r~   r   r   django.template.loaderr   dalr   rest_framework.permissionsr   r  rest_framework.viewsr   rest_framework.responser   rest_frameworkr   r   django.utils.dateparser   r   osceleryr   adminportal.tasksr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   orders.delhiveryr   r   r   r   r   r   r   r   r   r  r	  r  r$  r\  rd  rj  rq  r~  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r
  r  r  r%  r(  r.  r1  rE  rb  rh  rm  rt  ry  r{  r~  r  r  r  r  r  r  r  r  r  r  r  r6  r8  r;  rV  rY  r`  re  rh  rk  rm  rq  rt  rw  ry  r}  r  r  r  r  r  r  r  r  r  dateutilr  r  r  Select2QuerySetViewr  r2  r<  rh  rV  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r
  r  r  r  r  r  r  r  r"  r%  r(  r*  r2  r@  rC  rG  rK  r^  ri  rf  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r   r   <module>rQ     s     - I I K . 5 5 4 4 1 2       4 - - :   .   2 # ! H H  g g g{ { { { { { { { { {
 G F     q q q q q q & 1   -   , 1 1 3   6 
   &  ( , !  - ) 	  ,X X X X X  \ \A,F0&%6 66k\M`Sr /"XYZ2 [ 2, /=+">?@6 A 6*8 /=+">?@d5 A d5P /=+">?@! A  !8 /=+">?@% A  %.J& /=+">?@-6 A -6j /=+">?@2 A 2" /=+">?@7 A 7$ /=+">?@F5 A F5T /=+">?@ A   /=+">?@=5 A =5B + +
 /=+">?@%7 A %7` /=+">?@: A :$ /=+">?@&9 A &9T /=+">?@'9 A '9V /=+">?@>9 A >9D /=+">?@> A >> /=+">?@<8 A <8~ /=+">?@UR A URp /=+">?@A A A, /=+">?@'K A 'KT /=+">?@N A N:6Rt /=+">?@'R A 'RT%iR /=+">?@T< A T<n B B, B B,+\ $ % /=+">?@G+ A G+V %
ZPDW~, /=+">?@9 A 9 /=+">?@@9 A @9H /"LMN~> O ~>N
 /"LMNA? O A?H /=+">?@@ A @F%?P /<.12T 3 Tn /=+">?@H AHV4h /=+">?@. A .& /=+">?@5 A5* /=+">?@
  A  
  /=+">?@5 A 5< /=+">?@4 A 4D /=+">?@@ A@& /=+">?@# A  #  /=+">?@@ A @4 /=+">?@"? A "?J /=+">?@9 A9* /=+">?@$ A  $  /=+">?@!> A !>H /=+">?@#= A #=TDC DCR g  F /<"<=>> ?>D /<.12) 3  ) /<.12> 3 >4 /<.12(= 3 (=v "  /<"<=>D; ? D;X /:,/0A 1 A0
,:: 
"@&P},@ /
vqr C C2 AH AHF!qH /=+">?@8 A 8, /=+">?@$ A $6 /=+">?@j8 A j8Z /=+">?@i7 A i7X /"JKL? M?* /=+">?@* A  *" /=+">?@? A ?< /=+">?@!> A !>F /=+">?@: A :& /=+">?@& A &( /=+">?@j: A j:Z /=+">?@N9 A N9b /=+">?@. A .( /=+">?@4 A 4. /=+">?@ A   /=+">?@F4 A F4R /=+">?@J3 A J3` /=+">?@6? A 6?r /=+">?@@ A @8 /=+">?@M A MF /=+">?@4@ A 4@n I I:  I  IF A A6 /=+">?@&@ A &@R /=+">?@, A  ," /=+">?@A A A2 /=+">?@7 A 7" /=+">?@; A ;, /=+">?@H A H0 /=+">?@ A " /=+">?@D A DN /=+">?@ A , /=+">?@ A . /=+">?@5 A 5 /<"<=>x> ? x>v  !C@+4". /<.12[Q 3 [Q| /=+">?@3 A 3^&LRK*\? 
$Ri@r 
6({L {L| 
6(.H .Hf /=+">?@B AB2 /=+">?@) A  ) /=+">?@B A B> /=+">?@'A A 'AV0>hB@ $H)() /=+">?@ A   /=+">?@' A ' /=+">?@' A ' /=+">?@ A ( /=+">?@B A B& /=+">?@& A &+LFKr   