
    Td3                        d dl Z d dlmZ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T d dlZd dlZ e            Ze                    d           ed          fd	ed
efd            Ze                    d          d	efd            Ze                    d          d	efd            Ze                    d           ed          fd	ed
efd            ZdS )    N)	APIRouterRequestBodystatus)DictList)jsonable_encoder)JSONResponse)*z
/create_po.request
po_detailsc                   K   t          |          }| j        j                                        }|                    d          D ]M}g |}t          |d                   dk    rt          |d                   }5t          |d                   dz   }Nd                    t          j                                        d          t          j                                        d          |          |d<   t          j        |d         d	          j	        |d<   t          j        |d
         d	          j	        |d
<   t          j
                                        d          j	        |d<   |                    d|d         |d         |d         |d
         |d         |d         d|d         d 
  
         | j        j                                         |d         D ]X}|                    d||d         |d         |d         |d         |d         |d         |d         d|d         |d                    Y| j        j                                         |                                 t          d|i          S )Nzbselect count(*) as cnt,(select IDENT_CURRENT('PurchaseOrderList')) as idnt  from PurchaseOrderListr      zAVPL/PSD/{}/{}/{}YYYYMM	po_ref_nopo_datez
DD/MM/YYYYdelivery_datezAsia/Calcutta)tzinfoLast_updatez@insert into PurchaseOrderList values (?, ?, ?, ?, ?, ?, ?, ?, ?)	vendor_idpurchase_eventprepared_byproduct_detailszFinsert into PurchaseOrderItem values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
product_idhsn_codeqtyuom
base_pricecgstsgst	nick_namepo_no)r	   appmssqlcursorexecuteintformatarrowgetdatetimenowreplacecommitcloser
   )r   r   r&   itr	row_countprds         (D:\Sites\api_v1\routes\purchase_order.py	create_por5      su     !*--J[%%''F ~~  C  D  D ( (fs1v;;!CFIICFaII 2889K9KF9S9SUZU^U`U`UgUghlUmUmoxyyJ{!Ij&;\JJSJy"')J,G"V"V"_J %	 3 3? 3 K K TJ} NNUk*k*i(o../m,m,
 
 
 K +,  _ <(:5z5z<(6{6{!-0;'	 	 	 	 K
LLNNN        z /get_po/{type}/{details}/{po_id}c           	      	  K   | j         j                                        }|                                dk    ri }|                    d|          }|j        dk    r|D ]rt                    D ]\  }}||j        |         d         <   t          |           g |d<   |dk    r|                    d|          }|j        dk    ro|D ]LrHi }	t                    D ]\  }}||	j        |         d         <   |d         	                    |	           Mt          |           t          |          c S  dS t          |          c S ndS |                                dk    r|                    d	|          }g }
|j        dk    re|D ]Di }t                    D ]\  }}||j        |         d         <   |
	                    |           Et          |
           g }|dk    rg d
}|                    d|
d         d                   }|j        dk    rt          |          D ]{\  }g 	                    t          j                            |
fd          d         d                    |	                    t          t          |                               |t          |           |
d         d         |
d         d         |
d         d         |
d         d         |d}|S ndS |                                dk    r|                    d	|          }g }|j        dk    r|D ]Di }t                    D ]\  }}||j        |         d         <   |	                    |           Et          |           g }|dk    r9g d
}|                    d|d         d                   }|j        dk    rt          |          D ]\  }g t!          t          j                            |fd                    dk    rr	                    t          j                            |fd          d         d                    |	                    t          t          |                               t          |           |d         d         |d         d         |d         d         |d         d         |d}|S d S dS d S )NPOz=select * from PurchaseOrderList where PurchaseOrderListId = ?r   r   1a  select  distinct POL.PurchaseOrderListId , POI.PurchaseOrderItemId , POI.ProductID , POI.PdtNickName,  PAT.Name AS ProductName, POI.HSN_Code ,POI.Quantity, POI.UOM, POI.BasePrice,  SP.Name AS UOM_Name, POI.CGST, POI.SGST,  V.VendorId, V.VendorName, V.VendorAddress1,V.VendorAddress2,V.City,V.State,V.Country, V.CountryCode, V.MobileNo , V.landlineNo, V.GSTIN, V.Remarks from PurchaseOrderItem as POI JOIN  PurchaseOrderList AS POL(NOLOCK) ON POI.PurchaseOrderListId = POL.PurchaseOrderListId JOIN Product AS PP (NOLOCK) ON PP.ProductId = POI.ProductId  LEFT JOIN Vaaak.Vendor AS V (NOLOCK) ON POL.VendorId = V.VendorId  LEFT JOIN Specification AS SP (NOLOCK) ON POI.UOM = SP.SpecificationId  JOIN ProductTranslation AS PT  (NOLOCK)  ON PT.ProductId = POI.ProductId AND PT.LocaleId = 1 JOIN Vaaak.ProductAdditionalTranslation AS PAT  (NOLOCK)  ON PT.ProductAdditionalTransId = PAT.ProductAdditionalTransId  LEFT JOIN vaaak.GrnItem AS GI (NOLOCK)  ON POI.PurchaseOrderItemId=GI.PurchaseOrderItemId  where POI.PurchaseOrderListId = ?zRequested PO not foundGRNzselect GRI.GrnId,GRI.PurchaseOrderItemId,GRL.PurchaseOrderListId,GRI.ReceivedQty,GRI.InvoiceNumber,GRL.LastUpdate from Vaaak.GrnItem as GRI JOIN Vaaak.GrnList as GRL on GRL.GrnId = GRI.GrnId  where GRI.GrnId = ?)item_idr   product_namer"   r   order_quantityr   r   uom_namer    r!   VendorId
VendorNameVendorAddress1VendorAddress2CityStateCountryCountrycodemobilelandlinegstremarksreceived_qtyaL  select  distinct POI.PurchaseOrderItemId , POI.ProductID , PAT.Name AS ProductName, POI.PdtNickName, POI.HSN_Code ,POI.Quantity, POI.UOM, POI.BasePrice, SP.Name AS UOM_Name, POI.CGST, POI.SGST, V.VendorId, V.VendorName, V.VendorAddress1,V.VendorAddress2,V.City,V.State,V.Country, V.CountryCode, V.MobileNo , V.landlineNo, V.GSTIN, V.Remarks from PurchaseOrderItem as POI JOIN PurchaseOrderList as POL ON POL.PurchaseOrderListId = POI.PurchaseOrderListId JOIN Product AS PP  ON PP.ProductId = POI.ProductId  JOIN Vaaak.Vendor AS V  ON POL.VendorId = V.VendorId JOIN Specification AS SP ON SP.SpecificationId = POI.UOM JOIN ProductTranslation AS PT  ON PT.ProductId = POI.ProductId AND PT.LocaleId = 1 JOIN Vaaak.ProductAdditionalTranslation AS PAT  ON PT.ProductAdditionalTransId = PAT.ProductAdditionalTransId  where POI.PurchaseOrderListId = ?PurchaseOrderListIdc                 (    | d         d         k    S NPurchaseOrderItemIdr    xrows    r4   <lambda>zget_po.<locals>.<lambda>   s    ANcLdhklmhnLn r6   ReceivedQtyGrnIdInvoiceNumber
LastUpdate)grn_nor#   
invoice_nogrn_dater   zGRN not foundRNaK  select  distinct POI.PurchaseOrderItemId , POI.ProductID , PAT.Name AS ProductName,POI.PdtNickName, POI.HSN_Code ,POI.Quantity, POI.UOM, POI.BasePrice, SP.Name AS UOM_Name, POI.CGST, POI.SGST, V.VendorId, V.VendorName, V.VendorAddress1,V.VendorAddress2,V.City,V.State,V.Country, V.CountryCode, V.MobileNo , V.landlineNo, V.GSTIN, V.Remarks from PurchaseOrderItem as POI JOIN PurchaseOrderList as POL ON POL.PurchaseOrderListId = POI.PurchaseOrderListId JOIN Product AS PP  ON PP.ProductId = POI.ProductId  JOIN Vaaak.Vendor AS V  ON POL.VendorId = V.VendorId JOIN Specification AS SP ON SP.SpecificationId = POI.UOM JOIN ProductTranslation AS PT  ON PT.ProductId = POI.ProductId AND PT.LocaleId = 1 JOIN Vaaak.ProductAdditionalTranslation AS PAT  ON PT.ProductAdditionalTransId = PAT.ProductAdditionalTransId  where POI.PurchaseOrderListId = ?c                 (    | d         d         k    S rN   rP   rQ   s    r4   rT   zget_po.<locals>.<lambda>   s    aPeNfjmnojpNp r6   c                 (    | d         d         k    S rN   rP   rQ   s    r4   rT   zget_po.<locals>.<lambda>   s    qQfOgknopkqOq r6   )rn_nor#   rZ   rn_dater   zRN not found)r$   r%   r&   upperr'   rowcount	enumeratecursor_descriptionjson_serializerappendr
   pydashcollectionsfilter_dictziplen)r   typedetailspo_idr&   r   rowsr1   itr_itmproduct	grn_items	temp_dictr   columnsgrn_detailsrn_items
rn_detailsrS   s                    @r4   get_pory   J   s     [%%''Fzz||t
~~]_dee=A '8 '8 &8(1# M MWEL
3#9##>q#ABB
 $J///46J01#~~%~~[ " #  #  =A--'+ R R#& !R.0G8A# %Z %ZWRY0Fs0KA0N(O(O$./@$A$H$H$Q$Q$Q+J777#/
#;#;;;;#;#;#;+J77777M&8'8R ,+zz||u~~ 45:< < 	=A , ,	$-cNN H HLC@GIc4S9!<==  ++++I&&& O#~~W W W~~8 :C1F[9\	^ 	^ =A%%$-dOO H HS$f

".66yBnBnBnBnoopqr -/0 0 0 (..tC4E4E/F/FGGGG000'l73&q\*?@"+A,"? )!\ :'6  #"= @ #? zz||t~~ 45:< < =A + +	$-cNN H HLC@GIc4S9!<==	****H%%% O#~~W W W~~	8 :B!EZ9[
] 
] =A%%$-dOO L LS$fv199(DpDpDpDpqqrruvvvJJ & 2 : :8EqEqEqEq r rst u$1!34 4 4 ,224GS8I8I3J3JKKK  000%a[1%a[)>?"*1+o">'{<8'6 
 "!E ~H ">i r6   z/get_valid_poc                    K   | j         j                                        }g }|                    d          D ]}|                    |d                    |S )NzDselect PurchaseOrderListId from PurchaseOrderList where POStatus = 1r   )r$   r%   r&   r'   rf   )r   r&   r#   r1   s       r4   get_valid_por{      s[      [%%''FE~~dee  SVLr6   z
/cancel_poc                 j  K   t          |          }| j        j                                        }|                    d|d                   }|j        dk    r\|                    d|d         |d                    | j        j                                         d                    |d                   S dS )Nz9select * from Vaaak.GrnList where PurchaseOrderListId = ?r#   r   zUupdate PurchaseOrderList set POStatus = -1, remarks = ? where PurchaseOrderListId = ?rJ   zPO - {} Cancelledz#GRN/RN already created for this PO.)r	   r$   r%   r&   r'   rb   r/   r)   )r   r   r&   rp   s       r4   	cancel_por}      s      !*--J[%%''F>>UV`ahVijjD}noy  {D  pE  GQ  RY  GZ  	[  	[  	[  """"))*W*=>>>44r6   )r*   fastapir   r   r   r   typingr   r   fastapi.encodersr	   fastapi.responsesr
   
lib.commonjsonrg   routerpostr5   r+   ry   r{   r}   rP   r6   r4   <module>r      s    4 4 4 4 4 4 4 4 4 4 4 4         - - - - - - * * * * * *      	 \9=c 9 9W 9$ 9 9 9 9x .//Y"' Y" Y" Y" 0/Y"z O     \9=c 5 5W 5$ 5 5 5 5 5 5r6   