
    <d                        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 e            Ze                    d           ed          fd	ed
efd            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dS )    N)	APIRouterRequestBodystatus)DictListjsonable_encoder)JSONResponse)*z/create_grn.requestgrn_detailsc                 (   K   t          |          }d S )Nr	   )r   r   s     D:\api_v1\routes\grn.py
create_grnr      s      ";//KKK    z
/create_rn
rn_detailsc           	      F  K   t          |          }| j        j                                        }t	          j                                        d          j        }|                    d          D ]I}t          |d                   dk    rt          |d                   }1t          |d                   dz   }J|                    d|d         |           | j        j        
                                 |d         D ].}|                    d	||d
         |d         |d         |           /| j        j        
                                 |d         D ]%}|                    d|d         |d
                    &| j        j        
                                 |                    d|d                   }i }|D ]}|d         ||d         <   |                    d|d                   }g }	|D ]&}|	                    |d         |d         d           't          j        t          j                            |	d           d           }	|	                                D ].\  }
}||
         |k    r|                    d|
|d                    /|
                                 |                    d|d                   }|j        dk    r0|                    d|d                    |
                                 d                    |d                                         |          S )NzAsia/Calcutta)tzinfoz-SELECT IDENT_CURRENT('Vaaak.GrnList') as cnt;r      z'insert into Vaaak.GrnList values (?, ?)po_noitemsz0insert into Vaaak.GrnItem values (?, ?, ?, ?, ?)purchase_order_itemidreceived_qty
invoice_nozupdate inventory set Quantity = Quantity + ?  where ProductId = (select ProductId from PurchaseOrderItem where PurchaseOrderItemId = ?)z=select * from PurchaseOrderItem where PurchaseOrderListId = ?   zjselect * from Vaaak.GrnItem where GrnId in (select GrnId from Vaaak.GrnList where PurchaseOrderListId = ?)      )item_idqtyc                     | d         S )Nr    xs    r   <lambda>zcreate_rn.<locals>.<lambda>H   s    STU^S_ r   c                 .    t          j        | d           S )Nc                     | d         S )Nr    r"   )ys    r   r%   z-create_rn.<locals>.<lambda>.<locals>.<lambda>I   s
    1U8 r   )pydashsum_byr#   s    r   r%   zcreate_rn.<locals>.<lambda>I   s    a1C1C D D r   zgupdate PurchaseOrderItem set POItemStatus = 0 where PurchaseOrderItemId = ? and PurchaseOrderListId = ?zSselect * from PurchaseOrderItem where PurchaseOrderListId = ? and POItemStatus = 1;zGupdate PurchaseOrderList set POStatus = 0 where PurchaseOrderListId = ?z{}-{} created!type)r
   appmssqlcursorarrownowreplacedatetimeexecuteintcommitappendr)   
map_valuescollectionsgroup_byr   rowcountformatupper)r   r   r.   Last_updateitr	row_countrowspo_itemsrow	grn_itemskvs               r   	create_rnrF      s     !*--J[%%''F)++%%_%==FK ~~MNN ( (s1v;;!CFIICFaII
NN<g&  
 K '"  I 23>*<("	 	 	 	 K '" T T  a  be  ft  bu  wz  {R  wS  	T  	T  	T  	TK >>Y[efm[nooDH " "q6Q>>  G  IS  T[  I\  ]  ]DI  q61v
 
 	 	 	 	
 !&"4"="=iI_I_"`"`DDF FI    ^ ^!A;!NN  E  GH  JT  U\  J]  ^  ^  ^
MMOOO >>oq{  }D  rE  F  FD}`blmtbuvvv"":f#5#;#;#=#=yIIIr   z/grn_details1/type/po_noc           	        K   | j         j                                        }g }i }|                    d|          }|D ]}|d         ||d         <   |                    d|          }g d}|D ]}g |}|d         |v rW|d         ||d                  z
  |d<   |d         dk    r0|                    t          t          ||                               g|                    t          t          ||                               |S )Nzselect GRI.PurchaseOrderItemId, sum(GRI.ReceivedQty) as Received from Vaaak.GrnItem AS GRI where GrnId in (select GRL.GrnId from Vaaak.GrnList AS GRL where GRL.PurchaseOrderListId = ?) group by PurchaseOrderItemIdr   r   a   select  distinct POL.PurchaseOrderListId , POI.PurchaseOrderItemId , POI.ProductID , PAT.Name AS ProductName, POI.HSN_Code ,POI.Quantity, POI.UOM, POI.BasePrice, SP.Name AS UOM_Name, POI.CGST, POI.SGST, V.VendorName + ' ' + V.VendorAddress + ' ' + V.CountryCode + ' ' + V.MobileNo AS VendorDetails, V.VendorId 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 SpecificationType AS SP (NOLOCK) ON POI.UOM = SP.SpecificationTypeId  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 = ?PurchaseOrderListIdPurchaseOrderItemId	ProductIDProductNameHSN_CodeQuantityUOM	BasePriceUOM_NameCGSTSGSTVendorDetailsVendorId   )r,   r-   r.   r3   r6   dictzip)	r   r+   r   r.   rA   received_itemsr@   rB   columnss	            r   grn_details1r[   Z   s?     [%%''FHN>> 9:?A AD  ( (!$Qs1v>>  p  qv  w  wDU U UG 4 4fq6^##VnSV44CF1v{{S%5%5 6 6777OODWS!1!1223333Or   z/grn_details/type/po_noc           	      B  K   | j         j                                        }|                    d||          }g d}g }|j        dk    rF|D ]2}|                    t          t          ||                               3t          |          }|S |dk    rdS |dk    rdS d S )Nz"{call Vaaak.spGetGrnDetails(?, ?)}rH   r   rnzRN not foundgrnzGRN not found)	r,   r-   r.   r3   r:   r6   rW   rX   json_serializer)r   r+   r   r.   r@   rZ   r   rB   s           r   r   r   u   s      [%%''F>>>eLLD t  t  tGK} 	7 	7CtC$4$4556666%k224<<!>5=="? =r   )r/   fastapir   r   r   r   typingr   r   fastapi.encodersr
   fastapi.responsesr   
lib.commonr)   routerpostr   rF   getr[   r   r"   r   r   <module>rh      s    4 4 4 4 4 4 4 4 4 4 4 4         - - - - - - * * * * * *     	];?499 0 0g 0D 0 0 0 0 \9=c GJ GJW GJ$ GJ GJ GJ GJR &''    ('4 %&&#w # # # '&# # #r   